ads7828.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 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. /*
  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. for (ch = 0; ch < ADS7828_NCH; ch++) {
  173. u16 in_data;
  174. u8 cmd = channel_cmd_byte(ch);
  175. in_data = i2c_smbus_read_word_swapped(client, cmd);
  176. if (in_data & 0xF000) {
  177. pr_debug("%s : Doesn't look like an ads7828 device\n",
  178. __func__);
  179. return -ENODEV;
  180. }
  181. }
  182. strlcpy(info->type, "ads7828", I2C_NAME_SIZE);
  183. return 0;
  184. }
  185. static int ads7828_probe(struct i2c_client *client,
  186. const struct i2c_device_id *id)
  187. {
  188. struct ads7828_data *data;
  189. int err;
  190. data = devm_kzalloc(&client->dev, sizeof(struct ads7828_data),
  191. GFP_KERNEL);
  192. if (!data)
  193. return -ENOMEM;
  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. return err;
  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. return err;
  209. }
  210. static int __init sensors_ads7828_init(void)
  211. {
  212. /* Initialize the command byte according to module parameters */
  213. ads7828_cmd_byte = se_input ?
  214. ADS7828_CMD_SD_SE : ADS7828_CMD_SD_DIFF;
  215. ads7828_cmd_byte |= int_vref ?
  216. ADS7828_CMD_PD3 : ADS7828_CMD_PD1;
  217. /* Calculate the LSB resolution */
  218. ads7828_lsb_resol = (vref_mv*1000)/4096;
  219. return i2c_add_driver(&ads7828_driver);
  220. }
  221. static void __exit sensors_ads7828_exit(void)
  222. {
  223. i2c_del_driver(&ads7828_driver);
  224. }
  225. MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
  226. MODULE_DESCRIPTION("ADS7828 driver");
  227. MODULE_LICENSE("GPL");
  228. module_init(sensors_ads7828_init);
  229. module_exit(sensors_ads7828_exit);