treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / dac / ad5446.c
blob61c670f7fc5f13d76d440f7b0ffd14cf9ed8ea85
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * AD5446 SPI DAC driver
5 * Copyright 2010 Analog Devices Inc.
6 */
8 #include <linux/interrupt.h>
9 #include <linux/workqueue.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/list.h>
15 #include <linux/spi/spi.h>
16 #include <linux/i2c.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 #define MODE_PWRDWN_1k 0x1
25 #define MODE_PWRDWN_100k 0x2
26 #define MODE_PWRDWN_TRISTATE 0x3
28 /**
29 * struct ad5446_state - driver instance specific data
30 * @spi: spi_device
31 * @chip_info: chip model specific constants, available modes etc
32 * @reg: supply regulator
33 * @vref_mv: actual reference voltage used
36 struct ad5446_state {
37 struct device *dev;
38 const struct ad5446_chip_info *chip_info;
39 struct regulator *reg;
40 unsigned short vref_mv;
41 unsigned cached_val;
42 unsigned pwr_down_mode;
43 unsigned pwr_down;
46 /**
47 * struct ad5446_chip_info - chip specific information
48 * @channel: channel spec for the DAC
49 * @int_vref_mv: AD5620/40/60: the internal reference voltage
50 * @write: chip specific helper function to write to the register
53 struct ad5446_chip_info {
54 struct iio_chan_spec channel;
55 u16 int_vref_mv;
56 int (*write)(struct ad5446_state *st, unsigned val);
59 static const char * const ad5446_powerdown_modes[] = {
60 "1kohm_to_gnd", "100kohm_to_gnd", "three_state"
63 static int ad5446_set_powerdown_mode(struct iio_dev *indio_dev,
64 const struct iio_chan_spec *chan, unsigned int mode)
66 struct ad5446_state *st = iio_priv(indio_dev);
68 st->pwr_down_mode = mode + 1;
70 return 0;
73 static int ad5446_get_powerdown_mode(struct iio_dev *indio_dev,
74 const struct iio_chan_spec *chan)
76 struct ad5446_state *st = iio_priv(indio_dev);
78 return st->pwr_down_mode - 1;
81 static const struct iio_enum ad5446_powerdown_mode_enum = {
82 .items = ad5446_powerdown_modes,
83 .num_items = ARRAY_SIZE(ad5446_powerdown_modes),
84 .get = ad5446_get_powerdown_mode,
85 .set = ad5446_set_powerdown_mode,
88 static ssize_t ad5446_read_dac_powerdown(struct iio_dev *indio_dev,
89 uintptr_t private,
90 const struct iio_chan_spec *chan,
91 char *buf)
93 struct ad5446_state *st = iio_priv(indio_dev);
95 return sprintf(buf, "%d\n", st->pwr_down);
98 static ssize_t ad5446_write_dac_powerdown(struct iio_dev *indio_dev,
99 uintptr_t private,
100 const struct iio_chan_spec *chan,
101 const char *buf, size_t len)
103 struct ad5446_state *st = iio_priv(indio_dev);
104 unsigned int shift;
105 unsigned int val;
106 bool powerdown;
107 int ret;
109 ret = strtobool(buf, &powerdown);
110 if (ret)
111 return ret;
113 mutex_lock(&indio_dev->mlock);
114 st->pwr_down = powerdown;
116 if (st->pwr_down) {
117 shift = chan->scan_type.realbits + chan->scan_type.shift;
118 val = st->pwr_down_mode << shift;
119 } else {
120 val = st->cached_val;
123 ret = st->chip_info->write(st, val);
124 mutex_unlock(&indio_dev->mlock);
126 return ret ? ret : len;
129 static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown[] = {
131 .name = "powerdown",
132 .read = ad5446_read_dac_powerdown,
133 .write = ad5446_write_dac_powerdown,
134 .shared = IIO_SEPARATE,
136 IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5446_powerdown_mode_enum),
137 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum),
138 { },
141 #define _AD5446_CHANNEL(bits, storage, _shift, ext) { \
142 .type = IIO_VOLTAGE, \
143 .indexed = 1, \
144 .output = 1, \
145 .channel = 0, \
146 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
147 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
148 .scan_type = { \
149 .sign = 'u', \
150 .realbits = (bits), \
151 .storagebits = (storage), \
152 .shift = (_shift), \
153 }, \
154 .ext_info = (ext), \
157 #define AD5446_CHANNEL(bits, storage, shift) \
158 _AD5446_CHANNEL(bits, storage, shift, NULL)
160 #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \
161 _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown)
163 static int ad5446_read_raw(struct iio_dev *indio_dev,
164 struct iio_chan_spec const *chan,
165 int *val,
166 int *val2,
167 long m)
169 struct ad5446_state *st = iio_priv(indio_dev);
171 switch (m) {
172 case IIO_CHAN_INFO_RAW:
173 *val = st->cached_val;
174 return IIO_VAL_INT;
175 case IIO_CHAN_INFO_SCALE:
176 *val = st->vref_mv;
177 *val2 = chan->scan_type.realbits;
178 return IIO_VAL_FRACTIONAL_LOG2;
180 return -EINVAL;
183 static int ad5446_write_raw(struct iio_dev *indio_dev,
184 struct iio_chan_spec const *chan,
185 int val,
186 int val2,
187 long mask)
189 struct ad5446_state *st = iio_priv(indio_dev);
190 int ret = 0;
192 switch (mask) {
193 case IIO_CHAN_INFO_RAW:
194 if (val >= (1 << chan->scan_type.realbits) || val < 0)
195 return -EINVAL;
197 val <<= chan->scan_type.shift;
198 mutex_lock(&indio_dev->mlock);
199 st->cached_val = val;
200 if (!st->pwr_down)
201 ret = st->chip_info->write(st, val);
202 mutex_unlock(&indio_dev->mlock);
203 break;
204 default:
205 ret = -EINVAL;
208 return ret;
211 static const struct iio_info ad5446_info = {
212 .read_raw = ad5446_read_raw,
213 .write_raw = ad5446_write_raw,
216 static int ad5446_probe(struct device *dev, const char *name,
217 const struct ad5446_chip_info *chip_info)
219 struct ad5446_state *st;
220 struct iio_dev *indio_dev;
221 struct regulator *reg;
222 int ret, voltage_uv = 0;
224 reg = devm_regulator_get(dev, "vcc");
225 if (!IS_ERR(reg)) {
226 ret = regulator_enable(reg);
227 if (ret)
228 return ret;
230 ret = regulator_get_voltage(reg);
231 if (ret < 0)
232 goto error_disable_reg;
234 voltage_uv = ret;
237 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
238 if (indio_dev == NULL) {
239 ret = -ENOMEM;
240 goto error_disable_reg;
242 st = iio_priv(indio_dev);
243 st->chip_info = chip_info;
245 dev_set_drvdata(dev, indio_dev);
246 st->reg = reg;
247 st->dev = dev;
249 /* Establish that the iio_dev is a child of the device */
250 indio_dev->dev.parent = dev;
251 indio_dev->name = name;
252 indio_dev->info = &ad5446_info;
253 indio_dev->modes = INDIO_DIRECT_MODE;
254 indio_dev->channels = &st->chip_info->channel;
255 indio_dev->num_channels = 1;
257 st->pwr_down_mode = MODE_PWRDWN_1k;
259 if (st->chip_info->int_vref_mv)
260 st->vref_mv = st->chip_info->int_vref_mv;
261 else if (voltage_uv)
262 st->vref_mv = voltage_uv / 1000;
263 else
264 dev_warn(dev, "reference voltage unspecified\n");
266 ret = iio_device_register(indio_dev);
267 if (ret)
268 goto error_disable_reg;
270 return 0;
272 error_disable_reg:
273 if (!IS_ERR(reg))
274 regulator_disable(reg);
275 return ret;
278 static int ad5446_remove(struct device *dev)
280 struct iio_dev *indio_dev = dev_get_drvdata(dev);
281 struct ad5446_state *st = iio_priv(indio_dev);
283 iio_device_unregister(indio_dev);
284 if (!IS_ERR(st->reg))
285 regulator_disable(st->reg);
287 return 0;
290 #if IS_ENABLED(CONFIG_SPI_MASTER)
292 static int ad5446_write(struct ad5446_state *st, unsigned val)
294 struct spi_device *spi = to_spi_device(st->dev);
295 __be16 data = cpu_to_be16(val);
297 return spi_write(spi, &data, sizeof(data));
300 static int ad5660_write(struct ad5446_state *st, unsigned val)
302 struct spi_device *spi = to_spi_device(st->dev);
303 uint8_t data[3];
305 data[0] = (val >> 16) & 0xFF;
306 data[1] = (val >> 8) & 0xFF;
307 data[2] = val & 0xFF;
309 return spi_write(spi, data, sizeof(data));
313 * ad5446_supported_spi_device_ids:
314 * The AD5620/40/60 parts are available in different fixed internal reference
315 * voltage options. The actual part numbers may look differently
316 * (and a bit cryptic), however this style is used to make clear which
317 * parts are supported here.
319 enum ad5446_supported_spi_device_ids {
320 ID_AD5300,
321 ID_AD5310,
322 ID_AD5320,
323 ID_AD5444,
324 ID_AD5446,
325 ID_AD5450,
326 ID_AD5451,
327 ID_AD5541A,
328 ID_AD5512A,
329 ID_AD5553,
330 ID_AD5600,
331 ID_AD5601,
332 ID_AD5611,
333 ID_AD5621,
334 ID_AD5641,
335 ID_AD5620_2500,
336 ID_AD5620_1250,
337 ID_AD5640_2500,
338 ID_AD5640_1250,
339 ID_AD5660_2500,
340 ID_AD5660_1250,
341 ID_AD5662,
344 static const struct ad5446_chip_info ad5446_spi_chip_info[] = {
345 [ID_AD5300] = {
346 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
347 .write = ad5446_write,
349 [ID_AD5310] = {
350 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
351 .write = ad5446_write,
353 [ID_AD5320] = {
354 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
355 .write = ad5446_write,
357 [ID_AD5444] = {
358 .channel = AD5446_CHANNEL(12, 16, 2),
359 .write = ad5446_write,
361 [ID_AD5446] = {
362 .channel = AD5446_CHANNEL(14, 16, 0),
363 .write = ad5446_write,
365 [ID_AD5450] = {
366 .channel = AD5446_CHANNEL(8, 16, 6),
367 .write = ad5446_write,
369 [ID_AD5451] = {
370 .channel = AD5446_CHANNEL(10, 16, 4),
371 .write = ad5446_write,
373 [ID_AD5541A] = {
374 .channel = AD5446_CHANNEL(16, 16, 0),
375 .write = ad5446_write,
377 [ID_AD5512A] = {
378 .channel = AD5446_CHANNEL(12, 16, 4),
379 .write = ad5446_write,
381 [ID_AD5553] = {
382 .channel = AD5446_CHANNEL(14, 16, 0),
383 .write = ad5446_write,
385 [ID_AD5600] = {
386 .channel = AD5446_CHANNEL(16, 16, 0),
387 .write = ad5446_write,
389 [ID_AD5601] = {
390 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6),
391 .write = ad5446_write,
393 [ID_AD5611] = {
394 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4),
395 .write = ad5446_write,
397 [ID_AD5621] = {
398 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
399 .write = ad5446_write,
401 [ID_AD5641] = {
402 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
403 .write = ad5446_write,
405 [ID_AD5620_2500] = {
406 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
407 .int_vref_mv = 2500,
408 .write = ad5446_write,
410 [ID_AD5620_1250] = {
411 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
412 .int_vref_mv = 1250,
413 .write = ad5446_write,
415 [ID_AD5640_2500] = {
416 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
417 .int_vref_mv = 2500,
418 .write = ad5446_write,
420 [ID_AD5640_1250] = {
421 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
422 .int_vref_mv = 1250,
423 .write = ad5446_write,
425 [ID_AD5660_2500] = {
426 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
427 .int_vref_mv = 2500,
428 .write = ad5660_write,
430 [ID_AD5660_1250] = {
431 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
432 .int_vref_mv = 1250,
433 .write = ad5660_write,
435 [ID_AD5662] = {
436 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
437 .write = ad5660_write,
441 static const struct spi_device_id ad5446_spi_ids[] = {
442 {"ad5300", ID_AD5300},
443 {"ad5310", ID_AD5310},
444 {"ad5320", ID_AD5320},
445 {"ad5444", ID_AD5444},
446 {"ad5446", ID_AD5446},
447 {"ad5450", ID_AD5450},
448 {"ad5451", ID_AD5451},
449 {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */
450 {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */
451 {"ad5512a", ID_AD5512A},
452 {"ad5541a", ID_AD5541A},
453 {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */
454 {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */
455 {"ad5553", ID_AD5553},
456 {"ad5600", ID_AD5600},
457 {"ad5601", ID_AD5601},
458 {"ad5611", ID_AD5611},
459 {"ad5621", ID_AD5621},
460 {"ad5641", ID_AD5641},
461 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
462 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
463 {"ad5640-2500", ID_AD5640_2500},
464 {"ad5640-1250", ID_AD5640_1250},
465 {"ad5660-2500", ID_AD5660_2500},
466 {"ad5660-1250", ID_AD5660_1250},
467 {"ad5662", ID_AD5662},
468 {"dac081s101", ID_AD5300}, /* compatible Texas Instruments chips */
469 {"dac101s101", ID_AD5310},
470 {"dac121s101", ID_AD5320},
471 {"dac7512", ID_AD5320},
474 MODULE_DEVICE_TABLE(spi, ad5446_spi_ids);
476 #ifdef CONFIG_OF
477 static const struct of_device_id ad5446_of_ids[] = {
478 { .compatible = "ti,dac7512" },
481 MODULE_DEVICE_TABLE(of, ad5446_of_ids);
482 #endif
484 static int ad5446_spi_probe(struct spi_device *spi)
486 const struct spi_device_id *id = spi_get_device_id(spi);
488 return ad5446_probe(&spi->dev, id->name,
489 &ad5446_spi_chip_info[id->driver_data]);
492 static int ad5446_spi_remove(struct spi_device *spi)
494 return ad5446_remove(&spi->dev);
497 static struct spi_driver ad5446_spi_driver = {
498 .driver = {
499 .name = "ad5446",
500 .of_match_table = of_match_ptr(ad5446_of_ids),
502 .probe = ad5446_spi_probe,
503 .remove = ad5446_spi_remove,
504 .id_table = ad5446_spi_ids,
507 static int __init ad5446_spi_register_driver(void)
509 return spi_register_driver(&ad5446_spi_driver);
512 static void ad5446_spi_unregister_driver(void)
514 spi_unregister_driver(&ad5446_spi_driver);
517 #else
519 static inline int ad5446_spi_register_driver(void) { return 0; }
520 static inline void ad5446_spi_unregister_driver(void) { }
522 #endif
524 #if IS_ENABLED(CONFIG_I2C)
526 static int ad5622_write(struct ad5446_state *st, unsigned val)
528 struct i2c_client *client = to_i2c_client(st->dev);
529 __be16 data = cpu_to_be16(val);
531 return i2c_master_send(client, (char *)&data, sizeof(data));
535 * ad5446_supported_i2c_device_ids:
536 * The AD5620/40/60 parts are available in different fixed internal reference
537 * voltage options. The actual part numbers may look differently
538 * (and a bit cryptic), however this style is used to make clear which
539 * parts are supported here.
541 enum ad5446_supported_i2c_device_ids {
542 ID_AD5602,
543 ID_AD5612,
544 ID_AD5622,
547 static const struct ad5446_chip_info ad5446_i2c_chip_info[] = {
548 [ID_AD5602] = {
549 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
550 .write = ad5622_write,
552 [ID_AD5612] = {
553 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
554 .write = ad5622_write,
556 [ID_AD5622] = {
557 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
558 .write = ad5622_write,
562 static int ad5446_i2c_probe(struct i2c_client *i2c,
563 const struct i2c_device_id *id)
565 return ad5446_probe(&i2c->dev, id->name,
566 &ad5446_i2c_chip_info[id->driver_data]);
569 static int ad5446_i2c_remove(struct i2c_client *i2c)
571 return ad5446_remove(&i2c->dev);
574 static const struct i2c_device_id ad5446_i2c_ids[] = {
575 {"ad5301", ID_AD5602},
576 {"ad5311", ID_AD5612},
577 {"ad5321", ID_AD5622},
578 {"ad5602", ID_AD5602},
579 {"ad5612", ID_AD5612},
580 {"ad5622", ID_AD5622},
583 MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids);
585 static struct i2c_driver ad5446_i2c_driver = {
586 .driver = {
587 .name = "ad5446",
589 .probe = ad5446_i2c_probe,
590 .remove = ad5446_i2c_remove,
591 .id_table = ad5446_i2c_ids,
594 static int __init ad5446_i2c_register_driver(void)
596 return i2c_add_driver(&ad5446_i2c_driver);
599 static void __exit ad5446_i2c_unregister_driver(void)
601 i2c_del_driver(&ad5446_i2c_driver);
604 #else
606 static inline int ad5446_i2c_register_driver(void) { return 0; }
607 static inline void ad5446_i2c_unregister_driver(void) { }
609 #endif
611 static int __init ad5446_init(void)
613 int ret;
615 ret = ad5446_spi_register_driver();
616 if (ret)
617 return ret;
619 ret = ad5446_i2c_register_driver();
620 if (ret) {
621 ad5446_spi_unregister_driver();
622 return ret;
625 return 0;
627 module_init(ad5446_init);
629 static void __exit ad5446_exit(void)
631 ad5446_i2c_unregister_driver();
632 ad5446_spi_unregister_driver();
634 module_exit(ad5446_exit);
636 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
637 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
638 MODULE_LICENSE("GPL v2");