Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* -------------------------------------------------------------------------
0003  * Copyright (C) 2014-2016, Intel Corporation
0004  *
0005  * -------------------------------------------------------------------------
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/acpi.h>
0010 #include <linux/i2c.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/nfc.h>
0013 #include <linux/delay.h>
0014 #include <linux/gpio/consumer.h>
0015 #include <net/nfc/nfc.h>
0016 #include <net/nfc/nci_core.h>
0017 
0018 #include "fdp.h"
0019 
0020 #define FDP_I2C_DRIVER_NAME "fdp_nci_i2c"
0021 
0022 #define FDP_DP_CLOCK_TYPE_NAME  "clock-type"
0023 #define FDP_DP_CLOCK_FREQ_NAME  "clock-freq"
0024 #define FDP_DP_FW_VSC_CFG_NAME  "fw-vsc-cfg"
0025 
0026 #define FDP_FRAME_HEADROOM  2
0027 #define FDP_FRAME_TAILROOM  1
0028 
0029 #define FDP_NCI_I2C_MIN_PAYLOAD 5
0030 #define FDP_NCI_I2C_MAX_PAYLOAD 261
0031 
0032 #define FDP_POWER_OFF       0
0033 #define FDP_POWER_ON        1
0034 
0035 #define fdp_nci_i2c_dump_skb(dev, prefix, skb)              \
0036     print_hex_dump(KERN_DEBUG, prefix": ", DUMP_PREFIX_OFFSET,  \
0037                16, 1, (skb)->data, (skb)->len, 0)
0038 
0039 static void fdp_nci_i2c_reset(const struct fdp_i2c_phy *phy)
0040 {
0041     /* Reset RST/WakeUP for at least 100 micro-second */
0042     gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_OFF);
0043     usleep_range(1000, 4000);
0044     gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_ON);
0045     usleep_range(10000, 14000);
0046 }
0047 
0048 static int fdp_nci_i2c_enable(void *phy_id)
0049 {
0050     const struct fdp_i2c_phy *phy = phy_id;
0051 
0052     fdp_nci_i2c_reset(phy);
0053 
0054     return 0;
0055 }
0056 
0057 static void fdp_nci_i2c_disable(void *phy_id)
0058 {
0059     const struct fdp_i2c_phy *phy = phy_id;
0060 
0061     fdp_nci_i2c_reset(phy);
0062 }
0063 
0064 static void fdp_nci_i2c_add_len_lrc(struct sk_buff *skb)
0065 {
0066     u8 lrc = 0;
0067     u16 len, i;
0068 
0069     /* Add length header */
0070     len = skb->len;
0071     *(u8 *)skb_push(skb, 1) = len & 0xff;
0072     *(u8 *)skb_push(skb, 1) = len >> 8;
0073 
0074     /* Compute and add lrc */
0075     for (i = 0; i < len + 2; i++)
0076         lrc ^= skb->data[i];
0077 
0078     skb_put_u8(skb, lrc);
0079 }
0080 
0081 static void fdp_nci_i2c_remove_len_lrc(struct sk_buff *skb)
0082 {
0083     skb_pull(skb, FDP_FRAME_HEADROOM);
0084     skb_trim(skb, skb->len - FDP_FRAME_TAILROOM);
0085 }
0086 
0087 static int fdp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
0088 {
0089     struct fdp_i2c_phy *phy = phy_id;
0090     struct i2c_client *client = phy->i2c_dev;
0091     int r;
0092 
0093     if (phy->hard_fault != 0)
0094         return phy->hard_fault;
0095 
0096     fdp_nci_i2c_add_len_lrc(skb);
0097     fdp_nci_i2c_dump_skb(&client->dev, "fdp_wr", skb);
0098 
0099     r = i2c_master_send(client, skb->data, skb->len);
0100     if (r == -EREMOTEIO) {  /* Retry, chip was in standby */
0101         usleep_range(1000, 4000);
0102         r = i2c_master_send(client, skb->data, skb->len);
0103     }
0104 
0105     if (r < 0 || r != skb->len)
0106         dev_dbg(&client->dev, "%s: error err=%d len=%d\n",
0107             __func__, r, skb->len);
0108 
0109     if (r >= 0) {
0110         if (r != skb->len) {
0111             phy->hard_fault = r;
0112             r = -EREMOTEIO;
0113         } else {
0114             r = 0;
0115         }
0116     }
0117 
0118     fdp_nci_i2c_remove_len_lrc(skb);
0119 
0120     return r;
0121 }
0122 
0123 static const struct nfc_phy_ops i2c_phy_ops = {
0124     .write = fdp_nci_i2c_write,
0125     .enable = fdp_nci_i2c_enable,
0126     .disable = fdp_nci_i2c_disable,
0127 };
0128 
0129 static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb)
0130 {
0131     int r, len;
0132     u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD], lrc, k;
0133     u16 i;
0134     struct i2c_client *client = phy->i2c_dev;
0135 
0136     *skb = NULL;
0137 
0138     /* Read the length packet and the data packet */
0139     for (k = 0; k < 2; k++) {
0140 
0141         len = phy->next_read_size;
0142 
0143         r = i2c_master_recv(client, tmp, len);
0144         if (r != len) {
0145             dev_dbg(&client->dev, "%s: i2c recv err: %d\n",
0146                 __func__, r);
0147             goto flush;
0148         }
0149 
0150         /* Check packet integruty */
0151         for (lrc = i = 0; i < r; i++)
0152             lrc ^= tmp[i];
0153 
0154         /*
0155          * LRC check failed. This may due to transmission error or
0156          * desynchronization between driver and FDP. Drop the packet
0157          * and force resynchronization
0158          */
0159         if (lrc) {
0160             dev_dbg(&client->dev, "%s: corrupted packet\n",
0161                 __func__);
0162             phy->next_read_size = 5;
0163             goto flush;
0164         }
0165 
0166         /* Packet that contains a length */
0167         if (tmp[0] == 0 && tmp[1] == 0) {
0168             phy->next_read_size = (tmp[2] << 8) + tmp[3] + 3;
0169         } else {
0170             phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
0171 
0172             *skb = alloc_skb(len, GFP_KERNEL);
0173             if (*skb == NULL) {
0174                 r = -ENOMEM;
0175                 goto flush;
0176             }
0177 
0178             skb_put_data(*skb, tmp, len);
0179             fdp_nci_i2c_dump_skb(&client->dev, "fdp_rd", *skb);
0180 
0181             fdp_nci_i2c_remove_len_lrc(*skb);
0182         }
0183     }
0184 
0185     return 0;
0186 
0187 flush:
0188     /* Flush the remaining data */
0189     if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
0190         r = -EREMOTEIO;
0191 
0192     return r;
0193 }
0194 
0195 static irqreturn_t fdp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
0196 {
0197     struct fdp_i2c_phy *phy = phy_id;
0198     struct sk_buff *skb;
0199     int r;
0200 
0201     if (!phy || irq != phy->i2c_dev->irq) {
0202         WARN_ON_ONCE(1);
0203         return IRQ_NONE;
0204     }
0205 
0206     r = fdp_nci_i2c_read(phy, &skb);
0207 
0208     if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
0209         return IRQ_HANDLED;
0210 
0211     if (skb != NULL)
0212         nci_recv_frame(phy->ndev, skb);
0213 
0214     return IRQ_HANDLED;
0215 }
0216 
0217 static void fdp_nci_i2c_read_device_properties(struct device *dev,
0218                            u8 *clock_type, u32 *clock_freq,
0219                            u8 **fw_vsc_cfg)
0220 {
0221     int r;
0222     u8 len;
0223 
0224     r = device_property_read_u8(dev, FDP_DP_CLOCK_TYPE_NAME, clock_type);
0225     if (r) {
0226         dev_dbg(dev, "Using default clock type");
0227         *clock_type = 0;
0228     }
0229 
0230     r = device_property_read_u32(dev, FDP_DP_CLOCK_FREQ_NAME, clock_freq);
0231     if (r) {
0232         dev_dbg(dev, "Using default clock frequency\n");
0233         *clock_freq = 26000;
0234     }
0235 
0236     if (device_property_present(dev, FDP_DP_FW_VSC_CFG_NAME)) {
0237         r = device_property_read_u8(dev, FDP_DP_FW_VSC_CFG_NAME,
0238                         &len);
0239 
0240         if (r || len <= 0)
0241             goto vsc_read_err;
0242 
0243         /* Add 1 to the length to inclue the length byte itself */
0244         len++;
0245 
0246         *fw_vsc_cfg = devm_kmalloc_array(dev,
0247                        len, sizeof(**fw_vsc_cfg),
0248                        GFP_KERNEL);
0249 
0250         r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
0251                           *fw_vsc_cfg, len);
0252 
0253         if (r) {
0254             devm_kfree(dev, *fw_vsc_cfg);
0255             goto vsc_read_err;
0256         }
0257     } else {
0258 vsc_read_err:
0259         dev_dbg(dev, "FW vendor specific commands not present\n");
0260         *fw_vsc_cfg = NULL;
0261     }
0262 
0263     dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
0264         *clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
0265 }
0266 
0267 static const struct acpi_gpio_params power_gpios = { 0, 0, false };
0268 
0269 static const struct acpi_gpio_mapping acpi_fdp_gpios[] = {
0270     { "power-gpios", &power_gpios, 1 },
0271     {},
0272 };
0273 
0274 static int fdp_nci_i2c_probe(struct i2c_client *client)
0275 {
0276     struct fdp_i2c_phy *phy;
0277     struct device *dev = &client->dev;
0278     u8 *fw_vsc_cfg;
0279     u8 clock_type;
0280     u32 clock_freq;
0281     int r = 0;
0282 
0283     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0284         nfc_err(dev, "No I2C_FUNC_I2C support\n");
0285         return -ENODEV;
0286     }
0287 
0288     /* Checking if we have an irq */
0289     if (client->irq <= 0) {
0290         nfc_err(dev, "IRQ not present\n");
0291         return -ENODEV;
0292     }
0293 
0294     phy = devm_kzalloc(dev, sizeof(struct fdp_i2c_phy), GFP_KERNEL);
0295     if (!phy)
0296         return -ENOMEM;
0297 
0298     phy->i2c_dev = client;
0299     phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
0300     i2c_set_clientdata(client, phy);
0301 
0302     r = devm_request_threaded_irq(dev, client->irq,
0303                       NULL, fdp_nci_i2c_irq_thread_fn,
0304                       IRQF_TRIGGER_RISING | IRQF_ONESHOT,
0305                       FDP_I2C_DRIVER_NAME, phy);
0306 
0307     if (r < 0) {
0308         nfc_err(&client->dev, "Unable to register IRQ handler\n");
0309         return r;
0310     }
0311 
0312     r = devm_acpi_dev_add_driver_gpios(dev, acpi_fdp_gpios);
0313     if (r)
0314         dev_dbg(dev, "Unable to add GPIO mapping table\n");
0315 
0316     /* Requesting the power gpio */
0317     phy->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
0318     if (IS_ERR(phy->power_gpio)) {
0319         nfc_err(dev, "Power GPIO request failed\n");
0320         return PTR_ERR(phy->power_gpio);
0321     }
0322 
0323     /* read device properties to get the clock and production settings */
0324     fdp_nci_i2c_read_device_properties(dev, &clock_type, &clock_freq,
0325                        &fw_vsc_cfg);
0326 
0327     /* Call the NFC specific probe function */
0328     r = fdp_nci_probe(phy, &i2c_phy_ops, &phy->ndev,
0329               FDP_FRAME_HEADROOM, FDP_FRAME_TAILROOM,
0330               clock_type, clock_freq, fw_vsc_cfg);
0331     if (r < 0) {
0332         nfc_err(dev, "NCI probing error\n");
0333         return r;
0334     }
0335 
0336     return 0;
0337 }
0338 
0339 static int fdp_nci_i2c_remove(struct i2c_client *client)
0340 {
0341     struct fdp_i2c_phy *phy = i2c_get_clientdata(client);
0342 
0343     fdp_nci_remove(phy->ndev);
0344     fdp_nci_i2c_disable(phy);
0345 
0346     return 0;
0347 }
0348 
0349 static const struct acpi_device_id fdp_nci_i2c_acpi_match[] = {
0350     {"INT339A", 0},
0351     {}
0352 };
0353 MODULE_DEVICE_TABLE(acpi, fdp_nci_i2c_acpi_match);
0354 
0355 static struct i2c_driver fdp_nci_i2c_driver = {
0356     .driver = {
0357            .name = FDP_I2C_DRIVER_NAME,
0358            .acpi_match_table = fdp_nci_i2c_acpi_match,
0359           },
0360     .probe_new = fdp_nci_i2c_probe,
0361     .remove = fdp_nci_i2c_remove,
0362 };
0363 module_i2c_driver(fdp_nci_i2c_driver);
0364 
0365 MODULE_LICENSE("GPL");
0366 MODULE_DESCRIPTION("I2C driver for Intel Fields Peak NFC controller");
0367 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");