blob: 3109eb8648ce6c07aaefaae130f23dfd21ac3acc [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>
Samu Onkalof9deb412010-10-22 07:57:24 -040034#include <linux/slab.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080035#include <linux/freezer.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080036#include <linux/uaccess.h>
Pavel Machekef2cfc72009-02-18 14:48:23 -080037#include <linux/miscdevice.h>
Samu Onkalo2a346992010-10-22 07:57:23 -040038#include <linux/pm_runtime.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080039#include <asm/atomic.h>
40#include "lis3lv02d.h"
41
42#define DRIVER_NAME "lis3lv02d"
Pavel Machek455fbdd2008-11-12 13:27:02 -080043
44/* joystick device poll interval in milliseconds */
45#define MDPS_POLL_INTERVAL 50
Samu Onkalo4a70a412010-05-24 14:33:37 -070046#define MDPS_POLL_MIN 0
47#define MDPS_POLL_MAX 2000
Samu Onkalo2a346992010-10-22 07:57:23 -040048
49#define LIS3_SYSFS_POWERDOWN_DELAY 5000 /* In milliseconds */
50
Pavel Machek455fbdd2008-11-12 13:27:02 -080051/*
52 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
Éric Pielbc62c142009-12-14 18:01:39 -080053 * because they are generated even if the data do not change. So it's better
Pavel Machek455fbdd2008-11-12 13:27:02 -080054 * 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
57 * joystick.
58 */
59
Samu Onkalo641615a2009-12-14 18:01:41 -080060#define LIS3_PWRON_DELAY_WAI_12B (5000)
61#define LIS3_PWRON_DELAY_WAI_8B (3000)
62
Samu Onkalo32496c72009-12-14 18:01:46 -080063/*
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.
68 */
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)
73
74#define LIS3_DEFAULT_FUZZ 3
75#define LIS3_DEFAULT_FLAT 3
76
Daniel Macka38da2e2009-03-31 15:24:32 -070077struct lis3lv02d lis3_dev = {
Pavel Machekbe84cfc2009-03-31 15:24:26 -070078 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
Pavel Machekef2cfc72009-02-18 14:48:23 -080079};
80
Pavel Machekbe84cfc2009-03-31 15:24:26 -070081EXPORT_SYMBOL_GPL(lis3_dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080082
Takashi Iwai2ee32142010-10-01 17:14:25 -040083/* just like param_set_int() but does sanity-check so that it won't point
84 * over the axis array size
85 */
86static int param_set_axis(const char *val, const struct kernel_param *kp)
87{
88 int ret = param_set_int(val, kp);
89 if (!ret) {
90 int val = *(int *)kp->arg;
91 if (val < 0)
92 val = -val;
93 if (!val || val > 3)
94 return -EINVAL;
95 }
96 return ret;
97}
98
99static struct kernel_param_ops param_ops_axis = {
100 .set = param_set_axis,
101 .get = param_get_int,
102};
103
104module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
105MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
106
Daniel Macka38da2e2009-03-31 15:24:32 -0700107static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
108{
109 s8 lo;
110 if (lis3->read(lis3, reg, &lo) < 0)
111 return 0;
112
113 return lo;
114}
115
Éric Pielbc62c142009-12-14 18:01:39 -0800116static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700117{
118 u8 lo, hi;
119
Daniel Macka38da2e2009-03-31 15:24:32 -0700120 lis3->read(lis3, reg - 1, &lo);
121 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700122 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
123 return (s16)((hi << 8) | lo);
124}
125
Pavel Machek455fbdd2008-11-12 13:27:02 -0800126/**
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
130 *
131 * Returns the converted value.
132 */
133static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
134{
135 if (axis > 0)
136 return hw_values[axis - 1];
137 else
138 return -hw_values[-axis - 1];
139}
140
141/**
142 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -0700143 * @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
Pavel Machek455fbdd2008-11-12 13:27:02 -0800147 *
148 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
149 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700150static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800151{
152 int position[3];
Samu Onkalo32496c72009-12-14 18:01:46 -0800153 int i;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800154
Eric Piela002ee82009-06-16 15:34:14 -0700155 position[0] = lis3->read_data(lis3, OUTX);
156 position[1] = lis3->read_data(lis3, OUTY);
157 position[2] = lis3->read_data(lis3, OUTZ);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800158
Samu Onkalo32496c72009-12-14 18:01:46 -0800159 for (i = 0; i < 3; i++)
160 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
161
Eric Piela002ee82009-06-16 15:34:14 -0700162 *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);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800165}
166
Samu Onkalo641615a2009-12-14 18:01:41 -0800167/* conversion btw sampling rate and the register values */
168static int lis3_12_rates[4] = {40, 160, 640, 2560};
169static int lis3_8_rates[2] = {100, 400};
Takashi Iwai78537c32010-09-23 10:01:39 -0700170static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
Samu Onkalo641615a2009-12-14 18:01:41 -0800171
Samu Onkaloa253aae2009-12-14 18:01:44 -0800172/* ODR is Output Data Rate */
Samu Onkalo641615a2009-12-14 18:01:41 -0800173static int lis3lv02d_get_odr(void)
174{
175 u8 ctrl;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800176 int shift;
Samu Onkalo641615a2009-12-14 18:01:41 -0800177
178 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800179 ctrl &= lis3_dev.odr_mask;
180 shift = ffs(lis3_dev.odr_mask) - 1;
181 return lis3_dev.odrs[(ctrl >> shift)];
182}
Samu Onkalo641615a2009-12-14 18:01:41 -0800183
Samu Onkaloa253aae2009-12-14 18:01:44 -0800184static int lis3lv02d_set_odr(int rate)
185{
186 u8 ctrl;
187 int i, len, shift;
188
Takashi Iwai78537c32010-09-23 10:01:39 -0700189 if (!rate)
190 return -EINVAL;
191
Samu Onkaloa253aae2009-12-14 18:01:44 -0800192 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;
196
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));
201 return 0;
202 }
203 return -EINVAL;
Samu Onkalo641615a2009-12-14 18:01:41 -0800204}
205
Samu Onkalo2db4a762009-12-14 18:01:43 -0800206static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
207{
Takashi Iwai78537c32010-09-23 10:01:39 -0700208 u8 ctlreg, reg;
Samu Onkalo2db4a762009-12-14 18:01:43 -0800209 s16 x, y, z;
210 u8 selftest;
211 int ret;
212
213 mutex_lock(&lis3->mutex);
Takashi Iwai78537c32010-09-23 10:01:39 -0700214 if (lis3_dev.whoami == WAI_3DC) {
215 ctlreg = CTRL_REG4;
216 selftest = CTRL4_ST0;
217 } else {
218 ctlreg = CTRL_REG1;
219 if (lis3_dev.whoami == WAI_12B)
220 selftest = CTRL1_ST;
221 else
222 selftest = CTRL1_STP;
223 }
Samu Onkalo2db4a762009-12-14 18:01:43 -0800224
Takashi Iwai78537c32010-09-23 10:01:39 -0700225 lis3->read(lis3, ctlreg, &reg);
226 lis3->write(lis3, ctlreg, (reg | selftest));
Samu Onkalo2db4a762009-12-14 18:01:43 -0800227 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
228
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);
233
234 /* back to normal settings */
Takashi Iwai78537c32010-09-23 10:01:39 -0700235 lis3->write(lis3, ctlreg, reg);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800236 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
237
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);
241
242 ret = 0;
243 if (lis3->pdata) {
244 int i;
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])) {
249 ret = -EIO;
250 goto fail;
251 }
252 }
253 }
254
255 /* test passed */
256fail:
257 mutex_unlock(&lis3->mutex);
258 return ret;
259}
260
Samu Onkalof9deb412010-10-22 07:57:24 -0400261/*
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
265 */
266static 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};
271
272static 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};
276
277static inline void lis3_context_save(struct lis3lv02d *lis3)
278{
279 int i;
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;
283}
284
285static inline void lis3_context_restore(struct lis3lv02d *lis3)
286{
287 int i;
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]);
291}
292
Daniel Macka38da2e2009-03-31 15:24:32 -0700293void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800294{
Samu Onkalof9deb412010-10-22 07:57:24 -0400295 if (lis3->reg_ctrl)
296 lis3_context_save(lis3);
Eric Piela002ee82009-06-16 15:34:14 -0700297 /* disable X,Y,Z axis and power down */
298 lis3->write(lis3, CTRL_REG1, 0x00);
Samu Onkalof9deb412010-10-22 07:57:24 -0400299 if (lis3->reg_ctrl)
300 lis3->reg_ctrl(lis3, LIS3_REG_OFF);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800301}
Eric Pielcfce41a2009-01-09 16:41:01 -0800302EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800303
Daniel Macka38da2e2009-03-31 15:24:32 -0700304void lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800305{
Eric Piela002ee82009-06-16 15:34:14 -0700306 u8 reg;
307
308 lis3->init(lis3);
309
310 /*
311 * Common configuration
Éric Piel4b5d95b2009-12-14 18:01:40 -0800312 * BDU: (12 bits sensors only) LSB and MSB values are not updated until
313 * both have been read. So the value read will always be correct.
Samu Onkalo2a7fade2010-10-22 07:57:27 -0400314 * Set BOOT bit to refresh factory tuning values.
Eric Piela002ee82009-06-16 15:34:14 -0700315 */
Samu Onkalo2a7fade2010-10-22 07:57:27 -0400316 lis3->read(lis3, CTRL_REG2, &reg);
317 if (lis3->whoami == WAI_12B)
318 reg |= CTRL2_BDU | CTRL2_BOOT;
319 else
320 reg |= CTRL2_BOOT_8B;
321 lis3->write(lis3, CTRL_REG2, reg);
322
323 /* LIS3 power on delay is quite long */
324 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
325
Samu Onkalof9deb412010-10-22 07:57:24 -0400326 if (lis3->reg_ctrl)
327 lis3_context_restore(lis3);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800328}
Eric Pielcfce41a2009-01-09 16:41:01 -0800329EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800330
Pavel Machek455fbdd2008-11-12 13:27:02 -0800331
Samu Onkalo6d94d402010-05-24 14:33:37 -0700332static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
333{
334 int x, y, z;
335
336 mutex_lock(&lis3_dev.mutex);
337 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
338 input_report_abs(pidev->input, ABS_X, x);
339 input_report_abs(pidev->input, ABS_Y, y);
340 input_report_abs(pidev->input, ABS_Z, z);
341 input_sync(pidev->input);
342 mutex_unlock(&lis3_dev.mutex);
343}
344
Samu Onkalo2a346992010-10-22 07:57:23 -0400345static void lis3lv02d_joystick_open(struct input_polled_dev *pidev)
346{
347 if (lis3_dev.pm_dev)
348 pm_runtime_get_sync(lis3_dev.pm_dev);
Samu Onkaloe7261112010-10-22 07:57:25 -0400349
350 if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev)
351 atomic_set(&lis3_dev.wake_thread, 1);
Samu Onkalo821f6642010-10-22 07:57:26 -0400352 /*
353 * Update coordinates for the case where poll interval is 0 and
354 * the chip in running purely under interrupt control
355 */
356 lis3lv02d_joystick_poll(pidev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400357}
358
359static void lis3lv02d_joystick_close(struct input_polled_dev *pidev)
360{
Samu Onkaloe7261112010-10-22 07:57:25 -0400361 atomic_set(&lis3_dev.wake_thread, 0);
Samu Onkalo2a346992010-10-22 07:57:23 -0400362 if (lis3_dev.pm_dev)
363 pm_runtime_put(lis3_dev.pm_dev);
364}
365
Pavel Machekef2cfc72009-02-18 14:48:23 -0800366static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
367{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700368 if (!test_bit(0, &lis3_dev.misc_opened))
369 goto out;
370
Pavel Machekef2cfc72009-02-18 14:48:23 -0800371 /*
372 * Be careful: on some HP laptops the bios force DD when on battery and
373 * the lid is closed. This leads to interrupts as soon as a little move
374 * is done.
375 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700376 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800377
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700378 wake_up_interruptible(&lis3_dev.misc_wait);
379 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700380out:
Samu Onkaloe7261112010-10-22 07:57:25 -0400381 if (atomic_read(&lis3_dev.wake_thread))
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700382 return IRQ_WAKE_THREAD;
383 return IRQ_HANDLED;
384}
385
Samu Onkalo6d94d402010-05-24 14:33:37 -0700386static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
387{
388 struct input_dev *dev = lis3->idev->input;
389 u8 click_src;
390
391 mutex_lock(&lis3->mutex);
392 lis3->read(lis3, CLICK_SRC, &click_src);
393
394 if (click_src & CLICK_SINGLE_X) {
395 input_report_key(dev, lis3->mapped_btns[0], 1);
396 input_report_key(dev, lis3->mapped_btns[0], 0);
397 }
398
399 if (click_src & CLICK_SINGLE_Y) {
400 input_report_key(dev, lis3->mapped_btns[1], 1);
401 input_report_key(dev, lis3->mapped_btns[1], 0);
402 }
403
404 if (click_src & CLICK_SINGLE_Z) {
405 input_report_key(dev, lis3->mapped_btns[2], 1);
406 input_report_key(dev, lis3->mapped_btns[2], 0);
407 }
408 input_sync(dev);
409 mutex_unlock(&lis3->mutex);
410}
411
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700412static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
413{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700414
415 struct lis3lv02d *lis3 = data;
416
Samu Onkaloe7261112010-10-22 07:57:25 -0400417 if ((lis3->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
Samu Onkalo6d94d402010-05-24 14:33:37 -0700418 lis302dl_interrupt_handle_click(lis3);
419 else
Samu Onkaloe7261112010-10-22 07:57:25 -0400420 lis3lv02d_joystick_poll(lis3->idev);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700421
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700422 return IRQ_HANDLED;
423}
424
425static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
426{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700427
428 struct lis3lv02d *lis3 = data;
429
Samu Onkaloe7261112010-10-22 07:57:25 -0400430 if ((lis3->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
Samu Onkalo6d94d402010-05-24 14:33:37 -0700431 lis302dl_interrupt_handle_click(lis3);
432 else
Samu Onkaloe7261112010-10-22 07:57:25 -0400433 lis3lv02d_joystick_poll(lis3->idev);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700434
Pavel Machekef2cfc72009-02-18 14:48:23 -0800435 return IRQ_HANDLED;
436}
437
438static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
439{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700440 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800441 return -EBUSY; /* already open */
442
Samu Onkalo2a346992010-10-22 07:57:23 -0400443 if (lis3_dev.pm_dev)
444 pm_runtime_get_sync(lis3_dev.pm_dev);
445
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700446 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800447 return 0;
448}
449
450static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
451{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700452 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700453 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Samu Onkalo2a346992010-10-22 07:57:23 -0400454 if (lis3_dev.pm_dev)
455 pm_runtime_put(lis3_dev.pm_dev);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800456 return 0;
457}
458
459static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
460 size_t count, loff_t *pos)
461{
462 DECLARE_WAITQUEUE(wait, current);
463 u32 data;
464 unsigned char byte_data;
465 ssize_t retval = 1;
466
467 if (count < 1)
468 return -EINVAL;
469
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700470 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800471 while (true) {
472 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700473 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800474 if (data)
475 break;
476
477 if (file->f_flags & O_NONBLOCK) {
478 retval = -EAGAIN;
479 goto out;
480 }
481
482 if (signal_pending(current)) {
483 retval = -ERESTARTSYS;
484 goto out;
485 }
486
487 schedule();
488 }
489
490 if (data < 255)
491 byte_data = data;
492 else
493 byte_data = 255;
494
495 /* make sure we are not going into copy_to_user() with
496 * TASK_INTERRUPTIBLE state */
497 set_current_state(TASK_RUNNING);
498 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
499 retval = -EFAULT;
500
501out:
502 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700503 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800504
505 return retval;
506}
507
508static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
509{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700510 poll_wait(file, &lis3_dev.misc_wait, wait);
511 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800512 return POLLIN | POLLRDNORM;
513 return 0;
514}
515
516static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
517{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700518 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800519}
520
521static const struct file_operations lis3lv02d_misc_fops = {
522 .owner = THIS_MODULE,
523 .llseek = no_llseek,
524 .read = lis3lv02d_misc_read,
525 .open = lis3lv02d_misc_open,
526 .release = lis3lv02d_misc_release,
527 .poll = lis3lv02d_misc_poll,
528 .fasync = lis3lv02d_misc_fasync,
529};
530
531static struct miscdevice lis3lv02d_misc_device = {
532 .minor = MISC_DYNAMIC_MINOR,
533 .name = "freefall",
534 .fops = &lis3lv02d_misc_fops,
535};
536
Eric Pielcfce41a2009-01-09 16:41:01 -0800537int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800538{
Eric Pieldc6ea972009-06-16 15:34:15 -0700539 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800540 int err;
Samu Onkalo32496c72009-12-14 18:01:46 -0800541 int max_val, fuzz, flat;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700542 int btns[] = {BTN_X, BTN_Y, BTN_Z};
Pavel Machek455fbdd2008-11-12 13:27:02 -0800543
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700544 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800545 return -EINVAL;
546
Eric Pieldc6ea972009-06-16 15:34:15 -0700547 lis3_dev.idev = input_allocate_polled_device();
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700548 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800549 return -ENOMEM;
550
Eric Pieldc6ea972009-06-16 15:34:15 -0700551 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
Samu Onkalo2a346992010-10-22 07:57:23 -0400552 lis3_dev.idev->open = lis3lv02d_joystick_open;
553 lis3_dev.idev->close = lis3lv02d_joystick_close;
Eric Pieldc6ea972009-06-16 15:34:15 -0700554 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
Samu Onkalo4a70a412010-05-24 14:33:37 -0700555 lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN;
556 lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX;
Eric Pieldc6ea972009-06-16 15:34:15 -0700557 input_dev = lis3_dev.idev->input;
558
Eric Pieldc6ea972009-06-16 15:34:15 -0700559 input_dev->name = "ST LIS3LV02DL Accelerometer";
560 input_dev->phys = DRIVER_NAME "/input0";
561 input_dev->id.bustype = BUS_HOST;
562 input_dev->id.vendor = 0;
563 input_dev->dev.parent = &lis3_dev.pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800564
Eric Pieldc6ea972009-06-16 15:34:15 -0700565 set_bit(EV_ABS, input_dev->evbit);
Samu Onkalo32496c72009-12-14 18:01:46 -0800566 max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
567 fuzz = (LIS3_DEFAULT_FUZZ * lis3_dev.scale) / LIS3_ACCURACY;
568 flat = (LIS3_DEFAULT_FLAT * lis3_dev.scale) / LIS3_ACCURACY;
569 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
570 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
571 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800572
Samu Onkalo6d94d402010-05-24 14:33:37 -0700573 lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
574 lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
575 lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
576
Eric Pieldc6ea972009-06-16 15:34:15 -0700577 err = input_register_polled_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800578 if (err) {
Eric Pieldc6ea972009-06-16 15:34:15 -0700579 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700580 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800581 }
582
583 return err;
584}
Eric Pielcfce41a2009-01-09 16:41:01 -0800585EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800586
Eric Pielcfce41a2009-01-09 16:41:01 -0800587void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800588{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700589 if (lis3_dev.irq)
590 free_irq(lis3_dev.irq, &lis3_dev);
591 if (lis3_dev.pdata && lis3_dev.pdata->irq2)
592 free_irq(lis3_dev.pdata->irq2, &lis3_dev);
593
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700594 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800595 return;
596
Eric Pielc2884242009-06-16 15:34:13 -0700597 if (lis3_dev.irq)
598 misc_deregister(&lis3lv02d_misc_device);
Eric Pieldc6ea972009-06-16 15:34:15 -0700599 input_unregister_polled_device(lis3_dev.idev);
Samu Onkalo66c85692009-12-14 18:01:39 -0800600 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700601 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800602}
Eric Pielcfce41a2009-01-09 16:41:01 -0800603EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800604
Pavel Machek455fbdd2008-11-12 13:27:02 -0800605/* Sysfs stuff */
Samu Onkalo2a346992010-10-22 07:57:23 -0400606static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
607{
608 /*
609 * SYSFS functions are fast visitors so put-call
610 * immediately after the get-call. However, keep
611 * chip running for a while and schedule delayed
612 * suspend. This way periodic sysfs calls doesn't
613 * suffer from relatively long power up time.
614 */
615
616 if (lis3->pm_dev) {
617 pm_runtime_get_sync(lis3->pm_dev);
618 pm_runtime_put_noidle(lis3->pm_dev);
619 pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
620 }
621}
622
Samu Onkalo2db4a762009-12-14 18:01:43 -0800623static ssize_t lis3lv02d_selftest_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
625{
626 int result;
627 s16 values[3];
628
Samu Onkalo2a346992010-10-22 07:57:23 -0400629 lis3lv02d_sysfs_poweron(&lis3_dev);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800630 result = lis3lv02d_selftest(&lis3_dev, values);
631 return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
632 values[0], values[1], values[2]);
633}
634
Pavel Machek455fbdd2008-11-12 13:27:02 -0800635static ssize_t lis3lv02d_position_show(struct device *dev,
636 struct device_attribute *attr, char *buf)
637{
638 int x, y, z;
639
Samu Onkalo2a346992010-10-22 07:57:23 -0400640 lis3lv02d_sysfs_poweron(&lis3_dev);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700641 mutex_lock(&lis3_dev.mutex);
Daniel Macka38da2e2009-03-31 15:24:32 -0700642 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700643 mutex_unlock(&lis3_dev.mutex);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800644 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
645}
646
Pavel Machek455fbdd2008-11-12 13:27:02 -0800647static ssize_t lis3lv02d_rate_show(struct device *dev,
648 struct device_attribute *attr, char *buf)
649{
Samu Onkalo2a346992010-10-22 07:57:23 -0400650 lis3lv02d_sysfs_poweron(&lis3_dev);
Samu Onkalo641615a2009-12-14 18:01:41 -0800651 return sprintf(buf, "%d\n", lis3lv02d_get_odr());
Pavel Machek455fbdd2008-11-12 13:27:02 -0800652}
653
Samu Onkaloa253aae2009-12-14 18:01:44 -0800654static ssize_t lis3lv02d_rate_set(struct device *dev,
655 struct device_attribute *attr, const char *buf,
656 size_t count)
657{
658 unsigned long rate;
659
660 if (strict_strtoul(buf, 0, &rate))
661 return -EINVAL;
662
Samu Onkalo2a346992010-10-22 07:57:23 -0400663 lis3lv02d_sysfs_poweron(&lis3_dev);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800664 if (lis3lv02d_set_odr(rate))
665 return -EINVAL;
666
667 return count;
668}
669
Samu Onkalo2db4a762009-12-14 18:01:43 -0800670static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800671static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800672static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
673 lis3lv02d_rate_set);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800674
675static struct attribute *lis3lv02d_attributes[] = {
Samu Onkalo2db4a762009-12-14 18:01:43 -0800676 &dev_attr_selftest.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800677 &dev_attr_position.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800678 &dev_attr_rate.attr,
679 NULL
680};
681
682static struct attribute_group lis3lv02d_attribute_group = {
683 .attrs = lis3lv02d_attributes
684};
685
Eric Pielcfce41a2009-01-09 16:41:01 -0800686
Daniel Macka38da2e2009-03-31 15:24:32 -0700687static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800688{
Eric Piela002ee82009-06-16 15:34:14 -0700689 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
690 if (IS_ERR(lis3->pdev))
691 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800692
Eric Piela002ee82009-06-16 15:34:14 -0700693 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800694}
695
Eric Piela002ee82009-06-16 15:34:14 -0700696int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800697{
Eric Piela002ee82009-06-16 15:34:14 -0700698 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
699 platform_device_unregister(lis3->pdev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400700 if (lis3->pm_dev) {
701 /* Barrier after the sysfs remove */
702 pm_runtime_barrier(lis3->pm_dev);
703
704 /* SYSFS may have left chip running. Turn off if necessary */
705 if (!pm_runtime_suspended(lis3->pm_dev))
706 lis3lv02d_poweroff(&lis3_dev);
707
708 pm_runtime_disable(lis3->pm_dev);
709 pm_runtime_set_suspended(lis3->pm_dev);
710 }
Samu Onkalof9deb412010-10-22 07:57:24 -0400711 kfree(lis3->reg_cache);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800712 return 0;
713}
Eric Pielcfce41a2009-01-09 16:41:01 -0800714EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800715
Samu Onkaloecc437a2010-05-24 14:33:34 -0700716static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
717 struct lis3lv02d_platform_data *p)
718{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700719 int err;
Samu Onkalo342c5f12010-05-24 14:33:35 -0700720 int ctrl2 = p->hipass_ctrl;
721
Samu Onkaloecc437a2010-05-24 14:33:34 -0700722 if (p->click_flags) {
723 dev->write(dev, CLICK_CFG, p->click_flags);
724 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
725 dev->write(dev, CLICK_LATENCY, p->click_latency);
726 dev->write(dev, CLICK_WINDOW, p->click_window);
727 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
728 dev->write(dev, CLICK_THSY_X,
729 (p->click_thresh_x & 0xf) |
730 (p->click_thresh_y << 4));
Samu Onkalo6d94d402010-05-24 14:33:37 -0700731
732 if (dev->idev) {
733 struct input_dev *input_dev = lis3_dev.idev->input;
734 input_set_capability(input_dev, EV_KEY, BTN_X);
735 input_set_capability(input_dev, EV_KEY, BTN_Y);
736 input_set_capability(input_dev, EV_KEY, BTN_Z);
737 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700738 }
739
740 if (p->wakeup_flags) {
741 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
742 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
Samu Onkalocc23aa12010-10-22 07:57:29 -0400743 /* pdata value + 1 to keep this backward compatible*/
744 dev->write(dev, FF_WU_DURATION_1, p->duration1 + 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700745 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
Samu Onkaloecc437a2010-05-24 14:33:34 -0700746 }
Samu Onkalo342c5f12010-05-24 14:33:35 -0700747
748 if (p->wakeup_flags2) {
749 dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
750 dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
Samu Onkalocc23aa12010-10-22 07:57:29 -0400751 /* pdata value + 1 to keep this backward compatible*/
752 dev->write(dev, FF_WU_DURATION_2, p->duration2 + 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700753 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
754 }
755 /* Configure hipass filters */
756 dev->write(dev, CTRL_REG2, ctrl2);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700757
758 if (p->irq2) {
759 err = request_threaded_irq(p->irq2,
760 NULL,
761 lis302dl_interrupt_thread2_8b,
Samu Onkalocc23aa12010-10-22 07:57:29 -0400762 IRQF_TRIGGER_RISING | IRQF_ONESHOT |
763 (p->irq_flags2 & IRQF_TRIGGER_MASK),
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700764 DRIVER_NAME, &lis3_dev);
765 if (err < 0)
766 printk(KERN_ERR DRIVER_NAME
767 "No second IRQ. Limited functionality\n");
768 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700769}
770
Daniel Mackab337a62009-03-31 15:24:31 -0700771/*
772 * Initialise the accelerometer and the various subsystems.
Éric Pielbc62c142009-12-14 18:01:39 -0800773 * Should be rather independent of the bus system.
Daniel Mackab337a62009-03-31 15:24:31 -0700774 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700775int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700776{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700777 int err;
778 irq_handler_t thread_fn;
Samu Onkalocc23aa12010-10-22 07:57:29 -0400779 int irq_flags = 0;
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700780
Daniel Macka38da2e2009-03-31 15:24:32 -0700781 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
782
783 switch (dev->whoami) {
Éric Pielbc62c142009-12-14 18:01:39 -0800784 case WAI_12B:
785 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
786 dev->read_data = lis3lv02d_read_12;
Daniel Macka38da2e2009-03-31 15:24:32 -0700787 dev->mdps_max_val = 2048;
Samu Onkalo641615a2009-12-14 18:01:41 -0800788 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800789 dev->odrs = lis3_12_rates;
790 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
Samu Onkalo32496c72009-12-14 18:01:46 -0800791 dev->scale = LIS3_SENSITIVITY_12B;
Samu Onkalof9deb412010-10-22 07:57:24 -0400792 dev->regs = lis3_wai12_regs;
793 dev->regs_size = ARRAY_SIZE(lis3_wai12_regs);
Daniel Macka38da2e2009-03-31 15:24:32 -0700794 break;
Éric Pielbc62c142009-12-14 18:01:39 -0800795 case WAI_8B:
796 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
Daniel Macka38da2e2009-03-31 15:24:32 -0700797 dev->read_data = lis3lv02d_read_8;
798 dev->mdps_max_val = 128;
Samu Onkalo641615a2009-12-14 18:01:41 -0800799 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800800 dev->odrs = lis3_8_rates;
801 dev->odr_mask = CTRL1_DR;
Samu Onkalo32496c72009-12-14 18:01:46 -0800802 dev->scale = LIS3_SENSITIVITY_8B;
Samu Onkalof9deb412010-10-22 07:57:24 -0400803 dev->regs = lis3_wai8_regs;
804 dev->regs_size = ARRAY_SIZE(lis3_wai8_regs);
Daniel Macka38da2e2009-03-31 15:24:32 -0700805 break;
Takashi Iwai78537c32010-09-23 10:01:39 -0700806 case WAI_3DC:
807 printk(KERN_INFO DRIVER_NAME ": 8 bits 3DC sensor found\n");
808 dev->read_data = lis3lv02d_read_8;
809 dev->mdps_max_val = 128;
810 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
811 dev->odrs = lis3_3dc_rates;
812 dev->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
813 dev->scale = LIS3_SENSITIVITY_8B;
814 break;
Daniel Macka38da2e2009-03-31 15:24:32 -0700815 default:
816 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700817 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700818 return -EINVAL;
819 }
820
Samu Onkalof9deb412010-10-22 07:57:24 -0400821 dev->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
822 sizeof(lis3_wai12_regs)), GFP_KERNEL);
823
824 if (dev->reg_cache == NULL) {
825 printk(KERN_ERR DRIVER_NAME "out of memory\n");
826 return -ENOMEM;
827 }
828
Samu Onkalo2db4a762009-12-14 18:01:43 -0800829 mutex_init(&dev->mutex);
Samu Onkaloe7261112010-10-22 07:57:25 -0400830 atomic_set(&dev->wake_thread, 0);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800831
Daniel Macka38da2e2009-03-31 15:24:32 -0700832 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700833 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700834
Samu Onkalo2a346992010-10-22 07:57:23 -0400835 if (dev->pm_dev) {
836 pm_runtime_set_active(dev->pm_dev);
837 pm_runtime_enable(dev->pm_dev);
838 }
839
Daniel Mackab337a62009-03-31 15:24:31 -0700840 if (lis3lv02d_joystick_enable())
841 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
842
Daniel Mack8f3128e2009-06-16 15:34:17 -0700843 /* passing in platform specific data is purely optional and only
844 * used by the SPI transport layer at the moment */
845 if (dev->pdata) {
846 struct lis3lv02d_platform_data *p = dev->pdata;
847
Samu Onkaloecc437a2010-05-24 14:33:34 -0700848 if (dev->whoami == WAI_8B)
849 lis3lv02d_8b_configure(dev, p);
Daniel Mack8873c332009-09-21 17:04:43 -0700850
Samu Onkalocc23aa12010-10-22 07:57:29 -0400851 irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK;
852
Samu Onkaloe7261112010-10-22 07:57:25 -0400853 dev->irq_cfg = p->irq_cfg;
Daniel Mack8f3128e2009-06-16 15:34:17 -0700854 if (p->irq_cfg)
855 dev->write(dev, CTRL_REG3, p->irq_cfg);
Samu Onkalocc23aa12010-10-22 07:57:29 -0400856
857 if (p->default_rate)
858 lis3lv02d_set_odr(p->default_rate);
Daniel Mack8f3128e2009-06-16 15:34:17 -0700859 }
860
Daniel Macka38da2e2009-03-31 15:24:32 -0700861 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700862 if (!dev->irq) {
863 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700864 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700865 goto out;
866 }
867
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700868 /*
869 * The sensor can generate interrupts for free-fall and direction
870 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
871 * the things simple and _fast_ we activate it only for free-fall, so
872 * no need to read register (very slow with ACPI). For the same reason,
873 * we forbid shared interrupts.
874 *
875 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
876 * io-apic is not configurable (and generates a warning) but I keep it
877 * in case of support for other hardware.
878 */
Takashi Iwaif7c77a32010-09-23 10:01:11 -0700879 if (dev->pdata && dev->whoami == WAI_8B)
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700880 thread_fn = lis302dl_interrupt_thread1_8b;
881 else
882 thread_fn = NULL;
883
884 err = request_threaded_irq(dev->irq, lis302dl_interrupt,
885 thread_fn,
Samu Onkalocc23aa12010-10-22 07:57:29 -0400886 IRQF_TRIGGER_RISING | IRQF_ONESHOT |
887 irq_flags,
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700888 DRIVER_NAME, &lis3_dev);
889
890 if (err < 0) {
891 printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
892 goto out;
893 }
894
Daniel Mackab337a62009-03-31 15:24:31 -0700895 if (misc_register(&lis3lv02d_misc_device))
896 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
897out:
Daniel Mackab337a62009-03-31 15:24:31 -0700898 return 0;
899}
900EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
901
Pavel Machek455fbdd2008-11-12 13:27:02 -0800902MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800903MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800904MODULE_LICENSE("GPL");