Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Driver for NXP PN533 NFC Chip - I2C transport layer |
| 4 | * |
| 5 | * Copyright (C) 2011 Instituto Nokia de Tecnologia |
| 6 | * Copyright (C) 2012-2013 Tieto Poland |
| 7 | * Copyright (C) 2016 HALE electronic |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/nfc.h> |
| 16 | #include <linux/netdevice.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <net/nfc/nfc.h> |
| 19 | #include "pn533.h" |
| 20 | |
| 21 | #define VERSION "0.1" |
| 22 | |
| 23 | #define PN533_I2C_DRIVER_NAME "pn533_i2c" |
| 24 | |
| 25 | struct pn533_i2c_phy { |
| 26 | struct i2c_client *i2c_dev; |
| 27 | struct pn533 *priv; |
| 28 | |
Michael Thalmeier | 30f9848 | 2016-04-21 16:43:51 +0200 | [diff] [blame] | 29 | bool aborted; |
| 30 | |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 31 | int hard_fault; /* |
| 32 | * < 0 if hardware error occurred (e.g. i2c err) |
| 33 | * and prevents normal operation. |
| 34 | */ |
| 35 | }; |
| 36 | |
| 37 | static int pn533_i2c_send_ack(struct pn533 *dev, gfp_t flags) |
| 38 | { |
| 39 | struct pn533_i2c_phy *phy = dev->phy; |
| 40 | struct i2c_client *client = phy->i2c_dev; |
Michał Mirosław | 8b55d75 | 2017-04-06 05:49:02 +0200 | [diff] [blame] | 41 | static const u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00}; |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 42 | /* spec 6.2.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */ |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 43 | |
wengjianfeng | a115d24 | 2021-04-12 10:20:06 +0800 | [diff] [blame] | 44 | return i2c_master_send(client, ack, 6); |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int pn533_i2c_send_frame(struct pn533 *dev, |
| 48 | struct sk_buff *out) |
| 49 | { |
| 50 | struct pn533_i2c_phy *phy = dev->phy; |
| 51 | struct i2c_client *client = phy->i2c_dev; |
| 52 | int rc; |
| 53 | |
| 54 | if (phy->hard_fault != 0) |
| 55 | return phy->hard_fault; |
| 56 | |
| 57 | if (phy->priv == NULL) |
| 58 | phy->priv = dev; |
| 59 | |
Michael Thalmeier | 30f9848 | 2016-04-21 16:43:51 +0200 | [diff] [blame] | 60 | phy->aborted = false; |
| 61 | |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 62 | print_hex_dump_debug("PN533_i2c TX: ", DUMP_PREFIX_NONE, 16, 1, |
| 63 | out->data, out->len, false); |
| 64 | |
| 65 | rc = i2c_master_send(client, out->data, out->len); |
| 66 | |
| 67 | if (rc == -EREMOTEIO) { /* Retry, chip was in power down */ |
| 68 | usleep_range(6000, 10000); |
| 69 | rc = i2c_master_send(client, out->data, out->len); |
| 70 | } |
| 71 | |
| 72 | if (rc >= 0) { |
| 73 | if (rc != out->len) |
| 74 | rc = -EREMOTEIO; |
| 75 | else |
| 76 | rc = 0; |
| 77 | } |
| 78 | |
| 79 | return rc; |
| 80 | } |
| 81 | |
| 82 | static void pn533_i2c_abort_cmd(struct pn533 *dev, gfp_t flags) |
| 83 | { |
Michael Thalmeier | 30f9848 | 2016-04-21 16:43:51 +0200 | [diff] [blame] | 84 | struct pn533_i2c_phy *phy = dev->phy; |
| 85 | |
| 86 | phy->aborted = true; |
| 87 | |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 88 | /* An ack will cancel the last issued command */ |
| 89 | pn533_i2c_send_ack(dev, flags); |
| 90 | |
| 91 | /* schedule cmd_complete_work to finish current command execution */ |
Michael Thalmeier | 30f9848 | 2016-04-21 16:43:51 +0200 | [diff] [blame] | 92 | pn533_recv_frame(phy->priv, NULL, -ENOENT); |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int pn533_i2c_read(struct pn533_i2c_phy *phy, struct sk_buff **skb) |
| 96 | { |
| 97 | struct i2c_client *client = phy->i2c_dev; |
| 98 | int len = PN533_EXT_FRAME_HEADER_LEN + |
| 99 | PN533_STD_FRAME_MAX_PAYLOAD_LEN + |
| 100 | PN533_STD_FRAME_TAIL_LEN + 1; |
| 101 | int r; |
| 102 | |
| 103 | *skb = alloc_skb(len, GFP_KERNEL); |
| 104 | if (*skb == NULL) |
| 105 | return -ENOMEM; |
| 106 | |
| 107 | r = i2c_master_recv(client, skb_put(*skb, len), len); |
| 108 | if (r != len) { |
| 109 | nfc_err(&client->dev, "cannot read. r=%d len=%d\n", r, len); |
| 110 | kfree_skb(*skb); |
| 111 | return -EREMOTEIO; |
| 112 | } |
| 113 | |
| 114 | if (!((*skb)->data[0] & 0x01)) { |
| 115 | nfc_err(&client->dev, "READY flag not set"); |
| 116 | kfree_skb(*skb); |
| 117 | return -EBUSY; |
| 118 | } |
| 119 | |
| 120 | /* remove READY byte */ |
| 121 | skb_pull(*skb, 1); |
| 122 | /* trim to frame size */ |
| 123 | skb_trim(*skb, phy->priv->ops->rx_frame_size((*skb)->data)); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static irqreturn_t pn533_i2c_irq_thread_fn(int irq, void *data) |
| 129 | { |
| 130 | struct pn533_i2c_phy *phy = data; |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 131 | struct sk_buff *skb = NULL; |
| 132 | int r; |
| 133 | |
| 134 | if (!phy || irq != phy->i2c_dev->irq) { |
| 135 | WARN_ON_ONCE(1); |
| 136 | return IRQ_NONE; |
| 137 | } |
| 138 | |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 139 | if (phy->hard_fault != 0) |
| 140 | return IRQ_HANDLED; |
| 141 | |
| 142 | r = pn533_i2c_read(phy, &skb); |
| 143 | if (r == -EREMOTEIO) { |
| 144 | phy->hard_fault = r; |
| 145 | |
| 146 | pn533_recv_frame(phy->priv, NULL, -EREMOTEIO); |
| 147 | |
| 148 | return IRQ_HANDLED; |
| 149 | } else if ((r == -ENOMEM) || (r == -EBADMSG) || (r == -EBUSY)) { |
| 150 | return IRQ_HANDLED; |
| 151 | } |
| 152 | |
Michael Thalmeier | 30f9848 | 2016-04-21 16:43:51 +0200 | [diff] [blame] | 153 | if (!phy->aborted) |
| 154 | pn533_recv_frame(phy->priv, skb, 0); |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 155 | |
| 156 | return IRQ_HANDLED; |
| 157 | } |
| 158 | |
Rikard Falkeborn | bc64281 | 2021-10-07 00:47:38 +0200 | [diff] [blame] | 159 | static const struct pn533_phy_ops i2c_phy_ops = { |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 160 | .send_frame = pn533_i2c_send_frame, |
| 161 | .send_ack = pn533_i2c_send_ack, |
| 162 | .abort_cmd = pn533_i2c_abort_cmd, |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | static int pn533_i2c_probe(struct i2c_client *client, |
| 167 | const struct i2c_device_id *id) |
| 168 | { |
| 169 | struct pn533_i2c_phy *phy; |
| 170 | struct pn533 *priv; |
| 171 | int r = 0; |
| 172 | |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 173 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 174 | nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); |
| 175 | return -ENODEV; |
| 176 | } |
| 177 | |
| 178 | phy = devm_kzalloc(&client->dev, sizeof(struct pn533_i2c_phy), |
| 179 | GFP_KERNEL); |
| 180 | if (!phy) |
| 181 | return -ENOMEM; |
| 182 | |
| 183 | phy->i2c_dev = client; |
| 184 | i2c_set_clientdata(client, phy); |
| 185 | |
Lars Poeschel | 843cc92 | 2019-10-29 15:46:46 +0100 | [diff] [blame] | 186 | priv = pn53x_common_init(PN533_DEVICE_PN532, |
| 187 | PN533_PROTO_REQ_ACK_RESP, |
| 188 | phy, &i2c_phy_ops, NULL, |
| 189 | &phy->i2c_dev->dev); |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 190 | |
Krzysztof Kozlowski | feab6ba | 2021-05-31 09:38:58 +0200 | [diff] [blame] | 191 | if (IS_ERR(priv)) |
wengjianfeng | a115d24 | 2021-04-12 10:20:06 +0800 | [diff] [blame] | 192 | return PTR_ERR(priv); |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 193 | |
| 194 | phy->priv = priv; |
Lars Poeschel | 843cc92 | 2019-10-29 15:46:46 +0100 | [diff] [blame] | 195 | r = pn532_i2c_nfc_alloc(priv, PN533_NO_TYPE_B_PROTOCOLS, &client->dev); |
| 196 | if (r) |
| 197 | goto nfc_alloc_err; |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 198 | |
Andrey Rusalin | 32ecc75 | 2016-12-28 20:10:59 +0300 | [diff] [blame] | 199 | r = request_threaded_irq(client->irq, NULL, pn533_i2c_irq_thread_fn, |
| 200 | IRQF_TRIGGER_FALLING | |
| 201 | IRQF_SHARED | IRQF_ONESHOT, |
| 202 | PN533_I2C_DRIVER_NAME, phy); |
| 203 | if (r < 0) { |
| 204 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); |
| 205 | goto irq_rqst_err; |
| 206 | } |
| 207 | |
| 208 | r = pn533_finalize_setup(priv); |
| 209 | if (r) |
| 210 | goto fn_setup_err; |
| 211 | |
Lars Poeschel | 843cc92 | 2019-10-29 15:46:46 +0100 | [diff] [blame] | 212 | r = nfc_register_device(priv->nfc_dev); |
| 213 | if (r) |
| 214 | goto fn_setup_err; |
| 215 | |
| 216 | return r; |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 217 | |
Andrey Rusalin | 32ecc75 | 2016-12-28 20:10:59 +0300 | [diff] [blame] | 218 | fn_setup_err: |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 219 | free_irq(client->irq, phy); |
| 220 | |
Andrey Rusalin | 32ecc75 | 2016-12-28 20:10:59 +0300 | [diff] [blame] | 221 | irq_rqst_err: |
Lars Poeschel | 843cc92 | 2019-10-29 15:46:46 +0100 | [diff] [blame] | 222 | nfc_free_device(priv->nfc_dev); |
| 223 | |
| 224 | nfc_alloc_err: |
| 225 | pn53x_common_clean(phy->priv); |
Andrey Rusalin | 32ecc75 | 2016-12-28 20:10:59 +0300 | [diff] [blame] | 226 | |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 227 | return r; |
| 228 | } |
| 229 | |
| 230 | static int pn533_i2c_remove(struct i2c_client *client) |
| 231 | { |
| 232 | struct pn533_i2c_phy *phy = i2c_get_clientdata(client); |
| 233 | |
Michael Thalmeier | 79f09fa | 2016-04-21 16:43:49 +0200 | [diff] [blame] | 234 | free_irq(client->irq, phy); |
| 235 | |
Lars Poeschel | 843cc92 | 2019-10-29 15:46:46 +0100 | [diff] [blame] | 236 | pn53x_unregister_nfc(phy->priv); |
| 237 | pn53x_common_clean(phy->priv); |
Andrey Rusalin | 068a496 | 2016-12-28 20:10:57 +0300 | [diff] [blame] | 238 | |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
Krzysztof Kozlowski | b3a790d | 2021-05-28 08:41:55 -0400 | [diff] [blame] | 242 | static const struct of_device_id of_pn533_i2c_match[] __maybe_unused = { |
Lars Poeschel | 3d5f3a6 | 2019-10-29 15:43:14 +0100 | [diff] [blame] | 243 | { .compatible = "nxp,pn532", }, |
| 244 | /* |
| 245 | * NOTE: The use of the compatibles with the trailing "...-i2c" is |
| 246 | * deprecated and will be removed. |
| 247 | */ |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 248 | { .compatible = "nxp,pn533-i2c", }, |
| 249 | { .compatible = "nxp,pn532-i2c", }, |
| 250 | {}, |
| 251 | }; |
| 252 | MODULE_DEVICE_TABLE(of, of_pn533_i2c_match); |
| 253 | |
Arvind Yadav | f98786d | 2017-08-21 22:33:56 +0530 | [diff] [blame] | 254 | static const struct i2c_device_id pn533_i2c_id_table[] = { |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 255 | { PN533_I2C_DRIVER_NAME, 0 }, |
| 256 | {} |
| 257 | }; |
| 258 | MODULE_DEVICE_TABLE(i2c, pn533_i2c_id_table); |
| 259 | |
| 260 | static struct i2c_driver pn533_i2c_driver = { |
| 261 | .driver = { |
| 262 | .name = PN533_I2C_DRIVER_NAME, |
Michael Thalmeier | dd7bedc | 2016-03-25 15:46:54 +0100 | [diff] [blame] | 263 | .of_match_table = of_match_ptr(of_pn533_i2c_match), |
| 264 | }, |
| 265 | .probe = pn533_i2c_probe, |
| 266 | .id_table = pn533_i2c_id_table, |
| 267 | .remove = pn533_i2c_remove, |
| 268 | }; |
| 269 | |
| 270 | module_i2c_driver(pn533_i2c_driver); |
| 271 | |
| 272 | MODULE_AUTHOR("Michael Thalmeier <michael.thalmeier@hale.at>"); |
| 273 | MODULE_DESCRIPTION("PN533 I2C driver ver " VERSION); |
| 274 | MODULE_VERSION(VERSION); |
| 275 | MODULE_LICENSE("GPL"); |