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