xref: /linux/drivers/iio/dac/ti-dac082s085.c (revision 3593cd53962fa17e4eaaae8faa5c8f62ec7bbd5e)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
261011264SLukas Wunner /*
361011264SLukas Wunner  * ti-dac082s085.c - Texas Instruments 8/10/12-bit 2/4-channel DAC driver
461011264SLukas Wunner  *
561011264SLukas Wunner  * Copyright (C) 2017 KUNBUS GmbH
661011264SLukas Wunner  *
7*3593cd53SAlexander A. Klimov  * https://www.ti.com/lit/ds/symlink/dac082s085.pdf
8*3593cd53SAlexander A. Klimov  * https://www.ti.com/lit/ds/symlink/dac102s085.pdf
9*3593cd53SAlexander A. Klimov  * https://www.ti.com/lit/ds/symlink/dac122s085.pdf
10*3593cd53SAlexander A. Klimov  * https://www.ti.com/lit/ds/symlink/dac084s085.pdf
11*3593cd53SAlexander A. Klimov  * https://www.ti.com/lit/ds/symlink/dac104s085.pdf
12*3593cd53SAlexander A. Klimov  * https://www.ti.com/lit/ds/symlink/dac124s085.pdf
1361011264SLukas Wunner  */
1461011264SLukas Wunner 
1561011264SLukas Wunner #include <linux/iio/iio.h>
1661011264SLukas Wunner #include <linux/module.h>
1761011264SLukas Wunner #include <linux/regulator/consumer.h>
1861011264SLukas Wunner #include <linux/spi/spi.h>
1961011264SLukas Wunner 
20f98677cfSLukas Wunner enum { dual_8bit, dual_10bit, dual_12bit, quad_8bit, quad_10bit, quad_12bit };
21f98677cfSLukas Wunner 
22f98677cfSLukas Wunner struct ti_dac_spec {
23f98677cfSLukas Wunner 	u8 num_channels;
24f98677cfSLukas Wunner 	u8 resolution;
25f98677cfSLukas Wunner };
26f98677cfSLukas Wunner 
27f98677cfSLukas Wunner static const struct ti_dac_spec ti_dac_spec[] = {
28f98677cfSLukas Wunner 	[dual_8bit]  = { .num_channels = 2, .resolution = 8  },
29f98677cfSLukas Wunner 	[dual_10bit] = { .num_channels = 2, .resolution = 10 },
30f98677cfSLukas Wunner 	[dual_12bit] = { .num_channels = 2, .resolution = 12 },
31f98677cfSLukas Wunner 	[quad_8bit]  = { .num_channels = 4, .resolution = 8  },
32f98677cfSLukas Wunner 	[quad_10bit] = { .num_channels = 4, .resolution = 10 },
33f98677cfSLukas Wunner 	[quad_12bit] = { .num_channels = 4, .resolution = 12 },
34f98677cfSLukas Wunner };
35f98677cfSLukas Wunner 
3661011264SLukas Wunner /**
3761011264SLukas Wunner  * struct ti_dac_chip - TI DAC chip
3861011264SLukas Wunner  * @lock: protects write sequences
3961011264SLukas Wunner  * @vref: regulator generating Vref
4061011264SLukas Wunner  * @mesg: SPI message to perform a write
4161011264SLukas Wunner  * @xfer: SPI transfer used by @mesg
4261011264SLukas Wunner  * @val: cached value of each output
4361011264SLukas Wunner  * @powerdown: whether the chip is powered down
4461011264SLukas Wunner  * @powerdown_mode: selected by the user
4561011264SLukas Wunner  * @resolution: resolution of the chip
4661011264SLukas Wunner  * @buf: buffer for @xfer
4761011264SLukas Wunner  */
4861011264SLukas Wunner struct ti_dac_chip {
4961011264SLukas Wunner 	struct mutex lock;
5061011264SLukas Wunner 	struct regulator *vref;
5161011264SLukas Wunner 	struct spi_message mesg;
5261011264SLukas Wunner 	struct spi_transfer xfer;
5361011264SLukas Wunner 	u16 val[4];
5461011264SLukas Wunner 	bool powerdown;
5561011264SLukas Wunner 	u8 powerdown_mode;
5661011264SLukas Wunner 	u8 resolution;
5761011264SLukas Wunner 	u8 buf[2] ____cacheline_aligned;
5861011264SLukas Wunner };
5961011264SLukas Wunner 
6061011264SLukas Wunner #define WRITE_NOT_UPDATE(chan)	(0x00 | (chan) << 6)
6161011264SLukas Wunner #define WRITE_AND_UPDATE(chan)	(0x10 | (chan) << 6)
6261011264SLukas Wunner #define WRITE_ALL_UPDATE	 0x20
6361011264SLukas Wunner #define POWERDOWN(mode) 	(0x30 | ((mode) + 1) << 6)
6461011264SLukas Wunner 
6561011264SLukas Wunner static int ti_dac_cmd(struct ti_dac_chip *ti_dac, u8 cmd, u16 val)
6661011264SLukas Wunner {
6761011264SLukas Wunner 	u8 shift = 12 - ti_dac->resolution;
6861011264SLukas Wunner 
6961011264SLukas Wunner 	ti_dac->buf[0] = cmd | (val >> (8 - shift));
7061011264SLukas Wunner 	ti_dac->buf[1] = (val << shift) & 0xff;
7161011264SLukas Wunner 	return spi_sync(ti_dac->mesg.spi, &ti_dac->mesg);
7261011264SLukas Wunner }
7361011264SLukas Wunner 
7461011264SLukas Wunner static const char * const ti_dac_powerdown_modes[] = {
7561011264SLukas Wunner 	"2.5kohm_to_gnd", "100kohm_to_gnd", "three_state",
7661011264SLukas Wunner };
7761011264SLukas Wunner 
7861011264SLukas Wunner static int ti_dac_get_powerdown_mode(struct iio_dev *indio_dev,
7961011264SLukas Wunner 				     const struct iio_chan_spec *chan)
8061011264SLukas Wunner {
8161011264SLukas Wunner 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
8261011264SLukas Wunner 
8361011264SLukas Wunner 	return ti_dac->powerdown_mode;
8461011264SLukas Wunner }
8561011264SLukas Wunner 
8661011264SLukas Wunner static int ti_dac_set_powerdown_mode(struct iio_dev *indio_dev,
8761011264SLukas Wunner 				     const struct iio_chan_spec *chan,
8861011264SLukas Wunner 				     unsigned int mode)
8961011264SLukas Wunner {
9061011264SLukas Wunner 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
9161011264SLukas Wunner 	int ret = 0;
9261011264SLukas Wunner 
9361011264SLukas Wunner 	if (ti_dac->powerdown_mode == mode)
9461011264SLukas Wunner 		return 0;
9561011264SLukas Wunner 
9661011264SLukas Wunner 	mutex_lock(&ti_dac->lock);
9761011264SLukas Wunner 	if (ti_dac->powerdown) {
9861011264SLukas Wunner 		ret = ti_dac_cmd(ti_dac, POWERDOWN(mode), 0);
9961011264SLukas Wunner 		if (ret)
10061011264SLukas Wunner 			goto out;
10161011264SLukas Wunner 	}
10261011264SLukas Wunner 	ti_dac->powerdown_mode = mode;
10361011264SLukas Wunner 
10461011264SLukas Wunner out:
10561011264SLukas Wunner 	mutex_unlock(&ti_dac->lock);
10661011264SLukas Wunner 	return ret;
10761011264SLukas Wunner }
10861011264SLukas Wunner 
10961011264SLukas Wunner static const struct iio_enum ti_dac_powerdown_mode = {
11061011264SLukas Wunner 	.items = ti_dac_powerdown_modes,
11161011264SLukas Wunner 	.num_items = ARRAY_SIZE(ti_dac_powerdown_modes),
11261011264SLukas Wunner 	.get = ti_dac_get_powerdown_mode,
11361011264SLukas Wunner 	.set = ti_dac_set_powerdown_mode,
11461011264SLukas Wunner };
11561011264SLukas Wunner 
11661011264SLukas Wunner static ssize_t ti_dac_read_powerdown(struct iio_dev *indio_dev,
11761011264SLukas Wunner 				     uintptr_t private,
11861011264SLukas Wunner 				     const struct iio_chan_spec *chan,
11961011264SLukas Wunner 				     char *buf)
12061011264SLukas Wunner {
12161011264SLukas Wunner 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
12261011264SLukas Wunner 
12361011264SLukas Wunner 	return sprintf(buf, "%d\n", ti_dac->powerdown);
12461011264SLukas Wunner }
12561011264SLukas Wunner 
12661011264SLukas Wunner static ssize_t ti_dac_write_powerdown(struct iio_dev *indio_dev,
12761011264SLukas Wunner 				      uintptr_t private,
12861011264SLukas Wunner 				      const struct iio_chan_spec *chan,
12961011264SLukas Wunner 				      const char *buf, size_t len)
13061011264SLukas Wunner {
13161011264SLukas Wunner 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
13261011264SLukas Wunner 	bool powerdown;
13361011264SLukas Wunner 	int ret;
13461011264SLukas Wunner 
13561011264SLukas Wunner 	ret = strtobool(buf, &powerdown);
13661011264SLukas Wunner 	if (ret)
13761011264SLukas Wunner 		return ret;
13861011264SLukas Wunner 
13961011264SLukas Wunner 	if (ti_dac->powerdown == powerdown)
14061011264SLukas Wunner 		return len;
14161011264SLukas Wunner 
14261011264SLukas Wunner 	mutex_lock(&ti_dac->lock);
14361011264SLukas Wunner 	if (powerdown)
14461011264SLukas Wunner 		ret = ti_dac_cmd(ti_dac, POWERDOWN(ti_dac->powerdown_mode), 0);
14561011264SLukas Wunner 	else
14661011264SLukas Wunner 		ret = ti_dac_cmd(ti_dac, WRITE_AND_UPDATE(0), ti_dac->val[0]);
14761011264SLukas Wunner 	if (!ret)
14861011264SLukas Wunner 		ti_dac->powerdown = powerdown;
14961011264SLukas Wunner 	mutex_unlock(&ti_dac->lock);
15061011264SLukas Wunner 
15161011264SLukas Wunner 	return ret ? ret : len;
15261011264SLukas Wunner }
15361011264SLukas Wunner 
15461011264SLukas Wunner static const struct iio_chan_spec_ext_info ti_dac_ext_info[] = {
15561011264SLukas Wunner 	{
15661011264SLukas Wunner 		.name	   = "powerdown",
15761011264SLukas Wunner 		.read	   = ti_dac_read_powerdown,
15861011264SLukas Wunner 		.write	   = ti_dac_write_powerdown,
15961011264SLukas Wunner 		.shared	   = IIO_SHARED_BY_TYPE,
16061011264SLukas Wunner 	},
16161011264SLukas Wunner 	IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE, &ti_dac_powerdown_mode),
16261011264SLukas Wunner 	IIO_ENUM_AVAILABLE("powerdown_mode", &ti_dac_powerdown_mode),
16361011264SLukas Wunner 	{ },
16461011264SLukas Wunner };
16561011264SLukas Wunner 
16661011264SLukas Wunner #define TI_DAC_CHANNEL(chan) {					\
16761011264SLukas Wunner 	.type = IIO_VOLTAGE,					\
16861011264SLukas Wunner 	.channel = (chan),					\
16961011264SLukas Wunner 	.address = (chan),					\
17061011264SLukas Wunner 	.indexed = true,					\
17161011264SLukas Wunner 	.output = true,						\
17261011264SLukas Wunner 	.datasheet_name = (const char[]){ 'A' + (chan), 0 },	\
17361011264SLukas Wunner 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
17461011264SLukas Wunner 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
17561011264SLukas Wunner 	.ext_info = ti_dac_ext_info,				\
17661011264SLukas Wunner }
17761011264SLukas Wunner 
17861011264SLukas Wunner static const struct iio_chan_spec ti_dac_channels[] = {
17961011264SLukas Wunner 	TI_DAC_CHANNEL(0),
18061011264SLukas Wunner 	TI_DAC_CHANNEL(1),
18161011264SLukas Wunner 	TI_DAC_CHANNEL(2),
18261011264SLukas Wunner 	TI_DAC_CHANNEL(3),
18361011264SLukas Wunner };
18461011264SLukas Wunner 
18561011264SLukas Wunner static int ti_dac_read_raw(struct iio_dev *indio_dev,
18661011264SLukas Wunner 			   struct iio_chan_spec const *chan,
18761011264SLukas Wunner 			   int *val, int *val2, long mask)
18861011264SLukas Wunner {
18961011264SLukas Wunner 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
19061011264SLukas Wunner 	int ret;
19161011264SLukas Wunner 
19261011264SLukas Wunner 	switch (mask) {
19361011264SLukas Wunner 	case IIO_CHAN_INFO_RAW:
19461011264SLukas Wunner 		*val = ti_dac->val[chan->channel];
19561011264SLukas Wunner 		ret = IIO_VAL_INT;
19661011264SLukas Wunner 		break;
19761011264SLukas Wunner 
19861011264SLukas Wunner 	case IIO_CHAN_INFO_SCALE:
19961011264SLukas Wunner 		ret = regulator_get_voltage(ti_dac->vref);
20061011264SLukas Wunner 		if (ret < 0)
20161011264SLukas Wunner 			return ret;
20261011264SLukas Wunner 
20361011264SLukas Wunner 		*val = ret / 1000;
20461011264SLukas Wunner 		*val2 = ti_dac->resolution;
20561011264SLukas Wunner 		ret = IIO_VAL_FRACTIONAL_LOG2;
20661011264SLukas Wunner 		break;
20761011264SLukas Wunner 
20861011264SLukas Wunner 	default:
20961011264SLukas Wunner 		ret = -EINVAL;
21061011264SLukas Wunner 	}
21161011264SLukas Wunner 
21261011264SLukas Wunner 	return ret;
21361011264SLukas Wunner }
21461011264SLukas Wunner 
21561011264SLukas Wunner static int ti_dac_write_raw(struct iio_dev *indio_dev,
21661011264SLukas Wunner 			    struct iio_chan_spec const *chan,
21761011264SLukas Wunner 			    int val, int val2, long mask)
21861011264SLukas Wunner {
21961011264SLukas Wunner 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
22061011264SLukas Wunner 	int ret;
22161011264SLukas Wunner 
22261011264SLukas Wunner 	switch (mask) {
22361011264SLukas Wunner 	case IIO_CHAN_INFO_RAW:
22461011264SLukas Wunner 		if (ti_dac->val[chan->channel] == val)
22561011264SLukas Wunner 			return 0;
22661011264SLukas Wunner 
22761011264SLukas Wunner 		if (val >= (1 << ti_dac->resolution) || val < 0)
22861011264SLukas Wunner 			return -EINVAL;
22961011264SLukas Wunner 
23061011264SLukas Wunner 		if (ti_dac->powerdown)
23161011264SLukas Wunner 			return -EBUSY;
23261011264SLukas Wunner 
23361011264SLukas Wunner 		mutex_lock(&ti_dac->lock);
23461011264SLukas Wunner 		ret = ti_dac_cmd(ti_dac, WRITE_AND_UPDATE(chan->channel), val);
23561011264SLukas Wunner 		if (!ret)
23661011264SLukas Wunner 			ti_dac->val[chan->channel] = val;
23761011264SLukas Wunner 		mutex_unlock(&ti_dac->lock);
23861011264SLukas Wunner 		break;
23961011264SLukas Wunner 
24061011264SLukas Wunner 	default:
24161011264SLukas Wunner 		ret = -EINVAL;
24261011264SLukas Wunner 	}
24361011264SLukas Wunner 
24461011264SLukas Wunner 	return ret;
24561011264SLukas Wunner }
24661011264SLukas Wunner 
24761011264SLukas Wunner static int ti_dac_write_raw_get_fmt(struct iio_dev *indio_dev,
24861011264SLukas Wunner 				    struct iio_chan_spec const *chan, long mask)
24961011264SLukas Wunner {
25061011264SLukas Wunner 	return IIO_VAL_INT;
25161011264SLukas Wunner }
25261011264SLukas Wunner 
25361011264SLukas Wunner static const struct iio_info ti_dac_info = {
25461011264SLukas Wunner 	.read_raw	   = ti_dac_read_raw,
25561011264SLukas Wunner 	.write_raw	   = ti_dac_write_raw,
25661011264SLukas Wunner 	.write_raw_get_fmt = ti_dac_write_raw_get_fmt,
25761011264SLukas Wunner };
25861011264SLukas Wunner 
25961011264SLukas Wunner static int ti_dac_probe(struct spi_device *spi)
26061011264SLukas Wunner {
26161011264SLukas Wunner 	struct device *dev = &spi->dev;
262f98677cfSLukas Wunner 	const struct ti_dac_spec *spec;
26361011264SLukas Wunner 	struct ti_dac_chip *ti_dac;
26461011264SLukas Wunner 	struct iio_dev *indio_dev;
26561011264SLukas Wunner 	int ret;
26661011264SLukas Wunner 
26761011264SLukas Wunner 	indio_dev = devm_iio_device_alloc(dev, sizeof(*ti_dac));
26861011264SLukas Wunner 	if (!indio_dev)
26961011264SLukas Wunner 		return -ENOMEM;
27061011264SLukas Wunner 
27161011264SLukas Wunner 	indio_dev->info = &ti_dac_info;
27261011264SLukas Wunner 	indio_dev->name = spi->modalias;
27361011264SLukas Wunner 	indio_dev->modes = INDIO_DIRECT_MODE;
27461011264SLukas Wunner 	indio_dev->channels = ti_dac_channels;
27561011264SLukas Wunner 	spi_set_drvdata(spi, indio_dev);
27661011264SLukas Wunner 
27761011264SLukas Wunner 	ti_dac = iio_priv(indio_dev);
27861011264SLukas Wunner 	ti_dac->xfer.tx_buf = &ti_dac->buf;
27961011264SLukas Wunner 	ti_dac->xfer.len = sizeof(ti_dac->buf);
28061011264SLukas Wunner 	spi_message_init_with_transfers(&ti_dac->mesg, &ti_dac->xfer, 1);
28161011264SLukas Wunner 	ti_dac->mesg.spi = spi;
28261011264SLukas Wunner 
283f98677cfSLukas Wunner 	spec = &ti_dac_spec[spi_get_device_id(spi)->driver_data];
284f98677cfSLukas Wunner 	indio_dev->num_channels = spec->num_channels;
285f98677cfSLukas Wunner 	ti_dac->resolution = spec->resolution;
28661011264SLukas Wunner 
28761011264SLukas Wunner 	ti_dac->vref = devm_regulator_get(dev, "vref");
28861011264SLukas Wunner 	if (IS_ERR(ti_dac->vref))
28961011264SLukas Wunner 		return PTR_ERR(ti_dac->vref);
29061011264SLukas Wunner 
29161011264SLukas Wunner 	ret = regulator_enable(ti_dac->vref);
29261011264SLukas Wunner 	if (ret < 0)
29361011264SLukas Wunner 		return ret;
29461011264SLukas Wunner 
29561011264SLukas Wunner 	mutex_init(&ti_dac->lock);
29661011264SLukas Wunner 
29761011264SLukas Wunner 	ret = ti_dac_cmd(ti_dac, WRITE_ALL_UPDATE, 0);
29861011264SLukas Wunner 	if (ret) {
29961011264SLukas Wunner 		dev_err(dev, "failed to initialize outputs to 0\n");
30061011264SLukas Wunner 		goto err;
30161011264SLukas Wunner 	}
30261011264SLukas Wunner 
30361011264SLukas Wunner 	ret = iio_device_register(indio_dev);
30461011264SLukas Wunner 	if (ret)
30561011264SLukas Wunner 		goto err;
30661011264SLukas Wunner 
30761011264SLukas Wunner 	return 0;
30861011264SLukas Wunner 
30961011264SLukas Wunner err:
31061011264SLukas Wunner 	mutex_destroy(&ti_dac->lock);
31161011264SLukas Wunner 	regulator_disable(ti_dac->vref);
31261011264SLukas Wunner 	return ret;
31361011264SLukas Wunner }
31461011264SLukas Wunner 
31561011264SLukas Wunner static int ti_dac_remove(struct spi_device *spi)
31661011264SLukas Wunner {
31761011264SLukas Wunner 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
31861011264SLukas Wunner 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
31961011264SLukas Wunner 
32061011264SLukas Wunner 	iio_device_unregister(indio_dev);
32161011264SLukas Wunner 	mutex_destroy(&ti_dac->lock);
32261011264SLukas Wunner 	regulator_disable(ti_dac->vref);
32361011264SLukas Wunner 
32461011264SLukas Wunner 	return 0;
32561011264SLukas Wunner }
32661011264SLukas Wunner 
32761011264SLukas Wunner #ifdef CONFIG_OF
32861011264SLukas Wunner static const struct of_device_id ti_dac_of_id[] = {
32961011264SLukas Wunner 	{ .compatible = "ti,dac082s085" },
33061011264SLukas Wunner 	{ .compatible = "ti,dac102s085" },
33161011264SLukas Wunner 	{ .compatible = "ti,dac122s085" },
33261011264SLukas Wunner 	{ .compatible = "ti,dac084s085" },
33361011264SLukas Wunner 	{ .compatible = "ti,dac104s085" },
33461011264SLukas Wunner 	{ .compatible = "ti,dac124s085" },
33561011264SLukas Wunner 	{ }
33661011264SLukas Wunner };
33761011264SLukas Wunner MODULE_DEVICE_TABLE(of, ti_dac_of_id);
33861011264SLukas Wunner #endif
33961011264SLukas Wunner 
34061011264SLukas Wunner static const struct spi_device_id ti_dac_spi_id[] = {
341f98677cfSLukas Wunner 	{ "dac082s085", dual_8bit  },
342f98677cfSLukas Wunner 	{ "dac102s085", dual_10bit },
343f98677cfSLukas Wunner 	{ "dac122s085", dual_12bit },
344f98677cfSLukas Wunner 	{ "dac084s085", quad_8bit  },
345f98677cfSLukas Wunner 	{ "dac104s085", quad_10bit },
346f98677cfSLukas Wunner 	{ "dac124s085", quad_12bit },
34761011264SLukas Wunner 	{ }
34861011264SLukas Wunner };
34961011264SLukas Wunner MODULE_DEVICE_TABLE(spi, ti_dac_spi_id);
35061011264SLukas Wunner 
35161011264SLukas Wunner static struct spi_driver ti_dac_driver = {
35261011264SLukas Wunner 	.driver = {
35361011264SLukas Wunner 		.name		= "ti-dac082s085",
35461011264SLukas Wunner 		.of_match_table	= of_match_ptr(ti_dac_of_id),
35561011264SLukas Wunner 	},
35661011264SLukas Wunner 	.probe	  = ti_dac_probe,
35761011264SLukas Wunner 	.remove   = ti_dac_remove,
35861011264SLukas Wunner 	.id_table = ti_dac_spi_id,
35961011264SLukas Wunner };
36061011264SLukas Wunner module_spi_driver(ti_dac_driver);
36161011264SLukas Wunner 
36261011264SLukas Wunner MODULE_AUTHOR("Lukas Wunner <lukas@wunner.de>");
36361011264SLukas Wunner MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 2/4-channel DAC driver");
36461011264SLukas Wunner MODULE_LICENSE("GPL v2");
365