2 * I2C Link Layer for PN544 HCI based Driver
4 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/crc-ccitt.h>
22 #include <linux/module.h>
23 #include <linux/i2c.h>
24 #include <linux/gpio.h>
25 #include <linux/of_gpio.h>
26 #include <linux/of_irq.h>
27 #include <linux/acpi.h>
28 #include <linux/miscdevice.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/nfc.h>
32 #include <linux/firmware.h>
33 #include <linux/gpio/consumer.h>
34 #include <linux/platform_data/pn544.h>
35 #include <asm/unaligned.h>
37 #include <net/nfc/hci.h>
38 #include <net/nfc/llc.h>
39 #include <net/nfc/nfc.h>
43 #define PN544_I2C_FRAME_HEADROOM 1
44 #define PN544_I2C_FRAME_TAILROOM 2
47 #define PN544_GPIO_NAME_IRQ "pn544_irq"
48 #define PN544_GPIO_NAME_FW "pn544_fw"
49 #define PN544_GPIO_NAME_EN "pn544_en"
51 /* 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)
61 static struct i2c_device_id pn544_hci_i2c_id_table
[] = {
66 MODULE_DEVICE_TABLE(i2c
, pn544_hci_i2c_id_table
);
68 static const struct acpi_device_id pn544_hci_i2c_acpi_match
[] = {
73 MODULE_DEVICE_TABLE(acpi
, pn544_hci_i2c_acpi_match
);
75 #define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
78 * Exposed through the 4 most significant bytes
79 * from the HCI SW_VERSION first byte, a.k.a.
82 #define PN544_HW_VARIANT_C2 0xa
83 #define PN544_HW_VARIANT_C3 0xb
85 #define PN544_FW_CMD_RESET 0x01
86 #define PN544_FW_CMD_WRITE 0x08
87 #define PN544_FW_CMD_CHECK 0x06
88 #define PN544_FW_CMD_SECURE_WRITE 0x0C
89 #define PN544_FW_CMD_SECURE_CHUNK_WRITE 0x0D
91 struct pn544_i2c_fw_frame_write
{
99 struct pn544_i2c_fw_frame_check
{
107 struct pn544_i2c_fw_frame_response
{
112 struct pn544_i2c_fw_blob
{
118 struct pn544_i2c_fw_secure_frame
{
124 struct pn544_i2c_fw_secure_blob
{
129 #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
134 #define PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND 0x13
135 #define PN544_FW_CMD_RESULT_INVALID_LENGTH 0x18
136 #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
140 #define PN544_FW_CMD_RESULT_WRITE_FAILED 0x74
141 #define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0
142 #define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6
144 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
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)
152 #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
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
162 #define FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER 5
164 struct pn544_i2c_phy
{
165 struct i2c_client
*i2c_dev
;
166 struct nfc_hci_dev
*hdev
;
168 unsigned int gpio_en
;
169 unsigned int gpio_fw
;
170 unsigned int en_polarity
;
174 struct work_struct fw_work
;
176 char firmware_name
[NFC_FIRMWARE_NAME_MAXSIZE
+ 1];
177 const struct firmware
*fw
;
178 u32 fw_blob_dest_addr
;
180 const u8
*fw_blob_data
;
190 * < 0 if hardware error occured (e.g. i2c err)
191 * and prevents normal operation.
195 #define I2C_DUMP_SKB(info, skb) \
197 pr_debug("%s:\n", info); \
198 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
199 16, 1, (skb)->data, (skb)->len, 0); \
202 static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy
*phy
)
204 int polarity
, retry
, ret
;
205 char rset_cmd
[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
206 int count
= sizeof(rset_cmd
);
208 nfc_info(&phy
->i2c_dev
->dev
, "Detecting nfc_en polarity\n");
210 /* Disable fw download */
211 gpio_set_value_cansleep(phy
->gpio_fw
, 0);
213 for (polarity
= 0; polarity
< 2; polarity
++) {
214 phy
->en_polarity
= polarity
;
218 gpio_set_value_cansleep(phy
->gpio_en
,
220 usleep_range(10000, 15000);
223 gpio_set_value_cansleep(phy
->gpio_en
, phy
->en_polarity
);
224 usleep_range(10000, 15000);
227 dev_dbg(&phy
->i2c_dev
->dev
, "Sending reset cmd\n");
228 ret
= i2c_master_send(phy
->i2c_dev
, rset_cmd
, count
);
230 nfc_info(&phy
->i2c_dev
->dev
,
231 "nfc_en polarity : active %s\n",
232 (polarity
== 0 ? "low" : "high"));
238 nfc_err(&phy
->i2c_dev
->dev
,
239 "Could not detect nfc_en polarity, fallback to active high\n");
242 gpio_set_value_cansleep(phy
->gpio_en
, !phy
->en_polarity
);
245 static void pn544_hci_i2c_enable_mode(struct pn544_i2c_phy
*phy
, int run_mode
)
247 gpio_set_value_cansleep(phy
->gpio_fw
,
248 run_mode
== PN544_FW_MODE
? 1 : 0);
249 gpio_set_value_cansleep(phy
->gpio_en
, phy
->en_polarity
);
250 usleep_range(10000, 15000);
252 phy
->run_mode
= run_mode
;
255 static int pn544_hci_i2c_enable(void *phy_id
)
257 struct pn544_i2c_phy
*phy
= phy_id
;
259 pr_info("%s\n", __func__
);
261 pn544_hci_i2c_enable_mode(phy
, PN544_HCI_MODE
);
268 static void pn544_hci_i2c_disable(void *phy_id
)
270 struct pn544_i2c_phy
*phy
= phy_id
;
272 gpio_set_value_cansleep(phy
->gpio_fw
, 0);
273 gpio_set_value_cansleep(phy
->gpio_en
, !phy
->en_polarity
);
274 usleep_range(10000, 15000);
276 gpio_set_value_cansleep(phy
->gpio_en
, phy
->en_polarity
);
277 usleep_range(10000, 15000);
279 gpio_set_value_cansleep(phy
->gpio_en
, !phy
->en_polarity
);
280 usleep_range(10000, 15000);
285 static void pn544_hci_i2c_add_len_crc(struct sk_buff
*skb
)
291 *skb_push(skb
, 1) = len
;
293 crc
= crc_ccitt(0xffff, skb
->data
, skb
->len
);
295 *skb_put(skb
, 1) = crc
& 0xff;
296 *skb_put(skb
, 1) = crc
>> 8;
299 static void pn544_hci_i2c_remove_len_crc(struct sk_buff
*skb
)
301 skb_pull(skb
, PN544_I2C_FRAME_HEADROOM
);
302 skb_trim(skb
, PN544_I2C_FRAME_TAILROOM
);
306 * Writing a frame must not return the number of written bytes.
307 * It must return either zero for success, or <0 for error.
308 * In addition, it must not alter the skb
310 static int pn544_hci_i2c_write(void *phy_id
, struct sk_buff
*skb
)
313 struct pn544_i2c_phy
*phy
= phy_id
;
314 struct i2c_client
*client
= phy
->i2c_dev
;
316 if (phy
->hard_fault
!= 0)
317 return phy
->hard_fault
;
319 usleep_range(3000, 6000);
321 pn544_hci_i2c_add_len_crc(skb
);
323 I2C_DUMP_SKB("i2c frame written", skb
);
325 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
327 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
328 usleep_range(6000, 10000);
329 r
= i2c_master_send(client
, skb
->data
, skb
->len
);
339 pn544_hci_i2c_remove_len_crc(skb
);
344 static int check_crc(u8
*buf
, int buflen
)
350 crc
= crc_ccitt(0xffff, buf
, len
- 2);
353 if (buf
[len
- 2] != (crc
& 0xff) || buf
[len
- 1] != (crc
>> 8)) {
354 pr_err("CRC error 0x%x != 0x%x 0x%x\n",
355 crc
, buf
[len
- 1], buf
[len
- 2]);
356 pr_info("%s: BAD CRC\n", __func__
);
357 print_hex_dump(KERN_DEBUG
, "crc: ", DUMP_PREFIX_NONE
,
358 16, 2, buf
, buflen
, false);
365 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
366 * that i2c bus will be flushed and that next read will start on a new frame.
367 * returned skb contains only LLC header and payload.
369 * -EREMOTEIO : i2c read error (fatal)
370 * -EBADMSG : frame was incorrect and discarded
371 * -ENOMEM : cannot allocate skb, frame dropped
373 static int pn544_hci_i2c_read(struct pn544_i2c_phy
*phy
, struct sk_buff
**skb
)
377 u8 tmp
[PN544_HCI_I2C_LLC_MAX_SIZE
- 1];
378 struct i2c_client
*client
= phy
->i2c_dev
;
380 r
= i2c_master_recv(client
, &len
, 1);
382 nfc_err(&client
->dev
, "cannot read len byte\n");
386 if ((len
< (PN544_HCI_I2C_LLC_MIN_SIZE
- 1)) ||
387 (len
> (PN544_HCI_I2C_LLC_MAX_SIZE
- 1))) {
388 nfc_err(&client
->dev
, "invalid len byte\n");
393 *skb
= alloc_skb(1 + len
, GFP_KERNEL
);
399 *skb_put(*skb
, 1) = len
;
401 r
= i2c_master_recv(client
, skb_put(*skb
, len
), len
);
407 I2C_DUMP_SKB("i2c frame read", *skb
);
409 r
= check_crc((*skb
)->data
, (*skb
)->len
);
417 skb_trim(*skb
, (*skb
)->len
- 2);
419 usleep_range(3000, 6000);
424 if (i2c_master_recv(client
, tmp
, sizeof(tmp
)) < 0)
427 usleep_range(3000, 6000);
432 static int pn544_hci_i2c_fw_read_status(struct pn544_i2c_phy
*phy
)
435 struct pn544_i2c_fw_frame_response response
;
436 struct i2c_client
*client
= phy
->i2c_dev
;
438 r
= i2c_master_recv(client
, (char *) &response
, sizeof(response
));
439 if (r
!= sizeof(response
)) {
440 nfc_err(&client
->dev
, "cannot read fw status\n");
444 usleep_range(3000, 6000);
446 switch (response
.status
) {
449 case PN544_FW_CMD_RESULT_CHUNK_OK
:
450 return response
.status
;
451 case PN544_FW_CMD_RESULT_TIMEOUT
:
453 case PN544_FW_CMD_RESULT_BAD_CRC
:
455 case PN544_FW_CMD_RESULT_ACCESS_DENIED
:
457 case PN544_FW_CMD_RESULT_PROTOCOL_ERROR
:
459 case PN544_FW_CMD_RESULT_INVALID_PARAMETER
:
461 case PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND
:
463 case PN544_FW_CMD_RESULT_INVALID_LENGTH
:
465 case PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR
:
467 case PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR
:
469 case PN544_FW_CMD_RESULT_MEMORY_ERROR
:
471 case PN544_FW_CMD_RESULT_COMMAND_REJECTED
:
473 case PN544_FW_CMD_RESULT_WRITE_FAILED
:
474 case PN544_FW_CMD_RESULT_CHUNK_ERROR
:
482 * Reads an shdlc frame from the chip. This is not as straightforward as it
483 * seems. There are cases where we could loose the frame start synchronization.
484 * The frame format is len-data-crc, and corruption can occur anywhere while
485 * transiting on i2c bus, such that we could read an invalid len.
486 * In order to recover synchronization with the next frame, we must be sure
487 * to read the real amount of data without using the len byte. We do this by
488 * assuming the following:
489 * - the chip will always present only one single complete frame on the bus
490 * before triggering the interrupt
491 * - the chip will not present a new frame until we have completely read
492 * the previous one (or until we have handled the interrupt).
493 * The tricky case is when we read a corrupted len that is less than the real
494 * len. We must detect this here in order to determine that we need to flush
495 * the bus. This is the reason why we check the crc here.
497 static irqreturn_t
pn544_hci_i2c_irq_thread_fn(int irq
, void *phy_id
)
499 struct pn544_i2c_phy
*phy
= phy_id
;
500 struct i2c_client
*client
;
501 struct sk_buff
*skb
= NULL
;
504 if (!phy
|| irq
!= phy
->i2c_dev
->irq
) {
509 client
= phy
->i2c_dev
;
510 dev_dbg(&client
->dev
, "IRQ\n");
512 if (phy
->hard_fault
!= 0)
515 if (phy
->run_mode
== PN544_FW_MODE
) {
516 phy
->fw_cmd_result
= pn544_hci_i2c_fw_read_status(phy
);
517 schedule_work(&phy
->fw_work
);
519 r
= pn544_hci_i2c_read(phy
, &skb
);
520 if (r
== -EREMOTEIO
) {
523 nfc_hci_recv_frame(phy
->hdev
, NULL
);
526 } else if ((r
== -ENOMEM
) || (r
== -EBADMSG
)) {
530 nfc_hci_recv_frame(phy
->hdev
, skb
);
535 static struct nfc_phy_ops i2c_phy_ops
= {
536 .write
= pn544_hci_i2c_write
,
537 .enable
= pn544_hci_i2c_enable
,
538 .disable
= pn544_hci_i2c_disable
,
541 static int pn544_hci_i2c_fw_download(void *phy_id
, const char *firmware_name
,
544 struct pn544_i2c_phy
*phy
= phy_id
;
546 pr_info("Starting Firmware Download (%s)\n", firmware_name
);
548 strcpy(phy
->firmware_name
, firmware_name
);
550 phy
->hw_variant
= hw_variant
;
551 phy
->fw_work_state
= FW_WORK_STATE_START
;
553 schedule_work(&phy
->fw_work
);
558 static void pn544_hci_i2c_fw_work_complete(struct pn544_i2c_phy
*phy
,
561 pr_info("Firmware Download Complete, result=%d\n", result
);
563 pn544_hci_i2c_disable(phy
);
565 phy
->fw_work_state
= FW_WORK_STATE_IDLE
;
568 release_firmware(phy
->fw
);
572 nfc_fw_download_done(phy
->hdev
->ndev
, phy
->firmware_name
, (u32
) -result
);
575 static int pn544_hci_i2c_fw_write_cmd(struct i2c_client
*client
, u32 dest_addr
,
576 const u8
*data
, u16 datalen
)
578 u8 frame
[PN544_FW_I2C_MAX_PAYLOAD
];
579 struct pn544_i2c_fw_frame_write
*framep
;
584 if (datalen
> PN544_FW_I2C_WRITE_DATA_MAX_LEN
)
585 datalen
= PN544_FW_I2C_WRITE_DATA_MAX_LEN
;
587 framep
= (struct pn544_i2c_fw_frame_write
*) frame
;
589 params_len
= sizeof(framep
->be_dest_addr
) +
590 sizeof(framep
->be_datalen
) + datalen
;
591 framelen
= params_len
+ sizeof(framep
->cmd
) +
592 sizeof(framep
->be_length
);
594 framep
->cmd
= PN544_FW_CMD_WRITE
;
596 put_unaligned_be16(params_len
, &framep
->be_length
);
598 framep
->be_dest_addr
[0] = (dest_addr
& 0xff0000) >> 16;
599 framep
->be_dest_addr
[1] = (dest_addr
& 0xff00) >> 8;
600 framep
->be_dest_addr
[2] = dest_addr
& 0xff;
602 put_unaligned_be16(datalen
, &framep
->be_datalen
);
604 memcpy(framep
->data
, data
, datalen
);
606 r
= i2c_master_send(client
, frame
, framelen
);
616 static int pn544_hci_i2c_fw_check_cmd(struct i2c_client
*client
, u32 start_addr
,
617 const u8
*data
, u16 datalen
)
619 struct pn544_i2c_fw_frame_check frame
;
623 /* calculate local crc for the data we want to check */
624 crc
= crc_ccitt(0xffff, data
, datalen
);
626 frame
.cmd
= PN544_FW_CMD_CHECK
;
628 put_unaligned_be16(sizeof(frame
.be_start_addr
) +
629 sizeof(frame
.be_datalen
) + sizeof(frame
.be_crc
),
632 /* tell the chip the memory region to which our crc applies */
633 frame
.be_start_addr
[0] = (start_addr
& 0xff0000) >> 16;
634 frame
.be_start_addr
[1] = (start_addr
& 0xff00) >> 8;
635 frame
.be_start_addr
[2] = start_addr
& 0xff;
637 put_unaligned_be16(datalen
, &frame
.be_datalen
);
640 * and give our local crc. Chip will calculate its own crc for the
641 * region and compare with ours.
643 put_unaligned_be16(crc
, &frame
.be_crc
);
645 r
= i2c_master_send(client
, (const char *) &frame
, sizeof(frame
));
647 if (r
== sizeof(frame
))
655 static int pn544_hci_i2c_fw_write_chunk(struct pn544_i2c_phy
*phy
)
659 r
= pn544_hci_i2c_fw_write_cmd(phy
->i2c_dev
,
660 phy
->fw_blob_dest_addr
+ phy
->fw_written
,
661 phy
->fw_blob_data
+ phy
->fw_written
,
662 phy
->fw_blob_size
- phy
->fw_written
);
666 phy
->fw_written
+= r
;
667 phy
->fw_work_state
= FW_WORK_STATE_WAIT_WRITE_ANSWER
;
672 static int pn544_hci_i2c_fw_secure_write_frame_cmd(struct pn544_i2c_phy
*phy
,
673 const u8
*data
, u16 datalen
)
675 u8 buf
[PN544_FW_I2C_MAX_PAYLOAD
];
676 struct pn544_i2c_fw_secure_frame
*chunk
;
680 if (datalen
> PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN
)
681 datalen
= PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN
;
683 chunk
= (struct pn544_i2c_fw_secure_frame
*) buf
;
685 chunk
->cmd
= PN544_FW_CMD_SECURE_CHUNK_WRITE
;
687 put_unaligned_be16(datalen
, &chunk
->be_datalen
);
689 memcpy(chunk
->data
, data
, datalen
);
691 chunklen
= sizeof(chunk
->cmd
) + sizeof(chunk
->be_datalen
) + datalen
;
693 r
= i2c_master_send(phy
->i2c_dev
, buf
, chunklen
);
704 static int pn544_hci_i2c_fw_secure_write_frame(struct pn544_i2c_phy
*phy
)
706 struct pn544_i2c_fw_secure_frame
*framep
;
709 framep
= (struct pn544_i2c_fw_secure_frame
*) phy
->fw_blob_data
;
710 if (phy
->fw_written
== 0)
711 phy
->fw_blob_size
= get_unaligned_be16(&framep
->be_datalen
)
712 + PN544_FW_SECURE_FRAME_HEADER_LEN
;
714 /* Only secure write command can be chunked*/
715 if (phy
->fw_blob_size
> PN544_FW_I2C_MAX_PAYLOAD
&&
716 framep
->cmd
!= PN544_FW_CMD_SECURE_WRITE
)
719 /* The firmware also have other commands, we just send them directly */
720 if (phy
->fw_blob_size
< PN544_FW_I2C_MAX_PAYLOAD
) {
721 r
= i2c_master_send(phy
->i2c_dev
,
722 (const char *) phy
->fw_blob_data
, phy
->fw_blob_size
);
724 if (r
== phy
->fw_blob_size
)
732 r
= pn544_hci_i2c_fw_secure_write_frame_cmd(phy
,
733 phy
->fw_blob_data
+ phy
->fw_written
,
734 phy
->fw_blob_size
- phy
->fw_written
);
739 phy
->fw_written
+= r
;
740 phy
->fw_work_state
= FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER
;
742 /* SW reset command will not trig any response from PN544 */
743 if (framep
->cmd
== PN544_FW_CMD_RESET
) {
744 pn544_hci_i2c_enable_mode(phy
, PN544_FW_MODE
);
745 phy
->fw_cmd_result
= 0;
746 schedule_work(&phy
->fw_work
);
752 static void pn544_hci_i2c_fw_work(struct work_struct
*work
)
754 struct pn544_i2c_phy
*phy
= container_of(work
, struct pn544_i2c_phy
,
757 struct pn544_i2c_fw_blob
*blob
;
758 struct pn544_i2c_fw_secure_blob
*secure_blob
;
760 switch (phy
->fw_work_state
) {
761 case FW_WORK_STATE_START
:
762 pn544_hci_i2c_enable_mode(phy
, PN544_FW_MODE
);
764 r
= request_firmware(&phy
->fw
, phy
->firmware_name
,
767 goto exit_state_start
;
771 switch (phy
->hw_variant
) {
772 case PN544_HW_VARIANT_C2
:
773 blob
= (struct pn544_i2c_fw_blob
*) phy
->fw
->data
;
774 phy
->fw_blob_size
= get_unaligned_be32(&blob
->be_size
);
775 phy
->fw_blob_dest_addr
= get_unaligned_be32(
777 phy
->fw_blob_data
= blob
->data
;
779 r
= pn544_hci_i2c_fw_write_chunk(phy
);
781 case PN544_HW_VARIANT_C3
:
782 secure_blob
= (struct pn544_i2c_fw_secure_blob
*)
784 phy
->fw_blob_data
= secure_blob
->data
;
785 phy
->fw_size
= phy
->fw
->size
;
786 r
= pn544_hci_i2c_fw_secure_write_frame(phy
);
795 pn544_hci_i2c_fw_work_complete(phy
, r
);
798 case FW_WORK_STATE_WAIT_WRITE_ANSWER
:
799 r
= phy
->fw_cmd_result
;
801 goto exit_state_wait_write_answer
;
803 if (phy
->fw_written
== phy
->fw_blob_size
) {
804 r
= pn544_hci_i2c_fw_check_cmd(phy
->i2c_dev
,
805 phy
->fw_blob_dest_addr
,
809 goto exit_state_wait_write_answer
;
810 phy
->fw_work_state
= FW_WORK_STATE_WAIT_CHECK_ANSWER
;
814 r
= pn544_hci_i2c_fw_write_chunk(phy
);
816 exit_state_wait_write_answer
:
818 pn544_hci_i2c_fw_work_complete(phy
, r
);
821 case FW_WORK_STATE_WAIT_CHECK_ANSWER
:
822 r
= phy
->fw_cmd_result
;
824 goto exit_state_wait_check_answer
;
826 blob
= (struct pn544_i2c_fw_blob
*) (phy
->fw_blob_data
+
828 phy
->fw_blob_size
= get_unaligned_be32(&blob
->be_size
);
829 if (phy
->fw_blob_size
!= 0) {
830 phy
->fw_blob_dest_addr
=
831 get_unaligned_be32(&blob
->be_destaddr
);
832 phy
->fw_blob_data
= blob
->data
;
835 r
= pn544_hci_i2c_fw_write_chunk(phy
);
838 exit_state_wait_check_answer
:
839 if (r
< 0 || phy
->fw_blob_size
== 0)
840 pn544_hci_i2c_fw_work_complete(phy
, r
);
843 case FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER
:
844 r
= phy
->fw_cmd_result
;
846 goto exit_state_wait_secure_write_answer
;
848 if (r
== PN544_FW_CMD_RESULT_CHUNK_OK
) {
849 r
= pn544_hci_i2c_fw_secure_write_frame(phy
);
850 goto exit_state_wait_secure_write_answer
;
853 if (phy
->fw_written
== phy
->fw_blob_size
) {
854 secure_blob
= (struct pn544_i2c_fw_secure_blob
*)
855 (phy
->fw_blob_data
+ phy
->fw_blob_size
);
856 phy
->fw_size
-= phy
->fw_blob_size
+
857 PN544_FW_SECURE_BLOB_HEADER_LEN
;
858 if (phy
->fw_size
>= PN544_FW_SECURE_BLOB_HEADER_LEN
859 + PN544_FW_SECURE_FRAME_HEADER_LEN
) {
860 phy
->fw_blob_data
= secure_blob
->data
;
863 r
= pn544_hci_i2c_fw_secure_write_frame(phy
);
867 exit_state_wait_secure_write_answer
:
868 if (r
< 0 || phy
->fw_size
== 0)
869 pn544_hci_i2c_fw_work_complete(phy
, r
);
877 static int pn544_hci_i2c_acpi_request_resources(struct i2c_client
*client
)
879 struct pn544_i2c_phy
*phy
= i2c_get_clientdata(client
);
880 struct gpio_desc
*gpiod_en
, *gpiod_fw
;
881 struct device
*dev
= &client
->dev
;
883 /* Get EN GPIO from ACPI */
884 gpiod_en
= devm_gpiod_get_index(dev
, PN544_GPIO_NAME_EN
, 1,
886 if (IS_ERR(gpiod_en
)) {
887 nfc_err(dev
, "Unable to get EN GPIO\n");
891 phy
->gpio_en
= desc_to_gpio(gpiod_en
);
893 /* Get FW GPIO from ACPI */
894 gpiod_fw
= devm_gpiod_get_index(dev
, PN544_GPIO_NAME_FW
, 2,
896 if (IS_ERR(gpiod_fw
)) {
897 nfc_err(dev
, "Unable to get FW GPIO\n");
901 phy
->gpio_fw
= desc_to_gpio(gpiod_fw
);
906 static int pn544_hci_i2c_of_request_resources(struct i2c_client
*client
)
908 struct pn544_i2c_phy
*phy
= i2c_get_clientdata(client
);
909 struct device_node
*pp
;
912 pp
= client
->dev
.of_node
;
918 /* Obtention of EN GPIO from device tree */
919 ret
= of_get_named_gpio(pp
, "enable-gpios", 0);
921 if (ret
!= -EPROBE_DEFER
)
922 nfc_err(&client
->dev
,
923 "Failed to get EN gpio, error: %d\n", ret
);
928 /* Configuration of EN GPIO */
929 ret
= gpio_request(phy
->gpio_en
, PN544_GPIO_NAME_EN
);
931 nfc_err(&client
->dev
, "Fail EN pin\n");
934 ret
= gpio_direction_output(phy
->gpio_en
, 0);
936 nfc_err(&client
->dev
, "Fail EN pin direction\n");
940 /* Obtention of FW GPIO from device tree */
941 ret
= of_get_named_gpio(pp
, "firmware-gpios", 0);
943 if (ret
!= -EPROBE_DEFER
)
944 nfc_err(&client
->dev
,
945 "Failed to get FW gpio, error: %d\n", ret
);
950 /* Configuration of FW GPIO */
951 ret
= gpio_request(phy
->gpio_fw
, PN544_GPIO_NAME_FW
);
953 nfc_err(&client
->dev
, "Fail FW pin\n");
956 ret
= gpio_direction_output(phy
->gpio_fw
, 0);
958 nfc_err(&client
->dev
, "Fail FW pin direction\n");
965 gpio_free(phy
->gpio_fw
);
967 gpio_free(phy
->gpio_en
);
972 static int pn544_hci_i2c_probe(struct i2c_client
*client
,
973 const struct i2c_device_id
*id
)
975 struct pn544_i2c_phy
*phy
;
976 struct pn544_nfc_platform_data
*pdata
;
979 dev_dbg(&client
->dev
, "%s\n", __func__
);
980 dev_dbg(&client
->dev
, "IRQ: %d\n", client
->irq
);
982 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
983 nfc_err(&client
->dev
, "Need I2C_FUNC_I2C\n");
987 phy
= devm_kzalloc(&client
->dev
, sizeof(struct pn544_i2c_phy
),
992 INIT_WORK(&phy
->fw_work
, pn544_hci_i2c_fw_work
);
993 phy
->fw_work_state
= FW_WORK_STATE_IDLE
;
995 phy
->i2c_dev
= client
;
996 i2c_set_clientdata(client
, phy
);
998 pdata
= client
->dev
.platform_data
;
1000 /* No platform data, using device tree. */
1001 if (!pdata
&& client
->dev
.of_node
) {
1002 r
= pn544_hci_i2c_of_request_resources(client
);
1004 nfc_err(&client
->dev
, "No DT data\n");
1007 /* Using platform data. */
1010 if (pdata
->request_resources
== NULL
) {
1011 nfc_err(&client
->dev
, "request_resources() missing\n");
1015 r
= pdata
->request_resources(client
);
1017 nfc_err(&client
->dev
,
1018 "Cannot get platform resources\n");
1022 phy
->gpio_en
= pdata
->get_gpio(NFC_GPIO_ENABLE
);
1023 phy
->gpio_fw
= pdata
->get_gpio(NFC_GPIO_FW_RESET
);
1025 } else if (ACPI_HANDLE(&client
->dev
)) {
1026 r
= pn544_hci_i2c_acpi_request_resources(client
);
1028 nfc_err(&client
->dev
,
1029 "Cannot get ACPI data\n");
1033 nfc_err(&client
->dev
, "No platform data\n");
1037 pn544_hci_i2c_platform_init(phy
);
1039 r
= request_threaded_irq(client
->irq
, NULL
, pn544_hci_i2c_irq_thread_fn
,
1040 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1041 PN544_HCI_I2C_DRIVER_NAME
, phy
);
1043 nfc_err(&client
->dev
, "Unable to register IRQ handler\n");
1047 r
= pn544_hci_probe(phy
, &i2c_phy_ops
, LLC_SHDLC_NAME
,
1048 PN544_I2C_FRAME_HEADROOM
, PN544_I2C_FRAME_TAILROOM
,
1049 PN544_HCI_I2C_LLC_MAX_PAYLOAD
,
1050 pn544_hci_i2c_fw_download
, &phy
->hdev
);
1057 free_irq(client
->irq
, phy
);
1061 gpio_free(phy
->gpio_en
);
1062 gpio_free(phy
->gpio_fw
);
1063 } else if (pdata
->free_resources
) {
1064 pdata
->free_resources();
1070 static int pn544_hci_i2c_remove(struct i2c_client
*client
)
1072 struct pn544_i2c_phy
*phy
= i2c_get_clientdata(client
);
1073 struct pn544_nfc_platform_data
*pdata
= client
->dev
.platform_data
;
1075 dev_dbg(&client
->dev
, "%s\n", __func__
);
1077 cancel_work_sync(&phy
->fw_work
);
1078 if (phy
->fw_work_state
!= FW_WORK_STATE_IDLE
)
1079 pn544_hci_i2c_fw_work_complete(phy
, -ENODEV
);
1081 pn544_hci_remove(phy
->hdev
);
1084 pn544_hci_i2c_disable(phy
);
1086 free_irq(client
->irq
, phy
);
1088 /* No platform data, GPIOs have been requested by this driver */
1090 gpio_free(phy
->gpio_en
);
1091 gpio_free(phy
->gpio_fw
);
1092 /* Using platform data */
1093 } else if (pdata
->free_resources
) {
1094 pdata
->free_resources();
1100 static const struct of_device_id of_pn544_i2c_match
[] = {
1101 { .compatible
= "nxp,pn544-i2c", },
1104 MODULE_DEVICE_TABLE(of
, of_pn544_i2c_match
);
1106 static struct i2c_driver pn544_hci_i2c_driver
= {
1108 .name
= PN544_HCI_I2C_DRIVER_NAME
,
1109 .of_match_table
= of_match_ptr(of_pn544_i2c_match
),
1110 .acpi_match_table
= ACPI_PTR(pn544_hci_i2c_acpi_match
),
1112 .probe
= pn544_hci_i2c_probe
,
1113 .id_table
= pn544_hci_i2c_id_table
,
1114 .remove
= pn544_hci_i2c_remove
,
1117 module_i2c_driver(pn544_hci_i2c_driver
);
1119 MODULE_LICENSE("GPL");
1120 MODULE_DESCRIPTION(DRIVER_DESC
);