1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AD7303 Digital to analog converters driver 4 * 5 * Copyright 2013 Analog Devices Inc. 6 */ 7 8 #include <linux/err.h> 9 #include <linux/module.h> 10 #include <linux/mod_devicetable.h> 11 #include <linux/kernel.h> 12 #include <linux/spi/spi.h> 13 #include <linux/slab.h> 14 #include <linux/sysfs.h> 15 #include <linux/regulator/consumer.h> 16 17 #include <linux/iio/iio.h> 18 #include <linux/iio/sysfs.h> 19 20 #include <linux/platform_data/ad7303.h> 21 22 #define AD7303_CFG_EXTERNAL_VREF BIT(15) 23 #define AD7303_CFG_POWER_DOWN(ch) BIT(11 + (ch)) 24 #define AD7303_CFG_ADDR_OFFSET 10 25 26 #define AD7303_CMD_UPDATE_DAC (0x3 << 8) 27 28 /** 29 * struct ad7303_state - driver instance specific data 30 * @spi: the device for this driver instance 31 * @config: cached config register value 32 * @dac_cache: current DAC raw value (chip does not support readback) 33 * @vdd_reg: reference to VDD regulator 34 * @vref_reg: reference to VREF regulator 35 * @lock: protect writes and cache updates 36 * @data: spi transfer buffer 37 */ 38 39 struct ad7303_state { 40 struct spi_device *spi; 41 uint16_t config; 42 uint8_t dac_cache[2]; 43 44 struct regulator *vdd_reg; 45 struct regulator *vref_reg; 46 47 struct mutex lock; 48 /* 49 * DMA (thus cache coherency maintenance) requires the 50 * transfer buffers to live in their own cache lines. 51 */ 52 __be16 data ____cacheline_aligned; 53 }; 54 55 static int ad7303_write(struct ad7303_state *st, unsigned int chan, 56 uint8_t val) 57 { 58 st->data = cpu_to_be16(AD7303_CMD_UPDATE_DAC | 59 (chan << AD7303_CFG_ADDR_OFFSET) | 60 st->config | val); 61 62 return spi_write(st->spi, &st->data, sizeof(st->data)); 63 } 64 65 static ssize_t ad7303_read_dac_powerdown(struct iio_dev *indio_dev, 66 uintptr_t private, const struct iio_chan_spec *chan, char *buf) 67 { 68 struct ad7303_state *st = iio_priv(indio_dev); 69 70 return sprintf(buf, "%d\n", (bool)(st->config & 71 AD7303_CFG_POWER_DOWN(chan->channel))); 72 } 73 74 static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev, 75 uintptr_t private, const struct iio_chan_spec *chan, const char *buf, 76 size_t len) 77 { 78 struct ad7303_state *st = iio_priv(indio_dev); 79 bool pwr_down; 80 int ret; 81 82 ret = strtobool(buf, &pwr_down); 83 if (ret) 84 return ret; 85 86 mutex_lock(&st->lock); 87 88 if (pwr_down) 89 st->config |= AD7303_CFG_POWER_DOWN(chan->channel); 90 else 91 st->config &= ~AD7303_CFG_POWER_DOWN(chan->channel); 92 93 /* There is no noop cmd which allows us to only update the powerdown 94 * mode, so just write one of the DAC channels again */ 95 ad7303_write(st, chan->channel, st->dac_cache[chan->channel]); 96 97 mutex_unlock(&st->lock); 98 return len; 99 } 100 101 static int ad7303_get_vref(struct ad7303_state *st, 102 struct iio_chan_spec const *chan) 103 { 104 int ret; 105 106 if (st->config & AD7303_CFG_EXTERNAL_VREF) 107 return regulator_get_voltage(st->vref_reg); 108 109 ret = regulator_get_voltage(st->vdd_reg); 110 if (ret < 0) 111 return ret; 112 return ret / 2; 113 } 114 115 static int ad7303_read_raw(struct iio_dev *indio_dev, 116 struct iio_chan_spec const *chan, int *val, int *val2, long info) 117 { 118 struct ad7303_state *st = iio_priv(indio_dev); 119 int vref_uv; 120 121 switch (info) { 122 case IIO_CHAN_INFO_RAW: 123 mutex_lock(&st->lock); 124 *val = st->dac_cache[chan->channel]; 125 mutex_unlock(&st->lock); 126 return IIO_VAL_INT; 127 case IIO_CHAN_INFO_SCALE: 128 vref_uv = ad7303_get_vref(st, chan); 129 if (vref_uv < 0) 130 return vref_uv; 131 132 *val = 2 * vref_uv / 1000; 133 *val2 = chan->scan_type.realbits; 134 135 return IIO_VAL_FRACTIONAL_LOG2; 136 default: 137 break; 138 } 139 return -EINVAL; 140 } 141 142 static int ad7303_write_raw(struct iio_dev *indio_dev, 143 struct iio_chan_spec const *chan, int val, int val2, long mask) 144 { 145 struct ad7303_state *st = iio_priv(indio_dev); 146 int ret; 147 148 switch (mask) { 149 case IIO_CHAN_INFO_RAW: 150 if (val >= (1 << chan->scan_type.realbits) || val < 0) 151 return -EINVAL; 152 153 mutex_lock(&st->lock); 154 ret = ad7303_write(st, chan->address, val); 155 if (ret == 0) 156 st->dac_cache[chan->channel] = val; 157 mutex_unlock(&st->lock); 158 break; 159 default: 160 ret = -EINVAL; 161 } 162 163 return ret; 164 } 165 166 static const struct iio_info ad7303_info = { 167 .read_raw = ad7303_read_raw, 168 .write_raw = ad7303_write_raw, 169 }; 170 171 static const struct iio_chan_spec_ext_info ad7303_ext_info[] = { 172 { 173 .name = "powerdown", 174 .read = ad7303_read_dac_powerdown, 175 .write = ad7303_write_dac_powerdown, 176 .shared = IIO_SEPARATE, 177 }, 178 { }, 179 }; 180 181 #define AD7303_CHANNEL(chan) { \ 182 .type = IIO_VOLTAGE, \ 183 .indexed = 1, \ 184 .output = 1, \ 185 .channel = (chan), \ 186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 188 .address = (chan), \ 189 .scan_type = { \ 190 .sign = 'u', \ 191 .realbits = 8, \ 192 .storagebits = 8, \ 193 .shift = 0, \ 194 }, \ 195 .ext_info = ad7303_ext_info, \ 196 } 197 198 static const struct iio_chan_spec ad7303_channels[] = { 199 AD7303_CHANNEL(0), 200 AD7303_CHANNEL(1), 201 }; 202 203 static int ad7303_probe(struct spi_device *spi) 204 { 205 const struct spi_device_id *id = spi_get_device_id(spi); 206 struct iio_dev *indio_dev; 207 struct ad7303_state *st; 208 int ret; 209 210 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 211 if (indio_dev == NULL) 212 return -ENOMEM; 213 214 st = iio_priv(indio_dev); 215 spi_set_drvdata(spi, indio_dev); 216 217 st->spi = spi; 218 219 mutex_init(&st->lock); 220 221 st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd"); 222 if (IS_ERR(st->vdd_reg)) 223 return PTR_ERR(st->vdd_reg); 224 225 ret = regulator_enable(st->vdd_reg); 226 if (ret) 227 return ret; 228 229 st->vref_reg = devm_regulator_get_optional(&spi->dev, "REF"); 230 if (IS_ERR(st->vref_reg)) { 231 ret = PTR_ERR(st->vref_reg); 232 if (ret != -ENODEV) 233 goto err_disable_vdd_reg; 234 st->vref_reg = NULL; 235 } 236 237 if (st->vref_reg) { 238 ret = regulator_enable(st->vref_reg); 239 if (ret) 240 goto err_disable_vdd_reg; 241 242 st->config |= AD7303_CFG_EXTERNAL_VREF; 243 } 244 245 indio_dev->name = id->name; 246 indio_dev->info = &ad7303_info; 247 indio_dev->modes = INDIO_DIRECT_MODE; 248 indio_dev->channels = ad7303_channels; 249 indio_dev->num_channels = ARRAY_SIZE(ad7303_channels); 250 251 ret = iio_device_register(indio_dev); 252 if (ret) 253 goto err_disable_vref_reg; 254 255 return 0; 256 257 err_disable_vref_reg: 258 if (st->vref_reg) 259 regulator_disable(st->vref_reg); 260 err_disable_vdd_reg: 261 regulator_disable(st->vdd_reg); 262 return ret; 263 } 264 265 static int ad7303_remove(struct spi_device *spi) 266 { 267 struct iio_dev *indio_dev = spi_get_drvdata(spi); 268 struct ad7303_state *st = iio_priv(indio_dev); 269 270 iio_device_unregister(indio_dev); 271 272 if (st->vref_reg) 273 regulator_disable(st->vref_reg); 274 regulator_disable(st->vdd_reg); 275 276 return 0; 277 } 278 279 static const struct of_device_id ad7303_spi_of_match[] = { 280 { .compatible = "adi,ad7303", }, 281 { /* sentinel */ }, 282 }; 283 MODULE_DEVICE_TABLE(of, ad7303_spi_of_match); 284 285 static const struct spi_device_id ad7303_spi_ids[] = { 286 { "ad7303", 0 }, 287 {} 288 }; 289 MODULE_DEVICE_TABLE(spi, ad7303_spi_ids); 290 291 static struct spi_driver ad7303_driver = { 292 .driver = { 293 .name = "ad7303", 294 .of_match_table = ad7303_spi_of_match, 295 }, 296 .probe = ad7303_probe, 297 .remove = ad7303_remove, 298 .id_table = ad7303_spi_ids, 299 }; 300 module_spi_driver(ad7303_driver); 301 302 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 303 MODULE_DESCRIPTION("Analog Devices AD7303 DAC driver"); 304 MODULE_LICENSE("GPL v2"); 305