ads7828.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <steve@linuxrealtime.co.uk>
  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. /* Insmod parameters */
  41. I2C_CLIENT_INSMOD_1(ads7828);
  42. /* Other module parameters */
  43. static int se_input = 1; /* Default is SE, 0 == diff */
  44. static int int_vref = 1; /* Default is internal ref ON */
  45. static int vref_mv = ADS7828_INT_VREF_MV; /* set if vref != 2.5V */
  46. module_param(se_input, bool, S_IRUGO);
  47. module_param(int_vref, bool, S_IRUGO);
  48. module_param(vref_mv, int, S_IRUGO);
  49. /* Global Variables */
  50. static u8 ads7828_cmd_byte; /* cmd byte without channel bits */
  51. static unsigned int ads7828_lsb_resol; /* resolution of the ADC sample lsb */
  52. /* Each client has this additional data */
  53. struct ads7828_data {
  54. struct device *hwmon_dev;
  55. struct mutex update_lock; /* mutex protect updates */
  56. char valid; /* !=0 if following fields are valid */
  57. unsigned long last_updated; /* In jiffies */
  58. u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH 12-bit samples */
  59. };
  60. /* Function declaration - necessary due to function dependencies */
  61. static int ads7828_detect(struct i2c_client *client, int kind,
  62. struct i2c_board_info *info);
  63. static int ads7828_probe(struct i2c_client *client,
  64. const struct i2c_device_id *id);
  65. /* The ADS7828 returns the 12-bit sample in two bytes,
  66. these are read as a word then byte-swapped */
  67. static u16 ads7828_read_value(struct i2c_client *client, u8 reg)
  68. {
  69. return swab16(i2c_smbus_read_word_data(client, reg));
  70. }
  71. static inline u8 channel_cmd_byte(int ch)
  72. {
  73. /* cmd byte C2,C1,C0 - see datasheet */
  74. u8 cmd = (((ch>>1) | (ch&0x01)<<2)<<4);
  75. cmd |= ads7828_cmd_byte;
  76. return cmd;
  77. }
  78. /* Update data for the device (all 8 channels) */
  79. static struct ads7828_data *ads7828_update_device(struct device *dev)
  80. {
  81. struct i2c_client *client = to_i2c_client(dev);
  82. struct ads7828_data *data = i2c_get_clientdata(client);
  83. mutex_lock(&data->update_lock);
  84. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  85. || !data->valid) {
  86. unsigned int ch;
  87. dev_dbg(&client->dev, "Starting ads7828 update\n");
  88. for (ch = 0; ch < ADS7828_NCH; ch++) {
  89. u8 cmd = channel_cmd_byte(ch);
  90. data->adc_input[ch] = ads7828_read_value(client, cmd);
  91. }
  92. data->last_updated = jiffies;
  93. data->valid = 1;
  94. }
  95. mutex_unlock(&data->update_lock);
  96. return data;
  97. }
  98. /* sysfs callback function */
  99. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  100. char *buf)
  101. {
  102. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  103. struct ads7828_data *data = ads7828_update_device(dev);
  104. /* Print value (in mV as specified in sysfs-interface documentation) */
  105. return sprintf(buf, "%d\n", (data->adc_input[attr->index] *
  106. ads7828_lsb_resol)/1000);
  107. }
  108. #define in_reg(offset)\
  109. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in,\
  110. NULL, offset)
  111. in_reg(0);
  112. in_reg(1);
  113. in_reg(2);
  114. in_reg(3);
  115. in_reg(4);
  116. in_reg(5);
  117. in_reg(6);
  118. in_reg(7);
  119. static struct attribute *ads7828_attributes[] = {
  120. &sensor_dev_attr_in0_input.dev_attr.attr,
  121. &sensor_dev_attr_in1_input.dev_attr.attr,
  122. &sensor_dev_attr_in2_input.dev_attr.attr,
  123. &sensor_dev_attr_in3_input.dev_attr.attr,
  124. &sensor_dev_attr_in4_input.dev_attr.attr,
  125. &sensor_dev_attr_in5_input.dev_attr.attr,
  126. &sensor_dev_attr_in6_input.dev_attr.attr,
  127. &sensor_dev_attr_in7_input.dev_attr.attr,
  128. NULL
  129. };
  130. static const struct attribute_group ads7828_group = {
  131. .attrs = ads7828_attributes,
  132. };
  133. static int ads7828_remove(struct i2c_client *client)
  134. {
  135. struct ads7828_data *data = i2c_get_clientdata(client);
  136. hwmon_device_unregister(data->hwmon_dev);
  137. sysfs_remove_group(&client->dev.kobj, &ads7828_group);
  138. kfree(i2c_get_clientdata(client));
  139. return 0;
  140. }
  141. static const struct i2c_device_id ads7828_id[] = {
  142. { "ads7828", ads7828 },
  143. { }
  144. };
  145. MODULE_DEVICE_TABLE(i2c, ads7828_id);
  146. /* This is the driver that will be inserted */
  147. static struct i2c_driver ads7828_driver = {
  148. .class = I2C_CLASS_HWMON,
  149. .driver = {
  150. .name = "ads7828",
  151. },
  152. .probe = ads7828_probe,
  153. .remove = ads7828_remove,
  154. .id_table = ads7828_id,
  155. .detect = ads7828_detect,
  156. .address_data = &addr_data,
  157. };
  158. /* Return 0 if detection is successful, -ENODEV otherwise */
  159. static int ads7828_detect(struct i2c_client *client, int kind,
  160. struct i2c_board_info *info)
  161. {
  162. struct i2c_adapter *adapter = client->adapter;
  163. /* Check we have a valid client */
  164. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA))
  165. return -ENODEV;
  166. /* Now, we do the remaining detection. There is no identification
  167. dedicated register so attempt to sanity check using knowledge of
  168. the chip
  169. - Read from the 8 channel addresses
  170. - Check the top 4 bits of each result are not set (12 data bits)
  171. */
  172. if (kind < 0) {
  173. int ch;
  174. for (ch = 0; ch < ADS7828_NCH; ch++) {
  175. u16 in_data;
  176. u8 cmd = channel_cmd_byte(ch);
  177. in_data = ads7828_read_value(client, cmd);
  178. if (in_data & 0xF000) {
  179. printk(KERN_DEBUG
  180. "%s : Doesn't look like an ads7828 device\n",
  181. __func__);
  182. return -ENODEV;
  183. }
  184. }
  185. }
  186. strlcpy(info->type, "ads7828", I2C_NAME_SIZE);
  187. return 0;
  188. }
  189. static int ads7828_probe(struct i2c_client *client,
  190. const struct i2c_device_id *id)
  191. {
  192. struct ads7828_data *data;
  193. int err;
  194. data = kzalloc(sizeof(struct ads7828_data), GFP_KERNEL);
  195. if (!data) {
  196. err = -ENOMEM;
  197. goto exit;
  198. }
  199. i2c_set_clientdata(client, data);
  200. mutex_init(&data->update_lock);
  201. /* Register sysfs hooks */
  202. err = sysfs_create_group(&client->dev.kobj, &ads7828_group);
  203. if (err)
  204. goto exit_free;
  205. data->hwmon_dev = hwmon_device_register(&client->dev);
  206. if (IS_ERR(data->hwmon_dev)) {
  207. err = PTR_ERR(data->hwmon_dev);
  208. goto exit_remove;
  209. }
  210. return 0;
  211. exit_remove:
  212. sysfs_remove_group(&client->dev.kobj, &ads7828_group);
  213. exit_free:
  214. kfree(data);
  215. exit:
  216. return err;
  217. }
  218. static int __init sensors_ads7828_init(void)
  219. {
  220. /* Initialize the command byte according to module parameters */
  221. ads7828_cmd_byte = se_input ?
  222. ADS7828_CMD_SD_SE : ADS7828_CMD_SD_DIFF;
  223. ads7828_cmd_byte |= int_vref ?
  224. ADS7828_CMD_PD3 : ADS7828_CMD_PD1;
  225. /* Calculate the LSB resolution */
  226. ads7828_lsb_resol = (vref_mv*1000)/4096;
  227. return i2c_add_driver(&ads7828_driver);
  228. }
  229. static void __exit sensors_ads7828_exit(void)
  230. {
  231. i2c_del_driver(&ads7828_driver);
  232. }
  233. MODULE_AUTHOR("Steve Hardy <steve@linuxrealtime.co.uk>");
  234. MODULE_DESCRIPTION("ADS7828 driver");
  235. MODULE_LICENSE("GPL");
  236. module_init(sensors_ads7828_init);
  237. module_exit(sensors_ads7828_exit);