ads7828.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/jiffies.h>
  33. #include <linux/module.h>
  34. #include <linux/mutex.h>
  35. #include <linux/platform_data/ads7828.h>
  36. #include <linux/slab.h>
  37. /* The ADS7828 registers */
  38. #define ADS7828_NCH 8 /* 8 channels supported */
  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 device *hwmon_dev;
  50. struct mutex update_lock; /* Mutex protecting updates */
  51. unsigned long last_updated; /* Last updated time (in jiffies) */
  52. u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH samples */
  53. bool valid; /* Validity flag */
  54. bool diff_input; /* Differential input */
  55. bool ext_vref; /* External voltage reference */
  56. unsigned int vref_mv; /* voltage reference value */
  57. u8 cmd_byte; /* Command byte without channel bits */
  58. unsigned int lsb_resol; /* Resolution of the ADC sample LSB */
  59. s32 (*read_channel)(const struct i2c_client *client, u8 command);
  60. };
  61. /* Command byte C2,C1,C0 - see datasheet */
  62. static inline u8 ads7828_cmd_byte(u8 cmd, int ch)
  63. {
  64. return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4);
  65. }
  66. /* Update data for the device (all 8 channels) */
  67. static struct ads7828_data *ads7828_update_device(struct device *dev)
  68. {
  69. struct i2c_client *client = to_i2c_client(dev);
  70. struct ads7828_data *data = i2c_get_clientdata(client);
  71. mutex_lock(&data->update_lock);
  72. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  73. || !data->valid) {
  74. unsigned int ch;
  75. dev_dbg(&client->dev, "Starting ads7828 update\n");
  76. for (ch = 0; ch < ADS7828_NCH; ch++) {
  77. u8 cmd = ads7828_cmd_byte(data->cmd_byte, ch);
  78. data->adc_input[ch] = data->read_channel(client, cmd);
  79. }
  80. data->last_updated = jiffies;
  81. data->valid = true;
  82. }
  83. mutex_unlock(&data->update_lock);
  84. return data;
  85. }
  86. /* sysfs callback function */
  87. static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da,
  88. char *buf)
  89. {
  90. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  91. struct ads7828_data *data = ads7828_update_device(dev);
  92. unsigned int value = DIV_ROUND_CLOSEST(data->adc_input[attr->index] *
  93. data->lsb_resol, 1000);
  94. return sprintf(buf, "%d\n", value);
  95. }
  96. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0);
  97. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ads7828_show_in, NULL, 1);
  98. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ads7828_show_in, NULL, 2);
  99. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ads7828_show_in, NULL, 3);
  100. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ads7828_show_in, NULL, 4);
  101. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ads7828_show_in, NULL, 5);
  102. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ads7828_show_in, NULL, 6);
  103. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ads7828_show_in, NULL, 7);
  104. static struct attribute *ads7828_attributes[] = {
  105. &sensor_dev_attr_in0_input.dev_attr.attr,
  106. &sensor_dev_attr_in1_input.dev_attr.attr,
  107. &sensor_dev_attr_in2_input.dev_attr.attr,
  108. &sensor_dev_attr_in3_input.dev_attr.attr,
  109. &sensor_dev_attr_in4_input.dev_attr.attr,
  110. &sensor_dev_attr_in5_input.dev_attr.attr,
  111. &sensor_dev_attr_in6_input.dev_attr.attr,
  112. &sensor_dev_attr_in7_input.dev_attr.attr,
  113. NULL
  114. };
  115. static const struct attribute_group ads7828_group = {
  116. .attrs = ads7828_attributes,
  117. };
  118. static int ads7828_remove(struct i2c_client *client)
  119. {
  120. struct ads7828_data *data = i2c_get_clientdata(client);
  121. hwmon_device_unregister(data->hwmon_dev);
  122. sysfs_remove_group(&client->dev.kobj, &ads7828_group);
  123. return 0;
  124. }
  125. static int ads7828_probe(struct i2c_client *client,
  126. const struct i2c_device_id *id)
  127. {
  128. struct ads7828_platform_data *pdata = client->dev.platform_data;
  129. struct ads7828_data *data;
  130. int err;
  131. data = devm_kzalloc(&client->dev, sizeof(struct ads7828_data),
  132. GFP_KERNEL);
  133. if (!data)
  134. return -ENOMEM;
  135. if (pdata) {
  136. data->diff_input = pdata->diff_input;
  137. data->ext_vref = pdata->ext_vref;
  138. if (data->ext_vref)
  139. data->vref_mv = pdata->vref_mv;
  140. }
  141. /* Bound Vref with min/max values if it was provided */
  142. if (data->vref_mv)
  143. data->vref_mv = clamp_val(data->vref_mv,
  144. ADS7828_EXT_VREF_MV_MIN,
  145. ADS7828_EXT_VREF_MV_MAX);
  146. else
  147. data->vref_mv = ADS7828_INT_VREF_MV;
  148. /* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */
  149. if (id->driver_data == ads7828) {
  150. data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 4096);
  151. data->read_channel = i2c_smbus_read_word_swapped;
  152. } else {
  153. data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 256);
  154. data->read_channel = i2c_smbus_read_byte_data;
  155. }
  156. data->cmd_byte = data->ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3;
  157. if (!data->diff_input)
  158. data->cmd_byte |= ADS7828_CMD_SD_SE;
  159. i2c_set_clientdata(client, data);
  160. mutex_init(&data->update_lock);
  161. err = sysfs_create_group(&client->dev.kobj, &ads7828_group);
  162. if (err)
  163. return err;
  164. data->hwmon_dev = hwmon_device_register(&client->dev);
  165. if (IS_ERR(data->hwmon_dev)) {
  166. err = PTR_ERR(data->hwmon_dev);
  167. goto error;
  168. }
  169. return 0;
  170. error:
  171. sysfs_remove_group(&client->dev.kobj, &ads7828_group);
  172. return err;
  173. }
  174. static const struct i2c_device_id ads7828_device_ids[] = {
  175. { "ads7828", ads7828 },
  176. { "ads7830", ads7830 },
  177. { }
  178. };
  179. MODULE_DEVICE_TABLE(i2c, ads7828_device_ids);
  180. static struct i2c_driver ads7828_driver = {
  181. .driver = {
  182. .name = "ads7828",
  183. },
  184. .id_table = ads7828_device_ids,
  185. .probe = ads7828_probe,
  186. .remove = ads7828_remove,
  187. };
  188. module_i2c_driver(ads7828_driver);
  189. MODULE_LICENSE("GPL");
  190. MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
  191. MODULE_DESCRIPTION("Driver for TI ADS7828 A/D converter and compatibles");