mcp3422.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. /* LSB is in nV to eliminate floating point */
  55. static const u32 rates_to_lsb[] = {1000000, 250000, 62500, 15625};
  56. /*
  57. * scales calculated as:
  58. * rates_to_lsb[sample_rate] / (1 << pga);
  59. * pga is 1 for 0, 2
  60. */
  61. static const int mcp3422_scales[4][4] = {
  62. { 1000000, 250000, 62500, 15625 },
  63. { 500000 , 125000, 31250, 7812 },
  64. { 250000 , 62500 , 15625, 3906 },
  65. { 125000 , 31250 , 7812 , 1953 } };
  66. /* Constant msleep times for data acquisitions */
  67. static const int mcp3422_read_times[4] = {
  68. [MCP3422_SRATE_240] = 1000 / 240,
  69. [MCP3422_SRATE_60] = 1000 / 60,
  70. [MCP3422_SRATE_15] = 1000 / 15,
  71. [MCP3422_SRATE_3] = 1000 / 3 };
  72. /* sample rates to integer conversion table */
  73. static const int mcp3422_sample_rates[4] = {
  74. [MCP3422_SRATE_240] = 240,
  75. [MCP3422_SRATE_60] = 60,
  76. [MCP3422_SRATE_15] = 15,
  77. [MCP3422_SRATE_3] = 3 };
  78. /* sample rates to sign extension table */
  79. static const int mcp3422_sign_extend[4] = {
  80. [MCP3422_SRATE_240] = 11,
  81. [MCP3422_SRATE_60] = 13,
  82. [MCP3422_SRATE_15] = 15,
  83. [MCP3422_SRATE_3] = 17 };
  84. /* Client data (each client gets its own) */
  85. struct mcp3422 {
  86. struct i2c_client *i2c;
  87. u8 id;
  88. u8 config;
  89. u8 pga[4];
  90. struct mutex lock;
  91. };
  92. static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
  93. {
  94. int ret;
  95. mutex_lock(&adc->lock);
  96. ret = i2c_master_send(adc->i2c, &newconfig, 1);
  97. if (ret > 0) {
  98. adc->config = newconfig;
  99. ret = 0;
  100. }
  101. mutex_unlock(&adc->lock);
  102. return ret;
  103. }
  104. static int mcp3422_read(struct mcp3422 *adc, int *value, u8 *config)
  105. {
  106. int ret = 0;
  107. u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
  108. u8 buf[4] = {0, 0, 0, 0};
  109. u32 temp;
  110. if (sample_rate == MCP3422_SRATE_3) {
  111. ret = i2c_master_recv(adc->i2c, buf, 4);
  112. temp = buf[0] << 16 | buf[1] << 8 | buf[2];
  113. *config = buf[3];
  114. } else {
  115. ret = i2c_master_recv(adc->i2c, buf, 3);
  116. temp = buf[0] << 8 | buf[1];
  117. *config = buf[2];
  118. }
  119. *value = sign_extend32(temp, mcp3422_sign_extend[sample_rate]);
  120. return ret;
  121. }
  122. static int mcp3422_read_channel(struct mcp3422 *adc,
  123. struct iio_chan_spec const *channel, int *value)
  124. {
  125. int ret;
  126. u8 config;
  127. u8 req_channel = channel->channel;
  128. if (req_channel != MCP3422_CHANNEL(adc->config)) {
  129. config = adc->config;
  130. config &= ~MCP3422_CHANNEL_MASK;
  131. config |= MCP3422_CHANNEL_VALUE(req_channel);
  132. config &= ~MCP3422_PGA_MASK;
  133. config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
  134. ret = mcp3422_update_config(adc, config);
  135. if (ret < 0)
  136. return ret;
  137. msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
  138. }
  139. return mcp3422_read(adc, value, &config);
  140. }
  141. static int mcp3422_read_raw(struct iio_dev *iio,
  142. struct iio_chan_spec const *channel, int *val1,
  143. int *val2, long mask)
  144. {
  145. struct mcp3422 *adc = iio_priv(iio);
  146. int err;
  147. u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
  148. u8 pga = MCP3422_PGA(adc->config);
  149. switch (mask) {
  150. case IIO_CHAN_INFO_RAW:
  151. err = mcp3422_read_channel(adc, channel, val1);
  152. if (err < 0)
  153. return -EINVAL;
  154. return IIO_VAL_INT;
  155. case IIO_CHAN_INFO_SCALE:
  156. *val1 = 0;
  157. *val2 = mcp3422_scales[sample_rate][pga];
  158. return IIO_VAL_INT_PLUS_NANO;
  159. case IIO_CHAN_INFO_SAMP_FREQ:
  160. *val1 = mcp3422_sample_rates[MCP3422_SAMPLE_RATE(adc->config)];
  161. return IIO_VAL_INT;
  162. default:
  163. break;
  164. }
  165. return -EINVAL;
  166. }
  167. static int mcp3422_write_raw(struct iio_dev *iio,
  168. struct iio_chan_spec const *channel, int val1,
  169. int val2, long mask)
  170. {
  171. struct mcp3422 *adc = iio_priv(iio);
  172. u8 temp;
  173. u8 config = adc->config;
  174. u8 req_channel = channel->channel;
  175. u8 sample_rate = MCP3422_SAMPLE_RATE(config);
  176. u8 i;
  177. switch (mask) {
  178. case IIO_CHAN_INFO_SCALE:
  179. if (val1 != 0)
  180. return -EINVAL;
  181. for (i = 0; i < ARRAY_SIZE(mcp3422_scales[0]); i++) {
  182. if (val2 == mcp3422_scales[sample_rate][i]) {
  183. adc->pga[req_channel] = i;
  184. config &= ~MCP3422_CHANNEL_MASK;
  185. config |= MCP3422_CHANNEL_VALUE(req_channel);
  186. config &= ~MCP3422_PGA_MASK;
  187. config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
  188. return mcp3422_update_config(adc, config);
  189. }
  190. }
  191. return -EINVAL;
  192. case IIO_CHAN_INFO_SAMP_FREQ:
  193. switch (val1) {
  194. case 240:
  195. temp = MCP3422_SRATE_240;
  196. break;
  197. case 60:
  198. temp = MCP3422_SRATE_60;
  199. break;
  200. case 15:
  201. temp = MCP3422_SRATE_15;
  202. break;
  203. case 3:
  204. if (adc->id > 4)
  205. return -EINVAL;
  206. temp = MCP3422_SRATE_3;
  207. break;
  208. default:
  209. return -EINVAL;
  210. }
  211. config &= ~MCP3422_CHANNEL_MASK;
  212. config |= MCP3422_CHANNEL_VALUE(req_channel);
  213. config &= ~MCP3422_SRATE_MASK;
  214. config |= MCP3422_SAMPLE_RATE_VALUE(temp);
  215. return mcp3422_update_config(adc, config);
  216. default:
  217. break;
  218. }
  219. return -EINVAL;
  220. }
  221. static int mcp3422_write_raw_get_fmt(struct iio_dev *indio_dev,
  222. struct iio_chan_spec const *chan, long mask)
  223. {
  224. switch (mask) {
  225. case IIO_CHAN_INFO_SCALE:
  226. return IIO_VAL_INT_PLUS_NANO;
  227. case IIO_CHAN_INFO_SAMP_FREQ:
  228. return IIO_VAL_INT_PLUS_MICRO;
  229. default:
  230. return -EINVAL;
  231. }
  232. }
  233. static ssize_t mcp3422_show_samp_freqs(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
  237. if (adc->id > 4)
  238. return sprintf(buf, "240 60 15\n");
  239. return sprintf(buf, "240 60 15 3\n");
  240. }
  241. static ssize_t mcp3422_show_scales(struct device *dev,
  242. struct device_attribute *attr, char *buf)
  243. {
  244. struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
  245. u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
  246. return sprintf(buf, "0.%09u 0.%09u 0.%09u 0.%09u\n",
  247. mcp3422_scales[sample_rate][0],
  248. mcp3422_scales[sample_rate][1],
  249. mcp3422_scales[sample_rate][2],
  250. mcp3422_scales[sample_rate][3]);
  251. }
  252. static IIO_DEVICE_ATTR(sampling_frequency_available, S_IRUGO,
  253. mcp3422_show_samp_freqs, NULL, 0);
  254. static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
  255. mcp3422_show_scales, NULL, 0);
  256. static struct attribute *mcp3422_attributes[] = {
  257. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  258. &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
  259. NULL,
  260. };
  261. static const struct attribute_group mcp3422_attribute_group = {
  262. .attrs = mcp3422_attributes,
  263. };
  264. static const struct iio_chan_spec mcp3422_channels[] = {
  265. MCP3422_CHAN(0),
  266. MCP3422_CHAN(1),
  267. };
  268. static const struct iio_chan_spec mcp3424_channels[] = {
  269. MCP3422_CHAN(0),
  270. MCP3422_CHAN(1),
  271. MCP3422_CHAN(2),
  272. MCP3422_CHAN(3),
  273. };
  274. static const struct iio_info mcp3422_info = {
  275. .read_raw = mcp3422_read_raw,
  276. .write_raw = mcp3422_write_raw,
  277. .write_raw_get_fmt = mcp3422_write_raw_get_fmt,
  278. .attrs = &mcp3422_attribute_group,
  279. .driver_module = THIS_MODULE,
  280. };
  281. static int mcp3422_probe(struct i2c_client *client,
  282. const struct i2c_device_id *id)
  283. {
  284. struct iio_dev *indio_dev;
  285. struct mcp3422 *adc;
  286. int err;
  287. u8 config;
  288. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  289. return -ENODEV;
  290. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adc));
  291. if (!indio_dev)
  292. return -ENOMEM;
  293. adc = iio_priv(indio_dev);
  294. adc->i2c = client;
  295. adc->id = (u8)(id->driver_data);
  296. mutex_init(&adc->lock);
  297. indio_dev->dev.parent = &client->dev;
  298. indio_dev->name = dev_name(&client->dev);
  299. indio_dev->modes = INDIO_DIRECT_MODE;
  300. indio_dev->info = &mcp3422_info;
  301. switch (adc->id) {
  302. case 2:
  303. case 3:
  304. case 6:
  305. case 7:
  306. indio_dev->channels = mcp3422_channels;
  307. indio_dev->num_channels = ARRAY_SIZE(mcp3422_channels);
  308. break;
  309. case 4:
  310. case 8:
  311. indio_dev->channels = mcp3424_channels;
  312. indio_dev->num_channels = ARRAY_SIZE(mcp3424_channels);
  313. break;
  314. }
  315. /* meaningful default configuration */
  316. config = (MCP3422_CONT_SAMPLING
  317. | MCP3422_CHANNEL_VALUE(1)
  318. | MCP3422_PGA_VALUE(MCP3422_PGA_1)
  319. | MCP3422_SAMPLE_RATE_VALUE(MCP3422_SRATE_240));
  320. mcp3422_update_config(adc, config);
  321. err = devm_iio_device_register(&client->dev, indio_dev);
  322. if (err < 0)
  323. return err;
  324. i2c_set_clientdata(client, indio_dev);
  325. return 0;
  326. }
  327. static const struct i2c_device_id mcp3422_id[] = {
  328. { "mcp3422", 2 },
  329. { "mcp3423", 3 },
  330. { "mcp3424", 4 },
  331. { "mcp3426", 6 },
  332. { "mcp3427", 7 },
  333. { "mcp3428", 8 },
  334. { }
  335. };
  336. MODULE_DEVICE_TABLE(i2c, mcp3422_id);
  337. #ifdef CONFIG_OF
  338. static const struct of_device_id mcp3422_of_match[] = {
  339. { .compatible = "mcp3422" },
  340. { }
  341. };
  342. MODULE_DEVICE_TABLE(of, mcp3422_of_match);
  343. #endif
  344. static struct i2c_driver mcp3422_driver = {
  345. .driver = {
  346. .name = "mcp3422",
  347. .owner = THIS_MODULE,
  348. .of_match_table = of_match_ptr(mcp3422_of_match),
  349. },
  350. .probe = mcp3422_probe,
  351. .id_table = mcp3422_id,
  352. };
  353. module_i2c_driver(mcp3422_driver);
  354. MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
  355. MODULE_DESCRIPTION("Microchip mcp3422/3/4/6/7/8 driver");
  356. MODULE_LICENSE("GPL v2");