26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/i2c.h>
29 #include <linux/input.h>
33 #include <linux/slab.h>
38 #define ABSMAX_ACC_VAL 0x01FF
39 #define ABSMIN_ACC_VAL -(ABSMAX_ACC_VAL)
42 #define BMA150_XYZ_DATA_SIZE 6
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
66 #define BMA150_DATA_0_REG 0x00
67 #define BMA150_DATA_1_REG 0x01
68 #define BMA150_DATA_2_REG 0x02
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
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
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
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
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
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
166 .any_motion_thres = 0,
193 static int bma150_set_reg_bits(
struct i2c_client *client,
194 int val,
int shift,
u8 mask,
u8 reg)
202 data = (data & ~mask) | ((val << shift) &
mask);
203 return bma150_write_byte(client, reg, data);
257 error = bma150_set_reg_bits(bma150->
client, hyst,
271 return bma150_set_reg_bits(bma150->
client, !!enable,
277 u8 enable,
u8 hyst,
u8 dur,
u8 thres)
281 error = bma150_set_reg_bits(bma150->
client, hyst,
287 error = bma150_write_byte(bma150->
client,
292 error = bma150_write_byte(bma150->
client,
297 return bma150_set_reg_bits(bma150->
client, !!enable,
304 u8 enable,
u8 dur,
u8 thres)
308 error = bma150_set_reg_bits(bma150->
client, dur,
315 error = bma150_write_byte(bma150->
client,
320 error = bma150_set_reg_bits(bma150->
client, !!enable,
326 return bma150_set_reg_bits(bma150->
client, !!enable,
332 static void bma150_report_xyz(
struct bma150_data *bma150)
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);
348 x = (
s16) (x << 6) >> 6;
349 y = (
s16) (y << 6) >> 6;
350 z = (
s16) (z << 6) >> 6;
355 input_sync(bma150->
input);
360 bma150_report_xyz(dev);
365 static void bma150_poll(
struct input_polled_dev *
dev)
367 bma150_report_xyz(dev->private);
374 error = pm_runtime_get_sync(&bma150->
client->dev);
375 if (error && error != -
ENOSYS)
391 static void bma150_close(
struct bma150_data *bma150)
393 pm_runtime_put_sync(&bma150->
client->dev);
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)
420 static void bma150_poll_close(
struct input_polled_dev *ipoll_dev)
424 bma150_close(bma150);
432 error = bma150_soft_reset(bma150);
436 error = bma150_set_bandwidth(bma150, cfg->
bandwidth);
440 error = bma150_set_range(bma150, cfg->
range);
444 if (bma150->
client->irq) {
445 error = bma150_set_any_motion_interrupt(bma150,
452 error = bma150_set_high_g_interrupt(bma150,
458 error = bma150_set_low_g_interrupt(bma150,
469 struct input_dev *
idev)
474 idev->dev.parent = &bma150->
client->dev;
484 struct input_dev *
idev;
487 idev = input_allocate_device();
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);
499 input_free_device(idev);
509 struct input_polled_dev *ipoll_dev;
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;
524 bma150_init_input_device(bma150, ipoll_dev->
input);
533 bma150->
input = ipoll_dev->input;
548 dev_err(&client->
dev,
"i2c_check_functionality error\n");
554 dev_err(&client->
dev,
"BMA150 chip id error: %d\n", chip_id);
569 "IRQ GPIO conf. error %d, error %d\n",
579 error = bma150_initialize(bma150, cfg);
583 if (client->
irq > 0) {
584 error = bma150_register_input_device(bma150);
589 NULL, bma150_irq_thread,
594 "irq request failed %d, error %d\n",
596 input_unregister_device(bma150->
input);
600 error = bma150_register_polled_device(bma150);
605 i2c_set_clientdata(client, bma150);
618 struct bma150_data *bma150 = i2c_get_clientdata(client);
620 pm_runtime_disable(&client->
dev);
622 if (client->
irq > 0) {
624 input_unregister_device(bma150->
input);
636 static int bma150_suspend(
struct device *
dev)
639 struct bma150_data *bma150 = i2c_get_clientdata(client);
644 static int bma150_resume(
struct device *
dev)
647 struct bma150_data *bma150 = i2c_get_clientdata(client);
671 .id_table = bma150_id,
672 .probe = bma150_probe,