xref: /linux/drivers/iio/dac/ad5686-spi.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SPI driver for AD5686 and similar Digital to Analog Converters
4  *
5  * Copyright 2018 Analog Devices Inc.
6  */
7 
8 #include <linux/array_size.h>
9 #include <linux/bitfield.h>
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <linux/overflow.h>
13 #include <linux/spi/spi.h>
14 
15 #include <asm/byteorder.h>
16 
17 #include "ad5686.h"
18 
19 /**
20  * struct ad5686_spi_data - SPI bus specific data
21  * @msg: SPI message used for transfers
22  * @size: number of transfers currently in the message
23  * @capacity: maximum number of transfers that can be added to the message
24  * @xfers: array of SPI transfers, allocated with the provided capacity
25  */
26 struct ad5686_spi_data {
27 	struct spi_message msg;
28 	unsigned int size;
29 	unsigned int capacity;
30 	struct spi_transfer xfers[] __counted_by(capacity);
31 };
32 
33 static int ad5686_spi_write(struct ad5686_state *st,
34 			    u8 cmd, u8 addr, u16 val)
35 {
36 	struct ad5686_spi_data *bus_data = st->bus_data;
37 	struct spi_transfer *xfer;
38 
39 	if (bus_data->size >= bus_data->capacity)
40 		return -E2BIG;
41 
42 	/*
43 	 * This function stores spi transfers to a spi message to be sent over
44 	 * the bus when sync() op is called. If there are already transfers in
45 	 * the spi message, set the cs_change flag on the last transfer to
46 	 * ensure that the chip select is deasserted between transfers. If this
47 	 * is the first transfer, initialize the spi message. Later on, the
48 	 * current transfer is added to the message with spi_message_add_tail().
49 	 */
50 	if (bus_data->size)
51 		bus_data->xfers[bus_data->size - 1].cs_change = 1;
52 	else
53 		spi_message_init(&bus_data->msg);
54 
55 	xfer = &bus_data->xfers[bus_data->size];
56 	auto buf = &st->data[bus_data->size];
57 	switch (st->chip_info->regmap_type) {
58 	case AD5310_REGMAP:
59 		buf->d16 = cpu_to_be16(FIELD_PREP(AD5310_CMD_MSK, cmd) |
60 				       FIELD_PREP(AD5310_DATA_MSK, val));
61 		*xfer = (struct spi_transfer) {
62 			.tx_buf = &buf->d16,
63 			.len = sizeof(buf->d16),
64 		};
65 		break;
66 	case AD5683_REGMAP:
67 		buf->d32 = cpu_to_be32(FIELD_PREP(AD5686_CMD_MSK, cmd) |
68 				       FIELD_PREP(AD5683_DATA_MSK, val));
69 		*xfer = (struct spi_transfer) {
70 			.tx_buf = &buf->d8[1],
71 			.len = sizeof(buf->d8) - 1,
72 		};
73 		break;
74 	case AD5686_REGMAP:
75 		buf->d32 = cpu_to_be32(FIELD_PREP(AD5686_CMD_MSK, cmd) |
76 				       FIELD_PREP(AD5686_ADDR_MSK, addr) |
77 				       FIELD_PREP(AD5686_DATA_MSK, val));
78 		*xfer = (struct spi_transfer) {
79 			.tx_buf = &buf->d8[1],
80 			.len = sizeof(buf->d8) - 1,
81 		};
82 		break;
83 	default:
84 		return -EINVAL;
85 	}
86 
87 	spi_message_add_tail(xfer, &bus_data->msg);
88 	bus_data->size++;
89 
90 	return 0;
91 }
92 
93 static int ad5686_spi_sync(struct ad5686_state *st)
94 {
95 	struct spi_device *spi = to_spi_device(st->dev);
96 	struct ad5686_spi_data *bus_data = st->bus_data;
97 
98 	bus_data->size = 0; /* always reset, even on sync failure */
99 	return spi_sync(spi, &bus_data->msg);
100 }
101 
102 static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
103 {
104 	struct spi_device *spi = to_spi_device(st->dev);
105 	struct ad5686_spi_data *bus_data = st->bus_data;
106 	struct spi_transfer *xfer = &bus_data->xfers[0];
107 	u8 cmd = 0;
108 	int ret;
109 
110 	switch (st->chip_info->regmap_type) {
111 	case AD5310_REGMAP:
112 		return -ENOTSUPP;
113 	case AD5683_REGMAP:
114 		cmd = AD5686_CMD_READBACK_ENABLE_V2;
115 		break;
116 	case AD5686_REGMAP:
117 		cmd = AD5686_CMD_READBACK_ENABLE;
118 		break;
119 	default:
120 		return -EINVAL;
121 	}
122 
123 	st->data[0].d32 = cpu_to_be32(FIELD_PREP(AD5686_CMD_MSK, cmd) |
124 				      FIELD_PREP(AD5686_ADDR_MSK, addr));
125 	st->data[1].d32 = cpu_to_be32(FIELD_PREP(AD5686_CMD_MSK, AD5686_CMD_NOOP));
126 
127 	xfer[0] = (struct spi_transfer) {
128 		.tx_buf = &st->data[0].d8[1],
129 		.len = sizeof(st->data[0].d8) - 1,
130 		.cs_change = 1,
131 	};
132 	xfer[1] = (struct spi_transfer) {
133 		.tx_buf = &st->data[1].d8[1],
134 		.rx_buf = &st->data[2].d8[1],
135 		.len = sizeof(st->data[1].d8) - 1,
136 	};
137 
138 	spi_message_init_with_transfers(&bus_data->msg, xfer, 2);
139 
140 	ret = spi_sync(spi, &bus_data->msg);
141 	if (ret)
142 		return ret;
143 
144 	return be32_to_cpu(st->data[2].d32);
145 }
146 
147 static const struct ad5686_bus_ops ad5686_spi_ops = {
148 	.write = ad5686_spi_write,
149 	.read = ad5686_spi_read,
150 	.sync = ad5686_spi_sync,
151 };
152 
153 static int ad5686_spi_probe(struct spi_device *spi)
154 {
155 	const struct ad5686_chip_info *info;
156 	struct ad5686_spi_data *bus_data;
157 	struct device *dev = &spi->dev;
158 	unsigned int capacity;
159 
160 	info = spi_get_device_match_data(spi);
161 	if (!info)
162 		return -ENODATA;
163 
164 	/* read operation requires at least 2 transfers */
165 	capacity = max(info->num_channels, 2);
166 	bus_data = devm_kzalloc(dev, struct_size(bus_data, xfers, capacity),
167 				GFP_KERNEL);
168 	if (!bus_data)
169 		return -ENOMEM;
170 
171 	bus_data->capacity = capacity;
172 
173 	return ad5686_probe(dev, info, spi->modalias, &ad5686_spi_ops, bus_data);
174 }
175 
176 static const struct spi_device_id ad5686_spi_id[] = {
177 	{ .name = "ad5310r", .driver_data = (kernel_ulong_t)&ad5310r_chip_info },
178 	{ .name = "ad5313r", .driver_data = (kernel_ulong_t)&ad5338r_chip_info },
179 	{ .name = "ad5317r", .driver_data = (kernel_ulong_t)&ad5317r_chip_info },
180 	{ .name = "ad5672r", .driver_data = (kernel_ulong_t)&ad5672r_chip_info },
181 	{ .name = "ad5674",  .driver_data = (kernel_ulong_t)&ad5674_chip_info },
182 	{ .name = "ad5674r", .driver_data = (kernel_ulong_t)&ad5674r_chip_info },
183 	{ .name = "ad5676",  .driver_data = (kernel_ulong_t)&ad5676_chip_info },
184 	{ .name = "ad5676r", .driver_data = (kernel_ulong_t)&ad5676r_chip_info },
185 	{ .name = "ad5679",  .driver_data = (kernel_ulong_t)&ad5679_chip_info },
186 	{ .name = "ad5679r", .driver_data = (kernel_ulong_t)&ad5679r_chip_info },
187 	{ .name = "ad5681r", .driver_data = (kernel_ulong_t)&ad5681r_chip_info },
188 	{ .name = "ad5682r", .driver_data = (kernel_ulong_t)&ad5682r_chip_info },
189 	{ .name = "ad5683",  .driver_data = (kernel_ulong_t)&ad5683_chip_info },
190 	{ .name = "ad5683r", .driver_data = (kernel_ulong_t)&ad5683r_chip_info },
191 	{ .name = "ad5684",  .driver_data = (kernel_ulong_t)&ad5684_chip_info },
192 	{ .name = "ad5684r", .driver_data = (kernel_ulong_t)&ad5684r_chip_info },
193 	{ .name = "ad5685",  .driver_data = (kernel_ulong_t)&ad5685r_chip_info }, /* nonexistent */
194 	{ .name = "ad5685r", .driver_data = (kernel_ulong_t)&ad5685r_chip_info },
195 	{ .name = "ad5686",  .driver_data = (kernel_ulong_t)&ad5686_chip_info },
196 	{ .name = "ad5686r", .driver_data = (kernel_ulong_t)&ad5686r_chip_info },
197 	{ .name = "ad5687",  .driver_data = (kernel_ulong_t)&ad5687_chip_info },
198 	{ .name = "ad5687r", .driver_data = (kernel_ulong_t)&ad5687r_chip_info },
199 	{ .name = "ad5689",  .driver_data = (kernel_ulong_t)&ad5689_chip_info },
200 	{ .name = "ad5689r", .driver_data = (kernel_ulong_t)&ad5689r_chip_info },
201 	{ }
202 };
203 MODULE_DEVICE_TABLE(spi, ad5686_spi_id);
204 
205 static const struct of_device_id ad5686_of_match[] = {
206 	{ .compatible = "adi,ad5310r", .data = &ad5310r_chip_info },
207 	{ .compatible = "adi,ad5313r", .data = &ad5338r_chip_info },
208 	{ .compatible = "adi,ad5317r", .data = &ad5317r_chip_info },
209 	{ .compatible = "adi,ad5672r", .data = &ad5672r_chip_info },
210 	{ .compatible = "adi,ad5674",  .data = &ad5674_chip_info },
211 	{ .compatible = "adi,ad5674r", .data = &ad5674r_chip_info },
212 	{ .compatible = "adi,ad5676",  .data = &ad5676_chip_info },
213 	{ .compatible = "adi,ad5676r", .data = &ad5676r_chip_info },
214 	{ .compatible = "adi,ad5679",  .data = &ad5679_chip_info },
215 	{ .compatible = "adi,ad5679r", .data = &ad5679r_chip_info },
216 	{ .compatible = "adi,ad5681r", .data = &ad5681r_chip_info },
217 	{ .compatible = "adi,ad5682r", .data = &ad5682r_chip_info },
218 	{ .compatible = "adi,ad5683",  .data = &ad5683_chip_info },
219 	{ .compatible = "adi,ad5683r", .data = &ad5683r_chip_info },
220 	{ .compatible = "adi,ad5684",  .data = &ad5684_chip_info },
221 	{ .compatible = "adi,ad5684r", .data = &ad5684r_chip_info },
222 	{ .compatible = "adi,ad5685r", .data = &ad5685r_chip_info },
223 	{ .compatible = "adi,ad5686",  .data = &ad5686_chip_info },
224 	{ .compatible = "adi,ad5686r", .data = &ad5686r_chip_info },
225 	{ .compatible = "adi,ad5687",  .data = &ad5687_chip_info },
226 	{ .compatible = "adi,ad5687r", .data = &ad5687r_chip_info },
227 	{ .compatible = "adi,ad5689",  .data = &ad5689_chip_info },
228 	{ .compatible = "adi,ad5689r", .data = &ad5689r_chip_info },
229 	{ }
230 };
231 MODULE_DEVICE_TABLE(of, ad5686_of_match);
232 
233 static struct spi_driver ad5686_spi_driver = {
234 	.driver = {
235 		.name = "ad5686",
236 		.of_match_table = ad5686_of_match,
237 	},
238 	.probe = ad5686_spi_probe,
239 	.id_table = ad5686_spi_id,
240 };
241 
242 module_spi_driver(ad5686_spi_driver);
243 
244 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
245 MODULE_DESCRIPTION("Analog Devices AD5686 and similar multi-channel DACs");
246 MODULE_LICENSE("GPL v2");
247 MODULE_IMPORT_NS("IIO_AD5686");
248