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>
15 #include <linux/of_device.h>
17 #include <linux/platform_data/sc18is602.h>
18 #include <linux/gpio/consumer.h>
20 enum chips { sc18is602, sc18is602b, sc18is603 };
22 #define SC18IS602_BUFSIZ 200
23 #define SC18IS602_CLOCK 7372000
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
34 struct spi_master *master;
41 struct i2c_client *client;
43 u8 buffer[SC18IS602_BUFSIZ + 1];
44 int tlen; /* Data queued for tx in buffer */
45 int rindex; /* Receive data index in buffer */
47 struct gpio_desc *reset;
50 static int sc18is602_wait_ready(struct sc18is602 *hw, int len)
53 int usecs = 1000000 * len / hw->speed + 1;
56 for (i = 0; i < 10; i++) {
57 err = i2c_master_recv(hw->client, dummy, 1);
60 usleep_range(usecs, usecs * 2);
65 static int sc18is602_txrx(struct sc18is602 *hw, struct spi_message *msg,
66 struct spi_transfer *t, bool do_transfer)
68 unsigned int len = t->len;
72 /* First byte (I2C command) is chip select */
73 hw->buffer[0] = 1 << msg->spi->chip_select;
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.
83 memcpy(&hw->buffer[hw->tlen], t->tx_buf, len);
88 hw->rindex = hw->tlen - 1;
89 } else if (t->rx_buf) {
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
96 hw->rindex = hw->tlen - 1;
97 memset(&hw->buffer[hw->tlen], 0, len);
102 if (do_transfer && hw->tlen > 1) {
103 ret = sc18is602_wait_ready(hw, SC18IS602_BUFSIZ);
106 ret = i2c_master_send(hw->client, hw->buffer, hw->tlen);
113 int rlen = hw->rindex + len;
115 ret = sc18is602_wait_ready(hw, hw->tlen);
118 ret = i2c_master_recv(hw->client, hw->buffer, rlen);
123 memcpy(t->rx_buf, &hw->buffer[hw->rindex], len);
130 static int sc18is602_setup_transfer(struct sc18is602 *hw, u32 hz, u8 mode)
136 ctrl |= SC18IS602_MODE_CPHA;
138 ctrl |= SC18IS602_MODE_CPOL;
139 if (mode & SPI_LSB_FIRST)
140 ctrl |= SC18IS602_MODE_LSB_FIRST;
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;
153 ctrl |= SC18IS602_MODE_CLOCK_DIV_128;
154 hw->speed = hw->freq / 128;
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.
162 if (ctrl == hw->ctrl)
165 ret = i2c_smbus_write_byte_data(hw->client, 0xf0, ctrl);
174 static int sc18is602_check_transfer(struct spi_device *spi,
175 struct spi_transfer *t, int tlen)
177 if (t && t->len + tlen > SC18IS602_BUFSIZ + 1)
183 static int sc18is602_transfer_one(struct spi_master *master,
184 struct spi_message *m)
186 struct sc18is602 *hw = spi_master_get_devdata(master);
187 struct spi_device *spi = m->spi;
188 struct spi_transfer *t;
192 list_for_each_entry(t, &m->transfers, transfer_list) {
195 status = sc18is602_check_transfer(spi, t, hw->tlen);
199 status = sc18is602_setup_transfer(hw, t->speed_hz, spi->mode);
203 do_transfer = t->cs_change || list_is_last(&t->transfer_list,
207 status = sc18is602_txrx(hw, m, t, do_transfer);
210 m->actual_length += status;
214 spi_transfer_delay_exec(t);
217 spi_finalize_current_message(master);
222 static size_t sc18is602_max_transfer_size(struct spi_device *spi)
224 return SC18IS602_BUFSIZ;
227 static int sc18is602_setup(struct spi_device *spi)
229 struct sc18is602 *hw = spi_master_get_devdata(spi->master);
231 /* SC18IS602 does not support CS2 */
232 if (hw->id == sc18is602 && spi->chip_select == 2)
238 static int sc18is602_probe(struct i2c_client *client,
239 const struct i2c_device_id *id)
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;
247 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
248 I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
251 master = devm_spi_alloc_master(dev, sizeof(struct sc18is602));
255 hw = spi_master_get_devdata(master);
256 i2c_set_clientdata(client, hw);
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);
269 if (client->dev.of_node)
270 hw->id = (enum chips)of_device_get_match_data(&client->dev);
272 hw->id = id->driver_data;
277 master->num_chipselect = 4;
278 hw->freq = SC18IS602_CLOCK;
281 master->num_chipselect = 2;
283 hw->freq = pdata->clock_frequency;
288 val = of_get_property(np, "clock-frequency", &len);
289 if (val && len >= sizeof(__be32))
290 hw->freq = be32_to_cpup(val);
293 hw->freq = SC18IS602_CLOCK;
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;
307 return devm_spi_register_master(dev, master);
310 static const struct i2c_device_id sc18is602_id[] = {
311 { "sc18is602", sc18is602 },
312 { "sc18is602b", sc18is602b },
313 { "sc18is603", sc18is603 },
316 MODULE_DEVICE_TABLE(i2c, sc18is602_id);
318 static const struct of_device_id sc18is602_of_match[] = {
320 .compatible = "nxp,sc18is602",
321 .data = (void *)sc18is602
324 .compatible = "nxp,sc18is602b",
325 .data = (void *)sc18is602b
328 .compatible = "nxp,sc18is603",
329 .data = (void *)sc18is603
333 MODULE_DEVICE_TABLE(of, sc18is602_of_match);
335 static struct i2c_driver sc18is602_driver = {
338 .of_match_table = of_match_ptr(sc18is602_of_match),
340 .probe = sc18is602_probe,
341 .id_table = sc18is602_id,
344 module_i2c_driver(sc18is602_driver);
346 MODULE_DESCRIPTION("SC18IS602/603 SPI Master Driver");
347 MODULE_AUTHOR("Guenter Roeck");
348 MODULE_LICENSE("GPL");