Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: potentiometer: add driver for Maxim Integrated DS1803

The following functions are supported:
- write, read potentiometer value
- potentiometer scale

Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf

Signed-off-by: Slawomir Stepien <sst@poczta.fm>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Slawomir Stepien and committed by
Jonathan Cameron
fbbc5e70 148e45dc

+205
+21
Documentation/devicetree/bindings/iio/potentiometer/ds1803.txt
··· 1 + * Maxim Integrated DS1803 digital potentiometer driver 2 + 3 + The node for this driver must be a child node of a I2C controller, hence 4 + all mandatory properties for your controller must be specified. See directory: 5 + 6 + Documentation/devicetree/bindings/i2c 7 + 8 + for more details. 9 + 10 + Required properties: 11 + - compatible: Must be one of the following, depending on the 12 + model: 13 + "maxim,ds1803-010", 14 + "maxim,ds1803-050", 15 + "maxim,ds1803-100" 16 + 17 + Example: 18 + ds1803: ds1803@1 { 19 + reg = <0x28>; 20 + compatible = "maxim,ds1803-010"; 21 + };
+10
drivers/iio/potentiometer/Kconfig
··· 5 5 6 6 menu "Digital potentiometers" 7 7 8 + config DS1803 9 + tristate "Maxim Integrated DS1803 Digital Potentiometer driver" 10 + depends on I2C 11 + help 12 + Say yes here to build support for the Maxim Integrated DS1803 13 + digital potentiomenter chip. 14 + 15 + To compile this driver as a module, choose M here: the 16 + module will be called ds1803. 17 + 8 18 config MCP4131 9 19 tristate "Microchip MCP413X/414X/415X/416X/423X/424X/425X/426X Digital Potentiometer driver" 10 20 depends on SPI
+1
drivers/iio/potentiometer/Makefile
··· 3 3 # 4 4 5 5 # When adding new entries keep the list in alphabetical order 6 + obj-$(CONFIG_DS1803) += ds1803.o 6 7 obj-$(CONFIG_MCP4131) += mcp4131.o 7 8 obj-$(CONFIG_MCP4531) += mcp4531.o 8 9 obj-$(CONFIG_TPL0102) += tpl0102.o
+173
drivers/iio/potentiometer/ds1803.c
··· 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 + 15 + #include <linux/err.h> 16 + #include <linux/export.h> 17 + #include <linux/i2c.h> 18 + #include <linux/iio/iio.h> 19 + #include <linux/module.h> 20 + #include <linux/of.h> 21 + 22 + #define DS1803_MAX_POS 255 23 + #define DS1803_WRITE(chan) (0xa8 | ((chan) + 1)) 24 + 25 + enum ds1803_type { 26 + DS1803_010, 27 + DS1803_050, 28 + DS1803_100, 29 + }; 30 + 31 + struct ds1803_cfg { 32 + int kohms; 33 + }; 34 + 35 + static const struct ds1803_cfg ds1803_cfg[] = { 36 + [DS1803_010] = { .kohms = 10, }, 37 + [DS1803_050] = { .kohms = 50, }, 38 + [DS1803_100] = { .kohms = 100, }, 39 + }; 40 + 41 + struct ds1803_data { 42 + struct i2c_client *client; 43 + const struct ds1803_cfg *cfg; 44 + }; 45 + 46 + #define DS1803_CHANNEL(ch) { \ 47 + .type = IIO_RESISTANCE, \ 48 + .indexed = 1, \ 49 + .output = 1, \ 50 + .channel = (ch), \ 51 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 52 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 53 + } 54 + 55 + static const struct iio_chan_spec ds1803_channels[] = { 56 + DS1803_CHANNEL(0), 57 + DS1803_CHANNEL(1), 58 + }; 59 + 60 + static int ds1803_read_raw(struct iio_dev *indio_dev, 61 + struct iio_chan_spec const *chan, 62 + int *val, int *val2, long mask) 63 + { 64 + struct ds1803_data *data = iio_priv(indio_dev); 65 + int pot = chan->channel; 66 + int ret; 67 + u8 result[indio_dev->num_channels]; 68 + 69 + switch (mask) { 70 + case IIO_CHAN_INFO_RAW: 71 + ret = i2c_master_recv(data->client, result, 72 + indio_dev->num_channels); 73 + if (ret < 0) 74 + return ret; 75 + 76 + *val = result[pot]; 77 + return IIO_VAL_INT; 78 + 79 + case IIO_CHAN_INFO_SCALE: 80 + *val = 1000 * data->cfg->kohms; 81 + *val2 = DS1803_MAX_POS; 82 + return IIO_VAL_FRACTIONAL; 83 + } 84 + 85 + return -EINVAL; 86 + } 87 + 88 + static int ds1803_write_raw(struct iio_dev *indio_dev, 89 + struct iio_chan_spec const *chan, 90 + int val, int val2, long mask) 91 + { 92 + struct ds1803_data *data = iio_priv(indio_dev); 93 + int pot = chan->channel; 94 + 95 + if (val2 != 0) 96 + return -EINVAL; 97 + 98 + switch (mask) { 99 + case IIO_CHAN_INFO_RAW: 100 + if (val > DS1803_MAX_POS || val < 0) 101 + return -EINVAL; 102 + break; 103 + default: 104 + return -EINVAL; 105 + } 106 + 107 + return i2c_smbus_write_byte_data(data->client, DS1803_WRITE(pot), val); 108 + } 109 + 110 + static const struct iio_info ds1803_info = { 111 + .read_raw = ds1803_read_raw, 112 + .write_raw = ds1803_write_raw, 113 + .driver_module = THIS_MODULE, 114 + }; 115 + 116 + static int ds1803_probe(struct i2c_client *client, 117 + const struct i2c_device_id *id) 118 + { 119 + struct device *dev = &client->dev; 120 + struct ds1803_data *data; 121 + struct iio_dev *indio_dev; 122 + 123 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 124 + if (!indio_dev) 125 + return -ENOMEM; 126 + 127 + i2c_set_clientdata(client, indio_dev); 128 + 129 + data = iio_priv(indio_dev); 130 + data->client = client; 131 + data->cfg = &ds1803_cfg[id->driver_data]; 132 + 133 + indio_dev->dev.parent = dev; 134 + indio_dev->info = &ds1803_info; 135 + indio_dev->channels = ds1803_channels; 136 + indio_dev->num_channels = ARRAY_SIZE(ds1803_channels); 137 + indio_dev->name = client->name; 138 + 139 + return devm_iio_device_register(dev, indio_dev); 140 + } 141 + 142 + #if defined(CONFIG_OF) 143 + static const struct of_device_id ds1803_dt_ids[] = { 144 + { .compatible = "maxim,ds1803-010", .data = &ds1803_cfg[DS1803_010] }, 145 + { .compatible = "maxim,ds1803-050", .data = &ds1803_cfg[DS1803_050] }, 146 + { .compatible = "maxim,ds1803-100", .data = &ds1803_cfg[DS1803_100] }, 147 + {} 148 + }; 149 + MODULE_DEVICE_TABLE(of, ds1803_dt_ids); 150 + #endif /* CONFIG_OF */ 151 + 152 + static const struct i2c_device_id ds1803_id[] = { 153 + { "ds1803-010", DS1803_010 }, 154 + { "ds1803-050", DS1803_050 }, 155 + { "ds1803-100", DS1803_100 }, 156 + {} 157 + }; 158 + MODULE_DEVICE_TABLE(i2c, ds1803_id); 159 + 160 + static struct i2c_driver ds1803_driver = { 161 + .driver = { 162 + .name = "ds1803", 163 + .of_match_table = of_match_ptr(ds1803_dt_ids), 164 + }, 165 + .probe = ds1803_probe, 166 + .id_table = ds1803_id, 167 + }; 168 + 169 + module_i2c_driver(ds1803_driver); 170 + 171 + MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>"); 172 + MODULE_DESCRIPTION("DS1803 digital potentiometer"); 173 + MODULE_LICENSE("GPL v2");