ltc2497.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <asm/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. const struct i2c_device_id *id)
  87. {
  88. const struct ltc2497_chip_info *chip_info;
  89. struct iio_dev *indio_dev;
  90. struct ltc2497_driverdata *st;
  91. struct device *dev = &client->dev;
  92. u32 resolution;
  93. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  94. I2C_FUNC_SMBUS_WRITE_BYTE))
  95. return -EOPNOTSUPP;
  96. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  97. if (!indio_dev)
  98. return -ENOMEM;
  99. st = iio_priv(indio_dev);
  100. i2c_set_clientdata(client, indio_dev);
  101. st->client = client;
  102. st->common_ddata.result_and_measure = ltc2497_result_and_measure;
  103. chip_info = device_get_match_data(dev);
  104. if (!chip_info)
  105. chip_info = (const struct ltc2497_chip_info *)id->driver_data;
  106. st->common_ddata.chip_info = chip_info;
  107. resolution = chip_info->resolution;
  108. st->recv_size = BITS_TO_BYTES(resolution) + 1;
  109. return ltc2497core_probe(dev, indio_dev);
  110. }
  111. static void ltc2497_remove(struct i2c_client *client)
  112. {
  113. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  114. ltc2497core_remove(indio_dev);
  115. }
  116. static const struct ltc2497_chip_info ltc2497_info[] = {
  117. [TYPE_LTC2497] = {
  118. .resolution = 16,
  119. .name = NULL,
  120. },
  121. [TYPE_LTC2499] = {
  122. .resolution = 24,
  123. .name = "ltc2499",
  124. },
  125. };
  126. static const struct i2c_device_id ltc2497_id[] = {
  127. { "ltc2497", (kernel_ulong_t)&ltc2497_info[TYPE_LTC2497] },
  128. { "ltc2499", (kernel_ulong_t)&ltc2497_info[TYPE_LTC2499] },
  129. { }
  130. };
  131. MODULE_DEVICE_TABLE(i2c, ltc2497_id);
  132. static const struct of_device_id ltc2497_of_match[] = {
  133. { .compatible = "lltc,ltc2497", .data = &ltc2497_info[TYPE_LTC2497] },
  134. { .compatible = "lltc,ltc2499", .data = &ltc2497_info[TYPE_LTC2499] },
  135. {},
  136. };
  137. MODULE_DEVICE_TABLE(of, ltc2497_of_match);
  138. static struct i2c_driver ltc2497_driver = {
  139. .driver = {
  140. .name = "ltc2497",
  141. .of_match_table = ltc2497_of_match,
  142. },
  143. .probe = ltc2497_probe,
  144. .remove = ltc2497_remove,
  145. .id_table = ltc2497_id,
  146. };
  147. module_i2c_driver(ltc2497_driver);
  148. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  149. MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
  150. MODULE_LICENSE("GPL v2");