1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.h>
25#include <linux/i2c.h>
26#include <linux/err.h>
27
28#include <linux/iio/iio.h>
29#include <linux/iio/sysfs.h>
30#include <linux/iio/dac/max517.h>
31
32#define MAX517_DRV_NAME "max517"
33
34
35#define COMMAND_CHANNEL0 0x00
36#define COMMAND_CHANNEL1 0x01
37#define COMMAND_PD 0x08
38
39enum max517_device_ids {
40 ID_MAX517,
41 ID_MAX518,
42 ID_MAX519,
43};
44
45struct max517_data {
46 struct i2c_client *client;
47 unsigned short vref_mv[2];
48};
49
50
51
52
53
54
55static int max517_set_value(struct iio_dev *indio_dev,
56 long val, int channel)
57{
58 struct max517_data *data = iio_priv(indio_dev);
59 struct i2c_client *client = data->client;
60 u8 outbuf[2];
61 int res;
62
63 if (val < 0 || val > 255)
64 return -EINVAL;
65
66 outbuf[0] = channel;
67 outbuf[1] = val;
68
69 res = i2c_master_send(client, outbuf, 2);
70 if (res < 0)
71 return res;
72 else if (res != 2)
73 return -EIO;
74 else
75 return 0;
76}
77
78static int max517_read_raw(struct iio_dev *indio_dev,
79 struct iio_chan_spec const *chan,
80 int *val,
81 int *val2,
82 long m)
83{
84 struct max517_data *data = iio_priv(indio_dev);
85
86 switch (m) {
87 case IIO_CHAN_INFO_SCALE:
88
89 *val = data->vref_mv[chan->channel];
90 *val2 = 8;
91 return IIO_VAL_FRACTIONAL_LOG2;
92 default:
93 break;
94 }
95 return -EINVAL;
96}
97
98static int max517_write_raw(struct iio_dev *indio_dev,
99 struct iio_chan_spec const *chan, int val, int val2, long mask)
100{
101 int ret;
102
103 switch (mask) {
104 case IIO_CHAN_INFO_RAW:
105 ret = max517_set_value(indio_dev, val, chan->channel);
106 break;
107 default:
108 ret = -EINVAL;
109 break;
110 }
111
112 return ret;
113}
114
115#ifdef CONFIG_PM_SLEEP
116static int max517_suspend(struct device *dev)
117{
118 u8 outbuf = COMMAND_PD;
119
120 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
121}
122
123static int max517_resume(struct device *dev)
124{
125 u8 outbuf = 0;
126
127 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
128}
129
130static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
131#define MAX517_PM_OPS (&max517_pm_ops)
132#else
133#define MAX517_PM_OPS NULL
134#endif
135
136static const struct iio_info max517_info = {
137 .read_raw = max517_read_raw,
138 .write_raw = max517_write_raw,
139 .driver_module = THIS_MODULE,
140};
141
142#define MAX517_CHANNEL(chan) { \
143 .type = IIO_VOLTAGE, \
144 .indexed = 1, \
145 .output = 1, \
146 .channel = (chan), \
147 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
148 BIT(IIO_CHAN_INFO_SCALE), \
149 .scan_type = IIO_ST('u', 8, 8, 0), \
150}
151
152static const struct iio_chan_spec max517_channels[] = {
153 MAX517_CHANNEL(0),
154 MAX517_CHANNEL(1)
155};
156
157static int max517_probe(struct i2c_client *client,
158 const struct i2c_device_id *id)
159{
160 struct max517_data *data;
161 struct iio_dev *indio_dev;
162 struct max517_platform_data *platform_data = client->dev.platform_data;
163
164 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
165 if (!indio_dev)
166 return -ENOMEM;
167 data = iio_priv(indio_dev);
168 i2c_set_clientdata(client, indio_dev);
169 data->client = client;
170
171
172 indio_dev->dev.parent = &client->dev;
173
174
175 if (id->driver_data == ID_MAX517)
176 indio_dev->num_channels = 1;
177 else
178 indio_dev->num_channels = 2;
179 indio_dev->channels = max517_channels;
180 indio_dev->modes = INDIO_DIRECT_MODE;
181 indio_dev->info = &max517_info;
182
183
184
185
186
187 if (id->driver_data == ID_MAX518 || !platform_data) {
188 data->vref_mv[0] = data->vref_mv[1] = 5000;
189 } else {
190 data->vref_mv[0] = platform_data->vref_mv[0];
191 data->vref_mv[1] = platform_data->vref_mv[1];
192 }
193
194 return iio_device_register(indio_dev);
195}
196
197static int max517_remove(struct i2c_client *client)
198{
199 iio_device_unregister(i2c_get_clientdata(client));
200 return 0;
201}
202
203static const struct i2c_device_id max517_id[] = {
204 { "max517", ID_MAX517 },
205 { "max518", ID_MAX518 },
206 { "max519", ID_MAX519 },
207 { }
208};
209MODULE_DEVICE_TABLE(i2c, max517_id);
210
211static struct i2c_driver max517_driver = {
212 .driver = {
213 .name = MAX517_DRV_NAME,
214 .pm = MAX517_PM_OPS,
215 },
216 .probe = max517_probe,
217 .remove = max517_remove,
218 .id_table = max517_id,
219};
220module_i2c_driver(max517_driver);
221
222MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
223MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
224MODULE_LICENSE("GPL");
225