Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * NXP SC18IS602/603 SPI driver
4 *
5 * Copyright (C) Guenter Roeck <linux@roeck-us.net>
6 */
7
8#include <linux/kernel.h>
9#include <linux/err.h>
10#include <linux/module.h>
11#include <linux/spi/spi.h>
12#include <linux/i2c.h>
13#include <linux/delay.h>
14#include <linux/pm_runtime.h>
15#include <linux/of_device.h>
16#include <linux/of.h>
17#include <linux/platform_data/sc18is602.h>
18#include <linux/gpio/consumer.h>
19
20enum chips { sc18is602, sc18is602b, sc18is603 };
21
22#define SC18IS602_BUFSIZ 200
23#define SC18IS602_CLOCK 7372000
24
25#define SC18IS602_MODE_CPHA BIT(2)
26#define SC18IS602_MODE_CPOL BIT(3)
27#define SC18IS602_MODE_LSB_FIRST BIT(5)
28#define SC18IS602_MODE_CLOCK_DIV_4 0x0
29#define SC18IS602_MODE_CLOCK_DIV_16 0x1
30#define SC18IS602_MODE_CLOCK_DIV_64 0x2
31#define SC18IS602_MODE_CLOCK_DIV_128 0x3
32
33struct sc18is602 {
34 struct spi_master *master;
35 struct device *dev;
36 u8 ctrl;
37 u32 freq;
38 u32 speed;
39
40 /* I2C data */
41 struct i2c_client *client;
42 enum chips id;
43 u8 buffer[SC18IS602_BUFSIZ + 1];
44 int tlen; /* Data queued for tx in buffer */
45 int rindex; /* Receive data index in buffer */
46
47 struct gpio_desc *reset;
48};
49
50static int sc18is602_wait_ready(struct sc18is602 *hw, int len)
51{
52 int i, err;
53 int usecs = 1000000 * len / hw->speed + 1;
54 u8 dummy[1];
55
56 for (i = 0; i < 10; i++) {
57 err = i2c_master_recv(hw->client, dummy, 1);
58 if (err >= 0)
59 return 0;
60 usleep_range(usecs, usecs * 2);
61 }
62 return -ETIMEDOUT;
63}
64
65static int sc18is602_txrx(struct sc18is602 *hw, struct spi_message *msg,
66 struct spi_transfer *t, bool do_transfer)
67{
68 unsigned int len = t->len;
69 int ret;
70
71 if (hw->tlen == 0) {
72 /* First byte (I2C command) is chip select */
73 hw->buffer[0] = 1 << msg->spi->chip_select;
74 hw->tlen = 1;
75 hw->rindex = 0;
76 }
77 /*
78 * We can not immediately send data to the chip, since each I2C message
79 * resembles a full SPI message (from CS active to CS inactive).
80 * Enqueue messages up to the first read or until do_transfer is true.
81 */
82 if (t->tx_buf) {
83 memcpy(&hw->buffer[hw->tlen], t->tx_buf, len);
84 hw->tlen += len;
85 if (t->rx_buf)
86 do_transfer = true;
87 else
88 hw->rindex = hw->tlen - 1;
89 } else if (t->rx_buf) {
90 /*
91 * For receive-only transfers we still need to perform a dummy
92 * write to receive data from the SPI chip.
93 * Read data starts at the end of transmit data (minus 1 to
94 * account for CS).
95 */
96 hw->rindex = hw->tlen - 1;
97 memset(&hw->buffer[hw->tlen], 0, len);
98 hw->tlen += len;
99 do_transfer = true;
100 }
101
102 if (do_transfer && hw->tlen > 1) {
103 ret = sc18is602_wait_ready(hw, SC18IS602_BUFSIZ);
104 if (ret < 0)
105 return ret;
106 ret = i2c_master_send(hw->client, hw->buffer, hw->tlen);
107 if (ret < 0)
108 return ret;
109 if (ret != hw->tlen)
110 return -EIO;
111
112 if (t->rx_buf) {
113 int rlen = hw->rindex + len;
114
115 ret = sc18is602_wait_ready(hw, hw->tlen);
116 if (ret < 0)
117 return ret;
118 ret = i2c_master_recv(hw->client, hw->buffer, rlen);
119 if (ret < 0)
120 return ret;
121 if (ret != rlen)
122 return -EIO;
123 memcpy(t->rx_buf, &hw->buffer[hw->rindex], len);
124 }
125 hw->tlen = 0;
126 }
127 return len;
128}
129
130static int sc18is602_setup_transfer(struct sc18is602 *hw, u32 hz, u8 mode)
131{
132 u8 ctrl = 0;
133 int ret;
134
135 if (mode & SPI_CPHA)
136 ctrl |= SC18IS602_MODE_CPHA;
137 if (mode & SPI_CPOL)
138 ctrl |= SC18IS602_MODE_CPOL;
139 if (mode & SPI_LSB_FIRST)
140 ctrl |= SC18IS602_MODE_LSB_FIRST;
141
142 /* Find the closest clock speed */
143 if (hz >= hw->freq / 4) {
144 ctrl |= SC18IS602_MODE_CLOCK_DIV_4;
145 hw->speed = hw->freq / 4;
146 } else if (hz >= hw->freq / 16) {
147 ctrl |= SC18IS602_MODE_CLOCK_DIV_16;
148 hw->speed = hw->freq / 16;
149 } else if (hz >= hw->freq / 64) {
150 ctrl |= SC18IS602_MODE_CLOCK_DIV_64;
151 hw->speed = hw->freq / 64;
152 } else {
153 ctrl |= SC18IS602_MODE_CLOCK_DIV_128;
154 hw->speed = hw->freq / 128;
155 }
156
157 /*
158 * Don't do anything if the control value did not change. The initial
159 * value of 0xff for hw->ctrl ensures that the correct mode will be set
160 * with the first call to this function.
161 */
162 if (ctrl == hw->ctrl)
163 return 0;
164
165 ret = i2c_smbus_write_byte_data(hw->client, 0xf0, ctrl);
166 if (ret < 0)
167 return ret;
168
169 hw->ctrl = ctrl;
170
171 return 0;
172}
173
174static int sc18is602_check_transfer(struct spi_device *spi,
175 struct spi_transfer *t, int tlen)
176{
177 if (t && t->len + tlen > SC18IS602_BUFSIZ + 1)
178 return -EINVAL;
179
180 return 0;
181}
182
183static int sc18is602_transfer_one(struct spi_master *master,
184 struct spi_message *m)
185{
186 struct sc18is602 *hw = spi_master_get_devdata(master);
187 struct spi_device *spi = m->spi;
188 struct spi_transfer *t;
189 int status = 0;
190
191 hw->tlen = 0;
192 list_for_each_entry(t, &m->transfers, transfer_list) {
193 bool do_transfer;
194
195 status = sc18is602_check_transfer(spi, t, hw->tlen);
196 if (status < 0)
197 break;
198
199 status = sc18is602_setup_transfer(hw, t->speed_hz, spi->mode);
200 if (status < 0)
201 break;
202
203 do_transfer = t->cs_change || list_is_last(&t->transfer_list,
204 &m->transfers);
205
206 if (t->len) {
207 status = sc18is602_txrx(hw, m, t, do_transfer);
208 if (status < 0)
209 break;
210 m->actual_length += status;
211 }
212 status = 0;
213
214 spi_transfer_delay_exec(t);
215 }
216 m->status = status;
217 spi_finalize_current_message(master);
218
219 return status;
220}
221
222static size_t sc18is602_max_transfer_size(struct spi_device *spi)
223{
224 return SC18IS602_BUFSIZ;
225}
226
227static int sc18is602_setup(struct spi_device *spi)
228{
229 struct sc18is602 *hw = spi_master_get_devdata(spi->master);
230
231 /* SC18IS602 does not support CS2 */
232 if (hw->id == sc18is602 && spi->chip_select == 2)
233 return -ENXIO;
234
235 return 0;
236}
237
238static int sc18is602_probe(struct i2c_client *client)
239{
240 const struct i2c_device_id *id = i2c_client_get_device_id(client);
241 struct device *dev = &client->dev;
242 struct device_node *np = dev->of_node;
243 struct sc18is602_platform_data *pdata = dev_get_platdata(dev);
244 struct sc18is602 *hw;
245 struct spi_master *master;
246
247 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
248 I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
249 return -EINVAL;
250
251 master = devm_spi_alloc_master(dev, sizeof(struct sc18is602));
252 if (!master)
253 return -ENOMEM;
254
255 hw = spi_master_get_devdata(master);
256 i2c_set_clientdata(client, hw);
257
258 /* assert reset and then release */
259 hw->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
260 if (IS_ERR(hw->reset))
261 return PTR_ERR(hw->reset);
262 gpiod_set_value_cansleep(hw->reset, 0);
263
264 hw->master = master;
265 hw->client = client;
266 hw->dev = dev;
267 hw->ctrl = 0xff;
268
269 if (client->dev.of_node)
270 hw->id = (enum chips)of_device_get_match_data(&client->dev);
271 else
272 hw->id = id->driver_data;
273
274 switch (hw->id) {
275 case sc18is602:
276 case sc18is602b:
277 master->num_chipselect = 4;
278 hw->freq = SC18IS602_CLOCK;
279 break;
280 case sc18is603:
281 master->num_chipselect = 2;
282 if (pdata) {
283 hw->freq = pdata->clock_frequency;
284 } else {
285 const __be32 *val;
286 int len;
287
288 val = of_get_property(np, "clock-frequency", &len);
289 if (val && len >= sizeof(__be32))
290 hw->freq = be32_to_cpup(val);
291 }
292 if (!hw->freq)
293 hw->freq = SC18IS602_CLOCK;
294 break;
295 }
296 master->bus_num = np ? -1 : client->adapter->nr;
297 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
298 master->bits_per_word_mask = SPI_BPW_MASK(8);
299 master->setup = sc18is602_setup;
300 master->transfer_one_message = sc18is602_transfer_one;
301 master->max_transfer_size = sc18is602_max_transfer_size;
302 master->max_message_size = sc18is602_max_transfer_size;
303 master->dev.of_node = np;
304 master->min_speed_hz = hw->freq / 128;
305 master->max_speed_hz = hw->freq / 4;
306
307 return devm_spi_register_master(dev, master);
308}
309
310static const struct i2c_device_id sc18is602_id[] = {
311 { "sc18is602", sc18is602 },
312 { "sc18is602b", sc18is602b },
313 { "sc18is603", sc18is603 },
314 { }
315};
316MODULE_DEVICE_TABLE(i2c, sc18is602_id);
317
318static const struct of_device_id sc18is602_of_match[] = {
319 {
320 .compatible = "nxp,sc18is602",
321 .data = (void *)sc18is602
322 },
323 {
324 .compatible = "nxp,sc18is602b",
325 .data = (void *)sc18is602b
326 },
327 {
328 .compatible = "nxp,sc18is603",
329 .data = (void *)sc18is603
330 },
331 { },
332};
333MODULE_DEVICE_TABLE(of, sc18is602_of_match);
334
335static struct i2c_driver sc18is602_driver = {
336 .driver = {
337 .name = "sc18is602",
338 .of_match_table = of_match_ptr(sc18is602_of_match),
339 },
340 .probe_new = sc18is602_probe,
341 .id_table = sc18is602_id,
342};
343
344module_i2c_driver(sc18is602_driver);
345
346MODULE_DESCRIPTION("SC18IS602/603 SPI Master Driver");
347MODULE_AUTHOR("Guenter Roeck");
348MODULE_LICENSE("GPL");
1/*
2 * NXP SC18IS602/603 SPI driver
3 *
4 * Copyright (C) Guenter Roeck <linux@roeck-us.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/kernel.h>
18#include <linux/err.h>
19#include <linux/module.h>
20#include <linux/spi/spi.h>
21#include <linux/i2c.h>
22#include <linux/delay.h>
23#include <linux/pm_runtime.h>
24#include <linux/of_device.h>
25#include <linux/of.h>
26#include <linux/platform_data/sc18is602.h>
27#include <linux/gpio/consumer.h>
28
29enum chips { sc18is602, sc18is602b, sc18is603 };
30
31#define SC18IS602_BUFSIZ 200
32#define SC18IS602_CLOCK 7372000
33
34#define SC18IS602_MODE_CPHA BIT(2)
35#define SC18IS602_MODE_CPOL BIT(3)
36#define SC18IS602_MODE_LSB_FIRST BIT(5)
37#define SC18IS602_MODE_CLOCK_DIV_4 0x0
38#define SC18IS602_MODE_CLOCK_DIV_16 0x1
39#define SC18IS602_MODE_CLOCK_DIV_64 0x2
40#define SC18IS602_MODE_CLOCK_DIV_128 0x3
41
42struct sc18is602 {
43 struct spi_master *master;
44 struct device *dev;
45 u8 ctrl;
46 u32 freq;
47 u32 speed;
48
49 /* I2C data */
50 struct i2c_client *client;
51 enum chips id;
52 u8 buffer[SC18IS602_BUFSIZ + 1];
53 int tlen; /* Data queued for tx in buffer */
54 int rindex; /* Receive data index in buffer */
55
56 struct gpio_desc *reset;
57};
58
59static int sc18is602_wait_ready(struct sc18is602 *hw, int len)
60{
61 int i, err;
62 int usecs = 1000000 * len / hw->speed + 1;
63 u8 dummy[1];
64
65 for (i = 0; i < 10; i++) {
66 err = i2c_master_recv(hw->client, dummy, 1);
67 if (err >= 0)
68 return 0;
69 usleep_range(usecs, usecs * 2);
70 }
71 return -ETIMEDOUT;
72}
73
74static int sc18is602_txrx(struct sc18is602 *hw, struct spi_message *msg,
75 struct spi_transfer *t, bool do_transfer)
76{
77 unsigned int len = t->len;
78 int ret;
79
80 if (hw->tlen == 0) {
81 /* First byte (I2C command) is chip select */
82 hw->buffer[0] = 1 << msg->spi->chip_select;
83 hw->tlen = 1;
84 hw->rindex = 0;
85 }
86 /*
87 * We can not immediately send data to the chip, since each I2C message
88 * resembles a full SPI message (from CS active to CS inactive).
89 * Enqueue messages up to the first read or until do_transfer is true.
90 */
91 if (t->tx_buf) {
92 memcpy(&hw->buffer[hw->tlen], t->tx_buf, len);
93 hw->tlen += len;
94 if (t->rx_buf)
95 do_transfer = true;
96 else
97 hw->rindex = hw->tlen - 1;
98 } else if (t->rx_buf) {
99 /*
100 * For receive-only transfers we still need to perform a dummy
101 * write to receive data from the SPI chip.
102 * Read data starts at the end of transmit data (minus 1 to
103 * account for CS).
104 */
105 hw->rindex = hw->tlen - 1;
106 memset(&hw->buffer[hw->tlen], 0, len);
107 hw->tlen += len;
108 do_transfer = true;
109 }
110
111 if (do_transfer && hw->tlen > 1) {
112 ret = sc18is602_wait_ready(hw, SC18IS602_BUFSIZ);
113 if (ret < 0)
114 return ret;
115 ret = i2c_master_send(hw->client, hw->buffer, hw->tlen);
116 if (ret < 0)
117 return ret;
118 if (ret != hw->tlen)
119 return -EIO;
120
121 if (t->rx_buf) {
122 int rlen = hw->rindex + len;
123
124 ret = sc18is602_wait_ready(hw, hw->tlen);
125 if (ret < 0)
126 return ret;
127 ret = i2c_master_recv(hw->client, hw->buffer, rlen);
128 if (ret < 0)
129 return ret;
130 if (ret != rlen)
131 return -EIO;
132 memcpy(t->rx_buf, &hw->buffer[hw->rindex], len);
133 }
134 hw->tlen = 0;
135 }
136 return len;
137}
138
139static int sc18is602_setup_transfer(struct sc18is602 *hw, u32 hz, u8 mode)
140{
141 u8 ctrl = 0;
142 int ret;
143
144 if (mode & SPI_CPHA)
145 ctrl |= SC18IS602_MODE_CPHA;
146 if (mode & SPI_CPOL)
147 ctrl |= SC18IS602_MODE_CPOL;
148 if (mode & SPI_LSB_FIRST)
149 ctrl |= SC18IS602_MODE_LSB_FIRST;
150
151 /* Find the closest clock speed */
152 if (hz >= hw->freq / 4) {
153 ctrl |= SC18IS602_MODE_CLOCK_DIV_4;
154 hw->speed = hw->freq / 4;
155 } else if (hz >= hw->freq / 16) {
156 ctrl |= SC18IS602_MODE_CLOCK_DIV_16;
157 hw->speed = hw->freq / 16;
158 } else if (hz >= hw->freq / 64) {
159 ctrl |= SC18IS602_MODE_CLOCK_DIV_64;
160 hw->speed = hw->freq / 64;
161 } else {
162 ctrl |= SC18IS602_MODE_CLOCK_DIV_128;
163 hw->speed = hw->freq / 128;
164 }
165
166 /*
167 * Don't do anything if the control value did not change. The initial
168 * value of 0xff for hw->ctrl ensures that the correct mode will be set
169 * with the first call to this function.
170 */
171 if (ctrl == hw->ctrl)
172 return 0;
173
174 ret = i2c_smbus_write_byte_data(hw->client, 0xf0, ctrl);
175 if (ret < 0)
176 return ret;
177
178 hw->ctrl = ctrl;
179
180 return 0;
181}
182
183static int sc18is602_check_transfer(struct spi_device *spi,
184 struct spi_transfer *t, int tlen)
185{
186 if (t && t->len + tlen > SC18IS602_BUFSIZ)
187 return -EINVAL;
188
189 return 0;
190}
191
192static int sc18is602_transfer_one(struct spi_master *master,
193 struct spi_message *m)
194{
195 struct sc18is602 *hw = spi_master_get_devdata(master);
196 struct spi_device *spi = m->spi;
197 struct spi_transfer *t;
198 int status = 0;
199
200 hw->tlen = 0;
201 list_for_each_entry(t, &m->transfers, transfer_list) {
202 bool do_transfer;
203
204 status = sc18is602_check_transfer(spi, t, hw->tlen);
205 if (status < 0)
206 break;
207
208 status = sc18is602_setup_transfer(hw, t->speed_hz, spi->mode);
209 if (status < 0)
210 break;
211
212 do_transfer = t->cs_change || list_is_last(&t->transfer_list,
213 &m->transfers);
214
215 if (t->len) {
216 status = sc18is602_txrx(hw, m, t, do_transfer);
217 if (status < 0)
218 break;
219 m->actual_length += status;
220 }
221 status = 0;
222
223 if (t->delay_usecs)
224 udelay(t->delay_usecs);
225 }
226 m->status = status;
227 spi_finalize_current_message(master);
228
229 return status;
230}
231
232static int sc18is602_setup(struct spi_device *spi)
233{
234 struct sc18is602 *hw = spi_master_get_devdata(spi->master);
235
236 /* SC18IS602 does not support CS2 */
237 if (hw->id == sc18is602 && spi->chip_select == 2)
238 return -ENXIO;
239
240 return 0;
241}
242
243static int sc18is602_probe(struct i2c_client *client,
244 const struct i2c_device_id *id)
245{
246 struct device *dev = &client->dev;
247 struct device_node *np = dev->of_node;
248 struct sc18is602_platform_data *pdata = dev_get_platdata(dev);
249 struct sc18is602 *hw;
250 struct spi_master *master;
251 int error;
252
253 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
254 I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
255 return -EINVAL;
256
257 master = spi_alloc_master(dev, sizeof(struct sc18is602));
258 if (!master)
259 return -ENOMEM;
260
261 hw = spi_master_get_devdata(master);
262 i2c_set_clientdata(client, hw);
263
264 /* assert reset and then release */
265 hw->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
266 if (IS_ERR(hw->reset))
267 return PTR_ERR(hw->reset);
268 gpiod_set_value_cansleep(hw->reset, 0);
269
270 hw->master = master;
271 hw->client = client;
272 hw->dev = dev;
273 hw->ctrl = 0xff;
274
275 if (client->dev.of_node)
276 hw->id = (enum chips)of_device_get_match_data(&client->dev);
277 else
278 hw->id = id->driver_data;
279
280 switch (hw->id) {
281 case sc18is602:
282 case sc18is602b:
283 master->num_chipselect = 4;
284 hw->freq = SC18IS602_CLOCK;
285 break;
286 case sc18is603:
287 master->num_chipselect = 2;
288 if (pdata) {
289 hw->freq = pdata->clock_frequency;
290 } else {
291 const __be32 *val;
292 int len;
293
294 val = of_get_property(np, "clock-frequency", &len);
295 if (val && len >= sizeof(__be32))
296 hw->freq = be32_to_cpup(val);
297 }
298 if (!hw->freq)
299 hw->freq = SC18IS602_CLOCK;
300 break;
301 }
302 master->bus_num = np ? -1 : client->adapter->nr;
303 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
304 master->bits_per_word_mask = SPI_BPW_MASK(8);
305 master->setup = sc18is602_setup;
306 master->transfer_one_message = sc18is602_transfer_one;
307 master->dev.of_node = np;
308 master->min_speed_hz = hw->freq / 128;
309 master->max_speed_hz = hw->freq / 4;
310
311 error = devm_spi_register_master(dev, master);
312 if (error)
313 goto error_reg;
314
315 return 0;
316
317error_reg:
318 spi_master_put(master);
319 return error;
320}
321
322static const struct i2c_device_id sc18is602_id[] = {
323 { "sc18is602", sc18is602 },
324 { "sc18is602b", sc18is602b },
325 { "sc18is603", sc18is603 },
326 { }
327};
328MODULE_DEVICE_TABLE(i2c, sc18is602_id);
329
330static const struct of_device_id sc18is602_of_match[] = {
331 {
332 .compatible = "nxp,sc18is602",
333 .data = (void *)sc18is602
334 },
335 {
336 .compatible = "nxp,sc18is602b",
337 .data = (void *)sc18is602b
338 },
339 {
340 .compatible = "nxp,sc18is603",
341 .data = (void *)sc18is603
342 },
343 { },
344};
345MODULE_DEVICE_TABLE(of, sc18is602_of_match);
346
347static struct i2c_driver sc18is602_driver = {
348 .driver = {
349 .name = "sc18is602",
350 .of_match_table = of_match_ptr(sc18is602_of_match),
351 },
352 .probe = sc18is602_probe,
353 .id_table = sc18is602_id,
354};
355
356module_i2c_driver(sc18is602_driver);
357
358MODULE_DESCRIPTION("SC18IC602/603 SPI Master Driver");
359MODULE_AUTHOR("Guenter Roeck");
360MODULE_LICENSE("GPL");