blob: af96d3f32a0b4107b963cab7b1775968f34a871a [file] [log] [blame]
Samuel Huo84e7b7b2012-07-10 12:01:13 -07001/******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
2 *
3 * File Name : lis3dh_acc.c
4 * Authors : MSH - Motion Mems BU - Application Team
5 * : Matteo Dameno (matteo.dameno@st.com)
6 * : Carmine Iascone (carmine.iascone@st.com)
7 * : Samuel Huo (samuel.huo@st.com)
8 * Version : V.1.1.0
9 * Date : 07/10/2012
10 * Description : LIS3DH accelerometer sensor driver
11 *
12 *******************************************************************************
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 *
18 * THE PRESENT SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
19 * OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, FOR THE SOLE
20 * PURPOSE TO SUPPORT YOUR APPLICATION DEVELOPMENT.
21 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
22 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
23 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
24 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
25 *
26 * THIS SOFTWARE IS SPECIFICALLY DESIGNED FOR EXCLUSIVE USE WITH ST PARTS.
27 *
28 ******************************************************************************
29 Revision 1.0.0 05/11/09
30 First Release;
31 Revision 1.0.3 22/01/2010
32 Linux K&R Compliant Release;
33 Revision 1.0.5 16/08/2010
34 modified _get_acceleration_data function;
35 modified _update_odr function;
36 manages 2 interrupts;
37 Revision 1.0.6 15/11/2010
38 supports sysfs;
39 no more support for ioctl;
40 Revision 1.0.7 26/11/2010
41 checks for availability of interrupts pins
42 correction on FUZZ and FLAT values;
43 Revision 1.0.8 2010/Apr/01
44 corrects a bug in interrupt pin management in 1.0.7
45 Revision 1.0.9 07/25/2011
46 Romove several unused functions,add 5ms delay in init,change sysfs attributes.
47 Revision 1.1.0 07/10/2012
48 To replace some deprecated functions for 3.4 kernel;
49 To pass the checkpatch's formatting requirement;
50 To add regulator request;
51
52 ******************************************************************************/
53
54#include <linux/err.h>
55#include <linux/errno.h>
56#include <linux/delay.h>
57#include <linux/fs.h>
58#include <linux/i2c.h>
59#include <linux/input.h>
60#include <linux/uaccess.h>
61#include <linux/workqueue.h>
62#include <linux/irq.h>
63#include <linux/gpio.h>
64#include <linux/interrupt.h>
65#include <linux/slab.h>
66#include <linux/pm.h>
67#include <linux/input/lis3dh.h>
68#include <linux/module.h>
69#include <linux/regulator/consumer.h>
70
71
72#define DEBUG 1
73
74#define G_MAX 16000
75
76
77#define SENSITIVITY_2G 1 /** mg/LSB */
78#define SENSITIVITY_4G 2 /** mg/LSB */
79#define SENSITIVITY_8G 4 /** mg/LSB */
80#define SENSITIVITY_16G 12 /** mg/LSB */
81
82
83
84
85/* Accelerometer Sensor Operating Mode */
86#define LIS3DH_ACC_ENABLE 0x01
87#define LIS3DH_ACC_DISABLE 0x00
88
89#define HIGH_RESOLUTION 0x08
90
91#define AXISDATA_REG 0x28
92#define WHOAMI_LIS3DH_ACC 0x33 /* Expected content for WAI */
93
94/* CONTROL REGISTERS */
95#define WHO_AM_I 0x0F /* WhoAmI register */
96#define TEMP_CFG_REG 0x1F /* temper sens control reg */
97/* ctrl 1: ODR3 ODR2 ODR ODR0 LPen Zenable Yenable Zenable */
98#define CTRL_REG1 0x20 /* control reg 1 */
99#define CTRL_REG2 0x21 /* control reg 2 */
100#define CTRL_REG3 0x22 /* control reg 3 */
101#define CTRL_REG4 0x23 /* control reg 4 */
102#define CTRL_REG5 0x24 /* control reg 5 */
103#define CTRL_REG6 0x25 /* control reg 6 */
104
105#define FIFO_CTRL_REG 0x2E /* FiFo control reg */
106
107#define INT_CFG1 0x30 /* interrupt 1 config */
108#define INT_SRC1 0x31 /* interrupt 1 source */
109#define INT_THS1 0x32 /* interrupt 1 threshold */
110#define INT_DUR1 0x33 /* interrupt 1 duration */
111
112
113#define TT_CFG 0x38 /* tap config */
114#define TT_SRC 0x39 /* tap source */
115#define TT_THS 0x3A /* tap threshold */
116#define TT_LIM 0x3B /* tap time limit */
117#define TT_TLAT 0x3C /* tap time latency */
118#define TT_TW 0x3D /* tap time window */
119/* end CONTROL REGISTRES */
120
121
122#define ENABLE_HIGH_RESOLUTION 1
123
124#define LIS3DH_ACC_PM_OFF 0x00
125#define LIS3DH_ACC_ENABLE_ALL_AXES 0x07
126
127
128#define PMODE_MASK 0x08
129#define ODR_MASK 0XF0
130
131#define ODR1 0x10 /* 1Hz output data rate */
132#define ODR10 0x20 /* 10Hz output data rate */
133#define ODR25 0x30 /* 25Hz output data rate */
134#define ODR50 0x40 /* 50Hz output data rate */
135#define ODR100 0x50 /* 100Hz output data rate */
136#define ODR200 0x60 /* 200Hz output data rate */
137#define ODR400 0x70 /* 400Hz output data rate */
138#define ODR1250 0x90 /* 1250Hz output data rate */
139
140
141
142#define IA 0x40
143#define ZH 0x20
144#define ZL 0x10
145#define YH 0x08
146#define YL 0x04
147#define XH 0x02
148#define XL 0x01
149/* */
150/* CTRL REG BITS*/
151#define CTRL_REG3_I1_AOI1 0x40
152#define CTRL_REG6_I2_TAPEN 0x80
153#define CTRL_REG6_HLACTIVE 0x02
154/* */
155#define NO_MASK 0xFF
156#define INT1_DURATION_MASK 0x7F
157#define INT1_THRESHOLD_MASK 0x7F
158#define TAP_CFG_MASK 0x3F
159#define TAP_THS_MASK 0x7F
160#define TAP_TLIM_MASK 0x7F
161#define TAP_TLAT_MASK NO_MASK
162#define TAP_TW_MASK NO_MASK
163
164
165/* TAP_SOURCE_REG BIT */
166#define DTAP 0x20
167#define STAP 0x10
168#define SIGNTAP 0x08
169#define ZTAP 0x04
170#define YTAP 0x02
171#define XTAZ 0x01
172
173
174#define FUZZ 0
175#define FLAT 0
176#define I2C_RETRY_DELAY 5
177#define I2C_RETRIES 5
178#define I2C_AUTO_INCREMENT 0x80
179
180/* RESUME STATE INDICES */
181#define RES_CTRL_REG1 0
182#define RES_CTRL_REG2 1
183#define RES_CTRL_REG3 2
184#define RES_CTRL_REG4 3
185#define RES_CTRL_REG5 4
186#define RES_CTRL_REG6 5
187
188#define RES_INT_CFG1 6
189#define RES_INT_THS1 7
190#define RES_INT_DUR1 8
191
192#define RES_TT_CFG 9
193#define RES_TT_THS 10
194#define RES_TT_LIM 11
195#define RES_TT_TLAT 12
196#define RES_TT_TW 13
197
198#define RES_TEMP_CFG_REG 14
199#define RES_REFERENCE_REG 15
200#define RES_FIFO_CTRL_REG 16
201
202#define RESUME_ENTRIES 17
203/* end RESUME STATE INDICES */
204
205
206struct {
207 unsigned int cutoff_ms;
208 unsigned int mask;
209} lis3dh_acc_odr_table[] = {
210 { 1, ODR1250 },
211 { 3, ODR400 },
212 { 5, ODR200 },
213 { 10, ODR100 },
214 { 20, ODR50 },
215 { 40, ODR25 },
216 { 100, ODR10 },
217 { 1000, ODR1 },
218};
219
220struct lis3dh_acc_data {
221 struct i2c_client *client;
222 struct lis3dh_acc_platform_data *pdata;
223
224 struct mutex lock;
225 struct delayed_work input_work;
226
227 struct input_dev *input_dev;
228
229 int hw_initialized;
230 /* hw_working=-1 means not tested yet */
231 int hw_working;
232 atomic_t enabled;
233 int on_before_suspend;
234
235 u8 sensitivity;
236
237 u8 resume_state[RESUME_ENTRIES];
238
239 int irq1;
240 struct work_struct irq1_work;
241 struct workqueue_struct *irq1_work_queue;
242 int irq2;
243 struct work_struct irq2_work;
244 struct workqueue_struct *irq2_work_queue;
245
246#ifdef DEBUG
247 u8 reg_addr;
248#endif
249};
250
251struct sensor_regulator {
252 struct regulator *vreg;
253 const char *name;
254 u32 min_uV;
255 u32 max_uV;
256};
257
258struct sensor_regulator lis3dh_acc_vreg[] = {
259 {NULL, "vdd", 1700000, 3600000},
260 {NULL, "vddio", 1700000, 3600000},
261};
262
263static int lis3dh_acc_config_regulator(struct lis3dh_acc_data *acc, bool on)
264{
265 int rc = 0, i;
266 int num_reg = sizeof(lis3dh_acc_vreg) / sizeof(struct sensor_regulator);
267
268 if (on) {
269 for (i = 0; i < num_reg; i++) {
270 lis3dh_acc_vreg[i].vreg =
271 regulator_get(&acc->client->dev,
272 lis3dh_acc_vreg[i].name);
273 if (IS_ERR(lis3dh_acc_vreg[i].vreg)) {
274 rc = PTR_ERR(lis3dh_acc_vreg[i].vreg);
275 pr_err("%s:regulator get failed rc=%d\n",
276 __func__, rc);
277 goto error_vdd;
278 }
279
280 if (regulator_count_voltages(
281 lis3dh_acc_vreg[i].vreg) > 0) {
282 rc = regulator_set_voltage(
283 lis3dh_acc_vreg[i].vreg,
284 lis3dh_acc_vreg[i].min_uV,
285 lis3dh_acc_vreg[i].max_uV);
286 if (rc) {
287 pr_err("%s: set voltage failed rc=%d\n",
288 __func__, rc);
289 regulator_put(lis3dh_acc_vreg[i].vreg);
290 goto error_vdd;
291 }
292 }
293
294 rc = regulator_enable(lis3dh_acc_vreg[i].vreg);
295 if (rc) {
296 pr_err("%s: regulator_enable failed rc =%d\n",
297 __func__, rc);
298 if (regulator_count_voltages(
299 lis3dh_acc_vreg[i].vreg) > 0) {
300 regulator_set_voltage(
301 lis3dh_acc_vreg[i].vreg, 0,
302 lis3dh_acc_vreg[i].max_uV);
303 }
304 regulator_put(lis3dh_acc_vreg[i].vreg);
305 goto error_vdd;
306 }
307 }
308 return rc;
309 } else {
310 i = num_reg;
311 }
312
313error_vdd:
314 while (--i >= 0) {
315 if (regulator_count_voltages(lis3dh_acc_vreg[i].vreg) > 0) {
316 regulator_set_voltage(lis3dh_acc_vreg[i].vreg, 0,
317 lis3dh_acc_vreg[i].max_uV);
318 }
319 regulator_disable(lis3dh_acc_vreg[i].vreg);
320 regulator_put(lis3dh_acc_vreg[i].vreg);
321 }
322 return rc;
323}
324
325static int lis3dh_acc_i2c_read(struct lis3dh_acc_data *acc,
326 u8 *buf, int len)
327{
328 int err;
329 int tries = 0;
330
331 struct i2c_msg msgs[] = {
332 {
333 .addr = acc->client->addr,
334 .flags = acc->client->flags & I2C_M_TEN,
335 .len = 1,
336 .buf = buf,
337 },
338 {
339 .addr = acc->client->addr,
340 .flags = (acc->client->flags & I2C_M_TEN) | I2C_M_RD,
341 .len = len,
342 .buf = buf,
343 },
344 };
345
346 do {
347 err = i2c_transfer(acc->client->adapter, msgs, 2);
348 if (err != 2)
349 msleep_interruptible(I2C_RETRY_DELAY);
350 } while ((err != 2) && (++tries < I2C_RETRIES));
351
352 if (err != 2) {
353 dev_err(&acc->client->dev, "read transfer error\n");
354 err = -EIO;
355 } else {
356 err = 0;
357 }
358
359 return err;
360}
361
362static int lis3dh_acc_i2c_write(struct lis3dh_acc_data *acc, u8 *buf, int len)
363{
364 int err;
365 int tries = 0;
366
367 struct i2c_msg msgs[] = {
368 {
369 .addr = acc->client->addr,
370 .flags = acc->client->flags & I2C_M_TEN,
371 .len = len + 1,
372 .buf = buf,
373 },
374 };
375
376 do {
377 err = i2c_transfer(acc->client->adapter, msgs, 1);
378 if (err != 1)
379 msleep_interruptible(I2C_RETRY_DELAY);
380 } while ((err != 1) && (++tries < I2C_RETRIES));
381
382 if (err != 1) {
383 dev_err(&acc->client->dev, "write transfer error\n");
384 err = -EIO;
385 } else {
386 err = 0;
387 }
388
389 return err;
390}
391
392static int lis3dh_acc_hw_init(struct lis3dh_acc_data *acc)
393{
394 int err = -1;
395 u8 buf[7];
396
397 printk(KERN_INFO "%s: hw init start\n", LIS3DH_ACC_DEV_NAME);
398
399 buf[0] = WHO_AM_I;
400 err = lis3dh_acc_i2c_read(acc, buf, 1);
401 if (err < 0) {
402 dev_warn(&acc->client->dev,
403 "Error reading WHO_AM_I: is device available/working?\n");
404 goto err_firstread;
405 } else
406 acc->hw_working = 1;
407 if (buf[0] != WHOAMI_LIS3DH_ACC) {
408 dev_err(&acc->client->dev,
409 "device unknown. Expected: 0x%x, Replies: 0x%x\n",
410 WHOAMI_LIS3DH_ACC, buf[0]);
411 err = -1; /* choose the right coded error */
412 goto err_unknown_device;
413 }
414
415 buf[0] = CTRL_REG1;
416 buf[1] = acc->resume_state[RES_CTRL_REG1];
417 err = lis3dh_acc_i2c_write(acc, buf, 1);
418 if (err < 0)
419 goto err_resume_state;
420
421 buf[0] = TEMP_CFG_REG;
422 buf[1] = acc->resume_state[RES_TEMP_CFG_REG];
423 err = lis3dh_acc_i2c_write(acc, buf, 1);
424 if (err < 0)
425 goto err_resume_state;
426
427 buf[0] = FIFO_CTRL_REG;
428 buf[1] = acc->resume_state[RES_FIFO_CTRL_REG];
429 err = lis3dh_acc_i2c_write(acc, buf, 1);
430 if (err < 0)
431 goto err_resume_state;
432
433 buf[0] = (I2C_AUTO_INCREMENT | TT_THS);
434 buf[1] = acc->resume_state[RES_TT_THS];
435 buf[2] = acc->resume_state[RES_TT_LIM];
436 buf[3] = acc->resume_state[RES_TT_TLAT];
437 buf[4] = acc->resume_state[RES_TT_TW];
438 err = lis3dh_acc_i2c_write(acc, buf, 4);
439 if (err < 0)
440 goto err_resume_state;
441 buf[0] = TT_CFG;
442 buf[1] = acc->resume_state[RES_TT_CFG];
443 err = lis3dh_acc_i2c_write(acc, buf, 1);
444 if (err < 0)
445 goto err_resume_state;
446
447 buf[0] = (I2C_AUTO_INCREMENT | INT_THS1);
448 buf[1] = acc->resume_state[RES_INT_THS1];
449 buf[2] = acc->resume_state[RES_INT_DUR1];
450 err = lis3dh_acc_i2c_write(acc, buf, 2);
451 if (err < 0)
452 goto err_resume_state;
453 buf[0] = INT_CFG1;
454 buf[1] = acc->resume_state[RES_INT_CFG1];
455 err = lis3dh_acc_i2c_write(acc, buf, 1);
456 if (err < 0)
457 goto err_resume_state;
458
459
460 buf[0] = (I2C_AUTO_INCREMENT | CTRL_REG2);
461 buf[1] = acc->resume_state[RES_CTRL_REG2];
462 buf[2] = acc->resume_state[RES_CTRL_REG3];
463 buf[3] = acc->resume_state[RES_CTRL_REG4];
464 buf[4] = acc->resume_state[RES_CTRL_REG5];
465 buf[5] = acc->resume_state[RES_CTRL_REG6];
466 err = lis3dh_acc_i2c_write(acc, buf, 5);
467 if (err < 0)
468 goto err_resume_state;
469
470 acc->hw_initialized = 1;
471 printk(KERN_INFO "%s: hw init done\n", LIS3DH_ACC_DEV_NAME);
472 return 0;
473
474err_firstread:
475 acc->hw_working = 0;
476err_unknown_device:
477err_resume_state:
478 acc->hw_initialized = 0;
479 dev_err(&acc->client->dev, "hw init error 0x%x,0x%x: %d\n", buf[0],
480 buf[1], err);
481 return err;
482}
483
484static void lis3dh_acc_device_power_off(struct lis3dh_acc_data *acc)
485{
486 int err;
487 u8 buf[2] = { CTRL_REG1, LIS3DH_ACC_PM_OFF };
488
489 err = lis3dh_acc_i2c_write(acc, buf, 1);
490 if (err < 0)
491 dev_err(&acc->client->dev, "soft power off failed: %d\n", err);
492
493 if (acc->pdata->gpio_int1)
494 disable_irq_nosync(acc->irq1);
495 if (acc->pdata->gpio_int2)
496 disable_irq_nosync(acc->irq2);
497
498 lis3dh_acc_config_regulator(acc, false);
499
500 if (acc->hw_initialized) {
501 if (acc->pdata->gpio_int1)
502 disable_irq_nosync(acc->irq1);
503 if (acc->pdata->gpio_int2)
504 disable_irq_nosync(acc->irq2);
505 acc->hw_initialized = 0;
506 }
507}
508
509static int lis3dh_acc_device_power_on(struct lis3dh_acc_data *acc)
510{
511 int err = -1;
512
513 err = lis3dh_acc_config_regulator(acc, true);
514 if (err < 0) {
515 dev_err(&acc->client->dev,
516 "power_on failed: %d\n", err);
517 return err;
518 }
519
520 if (acc->pdata->gpio_int1 >= 0)
521 enable_irq(acc->irq1);
522 if (acc->pdata->gpio_int2 >= 0)
523 enable_irq(acc->irq2);
524
525 msleep(20);
526
527 if (!acc->hw_initialized) {
528 err = lis3dh_acc_hw_init(acc);
529 if (acc->hw_working == 1 && err < 0) {
530 lis3dh_acc_device_power_off(acc);
531 return err;
532 }
533 }
534
535 if (acc->hw_initialized) {
536 if (acc->pdata->gpio_int1 >= 0)
537 enable_irq(acc->irq1);
538 if (acc->pdata->gpio_int2 >= 0)
539 enable_irq(acc->irq2);
540 }
541 return 0;
542}
543
544static irqreturn_t lis3dh_acc_isr1(int irq, void *dev)
545{
546 struct lis3dh_acc_data *acc = dev;
547
548 disable_irq_nosync(irq);
549 queue_work(acc->irq1_work_queue, &acc->irq1_work);
550#ifdef DEBUG
551 printk(KERN_INFO "%s: isr1 queued\n", LIS3DH_ACC_DEV_NAME);
552#endif
553 return IRQ_HANDLED;
554}
555
556static irqreturn_t lis3dh_acc_isr2(int irq, void *dev)
557{
558 struct lis3dh_acc_data *acc = dev;
559
560 disable_irq_nosync(irq);
561 queue_work(acc->irq2_work_queue, &acc->irq2_work);
562#ifdef DEBUG
563 printk(KERN_INFO "%s: isr2 queued\n", LIS3DH_ACC_DEV_NAME);
564#endif
565 return IRQ_HANDLED;
566}
567
568static void lis3dh_acc_irq1_work_func(struct work_struct *work)
569{
570
571 struct lis3dh_acc_data *acc =
572 container_of(work, struct lis3dh_acc_data, irq1_work);
573 /* TODO add interrupt service procedure.
574 ie:lis3dh_acc_get_int1_source(acc); */
575 ;
576 /* */
577 printk(KERN_INFO "%s: IRQ1 triggered\n", LIS3DH_ACC_DEV_NAME);
578 goto exit;
579exit:
580 enable_irq(acc->irq1);
581}
582
583static void lis3dh_acc_irq2_work_func(struct work_struct *work)
584{
585
586 struct lis3dh_acc_data *acc =
587 container_of(work, struct lis3dh_acc_data, irq2_work);
588 /* TODO add interrupt service procedure.
589 ie:lis3dh_acc_get_tap_source(acc); */
590 ;
591 /* */
592 printk(KERN_INFO "%s: IRQ2 triggered\n", LIS3DH_ACC_DEV_NAME);
593 goto exit;
594exit:
595 enable_irq(acc->irq2);
596}
597
598int lis3dh_acc_update_g_range(struct lis3dh_acc_data *acc, u8 new_g_range)
599{
600 int err = -1;
601
602 u8 sensitivity;
603 u8 buf[2];
604 u8 updated_val;
605 u8 init_val;
606 u8 new_val;
607 u8 mask = LIS3DH_ACC_FS_MASK | HIGH_RESOLUTION;
608
609 switch (new_g_range) {
610 case LIS3DH_ACC_G_2G:
611
612 sensitivity = SENSITIVITY_2G;
613 break;
614 case LIS3DH_ACC_G_4G:
615
616 sensitivity = SENSITIVITY_4G;
617 break;
618 case LIS3DH_ACC_G_8G:
619
620 sensitivity = SENSITIVITY_8G;
621 break;
622 case LIS3DH_ACC_G_16G:
623
624 sensitivity = SENSITIVITY_16G;
625 break;
626 default:
627 dev_err(&acc->client->dev, "invalid g range requested: %u\n",
628 new_g_range);
629 return -EINVAL;
630 }
631
632 if (atomic_read(&acc->enabled)) {
633 /* Updates configuration register 4,
634 * which contains g range setting */
635 buf[0] = CTRL_REG4;
636 err = lis3dh_acc_i2c_read(acc, buf, 1);
637 if (err < 0)
638 goto error;
639 init_val = buf[0];
640 acc->resume_state[RES_CTRL_REG4] = init_val;
641 new_val = new_g_range | HIGH_RESOLUTION;
642 updated_val = ((mask & new_val) | ((~mask) & init_val));
643 buf[1] = updated_val;
644 buf[0] = CTRL_REG4;
645 err = lis3dh_acc_i2c_write(acc, buf, 1);
646 if (err < 0)
647 goto error;
648 acc->resume_state[RES_CTRL_REG4] = updated_val;
649 acc->sensitivity = sensitivity;
650 }
651
652
653 return err;
654error:
655 dev_err(&acc->client->dev, "update g range failed 0x%x,0x%x: %d\n",
656 buf[0], buf[1], err);
657
658 return err;
659}
660
661int lis3dh_acc_update_odr(struct lis3dh_acc_data *acc, int poll_interval_ms)
662{
663 int err = -1;
664 int i;
665 u8 config[2];
666
667 /* Following, looks for the longest possible odr interval scrolling the
668 * odr_table vector from the end (shortest interval) backward (longest
669 * interval), to support the poll_interval requested by the system.
670 * It must be the longest interval lower then the poll interval.*/
671 for (i = ARRAY_SIZE(lis3dh_acc_odr_table) - 1; i >= 0; i--) {
672 if (lis3dh_acc_odr_table[i].cutoff_ms <= poll_interval_ms)
673 break;
674 }
675 config[1] = lis3dh_acc_odr_table[i].mask;
676
677 config[1] |= LIS3DH_ACC_ENABLE_ALL_AXES;
678
679 /* If device is currently enabled, we need to write new
680 * configuration out to it */
681 if (atomic_read(&acc->enabled)) {
682 config[0] = CTRL_REG1;
683 err = lis3dh_acc_i2c_write(acc, config, 1);
684 if (err < 0)
685 goto error;
686 acc->resume_state[RES_CTRL_REG1] = config[1];
687 }
688
689 return err;
690
691error:
692 dev_err(&acc->client->dev, "update odr failed 0x%x,0x%x: %d\n",
693 config[0], config[1], err);
694
695 return err;
696}
697
698
699
700static int lis3dh_acc_register_write(struct lis3dh_acc_data *acc, u8 *buf,
701 u8 reg_address, u8 new_value)
702{
703 int err = -1;
704
705 /* Sets configuration register at reg_address
706 * NOTE: this is a straight overwrite */
707 buf[0] = reg_address;
708 buf[1] = new_value;
709 err = lis3dh_acc_i2c_write(acc, buf, 1);
710 if (err < 0)
711 return err;
712 return err;
713}
714
715static int lis3dh_acc_get_acceleration_data(struct lis3dh_acc_data *acc,
716 int *xyz)
717{
718 int err = -1;
719 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
720 u8 acc_data[6];
721 /* x,y,z hardware data */
722 s16 hw_d[3] = { 0 };
723
724 acc_data[0] = (I2C_AUTO_INCREMENT | AXISDATA_REG);
725 err = lis3dh_acc_i2c_read(acc, acc_data, 6);
726 if (err < 0)
727 return err;
728
729 hw_d[0] = (((s16) ((acc_data[1] << 8) | acc_data[0])) >> 4);
730 hw_d[1] = (((s16) ((acc_data[3] << 8) | acc_data[2])) >> 4);
731 hw_d[2] = (((s16) ((acc_data[5] << 8) | acc_data[4])) >> 4);
732
733 hw_d[0] = hw_d[0] * acc->sensitivity;
734 hw_d[1] = hw_d[1] * acc->sensitivity;
735 hw_d[2] = hw_d[2] * acc->sensitivity;
736
737
738 xyz[0] = ((acc->pdata->negate_x) ? (-hw_d[acc->pdata->axis_map_x])
739 : (hw_d[acc->pdata->axis_map_x]));
740 xyz[1] = ((acc->pdata->negate_y) ? (-hw_d[acc->pdata->axis_map_y])
741 : (hw_d[acc->pdata->axis_map_y]));
742 xyz[2] = ((acc->pdata->negate_z) ? (-hw_d[acc->pdata->axis_map_z])
743 : (hw_d[acc->pdata->axis_map_z]));
744
745 #ifdef DEBUG
746 /*
747 printk(KERN_INFO "%s read x=%d, y=%d, z=%d\n",
748 LIS3DH_ACC_DEV_NAME, xyz[0], xyz[1], xyz[2]);
749 */
750 #endif
751 return err;
752}
753
754static void lis3dh_acc_report_values(struct lis3dh_acc_data *acc,
755 int *xyz)
756{
757 input_report_abs(acc->input_dev, ABS_X, xyz[0]);
758 input_report_abs(acc->input_dev, ABS_Y, xyz[1]);
759 input_report_abs(acc->input_dev, ABS_Z, xyz[2]);
760 input_sync(acc->input_dev);
761}
762
763static int lis3dh_acc_enable(struct lis3dh_acc_data *acc)
764{
765 int err;
766
767 if (!atomic_cmpxchg(&acc->enabled, 0, 1)) {
768 err = lis3dh_acc_device_power_on(acc);
769 if (err < 0) {
770 atomic_set(&acc->enabled, 0);
771 return err;
772 }
773 schedule_delayed_work(&acc->input_work,
774 msecs_to_jiffies(acc->pdata->poll_interval));
775 }
776
777 return 0;
778}
779
780static int lis3dh_acc_disable(struct lis3dh_acc_data *acc)
781{
782 if (atomic_cmpxchg(&acc->enabled, 1, 0)) {
783 cancel_delayed_work_sync(&acc->input_work);
784 lis3dh_acc_device_power_off(acc);
785 }
786
787 return 0;
788}
789
790
791static ssize_t read_single_reg(struct device *dev, char *buf, u8 reg)
792{
793 ssize_t ret;
794 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
795 int err;
796
797 u8 data = reg;
798 err = lis3dh_acc_i2c_read(acc, &data, 1);
799 if (err < 0)
800 return err;
801 ret = snprintf(buf, 4, "0x%02x\n", data);
802 return ret;
803
804}
805
806static int write_reg(struct device *dev, const char *buf, u8 reg,
807 u8 mask, int resumeIndex)
808{
809 int err = -1;
810 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
811 u8 x[2];
812 u8 new_val;
813 unsigned long val;
814
815 if (kstrtoul(buf, 16, &val))
816 return -EINVAL;
817
818 new_val = ((u8) val & mask);
819 x[0] = reg;
820 x[1] = new_val;
821 err = lis3dh_acc_register_write(acc, x, reg, new_val);
822 if (err < 0)
823 return err;
824 acc->resume_state[resumeIndex] = new_val;
825 return err;
826}
827
828static ssize_t attr_get_polling_rate(struct device *dev,
829 struct device_attribute *attr,
830 char *buf)
831{
832 int val;
833 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
834 mutex_lock(&acc->lock);
835 val = acc->pdata->poll_interval;
836 mutex_unlock(&acc->lock);
837 return snprintf(buf, 8, "%d\n", val);
838}
839
840static ssize_t attr_set_polling_rate(struct device *dev,
841 struct device_attribute *attr,
842 const char *buf, size_t size)
843{
844 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
845 unsigned long interval_ms;
846
847 if (kstrtoul(buf, 10, &interval_ms))
848 return -EINVAL;
849 if (!interval_ms)
850 return -EINVAL;
851 mutex_lock(&acc->lock);
852 acc->pdata->poll_interval = interval_ms;
853 lis3dh_acc_update_odr(acc, interval_ms);
854 mutex_unlock(&acc->lock);
855 return size;
856}
857
858static ssize_t attr_get_range(struct device *dev,
859 struct device_attribute *attr, char *buf)
860{
861 char val;
862 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
863 char range = 2;
864 mutex_lock(&acc->lock);
865 val = acc->pdata->g_range ;
866 switch (val) {
867 case LIS3DH_ACC_G_2G:
868 range = 2;
869 break;
870 case LIS3DH_ACC_G_4G:
871 range = 4;
872 break;
873 case LIS3DH_ACC_G_8G:
874 range = 8;
875 break;
876 case LIS3DH_ACC_G_16G:
877 range = 16;
878 break;
879 }
880 mutex_unlock(&acc->lock);
881 return snprintf(buf, 4, "%d\n", range);
882}
883
884static ssize_t attr_set_range(struct device *dev,
885 struct device_attribute *attr,
886 const char *buf, size_t size)
887{
888 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
889 unsigned long val;
890 if (kstrtoul(buf, 10, &val))
891 return -EINVAL;
892 mutex_lock(&acc->lock);
893 acc->pdata->g_range = val;
894 lis3dh_acc_update_g_range(acc, val);
895 mutex_unlock(&acc->lock);
896 return size;
897}
898
899static ssize_t attr_get_enable(struct device *dev,
900 struct device_attribute *attr, char *buf)
901{
902 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
903 int val = atomic_read(&acc->enabled);
904 return snprintf(buf, sizeof(val) + 2, "%d\n", val);
905}
906
907static ssize_t attr_set_enable(struct device *dev,
908 struct device_attribute *attr,
909 const char *buf, size_t size)
910{
911 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
912 unsigned long val;
913
914 if (kstrtoul(buf, 10, &val))
915 return -EINVAL;
916
917 if (val)
918 lis3dh_acc_enable(acc);
919 else
920 lis3dh_acc_disable(acc);
921
922 return size;
923}
924
925static ssize_t attr_set_intconfig1(struct device *dev,
926 struct device_attribute *attr, const char *buf, size_t size)
927{
928 return write_reg(dev, buf, INT_CFG1, NO_MASK, RES_INT_CFG1);
929}
930
931static ssize_t attr_get_intconfig1(struct device *dev,
932 struct device_attribute *attr, char *buf)
933{
934 return read_single_reg(dev, buf, INT_CFG1);
935}
936
937static ssize_t attr_set_duration1(struct device *dev,
938 struct device_attribute *attr, const char *buf, size_t size)
939{
940 return write_reg(dev, buf, INT_DUR1, INT1_DURATION_MASK, RES_INT_DUR1);
941}
942
943static ssize_t attr_get_duration1(struct device *dev,
944 struct device_attribute *attr, char *buf)
945{
946 return read_single_reg(dev, buf, INT_DUR1);
947}
948
949static ssize_t attr_set_thresh1(struct device *dev,
950 struct device_attribute *attr, const char *buf, size_t size)
951{
952 return write_reg(dev, buf, INT_THS1, INT1_THRESHOLD_MASK, RES_INT_THS1);
953}
954
955static ssize_t attr_get_thresh1(struct device *dev,
956 struct device_attribute *attr, char *buf)
957{
958 return read_single_reg(dev, buf, INT_THS1);
959}
960
961static ssize_t attr_get_source1(struct device *dev,
962 struct device_attribute *attr, char *buf)
963{
964 return read_single_reg(dev, buf, INT_SRC1);
965}
966
967static ssize_t attr_set_click_cfg(struct device *dev,
968 struct device_attribute *attr, const char *buf, size_t size)
969{
970 return write_reg(dev, buf, TT_CFG, TAP_CFG_MASK, RES_TT_CFG);
971}
972
973static ssize_t attr_get_click_cfg(struct device *dev,
974 struct device_attribute *attr, char *buf)
975{
976
977 return read_single_reg(dev, buf, TT_CFG);
978}
979
980static ssize_t attr_get_click_source(struct device *dev,
981 struct device_attribute *attr, char *buf)
982{
983 return read_single_reg(dev, buf, TT_SRC);
984}
985
986static ssize_t attr_set_click_ths(struct device *dev,
987 struct device_attribute *attr, const char *buf, size_t size)
988{
989 return write_reg(dev, buf, TT_THS, TAP_THS_MASK, RES_TT_THS);
990}
991
992static ssize_t attr_get_click_ths(struct device *dev,
993 struct device_attribute *attr, char *buf)
994{
995 return read_single_reg(dev, buf, TT_THS);
996}
997
998static ssize_t attr_set_click_tlim(struct device *dev,
999 struct device_attribute *attr, const char *buf, size_t size)
1000{
1001 return write_reg(dev, buf, TT_LIM, TAP_TLIM_MASK, RES_TT_LIM);
1002}
1003
1004static ssize_t attr_get_click_tlim(struct device *dev,
1005 struct device_attribute *attr, char *buf)
1006{
1007 return read_single_reg(dev, buf, TT_LIM);
1008}
1009
1010static ssize_t attr_set_click_tlat(struct device *dev,
1011 struct device_attribute *attr, const char *buf, size_t size)
1012{
1013 return write_reg(dev, buf, TT_TLAT, TAP_TLAT_MASK, RES_TT_TLAT);
1014}
1015
1016static ssize_t attr_get_click_tlat(struct device *dev,
1017 struct device_attribute *attr, char *buf)
1018{
1019 return read_single_reg(dev, buf, TT_TLAT);
1020}
1021
1022static ssize_t attr_set_click_tw(struct device *dev,
1023 struct device_attribute *attr, const char *buf, size_t size)
1024{
1025 return write_reg(dev, buf, TT_TLAT, TAP_TW_MASK, RES_TT_TLAT);
1026}
1027
1028static ssize_t attr_get_click_tw(struct device *dev,
1029 struct device_attribute *attr, char *buf)
1030{
1031 return read_single_reg(dev, buf, TT_TLAT);
1032}
1033
1034
1035#ifdef DEBUG
1036/* PAY ATTENTION: These DEBUG funtions don't manage resume_state */
1037static ssize_t attr_reg_set(struct device *dev, struct device_attribute *attr,
1038 const char *buf, size_t size)
1039{
1040 int rc;
1041 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
1042 u8 x[2];
1043 unsigned long val;
1044
1045 if (kstrtoul(buf, 16, &val))
1046 return -EINVAL;
1047 mutex_lock(&acc->lock);
1048 x[0] = acc->reg_addr;
1049 mutex_unlock(&acc->lock);
1050 x[1] = val;
1051 rc = lis3dh_acc_i2c_write(acc, x, 1);
1052 /*TODO: error need to be managed */
1053 return size;
1054}
1055
1056static ssize_t attr_reg_get(struct device *dev, struct device_attribute *attr,
1057 char *buf)
1058{
1059 ssize_t ret;
1060 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
1061 int rc;
1062 u8 data;
1063
1064 mutex_lock(&acc->lock);
1065 data = acc->reg_addr;
1066 mutex_unlock(&acc->lock);
1067 rc = lis3dh_acc_i2c_read(acc, &data, 1);
1068 /* TODO: error need to be managed */
1069 ret = snprintf(buf, 8, "0x%02x\n", data);
1070 return ret;
1071}
1072
1073static ssize_t attr_addr_set(struct device *dev, struct device_attribute *attr,
1074 const char *buf, size_t size)
1075{
1076 struct lis3dh_acc_data *acc = dev_get_drvdata(dev);
1077 unsigned long val;
1078 if (kstrtoul(buf, 16, &val))
1079 return -EINVAL;
1080 mutex_lock(&acc->lock);
1081 acc->reg_addr = val;
1082 mutex_unlock(&acc->lock);
1083 return size;
1084}
1085#endif
1086
1087static struct device_attribute attributes[] = {
1088
1089 __ATTR(pollrate_ms, 0666, attr_get_polling_rate,
1090 attr_set_polling_rate),
1091 __ATTR(range, 0666, attr_get_range, attr_set_range),
1092 __ATTR(enable, 0666, attr_get_enable, attr_set_enable),
1093 __ATTR(int1_config, 0666, attr_get_intconfig1, attr_set_intconfig1),
1094 __ATTR(int1_duration, 0666, attr_get_duration1, attr_set_duration1),
1095 __ATTR(int1_threshold, 0666, attr_get_thresh1, attr_set_thresh1),
1096 __ATTR(int1_source, 0444, attr_get_source1, NULL),
1097 __ATTR(click_config, 0666, attr_get_click_cfg, attr_set_click_cfg),
1098 __ATTR(click_source, 0444, attr_get_click_source, NULL),
1099 __ATTR(click_threshold, 0666, attr_get_click_ths, attr_set_click_ths),
1100 __ATTR(click_timelimit, 0666, attr_get_click_tlim,
1101 attr_set_click_tlim),
1102 __ATTR(click_timelatency, 0666, attr_get_click_tlat,
1103 attr_set_click_tlat),
1104 __ATTR(click_timewindow, 0666, attr_get_click_tw, attr_set_click_tw),
1105
1106#ifdef DEBUG
1107 __ATTR(reg_value, 0666, attr_reg_get, attr_reg_set),
1108 __ATTR(reg_addr, 0222, NULL, attr_addr_set),
1109#endif
1110};
1111
1112static int create_sysfs_interfaces(struct device *dev)
1113{
1114 int i;
1115 int err;
1116 for (i = 0; i < ARRAY_SIZE(attributes); i++) {
1117 err = device_create_file(dev, attributes + i);
1118 if (err)
1119 goto error;
1120 }
1121 return 0;
1122
1123error:
1124 for ( ; i >= 0; i--)
1125 device_remove_file(dev, attributes + i);
1126 dev_err(dev, "%s:Unable to create interface\n", __func__);
1127 return err;
1128}
1129
1130static int remove_sysfs_interfaces(struct device *dev)
1131{
1132 int i;
1133 for (i = 0; i < ARRAY_SIZE(attributes); i++)
1134 device_remove_file(dev, attributes + i);
1135 return 0;
1136}
1137
1138static void lis3dh_acc_input_work_func(struct work_struct *work)
1139{
1140 struct lis3dh_acc_data *acc;
1141
1142 int xyz[3] = { 0 };
1143 int err;
1144
1145 acc = container_of((struct delayed_work *)work,
1146 struct lis3dh_acc_data, input_work);
1147
1148 mutex_lock(&acc->lock);
1149 err = lis3dh_acc_get_acceleration_data(acc, xyz);
1150 if (err < 0)
1151 dev_err(&acc->client->dev, "get_acceleration_data failed\n");
1152 else
1153 lis3dh_acc_report_values(acc, xyz);
1154
1155 schedule_delayed_work(&acc->input_work, msecs_to_jiffies(
1156 acc->pdata->poll_interval));
1157 mutex_unlock(&acc->lock);
1158}
1159
1160int lis3dh_acc_input_open(struct input_dev *input)
1161{
1162 struct lis3dh_acc_data *acc = input_get_drvdata(input);
1163
1164 return lis3dh_acc_enable(acc);
1165}
1166
1167void lis3dh_acc_input_close(struct input_dev *dev)
1168{
1169 struct lis3dh_acc_data *acc = input_get_drvdata(dev);
1170
1171 lis3dh_acc_disable(acc);
1172}
1173
1174static int lis3dh_acc_validate_pdata(struct lis3dh_acc_data *acc)
1175{
1176 acc->pdata->poll_interval = max(acc->pdata->poll_interval,
1177 acc->pdata->min_interval);
1178
1179 if (acc->pdata->axis_map_x > 2 ||
1180 acc->pdata->axis_map_y > 2 ||
1181 acc->pdata->axis_map_z > 2) {
1182 dev_err(&acc->client->dev,
1183 "invalid axis_map value x:%u y:%u z%u\n",
1184 acc->pdata->axis_map_x,
1185 acc->pdata->axis_map_y, acc->pdata->axis_map_z);
1186 return -EINVAL;
1187 }
1188
1189 /* Only allow 0 and 1 for negation boolean flag */
1190 if (acc->pdata->negate_x > 1 || acc->pdata->negate_y > 1
1191 || acc->pdata->negate_z > 1) {
1192 dev_err(&acc->client->dev,
1193 "invalid negate value x:%u y:%u z:%u\n",
1194 acc->pdata->negate_x,
1195 acc->pdata->negate_y, acc->pdata->negate_z);
1196 return -EINVAL;
1197 }
1198
1199 /* Enforce minimum polling interval */
1200 if (acc->pdata->poll_interval < acc->pdata->min_interval) {
1201 dev_err(&acc->client->dev, "minimum poll interval violated\n");
1202 return -EINVAL;
1203 }
1204
1205 return 0;
1206}
1207
1208static int lis3dh_acc_input_init(struct lis3dh_acc_data *acc)
1209{
1210 int err;
1211
1212 INIT_DELAYED_WORK(&acc->input_work, lis3dh_acc_input_work_func);
1213 acc->input_dev = input_allocate_device();
1214 if (!acc->input_dev) {
1215 err = -ENOMEM;
1216 dev_err(&acc->client->dev, "input device allocation failed\n");
1217 goto err0;
1218 }
1219
1220 acc->input_dev->open = lis3dh_acc_input_open;
1221 acc->input_dev->close = lis3dh_acc_input_close;
1222 acc->input_dev->name = LIS3DH_ACC_DEV_NAME;
1223 acc->input_dev->id.bustype = BUS_I2C;
1224 acc->input_dev->dev.parent = &acc->client->dev;
1225
1226 input_set_drvdata(acc->input_dev, acc);
1227
1228 set_bit(EV_ABS, acc->input_dev->evbit);
1229 /* next is used for interruptA sources data if the case */
1230 set_bit(ABS_MISC, acc->input_dev->absbit);
1231 /* next is used for interruptB sources data if the case */
1232 set_bit(ABS_WHEEL, acc->input_dev->absbit);
1233
1234 input_set_abs_params(acc->input_dev, ABS_X, -G_MAX, G_MAX, FUZZ, FLAT);
1235 input_set_abs_params(acc->input_dev, ABS_Y, -G_MAX, G_MAX, FUZZ, FLAT);
1236 input_set_abs_params(acc->input_dev, ABS_Z, -G_MAX, G_MAX, FUZZ, FLAT);
1237 /* next is used for interruptA sources data if the case */
1238 input_set_abs_params(acc->input_dev, ABS_MISC, INT_MIN, INT_MAX, 0, 0);
1239 /* next is used for interruptB sources data if the case */
1240 input_set_abs_params(acc->input_dev, ABS_WHEEL, INT_MIN, INT_MAX, 0, 0);
1241
1242
1243 err = input_register_device(acc->input_dev);
1244 if (err) {
1245 dev_err(&acc->client->dev,
1246 "unable to register input device %s\n",
1247 acc->input_dev->name);
1248 goto err1;
1249 }
1250
1251 return 0;
1252
1253err1:
1254 input_free_device(acc->input_dev);
1255err0:
1256 return err;
1257}
1258
1259static void lis3dh_acc_input_cleanup(struct lis3dh_acc_data *acc)
1260{
1261 input_unregister_device(acc->input_dev);
1262 input_free_device(acc->input_dev);
1263}
1264
1265static int lis3dh_acc_probe(struct i2c_client *client,
1266 const struct i2c_device_id *id)
1267{
1268
1269 struct lis3dh_acc_data *acc;
1270
1271 int err = -1;
1272
1273 pr_info("%s: probe start.\n", LIS3DH_ACC_DEV_NAME);
1274
1275 if (client->dev.platform_data == NULL) {
1276 dev_err(&client->dev, "platform data is NULL. exiting.\n");
1277 err = -ENODEV;
1278 goto exit_check_functionality_failed;
1279 }
1280
1281 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1282 dev_err(&client->dev, "client not i2c capable\n");
1283 err = -ENODEV;
1284 goto exit_check_functionality_failed;
1285 }
1286
1287 /*
1288 if (!i2c_check_functionality(client->adapter,
1289 I2C_FUNC_SMBUS_BYTE |
1290 I2C_FUNC_SMBUS_BYTE_DATA |
1291 I2C_FUNC_SMBUS_WORD_DATA)) {
1292 dev_err(&client->dev, "client not smb-i2c capable:2\n");
1293 err = -EIO;
1294 goto exit_check_functionality_failed;
1295 }
1296
1297
1298 if (!i2c_check_functionality(client->adapter,
1299 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1300 dev_err(&client->dev, "client not smb-i2c capable:3\n");
1301 err = -EIO;
1302 goto exit_check_functionality_failed;
1303 }
1304 */
1305
1306 acc = kzalloc(sizeof(struct lis3dh_acc_data), GFP_KERNEL);
1307 if (acc == NULL) {
1308 err = -ENOMEM;
1309 dev_err(&client->dev,
1310 "failed to allocate memory for module data: "
1311 "%d\n", err);
1312 goto exit_check_functionality_failed;
1313 }
1314
1315
1316 mutex_init(&acc->lock);
1317 mutex_lock(&acc->lock);
1318
1319 acc->client = client;
1320 i2c_set_clientdata(client, acc);
1321
1322 acc->pdata = kmalloc(sizeof(*acc->pdata), GFP_KERNEL);
1323 if (acc->pdata == NULL) {
1324 err = -ENOMEM;
1325 dev_err(&client->dev,
1326 "failed to allocate memory for pdata: %d\n",
1327 err);
1328 goto err_mutexunlock;
1329 }
1330
1331 memcpy(acc->pdata, client->dev.platform_data, sizeof(*acc->pdata));
1332
1333 err = lis3dh_acc_validate_pdata(acc);
1334 if (err < 0) {
1335 dev_err(&client->dev, "failed to validate platform data\n");
1336 goto exit_kfree_pdata;
1337 }
1338
1339
1340 if (acc->pdata->init) {
1341 err = acc->pdata->init();
1342 if (err < 0) {
1343 dev_err(&client->dev, "init failed: %d\n", err);
1344 goto err_pdata_init;
1345 }
1346 }
1347
1348 if (acc->pdata->gpio_int1 >= 0) {
1349 acc->irq1 = gpio_to_irq(acc->pdata->gpio_int1);
1350 printk(KERN_INFO "%s: %s has set irq1 to irq: %d\n",
1351 LIS3DH_ACC_DEV_NAME, __func__, acc->irq1);
1352 printk(KERN_INFO "%s: %s has mapped irq1 on gpio: %d\n",
1353 LIS3DH_ACC_DEV_NAME, __func__,
1354 acc->pdata->gpio_int1);
1355 }
1356
1357 if (acc->pdata->gpio_int2 >= 0) {
1358 acc->irq2 = gpio_to_irq(acc->pdata->gpio_int2);
1359 printk(KERN_INFO "%s: %s has set irq2 to irq: %d\n",
1360 LIS3DH_ACC_DEV_NAME, __func__, acc->irq2);
1361 printk(KERN_INFO "%s: %s has mapped irq2 on gpio: %d\n",
1362 LIS3DH_ACC_DEV_NAME, __func__,
1363 acc->pdata->gpio_int2);
1364 }
1365
1366 memset(acc->resume_state, 0, ARRAY_SIZE(acc->resume_state));
1367
1368 acc->resume_state[RES_CTRL_REG1] = LIS3DH_ACC_ENABLE_ALL_AXES;
1369 acc->resume_state[RES_CTRL_REG2] = 0x00;
1370 acc->resume_state[RES_CTRL_REG3] = 0x00;
1371 acc->resume_state[RES_CTRL_REG4] = 0x00;
1372 acc->resume_state[RES_CTRL_REG5] = 0x00;
1373 acc->resume_state[RES_CTRL_REG6] = 0x00;
1374
1375 acc->resume_state[RES_TEMP_CFG_REG] = 0x00;
1376 acc->resume_state[RES_FIFO_CTRL_REG] = 0x00;
1377 acc->resume_state[RES_INT_CFG1] = 0x00;
1378 acc->resume_state[RES_INT_THS1] = 0x00;
1379 acc->resume_state[RES_INT_DUR1] = 0x00;
1380
1381 acc->resume_state[RES_TT_CFG] = 0x00;
1382 acc->resume_state[RES_TT_THS] = 0x00;
1383 acc->resume_state[RES_TT_LIM] = 0x00;
1384 acc->resume_state[RES_TT_TLAT] = 0x00;
1385 acc->resume_state[RES_TT_TW] = 0x00;
1386
1387 err = lis3dh_acc_device_power_on(acc);
1388 if (err < 0) {
1389 dev_err(&client->dev, "power on failed: %d\n", err);
1390 goto err_pdata_init;
1391 }
1392
1393 atomic_set(&acc->enabled, 1);
1394
1395 err = lis3dh_acc_update_g_range(acc, acc->pdata->g_range);
1396 if (err < 0) {
1397 dev_err(&client->dev, "update_g_range failed\n");
1398 goto err_power_off;
1399 }
1400
1401 err = lis3dh_acc_update_odr(acc, acc->pdata->poll_interval);
1402 if (err < 0) {
1403 dev_err(&client->dev, "update_odr failed\n");
1404 goto err_power_off;
1405 }
1406
1407 err = lis3dh_acc_input_init(acc);
1408 if (err < 0) {
1409 dev_err(&client->dev, "input init failed\n");
1410 goto err_power_off;
1411 }
1412
1413
1414 err = create_sysfs_interfaces(&client->dev);
1415 if (err < 0) {
1416 dev_err(&client->dev,
1417 "device LIS3DH_ACC_DEV_NAME sysfs register failed\n");
1418 goto err_input_cleanup;
1419 }
1420
1421 lis3dh_acc_device_power_off(acc);
1422
1423 /* As default, do not report information */
1424 atomic_set(&acc->enabled, 0);
1425
1426 if (acc->pdata->gpio_int1 >= 0) {
1427 INIT_WORK(&acc->irq1_work, lis3dh_acc_irq1_work_func);
1428 acc->irq1_work_queue =
1429 create_singlethread_workqueue("lis3dh_acc_wq1");
1430 if (!acc->irq1_work_queue) {
1431 err = -ENOMEM;
1432 dev_err(&client->dev,
1433 "cannot create work queue1: %d\n", err);
1434 goto err_remove_sysfs_int;
1435 }
1436 err = request_irq(acc->irq1, lis3dh_acc_isr1,
1437 IRQF_TRIGGER_RISING, "lis3dh_acc_irq1", acc);
1438 if (err < 0) {
1439 dev_err(&client->dev, "request irq1 failed: %d\n", err);
1440 goto err_destoyworkqueue1;
1441 }
1442 disable_irq_nosync(acc->irq1);
1443 }
1444
1445 if (acc->pdata->gpio_int2 >= 0) {
1446 INIT_WORK(&acc->irq2_work, lis3dh_acc_irq2_work_func);
1447 acc->irq2_work_queue =
1448 create_singlethread_workqueue("lis3dh_acc_wq2");
1449 if (!acc->irq2_work_queue) {
1450 err = -ENOMEM;
1451 dev_err(&client->dev,
1452 "cannot create work queue2: %d\n", err);
1453 goto err_free_irq1;
1454 }
1455 err = request_irq(acc->irq2, lis3dh_acc_isr2,
1456 IRQF_TRIGGER_RISING, "lis3dh_acc_irq2", acc);
1457 if (err < 0) {
1458 dev_err(&client->dev, "request irq2 failed: %d\n", err);
1459 goto err_destoyworkqueue2;
1460 }
1461 disable_irq_nosync(acc->irq2);
1462 }
1463
1464
1465
1466 mutex_unlock(&acc->lock);
1467
1468 dev_info(&client->dev, "%s: probed\n", LIS3DH_ACC_DEV_NAME);
1469
1470 return 0;
1471
1472err_destoyworkqueue2:
1473 if (acc->pdata->gpio_int2 >= 0)
1474 destroy_workqueue(acc->irq2_work_queue);
1475err_free_irq1:
1476 free_irq(acc->irq1, acc);
1477err_destoyworkqueue1:
1478 if (acc->pdata->gpio_int1 >= 0)
1479 destroy_workqueue(acc->irq1_work_queue);
1480err_remove_sysfs_int:
1481 remove_sysfs_interfaces(&client->dev);
1482err_input_cleanup:
1483 lis3dh_acc_input_cleanup(acc);
1484err_power_off:
1485 lis3dh_acc_device_power_off(acc);
1486err_pdata_init:
1487 if (acc->pdata->exit)
1488 acc->pdata->exit();
1489exit_kfree_pdata:
1490 kfree(acc->pdata);
1491err_mutexunlock:
1492 mutex_unlock(&acc->lock);
1493 kfree(acc);
1494exit_check_functionality_failed:
1495 printk(KERN_ERR "%s: Driver Init failed\n", LIS3DH_ACC_DEV_NAME);
1496 return err;
1497}
1498
1499static int __devexit lis3dh_acc_remove(struct i2c_client *client)
1500{
1501 struct lis3dh_acc_data *acc = i2c_get_clientdata(client);
1502
1503 if (acc->pdata->gpio_int1 >= 0) {
1504 free_irq(acc->irq1, acc);
1505 gpio_free(acc->pdata->gpio_int1);
1506 destroy_workqueue(acc->irq1_work_queue);
1507 }
1508
1509 if (acc->pdata->gpio_int2 >= 0) {
1510 free_irq(acc->irq2, acc);
1511 gpio_free(acc->pdata->gpio_int2);
1512 destroy_workqueue(acc->irq2_work_queue);
1513 }
1514
1515 lis3dh_acc_input_cleanup(acc);
1516 lis3dh_acc_device_power_off(acc);
1517 remove_sysfs_interfaces(&client->dev);
1518
1519 if (acc->pdata->exit)
1520 acc->pdata->exit();
1521 kfree(acc->pdata);
1522 kfree(acc);
1523
1524 return 0;
1525}
1526
1527#ifdef CONFIG_PM
1528static int lis3dh_acc_resume(struct i2c_client *client)
1529{
1530 struct lis3dh_acc_data *acc = i2c_get_clientdata(client);
1531
1532 if (acc->on_before_suspend)
1533 return lis3dh_acc_enable(acc);
1534 return 0;
1535}
1536
1537static int lis3dh_acc_suspend(struct i2c_client *client, pm_message_t mesg)
1538{
1539 struct lis3dh_acc_data *acc = i2c_get_clientdata(client);
1540
1541 acc->on_before_suspend = atomic_read(&acc->enabled);
1542 return lis3dh_acc_disable(acc);
1543}
1544#else
1545#define lis3dh_acc_suspend NULL
1546#define lis3dh_acc_resume NULL
1547#endif /* CONFIG_PM */
1548
1549static const struct i2c_device_id lis3dh_acc_id[]
1550 = { { LIS3DH_ACC_DEV_NAME, 0 }, { }, };
1551
1552MODULE_DEVICE_TABLE(i2c, lis3dh_acc_id);
1553
1554static struct i2c_driver lis3dh_acc_driver = {
1555 .driver = {
1556 .owner = THIS_MODULE,
1557 .name = LIS3DH_ACC_DEV_NAME,
1558 },
1559 .probe = lis3dh_acc_probe,
1560 .remove = __devexit_p(lis3dh_acc_remove),
1561 .suspend = lis3dh_acc_suspend,
1562 .resume = lis3dh_acc_resume,
1563 .id_table = lis3dh_acc_id,
1564};
1565
1566static int __init lis3dh_acc_init(void)
1567{
1568 printk(KERN_INFO "%s accelerometer driver: init\n",
1569 LIS3DH_ACC_DEV_NAME);
1570 return i2c_add_driver(&lis3dh_acc_driver);
1571}
1572
1573static void __exit lis3dh_acc_exit(void)
1574{
1575#ifdef DEBUG
1576 printk(KERN_INFO "%s accelerometer driver exit\n",
1577 LIS3DH_ACC_DEV_NAME);
1578#endif /* DEBUG */
1579 i2c_del_driver(&lis3dh_acc_driver);
1580 return;
1581}
1582
1583module_init(lis3dh_acc_init);
1584module_exit(lis3dh_acc_exit);
1585
1586MODULE_DESCRIPTION("lis3dh digital accelerometer sysfs driver");
1587MODULE_AUTHOR("Matteo Dameno, Carmine Iascone, Samuel Huo, STMicroelectronics");
1588MODULE_LICENSE("GPL");
1589