Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: dac: Add Texas Instruments 8/10/12-bit 2/4-channel DAC driver

The DACrrcS085 (rr = 08/10/12, c = 2/4) family of SPI DACs was
inherited by TI when they acquired National Semiconductor in 2011.
This driver was developed for and tested with the DAC082S085 built into
the Revolution Pi by KUNBUS, but should work with any of the other
chips as they share the same programming interface.

There is also a family of I2C DACs with just a single channel called
DACrr1C08x (rr = 08/10/12, x = 1/5). Their programming interface is
very similar and it should be possible to extend the driver for these
chips with moderate effort. Alternatively they could be integrated into
ad5446.c. (The AD5301/AD5311/AD5321 use different power-down modes but
otherwise appear to be comparable.)

Furthermore there is a family of 8-channel DACs called DACrr8S085
(rr = 08/10/12) as well as two 16-bit DACs called DAC161Sxxx
(xxx = 055/997). These are more complicated devices with support for
daisy-chaining and the ability to power down each channel separately.
They could either be handled by a separate driver or integrated into the
present driver with a larger effort.

Cc: Mathias Duckeck <m.duckeck@kunbus.de>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Lukas Wunner and committed by
Jonathan Cameron
61011264 930cc2d2

+363
+1
Documentation/ABI/testing/sysfs-bus-iio
··· 522 522 Specifies the output powerdown mode. 523 523 DAC output stage is disconnected from the amplifier and 524 524 1kohm_to_gnd: connected to ground via an 1kOhm resistor, 525 + 2.5kohm_to_gnd: connected to ground via a 2.5kOhm resistor, 525 526 6kohm_to_gnd: connected to ground via a 6kOhm resistor, 526 527 20kohm_to_gnd: connected to ground via a 20kOhm resistor, 527 528 90kohm_to_gnd: connected to ground via a 90kOhm resistor,
+10
drivers/iio/dac/Kconfig
··· 310 310 config STM32_DAC_CORE 311 311 tristate 312 312 313 + config TI_DAC082S085 314 + tristate "Texas Instruments 8/10/12-bit 2/4-channel DAC driver" 315 + depends on SPI_MASTER 316 + help 317 + Driver for the Texas Instruments (formerly National Semiconductor) 318 + DAC082S085, DAC102S085, DAC122S085, DAC084S085, DAC104S085 and 319 + DAC124S085. 320 + 321 + If compiled as a module, it will be called ti-dac082s085. 322 + 313 323 config VF610_DAC 314 324 tristate "Vybrid vf610 DAC driver" 315 325 depends on OF
+1
drivers/iio/dac/Makefile
··· 33 33 obj-$(CONFIG_MCP4922) += mcp4922.o 34 34 obj-$(CONFIG_STM32_DAC_CORE) += stm32-dac-core.o 35 35 obj-$(CONFIG_STM32_DAC) += stm32-dac.o 36 + obj-$(CONFIG_TI_DAC082S085) += ti-dac082s085.o 36 37 obj-$(CONFIG_VF610_DAC) += vf610_dac.o
+351
drivers/iio/dac/ti-dac082s085.c
··· 1 + /* 2 + * ti-dac082s085.c - Texas Instruments 8/10/12-bit 2/4-channel DAC driver 3 + * 4 + * Copyright (C) 2017 KUNBUS GmbH 5 + * 6 + * http://www.ti.com/lit/ds/symlink/dac082s085.pdf 7 + * http://www.ti.com/lit/ds/symlink/dac102s085.pdf 8 + * http://www.ti.com/lit/ds/symlink/dac122s085.pdf 9 + * http://www.ti.com/lit/ds/symlink/dac084s085.pdf 10 + * http://www.ti.com/lit/ds/symlink/dac104s085.pdf 11 + * http://www.ti.com/lit/ds/symlink/dac124s085.pdf 12 + * 13 + * This program is free software; you can redistribute it and/or modify 14 + * it under the terms of the GNU General Public License (version 2) as 15 + * published by the Free Software Foundation. 16 + */ 17 + 18 + #include <linux/iio/iio.h> 19 + #include <linux/module.h> 20 + #include <linux/regulator/consumer.h> 21 + #include <linux/spi/spi.h> 22 + 23 + /** 24 + * struct ti_dac_chip - TI DAC chip 25 + * @lock: protects write sequences 26 + * @vref: regulator generating Vref 27 + * @mesg: SPI message to perform a write 28 + * @xfer: SPI transfer used by @mesg 29 + * @val: cached value of each output 30 + * @powerdown: whether the chip is powered down 31 + * @powerdown_mode: selected by the user 32 + * @resolution: resolution of the chip 33 + * @buf: buffer for @xfer 34 + */ 35 + struct ti_dac_chip { 36 + struct mutex lock; 37 + struct regulator *vref; 38 + struct spi_message mesg; 39 + struct spi_transfer xfer; 40 + u16 val[4]; 41 + bool powerdown; 42 + u8 powerdown_mode; 43 + u8 resolution; 44 + u8 buf[2] ____cacheline_aligned; 45 + }; 46 + 47 + #define WRITE_NOT_UPDATE(chan) (0x00 | (chan) << 6) 48 + #define WRITE_AND_UPDATE(chan) (0x10 | (chan) << 6) 49 + #define WRITE_ALL_UPDATE 0x20 50 + #define POWERDOWN(mode) (0x30 | ((mode) + 1) << 6) 51 + 52 + static int ti_dac_cmd(struct ti_dac_chip *ti_dac, u8 cmd, u16 val) 53 + { 54 + u8 shift = 12 - ti_dac->resolution; 55 + 56 + ti_dac->buf[0] = cmd | (val >> (8 - shift)); 57 + ti_dac->buf[1] = (val << shift) & 0xff; 58 + return spi_sync(ti_dac->mesg.spi, &ti_dac->mesg); 59 + } 60 + 61 + static const char * const ti_dac_powerdown_modes[] = { 62 + "2.5kohm_to_gnd", "100kohm_to_gnd", "three_state", 63 + }; 64 + 65 + static int ti_dac_get_powerdown_mode(struct iio_dev *indio_dev, 66 + const struct iio_chan_spec *chan) 67 + { 68 + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); 69 + 70 + return ti_dac->powerdown_mode; 71 + } 72 + 73 + static int ti_dac_set_powerdown_mode(struct iio_dev *indio_dev, 74 + const struct iio_chan_spec *chan, 75 + unsigned int mode) 76 + { 77 + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); 78 + int ret = 0; 79 + 80 + if (ti_dac->powerdown_mode == mode) 81 + return 0; 82 + 83 + mutex_lock(&ti_dac->lock); 84 + if (ti_dac->powerdown) { 85 + ret = ti_dac_cmd(ti_dac, POWERDOWN(mode), 0); 86 + if (ret) 87 + goto out; 88 + } 89 + ti_dac->powerdown_mode = mode; 90 + 91 + out: 92 + mutex_unlock(&ti_dac->lock); 93 + return ret; 94 + } 95 + 96 + static const struct iio_enum ti_dac_powerdown_mode = { 97 + .items = ti_dac_powerdown_modes, 98 + .num_items = ARRAY_SIZE(ti_dac_powerdown_modes), 99 + .get = ti_dac_get_powerdown_mode, 100 + .set = ti_dac_set_powerdown_mode, 101 + }; 102 + 103 + static ssize_t ti_dac_read_powerdown(struct iio_dev *indio_dev, 104 + uintptr_t private, 105 + const struct iio_chan_spec *chan, 106 + char *buf) 107 + { 108 + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); 109 + 110 + return sprintf(buf, "%d\n", ti_dac->powerdown); 111 + } 112 + 113 + static ssize_t ti_dac_write_powerdown(struct iio_dev *indio_dev, 114 + uintptr_t private, 115 + const struct iio_chan_spec *chan, 116 + const char *buf, size_t len) 117 + { 118 + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); 119 + bool powerdown; 120 + int ret; 121 + 122 + ret = strtobool(buf, &powerdown); 123 + if (ret) 124 + return ret; 125 + 126 + if (ti_dac->powerdown == powerdown) 127 + return len; 128 + 129 + mutex_lock(&ti_dac->lock); 130 + if (powerdown) 131 + ret = ti_dac_cmd(ti_dac, POWERDOWN(ti_dac->powerdown_mode), 0); 132 + else 133 + ret = ti_dac_cmd(ti_dac, WRITE_AND_UPDATE(0), ti_dac->val[0]); 134 + if (!ret) 135 + ti_dac->powerdown = powerdown; 136 + mutex_unlock(&ti_dac->lock); 137 + 138 + return ret ? ret : len; 139 + } 140 + 141 + static const struct iio_chan_spec_ext_info ti_dac_ext_info[] = { 142 + { 143 + .name = "powerdown", 144 + .read = ti_dac_read_powerdown, 145 + .write = ti_dac_write_powerdown, 146 + .shared = IIO_SHARED_BY_TYPE, 147 + }, 148 + IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE, &ti_dac_powerdown_mode), 149 + IIO_ENUM_AVAILABLE("powerdown_mode", &ti_dac_powerdown_mode), 150 + { }, 151 + }; 152 + 153 + #define TI_DAC_CHANNEL(chan) { \ 154 + .type = IIO_VOLTAGE, \ 155 + .channel = (chan), \ 156 + .address = (chan), \ 157 + .indexed = true, \ 158 + .output = true, \ 159 + .datasheet_name = (const char[]){ 'A' + (chan), 0 }, \ 160 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 161 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 162 + .ext_info = ti_dac_ext_info, \ 163 + } 164 + 165 + static const struct iio_chan_spec ti_dac_channels[] = { 166 + TI_DAC_CHANNEL(0), 167 + TI_DAC_CHANNEL(1), 168 + TI_DAC_CHANNEL(2), 169 + TI_DAC_CHANNEL(3), 170 + }; 171 + 172 + static int ti_dac_read_raw(struct iio_dev *indio_dev, 173 + struct iio_chan_spec const *chan, 174 + int *val, int *val2, long mask) 175 + { 176 + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); 177 + int ret; 178 + 179 + switch (mask) { 180 + case IIO_CHAN_INFO_RAW: 181 + *val = ti_dac->val[chan->channel]; 182 + ret = IIO_VAL_INT; 183 + break; 184 + 185 + case IIO_CHAN_INFO_SCALE: 186 + ret = regulator_get_voltage(ti_dac->vref); 187 + if (ret < 0) 188 + return ret; 189 + 190 + *val = ret / 1000; 191 + *val2 = ti_dac->resolution; 192 + ret = IIO_VAL_FRACTIONAL_LOG2; 193 + break; 194 + 195 + default: 196 + ret = -EINVAL; 197 + } 198 + 199 + return ret; 200 + } 201 + 202 + static int ti_dac_write_raw(struct iio_dev *indio_dev, 203 + struct iio_chan_spec const *chan, 204 + int val, int val2, long mask) 205 + { 206 + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); 207 + int ret; 208 + 209 + switch (mask) { 210 + case IIO_CHAN_INFO_RAW: 211 + if (ti_dac->val[chan->channel] == val) 212 + return 0; 213 + 214 + if (val >= (1 << ti_dac->resolution) || val < 0) 215 + return -EINVAL; 216 + 217 + if (ti_dac->powerdown) 218 + return -EBUSY; 219 + 220 + mutex_lock(&ti_dac->lock); 221 + ret = ti_dac_cmd(ti_dac, WRITE_AND_UPDATE(chan->channel), val); 222 + if (!ret) 223 + ti_dac->val[chan->channel] = val; 224 + mutex_unlock(&ti_dac->lock); 225 + break; 226 + 227 + default: 228 + ret = -EINVAL; 229 + } 230 + 231 + return ret; 232 + } 233 + 234 + static int ti_dac_write_raw_get_fmt(struct iio_dev *indio_dev, 235 + struct iio_chan_spec const *chan, long mask) 236 + { 237 + return IIO_VAL_INT; 238 + } 239 + 240 + static const struct iio_info ti_dac_info = { 241 + .read_raw = ti_dac_read_raw, 242 + .write_raw = ti_dac_write_raw, 243 + .write_raw_get_fmt = ti_dac_write_raw_get_fmt, 244 + }; 245 + 246 + static int ti_dac_probe(struct spi_device *spi) 247 + { 248 + struct device *dev = &spi->dev; 249 + struct ti_dac_chip *ti_dac; 250 + struct iio_dev *indio_dev; 251 + int ret; 252 + 253 + indio_dev = devm_iio_device_alloc(dev, sizeof(*ti_dac)); 254 + if (!indio_dev) 255 + return -ENOMEM; 256 + 257 + indio_dev->dev.parent = dev; 258 + indio_dev->info = &ti_dac_info; 259 + indio_dev->name = spi->modalias; 260 + indio_dev->modes = INDIO_DIRECT_MODE; 261 + indio_dev->channels = ti_dac_channels; 262 + spi_set_drvdata(spi, indio_dev); 263 + 264 + ti_dac = iio_priv(indio_dev); 265 + ti_dac->xfer.tx_buf = &ti_dac->buf; 266 + ti_dac->xfer.len = sizeof(ti_dac->buf); 267 + spi_message_init_with_transfers(&ti_dac->mesg, &ti_dac->xfer, 1); 268 + ti_dac->mesg.spi = spi; 269 + 270 + ret = sscanf(spi->modalias, "dac%2hhu%1d", 271 + &ti_dac->resolution, &indio_dev->num_channels); 272 + WARN_ON(ret != 2); 273 + 274 + ti_dac->vref = devm_regulator_get(dev, "vref"); 275 + if (IS_ERR(ti_dac->vref)) 276 + return PTR_ERR(ti_dac->vref); 277 + 278 + ret = regulator_enable(ti_dac->vref); 279 + if (ret < 0) 280 + return ret; 281 + 282 + mutex_init(&ti_dac->lock); 283 + 284 + ret = ti_dac_cmd(ti_dac, WRITE_ALL_UPDATE, 0); 285 + if (ret) { 286 + dev_err(dev, "failed to initialize outputs to 0\n"); 287 + goto err; 288 + } 289 + 290 + ret = iio_device_register(indio_dev); 291 + if (ret) 292 + goto err; 293 + 294 + return 0; 295 + 296 + err: 297 + mutex_destroy(&ti_dac->lock); 298 + regulator_disable(ti_dac->vref); 299 + return ret; 300 + } 301 + 302 + static int ti_dac_remove(struct spi_device *spi) 303 + { 304 + struct iio_dev *indio_dev = spi_get_drvdata(spi); 305 + struct ti_dac_chip *ti_dac = iio_priv(indio_dev); 306 + 307 + iio_device_unregister(indio_dev); 308 + mutex_destroy(&ti_dac->lock); 309 + regulator_disable(ti_dac->vref); 310 + 311 + return 0; 312 + } 313 + 314 + #ifdef CONFIG_OF 315 + static const struct of_device_id ti_dac_of_id[] = { 316 + { .compatible = "ti,dac082s085" }, 317 + { .compatible = "ti,dac102s085" }, 318 + { .compatible = "ti,dac122s085" }, 319 + { .compatible = "ti,dac084s085" }, 320 + { .compatible = "ti,dac104s085" }, 321 + { .compatible = "ti,dac124s085" }, 322 + { } 323 + }; 324 + MODULE_DEVICE_TABLE(of, ti_dac_of_id); 325 + #endif 326 + 327 + static const struct spi_device_id ti_dac_spi_id[] = { 328 + { "dac082s085" }, 329 + { "dac102s085" }, 330 + { "dac122s085" }, 331 + { "dac084s085" }, 332 + { "dac104s085" }, 333 + { "dac124s085" }, 334 + { } 335 + }; 336 + MODULE_DEVICE_TABLE(spi, ti_dac_spi_id); 337 + 338 + static struct spi_driver ti_dac_driver = { 339 + .driver = { 340 + .name = "ti-dac082s085", 341 + .of_match_table = of_match_ptr(ti_dac_of_id), 342 + }, 343 + .probe = ti_dac_probe, 344 + .remove = ti_dac_remove, 345 + .id_table = ti_dac_spi_id, 346 + }; 347 + module_spi_driver(ti_dac_driver); 348 + 349 + MODULE_AUTHOR("Lukas Wunner <lukas@wunner.de>"); 350 + MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 2/4-channel DAC driver"); 351 + MODULE_LICENSE("GPL v2");