ads7828.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/of_device.h>
  34. #include <linux/platform_data/ads7828.h>
  35. #include <linux/regmap.h>
  36. #include <linux/slab.h>
  37. #include <linux/regulator/consumer.h>
  38. /* The ADS7828 registers */
  39. #define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */
  40. #define ADS7828_CMD_PD1 0x04 /* Internal vref OFF && A/D ON */
  41. #define ADS7828_CMD_PD3 0x0C /* Internal vref ON && A/D ON */
  42. #define ADS7828_INT_VREF_MV 2500 /* Internal vref is 2.5V, 2500mV */
  43. #define ADS7828_EXT_VREF_MV_MIN 50 /* External vref min value 0.05V */
  44. #define ADS7828_EXT_VREF_MV_MAX 5250 /* External vref max value 5.25V */
  45. /* List of supported devices */
  46. enum ads7828_chips { ads7828, ads7830 };
  47. /* Client specific data */
  48. struct ads7828_data {
  49. struct regmap *regmap;
  50. u8 cmd_byte; /* Command byte without channel bits */
  51. unsigned int lsb_resol; /* Resolution of the ADC sample LSB */
  52. };
  53. /* Command byte C2,C1,C0 - see datasheet */
  54. static inline u8 ads7828_cmd_byte(u8 cmd, int ch)
  55. {
  56. return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4);
  57. }
  58. /* sysfs callback function */
  59. static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da,
  60. char *buf)
  61. {
  62. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  63. struct ads7828_data *data = dev_get_drvdata(dev);
  64. u8 cmd = ads7828_cmd_byte(data->cmd_byte, attr->index);
  65. unsigned int regval;
  66. int err;
  67. err = regmap_read(data->regmap, cmd, &regval);
  68. if (err < 0)
  69. return err;
  70. return sprintf(buf, "%d\n",
  71. DIV_ROUND_CLOSEST(regval * data->lsb_resol, 1000));
  72. }
  73. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0);
  74. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ads7828_show_in, NULL, 1);
  75. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ads7828_show_in, NULL, 2);
  76. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ads7828_show_in, NULL, 3);
  77. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ads7828_show_in, NULL, 4);
  78. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ads7828_show_in, NULL, 5);
  79. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ads7828_show_in, NULL, 6);
  80. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ads7828_show_in, NULL, 7);
  81. static struct attribute *ads7828_attrs[] = {
  82. &sensor_dev_attr_in0_input.dev_attr.attr,
  83. &sensor_dev_attr_in1_input.dev_attr.attr,
  84. &sensor_dev_attr_in2_input.dev_attr.attr,
  85. &sensor_dev_attr_in3_input.dev_attr.attr,
  86. &sensor_dev_attr_in4_input.dev_attr.attr,
  87. &sensor_dev_attr_in5_input.dev_attr.attr,
  88. &sensor_dev_attr_in6_input.dev_attr.attr,
  89. &sensor_dev_attr_in7_input.dev_attr.attr,
  90. NULL
  91. };
  92. ATTRIBUTE_GROUPS(ads7828);
  93. static const struct regmap_config ads2828_regmap_config = {
  94. .reg_bits = 8,
  95. .val_bits = 16,
  96. };
  97. static const struct regmap_config ads2830_regmap_config = {
  98. .reg_bits = 8,
  99. .val_bits = 8,
  100. };
  101. static int ads7828_probe(struct i2c_client *client,
  102. const struct i2c_device_id *id)
  103. {
  104. struct device *dev = &client->dev;
  105. struct ads7828_platform_data *pdata = dev_get_platdata(dev);
  106. struct ads7828_data *data;
  107. struct device *hwmon_dev;
  108. unsigned int vref_mv = ADS7828_INT_VREF_MV;
  109. unsigned int vref_uv;
  110. bool diff_input = false;
  111. bool ext_vref = false;
  112. unsigned int regval;
  113. enum ads7828_chips chip;
  114. struct regulator *reg;
  115. data = devm_kzalloc(dev, sizeof(struct ads7828_data), GFP_KERNEL);
  116. if (!data)
  117. return -ENOMEM;
  118. if (pdata) {
  119. diff_input = pdata->diff_input;
  120. ext_vref = pdata->ext_vref;
  121. if (ext_vref && pdata->vref_mv)
  122. vref_mv = pdata->vref_mv;
  123. } else if (dev->of_node) {
  124. diff_input = of_property_read_bool(dev->of_node,
  125. "ti,differential-input");
  126. reg = devm_regulator_get_optional(dev, "vref");
  127. if (!IS_ERR(reg)) {
  128. vref_uv = regulator_get_voltage(reg);
  129. vref_mv = DIV_ROUND_CLOSEST(vref_uv, 1000);
  130. if (vref_mv < ADS7828_EXT_VREF_MV_MIN ||
  131. vref_mv > ADS7828_EXT_VREF_MV_MAX)
  132. return -EINVAL;
  133. ext_vref = true;
  134. }
  135. }
  136. if (client->dev.of_node)
  137. chip = (enum ads7828_chips)
  138. of_device_get_match_data(&client->dev);
  139. else
  140. chip = id->driver_data;
  141. /* Bound Vref with min/max values */
  142. vref_mv = clamp_val(vref_mv, ADS7828_EXT_VREF_MV_MIN,
  143. ADS7828_EXT_VREF_MV_MAX);
  144. /* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */
  145. if (chip == ads7828) {
  146. data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 4096);
  147. data->regmap = devm_regmap_init_i2c(client,
  148. &ads2828_regmap_config);
  149. } else {
  150. data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 256);
  151. data->regmap = devm_regmap_init_i2c(client,
  152. &ads2830_regmap_config);
  153. }
  154. if (IS_ERR(data->regmap))
  155. return PTR_ERR(data->regmap);
  156. data->cmd_byte = ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3;
  157. if (!diff_input)
  158. data->cmd_byte |= ADS7828_CMD_SD_SE;
  159. /*
  160. * Datasheet specifies internal reference voltage is disabled by
  161. * default. The internal reference voltage needs to be enabled and
  162. * voltage needs to settle before getting valid ADC data. So perform a
  163. * dummy read to enable the internal reference voltage.
  164. */
  165. if (!ext_vref)
  166. regmap_read(data->regmap, data->cmd_byte, &regval);
  167. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  168. data,
  169. ads7828_groups);
  170. return PTR_ERR_OR_ZERO(hwmon_dev);
  171. }
  172. static const struct i2c_device_id ads7828_device_ids[] = {
  173. { "ads7828", ads7828 },
  174. { "ads7830", ads7830 },
  175. { }
  176. };
  177. MODULE_DEVICE_TABLE(i2c, ads7828_device_ids);
  178. static const struct of_device_id ads7828_of_match[] = {
  179. {
  180. .compatible = "ti,ads7828",
  181. .data = (void *)ads7828
  182. },
  183. {
  184. .compatible = "ti,ads7830",
  185. .data = (void *)ads7830
  186. },
  187. { },
  188. };
  189. MODULE_DEVICE_TABLE(of, ads7828_of_match);
  190. static struct i2c_driver ads7828_driver = {
  191. .driver = {
  192. .name = "ads7828",
  193. .of_match_table = of_match_ptr(ads7828_of_match),
  194. },
  195. .id_table = ads7828_device_ids,
  196. .probe = ads7828_probe,
  197. };
  198. module_i2c_driver(ads7828_driver);
  199. MODULE_LICENSE("GPL");
  200. MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
  201. MODULE_DESCRIPTION("Driver for TI ADS7828 A/D converter and compatibles");