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 #include <asm/unaligned.h>
17 #define LTC2632_CMD_WRITE_INPUT_N 0x0
18 #define LTC2632_CMD_UPDATE_DAC_N 0x1
19 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
20 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_N 0x3
21 #define LTC2632_CMD_POWERDOWN_DAC_N 0x4
22 #define LTC2632_CMD_POWERDOWN_CHIP 0x5
23 #define LTC2632_CMD_INTERNAL_REFER 0x6
24 #define LTC2632_CMD_EXTERNAL_REFER 0x7
27 * struct ltc2632_chip_info - chip specific information
28 * @channels: channel spec for the DAC
29 * @num_channels: DAC channel count of the chip
30 * @vref_mv: internal reference voltage
32 struct ltc2632_chip_info
{
33 const struct iio_chan_spec
*channels
;
34 const size_t num_channels
;
39 * struct ltc2632_state - driver instance specific data
40 * @spi_dev: pointer to the spi_device struct
41 * @powerdown_cache_mask: used to show current channel powerdown state
42 * @vref_mv: used reference voltage (internal or external)
43 * @vref_reg: regulator for the reference voltage
45 struct ltc2632_state
{
46 struct spi_device
*spi_dev
;
47 unsigned int powerdown_cache_mask
;
49 struct regulator
*vref_reg
;
52 enum ltc2632_supported_device_ids
{
73 static int ltc2632_spi_write(struct spi_device
*spi
,
74 u8 cmd
, u8 addr
, u16 val
, u8 shift
)
80 * The input shift register is 24 bits wide.
81 * The next four are the command bits, C3 to C0,
82 * followed by the 4-bit DAC address, A3 to A0, and then the
83 * 12-, 10-, 8-bit data-word. The data-word comprises the 12-,
84 * 10-, 8-bit input code followed by 4, 6, or 8 don't care bits.
86 data
= (cmd
<< 20) | (addr
<< 16) | (val
<< shift
);
87 put_unaligned_be24(data
, &msg
[0]);
89 return spi_write(spi
, msg
, sizeof(msg
));
92 static int ltc2632_read_raw(struct iio_dev
*indio_dev
,
93 struct iio_chan_spec
const *chan
,
98 const struct ltc2632_state
*st
= iio_priv(indio_dev
);
101 case IIO_CHAN_INFO_SCALE
:
103 *val2
= chan
->scan_type
.realbits
;
104 return IIO_VAL_FRACTIONAL_LOG2
;
109 static int ltc2632_write_raw(struct iio_dev
*indio_dev
,
110 struct iio_chan_spec
const *chan
,
115 struct ltc2632_state
*st
= iio_priv(indio_dev
);
118 case IIO_CHAN_INFO_RAW
:
119 if (val
>= (1 << chan
->scan_type
.realbits
) || val
< 0)
122 return ltc2632_spi_write(st
->spi_dev
,
123 LTC2632_CMD_WRITE_INPUT_N_UPDATE_N
,
125 chan
->scan_type
.shift
);
131 static ssize_t
ltc2632_read_dac_powerdown(struct iio_dev
*indio_dev
,
133 const struct iio_chan_spec
*chan
,
136 struct ltc2632_state
*st
= iio_priv(indio_dev
);
138 return sprintf(buf
, "%d\n",
139 !!(st
->powerdown_cache_mask
& (1 << chan
->channel
)));
142 static ssize_t
ltc2632_write_dac_powerdown(struct iio_dev
*indio_dev
,
144 const struct iio_chan_spec
*chan
,
150 struct ltc2632_state
*st
= iio_priv(indio_dev
);
152 ret
= strtobool(buf
, &pwr_down
);
157 st
->powerdown_cache_mask
|= (1 << chan
->channel
);
159 st
->powerdown_cache_mask
&= ~(1 << chan
->channel
);
161 ret
= ltc2632_spi_write(st
->spi_dev
,
162 LTC2632_CMD_POWERDOWN_DAC_N
,
163 chan
->channel
, 0, 0);
165 return ret
? ret
: len
;
168 static const struct iio_info ltc2632_info
= {
169 .write_raw
= ltc2632_write_raw
,
170 .read_raw
= ltc2632_read_raw
,
173 static const struct iio_chan_spec_ext_info ltc2632_ext_info
[] = {
176 .read
= ltc2632_read_dac_powerdown
,
177 .write
= ltc2632_write_dac_powerdown
,
178 .shared
= IIO_SEPARATE
,
183 #define LTC2632_CHANNEL(_chan, _bits) { \
184 .type = IIO_VOLTAGE, \
187 .channel = (_chan), \
188 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
189 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
190 .address = (_chan), \
192 .realbits = (_bits), \
193 .shift = 16 - (_bits), \
195 .ext_info = ltc2632_ext_info, \
198 #define DECLARE_LTC2632_CHANNELS(_name, _bits) \
199 const struct iio_chan_spec _name ## _channels[] = { \
200 LTC2632_CHANNEL(0, _bits), \
201 LTC2632_CHANNEL(1, _bits), \
202 LTC2632_CHANNEL(2, _bits), \
203 LTC2632_CHANNEL(3, _bits), \
204 LTC2632_CHANNEL(4, _bits), \
205 LTC2632_CHANNEL(5, _bits), \
206 LTC2632_CHANNEL(6, _bits), \
207 LTC2632_CHANNEL(7, _bits), \
210 static DECLARE_LTC2632_CHANNELS(ltc2632x12
, 12);
211 static DECLARE_LTC2632_CHANNELS(ltc2632x10
, 10);
212 static DECLARE_LTC2632_CHANNELS(ltc2632x8
, 8);
214 static const struct ltc2632_chip_info ltc2632_chip_info_tbl
[] = {
216 .channels
= ltc2632x12_channels
,
221 .channels
= ltc2632x10_channels
,
226 .channels
= ltc2632x8_channels
,
231 .channels
= ltc2632x12_channels
,
236 .channels
= ltc2632x10_channels
,
241 .channels
= ltc2632x8_channels
,
246 .channels
= ltc2632x12_channels
,
251 .channels
= ltc2632x10_channels
,
256 .channels
= ltc2632x8_channels
,
261 .channels
= ltc2632x12_channels
,
266 .channels
= ltc2632x10_channels
,
271 .channels
= ltc2632x8_channels
,
276 .channels
= ltc2632x12_channels
,
281 .channels
= ltc2632x10_channels
,
286 .channels
= ltc2632x8_channels
,
291 .channels
= ltc2632x12_channels
,
296 .channels
= ltc2632x10_channels
,
301 .channels
= ltc2632x8_channels
,
307 static int ltc2632_probe(struct spi_device
*spi
)
309 struct ltc2632_state
*st
;
310 struct iio_dev
*indio_dev
;
311 struct ltc2632_chip_info
*chip_info
;
314 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
318 st
= iio_priv(indio_dev
);
320 spi_set_drvdata(spi
, indio_dev
);
323 chip_info
= (struct ltc2632_chip_info
*)
324 spi_get_device_id(spi
)->driver_data
;
326 st
->vref_reg
= devm_regulator_get_optional(&spi
->dev
, "vref");
327 if (PTR_ERR(st
->vref_reg
) == -ENODEV
) {
328 /* use internal reference voltage */
330 st
->vref_mv
= chip_info
->vref_mv
;
332 ret
= ltc2632_spi_write(spi
, LTC2632_CMD_INTERNAL_REFER
,
336 "Set internal reference command failed, %d\n",
340 } else if (IS_ERR(st
->vref_reg
)) {
342 "Error getting voltage reference regulator\n");
343 return PTR_ERR(st
->vref_reg
);
345 /* use external reference voltage */
346 ret
= regulator_enable(st
->vref_reg
);
349 "enable reference regulator failed, %d\n",
353 st
->vref_mv
= regulator_get_voltage(st
->vref_reg
) / 1000;
355 ret
= ltc2632_spi_write(spi
, LTC2632_CMD_EXTERNAL_REFER
,
359 "Set external reference command failed, %d\n",
365 indio_dev
->name
= dev_of_node(&spi
->dev
) ? dev_of_node(&spi
->dev
)->name
366 : spi_get_device_id(spi
)->name
;
367 indio_dev
->info
= <c2632_info
;
368 indio_dev
->modes
= INDIO_DIRECT_MODE
;
369 indio_dev
->channels
= chip_info
->channels
;
370 indio_dev
->num_channels
= chip_info
->num_channels
;
372 return iio_device_register(indio_dev
);
375 static int ltc2632_remove(struct spi_device
*spi
)
377 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
378 struct ltc2632_state
*st
= iio_priv(indio_dev
);
380 iio_device_unregister(indio_dev
);
383 regulator_disable(st
->vref_reg
);
388 static const struct spi_device_id ltc2632_id
[] = {
389 { "ltc2632-l12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632L12
] },
390 { "ltc2632-l10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632L10
] },
391 { "ltc2632-l8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632L8
] },
392 { "ltc2632-h12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632H12
] },
393 { "ltc2632-h10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632H10
] },
394 { "ltc2632-h8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632H8
] },
395 { "ltc2634-l12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634L12
] },
396 { "ltc2634-l10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634L10
] },
397 { "ltc2634-l8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634L8
] },
398 { "ltc2634-h12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634H12
] },
399 { "ltc2634-h10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634H10
] },
400 { "ltc2634-h8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634H8
] },
401 { "ltc2636-l12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636L12
] },
402 { "ltc2636-l10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636L10
] },
403 { "ltc2636-l8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636L8
] },
404 { "ltc2636-h12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636H12
] },
405 { "ltc2636-h10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636H10
] },
406 { "ltc2636-h8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636H8
] },
409 MODULE_DEVICE_TABLE(spi
, ltc2632_id
);
411 static const struct of_device_id ltc2632_of_match
[] = {
413 .compatible
= "lltc,ltc2632-l12",
414 .data
= <c2632_chip_info_tbl
[ID_LTC2632L12
]
416 .compatible
= "lltc,ltc2632-l10",
417 .data
= <c2632_chip_info_tbl
[ID_LTC2632L10
]
419 .compatible
= "lltc,ltc2632-l8",
420 .data
= <c2632_chip_info_tbl
[ID_LTC2632L8
]
422 .compatible
= "lltc,ltc2632-h12",
423 .data
= <c2632_chip_info_tbl
[ID_LTC2632H12
]
425 .compatible
= "lltc,ltc2632-h10",
426 .data
= <c2632_chip_info_tbl
[ID_LTC2632H10
]
428 .compatible
= "lltc,ltc2632-h8",
429 .data
= <c2632_chip_info_tbl
[ID_LTC2632H8
]
431 .compatible
= "lltc,ltc2634-l12",
432 .data
= <c2632_chip_info_tbl
[ID_LTC2634L12
]
434 .compatible
= "lltc,ltc2634-l10",
435 .data
= <c2632_chip_info_tbl
[ID_LTC2634L10
]
437 .compatible
= "lltc,ltc2634-l8",
438 .data
= <c2632_chip_info_tbl
[ID_LTC2634L8
]
440 .compatible
= "lltc,ltc2634-h12",
441 .data
= <c2632_chip_info_tbl
[ID_LTC2634H12
]
443 .compatible
= "lltc,ltc2634-h10",
444 .data
= <c2632_chip_info_tbl
[ID_LTC2634H10
]
446 .compatible
= "lltc,ltc2634-h8",
447 .data
= <c2632_chip_info_tbl
[ID_LTC2634H8
]
449 .compatible
= "lltc,ltc2636-l12",
450 .data
= <c2632_chip_info_tbl
[ID_LTC2636L12
]
452 .compatible
= "lltc,ltc2636-l10",
453 .data
= <c2632_chip_info_tbl
[ID_LTC2636L10
]
455 .compatible
= "lltc,ltc2636-l8",
456 .data
= <c2632_chip_info_tbl
[ID_LTC2636L8
]
458 .compatible
= "lltc,ltc2636-h12",
459 .data
= <c2632_chip_info_tbl
[ID_LTC2636H12
]
461 .compatible
= "lltc,ltc2636-h10",
462 .data
= <c2632_chip_info_tbl
[ID_LTC2636H10
]
464 .compatible
= "lltc,ltc2636-h8",
465 .data
= <c2632_chip_info_tbl
[ID_LTC2636H8
]
469 MODULE_DEVICE_TABLE(of
, ltc2632_of_match
);
471 static struct spi_driver ltc2632_driver
= {
474 .of_match_table
= of_match_ptr(ltc2632_of_match
),
476 .probe
= ltc2632_probe
,
477 .remove
= ltc2632_remove
,
478 .id_table
= ltc2632_id
,
480 module_spi_driver(ltc2632_driver
);
482 MODULE_AUTHOR("Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>");
483 MODULE_DESCRIPTION("LTC2632 DAC SPI driver");
484 MODULE_LICENSE("GPL v2");