2 * Driver for NXP PN533 NFC Chip - I2C transport layer
4 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Copyright (C) 2012-2013 Tieto Poland
6 * Copyright (C) 2016 HALE electronic
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include <linux/device.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
28 #include <linux/nfc.h>
29 #include <linux/netdevice.h>
30 #include <linux/interrupt.h>
31 #include <net/nfc/nfc.h>
36 #define PN533_I2C_DRIVER_NAME "pn533_i2c"
38 struct pn533_i2c_phy
{
39 struct i2c_client
*i2c_dev
;
45 * < 0 if hardware error occurred (e.g. i2c err)
46 * and prevents normal operation.
50 static int pn533_i2c_send_ack(struct pn533
*dev
, gfp_t flags
)
52 struct pn533_i2c_phy
*phy
= dev
->phy
;
53 struct i2c_client
*client
= phy
->i2c_dev
;
54 static const u8 ack
[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
55 /* spec 6.2.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
58 rc
= i2c_master_send(client
, ack
, 6);
63 static int pn533_i2c_send_frame(struct pn533
*dev
,
66 struct pn533_i2c_phy
*phy
= dev
->phy
;
67 struct i2c_client
*client
= phy
->i2c_dev
;
70 if (phy
->hard_fault
!= 0)
71 return phy
->hard_fault
;
73 if (phy
->priv
== NULL
)
78 print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE
, 16, 1,
79 out
->data
, out
->len
, false);
81 rc
= i2c_master_send(client
, out
->data
, out
->len
);
83 if (rc
== -EREMOTEIO
) { /* Retry, chip was in power down */
84 usleep_range(6000, 10000);
85 rc
= i2c_master_send(client
, out
->data
, out
->len
);
98 static void pn533_i2c_abort_cmd(struct pn533
*dev
, gfp_t flags
)
100 struct pn533_i2c_phy
*phy
= dev
->phy
;
104 /* An ack will cancel the last issued command */
105 pn533_i2c_send_ack(dev
, flags
);
107 /* schedule cmd_complete_work to finish current command execution */
108 pn533_recv_frame(phy
->priv
, NULL
, -ENOENT
);
111 static int pn533_i2c_read(struct pn533_i2c_phy
*phy
, struct sk_buff
**skb
)
113 struct i2c_client
*client
= phy
->i2c_dev
;
114 int len
= PN533_EXT_FRAME_HEADER_LEN
+
115 PN533_STD_FRAME_MAX_PAYLOAD_LEN
+
116 PN533_STD_FRAME_TAIL_LEN
+ 1;
119 *skb
= alloc_skb(len
, GFP_KERNEL
);
123 r
= i2c_master_recv(client
, skb_put(*skb
, len
), len
);
125 nfc_err(&client
->dev
, "cannot read. r=%d len=%d\n", r
, len
);
130 if (!((*skb
)->data
[0] & 0x01)) {
131 nfc_err(&client
->dev
, "READY flag not set");
136 /* remove READY byte */
138 /* trim to frame size */
139 skb_trim(*skb
, phy
->priv
->ops
->rx_frame_size((*skb
)->data
));
144 static irqreturn_t
pn533_i2c_irq_thread_fn(int irq
, void *data
)
146 struct pn533_i2c_phy
*phy
= data
;
147 struct i2c_client
*client
;
148 struct sk_buff
*skb
= NULL
;
151 if (!phy
|| irq
!= phy
->i2c_dev
->irq
) {
156 client
= phy
->i2c_dev
;
157 dev_dbg(&client
->dev
, "IRQ\n");
159 if (phy
->hard_fault
!= 0)
162 r
= pn533_i2c_read(phy
, &skb
);
163 if (r
== -EREMOTEIO
) {
166 pn533_recv_frame(phy
->priv
, NULL
, -EREMOTEIO
);
169 } else if ((r
== -ENOMEM
) || (r
== -EBADMSG
) || (r
== -EBUSY
)) {
174 pn533_recv_frame(phy
->priv
, skb
, 0);
179 static struct pn533_phy_ops i2c_phy_ops
= {
180 .send_frame
= pn533_i2c_send_frame
,
181 .send_ack
= pn533_i2c_send_ack
,
182 .abort_cmd
= pn533_i2c_abort_cmd
,
186 static int pn533_i2c_probe(struct i2c_client
*client
,
187 const struct i2c_device_id
*id
)
189 struct pn533_i2c_phy
*phy
;
193 dev_dbg(&client
->dev
, "%s\n", __func__
);
194 dev_dbg(&client
->dev
, "IRQ: %d\n", client
->irq
);
196 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
197 nfc_err(&client
->dev
, "Need I2C_FUNC_I2C\n");
201 phy
= devm_kzalloc(&client
->dev
, sizeof(struct pn533_i2c_phy
),
206 phy
->i2c_dev
= client
;
207 i2c_set_clientdata(client
, phy
);
209 priv
= pn533_register_device(PN533_DEVICE_PN532
,
210 PN533_NO_TYPE_B_PROTOCOLS
,
211 PN533_PROTO_REQ_ACK_RESP
,
212 phy
, &i2c_phy_ops
, NULL
,
223 r
= request_threaded_irq(client
->irq
, NULL
, pn533_i2c_irq_thread_fn
,
224 IRQF_TRIGGER_FALLING
|
225 IRQF_SHARED
| IRQF_ONESHOT
,
226 PN533_I2C_DRIVER_NAME
, phy
);
228 nfc_err(&client
->dev
, "Unable to register IRQ handler\n");
232 r
= pn533_finalize_setup(priv
);
239 free_irq(client
->irq
, phy
);
242 pn533_unregister_device(phy
->priv
);
247 static int pn533_i2c_remove(struct i2c_client
*client
)
249 struct pn533_i2c_phy
*phy
= i2c_get_clientdata(client
);
251 dev_dbg(&client
->dev
, "%s\n", __func__
);
253 free_irq(client
->irq
, phy
);
255 pn533_unregister_device(phy
->priv
);
260 static const struct of_device_id of_pn533_i2c_match
[] = {
261 { .compatible
= "nxp,pn533-i2c", },
262 { .compatible
= "nxp,pn532-i2c", },
265 MODULE_DEVICE_TABLE(of
, of_pn533_i2c_match
);
267 static const struct i2c_device_id pn533_i2c_id_table
[] = {
268 { PN533_I2C_DRIVER_NAME
, 0 },
271 MODULE_DEVICE_TABLE(i2c
, pn533_i2c_id_table
);
273 static struct i2c_driver pn533_i2c_driver
= {
275 .name
= PN533_I2C_DRIVER_NAME
,
276 .owner
= THIS_MODULE
,
277 .of_match_table
= of_match_ptr(of_pn533_i2c_match
),
279 .probe
= pn533_i2c_probe
,
280 .id_table
= pn533_i2c_id_table
,
281 .remove
= pn533_i2c_remove
,
284 module_i2c_driver(pn533_i2c_driver
);
286 MODULE_AUTHOR("Michael Thalmeier <michael.thalmeier@hale.at>");
287 MODULE_DESCRIPTION("PN533 I2C driver ver " VERSION
);
288 MODULE_VERSION(VERSION
);
289 MODULE_LICENSE("GPL");