40 #include <linux/module.h>
42 #include <linux/tty.h>
43 #include <linux/device.h>
48 #include <linux/serial.h>
51 #include <linux/rfkill.h>
56 #include <linux/sched.h>
57 #include <linux/time.h>
58 #include <linux/wait.h>
66 #define IFX_SPI_MORE_MASK 0x10
67 #define IFX_SPI_MORE_BIT 12
68 #define IFX_SPI_CTS_BIT 13
69 #define IFX_SPI_MODE SPI_MODE_1
70 #define IFX_SPI_TTY_ID 0
71 #define IFX_SPI_TIMEOUT_SEC 2
72 #define IFX_SPI_HEADER_0 (-1)
73 #define IFX_SPI_HEADER_F (-2)
79 static int spi_bpw = 16;
125 pm_runtime_get(&ifx_dev->
spi_dev->dev);
128 spin_unlock_irqrestore(&ifx_dev->
power_lock, flags);
139 ifx_spi_power_state_clear(
struct ifx_spi_device *ifx_dev,
unsigned char val)
148 pm_runtime_put(&ifx_dev->
spi_dev->dev);
151 spin_unlock_irqrestore(&ifx_dev->
power_lock, flags);
162 static inline void swap_buf(
u16 *
buf,
int len,
void *
end)
166 len = ((len + 1) >> 1);
167 if ((
void *)&buf[len] >
end) {
168 pr_err(
"swap_buf: swap exceeds boundary (%p > %p)!",
172 for (n = 0; n < len; n++) {
200 mrdy_set_high(ifx_dev);
227 static void ifx_spi_timeout(
unsigned long arg)
232 ifx_spi_ttyhangup(ifx_dev);
233 mrdy_set_low(ifx_dev);
247 static int ifx_spi_tiocmget(
struct tty_struct *tty)
273 static int ifx_spi_tiocmset(
struct tty_struct *tty,
274 unsigned int set,
unsigned int clear)
301 static int ifx_spi_open(
struct tty_struct *tty,
struct file *filp)
314 static void ifx_spi_close(
struct tty_struct *tty,
struct file *filp)
334 static int ifx_spi_decode_spi_header(
unsigned char *
buffer,
int *
length,
335 unsigned char *more,
unsigned char *received_cts)
339 u16 *in_buffer = (
u16 *)buffer;
344 if (h1 == 0 && h2 == 0) {
347 }
else if (h1 == 0xffff && h2 == 0xffff) {
352 *length = h1 & 0xfff;
368 static void ifx_spi_setup_spi_header(
unsigned char *txbuffer,
int tx_count,
371 *(
u16 *)(txbuffer) = tx_count;
407 static int ifx_spi_prepare_tx_buffer(
struct ifx_spi_device *ifx_dev)
429 if (queue_length != 0) {
433 tx_buffer, temp_count,
437 tx_buffer += temp_count;
438 tx_count += temp_count;
439 if (temp_count == queue_length)
441 ifx_spi_wakeup_serial(ifx_dev);
448 ifx_spi_setup_spi_header(ifx_dev->
tx_buffer,
467 static int ifx_spi_write(
struct tty_struct *tty,
const unsigned char *buf,
471 unsigned char *tmp_buf = (
unsigned char *)buf;
474 mrdy_assert(ifx_dev);
485 static int ifx_spi_write_room(
struct tty_struct *tty)
498 static int ifx_spi_chars_in_buffer(
struct tty_struct *tty)
512 static void ifx_spi_hangup(
struct tty_struct *tty)
554 mrdy_set_low(ifx_dev);
560 .activate = ifx_port_activate,
561 .shutdown = ifx_port_shutdown,
565 .open = ifx_spi_open,
566 .close = ifx_spi_close,
567 .write = ifx_spi_write,
568 .hangup = ifx_spi_hangup,
569 .write_room = ifx_spi_write_room,
570 .chars_in_buffer = ifx_spi_chars_in_buffer,
571 .tiocmget = ifx_spi_tiocmget,
572 .tiocmset = ifx_spi_tiocmset,
584 static void ifx_spi_insert_flip_string(
struct ifx_spi_device *ifx_dev,
585 unsigned char *chars,
size_t size)
590 tty_insert_flip_string(tty, chars, size);
602 static void ifx_spi_complete(
void *
ctx)
611 int local_write_pending = 0;
616 mrdy_set_low(ifx_dev);
618 if (!ifx_dev->
spi_msg.status) {
622 decode_result = ifx_spi_decode_spi_header(ifx_dev->
rx_buffer,
623 &length, &more, &cts);
626 "ignore input: invalid header 0");
631 "ignore input: invalid header F");
637 actual_length =
min((
unsigned int)length,
638 ifx_dev->
spi_msg.actual_length);
642 ifx_spi_insert_flip_string(
645 (
size_t)actual_length);
654 local_write_pending = 1;
668 if (more || ifx_dev->
spi_more || queue_length > 0 ||
669 local_write_pending) {
672 mrdy_assert(ifx_dev);
674 mrdy_assert(ifx_dev);
681 ifx_spi_power_state_clear(ifx_dev,
687 ldisc->
ops->write_wakeup(tty);
703 static void ifx_spi_io(
unsigned long data)
709 if (ifx_dev->
gpio.unack_srdy_int_nb > 0)
710 ifx_dev->
gpio.unack_srdy_int_nb--;
712 ifx_spi_prepare_tx_buffer(ifx_dev);
714 spi_message_init(&ifx_dev->
spi_msg);
715 INIT_LIST_HEAD(&ifx_dev->
spi_msg.queue);
717 ifx_dev->
spi_msg.context = ifx_dev;
718 ifx_dev->
spi_msg.complete = ifx_spi_complete;
726 ifx_dev->
spi_xfer.bits_per_word = spi_bpw;
735 ifx_dev->
spi_msg.is_dma_mapped = 1;
741 ifx_dev->
spi_msg.is_dma_mapped = 0;
753 mrdy_assert(ifx_dev);
801 pport->
ops = &ifx_tty_port_ops;
805 if (IS_ERR(ifx_dev->
tty_dev)) {
807 "%s: registering tty device failed", __func__);
808 ret = PTR_ERR(ifx_dev->
tty_dev);
814 ifx_spi_free_port(ifx_dev);
851 ifx_dev->
gpio.unack_srdy_int_nb++;
852 ifx_spi_handle_srdy(ifx_dev);
878 ifx_spi_ttyhangup(ifx_dev);
899 ifx_spi_free_port(ifx_dev);
966 dev_dbg(&spi->
dev,
"ignoring subsequent detection");
979 dev_err(&spi->
dev,
"spi device allocation failed");
982 saved_ifx_dev = ifx_dev;
989 ifx_dev->
spi_timer.function = ifx_spi_timeout;
1000 dev_err(&spi->
dev,
"SPI setup wasn't successful %d", ret);
1014 dev_err(&spi->
dev,
"DMA-TX buffer allocation failed");
1023 dev_err(&spi->
dev,
"DMA-RX buffer allocation failed");
1031 spi_set_drvdata(spi, ifx_dev);
1033 (
unsigned long)ifx_dev);
1038 ret = ifx_spi_create_port(ifx_dev);
1040 dev_err(&spi->
dev,
"create default tty port failed");
1046 ifx_dev->
gpio.mrdy = pl_data->
mrdy;
1047 ifx_dev->
gpio.srdy = pl_data->
srdy;
1051 ifx_dev->
gpio.reset, ifx_dev->
gpio.po, ifx_dev->
gpio.mrdy,
1052 ifx_dev->
gpio.srdy, ifx_dev->
gpio.reset_out);
1057 dev_err(&spi->
dev,
"Unable to allocate GPIO%d (RESET)",
1058 ifx_dev->
gpio.reset);
1062 ret += gpio_export(ifx_dev->
gpio.reset, 1);
1064 dev_err(&spi->
dev,
"Unable to configure GPIO%d (RESET)",
1065 ifx_dev->
gpio.reset);
1072 ret += gpio_export(ifx_dev->
gpio.po, 1);
1074 dev_err(&spi->
dev,
"Unable to configure GPIO%d (ON)",
1082 dev_err(&spi->
dev,
"Unable to allocate GPIO%d (MRDY)",
1083 ifx_dev->
gpio.mrdy);
1086 ret += gpio_export(ifx_dev->
gpio.mrdy, 1);
1089 dev_err(&spi->
dev,
"Unable to configure GPIO%d (MRDY)",
1090 ifx_dev->
gpio.mrdy);
1097 dev_err(&spi->
dev,
"Unable to allocate GPIO%d (SRDY)",
1098 ifx_dev->
gpio.srdy);
1102 ret += gpio_export(ifx_dev->
gpio.srdy, 1);
1105 dev_err(&spi->
dev,
"Unable to configure GPIO%d (SRDY)",
1106 ifx_dev->
gpio.srdy);
1113 dev_err(&spi->
dev,
"Unable to allocate GPIO%d (RESET_OUT)",
1114 ifx_dev->
gpio.reset_out);
1117 ret += gpio_export(ifx_dev->
gpio.reset_out, 1);
1120 dev_err(&spi->
dev,
"Unable to configure GPIO%d (RESET_OUT)",
1121 ifx_dev->
gpio.reset_out);
1127 ifx_spi_reset_interrupt,
1136 ret = ifx_spi_reset(ifx_dev);
1139 ifx_spi_srdy_interrupt,
1149 pm_runtime_set_active(&spi->
dev);
1159 mrdy_assert(ifx_dev);
1160 ifx_spi_handle_srdy(ifx_dev);
1162 mrdy_set_low(ifx_dev);
1178 ifx_spi_free_device(ifx_dev);
1179 saved_ifx_dev =
NULL;
1191 static int ifx_spi_spi_remove(
struct spi_device *spi)
1207 ifx_spi_free_device(ifx_dev);
1209 saved_ifx_dev =
NULL;
1220 static void ifx_spi_spi_shutdown(
struct spi_device *spi)
1248 static int ifx_spi_spi_resume(
struct spi_device *spi)
1260 static int ifx_spi_pm_suspend(
struct device *dev)
1273 static int ifx_spi_pm_resume(
struct device *dev)
1284 static int ifx_spi_pm_runtime_resume(
struct device *dev)
1296 static int ifx_spi_pm_runtime_suspend(
struct device *dev)
1307 static int ifx_spi_pm_runtime_idle(
struct device *dev)
1313 pm_runtime_suspend(dev);
1318 static const struct dev_pm_ops ifx_spi_pm = {
1319 .resume = ifx_spi_pm_resume,
1320 .suspend = ifx_spi_pm_suspend,
1321 .runtime_resume = ifx_spi_pm_runtime_resume,
1322 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1323 .runtime_idle = ifx_spi_pm_runtime_idle
1339 .probe = ifx_spi_spi_probe,
1340 .shutdown = ifx_spi_spi_shutdown,
1342 .suspend = ifx_spi_spi_suspend,
1343 .resume = ifx_spi_spi_resume,
1344 .id_table = ifx_id_table
1353 static void __exit ifx_spi_exit(
void)
1357 spi_unregister_driver((
void *)&ifx_spi_driver);
1368 static int __init ifx_spi_init(
void)
1372 tty_drv = alloc_tty_driver(1);
1390 pr_err(
"%s: tty_register_driver failed(%d)",
1398 pr_err(
"%s: spi_register_driver failed(%d)",