mcp4018.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Industrial I/O driver for Microchip digital potentiometers
  4. * Copyright (c) 2018 Axentia Technologies AB
  5. * Author: Peter Rosin <peda@axentia.se>
  6. *
  7. * Datasheet: http://www.microchip.com/downloads/en/DeviceDoc/22147a.pdf
  8. *
  9. * DEVID #Wipers #Positions Resistor Opts (kOhm)
  10. * mcp4017 1 128 5, 10, 50, 100
  11. * mcp4018 1 128 5, 10, 50, 100
  12. * mcp4019 1 128 5, 10, 50, 100
  13. */
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #define MCP4018_WIPER_MAX 127
  21. struct mcp4018_cfg {
  22. int kohms;
  23. };
  24. enum mcp4018_type {
  25. MCP4018_502,
  26. MCP4018_103,
  27. MCP4018_503,
  28. MCP4018_104,
  29. };
  30. static const struct mcp4018_cfg mcp4018_cfg[] = {
  31. [MCP4018_502] = { .kohms = 5, },
  32. [MCP4018_103] = { .kohms = 10, },
  33. [MCP4018_503] = { .kohms = 50, },
  34. [MCP4018_104] = { .kohms = 100, },
  35. };
  36. struct mcp4018_data {
  37. struct i2c_client *client;
  38. const struct mcp4018_cfg *cfg;
  39. };
  40. static const struct iio_chan_spec mcp4018_channel = {
  41. .type = IIO_RESISTANCE,
  42. .indexed = 1,
  43. .output = 1,
  44. .channel = 0,
  45. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  46. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  47. };
  48. static int mcp4018_read_raw(struct iio_dev *indio_dev,
  49. struct iio_chan_spec const *chan,
  50. int *val, int *val2, long mask)
  51. {
  52. struct mcp4018_data *data = iio_priv(indio_dev);
  53. s32 ret;
  54. switch (mask) {
  55. case IIO_CHAN_INFO_RAW:
  56. ret = i2c_smbus_read_byte(data->client);
  57. if (ret < 0)
  58. return ret;
  59. *val = ret;
  60. return IIO_VAL_INT;
  61. case IIO_CHAN_INFO_SCALE:
  62. *val = 1000 * data->cfg->kohms;
  63. *val2 = MCP4018_WIPER_MAX;
  64. return IIO_VAL_FRACTIONAL;
  65. }
  66. return -EINVAL;
  67. }
  68. static int mcp4018_write_raw(struct iio_dev *indio_dev,
  69. struct iio_chan_spec const *chan,
  70. int val, int val2, long mask)
  71. {
  72. struct mcp4018_data *data = iio_priv(indio_dev);
  73. switch (mask) {
  74. case IIO_CHAN_INFO_RAW:
  75. if (val > MCP4018_WIPER_MAX || val < 0)
  76. return -EINVAL;
  77. break;
  78. default:
  79. return -EINVAL;
  80. }
  81. return i2c_smbus_write_byte(data->client, val);
  82. }
  83. static const struct iio_info mcp4018_info = {
  84. .read_raw = mcp4018_read_raw,
  85. .write_raw = mcp4018_write_raw,
  86. };
  87. static const struct i2c_device_id mcp4018_id[] = {
  88. { "mcp4017-502", MCP4018_502 },
  89. { "mcp4017-103", MCP4018_103 },
  90. { "mcp4017-503", MCP4018_503 },
  91. { "mcp4017-104", MCP4018_104 },
  92. { "mcp4018-502", MCP4018_502 },
  93. { "mcp4018-103", MCP4018_103 },
  94. { "mcp4018-503", MCP4018_503 },
  95. { "mcp4018-104", MCP4018_104 },
  96. { "mcp4019-502", MCP4018_502 },
  97. { "mcp4019-103", MCP4018_103 },
  98. { "mcp4019-503", MCP4018_503 },
  99. { "mcp4019-104", MCP4018_104 },
  100. {}
  101. };
  102. MODULE_DEVICE_TABLE(i2c, mcp4018_id);
  103. #ifdef CONFIG_OF
  104. #define MCP4018_COMPATIBLE(of_compatible, cfg) { \
  105. .compatible = of_compatible, \
  106. .data = &mcp4018_cfg[cfg], \
  107. }
  108. static const struct of_device_id mcp4018_of_match[] = {
  109. MCP4018_COMPATIBLE("microchip,mcp4017-502", MCP4018_502),
  110. MCP4018_COMPATIBLE("microchip,mcp4017-103", MCP4018_103),
  111. MCP4018_COMPATIBLE("microchip,mcp4017-503", MCP4018_503),
  112. MCP4018_COMPATIBLE("microchip,mcp4017-104", MCP4018_104),
  113. MCP4018_COMPATIBLE("microchip,mcp4018-502", MCP4018_502),
  114. MCP4018_COMPATIBLE("microchip,mcp4018-103", MCP4018_103),
  115. MCP4018_COMPATIBLE("microchip,mcp4018-503", MCP4018_503),
  116. MCP4018_COMPATIBLE("microchip,mcp4018-104", MCP4018_104),
  117. MCP4018_COMPATIBLE("microchip,mcp4019-502", MCP4018_502),
  118. MCP4018_COMPATIBLE("microchip,mcp4019-103", MCP4018_103),
  119. MCP4018_COMPATIBLE("microchip,mcp4019-503", MCP4018_503),
  120. MCP4018_COMPATIBLE("microchip,mcp4019-104", MCP4018_104),
  121. { /* sentinel */ }
  122. };
  123. MODULE_DEVICE_TABLE(of, mcp4018_of_match);
  124. #endif
  125. static int mcp4018_probe(struct i2c_client *client)
  126. {
  127. struct device *dev = &client->dev;
  128. struct mcp4018_data *data;
  129. struct iio_dev *indio_dev;
  130. const struct of_device_id *match;
  131. if (!i2c_check_functionality(client->adapter,
  132. I2C_FUNC_SMBUS_BYTE)) {
  133. dev_err(dev, "SMBUS Byte transfers not supported\n");
  134. return -EOPNOTSUPP;
  135. }
  136. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  137. if (!indio_dev)
  138. return -ENOMEM;
  139. data = iio_priv(indio_dev);
  140. i2c_set_clientdata(client, indio_dev);
  141. data->client = client;
  142. match = of_match_device(of_match_ptr(mcp4018_of_match), dev);
  143. if (match)
  144. data->cfg = of_device_get_match_data(dev);
  145. else
  146. data->cfg = &mcp4018_cfg[i2c_match_id(mcp4018_id, client)->driver_data];
  147. indio_dev->dev.parent = dev;
  148. indio_dev->info = &mcp4018_info;
  149. indio_dev->channels = &mcp4018_channel;
  150. indio_dev->num_channels = 1;
  151. indio_dev->name = client->name;
  152. return devm_iio_device_register(dev, indio_dev);
  153. }
  154. static struct i2c_driver mcp4018_driver = {
  155. .driver = {
  156. .name = "mcp4018",
  157. .of_match_table = of_match_ptr(mcp4018_of_match),
  158. },
  159. .probe_new = mcp4018_probe,
  160. .id_table = mcp4018_id,
  161. };
  162. module_i2c_driver(mcp4018_driver);
  163. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  164. MODULE_DESCRIPTION("MCP4018 digital potentiometer");
  165. MODULE_LICENSE("GPL");