{"thread":{"id":"17708","subject":"[PATCH] lis3lv02d: support both one- and two-byte sensors","startedAt":"2009-02-10T23:34:22Z","lastAt":"2009-02-10T23:34:22Z","messageCount":1,"participants":["Giuseppe Bilotta"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"104112","messageId":"1234308862-13609-1-git-send-email-giuseppe.bilotta@gmail.com","threadId":"17708","inReplyTo":null,"subject":"[PATCH] lis3lv02d: support both one- and two-byte sensors","fromName":"Giuseppe Bilotta","fromEmail":"giuseppe.bilotta@gmail.com","sentAt":"2009-02-10T23:34:22Z","receivedAt":"2009-02-10T23:34:22Z","isPatch":true,"sender":{"key":"giuseppe.bilotta@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1464?v=4"},"body":"Sensors responding with 0x3B to WHO_AM_I only have one data register per\ndirection, thus returning a signed byte from the position which is\noccupied by the MSB in sensors responding with 0x3A.\n\nWe support both kind of sensors by checking for the sensor type on init\nand defining appropriate data-access routines and sensor limits (for the\njoystick) depending on what we find.\n\nSigned-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>\n---\n\nTo make the patch smaller, I left a funky effect on the 8-bit sensor\nreading routine: it actually accesses the byte AFTER the one requested,\nso I didn't have to touch all the requests.\n\nAnother solution would be to let it access the requested bytes, change\nall requests to the subsequent byte, and have the 16-bit routine access\nthe requested byte and the one BEFORE it. I can remake the patch in this\nsense if reviewers judge it to be formally more correct.\n\n drivers/hwmon/lis3lv02d.c |   41 +++++++++++++++++++++++++++++++----------\n drivers/hwmon/lis3lv02d.h |    9 ++++++++-\n 2 files changed, 39 insertions(+), 11 deletions(-)\n\ndiff --git a/drivers/hwmon/lis3lv02d.c b/drivers/hwmon/lis3lv02d.c\nindex 219d2d0..28a1cae 100644\n--- a/drivers/hwmon/lis3lv02d.c\n+++ b/drivers/hwmon/lis3lv02d.c\n@@ -52,9 +52,6 @@\n  * joystick.\n  */\n \n-/* Maximum value our axis may get for the input device (signed 12 bits) */\n-#define MDPS_MAX_VAL 2048\n-\n struct acpi_lis3lv02d adev;\n EXPORT_SYMBOL_GPL(adev);\n \n@@ -70,6 +67,16 @@ static s16 lis3lv02d_read_16(acpi_handle handle, int reg)\n \treturn (s16)((hi << 8) | lo);\n }\n \n+static s16 lis3lv02d_read_8(acpi_handle handle, int reg)\n+{\n+\tu8 lo;\n+\t/* 8-bit sensors store their data in the byte which holds\n+\t   the MSB for 16-bit sensors, so we actually check the\n+\t   NEXT position */\n+\tadev.read(handle, reg + 1, &lo);\n+\treturn *((s8*)(&lo));\n+}\n+\n /**\n  * lis3lv02d_get_axis - For the given axis, give the value converted\n  * @axis:      1,2,3 - can also be negative\n@@ -98,9 +105,9 @@ static void lis3lv02d_get_xyz(acpi_handle handle, int *x, int *y, int *z)\n {\n \tint position[3];\n \n-\tposition[0] = lis3lv02d_read_16(handle, OUTX_L);\n-\tposition[1] = lis3lv02d_read_16(handle, OUTY_L);\n-\tposition[2] = lis3lv02d_read_16(handle, OUTZ_L);\n+\tposition[0] = adev.lis3lv02d_read(handle, OUTX_L);\n+\tposition[1] = adev.lis3lv02d_read(handle, OUTY_L);\n+\tposition[2] = adev.lis3lv02d_read(handle, OUTZ_L);\n \n \t*x = lis3lv02d_get_axis(adev.ac.x, position);\n \t*y = lis3lv02d_get_axis(adev.ac.y, position);\n@@ -121,7 +128,21 @@ void lis3lv02d_poweron(acpi_handle handle)\n \n \tadev.is_on = 1;\n \tadev.init(handle);\n-\tadev.write(handle, FF_WU_CFG, 0);\n+\tadev.read(handle, WHO_AM_I, &adev.whoami);\n+\t/* Tell apart LISxLV02Dy from LISx02Dy family by checking the LSB\n+\t * (0x3A vs 0x3B). TODO More sophisticated checks on other registers\n+\t * could be implemented, for example to see if we have 2 or 3 axes, and\n+\t * configure the joystick accordingly.\n+\t */\n+\tif (adev.whoami & 1) {\n+\t\tprintk(KERN_INFO DRIVER_NAME \": 1-byte sensor found\\n\");\n+\t\tadev.lis3lv02d_read = lis3lv02d_read_8;\n+\t\tadev.mdps_max_val = 128;\n+\t} else {\n+\t\tprintk(KERN_INFO DRIVER_NAME \": 2-byte sensor found\\n\");\n+\t\tadev.lis3lv02d_read = lis3lv02d_read_16;\n+\t\tadev.mdps_max_val = 2048;\n+\t}\n \t/*\n \t * BDU: LSB and MSB values are not updated until both have been read.\n \t *      So the value read will always be correct.\n@@ -231,9 +252,9 @@ int lis3lv02d_joystick_enable(void)\n \tadev.idev->close      = lis3lv02d_joystick_close;\n \n \tset_bit(EV_ABS, adev.idev->evbit);\n-\tinput_set_abs_params(adev.idev, ABS_X, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);\n-\tinput_set_abs_params(adev.idev, ABS_Y, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);\n-\tinput_set_abs_params(adev.idev, ABS_Z, -MDPS_MAX_VAL, MDPS_MAX_VAL, 3, 3);\n+\tinput_set_abs_params(adev.idev, ABS_X, -adev.mdps_max_val, adev.mdps_max_val, 3, 3);\n+\tinput_set_abs_params(adev.idev, ABS_Y, -adev.mdps_max_val, adev.mdps_max_val, 3, 3);\n+\tinput_set_abs_params(adev.idev, ABS_Z, -adev.mdps_max_val, adev.mdps_max_val, 3, 3);\n \n \terr = input_register_device(adev.idev);\n \tif (err) {\ndiff --git a/drivers/hwmon/lis3lv02d.h b/drivers/hwmon/lis3lv02d.h\nindex 223f1c0..cf61f0f 100644\n--- a/drivers/hwmon/lis3lv02d.h\n+++ b/drivers/hwmon/lis3lv02d.h\n@@ -22,11 +22,14 @@\n /*\n  * The actual chip is STMicroelectronics LIS3LV02DL or LIS3LV02DQ that seems to\n  * be connected via SPI. There exists also several similar chips (such as LIS302DL or\n- * LIS3L02DQ) but not in the HP laptops and they have slightly different registers.\n+ * LIS3L02DQ) and they have slightly different registers, but we can provide a\n+ * common interface for all of them.\n  * They can also be connected via IÂ²C.\n  */\n \n+/* 2-byte registers */\n #define LIS3LV02DL_ID\t0x3A /* Also the LIS3LV02DQ */\n+/* 1-byte registers */\n #define LIS302DL_ID\t0x3B /* Also the LIS202DL! */\n \n enum lis3lv02d_reg {\n@@ -159,6 +162,10 @@ struct acpi_lis3lv02d {\n \tacpi_status (*write) (acpi_handle handle, int reg, u8 val);\n \tacpi_status (*read) (acpi_handle handle, int reg, u8 *ret);\n \n+\tu8\t\t\twhoami;    /* 3Ah: 2-byte registries, 3Bh: 1-byte registries */\n+\ts16 (*lis3lv02d_read) (acpi_handle handle, int reg);\n+\tint\t\t\tmdps_max_val;\n+\n \tstruct input_dev\t*idev;     /* input device */\n \tstruct task_struct\t*kthread;  /* kthread for input */\n \tstruct mutex            lock;\n-- \n1.6.2.rc0.173.g5e148\n"}]}