1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2011 Bosch Sensortec GmbH
4 * Copyright (c) 2011 Unixphere
6 * This driver adds support for Bosch Sensortec's digital acceleration
7 * sensors BMA150 and SMB380.
8 * The SMB380 is fully compatible with BMA150 and only differs in packaging.
10 * The datasheet for the BMA150 chip can be found here:
11 * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMA150-DS000-07.pdf
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/bma150.h>
24 #define ABSMAX_ACC_VAL 0x01FF
25 #define ABSMIN_ACC_VAL -(ABSMAX_ACC_VAL)
27 /* Each axis is represented by a 2-byte data word */
28 #define BMA150_XYZ_DATA_SIZE 6
30 /* Input poll interval in milliseconds */
31 #define BMA150_POLL_INTERVAL 10
32 #define BMA150_POLL_MAX 200
33 #define BMA150_POLL_MIN 0
35 #define BMA150_MODE_NORMAL 0
36 #define BMA150_MODE_SLEEP 2
37 #define BMA150_MODE_WAKE_UP 3
39 /* Data register addresses */
40 #define BMA150_DATA_0_REG 0x00
41 #define BMA150_DATA_1_REG 0x01
42 #define BMA150_DATA_2_REG 0x02
44 /* Control register addresses */
45 #define BMA150_CTRL_0_REG 0x0A
46 #define BMA150_CTRL_1_REG 0x0B
47 #define BMA150_CTRL_2_REG 0x14
48 #define BMA150_CTRL_3_REG 0x15
50 /* Configuration/Setting register addresses */
51 #define BMA150_CFG_0_REG 0x0C
52 #define BMA150_CFG_1_REG 0x0D
53 #define BMA150_CFG_2_REG 0x0E
54 #define BMA150_CFG_3_REG 0x0F
55 #define BMA150_CFG_4_REG 0x10
56 #define BMA150_CFG_5_REG 0x11
58 #define BMA150_CHIP_ID 2
59 #define BMA150_CHIP_ID_REG BMA150_DATA_0_REG
61 #define BMA150_ACC_X_LSB_REG BMA150_DATA_2_REG
63 #define BMA150_SLEEP_POS 0
64 #define BMA150_SLEEP_MSK 0x01
65 #define BMA150_SLEEP_REG BMA150_CTRL_0_REG
67 #define BMA150_BANDWIDTH_POS 0
68 #define BMA150_BANDWIDTH_MSK 0x07
69 #define BMA150_BANDWIDTH_REG BMA150_CTRL_2_REG
71 #define BMA150_RANGE_POS 3
72 #define BMA150_RANGE_MSK 0x18
73 #define BMA150_RANGE_REG BMA150_CTRL_2_REG
75 #define BMA150_WAKE_UP_POS 0
76 #define BMA150_WAKE_UP_MSK 0x01
77 #define BMA150_WAKE_UP_REG BMA150_CTRL_3_REG
79 #define BMA150_SW_RES_POS 1
80 #define BMA150_SW_RES_MSK 0x02
81 #define BMA150_SW_RES_REG BMA150_CTRL_0_REG
83 /* Any-motion interrupt register fields */
84 #define BMA150_ANY_MOTION_EN_POS 6
85 #define BMA150_ANY_MOTION_EN_MSK 0x40
86 #define BMA150_ANY_MOTION_EN_REG BMA150_CTRL_1_REG
88 #define BMA150_ANY_MOTION_DUR_POS 6
89 #define BMA150_ANY_MOTION_DUR_MSK 0xC0
90 #define BMA150_ANY_MOTION_DUR_REG BMA150_CFG_5_REG
92 #define BMA150_ANY_MOTION_THRES_REG BMA150_CFG_4_REG
94 /* Advanced interrupt register fields */
95 #define BMA150_ADV_INT_EN_POS 6
96 #define BMA150_ADV_INT_EN_MSK 0x40
97 #define BMA150_ADV_INT_EN_REG BMA150_CTRL_3_REG
99 /* High-G interrupt register fields */
100 #define BMA150_HIGH_G_EN_POS 1
101 #define BMA150_HIGH_G_EN_MSK 0x02
102 #define BMA150_HIGH_G_EN_REG BMA150_CTRL_1_REG
104 #define BMA150_HIGH_G_HYST_POS 3
105 #define BMA150_HIGH_G_HYST_MSK 0x38
106 #define BMA150_HIGH_G_HYST_REG BMA150_CFG_5_REG
108 #define BMA150_HIGH_G_DUR_REG BMA150_CFG_3_REG
109 #define BMA150_HIGH_G_THRES_REG BMA150_CFG_2_REG
111 /* Low-G interrupt register fields */
112 #define BMA150_LOW_G_EN_POS 0
113 #define BMA150_LOW_G_EN_MSK 0x01
114 #define BMA150_LOW_G_EN_REG BMA150_CTRL_1_REG
116 #define BMA150_LOW_G_HYST_POS 0
117 #define BMA150_LOW_G_HYST_MSK 0x07
118 #define BMA150_LOW_G_HYST_REG BMA150_CFG_5_REG
120 #define BMA150_LOW_G_DUR_REG BMA150_CFG_1_REG
121 #define BMA150_LOW_G_THRES_REG BMA150_CFG_0_REG
124 struct i2c_client
*client
;
125 struct input_dev
*input
;
130 * The settings for the given range, bandwidth and interrupt features
131 * are stated and verified by Bosch Sensortec where they are configured
132 * to provide a generic sensitivity performance.
134 static const struct bma150_cfg default_cfg
= {
139 .any_motion_thres
= 0,
146 .range
= BMA150_RANGE_2G
,
147 .bandwidth
= BMA150_BW_50HZ
150 static int bma150_write_byte(struct i2c_client
*client
, u8 reg
, u8 val
)
154 /* As per specification, disable irq in between register writes */
156 disable_irq_nosync(client
->irq
);
158 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
161 enable_irq(client
->irq
);
166 static int bma150_set_reg_bits(struct i2c_client
*client
,
167 int val
, int shift
, u8 mask
, u8 reg
)
171 data
= i2c_smbus_read_byte_data(client
, reg
);
175 data
= (data
& ~mask
) | ((val
<< shift
) & mask
);
176 return bma150_write_byte(client
, reg
, data
);
179 static int bma150_set_mode(struct bma150_data
*bma150
, u8 mode
)
183 error
= bma150_set_reg_bits(bma150
->client
, mode
, BMA150_WAKE_UP_POS
,
184 BMA150_WAKE_UP_MSK
, BMA150_WAKE_UP_REG
);
188 error
= bma150_set_reg_bits(bma150
->client
, mode
, BMA150_SLEEP_POS
,
189 BMA150_SLEEP_MSK
, BMA150_SLEEP_REG
);
193 if (mode
== BMA150_MODE_NORMAL
)
194 usleep_range(2000, 2100);
200 static int bma150_soft_reset(struct bma150_data
*bma150
)
204 error
= bma150_set_reg_bits(bma150
->client
, 1, BMA150_SW_RES_POS
,
205 BMA150_SW_RES_MSK
, BMA150_SW_RES_REG
);
209 usleep_range(2000, 2100);
213 static int bma150_set_range(struct bma150_data
*bma150
, u8 range
)
215 return bma150_set_reg_bits(bma150
->client
, range
, BMA150_RANGE_POS
,
216 BMA150_RANGE_MSK
, BMA150_RANGE_REG
);
219 static int bma150_set_bandwidth(struct bma150_data
*bma150
, u8 bw
)
221 return bma150_set_reg_bits(bma150
->client
, bw
, BMA150_BANDWIDTH_POS
,
222 BMA150_BANDWIDTH_MSK
, BMA150_BANDWIDTH_REG
);
225 static int bma150_set_low_g_interrupt(struct bma150_data
*bma150
,
226 u8 enable
, u8 hyst
, u8 dur
, u8 thres
)
230 error
= bma150_set_reg_bits(bma150
->client
, hyst
,
231 BMA150_LOW_G_HYST_POS
, BMA150_LOW_G_HYST_MSK
,
232 BMA150_LOW_G_HYST_REG
);
236 error
= bma150_write_byte(bma150
->client
, BMA150_LOW_G_DUR_REG
, dur
);
240 error
= bma150_write_byte(bma150
->client
, BMA150_LOW_G_THRES_REG
, thres
);
244 return bma150_set_reg_bits(bma150
->client
, !!enable
,
245 BMA150_LOW_G_EN_POS
, BMA150_LOW_G_EN_MSK
,
246 BMA150_LOW_G_EN_REG
);
249 static int bma150_set_high_g_interrupt(struct bma150_data
*bma150
,
250 u8 enable
, u8 hyst
, u8 dur
, u8 thres
)
254 error
= bma150_set_reg_bits(bma150
->client
, hyst
,
255 BMA150_HIGH_G_HYST_POS
, BMA150_HIGH_G_HYST_MSK
,
256 BMA150_HIGH_G_HYST_REG
);
260 error
= bma150_write_byte(bma150
->client
,
261 BMA150_HIGH_G_DUR_REG
, dur
);
265 error
= bma150_write_byte(bma150
->client
,
266 BMA150_HIGH_G_THRES_REG
, thres
);
270 return bma150_set_reg_bits(bma150
->client
, !!enable
,
271 BMA150_HIGH_G_EN_POS
, BMA150_HIGH_G_EN_MSK
,
272 BMA150_HIGH_G_EN_REG
);
276 static int bma150_set_any_motion_interrupt(struct bma150_data
*bma150
,
277 u8 enable
, u8 dur
, u8 thres
)
281 error
= bma150_set_reg_bits(bma150
->client
, dur
,
282 BMA150_ANY_MOTION_DUR_POS
,
283 BMA150_ANY_MOTION_DUR_MSK
,
284 BMA150_ANY_MOTION_DUR_REG
);
288 error
= bma150_write_byte(bma150
->client
,
289 BMA150_ANY_MOTION_THRES_REG
, thres
);
293 error
= bma150_set_reg_bits(bma150
->client
, !!enable
,
294 BMA150_ADV_INT_EN_POS
, BMA150_ADV_INT_EN_MSK
,
295 BMA150_ADV_INT_EN_REG
);
299 return bma150_set_reg_bits(bma150
->client
, !!enable
,
300 BMA150_ANY_MOTION_EN_POS
,
301 BMA150_ANY_MOTION_EN_MSK
,
302 BMA150_ANY_MOTION_EN_REG
);
305 static void bma150_report_xyz(struct bma150_data
*bma150
)
307 u8 data
[BMA150_XYZ_DATA_SIZE
];
311 ret
= i2c_smbus_read_i2c_block_data(bma150
->client
,
312 BMA150_ACC_X_LSB_REG
, BMA150_XYZ_DATA_SIZE
, data
);
313 if (ret
!= BMA150_XYZ_DATA_SIZE
)
316 x
= ((0xc0 & data
[0]) >> 6) | (data
[1] << 2);
317 y
= ((0xc0 & data
[2]) >> 6) | (data
[3] << 2);
318 z
= ((0xc0 & data
[4]) >> 6) | (data
[5] << 2);
320 x
= sign_extend32(x
, 9);
321 y
= sign_extend32(y
, 9);
322 z
= sign_extend32(z
, 9);
324 input_report_abs(bma150
->input
, ABS_X
, x
);
325 input_report_abs(bma150
->input
, ABS_Y
, y
);
326 input_report_abs(bma150
->input
, ABS_Z
, z
);
327 input_sync(bma150
->input
);
330 static irqreturn_t
bma150_irq_thread(int irq
, void *dev
)
332 bma150_report_xyz(dev
);
337 static void bma150_poll(struct input_dev
*input
)
339 struct bma150_data
*bma150
= input_get_drvdata(input
);
341 bma150_report_xyz(bma150
);
344 static int bma150_open(struct input_dev
*input
)
346 struct bma150_data
*bma150
= input_get_drvdata(input
);
349 error
= pm_runtime_get_sync(&bma150
->client
->dev
);
350 if (error
< 0 && error
!= -ENOSYS
)
354 * See if runtime PM woke up the device. If runtime PM
355 * is disabled we need to do it ourselves.
357 if (bma150
->mode
!= BMA150_MODE_NORMAL
) {
358 error
= bma150_set_mode(bma150
, BMA150_MODE_NORMAL
);
366 static void bma150_close(struct input_dev
*input
)
368 struct bma150_data
*bma150
= input_get_drvdata(input
);
370 pm_runtime_put_sync(&bma150
->client
->dev
);
372 if (bma150
->mode
!= BMA150_MODE_SLEEP
)
373 bma150_set_mode(bma150
, BMA150_MODE_SLEEP
);
376 static int bma150_initialize(struct bma150_data
*bma150
,
377 const struct bma150_cfg
*cfg
)
381 error
= bma150_soft_reset(bma150
);
385 error
= bma150_set_bandwidth(bma150
, cfg
->bandwidth
);
389 error
= bma150_set_range(bma150
, cfg
->range
);
393 if (bma150
->client
->irq
) {
394 error
= bma150_set_any_motion_interrupt(bma150
,
397 cfg
->any_motion_thres
);
401 error
= bma150_set_high_g_interrupt(bma150
,
402 cfg
->hg_int
, cfg
->hg_hyst
,
403 cfg
->hg_dur
, cfg
->hg_thres
);
407 error
= bma150_set_low_g_interrupt(bma150
,
408 cfg
->lg_int
, cfg
->lg_hyst
,
409 cfg
->lg_dur
, cfg
->lg_thres
);
414 return bma150_set_mode(bma150
, BMA150_MODE_SLEEP
);
417 static int bma150_probe(struct i2c_client
*client
,
418 const struct i2c_device_id
*id
)
420 const struct bma150_platform_data
*pdata
=
421 dev_get_platdata(&client
->dev
);
422 const struct bma150_cfg
*cfg
;
423 struct bma150_data
*bma150
;
424 struct input_dev
*idev
;
428 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
429 dev_err(&client
->dev
, "i2c_check_functionality error\n");
433 chip_id
= i2c_smbus_read_byte_data(client
, BMA150_CHIP_ID_REG
);
434 if (chip_id
!= BMA150_CHIP_ID
) {
435 dev_err(&client
->dev
, "BMA150 chip id error: %d\n", chip_id
);
439 bma150
= devm_kzalloc(&client
->dev
, sizeof(*bma150
), GFP_KERNEL
);
443 bma150
->client
= client
;
446 if (pdata
->irq_gpio_cfg
) {
447 error
= pdata
->irq_gpio_cfg();
449 dev_err(&client
->dev
,
450 "IRQ GPIO conf. error %d, error %d\n",
460 error
= bma150_initialize(bma150
, cfg
);
464 idev
= devm_input_allocate_device(&bma150
->client
->dev
);
468 input_set_drvdata(idev
, bma150
);
469 bma150
->input
= idev
;
471 idev
->name
= BMA150_DRIVER
;
472 idev
->phys
= BMA150_DRIVER
"/input0";
473 idev
->id
.bustype
= BUS_I2C
;
475 idev
->open
= bma150_open
;
476 idev
->close
= bma150_close
;
478 input_set_abs_params(idev
, ABS_X
, ABSMIN_ACC_VAL
, ABSMAX_ACC_VAL
, 0, 0);
479 input_set_abs_params(idev
, ABS_Y
, ABSMIN_ACC_VAL
, ABSMAX_ACC_VAL
, 0, 0);
480 input_set_abs_params(idev
, ABS_Z
, ABSMIN_ACC_VAL
, ABSMAX_ACC_VAL
, 0, 0);
482 if (client
->irq
<= 0) {
483 error
= input_setup_polling(idev
, bma150_poll
);
487 input_set_poll_interval(idev
, BMA150_POLL_INTERVAL
);
488 input_set_min_poll_interval(idev
, BMA150_POLL_MIN
);
489 input_set_max_poll_interval(idev
, BMA150_POLL_MAX
);
492 error
= input_register_device(idev
);
496 if (client
->irq
> 0) {
497 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
498 NULL
, bma150_irq_thread
,
499 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
500 BMA150_DRIVER
, bma150
);
502 dev_err(&client
->dev
,
503 "irq request failed %d, error %d\n",
509 i2c_set_clientdata(client
, bma150
);
511 pm_runtime_enable(&client
->dev
);
516 static int bma150_remove(struct i2c_client
*client
)
518 pm_runtime_disable(&client
->dev
);
523 static int __maybe_unused
bma150_suspend(struct device
*dev
)
525 struct i2c_client
*client
= to_i2c_client(dev
);
526 struct bma150_data
*bma150
= i2c_get_clientdata(client
);
528 return bma150_set_mode(bma150
, BMA150_MODE_SLEEP
);
531 static int __maybe_unused
bma150_resume(struct device
*dev
)
533 struct i2c_client
*client
= to_i2c_client(dev
);
534 struct bma150_data
*bma150
= i2c_get_clientdata(client
);
536 return bma150_set_mode(bma150
, BMA150_MODE_NORMAL
);
539 static UNIVERSAL_DEV_PM_OPS(bma150_pm
, bma150_suspend
, bma150_resume
, NULL
);
541 static const struct i2c_device_id bma150_id
[] = {
548 MODULE_DEVICE_TABLE(i2c
, bma150_id
);
550 static struct i2c_driver bma150_driver
= {
552 .name
= BMA150_DRIVER
,
555 .class = I2C_CLASS_HWMON
,
556 .id_table
= bma150_id
,
557 .probe
= bma150_probe
,
558 .remove
= bma150_remove
,
561 module_i2c_driver(bma150_driver
);
563 MODULE_AUTHOR("Albert Zhang <xu.zhang@bosch-sensortec.com>");
564 MODULE_DESCRIPTION("BMA150 driver");
565 MODULE_LICENSE("GPL");