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/property.h>
14 #include <linux/regulator/consumer.h>
16 #include <linux/unaligned.h>
18 #define LTC2632_CMD_WRITE_INPUT_N 0x0
19 #define LTC2632_CMD_UPDATE_DAC_N 0x1
20 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
21 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_N 0x3
22 #define LTC2632_CMD_POWERDOWN_DAC_N 0x4
23 #define LTC2632_CMD_POWERDOWN_CHIP 0x5
24 #define LTC2632_CMD_INTERNAL_REFER 0x6
25 #define LTC2632_CMD_EXTERNAL_REFER 0x7
28 * struct ltc2632_chip_info - chip specific information
29 * @channels: channel spec for the DAC
30 * @num_channels: DAC channel count of the chip
31 * @vref_mv: internal reference voltage
33 struct ltc2632_chip_info
{
34 const struct iio_chan_spec
*channels
;
35 const size_t num_channels
;
40 * struct ltc2632_state - driver instance specific data
41 * @spi_dev: pointer to the spi_device struct
42 * @powerdown_cache_mask: used to show current channel powerdown state
43 * @vref_mv: used reference voltage (internal or external)
44 * @vref_reg: regulator for the reference voltage
46 struct ltc2632_state
{
47 struct spi_device
*spi_dev
;
48 unsigned int powerdown_cache_mask
;
50 struct regulator
*vref_reg
;
53 enum ltc2632_supported_device_ids
{
74 static int ltc2632_spi_write(struct spi_device
*spi
,
75 u8 cmd
, u8 addr
, u16 val
, u8 shift
)
81 * The input shift register is 24 bits wide.
82 * The next four are the command bits, C3 to C0,
83 * followed by the 4-bit DAC address, A3 to A0, and then the
84 * 12-, 10-, 8-bit data-word. The data-word comprises the 12-,
85 * 10-, 8-bit input code followed by 4, 6, or 8 don't care bits.
87 data
= (cmd
<< 20) | (addr
<< 16) | (val
<< shift
);
88 put_unaligned_be24(data
, &msg
[0]);
90 return spi_write(spi
, msg
, sizeof(msg
));
93 static int ltc2632_read_raw(struct iio_dev
*indio_dev
,
94 struct iio_chan_spec
const *chan
,
99 const struct ltc2632_state
*st
= iio_priv(indio_dev
);
102 case IIO_CHAN_INFO_SCALE
:
104 *val2
= chan
->scan_type
.realbits
;
105 return IIO_VAL_FRACTIONAL_LOG2
;
110 static int ltc2632_write_raw(struct iio_dev
*indio_dev
,
111 struct iio_chan_spec
const *chan
,
116 struct ltc2632_state
*st
= iio_priv(indio_dev
);
119 case IIO_CHAN_INFO_RAW
:
120 if (val
>= (1 << chan
->scan_type
.realbits
) || val
< 0)
123 return ltc2632_spi_write(st
->spi_dev
,
124 LTC2632_CMD_WRITE_INPUT_N_UPDATE_N
,
126 chan
->scan_type
.shift
);
132 static ssize_t
ltc2632_read_dac_powerdown(struct iio_dev
*indio_dev
,
134 const struct iio_chan_spec
*chan
,
137 struct ltc2632_state
*st
= iio_priv(indio_dev
);
139 return sysfs_emit(buf
, "%d\n",
140 !!(st
->powerdown_cache_mask
& (1 << chan
->channel
)));
143 static ssize_t
ltc2632_write_dac_powerdown(struct iio_dev
*indio_dev
,
145 const struct iio_chan_spec
*chan
,
151 struct ltc2632_state
*st
= iio_priv(indio_dev
);
153 ret
= kstrtobool(buf
, &pwr_down
);
158 st
->powerdown_cache_mask
|= (1 << chan
->channel
);
160 st
->powerdown_cache_mask
&= ~(1 << chan
->channel
);
162 ret
= ltc2632_spi_write(st
->spi_dev
,
163 LTC2632_CMD_POWERDOWN_DAC_N
,
164 chan
->channel
, 0, 0);
166 return ret
? ret
: len
;
169 static const struct iio_info ltc2632_info
= {
170 .write_raw
= ltc2632_write_raw
,
171 .read_raw
= ltc2632_read_raw
,
174 static const struct iio_chan_spec_ext_info ltc2632_ext_info
[] = {
177 .read
= ltc2632_read_dac_powerdown
,
178 .write
= ltc2632_write_dac_powerdown
,
179 .shared
= IIO_SEPARATE
,
184 #define LTC2632_CHANNEL(_chan, _bits) { \
185 .type = IIO_VOLTAGE, \
188 .channel = (_chan), \
189 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
190 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
191 .address = (_chan), \
193 .realbits = (_bits), \
194 .shift = 16 - (_bits), \
196 .ext_info = ltc2632_ext_info, \
199 #define DECLARE_LTC2632_CHANNELS(_name, _bits) \
200 const struct iio_chan_spec _name ## _channels[] = { \
201 LTC2632_CHANNEL(0, _bits), \
202 LTC2632_CHANNEL(1, _bits), \
203 LTC2632_CHANNEL(2, _bits), \
204 LTC2632_CHANNEL(3, _bits), \
205 LTC2632_CHANNEL(4, _bits), \
206 LTC2632_CHANNEL(5, _bits), \
207 LTC2632_CHANNEL(6, _bits), \
208 LTC2632_CHANNEL(7, _bits), \
211 static DECLARE_LTC2632_CHANNELS(ltc2632x12
, 12);
212 static DECLARE_LTC2632_CHANNELS(ltc2632x10
, 10);
213 static DECLARE_LTC2632_CHANNELS(ltc2632x8
, 8);
215 static const struct ltc2632_chip_info ltc2632_chip_info_tbl
[] = {
217 .channels
= ltc2632x12_channels
,
222 .channels
= ltc2632x10_channels
,
227 .channels
= ltc2632x8_channels
,
232 .channels
= ltc2632x12_channels
,
237 .channels
= ltc2632x10_channels
,
242 .channels
= ltc2632x8_channels
,
247 .channels
= ltc2632x12_channels
,
252 .channels
= ltc2632x10_channels
,
257 .channels
= ltc2632x8_channels
,
262 .channels
= ltc2632x12_channels
,
267 .channels
= ltc2632x10_channels
,
272 .channels
= ltc2632x8_channels
,
277 .channels
= ltc2632x12_channels
,
282 .channels
= ltc2632x10_channels
,
287 .channels
= ltc2632x8_channels
,
292 .channels
= ltc2632x12_channels
,
297 .channels
= ltc2632x10_channels
,
302 .channels
= ltc2632x8_channels
,
308 static int ltc2632_probe(struct spi_device
*spi
)
310 struct ltc2632_state
*st
;
311 struct iio_dev
*indio_dev
;
312 struct ltc2632_chip_info
*chip_info
;
315 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
319 st
= iio_priv(indio_dev
);
321 spi_set_drvdata(spi
, indio_dev
);
324 chip_info
= (struct ltc2632_chip_info
*)
325 spi_get_device_id(spi
)->driver_data
;
327 st
->vref_reg
= devm_regulator_get_optional(&spi
->dev
, "vref");
328 if (PTR_ERR(st
->vref_reg
) == -ENODEV
) {
329 /* use internal reference voltage */
331 st
->vref_mv
= chip_info
->vref_mv
;
333 ret
= ltc2632_spi_write(spi
, LTC2632_CMD_INTERNAL_REFER
,
337 "Set internal reference command failed, %d\n",
341 } else if (IS_ERR(st
->vref_reg
)) {
343 "Error getting voltage reference regulator\n");
344 return PTR_ERR(st
->vref_reg
);
346 /* use external reference voltage */
347 ret
= regulator_enable(st
->vref_reg
);
350 "enable reference regulator failed, %d\n",
354 st
->vref_mv
= regulator_get_voltage(st
->vref_reg
) / 1000;
356 ret
= ltc2632_spi_write(spi
, LTC2632_CMD_EXTERNAL_REFER
,
360 "Set external reference command failed, %d\n",
366 indio_dev
->name
= fwnode_get_name(dev_fwnode(&spi
->dev
)) ?: 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 void 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
);
386 static const struct spi_device_id ltc2632_id
[] = {
387 { "ltc2632-l12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632L12
] },
388 { "ltc2632-l10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632L10
] },
389 { "ltc2632-l8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632L8
] },
390 { "ltc2632-h12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632H12
] },
391 { "ltc2632-h10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632H10
] },
392 { "ltc2632-h8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2632H8
] },
393 { "ltc2634-l12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634L12
] },
394 { "ltc2634-l10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634L10
] },
395 { "ltc2634-l8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634L8
] },
396 { "ltc2634-h12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634H12
] },
397 { "ltc2634-h10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634H10
] },
398 { "ltc2634-h8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2634H8
] },
399 { "ltc2636-l12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636L12
] },
400 { "ltc2636-l10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636L10
] },
401 { "ltc2636-l8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636L8
] },
402 { "ltc2636-h12", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636H12
] },
403 { "ltc2636-h10", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636H10
] },
404 { "ltc2636-h8", (kernel_ulong_t
)<c2632_chip_info_tbl
[ID_LTC2636H8
] },
407 MODULE_DEVICE_TABLE(spi
, ltc2632_id
);
409 static const struct of_device_id ltc2632_of_match
[] = {
411 .compatible
= "lltc,ltc2632-l12",
412 .data
= <c2632_chip_info_tbl
[ID_LTC2632L12
]
414 .compatible
= "lltc,ltc2632-l10",
415 .data
= <c2632_chip_info_tbl
[ID_LTC2632L10
]
417 .compatible
= "lltc,ltc2632-l8",
418 .data
= <c2632_chip_info_tbl
[ID_LTC2632L8
]
420 .compatible
= "lltc,ltc2632-h12",
421 .data
= <c2632_chip_info_tbl
[ID_LTC2632H12
]
423 .compatible
= "lltc,ltc2632-h10",
424 .data
= <c2632_chip_info_tbl
[ID_LTC2632H10
]
426 .compatible
= "lltc,ltc2632-h8",
427 .data
= <c2632_chip_info_tbl
[ID_LTC2632H8
]
429 .compatible
= "lltc,ltc2634-l12",
430 .data
= <c2632_chip_info_tbl
[ID_LTC2634L12
]
432 .compatible
= "lltc,ltc2634-l10",
433 .data
= <c2632_chip_info_tbl
[ID_LTC2634L10
]
435 .compatible
= "lltc,ltc2634-l8",
436 .data
= <c2632_chip_info_tbl
[ID_LTC2634L8
]
438 .compatible
= "lltc,ltc2634-h12",
439 .data
= <c2632_chip_info_tbl
[ID_LTC2634H12
]
441 .compatible
= "lltc,ltc2634-h10",
442 .data
= <c2632_chip_info_tbl
[ID_LTC2634H10
]
444 .compatible
= "lltc,ltc2634-h8",
445 .data
= <c2632_chip_info_tbl
[ID_LTC2634H8
]
447 .compatible
= "lltc,ltc2636-l12",
448 .data
= <c2632_chip_info_tbl
[ID_LTC2636L12
]
450 .compatible
= "lltc,ltc2636-l10",
451 .data
= <c2632_chip_info_tbl
[ID_LTC2636L10
]
453 .compatible
= "lltc,ltc2636-l8",
454 .data
= <c2632_chip_info_tbl
[ID_LTC2636L8
]
456 .compatible
= "lltc,ltc2636-h12",
457 .data
= <c2632_chip_info_tbl
[ID_LTC2636H12
]
459 .compatible
= "lltc,ltc2636-h10",
460 .data
= <c2632_chip_info_tbl
[ID_LTC2636H10
]
462 .compatible
= "lltc,ltc2636-h8",
463 .data
= <c2632_chip_info_tbl
[ID_LTC2636H8
]
467 MODULE_DEVICE_TABLE(of
, ltc2632_of_match
);
469 static struct spi_driver ltc2632_driver
= {
472 .of_match_table
= ltc2632_of_match
,
474 .probe
= ltc2632_probe
,
475 .remove
= ltc2632_remove
,
476 .id_table
= ltc2632_id
,
478 module_spi_driver(ltc2632_driver
);
480 MODULE_AUTHOR("Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>");
481 MODULE_DESCRIPTION("LTC2632 DAC SPI driver");
482 MODULE_LICENSE("GPL v2");