blob: 0780de0550df3d8fad89c0f9e184f3ff66f23e51 [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
Samu Onkalo477bc912010-10-22 07:57:30 -040074#define LIS3_DEFAULT_FUZZ_12B 3
75#define LIS3_DEFAULT_FLAT_12B 3
76#define LIS3_DEFAULT_FUZZ_8B 1
77#define LIS3_DEFAULT_FLAT_8B 1
Samu Onkalo32496c72009-12-14 18:01:46 -080078
Daniel Macka38da2e2009-03-31 15:24:32 -070079struct lis3lv02d lis3_dev = {
Pavel Machekbe84cfc2009-03-31 15:24:26 -070080 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
Pavel Machekef2cfc72009-02-18 14:48:23 -080081};
82
Pavel Machekbe84cfc2009-03-31 15:24:26 -070083EXPORT_SYMBOL_GPL(lis3_dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080084
Takashi Iwai2ee32142010-10-01 17:14:25 -040085/* just like param_set_int() but does sanity-check so that it won't point
86 * over the axis array size
87 */
88static int param_set_axis(const char *val, const struct kernel_param *kp)
89{
90 int ret = param_set_int(val, kp);
91 if (!ret) {
92 int val = *(int *)kp->arg;
93 if (val < 0)
94 val = -val;
95 if (!val || val > 3)
96 return -EINVAL;
97 }
98 return ret;
99}
100
101static struct kernel_param_ops param_ops_axis = {
102 .set = param_set_axis,
103 .get = param_get_int,
104};
105
106module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
107MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
108
Daniel Macka38da2e2009-03-31 15:24:32 -0700109static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
110{
111 s8 lo;
112 if (lis3->read(lis3, reg, &lo) < 0)
113 return 0;
114
115 return lo;
116}
117
Éric Pielbc62c142009-12-14 18:01:39 -0800118static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700119{
120 u8 lo, hi;
121
Daniel Macka38da2e2009-03-31 15:24:32 -0700122 lis3->read(lis3, reg - 1, &lo);
123 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700124 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
125 return (s16)((hi << 8) | lo);
126}
127
Pavel Machek455fbdd2008-11-12 13:27:02 -0800128/**
129 * lis3lv02d_get_axis - For the given axis, give the value converted
130 * @axis: 1,2,3 - can also be negative
131 * @hw_values: raw values returned by the hardware
132 *
133 * Returns the converted value.
134 */
135static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
136{
137 if (axis > 0)
138 return hw_values[axis - 1];
139 else
140 return -hw_values[-axis - 1];
141}
142
143/**
144 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -0700145 * @lis3: pointer to the device struct
146 * @x: where to store the X axis value
147 * @y: where to store the Y axis value
148 * @z: where to store the Z axis value
Pavel Machek455fbdd2008-11-12 13:27:02 -0800149 *
150 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
151 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700152static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800153{
154 int position[3];
Samu Onkalo32496c72009-12-14 18:01:46 -0800155 int i;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800156
Samu Onkalof10a5402010-10-22 07:57:31 -0400157 if (lis3->blkread) {
158 if (lis3_dev.whoami == WAI_12B) {
159 u16 data[3];
160 lis3->blkread(lis3, OUTX_L, 6, (u8 *)data);
161 for (i = 0; i < 3; i++)
162 position[i] = (s16)le16_to_cpu(data[i]);
163 } else {
164 u8 data[5];
165 /* Data: x, dummy, y, dummy, z */
166 lis3->blkread(lis3, OUTX, 5, data);
167 for (i = 0; i < 3; i++)
168 position[i] = (s8)data[i * 2];
169 }
170 } else {
171 position[0] = lis3->read_data(lis3, OUTX);
172 position[1] = lis3->read_data(lis3, OUTY);
173 position[2] = lis3->read_data(lis3, OUTZ);
174 }
Pavel Machek455fbdd2008-11-12 13:27:02 -0800175
Samu Onkalo32496c72009-12-14 18:01:46 -0800176 for (i = 0; i < 3; i++)
177 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
178
Eric Piela002ee82009-06-16 15:34:14 -0700179 *x = lis3lv02d_get_axis(lis3->ac.x, position);
180 *y = lis3lv02d_get_axis(lis3->ac.y, position);
181 *z = lis3lv02d_get_axis(lis3->ac.z, position);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800182}
183
Samu Onkalo641615a2009-12-14 18:01:41 -0800184/* conversion btw sampling rate and the register values */
185static int lis3_12_rates[4] = {40, 160, 640, 2560};
186static int lis3_8_rates[2] = {100, 400};
Takashi Iwai78537c32010-09-23 10:01:39 -0700187static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
Samu Onkalo641615a2009-12-14 18:01:41 -0800188
Samu Onkaloa253aae2009-12-14 18:01:44 -0800189/* ODR is Output Data Rate */
Samu Onkalo641615a2009-12-14 18:01:41 -0800190static int lis3lv02d_get_odr(void)
191{
192 u8 ctrl;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800193 int shift;
Samu Onkalo641615a2009-12-14 18:01:41 -0800194
195 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800196 ctrl &= lis3_dev.odr_mask;
197 shift = ffs(lis3_dev.odr_mask) - 1;
198 return lis3_dev.odrs[(ctrl >> shift)];
199}
Samu Onkalo641615a2009-12-14 18:01:41 -0800200
Samu Onkaloa253aae2009-12-14 18:01:44 -0800201static int lis3lv02d_set_odr(int rate)
202{
203 u8 ctrl;
204 int i, len, shift;
205
Takashi Iwai78537c32010-09-23 10:01:39 -0700206 if (!rate)
207 return -EINVAL;
208
Samu Onkaloa253aae2009-12-14 18:01:44 -0800209 lis3_dev.read(&lis3_dev, CTRL_REG1, &ctrl);
210 ctrl &= ~lis3_dev.odr_mask;
211 len = 1 << hweight_long(lis3_dev.odr_mask); /* # of possible values */
212 shift = ffs(lis3_dev.odr_mask) - 1;
213
214 for (i = 0; i < len; i++)
215 if (lis3_dev.odrs[i] == rate) {
216 lis3_dev.write(&lis3_dev, CTRL_REG1,
217 ctrl | (i << shift));
218 return 0;
219 }
220 return -EINVAL;
Samu Onkalo641615a2009-12-14 18:01:41 -0800221}
222
Samu Onkalo2db4a762009-12-14 18:01:43 -0800223static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
224{
Takashi Iwai78537c32010-09-23 10:01:39 -0700225 u8 ctlreg, reg;
Samu Onkalo2db4a762009-12-14 18:01:43 -0800226 s16 x, y, z;
227 u8 selftest;
228 int ret;
229
230 mutex_lock(&lis3->mutex);
Takashi Iwai78537c32010-09-23 10:01:39 -0700231 if (lis3_dev.whoami == WAI_3DC) {
232 ctlreg = CTRL_REG4;
233 selftest = CTRL4_ST0;
234 } else {
235 ctlreg = CTRL_REG1;
236 if (lis3_dev.whoami == WAI_12B)
237 selftest = CTRL1_ST;
238 else
239 selftest = CTRL1_STP;
240 }
Samu Onkalo2db4a762009-12-14 18:01:43 -0800241
Takashi Iwai78537c32010-09-23 10:01:39 -0700242 lis3->read(lis3, ctlreg, &reg);
243 lis3->write(lis3, ctlreg, (reg | selftest));
Samu Onkalo2db4a762009-12-14 18:01:43 -0800244 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
245
246 /* Read directly to avoid axis remap */
247 x = lis3->read_data(lis3, OUTX);
248 y = lis3->read_data(lis3, OUTY);
249 z = lis3->read_data(lis3, OUTZ);
250
251 /* back to normal settings */
Takashi Iwai78537c32010-09-23 10:01:39 -0700252 lis3->write(lis3, ctlreg, reg);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800253 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
254
255 results[0] = x - lis3->read_data(lis3, OUTX);
256 results[1] = y - lis3->read_data(lis3, OUTY);
257 results[2] = z - lis3->read_data(lis3, OUTZ);
258
259 ret = 0;
260 if (lis3->pdata) {
261 int i;
262 for (i = 0; i < 3; i++) {
263 /* Check against selftest acceptance limits */
264 if ((results[i] < lis3->pdata->st_min_limits[i]) ||
265 (results[i] > lis3->pdata->st_max_limits[i])) {
266 ret = -EIO;
267 goto fail;
268 }
269 }
270 }
271
272 /* test passed */
273fail:
274 mutex_unlock(&lis3->mutex);
275 return ret;
276}
277
Samu Onkalof9deb412010-10-22 07:57:24 -0400278/*
279 * Order of registers in the list affects to order of the restore process.
280 * Perhaps it is a good idea to set interrupt enable register as a last one
281 * after all other configurations
282 */
283static u8 lis3_wai8_regs[] = { FF_WU_CFG_1, FF_WU_THS_1, FF_WU_DURATION_1,
284 FF_WU_CFG_2, FF_WU_THS_2, FF_WU_DURATION_2,
285 CLICK_CFG, CLICK_SRC, CLICK_THSY_X, CLICK_THSZ,
286 CLICK_TIMELIMIT, CLICK_LATENCY, CLICK_WINDOW,
287 CTRL_REG1, CTRL_REG2, CTRL_REG3};
288
289static u8 lis3_wai12_regs[] = {FF_WU_CFG, FF_WU_THS_L, FF_WU_THS_H,
290 FF_WU_DURATION, DD_CFG, DD_THSI_L, DD_THSI_H,
291 DD_THSE_L, DD_THSE_H,
292 CTRL_REG1, CTRL_REG3, CTRL_REG2};
293
294static inline void lis3_context_save(struct lis3lv02d *lis3)
295{
296 int i;
297 for (i = 0; i < lis3->regs_size; i++)
298 lis3->read(lis3, lis3->regs[i], &lis3->reg_cache[i]);
299 lis3->regs_stored = true;
300}
301
302static inline void lis3_context_restore(struct lis3lv02d *lis3)
303{
304 int i;
305 if (lis3->regs_stored)
306 for (i = 0; i < lis3->regs_size; i++)
307 lis3->write(lis3, lis3->regs[i], lis3->reg_cache[i]);
308}
309
Daniel Macka38da2e2009-03-31 15:24:32 -0700310void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800311{
Samu Onkalof9deb412010-10-22 07:57:24 -0400312 if (lis3->reg_ctrl)
313 lis3_context_save(lis3);
Eric Piela002ee82009-06-16 15:34:14 -0700314 /* disable X,Y,Z axis and power down */
315 lis3->write(lis3, CTRL_REG1, 0x00);
Samu Onkalof9deb412010-10-22 07:57:24 -0400316 if (lis3->reg_ctrl)
317 lis3->reg_ctrl(lis3, LIS3_REG_OFF);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800318}
Eric Pielcfce41a2009-01-09 16:41:01 -0800319EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800320
Daniel Macka38da2e2009-03-31 15:24:32 -0700321void lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800322{
Eric Piela002ee82009-06-16 15:34:14 -0700323 u8 reg;
324
325 lis3->init(lis3);
326
327 /*
328 * Common configuration
Éric Piel4b5d95b2009-12-14 18:01:40 -0800329 * BDU: (12 bits sensors only) LSB and MSB values are not updated until
330 * both have been read. So the value read will always be correct.
Samu Onkalo2a7fade2010-10-22 07:57:27 -0400331 * Set BOOT bit to refresh factory tuning values.
Eric Piela002ee82009-06-16 15:34:14 -0700332 */
Samu Onkalo2a7fade2010-10-22 07:57:27 -0400333 lis3->read(lis3, CTRL_REG2, &reg);
334 if (lis3->whoami == WAI_12B)
335 reg |= CTRL2_BDU | CTRL2_BOOT;
336 else
337 reg |= CTRL2_BOOT_8B;
338 lis3->write(lis3, CTRL_REG2, reg);
339
340 /* LIS3 power on delay is quite long */
341 msleep(lis3->pwron_delay / lis3lv02d_get_odr());
342
Samu Onkalof9deb412010-10-22 07:57:24 -0400343 if (lis3->reg_ctrl)
344 lis3_context_restore(lis3);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800345}
Eric Pielcfce41a2009-01-09 16:41:01 -0800346EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800347
Pavel Machek455fbdd2008-11-12 13:27:02 -0800348
Samu Onkalo6d94d402010-05-24 14:33:37 -0700349static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
350{
351 int x, y, z;
352
353 mutex_lock(&lis3_dev.mutex);
354 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
355 input_report_abs(pidev->input, ABS_X, x);
356 input_report_abs(pidev->input, ABS_Y, y);
357 input_report_abs(pidev->input, ABS_Z, z);
358 input_sync(pidev->input);
359 mutex_unlock(&lis3_dev.mutex);
360}
361
Samu Onkalo2a346992010-10-22 07:57:23 -0400362static void lis3lv02d_joystick_open(struct input_polled_dev *pidev)
363{
364 if (lis3_dev.pm_dev)
365 pm_runtime_get_sync(lis3_dev.pm_dev);
Samu Onkaloe7261112010-10-22 07:57:25 -0400366
367 if (lis3_dev.pdata && lis3_dev.whoami == WAI_8B && lis3_dev.idev)
368 atomic_set(&lis3_dev.wake_thread, 1);
Samu Onkalo821f6642010-10-22 07:57:26 -0400369 /*
370 * Update coordinates for the case where poll interval is 0 and
371 * the chip in running purely under interrupt control
372 */
373 lis3lv02d_joystick_poll(pidev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400374}
375
376static void lis3lv02d_joystick_close(struct input_polled_dev *pidev)
377{
Samu Onkaloe7261112010-10-22 07:57:25 -0400378 atomic_set(&lis3_dev.wake_thread, 0);
Samu Onkalo2a346992010-10-22 07:57:23 -0400379 if (lis3_dev.pm_dev)
380 pm_runtime_put(lis3_dev.pm_dev);
381}
382
Pavel Machekef2cfc72009-02-18 14:48:23 -0800383static irqreturn_t lis302dl_interrupt(int irq, void *dummy)
384{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700385 if (!test_bit(0, &lis3_dev.misc_opened))
386 goto out;
387
Pavel Machekef2cfc72009-02-18 14:48:23 -0800388 /*
389 * Be careful: on some HP laptops the bios force DD when on battery and
390 * the lid is closed. This leads to interrupts as soon as a little move
391 * is done.
392 */
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700393 atomic_inc(&lis3_dev.count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800394
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700395 wake_up_interruptible(&lis3_dev.misc_wait);
396 kill_fasync(&lis3_dev.async_queue, SIGIO, POLL_IN);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700397out:
Samu Onkaloe7261112010-10-22 07:57:25 -0400398 if (atomic_read(&lis3_dev.wake_thread))
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700399 return IRQ_WAKE_THREAD;
400 return IRQ_HANDLED;
401}
402
Samu Onkalo6d94d402010-05-24 14:33:37 -0700403static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
404{
405 struct input_dev *dev = lis3->idev->input;
406 u8 click_src;
407
408 mutex_lock(&lis3->mutex);
409 lis3->read(lis3, CLICK_SRC, &click_src);
410
411 if (click_src & CLICK_SINGLE_X) {
412 input_report_key(dev, lis3->mapped_btns[0], 1);
413 input_report_key(dev, lis3->mapped_btns[0], 0);
414 }
415
416 if (click_src & CLICK_SINGLE_Y) {
417 input_report_key(dev, lis3->mapped_btns[1], 1);
418 input_report_key(dev, lis3->mapped_btns[1], 0);
419 }
420
421 if (click_src & CLICK_SINGLE_Z) {
422 input_report_key(dev, lis3->mapped_btns[2], 1);
423 input_report_key(dev, lis3->mapped_btns[2], 0);
424 }
425 input_sync(dev);
426 mutex_unlock(&lis3->mutex);
427}
428
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700429static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
430{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700431
432 struct lis3lv02d *lis3 = data;
433
Samu Onkaloe7261112010-10-22 07:57:25 -0400434 if ((lis3->irq_cfg & LIS3_IRQ1_MASK) == LIS3_IRQ1_CLICK)
Samu Onkalo6d94d402010-05-24 14:33:37 -0700435 lis302dl_interrupt_handle_click(lis3);
436 else
Samu Onkaloe7261112010-10-22 07:57:25 -0400437 lis3lv02d_joystick_poll(lis3->idev);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700438
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700439 return IRQ_HANDLED;
440}
441
442static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
443{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700444
445 struct lis3lv02d *lis3 = data;
446
Samu Onkaloe7261112010-10-22 07:57:25 -0400447 if ((lis3->irq_cfg & LIS3_IRQ2_MASK) == LIS3_IRQ2_CLICK)
Samu Onkalo6d94d402010-05-24 14:33:37 -0700448 lis302dl_interrupt_handle_click(lis3);
449 else
Samu Onkaloe7261112010-10-22 07:57:25 -0400450 lis3lv02d_joystick_poll(lis3->idev);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700451
Pavel Machekef2cfc72009-02-18 14:48:23 -0800452 return IRQ_HANDLED;
453}
454
455static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
456{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700457 if (test_and_set_bit(0, &lis3_dev.misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800458 return -EBUSY; /* already open */
459
Samu Onkalo2a346992010-10-22 07:57:23 -0400460 if (lis3_dev.pm_dev)
461 pm_runtime_get_sync(lis3_dev.pm_dev);
462
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700463 atomic_set(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800464 return 0;
465}
466
467static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
468{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700469 fasync_helper(-1, file, 0, &lis3_dev.async_queue);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700470 clear_bit(0, &lis3_dev.misc_opened); /* release the device */
Samu Onkalo2a346992010-10-22 07:57:23 -0400471 if (lis3_dev.pm_dev)
472 pm_runtime_put(lis3_dev.pm_dev);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800473 return 0;
474}
475
476static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
477 size_t count, loff_t *pos)
478{
479 DECLARE_WAITQUEUE(wait, current);
480 u32 data;
481 unsigned char byte_data;
482 ssize_t retval = 1;
483
484 if (count < 1)
485 return -EINVAL;
486
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700487 add_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800488 while (true) {
489 set_current_state(TASK_INTERRUPTIBLE);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700490 data = atomic_xchg(&lis3_dev.count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800491 if (data)
492 break;
493
494 if (file->f_flags & O_NONBLOCK) {
495 retval = -EAGAIN;
496 goto out;
497 }
498
499 if (signal_pending(current)) {
500 retval = -ERESTARTSYS;
501 goto out;
502 }
503
504 schedule();
505 }
506
507 if (data < 255)
508 byte_data = data;
509 else
510 byte_data = 255;
511
512 /* make sure we are not going into copy_to_user() with
513 * TASK_INTERRUPTIBLE state */
514 set_current_state(TASK_RUNNING);
515 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
516 retval = -EFAULT;
517
518out:
519 __set_current_state(TASK_RUNNING);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700520 remove_wait_queue(&lis3_dev.misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800521
522 return retval;
523}
524
525static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
526{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700527 poll_wait(file, &lis3_dev.misc_wait, wait);
528 if (atomic_read(&lis3_dev.count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800529 return POLLIN | POLLRDNORM;
530 return 0;
531}
532
533static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
534{
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700535 return fasync_helper(fd, file, on, &lis3_dev.async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800536}
537
538static const struct file_operations lis3lv02d_misc_fops = {
539 .owner = THIS_MODULE,
540 .llseek = no_llseek,
541 .read = lis3lv02d_misc_read,
542 .open = lis3lv02d_misc_open,
543 .release = lis3lv02d_misc_release,
544 .poll = lis3lv02d_misc_poll,
545 .fasync = lis3lv02d_misc_fasync,
546};
547
548static struct miscdevice lis3lv02d_misc_device = {
549 .minor = MISC_DYNAMIC_MINOR,
550 .name = "freefall",
551 .fops = &lis3lv02d_misc_fops,
552};
553
Eric Pielcfce41a2009-01-09 16:41:01 -0800554int lis3lv02d_joystick_enable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800555{
Eric Pieldc6ea972009-06-16 15:34:15 -0700556 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800557 int err;
Samu Onkalo32496c72009-12-14 18:01:46 -0800558 int max_val, fuzz, flat;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700559 int btns[] = {BTN_X, BTN_Y, BTN_Z};
Pavel Machek455fbdd2008-11-12 13:27:02 -0800560
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700561 if (lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800562 return -EINVAL;
563
Eric Pieldc6ea972009-06-16 15:34:15 -0700564 lis3_dev.idev = input_allocate_polled_device();
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700565 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800566 return -ENOMEM;
567
Eric Pieldc6ea972009-06-16 15:34:15 -0700568 lis3_dev.idev->poll = lis3lv02d_joystick_poll;
Samu Onkalo2a346992010-10-22 07:57:23 -0400569 lis3_dev.idev->open = lis3lv02d_joystick_open;
570 lis3_dev.idev->close = lis3lv02d_joystick_close;
Eric Pieldc6ea972009-06-16 15:34:15 -0700571 lis3_dev.idev->poll_interval = MDPS_POLL_INTERVAL;
Samu Onkalo4a70a412010-05-24 14:33:37 -0700572 lis3_dev.idev->poll_interval_min = MDPS_POLL_MIN;
573 lis3_dev.idev->poll_interval_max = MDPS_POLL_MAX;
Eric Pieldc6ea972009-06-16 15:34:15 -0700574 input_dev = lis3_dev.idev->input;
575
Eric Pieldc6ea972009-06-16 15:34:15 -0700576 input_dev->name = "ST LIS3LV02DL Accelerometer";
577 input_dev->phys = DRIVER_NAME "/input0";
578 input_dev->id.bustype = BUS_HOST;
579 input_dev->id.vendor = 0;
580 input_dev->dev.parent = &lis3_dev.pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800581
Eric Pieldc6ea972009-06-16 15:34:15 -0700582 set_bit(EV_ABS, input_dev->evbit);
Samu Onkalo32496c72009-12-14 18:01:46 -0800583 max_val = (lis3_dev.mdps_max_val * lis3_dev.scale) / LIS3_ACCURACY;
Samu Onkalo477bc912010-10-22 07:57:30 -0400584 if (lis3_dev.whoami == WAI_12B) {
585 fuzz = LIS3_DEFAULT_FUZZ_12B;
586 flat = LIS3_DEFAULT_FLAT_12B;
587 } else {
588 fuzz = LIS3_DEFAULT_FUZZ_8B;
589 flat = LIS3_DEFAULT_FLAT_8B;
590 }
591 fuzz = (fuzz * lis3_dev.scale) / LIS3_ACCURACY;
592 flat = (flat * lis3_dev.scale) / LIS3_ACCURACY;
593
Samu Onkalo32496c72009-12-14 18:01:46 -0800594 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
595 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
596 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800597
Samu Onkalo6d94d402010-05-24 14:33:37 -0700598 lis3_dev.mapped_btns[0] = lis3lv02d_get_axis(abs(lis3_dev.ac.x), btns);
599 lis3_dev.mapped_btns[1] = lis3lv02d_get_axis(abs(lis3_dev.ac.y), btns);
600 lis3_dev.mapped_btns[2] = lis3lv02d_get_axis(abs(lis3_dev.ac.z), btns);
601
Eric Pieldc6ea972009-06-16 15:34:15 -0700602 err = input_register_polled_device(lis3_dev.idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800603 if (err) {
Eric Pieldc6ea972009-06-16 15:34:15 -0700604 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700605 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800606 }
607
608 return err;
609}
Eric Pielcfce41a2009-01-09 16:41:01 -0800610EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800611
Eric Pielcfce41a2009-01-09 16:41:01 -0800612void lis3lv02d_joystick_disable(void)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800613{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700614 if (lis3_dev.irq)
615 free_irq(lis3_dev.irq, &lis3_dev);
616 if (lis3_dev.pdata && lis3_dev.pdata->irq2)
617 free_irq(lis3_dev.pdata->irq2, &lis3_dev);
618
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700619 if (!lis3_dev.idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800620 return;
621
Eric Pielc2884242009-06-16 15:34:13 -0700622 if (lis3_dev.irq)
623 misc_deregister(&lis3lv02d_misc_device);
Eric Pieldc6ea972009-06-16 15:34:15 -0700624 input_unregister_polled_device(lis3_dev.idev);
Samu Onkalo66c85692009-12-14 18:01:39 -0800625 input_free_polled_device(lis3_dev.idev);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700626 lis3_dev.idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800627}
Eric Pielcfce41a2009-01-09 16:41:01 -0800628EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800629
Pavel Machek455fbdd2008-11-12 13:27:02 -0800630/* Sysfs stuff */
Samu Onkalo2a346992010-10-22 07:57:23 -0400631static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
632{
633 /*
634 * SYSFS functions are fast visitors so put-call
635 * immediately after the get-call. However, keep
636 * chip running for a while and schedule delayed
637 * suspend. This way periodic sysfs calls doesn't
638 * suffer from relatively long power up time.
639 */
640
641 if (lis3->pm_dev) {
642 pm_runtime_get_sync(lis3->pm_dev);
643 pm_runtime_put_noidle(lis3->pm_dev);
644 pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
645 }
646}
647
Samu Onkalo2db4a762009-12-14 18:01:43 -0800648static ssize_t lis3lv02d_selftest_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
650{
651 int result;
652 s16 values[3];
653
Samu Onkalo2a346992010-10-22 07:57:23 -0400654 lis3lv02d_sysfs_poweron(&lis3_dev);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800655 result = lis3lv02d_selftest(&lis3_dev, values);
656 return sprintf(buf, "%s %d %d %d\n", result == 0 ? "OK" : "FAIL",
657 values[0], values[1], values[2]);
658}
659
Pavel Machek455fbdd2008-11-12 13:27:02 -0800660static ssize_t lis3lv02d_position_show(struct device *dev,
661 struct device_attribute *attr, char *buf)
662{
663 int x, y, z;
664
Samu Onkalo2a346992010-10-22 07:57:23 -0400665 lis3lv02d_sysfs_poweron(&lis3_dev);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700666 mutex_lock(&lis3_dev.mutex);
Daniel Macka38da2e2009-03-31 15:24:32 -0700667 lis3lv02d_get_xyz(&lis3_dev, &x, &y, &z);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700668 mutex_unlock(&lis3_dev.mutex);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800669 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
670}
671
Pavel Machek455fbdd2008-11-12 13:27:02 -0800672static ssize_t lis3lv02d_rate_show(struct device *dev,
673 struct device_attribute *attr, char *buf)
674{
Samu Onkalo2a346992010-10-22 07:57:23 -0400675 lis3lv02d_sysfs_poweron(&lis3_dev);
Samu Onkalo641615a2009-12-14 18:01:41 -0800676 return sprintf(buf, "%d\n", lis3lv02d_get_odr());
Pavel Machek455fbdd2008-11-12 13:27:02 -0800677}
678
Samu Onkaloa253aae2009-12-14 18:01:44 -0800679static ssize_t lis3lv02d_rate_set(struct device *dev,
680 struct device_attribute *attr, const char *buf,
681 size_t count)
682{
683 unsigned long rate;
684
685 if (strict_strtoul(buf, 0, &rate))
686 return -EINVAL;
687
Samu Onkalo2a346992010-10-22 07:57:23 -0400688 lis3lv02d_sysfs_poweron(&lis3_dev);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800689 if (lis3lv02d_set_odr(rate))
690 return -EINVAL;
691
692 return count;
693}
694
Samu Onkalo2db4a762009-12-14 18:01:43 -0800695static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800696static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800697static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
698 lis3lv02d_rate_set);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800699
700static struct attribute *lis3lv02d_attributes[] = {
Samu Onkalo2db4a762009-12-14 18:01:43 -0800701 &dev_attr_selftest.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800702 &dev_attr_position.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800703 &dev_attr_rate.attr,
704 NULL
705};
706
707static struct attribute_group lis3lv02d_attribute_group = {
708 .attrs = lis3lv02d_attributes
709};
710
Eric Pielcfce41a2009-01-09 16:41:01 -0800711
Daniel Macka38da2e2009-03-31 15:24:32 -0700712static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800713{
Eric Piela002ee82009-06-16 15:34:14 -0700714 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
715 if (IS_ERR(lis3->pdev))
716 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800717
Eric Piela002ee82009-06-16 15:34:14 -0700718 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800719}
720
Eric Piela002ee82009-06-16 15:34:14 -0700721int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800722{
Eric Piela002ee82009-06-16 15:34:14 -0700723 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
724 platform_device_unregister(lis3->pdev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400725 if (lis3->pm_dev) {
726 /* Barrier after the sysfs remove */
727 pm_runtime_barrier(lis3->pm_dev);
728
729 /* SYSFS may have left chip running. Turn off if necessary */
730 if (!pm_runtime_suspended(lis3->pm_dev))
731 lis3lv02d_poweroff(&lis3_dev);
732
733 pm_runtime_disable(lis3->pm_dev);
734 pm_runtime_set_suspended(lis3->pm_dev);
735 }
Samu Onkalof9deb412010-10-22 07:57:24 -0400736 kfree(lis3->reg_cache);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800737 return 0;
738}
Eric Pielcfce41a2009-01-09 16:41:01 -0800739EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800740
Samu Onkaloecc437a2010-05-24 14:33:34 -0700741static void lis3lv02d_8b_configure(struct lis3lv02d *dev,
742 struct lis3lv02d_platform_data *p)
743{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700744 int err;
Samu Onkalo342c5f12010-05-24 14:33:35 -0700745 int ctrl2 = p->hipass_ctrl;
746
Samu Onkaloecc437a2010-05-24 14:33:34 -0700747 if (p->click_flags) {
748 dev->write(dev, CLICK_CFG, p->click_flags);
749 dev->write(dev, CLICK_TIMELIMIT, p->click_time_limit);
750 dev->write(dev, CLICK_LATENCY, p->click_latency);
751 dev->write(dev, CLICK_WINDOW, p->click_window);
752 dev->write(dev, CLICK_THSZ, p->click_thresh_z & 0xf);
753 dev->write(dev, CLICK_THSY_X,
754 (p->click_thresh_x & 0xf) |
755 (p->click_thresh_y << 4));
Samu Onkalo6d94d402010-05-24 14:33:37 -0700756
757 if (dev->idev) {
758 struct input_dev *input_dev = lis3_dev.idev->input;
759 input_set_capability(input_dev, EV_KEY, BTN_X);
760 input_set_capability(input_dev, EV_KEY, BTN_Y);
761 input_set_capability(input_dev, EV_KEY, BTN_Z);
762 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700763 }
764
765 if (p->wakeup_flags) {
766 dev->write(dev, FF_WU_CFG_1, p->wakeup_flags);
767 dev->write(dev, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
Samu Onkalocc23aa12010-10-22 07:57:29 -0400768 /* pdata value + 1 to keep this backward compatible*/
769 dev->write(dev, FF_WU_DURATION_1, p->duration1 + 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700770 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
Samu Onkaloecc437a2010-05-24 14:33:34 -0700771 }
Samu Onkalo342c5f12010-05-24 14:33:35 -0700772
773 if (p->wakeup_flags2) {
774 dev->write(dev, FF_WU_CFG_2, p->wakeup_flags2);
775 dev->write(dev, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
Samu Onkalocc23aa12010-10-22 07:57:29 -0400776 /* pdata value + 1 to keep this backward compatible*/
777 dev->write(dev, FF_WU_DURATION_2, p->duration2 + 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700778 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
779 }
780 /* Configure hipass filters */
781 dev->write(dev, CTRL_REG2, ctrl2);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700782
783 if (p->irq2) {
784 err = request_threaded_irq(p->irq2,
785 NULL,
786 lis302dl_interrupt_thread2_8b,
Samu Onkalocc23aa12010-10-22 07:57:29 -0400787 IRQF_TRIGGER_RISING | IRQF_ONESHOT |
788 (p->irq_flags2 & IRQF_TRIGGER_MASK),
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700789 DRIVER_NAME, &lis3_dev);
790 if (err < 0)
791 printk(KERN_ERR DRIVER_NAME
792 "No second IRQ. Limited functionality\n");
793 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700794}
795
Daniel Mackab337a62009-03-31 15:24:31 -0700796/*
797 * Initialise the accelerometer and the various subsystems.
Éric Pielbc62c142009-12-14 18:01:39 -0800798 * Should be rather independent of the bus system.
Daniel Mackab337a62009-03-31 15:24:31 -0700799 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700800int lis3lv02d_init_device(struct lis3lv02d *dev)
Daniel Mackab337a62009-03-31 15:24:31 -0700801{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700802 int err;
803 irq_handler_t thread_fn;
Samu Onkalocc23aa12010-10-22 07:57:29 -0400804 int irq_flags = 0;
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700805
Daniel Macka38da2e2009-03-31 15:24:32 -0700806 dev->whoami = lis3lv02d_read_8(dev, WHO_AM_I);
807
808 switch (dev->whoami) {
Éric Pielbc62c142009-12-14 18:01:39 -0800809 case WAI_12B:
810 printk(KERN_INFO DRIVER_NAME ": 12 bits sensor found\n");
811 dev->read_data = lis3lv02d_read_12;
Daniel Macka38da2e2009-03-31 15:24:32 -0700812 dev->mdps_max_val = 2048;
Samu Onkalo641615a2009-12-14 18:01:41 -0800813 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800814 dev->odrs = lis3_12_rates;
815 dev->odr_mask = CTRL1_DF0 | CTRL1_DF1;
Samu Onkalo32496c72009-12-14 18:01:46 -0800816 dev->scale = LIS3_SENSITIVITY_12B;
Samu Onkalof9deb412010-10-22 07:57:24 -0400817 dev->regs = lis3_wai12_regs;
818 dev->regs_size = ARRAY_SIZE(lis3_wai12_regs);
Daniel Macka38da2e2009-03-31 15:24:32 -0700819 break;
Éric Pielbc62c142009-12-14 18:01:39 -0800820 case WAI_8B:
821 printk(KERN_INFO DRIVER_NAME ": 8 bits sensor found\n");
Daniel Macka38da2e2009-03-31 15:24:32 -0700822 dev->read_data = lis3lv02d_read_8;
823 dev->mdps_max_val = 128;
Samu Onkalo641615a2009-12-14 18:01:41 -0800824 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800825 dev->odrs = lis3_8_rates;
826 dev->odr_mask = CTRL1_DR;
Samu Onkalo32496c72009-12-14 18:01:46 -0800827 dev->scale = LIS3_SENSITIVITY_8B;
Samu Onkalof9deb412010-10-22 07:57:24 -0400828 dev->regs = lis3_wai8_regs;
829 dev->regs_size = ARRAY_SIZE(lis3_wai8_regs);
Daniel Macka38da2e2009-03-31 15:24:32 -0700830 break;
Takashi Iwai78537c32010-09-23 10:01:39 -0700831 case WAI_3DC:
832 printk(KERN_INFO DRIVER_NAME ": 8 bits 3DC sensor found\n");
833 dev->read_data = lis3lv02d_read_8;
834 dev->mdps_max_val = 128;
835 dev->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
836 dev->odrs = lis3_3dc_rates;
837 dev->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
838 dev->scale = LIS3_SENSITIVITY_8B;
839 break;
Daniel Macka38da2e2009-03-31 15:24:32 -0700840 default:
841 printk(KERN_ERR DRIVER_NAME
Eric Piela002ee82009-06-16 15:34:14 -0700842 ": unknown sensor type 0x%X\n", dev->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700843 return -EINVAL;
844 }
845
Samu Onkalof9deb412010-10-22 07:57:24 -0400846 dev->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
847 sizeof(lis3_wai12_regs)), GFP_KERNEL);
848
849 if (dev->reg_cache == NULL) {
850 printk(KERN_ERR DRIVER_NAME "out of memory\n");
851 return -ENOMEM;
852 }
853
Samu Onkalo2db4a762009-12-14 18:01:43 -0800854 mutex_init(&dev->mutex);
Samu Onkaloe7261112010-10-22 07:57:25 -0400855 atomic_set(&dev->wake_thread, 0);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800856
Daniel Macka38da2e2009-03-31 15:24:32 -0700857 lis3lv02d_add_fs(dev);
Eric Piela002ee82009-06-16 15:34:14 -0700858 lis3lv02d_poweron(dev);
Daniel Mackab337a62009-03-31 15:24:31 -0700859
Samu Onkalo2a346992010-10-22 07:57:23 -0400860 if (dev->pm_dev) {
861 pm_runtime_set_active(dev->pm_dev);
862 pm_runtime_enable(dev->pm_dev);
863 }
864
Daniel Mackab337a62009-03-31 15:24:31 -0700865 if (lis3lv02d_joystick_enable())
866 printk(KERN_ERR DRIVER_NAME ": joystick initialization failed\n");
867
Daniel Mack8f3128e2009-06-16 15:34:17 -0700868 /* passing in platform specific data is purely optional and only
869 * used by the SPI transport layer at the moment */
870 if (dev->pdata) {
871 struct lis3lv02d_platform_data *p = dev->pdata;
872
Samu Onkaloecc437a2010-05-24 14:33:34 -0700873 if (dev->whoami == WAI_8B)
874 lis3lv02d_8b_configure(dev, p);
Daniel Mack8873c332009-09-21 17:04:43 -0700875
Samu Onkalocc23aa12010-10-22 07:57:29 -0400876 irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK;
877
Samu Onkaloe7261112010-10-22 07:57:25 -0400878 dev->irq_cfg = p->irq_cfg;
Daniel Mack8f3128e2009-06-16 15:34:17 -0700879 if (p->irq_cfg)
880 dev->write(dev, CTRL_REG3, p->irq_cfg);
Samu Onkalocc23aa12010-10-22 07:57:29 -0400881
882 if (p->default_rate)
883 lis3lv02d_set_odr(p->default_rate);
Daniel Mack8f3128e2009-06-16 15:34:17 -0700884 }
885
Daniel Macka38da2e2009-03-31 15:24:32 -0700886 /* bail if we did not get an IRQ from the bus layer */
Daniel Mackab337a62009-03-31 15:24:31 -0700887 if (!dev->irq) {
888 printk(KERN_ERR DRIVER_NAME
Daniel Macka38da2e2009-03-31 15:24:32 -0700889 ": No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700890 goto out;
891 }
892
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700893 /*
894 * The sensor can generate interrupts for free-fall and direction
895 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
896 * the things simple and _fast_ we activate it only for free-fall, so
897 * no need to read register (very slow with ACPI). For the same reason,
898 * we forbid shared interrupts.
899 *
900 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
901 * io-apic is not configurable (and generates a warning) but I keep it
902 * in case of support for other hardware.
903 */
Takashi Iwaif7c77a32010-09-23 10:01:11 -0700904 if (dev->pdata && dev->whoami == WAI_8B)
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700905 thread_fn = lis302dl_interrupt_thread1_8b;
906 else
907 thread_fn = NULL;
908
909 err = request_threaded_irq(dev->irq, lis302dl_interrupt,
910 thread_fn,
Samu Onkalocc23aa12010-10-22 07:57:29 -0400911 IRQF_TRIGGER_RISING | IRQF_ONESHOT |
912 irq_flags,
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700913 DRIVER_NAME, &lis3_dev);
914
915 if (err < 0) {
916 printk(KERN_ERR DRIVER_NAME "Cannot get IRQ\n");
917 goto out;
918 }
919
Daniel Mackab337a62009-03-31 15:24:31 -0700920 if (misc_register(&lis3lv02d_misc_device))
921 printk(KERN_ERR DRIVER_NAME ": misc_register failed\n");
922out:
Daniel Mackab337a62009-03-31 15:24:31 -0700923 return 0;
924}
925EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
926
Pavel Machek455fbdd2008-11-12 13:27:02 -0800927MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -0800928MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -0800929MODULE_LICENSE("GPL");