ads1015.c 8.0 KB

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