Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / nfc / pn533 / i2c.c
blob4389eb4c8d0b3cbead89b907986a6a90b17cc1cf
1 /*
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>
32 #include "pn533.h"
34 #define VERSION "0.1"
36 #define PN533_I2C_DRIVER_NAME "pn533_i2c"
38 struct pn533_i2c_phy {
39 struct i2c_client *i2c_dev;
40 struct pn533 *priv;
42 bool aborted;
44 int hard_fault; /*
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 */
56 int rc;
58 rc = i2c_master_send(client, ack, 6);
60 return rc;
63 static int pn533_i2c_send_frame(struct pn533 *dev,
64 struct sk_buff *out)
66 struct pn533_i2c_phy *phy = dev->phy;
67 struct i2c_client *client = phy->i2c_dev;
68 int rc;
70 if (phy->hard_fault != 0)
71 return phy->hard_fault;
73 if (phy->priv == NULL)
74 phy->priv = dev;
76 phy->aborted = false;
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);
88 if (rc >= 0) {
89 if (rc != out->len)
90 rc = -EREMOTEIO;
91 else
92 rc = 0;
95 return rc;
98 static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags)
100 struct pn533_i2c_phy *phy = dev->phy;
102 phy->aborted = true;
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;
117 int r;
119 *skb = alloc_skb(len, GFP_KERNEL);
120 if (*skb == NULL)
121 return -ENOMEM;
123 r = i2c_master_recv(client, skb_put(*skb, len), len);
124 if (r != len) {
125 nfc_err(&client->dev, "cannot read. r=%d len=%d\n", r, len);
126 kfree_skb(*skb);
127 return -EREMOTEIO;
130 if (!((*skb)->data[0] & 0x01)) {
131 nfc_err(&client->dev, "READY flag not set");
132 kfree_skb(*skb);
133 return -EBUSY;
136 /* remove READY byte */
137 skb_pull(*skb, 1);
138 /* trim to frame size */
139 skb_trim(*skb, phy->priv->ops->rx_frame_size((*skb)->data));
141 return 0;
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;
149 int r;
151 if (!phy || irq != phy->i2c_dev->irq) {
152 WARN_ON_ONCE(1);
153 return IRQ_NONE;
156 client = phy->i2c_dev;
157 dev_dbg(&client->dev, "IRQ\n");
159 if (phy->hard_fault != 0)
160 return IRQ_HANDLED;
162 r = pn533_i2c_read(phy, &skb);
163 if (r == -EREMOTEIO) {
164 phy->hard_fault = r;
166 pn533_recv_frame(phy->priv, NULL, -EREMOTEIO);
168 return IRQ_HANDLED;
169 } else if ((r == -ENOMEM) || (r == -EBADMSG) || (r == -EBUSY)) {
170 return IRQ_HANDLED;
173 if (!phy->aborted)
174 pn533_recv_frame(phy->priv, skb, 0);
176 return IRQ_HANDLED;
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;
190 struct pn533 *priv;
191 int r = 0;
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");
198 return -ENODEV;
201 phy = devm_kzalloc(&client->dev, sizeof(struct pn533_i2c_phy),
202 GFP_KERNEL);
203 if (!phy)
204 return -ENOMEM;
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,
213 &phy->i2c_dev->dev,
214 &client->dev);
216 if (IS_ERR(priv)) {
217 r = PTR_ERR(priv);
218 return r;
221 phy->priv = priv;
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);
227 if (r < 0) {
228 nfc_err(&client->dev, "Unable to register IRQ handler\n");
229 goto irq_rqst_err;
232 r = pn533_finalize_setup(priv);
233 if (r)
234 goto fn_setup_err;
236 return 0;
238 fn_setup_err:
239 free_irq(client->irq, phy);
241 irq_rqst_err:
242 pn533_unregister_device(phy->priv);
244 return r;
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);
257 return 0;
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 = {
274 .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");