2 * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
4 * Copyright (C) 2007-2008 Yan Burman
5 * Copyright (C) 2008 Eric Piel
6 * Copyright (C) 2008-2009 Pavel Machek
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.
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.
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
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/slab.h>
35 #include <linux/freezer.h>
36 #include <linux/uaccess.h>
37 #include <linux/miscdevice.h>
38 #include <linux/pm_runtime.h>
39 #include <asm/atomic.h>
40 #include "lis3lv02d.h"
42 #define DRIVER_NAME "lis3lv02d"
44 /* joystick device poll interval in milliseconds */
45 #define MDPS_POLL_INTERVAL 50
46 #define MDPS_POLL_MIN 0
47 #define MDPS_POLL_MAX 2000
49 #define LIS3_SYSFS_POWERDOWN_DELAY 5000 /* In milliseconds */
52 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
53 * because they are generated even if the data do not change. So it's better
54 * to keep the interrupt for the free-fall event. The values are updated at
55 * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
56 * some low processor, we poll the sensor only at 20Hz... enough for the
60 #define LIS3_PWRON_DELAY_WAI_12B (5000)
61 #define LIS3_PWRON_DELAY_WAI_8B (3000)
64 * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
65 * LIS302D spec says: 18 mG / digit
66 * LIS3_ACCURACY is used to increase accuracy of the intermediate
67 * calculation results.
69 #define LIS3_ACCURACY 1024
70 /* Sensitivity values for -2G +2G scale */
71 #define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024)
72 #define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY)
74 #define LIS3_DEFAULT_FUZZ 3
75 #define LIS3_DEFAULT_FLAT 3
77 struct lis3lv02d lis3_dev = {
78 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
81 EXPORT_SYMBOL_GPL(lis3_dev);
83 /* just like param_set_int() but does sanity-check so that it won't point
84 * over the axis array size
86 static int param_set_axis(const char *val, const struct kernel_param *kp)
88 int ret = param_set_int(val, kp);
90 int val = *(int *)kp->arg;
99 static struct kernel_param_ops param_ops_axis = {
100 .set = param_set_axis,
101 .get = param_get_int,
104 module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
105 MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
107 static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
110 if (lis3->read(lis3, reg, &lo) < 0)
116 static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
120 lis3->read(lis3, reg - 1, &lo);
121 lis3->read(lis3, reg, &hi);
122 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
123 return (s16)((hi << 8) | lo);
127 * lis3lv02d_get_axis - For the given axis, give the value converted
128 * @axis: 1,2,3 - can also be negative
129 * @hw_values: raw values returned by the hardware
131 * Returns the converted value.
133 static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
136 return hw_values[axis - 1];
138 return -hw_values[-axis - 1];
142 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
143 * @lis3: pointer to the device struct
144 * @x: where to store the X axis value
145 * @y: where to store the Y axis value
146 * @z: where to store the Z axis value
148 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
150 static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
155 position[0] = lis3->read_data(lis3, OUTX);
156 position[1] = lis3->read_data(lis3, OUTY);
157 position[2] = lis3->read_data(lis3, OUTZ);
159 for (i = 0; i < 3; i++)
160 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
162 *x = lis3lv02d_get_axis(lis3->ac.x, position);
163 *y = lis3lv02d_get_axis(lis3->ac.y, position);
164 *z = lis3lv02d_get_axis(lis3->ac.z, position);
167 /* conversion btw sampling rate and the register values */
168 static int lis3_12_rates[4] = {40, 160, 640, 2560};
169 static int lis3_8_rates[2] = {100, 400};
170 static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
172 /* ODR is Output Data Rate */
173 static int lis3lv02d_get_odr(void)
178 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
179 ctrl &= lis3_dev.odr_mask;
180 shift = ffs(lis3_dev.odr_mask) - 1;
181 return lis3_dev.odrs[(ctrl >> shift)];
184 static int lis3lv02d_set_odr(int rate)
192 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
193 ctrl &= ~lis3_dev.odr_mask;
194 len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
195 shift = ffs(lis3_dev.odr_mask) - 1;
197 for (i = 0; i < len; i++)
198 if (lis3_dev.odrs[i] == rate) {
199 lis3_dev.write(&lis3_dev, CTRL_REG1,
200 ctrl | (i << shift));
206 static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
213 mutex_lock(&lis3->mutex);
214 if (lis3_dev.whoami == WAI_3DC) {
216 selftest = CTRL4_ST0;
219 if (lis3_dev.whoami == WAI_12B)
222 selftest = CTRL1_STP;
225 lis3->read(lis3, ctlreg, ®);
226 lis3->write(lis3, ctlreg, (reg | selftest));
227 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
229 /* Read directly to avoid axis remap */
230 x = lis3->read_data(lis3, OUTX);
231 y = lis3->read_data(lis3, OUTY);
232 z = lis3->read_data(lis3, OUTZ);
234 /* back to normal settings */
235 lis3->write(lis3, ctlreg, reg);
236 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
238 results[0] = x - lis3->read_data(lis3, OUTX);
239 results[1] = y - lis3->read_data(lis3, OUTY);
240 results[2] = z - lis3->read_data(lis3, OUTZ);
245 for (i = 0; i < 3; i++) {
246 /* Check against selftest acceptance limits */
247 if ((results[i] < lis3->pdata->st_min_limits[i]) ||
248 (results[i] > lis3->pdata->st_max_limits[i])) {
257 mutex_unlock(&lis3->mutex);
262 * Order of registers in the list affects to order of the restore process.
263 * Perhaps it is a good idea to set interrupt enable register as a last one
264 * after all other configurations
266 static u8 lis3_wai8_regs[] = { FF_WU_CFG_1, FF_WU_THS_1, FF_WU_DURATION_1,
267 FF_WU_CFG_2, FF_WU_THS_2, FF_WU_DURATION_2,
268 CLICK_CFG, CLICK_SRC, CLICK_THSY_X, CLICK_THSZ,
269 CLICK_TIMELIMIT, CLICK_LATENCY, CLICK_WINDOW,
270 CTRL_REG1, CTRL_REG2, CTRL_REG3};
272 static u8 lis3_wai12_regs[] = {FF_WU_CFG, FF_WU_THS_L, FF_WU_THS_H,
273 FF_WU_DURATION, DD_CFG, DD_THSI_L, DD_THSI_H,
274 DD_THSE_L, DD_THSE_H,
275 CTRL_REG1, CTRL_REG3, CTRL_REG2};
277 static inline void lis3_context_save(struct lis3lv02d *lis3)
280 for (i = 0; i < lis3->regs_size; i++)
281 lis3->read(lis3, lis3->regs[i], &lis3->reg_cache[i]);
282 lis3->regs_stored = true;
285 static inline void lis3_context_restore(struct lis3lv02d *lis3)
288 if (lis3->regs_stored)
289 for (i = 0; i < lis3->regs_size; i++)
290 lis3->write(lis3, lis3->regs[i], lis3->reg_cache[i]);
293 void lis3lv02d_poweroff(struct lis3lv02d *lis3)
296 lis3_context_save(lis3);
297 /* disable X,Y,Z axis and power down */
298 lis3->write(lis3, CTRL_REG1, 0x00);
300 lis3->reg_ctrl(lis3, LIS3_REG_OFF);
302 EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
304 void lis3lv02d_poweron(struct lis3lv02d *lis3)
310 /* LIS3 power on delay is quite long */
311 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
314 * Common configuration
315 * BDU: (12 bits sensors only) LSB and MSB values are not updated until
316 * both have been read. So the value read will always be correct.
318 if (lis3->whoami == WAI_12B) {
319 lis3->read(lis3, CTRL_REG2, ®);
321 lis3->write(lis3, CTRL_REG2, reg);
324 lis3_context_restore(lis3);
326 EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
329 static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
333 mutex_lock(&lis3_dev.mutex);
334 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
335 input_report_abs(pidev->input, ABS_X, x);
336 input_report_abs(pidev->input, ABS_Y, y);
337 input_report_abs(pidev->input, ABS_Z, z);
338 input_sync(pidev->input);
339 mutex_unlock(&lis3_dev.mutex);
342 static void lis3lv02d_joystick_open(struct input_polled_dev *pidev)
345 pm_runtime_get_sync(lis3_dev.pm_dev);
347 if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev)
348 atomic_set(&lis3_dev.wake_thread, 1);
351 static void lis3lv02d_joystick_close(struct input_polled_dev *pidev)
353 atomic_set(&lis3_dev.wake_thread, 0);
355 pm_runtime_put(lis3_dev.pm_dev);
358 static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
360 if (!test_bit(0, &lis3_dev.misc_opened))
364 * Be careful: on some HP laptops the bios force DD when on battery and
365 * the lid is closed. This leads to interrupts as soon as a little move
368 atomic_inc(&lis3_dev.count);
370 wake_up_interruptible(&lis3_dev.misc_wait);
371 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
373 if (atomic_read(&lis3_dev.wake_thread))
374 return IRQ_WAKE_THREAD;
378 static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
380 struct input_dev *dev = lis3->idev->input;
383 mutex_lock(&lis3->mutex);
384 lis3->read(lis3, CLICK_SRC, &click_src);
386 if (click_src & CLICK_SINGLE_X) {
387 input_report_key(dev, lis3->mapped_btns[0], 1);
388 input_report_key(dev, lis3->mapped_btns[0], 0);
391 if (click_src & CLICK_SINGLE_Y) {
392 input_report_key(dev, lis3->mapped_btns[1], 1);
393 input_report_key(dev, lis3->mapped_btns[1], 0);
396 if (click_src & CLICK_SINGLE_Z) {
397 input_report_key(dev, lis3->mapped_btns[2], 1);
398 input_report_key(dev, lis3->mapped_btns[2], 0);
401 mutex_unlock(&lis3->mutex);
404 static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
407 struct lis3lv02d *lis3 = data;
409 if ((lis3->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
410 lis302dl_interrupt_handle_click(lis3);
412 lis3lv02d_joystick_poll(lis3->idev);
417 static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
420 struct lis3lv02d *lis3 = data;
422 if ((lis3->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
423 lis302dl_interrupt_handle_click(lis3);
425 lis3lv02d_joystick_poll(lis3->idev);
430 static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
432 if (test_and_set_bit(0, &lis3_dev.misc_opened))
433 return -EBUSY; /* already open */
436 pm_runtime_get_sync(lis3_dev.pm_dev);
438 atomic_set(&lis3_dev.count, 0);
442 static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
444 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
445 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
447 pm_runtime_put(lis3_dev.pm_dev);
451 static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
452 size_t count, loff_t *pos)
454 DECLARE_WAITQUEUE(wait, current);
456 unsigned char byte_data;
462 add_wait_queue(&lis3_dev.misc_wait, &wait);
464 set_current_state(TASK_INTERRUPTIBLE);
465 data = atomic_xchg(&lis3_dev.count, 0);
469 if (file->f_flags & O_NONBLOCK) {
474 if (signal_pending(current)) {
475 retval = -ERESTARTSYS;
487 /* make sure we are not going into copy_to_user() with
488 * TASK_INTERRUPTIBLE state */
489 set_current_state(TASK_RUNNING);
490 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
494 __set_current_state(TASK_RUNNING);
495 remove_wait_queue(&lis3_dev.misc_wait, &wait);
500 static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
502 poll_wait(file, &lis3_dev.misc_wait, wait);
503 if (atomic_read(&lis3_dev.count))
504 return POLLIN | POLLRDNORM;
508 static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
510 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
513 static const struct file_operations lis3lv02d_misc_fops = {
514 .owner = THIS_MODULE,
516 .read = lis3lv02d_misc_read,
517 .open = lis3lv02d_misc_open,
518 .release = lis3lv02d_misc_release,
519 .poll = lis3lv02d_misc_poll,
520 .fasync = lis3lv02d_misc_fasync,
523 static struct miscdevice lis3lv02d_misc_device = {
524 .minor = MISC_DYNAMIC_MINOR,
526 .fops = &lis3lv02d_misc_fops,
529 int lis3lv02d_joystick_enable(void)
531 struct input_dev *input_dev;
533 int max_val, fuzz, flat;
534 int btns[] = {BTN_X, BTN_Y, BTN_Z};
539 lis3_dev.idev = input_allocate_polled_device();
543 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
544 lis3_dev.idev->open = lis3lv02d_joystick_open;
545 lis3_dev.idev->close = lis3lv02d_joystick_close;
546 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
547 lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN;
548 lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX;
549 input_dev = lis3_dev.idev->input;
551 input_dev->name = "ST LIS3LV02DL Accelerometer";
552 input_dev->phys = DRIVER_NAME "/input0";
553 input_dev->id.bustype = BUS_HOST;
554 input_dev->id.vendor = 0;
555 input_dev->dev.parent = &lis3_dev.pdev->dev;
557 set_bit(EV_ABS, input_dev->evbit);
558 max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
559 fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
560 flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
561 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
562 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
563 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
565 lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
566 lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
567 lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
569 err = input_register_polled_device(lis3_dev.idev);
571 input_free_polled_device(lis3_dev.idev);
572 lis3_dev.idev = NULL;
577 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
579 void lis3lv02d_joystick_disable(void)
582 free_irq(lis3_dev.irq, &lis3_dev);
583 if (lis3_dev.pdata && lis3_dev.pdata->irq2)
584 free_irq(lis3_dev.pdata->irq2, &lis3_dev);
590 misc_deregister(&lis3lv02d_misc_device);
591 input_unregister_polled_device(lis3_dev.idev);
592 input_free_polled_device(lis3_dev.idev);
593 lis3_dev.idev = NULL;
595 EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
598 static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
601 * SYSFS functions are fast visitors so put-call
602 * immediately after the get-call. However, keep
603 * chip running for a while and schedule delayed
604 * suspend. This way periodic sysfs calls doesn't
605 * suffer from relatively long power up time.
609 pm_runtime_get_sync(lis3->pm_dev);
610 pm_runtime_put_noidle(lis3->pm_dev);
611 pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
615 static ssize_t lis3lv02d_selftest_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
621 lis3lv02d_sysfs_poweron(&lis3_dev);
622 result = lis3lv02d_selftest(&lis3_dev, values);
623 return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
624 values[0], values[1], values[2]);
627 static ssize_t lis3lv02d_position_show(struct device *dev,
628 struct device_attribute *attr, char *buf)
632 lis3lv02d_sysfs_poweron(&lis3_dev);
633 mutex_lock(&lis3_dev.mutex);
634 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
635 mutex_unlock(&lis3_dev.mutex);
636 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
639 static ssize_t lis3lv02d_rate_show(struct device *dev,
640 struct device_attribute *attr, char *buf)
642 lis3lv02d_sysfs_poweron(&lis3_dev);
643 return sprintf(buf, "%d\n", lis3lv02d_get_odr());
646 static ssize_t lis3lv02d_rate_set(struct device *dev,
647 struct device_attribute *attr, const char *buf,
652 if (strict_strtoul(buf, 0, &rate))
655 lis3lv02d_sysfs_poweron(&lis3_dev);
656 if (lis3lv02d_set_odr(rate))
662 static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
663 static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
664 static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
667 static struct attribute *lis3lv02d_attributes[] = {
668 &dev_attr_selftest.attr,
669 &dev_attr_position.attr,
674 static struct attribute_group lis3lv02d_attribute_group = {
675 .attrs = lis3lv02d_attributes
679 static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
681 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
682 if (IS_ERR(lis3->pdev))
683 return PTR_ERR(lis3->pdev);
685 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
688 int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
690 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
691 platform_device_unregister(lis3->pdev);
693 /* Barrier after the sysfs remove */
694 pm_runtime_barrier(lis3->pm_dev);
696 /* SYSFS may have left chip running. Turn off if necessary */
697 if (!pm_runtime_suspended(lis3->pm_dev))
698 lis3lv02d_poweroff(&lis3_dev);
700 pm_runtime_disable(lis3->pm_dev);
701 pm_runtime_set_suspended(lis3->pm_dev);
703 kfree(lis3->reg_cache);
706 EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
708 static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
709 struct lis3lv02d_platform_data *p)
712 int ctrl2 = p->hipass_ctrl;
714 if (p->click_flags) {
715 dev->write(dev, CLICK_CFG, p->click_flags);
716 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
717 dev->write(dev, CLICK_LATENCY, p->click_latency);
718 dev->write(dev, CLICK_WINDOW, p->click_window);
719 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
720 dev->write(dev, CLICK_THSY_X,
721 (p->click_thresh_x & 0xf) |
722 (p->click_thresh_y << 4));
725 struct input_dev *input_dev = lis3_dev.idev->input;
726 input_set_capability(input_dev, EV_KEY, BTN_X);
727 input_set_capability(input_dev, EV_KEY, BTN_Y);
728 input_set_capability(input_dev, EV_KEY, BTN_Z);
732 if (p->wakeup_flags) {
733 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
734 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
735 /* default to 2.5ms for now */
736 dev->write(dev, FF_WU_DURATION_1, 1);
737 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
740 if (p->wakeup_flags2) {
741 dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
742 dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
743 /* default to 2.5ms for now */
744 dev->write(dev, FF_WU_DURATION_2, 1);
745 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
747 /* Configure hipass filters */
748 dev->write(dev, CTRL_REG2, ctrl2);
751 err = request_threaded_irq(p->irq2,
753 lis302dl_interrupt_thread2_8b,
754 IRQF_TRIGGER_RISING |
756 DRIVER_NAME, &lis3_dev);
758 printk(KERN_ERR DRIVER_NAME
759 "No second IRQ. Limited functionality\n");
764 * Initialise the accelerometer and the various subsystems.
765 * Should be rather independent of the bus system.
767 int lis3lv02d_init_device(struct lis3lv02d *dev)
770 irq_handler_t thread_fn;
772 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
774 switch (dev->whoami) {
776 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
777 dev->read_data = lis3lv02d_read_12;
778 dev->mdps_max_val = 2048;
779 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
780 dev->odrs = lis3_12_rates;
781 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
782 dev->scale = LIS3_SENSITIVITY_12B;
783 dev->regs = lis3_wai12_regs;
784 dev->regs_size = ARRAY_SIZE(lis3_wai12_regs);
787 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
788 dev->read_data = lis3lv02d_read_8;
789 dev->mdps_max_val = 128;
790 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
791 dev->odrs = lis3_8_rates;
792 dev->odr_mask = CTRL1_DR;
793 dev->scale = LIS3_SENSITIVITY_8B;
794 dev->regs = lis3_wai8_regs;
795 dev->regs_size = ARRAY_SIZE(lis3_wai8_regs);
798 printk(KERN_INFO DRIVER_NAME ": 8 bits 3DC sensor found\n");
799 dev->read_data = lis3lv02d_read_8;
800 dev->mdps_max_val = 128;
801 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
802 dev->odrs = lis3_3dc_rates;
803 dev->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
804 dev->scale = LIS3_SENSITIVITY_8B;
807 printk(KERN_ERR DRIVER_NAME
808 ": unknown sensor type 0x%X\n", dev->whoami);
812 dev->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
813 sizeof(lis3_wai12_regs)), GFP_KERNEL);
815 if (dev->reg_cache == NULL) {
816 printk(KERN_ERR DRIVER_NAME "out of memory\n");
820 mutex_init(&dev->mutex);
821 atomic_set(&dev->wake_thread, 0);
823 lis3lv02d_add_fs(dev);
824 lis3lv02d_poweron(dev);
827 pm_runtime_set_active(dev->pm_dev);
828 pm_runtime_enable(dev->pm_dev);
831 if (lis3lv02d_joystick_enable())
832 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
834 /* passing in platform specific data is purely optional and only
835 * used by the SPI transport layer at the moment */
837 struct lis3lv02d_platform_data *p = dev->pdata;
839 if (dev->whoami == WAI_8B)
840 lis3lv02d_8b_configure(dev, p);
842 dev->irq_cfg = p->irq_cfg;
844 dev->write(dev, CTRL_REG3, p->irq_cfg);
847 /* bail if we did not get an IRQ from the bus layer */
849 printk(KERN_ERR DRIVER_NAME
850 ": No IRQ. Disabling /dev/freefall\n");
855 * The sensor can generate interrupts for free-fall and direction
856 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
857 * the things simple and _fast_ we activate it only for free-fall, so
858 * no need to read register (very slow with ACPI). For the same reason,
859 * we forbid shared interrupts.
861 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
862 * io-apic is not configurable (and generates a warning) but I keep it
863 * in case of support for other hardware.
865 if (dev->pdata && dev->whoami == WAI_8B)
866 thread_fn = lis302dl_interrupt_thread1_8b;
870 err = request_threaded_irq(dev->irq, lis302dl_interrupt,
872 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
873 DRIVER_NAME, &lis3_dev);
876 printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
880 if (misc_register(&lis3lv02d_misc_device))
881 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
885 EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
887 MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
888 MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
889 MODULE_LICENSE("GPL");