Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 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/miscdevice.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/nfc.h> |
| 28 | #include <linux/firmware.h> |
| 29 | #include <linux/unaligned/access_ok.h> |
| 30 | #include <linux/platform_data/st21nfca.h> |
| 31 | |
| 32 | #include <net/nfc/hci.h> |
| 33 | #include <net/nfc/llc.h> |
| 34 | #include <net/nfc/nfc.h> |
| 35 | |
| 36 | #include "st21nfca.h" |
| 37 | |
| 38 | /* |
| 39 | * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF. |
| 40 | * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism |
| 41 | * called byte stuffing has been introduced. |
| 42 | * |
| 43 | * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING |
| 44 | * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) |
| 45 | * - xor byte with ST21NFCA_BYTE_STUFFING_MASK |
| 46 | */ |
| 47 | #define ST21NFCA_SOF_EOF 0x7e |
| 48 | #define ST21NFCA_BYTE_STUFFING_MASK 0x20 |
| 49 | #define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d |
| 50 | |
| 51 | /* SOF + 00 fill size */ |
| 52 | #define ST21NFCA_FRAME_HEADROOM 2 |
| 53 | |
| 54 | /* 4 bytes crc (worst case byte stuffing) + EOF */ |
| 55 | #define ST21NFCA_FRAME_TAILROOM 5 |
| 56 | |
| 57 | #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c" |
| 58 | |
| 59 | static struct i2c_device_id st21nfca_hci_i2c_id_table[] = { |
| 60 | {ST21NFCA_HCI_DRIVER_NAME, 0}, |
| 61 | {} |
| 62 | }; |
| 63 | |
| 64 | MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table); |
| 65 | |
| 66 | struct st21nfca_i2c_phy { |
| 67 | struct i2c_client *i2c_dev; |
| 68 | struct nfc_hci_dev *hdev; |
| 69 | |
| 70 | unsigned int gpio_ena; |
| 71 | unsigned int gpio_irq; |
| 72 | unsigned int irq_polarity; |
| 73 | |
| 74 | struct sk_buff *pending_skb; |
| 75 | int current_read_len; |
| 76 | /* |
| 77 | * crc might have fail because i2c macro |
| 78 | * is disable due to other interface activity |
| 79 | */ |
| 80 | int crc_trials; |
| 81 | |
| 82 | int powered; |
| 83 | int run_mode; |
| 84 | |
| 85 | /* |
| 86 | * < 0 if hardware error occured (e.g. i2c err) |
| 87 | * and prevents normal operation. |
| 88 | */ |
| 89 | int hard_fault; |
| 90 | }; |
| 91 | static u8 len_seq[] = { 13, 24, 15, 29 }; |
| 92 | static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40}; |
| 93 | |
| 94 | #define I2C_DUMP_SKB(info, skb) \ |
| 95 | do { \ |
| 96 | pr_debug("%s:\n", info); \ |
| 97 | print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \ |
| 98 | 16, 1, (skb)->data, (skb)->len, 0); \ |
| 99 | } while (0) |
| 100 | |
| 101 | static void st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy) |
| 102 | { |
Christophe Ricard | c5b0c37 | 2014-04-01 00:34:03 +0200 | [diff] [blame^] | 103 | u16 wait_reboot[] = { 50, 300, 1000 }; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 104 | char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E }; |
| 105 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE]; |
| 106 | int i, r = -1; |
| 107 | |
Christophe Ricard | c5b0c37 | 2014-04-01 00:34:03 +0200 | [diff] [blame^] | 108 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 109 | r = i2c_master_recv(phy->i2c_dev, tmp, |
| 110 | ST21NFCA_HCI_LLC_MAX_SIZE); |
| 111 | |
| 112 | r = -1; |
Christophe Ricard | c5b0c37 | 2014-04-01 00:34:03 +0200 | [diff] [blame^] | 113 | for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 114 | r = i2c_master_send(phy->i2c_dev, reboot_cmd, |
| 115 | sizeof(reboot_cmd)); |
| 116 | usleep_range(1000, 1500); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | static int st21nfca_hci_i2c_enable(void *phy_id) |
| 121 | { |
| 122 | struct st21nfca_i2c_phy *phy = phy_id; |
| 123 | |
| 124 | gpio_set_value(phy->gpio_ena, 1); |
| 125 | phy->powered = 1; |
| 126 | phy->run_mode = ST21NFCA_HCI_MODE; |
| 127 | |
| 128 | usleep_range(10000, 15000); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static void st21nfca_hci_i2c_disable(void *phy_id) |
| 134 | { |
| 135 | struct st21nfca_i2c_phy *phy = phy_id; |
| 136 | |
| 137 | pr_info("\n"); |
| 138 | gpio_set_value(phy->gpio_ena, 0); |
| 139 | |
| 140 | phy->powered = 0; |
| 141 | } |
| 142 | |
| 143 | static int st21nfca_hci_add_len_crc(struct sk_buff *skb) |
| 144 | { |
| 145 | int ret = 2; |
| 146 | u16 crc; |
| 147 | u8 tmp; |
| 148 | |
| 149 | *skb_push(skb, 1) = 0; |
| 150 | |
| 151 | crc = crc_ccitt(0xffff, skb->data, skb->len); |
| 152 | crc = ~crc; |
| 153 | |
| 154 | tmp = crc & 0x00ff; |
| 155 | *skb_put(skb, 1) = tmp; |
| 156 | |
| 157 | tmp = (crc >> 8) & 0x00ff; |
| 158 | *skb_put(skb, 1) = tmp; |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static void st21nfca_hci_remove_len_crc(struct sk_buff *skb, int crc_len) |
| 164 | { |
| 165 | skb_pull(skb, ST21NFCA_FRAME_HEADROOM); |
| 166 | skb_trim(skb, crc_len); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Writing a frame must not return the number of written bytes. |
| 171 | * It must return either zero for success, or <0 for error. |
| 172 | * In addition, it must not alter the skb |
| 173 | */ |
| 174 | static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb) |
| 175 | { |
| 176 | int r = -1, i, j, len; |
| 177 | struct st21nfca_i2c_phy *phy = phy_id; |
| 178 | struct i2c_client *client = phy->i2c_dev; |
Christophe Ricard | 6895730 | 2014-03-25 06:51:47 +0100 | [diff] [blame] | 179 | u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2]; |
| 180 | |
| 181 | I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb); |
| 182 | |
| 183 | |
| 184 | if (phy->hard_fault != 0) |
| 185 | return phy->hard_fault; |
| 186 | |
| 187 | /* |
| 188 | * Compute CRC before byte stuffing computation on frame |
| 189 | * Note st21nfca_hci_add_len_crc is doing a byte stuffing |
| 190 | * on its own value |
| 191 | */ |
| 192 | len = st21nfca_hci_add_len_crc(skb); |
| 193 | |
| 194 | /* add ST21NFCA_SOF_EOF on tail */ |
| 195 | *skb_put(skb, 1) = ST21NFCA_SOF_EOF; |
| 196 | /* add ST21NFCA_SOF_EOF on head */ |
| 197 | *skb_push(skb, 1) = ST21NFCA_SOF_EOF; |
| 198 | |
| 199 | /* |
| 200 | * Compute byte stuffing |
| 201 | * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING |
| 202 | * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte) |
| 203 | * xor byte with ST21NFCA_BYTE_STUFFING_MASK |
| 204 | */ |
| 205 | tmp[0] = skb->data[0]; |
| 206 | for (i = 1, j = 1; i < skb->len - 1; i++, j++) { |
| 207 | if (skb->data[i] == ST21NFCA_SOF_EOF |
| 208 | || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) { |
| 209 | tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING; |
| 210 | j++; |
| 211 | tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK; |
| 212 | } else { |
| 213 | tmp[j] = skb->data[i]; |
| 214 | } |
| 215 | } |
| 216 | tmp[j] = skb->data[i]; |
| 217 | j++; |
| 218 | |
| 219 | /* |
| 220 | * Manage sleep mode |
| 221 | * Try 3 times to send data with delay between each |
| 222 | */ |
| 223 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) { |
| 224 | r = i2c_master_send(client, tmp, j); |
| 225 | if (r < 0) |
| 226 | msleep(wait_tab[i]); |
| 227 | } |
| 228 | |
| 229 | if (r >= 0) { |
| 230 | if (r != j) |
| 231 | r = -EREMOTEIO; |
| 232 | else |
| 233 | r = 0; |
| 234 | } |
| 235 | |
| 236 | st21nfca_hci_remove_len_crc(skb, len); |
| 237 | |
| 238 | return r; |
| 239 | } |
| 240 | |
| 241 | static int get_frame_size(u8 *buf, int buflen) |
| 242 | { |
| 243 | int len = 0; |
| 244 | if (buf[len + 1] == ST21NFCA_SOF_EOF) |
| 245 | return 0; |
| 246 | |
| 247 | for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++) |
| 248 | ; |
| 249 | |
| 250 | return len; |
| 251 | } |
| 252 | |
| 253 | static int check_crc(u8 *buf, int buflen) |
| 254 | { |
| 255 | u16 crc; |
| 256 | |
| 257 | crc = crc_ccitt(0xffff, buf, buflen - 2); |
| 258 | crc = ~crc; |
| 259 | |
| 260 | if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) { |
| 261 | pr_err(ST21NFCA_HCI_DRIVER_NAME |
| 262 | ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1], |
| 263 | buf[buflen - 2]); |
| 264 | |
| 265 | pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__); |
| 266 | print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE, |
| 267 | 16, 2, buf, buflen, false); |
| 268 | return -EPERM; |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Prepare received data for upper layer. |
| 275 | * Received data include byte stuffing, crc and sof/eof |
| 276 | * which is not usable by hci part. |
| 277 | * returns: |
| 278 | * frame size without sof/eof, header and byte stuffing |
| 279 | * -EBADMSG : frame was incorrect and discarded |
| 280 | */ |
| 281 | static int st21nfca_hci_i2c_repack(struct sk_buff *skb) |
| 282 | { |
| 283 | int i, j, r, size; |
| 284 | if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0)) |
| 285 | return -EBADMSG; |
| 286 | |
| 287 | size = get_frame_size(skb->data, skb->len); |
| 288 | if (size > 0) { |
| 289 | skb_trim(skb, size); |
| 290 | /* remove ST21NFCA byte stuffing for upper layer */ |
| 291 | for (i = 1, j = 0; i < skb->len; i++) { |
| 292 | if (skb->data[i] == |
| 293 | (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) { |
| 294 | skb->data[i] = |
| 295 | skb->data[i + |
| 296 | 1] | ST21NFCA_BYTE_STUFFING_MASK; |
| 297 | i++; |
| 298 | j++; |
| 299 | } |
| 300 | skb->data[i] = skb->data[i + j]; |
| 301 | } |
| 302 | /* remove byte stuffing useless byte */ |
| 303 | skb_trim(skb, i - j); |
| 304 | /* remove ST21NFCA_SOF_EOF from head */ |
| 305 | skb_pull(skb, 1); |
| 306 | |
| 307 | r = check_crc(skb->data, skb->len); |
| 308 | if (r != 0) { |
| 309 | i = 0; |
| 310 | return -EBADMSG; |
| 311 | } |
| 312 | |
| 313 | /* remove headbyte */ |
| 314 | skb_pull(skb, 1); |
| 315 | /* remove crc. Byte Stuffing is already removed here */ |
| 316 | skb_trim(skb, skb->len - 2); |
| 317 | return skb->len; |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees |
| 324 | * that i2c bus will be flushed and that next read will start on a new frame. |
| 325 | * returned skb contains only LLC header and payload. |
| 326 | * returns: |
| 327 | * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at |
| 328 | * end of read) |
| 329 | * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF |
| 330 | * at end of read) |
| 331 | * -EREMOTEIO : i2c read error (fatal) |
| 332 | * -EBADMSG : frame was incorrect and discarded |
| 333 | * (value returned from st21nfca_hci_i2c_repack) |
| 334 | * -EIO : if no ST21NFCA_SOF_EOF is found after reaching |
| 335 | * the read length end sequence |
| 336 | */ |
| 337 | static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy, |
| 338 | struct sk_buff *skb) |
| 339 | { |
| 340 | int r, i; |
| 341 | u8 len; |
| 342 | struct i2c_client *client = phy->i2c_dev; |
| 343 | |
| 344 | if (phy->current_read_len < ARRAY_SIZE(len_seq)) { |
| 345 | len = len_seq[phy->current_read_len]; |
| 346 | |
| 347 | /* |
| 348 | * Add retry mecanism |
| 349 | * Operation on I2C interface may fail in case of operation on |
| 350 | * RF or SWP interface |
| 351 | */ |
| 352 | r = 0; |
| 353 | for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) { |
| 354 | r = i2c_master_recv(client, skb_put(skb, len), len); |
| 355 | if (r < 0) |
| 356 | msleep(wait_tab[i]); |
| 357 | } |
| 358 | |
| 359 | if (r != len) { |
| 360 | phy->current_read_len = 0; |
| 361 | return -EREMOTEIO; |
| 362 | } |
| 363 | |
| 364 | if (memchr(skb->data + 2, ST21NFCA_SOF_EOF, |
| 365 | skb->len - 2) != NULL) { |
| 366 | phy->current_read_len = 0; |
| 367 | return st21nfca_hci_i2c_repack(skb); |
| 368 | } |
| 369 | phy->current_read_len++; |
| 370 | return -EAGAIN; |
| 371 | } |
| 372 | return -EIO; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Reads an shdlc frame from the chip. This is not as straightforward as it |
| 377 | * seems. The frame format is data-crc, and corruption can occur anywhere |
| 378 | * while transiting on i2c bus, such that we could read an invalid data. |
| 379 | * The tricky case is when we read a corrupted data or crc. We must detect |
| 380 | * this here in order to determine that data can be transmitted to the hci |
| 381 | * core. This is the reason why we check the crc here. |
| 382 | * The CLF will repeat a frame until we send a RR on that frame. |
| 383 | * |
| 384 | * On ST21NFCA, IRQ goes in idle when read starts. As no size information are |
| 385 | * available in the incoming data, other IRQ might come. Every IRQ will trigger |
| 386 | * a read sequence with different length and will fill the current frame. |
| 387 | * The reception is complete once we reach a ST21NFCA_SOF_EOF. |
| 388 | */ |
| 389 | static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id) |
| 390 | { |
| 391 | struct st21nfca_i2c_phy *phy = phy_id; |
| 392 | struct i2c_client *client; |
| 393 | |
| 394 | int r; |
| 395 | |
| 396 | if (!phy || irq != phy->i2c_dev->irq) { |
| 397 | WARN_ON_ONCE(1); |
| 398 | return IRQ_NONE; |
| 399 | } |
| 400 | |
| 401 | client = phy->i2c_dev; |
| 402 | dev_dbg(&client->dev, "IRQ\n"); |
| 403 | |
| 404 | if (phy->hard_fault != 0) |
| 405 | return IRQ_HANDLED; |
| 406 | |
| 407 | r = st21nfca_hci_i2c_read(phy, phy->pending_skb); |
| 408 | if (r == -EREMOTEIO) { |
| 409 | phy->hard_fault = r; |
| 410 | |
| 411 | nfc_hci_recv_frame(phy->hdev, NULL); |
| 412 | |
| 413 | return IRQ_HANDLED; |
| 414 | } else if (r == -EAGAIN || r == -EIO) { |
| 415 | return IRQ_HANDLED; |
| 416 | } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) { |
| 417 | /* |
| 418 | * With ST21NFCA, only one interface (I2C, RF or SWP) |
| 419 | * may be active at a time. |
| 420 | * Having incorrect crc is usually due to i2c macrocell |
| 421 | * deactivation in the middle of a transmission. |
| 422 | * It may generate corrupted data on i2c. |
| 423 | * We give sometime to get i2c back. |
| 424 | * The complete frame will be repeated. |
| 425 | */ |
| 426 | msleep(wait_tab[phy->crc_trials]); |
| 427 | phy->crc_trials++; |
| 428 | phy->current_read_len = 0; |
| 429 | } else if (r > 0) { |
| 430 | /* |
| 431 | * We succeeded to read data from the CLF and |
| 432 | * data is valid. |
| 433 | * Reset counter. |
| 434 | */ |
| 435 | nfc_hci_recv_frame(phy->hdev, phy->pending_skb); |
| 436 | phy->crc_trials = 0; |
| 437 | } |
| 438 | |
| 439 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); |
| 440 | if (phy->pending_skb == NULL) { |
| 441 | phy->hard_fault = -ENOMEM; |
| 442 | nfc_hci_recv_frame(phy->hdev, NULL); |
| 443 | } |
| 444 | |
| 445 | return IRQ_HANDLED; |
| 446 | } |
| 447 | |
| 448 | static struct nfc_phy_ops i2c_phy_ops = { |
| 449 | .write = st21nfca_hci_i2c_write, |
| 450 | .enable = st21nfca_hci_i2c_enable, |
| 451 | .disable = st21nfca_hci_i2c_disable, |
| 452 | }; |
| 453 | |
| 454 | static int st21nfca_request_resources(struct st21nfca_i2c_phy *phy, |
| 455 | struct i2c_client *client) |
| 456 | { |
| 457 | struct st21nfca_nfc_platform_data *pdata; |
| 458 | int r; |
| 459 | |
| 460 | pdata = client->dev.platform_data; |
| 461 | if (pdata == NULL) { |
| 462 | nfc_err(&client->dev, "No platform data\n"); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | /* store for later use */ |
| 467 | phy->gpio_irq = pdata->gpio_irq; |
| 468 | phy->gpio_ena = pdata->gpio_ena; |
| 469 | phy->irq_polarity = pdata->irq_polarity; |
| 470 | phy->i2c_dev = client; |
| 471 | |
| 472 | r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up"); |
| 473 | if (r) { |
| 474 | pr_err("%s : gpio_request failed\n", __FILE__); |
| 475 | return -ENODEV; |
| 476 | } |
| 477 | |
| 478 | r = gpio_direction_input(phy->gpio_irq); |
| 479 | if (r) { |
| 480 | pr_err("%s : gpio_direction_input failed\n", __FILE__); |
| 481 | return -ENODEV; |
| 482 | } |
| 483 | |
| 484 | if (phy->gpio_ena != 0) { |
| 485 | r = devm_gpio_request(&client->dev, |
| 486 | phy->gpio_ena, "clf_enable"); |
| 487 | if (r) { |
| 488 | pr_err("%s : ena gpio_request failed\n", __FILE__); |
| 489 | return -ENODEV; |
| 490 | } |
| 491 | r = gpio_direction_output(phy->gpio_ena, 1); |
| 492 | |
| 493 | if (r) { |
| 494 | pr_err("%s : ena gpio_direction_output failed\n", |
| 495 | __FILE__); |
| 496 | return -ENODEV; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL); |
| 501 | if (phy->pending_skb == NULL) |
| 502 | return -ENOMEM; |
| 503 | |
| 504 | phy->current_read_len = 0; |
| 505 | phy->crc_trials = 0; |
| 506 | return r; |
| 507 | } |
| 508 | |
| 509 | static int st21nfca_hci_i2c_probe(struct i2c_client *client, |
| 510 | const struct i2c_device_id *id) |
| 511 | { |
| 512 | struct st21nfca_i2c_phy *phy; |
| 513 | struct st21nfca_nfc_platform_data *pdata; |
| 514 | int r = 0; |
| 515 | |
| 516 | dev_dbg(&client->dev, "%s\n", __func__); |
| 517 | dev_dbg(&client->dev, "IRQ: %d\n", client->irq); |
| 518 | |
| 519 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 520 | nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); |
| 521 | return -ENODEV; |
| 522 | } |
| 523 | |
| 524 | phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy), |
| 525 | GFP_KERNEL); |
| 526 | if (!phy) { |
| 527 | nfc_err(&client->dev, |
| 528 | "Cannot allocate memory for st21nfca i2c phy.\n"); |
| 529 | return -ENOMEM; |
| 530 | } |
| 531 | |
| 532 | phy->i2c_dev = client; |
| 533 | |
| 534 | i2c_set_clientdata(client, phy); |
| 535 | |
| 536 | pdata = client->dev.platform_data; |
| 537 | if (pdata == NULL) { |
| 538 | nfc_err(&client->dev, "No platform data\n"); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
| 542 | r = st21nfca_request_resources(phy, client); |
| 543 | if (r) { |
| 544 | nfc_err(&client->dev, "Cannot get platform resources\n"); |
| 545 | return r; |
| 546 | } |
| 547 | |
| 548 | st21nfca_hci_platform_init(phy); |
| 549 | r = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 550 | st21nfca_hci_irq_thread_fn, |
| 551 | phy->irq_polarity | IRQF_ONESHOT, |
| 552 | ST21NFCA_HCI_DRIVER_NAME, phy); |
| 553 | if (r < 0) { |
| 554 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); |
| 555 | return r; |
| 556 | } |
| 557 | |
| 558 | r = st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, |
| 559 | ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM, |
| 560 | ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev); |
| 561 | |
| 562 | if (r < 0) |
| 563 | return r; |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int st21nfca_hci_i2c_remove(struct i2c_client *client) |
| 569 | { |
| 570 | struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client); |
| 571 | |
| 572 | dev_dbg(&client->dev, "%s\n", __func__); |
| 573 | |
| 574 | st21nfca_hci_remove(phy->hdev); |
| 575 | |
| 576 | if (phy->powered) |
| 577 | st21nfca_hci_i2c_disable(phy); |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static struct i2c_driver st21nfca_hci_i2c_driver = { |
| 583 | .driver = { |
| 584 | .name = ST21NFCA_HCI_I2C_DRIVER_NAME, |
| 585 | }, |
| 586 | .probe = st21nfca_hci_i2c_probe, |
| 587 | .id_table = st21nfca_hci_i2c_id_table, |
| 588 | .remove = st21nfca_hci_i2c_remove, |
| 589 | }; |
| 590 | |
| 591 | module_i2c_driver(st21nfca_hci_i2c_driver); |
| 592 | |
| 593 | MODULE_LICENSE("GPL"); |
| 594 | MODULE_DESCRIPTION(DRIVER_DESC); |