^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* -------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2014-2016, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/nfc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/nfc/nfc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/nfc/nci_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "fdp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define FDP_I2C_DRIVER_NAME "fdp_nci_i2c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define FDP_DP_CLOCK_TYPE_NAME "clock-type"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define FDP_DP_CLOCK_FREQ_NAME "clock-freq"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define FDP_DP_FW_VSC_CFG_NAME "fw-vsc-cfg"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define FDP_FRAME_HEADROOM 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define FDP_FRAME_TAILROOM 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define FDP_NCI_I2C_MIN_PAYLOAD 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define FDP_NCI_I2C_MAX_PAYLOAD 261
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define FDP_POWER_OFF 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define FDP_POWER_ON 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define fdp_nci_i2c_dump_skb(dev, prefix, skb) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) print_hex_dump(KERN_DEBUG, prefix": ", DUMP_PREFIX_OFFSET, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 16, 1, (skb)->data, (skb)->len, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void fdp_nci_i2c_reset(struct fdp_i2c_phy *phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Reset RST/WakeUP for at least 100 micro-second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) usleep_range(1000, 4000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) usleep_range(10000, 14000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int fdp_nci_i2c_enable(void *phy_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct fdp_i2c_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dev_dbg(&phy->i2c_dev->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) fdp_nci_i2c_reset(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 0;
^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) static void fdp_nci_i2c_disable(void *phy_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct fdp_i2c_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) dev_dbg(&phy->i2c_dev->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) fdp_nci_i2c_reset(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static void fdp_nci_i2c_add_len_lrc(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u8 lrc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u16 len, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* Add length header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *(u8 *)skb_push(skb, 1) = len & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) *(u8 *)skb_push(skb, 1) = len >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* Compute and add lrc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) for (i = 0; i < len + 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) lrc ^= skb->data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) skb_put_u8(skb, lrc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static void fdp_nci_i2c_remove_len_lrc(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) skb_pull(skb, FDP_FRAME_HEADROOM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) skb_trim(skb, skb->len - FDP_FRAME_TAILROOM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int fdp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct fdp_i2c_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct i2c_client *client = phy->i2c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (phy->hard_fault != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return phy->hard_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) fdp_nci_i2c_add_len_lrc(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) fdp_nci_i2c_dump_skb(&client->dev, "fdp_wr", skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) r = i2c_master_send(client, skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (r == -EREMOTEIO) { /* Retry, chip was in standby */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) usleep_range(1000, 4000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) r = i2c_master_send(client, skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (r < 0 || r != skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dev_dbg(&client->dev, "%s: error err=%d len=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) __func__, r, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (r >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (r != skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) phy->hard_fault = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) r = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) fdp_nci_i2c_remove_len_lrc(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static struct nfc_phy_ops i2c_phy_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .write = fdp_nci_i2c_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .enable = fdp_nci_i2c_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .disable = fdp_nci_i2c_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int r, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD], lrc, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) u16 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct i2c_client *client = phy->i2c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* Read the length packet and the data packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) for (k = 0; k < 2; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) len = phy->next_read_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) r = i2c_master_recv(client, tmp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (r != len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dev_dbg(&client->dev, "%s: i2c recv err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) __func__, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) goto flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* Check packet integruty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) for (lrc = i = 0; i < r; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) lrc ^= tmp[i];
^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) * LRC check failed. This may due to transmission error or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * desynchronization between driver and FDP. Drop the paquet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * and force resynchronization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (lrc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) dev_dbg(&client->dev, "%s: corrupted packet\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) phy->next_read_size = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) goto flush;
^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) /* Packet that contains a length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (tmp[0] == 0 && tmp[1] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) phy->next_read_size = (tmp[2] << 8) + tmp[3] + 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) *skb = alloc_skb(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (*skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) goto flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) skb_put_data(*skb, tmp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) fdp_nci_i2c_dump_skb(&client->dev, "fdp_rd", *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) fdp_nci_i2c_remove_len_lrc(*skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) flush:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* Flush the remaining data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) r = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static irqreturn_t fdp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct fdp_i2c_phy *phy = phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (!phy || irq != phy->i2c_dev->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) client = phy->i2c_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dev_dbg(&client->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) r = fdp_nci_i2c_read(phy, &skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (r == -EREMOTEIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) else if (r == -ENOMEM || r == -EBADMSG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (skb != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) fdp_nci_recv_frame(phy->ndev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static void fdp_nci_i2c_read_device_properties(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) u8 *clock_type, u32 *clock_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) u8 **fw_vsc_cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) u8 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) r = device_property_read_u8(dev, FDP_DP_CLOCK_TYPE_NAME, clock_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dev_dbg(dev, "Using default clock type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) *clock_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) r = device_property_read_u32(dev, FDP_DP_CLOCK_FREQ_NAME, clock_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) dev_dbg(dev, "Using default clock frequency\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *clock_freq = 26000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (device_property_present(dev, FDP_DP_FW_VSC_CFG_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) r = device_property_read_u8(dev, FDP_DP_FW_VSC_CFG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (r || len <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto vsc_read_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /* Add 1 to the length to inclue the length byte itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *fw_vsc_cfg = devm_kmalloc_array(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) len, sizeof(**fw_vsc_cfg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) *fw_vsc_cfg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) devm_kfree(dev, *fw_vsc_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) goto vsc_read_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) vsc_read_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dev_dbg(dev, "FW vendor specific commands not present\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) *fw_vsc_cfg = NULL;
^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) dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static const struct acpi_gpio_params power_gpios = { 0, 0, false };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static const struct acpi_gpio_mapping acpi_fdp_gpios[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) { "power-gpios", &power_gpios, 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int fdp_nci_i2c_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct fdp_i2c_phy *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) u8 *fw_vsc_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) u8 clock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) u32 clock_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) dev_dbg(dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) nfc_err(dev, "No I2C_FUNC_I2C support\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /* Checking if we have an irq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (client->irq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) nfc_err(dev, "IRQ not present\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) phy = devm_kzalloc(dev, sizeof(struct fdp_i2c_phy), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) phy->i2c_dev = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) i2c_set_clientdata(client, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) r = devm_request_threaded_irq(dev, client->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) NULL, fdp_nci_i2c_irq_thread_fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) IRQF_TRIGGER_RISING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) FDP_I2C_DRIVER_NAME, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) nfc_err(&client->dev, "Unable to register IRQ handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) r = devm_acpi_dev_add_driver_gpios(dev, acpi_fdp_gpios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) dev_dbg(dev, "Unable to add GPIO mapping table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* Requesting the power gpio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) phy->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (IS_ERR(phy->power_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) nfc_err(dev, "Power GPIO request failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return PTR_ERR(phy->power_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* read device properties to get the clock and production settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) fdp_nci_i2c_read_device_properties(dev, &clock_type, &clock_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) &fw_vsc_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* Call the NFC specific probe function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) r = fdp_nci_probe(phy, &i2c_phy_ops, &phy->ndev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) FDP_FRAME_HEADROOM, FDP_FRAME_TAILROOM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) clock_type, clock_freq, fw_vsc_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) nfc_err(dev, "NCI probing error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) dev_dbg(dev, "I2C driver loaded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static int fdp_nci_i2c_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct fdp_i2c_phy *phy = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) dev_dbg(&client->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) fdp_nci_remove(phy->ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) fdp_nci_i2c_disable(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static const struct acpi_device_id fdp_nci_i2c_acpi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {"INT339A", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) MODULE_DEVICE_TABLE(acpi, fdp_nci_i2c_acpi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static struct i2c_driver fdp_nci_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .name = FDP_I2C_DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) .acpi_match_table = ACPI_PTR(fdp_nci_i2c_acpi_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) .probe_new = fdp_nci_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) .remove = fdp_nci_i2c_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) module_i2c_driver(fdp_nci_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) MODULE_DESCRIPTION("I2C driver for Intel Fields Peak NFC controller");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");