2 * NXP SC18IS602/603 SPI driver
4 * Copyright (C) Guenter Roeck <linux@roeck-us.net>
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.
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.
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_device.h>
26 #include <linux/platform_data/sc18is602.h>
27 #include <linux/gpio/consumer.h>
29 enum chips
{ sc18is602
, sc18is602b
, sc18is603
};
31 #define SC18IS602_BUFSIZ 200
32 #define SC18IS602_CLOCK 7372000
34 #define SC18IS602_MODE_CPHA BIT(2)
35 #define SC18IS602_MODE_CPOL BIT(3)
36 #define SC18IS602_MODE_LSB_FIRST BIT(5)
37 #define SC18IS602_MODE_CLOCK_DIV_4 0x0
38 #define SC18IS602_MODE_CLOCK_DIV_16 0x1
39 #define SC18IS602_MODE_CLOCK_DIV_64 0x2
40 #define SC18IS602_MODE_CLOCK_DIV_128 0x3
43 struct spi_master
*master
;
50 struct i2c_client
*client
;
52 u8 buffer
[SC18IS602_BUFSIZ
+ 1];
53 int tlen
; /* Data queued for tx in buffer */
54 int rindex
; /* Receive data index in buffer */
56 struct gpio_desc
*reset
;
59 static int sc18is602_wait_ready(struct sc18is602
*hw
, int len
)
62 int usecs
= 1000000 * len
/ hw
->speed
+ 1;
65 for (i
= 0; i
< 10; i
++) {
66 err
= i2c_master_recv(hw
->client
, dummy
, 1);
69 usleep_range(usecs
, usecs
* 2);
74 static int sc18is602_txrx(struct sc18is602
*hw
, struct spi_message
*msg
,
75 struct spi_transfer
*t
, bool do_transfer
)
77 unsigned int len
= t
->len
;
81 /* First byte (I2C command) is chip select */
82 hw
->buffer
[0] = 1 << msg
->spi
->chip_select
;
87 * We can not immediately send data to the chip, since each I2C message
88 * resembles a full SPI message (from CS active to CS inactive).
89 * Enqueue messages up to the first read or until do_transfer is true.
92 memcpy(&hw
->buffer
[hw
->tlen
], t
->tx_buf
, len
);
97 hw
->rindex
= hw
->tlen
- 1;
98 } else if (t
->rx_buf
) {
100 * For receive-only transfers we still need to perform a dummy
101 * write to receive data from the SPI chip.
102 * Read data starts at the end of transmit data (minus 1 to
105 hw
->rindex
= hw
->tlen
- 1;
106 memset(&hw
->buffer
[hw
->tlen
], 0, len
);
111 if (do_transfer
&& hw
->tlen
> 1) {
112 ret
= sc18is602_wait_ready(hw
, SC18IS602_BUFSIZ
);
115 ret
= i2c_master_send(hw
->client
, hw
->buffer
, hw
->tlen
);
122 int rlen
= hw
->rindex
+ len
;
124 ret
= sc18is602_wait_ready(hw
, hw
->tlen
);
127 ret
= i2c_master_recv(hw
->client
, hw
->buffer
, rlen
);
132 memcpy(t
->rx_buf
, &hw
->buffer
[hw
->rindex
], len
);
139 static int sc18is602_setup_transfer(struct sc18is602
*hw
, u32 hz
, u8 mode
)
145 ctrl
|= SC18IS602_MODE_CPHA
;
147 ctrl
|= SC18IS602_MODE_CPOL
;
148 if (mode
& SPI_LSB_FIRST
)
149 ctrl
|= SC18IS602_MODE_LSB_FIRST
;
151 /* Find the closest clock speed */
152 if (hz
>= hw
->freq
/ 4) {
153 ctrl
|= SC18IS602_MODE_CLOCK_DIV_4
;
154 hw
->speed
= hw
->freq
/ 4;
155 } else if (hz
>= hw
->freq
/ 16) {
156 ctrl
|= SC18IS602_MODE_CLOCK_DIV_16
;
157 hw
->speed
= hw
->freq
/ 16;
158 } else if (hz
>= hw
->freq
/ 64) {
159 ctrl
|= SC18IS602_MODE_CLOCK_DIV_64
;
160 hw
->speed
= hw
->freq
/ 64;
162 ctrl
|= SC18IS602_MODE_CLOCK_DIV_128
;
163 hw
->speed
= hw
->freq
/ 128;
167 * Don't do anything if the control value did not change. The initial
168 * value of 0xff for hw->ctrl ensures that the correct mode will be set
169 * with the first call to this function.
171 if (ctrl
== hw
->ctrl
)
174 ret
= i2c_smbus_write_byte_data(hw
->client
, 0xf0, ctrl
);
183 static int sc18is602_check_transfer(struct spi_device
*spi
,
184 struct spi_transfer
*t
, int tlen
)
186 if (t
&& t
->len
+ tlen
> SC18IS602_BUFSIZ
)
192 static int sc18is602_transfer_one(struct spi_master
*master
,
193 struct spi_message
*m
)
195 struct sc18is602
*hw
= spi_master_get_devdata(master
);
196 struct spi_device
*spi
= m
->spi
;
197 struct spi_transfer
*t
;
201 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
204 status
= sc18is602_check_transfer(spi
, t
, hw
->tlen
);
208 status
= sc18is602_setup_transfer(hw
, t
->speed_hz
, spi
->mode
);
212 do_transfer
= t
->cs_change
|| list_is_last(&t
->transfer_list
,
216 status
= sc18is602_txrx(hw
, m
, t
, do_transfer
);
219 m
->actual_length
+= status
;
224 udelay(t
->delay_usecs
);
227 spi_finalize_current_message(master
);
232 static int sc18is602_setup(struct spi_device
*spi
)
234 struct sc18is602
*hw
= spi_master_get_devdata(spi
->master
);
236 /* SC18IS602 does not support CS2 */
237 if (hw
->id
== sc18is602
&& spi
->chip_select
== 2)
243 static int sc18is602_probe(struct i2c_client
*client
,
244 const struct i2c_device_id
*id
)
246 struct device
*dev
= &client
->dev
;
247 struct device_node
*np
= dev
->of_node
;
248 struct sc18is602_platform_data
*pdata
= dev_get_platdata(dev
);
249 struct sc18is602
*hw
;
250 struct spi_master
*master
;
253 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
|
254 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
257 master
= spi_alloc_master(dev
, sizeof(struct sc18is602
));
261 hw
= spi_master_get_devdata(master
);
262 i2c_set_clientdata(client
, hw
);
264 /* assert reset and then release */
265 hw
->reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
266 if (IS_ERR(hw
->reset
))
267 return PTR_ERR(hw
->reset
);
268 gpiod_set_value_cansleep(hw
->reset
, 0);
275 if (client
->dev
.of_node
)
276 hw
->id
= (enum chips
)of_device_get_match_data(&client
->dev
);
278 hw
->id
= id
->driver_data
;
283 master
->num_chipselect
= 4;
284 hw
->freq
= SC18IS602_CLOCK
;
287 master
->num_chipselect
= 2;
289 hw
->freq
= pdata
->clock_frequency
;
294 val
= of_get_property(np
, "clock-frequency", &len
);
295 if (val
&& len
>= sizeof(__be32
))
296 hw
->freq
= be32_to_cpup(val
);
299 hw
->freq
= SC18IS602_CLOCK
;
302 master
->bus_num
= np
? -1 : client
->adapter
->nr
;
303 master
->mode_bits
= SPI_CPHA
| SPI_CPOL
| SPI_LSB_FIRST
;
304 master
->bits_per_word_mask
= SPI_BPW_MASK(8);
305 master
->setup
= sc18is602_setup
;
306 master
->transfer_one_message
= sc18is602_transfer_one
;
307 master
->dev
.of_node
= np
;
308 master
->min_speed_hz
= hw
->freq
/ 128;
309 master
->max_speed_hz
= hw
->freq
/ 4;
311 error
= devm_spi_register_master(dev
, master
);
318 spi_master_put(master
);
322 static const struct i2c_device_id sc18is602_id
[] = {
323 { "sc18is602", sc18is602
},
324 { "sc18is602b", sc18is602b
},
325 { "sc18is603", sc18is603
},
328 MODULE_DEVICE_TABLE(i2c
, sc18is602_id
);
330 static const struct of_device_id sc18is602_of_match
[] = {
332 .compatible
= "nxp,sc18is602",
333 .data
= (void *)sc18is602
336 .compatible
= "nxp,sc18is602b",
337 .data
= (void *)sc18is602b
340 .compatible
= "nxp,sc18is603",
341 .data
= (void *)sc18is603
345 MODULE_DEVICE_TABLE(of
, sc18is602_of_match
);
347 static struct i2c_driver sc18is602_driver
= {
350 .of_match_table
= of_match_ptr(sc18is602_of_match
),
352 .probe
= sc18is602_probe
,
353 .id_table
= sc18is602_id
,
356 module_i2c_driver(sc18is602_driver
);
358 MODULE_DESCRIPTION("SC18IC602/603 SPI Master Driver");
359 MODULE_AUTHOR("Guenter Roeck");
360 MODULE_LICENSE("GPL");