ads7828.c 7.6 KB

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