1 // SPDX-License-Identifier: GPL-2.0-only
3 * LTC2632 Digital to analog convertors spi driver
5 * Copyright 2017 Maxime Roussin-BĂ©langer
6 * expanded by Silvan Murer <silvan.murer@gmail.com>
9 #include <linux/device.h>
10 #include <linux/spi/spi.h>
11 #include <linux/module.h>
12 #include <linux/iio/iio.h>
13 #include <linux/regulator/consumer.h>
15 #define LTC2632_CMD_WRITE_INPUT_N 0x0
16 #define LTC2632_CMD_UPDATE_DAC_N 0x1
17 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
18 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_N 0x3
19 #define LTC2632_CMD_POWERDOWN_DAC_N 0x4
20 #define LTC2632_CMD_POWERDOWN_CHIP 0x5
21 #define LTC2632_CMD_INTERNAL_REFER 0x6
22 #define LTC2632_CMD_EXTERNAL_REFER 0x7
25 * struct ltc2632_chip_info - chip specific information
26 * @channels: channel spec for the DAC
27 * @vref_mv: internal reference voltage
29 struct ltc2632_chip_info {
30 const struct iio_chan_spec *channels;
31 const size_t num_channels;
36 * struct ltc2632_state - driver instance specific data
37 * @spi_dev: pointer to the spi_device struct
38 * @powerdown_cache_mask used to show current channel powerdown state
39 * @vref_mv used reference voltage (internal or external)
40 * @vref_reg regulator for the reference voltage
42 struct ltc2632_state {
43 struct spi_device *spi_dev;
44 unsigned int powerdown_cache_mask;
46 struct regulator *vref_reg;
49 enum ltc2632_supported_device_ids {
64 static int ltc2632_spi_write(struct spi_device *spi,
65 u8 cmd, u8 addr, u16 val, u8 shift)
71 * The input shift register is 24 bits wide.
72 * The next four are the command bits, C3 to C0,
73 * followed by the 4-bit DAC address, A3 to A0, and then the
74 * 12-, 10-, 8-bit data-word. The data-word comprises the 12-,
75 * 10-, 8-bit input code followed by 4, 6, or 8 don't care bits.
77 data = (cmd << 20) | (addr << 16) | (val << shift);
82 return spi_write(spi, msg, sizeof(msg));
85 static int ltc2632_read_raw(struct iio_dev *indio_dev,
86 struct iio_chan_spec const *chan,
91 const struct ltc2632_state *st = iio_priv(indio_dev);
94 case IIO_CHAN_INFO_SCALE:
96 *val2 = chan->scan_type.realbits;
97 return IIO_VAL_FRACTIONAL_LOG2;
102 static int ltc2632_write_raw(struct iio_dev *indio_dev,
103 struct iio_chan_spec const *chan,
108 struct ltc2632_state *st = iio_priv(indio_dev);
111 case IIO_CHAN_INFO_RAW:
112 if (val >= (1 << chan->scan_type.realbits) || val < 0)
115 return ltc2632_spi_write(st->spi_dev,
116 LTC2632_CMD_WRITE_INPUT_N_UPDATE_N,
118 chan->scan_type.shift);
124 static ssize_t ltc2632_read_dac_powerdown(struct iio_dev *indio_dev,
126 const struct iio_chan_spec *chan,
129 struct ltc2632_state *st = iio_priv(indio_dev);
131 return sprintf(buf, "%d\n",
132 !!(st->powerdown_cache_mask & (1 << chan->channel)));
135 static ssize_t ltc2632_write_dac_powerdown(struct iio_dev *indio_dev,
137 const struct iio_chan_spec *chan,
143 struct ltc2632_state *st = iio_priv(indio_dev);
145 ret = strtobool(buf, &pwr_down);
150 st->powerdown_cache_mask |= (1 << chan->channel);
152 st->powerdown_cache_mask &= ~(1 << chan->channel);
154 ret = ltc2632_spi_write(st->spi_dev,
155 LTC2632_CMD_POWERDOWN_DAC_N,
156 chan->channel, 0, 0);
158 return ret ? ret : len;
161 static const struct iio_info ltc2632_info = {
162 .write_raw = ltc2632_write_raw,
163 .read_raw = ltc2632_read_raw,
166 static const struct iio_chan_spec_ext_info ltc2632_ext_info[] = {
169 .read = ltc2632_read_dac_powerdown,
170 .write = ltc2632_write_dac_powerdown,
171 .shared = IIO_SEPARATE,
176 #define LTC2632_CHANNEL(_chan, _bits) { \
177 .type = IIO_VOLTAGE, \
180 .channel = (_chan), \
181 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
182 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
183 .address = (_chan), \
185 .realbits = (_bits), \
186 .shift = 16 - (_bits), \
188 .ext_info = ltc2632_ext_info, \
191 #define DECLARE_LTC2632_CHANNELS(_name, _bits) \
192 const struct iio_chan_spec _name ## _channels[] = { \
193 LTC2632_CHANNEL(0, _bits), \
194 LTC2632_CHANNEL(1, _bits), \
195 LTC2632_CHANNEL(2, _bits), \
196 LTC2632_CHANNEL(3, _bits), \
197 LTC2632_CHANNEL(4, _bits), \
198 LTC2632_CHANNEL(5, _bits), \
199 LTC2632_CHANNEL(6, _bits), \
200 LTC2632_CHANNEL(7, _bits), \
203 static DECLARE_LTC2632_CHANNELS(ltc2632x12, 12);
204 static DECLARE_LTC2632_CHANNELS(ltc2632x10, 10);
205 static DECLARE_LTC2632_CHANNELS(ltc2632x8, 8);
207 static const struct ltc2632_chip_info ltc2632_chip_info_tbl[] = {
209 .channels = ltc2632x12_channels,
214 .channels = ltc2632x10_channels,
219 .channels = ltc2632x8_channels,
224 .channels = ltc2632x12_channels,
229 .channels = ltc2632x10_channels,
234 .channels = ltc2632x8_channels,
239 .channels = ltc2632x12_channels,
244 .channels = ltc2632x10_channels,
249 .channels = ltc2632x8_channels,
254 .channels = ltc2632x12_channels,
259 .channels = ltc2632x10_channels,
264 .channels = ltc2632x8_channels,
270 static int ltc2632_probe(struct spi_device *spi)
272 struct ltc2632_state *st;
273 struct iio_dev *indio_dev;
274 struct ltc2632_chip_info *chip_info;
277 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
281 st = iio_priv(indio_dev);
283 spi_set_drvdata(spi, indio_dev);
286 chip_info = (struct ltc2632_chip_info *)
287 spi_get_device_id(spi)->driver_data;
289 st->vref_reg = devm_regulator_get_optional(&spi->dev, "vref");
290 if (PTR_ERR(st->vref_reg) == -ENODEV) {
291 /* use internal reference voltage */
293 st->vref_mv = chip_info->vref_mv;
295 ret = ltc2632_spi_write(spi, LTC2632_CMD_INTERNAL_REFER,
299 "Set internal reference command failed, %d\n",
303 } else if (IS_ERR(st->vref_reg)) {
305 "Error getting voltage reference regulator\n");
306 return PTR_ERR(st->vref_reg);
308 /* use external reference voltage */
309 ret = regulator_enable(st->vref_reg);
312 "enable reference regulator failed, %d\n",
316 st->vref_mv = regulator_get_voltage(st->vref_reg) / 1000;
318 ret = ltc2632_spi_write(spi, LTC2632_CMD_EXTERNAL_REFER,
322 "Set external reference command failed, %d\n",
328 indio_dev->dev.parent = &spi->dev;
329 indio_dev->name = dev_of_node(&spi->dev) ? dev_of_node(&spi->dev)->name
330 : spi_get_device_id(spi)->name;
331 indio_dev->info = <c2632_info;
332 indio_dev->modes = INDIO_DIRECT_MODE;
333 indio_dev->channels = chip_info->channels;
334 indio_dev->num_channels = chip_info->num_channels;
336 return iio_device_register(indio_dev);
339 static int ltc2632_remove(struct spi_device *spi)
341 struct iio_dev *indio_dev = spi_get_drvdata(spi);
342 struct ltc2632_state *st = iio_priv(indio_dev);
344 iio_device_unregister(indio_dev);
347 regulator_disable(st->vref_reg);
352 static const struct spi_device_id ltc2632_id[] = {
353 { "ltc2632-l12", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632L12] },
354 { "ltc2632-l10", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632L10] },
355 { "ltc2632-l8", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632L8] },
356 { "ltc2632-h12", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632H12] },
357 { "ltc2632-h10", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632H10] },
358 { "ltc2632-h8", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2632H8] },
359 { "ltc2636-l12", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2636L12] },
360 { "ltc2636-l10", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2636L10] },
361 { "ltc2636-l8", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2636L8] },
362 { "ltc2636-h12", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2636H12] },
363 { "ltc2636-h10", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2636H10] },
364 { "ltc2636-h8", (kernel_ulong_t)<c2632_chip_info_tbl[ID_LTC2636H8] },
367 MODULE_DEVICE_TABLE(spi, ltc2632_id);
369 static const struct of_device_id ltc2632_of_match[] = {
371 .compatible = "lltc,ltc2632-l12",
372 .data = <c2632_chip_info_tbl[ID_LTC2632L12]
374 .compatible = "lltc,ltc2632-l10",
375 .data = <c2632_chip_info_tbl[ID_LTC2632L10]
377 .compatible = "lltc,ltc2632-l8",
378 .data = <c2632_chip_info_tbl[ID_LTC2632L8]
380 .compatible = "lltc,ltc2632-h12",
381 .data = <c2632_chip_info_tbl[ID_LTC2632H12]
383 .compatible = "lltc,ltc2632-h10",
384 .data = <c2632_chip_info_tbl[ID_LTC2632H10]
386 .compatible = "lltc,ltc2632-h8",
387 .data = <c2632_chip_info_tbl[ID_LTC2632H8]
389 .compatible = "lltc,ltc2636-l12",
390 .data = <c2632_chip_info_tbl[ID_LTC2636L12]
392 .compatible = "lltc,ltc2636-l10",
393 .data = <c2632_chip_info_tbl[ID_LTC2636L10]
395 .compatible = "lltc,ltc2636-l8",
396 .data = <c2632_chip_info_tbl[ID_LTC2636L8]
398 .compatible = "lltc,ltc2636-h12",
399 .data = <c2632_chip_info_tbl[ID_LTC2636H12]
401 .compatible = "lltc,ltc2636-h10",
402 .data = <c2632_chip_info_tbl[ID_LTC2636H10]
404 .compatible = "lltc,ltc2636-h8",
405 .data = <c2632_chip_info_tbl[ID_LTC2636H8]
409 MODULE_DEVICE_TABLE(of, ltc2632_of_match);
411 static struct spi_driver ltc2632_driver = {
414 .of_match_table = of_match_ptr(ltc2632_of_match),
416 .probe = ltc2632_probe,
417 .remove = ltc2632_remove,
418 .id_table = ltc2632_id,
420 module_spi_driver(ltc2632_driver);
422 MODULE_AUTHOR("Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>");
423 MODULE_DESCRIPTION("LTC2632 DAC SPI driver");
424 MODULE_LICENSE("GPL v2");