ads1015.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * ads1015.c - lm_sensors driver for ads1015 12-bit 4-input ADC
  3. * (C) Copyright 2010
  4. * Dirk Eibach, Guntermann & Drunck GmbH <eibach@gdsys.de>
  5. *
  6. * Based on the ads7828 driver by Steve Hardy.
  7. *
  8. * Datasheet available at: http://focus.ti.com/lit/ds/symlink/ads1015.pdf
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/i2c.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/err.h>
  32. #include <linux/mutex.h>
  33. #include <linux/of.h>
  34. #include <linux/i2c/ads1015.h>
  35. /* ADS1015 registers */
  36. enum {
  37. ADS1015_CONVERSION = 0,
  38. ADS1015_CONFIG = 1,
  39. };
  40. /* PGA fullscale voltages in mV */
  41. static const unsigned int fullscale_table[8] = {
  42. 6144, 4096, 2048, 1024, 512, 256, 256, 256 };
  43. #define ADS1015_CONFIG_CHANNELS 8
  44. #define ADS1015_DEFAULT_CHANNELS 0xff
  45. struct ads1015_data {
  46. struct device *hwmon_dev;
  47. struct mutex update_lock; /* mutex protect updates */
  48. struct attribute *attr_table[ADS1015_CONFIG_CHANNELS + 1];
  49. struct attribute_group attr_group;
  50. };
  51. static s32 ads1015_read_reg(struct i2c_client *client, unsigned int reg)
  52. {
  53. s32 data = i2c_smbus_read_word_data(client, reg);
  54. return (data < 0) ? data : swab16(data);
  55. }
  56. static s32 ads1015_write_reg(struct i2c_client *client, unsigned int reg,
  57. u16 val)
  58. {
  59. return i2c_smbus_write_word_data(client, reg, swab16(val));
  60. }
  61. static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
  62. int *value)
  63. {
  64. u16 config;
  65. s16 conversion;
  66. unsigned int pga;
  67. int fullscale;
  68. unsigned int k;
  69. struct ads1015_data *data = i2c_get_clientdata(client);
  70. int res;
  71. mutex_lock(&data->update_lock);
  72. /* get fullscale voltage */
  73. res = ads1015_read_reg(client, ADS1015_CONFIG);
  74. if (res < 0)
  75. goto err_unlock;
  76. config = res;
  77. pga = (config >> 9) & 0x0007;
  78. fullscale = fullscale_table[pga];
  79. /* set channel and start single conversion */
  80. config &= ~(0x0007 << 12);
  81. config |= (1 << 15) | (1 << 8) | (channel & 0x0007) << 12;
  82. /* wait until conversion finished */
  83. res = ads1015_write_reg(client, ADS1015_CONFIG, config);
  84. if (res < 0)
  85. goto err_unlock;
  86. for (k = 0; k < 5; ++k) {
  87. msleep(1);
  88. res = ads1015_read_reg(client, ADS1015_CONFIG);
  89. if (res < 0)
  90. goto err_unlock;
  91. config = res;
  92. if (config & (1 << 15))
  93. break;
  94. }
  95. if (k == 5) {
  96. res = -EIO;
  97. goto err_unlock;
  98. }
  99. res = ads1015_read_reg(client, ADS1015_CONVERSION);
  100. if (res < 0)
  101. goto err_unlock;
  102. conversion = res;
  103. mutex_unlock(&data->update_lock);
  104. *value = DIV_ROUND_CLOSEST(conversion * fullscale, 0x7ff0);
  105. return 0;
  106. err_unlock:
  107. mutex_unlock(&data->update_lock);
  108. return res;
  109. }
  110. /* sysfs callback function */
  111. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  112. char *buf)
  113. {
  114. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  115. struct i2c_client *client = to_i2c_client(dev);
  116. int in;
  117. int res;
  118. res = ads1015_read_value(client, attr->index, &in);
  119. return (res < 0) ? res : sprintf(buf, "%d\n", in);
  120. }
  121. #define in_reg(offset)\
  122. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in,\
  123. NULL, offset)
  124. in_reg(0);
  125. in_reg(1);
  126. in_reg(2);
  127. in_reg(3);
  128. in_reg(4);
  129. in_reg(5);
  130. in_reg(6);
  131. in_reg(7);
  132. static struct attribute *all_attributes[] = {
  133. &sensor_dev_attr_in0_input.dev_attr.attr,
  134. &sensor_dev_attr_in1_input.dev_attr.attr,
  135. &sensor_dev_attr_in2_input.dev_attr.attr,
  136. &sensor_dev_attr_in3_input.dev_attr.attr,
  137. &sensor_dev_attr_in4_input.dev_attr.attr,
  138. &sensor_dev_attr_in5_input.dev_attr.attr,
  139. &sensor_dev_attr_in6_input.dev_attr.attr,
  140. &sensor_dev_attr_in7_input.dev_attr.attr,
  141. };
  142. /*
  143. * Driver interface
  144. */
  145. static int ads1015_remove(struct i2c_client *client)
  146. {
  147. struct ads1015_data *data = i2c_get_clientdata(client);
  148. hwmon_device_unregister(data->hwmon_dev);
  149. sysfs_remove_group(&client->dev.kobj, &data->attr_group);
  150. kfree(data);
  151. return 0;
  152. }
  153. static unsigned int ads1015_get_exported_channels(struct i2c_client *client)
  154. {
  155. struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
  156. #ifdef CONFIG_OF
  157. struct device_node *np = client->dev.of_node;
  158. const __be32 *of_channels;
  159. int of_channels_size;
  160. #endif
  161. /* prefer platform data */
  162. if (pdata)
  163. return pdata->exported_channels;
  164. #ifdef CONFIG_OF
  165. /* fallback on OF */
  166. of_channels = of_get_property(np, "exported-channels",
  167. &of_channels_size);
  168. if (of_channels && (of_channels_size == sizeof(*of_channels)))
  169. return be32_to_cpup(of_channels);
  170. #endif
  171. /* fallback on default configuration */
  172. return ADS1015_DEFAULT_CHANNELS;
  173. }
  174. static int ads1015_probe(struct i2c_client *client,
  175. const struct i2c_device_id *id)
  176. {
  177. struct ads1015_data *data;
  178. int err;
  179. unsigned int exported_channels;
  180. unsigned int k;
  181. unsigned int n = 0;
  182. data = kzalloc(sizeof(struct ads1015_data), GFP_KERNEL);
  183. if (!data) {
  184. err = -ENOMEM;
  185. goto exit;
  186. }
  187. i2c_set_clientdata(client, data);
  188. mutex_init(&data->update_lock);
  189. /* build sysfs attribute group */
  190. data->attr_group.attrs = data->attr_table;
  191. exported_channels = ads1015_get_exported_channels(client);
  192. for (k = 0; k < ADS1015_CONFIG_CHANNELS; ++k) {
  193. if (!(exported_channels & (1<<k)))
  194. continue;
  195. data->attr_table[n++] = all_attributes[k];
  196. }
  197. err = sysfs_create_group(&client->dev.kobj, &data->attr_group);
  198. if (err)
  199. goto exit_free;
  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, &data->attr_group);
  208. exit_free:
  209. kfree(data);
  210. exit:
  211. return err;
  212. }
  213. static const struct i2c_device_id ads1015_id[] = {
  214. { "ads1015", 0 },
  215. { }
  216. };
  217. MODULE_DEVICE_TABLE(i2c, ads1015_id);
  218. static struct i2c_driver ads1015_driver = {
  219. .driver = {
  220. .name = "ads1015",
  221. },
  222. .probe = ads1015_probe,
  223. .remove = ads1015_remove,
  224. .id_table = ads1015_id,
  225. };
  226. static int __init sensors_ads1015_init(void)
  227. {
  228. return i2c_add_driver(&ads1015_driver);
  229. }
  230. static void __exit sensors_ads1015_exit(void)
  231. {
  232. i2c_del_driver(&ads1015_driver);
  233. }
  234. MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
  235. MODULE_DESCRIPTION("ADS1015 driver");
  236. MODULE_LICENSE("GPL");
  237. module_init(sensors_ads1015_init);
  238. module_exit(sensors_ads1015_exit);