Linux Audio

Check our new training course

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