Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * I2C link layer for the NXP NCI driver
4 *
5 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
6 * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
7 *
8 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
9 * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
10 *
11 * Derived from PN544 device driver:
12 * Copyright (C) 2012 Intel Corporation. All rights reserved.
13 */
14
15#include <linux/acpi.h>
16#include <linux/delay.h>
17#include <linux/i2c.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/nfc.h>
21#include <linux/gpio/consumer.h>
22#include <asm/unaligned.h>
23
24#include <net/nfc/nfc.h>
25
26#include "nxp-nci.h"
27
28#define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
29
30#define NXP_NCI_I2C_MAX_PAYLOAD 32
31
32struct nxp_nci_i2c_phy {
33 struct i2c_client *i2c_dev;
34 struct nci_dev *ndev;
35
36 struct gpio_desc *gpiod_en;
37 struct gpio_desc *gpiod_fw;
38
39 int hard_fault; /*
40 * < 0 if hardware error occurred (e.g. i2c err)
41 * and prevents normal operation.
42 */
43};
44
45static int nxp_nci_i2c_set_mode(void *phy_id,
46 enum nxp_nci_mode mode)
47{
48 struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
49
50 gpiod_set_value(phy->gpiod_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
51 gpiod_set_value(phy->gpiod_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
52 usleep_range(10000, 15000);
53
54 if (mode == NXP_NCI_MODE_COLD)
55 phy->hard_fault = 0;
56
57 return 0;
58}
59
60static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
61{
62 int r;
63 struct nxp_nci_i2c_phy *phy = phy_id;
64 struct i2c_client *client = phy->i2c_dev;
65
66 if (phy->hard_fault != 0)
67 return phy->hard_fault;
68
69 r = i2c_master_send(client, skb->data, skb->len);
70 if (r < 0) {
71 /* Retry, chip was in standby */
72 msleep(110);
73 r = i2c_master_send(client, skb->data, skb->len);
74 }
75
76 if (r < 0) {
77 nfc_err(&client->dev, "Error %d on I2C send\n", r);
78 } else if (r != skb->len) {
79 nfc_err(&client->dev,
80 "Invalid length sent: %u (expected %u)\n",
81 r, skb->len);
82 r = -EREMOTEIO;
83 } else {
84 /* Success but return 0 and not number of bytes */
85 r = 0;
86 }
87
88 return r;
89}
90
91static const struct nxp_nci_phy_ops i2c_phy_ops = {
92 .set_mode = nxp_nci_i2c_set_mode,
93 .write = nxp_nci_i2c_write,
94};
95
96static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
97 struct sk_buff **skb)
98{
99 struct i2c_client *client = phy->i2c_dev;
100 size_t frame_len;
101 __be16 header;
102 int r;
103
104 r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
105 if (r < 0) {
106 goto fw_read_exit;
107 } else if (r != NXP_NCI_FW_HDR_LEN) {
108 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
109 r = -EBADMSG;
110 goto fw_read_exit;
111 }
112
113 frame_len = (be16_to_cpu(header) & NXP_NCI_FW_FRAME_LEN_MASK) +
114 NXP_NCI_FW_CRC_LEN;
115
116 *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
117 if (*skb == NULL) {
118 r = -ENOMEM;
119 goto fw_read_exit;
120 }
121
122 skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN);
123
124 r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
125 if (r < 0) {
126 goto fw_read_exit_free_skb;
127 } else if (r != frame_len) {
128 nfc_err(&client->dev,
129 "Invalid frame length: %u (expected %zu)\n",
130 r, frame_len);
131 r = -EBADMSG;
132 goto fw_read_exit_free_skb;
133 }
134
135 return 0;
136
137fw_read_exit_free_skb:
138 kfree_skb(*skb);
139fw_read_exit:
140 return r;
141}
142
143static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
144 struct sk_buff **skb)
145{
146 struct nci_ctrl_hdr header; /* May actually be a data header */
147 struct i2c_client *client = phy->i2c_dev;
148 int r;
149
150 r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
151 if (r < 0) {
152 goto nci_read_exit;
153 } else if (r != NCI_CTRL_HDR_SIZE) {
154 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
155 r = -EBADMSG;
156 goto nci_read_exit;
157 }
158
159 *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
160 if (*skb == NULL) {
161 r = -ENOMEM;
162 goto nci_read_exit;
163 }
164
165 skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE);
166
167 if (!header.plen)
168 return 0;
169
170 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
171 if (r < 0) {
172 goto nci_read_exit_free_skb;
173 } else if (r != header.plen) {
174 nfc_err(&client->dev,
175 "Invalid frame payload length: %u (expected %u)\n",
176 r, header.plen);
177 r = -EBADMSG;
178 goto nci_read_exit_free_skb;
179 }
180
181 return 0;
182
183nci_read_exit_free_skb:
184 kfree_skb(*skb);
185nci_read_exit:
186 return r;
187}
188
189static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
190{
191 struct nxp_nci_i2c_phy *phy = phy_id;
192 struct i2c_client *client;
193 struct nxp_nci_info *info;
194
195 struct sk_buff *skb = NULL;
196 int r = 0;
197
198 if (!phy || !phy->ndev)
199 goto exit_irq_none;
200
201 client = phy->i2c_dev;
202
203 if (!client || irq != client->irq)
204 goto exit_irq_none;
205
206 info = nci_get_drvdata(phy->ndev);
207
208 if (!info)
209 goto exit_irq_none;
210
211 mutex_lock(&info->info_lock);
212
213 if (phy->hard_fault != 0)
214 goto exit_irq_handled;
215
216 switch (info->mode) {
217 case NXP_NCI_MODE_NCI:
218 r = nxp_nci_i2c_nci_read(phy, &skb);
219 break;
220 case NXP_NCI_MODE_FW:
221 r = nxp_nci_i2c_fw_read(phy, &skb);
222 break;
223 case NXP_NCI_MODE_COLD:
224 r = -EREMOTEIO;
225 break;
226 }
227
228 if (r == -EREMOTEIO) {
229 phy->hard_fault = r;
230 if (info->mode == NXP_NCI_MODE_FW)
231 nxp_nci_fw_recv_frame(phy->ndev, NULL);
232 }
233 if (r < 0) {
234 nfc_err(&client->dev, "Read failed with error %d\n", r);
235 goto exit_irq_handled;
236 }
237
238 switch (info->mode) {
239 case NXP_NCI_MODE_NCI:
240 nci_recv_frame(phy->ndev, skb);
241 break;
242 case NXP_NCI_MODE_FW:
243 nxp_nci_fw_recv_frame(phy->ndev, skb);
244 break;
245 case NXP_NCI_MODE_COLD:
246 break;
247 }
248
249exit_irq_handled:
250 mutex_unlock(&info->info_lock);
251 return IRQ_HANDLED;
252exit_irq_none:
253 WARN_ON_ONCE(1);
254 return IRQ_NONE;
255}
256
257static const struct acpi_gpio_params firmware_gpios = { 1, 0, false };
258static const struct acpi_gpio_params enable_gpios = { 2, 0, false };
259
260static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
261 { "enable-gpios", &enable_gpios, 1 },
262 { "firmware-gpios", &firmware_gpios, 1 },
263 { }
264};
265
266static int nxp_nci_i2c_probe(struct i2c_client *client)
267{
268 struct device *dev = &client->dev;
269 struct nxp_nci_i2c_phy *phy;
270 int r;
271
272 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
273 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
274 return -ENODEV;
275 }
276
277 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
278 GFP_KERNEL);
279 if (!phy)
280 return -ENOMEM;
281
282 phy->i2c_dev = client;
283 i2c_set_clientdata(client, phy);
284
285 r = devm_acpi_dev_add_driver_gpios(dev, acpi_nxp_nci_gpios);
286 if (r)
287 dev_dbg(dev, "Unable to add GPIO mapping table\n");
288
289 phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
290 if (IS_ERR(phy->gpiod_en)) {
291 nfc_err(dev, "Failed to get EN gpio\n");
292 return PTR_ERR(phy->gpiod_en);
293 }
294
295 phy->gpiod_fw = devm_gpiod_get_optional(dev, "firmware", GPIOD_OUT_LOW);
296 if (IS_ERR(phy->gpiod_fw)) {
297 nfc_err(dev, "Failed to get FW gpio\n");
298 return PTR_ERR(phy->gpiod_fw);
299 }
300
301 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
302 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
303 if (r < 0)
304 return r;
305
306 r = request_threaded_irq(client->irq, NULL,
307 nxp_nci_i2c_irq_thread_fn,
308 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
309 NXP_NCI_I2C_DRIVER_NAME, phy);
310 if (r < 0)
311 nfc_err(&client->dev, "Unable to register IRQ handler\n");
312
313 return r;
314}
315
316static void nxp_nci_i2c_remove(struct i2c_client *client)
317{
318 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
319
320 nxp_nci_remove(phy->ndev);
321 free_irq(client->irq, phy);
322}
323
324static const struct i2c_device_id nxp_nci_i2c_id_table[] = {
325 {"nxp-nci_i2c", 0},
326 {}
327};
328MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
329
330static const struct of_device_id of_nxp_nci_i2c_match[] = {
331 { .compatible = "nxp,nxp-nci-i2c", },
332 {}
333};
334MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
335
336#ifdef CONFIG_ACPI
337static const struct acpi_device_id acpi_id[] = {
338 { "NXP1001" },
339 { "NXP1002" },
340 { "NXP7471" },
341 { }
342};
343MODULE_DEVICE_TABLE(acpi, acpi_id);
344#endif
345
346static struct i2c_driver nxp_nci_i2c_driver = {
347 .driver = {
348 .name = NXP_NCI_I2C_DRIVER_NAME,
349 .acpi_match_table = ACPI_PTR(acpi_id),
350 .of_match_table = of_nxp_nci_i2c_match,
351 },
352 .probe = nxp_nci_i2c_probe,
353 .id_table = nxp_nci_i2c_id_table,
354 .remove = nxp_nci_i2c_remove,
355};
356
357module_i2c_driver(nxp_nci_i2c_driver);
358
359MODULE_LICENSE("GPL");
360MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
361MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
362MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");
1/*
2 * I2C link layer for the NXP NCI driver
3 *
4 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
5 * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
6 *
7 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
8 * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
9 *
10 * Derived from PN544 device driver:
11 * Copyright (C) 2012 Intel Corporation. All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 */
25
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28#include <linux/acpi.h>
29#include <linux/delay.h>
30#include <linux/i2c.h>
31#include <linux/interrupt.h>
32#include <linux/miscdevice.h>
33#include <linux/module.h>
34#include <linux/nfc.h>
35#include <linux/gpio/consumer.h>
36#include <linux/of_gpio.h>
37#include <linux/of_irq.h>
38#include <linux/platform_data/nxp-nci.h>
39#include <linux/unaligned/access_ok.h>
40
41#include <net/nfc/nfc.h>
42
43#include "nxp-nci.h"
44
45#define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
46
47#define NXP_NCI_I2C_MAX_PAYLOAD 32
48
49struct nxp_nci_i2c_phy {
50 struct i2c_client *i2c_dev;
51 struct nci_dev *ndev;
52
53 unsigned int gpio_en;
54 unsigned int gpio_fw;
55
56 int hard_fault; /*
57 * < 0 if hardware error occurred (e.g. i2c err)
58 * and prevents normal operation.
59 */
60};
61
62static int nxp_nci_i2c_set_mode(void *phy_id,
63 enum nxp_nci_mode mode)
64{
65 struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
66
67 gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
68 gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
69 usleep_range(10000, 15000);
70
71 if (mode == NXP_NCI_MODE_COLD)
72 phy->hard_fault = 0;
73
74 return 0;
75}
76
77static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
78{
79 int r;
80 struct nxp_nci_i2c_phy *phy = phy_id;
81 struct i2c_client *client = phy->i2c_dev;
82
83 if (phy->hard_fault != 0)
84 return phy->hard_fault;
85
86 r = i2c_master_send(client, skb->data, skb->len);
87 if (r < 0) {
88 /* Retry, chip was in standby */
89 usleep_range(110000, 120000);
90 r = i2c_master_send(client, skb->data, skb->len);
91 }
92
93 if (r < 0) {
94 nfc_err(&client->dev, "Error %d on I2C send\n", r);
95 } else if (r != skb->len) {
96 nfc_err(&client->dev,
97 "Invalid length sent: %u (expected %u)\n",
98 r, skb->len);
99 r = -EREMOTEIO;
100 } else {
101 /* Success but return 0 and not number of bytes */
102 r = 0;
103 }
104
105 return r;
106}
107
108static const struct nxp_nci_phy_ops i2c_phy_ops = {
109 .set_mode = nxp_nci_i2c_set_mode,
110 .write = nxp_nci_i2c_write,
111};
112
113static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
114 struct sk_buff **skb)
115{
116 struct i2c_client *client = phy->i2c_dev;
117 u16 header;
118 size_t frame_len;
119 int r;
120
121 r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
122 if (r < 0) {
123 goto fw_read_exit;
124 } else if (r != NXP_NCI_FW_HDR_LEN) {
125 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
126 r = -EBADMSG;
127 goto fw_read_exit;
128 }
129
130 frame_len = (get_unaligned_be16(&header) & NXP_NCI_FW_FRAME_LEN_MASK) +
131 NXP_NCI_FW_CRC_LEN;
132
133 *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
134 if (*skb == NULL) {
135 r = -ENOMEM;
136 goto fw_read_exit;
137 }
138
139 memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
140
141 r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
142 if (r != frame_len) {
143 nfc_err(&client->dev,
144 "Invalid frame length: %u (expected %zu)\n",
145 r, frame_len);
146 r = -EBADMSG;
147 goto fw_read_exit_free_skb;
148 }
149
150 return 0;
151
152fw_read_exit_free_skb:
153 kfree_skb(*skb);
154fw_read_exit:
155 return r;
156}
157
158static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
159 struct sk_buff **skb)
160{
161 struct nci_ctrl_hdr header; /* May actually be a data header */
162 struct i2c_client *client = phy->i2c_dev;
163 int r;
164
165 r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
166 if (r < 0) {
167 goto nci_read_exit;
168 } else if (r != NCI_CTRL_HDR_SIZE) {
169 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
170 r = -EBADMSG;
171 goto nci_read_exit;
172 }
173
174 *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
175 if (*skb == NULL) {
176 r = -ENOMEM;
177 goto nci_read_exit;
178 }
179
180 memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
181 NCI_CTRL_HDR_SIZE);
182
183 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
184 if (r != header.plen) {
185 nfc_err(&client->dev,
186 "Invalid frame payload length: %u (expected %u)\n",
187 r, header.plen);
188 r = -EBADMSG;
189 goto nci_read_exit_free_skb;
190 }
191
192 return 0;
193
194nci_read_exit_free_skb:
195 kfree_skb(*skb);
196nci_read_exit:
197 return r;
198}
199
200static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
201{
202 struct nxp_nci_i2c_phy *phy = phy_id;
203 struct i2c_client *client;
204 struct nxp_nci_info *info;
205
206 struct sk_buff *skb = NULL;
207 int r = 0;
208
209 if (!phy || !phy->ndev)
210 goto exit_irq_none;
211
212 client = phy->i2c_dev;
213
214 if (!client || irq != client->irq)
215 goto exit_irq_none;
216
217 info = nci_get_drvdata(phy->ndev);
218
219 if (!info)
220 goto exit_irq_none;
221
222 mutex_lock(&info->info_lock);
223
224 if (phy->hard_fault != 0)
225 goto exit_irq_handled;
226
227 switch (info->mode) {
228 case NXP_NCI_MODE_NCI:
229 r = nxp_nci_i2c_nci_read(phy, &skb);
230 break;
231 case NXP_NCI_MODE_FW:
232 r = nxp_nci_i2c_fw_read(phy, &skb);
233 break;
234 case NXP_NCI_MODE_COLD:
235 r = -EREMOTEIO;
236 break;
237 }
238
239 if (r == -EREMOTEIO) {
240 phy->hard_fault = r;
241 skb = NULL;
242 } else if (r < 0) {
243 nfc_err(&client->dev, "Read failed with error %d\n", r);
244 goto exit_irq_handled;
245 }
246
247 switch (info->mode) {
248 case NXP_NCI_MODE_NCI:
249 nci_recv_frame(phy->ndev, skb);
250 break;
251 case NXP_NCI_MODE_FW:
252 nxp_nci_fw_recv_frame(phy->ndev, skb);
253 break;
254 case NXP_NCI_MODE_COLD:
255 break;
256 }
257
258exit_irq_handled:
259 mutex_unlock(&info->info_lock);
260 return IRQ_HANDLED;
261exit_irq_none:
262 WARN_ON_ONCE(1);
263 return IRQ_NONE;
264}
265
266static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
267{
268 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
269 struct device_node *pp;
270 int r;
271
272 pp = client->dev.of_node;
273 if (!pp)
274 return -ENODEV;
275
276 r = of_get_named_gpio(pp, "enable-gpios", 0);
277 if (r == -EPROBE_DEFER)
278 r = of_get_named_gpio(pp, "enable-gpios", 0);
279 if (r < 0) {
280 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
281 return r;
282 }
283 phy->gpio_en = r;
284
285 r = of_get_named_gpio(pp, "firmware-gpios", 0);
286 if (r == -EPROBE_DEFER)
287 r = of_get_named_gpio(pp, "firmware-gpios", 0);
288 if (r < 0) {
289 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
290 return r;
291 }
292 phy->gpio_fw = r;
293
294 return 0;
295}
296
297static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
298{
299 struct i2c_client *client = phy->i2c_dev;
300 struct gpio_desc *gpiod_en, *gpiod_fw;
301
302 gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
303 gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
304
305 if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
306 nfc_err(&client->dev, "No GPIOs\n");
307 return -EINVAL;
308 }
309
310 phy->gpio_en = desc_to_gpio(gpiod_en);
311 phy->gpio_fw = desc_to_gpio(gpiod_fw);
312
313 return 0;
314}
315
316static int nxp_nci_i2c_probe(struct i2c_client *client,
317 const struct i2c_device_id *id)
318{
319 struct nxp_nci_i2c_phy *phy;
320 struct nxp_nci_nfc_platform_data *pdata;
321 int r;
322
323 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
324 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
325 r = -ENODEV;
326 goto probe_exit;
327 }
328
329 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
330 GFP_KERNEL);
331 if (!phy) {
332 r = -ENOMEM;
333 goto probe_exit;
334 }
335
336 phy->i2c_dev = client;
337 i2c_set_clientdata(client, phy);
338
339 pdata = client->dev.platform_data;
340
341 if (!pdata && client->dev.of_node) {
342 r = nxp_nci_i2c_parse_devtree(client);
343 if (r < 0) {
344 nfc_err(&client->dev, "Failed to get DT data\n");
345 goto probe_exit;
346 }
347 } else if (pdata) {
348 phy->gpio_en = pdata->gpio_en;
349 phy->gpio_fw = pdata->gpio_fw;
350 } else if (ACPI_HANDLE(&client->dev)) {
351 r = nxp_nci_i2c_acpi_config(phy);
352 if (r < 0)
353 goto probe_exit;
354 goto nci_probe;
355 } else {
356 nfc_err(&client->dev, "No platform data\n");
357 r = -EINVAL;
358 goto probe_exit;
359 }
360
361 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
362 GPIOF_OUT_INIT_LOW, "nxp_nci_en");
363 if (r < 0)
364 goto probe_exit;
365
366 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
367 GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
368 if (r < 0)
369 goto probe_exit;
370
371nci_probe:
372 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
373 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
374 if (r < 0)
375 goto probe_exit;
376
377 r = request_threaded_irq(client->irq, NULL,
378 nxp_nci_i2c_irq_thread_fn,
379 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
380 NXP_NCI_I2C_DRIVER_NAME, phy);
381 if (r < 0)
382 nfc_err(&client->dev, "Unable to register IRQ handler\n");
383
384probe_exit:
385 return r;
386}
387
388static int nxp_nci_i2c_remove(struct i2c_client *client)
389{
390 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
391
392 nxp_nci_remove(phy->ndev);
393 free_irq(client->irq, phy);
394
395 return 0;
396}
397
398static struct i2c_device_id nxp_nci_i2c_id_table[] = {
399 {"nxp-nci_i2c", 0},
400 {}
401};
402MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
403
404static const struct of_device_id of_nxp_nci_i2c_match[] = {
405 { .compatible = "nxp,nxp-nci-i2c", },
406 {},
407};
408MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
409
410#ifdef CONFIG_ACPI
411static struct acpi_device_id acpi_id[] = {
412 { "NXP7471" },
413 { },
414};
415MODULE_DEVICE_TABLE(acpi, acpi_id);
416#endif
417
418static struct i2c_driver nxp_nci_i2c_driver = {
419 .driver = {
420 .name = NXP_NCI_I2C_DRIVER_NAME,
421 .owner = THIS_MODULE,
422 .acpi_match_table = ACPI_PTR(acpi_id),
423 .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
424 },
425 .probe = nxp_nci_i2c_probe,
426 .id_table = nxp_nci_i2c_id_table,
427 .remove = nxp_nci_i2c_remove,
428};
429
430module_i2c_driver(nxp_nci_i2c_driver);
431
432MODULE_LICENSE("GPL");
433MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
434MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
435MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");