blob: dbd0b055d4b9317d750a286c4ae01dfc675d84a2 [file] [log] [blame]
Pavel Machek455fbdd2008-11-12 13:27:02 -08001/*
2 * lis3lv02d.c - ST LIS3LV02DL accelerometer driver
3 *
4 * Copyright (C) 2007-2008 Yan Burman
5 * Copyright (C) 2008 Eric Piel
Pavel Machekef2cfc72009-02-18 14:48:23 -08006 * Copyright (C) 2008-2009 Pavel Machek
Pavel Machek455fbdd2008-11-12 13:27:02 -08007 *
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>
Eric Pieldc6ea972009-06-16 15:34:15 -070030#include <linux/input-polldev.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080031#include <linux/delay.h>
32#include <linux/wait.h>
33#include <linux/poll.h>
34#include <linux/freezer.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080035#include <linux/uaccess.h>
Pavel Machekef2cfc72009-02-18 14:48:23 -080036#include <linux/miscdevice.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080037#include <asm/atomic.h>
38#include "lis3lv02d.h"
39
40#define DRIVER_NAME "lis3lv02d"
Pavel Machek455fbdd2008-11-12 13:27:02 -080041
42/* joystick device poll interval in milliseconds */
43#define MDPS_POLL_INTERVAL 50
44/*
45 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
46 * because their are generated even if the data do not change. So it's better
47 * to keep the interrupt for the free-fall event. The values are updated at
48 * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
49 * some low processor, we poll the sensor only at 20Hz... enough for the
50 * joystick.
51 */
52
Daniel Macka38da2e2009-03-31 15:24:32 -070053struct lis3lv02d lis3_dev = {
Pavel Machekbe84cfc2009-03-31 15:24:26 -070054 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
Pavel Machekef2cfc72009-02-18 14:48:23 -080055};
56
Pavel Machekbe84cfc2009-03-31 15:24:26 -070057EXPORT_SYMBOL_GPL(lis3_dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080058
Daniel Macka38da2e2009-03-31 15:24:32 -070059static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
60{
61 s8 lo;
62 if (lis3->read(lis3, reg, &lo) < 0)
63 return 0;
64
65 return lo;
66}
67
68static s16 lis3lv02d_read_16(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -070069{
70 u8 lo, hi;
71
Daniel Macka38da2e2009-03-31 15:24:32 -070072 lis3->read(lis3, reg - 1, &lo);
73 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -070074 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
75 return (s16)((hi << 8) | lo);
76}
77
Pavel Machek455fbdd2008-11-12 13:27:02 -080078/**
79 * lis3lv02d_get_axis - For the given axis, give the value converted
80 * @axis: 1,2,3 - can also be negative
81 * @hw_values: raw values returned by the hardware
82 *
83 * Returns the converted value.
84 */
85static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
86{
87 if (axis > 0)
88 return hw_values[axis - 1];
89 else
90 return -hw_values[-axis - 1];
91}
92
93/**
94 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -070095 * @lis3: pointer to the device struct
96 * @x: where to store the X axis value
97 * @y: where to store the Y axis value
98 * @z: where to store the Z axis value
Pavel Machek455fbdd2008-11-12 13:27:02 -080099 *
100 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
101 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700102static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800103{
104 int position[3];
105
Eric Piela002ee82009-06-16 15:34:14 -0700106 position[0] = lis3->read_data(lis3, OUTX);
107 position[1] = lis3->read_data(lis3, OUTY);
108 position[2] = lis3->read_data(lis3, OUTZ);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800109
Eric Piela002ee82009-06-16 15:34:14 -0700110 *x = lis3lv02d_get_axis(lis3->ac.x, position);
111 *y = lis3lv02d_get_axis(lis3->ac.y, position);
112 *z = lis3lv02d_get_axis(lis3->ac.z, position);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800113}
114
Daniel Macka38da2e2009-03-31 15:24:32 -0700115void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800116{
Eric Piela002ee82009-06-16 15:34:14 -0700117 /* disable X,Y,Z axis and power down */
118 lis3->write(lis3, CTRL_REG1, 0x00);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800119}
Eric Pielcfce41a2009-01-09 16:41:01 -0800120EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800121
Daniel Macka38da2e2009-03-31 15:24:32 -0700122void lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800123{
Eric Piela002ee82009-06-16 15:34:14 -0700124 u8 reg;
125
126 lis3->init(lis3);
127
128 /*
129 * Common configuration
130 * BDU: LSB and MSB values are not updated until both have been read.
131 * So the value read will always be correct.
132 */
133 lis3->read(lis3, CTRL_REG2, &reg);
134 reg |= CTRL2_BDU;
135 lis3->write(lis3, CTRL_REG2, reg);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800136}
Eric Pielcfce41a2009-01-09 16:41:01 -0800137EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800138
Pavel Machek455fbdd2008-11-12 13:27:02 -0800139
Pavel Machekef2cfc72009-02-18 14:48:23 -0800140static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
141{
142 /*
143 * Be careful: on some HP laptops the bios force DD when on battery and
144 * the lid is closed. This leads to interrupts as soon as a little move
145 * is done.
146 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700147 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800148
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700149 wake_up_interruptible(&lis3_dev.misc_wait);
150 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800151 return IRQ_HANDLED;
152}
153
154static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
155{
156 int ret;
157
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700158 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800159 return -EBUSY; /* already open */
160
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700161 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800162
163 /*
164 * The sensor can generate interrupts for free-fall and direction
165 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
166 * the things simple and _fast_ we activate it only for free-fall, so
167 * no need to read register (very slow with ACPI). For the same reason,
168 * we forbid shared interrupts.
169 *
170 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
171 * io-apic is not configurable (and generates a warning) but I keep it
172 * in case of support for other hardware.
173 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700174 ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
175 DRIVER_NAME, &lis3_dev);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800176
177 if (ret) {
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700178 clear_bit(0, &lis3_dev.misc_opened);
179 printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", lis3_dev.irq);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800180 return -EBUSY;
181 }
Pavel Machekef2cfc72009-02-18 14:48:23 -0800182 return 0;
183}
184
185static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
186{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700187 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700188 free_irq(lis3_dev.irq, &lis3_dev);
189 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Pavel Machekef2cfc72009-02-18 14:48:23 -0800190 return 0;
191}
192
193static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
194 size_t count, loff_t *pos)
195{
196 DECLARE_WAITQUEUE(wait, current);
197 u32 data;
198 unsigned char byte_data;
199 ssize_t retval = 1;
200
201 if (count < 1)
202 return -EINVAL;
203
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700204 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800205 while (true) {
206 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700207 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800208 if (data)
209 break;
210
211 if (file->f_flags & O_NONBLOCK) {
212 retval = -EAGAIN;
213 goto out;
214 }
215
216 if (signal_pending(current)) {
217 retval = -ERESTARTSYS;
218 goto out;
219 }
220
221 schedule();
222 }
223
224 if (data < 255)
225 byte_data = data;
226 else
227 byte_data = 255;
228
229 /* make sure we are not going into copy_to_user() with
230 * TASK_INTERRUPTIBLE state */
231 set_current_state(TASK_RUNNING);
232 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
233 retval = -EFAULT;
234
235out:
236 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700237 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800238
239 return retval;
240}
241
242static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
243{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700244 poll_wait(file, &lis3_dev.misc_wait, wait);
245 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800246 return POLLIN | POLLRDNORM;
247 return 0;
248}
249
250static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
251{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700252 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800253}
254
255static const struct file_operations lis3lv02d_misc_fops = {
256 .owner = THIS_MODULE,
257 .llseek = no_llseek,
258 .read = lis3lv02d_misc_read,
259 .open = lis3lv02d_misc_open,
260 .release = lis3lv02d_misc_release,
261 .poll = lis3lv02d_misc_poll,
262 .fasync = lis3lv02d_misc_fasync,
263};
264
265static struct miscdevice lis3lv02d_misc_device = {
266 .minor = MISC_DYNAMIC_MINOR,
267 .name = "freefall",
268 .fops = &lis3lv02d_misc_fops,
269};
270
Eric Pieldc6ea972009-06-16 15:34:15 -0700271static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800272{
273 int x, y, z;
274
Eric Pieldc6ea972009-06-16 15:34:15 -0700275 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
276 input_report_abs(pidev->input, ABS_X, x - lis3_dev.xcalib);
277 input_report_abs(pidev->input, ABS_Y, y - lis3_dev.ycalib);
278 input_report_abs(pidev->input, ABS_Z, z - lis3_dev.zcalib);
Samu Onkalod25a8c82009-12-14 18:01:38 -0800279 input_sync(pidev->input);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800280}
281
Pavel Machek455fbdd2008-11-12 13:27:02 -0800282
Pavel Machek455fbdd2008-11-12 13:27:02 -0800283static inline void lis3lv02d_calibrate_joystick(void)
284{
Daniel Macka38da2e2009-03-31 15:24:32 -0700285 lis3lv02d_get_xyz(&lis3_dev,
286 &lis3_dev.xcalib, &lis3_dev.ycalib, &lis3_dev.zcalib);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800287}
288
Eric Pielcfce41a2009-01-09 16:41:01 -0800289int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800290{
Eric Pieldc6ea972009-06-16 15:34:15 -0700291 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800292 int err;
293
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700294 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800295 return -EINVAL;
296
Eric Pieldc6ea972009-06-16 15:34:15 -0700297 lis3_dev.idev = input_allocate_polled_device();
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700298 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800299 return -ENOMEM;
300
Eric Pieldc6ea972009-06-16 15:34:15 -0700301 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
302 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
303 input_dev = lis3_dev.idev->input;
304
Pavel Machek455fbdd2008-11-12 13:27:02 -0800305 lis3lv02d_calibrate_joystick();
306
Eric Pieldc6ea972009-06-16 15:34:15 -0700307 input_dev->name = "ST LIS3LV02DL Accelerometer";
308 input_dev->phys = DRIVER_NAME "/input0";
309 input_dev->id.bustype = BUS_HOST;
310 input_dev->id.vendor = 0;
311 input_dev->dev.parent = &lis3_dev.pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800312
Eric Pieldc6ea972009-06-16 15:34:15 -0700313 set_bit(EV_ABS, input_dev->evbit);
314 input_set_abs_params(input_dev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
315 input_set_abs_params(input_dev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
316 input_set_abs_params(input_dev, ABS_Z, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800317
Eric Pieldc6ea972009-06-16 15:34:15 -0700318 err = input_register_polled_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800319 if (err) {
Eric Pieldc6ea972009-06-16 15:34:15 -0700320 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700321 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800322 }
323
324 return err;
325}
Eric Pielcfce41a2009-01-09 16:41:01 -0800326EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800327
Eric Pielcfce41a2009-01-09 16:41:01 -0800328void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800329{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700330 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800331 return;
332
Eric Pielc2884242009-06-16 15:34:13 -0700333 if (lis3_dev.irq)
334 misc_deregister(&lis3lv02d_misc_device);
Eric Pieldc6ea972009-06-16 15:34:15 -0700335 input_unregister_polled_device(lis3_dev.idev);
Samu Onkalo66c85692009-12-14 18:01:39 -0800336 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700337 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800338}
Eric Pielcfce41a2009-01-09 16:41:01 -0800339EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800340
Pavel Machek455fbdd2008-11-12 13:27:02 -0800341/* Sysfs stuff */
342static ssize_t lis3lv02d_position_show(struct device *dev,
343 struct device_attribute *attr, char *buf)
344{
345 int x, y, z;
346
Daniel Macka38da2e2009-03-31 15:24:32 -0700347 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800348 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
349}
350
351static ssize_t lis3lv02d_calibrate_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700354 return sprintf(buf, "(%d,%d,%d)\n", lis3_dev.xcalib, lis3_dev.ycalib, lis3_dev.zcalib);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800355}
356
357static ssize_t lis3lv02d_calibrate_store(struct device *dev,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
360{
Pavel Machek455fbdd2008-11-12 13:27:02 -0800361 lis3lv02d_calibrate_joystick();
Pavel Machek455fbdd2008-11-12 13:27:02 -0800362 return count;
363}
364
365/* conversion btw sampling rate and the register values */
366static int lis3lv02dl_df_val[4] = {40, 160, 640, 2560};
367static ssize_t lis3lv02d_rate_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
370 u8 ctrl;
371 int val;
372
Daniel Macka38da2e2009-03-31 15:24:32 -0700373 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800374 val = (ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4;
375 return sprintf(buf, "%d\n", lis3lv02dl_df_val[val]);
376}
377
378static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
379static DEVICE_ATTR(calibrate, S_IRUGO|S_IWUSR, lis3lv02d_calibrate_show,
380 lis3lv02d_calibrate_store);
381static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
382
383static struct attribute *lis3lv02d_attributes[] = {
384 &dev_attr_position.attr,
385 &dev_attr_calibrate.attr,
386 &dev_attr_rate.attr,
387 NULL
388};
389
390static struct attribute_group lis3lv02d_attribute_group = {
391 .attrs = lis3lv02d_attributes
392};
393
Eric Pielcfce41a2009-01-09 16:41:01 -0800394
Daniel Macka38da2e2009-03-31 15:24:32 -0700395static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800396{
Eric Piela002ee82009-06-16 15:34:14 -0700397 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
398 if (IS_ERR(lis3->pdev))
399 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800400
Eric Piela002ee82009-06-16 15:34:14 -0700401 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800402}
403
Eric Piela002ee82009-06-16 15:34:14 -0700404int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800405{
Eric Piela002ee82009-06-16 15:34:14 -0700406 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
407 platform_device_unregister(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800408 return 0;
409}
Eric Pielcfce41a2009-01-09 16:41:01 -0800410EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800411
Daniel Mackab337a62009-03-31 15:24:31 -0700412/*
413 * Initialise the accelerometer and the various subsystems.
414 * Should be rather independant of the bus system.
415 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700416int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700417{
Daniel Macka38da2e2009-03-31 15:24:32 -0700418 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
419
420 switch (dev->whoami) {
421 case LIS_DOUBLE_ID:
422 printk(KERN_INFO DRIVER_NAME ": 2-byte sensor found\n");
423 dev->read_data = lis3lv02d_read_16;
424 dev->mdps_max_val = 2048;
425 break;
426 case LIS_SINGLE_ID:
427 printk(KERN_INFO DRIVER_NAME ": 1-byte sensor found\n");
428 dev->read_data = lis3lv02d_read_8;
429 dev->mdps_max_val = 128;
430 break;
431 default:
432 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700433 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700434 return -EINVAL;
435 }
436
Daniel Macka38da2e2009-03-31 15:24:32 -0700437 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700438 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700439
440 if (lis3lv02d_joystick_enable())
441 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
442
Daniel Mack8f3128e2009-06-16 15:34:17 -0700443 /* passing in platform specific data is purely optional and only
444 * used by the SPI transport layer at the moment */
445 if (dev->pdata) {
446 struct lis3lv02d_platform_data *p = dev->pdata;
447
448 if (p->click_flags && (dev->whoami == LIS_SINGLE_ID)) {
449 dev->write(dev, CLICK_CFG, p->click_flags);
450 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
451 dev->write(dev, CLICK_LATENCY, p->click_latency);
452 dev->write(dev, CLICK_WINDOW, p->click_window);
453 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
454 dev->write(dev, CLICK_THSY_X,
455 (p->click_thresh_x & 0xf) |
456 (p->click_thresh_y << 4));
457 }
458
Daniel Mack8873c332009-09-21 17:04:43 -0700459 if (p->wakeup_flags && (dev->whoami == LIS_SINGLE_ID)) {
460 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
461 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
462 /* default to 2.5ms for now */
463 dev->write(dev, FF_WU_DURATION_1, 1);
464 /* enable high pass filter for both free-fall units */
465 dev->write(dev, CTRL_REG2, HP_FF_WU1 | HP_FF_WU2);
466 }
467
Daniel Mack8f3128e2009-06-16 15:34:17 -0700468 if (p->irq_cfg)
469 dev->write(dev, CTRL_REG3, p->irq_cfg);
470 }
471
Daniel Macka38da2e2009-03-31 15:24:32 -0700472 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700473 if (!dev->irq) {
474 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700475 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700476 goto out;
477 }
478
Daniel Mackab337a62009-03-31 15:24:31 -0700479 if (misc_register(&lis3lv02d_misc_device))
480 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
481out:
Daniel Mackab337a62009-03-31 15:24:31 -0700482 return 0;
483}
484EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
485
Pavel Machek455fbdd2008-11-12 13:27:02 -0800486MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800487MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800488MODULE_LICENSE("GPL");
489