blob: 0141f19ac5a728e575fe6534a855ba3aadab7f70 [file] [log] [blame]
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01001/*
2 * Driver for NXP PN533 NFC Chip - I2C transport layer
3 *
4 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Copyright (C) 2012-2013 Tieto Poland
6 * Copyright (C) 2016 HALE electronic
7 *
8 *
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.
13 *
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.
18 *
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/>.
21 */
22
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>
32#include "pn533.h"
33
34#define VERSION "0.1"
35
36#define PN533_I2C_DRIVER_NAME "pn533_i2c"
37
38struct pn533_i2c_phy {
39 struct i2c_client *i2c_dev;
40 struct pn533 *priv;
41
42 int hard_fault; /*
43 * < 0 if hardware error occurred (e.g. i2c err)
44 * and prevents normal operation.
45 */
46};
47
48static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags)
49{
50 struct pn533_i2c_phy *phy = dev->phy;
51 struct i2c_client *client = phy->i2c_dev;
52 u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
53 /* spec 6.2.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
54 int rc;
55
56 rc = i2c_master_send(client, ack, 6);
57
58 return rc;
59}
60
61static int pn533_i2c_send_frame(struct pn533 *dev,
62 struct sk_buff *out)
63{
64 struct pn533_i2c_phy *phy = dev->phy;
65 struct i2c_client *client = phy->i2c_dev;
66 int rc;
67
68 if (phy->hard_fault != 0)
69 return phy->hard_fault;
70
71 if (phy->priv == NULL)
72 phy->priv = dev;
73
74 print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1,
75 out->data, out->len, false);
76
77 rc = i2c_master_send(client, out->data, out->len);
78
79 if (rc == -EREMOTEIO) { /* Retry, chip was in power down */
80 usleep_range(6000, 10000);
81 rc = i2c_master_send(client, out->data, out->len);
82 }
83
84 if (rc >= 0) {
85 if (rc != out->len)
86 rc = -EREMOTEIO;
87 else
88 rc = 0;
89 }
90
91 return rc;
92}
93
94static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
95{
96 /* An ack will cancel the last issued command */
97 pn533_i2c_send_ack(dev, flags);
98
99 /* schedule cmd_complete_work to finish current command execution */
100 if (dev->cmd != NULL)
101 dev->cmd->status = -ENOENT;
102 queue_work(dev->wq, &dev->cmd_complete_work);
103}
104
105static int pn533_i2c_read(struct pn533_i2c_phy *phy, struct sk_buff **skb)
106{
107 struct i2c_client *client = phy->i2c_dev;
108 int len = PN533_EXT_FRAME_HEADER_LEN +
109 PN533_STD_FRAME_MAX_PAYLOAD_LEN +
110 PN533_STD_FRAME_TAIL_LEN + 1;
111 int r;
112
113 *skb = alloc_skb(len, GFP_KERNEL);
114 if (*skb == NULL)
115 return -ENOMEM;
116
117 r = i2c_master_recv(client, skb_put(*skb, len), len);
118 if (r != len) {
119 nfc_err(&client->dev, "cannot read. r=%d len=%d\n", r, len);
120 kfree_skb(*skb);
121 return -EREMOTEIO;
122 }
123
124 if (!((*skb)->data[0] & 0x01)) {
125 nfc_err(&client->dev, "READY flag not set");
126 kfree_skb(*skb);
127 return -EBUSY;
128 }
129
130 /* remove READY byte */
131 skb_pull(*skb, 1);
132 /* trim to frame size */
133 skb_trim(*skb, phy->priv->ops->rx_frame_size((*skb)->data));
134
135 return 0;
136}
137
138static irqreturn_t pn533_i2c_irq_thread_fn(int irq, void *data)
139{
140 struct pn533_i2c_phy *phy = data;
141 struct i2c_client *client;
142 struct sk_buff *skb = NULL;
143 int r;
144
145 if (!phy || irq != phy->i2c_dev->irq) {
146 WARN_ON_ONCE(1);
147 return IRQ_NONE;
148 }
149
150 client = phy->i2c_dev;
151 dev_dbg(&client->dev, "IRQ\n");
152
153 if (phy->hard_fault != 0)
154 return IRQ_HANDLED;
155
156 r = pn533_i2c_read(phy, &skb);
157 if (r == -EREMOTEIO) {
158 phy->hard_fault = r;
159
160 pn533_recv_frame(phy->priv, NULL, -EREMOTEIO);
161
162 return IRQ_HANDLED;
163 } else if ((r == -ENOMEM) || (r == -EBADMSG) || (r == -EBUSY)) {
164 return IRQ_HANDLED;
165 }
166
167 pn533_recv_frame(phy->priv, skb, 0);
168
169 return IRQ_HANDLED;
170}
171
172static struct pn533_phy_ops i2c_phy_ops = {
173 .send_frame = pn533_i2c_send_frame,
174 .send_ack = pn533_i2c_send_ack,
175 .abort_cmd = pn533_i2c_abort_cmd,
176};
177
178
179static int pn533_i2c_probe(struct i2c_client *client,
180 const struct i2c_device_id *id)
181{
182 struct pn533_i2c_phy *phy;
183 struct pn533 *priv;
184 int r = 0;
185
186 dev_dbg(&client->dev, "%s\n", __func__);
187 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
188
189 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
190 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
191 return -ENODEV;
192 }
193
194 phy = devm_kzalloc(&client->dev, sizeof(struct pn533_i2c_phy),
195 GFP_KERNEL);
196 if (!phy)
197 return -ENOMEM;
198
199 phy->i2c_dev = client;
200 i2c_set_clientdata(client, phy);
201
202 r = request_threaded_irq(client->irq, NULL, pn533_i2c_irq_thread_fn,
203 IRQF_TRIGGER_FALLING |
204 IRQF_SHARED | IRQF_ONESHOT,
205 PN533_I2C_DRIVER_NAME, phy);
206
207 if (r < 0)
208 nfc_err(&client->dev, "Unable to register IRQ handler\n");
209
210 priv = pn533_register_device(PN533_DEVICE_PN532,
211 PN533_NO_TYPE_B_PROTOCOLS,
212 PN533_PROTO_REQ_ACK_RESP,
213 phy, &i2c_phy_ops, NULL,
Michael Thalmeierb16931b2016-04-21 16:43:50 +0200214 &phy->i2c_dev->dev,
215 &client->dev);
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +0100216
217 if (IS_ERR(priv)) {
218 r = PTR_ERR(priv);
219 goto err_register;
220 }
221
222 phy->priv = priv;
223
224 return 0;
225
226err_register:
227 free_irq(client->irq, phy);
228
229 return r;
230}
231
232static int pn533_i2c_remove(struct i2c_client *client)
233{
234 struct pn533_i2c_phy *phy = i2c_get_clientdata(client);
235
236 dev_dbg(&client->dev, "%s\n", __func__);
237
238 pn533_unregister_device(phy->priv);
239
Michael Thalmeier79f09fa2016-04-21 16:43:49 +0200240 free_irq(client->irq, phy);
241
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +0100242 return 0;
243}
244
245static const struct of_device_id of_pn533_i2c_match[] = {
246 { .compatible = "nxp,pn533-i2c", },
247 { .compatible = "nxp,pn532-i2c", },
248 {},
249};
250MODULE_DEVICE_TABLE(of, of_pn533_i2c_match);
251
252static struct i2c_device_id pn533_i2c_id_table[] = {
253 { PN533_I2C_DRIVER_NAME, 0 },
254 {}
255};
256MODULE_DEVICE_TABLE(i2c, pn533_i2c_id_table);
257
258static struct i2c_driver pn533_i2c_driver = {
259 .driver = {
260 .name = PN533_I2C_DRIVER_NAME,
261 .owner = THIS_MODULE,
262 .of_match_table = of_match_ptr(of_pn533_i2c_match),
263 },
264 .probe = pn533_i2c_probe,
265 .id_table = pn533_i2c_id_table,
266 .remove = pn533_i2c_remove,
267};
268
269module_i2c_driver(pn533_i2c_driver);
270
271MODULE_AUTHOR("Michael Thalmeier <michael.thalmeier@hale.at>");
272MODULE_DESCRIPTION("PN533 I2C driver ver " VERSION);
273MODULE_VERSION(VERSION);
274MODULE_LICENSE("GPL");