blob: 02ac5fa5a05c138db19afeebdf7c4998fd8925b7 [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
Éric Pielbc62c142009-12-14 18:01:39 -080046 * because they are generated even if the data do not change. So it's better
Pavel Machek455fbdd2008-11-12 13:27:02 -080047 * 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
Samu Onkalo641615a2009-12-14 18:01:41 -080053#define LIS3_PWRON_DELAY_WAI_12B (5000)
54#define LIS3_PWRON_DELAY_WAI_8B (3000)
55
Daniel Macka38da2e2009-03-31 15:24:32 -070056struct lis3lv02d lis3_dev = {
Pavel Machekbe84cfc2009-03-31 15:24:26 -070057 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
Pavel Machekef2cfc72009-02-18 14:48:23 -080058};
59
Pavel Machekbe84cfc2009-03-31 15:24:26 -070060EXPORT_SYMBOL_GPL(lis3_dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080061
Daniel Macka38da2e2009-03-31 15:24:32 -070062static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
63{
64 s8 lo;
65 if (lis3->read(lis3, reg, &lo) < 0)
66 return 0;
67
68 return lo;
69}
70
Éric Pielbc62c142009-12-14 18:01:39 -080071static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -070072{
73 u8 lo, hi;
74
Daniel Macka38da2e2009-03-31 15:24:32 -070075 lis3->read(lis3, reg - 1, &lo);
76 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -070077 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
78 return (s16)((hi << 8) | lo);
79}
80
Pavel Machek455fbdd2008-11-12 13:27:02 -080081/**
82 * lis3lv02d_get_axis - For the given axis, give the value converted
83 * @axis: 1,2,3 - can also be negative
84 * @hw_values: raw values returned by the hardware
85 *
86 * Returns the converted value.
87 */
88static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
89{
90 if (axis > 0)
91 return hw_values[axis - 1];
92 else
93 return -hw_values[-axis - 1];
94}
95
96/**
97 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -070098 * @lis3: pointer to the device struct
99 * @x: where to store the X axis value
100 * @y: where to store the Y axis value
101 * @z: where to store the Z axis value
Pavel Machek455fbdd2008-11-12 13:27:02 -0800102 *
103 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
104 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700105static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800106{
107 int position[3];
108
Samu Onkalo2db4a762009-12-14 18:01:43 -0800109 mutex_lock(&lis3->mutex);
Eric Piela002ee82009-06-16 15:34:14 -0700110 position[0] = lis3->read_data(lis3, OUTX);
111 position[1] = lis3->read_data(lis3, OUTY);
112 position[2] = lis3->read_data(lis3, OUTZ);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800113 mutex_unlock(&lis3->mutex);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800114
Eric Piela002ee82009-06-16 15:34:14 -0700115 *x = lis3lv02d_get_axis(lis3->ac.x, position);
116 *y = lis3lv02d_get_axis(lis3->ac.y, position);
117 *z = lis3lv02d_get_axis(lis3->ac.z, position);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800118}
119
Samu Onkalo641615a2009-12-14 18:01:41 -0800120/* conversion btw sampling rate and the register values */
121static int lis3_12_rates[4] = {40, 160, 640, 2560};
122static int lis3_8_rates[2] = {100, 400};
123
124static int lis3lv02d_get_odr(void)
125{
126 u8 ctrl;
127 int val;
128
129 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
130
131 if (lis3_dev.whoami == WAI_12B)
132 val = lis3_12_rates[(ctrl & (CTRL1_DF0 | CTRL1_DF1)) >> 4];
133 else
134 val = lis3_8_rates[(ctrl & CTRL1_DR) >> 7];
135 return val;
136}
137
Samu Onkalo2db4a762009-12-14 18:01:43 -0800138static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
139{
140 u8 reg;
141 s16 x, y, z;
142 u8 selftest;
143 int ret;
144
145 mutex_lock(&lis3->mutex);
146 if (lis3_dev.whoami == WAI_12B)
147 selftest = CTRL1_ST;
148 else
149 selftest = CTRL1_STP;
150
151 lis3->read(lis3, CTRL_REG1, &reg);
152 lis3->write(lis3, CTRL_REG1, (reg | selftest));
153 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
154
155 /* Read directly to avoid axis remap */
156 x = lis3->read_data(lis3, OUTX);
157 y = lis3->read_data(lis3, OUTY);
158 z = lis3->read_data(lis3, OUTZ);
159
160 /* back to normal settings */
161 lis3->write(lis3, CTRL_REG1, reg);
162 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
163
164 results[0] = x - lis3->read_data(lis3, OUTX);
165 results[1] = y - lis3->read_data(lis3, OUTY);
166 results[2] = z - lis3->read_data(lis3, OUTZ);
167
168 ret = 0;
169 if (lis3->pdata) {
170 int i;
171 for (i = 0; i < 3; i++) {
172 /* Check against selftest acceptance limits */
173 if ((results[i] < lis3->pdata->st_min_limits[i]) ||
174 (results[i] > lis3->pdata->st_max_limits[i])) {
175 ret = -EIO;
176 goto fail;
177 }
178 }
179 }
180
181 /* test passed */
182fail:
183 mutex_unlock(&lis3->mutex);
184 return ret;
185}
186
Daniel Macka38da2e2009-03-31 15:24:32 -0700187void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800188{
Eric Piela002ee82009-06-16 15:34:14 -0700189 /* disable X,Y,Z axis and power down */
190 lis3->write(lis3, CTRL_REG1, 0x00);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800191}
Eric Pielcfce41a2009-01-09 16:41:01 -0800192EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800193
Daniel Macka38da2e2009-03-31 15:24:32 -0700194void lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800195{
Eric Piela002ee82009-06-16 15:34:14 -0700196 u8 reg;
197
198 lis3->init(lis3);
199
Samu Onkalo641615a2009-12-14 18:01:41 -0800200 /* LIS3 power on delay is quite long */
201 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
202
Eric Piela002ee82009-06-16 15:34:14 -0700203 /*
204 * Common configuration
Éric Piel4b5d95b2009-12-14 18:01:40 -0800205 * BDU: (12 bits sensors only) LSB and MSB values are not updated until
206 * both have been read. So the value read will always be correct.
Eric Piela002ee82009-06-16 15:34:14 -0700207 */
Éric Piel4b5d95b2009-12-14 18:01:40 -0800208 if (lis3->whoami == WAI_12B) {
209 lis3->read(lis3, CTRL_REG2, &reg);
210 reg |= CTRL2_BDU;
211 lis3->write(lis3, CTRL_REG2, reg);
212 }
Pavel Machek455fbdd2008-11-12 13:27:02 -0800213}
Eric Pielcfce41a2009-01-09 16:41:01 -0800214EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800215
Pavel Machek455fbdd2008-11-12 13:27:02 -0800216
Pavel Machekef2cfc72009-02-18 14:48:23 -0800217static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
218{
219 /*
220 * Be careful: on some HP laptops the bios force DD when on battery and
221 * the lid is closed. This leads to interrupts as soon as a little move
222 * is done.
223 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700224 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800225
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700226 wake_up_interruptible(&lis3_dev.misc_wait);
227 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800228 return IRQ_HANDLED;
229}
230
231static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
232{
233 int ret;
234
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700235 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800236 return -EBUSY; /* already open */
237
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700238 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800239
240 /*
241 * The sensor can generate interrupts for free-fall and direction
242 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
243 * the things simple and _fast_ we activate it only for free-fall, so
244 * no need to read register (very slow with ACPI). For the same reason,
245 * we forbid shared interrupts.
246 *
247 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
248 * io-apic is not configurable (and generates a warning) but I keep it
249 * in case of support for other hardware.
250 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700251 ret = request_irq(lis3_dev.irq, lis302dl_interrupt, IRQF_TRIGGER_RISING,
252 DRIVER_NAME, &lis3_dev);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800253
254 if (ret) {
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700255 clear_bit(0, &lis3_dev.misc_opened);
256 printk(KERN_ERR DRIVER_NAME ": IRQ%d allocation failed\n", lis3_dev.irq);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800257 return -EBUSY;
258 }
Pavel Machekef2cfc72009-02-18 14:48:23 -0800259 return 0;
260}
261
262static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
263{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700264 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700265 free_irq(lis3_dev.irq, &lis3_dev);
266 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Pavel Machekef2cfc72009-02-18 14:48:23 -0800267 return 0;
268}
269
270static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
271 size_t count, loff_t *pos)
272{
273 DECLARE_WAITQUEUE(wait, current);
274 u32 data;
275 unsigned char byte_data;
276 ssize_t retval = 1;
277
278 if (count < 1)
279 return -EINVAL;
280
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700281 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800282 while (true) {
283 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700284 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800285 if (data)
286 break;
287
288 if (file->f_flags & O_NONBLOCK) {
289 retval = -EAGAIN;
290 goto out;
291 }
292
293 if (signal_pending(current)) {
294 retval = -ERESTARTSYS;
295 goto out;
296 }
297
298 schedule();
299 }
300
301 if (data < 255)
302 byte_data = data;
303 else
304 byte_data = 255;
305
306 /* make sure we are not going into copy_to_user() with
307 * TASK_INTERRUPTIBLE state */
308 set_current_state(TASK_RUNNING);
309 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
310 retval = -EFAULT;
311
312out:
313 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700314 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800315
316 return retval;
317}
318
319static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
320{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700321 poll_wait(file, &lis3_dev.misc_wait, wait);
322 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800323 return POLLIN | POLLRDNORM;
324 return 0;
325}
326
327static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
328{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700329 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800330}
331
332static const struct file_operations lis3lv02d_misc_fops = {
333 .owner = THIS_MODULE,
334 .llseek = no_llseek,
335 .read = lis3lv02d_misc_read,
336 .open = lis3lv02d_misc_open,
337 .release = lis3lv02d_misc_release,
338 .poll = lis3lv02d_misc_poll,
339 .fasync = lis3lv02d_misc_fasync,
340};
341
342static struct miscdevice lis3lv02d_misc_device = {
343 .minor = MISC_DYNAMIC_MINOR,
344 .name = "freefall",
345 .fops = &lis3lv02d_misc_fops,
346};
347
Eric Pieldc6ea972009-06-16 15:34:15 -0700348static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800349{
350 int x, y, z;
351
Eric Pieldc6ea972009-06-16 15:34:15 -0700352 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Samu Onkalo53995412009-12-14 18:01:43 -0800353 input_report_abs(pidev->input, ABS_X, x);
354 input_report_abs(pidev->input, ABS_Y, y);
355 input_report_abs(pidev->input, ABS_Z, z);
Samu Onkalod25a8c82009-12-14 18:01:38 -0800356 input_sync(pidev->input);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800357}
358
Eric Pielcfce41a2009-01-09 16:41:01 -0800359int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800360{
Eric Pieldc6ea972009-06-16 15:34:15 -0700361 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800362 int err;
363
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700364 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800365 return -EINVAL;
366
Eric Pieldc6ea972009-06-16 15:34:15 -0700367 lis3_dev.idev = input_allocate_polled_device();
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700368 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800369 return -ENOMEM;
370
Eric Pieldc6ea972009-06-16 15:34:15 -0700371 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
372 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
373 input_dev = lis3_dev.idev->input;
374
Eric Pieldc6ea972009-06-16 15:34:15 -0700375 input_dev->name = "ST LIS3LV02DL Accelerometer";
376 input_dev->phys = DRIVER_NAME "/input0";
377 input_dev->id.bustype = BUS_HOST;
378 input_dev->id.vendor = 0;
379 input_dev->dev.parent = &lis3_dev.pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800380
Eric Pieldc6ea972009-06-16 15:34:15 -0700381 set_bit(EV_ABS, input_dev->evbit);
382 input_set_abs_params(input_dev, ABS_X, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
383 input_set_abs_params(input_dev, ABS_Y, -lis3_dev.mdps_max_val, lis3_dev.mdps_max_val, 3, 3);
384 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 -0800385
Eric Pieldc6ea972009-06-16 15:34:15 -0700386 err = input_register_polled_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800387 if (err) {
Eric Pieldc6ea972009-06-16 15:34:15 -0700388 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700389 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800390 }
391
392 return err;
393}
Eric Pielcfce41a2009-01-09 16:41:01 -0800394EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800395
Eric Pielcfce41a2009-01-09 16:41:01 -0800396void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800397{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700398 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800399 return;
400
Eric Pielc2884242009-06-16 15:34:13 -0700401 if (lis3_dev.irq)
402 misc_deregister(&lis3lv02d_misc_device);
Eric Pieldc6ea972009-06-16 15:34:15 -0700403 input_unregister_polled_device(lis3_dev.idev);
Samu Onkalo66c85692009-12-14 18:01:39 -0800404 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700405 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800406}
Eric Pielcfce41a2009-01-09 16:41:01 -0800407EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800408
Pavel Machek455fbdd2008-11-12 13:27:02 -0800409/* Sysfs stuff */
Samu Onkalo2db4a762009-12-14 18:01:43 -0800410static ssize_t lis3lv02d_selftest_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
412{
413 int result;
414 s16 values[3];
415
416 result = lis3lv02d_selftest(&lis3_dev, values);
417 return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
418 values[0], values[1], values[2]);
419}
420
Pavel Machek455fbdd2008-11-12 13:27:02 -0800421static ssize_t lis3lv02d_position_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
423{
424 int x, y, z;
425
Daniel Macka38da2e2009-03-31 15:24:32 -0700426 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800427 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
428}
429
Pavel Machek455fbdd2008-11-12 13:27:02 -0800430static ssize_t lis3lv02d_rate_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
432{
Samu Onkalo641615a2009-12-14 18:01:41 -0800433 return sprintf(buf, "%d\n", lis3lv02d_get_odr());
Pavel Machek455fbdd2008-11-12 13:27:02 -0800434}
435
Samu Onkalo2db4a762009-12-14 18:01:43 -0800436static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800437static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800438static DEVICE_ATTR(rate, S_IRUGO, lis3lv02d_rate_show, NULL);
439
440static struct attribute *lis3lv02d_attributes[] = {
Samu Onkalo2db4a762009-12-14 18:01:43 -0800441 &dev_attr_selftest.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800442 &dev_attr_position.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800443 &dev_attr_rate.attr,
444 NULL
445};
446
447static struct attribute_group lis3lv02d_attribute_group = {
448 .attrs = lis3lv02d_attributes
449};
450
Eric Pielcfce41a2009-01-09 16:41:01 -0800451
Daniel Macka38da2e2009-03-31 15:24:32 -0700452static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800453{
Eric Piela002ee82009-06-16 15:34:14 -0700454 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
455 if (IS_ERR(lis3->pdev))
456 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800457
Eric Piela002ee82009-06-16 15:34:14 -0700458 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800459}
460
Eric Piela002ee82009-06-16 15:34:14 -0700461int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800462{
Eric Piela002ee82009-06-16 15:34:14 -0700463 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
464 platform_device_unregister(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800465 return 0;
466}
Eric Pielcfce41a2009-01-09 16:41:01 -0800467EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800468
Daniel Mackab337a62009-03-31 15:24:31 -0700469/*
470 * Initialise the accelerometer and the various subsystems.
Éric Pielbc62c142009-12-14 18:01:39 -0800471 * Should be rather independent of the bus system.
Daniel Mackab337a62009-03-31 15:24:31 -0700472 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700473int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700474{
Daniel Macka38da2e2009-03-31 15:24:32 -0700475 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
476
477 switch (dev->whoami) {
Éric Pielbc62c142009-12-14 18:01:39 -0800478 case WAI_12B:
479 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
480 dev->read_data = lis3lv02d_read_12;
Daniel Macka38da2e2009-03-31 15:24:32 -0700481 dev->mdps_max_val = 2048;
Samu Onkalo641615a2009-12-14 18:01:41 -0800482 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700483 break;
Éric Pielbc62c142009-12-14 18:01:39 -0800484 case WAI_8B:
485 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
Daniel Macka38da2e2009-03-31 15:24:32 -0700486 dev->read_data = lis3lv02d_read_8;
487 dev->mdps_max_val = 128;
Samu Onkalo641615a2009-12-14 18:01:41 -0800488 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
Daniel Macka38da2e2009-03-31 15:24:32 -0700489 break;
490 default:
491 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700492 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700493 return -EINVAL;
494 }
495
Samu Onkalo2db4a762009-12-14 18:01:43 -0800496 mutex_init(&dev->mutex);
497
Daniel Macka38da2e2009-03-31 15:24:32 -0700498 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700499 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700500
501 if (lis3lv02d_joystick_enable())
502 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
503
Daniel Mack8f3128e2009-06-16 15:34:17 -0700504 /* passing in platform specific data is purely optional and only
505 * used by the SPI transport layer at the moment */
506 if (dev->pdata) {
507 struct lis3lv02d_platform_data *p = dev->pdata;
508
Éric Pielbc62c142009-12-14 18:01:39 -0800509 if (p->click_flags && (dev->whoami == WAI_8B)) {
Daniel Mack8f3128e2009-06-16 15:34:17 -0700510 dev->write(dev, CLICK_CFG, p->click_flags);
511 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
512 dev->write(dev, CLICK_LATENCY, p->click_latency);
513 dev->write(dev, CLICK_WINDOW, p->click_window);
514 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
515 dev->write(dev, CLICK_THSY_X,
516 (p->click_thresh_x & 0xf) |
517 (p->click_thresh_y << 4));
518 }
519
Éric Pielbc62c142009-12-14 18:01:39 -0800520 if (p->wakeup_flags && (dev->whoami == WAI_8B)) {
Daniel Mack8873c332009-09-21 17:04:43 -0700521 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
522 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
523 /* default to 2.5ms for now */
524 dev->write(dev, FF_WU_DURATION_1, 1);
525 /* enable high pass filter for both free-fall units */
526 dev->write(dev, CTRL_REG2, HP_FF_WU1 | HP_FF_WU2);
527 }
528
Daniel Mack8f3128e2009-06-16 15:34:17 -0700529 if (p->irq_cfg)
530 dev->write(dev, CTRL_REG3, p->irq_cfg);
531 }
532
Daniel Macka38da2e2009-03-31 15:24:32 -0700533 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700534 if (!dev->irq) {
535 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700536 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700537 goto out;
538 }
539
Daniel Mackab337a62009-03-31 15:24:31 -0700540 if (misc_register(&lis3lv02d_misc_device))
541 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
542out:
Daniel Mackab337a62009-03-31 15:24:31 -0700543 return 0;
544}
545EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
546
Pavel Machek455fbdd2008-11-12 13:27:02 -0800547MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800548MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800549MODULE_LICENSE("GPL");