Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Copyright (c) 2011 Bosch Sensortec GmbH
  3 * Copyright (c) 2011 Unixphere
  4 *
  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.
  8 *
  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
 11 *
 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.
 16 *
 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.
 21 *
 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.
 25 */
 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>
 37
 38#define ABSMAX_ACC_VAL		0x01FF
 39#define ABSMIN_ACC_VAL		-(ABSMAX_ACC_VAL)
 40
 41/* Each axis is represented by a 2-byte data word */
 42#define BMA150_XYZ_DATA_SIZE	6
 43
 44/* Input poll interval in milliseconds */
 45#define BMA150_POLL_INTERVAL	10
 46#define BMA150_POLL_MAX		200
 47#define BMA150_POLL_MIN		0
 48
 49#define BMA150_MODE_NORMAL	0
 50#define BMA150_MODE_SLEEP	2
 51#define BMA150_MODE_WAKE_UP	3
 52
 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
 57
 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
 63
 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
 71
 72#define BMA150_CHIP_ID		2
 73#define BMA180_CHIP_ID		3
 74#define BMA150_CHIP_ID_REG	BMA150_DATA_0_REG
 75
 76#define BMA150_ACC_X_LSB_REG	BMA150_DATA_2_REG
 77
 78#define BMA150_SLEEP_POS	0
 79#define BMA150_SLEEP_MSK	0x01
 80#define BMA150_SLEEP_REG	BMA150_CTRL_0_REG
 81
 82#define BMA150_BANDWIDTH_POS	0
 83#define BMA150_BANDWIDTH_MSK	0x07
 84#define BMA150_BANDWIDTH_REG	BMA150_CTRL_2_REG
 85
 86#define BMA150_RANGE_POS	3
 87#define BMA150_RANGE_MSK	0x18
 88#define BMA150_RANGE_REG	BMA150_CTRL_2_REG
 89
 90#define BMA150_WAKE_UP_POS	0
 91#define BMA150_WAKE_UP_MSK	0x01
 92#define BMA150_WAKE_UP_REG	BMA150_CTRL_3_REG
 93
 94#define BMA150_SW_RES_POS	1
 95#define BMA150_SW_RES_MSK	0x02
 96#define BMA150_SW_RES_REG	BMA150_CTRL_0_REG
 97
 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
