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