ti-adc081c.c 6.0 KB

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