tpl0102.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * tpl0102.c - Support for Texas Instruments digital potentiometers
  4. *
  5. * Copyright (C) 2016, 2018
  6. * Author: Matt Ranostay <matt.ranostay@konsulko.com>
  7. *
  8. * TODO: enable/disable hi-z output control
  9. */
  10. #include <linux/module.h>
  11. #include <linux/i2c.h>
  12. #include <linux/regmap.h>
  13. #include <linux/iio/iio.h>
  14. struct tpl0102_cfg {
  15. int wipers;
  16. int max_pos;
  17. int kohms;
  18. };
  19. enum tpl0102_type {
  20. CAT5140_503,
  21. CAT5140_104,
  22. TPL0102_104,
  23. TPL0401_103,
  24. };
  25. static const struct tpl0102_cfg tpl0102_cfg[] = {
  26. /* on-semiconductor parts */
  27. [CAT5140_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
  28. [CAT5140_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
  29. /* ti parts */
  30. [TPL0102_104] = { .wipers = 2, .max_pos = 256, .kohms = 100 },
  31. [TPL0401_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
  32. };
  33. struct tpl0102_data {
  34. struct regmap *regmap;
  35. unsigned long devid;
  36. };
  37. static const struct regmap_config tpl0102_regmap_config = {
  38. .reg_bits = 8,
  39. .val_bits = 8,
  40. };
  41. #define TPL0102_CHANNEL(ch) { \
  42. .type = IIO_RESISTANCE, \
  43. .indexed = 1, \
  44. .output = 1, \
  45. .channel = (ch), \
  46. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  47. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  48. }
  49. static const struct iio_chan_spec tpl0102_channels[] = {
  50. TPL0102_CHANNEL(0),
  51. TPL0102_CHANNEL(1),
  52. };
  53. static int tpl0102_read_raw(struct iio_dev *indio_dev,
  54. struct iio_chan_spec const *chan,
  55. int *val, int *val2, long mask)
  56. {
  57. struct tpl0102_data *data = iio_priv(indio_dev);
  58. switch (mask) {
  59. case IIO_CHAN_INFO_RAW: {
  60. int ret = regmap_read(data->regmap, chan->channel, val);
  61. return ret ? ret : IIO_VAL_INT;
  62. }
  63. case IIO_CHAN_INFO_SCALE:
  64. *val = 1000 * tpl0102_cfg[data->devid].kohms;
  65. *val2 = tpl0102_cfg[data->devid].max_pos;
  66. return IIO_VAL_FRACTIONAL;
  67. }
  68. return -EINVAL;
  69. }
  70. static int tpl0102_write_raw(struct iio_dev *indio_dev,
  71. struct iio_chan_spec const *chan,
  72. int val, int val2, long mask)
  73. {
  74. struct tpl0102_data *data = iio_priv(indio_dev);
  75. if (mask != IIO_CHAN_INFO_RAW)
  76. return -EINVAL;
  77. if (val >= tpl0102_cfg[data->devid].max_pos || val < 0)
  78. return -EINVAL;
  79. return regmap_write(data->regmap, chan->channel, val);
  80. }
  81. static const struct iio_info tpl0102_info = {
  82. .read_raw = tpl0102_read_raw,
  83. .write_raw = tpl0102_write_raw,
  84. };
  85. static int tpl0102_probe(struct i2c_client *client,
  86. const struct i2c_device_id *id)
  87. {
  88. struct device *dev = &client->dev;
  89. struct tpl0102_data *data;
  90. struct iio_dev *indio_dev;
  91. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  92. if (!indio_dev)
  93. return -ENOMEM;
  94. data = iio_priv(indio_dev);
  95. i2c_set_clientdata(client, indio_dev);
  96. data->devid = id->driver_data;
  97. data->regmap = devm_regmap_init_i2c(client, &tpl0102_regmap_config);
  98. if (IS_ERR(data->regmap)) {
  99. dev_err(dev, "regmap initialization failed\n");
  100. return PTR_ERR(data->regmap);
  101. }
  102. indio_dev->dev.parent = dev;
  103. indio_dev->info = &tpl0102_info;
  104. indio_dev->channels = tpl0102_channels;
  105. indio_dev->num_channels = tpl0102_cfg[data->devid].wipers;
  106. indio_dev->name = client->name;
  107. return devm_iio_device_register(dev, indio_dev);
  108. }
  109. static const struct i2c_device_id tpl0102_id[] = {
  110. { "cat5140-503", CAT5140_503 },
  111. { "cat5140-104", CAT5140_104 },
  112. { "tpl0102-104", TPL0102_104 },
  113. { "tpl0401-103", TPL0401_103 },
  114. {}
  115. };
  116. MODULE_DEVICE_TABLE(i2c, tpl0102_id);
  117. static struct i2c_driver tpl0102_driver = {
  118. .driver = {
  119. .name = "tpl0102",
  120. },
  121. .probe = tpl0102_probe,
  122. .id_table = tpl0102_id,
  123. };
  124. module_i2c_driver(tpl0102_driver);
  125. MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
  126. MODULE_DESCRIPTION("TPL0102 digital potentiometer");
  127. MODULE_LICENSE("GPL");