1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2016 Synaptics Incorporated
4 * Copyright (c) 2011 Unixphere
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/spi/spi.h>
13 #include "rmi_driver.h"
15 #define RMI_SPI_DEFAULT_XFER_BUF_SIZE 64
17 #define RMI_PAGE_SELECT_REGISTER 0x00FF
18 #define RMI_SPI_PAGE(addr) (((addr) >> 8) & 0x80)
19 #define RMI_SPI_XFER_SIZE_LIMIT 255
21 #define BUFFER_SIZE_INCREMENT 32
26 RMI_SPI_V2_READ_UNIFIED
,
27 RMI_SPI_V2_READ_SPLIT
,
36 struct rmi_spi_xport
{
37 struct rmi_transport_dev xport
;
38 struct spi_device
*spi
;
40 struct mutex page_mutex
;
47 struct spi_transfer
*rx_xfers
;
48 struct spi_transfer
*tx_xfers
;
53 static int rmi_spi_manage_pools(struct rmi_spi_xport
*rmi_spi
, int len
)
55 struct spi_device
*spi
= rmi_spi
->spi
;
56 int buf_size
= rmi_spi
->xfer_buf_size
57 ? rmi_spi
->xfer_buf_size
: RMI_SPI_DEFAULT_XFER_BUF_SIZE
;
58 struct spi_transfer
*xfer_buf
;
62 while (buf_size
< len
)
65 if (buf_size
> RMI_SPI_XFER_SIZE_LIMIT
)
66 buf_size
= RMI_SPI_XFER_SIZE_LIMIT
;
68 tmp
= rmi_spi
->rx_buf
;
69 buf
= devm_kcalloc(&spi
->dev
, buf_size
, 2,
70 GFP_KERNEL
| GFP_DMA
);
74 rmi_spi
->rx_buf
= buf
;
75 rmi_spi
->tx_buf
= &rmi_spi
->rx_buf
[buf_size
];
76 rmi_spi
->xfer_buf_size
= buf_size
;
79 devm_kfree(&spi
->dev
, tmp
);
81 if (rmi_spi
->xport
.pdata
.spi_data
.read_delay_us
)
82 rmi_spi
->rx_xfer_count
= buf_size
;
84 rmi_spi
->rx_xfer_count
= 1;
86 if (rmi_spi
->xport
.pdata
.spi_data
.write_delay_us
)
87 rmi_spi
->tx_xfer_count
= buf_size
;
89 rmi_spi
->tx_xfer_count
= 1;
92 * Allocate a pool of spi_transfer buffers for devices which need
95 tmp
= rmi_spi
->rx_xfers
;
96 xfer_buf
= devm_kcalloc(&spi
->dev
,
97 rmi_spi
->rx_xfer_count
+ rmi_spi
->tx_xfer_count
,
98 sizeof(struct spi_transfer
),
103 rmi_spi
->rx_xfers
= xfer_buf
;
104 rmi_spi
->tx_xfers
= &xfer_buf
[rmi_spi
->rx_xfer_count
];
107 devm_kfree(&spi
->dev
, tmp
);
112 static int rmi_spi_xfer(struct rmi_spi_xport
*rmi_spi
,
113 const struct rmi_spi_cmd
*cmd
, const u8
*tx_buf
,
114 int tx_len
, u8
*rx_buf
, int rx_len
)
116 struct spi_device
*spi
= rmi_spi
->spi
;
117 struct rmi_device_platform_data_spi
*spi_data
=
118 &rmi_spi
->xport
.pdata
.spi_data
;
119 struct spi_message msg
;
120 struct spi_transfer
*xfer
;
126 u16 addr
= cmd
->addr
;
128 spi_message_init(&msg
);
135 case RMI_SPI_V2_READ_UNIFIED
:
136 case RMI_SPI_V2_READ_SPLIT
:
137 case RMI_SPI_V2_WRITE
:
142 total_tx_len
= cmd_len
+ tx_len
;
143 len
= max(total_tx_len
, rx_len
);
145 if (len
> RMI_SPI_XFER_SIZE_LIMIT
)
148 if (rmi_spi
->xfer_buf_size
< len
) {
149 ret
= rmi_spi_manage_pools(rmi_spi
, len
);
156 * SPI needs an address. Use 0x7FF if we want to keep
157 * reading from the last position of the register pointer.
163 rmi_spi
->tx_buf
[0] = (addr
>> 8);
164 rmi_spi
->tx_buf
[1] = addr
& 0xFF;
167 rmi_spi
->tx_buf
[0] = (addr
>> 8) | 0x80;
168 rmi_spi
->tx_buf
[1] = addr
& 0xFF;
170 case RMI_SPI_V2_READ_UNIFIED
:
172 case RMI_SPI_V2_READ_SPLIT
:
174 case RMI_SPI_V2_WRITE
:
175 rmi_spi
->tx_buf
[0] = 0x40;
176 rmi_spi
->tx_buf
[1] = (addr
>> 8) & 0xFF;
177 rmi_spi
->tx_buf
[2] = addr
& 0xFF;
178 rmi_spi
->tx_buf
[3] = tx_len
;
183 memcpy(&rmi_spi
->tx_buf
[cmd_len
], tx_buf
, tx_len
);
185 if (rmi_spi
->tx_xfer_count
> 1) {
186 for (i
= 0; i
< total_tx_len
; i
++) {
187 xfer
= &rmi_spi
->tx_xfers
[i
];
188 memset(xfer
, 0, sizeof(struct spi_transfer
));
189 xfer
->tx_buf
= &rmi_spi
->tx_buf
[i
];
191 xfer
->delay_usecs
= spi_data
->write_delay_us
;
192 spi_message_add_tail(xfer
, &msg
);
195 xfer
= rmi_spi
->tx_xfers
;
196 memset(xfer
, 0, sizeof(struct spi_transfer
));
197 xfer
->tx_buf
= rmi_spi
->tx_buf
;
198 xfer
->len
= total_tx_len
;
199 spi_message_add_tail(xfer
, &msg
);
202 rmi_dbg(RMI_DEBUG_XPORT
, &spi
->dev
, "%s: cmd: %s tx_buf len: %d tx_buf: %*ph\n",
203 __func__
, cmd
->op
== RMI_SPI_WRITE
? "WRITE" : "READ",
204 total_tx_len
, total_tx_len
, rmi_spi
->tx_buf
);
207 if (rmi_spi
->rx_xfer_count
> 1) {
208 for (i
= 0; i
< rx_len
; i
++) {
209 xfer
= &rmi_spi
->rx_xfers
[i
];
210 memset(xfer
, 0, sizeof(struct spi_transfer
));
211 xfer
->rx_buf
= &rmi_spi
->rx_buf
[i
];
213 xfer
->delay_usecs
= spi_data
->read_delay_us
;
214 spi_message_add_tail(xfer
, &msg
);
217 xfer
= rmi_spi
->rx_xfers
;
218 memset(xfer
, 0, sizeof(struct spi_transfer
));
219 xfer
->rx_buf
= rmi_spi
->rx_buf
;
221 spi_message_add_tail(xfer
, &msg
);
225 ret
= spi_sync(spi
, &msg
);
227 dev_err(&spi
->dev
, "spi xfer failed: %d\n", ret
);
232 memcpy(rx_buf
, rmi_spi
->rx_buf
, rx_len
);
233 rmi_dbg(RMI_DEBUG_XPORT
, &spi
->dev
, "%s: (%d) %*ph\n",
234 __func__
, rx_len
, rx_len
, rx_buf
);
241 * rmi_set_page - Set RMI page
242 * @xport: The pointer to the rmi_transport_dev struct
243 * @page: The new page address.
245 * RMI devices have 16-bit addressing, but some of the transport
246 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
247 * a page address at 0xff of every page so we can reliable page addresses
248 * every 256 registers.
250 * The page_mutex lock must be held when this function is entered.
252 * Returns zero on success, non-zero on failure.
254 static int rmi_set_page(struct rmi_spi_xport
*rmi_spi
, u8 page
)
256 struct rmi_spi_cmd cmd
;
259 cmd
.op
= RMI_SPI_WRITE
;
260 cmd
.addr
= RMI_PAGE_SELECT_REGISTER
;
262 ret
= rmi_spi_xfer(rmi_spi
, &cmd
, &page
, 1, NULL
, 0);
265 rmi_spi
->page
= page
;
270 static int rmi_spi_write_block(struct rmi_transport_dev
*xport
, u16 addr
,
271 const void *buf
, size_t len
)
273 struct rmi_spi_xport
*rmi_spi
=
274 container_of(xport
, struct rmi_spi_xport
, xport
);
275 struct rmi_spi_cmd cmd
;
278 mutex_lock(&rmi_spi
->page_mutex
);
280 if (RMI_SPI_PAGE(addr
) != rmi_spi
->page
) {
281 ret
= rmi_set_page(rmi_spi
, RMI_SPI_PAGE(addr
));
286 cmd
.op
= RMI_SPI_WRITE
;
289 ret
= rmi_spi_xfer(rmi_spi
, &cmd
, buf
, len
, NULL
, 0);
292 mutex_unlock(&rmi_spi
->page_mutex
);
296 static int rmi_spi_read_block(struct rmi_transport_dev
*xport
, u16 addr
,
297 void *buf
, size_t len
)
299 struct rmi_spi_xport
*rmi_spi
=
300 container_of(xport
, struct rmi_spi_xport
, xport
);
301 struct rmi_spi_cmd cmd
;
304 mutex_lock(&rmi_spi
->page_mutex
);
306 if (RMI_SPI_PAGE(addr
) != rmi_spi
->page
) {
307 ret
= rmi_set_page(rmi_spi
, RMI_SPI_PAGE(addr
));
312 cmd
.op
= RMI_SPI_READ
;
315 ret
= rmi_spi_xfer(rmi_spi
, &cmd
, NULL
, 0, buf
, len
);
318 mutex_unlock(&rmi_spi
->page_mutex
);
322 static const struct rmi_transport_ops rmi_spi_ops
= {
323 .write_block
= rmi_spi_write_block
,
324 .read_block
= rmi_spi_read_block
,
328 static int rmi_spi_of_probe(struct spi_device
*spi
,
329 struct rmi_device_platform_data
*pdata
)
331 struct device
*dev
= &spi
->dev
;
334 retval
= rmi_of_property_read_u32(dev
,
335 &pdata
->spi_data
.read_delay_us
,
336 "spi-rx-delay-us", 1);
340 retval
= rmi_of_property_read_u32(dev
,
341 &pdata
->spi_data
.write_delay_us
,
342 "spi-tx-delay-us", 1);
349 static const struct of_device_id rmi_spi_of_match
[] = {
350 { .compatible
= "syna,rmi4-spi" },
353 MODULE_DEVICE_TABLE(of
, rmi_spi_of_match
);
355 static inline int rmi_spi_of_probe(struct spi_device
*spi
,
356 struct rmi_device_platform_data
*pdata
)
362 static void rmi_spi_unregister_transport(void *data
)
364 struct rmi_spi_xport
*rmi_spi
= data
;
366 rmi_unregister_transport_device(&rmi_spi
->xport
);
369 static int rmi_spi_probe(struct spi_device
*spi
)
371 struct rmi_spi_xport
*rmi_spi
;
372 struct rmi_device_platform_data
*pdata
;
373 struct rmi_device_platform_data
*spi_pdata
= spi
->dev
.platform_data
;
376 if (spi
->master
->flags
& SPI_MASTER_HALF_DUPLEX
)
379 rmi_spi
= devm_kzalloc(&spi
->dev
, sizeof(struct rmi_spi_xport
),
384 pdata
= &rmi_spi
->xport
.pdata
;
386 if (spi
->dev
.of_node
) {
387 error
= rmi_spi_of_probe(spi
, pdata
);
390 } else if (spi_pdata
) {
394 if (pdata
->spi_data
.bits_per_word
)
395 spi
->bits_per_word
= pdata
->spi_data
.bits_per_word
;
397 if (pdata
->spi_data
.mode
)
398 spi
->mode
= pdata
->spi_data
.mode
;
400 error
= spi_setup(spi
);
402 dev_err(&spi
->dev
, "spi_setup failed!\n");
406 pdata
->irq
= spi
->irq
;
409 mutex_init(&rmi_spi
->page_mutex
);
411 rmi_spi
->xport
.dev
= &spi
->dev
;
412 rmi_spi
->xport
.proto_name
= "spi";
413 rmi_spi
->xport
.ops
= &rmi_spi_ops
;
415 spi_set_drvdata(spi
, rmi_spi
);
417 error
= rmi_spi_manage_pools(rmi_spi
, RMI_SPI_DEFAULT_XFER_BUF_SIZE
);
422 * Setting the page to zero will (a) make sure the PSR is in a
423 * known state, and (b) make sure we can talk to the device.
425 error
= rmi_set_page(rmi_spi
, 0);
427 dev_err(&spi
->dev
, "Failed to set page select to 0.\n");
431 dev_info(&spi
->dev
, "registering SPI-connected sensor\n");
433 error
= rmi_register_transport_device(&rmi_spi
->xport
);
435 dev_err(&spi
->dev
, "failed to register sensor: %d\n", error
);
439 error
= devm_add_action_or_reset(&spi
->dev
,
440 rmi_spi_unregister_transport
,
448 #ifdef CONFIG_PM_SLEEP
449 static int rmi_spi_suspend(struct device
*dev
)
451 struct spi_device
*spi
= to_spi_device(dev
);
452 struct rmi_spi_xport
*rmi_spi
= spi_get_drvdata(spi
);
455 ret
= rmi_driver_suspend(rmi_spi
->xport
.rmi_dev
, true);
457 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
462 static int rmi_spi_resume(struct device
*dev
)
464 struct spi_device
*spi
= to_spi_device(dev
);
465 struct rmi_spi_xport
*rmi_spi
= spi_get_drvdata(spi
);
468 ret
= rmi_driver_resume(rmi_spi
->xport
.rmi_dev
, true);
470 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
477 static int rmi_spi_runtime_suspend(struct device
*dev
)
479 struct spi_device
*spi
= to_spi_device(dev
);
480 struct rmi_spi_xport
*rmi_spi
= spi_get_drvdata(spi
);
483 ret
= rmi_driver_suspend(rmi_spi
->xport
.rmi_dev
, false);
485 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
490 static int rmi_spi_runtime_resume(struct device
*dev
)
492 struct spi_device
*spi
= to_spi_device(dev
);
493 struct rmi_spi_xport
*rmi_spi
= spi_get_drvdata(spi
);
496 ret
= rmi_driver_resume(rmi_spi
->xport
.rmi_dev
, false);
498 dev_warn(dev
, "Failed to resume device: %d\n", ret
);
504 static const struct dev_pm_ops rmi_spi_pm
= {
505 SET_SYSTEM_SLEEP_PM_OPS(rmi_spi_suspend
, rmi_spi_resume
)
506 SET_RUNTIME_PM_OPS(rmi_spi_runtime_suspend
, rmi_spi_runtime_resume
,
510 static const struct spi_device_id rmi_id
[] = {
514 MODULE_DEVICE_TABLE(spi
, rmi_id
);
516 static struct spi_driver rmi_spi_driver
= {
520 .of_match_table
= of_match_ptr(rmi_spi_of_match
),
523 .probe
= rmi_spi_probe
,
526 module_spi_driver(rmi_spi_driver
);
528 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>");
529 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
530 MODULE_DESCRIPTION("RMI SPI driver");
531 MODULE_LICENSE("GPL");