{"thread":{"id":"17710","subject":"[PATCHv3] lis3lv02d: support both one- and two-byte sensors","startedAt":"2009-02-10T23:46:15Z","lastAt":"2009-02-10T23:46:15Z","messageCount":1,"participants":["Giuseppe Bilotta"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"104116","messageId":"1234309575-13760-1-git-send-email-giuseppe.bilotta@gmail.com","threadId":"17710","inReplyTo":null,"subject":"[PATCHv3] lis3lv02d: support both one- and two-byte sensors","fromName":"Giuseppe Bilotta","fromEmail":"giuseppe.bilotta@gmail.com","sentAt":"2009-02-10T23:46:15Z","receivedAt":"2009-02-10T23:46:15Z","isPatch":false,"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\nThis version of the patch should apply without conflicts to -mm's tree,\nas it's based on Pavel's \"don't touch stuff on init\" patch.\n\n drivers/hwmon/lis3lv02d.c |   42 ++++++++++++++++++++++++++++++------------\n drivers/hwmon/lis3lv02d.h |   12 +++++++++++-\n 2 files changed, 41 insertions(+), 13 deletions(-)\n\ndiff --git a/drivers/hwmon/lis3lv02d.c b/drivers/hwmon/lis3lv02d.c\nindex 3afa3af..408d57b 100644\n--- a/drivers/hwmon/lis3lv02d.c\n+++ b/drivers/hwmon/lis3lv02d.c\n@@ -53,13 +53,9 @@\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 \t.misc_wait   = __WAIT_QUEUE_HEAD_INITIALIZER(adev.misc_wait),\n };\n-\n EXPORT_SYMBOL_GPL(adev);\n \n static int lis3lv02d_add_fs(struct acpi_device *device);\n@@ -68,12 +64,19 @@ static s16 lis3lv02d_read_16(acpi_handle handle, int reg)\n {\n \tu8 lo, hi;\n \n-\tadev.read(handle, reg, &lo);\n-\tadev.read(handle, reg + 1, &hi);\n+\tadev.read(handle, reg - 1, &lo);\n+\tadev.read(handle, reg, &hi);\n \t/* In \"12 bit right justified\" mode, bit 6, bit 7, bit 8 = bit 5 */\n \treturn (s16)((hi << 8) | lo);\n }\n \n+static s16 lis3lv02d_read_8(acpi_handle handle, int reg)\n+{\n+\tu8 lo;\n+\tadev.read(handle, reg, &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@@ -102,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);\n+\tposition[1] = adev.lis3lv02d_read(handle, OUTY);\n+\tposition[2] = adev.lis3lv02d_read(handle, OUTZ);\n \n \t*x = lis3lv02d_get_axis(adev.ac.x, position);\n \t*y = lis3lv02d_get_axis(adev.ac.y, position);\n@@ -121,6 +124,21 @@ void lis3lv02d_poweron(acpi_handle handle)\n {\n \tadev.is_on = 1;\n \tadev.init(handle);\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 }\n EXPORT_SYMBOL_GPL(lis3lv02d_poweron);\n \n@@ -355,9 +373,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 2e7597c..cd0e838 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@@ -44,10 +47,13 @@ enum lis3lv02d_reg {\n \tSTATUS_REG\t= 0x27,\n \tOUTX_L\t\t= 0x28,\n \tOUTX_H\t\t= 0x29,\n+\tOUTX\t\t= 0x29,\n \tOUTY_L\t\t= 0x2A,\n \tOUTY_H\t\t= 0x2B,\n+\tOUTY\t\t= 0x2B,\n \tOUTZ_L\t\t= 0x2C,\n \tOUTZ_H\t\t= 0x2D,\n+\tOUTZ\t\t= 0x2D,\n \tFF_WU_CFG\t= 0x30,\n \tFF_WU_SRC\t= 0x31,\n \tFF_WU_ACK\t= 0x32,\n@@ -159,6 +165,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"}]}