1 From d3c59382c7e8429e2c86eb92cc2518dce26c4a69 Mon Sep 17 00:00:00 2001
2 From: Simon Kagstrom <simon.kagstrom@gmail.com>
3 Date: Thu, 16 Oct 2008 01:19:39 +0100
4 Subject: [PATCH] : lis302dl-configure-duration.patch
6 Allow configuration of the duration for thresholds
8 From: Simon Kagstrom <simon.kagstrom@gmail.com>
10 This patch allows the configuration of duration used when generating
11 filtered data (see the lis302dl application note). Also set a flag when
12 the DR bit in the ctrl1 register is set to change the data rate and
13 simplify the ms_to_duration utility functions.
15 The sysfs 'duration' file is used for this.
17 Signed-off-by: Simon Kagstrom <simon.kagstrom@gmail.com>
19 drivers/input/misc/lis302dl.c | 63 ++++++++++++++++++++++++++++-------------
20 include/linux/lis302dl.h | 2 +
21 2 files changed, 45 insertions(+), 20 deletions(-)
23 diff --git a/drivers/input/misc/lis302dl.c b/drivers/input/misc/lis302dl.c
24 index c1b1d67..d738199 100644
25 --- a/drivers/input/misc/lis302dl.c
26 +++ b/drivers/input/misc/lis302dl.c
27 @@ -69,29 +69,17 @@ static void __reg_set_bit_mask(struct lis302dl_info *lis, u8 reg, u8 mask,
29 static int __ms_to_duration(struct lis302dl_info *lis, int ms)
31 - u8 r = __reg_read(lis, LIS302DL_REG_CTRL1);
33 /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
34 * on 100 ms the stepping is 10ms */
35 - if (r & LIS302DL_CTRL1_DR) {
40 - return (ms * 10) / 25;
42 + if (lis->flags & LIS302DL_F_DR)
43 + return min((ms * 10) / 25, 637);
45 - /* Too large value */
49 + return min(ms / 10, 2550);
52 static int __duration_to_ms(struct lis302dl_info *lis, int duration)
54 - u8 r = __reg_read(lis, LIS302DL_REG_CTRL1);
56 - if (r & LIS302DL_CTRL1_DR)
57 + if (lis->flags & LIS302DL_F_DR)
58 return (duration * 25) / 10;
61 @@ -218,15 +206,20 @@ static ssize_t set_rate(struct device *dev, struct device_attribute *attr,
63 struct lis302dl_info *lis = dev_get_drvdata(dev);
65 + int duration_ms = __duration_to_ms(lis, lis->duration);
67 local_irq_save(flags);
69 - if (!strcmp(buf, "400\n"))
70 + if (!strcmp(buf, "400\n")) {
71 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
74 + lis->flags |= LIS302DL_F_DR;
76 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_DR,
79 + lis->flags &= ~LIS302DL_F_DR;
81 + lis->duration = __ms_to_duration(lis, duration_ms);
82 local_irq_restore(flags);
85 @@ -309,6 +302,34 @@ static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
87 static DEVICE_ATTR(threshold, S_IRUGO | S_IWUSR, show_threshold, set_threshold);
89 +static ssize_t show_duration(struct device *dev, struct device_attribute *attr,
92 + struct lis302dl_info *lis = dev_get_drvdata(dev);
94 + return sprintf(buf, "%d\n", __duration_to_ms(lis, lis->duration));
97 +static ssize_t set_duration(struct device *dev, struct device_attribute *attr,
98 + const char *buf, size_t count)
100 + struct lis302dl_info *lis = dev_get_drvdata(dev);
103 + if (sscanf(buf, "%d\n", &val) != 1)
105 + if (val < 0 || val > 2550)
108 + lis->duration = __ms_to_duration(lis, val);
109 + if (lis->flags & LIS302DL_F_INPUT_OPEN)
110 + __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, lis->duration);
115 +static DEVICE_ATTR(duration, S_IRUGO | S_IWUSR, show_duration, set_duration);
117 static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
120 @@ -528,6 +549,7 @@ static struct attribute *lis302dl_sysfs_entries[] = {
121 &dev_attr_sample_rate.attr,
122 &dev_attr_full_scale.attr,
123 &dev_attr_threshold.attr,
124 + &dev_attr_duration.attr,
126 &dev_attr_freefall_wakeup_1.attr,
127 &dev_attr_freefall_wakeup_2.attr,
128 @@ -556,7 +578,7 @@ static int lis302dl_input_open(struct input_dev *inp)
129 __reg_write(lis, LIS302DL_REG_CTRL2,
131 __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, lis->threshold);
132 - __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, 0);
133 + __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, lis->duration);
135 /* Clear the HP filter "starting point" */
136 __reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
137 @@ -670,6 +692,7 @@ static int __devinit lis302dl_probe(struct platform_device *pdev)
138 set_bit(BTN_Z, lis->input_dev->keybit);
143 lis->input_dev->private = lis;
144 lis->input_dev->name = pdata->name;
145 diff --git a/include/linux/lis302dl.h b/include/linux/lis302dl.h
146 index a756f55..f7aa956 100644
147 --- a/include/linux/lis302dl.h
148 +++ b/include/linux/lis302dl.h
149 @@ -30,6 +30,7 @@ struct lis302dl_info {
150 struct input_dev *input_dev;
152 unsigned int threshold;
153 + unsigned int duration;
157 @@ -142,6 +143,7 @@ enum lis302dl_reg_cloik_src {
158 #define LIS302DL_F_FS 0x0020 /* ADC full scale */
159 #define LIS302DL_F_INPUT_OPEN 0x0040 /* Set if input device is opened */
160 #define LIS302DL_F_IRQ_WAKE 0x0080 /* IRQ is setup in wake mode */
161 +#define LIS302DL_F_DR 0x0100 /* Data rate, 400Hz/100Hz */
164 #endif /* _LINUX_LIS302DL_H */