blob: 75c6d2103e07adad3b11919687e81f8dd7a8fca3 [file] [log] [blame]
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +01001/*
2 * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor
3 *
4 * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
5 *
Peter Meerwald2017cff2014-08-19 23:43:00 +01006 * Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net>
7 *
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +01008 * This file is subject to the terms and conditions of version 2 of
9 * the GNU General Public License. See the file COPYING in the main
10 * directory of this archive for more details.
Peter Meerwald2017cff2014-08-19 23:43:00 +010011 *
12 * SPI is not supported by driver
13 * BMA180: 7-bit I2C slave address 0x40 or 0x41
14 * BMA250: 7-bit I2C slave address 0x18 or 0x19
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +010015 */
16
17#include <linux/module.h>
18#include <linux/i2c.h>
19#include <linux/interrupt.h>
20#include <linux/delay.h>
21#include <linux/of.h>
22#include <linux/bitops.h>
23#include <linux/slab.h>
24#include <linux/string.h>
25#include <linux/iio/iio.h>
26#include <linux/iio/sysfs.h>
27#include <linux/iio/buffer.h>
28#include <linux/iio/trigger.h>
29#include <linux/iio/trigger_consumer.h>
30#include <linux/iio/triggered_buffer.h>
31
32#define BMA180_DRV_NAME "bma180"
33#define BMA180_IRQ_NAME "bma180_event"
34
Peter Meerwald1b9030f2014-08-19 23:43:00 +010035enum {
36 BMA180,
Peter Meerwald2017cff2014-08-19 23:43:00 +010037 BMA250,
Peter Meerwald1b9030f2014-08-19 23:43:00 +010038};
39
Peter Meerwaldc1949ec2014-08-19 23:43:00 +010040struct bma180_data;
41
Peter Meerwald1b9030f2014-08-19 23:43:00 +010042struct bma180_part_info {
43 const struct iio_chan_spec *channels;
44 unsigned num_channels;
45 const int *scale_table;
46 unsigned num_scales;
47 const int *bw_table;
48 unsigned num_bw;
Peter Meerwald2017cff2014-08-19 23:43:00 +010049
50 u8 int_reset_reg, int_reset_mask;
51 u8 sleep_reg, sleep_mask;
52 u8 bw_reg, bw_mask;
53 u8 scale_reg, scale_mask;
54 u8 power_reg, power_mask, lowpower_val;
55 u8 int_enable_reg, int_enable_mask;
56 u8 softreset_reg;
57
Peter Meerwaldc1949ec2014-08-19 23:43:00 +010058 int (*chip_config)(struct bma180_data *data);
59 void (*chip_disable)(struct bma180_data *data);
Peter Meerwald1b9030f2014-08-19 23:43:00 +010060};
61
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +010062/* Register set */
63#define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */
64#define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */
Peter Meerwaldb81fbab2014-08-19 23:43:00 +010065#define BMA180_TEMP 0x08
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +010066#define BMA180_CTRL_REG0 0x0d
67#define BMA180_RESET 0x10
68#define BMA180_BW_TCS 0x20
69#define BMA180_CTRL_REG3 0x21
70#define BMA180_TCO_Z 0x30
71#define BMA180_OFFSET_LSB1 0x35
72
73/* BMA180_CTRL_REG0 bits */
74#define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */
75#define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */
76#define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */
77#define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */
78
79/* BMA180_CTRL_REG3 bits */
80#define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */
81
82/* BMA180_OFFSET_LSB1 skipping mode bit */
83#define BMA180_SMP_SKIP BIT(0)
84
85/* Bit masks for registers bit fields */
Peter Meerwaldcab767d2014-08-19 23:43:00 +010086#define BMA180_RANGE 0x0e /* Range of measured accel values */
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +010087#define BMA180_BW 0xf0 /* Accel bandwidth */
88#define BMA180_MODE_CONFIG 0x03 /* Config operation modes */
89
90/* We have to write this value in reset register to do soft reset */
91#define BMA180_RESET_VAL 0xb6
92
Peter Meerwald9a70b142014-08-19 23:43:00 +010093#define BMA180_ID_REG_VAL 0x03
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +010094
95/* Chip power modes */
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +010096#define BMA180_LOW_POWER 0x03
97
Peter Meerwald2017cff2014-08-19 23:43:00 +010098#define BMA250_RANGE_REG 0x0f
99#define BMA250_BW_REG 0x10
100#define BMA250_POWER_REG 0x11
101#define BMA250_RESET_REG 0x14
102#define BMA250_INT_ENABLE_REG 0x17
103#define BMA250_INT_MAP_REG 0x1a
104#define BMA250_INT_RESET_REG 0x21
105
106#define BMA250_RANGE_MASK GENMASK(3, 0) /* Range of accel values */
107#define BMA250_BW_MASK GENMASK(4, 0) /* Accel bandwidth */
108#define BMA250_SUSPEND_MASK BIT(7) /* chip will sleep */
109#define BMA250_LOWPOWER_MASK BIT(6)
110#define BMA250_DATA_INTEN_MASK BIT(4)
111#define BMA250_INT1_DATA_MASK BIT(0)
112#define BMA250_INT_RESET_MASK BIT(7) /* Reset pending interrupts */
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100113
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100114struct bma180_data {
115 struct i2c_client *client;
116 struct iio_trigger *trig;
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100117 const struct bma180_part_info *part_info;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100118 struct mutex mutex;
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100119 bool sleep_state;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100120 int scale;
121 int bw;
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100122 bool pmode;
Peter Meerwaldb9a6a2372014-08-19 23:43:00 +0100123 u8 buff[16]; /* 3x 16-bit + 8-bit + padding + timestamp */
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100124};
125
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100126enum bma180_chan {
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100127 AXIS_X,
128 AXIS_Y,
129 AXIS_Z,
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100130 TEMP
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100131};
132
Peter Meerwaldcab767d2014-08-19 23:43:00 +0100133static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
134static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 };
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100135
Peter Meerwald2017cff2014-08-19 23:43:00 +0100136static int bma250_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */
137static int bma250_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0,
138 0, 0, 306458 };
139
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100140static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan)
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100141{
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100142 int ret;
143
144 if (data->sleep_state)
145 return -EBUSY;
146
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100147 switch (chan) {
148 case TEMP:
149 ret = i2c_smbus_read_byte_data(data->client, BMA180_TEMP);
150 if (ret < 0)
151 dev_err(&data->client->dev, "failed to read temp register\n");
152 break;
153 default:
154 ret = i2c_smbus_read_word_data(data->client,
155 BMA180_ACC_X_LSB + chan * 2);
156 if (ret < 0)
157 dev_err(&data->client->dev,
158 "failed to read accel_%c register\n",
159 'x' + chan);
160 }
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100161
162 return ret;
163}
164
165static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
166{
167 int ret = i2c_smbus_read_byte_data(data->client, reg);
168 u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1));
169
170 if (ret < 0)
171 return ret;
172
173 return i2c_smbus_write_byte_data(data->client, reg, reg_val);
174}
175
176static int bma180_reset_intr(struct bma180_data *data)
177{
Peter Meerwald2017cff2014-08-19 23:43:00 +0100178 int ret = bma180_set_bits(data, data->part_info->int_reset_reg,
179 data->part_info->int_reset_mask, 1);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100180
181 if (ret)
182 dev_err(&data->client->dev, "failed to reset interrupt\n");
183
184 return ret;
185}
186
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100187static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state)
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100188{
Peter Meerwald2017cff2014-08-19 23:43:00 +0100189 int ret = bma180_set_bits(data, data->part_info->int_enable_reg,
190 data->part_info->int_enable_mask, state);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100191 if (ret)
192 goto err;
193 ret = bma180_reset_intr(data);
194 if (ret)
195 goto err;
196
197 return 0;
198
199err:
200 dev_err(&data->client->dev,
201 "failed to set new data interrupt state %d\n", state);
202 return ret;
203}
204
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100205static int bma180_set_sleep_state(struct bma180_data *data, bool state)
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100206{
Peter Meerwald2017cff2014-08-19 23:43:00 +0100207 int ret = bma180_set_bits(data, data->part_info->sleep_reg,
208 data->part_info->sleep_mask, state);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100209
210 if (ret) {
211 dev_err(&data->client->dev,
212 "failed to set sleep state %d\n", state);
213 return ret;
214 }
215 data->sleep_state = state;
216
217 return 0;
218}
219
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100220static int bma180_set_ee_writing_state(struct bma180_data *data, bool state)
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100221{
222 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state);
223
224 if (ret)
225 dev_err(&data->client->dev,
226 "failed to set ee writing state %d\n", state);
227
228 return ret;
229}
230
231static int bma180_set_bw(struct bma180_data *data, int val)
232{
233 int ret, i;
234
235 if (data->sleep_state)
236 return -EBUSY;
237
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100238 for (i = 0; i < data->part_info->num_bw; ++i) {
239 if (data->part_info->bw_table[i] == val) {
Peter Meerwald2017cff2014-08-19 23:43:00 +0100240 ret = bma180_set_bits(data, data->part_info->bw_reg,
241 data->part_info->bw_mask, i);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100242 if (ret) {
243 dev_err(&data->client->dev,
244 "failed to set bandwidth\n");
245 return ret;
246 }
247 data->bw = val;
248 return 0;
249 }
250 }
251
252 return -EINVAL;
253}
254
255static int bma180_set_scale(struct bma180_data *data, int val)
256{
257 int ret, i;
258
259 if (data->sleep_state)
260 return -EBUSY;
261
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100262 for (i = 0; i < data->part_info->num_scales; ++i)
263 if (data->part_info->scale_table[i] == val) {
Peter Meerwald2017cff2014-08-19 23:43:00 +0100264 ret = bma180_set_bits(data, data->part_info->scale_reg,
265 data->part_info->scale_mask, i);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100266 if (ret) {
267 dev_err(&data->client->dev,
268 "failed to set scale\n");
269 return ret;
270 }
271 data->scale = val;
272 return 0;
273 }
274
275 return -EINVAL;
276}
277
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100278static int bma180_set_pmode(struct bma180_data *data, bool mode)
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100279{
Peter Meerwald2017cff2014-08-19 23:43:00 +0100280 u8 reg_val = mode ? data->part_info->lowpower_val : 0;
281 int ret = bma180_set_bits(data, data->part_info->power_reg,
282 data->part_info->power_mask, reg_val);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100283
284 if (ret) {
285 dev_err(&data->client->dev, "failed to set power mode\n");
286 return ret;
287 }
288 data->pmode = mode;
289
290 return 0;
291}
292
293static int bma180_soft_reset(struct bma180_data *data)
294{
295 int ret = i2c_smbus_write_byte_data(data->client,
Peter Meerwald2017cff2014-08-19 23:43:00 +0100296 data->part_info->softreset_reg, BMA180_RESET_VAL);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100297
298 if (ret)
299 dev_err(&data->client->dev, "failed to reset the chip\n");
300
301 return ret;
302}
303
304static int bma180_chip_init(struct bma180_data *data)
305{
306 /* Try to read chip_id register. It must return 0x03. */
307 int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID);
308
309 if (ret < 0)
Peter Meerwaldc1949ec2014-08-19 23:43:00 +0100310 return ret;
311 if (ret != BMA180_ID_REG_VAL)
312 return -ENODEV;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100313
314 ret = bma180_soft_reset(data);
315 if (ret)
Peter Meerwaldc1949ec2014-08-19 23:43:00 +0100316 return ret;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100317 /*
318 * No serial transaction should occur within minimum 10 us
319 * after soft_reset command
320 */
321 msleep(20);
322
Peter Meerwald2017cff2014-08-19 23:43:00 +0100323 ret = bma180_set_new_data_intr_state(data, false);
324 if (ret)
325 return ret;
326
327 return bma180_set_pmode(data, false);
Peter Meerwaldc1949ec2014-08-19 23:43:00 +0100328}
329
330static int bma180_chip_config(struct bma180_data *data)
331{
332 int ret = bma180_chip_init(data);
333
334 if (ret)
335 goto err;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100336 ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1);
337 if (ret)
338 goto err;
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100339 ret = bma180_set_ee_writing_state(data, true);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100340 if (ret)
341 goto err;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100342 ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
343 if (ret)
344 goto err;
Peter Meerwald2017cff2014-08-19 23:43:00 +0100345 ret = bma180_set_bw(data, 20); /* 20 Hz */
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100346 if (ret)
347 goto err;
Peter Meerwald2017cff2014-08-19 23:43:00 +0100348 ret = bma180_set_scale(data, 2452); /* 2 G */
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100349 if (ret)
350 goto err;
Peter Meerwald2017cff2014-08-19 23:43:00 +0100351
352 return 0;
353
354err:
355 dev_err(&data->client->dev, "failed to config the chip\n");
356 return ret;
357}
358
359static int bma250_chip_config(struct bma180_data *data)
360{
361 int ret = bma180_chip_init(data);
362
363 if (ret)
364 goto err;
365 ret = bma180_set_bw(data, 16); /* 16 Hz */
366 if (ret)
367 goto err;
368 ret = bma180_set_scale(data, 38344); /* 2 G */
369 if (ret)
370 goto err;
371 ret = bma180_set_bits(data, BMA250_INT_MAP_REG,
372 BMA250_INT1_DATA_MASK, 1);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100373 if (ret)
374 goto err;
375
376 return 0;
377
378err:
Peter Meerwaldc1949ec2014-08-19 23:43:00 +0100379 dev_err(&data->client->dev, "failed to config the chip\n");
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100380 return ret;
381}
382
383static void bma180_chip_disable(struct bma180_data *data)
384{
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100385 if (bma180_set_new_data_intr_state(data, false))
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100386 goto err;
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100387 if (bma180_set_ee_writing_state(data, false))
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100388 goto err;
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100389 if (bma180_set_sleep_state(data, true))
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100390 goto err;
391
392 return;
393
394err:
395 dev_err(&data->client->dev, "failed to disable the chip\n");
396}
397
Peter Meerwald2017cff2014-08-19 23:43:00 +0100398static void bma250_chip_disable(struct bma180_data *data)
399{
400 if (bma180_set_new_data_intr_state(data, false))
401 goto err;
402 if (bma180_set_sleep_state(data, true))
403 goto err;
404
405 return;
406
407err:
408 dev_err(&data->client->dev, "failed to disable the chip\n");
409}
410
Peter Meerwald402a3242014-08-19 23:43:00 +0100411static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned n,
412 bool micros)
413{
414 size_t len = 0;
415 int i;
416
417 for (i = 0; i < n; i++) {
418 if (!vals[i])
419 continue;
420 len += scnprintf(buf + len, PAGE_SIZE - len,
421 micros ? "0.%06d " : "%d ", vals[i]);
422 }
423 buf[len - 1] = '\n';
424
425 return len;
426}
427
428static ssize_t bma180_show_filter_freq_avail(struct device *dev,
429 struct device_attribute *attr, char *buf)
430{
431 struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
432
433 return bma180_show_avail(buf, data->part_info->bw_table,
434 data->part_info->num_bw, false);
435}
436
437static ssize_t bma180_show_scale_avail(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
440 struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
441
442 return bma180_show_avail(buf, data->part_info->scale_table,
443 data->part_info->num_scales, true);
444}
445
446static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
447 S_IRUGO, bma180_show_filter_freq_avail, NULL, 0);
448
449static IIO_DEVICE_ATTR(in_accel_scale_available,
450 S_IRUGO, bma180_show_scale_avail, NULL, 0);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100451
452static struct attribute *bma180_attributes[] = {
Peter Meerwald402a3242014-08-19 23:43:00 +0100453 &iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.
454 dev_attr.attr,
455 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100456 NULL,
457};
458
459static const struct attribute_group bma180_attrs_group = {
460 .attrs = bma180_attributes,
461};
462
463static int bma180_read_raw(struct iio_dev *indio_dev,
464 struct iio_chan_spec const *chan, int *val, int *val2,
465 long mask)
466{
467 struct bma180_data *data = iio_priv(indio_dev);
468 int ret;
469
470 switch (mask) {
471 case IIO_CHAN_INFO_RAW:
472 mutex_lock(&data->mutex);
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100473 if (iio_buffer_enabled(indio_dev)) {
474 mutex_unlock(&data->mutex);
475 return -EBUSY;
476 }
477 ret = bma180_get_data_reg(data, chan->scan_index);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100478 mutex_unlock(&data->mutex);
479 if (ret < 0)
480 return ret;
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100481 *val = sign_extend32(ret >> chan->scan_type.shift,
482 chan->scan_type.realbits - 1);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100483 return IIO_VAL_INT;
484 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
485 *val = data->bw;
486 return IIO_VAL_INT;
487 case IIO_CHAN_INFO_SCALE:
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100488 switch (chan->type) {
489 case IIO_ACCEL:
490 *val = 0;
491 *val2 = data->scale;
492 return IIO_VAL_INT_PLUS_MICRO;
493 case IIO_TEMP:
494 *val = 500;
495 return IIO_VAL_INT;
496 default:
497 return -EINVAL;
498 }
499 case IIO_CHAN_INFO_OFFSET:
500 *val = 48; /* 0 LSB @ 24 degree C */
501 return IIO_VAL_INT;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100502 default:
503 return -EINVAL;
504 }
505}
506
507static int bma180_write_raw(struct iio_dev *indio_dev,
508 struct iio_chan_spec const *chan, int val, int val2, long mask)
509{
510 struct bma180_data *data = iio_priv(indio_dev);
511 int ret;
512
513 switch (mask) {
514 case IIO_CHAN_INFO_SCALE:
515 if (val)
516 return -EINVAL;
517 mutex_lock(&data->mutex);
518 ret = bma180_set_scale(data, val2);
519 mutex_unlock(&data->mutex);
520 return ret;
521 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
Peter Meerwald9b2a4d32014-07-16 19:32:00 +0100522 if (val2)
523 return -EINVAL;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100524 mutex_lock(&data->mutex);
525 ret = bma180_set_bw(data, val);
526 mutex_unlock(&data->mutex);
527 return ret;
528 default:
529 return -EINVAL;
530 }
531}
532
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100533static const struct iio_info bma180_info = {
534 .attrs = &bma180_attrs_group,
535 .read_raw = bma180_read_raw,
536 .write_raw = bma180_write_raw,
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100537 .driver_module = THIS_MODULE,
538};
539
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100540static const char * const bma180_power_modes[] = { "low_noise", "low_power" };
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100541
542static int bma180_get_power_mode(struct iio_dev *indio_dev,
543 const struct iio_chan_spec *chan)
544{
545 struct bma180_data *data = iio_priv(indio_dev);
546
547 return data->pmode;
548}
549
550static int bma180_set_power_mode(struct iio_dev *indio_dev,
551 const struct iio_chan_spec *chan, unsigned int mode)
552{
553 struct bma180_data *data = iio_priv(indio_dev);
554 int ret;
555
556 mutex_lock(&data->mutex);
557 ret = bma180_set_pmode(data, mode);
558 mutex_unlock(&data->mutex);
559
560 return ret;
561}
562
563static const struct iio_enum bma180_power_mode_enum = {
564 .items = bma180_power_modes,
565 .num_items = ARRAY_SIZE(bma180_power_modes),
566 .get = bma180_get_power_mode,
567 .set = bma180_set_power_mode,
568};
569
570static const struct iio_chan_spec_ext_info bma180_ext_info[] = {
571 IIO_ENUM("power_mode", true, &bma180_power_mode_enum),
572 IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum),
573 { },
574};
575
Peter Meerwald6377aa42014-08-19 23:43:00 +0100576#define BMA180_ACC_CHANNEL(_axis, _bits) { \
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100577 .type = IIO_ACCEL, \
Peter Meerwald55852152014-10-01 21:37:00 +0100578 .modified = 1, \
579 .channel2 = IIO_MOD_##_axis, \
Peter Meerwald5c1449d2014-10-01 21:37:00 +0100580 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
581 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100582 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
Peter Meerwald55852152014-10-01 21:37:00 +0100583 .scan_index = AXIS_##_axis, \
Jonathan Cameron7d7feae2013-12-11 18:45:00 +0000584 .scan_type = { \
585 .sign = 's', \
Peter Meerwald6377aa42014-08-19 23:43:00 +0100586 .realbits = _bits, \
Jonathan Cameron7d7feae2013-12-11 18:45:00 +0000587 .storagebits = 16, \
Peter Meerwald6377aa42014-08-19 23:43:00 +0100588 .shift = 16 - _bits, \
Jonathan Cameron7d7feae2013-12-11 18:45:00 +0000589 }, \
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100590 .ext_info = bma180_ext_info, \
591}
592
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100593#define BMA180_TEMP_CHANNEL { \
594 .type = IIO_TEMP, \
595 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
596 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \
597 .scan_index = TEMP, \
598 .scan_type = { \
599 .sign = 's', \
600 .realbits = 8, \
601 .storagebits = 16, \
602 }, \
603}
604
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100605static const struct iio_chan_spec bma180_channels[] = {
Peter Meerwald6377aa42014-08-19 23:43:00 +0100606 BMA180_ACC_CHANNEL(X, 14),
607 BMA180_ACC_CHANNEL(Y, 14),
608 BMA180_ACC_CHANNEL(Z, 14),
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100609 BMA180_TEMP_CHANNEL,
610 IIO_CHAN_SOFT_TIMESTAMP(4),
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100611};
612
Peter Meerwald2017cff2014-08-19 23:43:00 +0100613static const struct iio_chan_spec bma250_channels[] = {
614 BMA180_ACC_CHANNEL(X, 10),
615 BMA180_ACC_CHANNEL(Y, 10),
616 BMA180_ACC_CHANNEL(Z, 10),
617 BMA180_TEMP_CHANNEL,
618 IIO_CHAN_SOFT_TIMESTAMP(4),
619};
620
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100621static const struct bma180_part_info bma180_part_info[] = {
622 [BMA180] = {
623 bma180_channels, ARRAY_SIZE(bma180_channels),
624 bma180_scale_table, ARRAY_SIZE(bma180_scale_table),
625 bma180_bw_table, ARRAY_SIZE(bma180_bw_table),
Peter Meerwald2017cff2014-08-19 23:43:00 +0100626 BMA180_CTRL_REG0, BMA180_RESET_INT,
627 BMA180_CTRL_REG0, BMA180_SLEEP,
628 BMA180_BW_TCS, BMA180_BW,
629 BMA180_OFFSET_LSB1, BMA180_RANGE,
630 BMA180_TCO_Z, BMA180_MODE_CONFIG, BMA180_LOW_POWER,
631 BMA180_CTRL_REG3, BMA180_NEW_DATA_INT,
632 BMA180_RESET,
Peter Meerwaldc1949ec2014-08-19 23:43:00 +0100633 bma180_chip_config,
634 bma180_chip_disable,
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100635 },
Peter Meerwald2017cff2014-08-19 23:43:00 +0100636 [BMA250] = {
637 bma250_channels, ARRAY_SIZE(bma250_channels),
638 bma250_scale_table, ARRAY_SIZE(bma250_scale_table),
639 bma250_bw_table, ARRAY_SIZE(bma250_bw_table),
640 BMA250_INT_RESET_REG, BMA250_INT_RESET_MASK,
641 BMA250_POWER_REG, BMA250_SUSPEND_MASK,
642 BMA250_BW_REG, BMA250_BW_MASK,
643 BMA250_RANGE_REG, BMA250_RANGE_MASK,
644 BMA250_POWER_REG, BMA250_LOWPOWER_MASK, 1,
645 BMA250_INT_ENABLE_REG, BMA250_DATA_INTEN_MASK,
646 BMA250_RESET_REG,
647 bma250_chip_config,
648 bma250_chip_disable,
649 },
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100650};
651
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100652static irqreturn_t bma180_trigger_handler(int irq, void *p)
653{
654 struct iio_poll_func *pf = p;
655 struct iio_dev *indio_dev = pf->indio_dev;
656 struct bma180_data *data = iio_priv(indio_dev);
Lars-Peter Clausen6bf9d8772013-09-19 13:59:00 +0100657 int64_t time_ns = iio_get_time_ns();
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100658 int bit, ret, i = 0;
659
660 mutex_lock(&data->mutex);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100661
Octavian Purdila70dddee2015-03-02 21:03:05 +0200662 for_each_set_bit(bit, indio_dev->active_scan_mask,
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100663 indio_dev->masklength) {
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100664 ret = bma180_get_data_reg(data, bit);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100665 if (ret < 0) {
666 mutex_unlock(&data->mutex);
667 goto err;
668 }
669 ((s16 *)data->buff)[i++] = ret;
670 }
Peter Meerwaldb81fbab2014-08-19 23:43:00 +0100671
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100672 mutex_unlock(&data->mutex);
673
Lars-Peter Clausen6bf9d8772013-09-19 13:59:00 +0100674 iio_push_to_buffers_with_timestamp(indio_dev, data->buff, time_ns);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100675err:
676 iio_trigger_notify_done(indio_dev->trig);
677
678 return IRQ_HANDLED;
679}
680
681static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig,
682 bool state)
683{
684 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
685 struct bma180_data *data = iio_priv(indio_dev);
686
687 return bma180_set_new_data_intr_state(data, state);
688}
689
690static int bma180_trig_try_reen(struct iio_trigger *trig)
691{
692 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
693 struct bma180_data *data = iio_priv(indio_dev);
694
695 return bma180_reset_intr(data);
696}
697
698static const struct iio_trigger_ops bma180_trigger_ops = {
699 .set_trigger_state = bma180_data_rdy_trigger_set_state,
700 .try_reenable = bma180_trig_try_reen,
701 .owner = THIS_MODULE,
702};
703
704static int bma180_probe(struct i2c_client *client,
705 const struct i2c_device_id *id)
706{
707 struct bma180_data *data;
708 struct iio_dev *indio_dev;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100709 int ret;
710
711 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
712 if (!indio_dev)
713 return -ENOMEM;
714
715 data = iio_priv(indio_dev);
716 i2c_set_clientdata(client, indio_dev);
717 data->client = client;
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100718 data->part_info = &bma180_part_info[id->driver_data];
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100719
Peter Meerwaldc1949ec2014-08-19 23:43:00 +0100720 ret = data->part_info->chip_config(data);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100721 if (ret < 0)
722 goto err_chip_disable;
723
724 mutex_init(&data->mutex);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100725 indio_dev->dev.parent = &client->dev;
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100726 indio_dev->channels = data->part_info->channels;
727 indio_dev->num_channels = data->part_info->num_channels;
Peter Meerwald2017cff2014-08-19 23:43:00 +0100728 indio_dev->name = id->name;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100729 indio_dev->modes = INDIO_DIRECT_MODE;
730 indio_dev->info = &bma180_info;
731
Peter Meerwald16ed8692014-08-19 23:43:00 +0100732 if (client->irq > 0) {
733 data->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
734 indio_dev->id);
735 if (!data->trig) {
736 ret = -ENOMEM;
737 goto err_chip_disable;
738 }
739
740 ret = devm_request_irq(&client->dev, client->irq,
741 iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING,
Peter Meerwald2017cff2014-08-19 23:43:00 +0100742 "bma180_event", data->trig);
Peter Meerwald16ed8692014-08-19 23:43:00 +0100743 if (ret) {
744 dev_err(&client->dev, "unable to request IRQ\n");
745 goto err_trigger_free;
746 }
747
748 data->trig->dev.parent = &client->dev;
749 data->trig->ops = &bma180_trigger_ops;
750 iio_trigger_set_drvdata(data->trig, indio_dev);
Greg Kroah-Hartman8deab472014-09-22 07:54:10 -0700751 indio_dev->trig = iio_trigger_get(data->trig);
Peter Meerwald16ed8692014-08-19 23:43:00 +0100752
753 ret = iio_trigger_register(data->trig);
754 if (ret)
755 goto err_trigger_free;
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100756 }
757
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100758 ret = iio_triggered_buffer_setup(indio_dev, NULL,
759 bma180_trigger_handler, NULL);
760 if (ret < 0) {
761 dev_err(&client->dev, "unable to setup iio triggered buffer\n");
762 goto err_trigger_unregister;
763 }
764
765 ret = iio_device_register(indio_dev);
766 if (ret < 0) {
767 dev_err(&client->dev, "unable to register iio device\n");
768 goto err_buffer_cleanup;
769 }
770
771 return 0;
772
773err_buffer_cleanup:
774 iio_triggered_buffer_cleanup(indio_dev);
775err_trigger_unregister:
Peter Meerwald16ed8692014-08-19 23:43:00 +0100776 if (data->trig)
777 iio_trigger_unregister(data->trig);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100778err_trigger_free:
Peter Meerwald16ed8692014-08-19 23:43:00 +0100779 iio_trigger_free(data->trig);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100780err_chip_disable:
Peter Meerwaldc1949ec2014-08-19 23:43:00 +0100781 data->part_info->chip_disable(data);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100782
783 return ret;
784}
785
786static int bma180_remove(struct i2c_client *client)
787{
788 struct iio_dev *indio_dev = i2c_get_clientdata(client);
789 struct bma180_data *data = iio_priv(indio_dev);
790
791 iio_device_unregister(indio_dev);
792 iio_triggered_buffer_cleanup(indio_dev);
Peter Meerwald16ed8692014-08-19 23:43:00 +0100793 if (data->trig) {
794 iio_trigger_unregister(data->trig);
795 iio_trigger_free(data->trig);
796 }
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100797
798 mutex_lock(&data->mutex);
Peter Meerwaldc1949ec2014-08-19 23:43:00 +0100799 data->part_info->chip_disable(data);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100800 mutex_unlock(&data->mutex);
801
802 return 0;
803}
804
805#ifdef CONFIG_PM_SLEEP
806static int bma180_suspend(struct device *dev)
807{
Peter Meerwald234efa12013-09-18 22:47:00 +0100808 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100809 struct bma180_data *data = iio_priv(indio_dev);
810 int ret;
811
812 mutex_lock(&data->mutex);
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100813 ret = bma180_set_sleep_state(data, true);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100814 mutex_unlock(&data->mutex);
815
816 return ret;
817}
818
819static int bma180_resume(struct device *dev)
820{
Peter Meerwald234efa12013-09-18 22:47:00 +0100821 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100822 struct bma180_data *data = iio_priv(indio_dev);
823 int ret;
824
825 mutex_lock(&data->mutex);
Peter Meerwaldc7c69e82014-08-19 23:43:00 +0100826 ret = bma180_set_sleep_state(data, false);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100827 mutex_unlock(&data->mutex);
828
829 return ret;
830}
831
832static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
833#define BMA180_PM_OPS (&bma180_pm_ops)
834#else
835#define BMA180_PM_OPS NULL
836#endif
837
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100838static struct i2c_device_id bma180_ids[] = {
Peter Meerwald2017cff2014-08-19 23:43:00 +0100839 { "bma180", BMA180 },
840 { "bma250", BMA250 },
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100841 { }
842};
843
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100844MODULE_DEVICE_TABLE(i2c, bma180_ids);
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100845
846static struct i2c_driver bma180_driver = {
847 .driver = {
Peter Meerwald2017cff2014-08-19 23:43:00 +0100848 .name = "bma180",
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100849 .owner = THIS_MODULE,
850 .pm = BMA180_PM_OPS,
851 },
852 .probe = bma180_probe,
853 .remove = bma180_remove,
Peter Meerwald1b9030f2014-08-19 23:43:00 +0100854 .id_table = bma180_ids,
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100855};
856
857module_i2c_driver(bma180_driver);
858
859MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
860MODULE_AUTHOR("Texas Instruments, Inc.");
Peter Meerwald2017cff2014-08-19 23:43:00 +0100861MODULE_DESCRIPTION("Bosch BMA180/BMA250 triaxial acceleration sensor");
Oleksandr Kravchenkofdadbce2013-08-28 12:01:00 +0100862MODULE_LICENSE("GPL");