tpl0102.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * tpl0102.c - Support for Texas Instruments digital potentiometers
  3. *
  4. * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * TODO: enable/disable hi-z output control
  17. */
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/regmap.h>
  21. #include <linux/iio/iio.h>
  22. struct tpl0102_cfg {
  23. int wipers;
  24. int max_pos;
  25. int kohms;
  26. };
  27. enum tpl0102_type {
  28. CAT5140_503,
  29. CAT5140_104,
  30. TPL0102_104,
  31. TPL0401_103,
  32. };
  33. static const struct tpl0102_cfg tpl0102_cfg[] = {
  34. /* on-semiconductor parts */
  35. [CAT5140_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
  36. [CAT5140_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
  37. /* ti parts */
  38. [TPL0102_104] = { .wipers = 2, .max_pos = 256, .kohms = 100 },
  39. [TPL0401_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
  40. };
  41. struct tpl0102_data {
  42. struct regmap *regmap;
  43. unsigned long devid;
  44. };
  45. static const struct regmap_config tpl0102_regmap_config = {
  46. .reg_bits = 8,
  47. .val_bits = 8,
  48. };
  49. #define TPL0102_CHANNEL(ch) { \
  50. .type = IIO_RESISTANCE, \
  51. .indexed = 1, \
  52. .output = 1, \
  53. .channel = (ch), \
  54. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  55. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  56. }
  57. static const struct iio_chan_spec tpl0102_channels[] = {
  58. TPL0102_CHANNEL(0),
  59. TPL0102_CHANNEL(1),
  60. };
  61. static int tpl0102_read_raw(struct iio_dev *indio_dev,
  62. struct iio_chan_spec const *chan,
  63. int *val, int *val2, long mask)
  64. {
  65. struct tpl0102_data *data = iio_priv(indio_dev);
  66. switch (mask) {
  67. case IIO_CHAN_INFO_RAW: {
  68. int ret = regmap_read(data->regmap, chan->channel, val);
  69. return ret ? ret : IIO_VAL_INT;
  70. }
  71. case IIO_CHAN_INFO_SCALE:
  72. *val = 1000 * tpl0102_cfg[data->devid].kohms;
  73. *val2 = tpl0102_cfg[data->devid].max_pos;
  74. return IIO_VAL_FRACTIONAL;
  75. }
  76. return -EINVAL;
  77. }
  78. static int tpl0102_write_raw(struct iio_dev *indio_dev,
  79. struct iio_chan_spec const *chan,
  80. int val, int val2, long mask)
  81. {
  82. struct tpl0102_data *data = iio_priv(indio_dev);
  83. if (mask != IIO_CHAN_INFO_RAW)
  84. return -EINVAL;
  85. if (val >= tpl0102_cfg[data->devid].max_pos || val < 0)
  86. return -EINVAL;
  87. return regmap_write(data->regmap, chan->channel, val);
  88. }
  89. static const struct iio_info tpl0102_info = {
  90. .read_raw = tpl0102_read_raw,
  91. .write_raw = tpl0102_write_raw,
  92. .driver_module = THIS_MODULE,
  93. };
  94. static int tpl0102_probe(struct i2c_client *client,
  95. const struct i2c_device_id *id)
  96. {
  97. struct device *dev = &client->dev;
  98. struct tpl0102_data *data;
  99. struct iio_dev *indio_dev;
  100. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  101. if (!indio_dev)
  102. return -ENOMEM;
  103. data = iio_priv(indio_dev);
  104. i2c_set_clientdata(client, indio_dev);
  105. data->devid = id->driver_data;
  106. data->regmap = devm_regmap_init_i2c(client, &tpl0102_regmap_config);
  107. if (IS_ERR(data->regmap)) {
  108. dev_err(dev, "regmap initialization failed\n");
  109. return PTR_ERR(data->regmap);
  110. }
  111. indio_dev->dev.parent = dev;
  112. indio_dev->info = &tpl0102_info;
  113. indio_dev->channels = tpl0102_channels;
  114. indio_dev->num_channels = tpl0102_cfg[data->devid].wipers;
  115. indio_dev->name = client->name;
  116. return devm_iio_device_register(dev, indio_dev);
  117. }
  118. static const struct i2c_device_id tpl0102_id[] = {
  119. { "cat5140-503", CAT5140_503 },
  120. { "cat5140-104", CAT5140_104 },
  121. { "tpl0102-104", TPL0102_104 },
  122. { "tpl0401-103", TPL0401_103 },
  123. {}
  124. };
  125. MODULE_DEVICE_TABLE(i2c, tpl0102_id);
  126. static struct i2c_driver tpl0102_driver = {
  127. .driver = {
  128. .name = "tpl0102",
  129. },
  130. .probe = tpl0102_probe,
  131. .id_table = tpl0102_id,
  132. };
  133. module_i2c_driver(tpl0102_driver);
  134. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  135. MODULE_DESCRIPTION("TPL0102 digital potentiometer");
  136. MODULE_LICENSE("GPL");