ltc2471.c 3.5 KB

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