summaryrefslogtreecommitdiff
path: root/drivers/iio/dac/ad5686-spi.c
blob: 69cd9753f3399d8e3f9ffc65fda701726b07ec85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// SPDX-License-Identifier: GPL-2.0
/*
 * SPI driver for AD5686 and similar Digital to Analog Converters
 *
 * Copyright 2018 Analog Devices Inc.
 */

#include <linux/array_size.h>
#include <linux/bitfield.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/overflow.h>
#include <linux/spi/spi.h>

#include <asm/byteorder.h>

#include "ad5686.h"

/**
 * struct ad5686_spi_data - SPI bus specific data
 * @msg: SPI message used for transfers
 * @size: number of transfers currently in the message
 * @capacity: maximum number of transfers that can be added to the message
 * @xfers: array of SPI transfers, allocated with the provided capacity
 */
struct ad5686_spi_data {
	struct spi_message msg;
	unsigned int size;
	unsigned int capacity;
	struct spi_transfer xfers[] __counted_by(capacity);
};

static int ad5686_spi_write(struct ad5686_state *st,
			    u8 cmd, u8 addr, u16 val)
{
	struct ad5686_spi_data *bus_data = st->bus_data;
	struct spi_transfer *xfer;

	if (bus_data->size >= bus_data->capacity)
		return -E2BIG;

	/*
	 * This function stores spi transfers to a spi message to be sent over
	 * the bus when sync() op is called. If there are already transfers in
	 * the spi message, set the cs_change flag on the last transfer to
	 * ensure that the chip select is deasserted between transfers. If this
	 * is the first transfer, initialize the spi message. Later on, the
	 * current transfer is added to the message with spi_message_add_tail().
	 */
	if (bus_data->size)
		bus_data->xfers[bus_data->size - 1].cs_change = 1;
	else
		spi_message_init(&bus_data->msg);

	xfer = &bus_data->xfers[bus_data->size];
	auto buf = &st->data[bus_data->size];
	switch (st->chip_info->regmap_type) {
	case AD5310_REGMAP:
		buf->d16 = cpu_to_be16(FIELD_PREP(AD5310_CMD_MSK, cmd) |
				       FIELD_PREP(AD5310_DATA_MSK, val));
		*xfer = (struct spi_transfer) {
			.tx_buf = &buf->d16,
			.len = sizeof(buf->d16),
		};
		break;
	case AD5683_REGMAP:
		buf->d32 = cpu_to_be32(FIELD_PREP(AD5686_CMD_MSK, cmd) |
				       FIELD_PREP(AD5683_DATA_MSK, val));
		*xfer = (struct spi_transfer) {
			.tx_buf = &buf->d8[1],
			.len = sizeof(buf->d8) - 1,
		};
		break;
	case AD5686_REGMAP:
		buf->d32 = cpu_to_be32(FIELD_PREP(AD5686_CMD_MSK, cmd) |
				       FIELD_PREP(AD5686_ADDR_MSK, addr) |
				       FIELD_PREP(AD5686_DATA_MSK, val));
		*xfer = (struct spi_transfer) {
			.tx_buf = &buf->d8[1],
			.len = sizeof(buf->d8) - 1,
		};
		break;
	default:
		return -EINVAL;
	}

	spi_message_add_tail(xfer, &bus_data->msg);
	bus_data->size++;

	return 0;
}

static int ad5686_spi_sync(struct ad5686_state *st)
{
	struct spi_device *spi = to_spi_device(st->dev);
	struct ad5686_spi_data *bus_data = st->bus_data;

	bus_data->size = 0; /* always reset, even on sync failure */
	return spi_sync(spi, &bus_data->msg);
}

static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
{
	struct spi_device *spi = to_spi_device(st->dev);
	struct ad5686_spi_data *bus_data = st->bus_data;
	struct spi_transfer *xfer = &bus_data->xfers[0];
	u8 cmd = 0;
	int ret;

	switch (st->chip_info->regmap_type) {
	case AD5310_REGMAP:
		return -ENOTSUPP;
	case AD5683_REGMAP:
		cmd = AD5686_CMD_READBACK_ENABLE_V2;
		break;
	case AD5686_REGMAP:
		cmd = AD5686_CMD_READBACK_ENABLE;
		break;
	default:
		return -EINVAL;
	}

	st->data[0].d32 = cpu_to_be32(FIELD_PREP(AD5686_CMD_MSK, cmd) |
				      FIELD_PREP(AD5686_ADDR_MSK, addr));
	st->data[1].d32 = cpu_to_be32(FIELD_PREP(AD5686_CMD_MSK, AD5686_CMD_NOOP));

	xfer[0] = (struct spi_transfer) {
		.tx_buf = &st->data[0].d8[1],
		.len = sizeof(st->data[0].d8) - 1,
		.cs_change = 1,
	};
	xfer[1] = (struct spi_transfer) {
		.tx_buf = &st->data[1].d8[1],
		.rx_buf = &st->data[2].d8[1],
		.len = sizeof(st->data[1].d8) - 1,
	};

	spi_message_init_with_transfers(&bus_data->msg, xfer, 2);

	ret = spi_sync(spi, &bus_data->msg);
	if (ret)
		return ret;

	return be32_to_cpu(st->data[2].d32);
}

static const struct ad5686_bus_ops ad5686_spi_ops = {
	.write = ad5686_spi_write,
	.read = ad5686_spi_read,
	.sync = ad5686_spi_sync,
};

