File Index
Files and directories
Sign in
FILE

spi-sc18is602.c

File preview with syntax highlighting.

linux-6.19.14/drivers/spi/spi-sc18is602.c C
  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/mod_devicetable.h>
 11#include <linux/module.h>
 12#include <linux/spi/spi.h>
 13#include <linux/i2c.h>
 14#include <linux/delay.h>
 15#include <linux/pm_runtime.h>
 16#include <linux/platform_data/sc18is602.h>
 17#include <linux/property.h>
 18
 19#include <linux/gpio/consumer.h>
 20
 21enum chips { sc18is602, sc18is602b, sc18is603 };
 22
 23#define SC18IS602_BUFSIZ		200
 24#define SC18IS602_CLOCK			7372000
 25
 26#define SC18IS602_MODE_CPHA		BIT(2)
 27#define SC18IS602_MODE_CPOL		BIT(3)
 28#define SC18IS602_MODE_LSB_FIRST	BIT(5)
 29#define SC18IS602_MODE_CLOCK_DIV_4	0x0
 30#define SC18IS602_MODE_CLOCK_DIV_16	0x1
 31#define SC18IS602_MODE_CLOCK_DIV_64	0x2
 32#define SC18IS602_MODE_CLOCK_DIV_128	0x3
 33
 34struct sc18is602 {
 35	struct spi_controller	*host;
 36	struct device		*dev;
 37	u8			ctrl;
 38	u32			freq;
 39	u32			speed;
 40
 41	/* I2C data */
 42	struct i2c_client	*client;
 43	enum chips		id;
 44	u8			buffer[SC18IS602_BUFSIZ + 1];
 45	int			tlen;	/* Data queued for tx in buffer */
 46	int			rindex;	/* Receive data index in buffer */
 47
 48	struct gpio_desc	*reset;
 49};
 50
 51static int sc18is602_wait_ready(struct sc18is602 *hw, int len)
 52{
 53	int i, err;
 54	int usecs = 1000000 * len / hw->speed + 1;
 55	u8 dummy[1];
 56
 57	for (i = 0; i < 10; i++) {
 58		err = i2c_master_recv(hw->client, dummy, 1);
 59		if (err >= 0)
 60			return 0;
 61		usleep_range(usecs, usecs * 2);
 62	}
 63	return -ETIMEDOUT;
 64}
 65
 66static int sc18is602_txrx(struct sc18is602 *hw, struct spi_message *msg,
 67			  struct spi_transfer *t, bool do_transfer)
 68{
 69	unsigned int len = t->len;
 70	int ret;
 71
 72	if (hw->tlen == 0) {
 73		/* First byte (I2C command) is chip select */
 74		hw->buffer[0] = 1 << spi_get_chipselect(msg->spi, 0);
 75		hw->tlen = 1;
 76		hw->rindex = 0;
 77	}
 78	/*
 79	 * We can not immediately send data to the chip, since each I2C message
 80	 * resembles a full SPI message (from CS active to CS inactive).
 81	 * Enqueue messages up to the first read or until do_transfer is true.
 82	 */
 83	if (t->tx_buf) {
 84		memcpy(&hw->buffer[hw->tlen], t->tx_buf, len);
 85		hw->tlen += len;
 86		if (t->rx_buf)
 87			do_transfer = true;
 88		else
 89			hw->rindex = hw->tlen - 1;
 90	} else if (t->rx_buf) {
 91		/*
 92		 * For receive-only transfers we still need to perform a dummy
 93		 * write to receive data from the SPI chip.
 94		 * Read data starts at the end of transmit data (minus 1 to
 95		 * account for CS).
 96		 */
 97		hw->rindex = hw->tlen - 1;
 98		memset(&hw->buffer[hw->tlen], 0, len);
 99		hw->tlen += len;
100		do_transfer = true;
101	}
102
103	if (do_transfer && hw->tlen > 1) {
104		ret = sc18is602_wait_ready(hw, SC18IS602_BUFSIZ);
105		if (ret < 0)
106			return ret;
107		ret = i2c_master_send(hw->client, hw->buffer, hw->tlen);
108		if (ret < 0)
109			return ret;
110		if (ret != hw->tlen)
111			return -EIO;
112
113		if (t->rx_buf) {
114			int rlen = hw->rindex + len;
115
116			ret = sc18is602_wait_ready(hw, hw->tlen);
117			if (ret < 0)
118				return ret;
119			ret = i2c_master_recv(hw->client, hw->buffer, rlen);
120			if (ret < 0)
121				return ret;
122			if (ret != rlen)
123				return -EIO;
124			memcpy(t->rx_buf, &hw->buffer[hw->rindex], len);
125		}
126		hw->tlen = 0;
127	}
128	return len;
129}
130
131static int sc18is602_setup_transfer(struct sc18is602 *hw, u32 hz, u8 mode)
132{
133	u8 ctrl = 0;
134	int ret;
135
136	if (mode & SPI_CPHA)
137		ctrl |= SC18IS602_MODE_CPHA;
138	if (mode & SPI_CPOL)
139		ctrl |= SC18IS602_MODE_CPOL;
140	if (mode & SPI_LSB_FIRST)
141		ctrl |= SC18IS602_MODE_LSB_FIRST;
142
143	/* Find the closest clock speed */
144	if (hz >= hw->freq / 4) {
145		ctrl |= SC18IS602_MODE_CLOCK_DIV_4;
146		hw->speed = hw->freq / 4;
147	} else if (hz >= hw->freq / 16) {
148		ctrl |= SC18IS602_MODE_CLOCK_DIV_16;
149		hw->speed = hw->freq / 16;
150	} else if (hz >= hw->freq / 64) {
151		ctrl |= SC18IS602_MODE_CLOCK_DIV_64;
152		hw->speed = hw->freq / 64;
153	} else {
154		ctrl |= SC18IS602_MODE_CLOCK_DIV_128;
155		hw->speed = hw->freq / 128;
156	}
157
158	/*
159	 * Don't do anything if the control value did not change. The initial
160	 * value of 0xff for hw->ctrl ensures that the correct mode will be set
161	 * with the first call to this function.
162	 */
163	if (ctrl == hw->ctrl)
164		return 0;
165
166	ret = i2c_smbus_write_byte_data(hw->client, 0xf0, ctrl);
167	if (ret < 0)
168		return ret;
169
170	hw->ctrl = ctrl;
171
172	return 0;
173}
174
175static int sc18is602_check_transfer(struct spi_device *spi,
176				    struct spi_transfer *t, int tlen)
177{
178	if (t && t->len + tlen > SC18IS602_BUFSIZ + 1)
179		return -EINVAL;
180
181	return 0;
182}
183
184static int sc18is602_transfer_one(struct spi_controller *host,
185				  struct spi_message *m)
186{
187	struct sc18is602 *hw = spi_controller_get_devdata(host);
188	struct spi_device *spi = m->spi;
189	struct spi_transfer *t;
190	int status = 0;
191
192	hw->tlen = 0;
193	list_for_each_entry(t, &m->transfers, transfer_list) {
194		bool do_transfer;
195
196		status = sc18is602_check_transfer(spi, t, hw->tlen);
197		if (status < 0)
198			break;
199
200		status = sc18is602_setup_transfer(hw, t->speed_hz, spi->mode);
201		if (status < 0)
202			break;
203
204		do_transfer = t->cs_change || list_is_last(&t->transfer_list,
205							   &m->transfers);
206
207		if (t->len) {
208			status = sc18is602_txrx(hw, m, t, do_transfer);
209			if (status < 0)
210				break;
211			m->actual_length += status;
212		}
213		status = 0;
214
215		spi_transfer_delay_exec(t);
216	}
217	m->status = status;
218	spi_finalize_current_message(host);
219
220	return status;
221}
222
223static size_t sc18is602_max_transfer_size(struct spi_device *spi)
224{
225	return SC18IS602_BUFSIZ;
226}
227
228static int sc18is602_setup(struct spi_device *spi)
229{
230	struct sc18is602 *hw = spi_controller_get_devdata(spi->controller);
231
232	/* SC18IS602 does not support CS2 */
233	if (hw->id == sc18is602 && (spi_get_chipselect(spi, 0) == 2))
234		return -ENXIO;
235
236	return 0;
237}
238
239static int sc18is602_probe(struct i2c_client *client)
240{
241	struct device *dev = &client->dev;
242	struct sc18is602_platform_data *pdata = dev_get_platdata(dev);
243	struct sc18is602 *hw;
244	struct spi_controller *host;
245
246	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
247				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
248		return -EINVAL;
249
250	host = devm_spi_alloc_host(dev, sizeof(struct sc18is602));
251	if (!host)
252		return -ENOMEM;
253
254	device_set_node(&host->dev, dev_fwnode(dev));
255
256	hw = spi_controller_get_devdata(host);
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->host = host;
265	hw->client = client;
266	hw->dev = dev;
267	hw->ctrl = 0xff;
268
269	hw->id = (uintptr_t)i2c_get_match_data(client);
270	switch (hw->id) {
271	case sc18is602:
272	case sc18is602b:
273		host->num_chipselect = 4;
274		hw->freq = SC18IS602_CLOCK;
275		break;
276	case sc18is603:
277		host->num_chipselect = 2;
278		if (pdata)
279			hw->freq = pdata->clock_frequency;
280		else
281			device_property_read_u32(dev, "clock-frequency", &hw->freq);
282		if (!hw->freq)
283			hw->freq = SC18IS602_CLOCK;
284		break;
285	}
286	host->bus_num = dev_fwnode(dev) ? -1 : client->adapter->nr;
287	host->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
288	host->bits_per_word_mask = SPI_BPW_MASK(8);
289	host->setup = sc18is602_setup;
290	host->transfer_one_message = sc18is602_transfer_one;
291	host->max_transfer_size = sc18is602_max_transfer_size;
292	host->max_message_size = sc18is602_max_transfer_size;
293	host->min_speed_hz = hw->freq / 128;
294	host->max_speed_hz = hw->freq / 4;
295
296	return devm_spi_register_controller(dev, host);
297}
298
299static const struct i2c_device_id sc18is602_id[] = {
300	{ "sc18is602", sc18is602 },
301	{ "sc18is602b", sc18is602b },
302	{ "sc18is603", sc18is603 },
303	{ }
304};
305MODULE_DEVICE_TABLE(i2c, sc18is602_id);
306
307static const struct of_device_id sc18is602_of_match[] = {
308	{
309		.compatible = "nxp,sc18is602",
310		.data = (void *)sc18is602
311	},
312	{
313		.compatible = "nxp,sc18is602b",
314		.data = (void *)sc18is602b
315	},
316	{
317		.compatible = "nxp,sc18is603",
318		.data = (void *)sc18is603
319	},
320	{ },
321};
322MODULE_DEVICE_TABLE(of, sc18is602_of_match);
323
324static struct i2c_driver sc18is602_driver = {
325	.driver = {
326		.name = "sc18is602",
327		.of_match_table = sc18is602_of_match,
328	},
329	.probe = sc18is602_probe,
330	.id_table = sc18is602_id,
331};
332
333module_i2c_driver(sc18is602_driver);
334
335MODULE_DESCRIPTION("SC18IS602/603 SPI Host Driver");
336MODULE_AUTHOR("Guenter Roeck");
337MODULE_LICENSE("GPL");
~/ Read-only file preview.