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