]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/iio/potentiometer/ds1803.c
Merge remote-tracking branches 'asoc/fix/intel', 'asoc/fix/topology' and 'asoc/fix...
[mirror_ubuntu-bionic-kernel.git] / drivers / iio / potentiometer / ds1803.c
CommitLineData
fbbc5e70
SS
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
25enum ds1803_type {
26 DS1803_010,
27 DS1803_050,
28 DS1803_100,
29};
30
31struct ds1803_cfg {
32 int kohms;
33};
34
35static const struct ds1803_cfg ds1803_cfg[] = {
36 [DS1803_010] = { .kohms = 10, },
37 [DS1803_050] = { .kohms = 50, },
38 [DS1803_100] = { .kohms = 100, },
39};
40
41struct 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
55static const struct iio_chan_spec ds1803_channels[] = {
56 DS1803_CHANNEL(0),
57 DS1803_CHANNEL(1),
58};
59
60static 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
88static 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
110static 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
116static 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)
143static 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};
149MODULE_DEVICE_TABLE(of, ds1803_dt_ids);
150#endif /* CONFIG_OF */
151
152static const struct i2c_device_id ds1803_id[] = {
153 { "ds1803-010", DS1803_010 },
154 { "ds1803-050", DS1803_050 },
155 { "ds1803-100", DS1803_100 },
156 {}
157};
158MODULE_DEVICE_TABLE(i2c, ds1803_id);
159
160static 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
169module_i2c_driver(ds1803_driver);
170
171MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
172MODULE_DESCRIPTION("DS1803 digital potentiometer");
173MODULE_LICENSE("GPL v2");