102
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
106
107#define BMA150_ANY_MOTION_THRES_REG	BMA150_CFG_4_REG
108
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
113
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
118
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
122
123#define BMA150_HIGH_G_DUR_REG		BMA150_CFG_3_REG
124#define BMA150_HIGH_G_THRES_REG		BMA150_CFG_2_REG
125
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
130
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
134
135#define BMA150_LOW_G_DUR_REG		BMA150_CFG_1_REG
136#define BMA150_LOW_G_THRES_REG		BMA150_CFG_0_REG
137
138struct bma150_data {
139	struct i2c_client *client;
140	struct input_polled_dev *input_polled;
141	struct input_dev *input;
142	u8 mode;
143};
144
145/*
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.
149 */
150static const struct bma150_cfg default_cfg = {
151	.any_motion_int = 1,
152	.hg_int = 1,
153	.lg_int = 1,
154	.any_motion_dur = 0,
155	.any_motion_thres = 0,
156	.hg_hyst = 0,
157	.hg_dur = 150,
158	.hg_thres = 160,
159	.lg_hyst = 0,
160	.lg_dur = 150,
161	.lg_thres = 20,
162	.range = BMA150_RANGE_2G,
163	.bandwidth = BMA150_BW_50HZ
164};
165
166static int bma150_write_byte(struct i2c_client *client, u8 reg, u8 val)
167{
168	s32 ret;
169
170	/* As per specification, disable irq in between register writes */
171	if (client->irq)
172		disable_irq_nosync(client->irq);
173
174	ret = i2c_smbus_write_byte_data(client, reg, val);
175
176	if (client->irq)
177		enable_irq(client->irq);
178
179	return ret;
180}
181
182static int bma150_set_reg_bits(struct i2c_client *client,
183					int val, int shift, u8 mask, u8 reg)
184{
185	int data;
186
187	data = i2c_smbus_read_byte_data(client, reg);
188	if (data < 0)
189		return data;
190
191	data = (data & ~mask) | ((val << shift) & mask);
192	return bma150_write_byte(client, reg, data);
193}
194
195static int bma150_set_mode(struct bma150_data *bma150, u8 mode)
196{
197	int error;
198
199	error = bma150_set_reg_bits(bma150->client, mode, BMA150_WAKE_UP_POS,
200				BMA150_WAKE_UP_MSK, BMA150_WAKE_UP_REG);
201	if (error)
202		return error;
203
204	error = bma150_set_reg_bits(bma150->client, mode, BMA150_SLEEP_POS,
205				BMA150_SLEEP_MSK, BMA150_SLEEP_REG);
206	if (error)
207		return error;
208
209	if (mode == BMA150_MODE_NORMAL)
210		msleep(2);
211
212	bma150->mode = mode;
213	return 0;
214}
215
216static int bma150_soft_reset(struct bma150_data *bma150)
217{
218	int error;
219
220	error = bma150_set_reg_bits(bma150->client, 1, BMA150_SW_RES_POS,
221				BMA150_SW_RES_MSK, BMA150_SW_RES_REG);
222	if (error)
223		return error;
224
225	msleep(2);
226	return 0;
227}
228
229static int bma150_set_range(struct bma150_data *bma150, u8 range)
230{
231	return bma150_set_reg_bits(bma150->client, range, BMA150_RANGE_POS,
232				BMA150_RANGE_MSK, BMA150_RANGE_REG);
233}
234
235static int bma150_set_bandwidth(struct bma150_data *bma150, u8 bw)
236{
237	return bma150_set_reg_bits(bma150->client, bw, BMA150_BANDWIDTH_POS,
238				BMA150_BANDWIDTH_MSK, BMA150_BANDWIDTH_REG);
239}
240
241static int bma150_set_low_g_interrupt(struct bma150_data *bma150,
242					u8 enable, u8 hyst, u8 dur, u8 thres)
243{
244	int error;
245
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);
249	if (error)
250		return error;
251
252	error = bma150_write_byte(bma150->client, BMA150_LOW_G_DUR_REG, dur);
253	if (error)
254		return error;
255
256	error = bma150_write_byte(bma150->client, BMA150_LOW_G_THRES_REG, thres);
257	if (error)
258		return error;
259
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);
263}
264
265static int bma150_set_high_g_interrupt(struct bma150_data *bma150,
266					u8 enable, u8 hyst, u8 dur, u8 thres)
267{
268	int error;
269
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);
273	if (error)
274		return error;
275
276	error = bma150_write_byte(bma150->client,
277				BMA150_HIGH_G_DUR_REG, dur);
278	if (error)
279		return error;
280
281	error = bma150_write_byte(bma150->client,
282				BMA150_HIGH_G_THRES_REG, thres);
283	if (error)
284		return error;
285
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);
289}
290
291
292static int bma150_set_any_motion_interrupt(struct bma150_data *bma150,
293						u8 enable, u8 dur, u8 thres)
294{
295	int error;
296
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);
301	if (error)
302		return error;
303
304	error = bma150_write_byte(bma150->client,
305				BMA150_ANY_MOTION_THRES_REG, thres);
306	if (error)
307		return error;
308
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);
312	if (error)
313		return error;
314
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);
319}
320
321static void bma150_report_xyz(struct bma150_data *bma150)
322{
323	u8 data[BMA150_XYZ_DATA_SIZE];
324	s16 x, y, z;
325	s32 ret;
326
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)
330		return;
331
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);
335
336	x = sign_extend32(x, 9);
337	y = sign_extend32(y, 9);
338	z = sign_extend32(z, 9);
339
340	input_report_abs(bma150->input, ABS_X, x);
341	input_report_abs(bma150->input, ABS_Y, y);
342	input_report_abs(bma150->input, ABS_Z, z);
343	input_sync(bma150->input);
344}
345
346static irqreturn_t bma150_irq_thread(int irq, void *dev)
347{
348	bma150_report_xyz(dev);
349
350	return IRQ_HANDLED;
351}
352
353static void bma150_poll(struct input_polled_dev *dev)
354{
355	bma150_report_xyz(dev->private);
356}
357
358static int bma150_open(struct bma150_data *bma150)
359{
360	int error;
361
362	error = pm_runtime_get_sync(&bma150->client->dev);
363	if (error < 0 && error != -ENOSYS)
364		return error;
365
366	/*
367	 * See if runtime PM woke up the device. If runtime PM
368	 * is disabled we need to do it ourselves.
369	 */
370	if (bma150->mode != BMA150_MODE_NORMAL) {
371		error = bma150_set_mode(bma150, BMA150_MODE_NORMAL);
372		if (error)
373			return error;
374	}
375
376	return 0;
377}
378
379static void bma150_close(struct bma150_data *bma150)
380{
381	pm_runtime_put_sync(&bma150->client->dev);
382
383	if (bma150->mode != BMA150_MODE_SLEEP)
384		bma150_set_mode(bma150, BMA150_MODE_SLEEP);
385}
386
387static int bma150_irq_open(struct input_dev *input)
388{
389	struct bma150_data *bma150 = input_get_drvdata(input);
390
391	return bma150_open(bma150);
392}
393
394static void bma150_irq_close(struct input_dev *input)
395{
396	struct bma150_data *bma150 = input_get_drvdata(input);
397
398	bma150_close(bma150);
399}
400
401static void bma150_poll_open(struct input_polled_dev *ipoll_dev)
402{
403	struct bma150_data *bma150 = ipoll_dev->private;
404
405	bma150_open(bma150);
406}
407
408static void bma150_poll_close(struct input_polled_dev *ipoll_dev)
409{
410	struct bma150_data *bma150 = ipoll_dev->private;
411
412	bma150_close(bma150);
413}
414
415static int bma150_initialize(struct bma150_data *bma150,
416				       const struct bma150_cfg *cfg)
417{
418	int error;
419
420	error = bma150_soft_reset(bma150);
421	if (error)
422		return error;
423
424	error = bma150_set_bandwidth(bma150, cfg->bandwidth);
425	if (error)
426		return error;
427
428	error = bma150_set_range(bma150, cfg->range);
429	if (error)
430		return error;
431
432	if (bma150->client->irq) {
433		error = bma150_set_any_motion_interrupt(bma150,
434					cfg->any_motion_int,
435					cfg->any_motion_dur,
436					cfg->any_motion_thres);
437		if (error)
438			return error;
439
440		error = bma150_set_high_g_interrupt(bma150,
441					cfg->hg_int, cfg->hg_hyst,
442					cfg->hg_dur, cfg->hg_thres);
443		if (error)
444			return error;
445
446		error = bma150_set_low_g_interrupt(bma150,
447					cfg->lg_int, cfg->lg_hyst,
448					cfg->lg_dur, cfg->lg_thres);
449		if (error)
450			return error;
451	}
452
453	return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
454}
455
456static void bma150_init_input_device(struct bma150_data *bma150,
457						struct input_dev *idev)
458{
459	idev->name = BMA150_DRIVER;
460	idev->phys = BMA150_DRIVER "/input0";
461	idev->id.bustype = BUS_I2C;
462	idev->dev.parent = &bma150->client->dev;
463
464	idev->evbit[0] = BIT_MASK(EV_ABS);
465	input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
466	input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
467	input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
468}
469
470static int bma150_register_input_device(struct bma150_data *bma150)
471{
472	struct input_dev *idev;
473	int error;
474
475	idev = input_allocate_device();
476	if (!idev)
477		return -ENOMEM;
478
479	bma150_init_input_device(bma150, idev);
480
481	idev->open = bma150_irq_open;
482	idev->close = bma150_irq_close;
483	input_set_drvdata(idev, bma150);
484
 
 
485	error = input_register_device(idev);
486	if (error) {
487		input_free_device(idev);
488		return error;
489	}
490
491	bma150->input = idev;
492	return 0;
493}
494
495static int bma150_register_polled_device(struct bma150_data *bma150)
496{
497	struct input_polled_dev *ipoll_dev;
498	int error;
499
500	ipoll_dev = input_allocate_polled_device();
501	if (!ipoll_dev)
502		return -ENOMEM;
503
504	ipoll_dev->private = bma150;
505	ipoll_dev->open = bma150_poll_open;
506	ipoll_dev->close = bma150_poll_close;
507	ipoll_dev->poll = bma150_poll;
508	ipoll_dev->poll_interval = BMA150_POLL_INTERVAL;
509	ipoll_dev->poll_interval_min = BMA150_POLL_MIN;
510	ipoll_dev->poll_interval_max = BMA150_POLL_MAX;
511
512	bma150_init_input_device(bma150, ipoll_dev->input);
513
 
 
 
514	error = input_register_polled_device(ipoll_dev);
515	if (error) {
516		input_free_polled_device(ipoll_dev);
517		return error;
518	}
519
520	bma150->input_polled = ipoll_dev;
521	bma150->input = ipoll_dev->input;
522
523	return 0;
524}
525
526static int bma150_probe(struct i2c_client *client,
527				  const struct i2c_device_id *id)
528{
529	const struct bma150_platform_data *pdata =
530			dev_get_platdata(&client->dev);
531	const struct bma150_cfg *cfg;
532	struct bma150_data *bma150;
533	int chip_id;
534	int error;
535
536	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
537		dev_err(&client->dev, "i2c_check_functionality error\n");
538		return -EIO;
539	}
540
541	chip_id = i2c_smbus_read_byte_data(client, BMA150_CHIP_ID_REG);
542	if (chip_id != BMA150_CHIP_ID && chip_id != BMA180_CHIP_ID) {
543		dev_err(&client->dev, "BMA150 chip id error: %d\n", chip_id);
544		return -EINVAL;
545	}
546
547	bma150 = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
548	if (!bma150)
549		return -ENOMEM;
550
551	bma150->client = client;
552
553	if (pdata) {
554		if (pdata->irq_gpio_cfg) {
555			error = pdata->irq_gpio_cfg();
556			if (error) {
557				dev_err(&client->dev,
558					"IRQ GPIO conf. error %d, error %d\n",
559					client->irq, error);
560				goto err_free_mem;
561			}
562		}
563		cfg = &pdata->cfg;
564	} else {
565		cfg = &default_cfg;
566	}
567
568	error = bma150_initialize(bma150, cfg);
569	if (error)
570		goto err_free_mem;
571
572	if (client->irq > 0) {
573		error = bma150_register_input_device(bma150);
574		if (error)
575			goto err_free_mem;
576
577		error = request_threaded_irq(client->irq,
578					NULL, bma150_irq_thread,
579					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
580					BMA150_DRIVER, bma150);
581		if (error) {
582			dev_err(&client->dev,
583				"irq request failed %d, error %d\n",
584				client->irq, error);
585			input_unregister_device(bma150->input);
586			goto err_free_mem;
587		}
588	} else {
589		error = bma150_register_polled_device(bma150);
590		if (error)
591			goto err_free_mem;
592	}
593
594	i2c_set_clientdata(client, bma150);
595
596	pm_runtime_enable(&client->dev);
597
598	return 0;
599
600err_free_mem:
601	kfree(bma150);
602	return error;
603}
604
605static int bma150_remove(struct i2c_client *client)
606{
607	struct bma150_data *bma150 = i2c_get_clientdata(client);
608
609	pm_runtime_disable(&client->dev);
610
611	if (client->irq > 0) {
612		free_irq(client->irq, bma150);
613		input_unregister_device(bma150->input);
614	} else {
615		input_unregister_polled_device(bma150->input_polled);
616		input_free_polled_device(bma150->input_polled);
617	}
618
619	kfree(bma150);
620
621	return 0;
622}
623
624#ifdef CONFIG_PM
625static int bma150_suspend(struct device *dev)
626{
627	struct i2c_client *client = to_i2c_client(dev);
628	struct bma150_data *bma150 = i2c_get_clientdata(client);
629
630	return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
631}
632
633static int bma150_resume(struct device *dev)
634{
635	struct i2c_client *client = to_i2c_client(dev);
636	struct bma150_data *bma150 = i2c_get_clientdata(client);
637
638	return bma150_set_mode(bma150, BMA150_MODE_NORMAL);
639}
640#endif
641
642static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL);
643
644static const struct i2c_device_id bma150_id[] = {
645	{ "bma150", 0 },
646	{ "bma180", 0 },
647	{ "smb380", 0 },
648	{ "bma023", 0 },
649	{ }
650};
651
652MODULE_DEVICE_TABLE(i2c, bma150_id);
653
654static struct i2c_driver bma150_driver = {
655	.driver = {
656		.name	= BMA150_DRIVER,
657		.pm	= &bma150_pm,
658	},
659	.class		= I2C_CLASS_HWMON,
660	.id_table	= bma150_id,
661	.probe		= bma150_probe,
662	.remove		= bma150_remove,
663};
664
665module_i2c_driver(bma150_driver);
666
667MODULE_AUTHOR("Albert Zhang <xu.zhang@bosch-sensortec.com>");
668MODULE_DESCRIPTION("BMA150 driver");
669MODULE_LICENSE("GPL");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (c) 2011 Bosch Sensortec GmbH
  4 * Copyright (c) 2011 Unixphere
  5 *
  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.
  9 *
 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 12 */
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/i2c.h>
 16#include <linux/input.h>
 17#include <linux/input-polldev.h>
 18#include <linux/interrupt.h>
 19#include <linux/delay.h>
 20#include <linux/slab.h>
 21#include <linux/pm.h>
 22#include <linux/pm_runtime.h>
 23#include <linux/bma150.h>
 24
 25#define ABSMAX_ACC_VAL		0x01FF
 26#define ABSMIN_ACC_VAL		-(ABSMAX_ACC_VAL)
 27
 28/* Each axis is represented by a 2-byte data word */
 29#define BMA150_XYZ_DATA_SIZE	6
 30
 31/* Input poll interval in milliseconds */
 32#define BMA150_POLL_INTERVAL	10
 33#define BMA150_POLL_MAX		200
 34#define BMA150_POLL_MIN		0
 35
 36#define BMA150_MODE_NORMAL	0
 37#define BMA150_MODE_SLEEP	2
 38#define BMA150_MODE_WAKE_UP	3
 39
 40/* Data register addresses */
 41#define BMA150_DATA_0_REG	0x00
 42#define BMA150_DATA_1_REG	0x01
 43#define BMA150_DATA_2_REG	0x02
 44
 45/* Control register addresses */
 46#define BMA150_CTRL_0_REG	0x0A
 47#define BMA150_CTRL_1_REG	0x0B
 48#define BMA150_CTRL_2_REG	0x14
 49#define BMA150_CTRL_3_REG	0x15
 50
 51/* Configuration/Setting register addresses */
 52#define BMA150_CFG_0_REG	0x0C
 53#define BMA150_CFG_1_REG	0x0D
 54#define BMA150_CFG_2_REG	0x0E
 55#define BMA150_CFG_3_REG	0x0F
 56#define BMA150_CFG_4_REG	0x10
 57#define BMA150_CFG_5_REG	0x11
 58
 59#define BMA150_CHIP_ID		2
 
 60#define BMA150_CHIP_ID_REG	BMA150_DATA_0_REG
 61
 62#define BMA150_ACC_X_LSB_REG	BMA150_DATA_2_REG
 63
 64#define BMA150_SLEEP_POS	0
 65#define BMA150_SLEEP_MSK	0x01
 66#define BMA150_SLEEP_REG	BMA150_CTRL_0_REG
 67
 68#define BMA150_BANDWIDTH_POS	0
 69#define BMA150_BANDWIDTH_MSK	0x07
 70#define BMA150_BANDWIDTH_REG	BMA150_CTRL_2_REG
 71
 72#define BMA150_RANGE_POS	3
 73#define BMA150_RANGE_MSK	0x18
 74#define BMA150_RANGE_REG	BMA150_CTRL_2_REG
 75
 76#define BMA150_WAKE_UP_POS	0
 77#define BMA150_WAKE_UP_MSK	0x01
 78#define BMA150_WAKE_UP_REG	BMA150_CTRL_3_REG
 79
 80#define BMA150_SW_RES_POS	1
 81#define BMA150_SW_RES_MSK	0x02
 82#define BMA150_SW_RES_REG	BMA150_CTRL_0_REG
 83
 84/* Any-motion interrupt register fields */
 85#define BMA150_ANY_MOTION_EN_POS	6
 86#define BMA150_ANY_MOTION_EN_MSK	0x40
 87#define BMA150_ANY_MOTION_EN_REG	BMA150_CTRL_1_REG
 88
 89#define BMA150_ANY_MOTION_DUR_POS	6
 90#define BMA150_ANY_MOTION_DUR_MSK	0xC0
 91#define BMA150_ANY_MOTION_DUR_REG	BMA150_CFG_5_REG
 92
 93#define BMA150_ANY_MOTION_THRES_REG	BMA150_CFG_4_REG
 94
 95/* Advanced interrupt register fields */
 96#define BMA150_ADV_INT_EN_POS		6
 97#define BMA150_ADV_INT_EN_MSK		0x40
 98#define BMA150_ADV_INT_EN_REG		BMA150_CTRL_3_REG
 99
