Linux Audio

Check our new training course

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