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>
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_MODE_NORMAL 0
50 #define BMA150_MODE_SLEEP 2
51 #define BMA150_MODE_WAKE_UP 3
53 /* Data register addresses */
54 #define BMA150_DATA_0_REG 0x00
55 #define BMA150_DATA_1_REG 0x01
56 #define BMA150_DATA_2_REG 0x02
58 /* Control register addresses */
59 #define BMA150_CTRL_0_REG 0x0A
60 #define BMA150_CTRL_1_REG 0x0B
61 #define BMA150_CTRL_2_REG 0x14
62 #define BMA150_CTRL_3_REG 0x15
64 /* Configuration/Setting register addresses */
65 #define BMA150_CFG_0_REG 0x0C
66 #define BMA150_CFG_1_REG 0x0D
67 #define BMA150_CFG_2_REG 0x0E
68 #define BMA150_CFG_3_REG 0x0F
69 #define BMA150_CFG_4_REG 0x10
70 #define BMA150_CFG_5_REG 0x11
72 #define BMA150_CHIP_ID 2
73 #define BMA180_CHIP_ID 3
74 #define BMA150_CHIP_ID_REG BMA150_DATA_0_REG
76 #define BMA150_ACC_X_LSB_REG BMA150_DATA_2_REG
78 #define BMA150_SLEEP_POS 0
79 #define BMA150_SLEEP_MSK 0x01
80 #define BMA150_SLEEP_REG BMA150_CTRL_0_REG
82 #define BMA150_BANDWIDTH_POS 0
83 #define BMA150_BANDWIDTH_MSK 0x07
84 #define BMA150_BANDWIDTH_REG BMA150_CTRL_2_REG
86 #define BMA150_RANGE_POS 3
87 #define BMA150_RANGE_MSK 0x18
88 #define BMA150_RANGE_REG BMA150_CTRL_2_REG
90 #define BMA150_WAKE_UP_POS 0
91 #define BMA150_WAKE_UP_MSK 0x01
92 #define BMA150_WAKE_UP_REG BMA150_CTRL_3_REG
94 #define BMA150_SW_RES_POS 1
95 #define BMA150_SW_RES_MSK 0x02
96 #define BMA150_SW_RES_REG BMA150_CTRL_0_REG
98 /* Any-motion interrupt register fields */
99 #define BMA150_ANY_MOTION_EN_POS 6
100 #define BMA150_ANY_MOTION_EN_MSK 0x40
101 #define BMA150_ANY_MOTION_EN_REG BMA150_CTRL_1_REG
103 #define BMA150_ANY_MOTION_DUR_POS 6
104 #define BMA150_ANY_MOTION_DUR_MSK 0xC0
105 #define BMA150_ANY_MOTION_DUR_REG BMA150_CFG_5_REG
107 #define BMA150_ANY_MOTION_THRES_REG BMA150_CFG_4_REG
109 /* Advanced interrupt register fields */
110 #define BMA150_ADV_INT_EN_POS 6
111 #define BMA150_ADV_INT_EN_MSK 0x40
112 #define BMA150_ADV_INT_EN_REG BMA150_CTRL_3_REG
114 /* High-G interrupt register fields */
115 #define BMA150_HIGH_G_EN_POS 1
116 #define BMA150_HIGH_G_EN_MSK 0x02
117 #define BMA150_HIGH_G_EN_REG BMA150_CTRL_1_REG
119 #define BMA150_HIGH_G_HYST_POS 3
120 #define BMA150_HIGH_G_HYST_MSK 0x38
121 #define BMA150_HIGH_G_HYST_REG BMA150_CFG_5_REG
123 #define BMA150_HIGH_G_DUR_REG BMA150_CFG_3_REG
124 #define BMA150_HIGH_G_THRES_REG BMA150_CFG_2_REG
126 /* Low-G interrupt register fields */
127 #define BMA150_LOW_G_EN_POS 0
128 #define BMA150_LOW_G_EN_MSK 0x01
129 #define BMA150_LOW_G_EN_REG BMA150_CTRL_1_REG
131 #define BMA150_LOW_G_HYST_POS 0
132 #define BMA150_LOW_G_HYST_MSK 0x07
133 #define BMA150_LOW_G_HYST_REG BMA150_CFG_5_REG
135 #define BMA150_LOW_G_DUR_REG BMA150_CFG_1_REG
136 #define BMA150_LOW_G_THRES_REG BMA150_CFG_0_REG
139 struct i2c_client *client;
140 struct input_polled_dev *input_polled;
141 struct input_dev *input;
146 * The settings for the given range, bandwidth and interrupt features
147 * are stated and verified by Bosch Sensortec where they are configured
148 * to provide a generic sensitivity performance.
150 static struct bma150_cfg default_cfg = {
155 .any_motion_thres = 0,
162 .range = BMA150_RANGE_2G,
163 .bandwidth = BMA150_BW_50HZ
166 static int bma150_write_byte(struct i2c_client *client, u8 reg, u8 val)
170 /* As per specification, disable irq in between register writes */
172 disable_irq_nosync(client->irq);
174 ret = i2c_smbus_write_byte_data(client, reg, val);
177 enable_irq(client->irq);
182 static int bma150_set_reg_bits(struct i2c_client *client,
183 int val, int shift, u8 mask, u8 reg)
187 data = i2c_smbus_read_byte_data(client, reg);
191 data = (data & ~mask) | ((val << shift) & mask);
192 return bma150_write_byte(client, reg, data);
195 static int bma150_set_mode(struct bma150_data *bma150, u8 mode)
199 error = bma150_set_reg_bits(bma150->client, mode, BMA150_WAKE_UP_POS,
200 BMA150_WAKE_UP_MSK, BMA150_WAKE_UP_REG);
204 error = bma150_set_reg_bits(bma150->client, mode, BMA150_SLEEP_POS,
205 BMA150_SLEEP_MSK, BMA150_SLEEP_REG);
209 if (mode == BMA150_MODE_NORMAL)
216 static int bma150_soft_reset(struct bma150_data *bma150)
220 error = bma150_set_reg_bits(bma150->client, 1, BMA150_SW_RES_POS,
221 BMA150_SW_RES_MSK, BMA150_SW_RES_REG);
229 static int bma150_set_range(struct bma150_data *bma150, u8 range)
231 return bma150_set_reg_bits(bma150->client, range, BMA150_RANGE_POS,
232 BMA150_RANGE_MSK, BMA150_RANGE_REG);
235 static int bma150_set_bandwidth(struct bma150_data *bma150, u8 bw)
237 return bma150_set_reg_bits(bma150->client, bw, BMA150_BANDWIDTH_POS,
238 BMA150_BANDWIDTH_MSK, BMA150_BANDWIDTH_REG);
241 static int bma150_set_low_g_interrupt(struct bma150_data *bma150,
242 u8 enable, u8 hyst, u8 dur, u8 thres)
246 error = bma150_set_reg_bits(bma150->client, hyst,
247 BMA150_LOW_G_HYST_POS, BMA150_LOW_G_HYST_MSK,
248 BMA150_LOW_G_HYST_REG);
252 error = bma150_write_byte(bma150->client, BMA150_LOW_G_DUR_REG, dur);
256 error = bma150_write_byte(bma150->client, BMA150_LOW_G_THRES_REG, thres);
260 return bma150_set_reg_bits(bma150->client, !!enable,
261 BMA150_LOW_G_EN_POS, BMA150_LOW_G_EN_MSK,
262 BMA150_LOW_G_EN_REG);
265 static int bma150_set_high_g_interrupt(struct bma150_data *bma150,
266 u8 enable, u8 hyst, u8 dur, u8 thres)
270 error = bma150_set_reg_bits(bma150->client, hyst,
271 BMA150_HIGH_G_HYST_POS, BMA150_HIGH_G_HYST_MSK,
272 BMA150_HIGH_G_HYST_REG);
276 error = bma150_write_byte(bma150->client,
277 BMA150_HIGH_G_DUR_REG, dur);
281 error = bma150_write_byte(bma150->client,
282 BMA150_HIGH_G_THRES_REG, thres);
286 return bma150_set_reg_bits(bma150->client, !!enable,
287 BMA150_HIGH_G_EN_POS, BMA150_HIGH_G_EN_MSK,
288 BMA150_HIGH_G_EN_REG);
292 static int bma150_set_any_motion_interrupt(struct bma150_data *bma150,
293 u8 enable, u8 dur, u8 thres)
297 error = bma150_set_reg_bits(bma150->client, dur,
298 BMA150_ANY_MOTION_DUR_POS,
299 BMA150_ANY_MOTION_DUR_MSK,
300 BMA150_ANY_MOTION_DUR_REG);
304 error = bma150_write_byte(bma150->client,
305 BMA150_ANY_MOTION_THRES_REG, thres);
309 error = bma150_set_reg_bits(bma150->client, !!enable,
310 BMA150_ADV_INT_EN_POS, BMA150_ADV_INT_EN_MSK,
311 BMA150_ADV_INT_EN_REG);
315 return bma150_set_reg_bits(bma150->client, !!enable,
316 BMA150_ANY_MOTION_EN_POS,
317 BMA150_ANY_MOTION_EN_MSK,
318 BMA150_ANY_MOTION_EN_REG);
321 static void bma150_report_xyz(struct bma150_data *bma150)
323 u8 data[BMA150_XYZ_DATA_SIZE];
327 ret = i2c_smbus_read_i2c_block_data(bma150->client,
328 BMA150_ACC_X_LSB_REG, BMA150_XYZ_DATA_SIZE, data);
329 if (ret != BMA150_XYZ_DATA_SIZE)
332 x = ((0xc0 & data[0]) >> 6) | (data[1] << 2);
333 y = ((0xc0 & data[2]) >> 6) | (data[3] << 2);
334 z = ((0xc0 & data[4]) >> 6) | (data[5] << 2);
337 x = (s16) (x << 6) >> 6;
338 y = (s16) (y << 6) >> 6;
339 z = (s16) (z << 6) >> 6;
341 input_report_abs(bma150->input, ABS_X, x);
342 input_report_abs(bma150->input, ABS_Y, y);
343 input_report_abs(bma150->input, ABS_Z, z);
344 input_sync(bma150->input);
347 static irqreturn_t bma150_irq_thread(int irq, void *dev)
349 bma150_report_xyz(dev);
354 static void bma150_poll(struct input_polled_dev *dev)
356 bma150_report_xyz(dev->private);
359 static int bma150_open(struct bma150_data *bma150)
363 error = pm_runtime_get_sync(&bma150->client->dev);
364 if (error < 0 && error != -ENOSYS)
368 * See if runtime PM woke up the device. If runtime PM
369 * is disabled we need to do it ourselves.
371 if (bma150->mode != BMA150_MODE_NORMAL) {
372 error = bma150_set_mode(bma150, BMA150_MODE_NORMAL);
380 static void bma150_close(struct bma150_data *bma150)
382 pm_runtime_put_sync(&bma150->client->dev);
384 if (bma150->mode != BMA150_MODE_SLEEP)
385 bma150_set_mode(bma150, BMA150_MODE_SLEEP);
388 static int bma150_irq_open(struct input_dev *input)
390 struct bma150_data *bma150 = input_get_drvdata(input);
392 return bma150_open(bma150);
395 static void bma150_irq_close(struct input_dev *input)
397 struct bma150_data *bma150 = input_get_drvdata(input);
399 bma150_close(bma150);
402 static void bma150_poll_open(struct input_polled_dev *ipoll_dev)
404 struct bma150_data *bma150 = ipoll_dev->private;
409 static void bma150_poll_close(struct input_polled_dev *ipoll_dev)
411 struct bma150_data *bma150 = ipoll_dev->private;
413 bma150_close(bma150);
416 static int bma150_initialize(struct bma150_data *bma150,
417 const struct bma150_cfg *cfg)
421 error = bma150_soft_reset(bma150);
425 error = bma150_set_bandwidth(bma150, cfg->bandwidth);
429 error = bma150_set_range(bma150, cfg->range);
433 if (bma150->client->irq) {
434 error = bma150_set_any_motion_interrupt(bma150,
437 cfg->any_motion_thres);
441 error = bma150_set_high_g_interrupt(bma150,
442 cfg->hg_int, cfg->hg_hyst,
443 cfg->hg_dur, cfg->hg_thres);
447 error = bma150_set_low_g_interrupt(bma150,
448 cfg->lg_int, cfg->lg_hyst,
449 cfg->lg_dur, cfg->lg_thres);
454 return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
457 static void bma150_init_input_device(struct bma150_data *bma150,
458 struct input_dev *idev)
460 idev->name = BMA150_DRIVER;
461 idev->phys = BMA150_DRIVER "/input0";
462 idev->id.bustype = BUS_I2C;
463 idev->dev.parent = &bma150->client->dev;
465 idev->evbit[0] = BIT_MASK(EV_ABS);
466 input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
467 input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
468 input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
471 static int bma150_register_input_device(struct bma150_data *bma150)
473 struct input_dev *idev;
476 idev = input_allocate_device();
480 bma150_init_input_device(bma150, idev);
482 idev->open = bma150_irq_open;
483 idev->close = bma150_irq_close;
484 input_set_drvdata(idev, bma150);
486 error = input_register_device(idev);
488 input_free_device(idev);
492 bma150->input = idev;
496 static int bma150_register_polled_device(struct bma150_data *bma150)
498 struct input_polled_dev *ipoll_dev;
501 ipoll_dev = input_allocate_polled_device();
505 ipoll_dev->private = bma150;
506 ipoll_dev->open = bma150_poll_open;
507 ipoll_dev->close = bma150_poll_close;
508 ipoll_dev->poll = bma150_poll;
509 ipoll_dev->poll_interval = BMA150_POLL_INTERVAL;
510 ipoll_dev->poll_interval_min = BMA150_POLL_MIN;
511 ipoll_dev->poll_interval_max = BMA150_POLL_MAX;
513 bma150_init_input_device(bma150, ipoll_dev->input);
515 error = input_register_polled_device(ipoll_dev);
517 input_free_polled_device(ipoll_dev);
521 bma150->input_polled = ipoll_dev;
522 bma150->input = ipoll_dev->input;
527 static int bma150_probe(struct i2c_client *client,
528 const struct i2c_device_id *id)
530 const struct bma150_platform_data *pdata =
531 dev_get_platdata(&client->dev);
532 const struct bma150_cfg *cfg;
533 struct bma150_data *bma150;
537 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
538 dev_err(&client->dev, "i2c_check_functionality error\n");
542 chip_id = i2c_smbus_read_byte_data(client, BMA150_CHIP_ID_REG);
543 if (chip_id != BMA150_CHIP_ID && chip_id != BMA180_CHIP_ID) {
544 dev_err(&client->dev, "BMA150 chip id error: %d\n", chip_id);
548 bma150 = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
552 bma150->client = client;
555 if (pdata->irq_gpio_cfg) {
556 error = pdata->irq_gpio_cfg();
558 dev_err(&client->dev,
559 "IRQ GPIO conf. error %d, error %d\n",
569 error = bma150_initialize(bma150, cfg);
573 if (client->irq > 0) {
574 error = bma150_register_input_device(bma150);
578 error = request_threaded_irq(client->irq,
579 NULL, bma150_irq_thread,
580 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
581 BMA150_DRIVER, bma150);
583 dev_err(&client->dev,
584 "irq request failed %d, error %d\n",
586 input_unregister_device(bma150->input);
590 error = bma150_register_polled_device(bma150);
595 i2c_set_clientdata(client, bma150);
597 pm_runtime_enable(&client->dev);
606 static int bma150_remove(struct i2c_client *client)
608 struct bma150_data *bma150 = i2c_get_clientdata(client);
610 pm_runtime_disable(&client->dev);
612 if (client->irq > 0) {
613 free_irq(client->irq, bma150);
614 input_unregister_device(bma150->input);
616 input_unregister_polled_device(bma150->input_polled);
617 input_free_polled_device(bma150->input_polled);
626 static int bma150_suspend(struct device *dev)
628 struct i2c_client *client = to_i2c_client(dev);
629 struct bma150_data *bma150 = i2c_get_clientdata(client);
631 return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
634 static int bma150_resume(struct device *dev)
636 struct i2c_client *client = to_i2c_client(dev);
637 struct bma150_data *bma150 = i2c_get_clientdata(client);
639 return bma150_set_mode(bma150, BMA150_MODE_NORMAL);
643 static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL);
645 static const struct i2c_device_id bma150_id[] = {
653 MODULE_DEVICE_TABLE(i2c, bma150_id);
655 static struct i2c_driver bma150_driver = {
657 .owner = THIS_MODULE,
658 .name = BMA150_DRIVER,
661 .class = I2C_CLASS_HWMON,
662 .id_table = bma150_id,
663 .probe = bma150_probe,
664 .remove = bma150_remove,
667 module_i2c_driver(bma150_driver);
670 MODULE_DESCRIPTION("BMA150 driver");
671 MODULE_LICENSE("GPL");