2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/rmi.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
16 #include "rmi_driver.h"
18 #define RMI_SPI_DEFAULT_XFER_BUF_SIZE 64
20 #define RMI_PAGE_SELECT_REGISTER 0x00FF
21 #define RMI_SPI_PAGE(addr) (((addr) >> 8) & 0x80)
22 #define RMI_SPI_XFER_SIZE_LIMIT 255
24 #define BUFFER_SIZE_INCREMENT 32
29 RMI_SPI_V2_READ_UNIFIED,
30 RMI_SPI_V2_READ_SPLIT,
39 struct rmi_spi_xport {
40 struct rmi_transport_dev xport;
41 struct spi_device *spi;
43 struct mutex page_mutex;
50 struct spi_transfer *rx_xfers;
51 struct spi_transfer *tx_xfers;
56 static int rmi_spi_manage_pools(struct rmi_spi_xport *rmi_spi, int len)
58 struct spi_device *spi = rmi_spi->spi;
59 int buf_size = rmi_spi->xfer_buf_size
60 ? rmi_spi->xfer_buf_size : RMI_SPI_DEFAULT_XFER_BUF_SIZE;
61 struct spi_transfer *xfer_buf;
65 while (buf_size < len)
68 if (buf_size > RMI_SPI_XFER_SIZE_LIMIT)
69 buf_size = RMI_SPI_XFER_SIZE_LIMIT;
71 tmp = rmi_spi->rx_buf;
72 buf = devm_kzalloc(&spi->dev, buf_size * 2,
73 GFP_KERNEL | GFP_DMA);
77 rmi_spi->rx_buf = buf;
78 rmi_spi->tx_buf = &rmi_spi->rx_buf[buf_size];
79 rmi_spi->xfer_buf_size = buf_size;
82 devm_kfree(&spi->dev, tmp);
84 if (rmi_spi->xport.pdata.spi_data.read_delay_us)
85 rmi_spi->rx_xfer_count = buf_size;
87 rmi_spi->rx_xfer_count = 1;
89 if (rmi_spi->xport.pdata.spi_data.write_delay_us)
90 rmi_spi->tx_xfer_count = buf_size;
92 rmi_spi->tx_xfer_count = 1;
95 * Allocate a pool of spi_transfer buffers for devices which need
98 tmp = rmi_spi->rx_xfers;
99 xfer_buf = devm_kzalloc(&spi->dev,
100 (rmi_spi->rx_xfer_count + rmi_spi->tx_xfer_count)
101 * sizeof(struct spi_transfer), GFP_KERNEL);
105 rmi_spi->rx_xfers = xfer_buf;
106 rmi_spi->tx_xfers = &xfer_buf[rmi_spi->rx_xfer_count];
109 devm_kfree(&spi->dev, tmp);
114 static int rmi_spi_xfer(struct rmi_spi_xport *rmi_spi,
115 const struct rmi_spi_cmd *cmd, const u8 *tx_buf,
116 int tx_len, u8 *rx_buf, int rx_len)
118 struct spi_device *spi = rmi_spi->spi;
119 struct rmi_device_platform_data_spi *spi_data =
120 &rmi_spi->xport.pdata.spi_data;
121 struct spi_message msg;
122 struct spi_transfer *xfer;
128 u16 addr = cmd->addr;
130 spi_message_init(&msg);
137 case RMI_SPI_V2_READ_UNIFIED:
138 case RMI_SPI_V2_READ_SPLIT:
139 case RMI_SPI_V2_WRITE:
144 total_tx_len = cmd_len + tx_len;
145 len = max(total_tx_len, rx_len);
147 if (len > RMI_SPI_XFER_SIZE_LIMIT)
150 if (rmi_spi->xfer_buf_size < len) {
151 ret = rmi_spi_manage_pools(rmi_spi, len);
158 * SPI needs an address. Use 0x7FF if we want to keep
159 * reading from the last position of the register pointer.
165 rmi_spi->tx_buf[0] = (addr >> 8);
166 rmi_spi->tx_buf[1] = addr & 0xFF;
169 rmi_spi->tx_buf[0] = (addr >> 8) | 0x80;
170 rmi_spi->tx_buf[1] = addr & 0xFF;
172 case RMI_SPI_V2_READ_UNIFIED:
174 case RMI_SPI_V2_READ_SPLIT:
176 case RMI_SPI_V2_WRITE:
177 rmi_spi->tx_buf[0] = 0x40;
178 rmi_spi->tx_buf[1] = (addr >> 8) & 0xFF;
179 rmi_spi->tx_buf[2] = addr & 0xFF;
180 rmi_spi->tx_buf[3] = tx_len;
185 memcpy(&rmi_spi->tx_buf[cmd_len], tx_buf, tx_len);
187 if (rmi_spi->tx_xfer_count > 1) {
188 for (i = 0; i < total_tx_len; i++) {
189 xfer = &rmi_spi->tx_xfers[i];
190 memset(xfer, 0, sizeof(struct spi_transfer));
191 xfer->tx_buf = &rmi_spi->tx_buf[i];
193 xfer->delay_usecs = spi_data->write_delay_us;
194 spi_message_add_tail(xfer, &msg);
197 xfer = rmi_spi->tx_xfers;
198 memset(xfer, 0, sizeof(struct spi_transfer));
199 xfer->tx_buf = rmi_spi->tx_buf;
200 xfer->len = total_tx_len;
201 spi_message_add_tail(xfer, &msg);
204 rmi_dbg(RMI_DEBUG_XPORT, &spi->dev, "%s: cmd: %s tx_buf len: %d tx_buf: %*ph\n",
205 __func__, cmd->op == RMI_SPI_WRITE ? "WRITE" : "READ",
206 total_tx_len, total_tx_len, rmi_spi->tx_buf);
209 if (rmi_spi->rx_xfer_count > 1) {
210 for (i = 0; i < rx_len; i++) {
211 xfer = &rmi_spi->rx_xfers[i];
212 memset(xfer, 0, sizeof(struct spi_transfer));
213 xfer->rx_buf = &rmi_spi->rx_buf[i];
215 xfer->delay_usecs = spi_data->read_delay_us;
216 spi_message_add_tail(xfer, &msg);
219 xfer = rmi_spi->rx_xfers;
220 memset(xfer, 0, sizeof(struct spi_transfer));
221 xfer->rx_buf = rmi_spi->rx_buf;
223 spi_message_add_tail(xfer, &msg);
227 ret = spi_sync(spi, &msg);
229 dev_err(&spi->dev, "spi xfer failed: %d\n", ret);
234 memcpy(rx_buf, rmi_spi->rx_buf, rx_len);
235 rmi_dbg(RMI_DEBUG_XPORT, &spi->dev, "%s: (%d) %*ph\n",
236 __func__, rx_len, rx_len, rx_buf);
243 * rmi_set_page - Set RMI page
244 * @xport: The pointer to the rmi_transport_dev struct
245 * @page: The new page address.
247 * RMI devices have 16-bit addressing, but some of the transport
248 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
249 * a page address at 0xff of every page so we can reliable page addresses
250 * every 256 registers.
252 * The page_mutex lock must be held when this function is entered.
254 * Returns zero on success, non-zero on failure.
256 static int rmi_set_page(struct rmi_spi_xport *rmi_spi, u8 page)
258 struct rmi_spi_cmd cmd;
261 cmd.op = RMI_SPI_WRITE;
262 cmd.addr = RMI_PAGE_SELECT_REGISTER;
264 ret = rmi_spi_xfer(rmi_spi, &cmd, &page, 1, NULL, 0);
267 rmi_spi->page = page;
272 static int rmi_spi_write_block(struct rmi_transport_dev *xport, u16 addr,
273 const void *buf, size_t len)
275 struct rmi_spi_xport *rmi_spi =
276 container_of(xport, struct rmi_spi_xport, xport);
277 struct rmi_spi_cmd cmd;
280 mutex_lock(&rmi_spi->page_mutex);
282 if (RMI_SPI_PAGE(addr) != rmi_spi->page) {
283 ret = rmi_set_page(rmi_spi, RMI_SPI_PAGE(addr));
288 cmd.op = RMI_SPI_WRITE;
291 ret = rmi_spi_xfer(rmi_spi, &cmd, buf, len, NULL, 0);
294 mutex_unlock(&rmi_spi->page_mutex);
298 static int rmi_spi_read_block(struct rmi_transport_dev *xport, u16 addr,
299 void *buf, size_t len)
301 struct rmi_spi_xport *rmi_spi =
302 container_of(xport, struct rmi_spi_xport, xport);
303 struct rmi_spi_cmd cmd;
306 mutex_lock(&rmi_spi->page_mutex);
308 if (RMI_SPI_PAGE(addr) != rmi_spi->page) {
309 ret = rmi_set_page(rmi_spi, RMI_SPI_PAGE(addr));
314 cmd.op = RMI_SPI_READ;
317 ret = rmi_spi_xfer(rmi_spi, &cmd, NULL, 0, buf, len);
320 mutex_unlock(&rmi_spi->page_mutex);
324 static const struct rmi_transport_ops rmi_spi_ops = {
325 .write_block = rmi_spi_write_block,
326 .read_block = rmi_spi_read_block,
330 static int rmi_spi_of_probe(struct spi_device *spi,
331 struct rmi_device_platform_data *pdata)
333 struct device *dev = &spi->dev;
336 retval = rmi_of_property_read_u32(dev,
337 &pdata->spi_data.read_delay_us,
338 "spi-rx-delay-us", 1);
342 retval = rmi_of_property_read_u32(dev,
343 &pdata->spi_data.write_delay_us,
344 "spi-tx-delay-us", 1);
351 static const struct of_device_id rmi_spi_of_match[] = {
352 { .compatible = "syna,rmi4-spi" },
355 MODULE_DEVICE_TABLE(of, rmi_spi_of_match);
357 static inline int rmi_spi_of_probe(struct spi_device *spi,
358 struct rmi_device_platform_data *pdata)
364 static void rmi_spi_unregister_transport(void *data)
366 struct rmi_spi_xport *rmi_spi = data;
368 rmi_unregister_transport_device(&rmi_spi->xport);
371 static int rmi_spi_probe(struct spi_device *spi)
373 struct rmi_spi_xport *rmi_spi;
374 struct rmi_device_platform_data *pdata;
375 struct rmi_device_platform_data *spi_pdata = spi->dev.platform_data;
378 if (spi->master->flags & SPI_MASTER_HALF_DUPLEX)
381 rmi_spi = devm_kzalloc(&spi->dev, sizeof(struct rmi_spi_xport),
386 pdata = &rmi_spi->xport.pdata;
388 if (spi->dev.of_node) {
389 error = rmi_spi_of_probe(spi, pdata);
392 } else if (spi_pdata) {
396 if (pdata->spi_data.bits_per_word)
397 spi->bits_per_word = pdata->spi_data.bits_per_word;
399 if (pdata->spi_data.mode)
400 spi->mode = pdata->spi_data.mode;
402 error = spi_setup(spi);
404 dev_err(&spi->dev, "spi_setup failed!\n");
408 pdata->irq = spi->irq;
411 mutex_init(&rmi_spi->page_mutex);
413 rmi_spi->xport.dev = &spi->dev;
414 rmi_spi->xport.proto_name = "spi";
415 rmi_spi->xport.ops = &rmi_spi_ops;
417 spi_set_drvdata(spi, rmi_spi);
419 error = rmi_spi_manage_pools(rmi_spi, RMI_SPI_DEFAULT_XFER_BUF_SIZE);
424 * Setting the page to zero will (a) make sure the PSR is in a
425 * known state, and (b) make sure we can talk to the device.
427 error = rmi_set_page(rmi_spi, 0);
429 dev_err(&spi->dev, "Failed to set page select to 0.\n");
433 dev_info(&spi->dev, "registering SPI-connected sensor\n");
435 error = rmi_register_transport_device(&rmi_spi->xport);
437 dev_err(&spi->dev, "failed to register sensor: %d\n", error);
441 error = devm_add_action_or_reset(&spi->dev,
442 rmi_spi_unregister_transport,
450 #ifdef CONFIG_PM_SLEEP
451 static int rmi_spi_suspend(struct device *dev)
453 struct spi_device *spi = to_spi_device(dev);
454 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
457 ret = rmi_driver_suspend(rmi_spi->xport.rmi_dev, true);
459 dev_warn(dev, "Failed to resume device: %d\n", ret);
464 static int rmi_spi_resume(struct device *dev)
466 struct spi_device *spi = to_spi_device(dev);
467 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
470 ret = rmi_driver_resume(rmi_spi->xport.rmi_dev, true);
472 dev_warn(dev, "Failed to resume device: %d\n", ret);
479 static int rmi_spi_runtime_suspend(struct device *dev)
481 struct spi_device *spi = to_spi_device(dev);
482 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
485 ret = rmi_driver_suspend(rmi_spi->xport.rmi_dev, false);
487 dev_warn(dev, "Failed to resume device: %d\n", ret);
492 static int rmi_spi_runtime_resume(struct device *dev)
494 struct spi_device *spi = to_spi_device(dev);
495 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi);
498 ret = rmi_driver_resume(rmi_spi->xport.rmi_dev, false);
500 dev_warn(dev, "Failed to resume device: %d\n", ret);
506 static const struct dev_pm_ops rmi_spi_pm = {
507 SET_SYSTEM_SLEEP_PM_OPS(rmi_spi_suspend, rmi_spi_resume)
508 SET_RUNTIME_PM_OPS(rmi_spi_runtime_suspend, rmi_spi_runtime_resume,
512 static const struct spi_device_id rmi_id[] = {
516 MODULE_DEVICE_TABLE(spi, rmi_id);
518 static struct spi_driver rmi_spi_driver = {
522 .of_match_table = of_match_ptr(rmi_spi_of_match),
525 .probe = rmi_spi_probe,
528 module_spi_driver(rmi_spi_driver);
530 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>");
531 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
532 MODULE_DESCRIPTION("RMI SPI driver");
533 MODULE_LICENSE("GPL");
534 MODULE_VERSION(RMI_DRIVER_VERSION);