mcp3422.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * mcp3422.c - driver for the Microchip mcp3422/3/4/6/7/8 chip family
  3. *
  4. * Copyright (C) 2013, Angelo Compagnucci
  5. * Author: Angelo Compagnucci <angelo.compagnucci@gmail.com>
  6. *
  7. * Datasheet: http://ww1.microchip.com/downloads/en/devicedoc/22088b.pdf
  8. * http://ww1.microchip.com/downloads/en/DeviceDoc/22226a.pdf
  9. *
  10. * This driver exports the value of analog input voltage to sysfs, the
  11. * voltage unit is nV.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. */
  18. #include <linux/err.h>
  19. #include <linux/i2c.h>
  20. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/of.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. /* Masks */
  27. #define MCP3422_CHANNEL_MASK 0x60
  28. #define MCP3422_PGA_MASK 0x03
  29. #define MCP3422_SRATE_MASK 0x0C
  30. #define MCP3422_SRATE_240 0x0
  31. #define MCP3422_SRATE_60 0x1
  32. #define MCP3422_SRATE_15 0x2
  33. #define MCP3422_SRATE_3 0x3
  34. #define MCP3422_PGA_1 0
  35. #define MCP3422_PGA_2 1
  36. #define MCP3422_PGA_4 2
  37. #define MCP3422_PGA_8 3
  38. #define MCP3422_CONT_SAMPLING 0x10
  39. #define MCP3422_CHANNEL(config) (((config) & MCP3422_CHANNEL_MASK) >> 5)
  40. #define MCP3422_PGA(config) ((config) & MCP3422_PGA_MASK)
  41. #define MCP3422_SAMPLE_RATE(config) (((config) & MCP3422_SRATE_MASK) >> 2)
  42. #define MCP3422_CHANNEL_VALUE(value) (((value) << 5) & MCP3422_CHANNEL_MASK)
  43. #define MCP3422_PGA_VALUE(value) ((value) & MCP3422_PGA_MASK)
  44. #define MCP3422_SAMPLE_RATE_VALUE(value) ((value << 2) & MCP3422_SRATE_MASK)
  45. #define MCP3422_CHAN(_index) \
  46. { \
  47. .type = IIO_VOLTAGE, \
  48. .indexed = 1, \
  49. .channel = _index, \
  50. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
  51. | BIT(IIO_CHAN_INFO_SCALE), \
  52. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  53. }
  54. static const int mcp3422_scales[4][4] = {
  55. { 1000000, 500000, 250000, 125000 },
  56. { 250000 , 125000, 62500 , 31250 },
  57. { 62500 , 31250 , 15625 , 7812 },
  58. { 15625 , 7812 , 3906 , 1953 } };
  59. /* Constant msleep times for data acquisitions */
  60. static const int mcp3422_read_times[4] = {
  61. [MCP3422_SRATE_240] = 1000 / 240,
  62. [MCP3422_SRATE_60] = 1000 / 60,
  63. [MCP3422_SRATE_15] = 1000 / 15,
  64. [MCP3422_SRATE_3] = 1000 / 3 };
  65. /* sample rates to integer conversion table */
  66. static const int mcp3422_sample_rates[4] = {
  67. [MCP3422_SRATE_240] = 240,
  68. [MCP3422_SRATE_60] = 60,
  69. [MCP3422_SRATE_15] = 15,
  70. [MCP3422_SRATE_3] = 3 };
  71. /* sample rates to sign extension table */
  72. static const int mcp3422_sign_extend[4] = {
  73. [MCP3422_SRATE_240] = 11,
  74. [MCP3422_SRATE_60] = 13,
  75. [MCP3422_SRATE_15] = 15,
  76. [MCP3422_SRATE_3] = 17 };
  77. /* Client data (each client gets its own) */
  78. struct mcp3422 {
  79. struct i2c_client *i2c;
  80. u8 id;
  81. u8 config;
  82. u8 pga[4];
  83. struct mutex lock;
  84. };
  85. static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
  86. {
  87. int ret;
  88. mutex_lock(&adc->lock);
  89. ret = i2c_master_send(adc->i2c, &newconfig, 1);
  90. if (ret > 0) {
  91. adc->config = newconfig;
  92. ret = 0;
  93. }
  94. mutex_unlock(&adc->lock);
  95. return ret;
  96. }
  97. static int mcp3422_read(struct mcp3422 *adc, int *value, u8 *config)
  98. {
  99. int ret = 0;
  100. u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
  101. u8 buf[4] = {0, 0, 0, 0};
  102. u32 temp;
  103. if (sample_rate == MCP3422_SRATE_3) {
  104. ret = i2c_master_recv(adc->i2c, buf, 4);
  105. temp = buf[0] << 16 | buf[1] << 8 | buf[2];
  106. *config = buf[3];
  107. } else {
  108. ret = i2c_master_recv(adc->i2c, buf, 3);
  109. temp = buf[0] << 8 | buf[1];
  110. *config = buf[2];
  111. }
  112. *value = sign_extend32(temp, mcp3422_sign_extend[sample_rate]);
  113. return ret;
  114. }
  115. static int mcp3422_read_channel(struct mcp3422 *adc,
  116. struct iio_chan_spec const *channel, int *value)
  117. {
  118. int ret;
  119. u8 config;
  120. u8 req_channel = channel->channel;
  121. if (req_channel != MCP3422_CHANNEL(adc->config)) {
  122. config = adc->config;
  123. config &= ~MCP3422_CHANNEL_MASK;
  124. config |= MCP3422_CHANNEL_VALUE(req_channel);
  125. config &= ~MCP3422_PGA_MASK;
  126. config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
  127. ret = mcp3422_update_config(adc, config);
  128. if (ret < 0)
  129. return ret;
  130. msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
  131. }
  132. return mcp3422_read(adc, value, &config);
  133. }
  134. static int mcp3422_read_raw(struct iio_dev *iio,
  135. struct iio_chan_spec const *channel, int *val1,
  136. int *val2, long mask)
  137. {
  138. struct mcp3422 *adc = iio_priv(iio);
  139. int err;
  140. u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
  141. u8 pga = MCP3422_PGA(adc->config);
  142. switch (mask) {
  143. case IIO_CHAN_INFO_RAW:
  144. err = mcp3422_read_channel(adc, channel, val1);
  145. if (err < 0)
  146. return -EINVAL;
  147. return IIO_VAL_INT;
  148. case IIO_CHAN_INFO_SCALE:
  149. *val1 = 0;
  150. *val2 = mcp3422_scales[sample_rate][pga];
  151. return IIO_VAL_INT_PLUS_NANO;
  152. case IIO_CHAN_INFO_SAMP_FREQ:
  153. *val1 = mcp3422_sample_rates[MCP3422_SAMPLE_RATE(adc->config)];
  154. return IIO_VAL_INT;
  155. default:
  156. break;
  157. }
  158. return -EINVAL;
  159. }
  160. static int mcp3422_write_raw(struct iio_dev *iio,
  161. struct iio_chan_spec const *channel, int val1,
  162. int val2, long mask)
  163. {
  164. struct mcp3422 *adc = iio_priv(iio);
  165. u8 temp;
  166. u8 config = adc->config;
  167. u8 req_channel = channel->channel;
  168. u8 sample_rate = MCP3422_SAMPLE_RATE(config);
  169. u8 i;
  170. switch (mask) {
  171. case IIO_CHAN_INFO_SCALE:
  172. if (val1 != 0)
  173. return -EINVAL;
  174. for (i = 0; i < ARRAY_SIZE(mcp3422_scales[0]); i++) {
  175. if (val2 == mcp3422_scales[sample_rate][i]) {
  176. adc->pga[req_channel] = i;
  177. config &= ~MCP3422_CHANNEL_MASK;
  178. config |= MCP3422_CHANNEL_VALUE(req_channel);
  179. config &= ~MCP3422_PGA_MASK;
  180. config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
  181. return mcp3422_update_config(adc, config);
  182. }
  183. }
  184. return -EINVAL;
  185. case IIO_CHAN_INFO_SAMP_FREQ:
  186. switch (val1) {
  187. case 240:
  188. temp = MCP3422_SRATE_240;
  189. break;
  190. case 60:
  191. temp = MCP3422_SRATE_60;
  192. break;
  193. case 15:
  194. temp = MCP3422_SRATE_15;
  195. break;
  196. case 3:
  197. if (adc->id > 4)
  198. return -EINVAL;
  199. temp = MCP3422_SRATE_3;
  200. break;
  201. default:
  202. return -EINVAL;
  203. }
  204. config &= ~MCP3422_CHANNEL_MASK;
  205. config |= MCP3422_CHANNEL_VALUE(req_channel);
  206. config &= ~MCP3422_SRATE_MASK;
  207. config |= MCP3422_SAMPLE_RATE_VALUE(temp);
  208. return mcp3422_update_config(adc, config);
  209. default:
  210. break;
  211. }
  212. return -EINVAL;
  213. }
  214. static int mcp3422_write_raw_get_fmt(struct iio_dev *indio_dev,
  215. struct iio_chan_spec const *chan, long mask)
  216. {
  217. switch (mask) {
  218. case IIO_CHAN_INFO_SCALE:
  219. return IIO_VAL_INT_PLUS_NANO;
  220. case IIO_CHAN_INFO_SAMP_FREQ:
  221. return IIO_VAL_INT_PLUS_MICRO;
  222. default:
  223. return -EINVAL;
  224. }
  225. }
  226. static ssize_t mcp3422_show_samp_freqs(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
  230. if (adc->id > 4)
  231. return sprintf(buf, "240 60 15\n");
  232. return sprintf(buf, "240 60 15 3\n");
  233. }
  234. static ssize_t mcp3422_show_scales(struct device *dev,
  235. struct device_attribute *attr, char *buf)
  236. {
  237. struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
  238. u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
  239. return sprintf(buf, "0.%09u 0.%09u 0.%09u 0.%09u\n",
  240. mcp3422_scales[sample_rate][0],
  241. mcp3422_scales[sample_rate][1],
  242. mcp3422_scales[sample_rate][2],
  243. mcp3422_scales[sample_rate][3]);
  244. }
  245. static IIO_DEVICE_ATTR(sampling_frequency_available, S_IRUGO,
  246. mcp3422_show_samp_freqs, NULL, 0);
  247. static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
  248. mcp3422_show_scales, NULL, 0);
  249. static struct attribute *mcp3422_attributes[] = {
  250. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  251. &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
  252. NULL,
  253. };
  254. static const struct attribute_group mcp3422_attribute_group = {
  255. .attrs = mcp3422_attributes,
  256. };
  257. static const struct iio_chan_spec mcp3422_channels[] = {
  258. MCP3422_CHAN(0),
  259. MCP3422_CHAN(1),
  260. };
  261. static const struct iio_chan_spec mcp3424_channels[] = {
  262. MCP3422_CHAN(0),
  263. MCP3422_CHAN(1),
  264. MCP3422_CHAN(2),
  265. MCP3422_CHAN(3),
  266. };
  267. static const struct iio_info mcp3422_info = {
  268. .read_raw = mcp3422_read_raw,
  269. .write_raw = mcp3422_write_raw,
  270. .write_raw_get_fmt = mcp3422_write_raw_get_fmt,
  271. .attrs = &mcp3422_attribute_group,
  272. .driver_module = THIS_MODULE,
  273. };
  274. static int mcp3422_probe(struct i2c_client *client,
  275. const struct i2c_device_id *id)
  276. {
  277. struct iio_dev *indio_dev;
  278. struct mcp3422 *adc;
  279. int err;
  280. u8 config;
  281. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  282. return -ENODEV;
  283. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adc));
  284. if (!indio_dev)
  285. return -ENOMEM;
  286. adc = iio_priv(indio_dev);
  287. adc->i2c = client;
  288. adc->id = (u8)(id->driver_data);
  289. mutex_init(&adc->lock);
  290. indio_dev->dev.parent = &client->dev;
  291. indio_dev->name = dev_name(&client->dev);
  292. indio_dev->modes = INDIO_DIRECT_MODE;
  293. indio_dev->info = &mcp3422_info;
  294. switch (adc->id) {
  295. case 2:
  296. case 3:
  297. case 6:
  298. case 7:
  299. indio_dev->channels = mcp3422_channels;
  300. indio_dev->num_channels = ARRAY_SIZE(mcp3422_channels);
  301. break;
  302. case 4:
  303. case 8:
  304. indio_dev->channels = mcp3424_channels;
  305. indio_dev->num_channels = ARRAY_SIZE(mcp3424_channels);
  306. break;
  307. }
  308. /* meaningful default configuration */
  309. config = (MCP3422_CONT_SAMPLING
  310. | MCP3422_CHANNEL_VALUE(1)
  311. | MCP3422_PGA_VALUE(MCP3422_PGA_1)
  312. | MCP3422_SAMPLE_RATE_VALUE(MCP3422_SRATE_240));
  313. mcp3422_update_config(adc, config);
  314. err = devm_iio_device_register(&client->dev, indio_dev);
  315. if (err < 0)
  316. return err;
  317. i2c_set_clientdata(client, indio_dev);
  318. return 0;
  319. }
  320. static const struct i2c_device_id mcp3422_id[] = {
  321. { "mcp3422", 2 },
  322. { "mcp3423", 3 },
  323. { "mcp3424", 4 },
  324. { "mcp3426", 6 },
  325. { "mcp3427", 7 },
  326. { "mcp3428", 8 },
  327. { }
  328. };
  329. MODULE_DEVICE_TABLE(i2c, mcp3422_id);
  330. #ifdef CONFIG_OF
  331. static const struct of_device_id mcp3422_of_match[] = {
  332. { .compatible = "mcp3422" },
  333. { }
  334. };
  335. MODULE_DEVICE_TABLE(of, mcp3422_of_match);
  336. #endif
  337. static struct i2c_driver mcp3422_driver = {
  338. .driver = {
  339. .name = "mcp3422",
  340. .of_match_table = of_match_ptr(mcp3422_of_match),
  341. },
  342. .probe = mcp3422_probe,
  343. .id_table = mcp3422_id,
  344. };
  345. module_i2c_driver(mcp3422_driver);
  346. MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
  347. MODULE_DESCRIPTION("Microchip mcp3422/3/4/6/7/8 driver");
  348. MODULE_LICENSE("GPL v2");