itg3200_buffer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * itg3200_buffer.c -- support InvenSense ITG3200
  3. * Digital 3-Axis Gyroscope driver
  4. *
  5. * Copyright (c) 2011 Christian Strobel <christian.strobel@iis.fraunhofer.de>
  6. * Copyright (c) 2011 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
  7. * Copyright (c) 2012 Thorsten Nowak <thorsten.nowak@iis.fraunhofer.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/trigger.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. #include <linux/iio/triggered_buffer.h>
  21. #include <linux/iio/gyro/itg3200.h>
  22. static int itg3200_read_all_channels(struct i2c_client *i2c, __be16 *buf)
  23. {
  24. u8 tx = 0x80 | ITG3200_REG_TEMP_OUT_H;
  25. struct i2c_msg msg[2] = {
  26. {
  27. .addr = i2c->addr,
  28. .flags = i2c->flags,
  29. .len = 1,
  30. .buf = &tx,
  31. },
  32. {
  33. .addr = i2c->addr,
  34. .flags = i2c->flags | I2C_M_RD,
  35. .len = ITG3200_SCAN_ELEMENTS * sizeof(s16),
  36. .buf = (char *)&buf,
  37. },
  38. };
  39. return i2c_transfer(i2c->adapter, msg, 2);
  40. }
  41. static irqreturn_t itg3200_trigger_handler(int irq, void *p)
  42. {
  43. struct iio_poll_func *pf = p;
  44. struct iio_dev *indio_dev = pf->indio_dev;
  45. struct itg3200 *st = iio_priv(indio_dev);
  46. /*
  47. * Ensure correct alignment and padding including for the
  48. * timestamp that may be inserted.
  49. */
  50. struct {
  51. __be16 buf[ITG3200_SCAN_ELEMENTS];
  52. s64 ts __aligned(8);
  53. } scan;
  54. int ret = itg3200_read_all_channels(st->i2c, scan.buf);
  55. if (ret < 0)
  56. goto error_ret;
  57. iio_push_to_buffers_with_timestamp(indio_dev, &scan, pf->timestamp);
  58. iio_trigger_notify_done(indio_dev->trig);
  59. error_ret:
  60. return IRQ_HANDLED;
  61. }
  62. int itg3200_buffer_configure(struct iio_dev *indio_dev)
  63. {
  64. return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  65. itg3200_trigger_handler, NULL);
  66. }
  67. void itg3200_buffer_unconfigure(struct iio_dev *indio_dev)
  68. {
  69. iio_triggered_buffer_cleanup(indio_dev);
  70. }
  71. static int itg3200_data_rdy_trigger_set_state(struct iio_trigger *trig,
  72. bool state)
  73. {
  74. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  75. int ret;
  76. u8 msc;
  77. ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, &msc);
  78. if (ret)
  79. goto error_ret;
  80. if (state)
  81. msc |= ITG3200_IRQ_DATA_RDY_ENABLE;
  82. else
  83. msc &= ~ITG3200_IRQ_DATA_RDY_ENABLE;
  84. ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, msc);
  85. if (ret)
  86. goto error_ret;
  87. error_ret:
  88. return ret;
  89. }
  90. static const struct iio_trigger_ops itg3200_trigger_ops = {
  91. .owner = THIS_MODULE,
  92. .set_trigger_state = &itg3200_data_rdy_trigger_set_state,
  93. };
  94. int itg3200_probe_trigger(struct iio_dev *indio_dev)
  95. {
  96. int ret;
  97. struct itg3200 *st = iio_priv(indio_dev);
  98. st->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
  99. indio_dev->id);
  100. if (!st->trig)
  101. return -ENOMEM;
  102. ret = request_irq(st->i2c->irq,
  103. &iio_trigger_generic_data_rdy_poll,
  104. IRQF_TRIGGER_RISING,
  105. "itg3200_data_rdy",
  106. st->trig);
  107. if (ret)
  108. goto error_free_trig;
  109. st->trig->dev.parent = &st->i2c->dev;
  110. st->trig->ops = &itg3200_trigger_ops;
  111. iio_trigger_set_drvdata(st->trig, indio_dev);
  112. ret = iio_trigger_register(st->trig);
  113. if (ret)
  114. goto error_free_irq;
  115. /* select default trigger */
  116. indio_dev->trig = iio_trigger_get(st->trig);
  117. return 0;
  118. error_free_irq:
  119. free_irq(st->i2c->irq, st->trig);
  120. error_free_trig:
  121. iio_trigger_free(st->trig);
  122. return ret;
  123. }
  124. void itg3200_remove_trigger(struct iio_dev *indio_dev)
  125. {
  126. struct itg3200 *st = iio_priv(indio_dev);
  127. iio_trigger_unregister(st->trig);
  128. free_irq(st->i2c->irq, st->trig);
  129. iio_trigger_free(st->trig);
  130. }