1 From 5e2b5ddaf95985744e0e0c28ba6dac83c093fd15 Mon Sep 17 00:00:00 2001
2 From: Simon Kagstrom <simon.kagstrom@gmail.com>
3 Date: Thu, 16 Oct 2008 01:19:29 +0100
4 Subject: [PATCH] : lis302dl-threshold-configuration-in-mg.patch
6 Change the threshold configuration to be set in mg instead
8 From: Simon Kagstrom <simon.kagstrom@gmail.com>
10 This patch changes the threshold sysfs file to handle values in mg
11 instead of the device-values. Overrun values have also been corrected
12 and the sampler function now honors the FS bit.
14 Signed-off-by: Simon Kagstrom <simon.kagstrom@gmail.com>
16 drivers/input/misc/lis302dl.c | 49 ++++++++++++++++++++++++++++++++--------
17 1 files changed, 39 insertions(+), 10 deletions(-)
19 diff --git a/drivers/input/misc/lis302dl.c b/drivers/input/misc/lis302dl.c
20 index 300918d..c1b1d67 100644
21 --- a/drivers/input/misc/lis302dl.c
22 +++ b/drivers/input/misc/lis302dl.c
23 @@ -97,6 +97,24 @@ static int __duration_to_ms(struct lis302dl_info *lis, int duration)
27 +static u8 __mg_to_threshold(struct lis302dl_info *lis, int mg)
29 + /* If FS is set each bit is 71mg, otherwise 18mg. The THS register
30 + * has 7 bits for the threshold value */
31 + if (lis->flags & LIS302DL_F_FS)
32 + return min(mg / 71, 127);
34 + return min(mg / 18, 127);
37 +static int __threshold_to_mg(struct lis302dl_info *lis, u8 threshold)
39 + if (lis->flags & LIS302DL_F_FS)
40 + return threshold * 71;
42 + return threshold * 18;
45 /* interrupt handling related */
47 enum lis302dl_intmode {
48 @@ -146,23 +164,24 @@ static void _report_btn_double(struct input_dev *inp, int btn)
52 -#define MG_PER_SAMPLE 18
54 static void lis302dl_bitbang_read_sample(struct lis302dl_info *lis)
56 u8 data = 0xc0 | LIS302DL_REG_OUT_X; /* read, autoincrement */
61 local_irq_save(flags);
62 + mg_per_sample = __threshold_to_mg(lis, 1);
64 (lis->pdata->lis302dl_bitbang)(lis, &data, 1, &read[0], 5);
66 local_irq_restore(flags);
68 - input_report_rel(lis->input_dev, REL_X, MG_PER_SAMPLE * (s8)read[0]);
69 - input_report_rel(lis->input_dev, REL_Y, MG_PER_SAMPLE * (s8)read[2]);
70 - input_report_rel(lis->input_dev, REL_Z, MG_PER_SAMPLE * (s8)read[4]);
71 + input_report_rel(lis->input_dev, REL_X, mg_per_sample * (s8)read[0]);
72 + input_report_rel(lis->input_dev, REL_Y, mg_per_sample * (s8)read[2]);
73 + input_report_rel(lis->input_dev, REL_Z, mg_per_sample * (s8)read[4]);
75 input_sync(lis->input_dev);
77 @@ -234,15 +253,24 @@ static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
79 struct lis302dl_info *lis = dev_get_drvdata(dev);
81 + int threshold_mg = __threshold_to_mg(lis, lis->threshold);
83 local_irq_save(flags);
85 - if (!strcmp(buf, "9.2\n"))
86 + if (!strcmp(buf, "9.2\n")) {
87 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
90 + lis->flags |= LIS302DL_F_FS;
92 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_FS,
95 + lis->flags &= ~LIS302DL_F_FS;
98 + /* Adjust the threshold */
99 + lis->threshold = __mg_to_threshold(lis, threshold_mg);
100 + if (lis->flags & LIS302DL_F_INPUT_OPEN)
101 + __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, lis->threshold);
103 local_irq_restore(flags);
105 @@ -256,7 +284,7 @@ static ssize_t show_threshold(struct device *dev, struct device_attribute *attr,
107 struct lis302dl_info *lis = dev_get_drvdata(dev);
109 - return sprintf(buf, "%d\n", lis->threshold);
110 + return sprintf(buf, "%d\n", __threshold_to_mg(lis, lis->threshold));
113 static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
114 @@ -267,11 +295,12 @@ static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
116 if (sscanf(buf, "%d\n", &val) != 1)
118 - if (val < 0 || val > 255)
119 + /* 8g is the maximum if FS is 1 */
120 + if (val < 0 || val > 8000)
123 /* Set the threshold and write it out if the device is used */
124 - lis->threshold = val;
125 + lis->threshold = __mg_to_threshold(lis, val);
126 if (lis->flags & LIS302DL_F_INPUT_OPEN)
127 __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, lis->threshold);