]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/nfc/st-nci/spi.c
Merge tag 'timers-urgent-2021-09-26' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / drivers / nfc / st-nci / spi.c
CommitLineData
46fe7771 1// SPDX-License-Identifier: GPL-2.0-only
2bc4d4f8
CR
2/*
3 * SPI Link Layer for ST NCI based Driver
4 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
2bc4d4f8
CR
5 */
6
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9#include <linux/module.h>
10#include <linux/spi/spi.h>
60cd6d89 11#include <linux/gpio/consumer.h>
60cd6d89 12#include <linux/acpi.h>
2bc4d4f8
CR
13#include <linux/interrupt.h>
14#include <linux/delay.h>
15#include <linux/nfc.h>
a89e68f1 16#include <linux/of.h>
a1269dd1 17#include <net/nfc/nci.h>
2bc4d4f8 18
e67e7e59 19#include "st-nci.h"
2bc4d4f8
CR
20
21#define DRIVER_DESC "NCI NFC driver for ST_NCI"
22
23/* ndlc header */
24#define ST_NCI_FRAME_HEADROOM 1
ba2c231c 25#define ST_NCI_FRAME_TAILROOM 0
2bc4d4f8
CR
26
27#define ST_NCI_SPI_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
28#define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
29
61a04101 30#define ST_NCI_DRIVER_NAME "st_nci"
2bc4d4f8
CR
31#define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
32
2bc4d4f8
CR
33struct st_nci_spi_phy {
34 struct spi_device *spi_dev;
35 struct llt_ndlc *ndlc;
36
bb2496c3
CR
37 bool irq_active;
38
a89e68f1 39 struct gpio_desc *gpiod_reset;
3648dc6d
CR
40
41 struct st_nci_se_status se_status;
2bc4d4f8
CR
42};
43
2bc4d4f8
CR
44static int st_nci_spi_enable(void *phy_id)
45{
46 struct st_nci_spi_phy *phy = phy_id;
47
a89e68f1 48 gpiod_set_value(phy->gpiod_reset, 0);
2bc4d4f8 49 usleep_range(10000, 15000);
a89e68f1 50 gpiod_set_value(phy->gpiod_reset, 1);
2bc4d4f8
CR
51 usleep_range(80000, 85000);
52
bb2496c3 53 if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
2bc4d4f8 54 enable_irq(phy->spi_dev->irq);
bb2496c3
CR
55 phy->irq_active = true;
56 }
2bc4d4f8
CR
57
58 return 0;
59}
60
61static void st_nci_spi_disable(void *phy_id)
62{
63 struct st_nci_spi_phy *phy = phy_id;
64
65 disable_irq_nosync(phy->spi_dev->irq);
bb2496c3 66 phy->irq_active = false;
2bc4d4f8
CR
67}
68
69/*
70 * Writing a frame must not return the number of written bytes.
71 * It must return either zero for success, or <0 for error.
72 * In addition, it must not alter the skb
73 */
74static int st_nci_spi_write(void *phy_id, struct sk_buff *skb)
75{
76 int r;
77 struct st_nci_spi_phy *phy = phy_id;
78 struct spi_device *dev = phy->spi_dev;
79 struct sk_buff *skb_rx;
a1269dd1
CR
80 u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE +
81 ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM];
2bc4d4f8
CR
82 struct spi_transfer spi_xfer = {
83 .tx_buf = skb->data,
84 .rx_buf = buf,
85 .len = skb->len,
86 };
87
2bc4d4f8
CR
88 if (phy->ndlc->hard_fault != 0)
89 return phy->ndlc->hard_fault;
90
91 r = spi_sync_transfer(dev, &spi_xfer, 1);
92 /*
93 * We may have received some valuable data on miso line.
94 * Send them back in the ndlc state machine.
95 */
96 if (!r) {
97 skb_rx = alloc_skb(skb->len, GFP_KERNEL);
eba43fac 98 if (!skb_rx)
99 return -ENOMEM;
2bc4d4f8
CR
100
101 skb_put(skb_rx, skb->len);
102 memcpy(skb_rx->data, buf, skb->len);
103 ndlc_recv(phy->ndlc, skb_rx);
104 }
105
2bc4d4f8
CR
106 return r;
107}
108
109/*
110 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
111 * returns:
112 * 0 : if received frame is complete
113 * -EREMOTEIO : i2c read error (fatal)
114 * -EBADMSG : frame was incorrect and discarded
115 * -ENOMEM : cannot allocate skb, frame dropped
116 */
117static int st_nci_spi_read(struct st_nci_spi_phy *phy,
118 struct sk_buff **skb)
119{
120 int r;
121 u8 len;
122 u8 buf[ST_NCI_SPI_MAX_SIZE];
123 struct spi_device *dev = phy->spi_dev;
124 struct spi_transfer spi_xfer = {
125 .rx_buf = buf,
126 .len = ST_NCI_SPI_MIN_SIZE,
127 };
128
129 r = spi_sync_transfer(dev, &spi_xfer, 1);
130 if (r < 0)
131 return -EREMOTEIO;
132
133 len = be16_to_cpu(*(__be16 *) (buf + 2));
134 if (len > ST_NCI_SPI_MAX_SIZE) {
135 nfc_err(&dev->dev, "invalid frame len\n");
136 phy->ndlc->hard_fault = 1;
137 return -EBADMSG;
138 }
139
140 *skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
141 if (*skb == NULL)
142 return -ENOMEM;
143
144 skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE);
145 skb_put(*skb, ST_NCI_SPI_MIN_SIZE);
146 memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE);
147
148 if (!len)
149 return 0;
150
151 spi_xfer.len = len;
152 r = spi_sync_transfer(dev, &spi_xfer, 1);
153 if (r < 0) {
154 kfree_skb(*skb);
155 return -EREMOTEIO;
156 }
157
158 skb_put(*skb, len);
159 memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
160
2bc4d4f8
CR
161 return 0;
162}
163
164/*
165 * Reads an ndlc frame from the chip.
166 *
167 * On ST21NFCB, IRQ goes in idle state when read starts.
168 */
169static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
170{
171 struct st_nci_spi_phy *phy = phy_id;
172 struct spi_device *dev;
173 struct sk_buff *skb = NULL;
174 int r;
175
176 if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
177 WARN_ON_ONCE(1);
178 return IRQ_NONE;
179 }
180
181 dev = phy->spi_dev;
182 dev_dbg(&dev->dev, "IRQ\n");
183
184 if (phy->ndlc->hard_fault)
185 return IRQ_HANDLED;
186
187 if (!phy->ndlc->powered) {
188 st_nci_spi_disable(phy);
189 return IRQ_HANDLED;
190 }
191
192 r = st_nci_spi_read(phy, &skb);
193 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
194 return IRQ_HANDLED;
195
196 ndlc_recv(phy->ndlc, skb);
197
198 return IRQ_HANDLED;
199}
200
7a5e98da 201static const struct nfc_phy_ops spi_phy_ops = {
2bc4d4f8
CR
202 .write = st_nci_spi_write,
203 .enable = st_nci_spi_enable,
204 .disable = st_nci_spi_disable,
205};
206
85d23e77
AS
207static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
208
209static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
210 { "reset-gpios", &reset_gpios, 1 },
211 {},
212};
213
2bc4d4f8
CR
214static int st_nci_spi_probe(struct spi_device *dev)
215{
216 struct st_nci_spi_phy *phy;
2bc4d4f8
CR
217 int r;
218
2bc4d4f8
CR
219 /* Check SPI platform functionnalities */
220 if (!dev) {
221 pr_debug("%s: dev is NULL. Device is not accessible.\n",
222 __func__);
223 return -ENODEV;
224 }
225
226 phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
227 GFP_KERNEL);
228 if (!phy)
229 return -ENOMEM;
230
231 phy->spi_dev = dev;
232
233 spi_set_drvdata(dev, phy);
234
c745120e
AS
235 r = devm_acpi_dev_add_driver_gpios(&dev->dev, acpi_st_nci_gpios);
236 if (r)
237 dev_dbg(&dev->dev, "Unable to add GPIO mapping table\n");
238
239 /* Get RESET GPIO */
240 phy->gpiod_reset = devm_gpiod_get(&dev->dev, "reset", GPIOD_OUT_HIGH);
241 if (IS_ERR(phy->gpiod_reset)) {
242 nfc_err(&dev->dev, "Unable to get RESET GPIO\n");
243 return PTR_ERR(phy->gpiod_reset);
2bc4d4f8
CR
244 }
245
75719b2b
AS
246 phy->se_status.is_ese_present =
247 device_property_read_bool(&dev->dev, "ese-present");
248 phy->se_status.is_uicc_present =
249 device_property_read_bool(&dev->dev, "uicc-present");
250
2bc4d4f8
CR
251 r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
252 ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
3648dc6d 253 &phy->ndlc, &phy->se_status);
2bc4d4f8
CR
254 if (r < 0) {
255 nfc_err(&dev->dev, "Unable to register ndlc layer\n");
256 return r;
257 }
258
bb2496c3 259 phy->irq_active = true;
2bc4d4f8
CR
260 r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
261 st_nci_irq_thread_fn,
1af9fea6 262 IRQF_ONESHOT,
2bc4d4f8
CR
263 ST_NCI_SPI_DRIVER_NAME, phy);
264 if (r < 0)
265 nfc_err(&dev->dev, "Unable to register IRQ handler\n");
266
267 return r;
268}
269
270static int st_nci_spi_remove(struct spi_device *dev)
271{
272 struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
273
2bc4d4f8
CR
274 ndlc_remove(phy->ndlc);
275
276 return 0;
277}
278
3252897f
CR
279static struct spi_device_id st_nci_spi_id_table[] = {
280 {ST_NCI_SPI_DRIVER_NAME, 0},
31339440 281 {"st21nfcb-spi", 0},
3252897f
CR
282 {}
283};
284MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
285
255fcc7b 286static const struct acpi_device_id st_nci_spi_acpi_match[] __maybe_unused = {
60cd6d89
CR
287 {"SMO2101", 0},
288 {}
289};
290MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match);
291
255fcc7b 292static const struct of_device_id of_st_nci_spi_match[] __maybe_unused = {
2bc4d4f8
CR
293 { .compatible = "st,st21nfcb-spi", },
294 {}
295};
296MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
2bc4d4f8
CR
297
298static struct spi_driver st_nci_spi_driver = {
299 .driver = {
2bc4d4f8
CR
300 .name = ST_NCI_SPI_DRIVER_NAME,
301 .of_match_table = of_match_ptr(of_st_nci_spi_match),
60cd6d89 302 .acpi_match_table = ACPI_PTR(st_nci_spi_acpi_match),
2bc4d4f8
CR
303 },
304 .probe = st_nci_spi_probe,
305 .id_table = st_nci_spi_id_table,
306 .remove = st_nci_spi_remove,
307};
2bc4d4f8
CR
308module_spi_driver(st_nci_spi_driver);
309
310MODULE_LICENSE("GPL");
311MODULE_DESCRIPTION(DRIVER_DESC);