1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Maxim Integrated
* 7-bit, Multi-Channel Sink/Source Current DAC Driver
* Copyright (C) 2017 Maxim Integrated
*/
#include <linux/array_size.h>
#include <linux/bits.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/time64.h>
#include <linux/types.h>
#include <linux/iio/driver.h>
#include <linux/iio/iio.h>
#include <linux/iio/machine.h>
#define DS4422_MAX_DAC_CHANNELS 2
#define DS4424_MAX_DAC_CHANNELS 4
#define DS4424_DAC_MASK GENMASK(6, 0)
#define DS4404_DAC_MASK GENMASK(4, 0)
#define DS4424_DAC_SOURCE BIT(7)
#define DS4424_DAC_ADDR(chan) ((chan) + 0xf8)
#define DS4424_CHANNEL(chan) { \
.type = IIO_CURRENT, \
.indexed = 1, \
.output = 1, \
.channel = chan, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
}
#define DS4424_CHANNEL_WITH_SCALE(chan) { \
.type = IIO_CURRENT, \
.indexed = 1, \
.output = 1, \
.channel = chan, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_SCALE), \
}
struct ds4424_chip_info {
const char *name;
int vref_mV;
int scale_denom;
u8 result_mask;
u8 num_channels;
};
static const struct ds4424_chip_info ds4402_info = {
.name = "ds4402",
.vref_mV = 1230,
.scale_denom = 4,
.result_mask = DS4404_DAC_MASK,
.num_channels = DS4422_MAX_DAC_CHANNELS,
};
static const struct ds4424_chip_info ds4404_info = {
.name = "ds4404",
.vref_mV = 1230,
.scale_denom = 4,
.result_mask = DS4404_DAC_MASK,
.num_channels = DS4424_MAX_DAC_CHANNELS,
};
static const struct ds4424_chip_info ds4422_info = {
.name = "ds4422",
.vref_mV = 976,
.scale_denom = 16,
.result_mask = DS4424_DAC_MASK,
.num_channels = DS4422_MAX_DAC_CHANNELS,
};
static const struct ds4424_chip_info ds4424_info = {
.name = "ds4424",
.vref_mV = 976,
.scale_denom = 16,
.result_mask = DS4424_DAC_MASK,
.num_channels = DS4424_MAX_DAC_CHANNELS,
};
struct ds4424_data {
struct regmap *regmap;
struct regulator *vcc_reg;
const struct ds4424_chip_info *chip_info;
u32 rfs_ohms[DS4424_MAX_DAC_CHANNELS];
bool has_rfs;
};
static const struct iio_chan_spec ds4424_channels[] = {
DS4424_CHANNEL(0),
DS4424_CHANNEL(1),
DS4424_CHANNEL(2),
DS4424_CHANNEL(3),
};
static const struct iio_chan_spec ds4424_channels_with_scale[] = {
DS4424_CHANNEL_WITH_SCALE(0),
DS4424_CHANNEL_WITH_SCALE(1),
DS4424_CHANNEL_WITH_SCALE(2),
DS4424_CHANNEL_WITH_SCALE(3),
};
static const struct regmap_range ds44x2_ranges[] = {
regmap_reg_range(DS4424_DAC_ADDR(0), DS4424_DAC_ADDR(1)),
};
static const struct regmap_range ds44x4_ranges[] = {
regmap_reg_range(DS4424_DAC_ADDR(0), DS4424_DAC_ADDR(3)),
};
static const struct regmap_access_table ds44x2_table = {
.yes_ranges = ds44x2_ranges,
.n_yes_ranges = ARRAY_SIZE(ds44x2_ranges),
};
static const struct regmap_access_table ds44x4_table = {
.yes_ranges = ds44x4_ranges,
.n_yes_ranges = ARRAY_SIZE(ds44x4_ranges),
};
static const struct regmap_config ds44x2_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.cache_type = REGCACHE_MAPLE,
.max_register = DS4424_DAC_ADDR(1),
.rd_table = &ds44x2_table,
.wr_table = &ds44x2_table,
};
static const struct regmap_config ds44x4_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.cache_type = REGCACHE_MAPLE,
.max_register = DS4424_DAC_ADDR(3),
.rd_table = &ds44x4_table,
.wr_table = &ds44x4_table,
};
static int ds4424_init_regmap(struct i2c_client *client,
struct iio_dev *indio_dev)
{
struct ds4424_data *data = iio_priv(indio_dev);
const struct regmap_config *regmap_config;
u8 vals[DS4424_MAX_DAC_CHANNELS];
int ret;
if (indio_dev->num_channels == DS4424_MAX_DAC_CHANNELS)
regmap_config = &ds44x4_regmap_config;
else
regmap_config = &ds44x2_regmap_config;
data->regmap = devm_regmap_init_i2c(client, regmap_config);
if (IS_ERR(data->regmap))
return dev_err_probe(&client->dev, PTR_ERR(data->regmap),
"Failed to init regmap.\n");
/*
* Prime the cache with the bootloader's configuration.
* regmap_bulk_read() will automatically populate the cache with
* the values read from the hardware.
*/
ret = regmap_bulk_read(data->regmap, DS4424_DAC_ADDR(0), vals,
indio_dev->num_channels);
if (ret)
return dev_err_probe(&client->dev, ret,
"Failed to read hardware values\n");
return 0;
}
static int ds4424_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
struct ds4424_data *data = iio_priv(indio_dev);
unsigned int regval;
int ret;
switch (mask) {
case IIO_CHAN_INFO_RAW:
ret = regmap_read(data->regmap, DS4424_DAC_ADDR(chan->channel),
®val);
if (ret < 0) {
dev_err_ratelimited(indio_dev->dev.parent,
"Failed to read channel %d: %pe\n",
chan->channel, ERR_PTR(ret));
return ret;
}
*val = regval & data->chip_info->result_mask;
if (!(regval & DS4424_DAC_SOURCE))
*val = -*val;
return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
if (!data->has_rfs)
return -EINVAL;
/* SCALE is mA/step: mV / Ohm = mA. */
*val = data->chip_info->vref_mV;
*val2 = data->rfs_ohms[chan->channel] *
data->chip_info->scale_denom;
return IIO_VAL_FRACTIONAL;
default:
return -EINVAL;
}
}
static int ds4424_write_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val, int val2, long mask)
{
struct ds4424_data *data = iio_priv(indio_dev);
unsigned int abs_val;
if (val2 != 0)
return -EINVAL;
switch (mask) {
case IIO_CHAN_INFO_RAW:
abs_val = abs(val);
if (abs_val > data->chip_info->result_mask)
return -EINVAL;
/*
* Currents exiting the IC (Source) are positive. 0 is a valid
* value for no current flow; the direction bit (Source vs Sink)
* is treated as don't-care by the hardware at 0.
*/
if (val > 0)
abs_val |= DS4424_DAC_SOURCE;
return regmap_write(data->regmap, DS4424_DAC_ADDR(chan->channel),
abs_val);
default:
return -EINVAL;
}
}
static int ds4424_parse_rfs(struct i2c_client *client,
struct ds4424_data *data,
struct iio_dev *indio_dev)
{
struct device *dev = &client->dev;
int count, ret;
if (!device_property_present(dev, "maxim,rfs-ohms"))
return 0;
count = device_property_count_u32(dev, "maxim,rfs-ohms");
if (count < 0)
return dev_err_probe(dev, count, "Failed to count maxim,rfs-ohms entries\n");
if (count != indio_dev->num_channels)
return dev_err_probe(dev, -EINVAL, "maxim,rfs-ohms must have %u entries\n",
indio_dev->num_channels);
ret = device_property_read_u32_array(dev, "maxim,rfs-ohms",
data->rfs_ohms,
indio_dev->num_channels);
if (ret)
return dev_err_probe(dev,
|