1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * I2C link layer for the NXP NCI driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2014 NXP Semiconductors All rights reserved.
6*4882a593Smuzhiyun * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
9*4882a593Smuzhiyun * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Derived from PN544 device driver:
12*4882a593Smuzhiyun * Copyright (C) 2012 Intel Corporation. All rights reserved.
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/acpi.h>
16*4882a593Smuzhiyun #include <linux/delay.h>
17*4882a593Smuzhiyun #include <linux/i2c.h>
18*4882a593Smuzhiyun #include <linux/interrupt.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/nfc.h>
21*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
22*4882a593Smuzhiyun #include <asm/unaligned.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include <net/nfc/nfc.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include "nxp-nci.h"
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define NXP_NCI_I2C_MAX_PAYLOAD 32
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun struct nxp_nci_i2c_phy {
33*4882a593Smuzhiyun struct i2c_client *i2c_dev;
34*4882a593Smuzhiyun struct nci_dev *ndev;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun struct gpio_desc *gpiod_en;
37*4882a593Smuzhiyun struct gpio_desc *gpiod_fw;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun int hard_fault; /*
40*4882a593Smuzhiyun * < 0 if hardware error occurred (e.g. i2c err)
41*4882a593Smuzhiyun * and prevents normal operation.
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
nxp_nci_i2c_set_mode(void * phy_id,enum nxp_nci_mode mode)45*4882a593Smuzhiyun static int nxp_nci_i2c_set_mode(void *phy_id,
46*4882a593Smuzhiyun enum nxp_nci_mode mode)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun gpiod_set_value(phy->gpiod_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
51*4882a593Smuzhiyun gpiod_set_value(phy->gpiod_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
52*4882a593Smuzhiyun usleep_range(10000, 15000);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (mode == NXP_NCI_MODE_COLD)
55*4882a593Smuzhiyun phy->hard_fault = 0;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun return 0;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
nxp_nci_i2c_write(void * phy_id,struct sk_buff * skb)60*4882a593Smuzhiyun static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun int r;
63*4882a593Smuzhiyun struct nxp_nci_i2c_phy *phy = phy_id;
64*4882a593Smuzhiyun struct i2c_client *client = phy->i2c_dev;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun if (phy->hard_fault != 0)
67*4882a593Smuzhiyun return phy->hard_fault;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun r = i2c_master_send(client, skb->data, skb->len);
70*4882a593Smuzhiyun if (r < 0) {
71*4882a593Smuzhiyun /* Retry, chip was in standby */
72*4882a593Smuzhiyun msleep(110);
73*4882a593Smuzhiyun r = i2c_master_send(client, skb->data, skb->len);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun if (r < 0) {
77*4882a593Smuzhiyun nfc_err(&client->dev, "Error %d on I2C send\n", r);
78*4882a593Smuzhiyun } else if (r != skb->len) {
79*4882a593Smuzhiyun nfc_err(&client->dev,
80*4882a593Smuzhiyun "Invalid length sent: %u (expected %u)\n",
81*4882a593Smuzhiyun r, skb->len);
82*4882a593Smuzhiyun r = -EREMOTEIO;
83*4882a593Smuzhiyun } else {
84*4882a593Smuzhiyun /* Success but return 0 and not number of bytes */
85*4882a593Smuzhiyun r = 0;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return r;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun static const struct nxp_nci_phy_ops i2c_phy_ops = {
92*4882a593Smuzhiyun .set_mode = nxp_nci_i2c_set_mode,
93*4882a593Smuzhiyun .write = nxp_nci_i2c_write,
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun
nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy * phy,struct sk_buff ** skb)96*4882a593Smuzhiyun static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
97*4882a593Smuzhiyun struct sk_buff **skb)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun struct i2c_client *client = phy->i2c_dev;
100*4882a593Smuzhiyun u16 header;
101*4882a593Smuzhiyun size_t frame_len;
102*4882a593Smuzhiyun int r;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
105*4882a593Smuzhiyun if (r < 0) {
106*4882a593Smuzhiyun goto fw_read_exit;
107*4882a593Smuzhiyun } else if (r != NXP_NCI_FW_HDR_LEN) {
108*4882a593Smuzhiyun nfc_err(&client->dev, "Incorrect header length: %u\n", r);
109*4882a593Smuzhiyun r = -EBADMSG;
110*4882a593Smuzhiyun goto fw_read_exit;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun frame_len = (be16_to_cpu(header) & NXP_NCI_FW_FRAME_LEN_MASK) +
114*4882a593Smuzhiyun NXP_NCI_FW_CRC_LEN;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
117*4882a593Smuzhiyun if (*skb == NULL) {
118*4882a593Smuzhiyun r = -ENOMEM;
119*4882a593Smuzhiyun goto fw_read_exit;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
125*4882a593Smuzhiyun if (r < 0) {
126*4882a593Smuzhiyun goto fw_read_exit_free_skb;
127*4882a593Smuzhiyun } else if (r != frame_len) {
128*4882a593Smuzhiyun nfc_err(&client->dev,
129*4882a593Smuzhiyun "Invalid frame length: %u (expected %zu)\n",
130*4882a593Smuzhiyun r, frame_len);
131*4882a593Smuzhiyun r = -EBADMSG;
132*4882a593Smuzhiyun goto fw_read_exit_free_skb;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun return 0;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun fw_read_exit_free_skb:
138*4882a593Smuzhiyun kfree_skb(*skb);
139*4882a593Smuzhiyun fw_read_exit:
140*4882a593Smuzhiyun return r;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy * phy,struct sk_buff ** skb)143*4882a593Smuzhiyun static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
144*4882a593Smuzhiyun struct sk_buff **skb)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun struct nci_ctrl_hdr header; /* May actually be a data header */
147*4882a593Smuzhiyun struct i2c_client *client = phy->i2c_dev;
148*4882a593Smuzhiyun int r;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
151*4882a593Smuzhiyun if (r < 0) {
152*4882a593Smuzhiyun goto nci_read_exit;
153*4882a593Smuzhiyun } else if (r != NCI_CTRL_HDR_SIZE) {
154*4882a593Smuzhiyun nfc_err(&client->dev, "Incorrect header length: %u\n", r);
155*4882a593Smuzhiyun r = -EBADMSG;
156*4882a593Smuzhiyun goto nci_read_exit;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
160*4882a593Smuzhiyun if (*skb == NULL) {
161*4882a593Smuzhiyun r = -ENOMEM;
162*4882a593Smuzhiyun goto nci_read_exit;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun if (!header.plen)
168*4882a593Smuzhiyun return 0;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
171*4882a593Smuzhiyun if (r < 0) {
172*4882a593Smuzhiyun goto nci_read_exit_free_skb;
173*4882a593Smuzhiyun } else if (r != header.plen) {
174*4882a593Smuzhiyun nfc_err(&client->dev,
175*4882a593Smuzhiyun "Invalid frame payload length: %u (expected %u)\n",
176*4882a593Smuzhiyun r, header.plen);
177*4882a593Smuzhiyun r = -EBADMSG;
178*4882a593Smuzhiyun goto nci_read_exit_free_skb;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun return 0;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun nci_read_exit_free_skb:
184*4882a593Smuzhiyun kfree_skb(*skb);
185*4882a593Smuzhiyun nci_read_exit:
186*4882a593Smuzhiyun return r;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
nxp_nci_i2c_irq_thread_fn(int irq,void * phy_id)189*4882a593Smuzhiyun static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun struct nxp_nci_i2c_phy *phy = phy_id;
192*4882a593Smuzhiyun struct i2c_client *client;
193*4882a593Smuzhiyun struct nxp_nci_info *info;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun struct sk_buff *skb = NULL;
196*4882a593Smuzhiyun int r = 0;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun if (!phy || !phy->ndev)
199*4882a593Smuzhiyun goto exit_irq_none;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun client = phy->i2c_dev;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (!client || irq != client->irq)
204*4882a593Smuzhiyun goto exit_irq_none;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun info = nci_get_drvdata(phy->ndev);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (!info)
209*4882a593Smuzhiyun goto exit_irq_none;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun mutex_lock(&info->info_lock);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun if (phy->hard_fault != 0)
214*4882a593Smuzhiyun goto exit_irq_handled;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun switch (info->mode) {
217*4882a593Smuzhiyun case NXP_NCI_MODE_NCI:
218*4882a593Smuzhiyun r = nxp_nci_i2c_nci_read(phy, &skb);
219*4882a593Smuzhiyun break;
220*4882a593Smuzhiyun case NXP_NCI_MODE_FW:
221*4882a593Smuzhiyun r = nxp_nci_i2c_fw_read(phy, &skb);
222*4882a593Smuzhiyun break;
223*4882a593Smuzhiyun case NXP_NCI_MODE_COLD:
224*4882a593Smuzhiyun r = -EREMOTEIO;
225*4882a593Smuzhiyun break;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun if (r == -EREMOTEIO) {
229*4882a593Smuzhiyun phy->hard_fault = r;
230*4882a593Smuzhiyun if (info->mode == NXP_NCI_MODE_FW)
231*4882a593Smuzhiyun nxp_nci_fw_recv_frame(phy->ndev, NULL);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun if (r < 0) {
234*4882a593Smuzhiyun nfc_err(&client->dev, "Read failed with error %d\n", r);
235*4882a593Smuzhiyun goto exit_irq_handled;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun switch (info->mode) {
239*4882a593Smuzhiyun case NXP_NCI_MODE_NCI:
240*4882a593Smuzhiyun nci_recv_frame(phy->ndev, skb);
241*4882a593Smuzhiyun break;
242*4882a593Smuzhiyun case NXP_NCI_MODE_FW:
243*4882a593Smuzhiyun nxp_nci_fw_recv_frame(phy->ndev, skb);
244*4882a593Smuzhiyun break;
245*4882a593Smuzhiyun case NXP_NCI_MODE_COLD:
246*4882a593Smuzhiyun break;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun exit_irq_handled:
250*4882a593Smuzhiyun mutex_unlock(&info->info_lock);
251*4882a593Smuzhiyun return IRQ_HANDLED;
252*4882a593Smuzhiyun exit_irq_none:
253*4882a593Smuzhiyun WARN_ON_ONCE(1);
254*4882a593Smuzhiyun return IRQ_NONE;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun static const struct acpi_gpio_params firmware_gpios = { 1, 0, false };
258*4882a593Smuzhiyun static const struct acpi_gpio_params enable_gpios = { 2, 0, false };
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
261*4882a593Smuzhiyun { "enable-gpios", &enable_gpios, 1 },
262*4882a593Smuzhiyun { "firmware-gpios", &firmware_gpios, 1 },
263*4882a593Smuzhiyun { }
264*4882a593Smuzhiyun };
265*4882a593Smuzhiyun
nxp_nci_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)266*4882a593Smuzhiyun static int nxp_nci_i2c_probe(struct i2c_client *client,
267*4882a593Smuzhiyun const struct i2c_device_id *id)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun struct device *dev = &client->dev;
270*4882a593Smuzhiyun struct nxp_nci_i2c_phy *phy;
271*4882a593Smuzhiyun int r;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
274*4882a593Smuzhiyun nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
275*4882a593Smuzhiyun return -ENODEV;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
279*4882a593Smuzhiyun GFP_KERNEL);
280*4882a593Smuzhiyun if (!phy)
281*4882a593Smuzhiyun return -ENOMEM;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun phy->i2c_dev = client;
284*4882a593Smuzhiyun i2c_set_clientdata(client, phy);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun r = devm_acpi_dev_add_driver_gpios(dev, acpi_nxp_nci_gpios);
287*4882a593Smuzhiyun if (r)
288*4882a593Smuzhiyun dev_dbg(dev, "Unable to add GPIO mapping table\n");
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
291*4882a593Smuzhiyun if (IS_ERR(phy->gpiod_en)) {
292*4882a593Smuzhiyun nfc_err(dev, "Failed to get EN gpio\n");
293*4882a593Smuzhiyun return PTR_ERR(phy->gpiod_en);
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun phy->gpiod_fw = devm_gpiod_get(dev, "firmware", GPIOD_OUT_LOW);
297*4882a593Smuzhiyun if (IS_ERR(phy->gpiod_fw)) {
298*4882a593Smuzhiyun nfc_err(dev, "Failed to get FW gpio\n");
299*4882a593Smuzhiyun return PTR_ERR(phy->gpiod_fw);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
303*4882a593Smuzhiyun NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
304*4882a593Smuzhiyun if (r < 0)
305*4882a593Smuzhiyun return r;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun r = request_threaded_irq(client->irq, NULL,
308*4882a593Smuzhiyun nxp_nci_i2c_irq_thread_fn,
309*4882a593Smuzhiyun IRQF_TRIGGER_RISING | IRQF_ONESHOT,
310*4882a593Smuzhiyun NXP_NCI_I2C_DRIVER_NAME, phy);
311*4882a593Smuzhiyun if (r < 0)
312*4882a593Smuzhiyun nfc_err(&client->dev, "Unable to register IRQ handler\n");
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun return r;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
nxp_nci_i2c_remove(struct i2c_client * client)317*4882a593Smuzhiyun static int nxp_nci_i2c_remove(struct i2c_client *client)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun nxp_nci_remove(phy->ndev);
322*4882a593Smuzhiyun free_irq(client->irq, phy);
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun return 0;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun static const struct i2c_device_id nxp_nci_i2c_id_table[] = {
328*4882a593Smuzhiyun {"nxp-nci_i2c", 0},
329*4882a593Smuzhiyun {}
330*4882a593Smuzhiyun };
331*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun static const struct of_device_id of_nxp_nci_i2c_match[] = {
334*4882a593Smuzhiyun { .compatible = "nxp,nxp-nci-i2c", },
335*4882a593Smuzhiyun {}
336*4882a593Smuzhiyun };
337*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun #ifdef CONFIG_ACPI
340*4882a593Smuzhiyun static const struct acpi_device_id acpi_id[] = {
341*4882a593Smuzhiyun { "NXP1001" },
342*4882a593Smuzhiyun { "NXP7471" },
343*4882a593Smuzhiyun { }
344*4882a593Smuzhiyun };
345*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, acpi_id);
346*4882a593Smuzhiyun #endif
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun static struct i2c_driver nxp_nci_i2c_driver = {
349*4882a593Smuzhiyun .driver = {
350*4882a593Smuzhiyun .name = NXP_NCI_I2C_DRIVER_NAME,
351*4882a593Smuzhiyun .acpi_match_table = ACPI_PTR(acpi_id),
352*4882a593Smuzhiyun .of_match_table = of_nxp_nci_i2c_match,
353*4882a593Smuzhiyun },
354*4882a593Smuzhiyun .probe = nxp_nci_i2c_probe,
355*4882a593Smuzhiyun .id_table = nxp_nci_i2c_id_table,
356*4882a593Smuzhiyun .remove = nxp_nci_i2c_remove,
357*4882a593Smuzhiyun };
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun module_i2c_driver(nxp_nci_i2c_driver);
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun MODULE_LICENSE("GPL");
362*4882a593Smuzhiyun MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
363*4882a593Smuzhiyun MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
364*4882a593Smuzhiyun MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");
365