ads1015.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /* Data rates in samples per second */
  44. static const unsigned int data_rate_table[8] = {
  45. 128, 250, 490, 920, 1600, 2400, 3300, 3300 };
  46. #define ADS1015_DEFAULT_CHANNELS 0xff
  47. #define ADS1015_DEFAULT_PGA 2
  48. #define ADS1015_DEFAULT_DATA_RATE 4
  49. struct ads1015_data {
  50. struct device *hwmon_dev;
  51. struct mutex update_lock; /* mutex protect updates */
  52. struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
  53. };
  54. static s32 ads1015_read_reg(struct i2c_client *client, unsigned int reg)
  55. {
  56. s32 data = i2c_smbus_read_word_data(client, reg);
  57. return (data < 0) ? data : swab16(data);
  58. }
  59. static s32 ads1015_write_reg(struct i2c_client *client, unsigned int reg,
  60. u16 val)
  61. {
  62. return i2c_smbus_write_word_data(client, reg, swab16(val));
  63. }
  64. static int ads1015_read_value(struct i2c_client *client, unsigned int channel,
  65. int *value)
  66. {
  67. u16 config;
  68. s16 conversion;
  69. struct ads1015_data *data = i2c_get_clientdata(client);
  70. unsigned int pga = data->channel_data[channel].pga;
  71. int fullscale;
  72. unsigned int data_rate = data->channel_data[channel].data_rate;
  73. unsigned int conversion_time_ms;
  74. int res;
  75. mutex_lock(&data->update_lock);
  76. /* get channel parameters */
  77. res = ads1015_read_reg(client, ADS1015_CONFIG);
  78. if (res < 0)
  79. goto err_unlock;
  80. config = res;
  81. fullscale = fullscale_table[pga];
  82. conversion_time_ms = DIV_ROUND_UP(1000, data_rate_table[data_rate]);
  83. /* setup and start single conversion */
  84. config &= 0x001f;
  85. config |= (1 << 15) | (1 << 8);
  86. config |= (channel & 0x0007) << 12;
  87. config |= (pga & 0x0007) << 9;
  88. config |= (data_rate & 0x0007) << 5;
  89. res = ads1015_write_reg(client, ADS1015_CONFIG, config);
  90. if (res < 0)
  91. goto err_unlock;
  92. /* wait until conversion finished */
  93. msleep(conversion_time_ms);
  94. res = ads1015_read_reg(client, ADS1015_CONFIG);
  95. if (res < 0)
  96. goto err_unlock;
  97. config = res;
  98. if (!(config & (1 << 15))) {
  99. /* conversion not finished in time */
  100. res = -EIO;
  101. goto err_unlock;
  102. }
  103. res = ads1015_read_reg(client, ADS1015_CONVERSION);
  104. if (res < 0)
  105. goto err_unlock;
  106. conversion = res;
  107. mutex_unlock(&data->update_lock);
  108. *value = DIV_ROUND_CLOSEST(conversion * fullscale, 0x7ff0);
  109. return 0;
  110. err_unlock:
  111. mutex_unlock(&data->update_lock);
  112. return res;
  113. }
  114. /* sysfs callback function */
  115. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  116. char *buf)
  117. {
  118. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  119. struct i2c_client *client = to_i2c_client(dev);
  120. int in;
  121. int res;
  122. res = ads1015_read_value(client, attr->index, &in);
  123. return (res < 0) ? res : sprintf(buf, "%d\n", in);
  124. }
  125. static const struct sensor_device_attribute ads1015_in[] = {
  126. SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
  127. SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
  128. SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
  129. SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
  130. SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
  131. SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
  132. SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
  133. SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
  134. };
  135. /*
  136. * Driver interface
  137. */
  138. static int ads1015_remove(struct i2c_client *client)
  139. {
  140. struct ads1015_data *data = i2c_get_clientdata(client);
  141. int k;
  142. hwmon_device_unregister(data->hwmon_dev);
  143. for (k = 0; k < ADS1015_CHANNELS; ++k)
  144. device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
  145. kfree(data);
  146. return 0;
  147. }
  148. #ifdef CONFIG_OF
  149. static int ads1015_get_channels_config_of(struct i2c_client *client)
  150. {
  151. struct ads1015_data *data = i2c_get_clientdata(client);
  152. struct device_node *node;
  153. if (!client->dev.of_node
  154. || !of_get_next_child(client->dev.of_node, NULL))
  155. return -EINVAL;
  156. for_each_child_of_node(client->dev.of_node, node) {
  157. const __be32 *property;
  158. int len;
  159. unsigned int channel;
  160. unsigned int pga = ADS1015_DEFAULT_PGA;
  161. unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
  162. property = of_get_property(node, "reg", &len);
  163. if (!property || len != sizeof(int)) {
  164. dev_err(&client->dev, "invalid reg on %s\n",
  165. node->full_name);
  166. continue;
  167. }
  168. channel = be32_to_cpup(property);
  169. if (channel > ADS1015_CHANNELS) {
  170. dev_err(&client->dev,
  171. "invalid channel index %d on %s\n",
  172. channel, node->full_name);
  173. continue;
  174. }
  175. property = of_get_property(node, "ti,gain", &len);
  176. if (property && len == sizeof(int)) {
  177. pga = be32_to_cpup(property);
  178. if (pga > 6) {
  179. dev_err(&client->dev,
  180. "invalid gain on %s\n",
  181. node->full_name);
  182. }
  183. }
  184. property = of_get_property(node, "ti,datarate", &len);
  185. if (property && len == sizeof(int)) {
  186. data_rate = be32_to_cpup(property);
  187. if (data_rate > 7) {
  188. dev_err(&client->dev,
  189. "invalid data_rate on %s\n",
  190. node->full_name);
  191. }
  192. }
  193. data->channel_data[channel].enabled = true;
  194. data->channel_data[channel].pga = pga;
  195. data->channel_data[channel].data_rate = data_rate;
  196. }
  197. return 0;
  198. }
  199. #endif
  200. static void ads1015_get_channels_config(struct i2c_client *client)
  201. {
  202. unsigned int k;
  203. struct ads1015_data *data = i2c_get_clientdata(client);
  204. struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
  205. /* prefer platform data */
  206. if (pdata) {
  207. memcpy(data->channel_data, pdata->channel_data,
  208. sizeof(data->channel_data));
  209. return;
  210. }
  211. #ifdef CONFIG_OF
  212. if (!ads1015_get_channels_config_of(client))
  213. return;
  214. #endif
  215. /* fallback on default configuration */
  216. for (k = 0; k < ADS1015_CHANNELS; ++k) {
  217. data->channel_data[k].enabled = true;
  218. data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
  219. data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
  220. }
  221. }
  222. static int ads1015_probe(struct i2c_client *client,
  223. const struct i2c_device_id *id)
  224. {
  225. struct ads1015_data *data;
  226. int err;
  227. unsigned int k;
  228. data = kzalloc(sizeof(struct ads1015_data), GFP_KERNEL);
  229. if (!data) {
  230. err = -ENOMEM;
  231. goto exit;
  232. }
  233. i2c_set_clientdata(client, data);
  234. mutex_init(&data->update_lock);
  235. /* build sysfs attribute group */
  236. ads1015_get_channels_config(client);
  237. for (k = 0; k < ADS1015_CHANNELS; ++k) {
  238. if (!data->channel_data[k].enabled)
  239. continue;
  240. err = device_create_file(&client->dev, &ads1015_in[k].dev_attr);
  241. if (err)
  242. goto exit_free;
  243. }
  244. data->hwmon_dev = hwmon_device_register(&client->dev);
  245. if (IS_ERR(data->hwmon_dev)) {
  246. err = PTR_ERR(data->hwmon_dev);
  247. goto exit_remove;
  248. }
  249. return 0;
  250. exit_remove:
  251. for (k = 0; k < ADS1015_CHANNELS; ++k)
  252. device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
  253. exit_free:
  254. kfree(data);
  255. exit:
  256. return err;
  257. }
  258. static const struct i2c_device_id ads1015_id[] = {
  259. { "ads1015", 0 },
  260. { }
  261. };
  262. MODULE_DEVICE_TABLE(i2c, ads1015_id);
  263. static struct i2c_driver ads1015_driver = {
  264. .driver = {
  265. .name = "ads1015",
  266. },
  267. .probe = ads1015_probe,
  268. .remove = ads1015_remove,
  269. .id_table = ads1015_id,
  270. };
  271. static int __init sensors_ads1015_init(void)
  272. {
  273. return i2c_add_driver(&ads1015_driver);
  274. }
  275. static void __exit sensors_ads1015_exit(void)
  276. {
  277. i2c_del_driver(&ads1015_driver);
  278. }
  279. MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
  280. MODULE_DESCRIPTION("ADS1015 driver");
  281. MODULE_LICENSE("GPL");
  282. module_init(sensors_ads1015_init);
  283. module_exit(sensors_ads1015_exit);