PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / iio / accel / bma180.c
blobbfec313492b3f0de16c8aa26bfb5b77ba9160473
1 /*
2 * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor
4 * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 */
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/delay.h>
15 #include <linux/of.h>
16 #include <linux/bitops.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
26 #define BMA180_DRV_NAME "bma180"
27 #define BMA180_IRQ_NAME "bma180_event"
29 /* Register set */
30 #define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */
31 #define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */
32 #define BMA180_CTRL_REG0 0x0d
33 #define BMA180_RESET 0x10
34 #define BMA180_BW_TCS 0x20
35 #define BMA180_CTRL_REG3 0x21
36 #define BMA180_TCO_Z 0x30
37 #define BMA180_OFFSET_LSB1 0x35
39 /* BMA180_CTRL_REG0 bits */
40 #define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */
41 #define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */
42 #define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */
43 #define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */
45 /* BMA180_CTRL_REG3 bits */
46 #define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */
48 /* BMA180_OFFSET_LSB1 skipping mode bit */
49 #define BMA180_SMP_SKIP BIT(0)
51 /* Bit masks for registers bit fields */
52 #define BMA180_RANGE 0x0e /* Range of measured accel values*/
53 #define BMA180_BW 0xf0 /* Accel bandwidth */
54 #define BMA180_MODE_CONFIG 0x03 /* Config operation modes */
56 /* We have to write this value in reset register to do soft reset */
57 #define BMA180_RESET_VAL 0xb6
59 #define BMA_180_ID_REG_VAL 0x03
61 /* Chip power modes */
62 #define BMA180_LOW_NOISE 0x00
63 #define BMA180_LOW_POWER 0x03
65 #define BMA180_LOW_NOISE_STR "low_noise"
66 #define BMA180_LOW_POWER_STR "low_power"
68 /* Defaults values */
69 #define BMA180_DEF_PMODE 0
70 #define BMA180_DEF_BW 20
71 #define BMA180_DEF_SCALE 250
73 /* Available values for sysfs */
74 #define BMA180_FLP_FREQ_AVAILABLE \
75 "10 20 40 75 150 300"
76 #define BMA180_SCALE_AVAILABLE \
77 "0.000130 0.000190 0.000250 0.000380 0.000500 0.000990 0.001980"
79 struct bma180_data {
80 struct i2c_client *client;
81 struct iio_trigger *trig;
82 struct mutex mutex;
83 int sleep_state;
84 int scale;
85 int bw;
86 int pmode;
87 char *buff;
90 enum bma180_axis {
91 AXIS_X,
92 AXIS_Y,
93 AXIS_Z,
96 static int bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
97 static int scale_table[] = { 130, 190, 250, 380, 500, 990, 1980 };
99 static int bma180_get_acc_reg(struct bma180_data *data, enum bma180_axis axis)
101 u8 reg = BMA180_ACC_X_LSB + axis * 2;
102 int ret;
104 if (data->sleep_state)
105 return -EBUSY;
107 ret = i2c_smbus_read_word_data(data->client, reg);
108 if (ret < 0)
109 dev_err(&data->client->dev,
110 "failed to read accel_%c registers\n", 'x' + axis);
112 return ret;
115 static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
117 int ret = i2c_smbus_read_byte_data(data->client, reg);
118 u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1));
120 if (ret < 0)
121 return ret;
123 return i2c_smbus_write_byte_data(data->client, reg, reg_val);
126 static int bma180_reset_intr(struct bma180_data *data)
128 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_RESET_INT, 1);
130 if (ret)
131 dev_err(&data->client->dev, "failed to reset interrupt\n");
133 return ret;
136 static int bma180_set_new_data_intr_state(struct bma180_data *data, int state)
138 u8 reg_val = state ? BMA180_NEW_DATA_INT : 0x00;
139 int ret = i2c_smbus_write_byte_data(data->client, BMA180_CTRL_REG3,
140 reg_val);
142 if (ret)
143 goto err;
144 ret = bma180_reset_intr(data);
145 if (ret)
146 goto err;
148 return 0;
150 err:
151 dev_err(&data->client->dev,
152 "failed to set new data interrupt state %d\n", state);
153 return ret;
156 static int bma180_set_sleep_state(struct bma180_data *data, int state)
158 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_SLEEP, state);
160 if (ret) {
161 dev_err(&data->client->dev,
162 "failed to set sleep state %d\n", state);
163 return ret;
165 data->sleep_state = state;
167 return 0;
170 static int bma180_set_ee_writing_state(struct bma180_data *data, int state)
172 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state);
174 if (ret)
175 dev_err(&data->client->dev,
176 "failed to set ee writing state %d\n", state);
178 return ret;
181 static int bma180_set_bw(struct bma180_data *data, int val)
183 int ret, i;
185 if (data->sleep_state)
186 return -EBUSY;
188 for (i = 0; i < ARRAY_SIZE(bw_table); ++i) {
189 if (bw_table[i] == val) {
190 ret = bma180_set_bits(data,
191 BMA180_BW_TCS, BMA180_BW, i);
192 if (ret) {
193 dev_err(&data->client->dev,
194 "failed to set bandwidth\n");
195 return ret;
197 data->bw = val;
198 return 0;
202 return -EINVAL;
205 static int bma180_set_scale(struct bma180_data *data, int val)
207 int ret, i;
209 if (data->sleep_state)
210 return -EBUSY;
212 for (i = 0; i < ARRAY_SIZE(scale_table); ++i)
213 if (scale_table[i] == val) {
214 ret = bma180_set_bits(data,
215 BMA180_OFFSET_LSB1, BMA180_RANGE, i);
216 if (ret) {
217 dev_err(&data->client->dev,
218 "failed to set scale\n");
219 return ret;
221 data->scale = val;
222 return 0;
225 return -EINVAL;
228 static int bma180_set_pmode(struct bma180_data *data, int mode)
230 u8 reg_val = mode ? BMA180_LOW_POWER : BMA180_LOW_NOISE;
231 int ret = bma180_set_bits(data, BMA180_TCO_Z, BMA180_MODE_CONFIG,
232 reg_val);
234 if (ret) {
235 dev_err(&data->client->dev, "failed to set power mode\n");
236 return ret;
238 data->pmode = mode;
240 return 0;
243 static int bma180_soft_reset(struct bma180_data *data)
245 int ret = i2c_smbus_write_byte_data(data->client,
246 BMA180_RESET, BMA180_RESET_VAL);
248 if (ret)
249 dev_err(&data->client->dev, "failed to reset the chip\n");
251 return ret;
254 static int bma180_chip_init(struct bma180_data *data)
256 /* Try to read chip_id register. It must return 0x03. */
257 int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID);
259 if (ret < 0)
260 goto err;
261 if (ret != BMA_180_ID_REG_VAL) {
262 ret = -ENODEV;
263 goto err;
266 ret = bma180_soft_reset(data);
267 if (ret)
268 goto err;
270 * No serial transaction should occur within minimum 10 us
271 * after soft_reset command
273 msleep(20);
275 ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1);
276 if (ret)
277 goto err;
278 ret = bma180_set_ee_writing_state(data, 1);
279 if (ret)
280 goto err;
281 ret = bma180_set_new_data_intr_state(data, 0);
282 if (ret)
283 goto err;
284 ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
285 if (ret)
286 goto err;
287 ret = bma180_set_pmode(data, BMA180_DEF_PMODE);
288 if (ret)
289 goto err;
290 ret = bma180_set_bw(data, BMA180_DEF_BW);
291 if (ret)
292 goto err;
293 ret = bma180_set_scale(data, BMA180_DEF_SCALE);
294 if (ret)
295 goto err;
297 return 0;
299 err:
300 dev_err(&data->client->dev, "failed to init the chip\n");
301 return ret;
304 static void bma180_chip_disable(struct bma180_data *data)
306 if (bma180_set_new_data_intr_state(data, 0))
307 goto err;
308 if (bma180_set_ee_writing_state(data, 0))
309 goto err;
310 if (bma180_set_sleep_state(data, 1))
311 goto err;
313 return;
315 err:
316 dev_err(&data->client->dev, "failed to disable the chip\n");
319 static IIO_CONST_ATTR(in_accel_filter_low_pass_3db_frequency_available,
320 BMA180_FLP_FREQ_AVAILABLE);
321 static IIO_CONST_ATTR(in_accel_scale_available, BMA180_SCALE_AVAILABLE);
323 static struct attribute *bma180_attributes[] = {
324 &iio_const_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr,
325 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
326 NULL,
329 static const struct attribute_group bma180_attrs_group = {
330 .attrs = bma180_attributes,
333 static int bma180_read_raw(struct iio_dev *indio_dev,
334 struct iio_chan_spec const *chan, int *val, int *val2,
335 long mask)
337 struct bma180_data *data = iio_priv(indio_dev);
338 int ret;
340 switch (mask) {
341 case IIO_CHAN_INFO_RAW:
342 mutex_lock(&data->mutex);
343 if (iio_buffer_enabled(indio_dev))
344 ret = -EBUSY;
345 else
346 ret = bma180_get_acc_reg(data, chan->scan_index);
347 mutex_unlock(&data->mutex);
348 if (ret < 0)
349 return ret;
350 *val = (s16)ret >> chan->scan_type.shift;
351 return IIO_VAL_INT;
352 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
353 *val = data->bw;
354 return IIO_VAL_INT;
355 case IIO_CHAN_INFO_SCALE:
356 *val = 0;
357 *val2 = data->scale;
358 return IIO_VAL_INT_PLUS_MICRO;
359 default:
360 return -EINVAL;
364 static int bma180_write_raw(struct iio_dev *indio_dev,
365 struct iio_chan_spec const *chan, int val, int val2, long mask)
367 struct bma180_data *data = iio_priv(indio_dev);
368 int ret;
370 switch (mask) {
371 case IIO_CHAN_INFO_SCALE:
372 if (val)
373 return -EINVAL;
374 mutex_lock(&data->mutex);
375 ret = bma180_set_scale(data, val2);
376 mutex_unlock(&data->mutex);
377 return ret;
378 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
379 mutex_lock(&data->mutex);
380 ret = bma180_set_bw(data, val);
381 mutex_unlock(&data->mutex);
382 return ret;
383 default:
384 return -EINVAL;
388 static int bma180_update_scan_mode(struct iio_dev *indio_dev,
389 const unsigned long *scan_mask)
391 struct bma180_data *data = iio_priv(indio_dev);
393 if (data->buff)
394 devm_kfree(&indio_dev->dev, data->buff);
395 data->buff = devm_kzalloc(&indio_dev->dev,
396 indio_dev->scan_bytes, GFP_KERNEL);
397 if (!data->buff)
398 return -ENOMEM;
400 return 0;
403 static const struct iio_info bma180_info = {
404 .attrs = &bma180_attrs_group,
405 .read_raw = bma180_read_raw,
406 .write_raw = bma180_write_raw,
407 .update_scan_mode = bma180_update_scan_mode,
408 .driver_module = THIS_MODULE,
411 static const char * const bma180_power_modes[] = {
412 BMA180_LOW_NOISE_STR,
413 BMA180_LOW_POWER_STR,
416 static int bma180_get_power_mode(struct iio_dev *indio_dev,
417 const struct iio_chan_spec *chan)
419 struct bma180_data *data = iio_priv(indio_dev);
421 return data->pmode;
424 static int bma180_set_power_mode(struct iio_dev *indio_dev,
425 const struct iio_chan_spec *chan, unsigned int mode)
427 struct bma180_data *data = iio_priv(indio_dev);
428 int ret;
430 mutex_lock(&data->mutex);
431 ret = bma180_set_pmode(data, mode);
432 mutex_unlock(&data->mutex);
434 return ret;
437 static const struct iio_enum bma180_power_mode_enum = {
438 .items = bma180_power_modes,
439 .num_items = ARRAY_SIZE(bma180_power_modes),
440 .get = bma180_get_power_mode,
441 .set = bma180_set_power_mode,
444 static const struct iio_chan_spec_ext_info bma180_ext_info[] = {
445 IIO_ENUM("power_mode", true, &bma180_power_mode_enum),
446 IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum),
447 { },
450 #define BMA180_CHANNEL(_axis) { \
451 .type = IIO_ACCEL, \
452 .modified = 1, \
453 .channel2 = IIO_MOD_##_axis, \
454 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
455 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
456 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
457 .scan_index = AXIS_##_axis, \
458 .scan_type = { \
459 .sign = 's', \
460 .realbits = 14, \
461 .storagebits = 16, \
462 .shift = 2, \
463 }, \
464 .ext_info = bma180_ext_info, \
467 static const struct iio_chan_spec bma180_channels[] = {
468 BMA180_CHANNEL(X),
469 BMA180_CHANNEL(Y),
470 BMA180_CHANNEL(Z),
471 IIO_CHAN_SOFT_TIMESTAMP(3),
474 static irqreturn_t bma180_trigger_handler(int irq, void *p)
476 struct iio_poll_func *pf = p;
477 struct iio_dev *indio_dev = pf->indio_dev;
478 struct bma180_data *data = iio_priv(indio_dev);
479 int64_t time_ns = iio_get_time_ns();
480 int bit, ret, i = 0;
482 mutex_lock(&data->mutex);
484 for_each_set_bit(bit, indio_dev->buffer->scan_mask,
485 indio_dev->masklength) {
486 ret = bma180_get_acc_reg(data, bit);
487 if (ret < 0) {
488 mutex_unlock(&data->mutex);
489 goto err;
491 ((s16 *)data->buff)[i++] = ret;
493 mutex_unlock(&data->mutex);
495 iio_push_to_buffers_with_timestamp(indio_dev, data->buff, time_ns);
496 err:
497 iio_trigger_notify_done(indio_dev->trig);
499 return IRQ_HANDLED;
502 static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig,
503 bool state)
505 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
506 struct bma180_data *data = iio_priv(indio_dev);
508 return bma180_set_new_data_intr_state(data, state);
511 static int bma180_trig_try_reen(struct iio_trigger *trig)
513 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
514 struct bma180_data *data = iio_priv(indio_dev);
516 return bma180_reset_intr(data);
519 static const struct iio_trigger_ops bma180_trigger_ops = {
520 .set_trigger_state = bma180_data_rdy_trigger_set_state,
521 .try_reenable = bma180_trig_try_reen,
522 .owner = THIS_MODULE,
525 static int bma180_probe(struct i2c_client *client,
526 const struct i2c_device_id *id)
528 struct bma180_data *data;
529 struct iio_dev *indio_dev;
530 struct iio_trigger *trig;
531 int ret;
533 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
534 if (!indio_dev)
535 return -ENOMEM;
537 data = iio_priv(indio_dev);
538 i2c_set_clientdata(client, indio_dev);
539 data->client = client;
541 ret = bma180_chip_init(data);
542 if (ret < 0)
543 goto err_chip_disable;
545 mutex_init(&data->mutex);
547 indio_dev->dev.parent = &client->dev;
548 indio_dev->channels = bma180_channels;
549 indio_dev->num_channels = ARRAY_SIZE(bma180_channels);
550 indio_dev->name = BMA180_DRV_NAME;
551 indio_dev->modes = INDIO_DIRECT_MODE;
552 indio_dev->info = &bma180_info;
554 trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, indio_dev->id);
555 if (!trig) {
556 ret = -ENOMEM;
557 goto err_chip_disable;
560 ret = devm_request_irq(&client->dev, client->irq,
561 iio_trigger_generic_data_rdy_poll,
562 IRQF_TRIGGER_RISING, BMA180_IRQ_NAME, trig);
563 if (ret) {
564 dev_err(&client->dev, "unable to request IRQ\n");
565 goto err_trigger_free;
568 trig->dev.parent = &client->dev;
569 trig->ops = &bma180_trigger_ops;
570 iio_trigger_set_drvdata(trig, indio_dev);
571 data->trig = trig;
572 indio_dev->trig = trig;
574 ret = iio_trigger_register(trig);
575 if (ret)
576 goto err_trigger_free;
578 ret = iio_triggered_buffer_setup(indio_dev, NULL,
579 bma180_trigger_handler, NULL);
580 if (ret < 0) {
581 dev_err(&client->dev, "unable to setup iio triggered buffer\n");
582 goto err_trigger_unregister;
585 ret = iio_device_register(indio_dev);
586 if (ret < 0) {
587 dev_err(&client->dev, "unable to register iio device\n");
588 goto err_buffer_cleanup;
591 return 0;
593 err_buffer_cleanup:
594 iio_triggered_buffer_cleanup(indio_dev);
595 err_trigger_unregister:
596 iio_trigger_unregister(trig);
597 err_trigger_free:
598 iio_trigger_free(trig);
599 err_chip_disable:
600 bma180_chip_disable(data);
602 return ret;
605 static int bma180_remove(struct i2c_client *client)
607 struct iio_dev *indio_dev = i2c_get_clientdata(client);
608 struct bma180_data *data = iio_priv(indio_dev);
610 iio_device_unregister(indio_dev);
611 iio_triggered_buffer_cleanup(indio_dev);
612 iio_trigger_unregister(data->trig);
613 iio_trigger_free(data->trig);
615 mutex_lock(&data->mutex);
616 bma180_chip_disable(data);
617 mutex_unlock(&data->mutex);
619 return 0;
622 #ifdef CONFIG_PM_SLEEP
623 static int bma180_suspend(struct device *dev)
625 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
626 struct bma180_data *data = iio_priv(indio_dev);
627 int ret;
629 mutex_lock(&data->mutex);
630 ret = bma180_set_sleep_state(data, 1);
631 mutex_unlock(&data->mutex);
633 return ret;
636 static int bma180_resume(struct device *dev)
638 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
639 struct bma180_data *data = iio_priv(indio_dev);
640 int ret;
642 mutex_lock(&data->mutex);
643 ret = bma180_set_sleep_state(data, 0);
644 mutex_unlock(&data->mutex);
646 return ret;
649 static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
650 #define BMA180_PM_OPS (&bma180_pm_ops)
651 #else
652 #define BMA180_PM_OPS NULL
653 #endif
655 static struct i2c_device_id bma180_id[] = {
656 { BMA180_DRV_NAME, 0 },
660 MODULE_DEVICE_TABLE(i2c, bma180_id);
662 static struct i2c_driver bma180_driver = {
663 .driver = {
664 .name = BMA180_DRV_NAME,
665 .owner = THIS_MODULE,
666 .pm = BMA180_PM_OPS,
668 .probe = bma180_probe,
669 .remove = bma180_remove,
670 .id_table = bma180_id,
673 module_i2c_driver(bma180_driver);
675 MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
676 MODULE_AUTHOR("Texas Instruments, Inc.");
677 MODULE_DESCRIPTION("Bosch BMA180 triaxial acceleration sensor");
678 MODULE_LICENSE("GPL");