mcp4531.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Industrial I/O driver for Microchip digital potentiometers
  3. * Copyright (c) 2015 Axentia Technologies AB
  4. * Author: Peter Rosin <peda@axentia.se>
  5. *
  6. * Datasheet: http://www.microchip.com/downloads/en/DeviceDoc/22096b.pdf
  7. *
  8. * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
  9. * mcp4531 1 129 5, 10, 50, 100 010111x
  10. * mcp4532 1 129 5, 10, 50, 100 01011xx
  11. * mcp4541 1 129 5, 10, 50, 100 010111x
  12. * mcp4542 1 129 5, 10, 50, 100 01011xx
  13. * mcp4551 1 257 5, 10, 50, 100 010111x
  14. * mcp4552 1 257 5, 10, 50, 100 01011xx
  15. * mcp4561 1 257 5, 10, 50, 100 010111x
  16. * mcp4562 1 257 5, 10, 50, 100 01011xx
  17. * mcp4631 2 129 5, 10, 50, 100 0101xxx
  18. * mcp4632 2 129 5, 10, 50, 100 01011xx
  19. * mcp4641 2 129 5, 10, 50, 100 0101xxx
  20. * mcp4642 2 129 5, 10, 50, 100 01011xx
  21. * mcp4651 2 257 5, 10, 50, 100 0101xxx
  22. * mcp4652 2 257 5, 10, 50, 100 01011xx
  23. * mcp4661 2 257 5, 10, 50, 100 0101xxx
  24. * mcp4662 2 257 5, 10, 50, 100 01011xx
  25. *
  26. * This program is free software; you can redistribute it and/or modify it
  27. * under the terms of the GNU General Public License version 2 as published by
  28. * the Free Software Foundation.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/i2c.h>
  32. #include <linux/err.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/iio/iio.h>
  36. struct mcp4531_cfg {
  37. int wipers;
  38. int max_pos;
  39. int kohms;
  40. };
  41. enum mcp4531_type {
  42. MCP453x_502,
  43. MCP453x_103,
  44. MCP453x_503,
  45. MCP453x_104,
  46. MCP454x_502,
  47. MCP454x_103,
  48. MCP454x_503,
  49. MCP454x_104,
  50. MCP455x_502,
  51. MCP455x_103,
  52. MCP455x_503,
  53. MCP455x_104,
  54. MCP456x_502,
  55. MCP456x_103,
  56. MCP456x_503,
  57. MCP456x_104,
  58. MCP463x_502,
  59. MCP463x_103,
  60. MCP463x_503,
  61. MCP463x_104,
  62. MCP464x_502,
  63. MCP464x_103,
  64. MCP464x_503,
  65. MCP464x_104,
  66. MCP465x_502,
  67. MCP465x_103,
  68. MCP465x_503,
  69. MCP465x_104,
  70. MCP466x_502,
  71. MCP466x_103,
  72. MCP466x_503,
  73. MCP466x_104,
  74. };
  75. static const struct mcp4531_cfg mcp4531_cfg[] = {
  76. [MCP453x_502] = { .wipers = 1, .max_pos = 128, .kohms = 5, },
  77. [MCP453x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
  78. [MCP453x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, },
  79. [MCP453x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, },
  80. [MCP454x_502] = { .wipers = 1, .max_pos = 128, .kohms = 5, },
  81. [MCP454x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
  82. [MCP454x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, },
  83. [MCP454x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, },
  84. [MCP455x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, },
  85. [MCP455x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, },
  86. [MCP455x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
  87. [MCP455x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
  88. [MCP456x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, },
  89. [MCP456x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, },
  90. [MCP456x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
  91. [MCP456x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
  92. [MCP463x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, },
  93. [MCP463x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, },
  94. [MCP463x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, },
  95. [MCP463x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, },
  96. [MCP464x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, },
  97. [MCP464x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, },
  98. [MCP464x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, },
  99. [MCP464x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, },
  100. [MCP465x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, },
  101. [MCP465x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, },
  102. [MCP465x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, },
  103. [MCP465x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, },
  104. [MCP466x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, },
  105. [MCP466x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, },
  106. [MCP466x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, },
  107. [MCP466x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, },
  108. };
  109. #define MCP4531_WRITE (0 << 2)
  110. #define MCP4531_INCR (1 << 2)
  111. #define MCP4531_DECR (2 << 2)
  112. #define MCP4531_READ (3 << 2)
  113. #define MCP4531_WIPER_SHIFT (4)
  114. struct mcp4531_data {
  115. struct i2c_client *client;
  116. const struct mcp4531_cfg *cfg;
  117. };
  118. #define MCP4531_CHANNEL(ch) { \
  119. .type = IIO_RESISTANCE, \
  120. .indexed = 1, \
  121. .output = 1, \
  122. .channel = (ch), \
  123. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  124. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  125. }
  126. static const struct iio_chan_spec mcp4531_channels[] = {
  127. MCP4531_CHANNEL(0),
  128. MCP4531_CHANNEL(1),
  129. };
  130. static int mcp4531_read_raw(struct iio_dev *indio_dev,
  131. struct iio_chan_spec const *chan,
  132. int *val, int *val2, long mask)
  133. {
  134. struct mcp4531_data *data = iio_priv(indio_dev);
  135. int address = chan->channel << MCP4531_WIPER_SHIFT;
  136. s32 ret;
  137. switch (mask) {
  138. case IIO_CHAN_INFO_RAW:
  139. ret = i2c_smbus_read_word_swapped(data->client,
  140. MCP4531_READ | address);
  141. if (ret < 0)
  142. return ret;
  143. *val = ret;
  144. return IIO_VAL_INT;
  145. case IIO_CHAN_INFO_SCALE:
  146. *val = 1000 * data->cfg->kohms;
  147. *val2 = data->cfg->max_pos;
  148. return IIO_VAL_FRACTIONAL;
  149. }
  150. return -EINVAL;
  151. }
  152. static int mcp4531_write_raw(struct iio_dev *indio_dev,
  153. struct iio_chan_spec const *chan,
  154. int val, int val2, long mask)
  155. {
  156. struct mcp4531_data *data = iio_priv(indio_dev);
  157. int address = chan->channel << MCP4531_WIPER_SHIFT;
  158. switch (mask) {
  159. case IIO_CHAN_INFO_RAW:
  160. if (val > data->cfg->max_pos || val < 0)
  161. return -EINVAL;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. return i2c_smbus_write_byte_data(data->client,
  167. MCP4531_WRITE | address | (val >> 8),
  168. val & 0xff);
  169. }
  170. static const struct iio_info mcp4531_info = {
  171. .read_raw = mcp4531_read_raw,
  172. .write_raw = mcp4531_write_raw,
  173. .driver_module = THIS_MODULE,
  174. };
  175. #ifdef CONFIG_OF
  176. #define MCP4531_COMPATIBLE(of_compatible, cfg) { \
  177. .compatible = of_compatible, \
  178. .data = &mcp4531_cfg[cfg], \
  179. }
  180. static const struct of_device_id mcp4531_of_match[] = {
  181. MCP4531_COMPATIBLE("microchip,mcp4531-502", MCP453x_502),
  182. MCP4531_COMPATIBLE("microchip,mcp4531-103", MCP453x_103),
  183. MCP4531_COMPATIBLE("microchip,mcp4531-503", MCP453x_503),
  184. MCP4531_COMPATIBLE("microchip,mcp4531-104", MCP453x_104),
  185. MCP4531_COMPATIBLE("microchip,mcp4532-502", MCP453x_502),
  186. MCP4531_COMPATIBLE("microchip,mcp4532-103", MCP453x_103),
  187. MCP4531_COMPATIBLE("microchip,mcp4532-503", MCP453x_503),
  188. MCP4531_COMPATIBLE("microchip,mcp4532-104", MCP453x_104),
  189. MCP4531_COMPATIBLE("microchip,mcp4541-502", MCP454x_502),
  190. MCP4531_COMPATIBLE("microchip,mcp4541-103", MCP454x_103),
  191. MCP4531_COMPATIBLE("microchip,mcp4541-503", MCP454x_503),
  192. MCP4531_COMPATIBLE("microchip,mcp4541-104", MCP454x_104),
  193. MCP4531_COMPATIBLE("microchip,mcp4542-502", MCP454x_502),
  194. MCP4531_COMPATIBLE("microchip,mcp4542-103", MCP454x_103),
  195. MCP4531_COMPATIBLE("microchip,mcp4542-503", MCP454x_503),
  196. MCP4531_COMPATIBLE("microchip,mcp4542-104", MCP454x_104),
  197. MCP4531_COMPATIBLE("microchip,mcp4551-502", MCP455x_502),
  198. MCP4531_COMPATIBLE("microchip,mcp4551-103", MCP455x_103),
  199. MCP4531_COMPATIBLE("microchip,mcp4551-503", MCP455x_503),
  200. MCP4531_COMPATIBLE("microchip,mcp4551-104", MCP455x_104),
  201. MCP4531_COMPATIBLE("microchip,mcp4552-502", MCP455x_502),
  202. MCP4531_COMPATIBLE("microchip,mcp4552-103", MCP455x_103),
  203. MCP4531_COMPATIBLE("microchip,mcp4552-503", MCP455x_503),
  204. MCP4531_COMPATIBLE("microchip,mcp4552-104", MCP455x_104),
  205. MCP4531_COMPATIBLE("microchip,mcp4561-502", MCP456x_502),
  206. MCP4531_COMPATIBLE("microchip,mcp4561-103", MCP456x_103),
  207. MCP4531_COMPATIBLE("microchip,mcp4561-503", MCP456x_503),
  208. MCP4531_COMPATIBLE("microchip,mcp4561-104", MCP456x_104),
  209. MCP4531_COMPATIBLE("microchip,mcp4562-502", MCP456x_502),
  210. MCP4531_COMPATIBLE("microchip,mcp4562-103", MCP456x_103),
  211. MCP4531_COMPATIBLE("microchip,mcp4562-503", MCP456x_503),
  212. MCP4531_COMPATIBLE("microchip,mcp4562-104", MCP456x_104),
  213. MCP4531_COMPATIBLE("microchip,mcp4631-502", MCP463x_502),
  214. MCP4531_COMPATIBLE("microchip,mcp4631-103", MCP463x_103),
  215. MCP4531_COMPATIBLE("microchip,mcp4631-503", MCP463x_503),
  216. MCP4531_COMPATIBLE("microchip,mcp4631-104", MCP463x_104),
  217. MCP4531_COMPATIBLE("microchip,mcp4632-502", MCP463x_502),
  218. MCP4531_COMPATIBLE("microchip,mcp4632-103", MCP463x_103),
  219. MCP4531_COMPATIBLE("microchip,mcp4632-503", MCP463x_503),
  220. MCP4531_COMPATIBLE("microchip,mcp4632-104", MCP463x_104),
  221. MCP4531_COMPATIBLE("microchip,mcp4641-502", MCP464x_502),
  222. MCP4531_COMPATIBLE("microchip,mcp4641-103", MCP464x_103),
  223. MCP4531_COMPATIBLE("microchip,mcp4641-503", MCP464x_503),
  224. MCP4531_COMPATIBLE("microchip,mcp4641-104", MCP464x_104),
  225. MCP4531_COMPATIBLE("microchip,mcp4642-502", MCP464x_502),
  226. MCP4531_COMPATIBLE("microchip,mcp4642-103", MCP464x_103),
  227. MCP4531_COMPATIBLE("microchip,mcp4642-503", MCP464x_503),
  228. MCP4531_COMPATIBLE("microchip,mcp4642-104", MCP464x_104),
  229. MCP4531_COMPATIBLE("microchip,mcp4651-502", MCP465x_502),
  230. MCP4531_COMPATIBLE("microchip,mcp4651-103", MCP465x_103),
  231. MCP4531_COMPATIBLE("microchip,mcp4651-503", MCP465x_503),
  232. MCP4531_COMPATIBLE("microchip,mcp4651-104", MCP465x_104),
  233. MCP4531_COMPATIBLE("microchip,mcp4652-502", MCP465x_502),
  234. MCP4531_COMPATIBLE("microchip,mcp4652-103", MCP465x_103),
  235. MCP4531_COMPATIBLE("microchip,mcp4652-503", MCP465x_503),
  236. MCP4531_COMPATIBLE("microchip,mcp4652-104", MCP465x_104),
  237. MCP4531_COMPATIBLE("microchip,mcp4661-502", MCP466x_502),
  238. MCP4531_COMPATIBLE("microchip,mcp4661-103", MCP466x_103),
  239. MCP4531_COMPATIBLE("microchip,mcp4661-503", MCP466x_503),
  240. MCP4531_COMPATIBLE("microchip,mcp4661-104", MCP466x_104),
  241. MCP4531_COMPATIBLE("microchip,mcp4662-502", MCP466x_502),
  242. MCP4531_COMPATIBLE("microchip,mcp4662-103", MCP466x_103),
  243. MCP4531_COMPATIBLE("microchip,mcp4662-503", MCP466x_503),
  244. MCP4531_COMPATIBLE("microchip,mcp4662-104", MCP466x_104),
  245. { /* sentinel */ }
  246. };
  247. #endif
  248. static int mcp4531_probe(struct i2c_client *client,
  249. const struct i2c_device_id *id)
  250. {
  251. struct device *dev = &client->dev;
  252. struct mcp4531_data *data;
  253. struct iio_dev *indio_dev;
  254. const struct of_device_id *match;
  255. if (!i2c_check_functionality(client->adapter,
  256. I2C_FUNC_SMBUS_WORD_DATA)) {
  257. dev_err(dev, "SMBUS Word Data not supported\n");
  258. return -EOPNOTSUPP;
  259. }
  260. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  261. if (!indio_dev)
  262. return -ENOMEM;
  263. data = iio_priv(indio_dev);
  264. i2c_set_clientdata(client, indio_dev);
  265. data->client = client;
  266. match = of_match_device(of_match_ptr(mcp4531_of_match), dev);
  267. if (match)
  268. data->cfg = of_device_get_match_data(dev);
  269. else
  270. data->cfg = &mcp4531_cfg[id->driver_data];
  271. indio_dev->dev.parent = dev;
  272. indio_dev->info = &mcp4531_info;
  273. indio_dev->channels = mcp4531_channels;
  274. indio_dev->num_channels = data->cfg->wipers;
  275. indio_dev->name = client->name;
  276. return devm_iio_device_register(dev, indio_dev);
  277. }
  278. static const struct i2c_device_id mcp4531_id[] = {
  279. { "mcp4531-502", MCP453x_502 },
  280. { "mcp4531-103", MCP453x_103 },
  281. { "mcp4531-503", MCP453x_503 },
  282. { "mcp4531-104", MCP453x_104 },
  283. { "mcp4532-502", MCP453x_502 },
  284. { "mcp4532-103", MCP453x_103 },
  285. { "mcp4532-503", MCP453x_503 },
  286. { "mcp4532-104", MCP453x_104 },
  287. { "mcp4541-502", MCP454x_502 },
  288. { "mcp4541-103", MCP454x_103 },
  289. { "mcp4541-503", MCP454x_503 },
  290. { "mcp4541-104", MCP454x_104 },
  291. { "mcp4542-502", MCP454x_502 },
  292. { "mcp4542-103", MCP454x_103 },
  293. { "mcp4542-503", MCP454x_503 },
  294. { "mcp4542-104", MCP454x_104 },
  295. { "mcp4551-502", MCP455x_502 },
  296. { "mcp4551-103", MCP455x_103 },
  297. { "mcp4551-503", MCP455x_503 },
  298. { "mcp4551-104", MCP455x_104 },
  299. { "mcp4552-502", MCP455x_502 },
  300. { "mcp4552-103", MCP455x_103 },
  301. { "mcp4552-503", MCP455x_503 },
  302. { "mcp4552-104", MCP455x_104 },
  303. { "mcp4561-502", MCP456x_502 },
  304. { "mcp4561-103", MCP456x_103 },
  305. { "mcp4561-503", MCP456x_503 },
  306. { "mcp4561-104", MCP456x_104 },
  307. { "mcp4562-502", MCP456x_502 },
  308. { "mcp4562-103", MCP456x_103 },
  309. { "mcp4562-503", MCP456x_503 },
  310. { "mcp4562-104", MCP456x_104 },
  311. { "mcp4631-502", MCP463x_502 },
  312. { "mcp4631-103", MCP463x_103 },
  313. { "mcp4631-503", MCP463x_503 },
  314. { "mcp4631-104", MCP463x_104 },
  315. { "mcp4632-502", MCP463x_502 },
  316. { "mcp4632-103", MCP463x_103 },
  317. { "mcp4632-503", MCP463x_503 },
  318. { "mcp4632-104", MCP463x_104 },
  319. { "mcp4641-502", MCP464x_502 },
  320. { "mcp4641-103", MCP464x_103 },
  321. { "mcp4641-503", MCP464x_503 },
  322. { "mcp4641-104", MCP464x_104 },
  323. { "mcp4642-502", MCP464x_502 },
  324. { "mcp4642-103", MCP464x_103 },
  325. { "mcp4642-503", MCP464x_503 },
  326. { "mcp4642-104", MCP464x_104 },
  327. { "mcp4651-502", MCP465x_502 },
  328. { "mcp4651-103", MCP465x_103 },
  329. { "mcp4651-503", MCP465x_503 },
  330. { "mcp4651-104", MCP465x_104 },
  331. { "mcp4652-502", MCP465x_502 },
  332. { "mcp4652-103", MCP465x_103 },
  333. { "mcp4652-503", MCP465x_503 },
  334. { "mcp4652-104", MCP465x_104 },
  335. { "mcp4661-502", MCP466x_502 },
  336. { "mcp4661-103", MCP466x_103 },
  337. { "mcp4661-503", MCP466x_503 },
  338. { "mcp4661-104", MCP466x_104 },
  339. { "mcp4662-502", MCP466x_502 },
  340. { "mcp4662-103", MCP466x_103 },
  341. { "mcp4662-503", MCP466x_503 },
  342. { "mcp4662-104", MCP466x_104 },
  343. {}
  344. };
  345. MODULE_DEVICE_TABLE(i2c, mcp4531_id);
  346. static struct i2c_driver mcp4531_driver = {
  347. .driver = {
  348. .name = "mcp4531",
  349. .of_match_table = of_match_ptr(mcp4531_of_match),
  350. },
  351. .probe = mcp4531_probe,
  352. .id_table = mcp4531_id,
  353. };
  354. module_i2c_driver(mcp4531_driver);
  355. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  356. MODULE_DESCRIPTION("MCP4531 digital potentiometer");
  357. MODULE_LICENSE("GPL");