1 From 35d910f40ce2c7c959edfbaa671f88e8da5fa6b1 Mon Sep 17 00:00:00 2001
2 From: Simon Kagstrom <simon.kagstrom@gmail.com>
3 Date: Mon, 13 Oct 2008 12:47:12 +0100
4 Subject: [PATCH] : Acceleration threshold configuration: lis302dl-configure-threshold-on-bitbang.patch
6 Configure threshold for accelerometer input
8 From: Simon Kagstrom <simon.kagstrom@gmail.com>
10 This patch, based on bitbang-all-the-way from the andy branch,
11 implements a configurable acceleration threshold that need to be
12 exceeded for the accelerometer to generate input data. The threshold can
15 cd /sys/devices/.../lis302dl.1/
18 A value of 10 (180 mg, from the lis302dl application note) seems
19 empirically good to keep the accelerometers quiet while riding a (calm)
23 The patch moves the sample reading for the accelerometers from
24 mach-gta02.c to lis302dl.c and instead exports the generic
25 lis302dl_bitbang function. This is to easier support both "raw" data
26 reading and reading with a threshold in the future (not yet implemented).
28 Signed-off-by: Simon Kagstrom <simon.kagstrom@gmail.com>
30 arch/arm/mach-s3c2440/mach-gta02.c | 35 ++-------------
31 drivers/input/misc/lis302dl.c | 83 ++++++++++++++++++++++++++++++++++--
32 include/linux/lis302dl.h | 3 +
33 3 files changed, 87 insertions(+), 34 deletions(-)
35 diff --git a/arch/arm/mach-s3c2440/mach-gta02.c b/arch/arm/mach-s3c2440/mach-gta02.c
36 index b19632c..1a9d823 100644
37 --- a/arch/arm/mach-s3c2440/mach-gta02.c
38 +++ b/arch/arm/mach-s3c2440/mach-gta02.c
39 @@ -1050,7 +1050,6 @@ static struct platform_device gta02_vibrator_dev = {
42 /* #define DEBUG_SPEW_MS */
43 -#define MG_PER_SAMPLE 18
45 struct lis302dl_platform_data lis302_pdata_top;
46 struct lis302dl_platform_data lis302_pdata_bottom;
47 @@ -1060,7 +1059,7 @@ struct lis302dl_platform_data lis302_pdata_bottom;
48 * only call with interrupts off!
51 -static void __gta02_lis302dl_bitbang(struct lis302dl_info *lis, u8 *tx,
52 +static void gta02_lis302dl_bitbang(struct lis302dl_info *lis, u8 *tx,
53 int tx_bytes, u8 *rx, int rx_bytes)
55 struct lis302dl_platform_data *pdata = lis->pdata;
56 @@ -1124,7 +1123,7 @@ static int gta02_lis302dl_bitbang_read_reg(struct lis302dl_info *lis, u8 reg)
58 local_irq_save(flags);
60 - __gta02_lis302dl_bitbang(lis, &data, 1, &data, 1);
61 + gta02_lis302dl_bitbang(lis, &data, 1, &data, 1);
63 local_irq_restore(flags);
65 @@ -1139,37 +1138,13 @@ static void gta02_lis302dl_bitbang_write_reg(struct lis302dl_info *lis, u8 reg,
67 local_irq_save(flags);
69 - __gta02_lis302dl_bitbang(lis, &data[0], 2, NULL, 0);
70 + gta02_lis302dl_bitbang(lis, &data[0], 2, NULL, 0);
72 local_irq_restore(flags);
77 -static void gta02_lis302dl_bitbang_sample(struct lis302dl_info *lis)
79 - u8 data = 0xc0 | LIS302DL_REG_OUT_X; /* read, autoincrement */
81 - unsigned long flags;
83 - local_irq_save(flags);
85 - __gta02_lis302dl_bitbang(lis, &data, 1, &read[0], 5);
87 - local_irq_restore(flags);
89 - input_report_rel(lis->input_dev, REL_X, MG_PER_SAMPLE * (s8)read[0]);
90 - input_report_rel(lis->input_dev, REL_Y, MG_PER_SAMPLE * (s8)read[2]);
91 - input_report_rel(lis->input_dev, REL_Z, MG_PER_SAMPLE * (s8)read[4]);
93 - input_sync(lis->input_dev);
95 - printk(KERN_INFO "%s: %d %d %d\n", pdata->name, read[0], read[2],
101 void gta02_lis302dl_suspend_io(struct lis302dl_info *lis, int resume)
103 struct lis302dl_platform_data *pdata = lis->pdata;
104 @@ -1210,7 +1185,7 @@ struct lis302dl_platform_data lis302_pdata_top = {
105 .pin_miso = S3C2410_GPG5,
106 .interrupt = GTA02_IRQ_GSENSOR_1,
107 .open_drain = 1, /* altered at runtime by PCB rev */
108 - .lis302dl_bitbang_read_sample = gta02_lis302dl_bitbang_sample,
109 + .lis302dl_bitbang = gta02_lis302dl_bitbang,
110 .lis302dl_bitbang_reg_read = gta02_lis302dl_bitbang_read_reg,
111 .lis302dl_bitbang_reg_write = gta02_lis302dl_bitbang_write_reg,
112 .lis302dl_suspend_io = gta02_lis302dl_suspend_io,
113 @@ -1224,7 +1199,7 @@ struct lis302dl_platform_data lis302_pdata_bottom = {
114 .pin_miso = S3C2410_GPG5,
115 .interrupt = GTA02_IRQ_GSENSOR_2,
116 .open_drain = 1, /* altered at runtime by PCB rev */
117 - .lis302dl_bitbang_read_sample = gta02_lis302dl_bitbang_sample,
118 + .lis302dl_bitbang = gta02_lis302dl_bitbang,
119 .lis302dl_bitbang_reg_read = gta02_lis302dl_bitbang_read_reg,
120 .lis302dl_bitbang_reg_write = gta02_lis302dl_bitbang_write_reg,
121 .lis302dl_suspend_io = gta02_lis302dl_suspend_io,
122 diff --git a/drivers/input/misc/lis302dl.c b/drivers/input/misc/lis302dl.c
123 index 1d5781d..1013f4a 100644
124 --- a/drivers/input/misc/lis302dl.c
125 +++ b/drivers/input/misc/lis302dl.c
127 * Author: Harald Welte <laforge@openmoko.org>
128 * converted to private bitbang by:
129 * Andy Green <andy@openmoko.com>
130 + * ability to set acceleration threshold added by:
131 + * Simon Kagstrom <simon.kagstrom@gmail.com>
132 * All rights reserved.
134 * This program is free software; you can redistribute it and/or
135 @@ -67,6 +69,31 @@ enum lis302dl_intmode {
136 LIS302DL_INTMODE_CLICK = 0x07,
139 +#define MG_PER_SAMPLE 18
141 +static void lis302dl_bitbang_read_sample(struct lis302dl_info *lis)
143 + u8 data = 0xc0 | LIS302DL_REG_OUT_X; /* read, autoincrement */
145 + unsigned long flags;
147 + local_irq_save(flags);
149 + (lis->pdata->lis302dl_bitbang)(lis, &data, 1, &read[0], 5);
151 + local_irq_restore(flags);
153 + input_report_rel(lis->input_dev, REL_X, MG_PER_SAMPLE * (s8)read[0]);
154 + input_report_rel(lis->input_dev, REL_Y, MG_PER_SAMPLE * (s8)read[2]);
155 + input_report_rel(lis->input_dev, REL_Z, MG_PER_SAMPLE * (s8)read[4]);
157 + input_sync(lis->input_dev);
159 + /* Reset the HP filter */
160 + (lis->pdata->lis302dl_bitbang_reg_read)(lis,
161 + LIS302DL_REG_HP_FILTER_RESET);
164 static void __lis302dl_int_mode(struct device *dev, int int_pin,
165 enum lis302dl_intmode mode)
167 @@ -108,7 +135,7 @@ static irqreturn_t lis302dl_interrupt(int irq, void *_lis)
169 struct lis302dl_info *lis = _lis;
171 - (lis->pdata->lis302dl_bitbang_read_sample)(lis);
172 + lis302dl_bitbang_read_sample(lis);
176 @@ -187,6 +214,36 @@ static ssize_t set_scale(struct device *dev, struct device_attribute *attr,
178 static DEVICE_ATTR(full_scale, S_IRUGO | S_IWUSR, show_scale, set_scale);
180 +static ssize_t show_threshold(struct device *dev, struct device_attribute *attr,
183 + struct lis302dl_info *lis = dev_get_drvdata(dev);
185 + return sprintf(buf, "%d\n", lis->threshold);
188 +static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
189 + const char *buf, size_t count)
191 + struct lis302dl_info *lis = dev_get_drvdata(dev);
194 + if (sscanf(buf, "%d\n", &val) != 1)
196 + if (val < 0 || val > 255)
199 + /* Set the threshold and write it out if the device is used */
200 + lis->threshold = val;
201 + if (lis->flags & LIS302DL_F_INPUT_OPEN)
202 + (lis->pdata->lis302dl_bitbang_reg_write)(lis,
203 + LIS302DL_REG_FF_WU_THS_1, lis->threshold);
208 +static DEVICE_ATTR(threshold, S_IRUGO | S_IWUSR, show_threshold, set_threshold);
210 static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
213 @@ -436,6 +493,7 @@ static DEVICE_ATTR(freefall_wakeup_2, S_IRUGO | S_IWUSR, show_freefall_2,
214 static struct attribute *lis302dl_sysfs_entries[] = {
215 &dev_attr_sample_rate.attr,
216 &dev_attr_full_scale.attr,
217 + &dev_attr_threshold.attr,
219 &dev_attr_freefall_wakeup_1.attr,
220 &dev_attr_freefall_wakeup_2.attr,
221 @@ -454,17 +512,32 @@ static int lis302dl_input_open(struct input_dev *inp)
222 struct lis302dl_info *lis = inp->private;
223 u_int8_t ctrl1 = LIS302DL_CTRL1_PD | LIS302DL_CTRL1_Xen |
224 LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen;
225 + u_int8_t ctrl2 = LIS302DL_CTRL2_HPFF1;
228 local_irq_save(flags);
229 /* make sure we're powered up and generate data ready */
230 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, ctrl1);
232 + (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL2,
234 + (lis->pdata->lis302dl_bitbang_reg_write)(lis,
235 + LIS302DL_REG_FF_WU_THS_1, lis->threshold);
236 + (lis->pdata->lis302dl_bitbang_reg_write)(lis,
237 + LIS302DL_REG_FF_WU_DURATION_1, 0);
239 + /* Clear the HP filter "starting point" */
240 + (lis->pdata->lis302dl_bitbang_reg_read)(lis,
241 + LIS302DL_REG_HP_FILTER_RESET);
242 + (lis->pdata->lis302dl_bitbang_reg_write)(lis,
243 + LIS302DL_REG_FF_WU_CFG_1, LIS302DL_FFWUCFG_XHIE |
244 + LIS302DL_FFWUCFG_YHIE | LIS302DL_FFWUCFG_ZHIE);
246 lis->flags |= LIS302DL_F_INPUT_OPEN;
248 /* kick it off -- since we are edge triggered, if we missed the edge
249 * permanent low interrupt is death for us */
250 - (lis->pdata->lis302dl_bitbang_read_sample)(lis);
251 + lis302dl_bitbang_read_sample(lis);
253 local_irq_restore(flags);
255 @@ -567,6 +640,8 @@ static int __devinit lis302dl_probe(struct platform_device *pdev)
256 set_bit(BTN_Y, lis->input_dev->keybit);
257 set_bit(BTN_Z, lis->input_dev->keybit);
259 + lis->threshold = 1;
261 lis->input_dev->private = lis;
262 lis->input_dev->name = pdata->name;
263 /* SPI Bus not defined as a valid bus for input subsystem*/
264 @@ -617,8 +692,8 @@ static int __devinit lis302dl_probe(struct platform_device *pdev)
265 (lis->pdata->lis302dl_bitbang_reg_write)(lis,
266 LIS302DL_REG_CTRL3, LIS302DL_CTRL3_IHL);
268 - __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_DATA_READY);
269 - __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_DATA_READY);
270 + __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_FF_WU_12);
271 + __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_FF_WU_12);
273 (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_STATUS);
274 (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_FF_WU_SRC_1);
275 diff --git a/include/linux/lis302dl.h b/include/linux/lis302dl.h
276 index 4578db4..f3f994d 100644
277 --- a/include/linux/lis302dl.h
278 +++ b/include/linux/lis302dl.h
279 @@ -16,6 +16,8 @@ struct lis302dl_platform_data {
280 unsigned long pin_miso;
283 + void (*lis302dl_bitbang)(struct lis302dl_info *lis, u8 *tx,
284 + int tx_bytes, u8 *rx, int rx_bytes);
285 void (*lis302dl_bitbang_read_sample)(struct lis302dl_info *);
286 void (*lis302dl_suspend_io)(struct lis302dl_info *, int resuming);
287 int (*lis302dl_bitbang_reg_read)(struct lis302dl_info *, u8 reg);
288 @@ -28,6 +30,7 @@ struct lis302dl_info {
290 struct input_dev *input_dev;
292 + unsigned int threshold;