2 * ----------------------------------------------------------------------------
3 * drivers/nfc/st95hf/spi.c function definitions for SPI communication
4 * ----------------------------------------------------------------------------
5 * Copyright (C) 2015 STMicroelectronics Pvt. Ltd. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 /* Function to send user provided buffer to ST95HF through SPI */
23 int st95hf_spi_send(struct st95hf_spi_context
*spicontext
,
24 unsigned char *buffertx
,
26 enum req_type reqtype
)
30 struct spi_device
*spidev
= spicontext
->spidev
;
31 struct spi_transfer tx_transfer
= {
36 mutex_lock(&spicontext
->spi_lock
);
38 if (reqtype
== SYNC
) {
39 spicontext
->req_issync
= true;
40 reinit_completion(&spicontext
->done
);
42 spicontext
->req_issync
= false;
46 spi_message_add_tail(&tx_transfer
, &m
);
48 result
= spi_sync(spidev
, &m
);
50 dev_err(&spidev
->dev
, "error: sending cmd to st95hf using SPI = %d\n",
52 mutex_unlock(&spicontext
->spi_lock
);
56 /* return for asynchronous or no-wait case */
57 if (reqtype
== ASYNC
) {
58 mutex_unlock(&spicontext
->spi_lock
);
62 result
= wait_for_completion_timeout(&spicontext
->done
,
63 msecs_to_jiffies(1000));
64 /* check for timeout or success */
66 dev_err(&spidev
->dev
, "error: response not ready timeout\n");
72 mutex_unlock(&spicontext
->spi_lock
);
76 EXPORT_SYMBOL_GPL(st95hf_spi_send
);
78 /* Function to Receive command Response */
79 int st95hf_spi_recv_response(struct st95hf_spi_context
*spicontext
,
80 unsigned char *receivebuff
)
83 struct spi_transfer tx_takedata
;
85 struct spi_device
*spidev
= spicontext
->spidev
;
86 unsigned char readdata_cmd
= ST95HF_COMMAND_RECEIVE
;
87 struct spi_transfer t
[2] = {
88 {.tx_buf
= &readdata_cmd
, .len
= 1,},
89 {.rx_buf
= receivebuff
, .len
= 2, .cs_change
= 1,},
94 memset(&tx_takedata
, 0x0, sizeof(struct spi_transfer
));
96 mutex_lock(&spicontext
->spi_lock
);
98 /* First spi transfer to know the length of valid data */
100 spi_message_add_tail(&t
[0], &m
);
101 spi_message_add_tail(&t
[1], &m
);
103 ret
= spi_sync(spidev
, &m
);
105 dev_err(&spidev
->dev
, "spi_recv_resp, data length error = %d\n",
107 mutex_unlock(&spicontext
->spi_lock
);
111 /* As 2 bytes are already read */
114 /* Support of long frame */
115 if (receivebuff
[0] & 0x60)
116 len
+= (((receivebuff
[0] & 0x60) >> 5) << 8) | receivebuff
[1];
118 len
+= receivebuff
[1];
120 /* Now make a transfer to read only relevant bytes */
121 tx_takedata
.rx_buf
= &receivebuff
[2];
122 tx_takedata
.len
= len
- 2;
124 spi_message_init(&m
);
125 spi_message_add_tail(&tx_takedata
, &m
);
127 ret
= spi_sync(spidev
, &m
);
129 mutex_unlock(&spicontext
->spi_lock
);
131 dev_err(&spidev
->dev
, "spi_recv_resp, data read error = %d\n",
138 EXPORT_SYMBOL_GPL(st95hf_spi_recv_response
);
140 int st95hf_spi_recv_echo_res(struct st95hf_spi_context
*spicontext
,
141 unsigned char *receivebuff
)
143 unsigned char readdata_cmd
= ST95HF_COMMAND_RECEIVE
;
144 struct spi_transfer t
[2] = {
145 {.tx_buf
= &readdata_cmd
, .len
= 1,},
146 {.rx_buf
= receivebuff
, .len
= 1,},
148 struct spi_message m
;
149 struct spi_device
*spidev
= spicontext
->spidev
;
152 mutex_lock(&spicontext
->spi_lock
);
154 spi_message_init(&m
);
155 spi_message_add_tail(&t
[0], &m
);
156 spi_message_add_tail(&t
[1], &m
);
157 ret
= spi_sync(spidev
, &m
);
159 mutex_unlock(&spicontext
->spi_lock
);
162 dev_err(&spidev
->dev
, "recv_echo_res, data read error = %d\n",
167 EXPORT_SYMBOL_GPL(st95hf_spi_recv_echo_res
);