ads7828.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * ads7828.c - driver for TI ADS7828 8-channel A/D converter and compatibles
  3. * (C) 2007 EADS Astrium
  4. *
  5. * This driver is based on the lm75 and other lm_sensors/hwmon drivers
  6. *
  7. * Written by Steve Hardy <shardy@redhat.com>
  8. *
  9. * ADS7830 support, by Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
  10. *
  11. * For further information, see the Documentation/hwmon/ads7828 file.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/err.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/i2c.h>
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/platform_data/ads7828.h>
  34. #include <linux/regmap.h>
  35. #include <linux/slab.h>
  36. /* The ADS7828 registers */
  37. #define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */
  38. #define ADS7828_CMD_PD1 0x04 /* Internal vref OFF && A/D ON */
  39. #define ADS7828_CMD_PD3 0x0C /* Internal vref ON && A/D ON */
  40. #define ADS7828_INT_VREF_MV 2500 /* Internal vref is 2.5V, 2500mV */
  41. #define ADS7828_EXT_VREF_MV_MIN 50 /* External vref min value 0.05V */
  42. #define ADS7828_EXT_VREF_MV_MAX 5250 /* External vref max value 5.25V */
  43. /* List of supported devices */
  44. enum ads7828_chips { ads7828, ads7830 };
  45. /* Client specific data */
  46. struct ads7828_data {
  47. struct regmap *regmap;
  48. u8 cmd_byte; /* Command byte without channel bits */
  49. unsigned int lsb_resol; /* Resolution of the ADC sample LSB */
  50. };
  51. /* Command byte C2,C1,C0 - see datasheet */
  52. static inline u8 ads7828_cmd_byte(u8 cmd, int ch)
  53. {
  54. return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4);
  55. }
  56. /* sysfs callback function */
  57. static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da,
  58. char *buf)
  59. {
  60. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  61. struct ads7828_data *data = dev_get_drvdata(dev);
  62. u8 cmd = ads7828_cmd_byte(data->cmd_byte, attr->index);
  63. unsigned int regval;
  64. int err;
  65. err = regmap_read(data->regmap, cmd, &regval);
  66. if (err < 0)
  67. return err;
  68. return sprintf(buf, "%d\n",
  69. DIV_ROUND_CLOSEST(regval * data->lsb_resol, 1000));
  70. }
  71. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0);
  72. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ads7828_show_in, NULL, 1);
  73. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ads7828_show_in, NULL, 2);
  74. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ads7828_show_in, NULL, 3);
  75. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ads7828_show_in, NULL, 4);
  76. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ads7828_show_in, NULL, 5);
  77. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ads7828_show_in, NULL, 6);
  78. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ads7828_show_in, NULL, 7);
  79. static struct attribute *ads7828_attrs[] = {
  80. &sensor_dev_attr_in0_input.dev_attr.attr,
  81. &sensor_dev_attr_in1_input.dev_attr.attr,
  82. &sensor_dev_attr_in2_input.dev_attr.attr,
  83. &sensor_dev_attr_in3_input.dev_attr.attr,
  84. &sensor_dev_attr_in4_input.dev_attr.attr,
  85. &sensor_dev_attr_in5_input.dev_attr.attr,
  86. &sensor_dev_attr_in6_input.dev_attr.attr,
  87. &sensor_dev_attr_in7_input.dev_attr.attr,
  88. NULL
  89. };
  90. ATTRIBUTE_GROUPS(ads7828);
  91. static const struct regmap_config ads2828_regmap_config = {
  92. .reg_bits = 8,
  93. .val_bits = 16,
  94. };
  95. static const struct regmap_config ads2830_regmap_config = {
  96. .reg_bits = 8,
  97. .val_bits = 8,
  98. };
  99. static int ads7828_probe(struct i2c_client *client,
  100. const struct i2c_device_id *id)
  101. {
  102. struct device *dev = &client->dev;
  103. struct ads7828_platform_data *pdata = dev_get_platdata(dev);
  104. struct ads7828_data *data;
  105. struct device *hwmon_dev;
  106. unsigned int vref_mv = ADS7828_INT_VREF_MV;
  107. bool diff_input = false;
  108. bool ext_vref = false;
  109. unsigned int regval;
  110. data = devm_kzalloc(dev, sizeof(struct ads7828_data), GFP_KERNEL);
  111. if (!data)
  112. return -ENOMEM;
  113. if (pdata) {
  114. diff_input = pdata->diff_input;
  115. ext_vref = pdata->ext_vref;
  116. if (ext_vref && pdata->vref_mv)
  117. vref_mv = pdata->vref_mv;
  118. }
  119. /* Bound Vref with min/max values */
  120. vref_mv = clamp_val(vref_mv, ADS7828_EXT_VREF_MV_MIN,
  121. ADS7828_EXT_VREF_MV_MAX);
  122. /* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */
  123. if (id->driver_data == ads7828) {
  124. data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 4096);
  125. data->regmap = devm_regmap_init_i2c(client,
  126. &ads2828_regmap_config);
  127. } else {
  128. data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 256);
  129. data->regmap = devm_regmap_init_i2c(client,
  130. &ads2830_regmap_config);
  131. }
  132. if (IS_ERR(data->regmap))
  133. return PTR_ERR(data->regmap);
  134. data->cmd_byte = ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3;
  135. if (!diff_input)
  136. data->cmd_byte |= ADS7828_CMD_SD_SE;
  137. /*
  138. * Datasheet specifies internal reference voltage is disabled by
  139. * default. The internal reference voltage needs to be enabled and
  140. * voltage needs to settle before getting valid ADC data. So perform a
  141. * dummy read to enable the internal reference voltage.
  142. */
  143. if (!ext_vref)
  144. regmap_read(data->regmap, data->cmd_byte, &regval);
  145. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  146. data,
  147. ads7828_groups);
  148. return PTR_ERR_OR_ZERO(hwmon_dev);
  149. }
  150. static const struct i2c_device_id ads7828_device_ids[] = {
  151. { "ads7828", ads7828 },
  152. { "ads7830", ads7830 },
  153. { }
  154. };
  155. MODULE_DEVICE_TABLE(i2c, ads7828_device_ids);
  156. static struct i2c_driver ads7828_driver = {
  157. .driver = {
  158. .name = "ads7828",
  159. },
  160. .id_table = ads7828_device_ids,
  161. .probe = ads7828_probe,
  162. };
  163. module_i2c_driver(ads7828_driver);
  164. MODULE_LICENSE("GPL");
  165. MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
  166. MODULE_DESCRIPTION("Driver for TI ADS7828 A/D converter and compatibles");