ads7828.c 7.6 KB

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