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 *dev; 785 struct device *spidevice; 786 struct nfc_digital_dev *nfcddev; 787 struct sk_buff *skb_resp; 788 struct st95hf_context *stcontext = 789 (struct st95hf_context *)st95hfcontext; 790 struct st95_digital_cmd_complete_arg *cb_arg; 791 792 spidevice = &stcontext->spicontext.spidev->dev; 793 794 /* 795 * check semaphore, if not down() already, then we don't 796 * know in which context the ISR is called and surely it 797 * will be a bug. Note that down() of the semaphore is done 798 * in the corresponding st95hf_in_send_cmd() and then 799 * only this ISR should be called. ISR will up() the 800 * semaphore before leaving. Hence when the ISR is called 801 * the correct behaviour is down_trylock() should always 802 * return 1 (indicating semaphore cant be taken and hence no 803 * change in semaphore count). 804 * If not, then we up() the semaphore and crash on 805 * a BUG() ! 806 */ 807 if (!down_trylock(&stcontext->exchange_lock)) { 808 up(&stcontext->exchange_lock); 809 WARN(1, "unknown context in ST95HF ISR"); 810 return IRQ_NONE; 811 } 812 813 cb_arg = &stcontext->complete_cb_arg; 814 skb_resp = cb_arg->skb_resp; 815 816 mutex_lock(&stcontext->rm_lock); 817 res_len = st95hf_spi_recv_response(&stcontext->spicontext, 818 skb_resp->data); 819 if (res_len < 0) { 820 dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len); 821 result = res_len; 822 goto end; 823 } 824 825 /* if stcontext->nfcdev_free is true, it means remove already ran */ 826 if (stcontext->nfcdev_free) { 827 result = -ENODEV; 828 goto end; 829 } 830 831 dev = &stcontext->nfcdev->dev; 832 nfcddev = stcontext->ddev; 833 if (skb_resp->data[2] == WTX_REQ_FROM_TAG) { 834 /* Request for new FWT from tag */ 835 result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]); 836 if (result) 837 goto end; 838 839 wtx = true; 840 mutex_unlock(&stcontext->rm_lock); 841 return IRQ_HANDLED; 842 } 843 844 result = st95hf_error_handling(stcontext, skb_resp, res_len); 845 if (result) 846 goto end; 847 848 result = st95hf_response_handler(stcontext, skb_resp, res_len); 849 if (result) 850 goto end; 851 852 /* 853 * If select protocol is done on wtx req. do select protocol 854 * again with default values 855 */ 856 if (wtx) { 857 wtx = false; 858 result = st95hf_handle_wtx(stcontext, false, 0); 859 if (result) 860 goto end; 861 } 862 863 /* call digital layer callback */ 864 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); 865 866 /* up the semaphore before returning */ 867 up(&stcontext->exchange_lock); 868 mutex_unlock(&stcontext->rm_lock); 869 870 return IRQ_HANDLED; 871 872 end: 873 kfree_skb(skb_resp); 874 wtx = false; 875 cb_arg->rats = false; 876 skb_resp = ERR_PTR(result); 877 /* call of callback with error */ 878 cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp); 879 /* up the semaphore before returning */ 880 up(&stcontext->exchange_lock); 881 mutex_unlock(&stcontext->rm_lock); 882 return IRQ_HANDLED; 883 } 884 885 /* NFC ops functions definition */ 886 static int st95hf_in_configure_hw(struct nfc_digital_dev *ddev, 887 int type, 888 int param) 889 { 890 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); 891 892 if (type == NFC_DIGITAL_CONFIG_RF_TECH) 893 return st95hf_select_protocol(stcontext, param); 894 895 if (type == NFC_DIGITAL_CONFIG_FRAMING) { 896 switch (param) { 897 case NFC_DIGITAL_FRAMING_NFCA_SHORT: 898 stcontext->sendrcv_trflag = TRFLAG_NFCA_SHORT_FRAME; 899 break; 900 case NFC_DIGITAL_FRAMING_NFCA_STANDARD: 901 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME; 902 break; 903 case NFC_DIGITAL_FRAMING_NFCA_T4T: 904 case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP: 905 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: 906 stcontext->sendrcv_trflag = TRFLAG_NFCA_STD_FRAME_CRC; 907 break; 908 case NFC_DIGITAL_FRAMING_NFCB: 909 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: 910 case NFC_DIGITAL_FRAMING_ISO15693_T5T: 911 break; 912 } 913 } 914 915 return 0; 916 } 917 918 static int rf_off(struct st95hf_context *stcontext) 919 { 920 int rc; 921 struct device *dev; 922 923 dev = &stcontext->nfcdev->dev; 924 925 rc = st95hf_send_recv_cmd(stcontext, CMD_FIELD_OFF, 0, NULL, true); 926 if (rc) 927 dev_err(dev, "protocol sel send field off, err = 0x%x\n", rc); 928 929 return rc; 930 } 931 932 static int st95hf_in_send_cmd(struct nfc_digital_dev *ddev, 933 struct sk_buff *skb, 934 u16 timeout, 935 nfc_digital_cmd_complete_t cb, 936 void *arg) 937 { 938 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); 939 int rc; 940 struct sk_buff *skb_resp; 941 int len_data_to_tag = 0; 942 943 skb_resp = nfc_alloc_recv_skb(MAX_RESPONSE_BUFFER_SIZE, GFP_KERNEL); 944 if (!skb_resp) { 945 rc = -ENOMEM; 946 goto error; 947 } 948 949 switch (stcontext->current_rf_tech) { 950 case NFC_DIGITAL_RF_TECH_106A: 951 len_data_to_tag = skb->len + 1; 952 *skb_put(skb, 1) = stcontext->sendrcv_trflag; 953 break; 954 case NFC_DIGITAL_RF_TECH_106B: 955 case NFC_DIGITAL_RF_TECH_ISO15693: 956 len_data_to_tag = skb->len; 957 break; 958 default: 959 rc = -EINVAL; 960 goto free_skb_resp; 961 } 962 963 skb_push(skb, 3); 964 skb->data[0] = ST95HF_COMMAND_SEND; 965 skb->data[1] = SEND_RECEIVE_CMD; 966 skb->data[2] = len_data_to_tag; 967 968 stcontext->complete_cb_arg.skb_resp = skb_resp; 969 stcontext->complete_cb_arg.cb_usrarg = arg; 970 stcontext->complete_cb_arg.complete_cb = cb; 971 972 if ((skb->data[3] == ISO14443A_RATS_REQ) && 973 ddev->curr_protocol == NFC_PROTO_ISO14443) 974 stcontext->complete_cb_arg.rats = true; 975 976 /* 977 * down the semaphore to indicate to remove func that an 978 * ISR is pending, note that it will not block here in any case. 979 * If found blocked, it is a BUG! 980 */ 981 rc = down_killable(&stcontext->exchange_lock); 982 if (rc) { 983 WARN(1, "Semaphore is not found up in st95hf_in_send_cmd\n"); 984 return rc; 985 } 986 987 rc = st95hf_spi_send(&stcontext->spicontext, skb->data, 988 skb->len, 989 ASYNC); 990 if (rc) { 991 dev_err(&stcontext->nfcdev->dev, 992 "Error %d trying to perform data_exchange", rc); 993 /* up the semaphore since ISR will never come in this case */ 994 up(&stcontext->exchange_lock); 995 goto free_skb_resp; 996 } 997 998 kfree_skb(skb); 999 1000 return rc; 1001 1002 free_skb_resp: 1003 kfree_skb(skb_resp); 1004 error: 1005 return rc; 1006 } 1007 1008 /* p2p will be supported in a later release ! */ 1009 static int st95hf_tg_configure_hw(struct nfc_digital_dev *ddev, 1010 int type, 1011 int param) 1012 { 1013 return 0; 1014 } 1015 1016 static int st95hf_tg_send_cmd(struct nfc_digital_dev *ddev, 1017 struct sk_buff *skb, 1018 u16 timeout, 1019 nfc_digital_cmd_complete_t cb, 1020 void *arg) 1021 { 1022 return 0; 1023 } 1024 1025 static int st95hf_tg_listen(struct nfc_digital_dev *ddev, 1026 u16 timeout, 1027 nfc_digital_cmd_complete_t cb, 1028 void *arg) 1029 { 1030 return 0; 1031 } 1032 1033 static int st95hf_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech) 1034 { 1035 return 0; 1036 } 1037 1038 static int st95hf_switch_rf(struct nfc_digital_dev *ddev, bool on) 1039 { 1040 u8 rf_tech; 1041 struct st95hf_context *stcontext = nfc_digital_get_drvdata(ddev); 1042 1043 rf_tech = ddev->curr_rf_tech; 1044 1045 if (on) 1046 /* switch on RF field */ 1047 return st95hf_select_protocol(stcontext, rf_tech); 1048 1049 /* switch OFF RF field */ 1050 return rf_off(stcontext); 1051 } 1052 1053 /* TODO st95hf_abort_cmd */ 1054 static void st95hf_abort_cmd(struct nfc_digital_dev *ddev) 1055 { 1056 } 1057 1058 static struct nfc_digital_ops st95hf_nfc_digital_ops = { 1059 .in_configure_hw = st95hf_in_configure_hw, 1060 .in_send_cmd = st95hf_in_send_cmd, 1061 1062 .tg_listen = st95hf_tg_listen, 1063 .tg_configure_hw = st95hf_tg_configure_hw, 1064 .tg_send_cmd = st95hf_tg_send_cmd, 1065 .tg_get_rf_tech = st95hf_tg_get_rf_tech, 1066 1067 .switch_rf = st95hf_switch_rf, 1068 .abort_cmd = st95hf_abort_cmd, 1069 }; 1070 1071 static const struct spi_device_id st95hf_id[] = { 1072 { "st95hf", 0 }, 1073 {} 1074 }; 1075 MODULE_DEVICE_TABLE(spi, st95hf_id); 1076 1077 static int st95hf_probe(struct spi_device *nfc_spi_dev) 1078 { 1079 int ret; 1080 1081 struct st95hf_context *st95context; 1082 struct st95hf_spi_context *spicontext; 1083 1084 nfc_info(&nfc_spi_dev->dev, "ST95HF driver probe called.\n"); 1085 1086 st95context = devm_kzalloc(&nfc_spi_dev->dev, 1087 sizeof(struct st95hf_context), 1088 GFP_KERNEL); 1089 if (!st95context) 1090 return -ENOMEM; 1091 1092 spicontext = &st95context->spicontext; 1093 1094 spicontext->spidev = nfc_spi_dev; 1095 1096 st95context->fwi = 1097 cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[2]; 1098 1099 if (device_property_present(&nfc_spi_dev->dev, "st95hfvin")) { 1100 st95context->st95hf_supply = 1101 devm_regulator_get(&nfc_spi_dev->dev, 1102 "st95hfvin"); 1103 if (IS_ERR(st95context->st95hf_supply)) { 1104 dev_err(&nfc_spi_dev->dev, "failed to acquire regulator\n"); 1105 return PTR_ERR(st95context->st95hf_supply); 1106 } 1107 1108 ret = regulator_enable(st95context->st95hf_supply); 1109 if (ret) { 1110 dev_err(&nfc_spi_dev->dev, "failed to enable regulator\n"); 1111 return ret; 1112 } 1113 } 1114 1115 init_completion(&spicontext->done); 1116 mutex_init(&spicontext->spi_lock); 1117 1118 /* 1119 * Store spicontext in spi device object for using it in 1120 * remove function 1121 */ 1122 dev_set_drvdata(&nfc_spi_dev->dev, spicontext); 1123 1124 st95context->enable_gpio = 1125 of_get_named_gpio(nfc_spi_dev->dev.of_node, 1126 "enable-gpio", 1127 0); 1128 if (!gpio_is_valid(st95context->enable_gpio)) { 1129 dev_err(&nfc_spi_dev->dev, "No valid enable gpio\n"); 1130 ret = st95context->enable_gpio; 1131 goto err_disable_regulator; 1132 } 1133 1134 ret = devm_gpio_request_one(&nfc_spi_dev->dev, st95context->enable_gpio, 1135 GPIOF_DIR_OUT | GPIOF_INIT_HIGH, 1136 "enable_gpio"); 1137 if (ret) 1138 goto err_disable_regulator; 1139 1140 if (nfc_spi_dev->irq > 0) { 1141 if (devm_request_threaded_irq(&nfc_spi_dev->dev, 1142 nfc_spi_dev->irq, 1143 st95hf_irq_handler, 1144 st95hf_irq_thread_handler, 1145 IRQF_TRIGGER_FALLING, 1146 "st95hf", 1147 (void *)st95context) < 0) { 1148 dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n"); 1149 ret = -EINVAL; 1150 goto err_disable_regulator; 1151 } 1152 } else { 1153 dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n"); 1154 ret = -EINVAL; 1155 goto err_disable_regulator; 1156 } 1157 1158 /* 1159 * First reset SPI to handle warm reset of the system. 1160 * It will put the ST95HF device in Power ON state 1161 * which make the state of device identical to state 1162 * at the time of cold reset of the system. 1163 */ 1164 ret = st95hf_send_spi_reset_sequence(st95context); 1165 if (ret) { 1166 dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n"); 1167 goto err_disable_regulator; 1168 } 1169 1170 /* call PowerOnReset sequence of ST95hf to activate it */ 1171 ret = st95hf_por_sequence(st95context); 1172 if (ret) { 1173 dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n"); 1174 goto err_disable_regulator; 1175 } 1176 1177 /* create NFC dev object and register with NFC Subsystem */ 1178 st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops, 1179 ST95HF_SUPPORTED_PROT, 1180 ST95HF_CAPABILITIES, 1181 ST95HF_HEADROOM_LEN, 1182 ST95HF_TAILROOM_LEN); 1183 if (!st95context->ddev) { 1184 ret = -ENOMEM; 1185 goto err_disable_regulator; 1186 } 1187 1188 st95context->nfcdev = st95context->ddev->nfc_dev; 1189 nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev); 1190 1191 ret = nfc_digital_register_device(st95context->ddev); 1192 if (ret) { 1193 dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n"); 1194 goto err_free_digital_device; 1195 } 1196 1197 /* store st95context in nfc device object */ 1198 nfc_digital_set_drvdata(st95context->ddev, st95context); 1199 1200 sema_init(&st95context->exchange_lock, 1); 1201 mutex_init(&st95context->rm_lock); 1202 1203 return ret; 1204 1205 err_free_digital_device: 1206 nfc_digital_free_device(st95context->ddev); 1207 err_disable_regulator: 1208 if (st95context->st95hf_supply) 1209 regulator_disable(st95context->st95hf_supply); 1210 1211 return ret; 1212 } 1213 1214 static int st95hf_remove(struct spi_device *nfc_spi_dev) 1215 { 1216 int result = 0; 1217 unsigned char reset_cmd = ST95HF_COMMAND_RESET; 1218 struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev); 1219 1220 struct st95hf_context *stcontext = container_of(spictx, 1221 struct st95hf_context, 1222 spicontext); 1223 1224 mutex_lock(&stcontext->rm_lock); 1225 1226 nfc_digital_unregister_device(stcontext->ddev); 1227 nfc_digital_free_device(stcontext->ddev); 1228 stcontext->nfcdev_free = true; 1229 1230 mutex_unlock(&stcontext->rm_lock); 1231 1232 /* if last in_send_cmd's ISR is pending, wait for it to finish */ 1233 result = down_killable(&stcontext->exchange_lock); 1234 if (result == -EINTR) 1235 dev_err(&spictx->spidev->dev, "sleep for semaphore interrupted by signal\n"); 1236 1237 /* next reset the ST95HF controller */ 1238 result = st95hf_spi_send(&stcontext->spicontext, 1239 &reset_cmd, 1240 ST95HF_RESET_CMD_LEN, 1241 ASYNC); 1242 if (result) { 1243 dev_err(&spictx->spidev->dev, 1244 "ST95HF reset failed in remove() err = %d\n", result); 1245 return result; 1246 } 1247 1248 /* wait for 3 ms to complete the controller reset process */ 1249 usleep_range(3000, 4000); 1250 1251 /* disable regulator */ 1252 if (stcontext->st95hf_supply) 1253 regulator_disable(stcontext->st95hf_supply); 1254 1255 return result; 1256 } 1257 1258 /* Register as SPI protocol driver */ 1259 static struct spi_driver st95hf_driver = { 1260 .driver = { 1261 .name = "st95hf", 1262 .owner = THIS_MODULE, 1263 }, 1264 .id_table = st95hf_id, 1265 .probe = st95hf_probe, 1266 .remove = st95hf_remove, 1267 }; 1268 1269 module_spi_driver(st95hf_driver); 1270 1271 MODULE_AUTHOR("Shikha Singh <shikha.singh@st.com>"); 1272 MODULE_DESCRIPTION("ST NFC Transceiver ST95HF driver"); 1273 MODULE_LICENSE("GPL v2"); 1274