Linux Audio

Check our new training course

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