2 * tpl0102.c - Support for Texas Instruments digital potentiometers
4 * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
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.
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.
16 * TODO: enable/disable hi-z output control
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/regmap.h>
22 #include <linux/iio/iio.h>
37 static const struct tpl0102_cfg tpl0102_cfg[] = {
38 /* on-semiconductor parts */
39 [CAT5140_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
40 [CAT5140_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
42 [TPL0102_104] = { .wipers = 2, .max_pos = 256, .kohms = 100 },
43 [TPL0401_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
47 struct regmap *regmap;
51 static const struct regmap_config tpl0102_regmap_config = {
56 #define TPL0102_CHANNEL(ch) { \
57 .type = IIO_RESISTANCE, \
61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
62 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
65 static const struct iio_chan_spec tpl0102_channels[] = {
70 static int tpl0102_read_raw(struct iio_dev *indio_dev,
71 struct iio_chan_spec const *chan,
72 int *val, int *val2, long mask)
74 struct tpl0102_data *data = iio_priv(indio_dev);
77 case IIO_CHAN_INFO_RAW: {
78 int ret = regmap_read(data->regmap, chan->channel, val);
80 return ret ? ret : IIO_VAL_INT;
82 case IIO_CHAN_INFO_SCALE:
83 *val = 1000 * tpl0102_cfg[data->devid].kohms;
84 *val2 = tpl0102_cfg[data->devid].max_pos;
85 return IIO_VAL_FRACTIONAL;
91 static int tpl0102_write_raw(struct iio_dev *indio_dev,
92 struct iio_chan_spec const *chan,
93 int val, int val2, long mask)
95 struct tpl0102_data *data = iio_priv(indio_dev);
97 if (mask != IIO_CHAN_INFO_RAW)
100 if (val >= tpl0102_cfg[data->devid].max_pos || val < 0)
103 return regmap_write(data->regmap, chan->channel, val);
106 static const struct iio_info tpl0102_info = {
107 .read_raw = tpl0102_read_raw,
108 .write_raw = tpl0102_write_raw,
109 .driver_module = THIS_MODULE,
112 static int tpl0102_probe(struct i2c_client *client,
113 const struct i2c_device_id *id)
115 struct device *dev = &client->dev;
116 struct tpl0102_data *data;
117 struct iio_dev *indio_dev;
119 if (!i2c_check_functionality(client->adapter,
120 I2C_FUNC_SMBUS_WORD_DATA))
123 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
126 data = iio_priv(indio_dev);
127 i2c_set_clientdata(client, indio_dev);
129 data->devid = id->driver_data;
130 data->regmap = devm_regmap_init_i2c(client, &tpl0102_regmap_config);
131 if (IS_ERR(data->regmap)) {
132 dev_err(dev, "regmap initialization failed\n");
133 return PTR_ERR(data->regmap);
136 indio_dev->dev.parent = dev;
137 indio_dev->info = &tpl0102_info;
138 indio_dev->channels = tpl0102_channels;
139 indio_dev->num_channels = tpl0102_cfg[data->devid].wipers;
140 indio_dev->name = client->name;
142 return devm_iio_device_register(dev, indio_dev);
145 static const struct i2c_device_id tpl0102_id[] = {
146 { "cat5140-503", CAT5140_503 },
147 { "cat5140-104", CAT5140_104 },
148 { "tpl0102-104", TPL0102_104 },
149 { "tpl0401-103", TPL0401_103 },
152 MODULE_DEVICE_TABLE(i2c, tpl0102_id);
154 static struct i2c_driver tpl0102_driver = {
158 .probe = tpl0102_probe,
159 .id_table = tpl0102_id,
162 module_i2c_driver(tpl0102_driver);
164 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
165 MODULE_DESCRIPTION("TPL0102 digital potentiometer");
166 MODULE_LICENSE("GPL");