ads7828.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. ads7828.c - lm_sensors driver for ads7828 12-bit 8-channel ADC
  3. (C) 2007 EADS Astrium
  4. This driver is based on the lm75 and other lm_sensors/hwmon drivers
  5. Written by Steve Hardy <shardy@redhat.com>
  6. Datasheet available at: http://focus.ti.com/lit/ds/symlink/ads7828.pdf
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-sysfs.h>
  26. #include <linux/err.h>
  27. #include <linux/mutex.h>
  28. /* The ADS7828 registers */
  29. #define ADS7828_NCH 8 /* 8 channels of 12-bit A-D supported */
  30. #define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */
  31. #define ADS7828_CMD_SD_DIFF 0x00 /* Differential inputs */
  32. #define ADS7828_CMD_PD0 0x0 /* Power Down between A-D conversions */
  33. #define ADS7828_CMD_PD1 0x04 /* Internal ref OFF && A-D ON */
  34. #define ADS7828_CMD_PD2 0x08 /* Internal ref ON && A-D OFF */
  35. #define ADS7828_CMD_PD3 0x0C /* Internal ref ON && A-D ON */
  36. #define ADS7828_INT_VREF_MV 2500 /* Internal vref is 2.5V, 2500mV */
  37. /* Addresses to scan */
  38. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
  39. I2C_CLIENT_END };
  40. /* Module parameters */
  41. static int se_input = 1; /* Default is SE, 0 == diff */
  42. static int int_vref = 1; /* Default is internal ref ON */
  43. static int vref_mv = ADS7828_INT_VREF_MV; /* set if vref != 2.5V */
  44. module_param(se_input, bool, S_IRUGO);
  45. module_param(int_vref, bool, S_IRUGO);
  46. module_param(vref_mv, int, S_IRUGO);
  47. /* Global Variables */
  48. static u8 ads7828_cmd_byte; /* cmd byte without channel bits */
  49. static unsigned int ads7828_lsb_resol; /* resolution of the ADC sample lsb */
  50. /* Each client has this additional data */
  51. struct ads7828_data {
  52. struct device *hwmon_dev;
  53. struct mutex update_lock; /* mutex protect updates */
  54. char valid; /* !=0 if following fields are valid */
  55. unsigned long last_updated; /* In jiffies */
  56. u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH 12-bit samples */
  57. };
  58. /* Function declaration - necessary due to function dependencies */
  59. static int ads7828_detect(struct i2c_client *client,
  60. struct i2c_board_info *info);
  61. static int ads7828_probe(struct i2c_client *client,
  62. const struct i2c_device_id *id);
  63. /* The ADS7828 returns the 12-bit sample in two bytes,
  64. these are read as a word then byte-swapped */
  65. static u16 ads7828_read_value(struct i2c_client *client, u8 reg)
  66. {
  67. return swab16(i2c_smbus_read_word_data(client, reg));
  68. }
  69. static inline u8 channel_cmd_byte(int ch)
  70. {
  71. /* cmd byte C2,C1,C0 - see datasheet */
  72. u8 cmd = (((ch>>1) | (ch&0x01)<<2)<<4);
  73. cmd |= ads7828_cmd_byte;
  74. return cmd;
  75. }
  76. /* Update data for the device (all 8 channels) */
  77. static struct ads7828_data *ads7828_update_device(struct device *dev)
  78. {
  79. struct i2c_client *client = to_i2c_client(dev);
  80. struct ads7828_data *data = i2c_get_clientdata(client);
  81. mutex_lock(&data->update_lock);
  82. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  83. || !data->valid) {
  84. unsigned int ch;
  85. dev_dbg(&client->dev, "Starting ads7828 update\n");
  86. for (ch = 0; ch < ADS7828_NCH; ch++) {
  87. u8 cmd = channel_cmd_byte(ch);
  88. data->adc_input[ch] = ads7828_read_value(client, cmd);
  89. }
  90. data->last_updated = jiffies;
  91. data->valid = 1;
  92. }
  93. mutex_unlock(&data->update_lock);
  94. return data;
  95. }
  96. /* sysfs callback function */
  97. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  98. char *buf)
  99. {
  100. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  101. struct ads7828_data *data = ads7828_update_device(dev);
  102. /* Print value (in mV as specified in sysfs-interface documentation) */
  103. return sprintf(buf, "%d\n", (data->adc_input[attr->index] *
  104. ads7828_lsb_resol)/1000);
  105. }
  106. #define in_reg(offset)\
  107. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in,\
  108. NULL, offset)
  109. in_reg(0);
  110. in_reg(1);
  111. in_reg(2);
  112. in_reg(3);
  113. in_reg(4);
  114. in_reg(5);
  115. in_reg(6);
  116. in_reg(7);
  117. static struct attribute *ads7828_attributes[] = {
  118. &sensor_dev_attr_in0_input.dev_attr.attr,
  119. &sensor_dev_attr_in1_input.dev_attr.attr,
  120. &sensor_dev_attr_in2_input.dev_attr.attr,
  121. &sensor_dev_attr_in3_input.dev_attr.attr,
  122. &sensor_dev_attr_in4_input.dev_attr.attr,
  123. &sensor_dev_attr_in5_input.dev_attr.attr,
  124. &sensor_dev_attr_in6_input.dev_attr.attr,
  125. &sensor_dev_attr_in7_input.dev_attr.attr,
  126. NULL
  127. };
  128. static const struct attribute_group ads7828_group = {
  129. .attrs = ads7828_attributes,
  130. };
  131. static int ads7828_remove(struct i2c_client *client)
  132. {
  133. struct ads7828_data *data = i2c_get_clientdata(client);
  134. hwmon_device_unregister(data->hwmon_dev);
  135. sysfs_remove_group(&client->dev.kobj, &ads7828_group);
  136. kfree(i2c_get_clientdata(client));
  137. return 0;
  138. }
  139. static const struct i2c_device_id ads7828_id[] = {
  140. { "ads7828", 0 },
  141. { }
  142. };
  143. MODULE_DEVICE_TABLE(i2c, ads7828_id);
  144. /* This is the driver that will be inserted */
  145. static struct i2c_driver ads7828_driver = {
  146. .class = I2C_CLASS_HWMON,
  147. .driver = {
  148. .name = "ads7828",
  149. },
  150. .probe = ads7828_probe,
  151. .remove = ads7828_remove,
  152. .id_table = ads7828_id,
  153. .detect = ads7828_detect,
  154. .address_list = normal_i2c,
  155. };
  156. /* Return 0 if detection is successful, -ENODEV otherwise */
  157. static int ads7828_detect(struct i2c_client *client,
  158. struct i2c_board_info *info)
  159. {
  160. struct i2c_adapter *adapter = client->adapter;
  161. int ch;
  162. /* Check we have a valid client */
  163. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA))
  164. return -ENODEV;
  165. /* Now, we do the remaining detection. There is no identification
  166. dedicated register so attempt to sanity check using knowledge of
  167. the chip
  168. - Read from the 8 channel addresses
  169. - Check the top 4 bits of each result are not set (12 data bits)
  170. */
  171. for (ch = 0; ch < ADS7828_NCH; ch++) {
  172. u16 in_data;
  173. u8 cmd = channel_cmd_byte(ch);
  174. in_data = ads7828_read_value(client, cmd);
  175. if (in_data & 0xF000) {
  176. pr_debug("%s : Doesn't look like an ads7828 device\n",
  177. __func__);
  178. return -ENODEV;
  179. }
  180. }
  181. strlcpy(info->type, "ads7828", I2C_NAME_SIZE);
  182. return 0;
  183. }
  184. static int ads7828_probe(struct i2c_client *client,
  185. const struct i2c_device_id *id)
  186. {
  187. struct ads7828_data *data;
  188. int err;
  189. data = kzalloc(sizeof(struct ads7828_data), GFP_KERNEL);
  190. if (!data) {
  191. err = -ENOMEM;
  192. goto exit;
  193. }
  194. i2c_set_clientdata(client, data);
  195. mutex_init(&data->update_lock);
  196. /* Register sysfs hooks */
  197. err = sysfs_create_group(&client->dev.kobj, &ads7828_group);
  198. if (err)
  199. goto exit_free;
  200. data->hwmon_dev = hwmon_device_register(&client->dev);
  201. if (IS_ERR(data->hwmon_dev)) {
  202. err = PTR_ERR(data->hwmon_dev);
  203. goto exit_remove;
  204. }
  205. return 0;
  206. exit_remove:
  207. sysfs_remove_group(&client->dev.kobj, &ads7828_group);
  208. exit_free:
  209. kfree(data);
  210. exit:
  211. return err;
  212. }
  213. static int __init sensors_ads7828_init(void)
  214. {
  215. /* Initialize the command byte according to module parameters */
  216. ads7828_cmd_byte = se_input ?
  217. ADS7828_CMD_SD_SE : ADS7828_CMD_SD_DIFF;
  218. ads7828_cmd_byte |= int_vref ?
  219. ADS7828_CMD_PD3 : ADS7828_CMD_PD1;
  220. /* Calculate the LSB resolution */
  221. ads7828_lsb_resol = (vref_mv*1000)/4096;
  222. return i2c_add_driver(&ads7828_driver);
  223. }
  224. static void __exit sensors_ads7828_exit(void)
  225. {
  226. i2c_del_driver(&ads7828_driver);
  227. }
  228. MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
  229. MODULE_DESCRIPTION("ADS7828 driver");
  230. MODULE_LICENSE("GPL");
  231. module_init(sensors_ads7828_init);
  232. module_exit(sensors_ads7828_exit);