ltc2485.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * ltc2485.c - Driver for Linear Technology LTC2485 ADC
  3. *
  4. * Copyright (C) 2016 Alison Schofield <amsfield22@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Datasheet: http://cds.linear.com/docs/en/datasheet/2485fd.pdf
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. /* Power-on configuration: rejects both 50/60Hz, operates at 1x speed */
  18. #define LTC2485_CONFIG_DEFAULT 0
  19. struct ltc2485_data {
  20. struct i2c_client *client;
  21. ktime_t time_prev; /* last conversion */
  22. };
  23. static void ltc2485_wait_conv(struct ltc2485_data *data)
  24. {
  25. const unsigned int conv_time = 147; /* conversion time ms */
  26. unsigned int time_elapsed;
  27. /* delay if conversion time not passed since last read or write */
  28. time_elapsed = ktime_ms_delta(ktime_get(), data->time_prev);
  29. if (time_elapsed < conv_time)
  30. msleep(conv_time - time_elapsed);
  31. }
  32. static int ltc2485_read(struct ltc2485_data *data, int *val)
  33. {
  34. struct i2c_client *client = data->client;
  35. __be32 buf = 0;
  36. int ret;
  37. ltc2485_wait_conv(data);
  38. ret = i2c_master_recv(client, (char *)&buf, 4);
  39. if (ret < 0) {
  40. dev_err(&client->dev, "i2c_master_recv failed\n");
  41. return ret;
  42. }
  43. data->time_prev = ktime_get();
  44. *val = sign_extend32(be32_to_cpu(buf) >> 6, 24);
  45. return ret;
  46. }
  47. static int ltc2485_read_raw(struct iio_dev *indio_dev,
  48. struct iio_chan_spec const *chan,
  49. int *val, int *val2, long mask)
  50. {
  51. struct ltc2485_data *data = iio_priv(indio_dev);
  52. int ret;
  53. if (mask == IIO_CHAN_INFO_RAW) {
  54. ret = ltc2485_read(data, val);
  55. if (ret < 0)
  56. return ret;
  57. return IIO_VAL_INT;
  58. } else if (mask == IIO_CHAN_INFO_SCALE) {
  59. *val = 5000; /* on board vref millivolts */
  60. *val2 = 25; /* 25 (24 + sign) data bits */
  61. return IIO_VAL_FRACTIONAL_LOG2;
  62. } else {
  63. return -EINVAL;
  64. }
  65. }
  66. static const struct iio_chan_spec ltc2485_channel[] = {
  67. {
  68. .type = IIO_VOLTAGE,
  69. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  70. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE)
  71. },
  72. };
  73. static const struct iio_info ltc2485_info = {
  74. .read_raw = ltc2485_read_raw,
  75. .driver_module = THIS_MODULE,
  76. };
  77. static int ltc2485_probe(struct i2c_client *client,
  78. const struct i2c_device_id *id)
  79. {
  80. struct iio_dev *indio_dev;
  81. struct ltc2485_data *data;
  82. int ret;
  83. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  84. I2C_FUNC_SMBUS_WRITE_BYTE))
  85. return -EOPNOTSUPP;
  86. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  87. if (!indio_dev)
  88. return -ENOMEM;
  89. data = iio_priv(indio_dev);
  90. i2c_set_clientdata(client, indio_dev);
  91. data->client = client;
  92. indio_dev->dev.parent = &client->dev;
  93. indio_dev->name = id->name;
  94. indio_dev->info = &ltc2485_info;
  95. indio_dev->modes = INDIO_DIRECT_MODE;
  96. indio_dev->channels = ltc2485_channel;
  97. indio_dev->num_channels = ARRAY_SIZE(ltc2485_channel);
  98. ret = i2c_smbus_write_byte(data->client, LTC2485_CONFIG_DEFAULT);
  99. if (ret < 0)
  100. return ret;
  101. data->time_prev = ktime_get();
  102. return devm_iio_device_register(&client->dev, indio_dev);
  103. }
  104. static const struct i2c_device_id ltc2485_id[] = {
  105. { "ltc2485", 0 },
  106. { }
  107. };
  108. MODULE_DEVICE_TABLE(i2c, ltc2485_id);
  109. static struct i2c_driver ltc2485_driver = {
  110. .driver = {
  111. .name = "ltc2485",
  112. },
  113. .probe = ltc2485_probe,
  114. .id_table = ltc2485_id,
  115. };
  116. module_i2c_driver(ltc2485_driver);
  117. MODULE_AUTHOR("Alison Schofield <amsfield22@gmail.com>");
  118. MODULE_DESCRIPTION("Linear Technology LTC2485 ADC driver");
  119. MODULE_LICENSE("GPL v2");