1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NXP SC18IS602/603 SPI driver
5 * Copyright (C) Guenter Roeck <linux@roeck-us.net>
8 #include <linux/kernel.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>
16 #include <linux/platform_data/sc18is602.h>
17 #include <linux/gpio/consumer.h>
19 enum chips { sc18is602, sc18is602b, sc18is603 };
21 #define SC18IS602_BUFSIZ 200
22 #define SC18IS602_CLOCK 7372000
24 #define SC18IS602_MODE_CPHA BIT(2)
25 #define SC18IS602_MODE_CPOL BIT(3)
26 #define SC18IS602_MODE_LSB_FIRST BIT(5)
27 #define SC18IS602_MODE_CLOCK_DIV_4 0x0
28 #define SC18IS602_MODE_CLOCK_DIV_16 0x1
29 #define SC18IS602_MODE_CLOCK_DIV_64 0x2
30 #define SC18IS602_MODE_CLOCK_DIV_128 0x3
33 struct spi_controller *host;
40 struct i2c_client *client;
42 u8 buffer[SC18IS602_BUFSIZ + 1];
43 int tlen; /* Data queued for tx in buffer */
44 int rindex; /* Receive data index in buffer */
46 struct gpio_desc *reset;
49 static int sc18is602_wait_ready(struct sc18is602 *hw, int len)
52 int usecs = 1000000 * len / hw->speed + 1;
55 for (i = 0; i < 10; i++) {
56 err = i2c_master_recv(hw->client, dummy, 1);
59 usleep_range(usecs, usecs * 2);
64 static int sc18is602_txrx(struct sc18is602 *hw, struct spi_message *msg,
65 struct spi_transfer *t, bool do_transfer)
67 unsigned int len = t->len;
71 /* First byte (I2C command) is chip select */
72 hw->buffer[0] = 1 << spi_get_chipselect(msg->spi, 0);
77 * We can not immediately send data to the chip, since each I2C message
78 * resembles a full SPI message (from CS active to CS inactive).
79 * Enqueue messages up to the first read or until do_transfer is true.
82 memcpy(&hw->buffer[hw->tlen], t->tx_buf, len);
87 hw->rindex = hw->tlen - 1;
88 } else if (t->rx_buf) {
90 * For receive-only transfers we still need to perform a dummy
91 * write to receive data from the SPI chip.
92 * Read data starts at the end of transmit data (minus 1 to
95 hw->rindex = hw->tlen - 1;
96 memset(&hw->buffer[hw->tlen], 0, len);
101 if (do_transfer && hw->tlen > 1) {
102 ret = sc18is602_wait_ready(hw, SC18IS602_BUFSIZ);
105 ret = i2c_master_send(hw->client, hw->buffer, hw->tlen);
112 int rlen = hw->rindex + len;
114 ret = sc18is602_wait_ready(hw, hw->tlen);
117 ret = i2c_master_recv(hw->client, hw->buffer, rlen);
122 memcpy(t->rx_buf, &hw->buffer[hw->rindex], len);
129 static int sc18is602_setup_transfer(struct sc18is602 *hw, u32 hz, u8 mode)
135 ctrl |= SC18IS602_MODE_CPHA;
137 ctrl |= SC18IS602_MODE_CPOL;
138 if (mode & SPI_LSB_FIRST)
139 ctrl |= SC18IS602_MODE_LSB_FIRST;
141 /* Find the closest clock speed */
142 if (hz >= hw->freq / 4) {
143 ctrl |= SC18IS602_MODE_CLOCK_DIV_4;
144 hw->speed = hw->freq / 4;
145 } else if (hz >= hw->freq / 16) {
146 ctrl |= SC18IS602_MODE_CLOCK_DIV_16;
147 hw->speed = hw->freq / 16;
148 } else if (hz >= hw->freq / 64) {
149 ctrl |= SC18IS602_MODE_CLOCK_DIV_64;
150 hw->speed = hw->freq / 64;
152 ctrl |= SC18IS602_MODE_CLOCK_DIV_128;
153 hw->speed = hw->freq / 128;
157 * Don't do anything if the control value did not change. The initial
158 * value of 0xff for hw->ctrl ensures that the correct mode will be set
159 * with the first call to this function.
161 if (ctrl == hw->ctrl)
164 ret = i2c_smbus_write_byte_data(hw->client, 0xf0, ctrl);
173 static int sc18is602_check_transfer(struct spi_device *spi,
174 struct spi_transfer *t, int tlen)
176 if (t && t->len + tlen > SC18IS602_BUFSIZ + 1)
182 static int sc18is602_transfer_one(struct spi_controller *host,
183 struct spi_message *m)
185 struct sc18is602 *hw = spi_controller_get_devdata(host);
186 struct spi_device *spi = m->spi;
187 struct spi_transfer *t;
191 list_for_each_entry(t, &m->transfers, transfer_list) {
194 status = sc18is602_check_transfer(spi, t, hw->tlen);
198 status = sc18is602_setup_transfer(hw, t->speed_hz, spi->mode);
202 do_transfer = t->cs_change || list_is_last(&t->transfer_list,
206 status = sc18is602_txrx(hw, m, t, do_transfer);
209 m->actual_length += status;
213 spi_transfer_delay_exec(t);
216 spi_finalize_current_message(host);
221 static size_t sc18is602_max_transfer_size(struct spi_device *spi)
223 return SC18IS602_BUFSIZ;
226 static int sc18is602_setup(struct spi_device *spi)
228 struct sc18is602 *hw = spi_controller_get_devdata(spi->controller);
230 /* SC18IS602 does not support CS2 */
231 if (hw->id == sc18is602 && (spi_get_chipselect(spi, 0) == 2))
237 static int sc18is602_probe(struct i2c_client *client)
239 const struct i2c_device_id *id = i2c_client_get_device_id(client);
240 struct device *dev = &client->dev;
241 struct device_node *np = dev->of_node;
242 struct sc18is602_platform_data *pdata = dev_get_platdata(dev);
243 struct sc18is602 *hw;
244 struct spi_controller *host;
246 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
247 I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
250 host = devm_spi_alloc_host(dev, sizeof(struct sc18is602));
254 hw = spi_controller_get_devdata(host);
255 i2c_set_clientdata(client, hw);
257 /* assert reset and then release */
258 hw->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
259 if (IS_ERR(hw->reset))
260 return PTR_ERR(hw->reset);
261 gpiod_set_value_cansleep(hw->reset, 0);
268 if (client->dev.of_node)
269 hw->id = (uintptr_t)of_device_get_match_data(&client->dev);
271 hw->id = id->driver_data;
276 host->num_chipselect = 4;
277 hw->freq = SC18IS602_CLOCK;
280 host->num_chipselect = 2;
282 hw->freq = pdata->clock_frequency;
287 val = of_get_property(np, "clock-frequency", &len);
288 if (val && len >= sizeof(__be32))
289 hw->freq = be32_to_cpup(val);
292 hw->freq = SC18IS602_CLOCK;
295 host->bus_num = np ? -1 : client->adapter->nr;
296 host->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
297 host->bits_per_word_mask = SPI_BPW_MASK(8);
298 host->setup = sc18is602_setup;
299 host->transfer_one_message = sc18is602_transfer_one;
300 host->max_transfer_size = sc18is602_max_transfer_size;
301 host->max_message_size = sc18is602_max_transfer_size;
302 host->dev.of_node = np;
303 host->min_speed_hz = hw->freq / 128;
304 host->max_speed_hz = hw->freq / 4;
306 return devm_spi_register_controller(dev, host);
309 static const struct i2c_device_id sc18is602_id[] = {
310 { "sc18is602", sc18is602 },
311 { "sc18is602b", sc18is602b },
312 { "sc18is603", sc18is603 },
315 MODULE_DEVICE_TABLE(i2c, sc18is602_id);
317 static const struct of_device_id sc18is602_of_match[] __maybe_unused = {
319 .compatible = "nxp,sc18is602",
320 .data = (void *)sc18is602
323 .compatible = "nxp,sc18is602b",
324 .data = (void *)sc18is602b
327 .compatible = "nxp,sc18is603",
328 .data = (void *)sc18is603
332 MODULE_DEVICE_TABLE(of, sc18is602_of_match);
334 static struct i2c_driver sc18is602_driver = {
337 .of_match_table = of_match_ptr(sc18is602_of_match),
339 .probe = sc18is602_probe,
340 .id_table = sc18is602_id,
343 module_i2c_driver(sc18is602_driver);
345 MODULE_DESCRIPTION("SC18IS602/603 SPI Host Driver");
346 MODULE_AUTHOR("Guenter Roeck");
347 MODULE_LICENSE("GPL");