1 // SPDX-License-Identifier: GPL-2.0
2 /* ti-dac7311.c - Texas Instruments 8/10/12-bit 1-channel DAC driver
4 * Copyright (C) 2018 CMC NV
6 * https://www.ti.com/lit/ds/symlink/dac7311.pdf
9 #include <linux/iio/iio.h>
10 #include <linux/module.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/spi/spi.h>
21 POWER_1KOHM_TO_GND
= 0,
30 static const struct ti_dac_spec ti_dac_spec
[] = {
31 [ID_DAC5311
] = { .resolution
= 8 },
32 [ID_DAC6311
] = { .resolution
= 10 },
33 [ID_DAC7311
] = { .resolution
= 12 },
37 * struct ti_dac_chip - TI DAC chip
38 * @lock: protects write sequences
39 * @vref: regulator generating Vref
40 * @spi: SPI device to send data to the device
42 * @powerdown: whether the chip is powered down
43 * @powerdown_mode: selected by the user
44 * @resolution: resolution of the chip
45 * @buf: buffer for transfer data
49 struct regulator
*vref
;
50 struct spi_device
*spi
;
55 u8 buf
[2] ____cacheline_aligned
;
58 static u8
ti_dac_get_power(struct ti_dac_chip
*ti_dac
, bool powerdown
)
61 return ti_dac
->powerdown_mode
+ 1;
66 static int ti_dac_cmd(struct ti_dac_chip
*ti_dac
, u8 power
, u16 val
)
68 u8 shift
= 14 - ti_dac
->resolution
;
70 ti_dac
->buf
[0] = (val
<< shift
) & 0xFF;
71 ti_dac
->buf
[1] = (power
<< 6) | (val
>> (8 - shift
));
72 return spi_write(ti_dac
->spi
, ti_dac
->buf
, 2);
75 static const char * const ti_dac_powerdown_modes
[] = {
81 static int ti_dac_get_powerdown_mode(struct iio_dev
*indio_dev
,
82 const struct iio_chan_spec
*chan
)
84 struct ti_dac_chip
*ti_dac
= iio_priv(indio_dev
);
86 return ti_dac
->powerdown_mode
;
89 static int ti_dac_set_powerdown_mode(struct iio_dev
*indio_dev
,
90 const struct iio_chan_spec
*chan
,
93 struct ti_dac_chip
*ti_dac
= iio_priv(indio_dev
);
95 ti_dac
->powerdown_mode
= mode
;
99 static const struct iio_enum ti_dac_powerdown_mode
= {
100 .items
= ti_dac_powerdown_modes
,
101 .num_items
= ARRAY_SIZE(ti_dac_powerdown_modes
),
102 .get
= ti_dac_get_powerdown_mode
,
103 .set
= ti_dac_set_powerdown_mode
,
106 static ssize_t
ti_dac_read_powerdown(struct iio_dev
*indio_dev
,
108 const struct iio_chan_spec
*chan
,
111 struct ti_dac_chip
*ti_dac
= iio_priv(indio_dev
);
113 return sprintf(buf
, "%d\n", ti_dac
->powerdown
);
116 static ssize_t
ti_dac_write_powerdown(struct iio_dev
*indio_dev
,
118 const struct iio_chan_spec
*chan
,
119 const char *buf
, size_t len
)
121 struct ti_dac_chip
*ti_dac
= iio_priv(indio_dev
);
126 ret
= strtobool(buf
, &powerdown
);
130 power
= ti_dac_get_power(ti_dac
, powerdown
);
132 mutex_lock(&ti_dac
->lock
);
133 ret
= ti_dac_cmd(ti_dac
, power
, 0);
135 ti_dac
->powerdown
= powerdown
;
136 mutex_unlock(&ti_dac
->lock
);
138 return ret
? ret
: len
;
141 static const struct iio_chan_spec_ext_info ti_dac_ext_info
[] = {
144 .read
= ti_dac_read_powerdown
,
145 .write
= ti_dac_write_powerdown
,
146 .shared
= IIO_SHARED_BY_TYPE
,
148 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE
, &ti_dac_powerdown_mode
),
149 IIO_ENUM_AVAILABLE("powerdown_mode", &ti_dac_powerdown_mode
),
153 #define TI_DAC_CHANNEL(chan) { \
154 .type = IIO_VOLTAGE, \
157 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
158 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
159 .ext_info = ti_dac_ext_info, \
162 static const struct iio_chan_spec ti_dac_channels
[] = {
166 static int ti_dac_read_raw(struct iio_dev
*indio_dev
,
167 struct iio_chan_spec
const *chan
,
168 int *val
, int *val2
, long mask
)
170 struct ti_dac_chip
*ti_dac
= iio_priv(indio_dev
);
174 case IIO_CHAN_INFO_RAW
:
178 case IIO_CHAN_INFO_SCALE
:
179 ret
= regulator_get_voltage(ti_dac
->vref
);
184 *val2
= ti_dac
->resolution
;
185 return IIO_VAL_FRACTIONAL_LOG2
;
191 static int ti_dac_write_raw(struct iio_dev
*indio_dev
,
192 struct iio_chan_spec
const *chan
,
193 int val
, int val2
, long mask
)
195 struct ti_dac_chip
*ti_dac
= iio_priv(indio_dev
);
196 u8 power
= ti_dac_get_power(ti_dac
, ti_dac
->powerdown
);
200 case IIO_CHAN_INFO_RAW
:
201 if (ti_dac
->val
== val
)
204 if (val
>= (1 << ti_dac
->resolution
) || val
< 0)
207 if (ti_dac
->powerdown
)
210 mutex_lock(&ti_dac
->lock
);
211 ret
= ti_dac_cmd(ti_dac
, power
, val
);
214 mutex_unlock(&ti_dac
->lock
);
224 static int ti_dac_write_raw_get_fmt(struct iio_dev
*indio_dev
,
225 struct iio_chan_spec
const *chan
, long mask
)
230 static const struct iio_info ti_dac_info
= {
231 .read_raw
= ti_dac_read_raw
,
232 .write_raw
= ti_dac_write_raw
,
233 .write_raw_get_fmt
= ti_dac_write_raw_get_fmt
,
236 static int ti_dac_probe(struct spi_device
*spi
)
238 struct device
*dev
= &spi
->dev
;
239 const struct ti_dac_spec
*spec
;
240 struct ti_dac_chip
*ti_dac
;
241 struct iio_dev
*indio_dev
;
244 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*ti_dac
));
246 dev_err(dev
, "can not allocate iio device\n");
250 spi
->mode
= SPI_MODE_1
;
251 spi
->bits_per_word
= 16;
254 indio_dev
->info
= &ti_dac_info
;
255 indio_dev
->name
= spi_get_device_id(spi
)->name
;
256 indio_dev
->modes
= INDIO_DIRECT_MODE
;
257 indio_dev
->channels
= ti_dac_channels
;
258 spi_set_drvdata(spi
, indio_dev
);
260 ti_dac
= iio_priv(indio_dev
);
261 ti_dac
->powerdown
= false;
264 spec
= &ti_dac_spec
[spi_get_device_id(spi
)->driver_data
];
265 indio_dev
->num_channels
= 1;
266 ti_dac
->resolution
= spec
->resolution
;
268 ti_dac
->vref
= devm_regulator_get(dev
, "vref");
269 if (IS_ERR(ti_dac
->vref
)) {
270 dev_err(dev
, "error to get regulator\n");
271 return PTR_ERR(ti_dac
->vref
);
274 ret
= regulator_enable(ti_dac
->vref
);
276 dev_err(dev
, "can not enable regulator\n");
280 mutex_init(&ti_dac
->lock
);
282 ret
= iio_device_register(indio_dev
);
284 dev_err(dev
, "fail to register iio device: %d\n", ret
);
291 mutex_destroy(&ti_dac
->lock
);
292 regulator_disable(ti_dac
->vref
);
296 static int ti_dac_remove(struct spi_device
*spi
)
298 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
299 struct ti_dac_chip
*ti_dac
= iio_priv(indio_dev
);
301 iio_device_unregister(indio_dev
);
302 mutex_destroy(&ti_dac
->lock
);
303 regulator_disable(ti_dac
->vref
);
307 static const struct of_device_id ti_dac_of_id
[] = {
308 { .compatible
= "ti,dac5311" },
309 { .compatible
= "ti,dac6311" },
310 { .compatible
= "ti,dac7311" },
313 MODULE_DEVICE_TABLE(of
, ti_dac_of_id
);
315 static const struct spi_device_id ti_dac_spi_id
[] = {
316 { "dac5311", ID_DAC5311
},
317 { "dac6311", ID_DAC6311
},
318 { "dac7311", ID_DAC7311
},
321 MODULE_DEVICE_TABLE(spi
, ti_dac_spi_id
);
323 static struct spi_driver ti_dac_driver
= {
325 .name
= "ti-dac7311",
326 .of_match_table
= ti_dac_of_id
,
328 .probe
= ti_dac_probe
,
329 .remove
= ti_dac_remove
,
330 .id_table
= ti_dac_spi_id
,
332 module_spi_driver(ti_dac_driver
);
334 MODULE_AUTHOR("Charles-Antoine Couret <charles-antoine.couret@essensium.com>");
335 MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 1-channel DAC driver");
336 MODULE_LICENSE("GPL v2");