ads7828.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 i2c_client client;
  55. struct device *hwmon_dev;
  56. struct mutex update_lock; /* mutex protect updates */
  57. char valid; /* !=0 if following fields are valid */
  58. unsigned long last_updated; /* In jiffies */
  59. u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH 12-bit samples */
  60. };
  61. /* Function declaration - necessary due to function dependencies */
  62. static int ads7828_detect(struct i2c_adapter *adapter, int address, int kind);
  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_attach_adapter(struct i2c_adapter *adapter)
  132. {
  133. if (!(adapter->class & I2C_CLASS_HWMON))
  134. return 0;
  135. return i2c_probe(adapter, &addr_data, ads7828_detect);
  136. }
  137. static int ads7828_detach_client(struct i2c_client *client)
  138. {
  139. struct ads7828_data *data = i2c_get_clientdata(client);
  140. hwmon_device_unregister(data->hwmon_dev);
  141. sysfs_remove_group(&client->dev.kobj, &ads7828_group);
  142. i2c_detach_client(client);
  143. kfree(i2c_get_clientdata(client));
  144. return 0;
  145. }
  146. /* This is the driver that will be inserted */
  147. static struct i2c_driver ads7828_driver = {
  148. .driver = {
  149. .name = "ads7828",
  150. },
  151. .attach_adapter = ads7828_attach_adapter,
  152. .detach_client = ads7828_detach_client,
  153. };
  154. /* This function is called by i2c_probe */
  155. static int ads7828_detect(struct i2c_adapter *adapter, int address, int kind)
  156. {
  157. struct i2c_client *client;
  158. struct ads7828_data *data;
  159. int err = 0;
  160. const char *name = "";
  161. /* Check we have a valid client */
  162. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA))
  163. goto exit;
  164. /* OK. For now, we presume we have a valid client. We now create the
  165. client structure, even though we cannot fill it completely yet.
  166. But it allows us to access ads7828_read_value. */
  167. data = kzalloc(sizeof(struct ads7828_data), GFP_KERNEL);
  168. if (!data) {
  169. err = -ENOMEM;
  170. goto exit;
  171. }
  172. client = &data->client;
  173. i2c_set_clientdata(client, data);
  174. client->addr = address;
  175. client->adapter = adapter;
  176. client->driver = &ads7828_driver;
  177. /* Now, we do the remaining detection. There is no identification
  178. dedicated register so attempt to sanity check using knowledge of
  179. the chip
  180. - Read from the 8 channel addresses
  181. - Check the top 4 bits of each result are not set (12 data bits)
  182. */
  183. if (kind < 0) {
  184. int ch;
  185. for (ch = 0; ch < ADS7828_NCH; ch++) {
  186. u16 in_data;
  187. u8 cmd = channel_cmd_byte(ch);
  188. in_data = ads7828_read_value(client, cmd);
  189. if (in_data & 0xF000) {
  190. printk(KERN_DEBUG
  191. "%s : Doesn't look like an ads7828 device\n",
  192. __func__);
  193. goto exit_free;
  194. }
  195. }
  196. }
  197. /* Determine the chip type - only one kind supported! */
  198. if (kind <= 0)
  199. kind = ads7828;
  200. if (kind == ads7828)
  201. name = "ads7828";
  202. /* Fill in the remaining client fields, put it into the global list */
  203. strlcpy(client->name, name, I2C_NAME_SIZE);
  204. mutex_init(&data->update_lock);
  205. /* Tell the I2C layer a new client has arrived */
  206. err = i2c_attach_client(client);
  207. if (err)
  208. goto exit_free;
  209. /* Register sysfs hooks */
  210. err = sysfs_create_group(&client->dev.kobj, &ads7828_group);
  211. if (err)
  212. goto exit_detach;
  213. data->hwmon_dev = hwmon_device_register(&client->dev);
  214. if (IS_ERR(data->hwmon_dev)) {
  215. err = PTR_ERR(data->hwmon_dev);
  216. goto exit_remove;
  217. }
  218. return 0;
  219. exit_remove:
  220. sysfs_remove_group(&client->dev.kobj, &ads7828_group);
  221. exit_detach:
  222. i2c_detach_client(client);
  223. exit_free:
  224. kfree(data);
  225. exit:
  226. return err;
  227. }
  228. static int __init sensors_ads7828_init(void)
  229. {
  230. /* Initialize the command byte according to module parameters */
  231. ads7828_cmd_byte = se_input ?
  232. ADS7828_CMD_SD_SE : ADS7828_CMD_SD_DIFF;
  233. ads7828_cmd_byte |= int_vref ?
  234. ADS7828_CMD_PD3 : ADS7828_CMD_PD1;
  235. /* Calculate the LSB resolution */
  236. ads7828_lsb_resol = (vref_mv*1000)/4096;
  237. return i2c_add_driver(&ads7828_driver);
  238. }
  239. static void __exit sensors_ads7828_exit(void)
  240. {
  241. i2c_del_driver(&ads7828_driver);
  242. }
  243. MODULE_AUTHOR("Steve Hardy <steve@linuxrealtime.co.uk>");
  244. MODULE_DESCRIPTION("ADS7828 driver");
  245. MODULE_LICENSE("GPL");
  246. module_init(sensors_ads7828_init);
  247. module_exit(sensors_ads7828_exit);