mcp4531.c 15 KB

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