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