1 /* 2 * LTC2632 Digital to analog convertors spi driver 3 * 4 * Copyright 2017 Maxime Roussin-B�langer 5 * expanded by Silvan Murer <silvan.murer@gmail.com> 6 * 7 * Licensed under the GPL-2. 8 */ 9 10 #include <linux/device.h> 11 #include <linux/spi/spi.h> 12 #include <linux/module.h> 13 #include <linux/iio/iio.h> 14 #include <linux/regulator/consumer.h> 15 16 #define LTC2632_DAC_CHANNELS 2 17 18 #define LTC2632_ADDR_DAC0 0x0 19 #define LTC2632_ADDR_DAC1 0x1 20 21 #define LTC2632_CMD_WRITE_INPUT_N 0x0 22 #define LTC2632_CMD_UPDATE_DAC_N 0x1 23 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2 24 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_N 0x3 25 #define LTC2632_CMD_POWERDOWN_DAC_N 0x4 26 #define LTC2632_CMD_POWERDOWN_CHIP 0x5 27 #define LTC2632_CMD_INTERNAL_REFER 0x6 28 #define LTC2632_CMD_EXTERNAL_REFER 0x7 29 30 /** 31 * struct ltc2632_chip_info - chip specific information 32 * @channels: channel spec for the DAC 33 * @vref_mv: internal reference voltage 34 */ 35 struct ltc2632_chip_info { 36 const struct iio_chan_spec *channels; 37 const int vref_mv; 38 }; 39 40 /** 41 * struct ltc2632_state - driver instance specific data 42 * @spi_dev: pointer to the spi_device struct 43 * @powerdown_cache_mask used to show current channel powerdown state 44 * @vref_mv used reference voltage (internal or external) 45 * @vref_reg regulator for the reference voltage 46 */ 47 struct ltc2632_state { 48 struct spi_device *spi_dev; 49 unsigned int powerdown_cache_mask; 50 int vref_mv; 51 struct regulator *vref_reg; 52 }; 53 54 enum ltc2632_supported_device_ids { 55 ID_LTC2632L12, 56 ID_LTC2632L10, 57 ID_LTC2632L8, 58 ID_LTC2632H12, 59 ID_LTC2632H10, 60 ID_LTC2632H8, 61 }; 62 63 static int ltc2632_spi_write(struct spi_device *spi, 64 u8 cmd, u8 addr, u16 val, u8 shift) 65 { 66 u32 data; 67 u8 msg[3]; 68 69 /* 70 * The input shift register is 24 bits wide. 71 * The next four are the command bits, C3 to C0, 72 * followed by the 4-bit DAC address, A3 to A0, and then the 73 * 12-, 10-, 8-bit data-word. The data-word comprises the 12-, 74 * 10-, 8-bit input code followed by 4, 6, or 8 don't care bits. 75 */ 76 data = (cmd << 20) | (addr << 16) | (val << shift); 77 msg[0] = data >> 16; 78 msg[1] = data >> 8; 79 msg[2] = data; 80 81 return spi_write(spi, msg, sizeof(msg)); 82 } 83 84 static int ltc2632_read_raw(struct iio_dev *indio_dev, 85 struct iio_chan_spec const *chan, 86 int *val, 87 int *val2, 88 long m) 89 { 90 struct ltc2632_chip_info *chip_info; 91 92 const struct ltc2632_state *st = iio_priv(indio_dev); 93 const struct spi_device_id *spi_dev_id = spi_get_device_id(st->spi_dev); 94 95 chip_info = (struct ltc2632_chip_info *)spi_dev_id->driver_data; 96 97 switch (m) { 98 case IIO_CHAN_INFO_SCALE: 99 *val = st->vref_mv; 100 *val2 = chan->scan_type.realbits; 101 return IIO_VAL_FRACTIONAL_LOG2; 102 } 103 return -EINVAL; 104 } 105 106 static int ltc2632_write_raw(struct iio_dev *indio_dev, 107 struct iio_chan_spec const *chan, 108 int val, 109 int val2, 110 long mask) 111 { 112 struct ltc2632_state *st = iio_priv(indio_dev); 113 114 switch (mask) { 115 case IIO_CHAN_INFO_RAW: 116 if (val >= (1 << chan->scan_type.realbits) || val < 0) 117 return -EINVAL; 118 119 return ltc2632_spi_write(st->spi_dev, 120 LTC2632_CMD_WRITE_INPUT_N_UPDATE_N, 121 chan->address, val, 122 chan->scan_type.shift); 123 default: 124 return -EINVAL; 125 } 126 } 127 128 static ssize_t ltc2632_read_dac_powerdown(struct iio_dev *indio_dev, 129 uintptr_t private, 130 const struct iio_chan_spec *chan, 131 char *buf) 132 { 133 struct ltc2632_state *st = iio_priv(indio_dev); 134 135 return sprintf(buf, "%d\n", 136 !!(st->powerdown_cache_mask & (1 << chan->channel))); 137 } 138 139 static ssize_t ltc2632_write_dac_powerdown(struct iio_dev *indio_dev, 140 uintptr_t private, 141 const struct iio_chan_spec *chan, 142 const char *buf, 143 size_t len) 144 { 145 bool pwr_down; 146 int ret; 147 struct ltc2632_state *st = iio_priv(indio_dev); 148 149 ret = strtobool(buf, &pwr_down); 150 if (ret) 151 return ret; 152 153 if (pwr_down) 154 st->powerdown_cache_mask |= (1 << chan->channel); 155 else 156 st->powerdown_cache_mask &= ~(1 << chan->channel); 157 158 ret = ltc2632_spi_write(st->spi_dev, 159 LTC2632_CMD_POWERDOWN_DAC_N, 160 chan->channel, 0, 0); 161 162 return ret ? ret : len; 163 } 164 165 static const struct iio_info ltc2632_info = { 166 .write_raw = ltc2632_write_raw, 167 .read_raw = ltc2632_read_raw, 168 }; 169 170 static const struct iio_chan_spec_ext_info ltc2632_ext_info[] = { 171 { 172 .name = "powerdown", 173 .read = ltc2632_read_dac_powerdown, 174 .write = ltc2632_write_dac_powerdown, 175 .shared = IIO_SEPARATE, 176 }, 177 { }, 178 }; 179 180 #define LTC2632_CHANNEL(_chan, _bits) { \ 181 .type = IIO_VOLTAGE, \ 182 .indexed = 1, \ 183 .output = 1, \ 184 .channel = (_chan), \ 185 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 186 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 187 .address = (_chan), \ 188 .scan_type = { \ 189 .realbits = (_bits), \ 190 .shift = 16 - (_bits), \ 191 }, \ 192 .ext_info = ltc2632_ext_info, \ 193 } 194 195 #define DECLARE_LTC2632_CHANNELS(_name, _bits) \ 196 const struct iio_chan_spec _name ## _channels[] = { \ 197 LTC2632_CHANNEL(0, _bits), \ 198 LTC2632_CHANNEL(1, _bits), \ 199 } 200 201 static DECLARE_LTC2632_CHANNELS(ltc2632l12, 12); 202 static DECLARE_LTC2632_CHANNELS(ltc2632l10, 10); 203 static DECLARE_LTC2632_CHANNELS(ltc2632l8, 8); 204 205 static DECLARE_LTC2632_CHANNELS(ltc2632h12, 12); 206 static DECLARE_LTC2632_CHANNELS(ltc2632h10, 10); 207 static DECLARE_LTC2632_CHANNELS(ltc2632h8, 8); 208 209 static const struct ltc2632_chip_info ltc2632_chip_info_tbl[] = { 210 [ID_LTC2632L12] = { 211 .channels = ltc2632l12_channels, 212 .vref_mv = 2500, 213 }, 214 [ID_LTC2632L10] = { 215 .channels = ltc2632l10_channels, 216 .vref_mv = 2500, 217 }, 218 [ID_LTC2632L8] = { 219 .channels = ltc2632l8_channels, 220 .vref_mv = 2500, 221 }, 222 [ID_LTC2632H12] = { 223 .channels = ltc2632h12_channels, 224 .vref_mv = 4096, 225 }, 226 [ID_LTC2632H10] = { 227 .channels = ltc2632h10_channels, 228 .vref_mv = 4096, 229 }, 230 [ID_LTC2632H8] = { 231 .channels = ltc2632h8_channels, 232 .vref_mv = 4096, 233 }, 234 }; 235 236 static int ltc2632_probe(struct spi_device *spi) 237 { 238 struct ltc2632_state *st; 239 struct iio_dev *indio_dev; 240 struct ltc2632_chip_info *chip_info; 241 int ret; 242 243 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 244 if (!indio_dev) 245 return -ENOMEM; 246 247 st = iio_priv(indio_dev); 248 249 spi_set_drvdata(spi, indio_dev); 250 st->spi_dev = spi; 251 252 chip_info = (struct ltc2632_chip_info *) 253 spi_get_device_id(spi)->driver_data; 254 255 st->vref_reg = devm_regulator_get_optional(&spi->dev, "vref"); 256 if (PTR_ERR(st->vref_reg) == -ENODEV) { 257 /* use internal reference voltage */ 258 st->vref_reg = NULL; 259 st->vref_mv = chip_info->vref_mv; 260 261 ret = ltc2632_spi_write(spi, LTC2632_CMD_INTERNAL_REFER, 262 0, 0, 0); 263 if (ret) { 264 dev_err(&spi->dev, 265 "Set internal reference command failed, %d\n", 266 ret); 267 return ret; 268 } 269 } else if (IS_ERR(st->vref_reg)) { 270 dev_err(&spi->dev, 271 "Error getting voltage reference regulator\n"); 272 return PTR_ERR(st->vref_reg); 273 } else { 274 /* use external reference voltage */ 275 ret = regulator_enable(st->vref_reg); 276 if (ret) { 277 dev_err(&spi->dev, 278 "enable reference regulator failed, %d\n", 279 ret); 280 return ret; 281 } 282 st->vref_mv = regulator_get_voltage(st->vref_reg) / 1000; 283 284 ret = ltc2632_spi_write(spi, LTC2632_CMD_EXTERNAL_REFER, 285 0, 0, 0); 286 if (ret) { 287 dev_err(&spi->dev, 288 "Set external reference command failed, %d\n", 289 ret); 290 return ret; 291 } 292 } 293 294 indio_dev->dev.parent = &spi->dev; 295 indio_dev->name = dev_of_node(&spi->dev) ? dev_of_node(&spi->dev)->name 296 : spi_get_device_id(spi)->name; 297 indio_dev->info = <c2632_info; 298 indio_dev->modes = INDIO_DIRECT_MODE; 299 indio_dev->channels = chip_info->channels; 300 indio_dev->num_channels = LTC2632_DAC_CHANNELS; 301 302 return iio_device_register(indio_dev); 303 } 304 305 static int ltc2632_remove(struct spi_device *spi) 306 { 307 struct iio_dev *indio_dev = spi_get_drvdata(spi); 308 struct ltc2632_state *st = iio_priv(indio_dev); 309 310 iio_device_unregister(indio_dev); 311 312 if (st->vref_reg) 313 regulator_disable(st->vref_reg); 314 315 return 0; 316 } 317 318 static const struct spi_device_id ltc2632_id[] = { 319 { "ltc2632-l12", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632L12] }, 320 { "ltc2632-l10", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632L10] }, 321 { "ltc2632-l8", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632L8] }, 322 { "ltc2632-h12", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632H12] }, 323 { "ltc2632-h10", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632H10] }, 324 { "ltc2632-h8", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632H8] }, 325 {} 326 }; 327 MODULE_DEVICE_TABLE(spi, ltc2632_id); 328 329 static const struct of_device_id ltc2632_of_match[] = { 330 { 331 .compatible = "lltc,ltc2632-l12", 332 .data = <c2632_chip_info_tbl[ID_LTC2632L12] 333 }, { 334 .compatible = "lltc,ltc2632-l10", 335 .data = <c2632_chip_info_tbl[ID_LTC2632L10] 336 }, { 337 .compatible = "lltc,ltc2632-l8", 338 .data = <c2632_chip_info_tbl[ID_LTC2632L8] 339 }, { 340 .compatible = "lltc,ltc2632-h12", 341 .data = <c2632_chip_info_tbl[ID_LTC2632H12] 342 }, { 343 .compatible = "lltc,ltc2632-h10", 344 .data = <c2632_chip_info_tbl[ID_LTC2632H10] 345 }, { 346 .compatible = "lltc,ltc2632-h8", 347 .data = <c2632_chip_info_tbl[ID_LTC2632H8] 348 }, 349 {} 350 }; 351 MODULE_DEVICE_TABLE(of, ltc2632_of_match); 352 353 static struct spi_driver ltc2632_driver = { 354 .driver = { 355 .name = "ltc2632", 356 .of_match_table = of_match_ptr(ltc2632_of_match), 357 }, 358 .probe = ltc2632_probe, 359 .remove = ltc2632_remove, 360 .id_table = ltc2632_id, 361 }; 362 module_spi_driver(ltc2632_driver); 363 364 MODULE_AUTHOR("Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>"); 365 MODULE_DESCRIPTION("LTC2632 DAC SPI driver"); 366 MODULE_LICENSE("GPL v2"); 367