ti-adc081c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. };
  36. #define REG_CONV_RES 0x00
  37. static int adc081c_read_raw(struct iio_dev *iio,
  38. struct iio_chan_spec const *channel, int *value,
  39. int *shift, long mask)
  40. {
  41. struct adc081c *adc = iio_priv(iio);
  42. int err;
  43. switch (mask) {
  44. case IIO_CHAN_INFO_RAW:
  45. err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
  46. if (err < 0)
  47. return err;
  48. *value = (err & 0xFFF) >> (12 - adc->bits);
  49. return IIO_VAL_INT;
  50. case IIO_CHAN_INFO_SCALE:
  51. err = regulator_get_voltage(adc->ref);
  52. if (err < 0)
  53. return err;
  54. *value = err / 1000;
  55. *shift = adc->bits;
  56. return IIO_VAL_FRACTIONAL_LOG2;
  57. default:
  58. break;
  59. }
  60. return -EINVAL;
  61. }
  62. #define ADCxx1C_CHAN(_bits) { \
  63. .type = IIO_VOLTAGE, \
  64. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  65. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  66. .scan_type = { \
  67. .sign = 'u', \
  68. .realbits = (_bits), \
  69. .storagebits = 16, \
  70. .shift = 12 - (_bits), \
  71. .endianness = IIO_CPU, \
  72. }, \
  73. }
  74. #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \
  75. static const struct iio_chan_spec _name ## _channels[] = { \
  76. ADCxx1C_CHAN((_bits)), \
  77. IIO_CHAN_SOFT_TIMESTAMP(1), \
  78. }; \
  79. #define ADC081C_NUM_CHANNELS 2
  80. struct adcxx1c_model {
  81. const struct iio_chan_spec* channels;
  82. int bits;
  83. };
  84. #define ADCxx1C_MODEL(_name, _bits) \
  85. { \
  86. .channels = _name ## _channels, \
  87. .bits = (_bits), \
  88. }
  89. DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
  90. DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
  91. DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
  92. /* Model ids are indexes in _models array */
  93. enum adcxx1c_model_id {
  94. ADC081C = 0,
  95. ADC101C = 1,
  96. ADC121C = 2,
  97. };
  98. static struct adcxx1c_model adcxx1c_models[] = {
  99. ADCxx1C_MODEL(adc081c, 8),
  100. ADCxx1C_MODEL(adc101c, 10),
  101. ADCxx1C_MODEL(adc121c, 12),
  102. };
  103. static const struct iio_info adc081c_info = {
  104. .read_raw = adc081c_read_raw,
  105. .driver_module = THIS_MODULE,
  106. };
  107. static irqreturn_t adc081c_trigger_handler(int irq, void *p)
  108. {
  109. struct iio_poll_func *pf = p;
  110. struct iio_dev *indio_dev = pf->indio_dev;
  111. struct adc081c *data = iio_priv(indio_dev);
  112. u16 buf[8]; /* 2 bytes data + 6 bytes padding + 8 bytes timestamp */
  113. int ret;
  114. ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
  115. if (ret < 0)
  116. goto out;
  117. buf[0] = ret;
  118. iio_push_to_buffers_with_timestamp(indio_dev, buf,
  119. iio_get_time_ns(indio_dev));
  120. out:
  121. iio_trigger_notify_done(indio_dev->trig);
  122. return IRQ_HANDLED;
  123. }
  124. static int adc081c_probe(struct i2c_client *client,
  125. const struct i2c_device_id *id)
  126. {
  127. struct iio_dev *iio;
  128. struct adc081c *adc;
  129. struct adcxx1c_model *model;
  130. int err;
  131. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
  132. return -EOPNOTSUPP;
  133. if (ACPI_COMPANION(&client->dev)) {
  134. const struct acpi_device_id *ad_id;
  135. ad_id = acpi_match_device(client->dev.driver->acpi_match_table,
  136. &client->dev);
  137. if (!ad_id)
  138. return -ENODEV;
  139. model = &adcxx1c_models[ad_id->driver_data];
  140. } else {
  141. model = &adcxx1c_models[id->driver_data];
  142. }
  143. iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
  144. if (!iio)
  145. return -ENOMEM;
  146. adc = iio_priv(iio);
  147. adc->i2c = client;
  148. adc->bits = model->bits;
  149. adc->ref = devm_regulator_get(&client->dev, "vref");
  150. if (IS_ERR(adc->ref))
  151. return PTR_ERR(adc->ref);
  152. err = regulator_enable(adc->ref);
  153. if (err < 0)
  154. return err;
  155. iio->dev.parent = &client->dev;
  156. iio->dev.of_node = client->dev.of_node;
  157. iio->name = dev_name(&client->dev);
  158. iio->modes = INDIO_DIRECT_MODE;
  159. iio->info = &adc081c_info;
  160. iio->channels = model->channels;
  161. iio->num_channels = ADC081C_NUM_CHANNELS;
  162. err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL);
  163. if (err < 0) {
  164. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  165. goto err_regulator_disable;
  166. }
  167. err = iio_device_register(iio);
  168. if (err < 0)
  169. goto err_buffer_cleanup;
  170. i2c_set_clientdata(client, iio);
  171. return 0;
  172. err_buffer_cleanup:
  173. iio_triggered_buffer_cleanup(iio);
  174. err_regulator_disable:
  175. regulator_disable(adc->ref);
  176. return err;
  177. }
  178. static int adc081c_remove(struct i2c_client *client)
  179. {
  180. struct iio_dev *iio = i2c_get_clientdata(client);
  181. struct adc081c *adc = iio_priv(iio);
  182. iio_device_unregister(iio);
  183. iio_triggered_buffer_cleanup(iio);
  184. regulator_disable(adc->ref);
  185. return 0;
  186. }
  187. static const struct i2c_device_id adc081c_id[] = {
  188. { "adc081c", ADC081C },
  189. { "adc101c", ADC101C },
  190. { "adc121c", ADC121C },
  191. { }
  192. };
  193. MODULE_DEVICE_TABLE(i2c, adc081c_id);
  194. #ifdef CONFIG_OF
  195. static const struct of_device_id adc081c_of_match[] = {
  196. { .compatible = "ti,adc081c" },
  197. { .compatible = "ti,adc101c" },
  198. { .compatible = "ti,adc121c" },
  199. { }
  200. };
  201. MODULE_DEVICE_TABLE(of, adc081c_of_match);
  202. #endif
  203. #ifdef CONFIG_ACPI
  204. static const struct acpi_device_id adc081c_acpi_match[] = {
  205. { "ADC081C", ADC081C },
  206. { "ADC101C", ADC101C },
  207. { "ADC121C", ADC121C },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match);
  211. #endif
  212. static struct i2c_driver adc081c_driver = {
  213. .driver = {
  214. .name = "adc081c",
  215. .of_match_table = of_match_ptr(adc081c_of_match),
  216. .acpi_match_table = ACPI_PTR(adc081c_acpi_match),
  217. },
  218. .probe = adc081c_probe,
  219. .remove = adc081c_remove,
  220. .id_table = adc081c_id,
  221. };
  222. module_i2c_driver(adc081c_driver);
  223. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  224. MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
  225. MODULE_LICENSE("GPL v2");