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