blob: 2c58bd32c9912d36c4df44fd96d05e00253fcec7 [file] [log] [blame]
Clément Perrochaud6be88672015-03-09 11:12:05 +01001/*
2 * I2C link layer for the NXP NCI driver
3 *
4 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +03005 * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
Clément Perrochaud6be88672015-03-09 11:12:05 +01006 *
7 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +03008 * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
Clément Perrochaud6be88672015-03-09 11:12:05 +01009 *
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
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +030028#include <linux/acpi.h>
Clément Perrochaud6be88672015-03-09 11:12:05 +010029#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/of_gpio.h>
36#include <linux/of_irq.h>
37#include <linux/platform_data/nxp-nci.h>
38#include <linux/unaligned/access_ok.h>
39
40#include <net/nfc/nfc.h>
41
42#include "nxp-nci.h"
43
44#define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
45
46#define NXP_NCI_I2C_MAX_PAYLOAD 32
47
48struct nxp_nci_i2c_phy {
49 struct i2c_client *i2c_dev;
50 struct nci_dev *ndev;
51
52 unsigned int gpio_en;
53 unsigned int gpio_fw;
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +030054 unsigned int gpio_irq;
Clément Perrochaud6be88672015-03-09 11:12:05 +010055
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 == -EREMOTEIO) {
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 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
266#ifdef CONFIG_OF
267
268static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
269{
270 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
271 struct device_node *pp;
272 int r;
273
274 pp = client->dev.of_node;
275 if (!pp)
276 return -ENODEV;
277
278 r = of_get_named_gpio(pp, "enable-gpios", 0);
279 if (r == -EPROBE_DEFER)
280 r = of_get_named_gpio(pp, "enable-gpios", 0);
281 if (r < 0) {
282 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
283 return r;
284 }
285 phy->gpio_en = r;
286
287 r = of_get_named_gpio(pp, "firmware-gpios", 0);
288 if (r == -EPROBE_DEFER)
289 r = of_get_named_gpio(pp, "firmware-gpios", 0);
290 if (r < 0) {
291 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
292 return r;
293 }
294 phy->gpio_fw = r;
295
296 r = irq_of_parse_and_map(pp, 0);
297 if (r < 0) {
298 nfc_err(&client->dev, "Unable to get irq, error: %d\n", r);
299 return r;
300 }
301 client->irq = r;
302
303 return 0;
304}
305
306#else
307
308static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
309{
310 return -ENODEV;
311}
312
313#endif
314
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300315static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
316{
317 struct i2c_client *client = phy->i2c_dev;
318 struct gpio_desc *gpiod_en, *gpiod_fw, *gpiod_irq;
319
320 gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2);
321 gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1);
322 gpiod_irq = devm_gpiod_get_index(&client->dev, NULL, 0);
323
324 if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw) || IS_ERR(gpiod_irq)) {
325 nfc_err(&client->dev, "No GPIOs\n");
326 return -EINVAL;
327 }
328
329 gpiod_direction_output(gpiod_en, 0);
330 gpiod_direction_output(gpiod_fw, 0);
331 gpiod_direction_input(gpiod_irq);
332
333 client->irq = gpiod_to_irq(gpiod_irq);
334 if (client->irq < 0) {
335 nfc_err(&client->dev, "No IRQ\n");
336 return -EINVAL;
337 }
338
339 phy->gpio_en = desc_to_gpio(gpiod_en);
340 phy->gpio_fw = desc_to_gpio(gpiod_fw);
341 phy->gpio_irq = desc_to_gpio(gpiod_irq);
342
343 return 0;
344}
345
Clément Perrochaud6be88672015-03-09 11:12:05 +0100346static int nxp_nci_i2c_probe(struct i2c_client *client,
347 const struct i2c_device_id *id)
348{
349 struct nxp_nci_i2c_phy *phy;
350 struct nxp_nci_nfc_platform_data *pdata;
351 int r;
352
353 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
354 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
355 r = -ENODEV;
356 goto probe_exit;
357 }
358
359 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
360 GFP_KERNEL);
361 if (!phy) {
362 r = -ENOMEM;
363 goto probe_exit;
364 }
365
366 phy->i2c_dev = client;
367 i2c_set_clientdata(client, phy);
368
369 pdata = client->dev.platform_data;
370
371 if (!pdata && client->dev.of_node) {
372 r = nxp_nci_i2c_parse_devtree(client);
373 if (r < 0) {
374 nfc_err(&client->dev, "Failed to get DT data\n");
375 goto probe_exit;
376 }
377 } else if (pdata) {
378 phy->gpio_en = pdata->gpio_en;
379 phy->gpio_fw = pdata->gpio_fw;
380 client->irq = pdata->irq;
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300381 } else if (ACPI_HANDLE(&client->dev)) {
382 r = nxp_nci_i2c_acpi_config(phy);
383 if (r < 0)
384 goto probe_exit;
385 goto nci_probe;
Clément Perrochaud6be88672015-03-09 11:12:05 +0100386 } else {
387 nfc_err(&client->dev, "No platform data\n");
388 r = -EINVAL;
389 goto probe_exit;
390 }
391
392 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
393 GPIOF_OUT_INIT_LOW, "nxp_nci_en");
394 if (r < 0)
395 goto probe_exit;
396
397 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
398 GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
399 if (r < 0)
400 goto probe_exit;
401
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300402nci_probe:
Clément Perrochaud6be88672015-03-09 11:12:05 +0100403 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
404 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
405 if (r < 0)
406 goto probe_exit;
407
408 r = request_threaded_irq(client->irq, NULL,
409 nxp_nci_i2c_irq_thread_fn,
410 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
411 NXP_NCI_I2C_DRIVER_NAME, phy);
412 if (r < 0)
413 nfc_err(&client->dev, "Unable to register IRQ handler\n");
414
415probe_exit:
416 return r;
417}
418
419static int nxp_nci_i2c_remove(struct i2c_client *client)
420{
421 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
422
423 nxp_nci_remove(phy->ndev);
424 free_irq(client->irq, phy);
425
426 return 0;
427}
428
429static struct i2c_device_id nxp_nci_i2c_id_table[] = {
430 {"nxp-nci_i2c", 0},
431 {}
432};
433MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
434
435static const struct of_device_id of_nxp_nci_i2c_match[] = {
436 { .compatible = "nxp,nxp-nci-i2c", },
437 {},
438};
439MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
440
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300441#ifdef CONFIG_ACPI
442static struct acpi_device_id acpi_id[] = {
443 { "NXP7471" },
444 { },
445};
446MODULE_DEVICE_TABLE(acpi, acpi_id);
447#endif
448
Clément Perrochaud6be88672015-03-09 11:12:05 +0100449static struct i2c_driver nxp_nci_i2c_driver = {
450 .driver = {
451 .name = NXP_NCI_I2C_DRIVER_NAME,
452 .owner = THIS_MODULE,
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300453 .acpi_match_table = ACPI_PTR(acpi_id),
Clément Perrochaud6be88672015-03-09 11:12:05 +0100454 .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
455 },
456 .probe = nxp_nci_i2c_probe,
457 .id_table = nxp_nci_i2c_id_table,
458 .remove = nxp_nci_i2c_remove,
459};
460
461module_i2c_driver(nxp_nci_i2c_driver);
462
463MODULE_LICENSE("GPL");
464MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
465MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300466MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");