Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * I2C Link Layer for Samsung S3FWRN5 NCI based Driver
0004  *
0005  * Copyright (C) 2015 Samsung Electrnoics
0006  * Robert Baldyga <r.baldyga@samsung.com>
0007  */
0008 
0009 #include <linux/clk.h>
0010 #include <linux/i2c.h>
0011 #include <linux/gpio.h>
0012 #include <linux/delay.h>
0013 #include <linux/of_gpio.h>
0014 #include <linux/of_irq.h>
0015 #include <linux/module.h>
0016 
0017 #include <net/nfc/nfc.h>
0018 
0019 #include "phy_common.h"
0020 
0021 #define S3FWRN5_I2C_DRIVER_NAME "s3fwrn5_i2c"
0022 
0023 struct s3fwrn5_i2c_phy {
0024     struct phy_common common;
0025     struct i2c_client *i2c_dev;
0026     struct clk *clk;
0027 
0028     unsigned int irq_skip:1;
0029 };
0030 
0031 static void s3fwrn5_i2c_set_mode(void *phy_id, enum s3fwrn5_mode mode)
0032 {
0033     struct s3fwrn5_i2c_phy *phy = phy_id;
0034 
0035     mutex_lock(&phy->common.mutex);
0036 
0037     if (s3fwrn5_phy_power_ctrl(&phy->common, mode) == false)
0038         goto out;
0039 
0040     phy->irq_skip = true;
0041 
0042 out:
0043     mutex_unlock(&phy->common.mutex);
0044 }
0045 
0046 static int s3fwrn5_i2c_write(void *phy_id, struct sk_buff *skb)
0047 {
0048     struct s3fwrn5_i2c_phy *phy = phy_id;
0049     int ret;
0050 
0051     mutex_lock(&phy->common.mutex);
0052 
0053     phy->irq_skip = false;
0054 
0055     ret = i2c_master_send(phy->i2c_dev, skb->data, skb->len);
0056     if (ret == -EREMOTEIO) {
0057         /* Retry, chip was in standby */
0058         usleep_range(110000, 120000);
0059         ret  = i2c_master_send(phy->i2c_dev, skb->data, skb->len);
0060     }
0061 
0062     mutex_unlock(&phy->common.mutex);
0063 
0064     if (ret < 0)
0065         return ret;
0066 
0067     if (ret != skb->len)
0068         return -EREMOTEIO;
0069 
0070     return 0;
0071 }
0072 
0073 static const struct s3fwrn5_phy_ops i2c_phy_ops = {
0074     .set_wake = s3fwrn5_phy_set_wake,
0075     .set_mode = s3fwrn5_i2c_set_mode,
0076     .get_mode = s3fwrn5_phy_get_mode,
0077     .write = s3fwrn5_i2c_write,
0078 };
0079 
0080 static int s3fwrn5_i2c_read(struct s3fwrn5_i2c_phy *phy)
0081 {
0082     struct sk_buff *skb;
0083     size_t hdr_size;
0084     size_t data_len;
0085     char hdr[4];
0086     int ret;
0087 
0088     hdr_size = (phy->common.mode == S3FWRN5_MODE_NCI) ?
0089         NCI_CTRL_HDR_SIZE : S3FWRN5_FW_HDR_SIZE;
0090     ret = i2c_master_recv(phy->i2c_dev, hdr, hdr_size);
0091     if (ret < 0)
0092         return ret;
0093 
0094     if (ret < hdr_size)
0095         return -EBADMSG;
0096 
0097     data_len = (phy->common.mode == S3FWRN5_MODE_NCI) ?
0098         ((struct nci_ctrl_hdr *)hdr)->plen :
0099         ((struct s3fwrn5_fw_header *)hdr)->len;
0100 
0101     skb = alloc_skb(hdr_size + data_len, GFP_KERNEL);
0102     if (!skb)
0103         return -ENOMEM;
0104 
0105     skb_put_data(skb, hdr, hdr_size);
0106 
0107     if (data_len == 0)
0108         goto out;
0109 
0110     ret = i2c_master_recv(phy->i2c_dev, skb_put(skb, data_len), data_len);
0111     if (ret != data_len) {
0112         kfree_skb(skb);
0113         return -EBADMSG;
0114     }
0115 
0116 out:
0117     return s3fwrn5_recv_frame(phy->common.ndev, skb, phy->common.mode);
0118 }
0119 
0120 static irqreturn_t s3fwrn5_i2c_irq_thread_fn(int irq, void *phy_id)
0121 {
0122     struct s3fwrn5_i2c_phy *phy = phy_id;
0123 
0124     if (!phy || !phy->common.ndev) {
0125         WARN_ON_ONCE(1);
0126         return IRQ_NONE;
0127     }
0128 
0129     mutex_lock(&phy->common.mutex);
0130 
0131     if (phy->irq_skip)
0132         goto out;
0133 
0134     switch (phy->common.mode) {
0135     case S3FWRN5_MODE_NCI:
0136     case S3FWRN5_MODE_FW:
0137         s3fwrn5_i2c_read(phy);
0138         break;
0139     case S3FWRN5_MODE_COLD:
0140         break;
0141     }
0142 
0143 out:
0144     mutex_unlock(&phy->common.mutex);
0145 
0146     return IRQ_HANDLED;
0147 }
0148 
0149 static int s3fwrn5_i2c_parse_dt(struct i2c_client *client)
0150 {
0151     struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
0152     struct device_node *np = client->dev.of_node;
0153 
0154     if (!np)
0155         return -ENODEV;
0156 
0157     phy->common.gpio_en = of_get_named_gpio(np, "en-gpios", 0);
0158     if (!gpio_is_valid(phy->common.gpio_en)) {
0159         /* Support also deprecated property */
0160         phy->common.gpio_en = of_get_named_gpio(np,
0161                             "s3fwrn5,en-gpios",
0162                             0);
0163         if (!gpio_is_valid(phy->common.gpio_en))
0164             return -ENODEV;
0165     }
0166 
0167     phy->common.gpio_fw_wake = of_get_named_gpio(np, "wake-gpios", 0);
0168     if (!gpio_is_valid(phy->common.gpio_fw_wake)) {
0169         /* Support also deprecated property */
0170         phy->common.gpio_fw_wake = of_get_named_gpio(np,
0171                                  "s3fwrn5,fw-gpios",
0172                                  0);
0173         if (!gpio_is_valid(phy->common.gpio_fw_wake))
0174             return -ENODEV;
0175     }
0176 
0177     return 0;
0178 }
0179 
0180 static int s3fwrn5_i2c_probe(struct i2c_client *client,
0181                   const struct i2c_device_id *id)
0182 {
0183     struct s3fwrn5_i2c_phy *phy;
0184     int ret;
0185 
0186     phy = devm_kzalloc(&client->dev, sizeof(*phy), GFP_KERNEL);
0187     if (!phy)
0188         return -ENOMEM;
0189 
0190     mutex_init(&phy->common.mutex);
0191     phy->common.mode = S3FWRN5_MODE_COLD;
0192     phy->irq_skip = true;
0193 
0194     phy->i2c_dev = client;
0195     i2c_set_clientdata(client, phy);
0196 
0197     ret = s3fwrn5_i2c_parse_dt(client);
0198     if (ret < 0)
0199         return ret;
0200 
0201     ret = devm_gpio_request_one(&phy->i2c_dev->dev, phy->common.gpio_en,
0202                     GPIOF_OUT_INIT_HIGH, "s3fwrn5_en");
0203     if (ret < 0)
0204         return ret;
0205 
0206     ret = devm_gpio_request_one(&phy->i2c_dev->dev,
0207                     phy->common.gpio_fw_wake,
0208                     GPIOF_OUT_INIT_LOW, "s3fwrn5_fw_wake");
0209     if (ret < 0)
0210         return ret;
0211 
0212     phy->clk = devm_clk_get_optional(&client->dev, NULL);
0213     if (IS_ERR(phy->clk))
0214         return dev_err_probe(&client->dev, PTR_ERR(phy->clk),
0215                      "failed to get clock\n");
0216 
0217     /*
0218      * S3FWRN5 depends on a clock input ("XI" pin) to function properly.
0219      * Depending on the hardware configuration this could be an always-on
0220      * oscillator or some external clock that must be explicitly enabled.
0221      * Make sure the clock is running before starting S3FWRN5.
0222      */
0223     ret = clk_prepare_enable(phy->clk);
0224     if (ret < 0) {
0225         dev_err(&client->dev, "failed to enable clock: %d\n", ret);
0226         return ret;
0227     }
0228 
0229     ret = s3fwrn5_probe(&phy->common.ndev, phy, &phy->i2c_dev->dev,
0230                 &i2c_phy_ops);
0231     if (ret < 0)
0232         goto disable_clk;
0233 
0234     ret = devm_request_threaded_irq(&client->dev, phy->i2c_dev->irq, NULL,
0235         s3fwrn5_i2c_irq_thread_fn, IRQF_ONESHOT,
0236         S3FWRN5_I2C_DRIVER_NAME, phy);
0237     if (ret)
0238         goto s3fwrn5_remove;
0239 
0240     return 0;
0241 
0242 s3fwrn5_remove:
0243     s3fwrn5_remove(phy->common.ndev);
0244 disable_clk:
0245     clk_disable_unprepare(phy->clk);
0246     return ret;
0247 }
0248 
0249 static int s3fwrn5_i2c_remove(struct i2c_client *client)
0250 {
0251     struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
0252 
0253     s3fwrn5_remove(phy->common.ndev);
0254     clk_disable_unprepare(phy->clk);
0255 
0256     return 0;
0257 }
0258 
0259 static const struct i2c_device_id s3fwrn5_i2c_id_table[] = {
0260     {S3FWRN5_I2C_DRIVER_NAME, 0},
0261     {}
0262 };
0263 MODULE_DEVICE_TABLE(i2c, s3fwrn5_i2c_id_table);
0264 
0265 static const struct of_device_id of_s3fwrn5_i2c_match[] __maybe_unused = {
0266     { .compatible = "samsung,s3fwrn5-i2c", },
0267     {}
0268 };
0269 MODULE_DEVICE_TABLE(of, of_s3fwrn5_i2c_match);
0270 
0271 static struct i2c_driver s3fwrn5_i2c_driver = {
0272     .driver = {
0273         .name = S3FWRN5_I2C_DRIVER_NAME,
0274         .of_match_table = of_match_ptr(of_s3fwrn5_i2c_match),
0275     },
0276     .probe = s3fwrn5_i2c_probe,
0277     .remove = s3fwrn5_i2c_remove,
0278     .id_table = s3fwrn5_i2c_id_table,
0279 };
0280 
0281 module_i2c_driver(s3fwrn5_i2c_driver);
0282 
0283 MODULE_LICENSE("GPL");
0284 MODULE_DESCRIPTION("I2C driver for Samsung S3FWRN5");
0285 MODULE_AUTHOR("Robert Baldyga <r.baldyga@samsung.com>");