i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / input / misc / bma150.c
blob8f55b54352b623ec6e2b9ca18e27985ac1576db4
1 /*
2 * Copyright (c) 2011 Bosch Sensortec GmbH
3 * Copyright (c) 2011 Unixphere
5 * This driver adds support for Bosch Sensortec's digital acceleration
6 * sensors BMA150 and SMB380.
7 * The SMB380 is fully compatible with BMA150 and only differs in packaging.
9 * The datasheet for the BMA150 chip can be found here:
10 * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMA150-DS000-07.pdf
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/i2c.h>
29 #include <linux/input.h>
30 #include <linux/input-polldev.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/pm.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/bma150.h>
38 #define ABSMAX_ACC_VAL 0x01FF
39 #define ABSMIN_ACC_VAL -(ABSMAX_ACC_VAL)
41 /* Each axis is represented by a 2-byte data word */
42 #define BMA150_XYZ_DATA_SIZE 6
44 /* Input poll interval in milliseconds */
45 #define BMA150_POLL_INTERVAL 10
46 #define BMA150_POLL_MAX 200
47 #define BMA150_POLL_MIN 0
49 #define BMA150_BW_25HZ 0
50 #define BMA150_BW_50HZ 1
51 #define BMA150_BW_100HZ 2
52 #define BMA150_BW_190HZ 3
53 #define BMA150_BW_375HZ 4
54 #define BMA150_BW_750HZ 5
55 #define BMA150_BW_1500HZ 6
57 #define BMA150_RANGE_2G 0
58 #define BMA150_RANGE_4G 1
59 #define BMA150_RANGE_8G 2
61 #define BMA150_MODE_NORMAL 0
62 #define BMA150_MODE_SLEEP 2
63 #define BMA150_MODE_WAKE_UP 3
65 /* Data register addresses */
66 #define BMA150_DATA_0_REG 0x00
67 #define BMA150_DATA_1_REG 0x01
68 #define BMA150_DATA_2_REG 0x02
70 /* Control register addresses */
71 #define BMA150_CTRL_0_REG 0x0A
72 #define BMA150_CTRL_1_REG 0x0B
73 #define BMA150_CTRL_2_REG 0x14
74 #define BMA150_CTRL_3_REG 0x15
76 /* Configuration/Setting register addresses */
77 #define BMA150_CFG_0_REG 0x0C
78 #define BMA150_CFG_1_REG 0x0D
79 #define BMA150_CFG_2_REG 0x0E
80 #define BMA150_CFG_3_REG 0x0F
81 #define BMA150_CFG_4_REG 0x10
82 #define BMA150_CFG_5_REG 0x11
84 #define BMA150_CHIP_ID 2
85 #define BMA150_CHIP_ID_REG BMA150_DATA_0_REG
87 #define BMA150_ACC_X_LSB_REG BMA150_DATA_2_REG
89 #define BMA150_SLEEP_POS 0
90 #define BMA150_SLEEP_MSK 0x01
91 #define BMA150_SLEEP_REG BMA150_CTRL_0_REG
93 #define BMA150_BANDWIDTH_POS 0
94 #define BMA150_BANDWIDTH_MSK 0x07
95 #define BMA150_BANDWIDTH_REG BMA150_CTRL_2_REG
97 #define BMA150_RANGE_POS 3
98 #define BMA150_RANGE_MSK 0x18
99 #define BMA150_RANGE_REG BMA150_CTRL_2_REG
101 #define BMA150_WAKE_UP_POS 0
102 #define BMA150_WAKE_UP_MSK 0x01
103 #define BMA150_WAKE_UP_REG BMA150_CTRL_3_REG
105 #define BMA150_SW_RES_POS 1
106 #define BMA150_SW_RES_MSK 0x02
107 #define BMA150_SW_RES_REG BMA150_CTRL_0_REG
109 /* Any-motion interrupt register fields */
110 #define BMA150_ANY_MOTION_EN_POS 6
111 #define BMA150_ANY_MOTION_EN_MSK 0x40
112 #define BMA150_ANY_MOTION_EN_REG BMA150_CTRL_1_REG
114 #define BMA150_ANY_MOTION_DUR_POS 6
115 #define BMA150_ANY_MOTION_DUR_MSK 0xC0
116 #define BMA150_ANY_MOTION_DUR_REG BMA150_CFG_5_REG
118 #define BMA150_ANY_MOTION_THRES_REG BMA150_CFG_4_REG
120 /* Advanced interrupt register fields */
121 #define BMA150_ADV_INT_EN_POS 6
122 #define BMA150_ADV_INT_EN_MSK 0x40
123 #define BMA150_ADV_INT_EN_REG BMA150_CTRL_3_REG
125 /* High-G interrupt register fields */
126 #define BMA150_HIGH_G_EN_POS 1
127 #define BMA150_HIGH_G_EN_MSK 0x02
128 #define BMA150_HIGH_G_EN_REG BMA150_CTRL_1_REG
130 #define BMA150_HIGH_G_HYST_POS 3
131 #define BMA150_HIGH_G_HYST_MSK 0x38
132 #define BMA150_HIGH_G_HYST_REG BMA150_CFG_5_REG
134 #define BMA150_HIGH_G_DUR_REG BMA150_CFG_3_REG
135 #define BMA150_HIGH_G_THRES_REG BMA150_CFG_2_REG
137 /* Low-G interrupt register fields */
138 #define BMA150_LOW_G_EN_POS 0
139 #define BMA150_LOW_G_EN_MSK 0x01
140 #define BMA150_LOW_G_EN_REG BMA150_CTRL_1_REG
142 #define BMA150_LOW_G_HYST_POS 0
143 #define BMA150_LOW_G_HYST_MSK 0x07
144 #define BMA150_LOW_G_HYST_REG BMA150_CFG_5_REG
146 #define BMA150_LOW_G_DUR_REG BMA150_CFG_1_REG
147 #define BMA150_LOW_G_THRES_REG BMA150_CFG_0_REG
149 struct bma150_data {
150 struct i2c_client *client;
151 struct input_polled_dev *input_polled;
152 struct input_dev *input;
153 u8 mode;
157 * The settings for the given range, bandwidth and interrupt features
158 * are stated and verified by Bosch Sensortec where they are configured
159 * to provide a generic sensitivity performance.
161 static struct bma150_cfg default_cfg __devinitdata = {
162 .any_motion_int = 1,
163 .hg_int = 1,
164 .lg_int = 1,
165 .any_motion_dur = 0,
166 .any_motion_thres = 0,
167 .hg_hyst = 0,
168 .hg_dur = 150,
169 .hg_thres = 160,
170 .lg_hyst = 0,
171 .lg_dur = 150,
172 .lg_thres = 20,
173 .range = BMA150_RANGE_2G,
174 .bandwidth = BMA150_BW_50HZ
177 static int bma150_write_byte(struct i2c_client *client, u8 reg, u8 val)
179 s32 ret;
181 /* As per specification, disable irq in between register writes */
182 if (client->irq)
183 disable_irq_nosync(client->irq);
185 ret = i2c_smbus_write_byte_data(client, reg, val);
187 if (client->irq)
188 enable_irq(client->irq);
190 return ret;
193 static int bma150_set_reg_bits(struct i2c_client *client,
194 int val, int shift, u8 mask, u8 reg)
196 int data;
198 data = i2c_smbus_read_byte_data(client, reg);
199 if (data < 0)
200 return data;
202 data = (data & ~mask) | ((val << shift) & mask);
203 return bma150_write_byte(client, reg, data);
206 static int bma150_set_mode(struct bma150_data *bma150, u8 mode)
208 int error;
210 error = bma150_set_reg_bits(bma150->client, mode, BMA150_WAKE_UP_POS,
211 BMA150_WAKE_UP_MSK, BMA150_WAKE_UP_REG);
212 if (error)
213 return error;
215 error = bma150_set_reg_bits(bma150->client, mode, BMA150_SLEEP_POS,
216 BMA150_SLEEP_MSK, BMA150_SLEEP_REG);
217 if (error)
218 return error;
220 if (mode == BMA150_MODE_NORMAL)
221 msleep(2);
223 bma150->mode = mode;
224 return 0;
227 static int __devinit bma150_soft_reset(struct bma150_data *bma150)
229 int error;
231 error = bma150_set_reg_bits(bma150->client, 1, BMA150_SW_RES_POS,
232 BMA150_SW_RES_MSK, BMA150_SW_RES_REG);
233 if (error)
234 return error;
236 msleep(2);
237 return 0;
240 static int __devinit bma150_set_range(struct bma150_data *bma150, u8 range)
242 return bma150_set_reg_bits(bma150->client, range, BMA150_RANGE_POS,
243 BMA150_RANGE_MSK, BMA150_RANGE_REG);
246 static int __devinit bma150_set_bandwidth(struct bma150_data *bma150, u8 bw)
248 return bma150_set_reg_bits(bma150->client, bw, BMA150_BANDWIDTH_POS,
249 BMA150_BANDWIDTH_MSK, BMA150_BANDWIDTH_REG);
252 static int __devinit bma150_set_low_g_interrupt(struct bma150_data *bma150,
253 u8 enable, u8 hyst, u8 dur, u8 thres)
255 int error;
257 error = bma150_set_reg_bits(bma150->client, hyst,
258 BMA150_LOW_G_HYST_POS, BMA150_LOW_G_HYST_MSK,
259 BMA150_LOW_G_HYST_REG);
260 if (error)
261 return error;
263 error = bma150_write_byte(bma150->client, BMA150_LOW_G_DUR_REG, dur);
264 if (error)
265 return error;
267 error = bma150_write_byte(bma150->client, BMA150_LOW_G_THRES_REG, thres);
268 if (error)
269 return error;
271 return bma150_set_reg_bits(bma150->client, !!enable,
272 BMA150_LOW_G_EN_POS, BMA150_LOW_G_EN_MSK,
273 BMA150_LOW_G_EN_REG);
276 static int __devinit bma150_set_high_g_interrupt(struct bma150_data *bma150,
277 u8 enable, u8 hyst, u8 dur, u8 thres)
279 int error;
281 error = bma150_set_reg_bits(bma150->client, hyst,
282 BMA150_HIGH_G_HYST_POS, BMA150_HIGH_G_HYST_MSK,
283 BMA150_HIGH_G_HYST_REG);
284 if (error)
285 return error;
287 error = bma150_write_byte(bma150->client,
288 BMA150_HIGH_G_DUR_REG, dur);
289 if (error)
290 return error;
292 error = bma150_write_byte(bma150->client,
293 BMA150_HIGH_G_THRES_REG, thres);
294 if (error)
295 return error;
297 return bma150_set_reg_bits(bma150->client, !!enable,
298 BMA150_HIGH_G_EN_POS, BMA150_HIGH_G_EN_MSK,
299 BMA150_HIGH_G_EN_REG);
303 static int __devinit bma150_set_any_motion_interrupt(struct bma150_data *bma150,
304 u8 enable, u8 dur, u8 thres)
306 int error;
308 error = bma150_set_reg_bits(bma150->client, dur,
309 BMA150_ANY_MOTION_DUR_POS,
310 BMA150_ANY_MOTION_DUR_MSK,
311 BMA150_ANY_MOTION_DUR_REG);
312 if (error)
313 return error;
315 error = bma150_write_byte(bma150->client,
316 BMA150_ANY_MOTION_THRES_REG, thres);
317 if (error)
318 return error;
320 error = bma150_set_reg_bits(bma150->client, !!enable,
321 BMA150_ADV_INT_EN_POS, BMA150_ADV_INT_EN_MSK,
322 BMA150_ADV_INT_EN_REG);
323 if (error)
324 return error;
326 return bma150_set_reg_bits(bma150->client, !!enable,
327 BMA150_ANY_MOTION_EN_POS,
328 BMA150_ANY_MOTION_EN_MSK,
329 BMA150_ANY_MOTION_EN_REG);
332 static void bma150_report_xyz(struct bma150_data *bma150)
334 u8 data[BMA150_XYZ_DATA_SIZE];
335 s16 x, y, z;
336 s32 ret;
338 ret = i2c_smbus_read_i2c_block_data(bma150->client,
339 BMA150_ACC_X_LSB_REG, BMA150_XYZ_DATA_SIZE, data);
340 if (ret != BMA150_XYZ_DATA_SIZE)
341 return;
343 x = ((0xc0 & data[0]) >> 6) | (data[1] << 2);
344 y = ((0xc0 & data[2]) >> 6) | (data[3] << 2);
345 z = ((0xc0 & data[4]) >> 6) | (data[5] << 2);
347 /* sign extension */
348 x = (s16) (x << 6) >> 6;
349 y = (s16) (y << 6) >> 6;
350 z = (s16) (z << 6) >> 6;
352 input_report_abs(bma150->input, ABS_X, x);
353 input_report_abs(bma150->input, ABS_Y, y);
354 input_report_abs(bma150->input, ABS_Z, z);
355 input_sync(bma150->input);
358 static irqreturn_t bma150_irq_thread(int irq, void *dev)
360 bma150_report_xyz(dev);
362 return IRQ_HANDLED;
365 static void bma150_poll(struct input_polled_dev *dev)
367 bma150_report_xyz(dev->private);
370 static int bma150_open(struct bma150_data *bma150)
372 int error;
374 error = pm_runtime_get_sync(&bma150->client->dev);
375 if (error && error != -ENOSYS)
376 return error;
379 * See if runtime PM woke up the device. If runtime PM
380 * is disabled we need to do it ourselves.
382 if (bma150->mode != BMA150_MODE_NORMAL) {
383 error = bma150_set_mode(bma150, BMA150_MODE_NORMAL);
384 if (error)
385 return error;
388 return 0;
391 static void bma150_close(struct bma150_data *bma150)
393 pm_runtime_put_sync(&bma150->client->dev);
395 if (bma150->mode != BMA150_MODE_SLEEP)
396 bma150_set_mode(bma150, BMA150_MODE_SLEEP);
399 static int bma150_irq_open(struct input_dev *input)
401 struct bma150_data *bma150 = input_get_drvdata(input);
403 return bma150_open(bma150);
406 static void bma150_irq_close(struct input_dev *input)
408 struct bma150_data *bma150 = input_get_drvdata(input);
410 bma150_close(bma150);
413 static void bma150_poll_open(struct input_polled_dev *ipoll_dev)
415 struct bma150_data *bma150 = ipoll_dev->private;
417 bma150_open(bma150);
420 static void bma150_poll_close(struct input_polled_dev *ipoll_dev)
422 struct bma150_data *bma150 = ipoll_dev->private;
424 bma150_close(bma150);
427 static int __devinit bma150_initialize(struct bma150_data *bma150,
428 const struct bma150_cfg *cfg)
430 int error;
432 error = bma150_soft_reset(bma150);
433 if (error)
434 return error;
436 error = bma150_set_bandwidth(bma150, cfg->bandwidth);
437 if (error)
438 return error;
440 error = bma150_set_range(bma150, cfg->range);
441 if (error)
442 return error;
444 if (bma150->client->irq) {
445 error = bma150_set_any_motion_interrupt(bma150,
446 cfg->any_motion_int,
447 cfg->any_motion_dur,
448 cfg->any_motion_thres);
449 if (error)
450 return error;
452 error = bma150_set_high_g_interrupt(bma150,
453 cfg->hg_int, cfg->hg_hyst,
454 cfg->hg_dur, cfg->hg_thres);
455 if (error)
456 return error;
458 error = bma150_set_low_g_interrupt(bma150,
459 cfg->lg_int, cfg->lg_hyst,
460 cfg->lg_dur, cfg->lg_thres);
461 if (error)
462 return error;
465 return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
468 static void __devinit bma150_init_input_device(struct bma150_data *bma150,
469 struct input_dev *idev)
471 idev->name = BMA150_DRIVER;
472 idev->phys = BMA150_DRIVER "/input0";
473 idev->id.bustype = BUS_I2C;
474 idev->dev.parent = &bma150->client->dev;
476 idev->evbit[0] = BIT_MASK(EV_ABS);
477 input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
478 input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
479 input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
482 static int __devinit bma150_register_input_device(struct bma150_data *bma150)
484 struct input_dev *idev;
485 int error;
487 idev = input_allocate_device();
488 if (!idev)
489 return -ENOMEM;
491 bma150_init_input_device(bma150, idev);
493 idev->open = bma150_irq_open;
494 idev->close = bma150_irq_close;
495 input_set_drvdata(idev, bma150);
497 error = input_register_device(idev);
498 if (error) {
499 input_free_device(idev);
500 return error;
503 bma150->input = idev;
504 return 0;
507 static int __devinit bma150_register_polled_device(struct bma150_data *bma150)
509 struct input_polled_dev *ipoll_dev;
510 int error;
512 ipoll_dev = input_allocate_polled_device();
513 if (!ipoll_dev)
514 return -ENOMEM;
516 ipoll_dev->private = bma150;
517 ipoll_dev->open = bma150_poll_open;
518 ipoll_dev->close = bma150_poll_close;
519 ipoll_dev->poll = bma150_poll;
520 ipoll_dev->poll_interval = BMA150_POLL_INTERVAL;
521 ipoll_dev->poll_interval_min = BMA150_POLL_MIN;
522 ipoll_dev->poll_interval_max = BMA150_POLL_MAX;
524 bma150_init_input_device(bma150, ipoll_dev->input);
526 error = input_register_polled_device(ipoll_dev);
527 if (error) {
528 input_free_polled_device(ipoll_dev);
529 return error;
532 bma150->input_polled = ipoll_dev;
533 bma150->input = ipoll_dev->input;
535 return 0;
538 static int __devinit bma150_probe(struct i2c_client *client,
539 const struct i2c_device_id *id)
541 const struct bma150_platform_data *pdata = client->dev.platform_data;
542 const struct bma150_cfg *cfg;
543 struct bma150_data *bma150;
544 int chip_id;
545 int error;
547 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
548 dev_err(&client->dev, "i2c_check_functionality error\n");
549 return -EIO;
552 chip_id = i2c_smbus_read_byte_data(client, BMA150_CHIP_ID_REG);
553 if (chip_id != BMA150_CHIP_ID) {
554 dev_err(&client->dev, "BMA150 chip id error: %d\n", chip_id);
555 return -EINVAL;
558 bma150 = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
559 if (!bma150)
560 return -ENOMEM;
562 bma150->client = client;
564 if (pdata) {
565 if (pdata->irq_gpio_cfg) {
566 error = pdata->irq_gpio_cfg();
567 if (error) {
568 dev_err(&client->dev,
569 "IRQ GPIO conf. error %d, error %d\n",
570 client->irq, error);
571 goto err_free_mem;
574 cfg = &pdata->cfg;
575 } else {
576 cfg = &default_cfg;
579 error = bma150_initialize(bma150, cfg);
580 if (error)
581 goto err_free_mem;
583 if (client->irq > 0) {
584 error = bma150_register_input_device(bma150);
585 if (error)
586 goto err_free_mem;
588 error = request_threaded_irq(client->irq,
589 NULL, bma150_irq_thread,
590 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
591 BMA150_DRIVER, bma150);
592 if (error) {
593 dev_err(&client->dev,
594 "irq request failed %d, error %d\n",
595 client->irq, error);
596 input_unregister_device(bma150->input);
597 goto err_free_mem;
599 } else {
600 error = bma150_register_polled_device(bma150);
601 if (error)
602 goto err_free_mem;
605 i2c_set_clientdata(client, bma150);
607 pm_runtime_enable(&client->dev);
609 return 0;
611 err_free_mem:
612 kfree(bma150);
613 return error;
616 static int __devexit bma150_remove(struct i2c_client *client)
618 struct bma150_data *bma150 = i2c_get_clientdata(client);
620 pm_runtime_disable(&client->dev);
622 if (client->irq > 0) {
623 free_irq(client->irq, bma150);
624 input_unregister_device(bma150->input);
625 } else {
626 input_unregister_polled_device(bma150->input_polled);
627 input_free_polled_device(bma150->input_polled);
630 kfree(bma150);
632 return 0;
635 #ifdef CONFIG_PM
636 static int bma150_suspend(struct device *dev)
638 struct i2c_client *client = to_i2c_client(dev);
639 struct bma150_data *bma150 = i2c_get_clientdata(client);
641 return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
644 static int bma150_resume(struct device *dev)
646 struct i2c_client *client = to_i2c_client(dev);
647 struct bma150_data *bma150 = i2c_get_clientdata(client);
649 return bma150_set_mode(bma150, BMA150_MODE_NORMAL);
651 #endif
653 static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL);
655 static const struct i2c_device_id bma150_id[] = {
656 { "bma150", 0 },
657 { "smb380", 0 },
658 { "bma023", 0 },
662 MODULE_DEVICE_TABLE(i2c, bma150_id);
664 static struct i2c_driver bma150_driver = {
665 .driver = {
666 .owner = THIS_MODULE,
667 .name = BMA150_DRIVER,
668 .pm = &bma150_pm,
670 .class = I2C_CLASS_HWMON,
671 .id_table = bma150_id,
672 .probe = bma150_probe,
673 .remove = __devexit_p(bma150_remove),
676 static int __init BMA150_init(void)
678 return i2c_add_driver(&bma150_driver);
681 static void __exit BMA150_exit(void)
683 i2c_del_driver(&bma150_driver);
686 MODULE_AUTHOR("Albert Zhang <xu.zhang@bosch-sensortec.com>");
687 MODULE_DESCRIPTION("BMA150 driver");
688 MODULE_LICENSE("GPL");
690 module_init(BMA150_init);
691 module_exit(BMA150_exit);