inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / iio / potentiometer / ds1803.c
blobfb9e2a337dc2a2e313e51e43b1480e20f8212998
1 /*
2 * Maxim Integrated DS1803 digital potentiometer driver
3 * Copyright (c) 2016 Slawomir Stepien
5 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf
7 * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
8 * ds1803 2 256 10, 50, 100 0101xxx
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.
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>
22 #define DS1803_MAX_POS 255
23 #define DS1803_WRITE(chan) (0xa8 | ((chan) + 1))
25 enum ds1803_type {
26 DS1803_010,
27 DS1803_050,
28 DS1803_100,
31 struct ds1803_cfg {
32 int kohms;
35 static const struct ds1803_cfg ds1803_cfg[] = {
36 [DS1803_010] = { .kohms = 10, },
37 [DS1803_050] = { .kohms = 50, },
38 [DS1803_100] = { .kohms = 100, },
41 struct ds1803_data {
42 struct i2c_client *client;
43 const struct ds1803_cfg *cfg;
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), \
55 static const struct iio_chan_spec ds1803_channels[] = {
56 DS1803_CHANNEL(0),
57 DS1803_CHANNEL(1),
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)
64 struct ds1803_data *data = iio_priv(indio_dev);
65 int pot = chan->channel;
66 int ret;
67 u8 result[indio_dev->num_channels];
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;
76 *val = result[pot];
77 return IIO_VAL_INT;
79 case IIO_CHAN_INFO_SCALE:
80 *val = 1000 * data->cfg->kohms;
81 *val2 = DS1803_MAX_POS;
82 return IIO_VAL_FRACTIONAL;
85 return -EINVAL;
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)
92 struct ds1803_data *data = iio_priv(indio_dev);
93 int pot = chan->channel;
95 if (val2 != 0)
96 return -EINVAL;
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;
107 return i2c_smbus_write_byte_data(data->client, DS1803_WRITE(pot), val);
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,
116 static int ds1803_probe(struct i2c_client *client,
117 const struct i2c_device_id *id)
119 struct device *dev = &client->dev;
120 struct ds1803_data *data;
121 struct iio_dev *indio_dev;
123 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
124 if (!indio_dev)
125 return -ENOMEM;
127 i2c_set_clientdata(client, indio_dev);
129 data = iio_priv(indio_dev);
130 data->client = client;
131 data->cfg = &ds1803_cfg[id->driver_data];
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;
139 return devm_iio_device_register(dev, indio_dev);
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] },
149 MODULE_DEVICE_TABLE(of, ds1803_dt_ids);
150 #endif /* CONFIG_OF */
152 static const struct i2c_device_id ds1803_id[] = {
153 { "ds1803-010", DS1803_010 },
154 { "ds1803-050", DS1803_050 },
155 { "ds1803-100", DS1803_100 },
158 MODULE_DEVICE_TABLE(i2c, ds1803_id);
160 static struct i2c_driver ds1803_driver = {
161 .driver = {
162 .name = "ds1803",
163 .of_match_table = of_match_ptr(ds1803_dt_ids),
165 .probe = ds1803_probe,
166 .id_table = ds1803_id,
169 module_i2c_driver(ds1803_driver);
171 MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
172 MODULE_DESCRIPTION("DS1803 digital potentiometer");
173 MODULE_LICENSE("GPL v2");