100/* High-G interrupt register fields */
101#define BMA150_HIGH_G_EN_POS		1
102#define BMA150_HIGH_G_EN_MSK		0x02
103#define BMA150_HIGH_G_EN_REG		BMA150_CTRL_1_REG
104
105#define BMA150_HIGH_G_HYST_POS		3
106#define BMA150_HIGH_G_HYST_MSK		0x38
107#define BMA150_HIGH_G_HYST_REG		BMA150_CFG_5_REG
108
109#define BMA150_HIGH_G_DUR_REG		BMA150_CFG_3_REG
110#define BMA150_HIGH_G_THRES_REG		BMA150_CFG_2_REG
111
112/* Low-G interrupt register fields */
113#define BMA150_LOW_G_EN_POS		0
114#define BMA150_LOW_G_EN_MSK		0x01
115#define BMA150_LOW_G_EN_REG		BMA150_CTRL_1_REG
116
117#define BMA150_LOW_G_HYST_POS		0
118#define BMA150_LOW_G_HYST_MSK		0x07
119#define BMA150_LOW_G_HYST_REG		BMA150_CFG_5_REG
120
121#define BMA150_LOW_G_DUR_REG		BMA150_CFG_1_REG
122#define BMA150_LOW_G_THRES_REG		BMA150_CFG_0_REG
123
124struct bma150_data {
125	struct i2c_client *client;
126	struct input_polled_dev *input_polled;
127	struct input_dev *input;
128	u8 mode;
129};
130
131/*
132 * The settings for the given range, bandwidth and interrupt features
133 * are stated and verified by Bosch Sensortec where they are configured
134 * to provide a generic sensitivity performance.
135 */
136static const struct bma150_cfg default_cfg = {
137	.any_motion_int = 1,
138	.hg_int = 1,
139	.lg_int = 1,
140	.any_motion_dur = 0,
141	.any_motion_thres = 0,
142	.hg_hyst = 0,
143	.hg_dur = 150,
144	.hg_thres = 160,
145	.lg_hyst = 0,
146	.lg_dur = 150,
147	.lg_thres = 20,
148	.range = BMA150_RANGE_2G,
149	.bandwidth = BMA150_BW_50HZ
150};
151
152static int bma150_write_byte(struct i2c_client *client, u8 reg, u8 val)
153{
154	s32 ret;
155
156	/* As per specification, disable irq in between register writes */
157	if (client->irq)
158		disable_irq_nosync(client->irq);
159
160	ret = i2c_smbus_write_byte_data(client, reg, val);
161
162	if (client->irq)
163		enable_irq(client->irq);
164
165	return ret;
166}
167
168static int bma150_set_reg_bits(struct i2c_client *client,
169					int val, int shift, u8 mask, u8 reg)
170{
171	int data;
172
173	data = i2c_smbus_read_byte_data(client, reg);
174	if (data < 0)
175		return data;
176
177	data = (data & ~mask) | ((val << shift) & mask);
178	return bma150_write_byte(client, reg, data);
179}
180
181static int bma150_set_mode(struct bma150_data *bma150, u8 mode)
182{
183	int error;
184
185	error = bma150_set_reg_bits(bma150->client, mode, BMA150_WAKE_UP_POS,
186				BMA150_WAKE_UP_MSK, BMA150_WAKE_UP_REG);
187	if (error)
188		return error;
189
190	error = bma150_set_reg_bits(bma150->client, mode, BMA150_SLEEP_POS,
191				BMA150_SLEEP_MSK, BMA150_SLEEP_REG);
192	if (error)
193		return error;
194
195	if (mode == BMA150_MODE_NORMAL)
196		usleep_range(2000, 2100);
197
198	bma150->mode = mode;
199	return 0;
200}
201
202static int bma150_soft_reset(struct bma150_data *bma150)
203{
204	int error;
205
206	error = bma150_set_reg_bits(bma150->client, 1, BMA150_SW_RES_POS,
207				BMA150_SW_RES_MSK, BMA150_SW_RES_REG);
208	if (error)
209		return error;
210
211	usleep_range(2000, 2100);
212	return 0;
213}
214
215static int bma150_set_range(struct bma150_data *bma150, u8 range)
216{
217	return bma150_set_reg_bits(bma150->client, range, BMA150_RANGE_POS,
218				BMA150_RANGE_MSK, BMA150_RANGE_REG);
219}
220
221static int bma150_set_bandwidth(struct bma150_data *bma150, u8 bw)
222{
223	return bma150_set_reg_bits(bma150->client, bw, BMA150_BANDWIDTH_POS,
224				BMA150_BANDWIDTH_MSK, BMA150_BANDWIDTH_REG);
225}
226
227static int bma150_set_low_g_interrupt(struct bma150_data *bma150,
228					u8 enable, u8 hyst, u8 dur, u8 thres)
229{
230	int error;
231
232	error = bma150_set_reg_bits(bma150->client, hyst,
233				BMA150_LOW_G_HYST_POS, BMA150_LOW_G_HYST_MSK,
234				BMA150_LOW_G_HYST_REG);
235	if (error)
236		return error;
237
238	error = bma150_write_byte(bma150->client, BMA150_LOW_G_DUR_REG, dur);
239	if (error)
240		return error;
241
242	error = bma150_write_byte(bma150->client, BMA150_LOW_G_THRES_REG, thres);
243	if (error)
244		return error;
245
246	return bma150_set_reg_bits(bma150->client, !!enable,
247				BMA150_LOW_G_EN_POS, BMA150_LOW_G_EN_MSK,
248				BMA150_LOW_G_EN_REG);
249}
250
251static int bma150_set_high_g_interrupt(struct bma150_data *bma150,
252					u8 enable, u8 hyst, u8 dur, u8 thres)
253{
254	int error;
255
256	error = bma150_set_reg_bits(bma150->client, hyst,
257				BMA150_HIGH_G_HYST_POS, BMA150_HIGH_G_HYST_MSK,
258				BMA150_HIGH_G_HYST_REG);
259	if (error)
260		return error;
261
262	error = bma150_write_byte(bma150->client,
263				BMA150_HIGH_G_DUR_REG, dur);
264	if (error)
265		return error;
266
267	error = bma150_write_byte(bma150->client,
268				BMA150_HIGH_G_THRES_REG, thres);
269	if (error)
270		return error;
271
272	return bma150_set_reg_bits(bma150->client, !!enable,
273				BMA150_HIGH_G_EN_POS, BMA150_HIGH_G_EN_MSK,
274				BMA150_HIGH_G_EN_REG);
275}
276
277
278static int bma150_set_any_motion_interrupt(struct bma150_data *bma150,
279						u8 enable, u8 dur, u8 thres)
280{
281	int error;
282
283	error = bma150_set_reg_bits(bma150->client, dur,
284				BMA150_ANY_MOTION_DUR_POS,
285				BMA150_ANY_MOTION_DUR_MSK,
286				BMA150_ANY_MOTION_DUR_REG);
287	if (error)
288		return error;
289
290	error = bma150_write_byte(bma150->client,
291				BMA150_ANY_MOTION_THRES_REG, thres);
292	if (error)
293		return error;
294
295	error = bma150_set_reg_bits(bma150->client, !!enable,
296				BMA150_ADV_INT_EN_POS, BMA150_ADV_INT_EN_MSK,
297				BMA150_ADV_INT_EN_REG);
298	if (error)
299		return error;
300
301	return bma150_set_reg_bits(bma150->client, !!enable,
302				BMA150_ANY_MOTION_EN_POS,
303				BMA150_ANY_MOTION_EN_MSK,
304				BMA150_ANY_MOTION_EN_REG);
305}
306
307static void bma150_report_xyz(struct bma150_data *bma150)
308{
309	u8 data[BMA150_XYZ_DATA_SIZE];
310	s16 x, y, z;
311	s32 ret;
312
313	ret = i2c_smbus_read_i2c_block_data(bma150->client,
314			BMA150_ACC_X_LSB_REG, BMA150_XYZ_DATA_SIZE, data);
315	if (ret != BMA150_XYZ_DATA_SIZE)
316		return;
317
318	x = ((0xc0 & data[0]) >> 6) | (data[1] << 2);
319	y = ((0xc0 & data[2]) >> 6) | (data[3] << 2);
320	z = ((0xc0 & data[4]) >> 6) | (data[5] << 2);
321
322	x = sign_extend32(x, 9);
323	y = sign_extend32(y, 9);
324	z = sign_extend32(z, 9);
325
326	input_report_abs(bma150->input, ABS_X, x);
327	input_report_abs(bma150->input, ABS_Y, y);
328	input_report_abs(bma150->input, ABS_Z, z);
329	input_sync(bma150->input);
330}
331
332static irqreturn_t bma150_irq_thread(int irq, void *dev)
333{
334	bma150_report_xyz(dev);
335
336	return IRQ_HANDLED;
337}
338
339static void bma150_poll(struct input_polled_dev *dev)
340{
341	bma150_report_xyz(dev->private);
342}
343
344static int bma150_open(struct bma150_data *bma150)
345{
346	int error;
347
348	error = pm_runtime_get_sync(&bma150->client->dev);
349	if (error < 0 && error != -ENOSYS)
350		return error;
351
352	/*
353	 * See if runtime PM woke up the device. If runtime PM
354	 * is disabled we need to do it ourselves.
355	 */
356	if (bma150->mode != BMA150_MODE_NORMAL) {
357		error = bma150_set_mode(bma150, BMA150_MODE_NORMAL);
358		if (error)
359			return error;
360	}
361
362	return 0;
363}
364
365static void bma150_close(struct bma150_data *bma150)
366{
367	pm_runtime_put_sync(&bma150->client->dev);
368
369	if (bma150->mode != BMA150_MODE_SLEEP)
370		bma150_set_mode(bma150, BMA150_MODE_SLEEP);
371}
372
373static int bma150_irq_open(struct input_dev *input)
374{
375	struct bma150_data *bma150 = input_get_drvdata(input);
376
377	return bma150_open(bma150);
378}
379
380static void bma150_irq_close(struct input_dev *input)
381{
382	struct bma150_data *bma150 = input_get_drvdata(input);
383
384	bma150_close(bma150);
385}
386
387static void bma150_poll_open(struct input_polled_dev *ipoll_dev)
388{
389	struct bma150_data *bma150 = ipoll_dev->private;
390
391	bma150_open(bma150);
392}
393
394static void bma150_poll_close(struct input_polled_dev *ipoll_dev)
395{
396	struct bma150_data *bma150 = ipoll_dev->private;
397
398	bma150_close(bma150);
399}
400
401static int bma150_initialize(struct bma150_data *bma150,
402				       const struct bma150_cfg *cfg)
403{
404	int error;
405
406	error = bma150_soft_reset(bma150);
407	if (error)
408		return error;
409
410	error = bma150_set_bandwidth(bma150, cfg->bandwidth);
411	if (error)
412		return error;
413
414	error = bma150_set_range(bma150, cfg->range);
415	if (error)
416		return error;
417
418	if (bma150->client->irq) {
419		error = bma150_set_any_motion_interrupt(bma150,
420					cfg->any_motion_int,
421					cfg->any_motion_dur,
422					cfg->any_motion_thres);
423		if (error)
424			return error;
425
426		error = bma150_set_high_g_interrupt(bma150,
427					cfg->hg_int, cfg->hg_hyst,
428					cfg->hg_dur, cfg->hg_thres);
429		if (error)
430			return error;
431
432		error = bma150_set_low_g_interrupt(bma150,
433					cfg->lg_int, cfg->lg_hyst,
434					cfg->lg_dur, cfg->lg_thres);
435		if (error)
436			return error;
437	}
438
439	return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
440}
441
442static void bma150_init_input_device(struct bma150_data *bma150,
443						struct input_dev *idev)
444{
445	idev->name = BMA150_DRIVER;
446	idev->phys = BMA150_DRIVER "/input0";
447	idev->id.bustype = BUS_I2C;
448	idev->dev.parent = &bma150->client->dev;
449
450	idev->evbit[0] = BIT_MASK(EV_ABS);
451	input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
452	input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
453	input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0);
454}
455
456static int bma150_register_input_device(struct bma150_data *bma150)
457{
458	struct input_dev *idev;
459	int error;
460
461	idev = input_allocate_device();
462	if (!idev)
463		return -ENOMEM;
464
465	bma150_init_input_device(bma150, idev);
466
467	idev->open = bma150_irq_open;
468	idev->close = bma150_irq_close;
469	input_set_drvdata(idev, bma150);
470
471	bma150->input = idev;
472
473	error = input_register_device(idev);
474	if (error) {
475		input_free_device(idev);
476		return error;
477	}
478
 
479	return 0;
480}
481
482static int bma150_register_polled_device(struct bma150_data *bma150)
483{
484	struct input_polled_dev *ipoll_dev;
485	int error;
486
487	ipoll_dev = input_allocate_polled_device();
488	if (!ipoll_dev)
489		return -ENOMEM;
490
491	ipoll_dev->private = bma150;
492	ipoll_dev->open = bma150_poll_open;
493	ipoll_dev->close = bma150_poll_close;
494	ipoll_dev->poll = bma150_poll;
495	ipoll_dev->poll_interval = BMA150_POLL_INTERVAL;
496	ipoll_dev->poll_interval_min = BMA150_POLL_MIN;
497	ipoll_dev->poll_interval_max = BMA150_POLL_MAX;
498
499	bma150_init_input_device(bma150, ipoll_dev->input);
500
501	bma150->input_polled = ipoll_dev;
502	bma150->input = ipoll_dev->input;
503
504	error = input_register_polled_device(ipoll_dev);
505	if (error) {
506		input_free_polled_device(ipoll_dev);
507		return error;
508	}
509
 
 
 
510	return 0;
511}
512
513static int bma150_probe(struct i2c_client *client,
514				  const struct i2c_device_id *id)
515{
516	const struct bma150_platform_data *pdata =
517			dev_get_platdata(&client->dev);
518	const struct bma150_cfg *cfg;
519	struct bma150_data *bma150;
520	int chip_id;
521	int error;
522
523	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
524		dev_err(&client->dev, "i2c_check_functionality error\n");
525		return -EIO;
526	}
527
528	chip_id = i2c_smbus_read_byte_data(client, BMA150_CHIP_ID_REG);
529	if (chip_id != BMA150_CHIP_ID) {
530		dev_err(&client->dev, "BMA150 chip id error: %d\n", chip_id);
531		return -EINVAL;
532	}
533
534	bma150 = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
535	if (!bma150)
536		return -ENOMEM;
537
538	bma150->client = client;
539
540	if (pdata) {
541		if (pdata->irq_gpio_cfg) {
542			error = pdata->irq_gpio_cfg();
543			if (error) {
544				dev_err(&client->dev,
545					"IRQ GPIO conf. error %d, error %d\n",
546					client->irq, error);
547				goto err_free_mem;
548			}
549		}
550		cfg = &pdata->cfg;
551	} else {
552		cfg = &default_cfg;
553	}
554
555	error = bma150_initialize(bma150, cfg);
556	if (error)
557		goto err_free_mem;
558
559	if (client->irq > 0) {
560		error = bma150_register_input_device(bma150);
561		if (error)
562			goto err_free_mem;
563
564		error = request_threaded_irq(client->irq,
565					NULL, bma150_irq_thread,
566					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
567					BMA150_DRIVER, bma150);
568		if (error) {
569			dev_err(&client->dev,
570				"irq request failed %d, error %d\n",
571				client->irq, error);
572			input_unregister_device(bma150->input);
573			goto err_free_mem;
574		}
575	} else {
576		error = bma150_register_polled_device(bma150);
577		if (error)
578			goto err_free_mem;
579	}
580
581	i2c_set_clientdata(client, bma150);
582
583	pm_runtime_enable(&client->dev);
584
585	return 0;
586
587err_free_mem:
588	kfree(bma150);
589	return error;
590}
591
592static int bma150_remove(struct i2c_client *client)
593{
594	struct bma150_data *bma150 = i2c_get_clientdata(client);
595
596	pm_runtime_disable(&client->dev);
597
598	if (client->irq > 0) {
599		free_irq(client->irq, bma150);
600		input_unregister_device(bma150->input);
601	} else {
602		input_unregister_polled_device(bma150->input_polled);
603		input_free_polled_device(bma150->input_polled);
604	}
605
606	kfree(bma150);
607
608	return 0;
609}
610
611#ifdef CONFIG_PM
612static int bma150_suspend(struct device *dev)
613{
614	struct i2c_client *client = to_i2c_client(dev);
615	struct bma150_data *bma150 = i2c_get_clientdata(client);
616
617	return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
618}
619
620static int bma150_resume(struct device *dev)
621{
622	struct i2c_client *client = to_i2c_client(dev);
623	struct bma150_data *bma150 = i2c_get_clientdata(client);
624
625	return bma150_set_mode(bma150, BMA150_MODE_NORMAL);
626}
627#endif
628
629static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL);
630
631static const struct i2c_device_id bma150_id[] = {
632	{ "bma150", 0 },
 
633	{ "smb380", 0 },
634	{ "bma023", 0 },
635	{ }
636};
637
638MODULE_DEVICE_TABLE(i2c, bma150_id);
639
640static struct i2c_driver bma150_driver = {
641	.driver = {
642		.name	= BMA150_DRIVER,
643		.pm	= &bma150_pm,
644	},
645	.class		= I2C_CLASS_HWMON,
646	.id_table	= bma150_id,
647	.probe		= bma150_probe,
648	.remove		= bma150_remove,
649};
650
651module_i2c_driver(bma150_driver);
652
653MODULE_AUTHOR("Albert Zhang <xu.zhang@bosch-sensortec.com>");
654MODULE_DESCRIPTION("BMA150 driver");
655MODULE_LICENSE("GPL");