aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/nfc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/nfc')
-rw-r--r--drivers/nfc/Kconfig1
-rw-r--r--drivers/nfc/Makefile1
-rw-r--r--drivers/nfc/pn544/i2c.c154
-rw-r--r--drivers/nfc/port100.c36
-rw-r--r--drivers/nfc/st21nfca/Kconfig23
-rw-r--r--drivers/nfc/st21nfca/Makefile8
-rw-r--r--drivers/nfc/st21nfca/i2c.c724
-rw-r--r--drivers/nfc/st21nfca/st21nfca.c698
-rw-r--r--drivers/nfc/st21nfca/st21nfca.h87
-rw-r--r--drivers/nfc/trf7970a.c261
10 files changed, 1905 insertions, 88 deletions
diff --git a/drivers/nfc/Kconfig b/drivers/nfc/Kconfig
index 65d4ca19d132..26c66a126551 100644
--- a/drivers/nfc/Kconfig
+++ b/drivers/nfc/Kconfig
@@ -71,5 +71,6 @@ config NFC_PORT100
71source "drivers/nfc/pn544/Kconfig" 71source "drivers/nfc/pn544/Kconfig"
72source "drivers/nfc/microread/Kconfig" 72source "drivers/nfc/microread/Kconfig"
73source "drivers/nfc/nfcmrvl/Kconfig" 73source "drivers/nfc/nfcmrvl/Kconfig"
74source "drivers/nfc/st21nfca/Kconfig"
74 75
75endmenu 76endmenu
diff --git a/drivers/nfc/Makefile b/drivers/nfc/Makefile
index ae42a3fa60c9..23225b0287fd 100644
--- a/drivers/nfc/Makefile
+++ b/drivers/nfc/Makefile
@@ -11,5 +11,6 @@ obj-$(CONFIG_NFC_SIM) += nfcsim.o
11obj-$(CONFIG_NFC_PORT100) += port100.o 11obj-$(CONFIG_NFC_PORT100) += port100.o
12obj-$(CONFIG_NFC_MRVL) += nfcmrvl/ 12obj-$(CONFIG_NFC_MRVL) += nfcmrvl/
13obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o 13obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o
14obj-$(CONFIG_NFC_ST21NFCA) += st21nfca/
14 15
15ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG 16ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG
diff --git a/drivers/nfc/pn544/i2c.c b/drivers/nfc/pn544/i2c.c
index f2acd85be86e..440291ab7263 100644
--- a/drivers/nfc/pn544/i2c.c
+++ b/drivers/nfc/pn544/i2c.c
@@ -22,6 +22,8 @@
22#include <linux/module.h> 22#include <linux/module.h>
23#include <linux/i2c.h> 23#include <linux/i2c.h>
24#include <linux/gpio.h> 24#include <linux/gpio.h>
25#include <linux/of_gpio.h>
26#include <linux/of_irq.h>
25#include <linux/miscdevice.h> 27#include <linux/miscdevice.h>
26#include <linux/interrupt.h> 28#include <linux/interrupt.h>
27#include <linux/delay.h> 29#include <linux/delay.h>
@@ -857,6 +859,92 @@ exit_state_wait_secure_write_answer:
857 } 859 }
858} 860}
859 861
862#ifdef CONFIG_OF
863
864static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
865{
866 struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
867 struct device_node *pp;
868 int ret;
869
870 pp = client->dev.of_node;
871 if (!pp) {
872 ret = -ENODEV;
873 goto err_dt;
874 }
875
876 /* Obtention of EN GPIO from device tree */
877 ret = of_get_named_gpio(pp, "enable-gpios", 0);
878 if (ret < 0) {
879 if (ret != -EPROBE_DEFER)
880 nfc_err(&client->dev,
881 "Failed to get EN gpio, error: %d\n", ret);
882 goto err_dt;
883 }
884 phy->gpio_en = ret;
885
886 /* Configuration of EN GPIO */
887 ret = gpio_request(phy->gpio_en, "pn544_en");
888 if (ret) {
889 nfc_err(&client->dev, "Fail EN pin\n");
890 goto err_dt;
891 }
892 ret = gpio_direction_output(phy->gpio_en, 0);
893 if (ret) {
894 nfc_err(&client->dev, "Fail EN pin direction\n");
895 goto err_gpio_en;
896 }
897
898 /* Obtention of FW GPIO from device tree */
899 ret = of_get_named_gpio(pp, "firmware-gpios", 0);
900 if (ret < 0) {
901 if (ret != -EPROBE_DEFER)
902 nfc_err(&client->dev,
903 "Failed to get FW gpio, error: %d\n", ret);
904 goto err_gpio_en;
905 }
906 phy->gpio_fw = ret;
907
908 /* Configuration of FW GPIO */
909 ret = gpio_request(phy->gpio_fw, "pn544_fw");
910 if (ret) {
911 nfc_err(&client->dev, "Fail FW pin\n");
912 goto err_gpio_en;
913 }
914 ret = gpio_direction_output(phy->gpio_fw, 0);
915 if (ret) {
916 nfc_err(&client->dev, "Fail FW pin direction\n");
917 goto err_gpio_fw;
918 }
919
920 /* IRQ */
921 ret = irq_of_parse_and_map(pp, 0);
922 if (ret < 0) {
923 nfc_err(&client->dev,
924 "Unable to get irq, error: %d\n", ret);
925 goto err_gpio_fw;
926 }
927 client->irq = ret;
928
929 return 0;
930
931err_gpio_fw:
932 gpio_free(phy->gpio_fw);
933err_gpio_en:
934 gpio_free(phy->gpio_en);
935err_dt:
936 return ret;
937}
938
939#else
940
941static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
942{
943 return -ENODEV;
944}
945
946#endif
947
860static int pn544_hci_i2c_probe(struct i2c_client *client, 948static int pn544_hci_i2c_probe(struct i2c_client *client,
861 const struct i2c_device_id *id) 949 const struct i2c_device_id *id)
862{ 950{
@@ -887,25 +975,36 @@ static int pn544_hci_i2c_probe(struct i2c_client *client,
887 i2c_set_clientdata(client, phy); 975 i2c_set_clientdata(client, phy);
888 976
889 pdata = client->dev.platform_data; 977 pdata = client->dev.platform_data;
890 if (pdata == NULL) {
891 nfc_err(&client->dev, "No platform data\n");
892 return -EINVAL;
893 }
894 978
895 if (pdata->request_resources == NULL) { 979 /* No platform data, using device tree. */
896 nfc_err(&client->dev, "request_resources() missing\n"); 980 if (!pdata && client->dev.of_node) {
897 return -EINVAL; 981 r = pn544_hci_i2c_of_request_resources(client);
898 } 982 if (r) {
983 nfc_err(&client->dev, "No DT data\n");
984 return r;
985 }
986 /* Using platform data. */
987 } else if (pdata) {
899 988
900 r = pdata->request_resources(client); 989 if (pdata->request_resources == NULL) {
901 if (r) { 990 nfc_err(&client->dev, "request_resources() missing\n");
902 nfc_err(&client->dev, "Cannot get platform resources\n"); 991 return -EINVAL;
903 return r; 992 }
904 }
905 993
906 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); 994 r = pdata->request_resources(client);
907 phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET); 995 if (r) {
908 phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ); 996 nfc_err(&client->dev,
997 "Cannot get platform resources\n");
998 return r;
999 }
1000
1001 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
1002 phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
1003 phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
1004 } else {
1005 nfc_err(&client->dev, "No platform data\n");
1006 return -EINVAL;
1007 }
909 1008
910 pn544_hci_i2c_platform_init(phy); 1009 pn544_hci_i2c_platform_init(phy);
911 1010
@@ -930,8 +1029,12 @@ err_hci:
930 free_irq(client->irq, phy); 1029 free_irq(client->irq, phy);
931 1030
932err_rti: 1031err_rti:
933 if (pdata->free_resources != NULL) 1032 if (!pdata) {
1033 gpio_free(phy->gpio_en);
1034 gpio_free(phy->gpio_fw);
1035 } else if (pdata->free_resources) {
934 pdata->free_resources(); 1036 pdata->free_resources();
1037 }
935 1038
936 return r; 1039 return r;
937} 1040}
@@ -953,15 +1056,30 @@ static int pn544_hci_i2c_remove(struct i2c_client *client)
953 pn544_hci_i2c_disable(phy); 1056 pn544_hci_i2c_disable(phy);
954 1057
955 free_irq(client->irq, phy); 1058 free_irq(client->irq, phy);
956 if (pdata->free_resources) 1059
1060 /* No platform data, GPIOs have been requested by this driver */
1061 if (!pdata) {
1062 gpio_free(phy->gpio_en);
1063 gpio_free(phy->gpio_fw);
1064 /* Using platform data */
1065 } else if (pdata->free_resources) {
957 pdata->free_resources(); 1066 pdata->free_resources();
1067 }
958 1068
959 return 0; 1069 return 0;
960} 1070}
961 1071
1072static const struct of_device_id of_pn544_i2c_match[] = {
1073 { .compatible = "nxp,pn544-i2c", },
1074 {},
1075};
1076MODULE_DEVICE_TABLE(of, of_pn544_i2c_match);
1077
962static struct i2c_driver pn544_hci_i2c_driver = { 1078static struct i2c_driver pn544_hci_i2c_driver = {
963 .driver = { 1079 .driver = {
964 .name = PN544_HCI_I2C_DRIVER_NAME, 1080 .name = PN544_HCI_I2C_DRIVER_NAME,
1081 .owner = THIS_MODULE,
1082 .of_match_table = of_match_ptr(of_pn544_i2c_match),
965 }, 1083 },
966 .probe = pn544_hci_i2c_probe, 1084 .probe = pn544_hci_i2c_probe,
967 .id_table = pn544_hci_i2c_id_table, 1085 .id_table = pn544_hci_i2c_id_table,
diff --git a/drivers/nfc/port100.c b/drivers/nfc/port100.c
index b7a372af5eb7..4ac4d31f6c59 100644
--- a/drivers/nfc/port100.c
+++ b/drivers/nfc/port100.c
@@ -28,7 +28,8 @@
28 NFC_PROTO_MIFARE_MASK | \ 28 NFC_PROTO_MIFARE_MASK | \
29 NFC_PROTO_FELICA_MASK | \ 29 NFC_PROTO_FELICA_MASK | \
30 NFC_PROTO_NFC_DEP_MASK | \ 30 NFC_PROTO_NFC_DEP_MASK | \
31 NFC_PROTO_ISO14443_MASK) 31 NFC_PROTO_ISO14443_MASK | \
32 NFC_PROTO_ISO14443_B_MASK)
32 33
33#define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \ 34#define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
34 NFC_DIGITAL_DRV_CAPS_TG_CRC) 35 NFC_DIGITAL_DRV_CAPS_TG_CRC)
@@ -120,6 +121,7 @@ struct port100_in_rf_setting {
120#define PORT100_COMM_TYPE_IN_212F 0x01 121#define PORT100_COMM_TYPE_IN_212F 0x01
121#define PORT100_COMM_TYPE_IN_424F 0x02 122#define PORT100_COMM_TYPE_IN_424F 0x02
122#define PORT100_COMM_TYPE_IN_106A 0x03 123#define PORT100_COMM_TYPE_IN_106A 0x03
124#define PORT100_COMM_TYPE_IN_106B 0x07
123 125
124static const struct port100_in_rf_setting in_rf_settings[] = { 126static const struct port100_in_rf_setting in_rf_settings[] = {
125 [NFC_DIGITAL_RF_TECH_212F] = { 127 [NFC_DIGITAL_RF_TECH_212F] = {
@@ -140,6 +142,12 @@ static const struct port100_in_rf_setting in_rf_settings[] = {
140 .in_recv_set_number = 15, 142 .in_recv_set_number = 15,
141 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A, 143 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A,
142 }, 144 },
145 [NFC_DIGITAL_RF_TECH_106B] = {
146 .in_send_set_number = 3,
147 .in_send_comm_type = PORT100_COMM_TYPE_IN_106B,
148 .in_recv_set_number = 15,
149 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106B,
150 },
143 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */ 151 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
144 [NFC_DIGITAL_RF_TECH_LAST] = { 0 }, 152 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
145}; 153};
@@ -340,6 +348,32 @@ in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = {
340 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = { 348 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
341 { PORT100_IN_PROT_END, 0 }, 349 { PORT100_IN_PROT_END, 0 },
342 }, 350 },
351 [NFC_DIGITAL_FRAMING_NFCB] = {
352 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 20 },
353 { PORT100_IN_PROT_ADD_CRC, 1 },
354 { PORT100_IN_PROT_CHECK_CRC, 1 },
355 { PORT100_IN_PROT_MULTI_CARD, 0 },
356 { PORT100_IN_PROT_ADD_PARITY, 0 },
357 { PORT100_IN_PROT_CHECK_PARITY, 0 },
358 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
359 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
360 { PORT100_IN_PROT_CRYPTO1, 0 },
361 { PORT100_IN_PROT_ADD_SOF, 1 },
362 { PORT100_IN_PROT_CHECK_SOF, 1 },
363 { PORT100_IN_PROT_ADD_EOF, 1 },
364 { PORT100_IN_PROT_CHECK_EOF, 1 },
365 { PORT100_IN_PROT_DEAF_TIME, 4 },
366 { PORT100_IN_PROT_CRM, 0 },
367 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
368 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
369 { PORT100_IN_PROT_RFCA, 0 },
370 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
371 { PORT100_IN_PROT_END, 0 },
372 },
373 [NFC_DIGITAL_FRAMING_NFCB_T4T] = {
374 /* nfc_digital_framing_nfcb */
375 { PORT100_IN_PROT_END, 0 },
376 },
343 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */ 377 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
344 [NFC_DIGITAL_FRAMING_LAST] = { 378 [NFC_DIGITAL_FRAMING_LAST] = {
345 { PORT100_IN_PROT_END, 0 }, 379 { PORT100_IN_PROT_END, 0 },
diff --git a/drivers/nfc/st21nfca/Kconfig b/drivers/nfc/st21nfca/Kconfig
new file mode 100644
index 000000000000..ee459f066ade
--- /dev/null
+++ b/drivers/nfc/st21nfca/Kconfig
@@ -0,0 +1,23 @@
1config NFC_ST21NFCA
2 tristate "STMicroelectronics ST21NFCA NFC driver"
3 depends on NFC_HCI
4 select CRC_CCITT
5 default n
6 ---help---
7 STMicroelectronics ST21NFCA core driver. It implements the chipset
8 HCI logic and hooks into the NFC kernel APIs. Physical layers will
9 register against it.
10
11 To compile this driver as a module, choose m here. The module will
12 be called st21nfca.
13 Say N if unsure.
14
15config NFC_ST21NFCA_I2C
16 tristate "NFC ST21NFCA i2c support"
17 depends on NFC_ST21NFCA && I2C && NFC_SHDLC
18 ---help---
19 This module adds support for the STMicroelectronics st21nfca i2c interface.
20 Select this if your platform is using the i2c bus.
21
22 If you choose to build a module, it'll be called st21nfca_i2c.
23 Say N if unsure.
diff --git a/drivers/nfc/st21nfca/Makefile b/drivers/nfc/st21nfca/Makefile
new file mode 100644
index 000000000000..038ed093a119
--- /dev/null
+++ b/drivers/nfc/st21nfca/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for ST21NFCA HCI based NFC driver
3#
4
5st21nfca_i2c-objs = i2c.o
6
7obj-$(CONFIG_NFC_ST21NFCA) += st21nfca.o
8obj-$(CONFIG_NFC_ST21NFCA_I2C) += st21nfca_i2c.o
diff --git a/drivers/nfc/st21nfca/i2c.c b/drivers/nfc/st21nfca/i2c.c
new file mode 100644
index 000000000000..3f954ed86d98
--- /dev/null
+++ b/drivers/nfc/st21nfca/i2c.c
@@ -0,0 +1,724 @@
1/*
2 * I2C Link Layer for ST21NFCA HCI based Driver
3 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/crc-ccitt.h>
21#include <linux/module.h>
22#include <linux/i2c.h>
23#include <linux/gpio.h>
24#include <linux/of_irq.h>
25#include <linux/of_gpio.h>
26#include <linux/miscdevice.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
29#include <linux/nfc.h>
30#include <linux/firmware.h>
31#include <linux/unaligned/access_ok.h>
32#include <linux/platform_data/st21nfca.h>
33
34#include <net/nfc/hci.h>
35#include <net/nfc/llc.h>
36#include <net/nfc/nfc.h>
37
38#include "st21nfca.h"
39
40/*
41 * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
42 * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
43 * called byte stuffing has been introduced.
44 *
45 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
46 * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
47 * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
48 */
49#define ST21NFCA_SOF_EOF 0x7e
50#define ST21NFCA_BYTE_STUFFING_MASK 0x20
51#define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
52
53/* SOF + 00 */
54#define ST21NFCA_FRAME_HEADROOM 2
55
56/* 2 bytes crc + EOF */
57#define ST21NFCA_FRAME_TAILROOM 3
58#define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
59 buf[1] == 0)
60
61#define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
62
63static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
64 {ST21NFCA_HCI_DRIVER_NAME, 0},
65 {}
66};
67
68MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
69
70struct st21nfca_i2c_phy {
71 struct i2c_client *i2c_dev;
72 struct nfc_hci_dev *hdev;
73
74 unsigned int gpio_ena;
75 unsigned int gpio_irq;
76 unsigned int irq_polarity;
77
78 struct sk_buff *pending_skb;
79 int current_read_len;
80 /*
81 * crc might have fail because i2c macro
82 * is disable due to other interface activity
83 */
84 int crc_trials;
85
86 int powered;
87 int run_mode;
88
89 /*
90 * < 0 if hardware error occured (e.g. i2c err)
91 * and prevents normal operation.
92 */
93 int hard_fault;
94 struct mutex phy_lock;
95};
96static u8 len_seq[] = { 13, 24, 15, 29 };
97static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
98
99#define I2C_DUMP_SKB(info, skb) \
100do { \
101 pr_debug("%s:\n", info); \
102 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
103 16, 1, (skb)->data, (skb)->len, 0); \
104} while (0)
105
106/*
107 * In order to get the CLF in a known state we generate an internal reboot
108 * using a proprietary command.
109 * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
110 * fill buffer.
111 */
112static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
113{
114 u16 wait_reboot[] = { 50, 300, 1000 };
115 char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
116 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
117 int i, r = -1;
118
119 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
120 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
121 sizeof(reboot_cmd));
122 if (r < 0)
123 msleep(wait_reboot[i]);
124 }
125 if (r < 0)
126 return r;
127
128 /* CLF is spending about 20ms to do an internal reboot */
129 msleep(20);
130 r = -1;
131 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
132 r = i2c_master_recv(phy->i2c_dev, tmp,
133 ST21NFCA_HCI_LLC_MAX_SIZE);
134 if (r < 0)
135 msleep(wait_reboot[i]);
136 }
137 if (r < 0)
138 return r;
139
140 for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
141 tmp[i] == ST21NFCA_SOF_EOF; i++)
142 ;
143
144 if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
145 return -ENODEV;
146
147 usleep_range(1000, 1500);
148 return 0;
149}
150
151static int st21nfca_hci_i2c_enable(void *phy_id)
152{
153 struct st21nfca_i2c_phy *phy = phy_id;
154
155 gpio_set_value(phy->gpio_ena, 1);
156 phy->powered = 1;
157 phy->run_mode = ST21NFCA_HCI_MODE;
158
159 usleep_range(10000, 15000);
160
161 return 0;
162}
163
164static void st21nfca_hci_i2c_disable(void *phy_id)
165{
166 struct st21nfca_i2c_phy *phy = phy_id;
167
168 pr_info("\n");
169 gpio_set_value(phy->gpio_ena, 0);
170
171 phy->powered = 0;
172}
173
174static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
175{
176 u16 crc;
177 u8 tmp;
178
179 *skb_push(skb, 1) = 0;
180
181 crc = crc_ccitt(0xffff, skb->data, skb->len);
182 crc = ~crc;
183
184 tmp = crc & 0x00ff;
185 *skb_put(skb, 1) = tmp;
186
187 tmp = (crc >> 8) & 0x00ff;
188 *skb_put(skb, 1) = tmp;
189}
190
191static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
192{
193 skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
194 skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
195}
196
197/*
198 * Writing a frame must not return the number of written bytes.
199 * It must return either zero for success, or <0 for error.
200 * In addition, it must not alter the skb
201 */
202static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
203{
204 int r = -1, i, j;
205 struct st21nfca_i2c_phy *phy = phy_id;
206 struct i2c_client *client = phy->i2c_dev;
207 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
208
209 I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
210
211
212 if (phy->hard_fault != 0)
213 return phy->hard_fault;
214
215 /*
216 * Compute CRC before byte stuffing computation on frame
217 * Note st21nfca_hci_add_len_crc is doing a byte stuffing
218 * on its own value
219 */
220 st21nfca_hci_add_len_crc(skb);
221
222 /* add ST21NFCA_SOF_EOF on tail */
223 *skb_put(skb, 1) = ST21NFCA_SOF_EOF;
224 /* add ST21NFCA_SOF_EOF on head */
225 *skb_push(skb, 1) = ST21NFCA_SOF_EOF;
226
227 /*
228 * Compute byte stuffing
229 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
230 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
231 * xor byte with ST21NFCA_BYTE_STUFFING_MASK
232 */
233 tmp[0] = skb->data[0];
234 for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
235 if (skb->data[i] == ST21NFCA_SOF_EOF
236 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
237 tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
238 j++;
239 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
240 } else {
241 tmp[j] = skb->data[i];
242 }
243 }
244 tmp[j] = skb->data[i];
245 j++;
246
247 /*
248 * Manage sleep mode
249 * Try 3 times to send data with delay between each
250 */
251 mutex_lock(&phy->phy_lock);
252 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
253 r = i2c_master_send(client, tmp, j);
254 if (r < 0)
255 msleep(wait_tab[i]);
256 }
257 mutex_unlock(&phy->phy_lock);
258
259 if (r >= 0) {
260 if (r != j)
261 r = -EREMOTEIO;
262 else
263 r = 0;
264 }
265
266 st21nfca_hci_remove_len_crc(skb);
267
268 return r;
269}
270
271static int get_frame_size(u8 *buf, int buflen)
272{
273 int len = 0;
274 if (buf[len + 1] == ST21NFCA_SOF_EOF)
275 return 0;
276
277 for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
278 ;
279
280 return len;
281}
282
283static int check_crc(u8 *buf, int buflen)
284{
285 u16 crc;
286
287 crc = crc_ccitt(0xffff, buf, buflen - 2);
288 crc = ~crc;
289
290 if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
291 pr_err(ST21NFCA_HCI_DRIVER_NAME
292 ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
293 buf[buflen - 2]);
294
295 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
296 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
297 16, 2, buf, buflen, false);
298 return -EPERM;
299 }
300 return 0;
301}
302
303/*
304 * Prepare received data for upper layer.
305 * Received data include byte stuffing, crc and sof/eof
306 * which is not usable by hci part.
307 * returns:
308 * frame size without sof/eof, header and byte stuffing
309 * -EBADMSG : frame was incorrect and discarded
310 */
311static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
312{
313 int i, j, r, size;
314 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
315 return -EBADMSG;
316
317 size = get_frame_size(skb->data, skb->len);
318 if (size > 0) {
319 skb_trim(skb, size);
320 /* remove ST21NFCA byte stuffing for upper layer */
321 for (i = 1, j = 0; i < skb->len; i++) {
322 if (skb->data[i + j] ==
323 (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
324 skb->data[i] = skb->data[i + j + 1]
325 | ST21NFCA_BYTE_STUFFING_MASK;
326 i++;
327 j++;
328 }
329 skb->data[i] = skb->data[i + j];
330 }
331 /* remove byte stuffing useless byte */
332 skb_trim(skb, i - j);
333 /* remove ST21NFCA_SOF_EOF from head */
334 skb_pull(skb, 1);
335
336 r = check_crc(skb->data, skb->len);
337 if (r != 0) {
338 i = 0;
339 return -EBADMSG;
340 }
341
342 /* remove headbyte */
343 skb_pull(skb, 1);
344 /* remove crc. Byte Stuffing is already removed here */
345 skb_trim(skb, skb->len - 2);
346 return skb->len;
347 }
348 return 0;
349}
350
351/*
352 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
353 * that i2c bus will be flushed and that next read will start on a new frame.
354 * returned skb contains only LLC header and payload.
355 * returns:
356 * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
357 * end of read)
358 * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
359 * at end of read)
360 * -EREMOTEIO : i2c read error (fatal)
361 * -EBADMSG : frame was incorrect and discarded
362 * (value returned from st21nfca_hci_i2c_repack)
363 * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
364 * the read length end sequence
365 */
366static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
367 struct sk_buff *skb)
368{
369 int r, i;
370 u8 len;
371 u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
372 struct i2c_client *client = phy->i2c_dev;
373
374 if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
375 len = len_seq[phy->current_read_len];
376
377 /*
378 * Add retry mecanism
379 * Operation on I2C interface may fail in case of operation on
380 * RF or SWP interface
381 */
382 r = 0;
383 mutex_lock(&phy->phy_lock);
384 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
385 r = i2c_master_recv(client, buf, len);
386 if (r < 0)
387 msleep(wait_tab[i]);
388 }
389 mutex_unlock(&phy->phy_lock);
390
391 if (r != len) {
392 phy->current_read_len = 0;
393 return -EREMOTEIO;
394 }
395
396 /*
397 * The first read sequence does not start with SOF.
398 * Data is corrupeted so we drop it.
399 */
400 if (!phy->current_read_len && buf[0] != ST21NFCA_SOF_EOF) {
401 skb_trim(skb, 0);
402 phy->current_read_len = 0;
403 return -EIO;
404 } else if (phy->current_read_len &&
405 IS_START_OF_FRAME(buf)) {
406 /*
407 * Previous frame transmission was interrupted and
408 * the frame got repeated.
409 * Received frame start with ST21NFCA_SOF_EOF + 00.
410 */
411 skb_trim(skb, 0);
412 phy->current_read_len = 0;
413 }
414
415 memcpy(skb_put(skb, len), buf, len);
416
417 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
418 phy->current_read_len = 0;
419 return st21nfca_hci_i2c_repack(skb);
420 }
421 phy->current_read_len++;
422 return -EAGAIN;
423 }
424 return -EIO;
425}
426
427/*
428 * Reads an shdlc frame from the chip. This is not as straightforward as it
429 * seems. The frame format is data-crc, and corruption can occur anywhere
430 * while transiting on i2c bus, such that we could read an invalid data.
431 * The tricky case is when we read a corrupted data or crc. We must detect
432 * this here in order to determine that data can be transmitted to the hci
433 * core. This is the reason why we check the crc here.
434 * The CLF will repeat a frame until we send a RR on that frame.
435 *
436 * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
437 * available in the incoming data, other IRQ might come. Every IRQ will trigger
438 * a read sequence with different length and will fill the current frame.
439 * The reception is complete once we reach a ST21NFCA_SOF_EOF.
440 */
441static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
442{
443 struct st21nfca_i2c_phy *phy = phy_id;
444 struct i2c_client *client;
445
446 int r;
447
448 if (!phy || irq != phy->i2c_dev->irq) {
449 WARN_ON_ONCE(1);
450 return IRQ_NONE;
451 }
452
453 client = phy->i2c_dev;
454 dev_dbg(&client->dev, "IRQ\n");
455
456 if (phy->hard_fault != 0)
457 return IRQ_HANDLED;
458
459 r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
460 if (r == -EREMOTEIO) {
461 phy->hard_fault = r;
462
463 nfc_hci_recv_frame(phy->hdev, NULL);
464
465 return IRQ_HANDLED;
466 } else if (r == -EAGAIN || r == -EIO) {
467 return IRQ_HANDLED;
468 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
469 /*
470 * With ST21NFCA, only one interface (I2C, RF or SWP)
471 * may be active at a time.
472 * Having incorrect crc is usually due to i2c macrocell
473 * deactivation in the middle of a transmission.
474 * It may generate corrupted data on i2c.
475 * We give sometime to get i2c back.
476 * The complete frame will be repeated.
477 */
478 msleep(wait_tab[phy->crc_trials]);
479 phy->crc_trials++;
480 phy->current_read_len = 0;
481 kfree_skb(phy->pending_skb);
482 } else if (r > 0) {
483 /*
484 * We succeeded to read data from the CLF and
485 * data is valid.
486 * Reset counter.
487 */
488 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
489 phy->crc_trials = 0;
490 }
491
492 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
493 if (phy->pending_skb == NULL) {
494 phy->hard_fault = -ENOMEM;
495 nfc_hci_recv_frame(phy->hdev, NULL);
496 }
497
498 return IRQ_HANDLED;
499}
500
501static struct nfc_phy_ops i2c_phy_ops = {
502 .write = st21nfca_hci_i2c_write,
503 .enable = st21nfca_hci_i2c_enable,
504 .disable = st21nfca_hci_i2c_disable,
505};
506
507#ifdef CONFIG_OF
508static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
509{
510 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
511 struct device_node *pp;
512 int gpio;
513 int r;
514
515 pp = client->dev.of_node;
516 if (!pp)
517 return -ENODEV;
518
519 /* Get GPIO from device tree */
520 gpio = of_get_named_gpio(pp, "enable-gpios", 0);
521 if (gpio < 0) {
522 nfc_err(&client->dev, "Failed to retrieve enable-gpios from device tree\n");
523 return gpio;
524 }
525
526 /* GPIO request and configuration */
527 r = devm_gpio_request(&client->dev, gpio, "clf_enable");
528 if (r) {
529 nfc_err(&client->dev, "Failed to request enable pin\n");
530 return -ENODEV;
531 }
532
533 r = gpio_direction_output(gpio, 1);
534 if (r) {
535 nfc_err(&client->dev, "Failed to set enable pin direction as output\n");
536 return -ENODEV;
537 }
538 phy->gpio_ena = gpio;
539
540 /* IRQ */
541 r = irq_of_parse_and_map(pp, 0);
542 if (r < 0) {
543 nfc_err(&client->dev,
544 "Unable to get irq, error: %d\n", r);
545 return r;
546 }
547
548 phy->irq_polarity = irq_get_trigger_type(r);
549 client->irq = r;
550
551 return 0;
552}
553#else
554static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
555{
556 return -ENODEV;
557}
558#endif
559
560static int st21nfca_hci_i2c_request_resources(struct i2c_client *client)
561{
562 struct st21nfca_nfc_platform_data *pdata;
563 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
564 int r;
565 int irq;
566
567 pdata = client->dev.platform_data;
568 if (pdata == NULL) {
569 nfc_err(&client->dev, "No platform data\n");
570 return -EINVAL;
571 }
572
573 /* store for later use */
574 phy->gpio_irq = pdata->gpio_irq;
575 phy->gpio_ena = pdata->gpio_ena;
576 phy->irq_polarity = pdata->irq_polarity;
577
578 r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
579 if (r) {
580 pr_err("%s : gpio_request failed\n", __FILE__);
581 return -ENODEV;
582 }
583
584 r = gpio_direction_input(phy->gpio_irq);
585 if (r) {
586 pr_err("%s : gpio_direction_input failed\n", __FILE__);
587 return -ENODEV;
588 }
589
590 if (phy->gpio_ena > 0) {
591 r = devm_gpio_request(&client->dev,
592 phy->gpio_ena, "clf_enable");
593 if (r) {
594 pr_err("%s : ena gpio_request failed\n", __FILE__);
595 return -ENODEV;
596 }
597 r = gpio_direction_output(phy->gpio_ena, 1);
598
599 if (r) {
600 pr_err("%s : ena gpio_direction_output failed\n",
601 __FILE__);
602 return -ENODEV;
603 }
604 }
605
606 /* IRQ */
607 irq = gpio_to_irq(phy->gpio_irq);
608 if (irq < 0) {
609 nfc_err(&client->dev,
610 "Unable to get irq number for GPIO %d error %d\n",
611 phy->gpio_irq, r);
612 return -ENODEV;
613 }
614 client->irq = irq;
615
616 return 0;
617}
618
619static int st21nfca_hci_i2c_probe(struct i2c_client *client,
620 const struct i2c_device_id *id)
621{
622 struct st21nfca_i2c_phy *phy;
623 struct st21nfca_nfc_platform_data *pdata;
624 int r;
625
626 dev_dbg(&client->dev, "%s\n", __func__);
627 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
628
629 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
630 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
631 return -ENODEV;
632 }
633
634 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
635 GFP_KERNEL);
636 if (!phy) {
637 nfc_err(&client->dev,
638 "Cannot allocate memory for st21nfca i2c phy.\n");
639 return -ENOMEM;
640 }
641
642 phy->i2c_dev = client;
643 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
644 if (phy->pending_skb == NULL)
645 return -ENOMEM;
646
647 phy->current_read_len = 0;
648 phy->crc_trials = 0;
649 mutex_init(&phy->phy_lock);
650 i2c_set_clientdata(client, phy);
651
652 pdata = client->dev.platform_data;
653 if (!pdata && client->dev.of_node) {
654 r = st21nfca_hci_i2c_of_request_resources(client);
655 if (r) {
656 nfc_err(&client->dev, "No platform data\n");
657 return r;
658 }
659 } else if (pdata) {
660 r = st21nfca_hci_i2c_request_resources(client);
661 if (r) {
662 nfc_err(&client->dev, "Cannot get platform resources\n");
663 return r;
664 }
665 } else {
666 nfc_err(&client->dev, "st21nfca platform resources not available\n");
667 return -ENODEV;
668 }
669
670 r = st21nfca_hci_platform_init(phy);
671 if (r < 0) {
672 nfc_err(&client->dev, "Unable to reboot st21nfca\n");
673 return -ENODEV;
674 }
675
676 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
677 st21nfca_hci_irq_thread_fn,
678 phy->irq_polarity | IRQF_ONESHOT,
679 ST21NFCA_HCI_DRIVER_NAME, phy);
680 if (r < 0) {
681 nfc_err(&client->dev, "Unable to register IRQ handler\n");
682 return r;
683 }
684
685 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
686 ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM,
687 ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev);
688}
689
690static int st21nfca_hci_i2c_remove(struct i2c_client *client)
691{
692 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
693
694 dev_dbg(&client->dev, "%s\n", __func__);
695
696 st21nfca_hci_remove(phy->hdev);
697
698 if (phy->powered)
699 st21nfca_hci_i2c_disable(phy);
700
701 return 0;
702}
703
704static const struct of_device_id of_st21nfca_i2c_match[] = {
705 { .compatible = "st,st21nfca_i2c", },
706 {}
707};
708
709static struct i2c_driver st21nfca_hci_i2c_driver = {
710 .driver = {
711 .owner = THIS_MODULE,
712 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
713 .owner = THIS_MODULE,
714 .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
715 },
716 .probe = st21nfca_hci_i2c_probe,
717 .id_table = st21nfca_hci_i2c_id_table,
718 .remove = st21nfca_hci_i2c_remove,
719};
720
721module_i2c_driver(st21nfca_hci_i2c_driver);
722
723MODULE_LICENSE("GPL");
724MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/st21nfca/st21nfca.c b/drivers/nfc/st21nfca/st21nfca.c
new file mode 100644
index 000000000000..51e0f00b3a4f
--- /dev/null
+++ b/drivers/nfc/st21nfca/st21nfca.c
@@ -0,0 +1,698 @@
1/*
2 * HCI based Driver for STMicroelectronics NFC Chip
3 *
4 * Copyright (C) 2014 STMicroelectronics SAS. 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
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/module.h>
20#include <linux/nfc.h>
21#include <net/nfc/hci.h>
22#include <net/nfc/llc.h>
23
24#include "st21nfca.h"
25
26#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
27
28#define FULL_VERSION_LEN 3
29
30/* Proprietary gates, events, commands and registers */
31
32/* Commands that apply to all RF readers */
33#define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
34
35#define ST21NFCA_RF_READER_ISO15693_GATE 0x12
36#define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
37
38/*
39 * Reader gate for communication with contact-less cards using Type A
40 * protocol ISO14443-3 but not compliant with ISO14443-4
41 */
42#define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
43#define ST21NFCA_RF_READER_14443_3_A_UID 0x02
44#define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
45#define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
46
47#define ST21NFCA_DEVICE_MGNT_GATE 0x01
48#define ST21NFCA_DEVICE_MGNT_PIPE 0x02
49
50#define ST21NFCA_DM_GETINFO 0x13
51#define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
52#define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
53#define ST21NFCA_DM_PIPE_CREATED 0x02
54#define ST21NFCA_DM_PIPE_OPEN 0x04
55#define ST21NFCA_DM_RF_ACTIVE 0x80
56
57#define ST21NFCA_DM_IS_PIPE_OPEN(p) \
58 ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
59
60#define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
61
62static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
63
64static struct nfc_hci_gate st21nfca_gates[] = {
65 {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
66 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
67 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
68 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
69 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
70 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
71 {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
72 {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
73 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
74 {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
75};
76
77struct st21nfca_pipe_info {
78 u8 pipe_state;
79 u8 src_host_id;
80 u8 src_gate_id;
81 u8 dst_host_id;
82 u8 dst_gate_id;
83} __packed;
84
85/* Largest headroom needed for outgoing custom commands */
86#define ST21NFCA_CMDS_HEADROOM 7
87
88static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
89{
90 int i, j, r;
91 struct sk_buff *skb_pipe_list, *skb_pipe_info;
92 struct st21nfca_pipe_info *info;
93
94 u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
95 NFC_HCI_TERMINAL_HOST_ID
96 };
97 u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
98 NFC_HCI_TERMINAL_HOST_ID, 0
99 };
100
101 skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
102 if (!skb_pipe_list) {
103 r = -ENOMEM;
104 goto free_list;
105 }
106
107 skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
108 if (!skb_pipe_info) {
109 r = -ENOMEM;
110 goto free_info;
111 }
112
113 /* On ST21NFCA device pipes number are dynamics
114 * A maximum of 16 pipes can be created at the same time
115 * If pipes are already created, hci_dev_up will fail.
116 * Doing a clear all pipe is a bad idea because:
117 * - It does useless EEPROM cycling
118 * - It might cause issue for secure elements support
119 * (such as removing connectivity or APDU reader pipe)
120 * A better approach on ST21NFCA is to:
121 * - get a pipe list for each host.
122 * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
123 * (TODO Later on UICC HOST and eSE HOST)
124 * - get pipe information
125 * - match retrieved pipe list in st21nfca_gates
126 * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
127 * with ST21NFCA_DEVICE_MGNT_PIPE.
128 * Pipe can be closed and need to be open.
129 */
130 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
131 ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
132 if (r < 0)
133 goto free_info;
134
135 /* Get pipe list */
136 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
137 ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
138 &skb_pipe_list);
139 if (r < 0)
140 goto free_info;
141
142 /* Complete the existing gate_pipe table */
143 for (i = 0; i < skb_pipe_list->len; i++) {
144 pipe_info[2] = skb_pipe_list->data[i];
145 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
146 ST21NFCA_DM_GETINFO, pipe_info,
147 sizeof(pipe_info), &skb_pipe_info);
148
149 if (r)
150 continue;
151
152 /*
153 * Match pipe ID and gate ID
154 * Output format from ST21NFC_DM_GETINFO is:
155 * - pipe state (1byte)
156 * - source hid (1byte)
157 * - source gid (1byte)
158 * - destination hid (1byte)
159 * - destination gid (1byte)
160 */
161 info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
162 for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
163 (st21nfca_gates[j].gate != info->dst_gate_id);
164 j++)
165 ;
166
167 if (j < ARRAY_SIZE(st21nfca_gates) &&
168 st21nfca_gates[j].gate == info->dst_gate_id &&
169 ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
170 st21nfca_gates[j].pipe = pipe_info[2];
171 hdev->gate2pipe[st21nfca_gates[j].gate] =
172 st21nfca_gates[j].pipe;
173 }
174 }
175
176 /*
177 * 3 gates have a well known pipe ID.
178 * They will never appear in the pipe list
179 */
180 if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
181 for (i = skb_pipe_list->len + 3;
182 i < ARRAY_SIZE(st21nfca_gates); i++) {
183 r = nfc_hci_connect_gate(hdev,
184 NFC_HCI_HOST_CONTROLLER_ID,
185 st21nfca_gates[i].gate,
186 st21nfca_gates[i].pipe);
187 if (r < 0)
188 goto free_info;
189 }
190 }
191
192 memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
193free_info:
194 kfree_skb(skb_pipe_info);
195free_list:
196 kfree_skb(skb_pipe_list);
197 return r;
198}
199
200static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
201{
202 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
203 int r;
204
205 mutex_lock(&info->info_lock);
206
207 if (info->state != ST21NFCA_ST_COLD) {
208 r = -EBUSY;
209 goto out;
210 }
211
212 r = info->phy_ops->enable(info->phy_id);
213
214 if (r == 0)
215 info->state = ST21NFCA_ST_READY;
216
217out:
218 mutex_unlock(&info->info_lock);
219 return r;
220}
221
222static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
223{
224 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
225
226 mutex_lock(&info->info_lock);
227
228 if (info->state == ST21NFCA_ST_COLD)
229 goto out;
230
231 info->phy_ops->disable(info->phy_id);
232 info->state = ST21NFCA_ST_COLD;
233
234out:
235 mutex_unlock(&info->info_lock);
236}
237
238static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
239{
240 struct sk_buff *skb;
241
242 u8 param;
243 int r;
244
245 param = NFC_HCI_UICC_HOST_ID;
246 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
247 NFC_HCI_ADMIN_WHITELIST, &param, 1);
248 if (r < 0)
249 return r;
250
251 /* Set NFC_MODE in device management gate to enable */
252 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
253 ST21NFCA_NFC_MODE, &skb);
254 if (r < 0)
255 return r;
256
257 if (skb->data[0] == 0) {
258 kfree_skb(skb);
259 param = 1;
260
261 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
262 ST21NFCA_NFC_MODE, &param, 1);
263 if (r < 0)
264 return r;
265 }
266
267 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
268 NFC_HCI_EVT_END_OPERATION, NULL, 0);
269 if (r < 0)
270 return r;
271
272 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
273 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
274 if (r < 0)
275 return r;
276
277 if (skb->len != FULL_VERSION_LEN) {
278 kfree_skb(skb);
279 return -EINVAL;
280 }
281
282 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
283 DUMP_PREFIX_NONE, 16, 1,
284 skb->data, FULL_VERSION_LEN, false);
285
286 kfree_skb(skb);
287
288 return 0;
289}
290
291static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
292{
293 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
294
295 return info->phy_ops->write(info->phy_id, skb);
296}
297
298static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
299 u32 im_protocols, u32 tm_protocols)
300{
301 int r;
302
303 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
304 __func__, im_protocols, tm_protocols);
305
306 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
307 NFC_HCI_EVT_END_OPERATION, NULL, 0);
308 if (r < 0)
309 return r;
310 if (im_protocols) {
311 /*
312 * enable polling according to im_protocols & tm_protocols
313 * - CLOSE pipe according to im_protocols & tm_protocols
314 */
315 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
316 r = nfc_hci_disconnect_gate(hdev,
317 NFC_HCI_RF_READER_B_GATE);
318 if (r < 0)
319 return r;
320 }
321
322 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
323 r = nfc_hci_disconnect_gate(hdev,
324 NFC_HCI_RF_READER_A_GATE);
325 if (r < 0)
326 return r;
327 }
328
329 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
330 r = nfc_hci_disconnect_gate(hdev,
331 ST21NFCA_RF_READER_F_GATE);
332 if (r < 0)
333 return r;
334 }
335
336 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
337 r = nfc_hci_disconnect_gate(hdev,
338 ST21NFCA_RF_READER_14443_3_A_GATE);
339 if (r < 0)
340 return r;
341 }
342
343 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
344 r = nfc_hci_disconnect_gate(hdev,
345 ST21NFCA_RF_READER_ISO15693_GATE);
346 if (r < 0)
347 return r;
348 }
349
350 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
351 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
352 if (r < 0)
353 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
354 NFC_HCI_EVT_END_OPERATION, NULL, 0);
355 }
356 return r;
357}
358
359static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
360{
361 int r;
362 struct sk_buff *atqa_skb = NULL;
363
364 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
365 ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
366 if (r < 0)
367 goto exit;
368
369 if (atqa_skb->len != 2) {
370 r = -EPROTO;
371 goto exit;
372 }
373
374 *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
375
376exit:
377 kfree_skb(atqa_skb);
378 return r;
379}
380
381static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
382{
383 int r;
384 struct sk_buff *sak_skb = NULL;
385
386 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
387 ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
388 if (r < 0)
389 goto exit;
390
391 if (sak_skb->len != 1) {
392 r = -EPROTO;
393 goto exit;
394 }
395
396 *sak = sak_skb->data[0];
397
398exit:
399 kfree_skb(sak_skb);
400 return r;
401}
402
403static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
404 int *len)
405{
406 int r;
407 struct sk_buff *uid_skb = NULL;
408
409 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
410 ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
411 if (r < 0)
412 goto exit;
413
414 if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
415 r = -EPROTO;
416 goto exit;
417 }
418
419 gate = uid_skb->data;
420 *len = uid_skb->len;
421exit:
422 kfree_skb(uid_skb);
423 return r;
424}
425
426static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
427 struct nfc_target *target)
428{
429 int r;
430 struct sk_buff *inventory_skb = NULL;
431
432 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
433 ST21NFCA_RF_READER_ISO15693_INVENTORY,
434 &inventory_skb);
435 if (r < 0)
436 goto exit;
437
438 skb_pull(inventory_skb, 2);
439
440 if (inventory_skb->len == 0 ||
441 inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
442 r = -EPROTO;
443 goto exit;
444 }
445
446 memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
447 target->iso15693_dsfid = inventory_skb->data[1];
448 target->is_iso15693 = 1;
449exit:
450 kfree_skb(inventory_skb);
451 return r;
452}
453
454static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
455 struct nfc_target *target)
456{
457 int r, len;
458 u16 atqa;
459 u8 sak;
460 u8 uid[NFC_NFCID1_MAXSIZE];
461
462 switch (gate) {
463 case ST21NFCA_RF_READER_F_GATE:
464 target->supported_protocols = NFC_PROTO_FELICA_MASK;
465 break;
466 case ST21NFCA_RF_READER_14443_3_A_GATE:
467 /* ISO14443-3 type 1 or 2 tags */
468 r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
469 if (r < 0)
470 return r;
471 if (atqa == 0x000c) {
472 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
473 target->sens_res = 0x0c00;
474 } else {
475 r = st21nfca_get_iso14443_3_sak(hdev, &sak);
476 if (r < 0)
477 return r;
478
479 r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
480 if (r < 0)
481 return r;
482
483 target->supported_protocols =
484 nfc_hci_sak_to_protocol(sak);
485 if (target->supported_protocols == 0xffffffff)
486 return -EPROTO;
487
488 target->sens_res = atqa;
489 target->sel_res = sak;
490 memcpy(target->nfcid1, uid, len);
491 target->nfcid1_len = len;
492 }
493
494 break;
495 case ST21NFCA_RF_READER_ISO15693_GATE:
496 target->supported_protocols = NFC_PROTO_ISO15693_MASK;
497 r = st21nfca_get_iso15693_inventory(hdev, target);
498 if (r < 0)
499 return r;
500 break;
501 default:
502 return -EPROTO;
503 }
504
505 return 0;
506}
507
508#define ST21NFCA_CB_TYPE_READER_ISO15693 1
509static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
510 int err)
511{
512 struct st21nfca_hci_info *info = context;
513
514 switch (info->async_cb_type) {
515 case ST21NFCA_CB_TYPE_READER_ISO15693:
516 if (err == 0)
517 skb_trim(skb, skb->len - 1);
518 info->async_cb(info->async_cb_context, skb, err);
519 break;
520 default:
521 if (err == 0)
522 kfree_skb(skb);
523 break;
524 }
525}
526
527/*
528 * Returns:
529 * <= 0: driver handled the data exchange
530 * 1: driver doesn't especially handle, please do standard processing
531 */
532static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
533 struct nfc_target *target,
534 struct sk_buff *skb,
535 data_exchange_cb_t cb, void *cb_context)
536{
537 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
538
539 pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
540 target->hci_reader_gate, skb->len);
541
542 switch (target->hci_reader_gate) {
543 case ST21NFCA_RF_READER_F_GATE:
544 *skb_push(skb, 1) = 0x1a;
545 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
546 ST21NFCA_WR_XCHG_DATA, skb->data,
547 skb->len, cb, cb_context);
548 case ST21NFCA_RF_READER_14443_3_A_GATE:
549 *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
550
551 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
552 ST21NFCA_WR_XCHG_DATA, skb->data,
553 skb->len, cb, cb_context);
554 case ST21NFCA_RF_READER_ISO15693_GATE:
555 info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
556 info->async_cb = cb;
557 info->async_cb_context = cb_context;
558
559 *skb_push(skb, 1) = 0x17;
560
561 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
562 ST21NFCA_WR_XCHG_DATA, skb->data,
563 skb->len,
564 st21nfca_hci_data_exchange_cb,
565 info);
566 break;
567 default:
568 return 1;
569 }
570}
571
572static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
573 struct nfc_target *target)
574{
575 u8 fwi = 0x11;
576 switch (target->hci_reader_gate) {
577 case NFC_HCI_RF_READER_A_GATE:
578 case NFC_HCI_RF_READER_B_GATE:
579 /*
580 * PRESENCE_CHECK on those gates is available
581 * However, the answer to this command is taking 3 * fwi
582 * if the card is no present.
583 * Instead, we send an empty I-Frame with a very short
584 * configurable fwi ~604µs.
585 */
586 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
587 ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
588 case ST21NFCA_RF_READER_14443_3_A_GATE:
589 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
590 ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
591 NULL, 0, NULL);
592 default:
593 return -EOPNOTSUPP;
594 }
595}
596
597static struct nfc_hci_ops st21nfca_hci_ops = {
598 .open = st21nfca_hci_open,
599 .close = st21nfca_hci_close,
600 .load_session = st21nfca_hci_load_session,
601 .hci_ready = st21nfca_hci_ready,
602 .xmit = st21nfca_hci_xmit,
603 .start_poll = st21nfca_hci_start_poll,
604 .target_from_gate = st21nfca_hci_target_from_gate,
605 .im_transceive = st21nfca_hci_im_transceive,
606 .check_presence = st21nfca_hci_check_presence,
607};
608
609int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
610 char *llc_name, int phy_headroom, int phy_tailroom,
611 int phy_payload, struct nfc_hci_dev **hdev)
612{
613 struct st21nfca_hci_info *info;
614 int r = 0;
615 int dev_num;
616 u32 protocols;
617 struct nfc_hci_init_data init_data;
618 unsigned long quirks = 0;
619
620 info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
621 if (!info) {
622 r = -ENOMEM;
623 goto err_alloc_hdev;
624 }
625
626 info->phy_ops = phy_ops;
627 info->phy_id = phy_id;
628 info->state = ST21NFCA_ST_COLD;
629 mutex_init(&info->info_lock);
630
631 init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
632
633 memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
634
635 /*
636 * Session id must include the driver name + i2c bus addr
637 * persistent info to discriminate 2 identical chips
638 */
639 dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
640 if (dev_num >= ST21NFCA_NUM_DEVICES)
641 goto err_alloc_hdev;
642
643 scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
644 "ST21AH", dev_num);
645
646 protocols = NFC_PROTO_JEWEL_MASK |
647 NFC_PROTO_MIFARE_MASK |
648 NFC_PROTO_FELICA_MASK |
649 NFC_PROTO_ISO14443_MASK |
650 NFC_PROTO_ISO14443_B_MASK |
651 NFC_PROTO_ISO15693_MASK;
652
653 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
654
655 info->hdev =
656 nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
657 protocols, llc_name,
658 phy_headroom + ST21NFCA_CMDS_HEADROOM,
659 phy_tailroom, phy_payload);
660
661 if (!info->hdev) {
662 pr_err("Cannot allocate nfc hdev.\n");
663 r = -ENOMEM;
664 goto err_alloc_hdev;
665 }
666
667 nfc_hci_set_clientdata(info->hdev, info);
668
669 r = nfc_hci_register_device(info->hdev);
670 if (r)
671 goto err_regdev;
672
673 *hdev = info->hdev;
674
675 return 0;
676
677err_regdev:
678 nfc_hci_free_device(info->hdev);
679
680err_alloc_hdev:
681 kfree(info);
682
683 return r;
684}
685EXPORT_SYMBOL(st21nfca_hci_probe);
686
687void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
688{
689 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
690
691 nfc_hci_unregister_device(hdev);
692 nfc_hci_free_device(hdev);
693 kfree(info);
694}
695EXPORT_SYMBOL(st21nfca_hci_remove);
696
697MODULE_LICENSE("GPL");
698MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/st21nfca/st21nfca.h b/drivers/nfc/st21nfca/st21nfca.h
new file mode 100644
index 000000000000..334cd90bcc8c
--- /dev/null
+++ b/drivers/nfc/st21nfca/st21nfca.h
@@ -0,0 +1,87 @@
1/*
2 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef __LOCAL_ST21NFCA_H_
18#define __LOCAL_ST21NFCA_H_
19
20#include <net/nfc/hci.h>
21
22#define HCI_MODE 0
23
24/* framing in HCI mode */
25#define ST21NFCA_SOF_EOF_LEN 2
26
27/* Almost every time value is 0 */
28#define ST21NFCA_HCI_LLC_LEN 1
29
30/* Size in worst case :
31 * In normal case CRC len = 2 but byte stuffing
32 * may appear in case one CRC byte = ST21NFCA_SOF_EOF
33 */
34#define ST21NFCA_HCI_LLC_CRC 4
35
36#define ST21NFCA_HCI_LLC_LEN_CRC (ST21NFCA_SOF_EOF_LEN + \
37 ST21NFCA_HCI_LLC_LEN + \
38 ST21NFCA_HCI_LLC_CRC)
39#define ST21NFCA_HCI_LLC_MIN_SIZE (1 + ST21NFCA_HCI_LLC_LEN_CRC)
40
41/* Worst case when adding byte stuffing between each byte */
42#define ST21NFCA_HCI_LLC_MAX_PAYLOAD 29
43#define ST21NFCA_HCI_LLC_MAX_SIZE (ST21NFCA_HCI_LLC_LEN_CRC + 1 + \
44 ST21NFCA_HCI_LLC_MAX_PAYLOAD)
45
46#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
47
48#define ST21NFCA_HCI_MODE 0
49
50#define ST21NFCA_NUM_DEVICES 256
51
52int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
53 char *llc_name, int phy_headroom, int phy_tailroom,
54 int phy_payload, struct nfc_hci_dev **hdev);
55void st21nfca_hci_remove(struct nfc_hci_dev *hdev);
56
57enum st21nfca_state {
58 ST21NFCA_ST_COLD,
59 ST21NFCA_ST_READY,
60};
61
62struct st21nfca_hci_info {
63 struct nfc_phy_ops *phy_ops;
64 void *phy_id;
65
66 struct nfc_hci_dev *hdev;
67
68 enum st21nfca_state state;
69
70 struct mutex info_lock;
71
72 int async_cb_type;
73 data_exchange_cb_t async_cb;
74 void *async_cb_context;
75
76} __packed;
77
78/* Reader RF commands */
79#define ST21NFCA_WR_XCHG_DATA 0x10
80
81#define ST21NFCA_RF_READER_F_GATE 0x14
82#define ST21NFCA_RF_READER_F_DATARATE 0x01
83#define ST21NFCA_RF_READER_F_DATARATE_106 0x01
84#define ST21NFCA_RF_READER_F_DATARATE_212 0x02
85#define ST21NFCA_RF_READER_F_DATARATE_424 0x04
86
87#endif /* __LOCAL_ST21NFCA_H_ */
diff --git a/drivers/nfc/trf7970a.c b/drivers/nfc/trf7970a.c
index d9babe986473..3b78b031e617 100644
--- a/drivers/nfc/trf7970a.c
+++ b/drivers/nfc/trf7970a.c
@@ -16,6 +16,7 @@
16#include <linux/device.h> 16#include <linux/device.h>
17#include <linux/netdevice.h> 17#include <linux/netdevice.h>
18#include <linux/interrupt.h> 18#include <linux/interrupt.h>
19#include <linux/pm_runtime.h>
19#include <linux/nfc.h> 20#include <linux/nfc.h>
20#include <linux/skbuff.h> 21#include <linux/skbuff.h>
21#include <linux/delay.h> 22#include <linux/delay.h>
@@ -67,14 +68,14 @@
67 * only the SRX bit set, it means that all of the data has been received 68 * only the SRX bit set, it means that all of the data has been received
68 * (once what's in the fifo has been read). However, depending on timing 69 * (once what's in the fifo has been read). However, depending on timing
69 * an interrupt status with only the SRX bit set may not be recived. In 70 * an interrupt status with only the SRX bit set may not be recived. In
70 * those cases, the timeout mechanism is used to wait 5 ms in case more 71 * those cases, the timeout mechanism is used to wait 20 ms in case more
71 * data arrives. After 5 ms, it is assumed that all of the data has been 72 * data arrives. After 20 ms, it is assumed that all of the data has been
72 * received and the accumulated rx data is sent upstream. The 73 * received and the accumulated rx data is sent upstream. The
73 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose 74 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
74 * (i.e., it indicates that some data has been received but we're not sure 75 * (i.e., it indicates that some data has been received but we're not sure
75 * if there is more coming so a timeout in this state means all data has 76 * if there is more coming so a timeout in this state means all data has
76 * been received and there isn't an error). The delay is 5 ms since delays 77 * been received and there isn't an error). The delay is 20 ms since delays
77 * over 2 ms have been observed during testing (a little extra just in case). 78 * of ~16 ms have been observed during testing.
78 * 79 *
79 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK. 80 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
80 * Having only 4 bits in the FIFO won't normally generate an interrupt so 81 * Having only 4 bits in the FIFO won't normally generate an interrupt so
@@ -104,8 +105,11 @@
104 105
105#define TRF7970A_SUPPORTED_PROTOCOLS \ 106#define TRF7970A_SUPPORTED_PROTOCOLS \
106 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \ 107 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
108 NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
107 NFC_PROTO_ISO15693_MASK) 109 NFC_PROTO_ISO15693_MASK)
108 110
111#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
112
109/* TX data must be prefixed with a FIFO reset cmd, a cmd that depends 113/* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
110 * on what the current framing is, the address of the TX length byte 1 114 * on what the current framing is, the address of the TX length byte 1
111 * register (0x1d), and the 2 byte length of the data to be transmitted. 115 * register (0x1d), and the 2 byte length of the data to be transmitted.
@@ -120,7 +124,7 @@
120/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */ 124/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
121#define TRF7970A_TX_MAX (4096 - 1) 125#define TRF7970A_TX_MAX (4096 - 1)
122 126
123#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 5 127#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
124#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3 128#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
125#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20 129#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
126 130
@@ -330,13 +334,15 @@ struct trf7970a {
330 struct regulator *regulator; 334 struct regulator *regulator;
331 struct nfc_digital_dev *ddev; 335 struct nfc_digital_dev *ddev;
332 u32 quirks; 336 u32 quirks;
333 bool powering_up;
334 bool aborting; 337 bool aborting;
335 struct sk_buff *tx_skb; 338 struct sk_buff *tx_skb;
336 struct sk_buff *rx_skb; 339 struct sk_buff *rx_skb;
337 nfc_digital_cmd_complete_t cb; 340 nfc_digital_cmd_complete_t cb;
338 void *cb_arg; 341 void *cb_arg;
342 u8 chip_status_ctrl;
339 u8 iso_ctrl; 343 u8 iso_ctrl;
344 u8 iso_ctrl_tech;
345 u8 modulator_sys_clk_ctrl;
340 u8 special_fcn_reg1; 346 u8 special_fcn_reg1;
341 int technology; 347 int technology;
342 int framing; 348 int framing;
@@ -681,7 +687,9 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id)
681 trf->ignore_timeout = 687 trf->ignore_timeout =
682 !cancel_delayed_work(&trf->timeout_work); 688 !cancel_delayed_work(&trf->timeout_work);
683 trf7970a_drain_fifo(trf, status); 689 trf7970a_drain_fifo(trf, status);
684 } else if (!(status & TRF7970A_IRQ_STATUS_TX)) { 690 } else if (status == TRF7970A_IRQ_STATUS_TX) {
691 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
692 } else {
685 trf7970a_send_err_upstream(trf, -EIO); 693 trf7970a_send_err_upstream(trf, -EIO);
686 } 694 }
687 break; 695 break;
@@ -757,8 +765,8 @@ static int trf7970a_init(struct trf7970a *trf)
757 if (ret) 765 if (ret)
758 goto err_out; 766 goto err_out;
759 767
760 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, 768 /* Must clear NFC Target Detection Level reg due to erratum */
761 TRF7970A_MODULATOR_DEPTH_OOK); 769 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
762 if (ret) 770 if (ret)
763 goto err_out; 771 goto err_out;
764 772
@@ -774,12 +782,7 @@ static int trf7970a_init(struct trf7970a *trf)
774 782
775 trf->special_fcn_reg1 = 0; 783 trf->special_fcn_reg1 = 0;
776 784
777 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, 785 trf->iso_ctrl = 0xff;
778 TRF7970A_CHIP_STATUS_RF_ON |
779 TRF7970A_CHIP_STATUS_VRS5_3);
780 if (ret)
781 goto err_out;
782
783 return 0; 786 return 0;
784 787
785err_out: 788err_out:
@@ -791,53 +794,29 @@ static void trf7970a_switch_rf_off(struct trf7970a *trf)
791{ 794{
792 dev_dbg(trf->dev, "Switching rf off\n"); 795 dev_dbg(trf->dev, "Switching rf off\n");
793 796
794 gpio_set_value(trf->en_gpio, 0); 797 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
795 gpio_set_value(trf->en2_gpio, 0); 798
799 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
796 800
797 trf->aborting = false; 801 trf->aborting = false;
798 trf->state = TRF7970A_ST_OFF; 802 trf->state = TRF7970A_ST_OFF;
803
804 pm_runtime_mark_last_busy(trf->dev);
805 pm_runtime_put_autosuspend(trf->dev);
799} 806}
800 807
801static int trf7970a_switch_rf_on(struct trf7970a *trf) 808static void trf7970a_switch_rf_on(struct trf7970a *trf)
802{ 809{
803 unsigned long delay;
804 int ret;
805
806 dev_dbg(trf->dev, "Switching rf on\n"); 810 dev_dbg(trf->dev, "Switching rf on\n");
807 811
808 if (trf->powering_up) 812 pm_runtime_get_sync(trf->dev);
809 usleep_range(5000, 6000);
810
811 gpio_set_value(trf->en2_gpio, 1);
812 usleep_range(1000, 2000);
813 gpio_set_value(trf->en_gpio, 1);
814 813
815 /* The delay between enabling the trf7970a and issuing the first 814 trf->state = TRF7970A_ST_IDLE;
816 * command is significantly longer the very first time after powering
817 * up. Make sure the longer delay is only done the first time.
818 */
819 if (trf->powering_up) {
820 delay = 20000;
821 trf->powering_up = false;
822 } else {
823 delay = 5000;
824 }
825
826 usleep_range(delay, delay + 1000);
827
828 ret = trf7970a_init(trf);
829 if (ret)
830 trf7970a_switch_rf_off(trf);
831 else
832 trf->state = TRF7970A_ST_IDLE;
833
834 return ret;
835} 815}
836 816
837static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) 817static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
838{ 818{
839 struct trf7970a *trf = nfc_digital_get_drvdata(ddev); 819 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
840 int ret = 0;
841 820
842 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); 821 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
843 822
@@ -846,7 +825,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
846 if (on) { 825 if (on) {
847 switch (trf->state) { 826 switch (trf->state) {
848 case TRF7970A_ST_OFF: 827 case TRF7970A_ST_OFF:
849 ret = trf7970a_switch_rf_on(trf); 828 trf7970a_switch_rf_on(trf);
850 break; 829 break;
851 case TRF7970A_ST_IDLE: 830 case TRF7970A_ST_IDLE:
852 case TRF7970A_ST_IDLE_RX_BLOCKED: 831 case TRF7970A_ST_IDLE_RX_BLOCKED:
@@ -871,7 +850,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
871 } 850 }
872 851
873 mutex_unlock(&trf->lock); 852 mutex_unlock(&trf->lock);
874 return ret; 853 return 0;
875} 854}
876 855
877static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) 856static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
@@ -882,10 +861,24 @@ static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
882 861
883 switch (tech) { 862 switch (tech) {
884 case NFC_DIGITAL_RF_TECH_106A: 863 case NFC_DIGITAL_RF_TECH_106A:
885 trf->iso_ctrl = TRF7970A_ISO_CTRL_14443A_106; 864 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
865 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
866 break;
867 case NFC_DIGITAL_RF_TECH_106B:
868 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
869 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
870 break;
871 case NFC_DIGITAL_RF_TECH_212F:
872 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
873 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
874 break;
875 case NFC_DIGITAL_RF_TECH_424F:
876 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
877 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
886 break; 878 break;
887 case NFC_DIGITAL_RF_TECH_ISO15693: 879 case NFC_DIGITAL_RF_TECH_ISO15693:
888 trf->iso_ctrl = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; 880 trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
881 trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
889 break; 882 break;
890 default: 883 default:
891 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); 884 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
@@ -899,24 +892,31 @@ static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
899 892
900static int trf7970a_config_framing(struct trf7970a *trf, int framing) 893static int trf7970a_config_framing(struct trf7970a *trf, int framing)
901{ 894{
895 u8 iso_ctrl = trf->iso_ctrl_tech;
896 int ret;
897
902 dev_dbg(trf->dev, "framing: %d\n", framing); 898 dev_dbg(trf->dev, "framing: %d\n", framing);
903 899
904 switch (framing) { 900 switch (framing) {
905 case NFC_DIGITAL_FRAMING_NFCA_SHORT: 901 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
906 case NFC_DIGITAL_FRAMING_NFCA_STANDARD: 902 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
907 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; 903 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
908 trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; 904 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
909 break; 905 break;
910 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: 906 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
911 case NFC_DIGITAL_FRAMING_NFCA_T4T: 907 case NFC_DIGITAL_FRAMING_NFCA_T4T:
908 case NFC_DIGITAL_FRAMING_NFCB:
909 case NFC_DIGITAL_FRAMING_NFCB_T4T:
910 case NFC_DIGITAL_FRAMING_NFCF:
911 case NFC_DIGITAL_FRAMING_NFCF_T3T:
912 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: 912 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
913 case NFC_DIGITAL_FRAMING_ISO15693_T5T: 913 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
914 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; 914 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
915 trf->iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; 915 iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
916 break; 916 break;
917 case NFC_DIGITAL_FRAMING_NFCA_T2T: 917 case NFC_DIGITAL_FRAMING_NFCA_T2T:
918 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; 918 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
919 trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; 919 iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
920 break; 920 break;
921 default: 921 default:
922 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); 922 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
@@ -925,24 +925,46 @@ static int trf7970a_config_framing(struct trf7970a *trf, int framing)
925 925
926 trf->framing = framing; 926 trf->framing = framing;
927 927
928 return trf7970a_write(trf, TRF7970A_ISO_CTRL, trf->iso_ctrl); 928 if (iso_ctrl != trf->iso_ctrl) {
929 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
930 if (ret)
931 return ret;
932
933 trf->iso_ctrl = iso_ctrl;
934
935 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
936 trf->modulator_sys_clk_ctrl);
937 if (ret)
938 return ret;
939 }
940
941 if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
942 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
943 trf->chip_status_ctrl |
944 TRF7970A_CHIP_STATUS_RF_ON);
945 if (ret)
946 return ret;
947
948 trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
949
950 usleep_range(5000, 6000);
951 }
952
953 return 0;
929} 954}
930 955
931static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, 956static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
932 int param) 957 int param)
933{ 958{
934 struct trf7970a *trf = nfc_digital_get_drvdata(ddev); 959 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
935 int ret = 0; 960 int ret;
936 961
937 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); 962 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
938 963
939 mutex_lock(&trf->lock); 964 mutex_lock(&trf->lock);
940 965
941 if (trf->state == TRF7970A_ST_OFF) { 966 if (trf->state == TRF7970A_ST_OFF)
942 ret = trf7970a_switch_rf_on(trf); 967 trf7970a_switch_rf_on(trf);
943 if (ret)
944 goto err_out;
945 }
946 968
947 switch (type) { 969 switch (type) {
948 case NFC_DIGITAL_CONFIG_RF_TECH: 970 case NFC_DIGITAL_CONFIG_RF_TECH:
@@ -956,7 +978,6 @@ static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
956 ret = -EINVAL; 978 ret = -EINVAL;
957 } 979 }
958 980
959err_out:
960 mutex_unlock(&trf->lock); 981 mutex_unlock(&trf->lock);
961 return ret; 982 return ret;
962} 983}
@@ -1191,7 +1212,18 @@ static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1191 dev_dbg(trf->dev, "Abort process initiated\n"); 1212 dev_dbg(trf->dev, "Abort process initiated\n");
1192 1213
1193 mutex_lock(&trf->lock); 1214 mutex_lock(&trf->lock);
1194 trf->aborting = true; 1215
1216 switch (trf->state) {
1217 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1218 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1219 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1220 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1221 trf->aborting = true;
1222 break;
1223 default:
1224 break;
1225 }
1226
1195 mutex_unlock(&trf->lock); 1227 mutex_unlock(&trf->lock);
1196} 1228}
1197 1229
@@ -1206,12 +1238,25 @@ static struct nfc_digital_ops trf7970a_nfc_ops = {
1206 .abort_cmd = trf7970a_abort_cmd, 1238 .abort_cmd = trf7970a_abort_cmd,
1207}; 1239};
1208 1240
1241static int trf7970a_get_autosuspend_delay(struct device_node *np)
1242{
1243 int autosuspend_delay, ret;
1244
1245 ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
1246 if (ret)
1247 autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
1248
1249 of_node_put(np);
1250
1251 return autosuspend_delay;
1252}
1253
1209static int trf7970a_probe(struct spi_device *spi) 1254static int trf7970a_probe(struct spi_device *spi)
1210{ 1255{
1211 struct device_node *np = spi->dev.of_node; 1256 struct device_node *np = spi->dev.of_node;
1212 const struct spi_device_id *id = spi_get_device_id(spi); 1257 const struct spi_device_id *id = spi_get_device_id(spi);
1213 struct trf7970a *trf; 1258 struct trf7970a *trf;
1214 int ret; 1259 int uvolts, autosuspend_delay, ret;
1215 1260
1216 if (!np) { 1261 if (!np) {
1217 dev_err(&spi->dev, "No Device Tree entry\n"); 1262 dev_err(&spi->dev, "No Device Tree entry\n");
@@ -1281,7 +1326,10 @@ static int trf7970a_probe(struct spi_device *spi)
1281 goto err_destroy_lock; 1326 goto err_destroy_lock;
1282 } 1327 }
1283 1328
1284 trf->powering_up = true; 1329 uvolts = regulator_get_voltage(trf->regulator);
1330
1331 if (uvolts > 4000000)
1332 trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
1285 1333
1286 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, 1334 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
1287 TRF7970A_SUPPORTED_PROTOCOLS, 1335 TRF7970A_SUPPORTED_PROTOCOLS,
@@ -1297,6 +1345,12 @@ static int trf7970a_probe(struct spi_device *spi)
1297 nfc_digital_set_drvdata(trf->ddev, trf); 1345 nfc_digital_set_drvdata(trf->ddev, trf);
1298 spi_set_drvdata(spi, trf); 1346 spi_set_drvdata(spi, trf);
1299 1347
1348 autosuspend_delay = trf7970a_get_autosuspend_delay(np);
1349
1350 pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
1351 pm_runtime_use_autosuspend(trf->dev);
1352 pm_runtime_enable(trf->dev);
1353
1300 ret = nfc_digital_register_device(trf->ddev); 1354 ret = nfc_digital_register_device(trf->ddev);
1301 if (ret) { 1355 if (ret) {
1302 dev_err(trf->dev, "Can't register NFC digital device: %d\n", 1356 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
@@ -1307,6 +1361,7 @@ static int trf7970a_probe(struct spi_device *spi)
1307 return 0; 1361 return 0;
1308 1362
1309err_free_ddev: 1363err_free_ddev:
1364 pm_runtime_disable(trf->dev);
1310 nfc_digital_free_device(trf->ddev); 1365 nfc_digital_free_device(trf->ddev);
1311err_disable_regulator: 1366err_disable_regulator:
1312 regulator_disable(trf->regulator); 1367 regulator_disable(trf->regulator);
@@ -1321,15 +1376,16 @@ static int trf7970a_remove(struct spi_device *spi)
1321 1376
1322 mutex_lock(&trf->lock); 1377 mutex_lock(&trf->lock);
1323 1378
1324 trf7970a_switch_rf_off(trf);
1325 trf7970a_init(trf);
1326
1327 switch (trf->state) { 1379 switch (trf->state) {
1328 case TRF7970A_ST_WAIT_FOR_TX_FIFO: 1380 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1329 case TRF7970A_ST_WAIT_FOR_RX_DATA: 1381 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1330 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: 1382 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1331 case TRF7970A_ST_WAIT_TO_ISSUE_EOF: 1383 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1332 trf7970a_send_err_upstream(trf, -ECANCELED); 1384 trf7970a_send_err_upstream(trf, -ECANCELED);
1385 /* FALLTHROUGH */
1386 case TRF7970A_ST_IDLE:
1387 case TRF7970A_ST_IDLE_RX_BLOCKED:
1388 pm_runtime_put_sync(trf->dev);
1333 break; 1389 break;
1334 default: 1390 default:
1335 break; 1391 break;
@@ -1337,6 +1393,8 @@ static int trf7970a_remove(struct spi_device *spi)
1337 1393
1338 mutex_unlock(&trf->lock); 1394 mutex_unlock(&trf->lock);
1339 1395
1396 pm_runtime_disable(trf->dev);
1397
1340 nfc_digital_unregister_device(trf->ddev); 1398 nfc_digital_unregister_device(trf->ddev);
1341 nfc_digital_free_device(trf->ddev); 1399 nfc_digital_free_device(trf->ddev);
1342 1400
@@ -1347,6 +1405,70 @@ static int trf7970a_remove(struct spi_device *spi)
1347 return 0; 1405 return 0;
1348} 1406}
1349 1407
1408#ifdef CONFIG_PM_RUNTIME
1409static int trf7970a_pm_runtime_suspend(struct device *dev)
1410{
1411 struct spi_device *spi = container_of(dev, struct spi_device, dev);
1412 struct trf7970a *trf = spi_get_drvdata(spi);
1413 int ret;
1414
1415 dev_dbg(dev, "Runtime suspend\n");
1416
1417 if (trf->state != TRF7970A_ST_OFF) {
1418 dev_dbg(dev, "Can't suspend - not in OFF state (%d)\n",
1419 trf->state);
1420 return -EBUSY;
1421 }
1422
1423 gpio_set_value(trf->en_gpio, 0);
1424 gpio_set_value(trf->en2_gpio, 0);
1425
1426 ret = regulator_disable(trf->regulator);
1427 if (ret)
1428 dev_err(dev, "%s - Can't disable VIN: %d\n", __func__, ret);
1429
1430 return ret;
1431}
1432
1433static int trf7970a_pm_runtime_resume(struct device *dev)
1434{
1435 struct spi_device *spi = container_of(dev, struct spi_device, dev);
1436 struct trf7970a *trf = spi_get_drvdata(spi);
1437 int ret;
1438
1439 dev_dbg(dev, "Runtime resume\n");
1440
1441 ret = regulator_enable(trf->regulator);
1442 if (ret) {
1443 dev_err(dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1444 return ret;
1445 }
1446
1447 usleep_range(5000, 6000);
1448
1449 gpio_set_value(trf->en2_gpio, 1);
1450 usleep_range(1000, 2000);
1451 gpio_set_value(trf->en_gpio, 1);
1452
1453 usleep_range(20000, 21000);
1454
1455 ret = trf7970a_init(trf);
1456 if (ret) {
1457 dev_err(dev, "%s - Can't initialize: %d\n", __func__, ret);
1458 return ret;
1459 }
1460
1461 pm_runtime_mark_last_busy(dev);
1462
1463 return 0;
1464}
1465#endif
1466
1467static const struct dev_pm_ops trf7970a_pm_ops = {
1468 SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
1469 trf7970a_pm_runtime_resume, NULL)
1470};
1471
1350static const struct spi_device_id trf7970a_id_table[] = { 1472static const struct spi_device_id trf7970a_id_table[] = {
1351 { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA }, 1473 { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA },
1352 { } 1474 { }
@@ -1360,6 +1482,7 @@ static struct spi_driver trf7970a_spi_driver = {
1360 .driver = { 1482 .driver = {
1361 .name = "trf7970a", 1483 .name = "trf7970a",
1362 .owner = THIS_MODULE, 1484 .owner = THIS_MODULE,
1485 .pm = &trf7970a_pm_ops,
1363 }, 1486 },
1364}; 1487};
1365 1488