hwmon: lis3: pm_runtime support
[pandora-kernel.git] / drivers / hwmon / lis3lv02d.c
1 /*
2  *  lis3lv02d.c - ST LIS3LV02DL accelerometer driver
3  *
4  *  Copyright (C) 2007-2008 Yan Burman
5  *  Copyright (C) 2008 Eric Piel
6  *  Copyright (C) 2008-2009 Pavel Machek
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/dmi.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/platform_device.h>
29 #include <linux/interrupt.h>
30 #include <linux/input-polldev.h>
31 #include <linux/delay.h>
32 #include <linux/wait.h>
33 #include <linux/poll.h>
34 #include <linux/freezer.h>
35 #include <linux/uaccess.h>
36 #include <linux/miscdevice.h>
37 #include <linux/pm_runtime.h>
38 #include <asm/atomic.h>
39 #include "lis3lv02d.h"
40
41 #define DRIVER_NAME     "lis3lv02d"
42
43 /* joystick device poll interval in milliseconds */
44 #define MDPS_POLL_INTERVAL 50
45 #define MDPS_POLL_MIN      0
46 #define MDPS_POLL_MAX      2000
47
48 #define LIS3_SYSFS_POWERDOWN_DELAY 5000 /* In milliseconds */
49
50 /*
51  * The sensor can also generate interrupts (DRDY) but it's pretty pointless
52  * because they are generated even if the data do not change. So it's better
53  * to keep the interrupt for the free-fall event. The values are updated at
54  * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
55  * some low processor, we poll the sensor only at 20Hz... enough for the
56  * joystick.
57  */
58
59 #define LIS3_PWRON_DELAY_WAI_12B        (5000)
60 #define LIS3_PWRON_DELAY_WAI_8B         (3000)
61
62 /*
63  * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
64  * LIS302D spec says: 18 mG / digit
65  * LIS3_ACCURACY is used to increase accuracy of the intermediate
66  * calculation results.
67  */
68 #define LIS3_ACCURACY                   1024
69 /* Sensitivity values for -2G +2G scale */
70 #define LIS3_SENSITIVITY_12B            ((LIS3_ACCURACY * 1000) / 1024)
71 #define LIS3_SENSITIVITY_8B             (18 * LIS3_ACCURACY)
72
73 #define LIS3_DEFAULT_FUZZ               3
74 #define LIS3_DEFAULT_FLAT               3
75
76 struct lis3lv02d lis3_dev = {
77         .misc_wait   = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
78 };
79
80 EXPORT_SYMBOL_GPL(lis3_dev);
81
82 /* just like param_set_int() but does sanity-check so that it won't point
83  * over the axis array size
84  */
85 static int param_set_axis(const char *val, const struct kernel_param *kp)
86 {
87         int ret = param_set_int(val, kp);
88         if (!ret) {
89                 int val = *(int *)kp->arg;
90                 if (val < 0)
91                         val = -val;
92                 if (!val || val > 3)
93                         return -EINVAL;
94         }
95         return ret;
96 }
97
98 static struct kernel_param_ops param_ops_axis = {
99         .set = param_set_axis,
100         .get = param_get_int,
101 };
102
103 module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
104 MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
105
106 static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
107 {
108         s8 lo;
109         if (lis3->read(lis3, reg, &lo) < 0)
110                 return 0;
111
112         return lo;
113 }
114
115 static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
116 {
117         u8 lo, hi;
118
119         lis3->read(lis3, reg - 1, &lo);
120         lis3->read(lis3, reg, &hi);
121         /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
122         return (s16)((hi << 8) | lo);
123 }
124
125 /**
126  * lis3lv02d_get_axis - For the given axis, give the value converted
127  * @axis:      1,2,3 - can also be negative
128  * @hw_values: raw values returned by the hardware
129  *
130  * Returns the converted value.
131  */
132 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
133 {
134         if (axis > 0)
135                 return hw_values[axis - 1];
136         else
137                 return -hw_values[-axis - 1];
138 }
139
140 /**
141  * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
142  * @lis3: pointer to the device struct
143  * @x:    where to store the X axis value
144  * @y:    where to store the Y axis value
145  * @z:    where to store the Z axis value
146  *
147  * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
148  */
149 static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
150 {
151         int position[3];
152         int i;
153
154         position[0] = lis3->read_data(lis3, OUTX);
155         position[1] = lis3->read_data(lis3, OUTY);
156         position[2] = lis3->read_data(lis3, OUTZ);
157
158         for (i = 0; i < 3; i++)
159                 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
160
161         *x = lis3lv02d_get_axis(lis3->ac.x, position);
162         *y = lis3lv02d_get_axis(lis3->ac.y, position);
163         *z = lis3lv02d_get_axis(lis3->ac.z, position);
164 }
165
166 /* conversion btw sampling rate and the register values */
167 static int lis3_12_rates[4] = {40, 160, 640, 2560};
168 static int lis3_8_rates[2] = {100, 400};
169 static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
170
171 /* ODR is Output Data Rate */
172 static int lis3lv02d_get_odr(void)
173 {
174         u8 ctrl;
175         int shift;
176
177         lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
178         ctrl &= lis3_dev.odr_mask;
179         shift = ffs(lis3_dev.odr_mask) - 1;
180         return lis3_dev.odrs[(ctrl >> shift)];
181 }
182
183 static int lis3lv02d_set_odr(int rate)
184 {
185         u8 ctrl;
186         int i, len, shift;
187
188         if (!rate)
189                 return -EINVAL;
190
191         lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
192         ctrl &= ~lis3_dev.odr_mask;
193         len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
194         shift = ffs(lis3_dev.odr_mask) - 1;
195
196         for (i = 0; i < len; i++)
197                 if (lis3_dev.odrs[i] == rate) {
198                         lis3_dev.write(&lis3_dev, CTRL_REG1,
199                                         ctrl | (i << shift));
200                         return 0;
201                 }
202         return -EINVAL;
203 }
204
205 static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
206 {
207         u8 ctlreg, reg;
208         s16 x, y, z;
209         u8 selftest;
210         int ret;
211
212         mutex_lock(&lis3->mutex);
213         if (lis3_dev.whoami == WAI_3DC) {
214                 ctlreg = CTRL_REG4;
215                 selftest = CTRL4_ST0;
216         } else {
217                 ctlreg = CTRL_REG1;
218                 if (lis3_dev.whoami == WAI_12B)
219                         selftest = CTRL1_ST;
220                 else
221                         selftest = CTRL1_STP;
222         }
223
224         lis3->read(lis3, ctlreg, &reg);
225         lis3->write(lis3, ctlreg, (reg | selftest));
226         msleep(lis3->pwron_delay / lis3lv02d_get_odr());
227
228         /* Read directly to avoid axis remap */
229         x = lis3->read_data(lis3, OUTX);
230         y = lis3->read_data(lis3, OUTY);
231         z = lis3->read_data(lis3, OUTZ);
232
233         /* back to normal settings */
234         lis3->write(lis3, ctlreg, reg);
235         msleep(lis3->pwron_delay / lis3lv02d_get_odr());
236
237         results[0] = x - lis3->read_data(lis3, OUTX);
238         results[1] = y - lis3->read_data(lis3, OUTY);
239         results[2] = z - lis3->read_data(lis3, OUTZ);
240
241         ret = 0;
242         if (lis3->pdata) {
243                 int i;
244                 for (i = 0; i < 3; i++) {
245                         /* Check against selftest acceptance limits */
246                         if ((results[i] < lis3->pdata->st_min_limits[i]) ||
247                             (results[i] > lis3->pdata->st_max_limits[i])) {
248                                 ret = -EIO;
249                                 goto fail;
250                         }
251                 }
252         }
253
254         /* test passed */
255 fail:
256         mutex_unlock(&lis3->mutex);
257         return ret;
258 }
259
260 void lis3lv02d_poweroff(struct lis3lv02d *lis3)
261 {
262         /* disable X,Y,Z axis and power down */
263         lis3->write(lis3, CTRL_REG1, 0x00);
264 }
265 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
266
267 void lis3lv02d_poweron(struct lis3lv02d *lis3)
268 {
269         u8 reg;
270
271         lis3->init(lis3);
272
273         /* LIS3 power on delay is quite long */
274         msleep(lis3->pwron_delay / lis3lv02d_get_odr());
275
276         /*
277          * Common configuration
278          * BDU: (12 bits sensors only) LSB and MSB values are not updated until
279          *      both have been read. So the value read will always be correct.
280          */
281         if (lis3->whoami ==  WAI_12B) {
282                 lis3->read(lis3, CTRL_REG2, &reg);
283                 reg |= CTRL2_BDU;
284                 lis3->write(lis3, CTRL_REG2, reg);
285         }
286 }
287 EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
288
289
290 static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
291 {
292         int x, y, z;
293
294         mutex_lock(&lis3_dev.mutex);
295         lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
296         input_report_abs(pidev->input, ABS_X, x);
297         input_report_abs(pidev->input, ABS_Y, y);
298         input_report_abs(pidev->input, ABS_Z, z);
299         input_sync(pidev->input);
300         mutex_unlock(&lis3_dev.mutex);
301 }
302
303 static void lis3lv02d_joystick_open(struct input_polled_dev *pidev)
304 {
305         if (lis3_dev.pm_dev)
306                 pm_runtime_get_sync(lis3_dev.pm_dev);
307 }
308
309 static void lis3lv02d_joystick_close(struct input_polled_dev *pidev)
310 {
311         if (lis3_dev.pm_dev)
312                 pm_runtime_put(lis3_dev.pm_dev);
313 }
314
315 static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
316 {
317         if (!test_bit(0, &lis3_dev.misc_opened))
318                 goto out;
319
320         /*
321          * Be careful: on some HP laptops the bios force DD when on battery and
322          * the lid is closed. This leads to interrupts as soon as a little move
323          * is done.
324          */
325         atomic_inc(&lis3_dev.count);
326
327         wake_up_interruptible(&lis3_dev.misc_wait);
328         kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
329 out:
330         if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev &&
331             lis3_dev.idev->input->users)
332                 return IRQ_WAKE_THREAD;
333         return IRQ_HANDLED;
334 }
335
336 static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
337 {
338         struct input_dev *dev = lis3->idev->input;
339         u8 click_src;
340
341         mutex_lock(&lis3->mutex);
342         lis3->read(lis3, CLICK_SRC, &click_src);
343
344         if (click_src & CLICK_SINGLE_X) {
345                 input_report_key(dev, lis3->mapped_btns[0], 1);
346                 input_report_key(dev, lis3->mapped_btns[0], 0);
347         }
348
349         if (click_src & CLICK_SINGLE_Y) {
350                 input_report_key(dev, lis3->mapped_btns[1], 1);
351                 input_report_key(dev, lis3->mapped_btns[1], 0);
352         }
353
354         if (click_src & CLICK_SINGLE_Z) {
355                 input_report_key(dev, lis3->mapped_btns[2], 1);
356                 input_report_key(dev, lis3->mapped_btns[2], 0);
357         }
358         input_sync(dev);
359         mutex_unlock(&lis3->mutex);
360 }
361
362 static void lis302dl_interrupt_handle_ff_wu(struct lis3lv02d *lis3)
363 {
364         u8 wu1_src;
365         u8 wu2_src;
366
367         lis3->read(lis3, FF_WU_SRC_1, &wu1_src);
368         lis3->read(lis3, FF_WU_SRC_2, &wu2_src);
369
370         wu1_src = wu1_src & FF_WU_SRC_IA ? wu1_src : 0;
371         wu2_src = wu2_src & FF_WU_SRC_IA ? wu2_src : 0;
372
373         /* joystick poll is internally protected by the lis3->mutex. */
374         if (wu1_src || wu2_src)
375                 lis3lv02d_joystick_poll(lis3_dev.idev);
376 }
377
378 static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
379 {
380
381         struct lis3lv02d *lis3 = data;
382
383         if ((lis3->pdata->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
384                 lis302dl_interrupt_handle_click(lis3);
385         else
386                 lis302dl_interrupt_handle_ff_wu(lis3);
387
388         return IRQ_HANDLED;
389 }
390
391 static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
392 {
393
394         struct lis3lv02d *lis3 = data;
395
396         if ((lis3->pdata->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
397                 lis302dl_interrupt_handle_click(lis3);
398         else
399                 lis302dl_interrupt_handle_ff_wu(lis3);
400
401         return IRQ_HANDLED;
402 }
403
404 static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
405 {
406         if (test_and_set_bit(0, &lis3_dev.misc_opened))
407                 return -EBUSY; /* already open */
408
409         if (lis3_dev.pm_dev)
410                 pm_runtime_get_sync(lis3_dev.pm_dev);
411
412         atomic_set(&lis3_dev.count, 0);
413         return 0;
414 }
415
416 static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
417 {
418         fasync_helper(-1, file, 0, &lis3_dev.async_queue);
419         clear_bit(0, &lis3_dev.misc_opened); /* release the device */
420         if (lis3_dev.pm_dev)
421                 pm_runtime_put(lis3_dev.pm_dev);
422         return 0;
423 }
424
425 static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
426                                 size_t count, loff_t *pos)
427 {
428         DECLARE_WAITQUEUE(wait, current);
429         u32 data;
430         unsigned char byte_data;
431         ssize_t retval = 1;
432
433         if (count < 1)
434                 return -EINVAL;
435
436         add_wait_queue(&lis3_dev.misc_wait, &wait);
437         while (true) {
438                 set_current_state(TASK_INTERRUPTIBLE);
439                 data = atomic_xchg(&lis3_dev.count, 0);
440                 if (data)
441                         break;
442
443                 if (file->f_flags & O_NONBLOCK) {
444                         retval = -EAGAIN;
445                         goto out;
446                 }
447
448                 if (signal_pending(current)) {
449                         retval = -ERESTARTSYS;
450                         goto out;
451                 }
452
453                 schedule();
454         }
455
456         if (data < 255)
457                 byte_data = data;
458         else
459                 byte_data = 255;
460
461         /* make sure we are not going into copy_to_user() with
462          * TASK_INTERRUPTIBLE state */
463         set_current_state(TASK_RUNNING);
464         if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
465                 retval = -EFAULT;
466
467 out:
468         __set_current_state(TASK_RUNNING);
469         remove_wait_queue(&lis3_dev.misc_wait, &wait);
470
471         return retval;
472 }
473
474 static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
475 {
476         poll_wait(file, &lis3_dev.misc_wait, wait);
477         if (atomic_read(&lis3_dev.count))
478                 return POLLIN | POLLRDNORM;
479         return 0;
480 }
481
482 static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
483 {
484         return fasync_helper(fd, file, on, &lis3_dev.async_queue);
485 }
486
487 static const struct file_operations lis3lv02d_misc_fops = {
488         .owner   = THIS_MODULE,
489         .llseek  = no_llseek,
490         .read    = lis3lv02d_misc_read,
491         .open    = lis3lv02d_misc_open,
492         .release = lis3lv02d_misc_release,
493         .poll    = lis3lv02d_misc_poll,
494         .fasync  = lis3lv02d_misc_fasync,
495 };
496
497 static struct miscdevice lis3lv02d_misc_device = {
498         .minor   = MISC_DYNAMIC_MINOR,
499         .name    = "freefall",
500         .fops    = &lis3lv02d_misc_fops,
501 };
502
503 int lis3lv02d_joystick_enable(void)
504 {
505         struct input_dev *input_dev;
506         int err;
507         int max_val, fuzz, flat;
508         int btns[] = {BTN_X, BTN_Y, BTN_Z};
509
510         if (lis3_dev.idev)
511                 return -EINVAL;
512
513         lis3_dev.idev = input_allocate_polled_device();
514         if (!lis3_dev.idev)
515                 return -ENOMEM;
516
517         lis3_dev.idev->poll = lis3lv02d_joystick_poll;
518         lis3_dev.idev->open = lis3lv02d_joystick_open;
519         lis3_dev.idev->close = lis3lv02d_joystick_close;
520         lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
521         lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN;
522         lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX;
523         input_dev = lis3_dev.idev->input;
524
525         input_dev->name       = "ST LIS3LV02DL Accelerometer";
526         input_dev->phys       = DRIVER_NAME "/input0";
527         input_dev->id.bustype = BUS_HOST;
528         input_dev->id.vendor  = 0;
529         input_dev->dev.parent = &lis3_dev.pdev->dev;
530
531         set_bit(EV_ABS, input_dev->evbit);
532         max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
533         fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
534         flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
535         input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
536         input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
537         input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
538
539         lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
540         lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
541         lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
542
543         err = input_register_polled_device(lis3_dev.idev);
544         if (err) {
545                 input_free_polled_device(lis3_dev.idev);
546                 lis3_dev.idev = NULL;
547         }
548
549         return err;
550 }
551 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
552
553 void lis3lv02d_joystick_disable(void)
554 {
555         if (lis3_dev.irq)
556                 free_irq(lis3_dev.irq, &lis3_dev);
557         if (lis3_dev.pdata && lis3_dev.pdata->irq2)
558                 free_irq(lis3_dev.pdata->irq2, &lis3_dev);
559
560         if (!lis3_dev.idev)
561                 return;
562
563         if (lis3_dev.irq)
564                 misc_deregister(&lis3lv02d_misc_device);
565         input_unregister_polled_device(lis3_dev.idev);
566         input_free_polled_device(lis3_dev.idev);
567         lis3_dev.idev = NULL;
568 }
569 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
570
571 /* Sysfs stuff */
572 static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
573 {
574         /*
575          * SYSFS functions are fast visitors so put-call
576          * immediately after the get-call. However, keep
577          * chip running for a while and schedule delayed
578          * suspend. This way periodic sysfs calls doesn't
579          * suffer from relatively long power up time.
580          */
581
582         if (lis3->pm_dev) {
583                 pm_runtime_get_sync(lis3->pm_dev);
584                 pm_runtime_put_noidle(lis3->pm_dev);
585                 pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
586         }
587 }
588
589 static ssize_t lis3lv02d_selftest_show(struct device *dev,
590                                 struct device_attribute *attr, char *buf)
591 {
592         int result;
593         s16 values[3];
594
595         lis3lv02d_sysfs_poweron(&lis3_dev);
596         result = lis3lv02d_selftest(&lis3_dev, values);
597         return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
598                 values[0], values[1], values[2]);
599 }
600
601 static ssize_t lis3lv02d_position_show(struct device *dev,
602                                 struct device_attribute *attr, char *buf)
603 {
604         int x, y, z;
605
606         lis3lv02d_sysfs_poweron(&lis3_dev);
607         mutex_lock(&lis3_dev.mutex);
608         lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
609         mutex_unlock(&lis3_dev.mutex);
610         return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
611 }
612
613 static ssize_t lis3lv02d_rate_show(struct device *dev,
614                         struct device_attribute *attr, char *buf)
615 {
616         lis3lv02d_sysfs_poweron(&lis3_dev);
617         return sprintf(buf, "%d\n", lis3lv02d_get_odr());
618 }
619
620 static ssize_t lis3lv02d_rate_set(struct device *dev,
621                                 struct device_attribute *attr, const char *buf,
622                                 size_t count)
623 {
624         unsigned long rate;
625
626         if (strict_strtoul(buf, 0, &rate))
627                 return -EINVAL;
628
629         lis3lv02d_sysfs_poweron(&lis3_dev);
630         if (lis3lv02d_set_odr(rate))
631                 return -EINVAL;
632
633         return count;
634 }
635
636 static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
637 static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
638 static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
639                                             lis3lv02d_rate_set);
640
641 static struct attribute *lis3lv02d_attributes[] = {
642         &dev_attr_selftest.attr,
643         &dev_attr_position.attr,
644         &dev_attr_rate.attr,
645         NULL
646 };
647
648 static struct attribute_group lis3lv02d_attribute_group = {
649         .attrs = lis3lv02d_attributes
650 };
651
652
653 static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
654 {
655         lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
656         if (IS_ERR(lis3->pdev))
657                 return PTR_ERR(lis3->pdev);
658
659         return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
660 }
661
662 int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
663 {
664         sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
665         platform_device_unregister(lis3->pdev);
666         if (lis3->pm_dev) {
667                 /* Barrier after the sysfs remove */
668                 pm_runtime_barrier(lis3->pm_dev);
669
670                 /* SYSFS may have left chip running. Turn off if necessary */
671                 if (!pm_runtime_suspended(lis3->pm_dev))
672                         lis3lv02d_poweroff(&lis3_dev);
673
674                 pm_runtime_disable(lis3->pm_dev);
675                 pm_runtime_set_suspended(lis3->pm_dev);
676         }
677         return 0;
678 }
679 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
680
681 static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
682                                 struct lis3lv02d_platform_data *p)
683 {
684         int err;
685         int ctrl2 = p->hipass_ctrl;
686
687         if (p->click_flags) {
688                 dev->write(dev, CLICK_CFG, p->click_flags);
689                 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
690                 dev->write(dev, CLICK_LATENCY, p->click_latency);
691                 dev->write(dev, CLICK_WINDOW, p->click_window);
692                 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
693                 dev->write(dev, CLICK_THSY_X,
694                         (p->click_thresh_x & 0xf) |
695                         (p->click_thresh_y << 4));
696
697                 if (dev->idev) {
698                         struct input_dev *input_dev = lis3_dev.idev->input;
699                         input_set_capability(input_dev, EV_KEY, BTN_X);
700                         input_set_capability(input_dev, EV_KEY, BTN_Y);
701                         input_set_capability(input_dev, EV_KEY, BTN_Z);
702                 }
703         }
704
705         if (p->wakeup_flags) {
706                 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
707                 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
708                 /* default to 2.5ms for now */
709                 dev->write(dev, FF_WU_DURATION_1, 1);
710                 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
711         }
712
713         if (p->wakeup_flags2) {
714                 dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
715                 dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
716                 /* default to 2.5ms for now */
717                 dev->write(dev, FF_WU_DURATION_2, 1);
718                 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
719         }
720         /* Configure hipass filters */
721         dev->write(dev, CTRL_REG2, ctrl2);
722
723         if (p->irq2) {
724                 err = request_threaded_irq(p->irq2,
725                                         NULL,
726                                         lis302dl_interrupt_thread2_8b,
727                                         IRQF_TRIGGER_RISING |
728                                         IRQF_ONESHOT,
729                                         DRIVER_NAME, &lis3_dev);
730                 if (err < 0)
731                         printk(KERN_ERR DRIVER_NAME
732                                 "No second IRQ. Limited functionality\n");
733         }
734 }
735
736 /*
737  * Initialise the accelerometer and the various subsystems.
738  * Should be rather independent of the bus system.
739  */
740 int lis3lv02d_init_device(struct lis3lv02d *dev)
741 {
742         int err;
743         irq_handler_t thread_fn;
744
745         dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
746
747         switch (dev->whoami) {
748         case WAI_12B:
749                 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
750                 dev->read_data = lis3lv02d_read_12;
751                 dev->mdps_max_val = 2048;
752                 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
753                 dev->odrs = lis3_12_rates;
754                 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
755                 dev->scale = LIS3_SENSITIVITY_12B;
756                 break;
757         case WAI_8B:
758                 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
759                 dev->read_data = lis3lv02d_read_8;
760                 dev->mdps_max_val = 128;
761                 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
762                 dev->odrs = lis3_8_rates;
763                 dev->odr_mask = CTRL1_DR;
764                 dev->scale = LIS3_SENSITIVITY_8B;
765                 break;
766         case WAI_3DC:
767                 printk(KERN_INFO DRIVER_NAME ": 8 bits 3DC sensor found\n");
768                 dev->read_data = lis3lv02d_read_8;
769                 dev->mdps_max_val = 128;
770                 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
771                 dev->odrs = lis3_3dc_rates;
772                 dev->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
773                 dev->scale = LIS3_SENSITIVITY_8B;
774                 break;
775         default:
776                 printk(KERN_ERR DRIVER_NAME
777                         ": unknown sensor type 0x%X\n", dev->whoami);
778                 return -EINVAL;
779         }
780
781         mutex_init(&dev->mutex);
782
783         lis3lv02d_add_fs(dev);
784         lis3lv02d_poweron(dev);
785
786         if (dev->pm_dev) {
787                 pm_runtime_set_active(dev->pm_dev);
788                 pm_runtime_enable(dev->pm_dev);
789         }
790
791         if (lis3lv02d_joystick_enable())
792                 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
793
794         /* passing in platform specific data is purely optional and only
795          * used by the SPI transport layer at the moment */
796         if (dev->pdata) {
797                 struct lis3lv02d_platform_data *p = dev->pdata;
798
799                 if (dev->whoami == WAI_8B)
800                         lis3lv02d_8b_configure(dev, p);
801
802                 if (p->irq_cfg)
803                         dev->write(dev, CTRL_REG3, p->irq_cfg);
804         }
805
806         /* bail if we did not get an IRQ from the bus layer */
807         if (!dev->irq) {
808                 printk(KERN_ERR DRIVER_NAME
809                         ": No IRQ. Disabling /dev/freefall\n");
810                 goto out;
811         }
812
813         /*
814          * The sensor can generate interrupts for free-fall and direction
815          * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
816          * the things simple and _fast_ we activate it only for free-fall, so
817          * no need to read register (very slow with ACPI). For the same reason,
818          * we forbid shared interrupts.
819          *
820          * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
821          * io-apic is not configurable (and generates a warning) but I keep it
822          * in case of support for other hardware.
823          */
824         if (dev->pdata && dev->whoami == WAI_8B)
825                 thread_fn = lis302dl_interrupt_thread1_8b;
826         else
827                 thread_fn = NULL;
828
829         err = request_threaded_irq(dev->irq, lis302dl_interrupt,
830                                 thread_fn,
831                                 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
832                                 DRIVER_NAME, &lis3_dev);
833
834         if (err < 0) {
835                 printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
836                 goto out;
837         }
838
839         if (misc_register(&lis3lv02d_misc_device))
840                 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
841 out:
842         return 0;
843 }
844 EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
845
846 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
847 MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
848 MODULE_LICENSE("GPL");