static int ad5686_spi_probe(struct spi_device *spi)
{
	const struct ad5686_chip_info *info;
	struct ad5686_spi_data *bus_data;
	struct device *dev = &spi->dev;
	unsigned int capacity;

	info = spi_get_device_match_data(spi);
	if (!info)
		return -ENODATA;

	/* read operation requires at least 2 transfers */
	capacity = max(info->num_channels, 2);
	bus_data = devm_kzalloc(dev, struct_size(bus_data, xfers, capacity),
				GFP_KERNEL);
	if (!bus_data)
		return -ENOMEM;

	bus_data->capacity = capacity;

	return ad5686_probe(dev, info, spi->modalias, &ad5686_spi_ops, bus_data);
}

static const struct spi_device_id ad5686_spi_id[] = {
	{ .name = "ad5310r", .driver_data = (kernel_ulong_t)&ad5310r_chip_info },
	{ .name = "ad5313r", .driver_data = (kernel_ulong_t)&ad5338r_chip_info },
	{ .name = "ad5317r", .driver_data = (kernel_ulong_t)&ad5317r_chip_info },
	{ .name = "ad5672r", .driver_data = (kernel_ulong_t)&ad5672r_chip_info },
	{ .name = "ad5674",  .driver_data = (kernel_ulong_t)&ad5674_chip_info },
	{ .name = "ad5674r", .driver_data = (kernel_ulong_t)&ad5674r_chip_info },
	{ .name = "ad5676",  .driver_data = (kernel_ulong_t)&ad5676_chip_info },
	{ .name = "ad5676r", .driver_data = (kernel_ulong_t)&ad5676r_chip_info },
	{ .name = "ad5679",  .driver_data = (kernel_ulong_t)&ad5679_chip_info },
	{ .name = "ad5679r", .driver_data = (kernel_ulong_t)&ad5679r_chip_info },
	{ .name = "ad5681r", .driver_data = (kernel_ulong_t)&ad5681r_chip_info },
	{ .name = "ad5682r", .driver_data = (kernel_ulong_t)&ad5682r_chip_info },
	{ .name = "ad5683",  .driver_data = (kernel_ulong_t)&ad5683_chip_info },
	{ .name = "ad5683r", .driver_data = (kernel_ulong_t)&ad5683r_chip_info },
	{ .name = "ad5684",  .driver_data = (kernel_ulong_t)&ad5684_chip_info },
	{ .name = "ad5684r", .driver_data = (kernel_ulong_t)&ad5684r_chip_info },
	{ .name = "ad5685",  .driver_data = (kernel_ulong_t)&ad5685r_chip_info }, /* nonexistent */
	{ .name = "ad5685r", .driver_data = (kernel_ulong_t)&ad5685r_chip_info },
	{ .name = "ad5686",  .driver_data = (kernel_ulong_t)&ad5686_chip_info },
	{ .name = "ad5686r", .driver_data = (kernel_ulong_t)&ad5686r_chip_info },
	{ .name = "ad5687",  .driver_data = (kernel_ulong_t)&ad5687_chip_info },
	{ .name = "ad5687r", .driver_data = (kernel_ulong_t)&ad5687r_chip_info },
	{ .name = "ad5689",  .driver_data = (kernel_ulong_t)&ad5689_chip_info },
	{ .name = "ad5689r", .driver_data = (kernel_ulong_t)&ad5689r_chip_info },
	{ }
};
MODULE_DEVICE_TABLE(spi, ad5686_spi_id);

static const struct of_device_id ad5686_of_match[] = {
	{ .compatible = "adi,ad5310r", .data = &ad5310r_chip_info },
	{ .compatible = "adi,ad5313r", .data = &ad5338r_chip_info },
	{ .compatible = "adi,ad5317r", .data = &ad5317r_chip_info },
	{ .compatible = "adi,ad5672r", .data = &ad5672r_chip_info },
	{ .compatible = "adi,ad5674",  .data = &ad5674_chip_info },
	{ .compatible = "adi,ad5674r", .data = &ad5674r_chip_info },
	{ .compatible = "adi,ad5676",  .data = &ad5676_chip_info },
	{ .compatible = "adi,ad5676r", .data = &ad5676r_chip_info },
	{ .compatible = "adi,ad5679",  .data = &ad5679_chip_info },
	{ .compatible = "adi,ad5679r", .data = &ad5679r_chip_info },
	{ .compatible = "adi,ad5681r", .data = &ad5681r_chip_info },
	{ .compatible = "adi,ad5682r", .data = &ad5682r_chip_info },
	{ .compatible = "adi,ad5683",  .data = &ad5683_chip_info },
	{ .compatible = "adi,ad5683r", .data = &ad5683r_chip_info },
	{ .compatible = "adi,ad5684",  .data = &ad5684_chip_info },
	{ .compatible = "adi,ad5684r", .data = &ad5684r_chip_info },
	{ .compatible = "adi,ad5685r", .data = &ad5685r_chip_info },
	{ .compatible = "adi,ad5686",  .data = &ad5686_chip_info },
	{ .compatible = "adi,ad5686r", .data = &ad5686r_chip_info },
	{ .compatible = "adi,ad5687",  .data = &ad5687_chip_info },
	{ .compatible = "adi,ad5687r", .data = &ad5687r_chip_info },
	{ .compatible = "adi,ad5689",  .data = &ad5689_chip_info },
	{ .compatible = "adi,ad5689r", .data = &ad5689r_chip_info },
	{ }
};
MODULE_DEVICE_TABLE(of, ad5686_of_match);

static struct spi_driver ad5686_spi_driver = {
	.driver = {
		.name = "ad5686",
		.of_match_table = ad5686_of_match,
	},
	.probe = ad5686_spi_probe,
	.id_table = ad5686_spi_id,
};

module_spi_driver(ad5686_spi_driver);

MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
MODULE_DESCRIPTION("Analog Devices AD5686 and similar multi-channel DACs");
MODULE_LICENSE("GPL v2");
MODULE_IMPORT_NS("IIO_AD5686");