Lines Matching +full:spi +full:- +full:tx +full:- +full:delay +full:- +full:us
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2016 Synaptics Incorporated
11 #include <linux/spi/spi.h>
38 struct spi_device *spi; member
55 struct spi_device *spi = rmi_spi->spi; in rmi_spi_manage_pools() local
56 int buf_size = rmi_spi->xfer_buf_size in rmi_spi_manage_pools()
57 ? rmi_spi->xfer_buf_size : RMI_SPI_DEFAULT_XFER_BUF_SIZE; in rmi_spi_manage_pools()
68 tmp = rmi_spi->rx_buf; in rmi_spi_manage_pools()
69 buf = devm_kcalloc(&spi->dev, buf_size, 2, in rmi_spi_manage_pools()
72 return -ENOMEM; in rmi_spi_manage_pools()
74 rmi_spi->rx_buf = buf; in rmi_spi_manage_pools()
75 rmi_spi->tx_buf = &rmi_spi->rx_buf[buf_size]; in rmi_spi_manage_pools()
76 rmi_spi->xfer_buf_size = buf_size; in rmi_spi_manage_pools()
79 devm_kfree(&spi->dev, tmp); in rmi_spi_manage_pools()
81 if (rmi_spi->xport.pdata.spi_data.read_delay_us) in rmi_spi_manage_pools()
82 rmi_spi->rx_xfer_count = buf_size; in rmi_spi_manage_pools()
84 rmi_spi->rx_xfer_count = 1; in rmi_spi_manage_pools()
86 if (rmi_spi->xport.pdata.spi_data.write_delay_us) in rmi_spi_manage_pools()
87 rmi_spi->tx_xfer_count = buf_size; in rmi_spi_manage_pools()
89 rmi_spi->tx_xfer_count = 1; in rmi_spi_manage_pools()
95 tmp = rmi_spi->rx_xfers; in rmi_spi_manage_pools()
96 xfer_buf = devm_kcalloc(&spi->dev, in rmi_spi_manage_pools()
97 rmi_spi->rx_xfer_count + rmi_spi->tx_xfer_count, in rmi_spi_manage_pools()
101 return -ENOMEM; in rmi_spi_manage_pools()
103 rmi_spi->rx_xfers = xfer_buf; in rmi_spi_manage_pools()
104 rmi_spi->tx_xfers = &xfer_buf[rmi_spi->rx_xfer_count]; in rmi_spi_manage_pools()
107 devm_kfree(&spi->dev, tmp); in rmi_spi_manage_pools()
116 struct spi_device *spi = rmi_spi->spi; in rmi_spi_xfer() local
118 &rmi_spi->xport.pdata.spi_data; in rmi_spi_xfer()
126 u16 addr = cmd->addr; in rmi_spi_xfer()
130 switch (cmd->op) { in rmi_spi_xfer()
146 return -EINVAL; in rmi_spi_xfer()
148 if (rmi_spi->xfer_buf_size < len) { in rmi_spi_xfer()
156 * SPI needs an address. Use 0x7FF if we want to keep in rmi_spi_xfer()
161 switch (cmd->op) { in rmi_spi_xfer()
163 rmi_spi->tx_buf[0] = (addr >> 8); in rmi_spi_xfer()
164 rmi_spi->tx_buf[1] = addr & 0xFF; in rmi_spi_xfer()
167 rmi_spi->tx_buf[0] = (addr >> 8) | 0x80; in rmi_spi_xfer()
168 rmi_spi->tx_buf[1] = addr & 0xFF; in rmi_spi_xfer()
175 rmi_spi->tx_buf[0] = 0x40; in rmi_spi_xfer()
176 rmi_spi->tx_buf[1] = (addr >> 8) & 0xFF; in rmi_spi_xfer()
177 rmi_spi->tx_buf[2] = addr & 0xFF; in rmi_spi_xfer()
178 rmi_spi->tx_buf[3] = tx_len; in rmi_spi_xfer()
183 memcpy(&rmi_spi->tx_buf[cmd_len], tx_buf, tx_len); in rmi_spi_xfer()
185 if (rmi_spi->tx_xfer_count > 1) { in rmi_spi_xfer()
187 xfer = &rmi_spi->tx_xfers[i]; in rmi_spi_xfer()
189 xfer->tx_buf = &rmi_spi->tx_buf[i]; in rmi_spi_xfer()
190 xfer->len = 1; in rmi_spi_xfer()
191 xfer->delay.value = spi_data->write_delay_us; in rmi_spi_xfer()
192 xfer->delay.unit = SPI_DELAY_UNIT_USECS; in rmi_spi_xfer()
196 xfer = rmi_spi->tx_xfers; in rmi_spi_xfer()
198 xfer->tx_buf = rmi_spi->tx_buf; in rmi_spi_xfer()
199 xfer->len = total_tx_len; in rmi_spi_xfer()
203 rmi_dbg(RMI_DEBUG_XPORT, &spi->dev, "%s: cmd: %s tx_buf len: %d tx_buf: %*ph\n", in rmi_spi_xfer()
204 __func__, cmd->op == RMI_SPI_WRITE ? "WRITE" : "READ", in rmi_spi_xfer()
205 total_tx_len, total_tx_len, rmi_spi->tx_buf); in rmi_spi_xfer()
208 if (rmi_spi->rx_xfer_count > 1) { in rmi_spi_xfer()
210 xfer = &rmi_spi->rx_xfers[i]; in rmi_spi_xfer()
212 xfer->rx_buf = &rmi_spi->rx_buf[i]; in rmi_spi_xfer()
213 xfer->len = 1; in rmi_spi_xfer()
214 xfer->delay.value = spi_data->read_delay_us; in rmi_spi_xfer()
215 xfer->delay.unit = SPI_DELAY_UNIT_USECS; in rmi_spi_xfer()
219 xfer = rmi_spi->rx_xfers; in rmi_spi_xfer()
221 xfer->rx_buf = rmi_spi->rx_buf; in rmi_spi_xfer()
222 xfer->len = rx_len; in rmi_spi_xfer()
227 ret = spi_sync(spi, &msg); in rmi_spi_xfer()
229 dev_err(&spi->dev, "spi xfer failed: %d\n", ret); in rmi_spi_xfer()
234 memcpy(rx_buf, rmi_spi->rx_buf, rx_len); in rmi_spi_xfer()
235 rmi_dbg(RMI_DEBUG_XPORT, &spi->dev, "%s: (%d) %*ph\n", in rmi_spi_xfer()
243 * rmi_set_page - Set RMI page
247 * RMI devices have 16-bit addressing, but some of the transport
248 * implementations (like SMBus) only have 8-bit addressing. So RMI implements
254 * Returns zero on success, non-zero on failure.
267 rmi_spi->page = page; in rmi_set_page()
280 mutex_lock(&rmi_spi->page_mutex); in rmi_spi_write_block()
282 if (RMI_SPI_PAGE(addr) != rmi_spi->page) { in rmi_spi_write_block()
294 mutex_unlock(&rmi_spi->page_mutex); in rmi_spi_write_block()
306 mutex_lock(&rmi_spi->page_mutex); in rmi_spi_read_block()
308 if (RMI_SPI_PAGE(addr) != rmi_spi->page) { in rmi_spi_read_block()
320 mutex_unlock(&rmi_spi->page_mutex); in rmi_spi_read_block()
330 static int rmi_spi_of_probe(struct spi_device *spi, in rmi_spi_of_probe() argument
333 struct device *dev = &spi->dev; in rmi_spi_of_probe()
337 &pdata->spi_data.read_delay_us, in rmi_spi_of_probe()
338 "spi-rx-delay-us", 1); in rmi_spi_of_probe()
343 &pdata->spi_data.write_delay_us, in rmi_spi_of_probe()
344 "spi-tx-delay-us", 1); in rmi_spi_of_probe()
352 { .compatible = "syna,rmi4-spi" },
357 static inline int rmi_spi_of_probe(struct spi_device *spi, in rmi_spi_of_probe() argument
360 return -ENODEV; in rmi_spi_of_probe()
368 rmi_unregister_transport_device(&rmi_spi->xport); in rmi_spi_unregister_transport()
371 static int rmi_spi_probe(struct spi_device *spi) in rmi_spi_probe() argument
375 struct rmi_device_platform_data *spi_pdata = spi->dev.platform_data; in rmi_spi_probe()
378 if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) in rmi_spi_probe()
379 return -EINVAL; in rmi_spi_probe()
381 rmi_spi = devm_kzalloc(&spi->dev, sizeof(struct rmi_spi_xport), in rmi_spi_probe()
384 return -ENOMEM; in rmi_spi_probe()
386 pdata = &rmi_spi->xport.pdata; in rmi_spi_probe()
388 if (spi->dev.of_node) { in rmi_spi_probe()
389 error = rmi_spi_of_probe(spi, pdata); in rmi_spi_probe()
396 if (pdata->spi_data.bits_per_word) in rmi_spi_probe()
397 spi->bits_per_word = pdata->spi_data.bits_per_word; in rmi_spi_probe()
399 if (pdata->spi_data.mode) in rmi_spi_probe()
400 spi->mode = pdata->spi_data.mode; in rmi_spi_probe()
402 error = spi_setup(spi); in rmi_spi_probe()
404 dev_err(&spi->dev, "spi_setup failed!\n"); in rmi_spi_probe()
408 pdata->irq = spi->irq; in rmi_spi_probe()
410 rmi_spi->spi = spi; in rmi_spi_probe()
411 mutex_init(&rmi_spi->page_mutex); in rmi_spi_probe()
413 rmi_spi->xport.dev = &spi->dev; in rmi_spi_probe()
414 rmi_spi->xport.proto_name = "spi"; in rmi_spi_probe()
415 rmi_spi->xport.ops = &rmi_spi_ops; in rmi_spi_probe()
417 spi_set_drvdata(spi, rmi_spi); in rmi_spi_probe()
429 dev_err(&spi->dev, "Failed to set page select to 0.\n"); in rmi_spi_probe()
433 dev_info(&spi->dev, "registering SPI-connected sensor\n"); in rmi_spi_probe()
435 error = rmi_register_transport_device(&rmi_spi->xport); in rmi_spi_probe()
437 dev_err(&spi->dev, "failed to register sensor: %d\n", error); in rmi_spi_probe()
441 error = devm_add_action_or_reset(&spi->dev, in rmi_spi_probe()
452 struct spi_device *spi = to_spi_device(dev); in rmi_spi_suspend() local
453 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi); in rmi_spi_suspend()
456 ret = rmi_driver_suspend(rmi_spi->xport.rmi_dev, true); in rmi_spi_suspend()
465 struct spi_device *spi = to_spi_device(dev); in rmi_spi_resume() local
466 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi); in rmi_spi_resume()
469 ret = rmi_driver_resume(rmi_spi->xport.rmi_dev, true); in rmi_spi_resume()
478 struct spi_device *spi = to_spi_device(dev); in rmi_spi_runtime_suspend() local
479 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi); in rmi_spi_runtime_suspend()
482 ret = rmi_driver_suspend(rmi_spi->xport.rmi_dev, false); in rmi_spi_runtime_suspend()
491 struct spi_device *spi = to_spi_device(dev); in rmi_spi_runtime_resume() local
492 struct rmi_spi_xport *rmi_spi = spi_get_drvdata(spi); in rmi_spi_runtime_resume()
495 ret = rmi_driver_resume(rmi_spi->xport.rmi_dev, false); in rmi_spi_runtime_resume()
508 { "rmi4-spi", 0 },
511 MODULE_DEVICE_TABLE(spi, rmi_id);
527 MODULE_DESCRIPTION("RMI SPI driver");