blob: fa75c53f3fa53878fc6b21136aa2f14d410de59b [file] [log] [blame]
Eric Lapuyade97f18412012-10-02 18:44:06 +02001/*
2 * I2C Link Layer for PN544 HCI based Driver
3 *
4 * Copyright (C) 2012 Intel Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsher98b32de2013-12-06 08:56:16 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Eric Lapuyade97f18412012-10-02 18:44:06 +020017 */
18
Joe Perches17936b42013-04-05 12:27:39 -070019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Eric Lapuyade97f18412012-10-02 18:44:06 +020021#include <linux/crc-ccitt.h>
22#include <linux/module.h>
23#include <linux/i2c.h>
24#include <linux/gpio.h>
Clement Perrochaudeda85652014-04-02 09:02:39 +000025#include <linux/of_gpio.h>
26#include <linux/of_irq.h>
Robert Dolca0a5942c2015-01-26 13:13:37 +020027#include <linux/acpi.h>
Eric Lapuyade97f18412012-10-02 18:44:06 +020028#include <linux/miscdevice.h>
29#include <linux/interrupt.h>
30#include <linux/delay.h>
Eric Lapuyade06c66032013-07-19 14:59:45 +020031#include <linux/nfc.h>
32#include <linux/firmware.h>
Robert Dolca0a5942c2015-01-26 13:13:37 +020033#include <linux/gpio/consumer.h>
Marcel Holtmann61cdb012012-10-24 11:45:26 -070034#include <linux/platform_data/pn544.h>
Johannes Bergdb083bc2014-11-19 21:17:22 +010035#include <asm/unaligned.h>
Eric Lapuyade97f18412012-10-02 18:44:06 +020036
37#include <net/nfc/hci.h>
38#include <net/nfc/llc.h>
Eric Lapuyade06c66032013-07-19 14:59:45 +020039#include <net/nfc/nfc.h>
Eric Lapuyade97f18412012-10-02 18:44:06 +020040
41#include "pn544.h"
42
43#define PN544_I2C_FRAME_HEADROOM 1
44#define PN544_I2C_FRAME_TAILROOM 2
45
Robert Dolca0a5942c2015-01-26 13:13:37 +020046/* GPIO names */
47#define PN544_GPIO_NAME_IRQ "pn544_irq"
48#define PN544_GPIO_NAME_FW "pn544_fw"
49#define PN544_GPIO_NAME_EN "pn544_en"
50
Eric Lapuyade97f18412012-10-02 18:44:06 +020051/* framing in HCI mode */
52#define PN544_HCI_I2C_LLC_LEN 1
53#define PN544_HCI_I2C_LLC_CRC 2
54#define PN544_HCI_I2C_LLC_LEN_CRC (PN544_HCI_I2C_LLC_LEN + \
55 PN544_HCI_I2C_LLC_CRC)
56#define PN544_HCI_I2C_LLC_MIN_SIZE (1 + PN544_HCI_I2C_LLC_LEN_CRC)
57#define PN544_HCI_I2C_LLC_MAX_PAYLOAD 29
58#define PN544_HCI_I2C_LLC_MAX_SIZE (PN544_HCI_I2C_LLC_LEN_CRC + 1 + \
59 PN544_HCI_I2C_LLC_MAX_PAYLOAD)
60
61static struct i2c_device_id pn544_hci_i2c_id_table[] = {
62 {"pn544", 0},
63 {}
64};
65
66MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
67
Robert Dolca0a5942c2015-01-26 13:13:37 +020068static const struct acpi_device_id pn544_hci_i2c_acpi_match[] = {
69 {"NXP5440", 0},
70 {}
71};
72
73MODULE_DEVICE_TABLE(acpi, pn544_hci_i2c_acpi_match);
74
Eric Lapuyade97f18412012-10-02 18:44:06 +020075#define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
76
Arron Wang971d63c2013-12-11 17:25:23 +080077/*
78 * Exposed through the 4 most significant bytes
79 * from the HCI SW_VERSION first byte, a.k.a.
80 * SW RomLib.
81 */
82#define PN544_HW_VARIANT_C2 0xa
83#define PN544_HW_VARIANT_C3 0xb
84
Arron Wangf1dd56f2013-12-11 17:25:24 +080085#define PN544_FW_CMD_RESET 0x01
Eric Lapuyade06c66032013-07-19 14:59:45 +020086#define PN544_FW_CMD_WRITE 0x08
87#define PN544_FW_CMD_CHECK 0x06
Arron Wangf1dd56f2013-12-11 17:25:24 +080088#define PN544_FW_CMD_SECURE_WRITE 0x0C
89#define PN544_FW_CMD_SECURE_CHUNK_WRITE 0x0D
Eric Lapuyade06c66032013-07-19 14:59:45 +020090
91struct pn544_i2c_fw_frame_write {
92 u8 cmd;
93 u16 be_length;
94 u8 be_dest_addr[3];
95 u16 be_datalen;
96 u8 data[];
97} __packed;
98
99struct pn544_i2c_fw_frame_check {
100 u8 cmd;
101 u16 be_length;
102 u8 be_start_addr[3];
103 u16 be_datalen;
104 u16 be_crc;
105} __packed;
106
107struct pn544_i2c_fw_frame_response {
108 u8 status;
109 u16 be_length;
110} __packed;
111
112struct pn544_i2c_fw_blob {
113 u32 be_size;
114 u32 be_destaddr;
115 u8 data[];
116};
117
Arron Wangf1dd56f2013-12-11 17:25:24 +0800118struct pn544_i2c_fw_secure_frame {
119 u8 cmd;
120 u16 be_datalen;
121 u8 data[];
122} __packed;
123
124struct pn544_i2c_fw_secure_blob {
125 u64 header;
126 u8 data[];
127};
128
Eric Lapuyade06c66032013-07-19 14:59:45 +0200129#define PN544_FW_CMD_RESULT_TIMEOUT 0x01
130#define PN544_FW_CMD_RESULT_BAD_CRC 0x02
131#define PN544_FW_CMD_RESULT_ACCESS_DENIED 0x08
132#define PN544_FW_CMD_RESULT_PROTOCOL_ERROR 0x0B
133#define PN544_FW_CMD_RESULT_INVALID_PARAMETER 0x11
Arron Wangf1dd56f2013-12-11 17:25:24 +0800134#define PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND 0x13
Eric Lapuyade06c66032013-07-19 14:59:45 +0200135#define PN544_FW_CMD_RESULT_INVALID_LENGTH 0x18
Arron Wangf1dd56f2013-12-11 17:25:24 +0800136#define PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR 0x19
137#define PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR 0x1D
138#define PN544_FW_CMD_RESULT_MEMORY_ERROR 0x20
139#define PN544_FW_CMD_RESULT_CHUNK_OK 0x21
Eric Lapuyade06c66032013-07-19 14:59:45 +0200140#define PN544_FW_CMD_RESULT_WRITE_FAILED 0x74
Arron Wangf1dd56f2013-12-11 17:25:24 +0800141#define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0
142#define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6
Eric Lapuyade06c66032013-07-19 14:59:45 +0200143
144#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
145
146#define PN544_FW_WRITE_BUFFER_MAX_LEN 0x9f7
147#define PN544_FW_I2C_MAX_PAYLOAD PN544_HCI_I2C_LLC_MAX_SIZE
148#define PN544_FW_I2C_WRITE_FRAME_HEADER_LEN 8
149#define PN544_FW_I2C_WRITE_DATA_MAX_LEN MIN((PN544_FW_I2C_MAX_PAYLOAD -\
150 PN544_FW_I2C_WRITE_FRAME_HEADER_LEN),\
151 PN544_FW_WRITE_BUFFER_MAX_LEN)
Arron Wangf1dd56f2013-12-11 17:25:24 +0800152#define PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN 3
153#define PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN (PN544_FW_I2C_MAX_PAYLOAD -\
154 PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN)
155#define PN544_FW_SECURE_FRAME_HEADER_LEN 3
156#define PN544_FW_SECURE_BLOB_HEADER_LEN 8
Eric Lapuyade06c66032013-07-19 14:59:45 +0200157
158#define FW_WORK_STATE_IDLE 1
159#define FW_WORK_STATE_START 2
160#define FW_WORK_STATE_WAIT_WRITE_ANSWER 3
161#define FW_WORK_STATE_WAIT_CHECK_ANSWER 4
Arron Wangf1dd56f2013-12-11 17:25:24 +0800162#define FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER 5
Eric Lapuyade06c66032013-07-19 14:59:45 +0200163
Eric Lapuyade97f18412012-10-02 18:44:06 +0200164struct pn544_i2c_phy {
165 struct i2c_client *i2c_dev;
166 struct nfc_hci_dev *hdev;
167
168 unsigned int gpio_en;
169 unsigned int gpio_irq;
170 unsigned int gpio_fw;
171 unsigned int en_polarity;
172
Arron Wang971d63c2013-12-11 17:25:23 +0800173 u8 hw_variant;
174
Eric Lapuyade06c66032013-07-19 14:59:45 +0200175 struct work_struct fw_work;
176 int fw_work_state;
177 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
178 const struct firmware *fw;
179 u32 fw_blob_dest_addr;
180 size_t fw_blob_size;
181 const u8 *fw_blob_data;
182 size_t fw_written;
Arron Wangf1dd56f2013-12-11 17:25:24 +0800183 size_t fw_size;
184
Eric Lapuyade06c66032013-07-19 14:59:45 +0200185 int fw_cmd_result;
186
Eric Lapuyade97f18412012-10-02 18:44:06 +0200187 int powered;
Eric Lapuyadeeab10b72013-07-19 14:57:13 +0200188 int run_mode;
Eric Lapuyade97f18412012-10-02 18:44:06 +0200189
190 int hard_fault; /*
191 * < 0 if hardware error occured (e.g. i2c err)
192 * and prevents normal operation.
193 */
194};
195
196#define I2C_DUMP_SKB(info, skb) \
197do { \
198 pr_debug("%s:\n", info); \
199 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
200 16, 1, (skb)->data, (skb)->len, 0); \
201} while (0)
202
203static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy *phy)
204{
205 int polarity, retry, ret;
206 char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
207 int count = sizeof(rset_cmd);
208
Joe Perches17936b42013-04-05 12:27:39 -0700209 nfc_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n");
Eric Lapuyade97f18412012-10-02 18:44:06 +0200210
211 /* Disable fw download */
Robert Dolca75dda422015-01-26 13:13:36 +0200212 gpio_set_value_cansleep(phy->gpio_fw, 0);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200213
214 for (polarity = 0; polarity < 2; polarity++) {
215 phy->en_polarity = polarity;
216 retry = 3;
217 while (retry--) {
218 /* power off */
Robert Dolca75dda422015-01-26 13:13:36 +0200219 gpio_set_value_cansleep(phy->gpio_en,
220 !phy->en_polarity);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200221 usleep_range(10000, 15000);
222
223 /* power on */
Robert Dolca75dda422015-01-26 13:13:36 +0200224 gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200225 usleep_range(10000, 15000);
226
227 /* send reset */
228 dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n");
229 ret = i2c_master_send(phy->i2c_dev, rset_cmd, count);
230 if (ret == count) {
Joe Perches17936b42013-04-05 12:27:39 -0700231 nfc_info(&phy->i2c_dev->dev,
Eric Lapuyade97f18412012-10-02 18:44:06 +0200232 "nfc_en polarity : active %s\n",
233 (polarity == 0 ? "low" : "high"));
234 goto out;
235 }
236 }
237 }
238
Joe Perches17936b42013-04-05 12:27:39 -0700239 nfc_err(&phy->i2c_dev->dev,
Eric Lapuyade97f18412012-10-02 18:44:06 +0200240 "Could not detect nfc_en polarity, fallback to active high\n");
241
242out:
Robert Dolca75dda422015-01-26 13:13:36 +0200243 gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200244}
245
Eric Lapuyadeeab10b72013-07-19 14:57:13 +0200246static void pn544_hci_i2c_enable_mode(struct pn544_i2c_phy *phy, int run_mode)
247{
Robert Dolca75dda422015-01-26 13:13:36 +0200248 gpio_set_value_cansleep(phy->gpio_fw,
249 run_mode == PN544_FW_MODE ? 1 : 0);
250 gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
Eric Lapuyadeeab10b72013-07-19 14:57:13 +0200251 usleep_range(10000, 15000);
252
253 phy->run_mode = run_mode;
254}
255
Eric Lapuyade97f18412012-10-02 18:44:06 +0200256static int pn544_hci_i2c_enable(void *phy_id)
257{
258 struct pn544_i2c_phy *phy = phy_id;
259
Joe Perches17936b42013-04-05 12:27:39 -0700260 pr_info("%s\n", __func__);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200261
Eric Lapuyadeeab10b72013-07-19 14:57:13 +0200262 pn544_hci_i2c_enable_mode(phy, PN544_HCI_MODE);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200263
264 phy->powered = 1;
265
266 return 0;
267}
268
269static void pn544_hci_i2c_disable(void *phy_id)
270{
271 struct pn544_i2c_phy *phy = phy_id;
272
Robert Dolca75dda422015-01-26 13:13:36 +0200273 gpio_set_value_cansleep(phy->gpio_fw, 0);
274 gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200275 usleep_range(10000, 15000);
276
Robert Dolca75dda422015-01-26 13:13:36 +0200277 gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200278 usleep_range(10000, 15000);
279
Robert Dolca75dda422015-01-26 13:13:36 +0200280 gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200281 usleep_range(10000, 15000);
282
283 phy->powered = 0;
284}
285
286static void pn544_hci_i2c_add_len_crc(struct sk_buff *skb)
287{
288 u16 crc;
289 int len;
290
291 len = skb->len + 2;
292 *skb_push(skb, 1) = len;
293
294 crc = crc_ccitt(0xffff, skb->data, skb->len);
295 crc = ~crc;
296 *skb_put(skb, 1) = crc & 0xff;
297 *skb_put(skb, 1) = crc >> 8;
298}
299
300static void pn544_hci_i2c_remove_len_crc(struct sk_buff *skb)
301{
302 skb_pull(skb, PN544_I2C_FRAME_HEADROOM);
303 skb_trim(skb, PN544_I2C_FRAME_TAILROOM);
304}
305
306/*
307 * Writing a frame must not return the number of written bytes.
308 * It must return either zero for success, or <0 for error.
309 * In addition, it must not alter the skb
310 */
311static int pn544_hci_i2c_write(void *phy_id, struct sk_buff *skb)
312{
313 int r;
314 struct pn544_i2c_phy *phy = phy_id;
315 struct i2c_client *client = phy->i2c_dev;
316
317 if (phy->hard_fault != 0)
318 return phy->hard_fault;
319
320 usleep_range(3000, 6000);
321
322 pn544_hci_i2c_add_len_crc(skb);
323
324 I2C_DUMP_SKB("i2c frame written", skb);
325
326 r = i2c_master_send(client, skb->data, skb->len);
327
328 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
329 usleep_range(6000, 10000);
330 r = i2c_master_send(client, skb->data, skb->len);
331 }
332
333 if (r >= 0) {
334 if (r != skb->len)
335 r = -EREMOTEIO;
336 else
337 r = 0;
338 }
339
340 pn544_hci_i2c_remove_len_crc(skb);
341
342 return r;
343}
344
345static int check_crc(u8 *buf, int buflen)
346{
347 int len;
348 u16 crc;
349
350 len = buf[0] + 1;
351 crc = crc_ccitt(0xffff, buf, len - 2);
352 crc = ~crc;
353
354 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
Joe Perches17936b42013-04-05 12:27:39 -0700355 pr_err("CRC error 0x%x != 0x%x 0x%x\n",
Eric Lapuyade97f18412012-10-02 18:44:06 +0200356 crc, buf[len - 1], buf[len - 2]);
Joe Perches17936b42013-04-05 12:27:39 -0700357 pr_info("%s: BAD CRC\n", __func__);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200358 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
359 16, 2, buf, buflen, false);
360 return -EPERM;
361 }
362 return 0;
363}
364
365/*
366 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
367 * that i2c bus will be flushed and that next read will start on a new frame.
368 * returned skb contains only LLC header and payload.
369 * returns:
370 * -EREMOTEIO : i2c read error (fatal)
371 * -EBADMSG : frame was incorrect and discarded
372 * -ENOMEM : cannot allocate skb, frame dropped
373 */
374static int pn544_hci_i2c_read(struct pn544_i2c_phy *phy, struct sk_buff **skb)
375{
376 int r;
377 u8 len;
378 u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1];
379 struct i2c_client *client = phy->i2c_dev;
380
381 r = i2c_master_recv(client, &len, 1);
382 if (r != 1) {
Joe Perches17936b42013-04-05 12:27:39 -0700383 nfc_err(&client->dev, "cannot read len byte\n");
Eric Lapuyade97f18412012-10-02 18:44:06 +0200384 return -EREMOTEIO;
385 }
386
387 if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) ||
388 (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) {
Joe Perches17936b42013-04-05 12:27:39 -0700389 nfc_err(&client->dev, "invalid len byte\n");
Eric Lapuyade97f18412012-10-02 18:44:06 +0200390 r = -EBADMSG;
391 goto flush;
392 }
393
394 *skb = alloc_skb(1 + len, GFP_KERNEL);
395 if (*skb == NULL) {
396 r = -ENOMEM;
397 goto flush;
398 }
399
400 *skb_put(*skb, 1) = len;
401
402 r = i2c_master_recv(client, skb_put(*skb, len), len);
403 if (r != len) {
404 kfree_skb(*skb);
405 return -EREMOTEIO;
406 }
407
408 I2C_DUMP_SKB("i2c frame read", *skb);
409
410 r = check_crc((*skb)->data, (*skb)->len);
411 if (r != 0) {
412 kfree_skb(*skb);
413 r = -EBADMSG;
414 goto flush;
415 }
416
417 skb_pull(*skb, 1);
418 skb_trim(*skb, (*skb)->len - 2);
419
420 usleep_range(3000, 6000);
421
422 return 0;
423
424flush:
425 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
426 r = -EREMOTEIO;
427
428 usleep_range(3000, 6000);
429
430 return r;
431}
432
Eric Lapuyade06c66032013-07-19 14:59:45 +0200433static int pn544_hci_i2c_fw_read_status(struct pn544_i2c_phy *phy)
434{
435 int r;
436 struct pn544_i2c_fw_frame_response response;
437 struct i2c_client *client = phy->i2c_dev;
438
439 r = i2c_master_recv(client, (char *) &response, sizeof(response));
440 if (r != sizeof(response)) {
Joe Perches17936b42013-04-05 12:27:39 -0700441 nfc_err(&client->dev, "cannot read fw status\n");
Eric Lapuyade06c66032013-07-19 14:59:45 +0200442 return -EIO;
443 }
444
445 usleep_range(3000, 6000);
446
447 switch (response.status) {
448 case 0:
449 return 0;
Arron Wangf1dd56f2013-12-11 17:25:24 +0800450 case PN544_FW_CMD_RESULT_CHUNK_OK:
451 return response.status;
Eric Lapuyade06c66032013-07-19 14:59:45 +0200452 case PN544_FW_CMD_RESULT_TIMEOUT:
453 return -ETIMEDOUT;
454 case PN544_FW_CMD_RESULT_BAD_CRC:
455 return -ENODATA;
456 case PN544_FW_CMD_RESULT_ACCESS_DENIED:
457 return -EACCES;
458 case PN544_FW_CMD_RESULT_PROTOCOL_ERROR:
459 return -EPROTO;
460 case PN544_FW_CMD_RESULT_INVALID_PARAMETER:
461 return -EINVAL;
Arron Wangf1dd56f2013-12-11 17:25:24 +0800462 case PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND:
463 return -ENOTSUPP;
Eric Lapuyade06c66032013-07-19 14:59:45 +0200464 case PN544_FW_CMD_RESULT_INVALID_LENGTH:
465 return -EBADMSG;
Arron Wangf1dd56f2013-12-11 17:25:24 +0800466 case PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR:
467 return -ENOKEY;
468 case PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR:
469 return -EINVAL;
470 case PN544_FW_CMD_RESULT_MEMORY_ERROR:
471 return -ENOMEM;
472 case PN544_FW_CMD_RESULT_COMMAND_REJECTED:
473 return -EACCES;
Eric Lapuyade06c66032013-07-19 14:59:45 +0200474 case PN544_FW_CMD_RESULT_WRITE_FAILED:
Arron Wangf1dd56f2013-12-11 17:25:24 +0800475 case PN544_FW_CMD_RESULT_CHUNK_ERROR:
Eric Lapuyade06c66032013-07-19 14:59:45 +0200476 return -EIO;
477 default:
478 return -EIO;
479 }
480}
481
Eric Lapuyade97f18412012-10-02 18:44:06 +0200482/*
483 * Reads an shdlc frame from the chip. This is not as straightforward as it
484 * seems. There are cases where we could loose the frame start synchronization.
485 * The frame format is len-data-crc, and corruption can occur anywhere while
486 * transiting on i2c bus, such that we could read an invalid len.
487 * In order to recover synchronization with the next frame, we must be sure
488 * to read the real amount of data without using the len byte. We do this by
489 * assuming the following:
490 * - the chip will always present only one single complete frame on the bus
491 * before triggering the interrupt
492 * - the chip will not present a new frame until we have completely read
493 * the previous one (or until we have handled the interrupt).
494 * The tricky case is when we read a corrupted len that is less than the real
495 * len. We must detect this here in order to determine that we need to flush
496 * the bus. This is the reason why we check the crc here.
497 */
498static irqreturn_t pn544_hci_i2c_irq_thread_fn(int irq, void *phy_id)
499{
500 struct pn544_i2c_phy *phy = phy_id;
501 struct i2c_client *client;
502 struct sk_buff *skb = NULL;
503 int r;
504
505 if (!phy || irq != phy->i2c_dev->irq) {
506 WARN_ON_ONCE(1);
507 return IRQ_NONE;
508 }
509
510 client = phy->i2c_dev;
511 dev_dbg(&client->dev, "IRQ\n");
512
513 if (phy->hard_fault != 0)
514 return IRQ_HANDLED;
515
Eric Lapuyade06c66032013-07-19 14:59:45 +0200516 if (phy->run_mode == PN544_FW_MODE) {
517 phy->fw_cmd_result = pn544_hci_i2c_fw_read_status(phy);
518 schedule_work(&phy->fw_work);
519 } else {
520 r = pn544_hci_i2c_read(phy, &skb);
521 if (r == -EREMOTEIO) {
522 phy->hard_fault = r;
Eric Lapuyade97f18412012-10-02 18:44:06 +0200523
Eric Lapuyade06c66032013-07-19 14:59:45 +0200524 nfc_hci_recv_frame(phy->hdev, NULL);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200525
Eric Lapuyade06c66032013-07-19 14:59:45 +0200526 return IRQ_HANDLED;
527 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
528 return IRQ_HANDLED;
529 }
530
531 nfc_hci_recv_frame(phy->hdev, skb);
Eric Lapuyade97f18412012-10-02 18:44:06 +0200532 }
Eric Lapuyade97f18412012-10-02 18:44:06 +0200533 return IRQ_HANDLED;
534}
535
536static struct nfc_phy_ops i2c_phy_ops = {
537 .write = pn544_hci_i2c_write,
538 .enable = pn544_hci_i2c_enable,
539 .disable = pn544_hci_i2c_disable,
540};
541
Arron Wang971d63c2013-12-11 17:25:23 +0800542static int pn544_hci_i2c_fw_download(void *phy_id, const char *firmware_name,
543 u8 hw_variant)
Eric Lapuyade06c66032013-07-19 14:59:45 +0200544{
545 struct pn544_i2c_phy *phy = phy_id;
546
Joe Perches17936b42013-04-05 12:27:39 -0700547 pr_info("Starting Firmware Download (%s)\n", firmware_name);
Eric Lapuyade06c66032013-07-19 14:59:45 +0200548
549 strcpy(phy->firmware_name, firmware_name);
550
Arron Wang971d63c2013-12-11 17:25:23 +0800551 phy->hw_variant = hw_variant;
Eric Lapuyade06c66032013-07-19 14:59:45 +0200552 phy->fw_work_state = FW_WORK_STATE_START;
553
554 schedule_work(&phy->fw_work);
555
556 return 0;
557}
558
559static void pn544_hci_i2c_fw_work_complete(struct pn544_i2c_phy *phy,
560 int result)
561{
Joe Perches17936b42013-04-05 12:27:39 -0700562 pr_info("Firmware Download Complete, result=%d\n", result);
Eric Lapuyade06c66032013-07-19 14:59:45 +0200563
564 pn544_hci_i2c_disable(phy);
565
566 phy->fw_work_state = FW_WORK_STATE_IDLE;
567
568 if (phy->fw) {
569 release_firmware(phy->fw);
570 phy->fw = NULL;
571 }
572
573 nfc_fw_download_done(phy->hdev->ndev, phy->firmware_name, (u32) -result);
574}
575
576static int pn544_hci_i2c_fw_write_cmd(struct i2c_client *client, u32 dest_addr,
577 const u8 *data, u16 datalen)
578{
579 u8 frame[PN544_FW_I2C_MAX_PAYLOAD];
580 struct pn544_i2c_fw_frame_write *framep;
581 u16 params_len;
582 int framelen;
583 int r;
584
585 if (datalen > PN544_FW_I2C_WRITE_DATA_MAX_LEN)
586 datalen = PN544_FW_I2C_WRITE_DATA_MAX_LEN;
587
588 framep = (struct pn544_i2c_fw_frame_write *) frame;
589
590 params_len = sizeof(framep->be_dest_addr) +
591 sizeof(framep->be_datalen) + datalen;
592 framelen = params_len + sizeof(framep->cmd) +
593 sizeof(framep->be_length);
594
595 framep->cmd = PN544_FW_CMD_WRITE;
596
597 put_unaligned_be16(params_len, &framep->be_length);
598
599 framep->be_dest_addr[0] = (dest_addr & 0xff0000) >> 16;
600 framep->be_dest_addr[1] = (dest_addr & 0xff00) >> 8;
601 framep->be_dest_addr[2] = dest_addr & 0xff;
602
603 put_unaligned_be16(datalen, &framep->be_datalen);
604
605 memcpy(framep->data, data, datalen);
606
607 r = i2c_master_send(client, frame, framelen);
608
609 if (r == framelen)
610 return datalen;
611 else if (r < 0)
612 return r;
613 else
614 return -EIO;
615}
616
617static int pn544_hci_i2c_fw_check_cmd(struct i2c_client *client, u32 start_addr,
618 const u8 *data, u16 datalen)
619{
620 struct pn544_i2c_fw_frame_check frame;
621 int r;
622 u16 crc;
623
624 /* calculate local crc for the data we want to check */
625 crc = crc_ccitt(0xffff, data, datalen);
626
627 frame.cmd = PN544_FW_CMD_CHECK;
628
629 put_unaligned_be16(sizeof(frame.be_start_addr) +
630 sizeof(frame.be_datalen) + sizeof(frame.be_crc),
631 &frame.be_length);
632
633 /* tell the chip the memory region to which our crc applies */
634 frame.be_start_addr[0] = (start_addr & 0xff0000) >> 16;
635 frame.be_start_addr[1] = (start_addr & 0xff00) >> 8;
636 frame.be_start_addr[2] = start_addr & 0xff;
637
638 put_unaligned_be16(datalen, &frame.be_datalen);
639
640 /*
641 * and give our local crc. Chip will calculate its own crc for the
642 * region and compare with ours.
643 */
644 put_unaligned_be16(crc, &frame.be_crc);
645
646 r = i2c_master_send(client, (const char *) &frame, sizeof(frame));
647
648 if (r == sizeof(frame))
649 return 0;
650 else if (r < 0)
651 return r;
652 else
653 return -EIO;
654}
655
656static int pn544_hci_i2c_fw_write_chunk(struct pn544_i2c_phy *phy)
657{
658 int r;
659
660 r = pn544_hci_i2c_fw_write_cmd(phy->i2c_dev,
661 phy->fw_blob_dest_addr + phy->fw_written,
662 phy->fw_blob_data + phy->fw_written,
663 phy->fw_blob_size - phy->fw_written);
664 if (r < 0)
665 return r;
666
667 phy->fw_written += r;
668 phy->fw_work_state = FW_WORK_STATE_WAIT_WRITE_ANSWER;
669
670 return 0;
671}
672
Arron Wangf1dd56f2013-12-11 17:25:24 +0800673static int pn544_hci_i2c_fw_secure_write_frame_cmd(struct pn544_i2c_phy *phy,
674 const u8 *data, u16 datalen)
675{
676 u8 buf[PN544_FW_I2C_MAX_PAYLOAD];
677 struct pn544_i2c_fw_secure_frame *chunk;
678 int chunklen;
679 int r;
680
681 if (datalen > PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN)
682 datalen = PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN;
683
684 chunk = (struct pn544_i2c_fw_secure_frame *) buf;
685
686 chunk->cmd = PN544_FW_CMD_SECURE_CHUNK_WRITE;
687
688 put_unaligned_be16(datalen, &chunk->be_datalen);
689
690 memcpy(chunk->data, data, datalen);
691
692 chunklen = sizeof(chunk->cmd) + sizeof(chunk->be_datalen) + datalen;
693
694 r = i2c_master_send(phy->i2c_dev, buf, chunklen);
695
696 if (r == chunklen)
697 return datalen;
698 else if (r < 0)
699 return r;
700 else
701 return -EIO;
702
703}
704
705static int pn544_hci_i2c_fw_secure_write_frame(struct pn544_i2c_phy *phy)
706{
707 struct pn544_i2c_fw_secure_frame *framep;
708 int r;
709
710 framep = (struct pn544_i2c_fw_secure_frame *) phy->fw_blob_data;
711 if (phy->fw_written == 0)
712 phy->fw_blob_size = get_unaligned_be16(&framep->be_datalen)
713 + PN544_FW_SECURE_FRAME_HEADER_LEN;
714
715 /* Only secure write command can be chunked*/
716 if (phy->fw_blob_size > PN544_FW_I2C_MAX_PAYLOAD &&
717 framep->cmd != PN544_FW_CMD_SECURE_WRITE)
718 return -EINVAL;
719
720 /* The firmware also have other commands, we just send them directly */
721 if (phy->fw_blob_size < PN544_FW_I2C_MAX_PAYLOAD) {
722 r = i2c_master_send(phy->i2c_dev,
723 (const char *) phy->fw_blob_data, phy->fw_blob_size);
724
725 if (r == phy->fw_blob_size)
726 goto exit;
727 else if (r < 0)
728 return r;
729 else
730 return -EIO;
731 }
732
733 r = pn544_hci_i2c_fw_secure_write_frame_cmd(phy,
734 phy->fw_blob_data + phy->fw_written,
735 phy->fw_blob_size - phy->fw_written);
736 if (r < 0)
737 return r;
738
739exit:
740 phy->fw_written += r;
741 phy->fw_work_state = FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER;
742
743 /* SW reset command will not trig any response from PN544 */
744 if (framep->cmd == PN544_FW_CMD_RESET) {
745 pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
746 phy->fw_cmd_result = 0;
747 schedule_work(&phy->fw_work);
748 }
749
750 return 0;
751}
752
Eric Lapuyade06c66032013-07-19 14:59:45 +0200753static void pn544_hci_i2c_fw_work(struct work_struct *work)
754{
755 struct pn544_i2c_phy *phy = container_of(work, struct pn544_i2c_phy,
756 fw_work);
757 int r;
758 struct pn544_i2c_fw_blob *blob;
Arron Wangf1dd56f2013-12-11 17:25:24 +0800759 struct pn544_i2c_fw_secure_blob *secure_blob;
Eric Lapuyade06c66032013-07-19 14:59:45 +0200760
761 switch (phy->fw_work_state) {
762 case FW_WORK_STATE_START:
763 pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
764
765 r = request_firmware(&phy->fw, phy->firmware_name,
766 &phy->i2c_dev->dev);
767 if (r < 0)
768 goto exit_state_start;
769
Eric Lapuyade06c66032013-07-19 14:59:45 +0200770 phy->fw_written = 0;
Arron Wangf1dd56f2013-12-11 17:25:24 +0800771
772 switch (phy->hw_variant) {
773 case PN544_HW_VARIANT_C2:
774 blob = (struct pn544_i2c_fw_blob *) phy->fw->data;
775 phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
776 phy->fw_blob_dest_addr = get_unaligned_be32(
777 &blob->be_destaddr);
778 phy->fw_blob_data = blob->data;
779
780 r = pn544_hci_i2c_fw_write_chunk(phy);
781 break;
782 case PN544_HW_VARIANT_C3:
783 secure_blob = (struct pn544_i2c_fw_secure_blob *)
784 phy->fw->data;
785 phy->fw_blob_data = secure_blob->data;
786 phy->fw_size = phy->fw->size;
787 r = pn544_hci_i2c_fw_secure_write_frame(phy);
788 break;
789 default:
790 r = -ENOTSUPP;
791 break;
792 }
Eric Lapuyade06c66032013-07-19 14:59:45 +0200793
794exit_state_start:
795 if (r < 0)
796 pn544_hci_i2c_fw_work_complete(phy, r);
797 break;
798
799 case FW_WORK_STATE_WAIT_WRITE_ANSWER:
800 r = phy->fw_cmd_result;
801 if (r < 0)
802 goto exit_state_wait_write_answer;
803
804 if (phy->fw_written == phy->fw_blob_size) {
805 r = pn544_hci_i2c_fw_check_cmd(phy->i2c_dev,
806 phy->fw_blob_dest_addr,
807 phy->fw_blob_data,
808 phy->fw_blob_size);
809 if (r < 0)
810 goto exit_state_wait_write_answer;
811 phy->fw_work_state = FW_WORK_STATE_WAIT_CHECK_ANSWER;
812 break;
813 }
814
815 r = pn544_hci_i2c_fw_write_chunk(phy);
816
817exit_state_wait_write_answer:
818 if (r < 0)
819 pn544_hci_i2c_fw_work_complete(phy, r);
820 break;
821
822 case FW_WORK_STATE_WAIT_CHECK_ANSWER:
823 r = phy->fw_cmd_result;
824 if (r < 0)
825 goto exit_state_wait_check_answer;
826
827 blob = (struct pn544_i2c_fw_blob *) (phy->fw_blob_data +
828 phy->fw_blob_size);
829 phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
830 if (phy->fw_blob_size != 0) {
831 phy->fw_blob_dest_addr =
832 get_unaligned_be32(&blob->be_destaddr);
833 phy->fw_blob_data = blob->data;
834
835 phy->fw_written = 0;
836 r = pn544_hci_i2c_fw_write_chunk(phy);
837 }
838
839exit_state_wait_check_answer:
840 if (r < 0 || phy->fw_blob_size == 0)
841 pn544_hci_i2c_fw_work_complete(phy, r);
842 break;
843
Arron Wangf1dd56f2013-12-11 17:25:24 +0800844 case FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER:
845 r = phy->fw_cmd_result;
846 if (r < 0)
847 goto exit_state_wait_secure_write_answer;
848
849 if (r == PN544_FW_CMD_RESULT_CHUNK_OK) {
850 r = pn544_hci_i2c_fw_secure_write_frame(phy);
851 goto exit_state_wait_secure_write_answer;
852 }
853
854 if (phy->fw_written == phy->fw_blob_size) {
855 secure_blob = (struct pn544_i2c_fw_secure_blob *)
856 (phy->fw_blob_data + phy->fw_blob_size);
857 phy->fw_size -= phy->fw_blob_size +
858 PN544_FW_SECURE_BLOB_HEADER_LEN;
859 if (phy->fw_size >= PN544_FW_SECURE_BLOB_HEADER_LEN
860 + PN544_FW_SECURE_FRAME_HEADER_LEN) {
861 phy->fw_blob_data = secure_blob->data;
862
863 phy->fw_written = 0;
864 r = pn544_hci_i2c_fw_secure_write_frame(phy);
865 }
866 }
867
868exit_state_wait_secure_write_answer:
869 if (r < 0 || phy->fw_size == 0)
870 pn544_hci_i2c_fw_work_complete(phy, r);
871 break;
872
Eric Lapuyade06c66032013-07-19 14:59:45 +0200873 default:
874 break;
875 }
876}
877
Robert Dolca0a5942c2015-01-26 13:13:37 +0200878static int pn544_hci_i2c_acpi_request_resources(struct i2c_client *client)
879{
880 struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
881 const struct acpi_device_id *id;
882 struct gpio_desc *gpiod_en, *gpiod_irq, *gpiod_fw;
883 struct device *dev;
884 int ret;
885
886 if (!client)
887 return -EINVAL;
888
889 dev = &client->dev;
890
891 /* Match the struct device against a given list of ACPI IDs */
892 id = acpi_match_device(dev->driver->acpi_match_table, dev);
893
894 if (!id)
895 return -ENODEV;
896
897 /* Get EN GPIO from ACPI */
Uwe Kleine-König8a2151c2015-05-19 09:22:56 +0200898 gpiod_en = devm_gpiod_get_index(dev, PN544_GPIO_NAME_EN, 1,
899 GPIOD_OUT_LOW);
Robert Dolca0a5942c2015-01-26 13:13:37 +0200900 if (IS_ERR(gpiod_en)) {
Uwe Kleine-König8a2151c2015-05-19 09:22:56 +0200901 nfc_err(dev, "Unable to get EN GPIO\n");
Robert Dolca0a5942c2015-01-26 13:13:37 +0200902 return -ENODEV;
903 }
904
Uwe Kleine-König8a2151c2015-05-19 09:22:56 +0200905 phy->gpio_en = desc_to_gpio(gpiod_en);
Robert Dolca0a5942c2015-01-26 13:13:37 +0200906
907 /* Get FW GPIO from ACPI */
Uwe Kleine-König8a2151c2015-05-19 09:22:56 +0200908 gpiod_fw = devm_gpiod_get_index(dev, PN544_GPIO_NAME_FW, 2,
909 GPIOD_OUT_LOW);
Robert Dolca0a5942c2015-01-26 13:13:37 +0200910 if (IS_ERR(gpiod_fw)) {
Uwe Kleine-König8a2151c2015-05-19 09:22:56 +0200911 nfc_err(dev, "Unable to get FW GPIO\n");
Robert Dolca0a5942c2015-01-26 13:13:37 +0200912 return -ENODEV;
913 }
914
Uwe Kleine-König8a2151c2015-05-19 09:22:56 +0200915 phy->gpio_fw = desc_to_gpio(gpiod_fw);
Robert Dolca0a5942c2015-01-26 13:13:37 +0200916
917 /* Get IRQ GPIO */
Uwe Kleine-König8a2151c2015-05-19 09:22:56 +0200918 gpiod_irq = devm_gpiod_get_index(dev, PN544_GPIO_NAME_IRQ, 0,
919 GPIOD_IN);
Robert Dolca0a5942c2015-01-26 13:13:37 +0200920 if (IS_ERR(gpiod_irq)) {
Uwe Kleine-König8a2151c2015-05-19 09:22:56 +0200921 nfc_err(dev, "Unable to get IRQ GPIO\n");
Robert Dolca0a5942c2015-01-26 13:13:37 +0200922 return -ENODEV;
923 }
924
925 phy->gpio_irq = desc_to_gpio(gpiod_irq);
926
Robert Dolca0a5942c2015-01-26 13:13:37 +0200927 /* Map the pin to an IRQ */
928 ret = gpiod_to_irq(gpiod_irq);
929 if (ret < 0) {
930 nfc_err(dev, "Fail pin IRQ mapping\n");
931 return ret;
932 }
933
934 nfc_info(dev, "GPIO resource, no:%d irq:%d\n",
Joe Perches3590ebc2015-04-07 00:17:00 -0700935 desc_to_gpio(gpiod_irq), ret);
Robert Dolca0a5942c2015-01-26 13:13:37 +0200936 client->irq = ret;
937
938 return 0;
939}
940
Clement Perrochaudeda85652014-04-02 09:02:39 +0000941#ifdef CONFIG_OF
942
943static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
944{
945 struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
946 struct device_node *pp;
947 int ret;
948
949 pp = client->dev.of_node;
950 if (!pp) {
951 ret = -ENODEV;
952 goto err_dt;
953 }
954
955 /* Obtention of EN GPIO from device tree */
956 ret = of_get_named_gpio(pp, "enable-gpios", 0);
957 if (ret < 0) {
958 if (ret != -EPROBE_DEFER)
959 nfc_err(&client->dev,
960 "Failed to get EN gpio, error: %d\n", ret);
961 goto err_dt;
962 }
963 phy->gpio_en = ret;
964
965 /* Configuration of EN GPIO */
Robert Dolca0a5942c2015-01-26 13:13:37 +0200966 ret = gpio_request(phy->gpio_en, PN544_GPIO_NAME_EN);
Clement Perrochaudeda85652014-04-02 09:02:39 +0000967 if (ret) {
968 nfc_err(&client->dev, "Fail EN pin\n");
969 goto err_dt;
970 }
971 ret = gpio_direction_output(phy->gpio_en, 0);
972 if (ret) {
973 nfc_err(&client->dev, "Fail EN pin direction\n");
974 goto err_gpio_en;
975 }
976
977 /* Obtention of FW GPIO from device tree */
978 ret = of_get_named_gpio(pp, "firmware-gpios", 0);
979 if (ret < 0) {
980 if (ret != -EPROBE_DEFER)
981 nfc_err(&client->dev,
982 "Failed to get FW gpio, error: %d\n", ret);
983 goto err_gpio_en;
984 }
985 phy->gpio_fw = ret;
986
987 /* Configuration of FW GPIO */
Robert Dolca0a5942c2015-01-26 13:13:37 +0200988 ret = gpio_request(phy->gpio_fw, PN544_GPIO_NAME_FW);
Clement Perrochaudeda85652014-04-02 09:02:39 +0000989 if (ret) {
990 nfc_err(&client->dev, "Fail FW pin\n");
991 goto err_gpio_en;
992 }
993 ret = gpio_direction_output(phy->gpio_fw, 0);
994 if (ret) {
995 nfc_err(&client->dev, "Fail FW pin direction\n");
996 goto err_gpio_fw;
997 }
998
999 /* IRQ */
1000 ret = irq_of_parse_and_map(pp, 0);
1001 if (ret < 0) {
1002 nfc_err(&client->dev,
1003 "Unable to get irq, error: %d\n", ret);
1004 goto err_gpio_fw;
1005 }
1006 client->irq = ret;
1007
1008 return 0;
1009
1010err_gpio_fw:
1011 gpio_free(phy->gpio_fw);
1012err_gpio_en:
1013 gpio_free(phy->gpio_en);
1014err_dt:
1015 return ret;
1016}
1017
1018#else
1019
1020static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
1021{
1022 return -ENODEV;
1023}
1024
1025#endif
1026
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001027static int pn544_hci_i2c_probe(struct i2c_client *client,
1028 const struct i2c_device_id *id)
Eric Lapuyade97f18412012-10-02 18:44:06 +02001029{
1030 struct pn544_i2c_phy *phy;
1031 struct pn544_nfc_platform_data *pdata;
1032 int r = 0;
1033
1034 dev_dbg(&client->dev, "%s\n", __func__);
1035 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
1036
1037 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
Joe Perches17936b42013-04-05 12:27:39 -07001038 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
Eric Lapuyade97f18412012-10-02 18:44:06 +02001039 return -ENODEV;
1040 }
1041
Samuel Ortiza0f36532012-12-18 18:07:37 +01001042 phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy),
1043 GFP_KERNEL);
Joe Perches3590ebc2015-04-07 00:17:00 -07001044 if (!phy)
Samuel Ortiza0f36532012-12-18 18:07:37 +01001045 return -ENOMEM;
Eric Lapuyade97f18412012-10-02 18:44:06 +02001046
Eric Lapuyade06c66032013-07-19 14:59:45 +02001047 INIT_WORK(&phy->fw_work, pn544_hci_i2c_fw_work);
1048 phy->fw_work_state = FW_WORK_STATE_IDLE;
1049
Eric Lapuyade97f18412012-10-02 18:44:06 +02001050 phy->i2c_dev = client;
1051 i2c_set_clientdata(client, phy);
1052
1053 pdata = client->dev.platform_data;
Clement Perrochaudeda85652014-04-02 09:02:39 +00001054
1055 /* No platform data, using device tree. */
1056 if (!pdata && client->dev.of_node) {
1057 r = pn544_hci_i2c_of_request_resources(client);
1058 if (r) {
1059 nfc_err(&client->dev, "No DT data\n");
1060 return r;
1061 }
1062 /* Using platform data. */
1063 } else if (pdata) {
1064
1065 if (pdata->request_resources == NULL) {
1066 nfc_err(&client->dev, "request_resources() missing\n");
1067 return -EINVAL;
1068 }
1069
1070 r = pdata->request_resources(client);
1071 if (r) {
1072 nfc_err(&client->dev,
1073 "Cannot get platform resources\n");
1074 return r;
1075 }
1076
1077 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
1078 phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
1079 phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
Robert Dolca0a5942c2015-01-26 13:13:37 +02001080 /* Using ACPI */
1081 } else if (ACPI_HANDLE(&client->dev)) {
1082 r = pn544_hci_i2c_acpi_request_resources(client);
1083 if (r) {
1084 nfc_err(&client->dev,
1085 "Cannot get ACPI data\n");
1086 return r;
1087 }
Clement Perrochaudeda85652014-04-02 09:02:39 +00001088 } else {
Joe Perches17936b42013-04-05 12:27:39 -07001089 nfc_err(&client->dev, "No platform data\n");
Samuel Ortiza0f36532012-12-18 18:07:37 +01001090 return -EINVAL;
Eric Lapuyade97f18412012-10-02 18:44:06 +02001091 }
1092
Eric Lapuyade97f18412012-10-02 18:44:06 +02001093 pn544_hci_i2c_platform_init(phy);
1094
1095 r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn,
1096 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1097 PN544_HCI_I2C_DRIVER_NAME, phy);
1098 if (r < 0) {
Joe Perches17936b42013-04-05 12:27:39 -07001099 nfc_err(&client->dev, "Unable to register IRQ handler\n");
Eric Lapuyade97f18412012-10-02 18:44:06 +02001100 goto err_rti;
1101 }
1102
1103 r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
1104 PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM,
Eric Lapuyade06c66032013-07-19 14:59:45 +02001105 PN544_HCI_I2C_LLC_MAX_PAYLOAD,
1106 pn544_hci_i2c_fw_download, &phy->hdev);
Eric Lapuyade97f18412012-10-02 18:44:06 +02001107 if (r < 0)
1108 goto err_hci;
1109
1110 return 0;
1111
1112err_hci:
1113 free_irq(client->irq, phy);
1114
1115err_rti:
Clement Perrochaud12b25db2014-04-08 11:13:49 +00001116 if (!pdata) {
1117 gpio_free(phy->gpio_en);
1118 gpio_free(phy->gpio_fw);
1119 } else if (pdata->free_resources) {
Eric Lapuyade97f18412012-10-02 18:44:06 +02001120 pdata->free_resources();
Clement Perrochaud12b25db2014-04-08 11:13:49 +00001121 }
Eric Lapuyade97f18412012-10-02 18:44:06 +02001122
Eric Lapuyade97f18412012-10-02 18:44:06 +02001123 return r;
1124}
1125
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001126static int pn544_hci_i2c_remove(struct i2c_client *client)
Eric Lapuyade97f18412012-10-02 18:44:06 +02001127{
1128 struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
1129 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
1130
1131 dev_dbg(&client->dev, "%s\n", __func__);
1132
Eric Lapuyade06c66032013-07-19 14:59:45 +02001133 cancel_work_sync(&phy->fw_work);
1134 if (phy->fw_work_state != FW_WORK_STATE_IDLE)
1135 pn544_hci_i2c_fw_work_complete(phy, -ENODEV);
1136
Eric Lapuyade97f18412012-10-02 18:44:06 +02001137 pn544_hci_remove(phy->hdev);
1138
1139 if (phy->powered)
1140 pn544_hci_i2c_disable(phy);
1141
1142 free_irq(client->irq, phy);
Clement Perrochaudeda85652014-04-02 09:02:39 +00001143
1144 /* No platform data, GPIOs have been requested by this driver */
1145 if (!pdata) {
1146 gpio_free(phy->gpio_en);
1147 gpio_free(phy->gpio_fw);
1148 /* Using platform data */
1149 } else if (pdata->free_resources) {
Eric Lapuyade97f18412012-10-02 18:44:06 +02001150 pdata->free_resources();
Clement Perrochaudeda85652014-04-02 09:02:39 +00001151 }
Eric Lapuyade97f18412012-10-02 18:44:06 +02001152
Eric Lapuyade97f18412012-10-02 18:44:06 +02001153 return 0;
1154}
1155
Clement Perrochaudeda85652014-04-02 09:02:39 +00001156static const struct of_device_id of_pn544_i2c_match[] = {
1157 { .compatible = "nxp,pn544-i2c", },
1158 {},
1159};
1160MODULE_DEVICE_TABLE(of, of_pn544_i2c_match);
1161
Eric Lapuyade97f18412012-10-02 18:44:06 +02001162static struct i2c_driver pn544_hci_i2c_driver = {
1163 .driver = {
1164 .name = PN544_HCI_I2C_DRIVER_NAME,
Clement Perrochaudeda85652014-04-02 09:02:39 +00001165 .owner = THIS_MODULE,
1166 .of_match_table = of_match_ptr(of_pn544_i2c_match),
Robert Dolca0a5942c2015-01-26 13:13:37 +02001167 .acpi_match_table = ACPI_PTR(pn544_hci_i2c_acpi_match),
Eric Lapuyade97f18412012-10-02 18:44:06 +02001168 },
1169 .probe = pn544_hci_i2c_probe,
1170 .id_table = pn544_hci_i2c_id_table,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001171 .remove = pn544_hci_i2c_remove,
Eric Lapuyade97f18412012-10-02 18:44:06 +02001172};
1173
Samuel Ortiz234d4d62012-12-18 16:40:16 +01001174module_i2c_driver(pn544_hci_i2c_driver);
Eric Lapuyade97f18412012-10-02 18:44:06 +02001175
1176MODULE_LICENSE("GPL");
1177MODULE_DESCRIPTION(DRIVER_DESC);