ltc2497.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "ltc2497.h"
  15. struct ltc2497_driverdata {
  16. /* this must be the first member */
  17. struct ltc2497core_driverdata common_ddata;
  18. struct i2c_client *client;
  19. /*
  20. * DMA (thus cache coherency maintenance) requires the
  21. * transfer buffers to live in their own cache lines.
  22. */
  23. __be32 buf ____cacheline_aligned;
  24. };
  25. static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
  26. u8 address, int *val)
  27. {
  28. struct ltc2497_driverdata *st =
  29. container_of(ddata, struct ltc2497_driverdata, common_ddata);
  30. int ret;
  31. if (val) {
  32. ret = i2c_master_recv(st->client, (char *)&st->buf, 3);
  33. if (ret < 0) {
  34. dev_err(&st->client->dev, "i2c_master_recv failed\n");
  35. return ret;
  36. }
  37. *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
  38. }
  39. ret = i2c_smbus_write_byte(st->client,
  40. LTC2497_ENABLE | address);
  41. if (ret)
  42. dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
  43. ERR_PTR(ret));
  44. return ret;
  45. }
  46. static int ltc2497_probe(struct i2c_client *client,
  47. const struct i2c_device_id *id)
  48. {
  49. struct iio_dev *indio_dev;
  50. struct ltc2497_driverdata *st;
  51. struct device *dev = &client->dev;
  52. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  53. I2C_FUNC_SMBUS_WRITE_BYTE))
  54. return -EOPNOTSUPP;
  55. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  56. if (!indio_dev)
  57. return -ENOMEM;
  58. st = iio_priv(indio_dev);
  59. i2c_set_clientdata(client, indio_dev);
  60. st->client = client;
  61. st->common_ddata.result_and_measure = ltc2497_result_and_measure;
  62. return ltc2497core_probe(dev, indio_dev);
  63. }
  64. static int ltc2497_remove(struct i2c_client *client)
  65. {
  66. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  67. ltc2497core_remove(indio_dev);
  68. return 0;
  69. }
  70. static const struct i2c_device_id ltc2497_id[] = {
  71. { "ltc2497", 0 },
  72. { }
  73. };
  74. MODULE_DEVICE_TABLE(i2c, ltc2497_id);
  75. static const struct of_device_id ltc2497_of_match[] = {
  76. { .compatible = "lltc,ltc2497", },
  77. {},
  78. };
  79. MODULE_DEVICE_TABLE(of, ltc2497_of_match);
  80. static struct i2c_driver ltc2497_driver = {
  81. .driver = {
  82. .name = "ltc2497",
  83. .of_match_table = ltc2497_of_match,
  84. },
  85. .probe = ltc2497_probe,
  86. .remove = ltc2497_remove,
  87. .id_table = ltc2497_id,
  88. };
  89. module_i2c_driver(ltc2497_driver);
  90. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  91. MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
  92. MODULE_LICENSE("GPL v2");