ti-adc081c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
  3. *
  4. * Copyright (C) 2012 Avionic Design GmbH
  5. * Copyright (C) 2016 Intel
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Datasheets:
  12. * http://www.ti.com/lit/ds/symlink/adc081c021.pdf
  13. * http://www.ti.com/lit/ds/symlink/adc101c021.pdf
  14. * http://www.ti.com/lit/ds/symlink/adc121c021.pdf
  15. *
  16. * The devices have a very similar interface and differ mostly in the number of
  17. * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2
  18. * bits of value registers are reserved.
  19. */
  20. #include <linux/err.h>
  21. #include <linux/i2c.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/acpi.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/trigger_consumer.h>
  28. #include <linux/iio/triggered_buffer.h>
  29. #include <linux/regulator/consumer.h>
  30. struct adc081c {
  31. struct i2c_client *i2c;
  32. struct regulator *ref;
  33. /* 8, 10 or 12 */
  34. int bits;
  35. /* Ensure natural alignment of buffer elements */
  36. struct {
  37. u16 channel;
  38. s64 ts __aligned(8);
  39. } scan;
  40. };
  41. #define REG_CONV_RES 0x00
  42. static int adc081c_read_raw(struct iio_dev *iio,
  43. struct iio_chan_spec const *channel, int *value,
  44. int *shift, long mask)
  45. {
  46. struct adc081c *adc = iio_priv(iio);
  47. int err;
  48. switch (mask) {
  49. case IIO_CHAN_INFO_RAW:
  50. err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
  51. if (err < 0)
  52. return err;
  53. *value = (err & 0xFFF) >> (12 - adc->bits);
  54. return IIO_VAL_INT;
  55. case IIO_CHAN_INFO_SCALE:
  56. err = regulator_get_voltage(adc->ref);
  57. if (err < 0)
  58. return err;
  59. *value = err / 1000;
  60. *shift = adc->bits;
  61. return IIO_VAL_FRACTIONAL_LOG2;
  62. default:
  63. break;
  64. }
  65. return -EINVAL;
  66. }
  67. #define ADCxx1C_CHAN(_bits) { \
  68. .type = IIO_VOLTAGE, \
  69. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  70. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  71. .scan_type = { \
  72. .sign = 'u', \
  73. .realbits = (_bits), \
  74. .storagebits = 16, \
  75. .shift = 12 - (_bits), \
  76. .endianness = IIO_CPU, \
  77. }, \
  78. }
  79. #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \
  80. static const struct iio_chan_spec _name ## _channels[] = { \
  81. ADCxx1C_CHAN((_bits)), \
  82. IIO_CHAN_SOFT_TIMESTAMP(1), \
  83. }; \
  84. #define ADC081C_NUM_CHANNELS 2
  85. struct adcxx1c_model {
  86. const struct iio_chan_spec* channels;
  87. int bits;
  88. };
  89. #define ADCxx1C_MODEL(_name, _bits) \
  90. { \
  91. .channels = _name ## _channels, \
  92. .bits = (_bits), \
  93. }
  94. DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
  95. DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
  96. DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
  97. /* Model ids are indexes in _models array */
  98. enum adcxx1c_model_id {
  99. ADC081C = 0,
  100. ADC101C = 1,
  101. ADC121C = 2,
  102. };
  103. static struct adcxx1c_model adcxx1c_models[] = {
  104. ADCxx1C_MODEL(adc081c, 8),
  105. ADCxx1C_MODEL(adc101c, 10),
  106. ADCxx1C_MODEL(adc121c, 12),
  107. };
  108. static const struct iio_info adc081c_info = {
  109. .read_raw = adc081c_read_raw,
  110. };
  111. static irqreturn_t adc081c_trigger_handler(int irq, void *p)
  112. {
  113. struct iio_poll_func *pf = p;
  114. struct iio_dev *indio_dev = pf->indio_dev;
  115. struct adc081c *data = iio_priv(indio_dev);
  116. int ret;
  117. ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
  118. if (ret < 0)
  119. goto out;
  120. data->scan.channel = ret;
  121. iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
  122. iio_get_time_ns(indio_dev));
  123. out:
  124. iio_trigger_notify_done(indio_dev->trig);
  125. return IRQ_HANDLED;
  126. }
  127. static int adc081c_probe(struct i2c_client *client,
  128. const struct i2c_device_id *id)
  129. {
  130. struct iio_dev *iio;
  131. struct adc081c *adc;
  132. struct adcxx1c_model *model;
  133. int err;
  134. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
  135. return -EOPNOTSUPP;
  136. if (ACPI_COMPANION(&client->dev)) {
  137. const struct acpi_device_id *ad_id;
  138. ad_id = acpi_match_device(client->dev.driver->acpi_match_table,
  139. &client->dev);
  140. if (!ad_id)
  141. return -ENODEV;
  142. model = &adcxx1c_models[ad_id->driver_data];
  143. } else {
  144. model = &adcxx1c_models[id->driver_data];
  145. }
  146. iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
  147. if (!iio)
  148. return -ENOMEM;
  149. adc = iio_priv(iio);
  150. adc->i2c = client;
  151. adc->bits = model->bits;
  152. adc->ref = devm_regulator_get(&client->dev, "vref");
  153. if (IS_ERR(adc->ref))
  154. return PTR_ERR(adc->ref);
  155. err = regulator_enable(adc->ref);
  156. if (err < 0)
  157. return err;
  158. iio->dev.parent = &client->dev;
  159. iio->dev.of_node = client->dev.of_node;
  160. iio->name = dev_name(&client->dev);
  161. iio->modes = INDIO_DIRECT_MODE;
  162. iio->info = &adc081c_info;
  163. iio->channels = model->channels;
  164. iio->num_channels = ADC081C_NUM_CHANNELS;
  165. err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL);
  166. if (err < 0) {
  167. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  168. goto err_regulator_disable;
  169. }
  170. err = iio_device_register(iio);
  171. if (err < 0)
  172. goto err_buffer_cleanup;
  173. i2c_set_clientdata(client, iio);
  174. return 0;
  175. err_buffer_cleanup:
  176. iio_triggered_buffer_cleanup(iio);
  177. err_regulator_disable:
  178. regulator_disable(adc->ref);
  179. return err;
  180. }
  181. static int adc081c_remove(struct i2c_client *client)
  182. {
  183. struct iio_dev *iio = i2c_get_clientdata(client);
  184. struct adc081c *adc = iio_priv(iio);
  185. iio_device_unregister(iio);
  186. iio_triggered_buffer_cleanup(iio);
  187. regulator_disable(adc->ref);
  188. return 0;
  189. }
  190. static const struct i2c_device_id adc081c_id[] = {
  191. { "adc081c", ADC081C },
  192. { "adc101c", ADC101C },
  193. { "adc121c", ADC121C },
  194. { }
  195. };
  196. MODULE_DEVICE_TABLE(i2c, adc081c_id);
  197. #ifdef CONFIG_OF
  198. static const struct of_device_id adc081c_of_match[] = {
  199. { .compatible = "ti,adc081c" },
  200. { .compatible = "ti,adc101c" },
  201. { .compatible = "ti,adc121c" },
  202. { }
  203. };
  204. MODULE_DEVICE_TABLE(of, adc081c_of_match);
  205. #endif
  206. #ifdef CONFIG_ACPI
  207. static const struct acpi_device_id adc081c_acpi_match[] = {
  208. { "ADC081C", ADC081C },
  209. { "ADC101C", ADC101C },
  210. { "ADC121C", ADC121C },
  211. { }
  212. };
  213. MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match);
  214. #endif
  215. static struct i2c_driver adc081c_driver = {
  216. .driver = {
  217. .name = "adc081c",
  218. .of_match_table = of_match_ptr(adc081c_of_match),
  219. .acpi_match_table = ACPI_PTR(adc081c_acpi_match),
  220. },
  221. .probe = adc081c_probe,
  222. .remove = adc081c_remove,
  223. .id_table = adc081c_id,
  224. };
  225. module_i2c_driver(adc081c_driver);
  226. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  227. MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
  228. MODULE_LICENSE("GPL v2");