xref: /openbmc/linux/drivers/iio/accel/bma180.c (revision 1240c94c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor
4  *
5  * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
6  *
7  * Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net>
8  *
9  * SPI is not supported by driver
10  * BMA023/BMA150/SMB380: 7-bit I2C slave address 0x38
11  * BMA180: 7-bit I2C slave address 0x40 or 0x41
12  * BMA250: 7-bit I2C slave address 0x18 or 0x19
13  */
14 
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/of.h>
20 #include <linux/bitops.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 
31 #define BMA180_DRV_NAME "bma180"
32 #define BMA180_IRQ_NAME "bma180_event"
33 
34 enum chip_ids {
35 	BMA023,
36 	BMA150,
37 	BMA180,
38 	BMA250,
39 };
40 
41 struct bma180_data;
42 
43 struct bma180_part_info {
44 	u8 chip_id;
45 	const struct iio_chan_spec *channels;
46 	unsigned int num_channels;
47 	const int *scale_table;
48 	unsigned int num_scales;
49 	const int *bw_table;
50 	unsigned int num_bw;
51 	int temp_offset;
52 
53 	u8 int_reset_reg, int_reset_mask;
54 	u8 sleep_reg, sleep_mask;
55 	u8 bw_reg, bw_mask, bw_offset;
56 	u8 scale_reg, scale_mask;
57 	u8 power_reg, power_mask, lowpower_val;
58 	u8 int_enable_reg, int_enable_mask;
59 	u8 softreset_reg, softreset_val;
60 
61 	int (*chip_config)(struct bma180_data *data);
62 	void (*chip_disable)(struct bma180_data *data);
63 };
64 
65 /* Register set */
66 #define BMA023_CTRL_REG0	0x0a
67 #define BMA023_CTRL_REG1	0x0b
68 #define BMA023_CTRL_REG2	0x14
69 #define BMA023_CTRL_REG3	0x15
70 
71 #define BMA023_RANGE_MASK	GENMASK(4, 3) /* Range of accel values */
72 #define BMA023_BW_MASK		GENMASK(2, 0) /* Accel bandwidth */
73 #define BMA023_SLEEP		BIT(0)
74 #define BMA023_INT_RESET_MASK	BIT(6)
75 #define BMA023_NEW_DATA_INT	BIT(5) /* Intr every new accel data is ready */
76 #define BMA023_RESET_VAL	BIT(1)
77 
78 #define BMA180_CHIP_ID		0x00 /* Need to distinguish BMA180 from other */
79 #define BMA180_ACC_X_LSB	0x02 /* First of 6 registers of accel data */
80 #define BMA180_TEMP		0x08
81 #define BMA180_CTRL_REG0	0x0d
82 #define BMA180_RESET		0x10
83 #define BMA180_BW_TCS		0x20
84 #define BMA180_CTRL_REG3	0x21
85 #define BMA180_TCO_Z		0x30
86 #define BMA180_OFFSET_LSB1	0x35
87 
88 /* BMA180_CTRL_REG0 bits */
89 #define BMA180_DIS_WAKE_UP	BIT(0) /* Disable wake up mode */
90 #define BMA180_SLEEP		BIT(1) /* 1 - chip will sleep */
91 #define BMA180_EE_W		BIT(4) /* Unlock writing to addr from 0x20 */
92 #define BMA180_RESET_INT	BIT(6) /* Reset pending interrupts */
93 
94 /* BMA180_CTRL_REG3 bits */
95 #define BMA180_NEW_DATA_INT	BIT(1) /* Intr every new accel data is ready */
96 
97 /* BMA180_OFFSET_LSB1 skipping mode bit */
98 #define BMA180_SMP_SKIP		BIT(0)
99 
100 /* Bit masks for registers bit fields */
101 #define BMA180_RANGE		0x0e /* Range of measured accel values */
102 #define BMA180_BW		0xf0 /* Accel bandwidth */
103 #define BMA180_MODE_CONFIG	0x03 /* Config operation modes */
104 
105 /* We have to write this value in reset register to do soft reset */
106 #define BMA180_RESET_VAL	0xb6
107 
108 #define BMA023_ID_REG_VAL	0x02
109 #define BMA180_ID_REG_VAL	0x03
110 #define BMA250_ID_REG_VAL	0x03
111 
112 /* Chip power modes */
113 #define BMA180_LOW_POWER	0x03
114 
115 #define BMA250_RANGE_REG	0x0f
116 #define BMA250_BW_REG		0x10
117 #define BMA250_POWER_REG	0x11
118 #define BMA250_RESET_REG	0x14
119 #define BMA250_INT_ENABLE_REG	0x17
120 #define BMA250_INT_MAP_REG	0x1a
121 #define BMA250_INT_RESET_REG	0x21
122 
123 #define BMA250_RANGE_MASK	GENMASK(3, 0) /* Range of accel values */
124 #define BMA250_BW_MASK		GENMASK(4, 0) /* Accel bandwidth */
125 #define BMA250_BW_OFFSET	8
126 #define BMA250_SUSPEND_MASK	BIT(7) /* chip will sleep */
127 #define BMA250_LOWPOWER_MASK	BIT(6)
128 #define BMA250_DATA_INTEN_MASK	BIT(4)
129 #define BMA250_INT1_DATA_MASK	BIT(0)
130 #define BMA250_INT_RESET_MASK	BIT(7) /* Reset pending interrupts */
131 
132 struct bma180_data {
133 	struct regulator *vdd_supply;
134 	struct regulator *vddio_supply;
135 	struct i2c_client *client;
136 	struct iio_trigger *trig;
137 	const struct bma180_part_info *part_info;
138 	struct iio_mount_matrix orientation;
139 	struct mutex mutex;
140 	bool sleep_state;
141 	int scale;
142 	int bw;
143 	bool pmode;
144 	/* Ensure timestamp is naturally aligned */
145 	struct {
146 		s16 chan[4];
147 		s64 timestamp __aligned(8);
148 	} scan;
149 };
150 
151 enum bma180_chan {
152 	AXIS_X,
153 	AXIS_Y,
154 	AXIS_Z,
155 	TEMP
156 };
157 
158 static int bma023_bw_table[] = { 25, 50, 100, 190, 375, 750, 1500 }; /* Hz */
159 static int bma023_scale_table[] = { 2452, 4903, 9709, };
160 
161 static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
162 static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 };
163 
164 static int bma250_bw_table[] = { 8, 16, 31, 63, 125, 250, 500, 1000 }; /* Hz */
165 static int bma250_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0,
166 	0, 0, 306458 };
167 
bma180_get_data_reg(struct bma180_data * data,enum bma180_chan chan)168 static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan)
169 {
170 	int ret;
171 
172 	if (data->sleep_state)
173 		return -EBUSY;
174 
175 	switch (chan) {
176 	case TEMP:
177 		ret = i2c_smbus_read_byte_data(data->client, BMA180_TEMP);
178 		if (ret < 0)
179 			dev_err(&data->client->dev, "failed to read temp register\n");
180 		break;
181 	default:
182 		ret = i2c_smbus_read_word_data(data->client,
183 			BMA180_ACC_X_LSB + chan * 2);
184 		if (ret < 0)
185 			dev_err(&data->client->dev,
186 				"failed to read accel_%c register\n",
187 				'x' + chan);
188 	}
189 
190 	return ret;
191 }
192 
bma180_set_bits(struct bma180_data * data,u8 reg,u8 mask,u8 val)193 static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
194 {
195 	int ret = i2c_smbus_read_byte_data(data->client, reg);
196 	u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1));
197 
198 	if (ret < 0)
199 		return ret;
200 
201 	return i2c_smbus_write_byte_data(data->client, reg, reg_val);
202 }
203 
bma180_reset_intr(struct bma180_data * data)204 static int bma180_reset_intr(struct bma180_data *data)
205 {
206 	int ret = bma180_set_bits(data, data->part_info->int_reset_reg,
207 		data->part_info->int_reset_mask, 1);
208 
209 	if (ret)
210 		dev_err(&data->client->dev, "failed to reset interrupt\n");
211 
212 	return ret;
213 }
214 
bma180_set_new_data_intr_state(struct bma180_data * data,bool state)215 static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state)
216 {
217 	int ret = bma180_set_bits(data, data->part_info->int_enable_reg,
218 			data->part_info->int_enable_mask, state);
219 	if (ret)
220 		goto err;
221 	ret = bma180_reset_intr(data);
222 	if (ret)
223 		goto err;
224 
225 	return 0;
226 
227 err:
228 	dev_err(&data->client->dev,
229 		"failed to set new data interrupt state %d\n", state);
230 	return ret;
231 }
232 
bma180_set_sleep_state(struct bma180_data * data,bool state)233 static int bma180_set_sleep_state(struct bma180_data *data, bool state)
234 {
235 	int ret = bma180_set_bits(data, data->part_info->sleep_reg,
236 		data->part_info->sleep_mask, state);
237 
238 	if (ret) {
239 		dev_err(&data->client->dev,
240 			"failed to set sleep state %d\n", state);
241 		return ret;
242 	}
243 	data->sleep_state = state;
244 
245 	return 0;
246 }
247 
bma180_set_ee_writing_state(struct bma180_data * data,bool state)248 static int bma180_set_ee_writing_state(struct bma180_data *data, bool state)
249 {
250 	int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state);
251 
252 	if (ret)
253 		dev_err(&data->client->dev,
254 			"failed to set ee writing state %d\n", state);
255 
256 	return ret;
257 }
258 
bma180_set_bw(struct bma180_data * data,int val)259 static int bma180_set_bw(struct bma180_data *data, int val)
260 {
261 	int ret, i;
262 
263 	if (data->sleep_state)
264 		return -EBUSY;
265 
266 	for (i = 0; i < data->part_info->num_bw; ++i) {
267 		if (data->part_info->bw_table[i] == val) {
268 			ret = bma180_set_bits(data, data->part_info->bw_reg,
269 				data->part_info->bw_mask,
270 				i + data->part_info->bw_offset);
271 			if (ret) {
272 				dev_err(&data->client->dev,
273 					"failed to set bandwidth\n");
274 				return ret;
275 			}
276 			data->bw = val;
277 			return 0;
278 		}
279 	}
280 
281 	return -EINVAL;
282 }
283 
bma180_set_scale(struct bma180_data * data,int val)284 static int bma180_set_scale(struct bma180_data *data, int val)
285 {
286 	int ret, i;
287 
288 	if (data->sleep_state)
289 		return -EBUSY;
290 
291 	for (i = 0; i < data->part_info->num_scales; ++i)
292 		if (data->part_info->scale_table[i] == val) {
293 			ret = bma180_set_bits(data, data->part_info->scale_reg,
294 				data->part_info->scale_mask, i);
295 			if (ret) {
296 				dev_err(&data->client->dev,
297 					"failed to set scale\n");
298 				return ret;
299 			}
300 			data->scale = val;
301 			return 0;
302 		}
303 
304 	return -EINVAL;
305 }
306 
bma180_set_pmode(struct bma180_data * data,bool mode)307 static int bma180_set_pmode(struct bma180_data *data, bool mode)
308 {
309 	u8 reg_val = mode ? data->part_info->lowpower_val : 0;
310 	int ret = bma180_set_bits(data, data->part_info->power_reg,
311 		data->part_info->power_mask, reg_val);
312 
313 	if (ret) {
314 		dev_err(&data->client->dev, "failed to set power mode\n");
315 		return ret;
316 	}
317 	data->pmode = mode;
318 
319 	return 0;
320 }
321 
bma180_soft_reset(struct bma180_data * data)322 static int bma180_soft_reset(struct bma180_data *data)
323 {
324 	int ret = i2c_smbus_write_byte_data(data->client,
325 		data->part_info->softreset_reg,
326 		data->part_info->softreset_val);
327 
328 	if (ret)
329 		dev_err(&data->client->dev, "failed to reset the chip\n");
330 
331 	return ret;
332 }
333 
bma180_chip_init(struct bma180_data * data)334 static int bma180_chip_init(struct bma180_data *data)
335 {
336 	/* Try to read chip_id register. It must return 0x03. */
337 	int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID);
338 
339 	if (ret < 0)
340 		return ret;
341 	if (ret != data->part_info->chip_id) {
342 		dev_err(&data->client->dev, "wrong chip ID %d expected %d\n",
343 			ret, data->part_info->chip_id);
344 		return -ENODEV;
345 	}
346 
347 	ret = bma180_soft_reset(data);
348 	if (ret)
349 		return ret;
350 	/*
351 	 * No serial transaction should occur within minimum 10 us
352 	 * after soft_reset command
353 	 */
354 	msleep(20);
355 
356 	return bma180_set_new_data_intr_state(data, false);
357 }
358 
bma023_chip_config(struct bma180_data * data)359 static int bma023_chip_config(struct bma180_data *data)
360 {
361 	int ret = bma180_chip_init(data);
362 
363 	if (ret)
364 		goto err;
365 
366 	ret = bma180_set_bw(data, 50); /* 50 Hz */
367 	if (ret)
368 		goto err;
369 	ret = bma180_set_scale(data, 2452); /* 2 G */
370 	if (ret)
371 		goto err;
372 
373 	return 0;
374 
375 err:
376 	dev_err(&data->client->dev, "failed to config the chip\n");
377 	return ret;
378 }
379 
bma180_chip_config(struct bma180_data * data)380 static int bma180_chip_config(struct bma180_data *data)
381 {
382 	int ret = bma180_chip_init(data);
383 
384 	if (ret)
385 		goto err;
386 	ret = bma180_set_pmode(data, false);
387 	if (ret)
388 		goto err;
389 	ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1);
390 	if (ret)
391 		goto err;
392 	ret = bma180_set_ee_writing_state(data, true);
393 	if (ret)
394 		goto err;
395 	ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
396 	if (ret)
397 		goto err;
398 	ret = bma180_set_bw(data, 20); /* 20 Hz */
399 	if (ret)
400 		goto err;
401 	ret = bma180_set_scale(data, 2452); /* 2 G */
402 	if (ret)
403 		goto err;
404 
405 	return 0;
406 
407 err:
408 	dev_err(&data->client->dev, "failed to config the chip\n");
409 	return ret;
410 }
411 
bma250_chip_config(struct bma180_data * data)412 static int bma250_chip_config(struct bma180_data *data)
413 {
414 	int ret = bma180_chip_init(data);
415 
416 	if (ret)
417 		goto err;
418 	ret = bma180_set_pmode(data, false);
419 	if (ret)
420 		goto err;
421 	ret = bma180_set_bw(data, 16); /* 16 Hz */
422 	if (ret)
423 		goto err;
424 	ret = bma180_set_scale(data, 38344); /* 2 G */
425 	if (ret)
426 		goto err;
427 	/*
428 	 * This enables dataready interrupt on the INT1 pin
429 	 * FIXME: support using the INT2 pin
430 	 */
431 	ret = bma180_set_bits(data, BMA250_INT_MAP_REG, BMA250_INT1_DATA_MASK, 1);
432 	if (ret)
433 		goto err;
434 
435 	return 0;
436 
437 err:
438 	dev_err(&data->client->dev, "failed to config the chip\n");
439 	return ret;
440 }
441 
bma023_chip_disable(struct bma180_data * data)442 static void bma023_chip_disable(struct bma180_data *data)
443 {
444 	if (bma180_set_sleep_state(data, true))
445 		goto err;
446 
447 	return;
448 
449 err:
450 	dev_err(&data->client->dev, "failed to disable the chip\n");
451 }
452 
bma180_chip_disable(struct bma180_data * data)453 static void bma180_chip_disable(struct bma180_data *data)
454 {
455 	if (bma180_set_new_data_intr_state(data, false))
456 		goto err;
457 	if (bma180_set_ee_writing_state(data, false))
458 		goto err;
459 	if (bma180_set_sleep_state(data, true))
460 		goto err;
461 
462 	return;
463 
464 err:
465 	dev_err(&data->client->dev, "failed to disable the chip\n");
466 }
467 
bma250_chip_disable(struct bma180_data * data)468 static void bma250_chip_disable(struct bma180_data *data)
469 {
470 	if (bma180_set_new_data_intr_state(data, false))
471 		goto err;
472 	if (bma180_set_sleep_state(data, true))
473 		goto err;
474 
475 	return;
476 
477 err:
478 	dev_err(&data->client->dev, "failed to disable the chip\n");
479 }
480 
bma180_show_avail(char * buf,const int * vals,unsigned int n,bool micros)481 static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned int n,
482 				 bool micros)
483 {
484 	size_t len = 0;
485 	int i;
486 
487 	for (i = 0; i < n; i++) {
488 		if (!vals[i])
489 			continue;
490 		len += scnprintf(buf + len, PAGE_SIZE - len,
491 			micros ? "0.%06d " : "%d ", vals[i]);
492 	}
493 	buf[len - 1] = '\n';
494 
495 	return len;
496 }
497 
bma180_show_filter_freq_avail(struct device * dev,struct device_attribute * attr,char * buf)498 static ssize_t bma180_show_filter_freq_avail(struct device *dev,
499 				struct device_attribute *attr, char *buf)
500 {
501 	struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
502 
503 	return bma180_show_avail(buf, data->part_info->bw_table,
504 		data->part_info->num_bw, false);
505 }
506 
bma180_show_scale_avail(struct device * dev,struct device_attribute * attr,char * buf)507 static ssize_t bma180_show_scale_avail(struct device *dev,
508 				struct device_attribute *attr, char *buf)
509 {
510 	struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
511 
512 	return bma180_show_avail(buf, data->part_info->scale_table,
513 		data->part_info->num_scales, true);
514 }
515 
516 static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
517 	S_IRUGO, bma180_show_filter_freq_avail, NULL, 0);
518 
519 static IIO_DEVICE_ATTR(in_accel_scale_available,
520 	S_IRUGO, bma180_show_scale_avail, NULL, 0);
521 
522 static struct attribute *bma180_attributes[] = {
523 	&iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.
524 		dev_attr.attr,
525 	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
526 	NULL,
527 };
528 
529 static const struct attribute_group bma180_attrs_group = {
530 	.attrs = bma180_attributes,
531 };
532 
bma180_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)533 static int bma180_read_raw(struct iio_dev *indio_dev,
534 		struct iio_chan_spec const *chan, int *val, int *val2,
535 		long mask)
536 {
537 	struct bma180_data *data = iio_priv(indio_dev);
538 	int ret;
539 
540 	switch (mask) {
541 	case IIO_CHAN_INFO_RAW:
542 		ret = iio_device_claim_direct_mode(indio_dev);
543 		if (ret)
544 			return ret;
545 
546 		mutex_lock(&data->mutex);
547 		ret = bma180_get_data_reg(data, chan->scan_index);
548 		mutex_unlock(&data->mutex);
549 		iio_device_release_direct_mode(indio_dev);
550 		if (ret < 0)
551 			return ret;
552 		if (chan->scan_type.sign == 's') {
553 			*val = sign_extend32(ret >> chan->scan_type.shift,
554 				chan->scan_type.realbits - 1);
555 		} else {
556 			*val = ret;
557 		}
558 		return IIO_VAL_INT;
559 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
560 		*val = data->bw;
561 		return IIO_VAL_INT;
562 	case IIO_CHAN_INFO_SCALE:
563 		switch (chan->type) {
564 		case IIO_ACCEL:
565 			*val = 0;
566 			*val2 = data->scale;
567 			return IIO_VAL_INT_PLUS_MICRO;
568 		case IIO_TEMP:
569 			*val = 500;
570 			return IIO_VAL_INT;
571 		default:
572 			return -EINVAL;
573 		}
574 	case IIO_CHAN_INFO_OFFSET:
575 		*val = data->part_info->temp_offset;
576 		return IIO_VAL_INT;
577 	default:
578 		return -EINVAL;
579 	}
580 }
581 
bma180_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)582 static int bma180_write_raw(struct iio_dev *indio_dev,
583 		struct iio_chan_spec const *chan, int val, int val2, long mask)
584 {
585 	struct bma180_data *data = iio_priv(indio_dev);
586 	int ret;
587 
588 	switch (mask) {
589 	case IIO_CHAN_INFO_SCALE:
590 		if (val)
591 			return -EINVAL;
592 		mutex_lock(&data->mutex);
593 		ret = bma180_set_scale(data, val2);
594 		mutex_unlock(&data->mutex);
595 		return ret;
596 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
597 		if (val2)
598 			return -EINVAL;
599 		mutex_lock(&data->mutex);
600 		ret = bma180_set_bw(data, val);
601 		mutex_unlock(&data->mutex);
602 		return ret;
603 	default:
604 		return -EINVAL;
605 	}
606 }
607 
608 static const struct iio_info bma180_info = {
609 	.attrs			= &bma180_attrs_group,
610 	.read_raw		= bma180_read_raw,
611 	.write_raw		= bma180_write_raw,
612 };
613 
614 static const char * const bma180_power_modes[] = { "low_noise", "low_power" };
615 
bma180_get_power_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan)616 static int bma180_get_power_mode(struct iio_dev *indio_dev,
617 		const struct iio_chan_spec *chan)
618 {
619 	struct bma180_data *data = iio_priv(indio_dev);
620 
621 	return data->pmode;
622 }
623 
bma180_set_power_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,unsigned int mode)624 static int bma180_set_power_mode(struct iio_dev *indio_dev,
625 		const struct iio_chan_spec *chan, unsigned int mode)
626 {
627 	struct bma180_data *data = iio_priv(indio_dev);
628 	int ret;
629 
630 	mutex_lock(&data->mutex);
631 	ret = bma180_set_pmode(data, mode);
632 	mutex_unlock(&data->mutex);
633 
634 	return ret;
635 }
636 
637 static const struct iio_mount_matrix *
bma180_accel_get_mount_matrix(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)638 bma180_accel_get_mount_matrix(const struct iio_dev *indio_dev,
639 				const struct iio_chan_spec *chan)
640 {
641 	struct bma180_data *data = iio_priv(indio_dev);
642 
643 	return &data->orientation;
644 }
645 
646 static const struct iio_enum bma180_power_mode_enum = {
647 	.items = bma180_power_modes,
648 	.num_items = ARRAY_SIZE(bma180_power_modes),
649 	.get = bma180_get_power_mode,
650 	.set = bma180_set_power_mode,
651 };
652 
653 static const struct iio_chan_spec_ext_info bma023_ext_info[] = {
654 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bma180_accel_get_mount_matrix),
655 	{ }
656 };
657 
658 static const struct iio_chan_spec_ext_info bma180_ext_info[] = {
659 	IIO_ENUM("power_mode", IIO_SHARED_BY_TYPE, &bma180_power_mode_enum),
660 	IIO_ENUM_AVAILABLE("power_mode", IIO_SHARED_BY_TYPE, &bma180_power_mode_enum),
661 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bma180_accel_get_mount_matrix),
662 	{ }
663 };
664 
665 #define BMA023_ACC_CHANNEL(_axis, _bits) {				\
666 	.type = IIO_ACCEL,						\
667 	.modified = 1,							\
668 	.channel2 = IIO_MOD_##_axis,					\
669 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
670 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
671 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),	\
672 	.scan_index = AXIS_##_axis,					\
673 	.scan_type = {							\
674 		.sign = 's',						\
675 		.realbits = _bits,					\
676 		.storagebits = 16,					\
677 		.shift = 16 - _bits,					\
678 	},								\
679 	.ext_info = bma023_ext_info,					\
680 }
681 
682 #define BMA150_TEMP_CHANNEL {						\
683 	.type = IIO_TEMP,						\
684 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |			\
685 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),	\
686 	.scan_index = TEMP,						\
687 	.scan_type = {							\
688 		.sign = 'u',						\
689 		.realbits = 8,						\
690 		.storagebits = 16,					\
691 	},								\
692 }
693 
694 #define BMA180_ACC_CHANNEL(_axis, _bits) {				\
695 	.type = IIO_ACCEL,						\
696 	.modified = 1,							\
697 	.channel2 = IIO_MOD_##_axis,					\
698 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
699 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
700 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),	\
701 	.scan_index = AXIS_##_axis,					\
702 	.scan_type = {							\
703 		.sign = 's',						\
704 		.realbits = _bits,					\
705 		.storagebits = 16,					\
706 		.shift = 16 - _bits,					\
707 	},								\
708 	.ext_info = bma180_ext_info,					\
709 }
710 
711 #define BMA180_TEMP_CHANNEL {						\
712 	.type = IIO_TEMP,						\
713 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |			\
714 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),	\
715 	.scan_index = TEMP,						\
716 	.scan_type = {							\
717 		.sign = 's',						\
718 		.realbits = 8,						\
719 		.storagebits = 16,					\
720 	},								\
721 }
722 
723 static const struct iio_chan_spec bma023_channels[] = {
724 	BMA023_ACC_CHANNEL(X, 10),
725 	BMA023_ACC_CHANNEL(Y, 10),
726 	BMA023_ACC_CHANNEL(Z, 10),
727 	IIO_CHAN_SOFT_TIMESTAMP(4),
728 };
729 
730 static const struct iio_chan_spec bma150_channels[] = {
731 	BMA023_ACC_CHANNEL(X, 10),
732 	BMA023_ACC_CHANNEL(Y, 10),
733 	BMA023_ACC_CHANNEL(Z, 10),
734 	BMA150_TEMP_CHANNEL,
735 	IIO_CHAN_SOFT_TIMESTAMP(4),
736 };
737 
738 static const struct iio_chan_spec bma180_channels[] = {
739 	BMA180_ACC_CHANNEL(X, 14),
740 	BMA180_ACC_CHANNEL(Y, 14),
741 	BMA180_ACC_CHANNEL(Z, 14),
742 	BMA180_TEMP_CHANNEL,
743 	IIO_CHAN_SOFT_TIMESTAMP(4),
744 };
745 
746 static const struct iio_chan_spec bma250_channels[] = {
747 	BMA180_ACC_CHANNEL(X, 10),
748 	BMA180_ACC_CHANNEL(Y, 10),
749 	BMA180_ACC_CHANNEL(Z, 10),
750 	BMA180_TEMP_CHANNEL,
751 	IIO_CHAN_SOFT_TIMESTAMP(4),
752 };
753 
754 static const struct bma180_part_info bma180_part_info[] = {
755 	[BMA023] = {
756 		.chip_id = BMA023_ID_REG_VAL,
757 		.channels = bma023_channels,
758 		.num_channels = ARRAY_SIZE(bma023_channels),
759 		.scale_table = bma023_scale_table,
760 		.num_scales = ARRAY_SIZE(bma023_scale_table),
761 		.bw_table = bma023_bw_table,
762 		.num_bw = ARRAY_SIZE(bma023_bw_table),
763 		/* No temperature channel */
764 		.temp_offset = 0,
765 		.int_reset_reg = BMA023_CTRL_REG0,
766 		.int_reset_mask = BMA023_INT_RESET_MASK,
767 		.sleep_reg = BMA023_CTRL_REG0,
768 		.sleep_mask = BMA023_SLEEP,
769 		.bw_reg = BMA023_CTRL_REG2,
770 		.bw_mask = BMA023_BW_MASK,
771 		.scale_reg = BMA023_CTRL_REG2,
772 		.scale_mask = BMA023_RANGE_MASK,
773 		/* No power mode on bma023 */
774 		.power_reg = 0,
775 		.power_mask = 0,
776 		.lowpower_val = 0,
777 		.int_enable_reg = BMA023_CTRL_REG3,
778 		.int_enable_mask = BMA023_NEW_DATA_INT,
779 		.softreset_reg = BMA023_CTRL_REG0,
780 		.softreset_val = BMA023_RESET_VAL,
781 		.chip_config = bma023_chip_config,
782 		.chip_disable = bma023_chip_disable,
783 	},
784 	[BMA150] = {
785 		.chip_id = BMA023_ID_REG_VAL,
786 		.channels = bma150_channels,
787 		.num_channels = ARRAY_SIZE(bma150_channels),
788 		.scale_table = bma023_scale_table,
789 		.num_scales = ARRAY_SIZE(bma023_scale_table),
790 		.bw_table = bma023_bw_table,
791 		.num_bw = ARRAY_SIZE(bma023_bw_table),
792 		.temp_offset = -60, /* 0 LSB @ -30 degree C */
793 		.int_reset_reg = BMA023_CTRL_REG0,
794 		.int_reset_mask = BMA023_INT_RESET_MASK,
795 		.sleep_reg = BMA023_CTRL_REG0,
796 		.sleep_mask = BMA023_SLEEP,
797 		.bw_reg = BMA023_CTRL_REG2,
798 		.bw_mask = BMA023_BW_MASK,
799 		.scale_reg = BMA023_CTRL_REG2,
800 		.scale_mask = BMA023_RANGE_MASK,
801 		/* No power mode on bma150 */
802 		.power_reg = 0,
803 		.power_mask = 0,
804 		.lowpower_val = 0,
805 		.int_enable_reg = BMA023_CTRL_REG3,
806 		.int_enable_mask = BMA023_NEW_DATA_INT,
807 		.softreset_reg = BMA023_CTRL_REG0,
808 		.softreset_val = BMA023_RESET_VAL,
809 		.chip_config = bma023_chip_config,
810 		.chip_disable = bma023_chip_disable,
811 	},
812 	[BMA180] = {
813 		.chip_id = BMA180_ID_REG_VAL,
814 		.channels = bma180_channels,
815 		.num_channels = ARRAY_SIZE(bma180_channels),
816 		.scale_table = bma180_scale_table,
817 		.num_scales = ARRAY_SIZE(bma180_scale_table),
818 		.bw_table = bma180_bw_table,
819 		.num_bw = ARRAY_SIZE(bma180_bw_table),
820 		.temp_offset = 48, /* 0 LSB @ 24 degree C */
821 		.int_reset_reg = BMA180_CTRL_REG0,
822 		.int_reset_mask = BMA180_RESET_INT,
823 		.sleep_reg = BMA180_CTRL_REG0,
824 		.sleep_mask = BMA180_SLEEP,
825 		.bw_reg = BMA180_BW_TCS,
826 		.bw_mask = BMA180_BW,
827 		.scale_reg = BMA180_OFFSET_LSB1,
828 		.scale_mask = BMA180_RANGE,
829 		.power_reg = BMA180_TCO_Z,
830 		.power_mask = BMA180_MODE_CONFIG,
831 		.lowpower_val = BMA180_LOW_POWER,
832 		.int_enable_reg = BMA180_CTRL_REG3,
833 		.int_enable_mask = BMA180_NEW_DATA_INT,
834 		.softreset_reg = BMA180_RESET,
835 		.softreset_val = BMA180_RESET_VAL,
836 		.chip_config = bma180_chip_config,
837 		.chip_disable = bma180_chip_disable,
838 	},
839 	[BMA250] = {
840 		.chip_id = BMA250_ID_REG_VAL,
841 		.channels = bma250_channels,
842 		.num_channels = ARRAY_SIZE(bma250_channels),
843 		.scale_table = bma250_scale_table,
844 		.num_scales = ARRAY_SIZE(bma250_scale_table),
845 		.bw_table = bma250_bw_table,
846 		.num_bw = ARRAY_SIZE(bma250_bw_table),
847 		.temp_offset = 48, /* 0 LSB @ 24 degree C */
848 		.int_reset_reg = BMA250_INT_RESET_REG,
849 		.int_reset_mask = BMA250_INT_RESET_MASK,
850 		.sleep_reg = BMA250_POWER_REG,
851 		.sleep_mask = BMA250_SUSPEND_MASK,
852 		.bw_reg = BMA250_BW_REG,
853 		.bw_mask = BMA250_BW_MASK,
854 		.bw_offset = BMA250_BW_OFFSET,
855 		.scale_reg = BMA250_RANGE_REG,
856 		.scale_mask = BMA250_RANGE_MASK,
857 		.power_reg = BMA250_POWER_REG,
858 		.power_mask = BMA250_LOWPOWER_MASK,
859 		.lowpower_val = 1,
860 		.int_enable_reg = BMA250_INT_ENABLE_REG,
861 		.int_enable_mask = BMA250_DATA_INTEN_MASK,
862 		.softreset_reg = BMA250_RESET_REG,
863 		.softreset_val = BMA180_RESET_VAL,
864 		.chip_config = bma250_chip_config,
865 		.chip_disable = bma250_chip_disable,
866 	},
867 };
868 
bma180_trigger_handler(int irq,void * p)869 static irqreturn_t bma180_trigger_handler(int irq, void *p)
870 {
871 	struct iio_poll_func *pf = p;
872 	struct iio_dev *indio_dev = pf->indio_dev;
873 	struct bma180_data *data = iio_priv(indio_dev);
874 	s64 time_ns = iio_get_time_ns(indio_dev);
875 	int bit, ret, i = 0;
876 
877 	mutex_lock(&data->mutex);
878 
879 	for_each_set_bit(bit, indio_dev->active_scan_mask,
880 			 indio_dev->masklength) {
881 		ret = bma180_get_data_reg(data, bit);
882 		if (ret < 0) {
883 			mutex_unlock(&data->mutex);
884 			goto err;
885 		}
886 		data->scan.chan[i++] = ret;
887 	}
888 
889 	mutex_unlock(&data->mutex);
890 
891 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, time_ns);
892 err:
893 	iio_trigger_notify_done(indio_dev->trig);
894 
895 	return IRQ_HANDLED;
896 }
897 
bma180_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)898 static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig,
899 		bool state)
900 {
901 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
902 	struct bma180_data *data = iio_priv(indio_dev);
903 
904 	return bma180_set_new_data_intr_state(data, state);
905 }
906 
bma180_trig_reen(struct iio_trigger * trig)907 static void bma180_trig_reen(struct iio_trigger *trig)
908 {
909 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
910 	struct bma180_data *data = iio_priv(indio_dev);
911 	int ret;
912 
913 	ret = bma180_reset_intr(data);
914 	if (ret)
915 		dev_err(&data->client->dev, "failed to reset interrupt\n");
916 }
917 
918 static const struct iio_trigger_ops bma180_trigger_ops = {
919 	.set_trigger_state = bma180_data_rdy_trigger_set_state,
920 	.reenable = bma180_trig_reen,
921 };
922 
bma180_probe(struct i2c_client * client)923 static int bma180_probe(struct i2c_client *client)
924 {
925 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
926 	struct device *dev = &client->dev;
927 	struct bma180_data *data;
928 	struct iio_dev *indio_dev;
929 	enum chip_ids chip;
930 	int ret;
931 
932 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
933 	if (!indio_dev)
934 		return -ENOMEM;
935 
936 	data = iio_priv(indio_dev);
937 	i2c_set_clientdata(client, indio_dev);
938 	data->client = client;
939 	if (client->dev.of_node)
940 		chip = (uintptr_t)of_device_get_match_data(dev);
941 	else
942 		chip = id->driver_data;
943 	data->part_info = &bma180_part_info[chip];
944 
945 	ret = iio_read_mount_matrix(dev, &data->orientation);
946 	if (ret)
947 		return ret;
948 
949 	data->vdd_supply = devm_regulator_get(dev, "vdd");
950 	if (IS_ERR(data->vdd_supply))
951 		return dev_err_probe(dev, PTR_ERR(data->vdd_supply),
952 				     "Failed to get vdd regulator\n");
953 
954 	data->vddio_supply = devm_regulator_get(dev, "vddio");
955 	if (IS_ERR(data->vddio_supply))
956 		return dev_err_probe(dev, PTR_ERR(data->vddio_supply),
957 				     "Failed to get vddio regulator\n");
958 
959 	/* Typical voltage 2.4V these are min and max */
960 	ret = regulator_set_voltage(data->vdd_supply, 1620000, 3600000);
961 	if (ret)
962 		return ret;
963 	ret = regulator_set_voltage(data->vddio_supply, 1200000, 3600000);
964 	if (ret)
965 		return ret;
966 	ret = regulator_enable(data->vdd_supply);
967 	if (ret) {
968 		dev_err(dev, "Failed to enable vdd regulator: %d\n", ret);
969 		return ret;
970 	}
971 	ret = regulator_enable(data->vddio_supply);
972 	if (ret) {
973 		dev_err(dev, "Failed to enable vddio regulator: %d\n", ret);
974 		goto err_disable_vdd;
975 	}
976 	/* Wait to make sure we started up properly (3 ms at least) */
977 	usleep_range(3000, 5000);
978 
979 	ret = data->part_info->chip_config(data);
980 	if (ret < 0)
981 		goto err_chip_disable;
982 
983 	mutex_init(&data->mutex);
984 	indio_dev->channels = data->part_info->channels;
985 	indio_dev->num_channels = data->part_info->num_channels;
986 	indio_dev->name = id->name;
987 	indio_dev->modes = INDIO_DIRECT_MODE;
988 	indio_dev->info = &bma180_info;
989 
990 	if (client->irq > 0) {
991 		data->trig = iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
992 					       iio_device_id(indio_dev));
993 		if (!data->trig) {
994 			ret = -ENOMEM;
995 			goto err_chip_disable;
996 		}
997 
998 		ret = devm_request_irq(dev, client->irq,
999 			iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING,
1000 			"bma180_event", data->trig);
1001 		if (ret) {
1002 			dev_err(dev, "unable to request IRQ\n");
1003 			goto err_trigger_free;
1004 		}
1005 
1006 		data->trig->ops = &bma180_trigger_ops;
1007 		iio_trigger_set_drvdata(data->trig, indio_dev);
1008 
1009 		ret = iio_trigger_register(data->trig);
1010 		if (ret)
1011 			goto err_trigger_free;
1012 
1013 		indio_dev->trig = iio_trigger_get(data->trig);
1014 	}
1015 
1016 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
1017 			bma180_trigger_handler, NULL);
1018 	if (ret < 0) {
1019 		dev_err(dev, "unable to setup iio triggered buffer\n");
1020 		goto err_trigger_unregister;
1021 	}
1022 
1023 	ret = iio_device_register(indio_dev);
1024 	if (ret < 0) {
1025 		dev_err(dev, "unable to register iio device\n");
1026 		goto err_buffer_cleanup;
1027 	}
1028 
1029 	return 0;
1030 
1031 err_buffer_cleanup:
1032 	iio_triggered_buffer_cleanup(indio_dev);
1033 err_trigger_unregister:
1034 	if (data->trig)
1035 		iio_trigger_unregister(data->trig);
1036 err_trigger_free:
1037 	iio_trigger_free(data->trig);
1038 err_chip_disable:
1039 	data->part_info->chip_disable(data);
1040 	regulator_disable(data->vddio_supply);
1041 err_disable_vdd:
1042 	regulator_disable(data->vdd_supply);
1043 
1044 	return ret;
1045 }
1046 
bma180_remove(struct i2c_client * client)1047 static void bma180_remove(struct i2c_client *client)
1048 {
1049 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1050 	struct bma180_data *data = iio_priv(indio_dev);
1051 
1052 	iio_device_unregister(indio_dev);
1053 	iio_triggered_buffer_cleanup(indio_dev);
1054 	if (data->trig) {
1055 		iio_trigger_unregister(data->trig);
1056 		iio_trigger_free(data->trig);
1057 	}
1058 
1059 	mutex_lock(&data->mutex);
1060 	data->part_info->chip_disable(data);
1061 	mutex_unlock(&data->mutex);
1062 	regulator_disable(data->vddio_supply);
1063 	regulator_disable(data->vdd_supply);
1064 }
1065 
bma180_suspend(struct device * dev)1066 static int bma180_suspend(struct device *dev)
1067 {
1068 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1069 	struct bma180_data *data = iio_priv(indio_dev);
1070 	int ret;
1071 
1072 	mutex_lock(&data->mutex);
1073 	ret = bma180_set_sleep_state(data, true);
1074 	mutex_unlock(&data->mutex);
1075 
1076 	return ret;
1077 }
1078 
bma180_resume(struct device * dev)1079 static int bma180_resume(struct device *dev)
1080 {
1081 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1082 	struct bma180_data *data = iio_priv(indio_dev);
1083 	int ret;
1084 
1085 	mutex_lock(&data->mutex);
1086 	ret = bma180_set_sleep_state(data, false);
1087 	mutex_unlock(&data->mutex);
1088 
1089 	return ret;
1090 }
1091 
1092 static DEFINE_SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
1093 
1094 static const struct i2c_device_id bma180_ids[] = {
1095 	{ "bma023", BMA023 },
1096 	{ "bma150", BMA150 },
1097 	{ "bma180", BMA180 },
1098 	{ "bma250", BMA250 },
1099 	{ "smb380", BMA150 },
1100 	{ }
1101 };
1102 
1103 MODULE_DEVICE_TABLE(i2c, bma180_ids);
1104 
1105 static const struct of_device_id bma180_of_match[] = {
1106 	{
1107 		.compatible = "bosch,bma023",
1108 		.data = (void *)BMA023
1109 	},
1110 	{
1111 		.compatible = "bosch,bma150",
1112 		.data = (void *)BMA150
1113 	},
1114 	{
1115 		.compatible = "bosch,bma180",
1116 		.data = (void *)BMA180
1117 	},
1118 	{
1119 		.compatible = "bosch,bma250",
1120 		.data = (void *)BMA250
1121 	},
1122 	{
1123 		.compatible = "bosch,smb380",
1124 		.data = (void *)BMA150
1125 	},
1126 	{ }
1127 };
1128 MODULE_DEVICE_TABLE(of, bma180_of_match);
1129 
1130 static struct i2c_driver bma180_driver = {
1131 	.driver = {
1132 		.name	= "bma180",
1133 		.pm	= pm_sleep_ptr(&bma180_pm_ops),
1134 		.of_match_table = bma180_of_match,
1135 	},
1136 	.probe		= bma180_probe,
1137 	.remove		= bma180_remove,
1138 	.id_table	= bma180_ids,
1139 };
1140 
1141 module_i2c_driver(bma180_driver);
1142 
1143 MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
1144 MODULE_AUTHOR("Texas Instruments, Inc.");
1145 MODULE_DESCRIPTION("Bosch BMA023/BMA1x0/BMA250 triaxial acceleration sensor");
1146 MODULE_LICENSE("GPL");
1147