itg3200_buffer.c 3.5 KB

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