Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * I2C link layer for the NXP NCI driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2014  NXP Semiconductors  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2012-2015  Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Derived from PN544 device driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Copyright (C) 2012  Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/nfc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <net/nfc/nfc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "nxp-nci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define NXP_NCI_I2C_DRIVER_NAME	"nxp-nci_i2c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define NXP_NCI_I2C_MAX_PAYLOAD	32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct nxp_nci_i2c_phy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct i2c_client *i2c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct nci_dev *ndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct gpio_desc *gpiod_en;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct gpio_desc *gpiod_fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int hard_fault; /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			 * < 0 if hardware error occurred (e.g. i2c err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 			 * and prevents normal operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static int nxp_nci_i2c_set_mode(void *phy_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 				    enum nxp_nci_mode mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	gpiod_set_value(phy->gpiod_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	gpiod_set_value(phy->gpiod_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	usleep_range(10000, 15000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (mode == NXP_NCI_MODE_COLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		phy->hard_fault = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct nxp_nci_i2c_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct i2c_client *client = phy->i2c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (phy->hard_fault != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return phy->hard_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	r = i2c_master_send(client, skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		/* Retry, chip was in standby */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		msleep(110);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		r = i2c_master_send(client, skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		nfc_err(&client->dev, "Error %d on I2C send\n", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	} else if (r != skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		nfc_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			"Invalid length sent: %u (expected %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			r, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		r = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		/* Success but return 0 and not number of bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static const struct nxp_nci_phy_ops i2c_phy_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	.set_mode = nxp_nci_i2c_set_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	.write = nxp_nci_i2c_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			       struct sk_buff **skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct i2c_client *client = phy->i2c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	u16 header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	size_t frame_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		goto fw_read_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	} else if (r != NXP_NCI_FW_HDR_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		nfc_err(&client->dev, "Incorrect header length: %u\n", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		r = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		goto fw_read_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	frame_len = (be16_to_cpu(header) & NXP_NCI_FW_FRAME_LEN_MASK) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		    NXP_NCI_FW_CRC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	*skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (*skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		goto fw_read_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (r != frame_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		nfc_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			"Invalid frame length: %u (expected %zu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			r, frame_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		r = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		goto fw_read_exit_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) fw_read_exit_free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	kfree_skb(*skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) fw_read_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 				struct sk_buff **skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct nci_ctrl_hdr header; /* May actually be a data header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct i2c_client *client = phy->i2c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		goto nci_read_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	} else if (r != NCI_CTRL_HDR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		nfc_err(&client->dev, "Incorrect header length: %u\n", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		r = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		goto nci_read_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	*skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (*skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		goto nci_read_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (r != header.plen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		nfc_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			"Invalid frame payload length: %u (expected %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			r, header.plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		r = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		goto nci_read_exit_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) nci_read_exit_free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	kfree_skb(*skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) nci_read_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct nxp_nci_i2c_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct nxp_nci_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct sk_buff *skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (!phy || !phy->ndev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		goto exit_irq_none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	client = phy->i2c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (!client || irq != client->irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		goto exit_irq_none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	info = nci_get_drvdata(phy->ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		goto exit_irq_none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	mutex_lock(&info->info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (phy->hard_fault != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto exit_irq_handled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	switch (info->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	case NXP_NCI_MODE_NCI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		r = nxp_nci_i2c_nci_read(phy, &skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	case NXP_NCI_MODE_FW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		r = nxp_nci_i2c_fw_read(phy, &skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	case NXP_NCI_MODE_COLD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		r = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (r == -EREMOTEIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		phy->hard_fault = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		if (info->mode == NXP_NCI_MODE_FW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			nxp_nci_fw_recv_frame(phy->ndev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		nfc_err(&client->dev, "Read failed with error %d\n", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		goto exit_irq_handled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	switch (info->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	case NXP_NCI_MODE_NCI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		nci_recv_frame(phy->ndev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	case NXP_NCI_MODE_FW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		nxp_nci_fw_recv_frame(phy->ndev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	case NXP_NCI_MODE_COLD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) exit_irq_handled:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	mutex_unlock(&info->info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) exit_irq_none:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static const struct acpi_gpio_params firmware_gpios = { 1, 0, false };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static const struct acpi_gpio_params enable_gpios = { 2, 0, false };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	{ "enable-gpios", &enable_gpios, 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	{ "firmware-gpios", &firmware_gpios, 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int nxp_nci_i2c_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			    const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct nxp_nci_i2c_phy *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			   GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (!phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	phy->i2c_dev = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	i2c_set_clientdata(client, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	r = devm_acpi_dev_add_driver_gpios(dev, acpi_nxp_nci_gpios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		dev_dbg(dev, "Unable to add GPIO mapping table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (IS_ERR(phy->gpiod_en)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		nfc_err(dev, "Failed to get EN gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return PTR_ERR(phy->gpiod_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	phy->gpiod_fw = devm_gpiod_get(dev, "firmware", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (IS_ERR(phy->gpiod_fw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		nfc_err(dev, "Failed to get FW gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		return PTR_ERR(phy->gpiod_fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			  NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	r = request_threaded_irq(client->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				 nxp_nci_i2c_irq_thread_fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 				 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				 NXP_NCI_I2C_DRIVER_NAME, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		nfc_err(&client->dev, "Unable to register IRQ handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int nxp_nci_i2c_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	nxp_nci_remove(phy->ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	free_irq(client->irq, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static const struct i2c_device_id nxp_nci_i2c_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	{"nxp-nci_i2c", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static const struct of_device_id of_nxp_nci_i2c_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	{ .compatible = "nxp,nxp-nci-i2c", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static const struct acpi_device_id acpi_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	{ "NXP1001" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	{ "NXP7471" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) MODULE_DEVICE_TABLE(acpi, acpi_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static struct i2c_driver nxp_nci_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		   .name = NXP_NCI_I2C_DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		   .acpi_match_table = ACPI_PTR(acpi_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		   .of_match_table = of_nxp_nci_i2c_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		  },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	.probe = nxp_nci_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	.id_table = nxp_nci_i2c_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	.remove = nxp_nci_i2c_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) module_i2c_driver(nxp_nci_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");