Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver
4 *
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/delay.h>
12#include <linux/i2c.h>
13#include <linux/gpio/driver.h>
14#include <linux/spi/spi.h>
15#include <linux/unaligned.h>
16
17#define SPI_XCOMM_SETTINGS_LEN_OFFSET 10
18#define SPI_XCOMM_SETTINGS_3WIRE BIT(6)
19#define SPI_XCOMM_SETTINGS_CS_HIGH BIT(5)
20#define SPI_XCOMM_SETTINGS_SAMPLE_END BIT(4)
21#define SPI_XCOMM_SETTINGS_CPHA BIT(3)
22#define SPI_XCOMM_SETTINGS_CPOL BIT(2)
23#define SPI_XCOMM_SETTINGS_CLOCK_DIV_MASK 0x3
24#define SPI_XCOMM_SETTINGS_CLOCK_DIV_64 0x2
25#define SPI_XCOMM_SETTINGS_CLOCK_DIV_16 0x1
26#define SPI_XCOMM_SETTINGS_CLOCK_DIV_4 0x0
27
28#define SPI_XCOMM_CMD_UPDATE_CONFIG 0x03
29#define SPI_XCOMM_CMD_WRITE 0x04
30#define SPI_XCOMM_CMD_GPIO_SET 0x05
31
32#define SPI_XCOMM_CLOCK 48000000
33
34struct spi_xcomm {
35 struct i2c_client *i2c;
36
37 struct gpio_chip gc;
38
39 u16 settings;
40 u16 chipselect;
41
42 unsigned int current_speed;
43
44 u8 buf[63];
45};
46
47static void spi_xcomm_gpio_set_value(struct gpio_chip *chip,
48 unsigned int offset, int val)
49{
50 struct spi_xcomm *spi_xcomm = gpiochip_get_data(chip);
51 unsigned char buf[2];
52
53 buf[0] = SPI_XCOMM_CMD_GPIO_SET;
54 buf[1] = !!val;
55
56 i2c_master_send(spi_xcomm->i2c, buf, 2);
57}
58
59static int spi_xcomm_gpio_get_direction(struct gpio_chip *chip,
60 unsigned int offset)
61{
62 return GPIO_LINE_DIRECTION_OUT;
63}
64
65static int spi_xcomm_gpio_add(struct spi_xcomm *spi_xcomm)
66{
67 struct device *dev = &spi_xcomm->i2c->dev;
68
69 if (!IS_ENABLED(CONFIG_GPIOLIB))
70 return 0;
71
72 spi_xcomm->gc.get_direction = spi_xcomm_gpio_get_direction;
73 spi_xcomm->gc.set = spi_xcomm_gpio_set_value;
74 spi_xcomm->gc.can_sleep = 1;
75 spi_xcomm->gc.base = -1;
76 spi_xcomm->gc.ngpio = 1;
77 spi_xcomm->gc.label = spi_xcomm->i2c->name;
78 spi_xcomm->gc.owner = THIS_MODULE;
79
80 return devm_gpiochip_add_data(dev, &spi_xcomm->gc, spi_xcomm);
81}
82
83static int spi_xcomm_sync_config(struct spi_xcomm *spi_xcomm, unsigned int len)
84{
85 u16 settings;
86 u8 *buf = spi_xcomm->buf;
87
88 settings = spi_xcomm->settings;
89 settings |= len << SPI_XCOMM_SETTINGS_LEN_OFFSET;
90
91 buf[0] = SPI_XCOMM_CMD_UPDATE_CONFIG;
92 put_unaligned_be16(settings, &buf[1]);
93 put_unaligned_be16(spi_xcomm->chipselect, &buf[3]);
94
95 return i2c_master_send(spi_xcomm->i2c, buf, 5);
96}
97
98static void spi_xcomm_chipselect(struct spi_xcomm *spi_xcomm,
99 struct spi_device *spi, int is_active)
100{
101 unsigned long cs = spi_get_chipselect(spi, 0);
102 u16 chipselect = spi_xcomm->chipselect;
103
104 if (is_active)
105 chipselect |= BIT(cs);
106 else
107 chipselect &= ~BIT(cs);
108
109 spi_xcomm->chipselect = chipselect;
110}
111
112static int spi_xcomm_setup_transfer(struct spi_xcomm *spi_xcomm,
113 struct spi_device *spi, struct spi_transfer *t,
114 unsigned int *settings)
115{
116 if (t->len > 62)
117 return -EINVAL;
118
119 if (t->speed_hz != spi_xcomm->current_speed) {
120 unsigned int divider;
121
122 divider = DIV_ROUND_UP(SPI_XCOMM_CLOCK, t->speed_hz);
123 if (divider >= 64)
124 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_64;
125 else if (divider >= 16)
126 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_16;
127 else
128 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_4;
129
130 spi_xcomm->current_speed = t->speed_hz;
131 }
132
133 if (spi->mode & SPI_CPOL)
134 *settings |= SPI_XCOMM_SETTINGS_CPOL;
135 else
136 *settings &= ~SPI_XCOMM_SETTINGS_CPOL;
137
138 if (spi->mode & SPI_CPHA)
139 *settings &= ~SPI_XCOMM_SETTINGS_CPHA;
140 else
141 *settings |= SPI_XCOMM_SETTINGS_CPHA;
142
143 if (spi->mode & SPI_3WIRE)
144 *settings |= SPI_XCOMM_SETTINGS_3WIRE;
145 else
146 *settings &= ~SPI_XCOMM_SETTINGS_3WIRE;
147
148 return 0;
149}
150
151static int spi_xcomm_txrx_bufs(struct spi_xcomm *spi_xcomm,
152 struct spi_device *spi, struct spi_transfer *t)
153{
154 int ret;
155
156 if (t->tx_buf) {
157 spi_xcomm->buf[0] = SPI_XCOMM_CMD_WRITE;
158 memcpy(spi_xcomm->buf + 1, t->tx_buf, t->len);
159
160 ret = i2c_master_send(spi_xcomm->i2c, spi_xcomm->buf, t->len + 1);
161 if (ret < 0)
162 return ret;
163 if (ret != t->len + 1)
164 return -EIO;
165 } else if (t->rx_buf) {
166 ret = i2c_master_recv(spi_xcomm->i2c, t->rx_buf, t->len);
167 if (ret < 0)
168 return ret;
169 if (ret != t->len)
170 return -EIO;
171 }
172
173 return t->len;
174}
175
176static int spi_xcomm_transfer_one(struct spi_controller *host,
177 struct spi_message *msg)
178{
179 struct spi_xcomm *spi_xcomm = spi_controller_get_devdata(host);
180 unsigned int settings = spi_xcomm->settings;
181 struct spi_device *spi = msg->spi;
182 unsigned int cs_change = 0;
183 struct spi_transfer *t;
184 bool is_first = true;
185 int status = 0;
186 bool is_last;
187
188 spi_xcomm_chipselect(spi_xcomm, spi, true);
189
190 list_for_each_entry(t, &msg->transfers, transfer_list) {
191 if (!t->tx_buf && !t->rx_buf && t->len) {
192 status = -EINVAL;
193 break;
194 }
195
196 status = spi_xcomm_setup_transfer(spi_xcomm, spi, t, &settings);
197 if (status < 0)
198 break;
199
200 is_last = list_is_last(&t->transfer_list, &msg->transfers);
201 cs_change = t->cs_change;
202
203 if (cs_change ^ is_last)
204 settings |= BIT(5);
205 else
206 settings &= ~BIT(5);
207
208 if (t->rx_buf) {
209 spi_xcomm->settings = settings;
210 status = spi_xcomm_sync_config(spi_xcomm, t->len);
211 if (status < 0)
212 break;
213 } else if (settings != spi_xcomm->settings || is_first) {
214 spi_xcomm->settings = settings;
215 status = spi_xcomm_sync_config(spi_xcomm, 0);
216 if (status < 0)
217 break;
218 }
219
220 if (t->len) {
221 status = spi_xcomm_txrx_bufs(spi_xcomm, spi, t);
222
223 if (status < 0)
224 break;
225
226 if (status > 0)
227 msg->actual_length += status;
228 }
229 status = 0;
230
231 spi_transfer_delay_exec(t);
232
233 is_first = false;
234 }
235
236 if (status != 0 || !cs_change)
237 spi_xcomm_chipselect(spi_xcomm, spi, false);
238
239 msg->status = status;
240 spi_finalize_current_message(host);
241
242 return status;
243}
244
245static int spi_xcomm_probe(struct i2c_client *i2c)
246{
247 struct spi_xcomm *spi_xcomm;
248 struct spi_controller *host;
249 int ret;
250
251 host = devm_spi_alloc_host(&i2c->dev, sizeof(*spi_xcomm));
252 if (!host)
253 return -ENOMEM;
254
255 spi_xcomm = spi_controller_get_devdata(host);
256 spi_xcomm->i2c = i2c;
257
258 host->num_chipselect = 16;
259 host->mode_bits = SPI_CPHA | SPI_CPOL | SPI_3WIRE;
260 host->bits_per_word_mask = SPI_BPW_MASK(8);
261 host->flags = SPI_CONTROLLER_HALF_DUPLEX;
262 host->transfer_one_message = spi_xcomm_transfer_one;
263 host->dev.of_node = i2c->dev.of_node;
264
265 ret = devm_spi_register_controller(&i2c->dev, host);
266 if (ret < 0)
267 return ret;
268
269 return spi_xcomm_gpio_add(spi_xcomm);
270}
271
272static const struct i2c_device_id spi_xcomm_ids[] = {
273 { "spi-xcomm" },
274 { },
275};
276MODULE_DEVICE_TABLE(i2c, spi_xcomm_ids);
277
278static struct i2c_driver spi_xcomm_driver = {
279 .driver = {
280 .name = "spi-xcomm",
281 },
282 .id_table = spi_xcomm_ids,
283 .probe = spi_xcomm_probe,
284};
285module_i2c_driver(spi_xcomm_driver);
286
287MODULE_LICENSE("GPL");
288MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
289MODULE_DESCRIPTION("Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver");
1/*
2 * Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver
3 *
4 * Copyright 2012 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
6 *
7 * Licensed under the GPL-2 or later.
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/delay.h>
13#include <linux/i2c.h>
14#include <linux/spi/spi.h>
15#include <asm/unaligned.h>
16
17#define SPI_XCOMM_SETTINGS_LEN_OFFSET 10
18#define SPI_XCOMM_SETTINGS_3WIRE BIT(6)
19#define SPI_XCOMM_SETTINGS_CS_HIGH BIT(5)
20#define SPI_XCOMM_SETTINGS_SAMPLE_END BIT(4)
21#define SPI_XCOMM_SETTINGS_CPHA BIT(3)
22#define SPI_XCOMM_SETTINGS_CPOL BIT(2)
23#define SPI_XCOMM_SETTINGS_CLOCK_DIV_MASK 0x3
24#define SPI_XCOMM_SETTINGS_CLOCK_DIV_64 0x2
25#define SPI_XCOMM_SETTINGS_CLOCK_DIV_16 0x1
26#define SPI_XCOMM_SETTINGS_CLOCK_DIV_4 0x0
27
28#define SPI_XCOMM_CMD_UPDATE_CONFIG 0x03
29#define SPI_XCOMM_CMD_WRITE 0x04
30
31#define SPI_XCOMM_CLOCK 48000000
32
33struct spi_xcomm {
34 struct i2c_client *i2c;
35
36 uint16_t settings;
37 uint16_t chipselect;
38
39 unsigned int current_speed;
40
41 uint8_t buf[63];
42};
43
44static int spi_xcomm_sync_config(struct spi_xcomm *spi_xcomm, unsigned int len)
45{
46 uint16_t settings;
47 uint8_t *buf = spi_xcomm->buf;
48
49 settings = spi_xcomm->settings;
50 settings |= len << SPI_XCOMM_SETTINGS_LEN_OFFSET;
51
52 buf[0] = SPI_XCOMM_CMD_UPDATE_CONFIG;
53 put_unaligned_be16(settings, &buf[1]);
54 put_unaligned_be16(spi_xcomm->chipselect, &buf[3]);
55
56 return i2c_master_send(spi_xcomm->i2c, buf, 5);
57}
58
59static void spi_xcomm_chipselect(struct spi_xcomm *spi_xcomm,
60 struct spi_device *spi, int is_active)
61{
62 unsigned long cs = spi->chip_select;
63 uint16_t chipselect = spi_xcomm->chipselect;
64
65 if (is_active)
66 chipselect |= BIT(cs);
67 else
68 chipselect &= ~BIT(cs);
69
70 spi_xcomm->chipselect = chipselect;
71}
72
73static int spi_xcomm_setup_transfer(struct spi_xcomm *spi_xcomm,
74 struct spi_device *spi, struct spi_transfer *t, unsigned int *settings)
75{
76 if (t->len > 62)
77 return -EINVAL;
78
79 if (t->speed_hz != spi_xcomm->current_speed) {
80 unsigned int divider;
81
82 divider = DIV_ROUND_UP(SPI_XCOMM_CLOCK, t->speed_hz);
83 if (divider >= 64)
84 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_64;
85 else if (divider >= 16)
86 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_16;
87 else
88 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_4;
89
90 spi_xcomm->current_speed = t->speed_hz;
91 }
92
93 if (spi->mode & SPI_CPOL)
94 *settings |= SPI_XCOMM_SETTINGS_CPOL;
95 else
96 *settings &= ~SPI_XCOMM_SETTINGS_CPOL;
97
98 if (spi->mode & SPI_CPHA)
99 *settings &= ~SPI_XCOMM_SETTINGS_CPHA;
100 else
101 *settings |= SPI_XCOMM_SETTINGS_CPHA;
102
103 if (spi->mode & SPI_3WIRE)
104 *settings |= SPI_XCOMM_SETTINGS_3WIRE;
105 else
106 *settings &= ~SPI_XCOMM_SETTINGS_3WIRE;
107
108 return 0;
109}
110
111static int spi_xcomm_txrx_bufs(struct spi_xcomm *spi_xcomm,
112 struct spi_device *spi, struct spi_transfer *t)
113{
114 int ret;
115
116 if (t->tx_buf) {
117 spi_xcomm->buf[0] = SPI_XCOMM_CMD_WRITE;
118 memcpy(spi_xcomm->buf + 1, t->tx_buf, t->len);
119
120 ret = i2c_master_send(spi_xcomm->i2c, spi_xcomm->buf, t->len + 1);
121 if (ret < 0)
122 return ret;
123 else if (ret != t->len + 1)
124 return -EIO;
125 } else if (t->rx_buf) {
126 ret = i2c_master_recv(spi_xcomm->i2c, t->rx_buf, t->len);
127 if (ret < 0)
128 return ret;
129 else if (ret != t->len)
130 return -EIO;
131 }
132
133 return t->len;
134}
135
136static int spi_xcomm_transfer_one(struct spi_master *master,
137 struct spi_message *msg)
138{
139 struct spi_xcomm *spi_xcomm = spi_master_get_devdata(master);
140 unsigned int settings = spi_xcomm->settings;
141 struct spi_device *spi = msg->spi;
142 unsigned cs_change = 0;
143 struct spi_transfer *t;
144 bool is_first = true;
145 int status = 0;
146 bool is_last;
147
148 spi_xcomm_chipselect(spi_xcomm, spi, true);
149
150 list_for_each_entry(t, &msg->transfers, transfer_list) {
151
152 if (!t->tx_buf && !t->rx_buf && t->len) {
153 status = -EINVAL;
154 break;
155 }
156
157 status = spi_xcomm_setup_transfer(spi_xcomm, spi, t, &settings);
158 if (status < 0)
159 break;
160
161 is_last = list_is_last(&t->transfer_list, &msg->transfers);
162 cs_change = t->cs_change;
163
164 if (cs_change ^ is_last)
165 settings |= BIT(5);
166 else
167 settings &= ~BIT(5);
168
169 if (t->rx_buf) {
170 spi_xcomm->settings = settings;
171 status = spi_xcomm_sync_config(spi_xcomm, t->len);
172 if (status < 0)
173 break;
174 } else if (settings != spi_xcomm->settings || is_first) {
175 spi_xcomm->settings = settings;
176 status = spi_xcomm_sync_config(spi_xcomm, 0);
177 if (status < 0)
178 break;
179 }
180
181 if (t->len) {
182 status = spi_xcomm_txrx_bufs(spi_xcomm, spi, t);
183
184 if (status < 0)
185 break;
186
187 if (status > 0)
188 msg->actual_length += status;
189 }
190 status = 0;
191
192 if (t->delay_usecs)
193 udelay(t->delay_usecs);
194
195 is_first = false;
196 }
197
198 if (status != 0 || !cs_change)
199 spi_xcomm_chipselect(spi_xcomm, spi, false);
200
201 msg->status = status;
202 spi_finalize_current_message(master);
203
204 return status;
205}
206
207static int spi_xcomm_probe(struct i2c_client *i2c,
208 const struct i2c_device_id *id)
209{
210 struct spi_xcomm *spi_xcomm;
211 struct spi_master *master;
212 int ret;
213
214 master = spi_alloc_master(&i2c->dev, sizeof(*spi_xcomm));
215 if (!master)
216 return -ENOMEM;
217
218 spi_xcomm = spi_master_get_devdata(master);
219 spi_xcomm->i2c = i2c;
220
221 master->num_chipselect = 16;
222 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_3WIRE;
223 master->bits_per_word_mask = SPI_BPW_MASK(8);
224 master->flags = SPI_MASTER_HALF_DUPLEX;
225 master->transfer_one_message = spi_xcomm_transfer_one;
226 master->dev.of_node = i2c->dev.of_node;
227 i2c_set_clientdata(i2c, master);
228
229 ret = devm_spi_register_master(&i2c->dev, master);
230 if (ret < 0)
231 spi_master_put(master);
232
233 return ret;
234}
235
236static const struct i2c_device_id spi_xcomm_ids[] = {
237 { "spi-xcomm" },
238 { },
239};
240MODULE_DEVICE_TABLE(i2c, spi_xcomm_ids);
241
242static struct i2c_driver spi_xcomm_driver = {
243 .driver = {
244 .name = "spi-xcomm",
245 },
246 .id_table = spi_xcomm_ids,
247 .probe = spi_xcomm_probe,
248};
249module_i2c_driver(spi_xcomm_driver);
250
251MODULE_LICENSE("GPL");
252MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
253MODULE_DESCRIPTION("Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver");