ltc2497.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ltc2497.c - Driver for Analog Devices/Linear Technology LTC2497 ADC
  4. *
  5. * Copyright (C) 2017 Analog Devices Inc.
  6. *
  7. * Datasheet: http://cds.linear.com/docs/en/datasheet/2497fd.pdf
  8. */
  9. #include <linux/i2c.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/iio/driver.h>
  12. #include <linux/module.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/property.h>
  15. #include <linux/unaligned.h>
  16. #include "ltc2497.h"
  17. enum ltc2497_chip_type {
  18. TYPE_LTC2497,
  19. TYPE_LTC2499,
  20. };
  21. struct ltc2497_driverdata {
  22. /* this must be the first member */
  23. struct ltc2497core_driverdata common_ddata;
  24. struct i2c_client *client;
  25. u32 recv_size;
  26. /*
  27. * DMA (thus cache coherency maintenance) may require the
  28. * transfer buffers to live in their own cache lines.
  29. */
  30. union {
  31. __be32 d32;
  32. u8 d8[3];
  33. } data __aligned(IIO_DMA_MINALIGN);
  34. };
  35. static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
  36. u8 address, int *val)
  37. {
  38. struct ltc2497_driverdata *st =
  39. container_of(ddata, struct ltc2497_driverdata, common_ddata);
  40. int ret;
  41. if (val) {
  42. if (st->recv_size == 3)
  43. ret = i2c_master_recv(st->client, (char *)&st->data.d8,
  44. st->recv_size);
  45. else
  46. ret = i2c_master_recv(st->client, (char *)&st->data.d32,
  47. st->recv_size);
  48. if (ret < 0) {
  49. dev_err(&st->client->dev, "i2c_master_recv failed\n");
  50. return ret;
  51. }
  52. /*
  53. * The data format is 16/24 bit 2s complement, but with an upper sign bit on the
  54. * resolution + 1 position, which is set for positive values only. Given this
  55. * bit's value, subtracting BIT(resolution + 1) from the ADC's result is
  56. * equivalent to a sign extension.
  57. */
  58. if (st->recv_size == 3) {
  59. *val = (get_unaligned_be24(st->data.d8) >> 6)
  60. - BIT(ddata->chip_info->resolution + 1);
  61. } else {
  62. *val = (be32_to_cpu(st->data.d32) >> 6)
  63. - BIT(ddata->chip_info->resolution + 1);
  64. }
  65. /*
  66. * The part started a new conversion at the end of the above i2c
  67. * transfer, so if the address didn't change since the last call
  68. * everything is fine and we can return early.
  69. * If not (which should only happen when some sort of bulk
  70. * conversion is implemented) we have to program the new
  71. * address. Note that this probably fails as the conversion that
  72. * was triggered above is like not complete yet and the two
  73. * operations have to be done in a single transfer.
  74. */
  75. if (ddata->addr_prev == address)
  76. return 0;
  77. }
  78. ret = i2c_smbus_write_byte(st->client,
  79. LTC2497_ENABLE | address);
  80. if (ret)
  81. dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
  82. ERR_PTR(ret));
  83. return ret;
  84. }
  85. static int ltc2497_probe(struct i2c_client *client)
  86. {
  87. const struct ltc2497_chip_info *chip_info;
  88. struct iio_dev *indio_dev;
  89. struct ltc2497_driverdata *st;
  90. struct device *dev = &client->dev;
  91. u32 resolution;
  92. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  93. I2C_FUNC_SMBUS_WRITE_BYTE))
  94. return -EOPNOTSUPP;
  95. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  96. if (!indio_dev)
  97. return -ENOMEM;
  98. st = iio_priv(indio_dev);
  99. i2c_set_clientdata(client, indio_dev);
  100. st->client = client;
  101. st->common_ddata.result_and_measure = ltc2497_result_and_measure;
  102. chip_info = i2c_get_match_data(client);
  103. st->common_ddata.chip_info = chip_info;
  104. resolution = chip_info->resolution;
  105. st->recv_size = BITS_TO_BYTES(resolution) + 1;
  106. return ltc2497core_probe(dev, indio_dev);
  107. }
  108. static void ltc2497_remove(struct i2c_client *client)
  109. {
  110. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  111. ltc2497core_remove(indio_dev);
  112. }
  113. static const struct ltc2497_chip_info ltc2497_info[] = {
  114. [TYPE_LTC2497] = {
  115. .resolution = 16,
  116. .name = NULL,
  117. },
  118. [TYPE_LTC2499] = {
  119. .resolution = 24,
  120. .name = "ltc2499",
  121. },
  122. };
  123. static const struct i2c_device_id ltc2497_id[] = {
  124. { "ltc2497", (kernel_ulong_t)&ltc2497_info[TYPE_LTC2497] },
  125. { "ltc2499", (kernel_ulong_t)&ltc2497_info[TYPE_LTC2499] },
  126. { }
  127. };
  128. MODULE_DEVICE_TABLE(i2c, ltc2497_id);
  129. static const struct of_device_id ltc2497_of_match[] = {
  130. { .compatible = "lltc,ltc2497", .data = &ltc2497_info[TYPE_LTC2497] },
  131. { .compatible = "lltc,ltc2499", .data = &ltc2497_info[TYPE_LTC2499] },
  132. { }
  133. };
  134. MODULE_DEVICE_TABLE(of, ltc2497_of_match);
  135. static struct i2c_driver ltc2497_driver = {
  136. .driver = {
  137. .name = "ltc2497",
  138. .of_match_table = ltc2497_of_match,
  139. },
  140. .probe = ltc2497_probe,
  141. .remove = ltc2497_remove,
  142. .id_table = ltc2497_id,
  143. };
  144. module_i2c_driver(ltc2497_driver);
  145. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  146. MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
  147. MODULE_LICENSE("GPL v2");