^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* ti-dac7311.c - Texas Instruments 8/10/12-bit 1-channel DAC driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2018 CMC NV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * https://www.ti.com/lit/ds/symlink/dac7311.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) ID_DAC5311 = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) ID_DAC6311,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) ID_DAC7311,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) POWER_1KOHM_TO_GND = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) POWER_100KOHM_TO_GND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) POWER_TRI_STATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct ti_dac_spec {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u8 resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static const struct ti_dac_spec ti_dac_spec[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) [ID_DAC5311] = { .resolution = 8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) [ID_DAC6311] = { .resolution = 10 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) [ID_DAC7311] = { .resolution = 12 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * struct ti_dac_chip - TI DAC chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @lock: protects write sequences
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @vref: regulator generating Vref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @spi: SPI device to send data to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @val: cached value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @powerdown: whether the chip is powered down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @powerdown_mode: selected by the user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @resolution: resolution of the chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @buf: buffer for transfer data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct ti_dac_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct regulator *vref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct spi_device *spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) u16 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) bool powerdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u8 powerdown_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u8 resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) u8 buf[2] ____cacheline_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static u8 ti_dac_get_power(struct ti_dac_chip *ti_dac, bool powerdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (powerdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return ti_dac->powerdown_mode + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static int ti_dac_cmd(struct ti_dac_chip *ti_dac, u8 power, u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u8 shift = 14 - ti_dac->resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ti_dac->buf[0] = (val << shift) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ti_dac->buf[1] = (power << 6) | (val >> (8 - shift));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return spi_write(ti_dac->spi, ti_dac->buf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static const char * const ti_dac_powerdown_modes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) "1kohm_to_gnd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) "100kohm_to_gnd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) "three_state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int ti_dac_get_powerdown_mode(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) const struct iio_chan_spec *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return ti_dac->powerdown_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int ti_dac_set_powerdown_mode(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const struct iio_chan_spec *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ti_dac->powerdown_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static const struct iio_enum ti_dac_powerdown_mode = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .items = ti_dac_powerdown_modes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .num_items = ARRAY_SIZE(ti_dac_powerdown_modes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .get = ti_dac_get_powerdown_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .set = ti_dac_set_powerdown_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static ssize_t ti_dac_read_powerdown(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) uintptr_t private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) const struct iio_chan_spec *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return sprintf(buf, "%d\n", ti_dac->powerdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static ssize_t ti_dac_write_powerdown(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) uintptr_t private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) const struct iio_chan_spec *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) bool powerdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) u8 power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = strtobool(buf, &powerdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) power = ti_dac_get_power(ti_dac, powerdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) mutex_lock(&ti_dac->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = ti_dac_cmd(ti_dac, power, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ti_dac->powerdown = powerdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) mutex_unlock(&ti_dac->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return ret ? ret : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static const struct iio_chan_spec_ext_info ti_dac_ext_info[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .name = "powerdown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .read = ti_dac_read_powerdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .write = ti_dac_write_powerdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .shared = IIO_SHARED_BY_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE, &ti_dac_powerdown_mode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) IIO_ENUM_AVAILABLE("powerdown_mode", &ti_dac_powerdown_mode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define TI_DAC_CHANNEL(chan) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .type = IIO_VOLTAGE, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .channel = (chan), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .output = true, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .ext_info = ti_dac_ext_info, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static const struct iio_chan_spec ti_dac_channels[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) TI_DAC_CHANNEL(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int ti_dac_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int *val, int *val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *val = ti_dac->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) case IIO_CHAN_INFO_SCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = regulator_get_voltage(ti_dac->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *val = ret / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *val2 = ti_dac->resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return IIO_VAL_FRACTIONAL_LOG2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int ti_dac_write_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int val, int val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) u8 power = ti_dac_get_power(ti_dac, ti_dac->powerdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (ti_dac->val == val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (val >= (1 << ti_dac->resolution) || val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (ti_dac->powerdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) mutex_lock(&ti_dac->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ret = ti_dac_cmd(ti_dac, power, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ti_dac->val = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) mutex_unlock(&ti_dac->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int ti_dac_write_raw_get_fmt(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct iio_chan_spec const *chan, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static const struct iio_info ti_dac_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) .read_raw = ti_dac_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .write_raw = ti_dac_write_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .write_raw_get_fmt = ti_dac_write_raw_get_fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int ti_dac_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct device *dev = &spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) const struct ti_dac_spec *spec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct ti_dac_chip *ti_dac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) indio_dev = devm_iio_device_alloc(dev, sizeof(*ti_dac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!indio_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dev_err(dev, "can not allocate iio device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) spi->mode = SPI_MODE_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) spi->bits_per_word = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) spi_setup(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) indio_dev->info = &ti_dac_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) indio_dev->name = spi_get_device_id(spi)->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) indio_dev->channels = ti_dac_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) spi_set_drvdata(spi, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) ti_dac = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ti_dac->powerdown = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) ti_dac->spi = spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) spec = &ti_dac_spec[spi_get_device_id(spi)->driver_data];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) indio_dev->num_channels = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ti_dac->resolution = spec->resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ti_dac->vref = devm_regulator_get(dev, "vref");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (IS_ERR(ti_dac->vref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) dev_err(dev, "error to get regulator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return PTR_ERR(ti_dac->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) ret = regulator_enable(ti_dac->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) dev_err(dev, "can not enable regulator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) mutex_init(&ti_dac->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) ret = iio_device_register(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dev_err(dev, "fail to register iio device: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) mutex_destroy(&ti_dac->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) regulator_disable(ti_dac->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static int ti_dac_remove(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct iio_dev *indio_dev = spi_get_drvdata(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) mutex_destroy(&ti_dac->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) regulator_disable(ti_dac->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static const struct of_device_id ti_dac_of_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) { .compatible = "ti,dac5311" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) { .compatible = "ti,dac6311" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) { .compatible = "ti,dac7311" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) MODULE_DEVICE_TABLE(of, ti_dac_of_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static const struct spi_device_id ti_dac_spi_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) { "dac5311", ID_DAC5311 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) { "dac6311", ID_DAC6311 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) { "dac7311", ID_DAC7311 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) MODULE_DEVICE_TABLE(spi, ti_dac_spi_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static struct spi_driver ti_dac_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .name = "ti-dac7311",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .of_match_table = ti_dac_of_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) .probe = ti_dac_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) .remove = ti_dac_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .id_table = ti_dac_spi_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) module_spi_driver(ti_dac_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) MODULE_AUTHOR("Charles-Antoine Couret <charles-antoine.couret@essensium.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 1-channel DAC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) MODULE_LICENSE("GPL v2");