ds1803.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Maxim Integrated DS1803 digital potentiometer driver
  3. * Copyright (c) 2016 Slawomir Stepien
  4. *
  5. * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf
  6. *
  7. * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
  8. * ds1803 2 256 10, 50, 100 0101xxx
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/export.h>
  16. #include <linux/i2c.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #define DS1803_MAX_POS 255
  21. #define DS1803_WRITE(chan) (0xa8 | ((chan) + 1))
  22. enum ds1803_type {
  23. DS1803_010,
  24. DS1803_050,
  25. DS1803_100,
  26. };
  27. struct ds1803_cfg {
  28. int kohms;
  29. };
  30. static const struct ds1803_cfg ds1803_cfg[] = {
  31. [DS1803_010] = { .kohms = 10, },
  32. [DS1803_050] = { .kohms = 50, },
  33. [DS1803_100] = { .kohms = 100, },
  34. };
  35. struct ds1803_data {
  36. struct i2c_client *client;
  37. const struct ds1803_cfg *cfg;
  38. };
  39. #define DS1803_CHANNEL(ch) { \
  40. .type = IIO_RESISTANCE, \
  41. .indexed = 1, \
  42. .output = 1, \
  43. .channel = (ch), \
  44. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  45. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  46. }
  47. static const struct iio_chan_spec ds1803_channels[] = {
  48. DS1803_CHANNEL(0),
  49. DS1803_CHANNEL(1),
  50. };
  51. static int ds1803_read_raw(struct iio_dev *indio_dev,
  52. struct iio_chan_spec const *chan,
  53. int *val, int *val2, long mask)
  54. {
  55. struct ds1803_data *data = iio_priv(indio_dev);
  56. int pot = chan->channel;
  57. int ret;
  58. u8 result[ARRAY_SIZE(ds1803_channels)];
  59. switch (mask) {
  60. case IIO_CHAN_INFO_RAW:
  61. ret = i2c_master_recv(data->client, result,
  62. indio_dev->num_channels);
  63. if (ret < 0)
  64. return ret;
  65. *val = result[pot];
  66. return IIO_VAL_INT;
  67. case IIO_CHAN_INFO_SCALE:
  68. *val = 1000 * data->cfg->kohms;
  69. *val2 = DS1803_MAX_POS;
  70. return IIO_VAL_FRACTIONAL;
  71. }
  72. return -EINVAL;
  73. }
  74. static int ds1803_write_raw(struct iio_dev *indio_dev,
  75. struct iio_chan_spec const *chan,
  76. int val, int val2, long mask)
  77. {
  78. struct ds1803_data *data = iio_priv(indio_dev);
  79. int pot = chan->channel;
  80. if (val2 != 0)
  81. return -EINVAL;
  82. switch (mask) {
  83. case IIO_CHAN_INFO_RAW:
  84. if (val > DS1803_MAX_POS || val < 0)
  85. return -EINVAL;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. return i2c_smbus_write_byte_data(data->client, DS1803_WRITE(pot), val);
  91. }
  92. static const struct iio_info ds1803_info = {
  93. .read_raw = ds1803_read_raw,
  94. .write_raw = ds1803_write_raw,
  95. };
  96. static int ds1803_probe(struct i2c_client *client,
  97. const struct i2c_device_id *id)
  98. {
  99. struct device *dev = &client->dev;
  100. struct ds1803_data *data;
  101. struct iio_dev *indio_dev;
  102. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  103. if (!indio_dev)
  104. return -ENOMEM;
  105. i2c_set_clientdata(client, indio_dev);
  106. data = iio_priv(indio_dev);
  107. data->client = client;
  108. data->cfg = &ds1803_cfg[id->driver_data];
  109. indio_dev->dev.parent = dev;
  110. indio_dev->info = &ds1803_info;
  111. indio_dev->channels = ds1803_channels;
  112. indio_dev->num_channels = ARRAY_SIZE(ds1803_channels);
  113. indio_dev->name = client->name;
  114. return devm_iio_device_register(dev, indio_dev);
  115. }
  116. #if defined(CONFIG_OF)
  117. static const struct of_device_id ds1803_dt_ids[] = {
  118. { .compatible = "maxim,ds1803-010", .data = &ds1803_cfg[DS1803_010] },
  119. { .compatible = "maxim,ds1803-050", .data = &ds1803_cfg[DS1803_050] },
  120. { .compatible = "maxim,ds1803-100", .data = &ds1803_cfg[DS1803_100] },
  121. {}
  122. };
  123. MODULE_DEVICE_TABLE(of, ds1803_dt_ids);
  124. #endif /* CONFIG_OF */
  125. static const struct i2c_device_id ds1803_id[] = {
  126. { "ds1803-010", DS1803_010 },
  127. { "ds1803-050", DS1803_050 },
  128. { "ds1803-100", DS1803_100 },
  129. {}
  130. };
  131. MODULE_DEVICE_TABLE(i2c, ds1803_id);
  132. static struct i2c_driver ds1803_driver = {
  133. .driver = {
  134. .name = "ds1803",
  135. .of_match_table = of_match_ptr(ds1803_dt_ids),
  136. },
  137. .probe = ds1803_probe,
  138. .id_table = ds1803_id,
  139. };
  140. module_i2c_driver(ds1803_driver);
  141. MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
  142. MODULE_DESCRIPTION("DS1803 digital potentiometer");
  143. MODULE_LICENSE("GPL v2");