1 From 3318a7cb3165a33046e122294e1878e3bd6555d8 Mon Sep 17 00:00:00 2001
2 From: Simon Kagstrom <simon.kagstrom@gmail.com>
3 Date: Thu, 16 Oct 2008 01:19:20 +0100
4 Subject: [PATCH] : lis302dl-refactor-and-cleanup.patch
8 From: Simon Kagstrom <simon.kagstrom@gmail.com>
10 Re-added __reg_read and __reg_write and re-indent the code according to
11 these. Move some input-related functions to get a more "logical" order,
12 e.g., in the future we might want to support clicks again and call these
13 from read_sample. Removed unused function pointer in the structure
15 Signed-off-by: Simon Kagstrom <simon.kagstrom@gmail.com>
17 drivers/input/misc/lis302dl.c | 260 ++++++++++++++++++++---------------------
18 include/linux/lis302dl.h | 1 -
19 2 files changed, 125 insertions(+), 136 deletions(-)
21 diff --git a/drivers/input/misc/lis302dl.c b/drivers/input/misc/lis302dl.c
22 index 1013f4a..300918d 100644
23 --- a/drivers/input/misc/lis302dl.c
24 +++ b/drivers/input/misc/lis302dl.c
27 #include <linux/lis302dl.h>
29 +/* Utility functions */
30 +static u8 __reg_read(struct lis302dl_info *lis, u8 reg)
32 + return (lis->pdata->lis302dl_bitbang_reg_read)(lis, reg);
35 +static void __reg_write(struct lis302dl_info *lis, u8 reg, u8 val)
37 + (lis->pdata->lis302dl_bitbang_reg_write)(lis, reg, val);
40 static void __reg_set_bit_mask(struct lis302dl_info *lis, u8 reg, u8 mask,
48 - tmp = (lis->pdata->lis302dl_bitbang_reg_read)(lis, reg);
49 + tmp = __reg_read(lis, reg);
52 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, reg, tmp);
53 + __reg_write(lis, reg, tmp);
56 +static int __ms_to_duration(struct lis302dl_info *lis, int ms)
58 + u8 r = __reg_read(lis, LIS302DL_REG_CTRL1);
60 + /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
61 + * on 100 ms the stepping is 10ms */
62 + if (r & LIS302DL_CTRL1_DR) {
67 + return (ms * 10) / 25;
70 + /* Too large value */
76 +static int __duration_to_ms(struct lis302dl_info *lis, int duration)
78 + u8 r = __reg_read(lis, LIS302DL_REG_CTRL1);
80 + if (r & LIS302DL_CTRL1_DR)
81 + return (duration * 25) / 10;
83 + return duration * 10;
86 /* interrupt handling related */
87 @@ -69,30 +108,6 @@ enum lis302dl_intmode {
88 LIS302DL_INTMODE_CLICK = 0x07,
91 -#define MG_PER_SAMPLE 18
93 -static void lis302dl_bitbang_read_sample(struct lis302dl_info *lis)
95 - u8 data = 0xc0 | LIS302DL_REG_OUT_X; /* read, autoincrement */
97 - unsigned long flags;
99 - local_irq_save(flags);
101 - (lis->pdata->lis302dl_bitbang)(lis, &data, 1, &read[0], 5);
103 - local_irq_restore(flags);
105 - input_report_rel(lis->input_dev, REL_X, MG_PER_SAMPLE * (s8)read[0]);
106 - input_report_rel(lis->input_dev, REL_Y, MG_PER_SAMPLE * (s8)read[2]);
107 - input_report_rel(lis->input_dev, REL_Z, MG_PER_SAMPLE * (s8)read[4]);
109 - input_sync(lis->input_dev);
111 - /* Reset the HP filter */
112 - (lis->pdata->lis302dl_bitbang_reg_read)(lis,
113 - LIS302DL_REG_HP_FILTER_RESET);
116 static void __lis302dl_int_mode(struct device *dev, int int_pin,
117 enum lis302dl_intmode mode)
118 @@ -110,6 +125,7 @@ static void __lis302dl_int_mode(struct device *dev, int int_pin,
124 static void _report_btn_single(struct input_dev *inp, int btn)
126 @@ -130,6 +146,29 @@ static void _report_btn_double(struct input_dev *inp, int btn)
130 +#define MG_PER_SAMPLE 18
132 +static void lis302dl_bitbang_read_sample(struct lis302dl_info *lis)
134 + u8 data = 0xc0 | LIS302DL_REG_OUT_X; /* read, autoincrement */
136 + unsigned long flags;
138 + local_irq_save(flags);
140 + (lis->pdata->lis302dl_bitbang)(lis, &data, 1, &read[0], 5);
142 + local_irq_restore(flags);
144 + input_report_rel(lis->input_dev, REL_X, MG_PER_SAMPLE * (s8)read[0]);
145 + input_report_rel(lis->input_dev, REL_Y, MG_PER_SAMPLE * (s8)read[2]);
146 + input_report_rel(lis->input_dev, REL_Z, MG_PER_SAMPLE * (s8)read[4]);
148 + input_sync(lis->input_dev);
150 + /* Reset the HP filter */
151 + __reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
154 static irqreturn_t lis302dl_interrupt(int irq, void *_lis)
156 @@ -149,8 +188,7 @@ static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
159 local_irq_save(flags);
160 - ctrl1 = (lis->pdata->lis302dl_bitbang_reg_read)
161 - (lis, LIS302DL_REG_CTRL1);
162 + ctrl1 = __reg_read(lis, LIS302DL_REG_CTRL1);
163 local_irq_restore(flags);
165 return sprintf(buf, "%d\n", ctrl1 & LIS302DL_CTRL1_DR ? 400 : 100);
166 @@ -185,8 +223,7 @@ static ssize_t show_scale(struct device *dev, struct device_attribute *attr,
169 local_irq_save(flags);
170 - ctrl1 = (lis->pdata->lis302dl_bitbang_reg_read)(lis,
171 - LIS302DL_REG_CTRL1);
172 + ctrl1 = __reg_read(lis, LIS302DL_REG_CTRL1);
173 local_irq_restore(flags);
175 return sprintf(buf, "%s\n", ctrl1 & LIS302DL_CTRL1_FS ? "9.2" : "2.3");
176 @@ -236,8 +273,7 @@ static ssize_t set_threshold(struct device *dev, struct device_attribute *attr,
177 /* Set the threshold and write it out if the device is used */
178 lis->threshold = val;
179 if (lis->flags & LIS302DL_F_INPUT_OPEN)
180 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
181 - LIS302DL_REG_FF_WU_THS_1, lis->threshold);
182 + __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, lis->threshold);
186 @@ -256,7 +292,7 @@ static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
187 local_irq_save(flags);
189 for (n = 0; n < sizeof(reg); n++)
190 - reg[n] = (lis->pdata->lis302dl_bitbang_reg_read)(lis, n);
191 + reg[n] = __reg_read(lis, n);
193 local_irq_restore(flags);
195 @@ -271,36 +307,6 @@ static ssize_t lis302dl_dump(struct device *dev, struct device_attribute *attr,
197 static DEVICE_ATTR(dump, S_IRUGO, lis302dl_dump, NULL);
199 -static int __freefall_ms_to_duration(struct lis302dl_info *lis, int ms)
201 - u8 r = (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_CTRL1);
203 - /* If we have 400 ms sampling rate, the stepping is 2.5 ms,
204 - * on 100 ms the stepping is 10ms */
205 - if (r & LIS302DL_CTRL1_DR) {
210 - return (ms * 10) / 25;
213 - /* Too large value */
219 -static int __freefall_duration_to_ms(struct lis302dl_info *lis, int duration)
221 - u8 r = (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_CTRL1);
223 - if (r & LIS302DL_CTRL1_DR)
224 - return (duration * 25) / 10;
226 - return duration * 10;
229 /* Configure freefall/wakeup interrupts */
230 static ssize_t set_freefall_common(int which, struct device *dev,
231 struct device_attribute *attr, const char *buf, size_t count)
232 @@ -342,9 +348,9 @@ static ssize_t set_freefall_common(int which, struct device *dev,
233 LIS302DL_INTMODE_DATA_READY);
234 lis->flags &= ~(flag_mask | LIS302DL_F_IRQ_WAKE);
236 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, r_cfg, 0);
237 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, r_ths, 0);
238 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, r_duration, 0);
239 + __reg_write(lis, r_cfg, 0);
240 + __reg_write(lis, r_ths, 0);
241 + __reg_write(lis, r_duration, 0);
243 /* Power off unless the input subsystem is using the device */
244 if (!(lis->flags & LIS302DL_F_INPUT_OPEN))
245 @@ -361,7 +367,7 @@ static ssize_t set_freefall_common(int which, struct device *dev,
248 local_irq_save(flags);
249 - duration = __freefall_ms_to_duration(lis, ms);
250 + duration = __ms_to_duration(lis, ms);
251 local_irq_save(flags);
254 @@ -382,13 +388,12 @@ static ssize_t set_freefall_common(int which, struct device *dev,
255 /* Setup the configuration registers */
256 local_irq_save(flags);
257 /* First zero to get to a known state */
258 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, r_cfg, 0);
259 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, r_cfg,
260 + __reg_write(lis, r_cfg, 0);
261 + __reg_write(lis, r_cfg,
262 (and_events ? LIS302DL_FFWUCFG_AOI : 0) |
263 x_lo | x_hi | y_lo | y_hi | z_lo | z_hi);
264 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, r_ths,
265 - threshold & ~LIS302DL_FFWUTHS_DCRM);
266 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, r_duration, duration);
267 + __reg_write(lis, r_ths, threshold & ~LIS302DL_FFWUTHS_DCRM);
268 + __reg_write(lis, r_duration, duration);
270 /* Route the interrupt for wakeup */
271 __lis302dl_int_mode(lis->dev, which, intmode);
272 @@ -443,12 +448,12 @@ static ssize_t show_freefall_common(int which, struct device *dev,
275 local_irq_save(flags);
276 - config = (lis->pdata->lis302dl_bitbang_reg_read)(lis, r_cfg);
277 - threshold = (lis->pdata->lis302dl_bitbang_reg_read)(lis, r_ths);
278 - duration = (lis->pdata->lis302dl_bitbang_reg_read)(lis, r_duration);
279 - r4 = (lis->pdata->lis302dl_bitbang_reg_read)(lis, r_src);
280 - r5 = (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_CTRL3);
281 - ms = __freefall_duration_to_ms(lis, duration);
282 + config = __reg_read(lis, r_cfg);
283 + threshold = __reg_read(lis, r_ths);
284 + duration = __reg_read(lis, r_duration);
285 + r4 = __reg_read(lis, r_src);
286 + r5 = __reg_read(lis, LIS302DL_REG_CTRL3);
287 + ms = __duration_to_ms(lis, duration);
288 local_irq_restore(flags);
290 /* All events off? */
291 @@ -519,18 +524,14 @@ static int lis302dl_input_open(struct input_dev *inp)
292 /* make sure we're powered up and generate data ready */
293 __reg_set_bit_mask(lis, LIS302DL_REG_CTRL1, ctrl1, ctrl1);
295 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL2,
296 + __reg_write(lis, LIS302DL_REG_CTRL2,
298 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
299 - LIS302DL_REG_FF_WU_THS_1, lis->threshold);
300 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
301 - LIS302DL_REG_FF_WU_DURATION_1, 0);
302 + __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, lis->threshold);
303 + __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, 0);
305 /* Clear the HP filter "starting point" */
306 - (lis->pdata->lis302dl_bitbang_reg_read)(lis,
307 - LIS302DL_REG_HP_FILTER_RESET);
308 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
309 - LIS302DL_REG_FF_WU_CFG_1, LIS302DL_FFWUCFG_XHIE |
310 + __reg_read(lis, LIS302DL_REG_HP_FILTER_RESET);
311 + __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, LIS302DL_FFWUCFG_XHIE |
312 LIS302DL_FFWUCFG_YHIE | LIS302DL_FFWUCFG_ZHIE);
314 lis->flags |= LIS302DL_F_INPUT_OPEN;
315 @@ -576,11 +577,11 @@ static int __lis302dl_reset_device(struct lis302dl_info *lis)
319 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL2,
320 - LIS302DL_CTRL2_BOOT | LIS302DL_CTRL2_FDS);
321 + __reg_write(lis, LIS302DL_REG_CTRL2,
322 + LIS302DL_CTRL2_BOOT | LIS302DL_CTRL2_FDS);
324 - while (((lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_CTRL2)
325 - & LIS302DL_CTRL2_BOOT) && (timeout--))
326 + while ((__reg_read(lis, LIS302DL_REG_CTRL2)
327 + & LIS302DL_CTRL2_BOOT) && (timeout--))
330 return !!(timeout < 0);
331 @@ -609,8 +610,7 @@ static int __devinit lis302dl_probe(struct platform_device *pdev)
332 /* Configure our IO */
333 (lis->pdata->lis302dl_suspend_io)(lis, 1);
335 - wai = (lis->pdata->lis302dl_bitbang_reg_read)(lis,
336 - LIS302DL_REG_WHO_AM_I);
337 + wai = __reg_read(lis, LIS302DL_REG_WHO_AM_I);
338 if (wai != LIS302DL_WHO_AM_I_MAGIC) {
339 dev_err(lis->dev, "unknown who_am_i signature 0x%02x\n", wai);
340 dev_set_drvdata(lis->dev, NULL);
341 @@ -659,46 +659,38 @@ static int __devinit lis302dl_probe(struct platform_device *pdev)
342 dev_err(lis->dev, "device BOOT reload failed\n");
344 /* force us powered */
345 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL1,
346 - LIS302DL_CTRL1_PD |
347 - LIS302DL_CTRL1_Xen |
348 - LIS302DL_CTRL1_Yen |
349 - LIS302DL_CTRL1_Zen);
350 + __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_PD |
351 + LIS302DL_CTRL1_Xen |
352 + LIS302DL_CTRL1_Yen |
353 + LIS302DL_CTRL1_Zen);
356 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL2, 0);
357 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL3,
358 - LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
359 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
360 - LIS302DL_REG_FF_WU_THS_1, 0x0);
361 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
362 - LIS302DL_REG_FF_WU_DURATION_1, 0x00);
363 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
364 - LIS302DL_REG_FF_WU_CFG_1, 0x0);
365 + __reg_write(lis, LIS302DL_REG_CTRL2, 0);
366 + __reg_write(lis, LIS302DL_REG_CTRL3,
367 + LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
368 + __reg_write(lis, LIS302DL_REG_FF_WU_THS_1, 0x0);
369 + __reg_write(lis, LIS302DL_REG_FF_WU_DURATION_1, 0x00);
370 + __reg_write(lis, LIS302DL_REG_FF_WU_CFG_1, 0x0);
372 /* start off in powered down mode; we power up when someone opens us */
373 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL1,
374 - LIS302DL_CTRL1_Xen |
375 - LIS302DL_CTRL1_Yen |
376 - LIS302DL_CTRL1_Zen);
377 + __reg_write(lis, LIS302DL_REG_CTRL1, LIS302DL_CTRL1_Xen |
378 + LIS302DL_CTRL1_Yen | LIS302DL_CTRL1_Zen);
380 if (pdata->open_drain)
381 /* switch interrupt to open collector, active-low */
382 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
383 - LIS302DL_REG_CTRL3, LIS302DL_CTRL3_PP_OD |
384 - LIS302DL_CTRL3_IHL);
385 + __reg_write(lis, LIS302DL_REG_CTRL3,
386 + LIS302DL_CTRL3_PP_OD | LIS302DL_CTRL3_IHL);
388 /* push-pull, active-low */
389 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
390 - LIS302DL_REG_CTRL3, LIS302DL_CTRL3_IHL);
391 + __reg_write(lis, LIS302DL_REG_CTRL3, LIS302DL_CTRL3_IHL);
393 __lis302dl_int_mode(lis->dev, 1, LIS302DL_INTMODE_FF_WU_12);
394 __lis302dl_int_mode(lis->dev, 2, LIS302DL_INTMODE_FF_WU_12);
396 - (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_STATUS);
397 - (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_FF_WU_SRC_1);
398 - (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_FF_WU_SRC_2);
399 - (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_CLICK_SRC);
400 + __reg_read(lis, LIS302DL_REG_STATUS);
401 + __reg_read(lis, LIS302DL_REG_FF_WU_SRC_1);
402 + __reg_read(lis, LIS302DL_REG_FF_WU_SRC_2);
403 + __reg_read(lis, LIS302DL_REG_CLICK_SRC);
405 dev_info(lis->dev, "Found %s\n", pdata->name);
407 @@ -733,9 +725,9 @@ static int __devexit lis302dl_remove(struct platform_device *pdev)
409 /* Reset and power down the device */
410 local_irq_save(flags);
411 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL3, 0x00);
412 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL2, 0x00);
413 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL1, 0x00);
414 + __reg_write(lis, LIS302DL_REG_CTRL3, 0x00);
415 + __reg_write(lis, LIS302DL_REG_CTRL2, 0x00);
416 + __reg_write(lis, LIS302DL_REG_CTRL1, 0x00);
417 local_irq_restore(flags);
419 /* Cleanup resources */
420 @@ -798,13 +790,12 @@ static int lis302dl_suspend(struct platform_device *pdev, pm_message_t state)
422 for (n = 0; n < ARRAY_SIZE(regs_to_save); n++)
423 lis->regs[regs_to_save[n]] =
424 - (lis->pdata->lis302dl_bitbang_reg_read)(lis,
426 + __reg_read(lis, regs_to_save[n]);
429 - tmp = (lis->pdata->lis302dl_bitbang_reg_read)(lis, LIS302DL_REG_CTRL1);
430 + tmp = __reg_read(lis, LIS302DL_REG_CTRL1);
431 tmp &= ~LIS302DL_CTRL1_PD;
432 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL1, tmp);
433 + __reg_write(lis, LIS302DL_REG_CTRL1, tmp);
435 /* place our IO to the device in sleep-compatible states */
436 (lis->pdata->lis302dl_suspend_io)(lis, 0);
437 @@ -830,11 +821,11 @@ static int lis302dl_resume(struct platform_device *pdev)
438 (lis->pdata->lis302dl_suspend_io)(lis, 1);
440 /* resume from powerdown first! */
441 - (lis->pdata->lis302dl_bitbang_reg_write)(lis, LIS302DL_REG_CTRL1,
442 - LIS302DL_CTRL1_PD |
443 - LIS302DL_CTRL1_Xen |
444 - LIS302DL_CTRL1_Yen |
445 - LIS302DL_CTRL1_Zen);
446 + __reg_write(lis, LIS302DL_REG_CTRL1,
447 + LIS302DL_CTRL1_PD |
448 + LIS302DL_CTRL1_Xen |
449 + LIS302DL_CTRL1_Yen |
450 + LIS302DL_CTRL1_Zen);
453 if (__lis302dl_reset_device(lis))
454 @@ -847,8 +838,7 @@ static int lis302dl_resume(struct platform_device *pdev)
456 /* restore registers after resume */
457 for (n = 0; n < ARRAY_SIZE(regs_to_save); n++)
458 - (lis->pdata->lis302dl_bitbang_reg_write)(lis,
459 - regs_to_save[n], lis->regs[regs_to_save[n]]);
460 + __reg_write(lis, regs_to_save[n], lis->regs[regs_to_save[n]]);
462 local_irq_restore(flags);
463 enable_irq(lis->pdata->interrupt);
464 diff --git a/include/linux/lis302dl.h b/include/linux/lis302dl.h
465 index f3f994d..a756f55 100644
466 --- a/include/linux/lis302dl.h
467 +++ b/include/linux/lis302dl.h
468 @@ -18,7 +18,6 @@ struct lis302dl_platform_data {
470 void (*lis302dl_bitbang)(struct lis302dl_info *lis, u8 *tx,
471 int tx_bytes, u8 *rx, int rx_bytes);
472 - void (*lis302dl_bitbang_read_sample)(struct lis302dl_info *);
473 void (*lis302dl_suspend_io)(struct lis302dl_info *, int resuming);
474 int (*lis302dl_bitbang_reg_read)(struct lis302dl_info *, u8 reg);
475 void (*lis302dl_bitbang_reg_write)(struct lis302dl_info *, u8 reg,