itg3200_buffer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. __be16 buf[ITG3200_SCAN_ELEMENTS + sizeof(s64)/sizeof(u16)];
  47. int ret = itg3200_read_all_channels(st->i2c, buf);
  48. if (ret < 0)
  49. goto error_ret;
  50. if (indio_dev->scan_timestamp)
  51. memcpy(buf + indio_dev->scan_bytes - sizeof(s64),
  52. &pf->timestamp, sizeof(pf->timestamp));
  53. iio_push_to_buffers(indio_dev, (u8 *)buf);
  54. iio_trigger_notify_done(indio_dev->trig);
  55. error_ret:
  56. return IRQ_HANDLED;
  57. }
  58. int itg3200_buffer_configure(struct iio_dev *indio_dev)
  59. {
  60. return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  61. itg3200_trigger_handler, NULL);
  62. }
  63. void itg3200_buffer_unconfigure(struct iio_dev *indio_dev)
  64. {
  65. iio_triggered_buffer_cleanup(indio_dev);
  66. }
  67. static int itg3200_data_rdy_trigger_set_state(struct iio_trigger *trig,
  68. bool state)
  69. {
  70. struct iio_dev *indio_dev = trig->private_data;
  71. int ret;
  72. u8 msc;
  73. ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, &msc);
  74. if (ret)
  75. goto error_ret;
  76. if (state)
  77. msc |= ITG3200_IRQ_DATA_RDY_ENABLE;
  78. else
  79. msc &= ~ITG3200_IRQ_DATA_RDY_ENABLE;
  80. ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_IRQ_CONFIG, msc);
  81. if (ret)
  82. goto error_ret;
  83. error_ret:
  84. return ret;
  85. }
  86. static const struct iio_trigger_ops itg3200_trigger_ops = {
  87. .owner = THIS_MODULE,
  88. .set_trigger_state = &itg3200_data_rdy_trigger_set_state,
  89. };
  90. int itg3200_probe_trigger(struct iio_dev *indio_dev)
  91. {
  92. int ret;
  93. struct itg3200 *st = iio_priv(indio_dev);
  94. st->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
  95. indio_dev->id);
  96. if (!st->trig)
  97. return -ENOMEM;
  98. ret = request_irq(st->i2c->irq,
  99. &iio_trigger_generic_data_rdy_poll,
  100. IRQF_TRIGGER_RISING,
  101. "itg3200_data_rdy",
  102. st->trig);
  103. if (ret)
  104. goto error_free_trig;
  105. st->trig->dev.parent = &st->i2c->dev;
  106. st->trig->ops = &itg3200_trigger_ops;
  107. st->trig->private_data = indio_dev;
  108. ret = iio_trigger_register(st->trig);
  109. if (ret)
  110. goto error_free_irq;
  111. /* select default trigger */
  112. indio_dev->trig = st->trig;
  113. return 0;
  114. error_free_irq:
  115. free_irq(st->i2c->irq, st->trig);
  116. error_free_trig:
  117. iio_trigger_free(st->trig);
  118. return ret;
  119. }
  120. void itg3200_remove_trigger(struct iio_dev *indio_dev)
  121. {
  122. struct itg3200 *st = iio_priv(indio_dev);
  123. iio_trigger_unregister(st->trig);
  124. free_irq(st->i2c->irq, st->trig);
  125. iio_trigger_free(st->trig);
  126. }