ltc2471.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Driver for Linear Technology LTC2471 and LTC2473 voltage monitors
  3. * The LTC2473 is identical to the 2471, but reports a differential signal.
  4. *
  5. * Copyright (C) 2017 Topic Embedded Products
  6. * Author: Mike Looijmans <mike.looijmans@topic.nl>
  7. *
  8. * License: GPLv2
  9. */
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/sysfs.h>
  16. enum ltc2471_chips {
  17. ltc2471,
  18. ltc2473,
  19. };
  20. struct ltc2471_data {
  21. struct i2c_client *client;
  22. };
  23. /* Reference voltage is 1.25V */
  24. #define LTC2471_VREF 1250
  25. /* Read two bytes from the I2C bus to obtain the ADC result */
  26. static int ltc2471_get_value(struct i2c_client *client)
  27. {
  28. int ret;
  29. __be16 buf;
  30. ret = i2c_master_recv(client, (char *)&buf, sizeof(buf));
  31. if (ret < 0)
  32. return ret;
  33. if (ret != sizeof(buf))
  34. return -EIO;
  35. /* MSB first */
  36. return be16_to_cpu(buf);
  37. }
  38. static int ltc2471_read_raw(struct iio_dev *indio_dev,
  39. struct iio_chan_spec const *chan,
  40. int *val, int *val2, long info)
  41. {
  42. struct ltc2471_data *data = iio_priv(indio_dev);
  43. int ret;
  44. switch (info) {
  45. case IIO_CHAN_INFO_RAW:
  46. ret = ltc2471_get_value(data->client);
  47. if (ret < 0)
  48. return ret;
  49. *val = ret;
  50. return IIO_VAL_INT;
  51. case IIO_CHAN_INFO_SCALE:
  52. if (chan->differential)
  53. /* Output ranges from -VREF to +VREF */
  54. *val = 2 * LTC2471_VREF;
  55. else
  56. /* Output ranges from 0 to VREF */
  57. *val = LTC2471_VREF;
  58. *val2 = 16; /* 16 data bits */
  59. return IIO_VAL_FRACTIONAL_LOG2;
  60. case IIO_CHAN_INFO_OFFSET:
  61. /* Only differential chip has this property */
  62. *val = -LTC2471_VREF;
  63. return IIO_VAL_INT;
  64. default:
  65. return -EINVAL;
  66. }
  67. }
  68. static const struct iio_chan_spec ltc2471_channel[] = {
  69. {
  70. .type = IIO_VOLTAGE,
  71. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  72. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  73. },
  74. };
  75. static const struct iio_chan_spec ltc2473_channel[] = {
  76. {
  77. .type = IIO_VOLTAGE,
  78. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  79. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
  80. BIT(IIO_CHAN_INFO_OFFSET),
  81. .differential = 1,
  82. },
  83. };
  84. static const struct iio_info ltc2471_info = {
  85. .read_raw = ltc2471_read_raw,
  86. };
  87. static int ltc2471_i2c_probe(struct i2c_client *client,
  88. const struct i2c_device_id *id)
  89. {
  90. struct iio_dev *indio_dev;
  91. struct ltc2471_data *data;
  92. int ret;
  93. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  94. return -EOPNOTSUPP;
  95. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  96. if (!indio_dev)
  97. return -ENOMEM;
  98. data = iio_priv(indio_dev);
  99. data->client = client;
  100. indio_dev->dev.parent = &client->dev;
  101. indio_dev->name = id->name;
  102. indio_dev->info = &ltc2471_info;
  103. indio_dev->modes = INDIO_DIRECT_MODE;
  104. if (id->driver_data == ltc2473)
  105. indio_dev->channels = ltc2473_channel;
  106. else
  107. indio_dev->channels = ltc2471_channel;
  108. indio_dev->num_channels = 1;
  109. /* Trigger once to start conversion and check if chip is there */
  110. ret = ltc2471_get_value(client);
  111. if (ret < 0) {
  112. dev_err(&client->dev, "Cannot read from device.\n");
  113. return ret;
  114. }
  115. return devm_iio_device_register(&client->dev, indio_dev);
  116. }
  117. static const struct i2c_device_id ltc2471_i2c_id[] = {
  118. { "ltc2471", ltc2471 },
  119. { "ltc2473", ltc2473 },
  120. {}
  121. };
  122. MODULE_DEVICE_TABLE(i2c, ltc2471_i2c_id);
  123. static struct i2c_driver ltc2471_i2c_driver = {
  124. .driver = {
  125. .name = "ltc2471",
  126. },
  127. .probe = ltc2471_i2c_probe,
  128. .id_table = ltc2471_i2c_id,
  129. };
  130. module_i2c_driver(ltc2471_i2c_driver);
  131. MODULE_DESCRIPTION("LTC2471/LTC2473 ADC driver");
  132. MODULE_AUTHOR("Topic Embedded Products");
  133. MODULE_LICENSE("GPL v2");