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)  * SPI Link Layer for ST NCI based Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/nfc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <net/nfc/nci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "st-nci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define DRIVER_DESC "NCI NFC driver for ST_NCI"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /* ndlc header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define ST_NCI_FRAME_HEADROOM	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define ST_NCI_FRAME_TAILROOM	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define ST_NCI_SPI_MIN_SIZE 4   /* PCB(1) + NCI Packet header(3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define ST_NCI_SPI_MAX_SIZE 250 /* req 4.2.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define ST_NCI_DRIVER_NAME "st_nci"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define ST_NCI_SPI_DRIVER_NAME "st_nci_spi"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct st_nci_spi_phy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct spi_device *spi_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct llt_ndlc *ndlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	bool irq_active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct gpio_desc *gpiod_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct st_nci_se_status se_status;
^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) static int st_nci_spi_enable(void *phy_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct st_nci_spi_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	gpiod_set_value(phy->gpiod_reset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	usleep_range(10000, 15000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	gpiod_set_value(phy->gpiod_reset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	usleep_range(80000, 85000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		enable_irq(phy->spi_dev->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		phy->irq_active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static void st_nci_spi_disable(void *phy_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct st_nci_spi_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	disable_irq_nosync(phy->spi_dev->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	phy->irq_active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * Writing a frame must not return the number of written bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * It must return either zero for success, or <0 for error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * In addition, it must not alter the skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static int st_nci_spi_write(void *phy_id, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct st_nci_spi_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct spi_device *dev = phy->spi_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct sk_buff *skb_rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u8 buf[ST_NCI_SPI_MAX_SIZE + NCI_DATA_HDR_SIZE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	       ST_NCI_FRAME_HEADROOM + ST_NCI_FRAME_TAILROOM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct spi_transfer spi_xfer = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		.tx_buf = skb->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		.rx_buf = buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		.len = skb->len,
^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) 	if (phy->ndlc->hard_fault != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return phy->ndlc->hard_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	r = spi_sync_transfer(dev, &spi_xfer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * We may have received some valuable data on miso line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * Send them back in the ndlc state machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		skb_rx = alloc_skb(skb->len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (!skb_rx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		skb_put(skb_rx, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		memcpy(skb_rx->data, buf, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		ndlc_recv(phy->ndlc, skb_rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^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)  * Reads an ndlc frame and returns it in a newly allocated sk_buff.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * 0 : if received frame is complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * -EREMOTEIO : i2c read error (fatal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * -EBADMSG : frame was incorrect and discarded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * -ENOMEM : cannot allocate skb, frame dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int st_nci_spi_read(struct st_nci_spi_phy *phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			struct sk_buff **skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	u8 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	u8 buf[ST_NCI_SPI_MAX_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct spi_device *dev = phy->spi_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct spi_transfer spi_xfer = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		.rx_buf = buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		.len = ST_NCI_SPI_MIN_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	r = spi_sync_transfer(dev, &spi_xfer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	len = be16_to_cpu(*(__be16 *) (buf + 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (len > ST_NCI_SPI_MAX_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		nfc_err(&dev->dev, "invalid frame len\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		phy->ndlc->hard_fault = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	*skb = alloc_skb(ST_NCI_SPI_MIN_SIZE + len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (*skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	skb_reserve(*skb, ST_NCI_SPI_MIN_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	skb_put(*skb, ST_NCI_SPI_MIN_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	memcpy((*skb)->data, buf, ST_NCI_SPI_MIN_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	spi_xfer.len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	r = spi_sync_transfer(dev, &spi_xfer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		kfree_skb(*skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		return -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	skb_put(*skb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	memcpy((*skb)->data + ST_NCI_SPI_MIN_SIZE, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * Reads an ndlc frame from the chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * On ST21NFCB, IRQ goes in idle state when read starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct st_nci_spi_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct spi_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct sk_buff *skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (!phy || !phy->ndlc || irq != phy->spi_dev->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	dev = phy->spi_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	dev_dbg(&dev->dev, "IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (phy->ndlc->hard_fault)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (!phy->ndlc->powered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		st_nci_spi_disable(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	r = st_nci_spi_read(phy, &skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	ndlc_recv(phy->ndlc, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static struct nfc_phy_ops spi_phy_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	.write = st_nci_spi_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	.enable = st_nci_spi_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	.disable = st_nci_spi_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	{ "reset-gpios", &reset_gpios, 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int st_nci_spi_probe(struct spi_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct st_nci_spi_phy *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	dev_dbg(&dev->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	dev_dbg(&dev->dev, "IRQ: %d\n", dev->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	/* Check SPI platform functionnalities */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		pr_debug("%s: dev is NULL. Device is not accessible.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	phy = devm_kzalloc(&dev->dev, sizeof(struct st_nci_spi_phy),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			   GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (!phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	phy->spi_dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	spi_set_drvdata(dev, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	r = devm_acpi_dev_add_driver_gpios(&dev->dev, acpi_st_nci_gpios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		dev_dbg(&dev->dev, "Unable to add GPIO mapping table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	/* Get RESET GPIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	phy->gpiod_reset = devm_gpiod_get(&dev->dev, "reset", GPIOD_OUT_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (IS_ERR(phy->gpiod_reset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		nfc_err(&dev->dev, "Unable to get RESET GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return PTR_ERR(phy->gpiod_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	phy->se_status.is_ese_present =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			device_property_read_bool(&dev->dev, "ese-present");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	phy->se_status.is_uicc_present =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			device_property_read_bool(&dev->dev, "uicc-present");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	r = ndlc_probe(phy, &spi_phy_ops, &dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			&phy->ndlc, &phy->se_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		nfc_err(&dev->dev, "Unable to register ndlc layer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	phy->irq_active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	r = devm_request_threaded_irq(&dev->dev, dev->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				st_nci_irq_thread_fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 				IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 				ST_NCI_SPI_DRIVER_NAME, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		nfc_err(&dev->dev, "Unable to register IRQ handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static int st_nci_spi_remove(struct spi_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct st_nci_spi_phy *phy = spi_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	dev_dbg(&dev->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	ndlc_remove(phy->ndlc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static struct spi_device_id st_nci_spi_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	{ST_NCI_SPI_DRIVER_NAME, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) MODULE_DEVICE_TABLE(spi, st_nci_spi_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static const struct acpi_device_id st_nci_spi_acpi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	{"SMO2101", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) MODULE_DEVICE_TABLE(acpi, st_nci_spi_acpi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static const struct of_device_id of_st_nci_spi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	{ .compatible = "st,st21nfcb-spi", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) MODULE_DEVICE_TABLE(of, of_st_nci_spi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static struct spi_driver st_nci_spi_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		.name = ST_NCI_SPI_DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		.of_match_table = of_match_ptr(of_st_nci_spi_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		.acpi_match_table = ACPI_PTR(st_nci_spi_acpi_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	.probe = st_nci_spi_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	.id_table = st_nci_spi_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	.remove = st_nci_spi_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) module_spi_driver(st_nci_spi_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) MODULE_DESCRIPTION(DRIVER_DESC);