From 80e108e8230ff1055aea30dfe9835f2b2e51e8f5 Mon Sep 17 00:00:00 2001 From: Alex Gonzalez Date: Tue, 21 Feb 2012 17:24:47 +0100 Subject: mma7455: Fix sign extension for 10bits read. When reading a 10bits number, the 2s complement MSB is the sign. This bit cannot be just shifted to bit 16, the number has to be sign extended. 0abcdefghij => 00000abcdefghij 1abcdefghij => 11111abcdefghij Signed-off-by: Alex Gonzalez --- drivers/input/misc/mma7455l.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/input/misc/mma7455l.c b/drivers/input/misc/mma7455l.c index e01ecb7847c6..f8e80f1f6728 100644 --- a/drivers/input/misc/mma7455l.c +++ b/drivers/input/misc/mma7455l.c @@ -218,11 +218,16 @@ static u_int8_t reg_read(struct mma7455l_info *mma, u_int8_t reg) static s16 __reg_read_10(struct mma7455l_info *mma, u8 reg1, u8 reg2) { u8 v1, v2; + s16 ret; v1 = __reg_read(mma, reg1); v2 = __reg_read(mma, reg2); - return (v2 & 0x4) << 13 | (v2 & 0x3) << 8 | v1; + // ret is 11bits, 3 MSB from v2 and 8 LSB from v1. + ret = (((v2 & 0x7) << 8 | v1) & 0x7FF) << 5; + // Sign extension. Being ret signed, this will fill the MSBs with + // either 0s o 1s depending on whether bit10 is 0 or 1. + return (ret >>= 5); } static int reg_write(struct mma7455l_info *mma, u_int8_t reg, u_int8_t val) -- cgit v1.2.3