blob: a981e2a42f92a9427058d528fd784ed2798c6883 [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
Joe Perches63366d32010-10-20 06:51:40 +000023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Pavel Machek455fbdd2008-11-12 13:27:02 -080025#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/dmi.h>
28#include <linux/module.h>
29#include <linux/types.h>
30#include <linux/platform_device.h>
31#include <linux/interrupt.h>
Eric Pieldc6ea972009-06-16 15:34:15 -070032#include <linux/input-polldev.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080033#include <linux/delay.h>
34#include <linux/wait.h>
35#include <linux/poll.h>
Samu Onkalof9deb412010-10-22 07:57:24 -040036#include <linux/slab.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080037#include <linux/freezer.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080038#include <linux/uaccess.h>
Pavel Machekef2cfc72009-02-18 14:48:23 -080039#include <linux/miscdevice.h>
Samu Onkalo2a346992010-10-22 07:57:23 -040040#include <linux/pm_runtime.h>
Jean Delvareff606672011-03-21 17:59:36 +010041#include <linux/atomic.h>
Pavel Machek455fbdd2008-11-12 13:27:02 -080042#include "lis3lv02d.h"
43
44#define DRIVER_NAME "lis3lv02d"
Pavel Machek455fbdd2008-11-12 13:27:02 -080045
46/* joystick device poll interval in milliseconds */
47#define MDPS_POLL_INTERVAL 50
Samu Onkalo4a70a412010-05-24 14:33:37 -070048#define MDPS_POLL_MIN 0
49#define MDPS_POLL_MAX 2000
Samu Onkalo2a346992010-10-22 07:57:23 -040050
51#define LIS3_SYSFS_POWERDOWN_DELAY 5000 /* In milliseconds */
52
Samu Onkalo029756d2010-10-22 07:57:32 -040053#define SELFTEST_OK 0
54#define SELFTEST_FAIL -1
55#define SELFTEST_IRQ -2
56
57#define IRQ_LINE0 0
58#define IRQ_LINE1 1
59
Pavel Machek455fbdd2008-11-12 13:27:02 -080060/*
61 * The sensor can also generate interrupts (DRDY) but it's pretty pointless
Éric Pielbc62c142009-12-14 18:01:39 -080062 * because they are generated even if the data do not change. So it's better
Pavel Machek455fbdd2008-11-12 13:27:02 -080063 * to keep the interrupt for the free-fall event. The values are updated at
64 * 40Hz (at the lowest frequency), but as it can be pretty time consuming on
65 * some low processor, we poll the sensor only at 20Hz... enough for the
66 * joystick.
67 */
68
Samu Onkalo641615a2009-12-14 18:01:41 -080069#define LIS3_PWRON_DELAY_WAI_12B (5000)
70#define LIS3_PWRON_DELAY_WAI_8B (3000)
71
Samu Onkalo32496c72009-12-14 18:01:46 -080072/*
73 * LIS3LV02D spec says 1024 LSBs corresponds 1 G -> 1LSB is 1000/1024 mG
74 * LIS302D spec says: 18 mG / digit
75 * LIS3_ACCURACY is used to increase accuracy of the intermediate
76 * calculation results.
77 */
78#define LIS3_ACCURACY 1024
79/* Sensitivity values for -2G +2G scale */
80#define LIS3_SENSITIVITY_12B ((LIS3_ACCURACY * 1000) / 1024)
81#define LIS3_SENSITIVITY_8B (18 * LIS3_ACCURACY)
82
Samu Onkalo477bc912010-10-22 07:57:30 -040083#define LIS3_DEFAULT_FUZZ_12B 3
84#define LIS3_DEFAULT_FLAT_12B 3
85#define LIS3_DEFAULT_FUZZ_8B 1
86#define LIS3_DEFAULT_FLAT_8B 1
Samu Onkalo32496c72009-12-14 18:01:46 -080087
Daniel Macka38da2e2009-03-31 15:24:32 -070088struct lis3lv02d lis3_dev = {
Pavel Machekbe84cfc2009-03-31 15:24:26 -070089 .misc_wait = __WAIT_QUEUE_HEAD_INITIALIZER(lis3_dev.misc_wait),
Pavel Machekef2cfc72009-02-18 14:48:23 -080090};
Pavel Machekbe84cfc2009-03-31 15:24:26 -070091EXPORT_SYMBOL_GPL(lis3_dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -080092
Takashi Iwai2ee32142010-10-01 17:14:25 -040093/* just like param_set_int() but does sanity-check so that it won't point
94 * over the axis array size
95 */
96static int param_set_axis(const char *val, const struct kernel_param *kp)
97{
98 int ret = param_set_int(val, kp);
99 if (!ret) {
100 int val = *(int *)kp->arg;
101 if (val < 0)
102 val = -val;
103 if (!val || val > 3)
104 return -EINVAL;
105 }
106 return ret;
107}
108
109static struct kernel_param_ops param_ops_axis = {
110 .set = param_set_axis,
111 .get = param_get_int,
112};
113
Rusty Russellbafeafe2012-01-13 09:32:16 +1030114#define param_check_axis(name, p) param_check_int(name, p)
115
Takashi Iwai2ee32142010-10-01 17:14:25 -0400116module_param_array_named(axes, lis3_dev.ac.as_array, axis, NULL, 0644);
117MODULE_PARM_DESC(axes, "Axis-mapping for x,y,z directions");
118
Daniel Macka38da2e2009-03-31 15:24:32 -0700119static s16 lis3lv02d_read_8(struct lis3lv02d *lis3, int reg)
120{
121 s8 lo;
122 if (lis3->read(lis3, reg, &lo) < 0)
123 return 0;
124
125 return lo;
126}
127
Éric Pielbc62c142009-12-14 18:01:39 -0800128static s16 lis3lv02d_read_12(struct lis3lv02d *lis3, int reg)
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700129{
130 u8 lo, hi;
131
Daniel Macka38da2e2009-03-31 15:24:32 -0700132 lis3->read(lis3, reg - 1, &lo);
133 lis3->read(lis3, reg, &hi);
Pavel Machekbe84cfc2009-03-31 15:24:26 -0700134 /* In "12 bit right justified" mode, bit 6, bit 7, bit 8 = bit 5 */
135 return (s16)((hi << 8) | lo);
136}
137
Pavel Machek455fbdd2008-11-12 13:27:02 -0800138/**
139 * lis3lv02d_get_axis - For the given axis, give the value converted
140 * @axis: 1,2,3 - can also be negative
141 * @hw_values: raw values returned by the hardware
142 *
143 * Returns the converted value.
144 */
145static inline int lis3lv02d_get_axis(s8 axis, int hw_values[3])
146{
147 if (axis > 0)
148 return hw_values[axis - 1];
149 else
150 return -hw_values[-axis - 1];
151}
152
153/**
154 * lis3lv02d_get_xyz - Get X, Y and Z axis values from the accelerometer
Daniel Macka38da2e2009-03-31 15:24:32 -0700155 * @lis3: pointer to the device struct
156 * @x: where to store the X axis value
157 * @y: where to store the Y axis value
158 * @z: where to store the Z axis value
Pavel Machek455fbdd2008-11-12 13:27:02 -0800159 *
160 * Note that 40Hz input device can eat up about 10% CPU at 800MHZ
161 */
Daniel Macka38da2e2009-03-31 15:24:32 -0700162static void lis3lv02d_get_xyz(struct lis3lv02d *lis3, int *x, int *y, int *z)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800163{
164 int position[3];
Samu Onkalo32496c72009-12-14 18:01:46 -0800165 int i;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800166
Samu Onkalof10a5402010-10-22 07:57:31 -0400167 if (lis3->blkread) {
Éric Piel895c1562011-10-31 17:11:05 -0700168 if (lis3->whoami == WAI_12B) {
Samu Onkalof10a5402010-10-22 07:57:31 -0400169 u16 data[3];
170 lis3->blkread(lis3, OUTX_L, 6, (u8 *)data);
171 for (i = 0; i < 3; i++)
172 position[i] = (s16)le16_to_cpu(data[i]);
173 } else {
174 u8 data[5];
175 /* Data: x, dummy, y, dummy, z */
176 lis3->blkread(lis3, OUTX, 5, data);
177 for (i = 0; i < 3; i++)
178 position[i] = (s8)data[i * 2];
179 }
180 } else {
181 position[0] = lis3->read_data(lis3, OUTX);
182 position[1] = lis3->read_data(lis3, OUTY);
183 position[2] = lis3->read_data(lis3, OUTZ);
184 }
Pavel Machek455fbdd2008-11-12 13:27:02 -0800185
Samu Onkalo32496c72009-12-14 18:01:46 -0800186 for (i = 0; i < 3; i++)
187 position[i] = (position[i] * lis3->scale) / LIS3_ACCURACY;
188
Eric Piela002ee82009-06-16 15:34:14 -0700189 *x = lis3lv02d_get_axis(lis3->ac.x, position);
190 *y = lis3lv02d_get_axis(lis3->ac.y, position);
191 *z = lis3lv02d_get_axis(lis3->ac.z, position);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800192}
193
Samu Onkalo641615a2009-12-14 18:01:41 -0800194/* conversion btw sampling rate and the register values */
195static int lis3_12_rates[4] = {40, 160, 640, 2560};
196static int lis3_8_rates[2] = {100, 400};
Takashi Iwai78537c32010-09-23 10:01:39 -0700197static int lis3_3dc_rates[16] = {0, 1, 10, 25, 50, 100, 200, 400, 1600, 5000};
Samu Onkalo641615a2009-12-14 18:01:41 -0800198
Samu Onkaloa253aae2009-12-14 18:01:44 -0800199/* ODR is Output Data Rate */
Éric Piel895c1562011-10-31 17:11:05 -0700200static int lis3lv02d_get_odr(struct lis3lv02d *lis3)
Samu Onkalo641615a2009-12-14 18:01:41 -0800201{
202 u8 ctrl;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800203 int shift;
Samu Onkalo641615a2009-12-14 18:01:41 -0800204
Éric Piel895c1562011-10-31 17:11:05 -0700205 lis3->read(lis3, CTRL_REG1, &ctrl);
206 ctrl &= lis3->odr_mask;
207 shift = ffs(lis3->odr_mask) - 1;
208 return lis3->odrs[(ctrl >> shift)];
Samu Onkaloa253aae2009-12-14 18:01:44 -0800209}
Samu Onkalo641615a2009-12-14 18:01:41 -0800210
Éric Piel1510dd52011-10-31 17:10:31 -0700211static int lis3lv02d_get_pwron_wait(struct lis3lv02d *lis3)
212{
Éric Piel895c1562011-10-31 17:11:05 -0700213 int div = lis3lv02d_get_odr(lis3);
Éric Piel1510dd52011-10-31 17:10:31 -0700214
215 if (WARN_ONCE(div == 0, "device returned spurious data"))
216 return -ENXIO;
217
218 /* LIS3 power on delay is quite long */
219 msleep(lis3->pwron_delay / div);
220 return 0;
221}
222
Éric Piel895c1562011-10-31 17:11:05 -0700223static int lis3lv02d_set_odr(struct lis3lv02d *lis3, int rate)
Samu Onkaloa253aae2009-12-14 18:01:44 -0800224{
225 u8 ctrl;
226 int i, len, shift;
227
Takashi Iwai78537c32010-09-23 10:01:39 -0700228 if (!rate)
229 return -EINVAL;
230
Éric Piel895c1562011-10-31 17:11:05 -0700231 lis3->read(lis3, CTRL_REG1, &ctrl);
232 ctrl &= ~lis3->odr_mask;
233 len = 1 << hweight_long(lis3->odr_mask); /* # of possible values */
234 shift = ffs(lis3->odr_mask) - 1;
Samu Onkaloa253aae2009-12-14 18:01:44 -0800235
236 for (i = 0; i < len; i++)
Éric Piel895c1562011-10-31 17:11:05 -0700237 if (lis3->odrs[i] == rate) {
238 lis3->write(lis3, CTRL_REG1,
Samu Onkaloa253aae2009-12-14 18:01:44 -0800239 ctrl | (i << shift));
240 return 0;
241 }
242 return -EINVAL;
Samu Onkalo641615a2009-12-14 18:01:41 -0800243}
244
Samu Onkalo2db4a762009-12-14 18:01:43 -0800245static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
246{
Takashi Iwai78537c32010-09-23 10:01:39 -0700247 u8 ctlreg, reg;
Samu Onkalo2db4a762009-12-14 18:01:43 -0800248 s16 x, y, z;
249 u8 selftest;
250 int ret;
Samu Onkalo029756d2010-10-22 07:57:32 -0400251 u8 ctrl_reg_data;
252 unsigned char irq_cfg;
Samu Onkalo2db4a762009-12-14 18:01:43 -0800253
254 mutex_lock(&lis3->mutex);
Samu Onkalo029756d2010-10-22 07:57:32 -0400255
256 irq_cfg = lis3->irq_cfg;
Éric Piel895c1562011-10-31 17:11:05 -0700257 if (lis3->whoami == WAI_8B) {
Samu Onkalo029756d2010-10-22 07:57:32 -0400258 lis3->data_ready_count[IRQ_LINE0] = 0;
259 lis3->data_ready_count[IRQ_LINE1] = 0;
260
261 /* Change interrupt cfg to data ready for selftest */
Éric Piel895c1562011-10-31 17:11:05 -0700262 atomic_inc(&lis3->wake_thread);
Samu Onkalo029756d2010-10-22 07:57:32 -0400263 lis3->irq_cfg = LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY;
264 lis3->read(lis3, CTRL_REG3, &ctrl_reg_data);
265 lis3->write(lis3, CTRL_REG3, (ctrl_reg_data &
266 ~(LIS3_IRQ1_MASK | LIS3_IRQ2_MASK)) |
267 (LIS3_IRQ1_DATA_READY | LIS3_IRQ2_DATA_READY));
268 }
269
Éric Piel895c1562011-10-31 17:11:05 -0700270 if (lis3->whoami == WAI_3DC) {
Takashi Iwai78537c32010-09-23 10:01:39 -0700271 ctlreg = CTRL_REG4;
272 selftest = CTRL4_ST0;
273 } else {
274 ctlreg = CTRL_REG1;
Éric Piel895c1562011-10-31 17:11:05 -0700275 if (lis3->whoami == WAI_12B)
Takashi Iwai78537c32010-09-23 10:01:39 -0700276 selftest = CTRL1_ST;
277 else
278 selftest = CTRL1_STP;
279 }
Samu Onkalo2db4a762009-12-14 18:01:43 -0800280
Takashi Iwai78537c32010-09-23 10:01:39 -0700281 lis3->read(lis3, ctlreg, &reg);
282 lis3->write(lis3, ctlreg, (reg | selftest));
Éric Piel1510dd52011-10-31 17:10:31 -0700283 ret = lis3lv02d_get_pwron_wait(lis3);
284 if (ret)
285 goto fail;
Samu Onkalo2db4a762009-12-14 18:01:43 -0800286
287 /* Read directly to avoid axis remap */
288 x = lis3->read_data(lis3, OUTX);
289 y = lis3->read_data(lis3, OUTY);
290 z = lis3->read_data(lis3, OUTZ);
291
292 /* back to normal settings */
Takashi Iwai78537c32010-09-23 10:01:39 -0700293 lis3->write(lis3, ctlreg, reg);
Éric Piel1510dd52011-10-31 17:10:31 -0700294 ret = lis3lv02d_get_pwron_wait(lis3);
295 if (ret)
296 goto fail;
Samu Onkalo2db4a762009-12-14 18:01:43 -0800297
298 results[0] = x - lis3->read_data(lis3, OUTX);
299 results[1] = y - lis3->read_data(lis3, OUTY);
300 results[2] = z - lis3->read_data(lis3, OUTZ);
301
302 ret = 0;
Samu Onkalo029756d2010-10-22 07:57:32 -0400303
Éric Piel895c1562011-10-31 17:11:05 -0700304 if (lis3->whoami == WAI_8B) {
Samu Onkalo029756d2010-10-22 07:57:32 -0400305 /* Restore original interrupt configuration */
Éric Piel895c1562011-10-31 17:11:05 -0700306 atomic_dec(&lis3->wake_thread);
Samu Onkalo029756d2010-10-22 07:57:32 -0400307 lis3->write(lis3, CTRL_REG3, ctrl_reg_data);
308 lis3->irq_cfg = irq_cfg;
309
310 if ((irq_cfg & LIS3_IRQ1_MASK) &&
311 lis3->data_ready_count[IRQ_LINE0] < 2) {
312 ret = SELFTEST_IRQ;
313 goto fail;
314 }
315
316 if ((irq_cfg & LIS3_IRQ2_MASK) &&
317 lis3->data_ready_count[IRQ_LINE1] < 2) {
318 ret = SELFTEST_IRQ;
319 goto fail;
320 }
321 }
322
Samu Onkalo2db4a762009-12-14 18:01:43 -0800323 if (lis3->pdata) {
324 int i;
325 for (i = 0; i < 3; i++) {
326 /* Check against selftest acceptance limits */
327 if ((results[i] < lis3->pdata->st_min_limits[i]) ||
328 (results[i] > lis3->pdata->st_max_limits[i])) {
Samu Onkalo029756d2010-10-22 07:57:32 -0400329 ret = SELFTEST_FAIL;
Samu Onkalo2db4a762009-12-14 18:01:43 -0800330 goto fail;
331 }
332 }
333 }
334
335 /* test passed */
336fail:
337 mutex_unlock(&lis3->mutex);
338 return ret;
339}
340
Samu Onkalof9deb412010-10-22 07:57:24 -0400341/*
342 * Order of registers in the list affects to order of the restore process.
343 * Perhaps it is a good idea to set interrupt enable register as a last one
344 * after all other configurations
345 */
346static u8 lis3_wai8_regs[] = { FF_WU_CFG_1, FF_WU_THS_1, FF_WU_DURATION_1,
347 FF_WU_CFG_2, FF_WU_THS_2, FF_WU_DURATION_2,
348 CLICK_CFG, CLICK_SRC, CLICK_THSY_X, CLICK_THSZ,
349 CLICK_TIMELIMIT, CLICK_LATENCY, CLICK_WINDOW,
350 CTRL_REG1, CTRL_REG2, CTRL_REG3};
351
352static u8 lis3_wai12_regs[] = {FF_WU_CFG, FF_WU_THS_L, FF_WU_THS_H,
353 FF_WU_DURATION, DD_CFG, DD_THSI_L, DD_THSI_H,
354 DD_THSE_L, DD_THSE_H,
355 CTRL_REG1, CTRL_REG3, CTRL_REG2};
356
357static inline void lis3_context_save(struct lis3lv02d *lis3)
358{
359 int i;
360 for (i = 0; i < lis3->regs_size; i++)
361 lis3->read(lis3, lis3->regs[i], &lis3->reg_cache[i]);
362 lis3->regs_stored = true;
363}
364
365static inline void lis3_context_restore(struct lis3lv02d *lis3)
366{
367 int i;
368 if (lis3->regs_stored)
369 for (i = 0; i < lis3->regs_size; i++)
370 lis3->write(lis3, lis3->regs[i], lis3->reg_cache[i]);
371}
372
Daniel Macka38da2e2009-03-31 15:24:32 -0700373void lis3lv02d_poweroff(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800374{
Samu Onkalof9deb412010-10-22 07:57:24 -0400375 if (lis3->reg_ctrl)
376 lis3_context_save(lis3);
Eric Piela002ee82009-06-16 15:34:14 -0700377 /* disable X,Y,Z axis and power down */
378 lis3->write(lis3, CTRL_REG1, 0x00);
Samu Onkalof9deb412010-10-22 07:57:24 -0400379 if (lis3->reg_ctrl)
380 lis3->reg_ctrl(lis3, LIS3_REG_OFF);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800381}
Eric Pielcfce41a2009-01-09 16:41:01 -0800382EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800383
Éric Piel1510dd52011-10-31 17:10:31 -0700384int lis3lv02d_poweron(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800385{
Éric Piel1510dd52011-10-31 17:10:31 -0700386 int err;
Eric Piela002ee82009-06-16 15:34:14 -0700387 u8 reg;
388
389 lis3->init(lis3);
390
391 /*
392 * Common configuration
Éric Piel4b5d95b2009-12-14 18:01:40 -0800393 * BDU: (12 bits sensors only) LSB and MSB values are not updated until
394 * both have been read. So the value read will always be correct.
Samu Onkalo2a7fade2010-10-22 07:57:27 -0400395 * Set BOOT bit to refresh factory tuning values.
Eric Piela002ee82009-06-16 15:34:14 -0700396 */
Takashi Iwai05faadc2011-10-03 18:09:14 -0700397 if (lis3->pdata) {
398 lis3->read(lis3, CTRL_REG2, &reg);
399 if (lis3->whoami == WAI_12B)
400 reg |= CTRL2_BDU | CTRL2_BOOT;
401 else
402 reg |= CTRL2_BOOT_8B;
403 lis3->write(lis3, CTRL_REG2, reg);
404 }
Samu Onkalo2a7fade2010-10-22 07:57:27 -0400405
Éric Piel1510dd52011-10-31 17:10:31 -0700406 err = lis3lv02d_get_pwron_wait(lis3);
407 if (err)
408 return err;
Samu Onkalo2a7fade2010-10-22 07:57:27 -0400409
Samu Onkalof9deb412010-10-22 07:57:24 -0400410 if (lis3->reg_ctrl)
411 lis3_context_restore(lis3);
Éric Piel1510dd52011-10-31 17:10:31 -0700412
413 return 0;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800414}
Eric Pielcfce41a2009-01-09 16:41:01 -0800415EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800416
Pavel Machek455fbdd2008-11-12 13:27:02 -0800417
Samu Onkalo6d94d402010-05-24 14:33:37 -0700418static void lis3lv02d_joystick_poll(struct input_polled_dev *pidev)
419{
Éric Piel895c1562011-10-31 17:11:05 -0700420 struct lis3lv02d *lis3 = pidev->private;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700421 int x, y, z;
422
Éric Piel895c1562011-10-31 17:11:05 -0700423 mutex_lock(&lis3->mutex);
424 lis3lv02d_get_xyz(lis3, &x, &y, &z);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700425 input_report_abs(pidev->input, ABS_X, x);
426 input_report_abs(pidev->input, ABS_Y, y);
427 input_report_abs(pidev->input, ABS_Z, z);
428 input_sync(pidev->input);
Éric Piel895c1562011-10-31 17:11:05 -0700429 mutex_unlock(&lis3->mutex);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700430}
431
Samu Onkalo2a346992010-10-22 07:57:23 -0400432static void lis3lv02d_joystick_open(struct input_polled_dev *pidev)
433{
Éric Piel895c1562011-10-31 17:11:05 -0700434 struct lis3lv02d *lis3 = pidev->private;
Samu Onkaloe7261112010-10-22 07:57:25 -0400435
Éric Piel895c1562011-10-31 17:11:05 -0700436 if (lis3->pm_dev)
437 pm_runtime_get_sync(lis3->pm_dev);
438
439 if (lis3->pdata && lis3->whoami == WAI_8B && lis3->idev)
440 atomic_set(&lis3->wake_thread, 1);
Samu Onkalo821f6642010-10-22 07:57:26 -0400441 /*
442 * Update coordinates for the case where poll interval is 0 and
443 * the chip in running purely under interrupt control
444 */
445 lis3lv02d_joystick_poll(pidev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400446}
447
448static void lis3lv02d_joystick_close(struct input_polled_dev *pidev)
449{
Éric Piel895c1562011-10-31 17:11:05 -0700450 struct lis3lv02d *lis3 = pidev->private;
451
452 atomic_set(&lis3->wake_thread, 0);
453 if (lis3->pm_dev)
454 pm_runtime_put(lis3->pm_dev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400455}
456
Éric Piel895c1562011-10-31 17:11:05 -0700457static irqreturn_t lis302dl_interrupt(int irq, void *data)
Pavel Machekef2cfc72009-02-18 14:48:23 -0800458{
Éric Piel895c1562011-10-31 17:11:05 -0700459 struct lis3lv02d *lis3 = data;
460
461 if (!test_bit(0, &lis3->misc_opened))
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700462 goto out;
463
Pavel Machekef2cfc72009-02-18 14:48:23 -0800464 /*
465 * Be careful: on some HP laptops the bios force DD when on battery and
466 * the lid is closed. This leads to interrupts as soon as a little move
467 * is done.
468 */
Éric Piel895c1562011-10-31 17:11:05 -0700469 atomic_inc(&lis3->count);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800470
Éric Piel895c1562011-10-31 17:11:05 -0700471 wake_up_interruptible(&lis3->misc_wait);
472 kill_fasync(&lis3->async_queue, SIGIO, POLL_IN);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700473out:
Éric Piel895c1562011-10-31 17:11:05 -0700474 if (atomic_read(&lis3->wake_thread))
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700475 return IRQ_WAKE_THREAD;
476 return IRQ_HANDLED;
477}
478
Samu Onkalo6d94d402010-05-24 14:33:37 -0700479static void lis302dl_interrupt_handle_click(struct lis3lv02d *lis3)
480{
481 struct input_dev *dev = lis3->idev->input;
482 u8 click_src;
483
484 mutex_lock(&lis3->mutex);
485 lis3->read(lis3, CLICK_SRC, &click_src);
486
487 if (click_src & CLICK_SINGLE_X) {
488 input_report_key(dev, lis3->mapped_btns[0], 1);
489 input_report_key(dev, lis3->mapped_btns[0], 0);
490 }
491
492 if (click_src & CLICK_SINGLE_Y) {
493 input_report_key(dev, lis3->mapped_btns[1], 1);
494 input_report_key(dev, lis3->mapped_btns[1], 0);
495 }
496
497 if (click_src & CLICK_SINGLE_Z) {
498 input_report_key(dev, lis3->mapped_btns[2], 1);
499 input_report_key(dev, lis3->mapped_btns[2], 0);
500 }
501 input_sync(dev);
502 mutex_unlock(&lis3->mutex);
503}
504
Samu Onkalo029756d2010-10-22 07:57:32 -0400505static inline void lis302dl_data_ready(struct lis3lv02d *lis3, int index)
506{
507 int dummy;
508
509 /* Dummy read to ack interrupt */
510 lis3lv02d_get_xyz(lis3, &dummy, &dummy, &dummy);
511 lis3->data_ready_count[index]++;
512}
513
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700514static irqreturn_t lis302dl_interrupt_thread1_8b(int irq, void *data)
515{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700516 struct lis3lv02d *lis3 = data;
Samu Onkalo029756d2010-10-22 07:57:32 -0400517 u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ1_MASK;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700518
Samu Onkalo029756d2010-10-22 07:57:32 -0400519 if (irq_cfg == LIS3_IRQ1_CLICK)
Samu Onkalo6d94d402010-05-24 14:33:37 -0700520 lis302dl_interrupt_handle_click(lis3);
Samu Onkalo029756d2010-10-22 07:57:32 -0400521 else if (unlikely(irq_cfg == LIS3_IRQ1_DATA_READY))
522 lis302dl_data_ready(lis3, IRQ_LINE0);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700523 else
Samu Onkaloe7261112010-10-22 07:57:25 -0400524 lis3lv02d_joystick_poll(lis3->idev);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700525
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700526 return IRQ_HANDLED;
527}
528
529static irqreturn_t lis302dl_interrupt_thread2_8b(int irq, void *data)
530{
Samu Onkalo6d94d402010-05-24 14:33:37 -0700531 struct lis3lv02d *lis3 = data;
Samu Onkalo029756d2010-10-22 07:57:32 -0400532 u8 irq_cfg = lis3->irq_cfg & LIS3_IRQ2_MASK;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700533
Samu Onkalo029756d2010-10-22 07:57:32 -0400534 if (irq_cfg == LIS3_IRQ2_CLICK)
Samu Onkalo6d94d402010-05-24 14:33:37 -0700535 lis302dl_interrupt_handle_click(lis3);
Samu Onkalo029756d2010-10-22 07:57:32 -0400536 else if (unlikely(irq_cfg == LIS3_IRQ2_DATA_READY))
537 lis302dl_data_ready(lis3, IRQ_LINE1);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700538 else
Samu Onkaloe7261112010-10-22 07:57:25 -0400539 lis3lv02d_joystick_poll(lis3->idev);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700540
Pavel Machekef2cfc72009-02-18 14:48:23 -0800541 return IRQ_HANDLED;
542}
543
544static int lis3lv02d_misc_open(struct inode *inode, struct file *file)
545{
Éric Piel895c1562011-10-31 17:11:05 -0700546 struct lis3lv02d *lis3 = container_of(file->private_data,
547 struct lis3lv02d, miscdev);
548
549 if (test_and_set_bit(0, &lis3->misc_opened))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800550 return -EBUSY; /* already open */
551
Éric Piel895c1562011-10-31 17:11:05 -0700552 if (lis3->pm_dev)
553 pm_runtime_get_sync(lis3->pm_dev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400554
Éric Piel895c1562011-10-31 17:11:05 -0700555 atomic_set(&lis3->count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800556 return 0;
557}
558
559static int lis3lv02d_misc_release(struct inode *inode, struct file *file)
560{
Éric Piel895c1562011-10-31 17:11:05 -0700561 struct lis3lv02d *lis3 = container_of(file->private_data,
562 struct lis3lv02d, miscdev);
563
564 fasync_helper(-1, file, 0, &lis3->async_queue);
565 clear_bit(0, &lis3->misc_opened); /* release the device */
566 if (lis3->pm_dev)
567 pm_runtime_put(lis3->pm_dev);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800568 return 0;
569}
570
571static ssize_t lis3lv02d_misc_read(struct file *file, char __user *buf,
572 size_t count, loff_t *pos)
573{
Éric Piel895c1562011-10-31 17:11:05 -0700574 struct lis3lv02d *lis3 = container_of(file->private_data,
575 struct lis3lv02d, miscdev);
576
Pavel Machekef2cfc72009-02-18 14:48:23 -0800577 DECLARE_WAITQUEUE(wait, current);
578 u32 data;
579 unsigned char byte_data;
580 ssize_t retval = 1;
581
582 if (count < 1)
583 return -EINVAL;
584
Éric Piel895c1562011-10-31 17:11:05 -0700585 add_wait_queue(&lis3->misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800586 while (true) {
587 set_current_state(TASK_INTERRUPTIBLE);
Éric Piel895c1562011-10-31 17:11:05 -0700588 data = atomic_xchg(&lis3->count, 0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800589 if (data)
590 break;
591
592 if (file->f_flags & O_NONBLOCK) {
593 retval = -EAGAIN;
594 goto out;
595 }
596
597 if (signal_pending(current)) {
598 retval = -ERESTARTSYS;
599 goto out;
600 }
601
602 schedule();
603 }
604
605 if (data < 255)
606 byte_data = data;
607 else
608 byte_data = 255;
609
610 /* make sure we are not going into copy_to_user() with
611 * TASK_INTERRUPTIBLE state */
612 set_current_state(TASK_RUNNING);
613 if (copy_to_user(buf, &byte_data, sizeof(byte_data)))
614 retval = -EFAULT;
615
616out:
617 __set_current_state(TASK_RUNNING);
Éric Piel895c1562011-10-31 17:11:05 -0700618 remove_wait_queue(&lis3->misc_wait, &wait);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800619
620 return retval;
621}
622
623static unsigned int lis3lv02d_misc_poll(struct file *file, poll_table *wait)
624{
Éric Piel895c1562011-10-31 17:11:05 -0700625 struct lis3lv02d *lis3 = container_of(file->private_data,
626 struct lis3lv02d, miscdev);
627
628 poll_wait(file, &lis3->misc_wait, wait);
629 if (atomic_read(&lis3->count))
Pavel Machekef2cfc72009-02-18 14:48:23 -0800630 return POLLIN | POLLRDNORM;
631 return 0;
632}
633
634static int lis3lv02d_misc_fasync(int fd, struct file *file, int on)
635{
Éric Piel895c1562011-10-31 17:11:05 -0700636 struct lis3lv02d *lis3 = container_of(file->private_data,
637 struct lis3lv02d, miscdev);
638
639 return fasync_helper(fd, file, on, &lis3->async_queue);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800640}
641
642static const struct file_operations lis3lv02d_misc_fops = {
643 .owner = THIS_MODULE,
644 .llseek = no_llseek,
645 .read = lis3lv02d_misc_read,
646 .open = lis3lv02d_misc_open,
647 .release = lis3lv02d_misc_release,
648 .poll = lis3lv02d_misc_poll,
649 .fasync = lis3lv02d_misc_fasync,
650};
651
Éric Piele1e56872011-10-31 17:11:02 -0700652int lis3lv02d_joystick_enable(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800653{
Eric Pieldc6ea972009-06-16 15:34:15 -0700654 struct input_dev *input_dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800655 int err;
Samu Onkalo32496c72009-12-14 18:01:46 -0800656 int max_val, fuzz, flat;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700657 int btns[] = {BTN_X, BTN_Y, BTN_Z};
Pavel Machek455fbdd2008-11-12 13:27:02 -0800658
Éric Piel895c1562011-10-31 17:11:05 -0700659 if (lis3->idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800660 return -EINVAL;
661
Éric Piel895c1562011-10-31 17:11:05 -0700662 lis3->idev = input_allocate_polled_device();
663 if (!lis3->idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800664 return -ENOMEM;
665
Éric Piel895c1562011-10-31 17:11:05 -0700666 lis3->idev->poll = lis3lv02d_joystick_poll;
667 lis3->idev->open = lis3lv02d_joystick_open;
668 lis3->idev->close = lis3lv02d_joystick_close;
669 lis3->idev->poll_interval = MDPS_POLL_INTERVAL;
670 lis3->idev->poll_interval_min = MDPS_POLL_MIN;
671 lis3->idev->poll_interval_max = MDPS_POLL_MAX;
672 lis3->idev->private = lis3;
673 input_dev = lis3->idev->input;
Eric Pieldc6ea972009-06-16 15:34:15 -0700674
Eric Pieldc6ea972009-06-16 15:34:15 -0700675 input_dev->name = "ST LIS3LV02DL Accelerometer";
676 input_dev->phys = DRIVER_NAME "/input0";
677 input_dev->id.bustype = BUS_HOST;
678 input_dev->id.vendor = 0;
Éric Piel895c1562011-10-31 17:11:05 -0700679 input_dev->dev.parent = &lis3->pdev->dev;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800680
Eric Pieldc6ea972009-06-16 15:34:15 -0700681 set_bit(EV_ABS, input_dev->evbit);
Éric Piel895c1562011-10-31 17:11:05 -0700682 max_val = (lis3->mdps_max_val * lis3->scale) / LIS3_ACCURACY;
683 if (lis3->whoami == WAI_12B) {
Samu Onkalo477bc912010-10-22 07:57:30 -0400684 fuzz = LIS3_DEFAULT_FUZZ_12B;
685 flat = LIS3_DEFAULT_FLAT_12B;
686 } else {
687 fuzz = LIS3_DEFAULT_FUZZ_8B;
688 flat = LIS3_DEFAULT_FLAT_8B;
689 }
Éric Piel895c1562011-10-31 17:11:05 -0700690 fuzz = (fuzz * lis3->scale) / LIS3_ACCURACY;
691 flat = (flat * lis3->scale) / LIS3_ACCURACY;
Samu Onkalo477bc912010-10-22 07:57:30 -0400692
Samu Onkalo32496c72009-12-14 18:01:46 -0800693 input_set_abs_params(input_dev, ABS_X, -max_val, max_val, fuzz, flat);
694 input_set_abs_params(input_dev, ABS_Y, -max_val, max_val, fuzz, flat);
695 input_set_abs_params(input_dev, ABS_Z, -max_val, max_val, fuzz, flat);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800696
Éric Piel895c1562011-10-31 17:11:05 -0700697 lis3->mapped_btns[0] = lis3lv02d_get_axis(abs(lis3->ac.x), btns);
698 lis3->mapped_btns[1] = lis3lv02d_get_axis(abs(lis3->ac.y), btns);
699 lis3->mapped_btns[2] = lis3lv02d_get_axis(abs(lis3->ac.z), btns);
Samu Onkalo6d94d402010-05-24 14:33:37 -0700700
Éric Piel895c1562011-10-31 17:11:05 -0700701 err = input_register_polled_device(lis3->idev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800702 if (err) {
Éric Piel895c1562011-10-31 17:11:05 -0700703 input_free_polled_device(lis3->idev);
704 lis3->idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800705 }
706
707 return err;
708}
Eric Pielcfce41a2009-01-09 16:41:01 -0800709EXPORT_SYMBOL_GPL(lis3lv02d_joystick_enable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800710
Éric Piele1e56872011-10-31 17:11:02 -0700711void lis3lv02d_joystick_disable(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800712{
Éric Piel895c1562011-10-31 17:11:05 -0700713 if (lis3->irq)
714 free_irq(lis3->irq, lis3);
715 if (lis3->pdata && lis3->pdata->irq2)
716 free_irq(lis3->pdata->irq2, lis3);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700717
Éric Piel895c1562011-10-31 17:11:05 -0700718 if (!lis3->idev)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800719 return;
720
Éric Piel895c1562011-10-31 17:11:05 -0700721 if (lis3->irq)
722 misc_deregister(&lis3->miscdev);
723 input_unregister_polled_device(lis3->idev);
724 input_free_polled_device(lis3->idev);
725 lis3->idev = NULL;
Pavel Machek455fbdd2008-11-12 13:27:02 -0800726}
Eric Pielcfce41a2009-01-09 16:41:01 -0800727EXPORT_SYMBOL_GPL(lis3lv02d_joystick_disable);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800728
Pavel Machek455fbdd2008-11-12 13:27:02 -0800729/* Sysfs stuff */
Samu Onkalo2a346992010-10-22 07:57:23 -0400730static void lis3lv02d_sysfs_poweron(struct lis3lv02d *lis3)
731{
732 /*
733 * SYSFS functions are fast visitors so put-call
734 * immediately after the get-call. However, keep
735 * chip running for a while and schedule delayed
736 * suspend. This way periodic sysfs calls doesn't
737 * suffer from relatively long power up time.
738 */
739
740 if (lis3->pm_dev) {
741 pm_runtime_get_sync(lis3->pm_dev);
742 pm_runtime_put_noidle(lis3->pm_dev);
743 pm_schedule_suspend(lis3->pm_dev, LIS3_SYSFS_POWERDOWN_DELAY);
744 }
745}
746
Samu Onkalo2db4a762009-12-14 18:01:43 -0800747static ssize_t lis3lv02d_selftest_show(struct device *dev,
748 struct device_attribute *attr, char *buf)
749{
Éric Piel895c1562011-10-31 17:11:05 -0700750 struct lis3lv02d *lis3 = dev_get_drvdata(dev);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800751 s16 values[3];
752
Samu Onkalo029756d2010-10-22 07:57:32 -0400753 static const char ok[] = "OK";
754 static const char fail[] = "FAIL";
755 static const char irq[] = "FAIL_IRQ";
756 const char *res;
757
Éric Piel895c1562011-10-31 17:11:05 -0700758 lis3lv02d_sysfs_poweron(lis3);
759 switch (lis3lv02d_selftest(lis3, values)) {
Samu Onkalo029756d2010-10-22 07:57:32 -0400760 case SELFTEST_FAIL:
761 res = fail;
762 break;
763 case SELFTEST_IRQ:
764 res = irq;
765 break;
766 case SELFTEST_OK:
767 default:
768 res = ok;
769 break;
770 }
771 return sprintf(buf, "%s %d %d %d\n", res,
Samu Onkalo2db4a762009-12-14 18:01:43 -0800772 values[0], values[1], values[2]);
773}
774
Pavel Machek455fbdd2008-11-12 13:27:02 -0800775static ssize_t lis3lv02d_position_show(struct device *dev,
776 struct device_attribute *attr, char *buf)
777{
Éric Piel895c1562011-10-31 17:11:05 -0700778 struct lis3lv02d *lis3 = dev_get_drvdata(dev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800779 int x, y, z;
780
Éric Piel895c1562011-10-31 17:11:05 -0700781 lis3lv02d_sysfs_poweron(lis3);
782 mutex_lock(&lis3->mutex);
783 lis3lv02d_get_xyz(lis3, &x, &y, &z);
784 mutex_unlock(&lis3->mutex);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800785 return sprintf(buf, "(%d,%d,%d)\n", x, y, z);
786}
787
Pavel Machek455fbdd2008-11-12 13:27:02 -0800788static ssize_t lis3lv02d_rate_show(struct device *dev,
789 struct device_attribute *attr, char *buf)
790{
Éric Piel895c1562011-10-31 17:11:05 -0700791 struct lis3lv02d *lis3 = dev_get_drvdata(dev);
792
793 lis3lv02d_sysfs_poweron(lis3);
794 return sprintf(buf, "%d\n", lis3lv02d_get_odr(lis3));
Pavel Machek455fbdd2008-11-12 13:27:02 -0800795}
796
Samu Onkaloa253aae2009-12-14 18:01:44 -0800797static ssize_t lis3lv02d_rate_set(struct device *dev,
798 struct device_attribute *attr, const char *buf,
799 size_t count)
800{
Éric Piel895c1562011-10-31 17:11:05 -0700801 struct lis3lv02d *lis3 = dev_get_drvdata(dev);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800802 unsigned long rate;
803
804 if (strict_strtoul(buf, 0, &rate))
805 return -EINVAL;
806
Éric Piel895c1562011-10-31 17:11:05 -0700807 lis3lv02d_sysfs_poweron(lis3);
808 if (lis3lv02d_set_odr(lis3, rate))
Samu Onkaloa253aae2009-12-14 18:01:44 -0800809 return -EINVAL;
810
811 return count;
812}
813
Samu Onkalo2db4a762009-12-14 18:01:43 -0800814static DEVICE_ATTR(selftest, S_IRUSR, lis3lv02d_selftest_show, NULL);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800815static DEVICE_ATTR(position, S_IRUGO, lis3lv02d_position_show, NULL);
Samu Onkaloa253aae2009-12-14 18:01:44 -0800816static DEVICE_ATTR(rate, S_IRUGO | S_IWUSR, lis3lv02d_rate_show,
817 lis3lv02d_rate_set);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800818
819static struct attribute *lis3lv02d_attributes[] = {
Samu Onkalo2db4a762009-12-14 18:01:43 -0800820 &dev_attr_selftest.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800821 &dev_attr_position.attr,
Pavel Machek455fbdd2008-11-12 13:27:02 -0800822 &dev_attr_rate.attr,
823 NULL
824};
825
826static struct attribute_group lis3lv02d_attribute_group = {
827 .attrs = lis3lv02d_attributes
828};
829
Eric Pielcfce41a2009-01-09 16:41:01 -0800830
Daniel Macka38da2e2009-03-31 15:24:32 -0700831static int lis3lv02d_add_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800832{
Eric Piela002ee82009-06-16 15:34:14 -0700833 lis3->pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
834 if (IS_ERR(lis3->pdev))
835 return PTR_ERR(lis3->pdev);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800836
Éric Piel895c1562011-10-31 17:11:05 -0700837 platform_set_drvdata(lis3->pdev, lis3);
Eric Piela002ee82009-06-16 15:34:14 -0700838 return sysfs_create_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800839}
840
Eric Piela002ee82009-06-16 15:34:14 -0700841int lis3lv02d_remove_fs(struct lis3lv02d *lis3)
Pavel Machek455fbdd2008-11-12 13:27:02 -0800842{
Eric Piela002ee82009-06-16 15:34:14 -0700843 sysfs_remove_group(&lis3->pdev->dev.kobj, &lis3lv02d_attribute_group);
844 platform_device_unregister(lis3->pdev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400845 if (lis3->pm_dev) {
846 /* Barrier after the sysfs remove */
847 pm_runtime_barrier(lis3->pm_dev);
848
849 /* SYSFS may have left chip running. Turn off if necessary */
850 if (!pm_runtime_suspended(lis3->pm_dev))
Éric Piel895c1562011-10-31 17:11:05 -0700851 lis3lv02d_poweroff(lis3);
Samu Onkalo2a346992010-10-22 07:57:23 -0400852
853 pm_runtime_disable(lis3->pm_dev);
854 pm_runtime_set_suspended(lis3->pm_dev);
855 }
Samu Onkalof9deb412010-10-22 07:57:24 -0400856 kfree(lis3->reg_cache);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800857 return 0;
858}
Eric Pielcfce41a2009-01-09 16:41:01 -0800859EXPORT_SYMBOL_GPL(lis3lv02d_remove_fs);
Pavel Machek455fbdd2008-11-12 13:27:02 -0800860
Éric Pield7f81d42011-10-31 17:10:58 -0700861static void lis3lv02d_8b_configure(struct lis3lv02d *lis3,
Samu Onkaloecc437a2010-05-24 14:33:34 -0700862 struct lis3lv02d_platform_data *p)
863{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700864 int err;
Samu Onkalo342c5f12010-05-24 14:33:35 -0700865 int ctrl2 = p->hipass_ctrl;
866
Samu Onkaloecc437a2010-05-24 14:33:34 -0700867 if (p->click_flags) {
Éric Pield7f81d42011-10-31 17:10:58 -0700868 lis3->write(lis3, CLICK_CFG, p->click_flags);
869 lis3->write(lis3, CLICK_TIMELIMIT, p->click_time_limit);
870 lis3->write(lis3, CLICK_LATENCY, p->click_latency);
871 lis3->write(lis3, CLICK_WINDOW, p->click_window);
872 lis3->write(lis3, CLICK_THSZ, p->click_thresh_z & 0xf);
873 lis3->write(lis3, CLICK_THSY_X,
Samu Onkaloecc437a2010-05-24 14:33:34 -0700874 (p->click_thresh_x & 0xf) |
875 (p->click_thresh_y << 4));
Samu Onkalo6d94d402010-05-24 14:33:37 -0700876
Éric Pield7f81d42011-10-31 17:10:58 -0700877 if (lis3->idev) {
Éric Piel895c1562011-10-31 17:11:05 -0700878 struct input_dev *input_dev = lis3->idev->input;
Samu Onkalo6d94d402010-05-24 14:33:37 -0700879 input_set_capability(input_dev, EV_KEY, BTN_X);
880 input_set_capability(input_dev, EV_KEY, BTN_Y);
881 input_set_capability(input_dev, EV_KEY, BTN_Z);
882 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700883 }
884
885 if (p->wakeup_flags) {
Éric Pield7f81d42011-10-31 17:10:58 -0700886 lis3->write(lis3, FF_WU_CFG_1, p->wakeup_flags);
887 lis3->write(lis3, FF_WU_THS_1, p->wakeup_thresh & 0x7f);
Samu Onkalocc23aa12010-10-22 07:57:29 -0400888 /* pdata value + 1 to keep this backward compatible*/
Éric Pield7f81d42011-10-31 17:10:58 -0700889 lis3->write(lis3, FF_WU_DURATION_1, p->duration1 + 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700890 ctrl2 ^= HP_FF_WU1; /* Xor to keep compatible with old pdata*/
Samu Onkaloecc437a2010-05-24 14:33:34 -0700891 }
Samu Onkalo342c5f12010-05-24 14:33:35 -0700892
893 if (p->wakeup_flags2) {
Éric Pield7f81d42011-10-31 17:10:58 -0700894 lis3->write(lis3, FF_WU_CFG_2, p->wakeup_flags2);
895 lis3->write(lis3, FF_WU_THS_2, p->wakeup_thresh2 & 0x7f);
Samu Onkalocc23aa12010-10-22 07:57:29 -0400896 /* pdata value + 1 to keep this backward compatible*/
Éric Pield7f81d42011-10-31 17:10:58 -0700897 lis3->write(lis3, FF_WU_DURATION_2, p->duration2 + 1);
Samu Onkalo342c5f12010-05-24 14:33:35 -0700898 ctrl2 ^= HP_FF_WU2; /* Xor to keep compatible with old pdata*/
899 }
900 /* Configure hipass filters */
Éric Pield7f81d42011-10-31 17:10:58 -0700901 lis3->write(lis3, CTRL_REG2, ctrl2);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700902
903 if (p->irq2) {
904 err = request_threaded_irq(p->irq2,
905 NULL,
906 lis302dl_interrupt_thread2_8b,
Samu Onkalocc23aa12010-10-22 07:57:29 -0400907 IRQF_TRIGGER_RISING | IRQF_ONESHOT |
908 (p->irq_flags2 & IRQF_TRIGGER_MASK),
Éric Piel895c1562011-10-31 17:11:05 -0700909 DRIVER_NAME, lis3);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700910 if (err < 0)
Joe Perches63366d32010-10-20 06:51:40 +0000911 pr_err("No second IRQ. Limited functionality\n");
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700912 }
Samu Onkaloecc437a2010-05-24 14:33:34 -0700913}
914
Daniel Mackab337a62009-03-31 15:24:31 -0700915/*
916 * Initialise the accelerometer and the various subsystems.
Éric Pielbc62c142009-12-14 18:01:39 -0800917 * Should be rather independent of the bus system.
Daniel Mackab337a62009-03-31 15:24:31 -0700918 */
Éric Pield7f81d42011-10-31 17:10:58 -0700919int lis3lv02d_init_device(struct lis3lv02d *lis3)
Daniel Mackab337a62009-03-31 15:24:31 -0700920{
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700921 int err;
922 irq_handler_t thread_fn;
Samu Onkalocc23aa12010-10-22 07:57:29 -0400923 int irq_flags = 0;
Samu Onkalo92ba4fe2010-05-24 14:33:36 -0700924
Éric Pield7f81d42011-10-31 17:10:58 -0700925 lis3->whoami = lis3lv02d_read_8(lis3, WHO_AM_I);
Daniel Macka38da2e2009-03-31 15:24:32 -0700926
Éric Pield7f81d42011-10-31 17:10:58 -0700927 switch (lis3->whoami) {
Éric Pielbc62c142009-12-14 18:01:39 -0800928 case WAI_12B:
Joe Perches63366d32010-10-20 06:51:40 +0000929 pr_info("12 bits sensor found\n");
Éric Pield7f81d42011-10-31 17:10:58 -0700930 lis3->read_data = lis3lv02d_read_12;
931 lis3->mdps_max_val = 2048;
932 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_12B;
933 lis3->odrs = lis3_12_rates;
934 lis3->odr_mask = CTRL1_DF0 | CTRL1_DF1;
935 lis3->scale = LIS3_SENSITIVITY_12B;
936 lis3->regs = lis3_wai12_regs;
937 lis3->regs_size = ARRAY_SIZE(lis3_wai12_regs);
Daniel Macka38da2e2009-03-31 15:24:32 -0700938 break;
Éric Pielbc62c142009-12-14 18:01:39 -0800939 case WAI_8B:
Joe Perches63366d32010-10-20 06:51:40 +0000940 pr_info("8 bits sensor found\n");
Éric Pield7f81d42011-10-31 17:10:58 -0700941 lis3->read_data = lis3lv02d_read_8;
942 lis3->mdps_max_val = 128;
943 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
944 lis3->odrs = lis3_8_rates;
945 lis3->odr_mask = CTRL1_DR;
946 lis3->scale = LIS3_SENSITIVITY_8B;
947 lis3->regs = lis3_wai8_regs;
948 lis3->regs_size = ARRAY_SIZE(lis3_wai8_regs);
Daniel Macka38da2e2009-03-31 15:24:32 -0700949 break;
Takashi Iwai78537c32010-09-23 10:01:39 -0700950 case WAI_3DC:
Joe Perches63366d32010-10-20 06:51:40 +0000951 pr_info("8 bits 3DC sensor found\n");
Éric Pield7f81d42011-10-31 17:10:58 -0700952 lis3->read_data = lis3lv02d_read_8;
953 lis3->mdps_max_val = 128;
954 lis3->pwron_delay = LIS3_PWRON_DELAY_WAI_8B;
955 lis3->odrs = lis3_3dc_rates;
956 lis3->odr_mask = CTRL1_ODR0|CTRL1_ODR1|CTRL1_ODR2|CTRL1_ODR3;
957 lis3->scale = LIS3_SENSITIVITY_8B;
Takashi Iwai78537c32010-09-23 10:01:39 -0700958 break;
Daniel Macka38da2e2009-03-31 15:24:32 -0700959 default:
Éric Pield7f81d42011-10-31 17:10:58 -0700960 pr_err("unknown sensor type 0x%X\n", lis3->whoami);
Daniel Macka38da2e2009-03-31 15:24:32 -0700961 return -EINVAL;
962 }
963
Éric Pield7f81d42011-10-31 17:10:58 -0700964 lis3->reg_cache = kzalloc(max(sizeof(lis3_wai8_regs),
Samu Onkalof9deb412010-10-22 07:57:24 -0400965 sizeof(lis3_wai12_regs)), GFP_KERNEL);
966
Éric Pield7f81d42011-10-31 17:10:58 -0700967 if (lis3->reg_cache == NULL) {
Samu Onkalof9deb412010-10-22 07:57:24 -0400968 printk(KERN_ERR DRIVER_NAME "out of memory\n");
969 return -ENOMEM;
970 }
971
Éric Pield7f81d42011-10-31 17:10:58 -0700972 mutex_init(&lis3->mutex);
973 atomic_set(&lis3->wake_thread, 0);
Samu Onkalo2db4a762009-12-14 18:01:43 -0800974
Éric Pield7f81d42011-10-31 17:10:58 -0700975 lis3lv02d_add_fs(lis3);
976 err = lis3lv02d_poweron(lis3);
Éric Piel1510dd52011-10-31 17:10:31 -0700977 if (err) {
Éric Pield7f81d42011-10-31 17:10:58 -0700978 lis3lv02d_remove_fs(lis3);
Éric Piel1510dd52011-10-31 17:10:31 -0700979 return err;
980 }
Daniel Mackab337a62009-03-31 15:24:31 -0700981
Éric Pield7f81d42011-10-31 17:10:58 -0700982 if (lis3->pm_dev) {
983 pm_runtime_set_active(lis3->pm_dev);
984 pm_runtime_enable(lis3->pm_dev);
Samu Onkalo2a346992010-10-22 07:57:23 -0400985 }
986
Éric Piele1e56872011-10-31 17:11:02 -0700987 if (lis3lv02d_joystick_enable(lis3))
Joe Perches63366d32010-10-20 06:51:40 +0000988 pr_err("joystick initialization failed\n");
Daniel Mackab337a62009-03-31 15:24:31 -0700989
Daniel Mack8f3128e2009-06-16 15:34:17 -0700990 /* passing in platform specific data is purely optional and only
991 * used by the SPI transport layer at the moment */
Éric Pield7f81d42011-10-31 17:10:58 -0700992 if (lis3->pdata) {
993 struct lis3lv02d_platform_data *p = lis3->pdata;
Daniel Mack8f3128e2009-06-16 15:34:17 -0700994
Éric Pield7f81d42011-10-31 17:10:58 -0700995 if (lis3->whoami == WAI_8B)
996 lis3lv02d_8b_configure(lis3, p);
Daniel Mack8873c332009-09-21 17:04:43 -0700997
Samu Onkalocc23aa12010-10-22 07:57:29 -0400998 irq_flags = p->irq_flags1 & IRQF_TRIGGER_MASK;
999
Éric Pield7f81d42011-10-31 17:10:58 -07001000 lis3->irq_cfg = p->irq_cfg;
Daniel Mack8f3128e2009-06-16 15:34:17 -07001001 if (p->irq_cfg)
Éric Pield7f81d42011-10-31 17:10:58 -07001002 lis3->write(lis3, CTRL_REG3, p->irq_cfg);
Samu Onkalocc23aa12010-10-22 07:57:29 -04001003
1004 if (p->default_rate)
Éric Piel895c1562011-10-31 17:11:05 -07001005 lis3lv02d_set_odr(lis3, p->default_rate);
Daniel Mack8f3128e2009-06-16 15:34:17 -07001006 }
1007
Daniel Macka38da2e2009-03-31 15:24:32 -07001008 /* bail if we did not get an IRQ from the bus layer */
Éric Pield7f81d42011-10-31 17:10:58 -07001009 if (!lis3->irq) {
Kalhan Trisala20f0bc2011-01-25 14:24:37 +00001010 pr_debug("No IRQ. Disabling /dev/freefall\n");
Daniel Mackab337a62009-03-31 15:24:31 -07001011 goto out;
1012 }
1013
Samu Onkalo92ba4fe2010-05-24 14:33:36 -07001014 /*
1015 * The sensor can generate interrupts for free-fall and direction
1016 * detection (distinguishable with FF_WU_SRC and DD_SRC) but to keep
1017 * the things simple and _fast_ we activate it only for free-fall, so
1018 * no need to read register (very slow with ACPI). For the same reason,
1019 * we forbid shared interrupts.
1020 *
1021 * IRQF_TRIGGER_RISING seems pointless on HP laptops because the
1022 * io-apic is not configurable (and generates a warning) but I keep it
1023 * in case of support for other hardware.
1024 */
Éric Pield7f81d42011-10-31 17:10:58 -07001025 if (lis3->pdata && lis3->whoami == WAI_8B)
Samu Onkalo92ba4fe2010-05-24 14:33:36 -07001026 thread_fn = lis302dl_interrupt_thread1_8b;
1027 else
1028 thread_fn = NULL;
1029
Éric Pield7f81d42011-10-31 17:10:58 -07001030 err = request_threaded_irq(lis3->irq, lis302dl_interrupt,
Samu Onkalo92ba4fe2010-05-24 14:33:36 -07001031 thread_fn,
Samu Onkalocc23aa12010-10-22 07:57:29 -04001032 IRQF_TRIGGER_RISING | IRQF_ONESHOT |
1033 irq_flags,
Éric Piel895c1562011-10-31 17:11:05 -07001034 DRIVER_NAME, lis3);
Samu Onkalo92ba4fe2010-05-24 14:33:36 -07001035
1036 if (err < 0) {
Joe Perches63366d32010-10-20 06:51:40 +00001037 pr_err("Cannot get IRQ\n");
Samu Onkalo92ba4fe2010-05-24 14:33:36 -07001038 goto out;
1039 }
1040
Éric Piel895c1562011-10-31 17:11:05 -07001041 lis3->miscdev.minor = MISC_DYNAMIC_MINOR;
1042 lis3->miscdev.name = "freefall";
1043 lis3->miscdev.fops = &lis3lv02d_misc_fops;
1044
1045 if (misc_register(&lis3->miscdev))
Joe Perches63366d32010-10-20 06:51:40 +00001046 pr_err("misc_register failed\n");
Daniel Mackab337a62009-03-31 15:24:31 -07001047out:
Daniel Mackab337a62009-03-31 15:24:31 -07001048 return 0;
1049}
1050EXPORT_SYMBOL_GPL(lis3lv02d_init_device);
1051
Pavel Machek455fbdd2008-11-12 13:27:02 -08001052MODULE_DESCRIPTION("ST LIS3LV02Dx three-axis digital accelerometer driver");
Pavel Machekef2cfc72009-02-18 14:48:23 -08001053MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
Pavel Machek455fbdd2008-11-12 13:27:02 -08001054MODULE_LICENSE("GPL");