1 /* 2 * -------------------------------------------------------------------- 3 * Driver for ST NFC Transceiver ST95HF 4 * -------------------------------------------------------------------- 5 * Copyright (C) 2015 STMicroelectronics Pvt. Ltd. All rights reserved. 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms and conditions of the GNU General Public License, 9 * version 2, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include <linux/err.h> 21 #include <linux/gpio.h> 22 #include <linux/init.h> 23 #include <linux/interrupt.h> 24 #include <linux/irq.h> 25 #include <linux/module.h> 26 #include <linux/netdevice.h> 27 #include <linux/nfc.h> 28 #include <linux/of_gpio.h> 29 #include <linux/of.h> 30 #include <linux/of_irq.h> 31 #include <linux/property.h> 32 #include <linux/regulator/consumer.h> 33 #include <linux/wait.h> 34 #include <net/nfc/digital.h> 35 #include <net/nfc/nfc.h> 36 37 #include "spi.h" 38 39 /* supported protocols */ 40 #define ST95HF_SUPPORTED_PROT (NFC_PROTO_ISO14443_MASK | \ 41 NFC_PROTO_ISO14443_B_MASK | \ 42 NFC_PROTO_ISO15693_MASK) 43 /* driver capabilities */ 44 #define ST95HF_CAPABILITIES NFC_DIGITAL_DRV_CAPS_IN_CRC 45 46 /* Command Send Interface */ 47 /* ST95HF_COMMAND_SEND CMD Ids */ 48 #define ECHO_CMD 0x55 49 #define WRITE_REGISTER_CMD 0x9 50 #define PROTOCOL_SELECT_CMD 0x2 51 #define SEND_RECEIVE_CMD 0x4 52 53 /* Select protocol codes */ 54 #define ISO15693_PROTOCOL_CODE 0x1 55 #define ISO14443A_PROTOCOL_CODE 0x2 56 #define ISO14443B_PROTOCOL_CODE 0x3 57 58 /* 59 * head room len is 3 60 * 1 byte for control byte 61 * 1 byte for cmd 62 * 1 byte for size 63 */ 64 #define ST95HF_HEADROOM_LEN 3 65 66 /* 67 * tailroom is 1 for ISO14443A 68 * and 0 for ISO14443B/ISO15693, 69 * hence the max value 1 should be 70 * taken. 71 */ 72 #define ST95HF_TAILROOM_LEN 1 73 74 /* Command Response interface */ 75 #define MAX_RESPONSE_BUFFER_SIZE 280 76 #define ECHORESPONSE 0x55 77 #define ST95HF_ERR_MASK 0xF 78 #define ST95HF_TIMEOUT_ERROR 0x87 79 #define ST95HF_NFCA_CRC_ERR_MASK 0x20 80 #define ST95HF_NFCB_CRC_ERR_MASK 0x01 81 82 /* ST95HF transmission flag values */ 83 #define TRFLAG_NFCA_SHORT_FRAME 0x07 84 #define TRFLAG_NFCA_STD_FRAME 0x08 85 #define TRFLAG_NFCA_STD_FRAME_CRC 0x28 86 87 /* Misc defs */ 88 #define HIGH 1 89 #define LOW 0 90 #define ISO14443A_RATS_REQ 0xE0 91 #define RATS_TB1_PRESENT_MASK 0x20 92 #define RATS_TA1_PRESENT_MASK 0x10 93 #define TB1_FWI_MASK 0xF0 94 #define WTX_REQ_FROM_TAG 0xF2 95 96 #define MAX_CMD_LEN 0x7 97 98 #define MAX_CMD_PARAMS 4 99 struct cmd { 100 int cmd_len; 101 unsigned char cmd_id; 102 unsigned char no_cmd_params; 103 unsigned char cmd_params[MAX_CMD_PARAMS]; 104 enum req_type req; 105 }; 106 107 struct param_list { 108 int param_offset; 109 int new_param_val; 110 }; 111 112 /* 113 * List of top-level cmds to be used internally by the driver. 114 * All these commands are build on top of ST95HF basic commands 115 * such as SEND_RECEIVE_CMD, PROTOCOL_SELECT_CMD, etc. 116 * These top level cmds are used internally while implementing various ops of 117 * digital layer/driver probe or extending the digital framework layer for 118 * features that are not yet implemented there, for example, WTX cmd handling. 119 */ 120 enum st95hf_cmd_list { 121 CMD_ECHO, 122 CMD_ISO14443A_CONFIG, 123 CMD_ISO14443A_DEMOGAIN, 124 CMD_ISO14443B_DEMOGAIN, 125 CMD_ISO14443A_PROTOCOL_SELECT, 126 CMD_ISO14443B_PROTOCOL_SELECT, 127 CMD_WTX_RESPONSE, 128 CMD_FIELD_OFF, 129 CMD_ISO15693_PROTOCOL_SELECT, 130 }; 131 132 static const struct cmd cmd_array[] = { 133 [CMD_ECHO] = { 134 .cmd_len = 0x2, 135 .cmd_id = ECHO_CMD, 136 .no_cmd_params = 0, 137 .req = SYNC, 138 }, 139 [CMD_ISO14443A_CONFIG] = { 140 .cmd_len = 0x7, 141 .cmd_id = WRITE_REGISTER_CMD, 142 .no_cmd_params = 0x4, 143 .cmd_params = {0x3A, 0x00, 0x5A, 0x04}, 144 .req = SYNC, 145 }, 146 [CMD_ISO14443A_DEMOGAIN] = { 147 .cmd_len = 0x7, 148 .cmd_id = WRITE_REGISTER_CMD, 149 .no_cmd_params = 0x4, 150 .cmd_params = {0x68, 0x01, 0x01, 0xDF}, 151 .req = SYNC, 152 }, 153 [CMD_ISO14443B_DEMOGAIN] = { 154 .cmd_len = 0x7, 155 .cmd_id = WRITE_REGISTER_CMD, 156 .no_cmd_params = 0x4, 157 .cmd_params = {0x68, 0x01, 0x01, 0x51}, 158 .req = SYNC, 159 }, 160 [CMD_ISO14443A_PROTOCOL_SELECT] = { 161 .cmd_len = 0x7, 162 .cmd_id = PROTOCOL_SELECT_CMD, 163 .no_cmd_params = 0x4, 164 .cmd_params = {ISO14443A_PROTOCOL_CODE, 0x00, 0x01, 0xA0}, 165 .req = SYNC, 166 }, 167 [CMD_ISO14443B_PROTOCOL_SELECT] = { 168 .cmd_len = 0x7, 169 .cmd_id = PROTOCOL_SELECT_CMD, 170 .no_cmd_params = 0x4, 171 .cmd_params = {ISO14443B_PROTOCOL_CODE, 0x01, 0x03, 0xFF}, 172 .req = SYNC, 173 }, 174 [CMD_WTX_RESPONSE] = { 175 .cmd_len = 0x6, 176 .cmd_id = SEND_RECEIVE_CMD, 177 .no_cmd_params = 0x3, 178 .cmd_params = {0xF2, 0x00, TRFLAG_NFCA_STD_FRAME_CRC}, 179 .req = ASYNC, 180 }, 181 [CMD_FIELD_OFF] = { 182 .cmd_len = 0x5, 183 .cmd_id = PROTOCOL_SELECT_CMD, 184 .no_cmd_params = 0x2, 185 .cmd_params = {0x0, 0x0}, 186 .req = SYNC, 187 }, 188 [CMD_ISO15693_PROTOCOL_SELECT] = { 189 .cmd_len = 0x5, 190 .cmd_id = PROTOCOL_SELECT_CMD, 191 .no_cmd_params = 0x2, 192 .cmd_params = {ISO15693_PROTOCOL_CODE, 0x0D}, 193 .req = SYNC, 194 }, 195 }; 196 197 /* st95_digital_cmd_complete_arg stores client context */ 198 struct st95_digital_cmd_complete_arg { 199 struct sk_buff *skb_resp; 200 nfc_digital_cmd_complete_t complete_cb; 201 void *cb_usrarg; 202 bool rats; 203 }; 204 205 /* 206 * structure containing ST95HF driver specific data. 207 * @spicontext: structure containing information required 208 * for spi communication between st95hf and host. 209 * @ddev: nfc digital device object. 210 * @nfcdev: nfc device object. 211 * @enable_gpio: gpio used to enable st95hf transceiver. 212 * @complete_cb_arg: structure to store various context information 213 * that is passed from nfc requesting thread to the threaded ISR. 214 * @st95hf_supply: regulator "consumer" for NFC device. 215 * @sendrcv_trflag: last byte of frame send by sendrecv command 216 * of st95hf. This byte contains transmission flag info. 217 * @exchange_lock: semaphore used for signaling the st95hf_remove 218 * function that the last outstanding async nfc request is finished. 219 * @rm_lock: mutex for ensuring safe access of nfc digital object 220 * from threaded ISR. Usage of this mutex avoids any race between 221 * deletion of the object from st95hf_remove() and its access from 222 * the threaded ISR. 223 * @nfcdev_free: flag to have the state of nfc device object. 224 * [alive | died] 225 * @current_protocol: current nfc protocol. 226 * @current_rf_tech: current rf technology. 227 * @fwi: frame waiting index, received in reply of RATS according to 228 * digital protocol. 229 */ 230 struct st95hf_context { 231 struct st95hf_spi_context spicontext; 232 struct nfc_digital_dev *ddev; 233 struct nfc_dev *nfcdev; 234 unsigned int enable_gpio; 235 struct st95_digital_cmd_complete_arg complete_cb_arg; 236 struct regulator *st95hf_supply; 237 unsigned char sendrcv_trflag; 238 struct semaphore exchange_lock; 239 struct mutex rm_lock; 240 bool nfcdev_free; 241 u8 current_protocol; 242 u8 current_rf_tech; 243 int fwi; 244 }; 245 246 /* 247 * st95hf_send_recv_cmd() is for sending commands to ST95HF 248 * that are described in the cmd_array[]. It can optionally 249 * receive the response if the cmd request is of type 250 * SYNC. For that to happen caller must pass true to recv_res. 251 * For ASYNC request, recv_res is ignored and the 252 * function will never try to receive the response on behalf 253 * of the caller. 254 */ 255 static int st95hf_send_recv_cmd(struct st95hf_context *st95context, 256 enum st95hf_cmd_list cmd, 257 int no_modif, 258 struct param_list *list_array, 259 bool recv_res) 260 { 261 unsigned char spi_cmd_buffer[MAX_CMD_LEN]; 262 int i, ret; 263 struct device *dev = &st95context->spicontext.spidev->dev; 264 265 if (cmd_array[cmd].cmd_len > MAX_CMD_LEN) 266 return -EINVAL; 267 if (cmd_array[cmd].no_cmd_params < no_modif) 268 return -EINVAL; 269 if (no_modif && !list_array) 270 return -EINVAL; 271 272 spi_cmd_buffer[0] = ST95HF_COMMAND_SEND; 273 spi_cmd_buffer[1] = cmd_array[cmd].cmd_id; 274 spi_cmd_buffer[2] = cmd_array[cmd].no_cmd_params; 275 276 memcpy(&spi_cmd_buffer[3], cmd_array[cmd].cmd_params, 277 spi_cmd_buffer[2]); 278 279 for (i = 0; i < no_modif; i++) { 280 if (list_array[i].param_offset >= cmd_array[cmd].no_cmd_params) 281 return -EINVAL; 282 spi_cmd_buffer[3 + list_array[i].param_offset] = 283 list_array[i].new_param_val; 284 } 285 286 ret = st95hf_spi_send(&st95context->spicontext, 287 spi_cmd_buffer, 288 cmd_array[cmd].cmd_len, 289 cmd_array[cmd].req); 290 if (ret) { 291 dev_err(dev, "st95hf_spi_send failed with error %d\n", ret); 292 return ret; 293 } 294 295 if (cmd_array[cmd].req == SYNC && recv_res) { 296 unsigned char st95hf_response_arr[2]; 297 298 ret = st95hf_spi_recv_response(&st95context->spicontext, 299 st95hf_response_arr); 300 if (ret < 0) { 301 dev_err(dev, "spi error from st95hf_spi_recv_response(), err = 0x%x\n", 302 ret); 303 return ret; 304 } 305 306 if (st95hf_response_arr[0]) { 307 dev_err(dev, "st95hf error from st95hf_spi_recv_response(), err = 0x%x\n", 308 st95hf_response_arr[0]); 309 return -EIO; 310 } 311 } 312 313 return 0; 314 } 315 316 static int st95hf_echo_command(struct st95hf_context *st95context) 317 { 318 int result = 0; 319 unsigned char echo_response; 320 321 result = st95hf_send_recv_cmd(st95context, CMD_ECHO, 0, NULL, false); 322 if (result) 323 return result; 324 325 /* If control reached here, response can be taken */ 326 result = st95hf_spi_recv_echo_res(&st95context->spicontext, 327 &echo_response); 328 if (result) { 329 dev_err(&st95context->spicontext.spidev->dev, 330 "err: echo response receieve error = 0x%x\n", result); 331 return result; 332 } 333 334 if (echo_response == ECHORESPONSE) 335 return 0; 336 337 dev_err(&st95context->spicontext.spidev->dev, "err: echo res is 0x%x\n", 338 echo_response); 339 340 return -EIO; 341 } 342 343 static int secondary_configuration_type4a(struct st95hf_context *stcontext) 344 { 345 int result = 0; 346 struct device *dev = &stcontext->nfcdev->dev; 347 348 /* 14443A config setting after select protocol */ 349 result = st95hf_send_recv_cmd(stcontext, 350 CMD_ISO14443A_CONFIG, 351 0, 352 NULL, 353 true); 354 if (result) { 355 dev_err(dev, "type a config cmd, err = 0x%x\n", result); 356 return result; 357 } 358 359 /* 14443A demo gain setting */ 360 result = st95hf_send_recv_cmd(stcontext, 361 CMD_ISO14443A_DEMOGAIN, 362 0, 363 NULL, 364 true); 365 if (result) 366 dev_err(dev, "type a demogain cmd, err = 0x%x\n", result); 367 368 return result; 369 } 370 371 static int secondary_configuration_type4b(struct st95hf_context *stcontext) 372 { 373 int result = 0; 374 struct device *dev = &stcontext->nfcdev->dev; 375 376 result = st95hf_send_recv_cmd(stcontext, 377 CMD_ISO14443B_DEMOGAIN, 378 0, 379 NULL, 380 true); 381 if (result) 382 dev_err(dev, "type b demogain cmd, err = 0x%x\n", result); 383 384 return result; 385 } 386 387 static int st95hf_select_protocol(struct st95hf_context *stcontext, int type) 388 { 389 int result = 0; 390 struct device *dev; 391 392 dev = &stcontext->nfcdev->dev; 393 394 switch (type) { 395 case NFC_DIGITAL_RF_TECH_106A: 396 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106A; 397 result = st95hf_send_recv_cmd(stcontext, 398 CMD_ISO14443A_PROTOCOL_SELECT, 399 0, 400 NULL, 401 true); 402 if (result) { 403 dev_err(dev, "protocol sel, err = 0x%x\n", 404 result); 405 return result; 406 } 407 408 /* secondary config. for 14443Type 4A after protocol select */ 409 result = secondary_configuration_type4a(stcontext); 410 if (result) { 411 dev_err(dev, "type a secondary config, err = 0x%x\n", 412 result); 413 return result; 414 } 415 break; 416 case NFC_DIGITAL_RF_TECH_106B: 417 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_106B; 418 result = st95hf_send_recv_cmd(stcontext, 419 CMD_ISO14443B_PROTOCOL_SELECT, 420 0, 421 NULL, 422 true); 423 if (result) { 424 dev_err(dev, "protocol sel send, err = 0x%x\n", 425 result); 426 return result; 427 } 428 429 /* 430 * delay of 5-6 ms is required after select protocol 431 * command in case of ISO14443 Type B 432 */ 433 usleep_range(50000, 60000); 434 435 /* secondary config. for 14443Type 4B after protocol select */ 436 result = secondary_configuration_type4b(stcontext); 437 if (result) { 438 dev_err(dev, "type b secondary config, err = 0x%x\n", 439 result); 440 return result; 441 } 442 break; 443 case NFC_DIGITAL_RF_TECH_ISO15693: 444 stcontext->current_rf_tech = NFC_DIGITAL_RF_TECH_ISO15693; 445 result = st95hf_send_recv_cmd(stcontext, 446 CMD_ISO15693_PROTOCOL_SELECT, 447 0, 448 NULL, 449 true); 450 if (result) { 451 dev_err(dev, "protocol sel send, err = 0x%x\n", 452 result); 453 return result; 454 } 455 break; 456 default: 457 return -EINVAL; 458 } 459 460 return 0; 461 } 462 463 static void st95hf_send_st95enable_negativepulse(struct st95hf_context *st95con) 464 { 465 /* First make irq_in pin high */ 466 gpio_set_value(st95con->enable_gpio, HIGH); 467 468 /* wait for 1 milisecond */ 469 usleep_range(1000, 2000); 470 471 /* Make irq_in pin low */ 472 gpio_set_value(st95con->enable_gpio, LOW); 473 474 /* wait for minimum interrupt pulse to make st95 active */ 475 usleep_range(1000, 2000); 476 477 /* At end make it high */ 478 gpio_set_value(st95con->enable_gpio, HIGH); 479 } 480 481 /* 482 * Send a reset sequence over SPI bus (Reset command + wait 3ms + 483 * negative pulse on st95hf enable gpio 484 */ 485 static int st95hf_send_spi_reset_sequence(struct st95hf_context *st95context) 486 { 487 int result = 0; 488 unsigned char reset_cmd = ST95HF_COMMAND_RESET; 489 490 result = st95hf_spi_send(&st95context->spicontext, 491 &reset_cmd, 492 ST95HF_RESET_CMD_LEN, 493 ASYNC); 494 if (result) { 495 dev_err(&st95context->spicontext.spidev->dev, 496 "spi reset sequence cmd error = %d", result); 497 return result; 498 } 499 500 /* wait for 3 milisecond to complete the controller reset process */ 501 usleep_range(3000, 4000); 502 503 /* send negative pulse to make st95hf active */ 504 st95hf_send_st95enable_negativepulse(st95context); 505 506 /* wait for 10 milisecond : HFO setup time */ 507 usleep_range(10000, 20000); 508 509 return result; 510 } 511 512 static int st95hf_por_sequence(struct st95hf_context *st95context) 513 { 514 int nth_attempt = 1; 515 int result; 516 517 st95hf_send_st95enable_negativepulse(st95context); 518 519 usleep_range(5000, 6000); 520 do { 521 /* send an ECHO command and checks ST95HF response */ 522 result = st95hf_echo_command(st95context); 523 524 dev_dbg(&st95context->spicontext.spidev->dev, 525 "response from echo function = 0x%x, attempt = %d\n", 526 result, nth_attempt); 527 528 if (!result) 529 return 0; 530 531 /* send an pulse on IRQ in case of the chip is on sleep state */ 532 if (nth_attempt == 2) 533 st95hf_send_st95enable_negativepulse(st95context); 534 else 535 st95hf_send_spi_reset_sequence(st95context); 536 537 /* delay of 50 milisecond */ 538 usleep_range(50000, 51000); 539 } while (nth_attempt++ < 3); 540 541 return -ETIMEDOUT; 542 } 543 544 static int iso14443_config_fdt(struct st95hf_context *st95context, int wtxm) 545 { 546 int result = 0; 547 struct device *dev = &st95context->spicontext.spidev->dev; 548 struct nfc_digital_dev *nfcddev = st95context->ddev; 549 unsigned char pp_typeb; 550 struct param_list new_params[2]; 551 552 pp_typeb = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[2]; 553 554 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && 555 st95context->fwi < 4) 556 st95context->fwi = 4; 557 558 new_params[0].param_offset = 2; 559 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443) 560 new_params[0].new_param_val = st95context->fwi; 561 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B) 562 new_params[0].new_param_val = pp_typeb; 563 564 new_params[1].param_offset = 3; 565 new_params[1].new_param_val = wtxm; 566 567 switch (nfcddev->curr_protocol) { 568 case NFC_PROTO_ISO14443: 569 result = st95hf_send_recv_cmd(st95context, 570 CMD_ISO14443A_PROTOCOL_SELECT, 571 2, 572 new_params, 573 true); 574 if (result) { 575 dev_err(dev, "WTX type a sel proto, err = 0x%x\n", 576 result); 577 return result; 578 } 579 580 /* secondary config. for 14443Type 4A after protocol select */ 581 result = secondary_configuration_type4a(st95context); 582 if (result) { 583 dev_err(dev, "WTX type a second. config, err = 0x%x\n", 584 result); 585 return result; 586 } 587 break; 588 case NFC_PROTO_ISO14443_B: 589 result = st95hf_send_recv_cmd(st95context, 590 CMD_ISO14443B_PROTOCOL_SELECT, 591 2, 592 new_params, 593 true); 594 if (result) { 595 dev_err(dev, "WTX type b sel proto, err = 0x%x\n", 596 result); 597 return result; 598 } 599 600 /* secondary config. for 14443Type 4B after protocol select */ 601 result = secondary_configuration_type4b(st95context); 602 if (result) { 603 dev_err(dev, "WTX type b second. config, err = 0x%x\n", 604 result); 605 return result; 606 } 607 break; 608 default: 609 return -EINVAL; 610 } 611 612 return 0; 613 } 614 615 static int st95hf_handle_wtx(struct st95hf_context *stcontext, 616 bool new_wtx, 617 int wtx_val) 618 { 619 int result = 0; 620 unsigned char val_mm = 0; 621 struct param_list new_params[1]; 622 struct nfc_digital_dev *nfcddev = stcontext->ddev; 623 struct device *dev = &stcontext->nfcdev->dev; 624 625 if (new_wtx) { 626 result = iso14443_config_fdt(stcontext, wtx_val & 0x3f); 627 if (result) { 628 dev_err(dev, "Config. setting error on WTX req, err = 0x%x\n", 629 result); 630 return result; 631 } 632 633 /* Send response of wtx with ASYNC as no response expected */ 634 new_params[0].param_offset = 1; 635 new_params[0].new_param_val = wtx_val; 636 637 result = st95hf_send_recv_cmd(stcontext, 638 CMD_WTX_RESPONSE, 639 1, 640 new_params, 641 false); 642 if (result) 643 dev_err(dev, "WTX response send, err = 0x%x\n", result); 644 return result; 645 } 646 647 /* if no new wtx, cofigure with default values */ 648 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443) 649 val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3]; 650 else if (nfcddev->curr_protocol == NFC_PROTO_ISO14443_B) 651 val_mm = cmd_array[CMD_ISO14443B_PROTOCOL_SELECT].cmd_params[3]; 652 653 result = iso14443_config_fdt(stcontext, val_mm); 654 if (result) 655 dev_err(dev, "Default config. setting error after WTX processing, err = 0x%x\n", 656 result); 657 658 return result; 659 } 660 661 static int st95hf_error_handling(struct st95hf_context *stcontext, 662 struct sk_buff *skb_resp, 663 int res_len) 664 { 665 int result = 0; 666 unsigned char error_byte; 667 struct device *dev = &stcontext->nfcdev->dev; 668 669 /* First check ST95HF specific error */ 670 if (skb_resp->data[0] & ST95HF_ERR_MASK) { 671 if (skb_resp->data[0] == ST95HF_TIMEOUT_ERROR) 672 result = -ETIMEDOUT; 673 else 674 result = -EIO; 675 return result; 676 } 677 678 /* Check for CRC err only if CRC is present in the tag response */ 679 switch (stcontext->current_rf_tech) { 680 case NFC_DIGITAL_RF_TECH_106A: 681 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) { 682 error_byte = skb_resp->data[res_len - 3]; 683 if (error_byte & ST95HF_NFCA_CRC_ERR_MASK) { 684 /* CRC error occurred */ 685 dev_err(dev, "CRC error, byte received = 0x%x\n", 686 error_byte); 687 result = -EIO; 688 } 689 } 690 break; 691 case NFC_DIGITAL_RF_TECH_106B: 692 case NFC_DIGITAL_RF_TECH_ISO15693: 693 error_byte = skb_resp->data[res_len - 1]; 694 if (error_byte & ST95HF_NFCB_CRC_ERR_MASK) { 695 /* CRC error occurred */ 696 dev_err(dev, "CRC error, byte received = 0x%x\n", 697 error_byte); 698 result = -EIO; 699 } 700 break; 701 } 702 703 return result; 704 } 705 706 static int st95hf_response_handler(struct st95hf_context *stcontext, 707 struct sk_buff *skb_resp, 708 int res_len) 709 { 710 int result = 0; 711 int skb_len; 712 unsigned char val_mm; 713 struct nfc_digital_dev *nfcddev = stcontext->ddev; 714 struct device *dev = &stcontext->nfcdev->dev; 715 struct st95_digital_cmd_complete_arg *cb_arg; 716 717 cb_arg = &stcontext->complete_cb_arg; 718 719 /* Process the response */ 720 skb_put(skb_resp, res_len); 721 722 /* Remove st95 header */ 723 skb_pull(skb_resp, 2); 724 725 skb_len = skb_resp->len; 726 727 /* check if it is case of RATS request reply & FWI is present */ 728 if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && cb_arg->rats && 729 (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) { 730 if (skb_resp->data[1] & RATS_TA1_PRESENT_MASK) 731 stcontext->fwi = 732 (skb_resp->data[3] & TB1_FWI_MASK) >> 4; 733 else 734 stcontext->fwi = 735 (skb_resp->data[2] & TB1_FWI_MASK) >> 4; 736 737 val_mm = cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[3]; 738 739 result = iso14443_config_fdt(stcontext, val_mm); 740 if (result) { 741 dev_err(dev, "error in config_fdt to handle fwi of ATS, error=%d\n", 742 result); 743 return result; 744 } 745 } 746 cb_arg->rats = false; 747 748 /* Remove CRC bytes only if received frames data has an eod (CRC) */ 749 switch (stcontext->current_rf_tech) { 750 case NFC_DIGITAL_RF_TECH_106A: 751 if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) 752 skb_trim(skb_resp, (skb_len - 5)); 753 else 754 skb_trim(skb_resp, (skb_len - 3)); 755 break; 756 case NFC_DIGITAL_RF_TECH_106B: 757 case NFC_DIGITAL_RF_TECH_ISO15693: 758 skb_trim(skb_resp, (skb_len - 3)); 759 break; 760 } 761 762 return result; 763 } 764 765 static irqreturn_t st95hf_irq_handler(int irq, void *st95hfcontext) 766 { 767 struct st95hf_context *stcontext = 768 (struct st95hf_context *)st95hfcontext; 769 770 if (stcontext->spicontext.req_issync) { 771 complete(&stcontext->spicontext.done); 772 stcontext->spicontext.req_issync = false; 773 return IRQ_HANDLED; 774 } 775 776 return IRQ_WAKE_THREAD; 777 } 778 779 static irqreturn_t st95hf_irq_thread_handler(int irq, void *st95hfcontext) 780 { 781 int result = 0; 782 int res_len; 783 static bool wtx; 784 struct device *spidevice; 785 struct sk_buff *skb_resp; 786 struct st95hf_context *stcontext = 787 (struct st95hf_context *)st95hfcontext; 788 struct st95_digital_cmd_complete_arg *cb_arg; 789 790 spidevice = &stcontext->spicontext.spidev->dev; 791 792 /* 793 * check semaphore, if not down() already, then we don't 794 * know in which context the ISR is called and surely it 795 * will be a bug. Note that down() of the semaphore is done 796 * in the corresponding st95hf_in_send_cmd() and then 797 * only this ISR should be called. ISR will up() the 798 * semaphore before leaving. Hence when the ISR is called 799 * the correct behaviour is down_trylock() should always 800 * return 1 (indicating semaphore cant be taken and hence no 801 * change in semaphore count). 802 * If not, then we up() the semaphore and crash on 803 * a BUG() ! 804 */ 805 if (!down_trylock(&stcontext->exchange_lock)) { 806 up(&stcontext->exchange_lock); 807 WARN(1, "unknown context in ST95HF ISR"); 808 return IRQ_NONE; 809 } 810 811 cb_arg = &stcontext->complete_cb_arg; 812 skb_resp = cb_arg->skb_resp; 813 814 mutex_lock(&stcontext->rm_lock); 815 res_len = st95hf_spi_recv_response(&stcontext->spicontext, 816 skb_resp->data); 817 if (res_len < 0) { 818 dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len); 819 result = res_len; 820 goto end; 821 } 822 823 /* if stcontext->nfcdev_free is true, it means remove already ran */ 824 if (stcontext->nfcdev_free) { 825 result = -ENODEV; 826 goto end; 827 } 828 829 if (skb_resp->data[2] == WTX_REQ_FROM_TAG) { 830 /* Request for new FWT from tag */ 831 result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]); 832 if (result) 833 goto end; 834 835 wtx = true; 836 mutex_unlock(&stcontext->rm_lock); 837 return IRQ_HANDLED; 838 } 839 840 result = st95hf_error_handling(stcontext, skb_resp, res_len); 841 if (result) 842 goto end; 843 844 result = st95hf_response_handler(stcontext, skb_resp, res_len); 845 if (result) 846 goto end; 847 848 /* 849 * If select protocol is done on wtx req. do select protocol 850 * again with default values 851 */ 852 if (wtx) { 853 wtx = false; 854 result = st95hf_handle_wtx(stcontext, false, 0); 855 if (result) 856 goto end; 857 } 858 859 /* call digital layer callback */ 860 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); 861 862 /* up the semaphore before returning */ 863 up(&stcontext->exchange_lock); 864 mutex_unlock(&stcontext->rm_lock); 865 866 return IRQ_HANDLED; 867 868 end: 869 kfree_skb(skb_resp); 870 wtx = false; 871 cb_arg->rats = false; 872 skb_resp = ERR_PTR(result); 873 /* call of callback with error */ 874 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); 875 /* up the semaphore before returning */ 876 up(&stcontext->exchange_lock); 877 mutex_unlock(&stcontext->rm_lock); 878 return IRQ_HANDLED; 879 } 880 881 /* NFC ops functions definition */ 882 static int st95hf_in_configure_hw(struct nfc_digital_dev *ddev, 883 int type, 884 int param) 885 { 886 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); 887 888 if (type == NFC_DIGITAL_CONFIG_RF_TECH) 889 return st95hf_select_protocol(stcontext, param); 890 891 if (type == NFC_DIGITAL_CONFIG_FRAMING) { 892 switch (param) { 893 case NFC_DIGITAL_FRAMING_NFCA_SHORT: 894 stcontext->sendrcv_trflag = TRFLAG_NFCA_SHORT_FRAME; 895 break; 896 case NFC_DIGITAL_FRAMING_NFCA_STANDARD: 897 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME; 898 break; 899 case NFC_DIGITAL_FRAMING_NFCA_T4T: 900 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP: 901 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: 902 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME_CRC; 903 break; 904 case NFC_DIGITAL_FRAMING_NFCB: 905 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: 906 case NFC_DIGITAL_FRAMING_ISO15693_T5T: 907 break; 908 } 909 } 910 911 return 0; 912 } 913 914 static int rf_off(struct st95hf_context *stcontext) 915 { 916 int rc; 917 struct device *dev; 918 919 dev = &stcontext->nfcdev->dev; 920 921 rc = st95hf_send_recv_cmd(stcontext, CMD_FIELD_OFF, 0, NULL, true); 922 if (rc) 923 dev_err(dev, "protocol sel send field off, err = 0x%x\n", rc); 924 925 return rc; 926 } 927 928 static int st95hf_in_send_cmd(struct nfc_digital_dev *ddev, 929 struct sk_buff *skb, 930 u16 timeout, 931 nfc_digital_cmd_complete_t cb, 932 void *arg) 933 { 934 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); 935 int rc; 936 struct sk_buff *skb_resp; 937 int len_data_to_tag = 0; 938 939 skb_resp = nfc_alloc_recv_skb(MAX_RESPONSE_BUFFER_SIZE, GFP_KERNEL); 940 if (!skb_resp) { 941 rc = -ENOMEM; 942 goto error; 943 } 944 945 switch (stcontext->current_rf_tech) { 946 case NFC_DIGITAL_RF_TECH_106A: 947 len_data_to_tag = skb->len + 1; 948 skb_put_u8(skb, stcontext->sendrcv_trflag); 949 break; 950 case NFC_DIGITAL_RF_TECH_106B: 951 case NFC_DIGITAL_RF_TECH_ISO15693: 952 len_data_to_tag = skb->len; 953 break; 954 default: 955 rc = -EINVAL; 956 goto free_skb_resp; 957 } 958 959 skb_push(skb, 3); 960 skb->data[0] = ST95HF_COMMAND_SEND; 961 skb->data[1] = SEND_RECEIVE_CMD; 962 skb->data[2] = len_data_to_tag; 963 964 stcontext->complete_cb_arg.skb_resp = skb_resp; 965 stcontext->complete_cb_arg.cb_usrarg = arg; 966 stcontext->complete_cb_arg.complete_cb = cb; 967 968 if ((skb->data[3] == ISO14443A_RATS_REQ) && 969 ddev->curr_protocol == NFC_PROTO_ISO14443) 970 stcontext->complete_cb_arg.rats = true; 971 972 /* 973 * down the semaphore to indicate to remove func that an 974 * ISR is pending, note that it will not block here in any case. 975 * If found blocked, it is a BUG! 976 */ 977 rc = down_killable(&stcontext->exchange_lock); 978 if (rc) { 979 WARN(1, "Semaphore is not found up in st95hf_in_send_cmd\n"); 980 return rc; 981 } 982 983 rc = st95hf_spi_send(&stcontext->spicontext, skb->data, 984 skb->len, 985 ASYNC); 986 if (rc) { 987 dev_err(&stcontext->nfcdev->dev, 988 "Error %d trying to perform data_exchange", rc); 989 /* up the semaphore since ISR will never come in this case */ 990 up(&stcontext->exchange_lock); 991 goto free_skb_resp; 992 } 993 994 kfree_skb(skb); 995 996 return rc; 997 998 free_skb_resp: 999 kfree_skb(skb_resp); 1000 error: 1001 return rc; 1002 } 1003 1004 /* p2p will be supported in a later release ! */ 1005 static int st95hf_tg_configure_hw(struct nfc_digital_dev *ddev, 1006 int type, 1007 int param) 1008 { 1009 return 0; 1010 } 1011 1012 static int st95hf_tg_send_cmd(struct nfc_digital_dev *ddev, 1013 struct sk_buff *skb, 1014 u16 timeout, 1015 nfc_digital_cmd_complete_t cb, 1016 void *arg) 1017 { 1018 return 0; 1019 } 1020 1021 static int st95hf_tg_listen(struct nfc_digital_dev *ddev, 1022 u16 timeout, 1023 nfc_digital_cmd_complete_t cb, 1024 void *arg) 1025 { 1026 return 0; 1027 } 1028 1029 static int st95hf_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech) 1030 { 1031 return 0; 1032 } 1033 1034 static int st95hf_switch_rf(struct nfc_digital_dev *ddev, bool on) 1035 { 1036 u8 rf_tech; 1037 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); 1038 1039 rf_tech = ddev->curr_rf_tech; 1040 1041 if (on) 1042 /* switch on RF field */ 1043 return st95hf_select_protocol(stcontext, rf_tech); 1044 1045 /* switch OFF RF field */ 1046 return rf_off(stcontext); 1047 } 1048 1049 /* TODO st95hf_abort_cmd */ 1050 static void st95hf_abort_cmd(struct nfc_digital_dev *ddev) 1051 { 1052 } 1053 1054 static struct nfc_digital_ops st95hf_nfc_digital_ops = { 1055 .in_configure_hw = st95hf_in_configure_hw, 1056 .in_send_cmd = st95hf_in_send_cmd, 1057 1058 .tg_listen = st95hf_tg_listen, 1059 .tg_configure_hw = st95hf_tg_configure_hw, 1060 .tg_send_cmd = st95hf_tg_send_cmd, 1061 .tg_get_rf_tech = st95hf_tg_get_rf_tech, 1062 1063 .switch_rf = st95hf_switch_rf, 1064 .abort_cmd = st95hf_abort_cmd, 1065 }; 1066 1067 static const struct spi_device_id st95hf_id[] = { 1068 { "st95hf", 0 }, 1069 {} 1070 }; 1071 MODULE_DEVICE_TABLE(spi, st95hf_id); 1072 1073 static const struct of_device_id st95hf_spi_of_match[] = { 1074 { .compatible = "st,st95hf" }, 1075 { }, 1076 }; 1077 MODULE_DEVICE_TABLE(of, st95hf_spi_of_match); 1078 1079 static int st95hf_probe(struct spi_device *nfc_spi_dev) 1080 { 1081 int ret; 1082 1083 struct st95hf_context *st95context; 1084 struct st95hf_spi_context *spicontext; 1085 1086 nfc_info(&nfc_spi_dev->dev, "ST95HF driver probe called.\n"); 1087 1088 st95context = devm_kzalloc(&nfc_spi_dev->dev, 1089 sizeof(struct st95hf_context), 1090 GFP_KERNEL); 1091 if (!st95context) 1092 return -ENOMEM; 1093 1094 spicontext = &st95context->spicontext; 1095 1096 spicontext->spidev = nfc_spi_dev; 1097 1098 st95context->fwi = 1099 cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[2]; 1100 1101 if (device_property_present(&nfc_spi_dev->dev, "st95hfvin")) { 1102 st95context->st95hf_supply = 1103 devm_regulator_get(&nfc_spi_dev->dev, 1104 "st95hfvin"); 1105 if (IS_ERR(st95context->st95hf_supply)) { 1106 dev_err(&nfc_spi_dev->dev, "failed to acquire regulator\n"); 1107 return PTR_ERR(st95context->st95hf_supply); 1108 } 1109 1110 ret = regulator_enable(st95context->st95hf_supply); 1111 if (ret) { 1112 dev_err(&nfc_spi_dev->dev, "failed to enable regulator\n"); 1113 return ret; 1114 } 1115 } 1116 1117 init_completion(&spicontext->done); 1118 mutex_init(&spicontext->spi_lock); 1119 1120 /* 1121 * Store spicontext in spi device object for using it in 1122 * remove function 1123 */ 1124 dev_set_drvdata(&nfc_spi_dev->dev, spicontext); 1125 1126 st95context->enable_gpio = 1127 of_get_named_gpio(nfc_spi_dev->dev.of_node, 1128 "enable-gpio", 1129 0); 1130 if (!gpio_is_valid(st95context->enable_gpio)) { 1131 dev_err(&nfc_spi_dev->dev, "No valid enable gpio\n"); 1132 ret = st95context->enable_gpio; 1133 goto err_disable_regulator; 1134 } 1135 1136 ret = devm_gpio_request_one(&nfc_spi_dev->dev, st95context->enable_gpio, 1137 GPIOF_DIR_OUT | GPIOF_INIT_HIGH, 1138 "enable_gpio"); 1139 if (ret) 1140 goto err_disable_regulator; 1141 1142 if (nfc_spi_dev->irq > 0) { 1143 if (devm_request_threaded_irq(&nfc_spi_dev->dev, 1144 nfc_spi_dev->irq, 1145 st95hf_irq_handler, 1146 st95hf_irq_thread_handler, 1147 IRQF_TRIGGER_FALLING, 1148 "st95hf", 1149 (void *)st95context) < 0) { 1150 dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n"); 1151 ret = -EINVAL; 1152 goto err_disable_regulator; 1153 } 1154 } else { 1155 dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n"); 1156 ret = -EINVAL; 1157 goto err_disable_regulator; 1158 } 1159 1160 /* 1161 * First reset SPI to handle warm reset of the system. 1162 * It will put the ST95HF device in Power ON state 1163 * which make the state of device identical to state 1164 * at the time of cold reset of the system. 1165 */ 1166 ret = st95hf_send_spi_reset_sequence(st95context); 1167 if (ret) { 1168 dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n"); 1169 goto err_disable_regulator; 1170 } 1171 1172 /* call PowerOnReset sequence of ST95hf to activate it */ 1173 ret = st95hf_por_sequence(st95context); 1174 if (ret) { 1175 dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n"); 1176 goto err_disable_regulator; 1177 } 1178 1179 /* create NFC dev object and register with NFC Subsystem */ 1180 st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops, 1181 ST95HF_SUPPORTED_PROT, 1182 ST95HF_CAPABILITIES, 1183 ST95HF_HEADROOM_LEN, 1184 ST95HF_TAILROOM_LEN); 1185 if (!st95context->ddev) { 1186 ret = -ENOMEM; 1187 goto err_disable_regulator; 1188 } 1189 1190 st95context->nfcdev = st95context->ddev->nfc_dev; 1191 nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev); 1192 1193 ret = nfc_digital_register_device(st95context->ddev); 1194 if (ret) { 1195 dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n"); 1196 goto err_free_digital_device; 1197 } 1198 1199 /* store st95context in nfc device object */ 1200 nfc_digital_set_drvdata(st95context->ddev, st95context); 1201 1202 sema_init(&st95context->exchange_lock, 1); 1203 mutex_init(&st95context->rm_lock); 1204 1205 return ret; 1206 1207 err_free_digital_device: 1208 nfc_digital_free_device(st95context->ddev); 1209 err_disable_regulator: 1210 if (st95context->st95hf_supply) 1211 regulator_disable(st95context->st95hf_supply); 1212 1213 return ret; 1214 } 1215 1216 static int st95hf_remove(struct spi_device *nfc_spi_dev) 1217 { 1218 int result = 0; 1219 unsigned char reset_cmd = ST95HF_COMMAND_RESET; 1220 struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev); 1221 1222 struct st95hf_context *stcontext = container_of(spictx, 1223 struct st95hf_context, 1224 spicontext); 1225 1226 mutex_lock(&stcontext->rm_lock); 1227 1228 nfc_digital_unregister_device(stcontext->ddev); 1229 nfc_digital_free_device(stcontext->ddev); 1230 stcontext->nfcdev_free = true; 1231 1232 mutex_unlock(&stcontext->rm_lock); 1233 1234 /* if last in_send_cmd's ISR is pending, wait for it to finish */ 1235 result = down_killable(&stcontext->exchange_lock); 1236 if (result == -EINTR) 1237 dev_err(&spictx->spidev->dev, "sleep for semaphore interrupted by signal\n"); 1238 1239 /* next reset the ST95HF controller */ 1240 result = st95hf_spi_send(&stcontext->spicontext, 1241 &reset_cmd, 1242 ST95HF_RESET_CMD_LEN, 1243 ASYNC); 1244 if (result) { 1245 dev_err(&spictx->spidev->dev, 1246 "ST95HF reset failed in remove() err = %d\n", result); 1247 return result; 1248 } 1249 1250 /* wait for 3 ms to complete the controller reset process */ 1251 usleep_range(3000, 4000); 1252 1253 /* disable regulator */ 1254 if (stcontext->st95hf_supply) 1255 regulator_disable(stcontext->st95hf_supply); 1256 1257 return result; 1258 } 1259 1260 /* Register as SPI protocol driver */ 1261 static struct spi_driver st95hf_driver = { 1262 .driver = { 1263 .name = "st95hf", 1264 .owner = THIS_MODULE, 1265 .of_match_table = of_match_ptr(st95hf_spi_of_match), 1266 }, 1267 .id_table = st95hf_id, 1268 .probe = st95hf_probe, 1269 .remove = st95hf_remove, 1270 }; 1271 1272 module_spi_driver(st95hf_driver); 1273 1274 MODULE_AUTHOR("Shikha Singh <shikha.singh@st.com>"); 1275 MODULE_DESCRIPTION("ST NFC Transceiver ST95HF driver"); 1276 MODULE_LICENSE("GPL v2"); 1277