Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Marvell NFC-over-I2C driver: I2C interface related functions
0004  *
0005  * Copyright (C) 2015, Marvell International Ltd.
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/i2c.h>
0011 #include <linux/nfc.h>
0012 #include <linux/delay.h>
0013 #include <linux/of_irq.h>
0014 #include <net/nfc/nci.h>
0015 #include <net/nfc/nci_core.h>
0016 #include "nfcmrvl.h"
0017 
0018 struct nfcmrvl_i2c_drv_data {
0019     unsigned long flags;
0020     struct device *dev;
0021     struct i2c_client *i2c;
0022     struct nfcmrvl_private *priv;
0023 };
0024 
0025 static int nfcmrvl_i2c_read(struct nfcmrvl_i2c_drv_data *drv_data,
0026                 struct sk_buff **skb)
0027 {
0028     int ret;
0029     struct nci_ctrl_hdr nci_hdr;
0030 
0031     /* Read NCI header to know the payload size */
0032     ret = i2c_master_recv(drv_data->i2c, (u8 *)&nci_hdr, NCI_CTRL_HDR_SIZE);
0033     if (ret != NCI_CTRL_HDR_SIZE) {
0034         nfc_err(&drv_data->i2c->dev, "cannot read NCI header\n");
0035         return -EBADMSG;
0036     }
0037 
0038     *skb = nci_skb_alloc(drv_data->priv->ndev,
0039                  nci_hdr.plen + NCI_CTRL_HDR_SIZE, GFP_KERNEL);
0040     if (!*skb)
0041         return -ENOMEM;
0042 
0043     /* Copy NCI header into the SKB */
0044     skb_put_data(*skb, &nci_hdr, NCI_CTRL_HDR_SIZE);
0045 
0046     if (nci_hdr.plen) {
0047         /* Read the NCI payload */
0048         ret = i2c_master_recv(drv_data->i2c,
0049                       skb_put(*skb, nci_hdr.plen),
0050                       nci_hdr.plen);
0051 
0052         if (ret != nci_hdr.plen) {
0053             nfc_err(&drv_data->i2c->dev,
0054                 "Invalid frame payload length: %u (expected %u)\n",
0055                 ret, nci_hdr.plen);
0056             kfree_skb(*skb);
0057             return -EBADMSG;
0058         }
0059     }
0060 
0061     return 0;
0062 }
0063 
0064 static irqreturn_t nfcmrvl_i2c_int_irq_thread_fn(int irq, void *drv_data_ptr)
0065 {
0066     struct nfcmrvl_i2c_drv_data *drv_data = drv_data_ptr;
0067     struct sk_buff *skb = NULL;
0068     int ret;
0069 
0070     if (!drv_data->priv)
0071         return IRQ_HANDLED;
0072 
0073     if (test_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags))
0074         return IRQ_HANDLED;
0075 
0076     ret = nfcmrvl_i2c_read(drv_data, &skb);
0077 
0078     switch (ret) {
0079     case -EREMOTEIO:
0080         set_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags);
0081         break;
0082     case -ENOMEM:
0083     case -EBADMSG:
0084         nfc_err(&drv_data->i2c->dev, "read failed %d\n", ret);
0085         break;
0086     default:
0087         if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
0088             nfc_err(&drv_data->i2c->dev, "corrupted RX packet\n");
0089         break;
0090     }
0091     return IRQ_HANDLED;
0092 }
0093 
0094 static int nfcmrvl_i2c_nci_open(struct nfcmrvl_private *priv)
0095 {
0096     struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
0097 
0098     if (!drv_data)
0099         return -ENODEV;
0100 
0101     return 0;
0102 }
0103 
0104 static int nfcmrvl_i2c_nci_close(struct nfcmrvl_private *priv)
0105 {
0106     return 0;
0107 }
0108 
0109 static int nfcmrvl_i2c_nci_send(struct nfcmrvl_private *priv,
0110                 struct sk_buff *skb)
0111 {
0112     struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
0113     int ret;
0114 
0115     if (test_bit(NFCMRVL_PHY_ERROR, &priv->flags))
0116         return -EREMOTEIO;
0117 
0118     ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
0119 
0120     /* Retry if chip was in standby */
0121     if (ret == -EREMOTEIO) {
0122         nfc_info(drv_data->dev, "chip may sleep, retry\n");
0123         usleep_range(6000, 10000);
0124         ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
0125     }
0126 
0127     if (ret >= 0) {
0128         if (ret != skb->len) {
0129             nfc_err(drv_data->dev,
0130                 "Invalid length sent: %u (expected %u)\n",
0131                 ret, skb->len);
0132             ret = -EREMOTEIO;
0133         } else
0134             ret = 0;
0135         kfree_skb(skb);
0136     }
0137 
0138     return ret;
0139 }
0140 
0141 static void nfcmrvl_i2c_nci_update_config(struct nfcmrvl_private *priv,
0142                       const void *param)
0143 {
0144 }
0145 
0146 static const struct nfcmrvl_if_ops i2c_ops = {
0147     .nci_open = nfcmrvl_i2c_nci_open,
0148     .nci_close = nfcmrvl_i2c_nci_close,
0149     .nci_send = nfcmrvl_i2c_nci_send,
0150     .nci_update_config = nfcmrvl_i2c_nci_update_config,
0151 };
0152 
0153 static int nfcmrvl_i2c_parse_dt(struct device_node *node,
0154                 struct nfcmrvl_platform_data *pdata)
0155 {
0156     int ret;
0157 
0158     ret = nfcmrvl_parse_dt(node, pdata);
0159     if (ret < 0) {
0160         pr_err("Failed to get generic entries\n");
0161         return ret;
0162     }
0163 
0164     if (of_find_property(node, "i2c-int-falling", NULL))
0165         pdata->irq_polarity = IRQF_TRIGGER_FALLING;
0166     else
0167         pdata->irq_polarity = IRQF_TRIGGER_RISING;
0168 
0169     ret = irq_of_parse_and_map(node, 0);
0170     if (!ret) {
0171         pr_err("Unable to get irq\n");
0172         return -EINVAL;
0173     }
0174     pdata->irq = ret;
0175 
0176     return 0;
0177 }
0178 
0179 static int nfcmrvl_i2c_probe(struct i2c_client *client,
0180                  const struct i2c_device_id *id)
0181 {
0182     const struct nfcmrvl_platform_data *pdata;
0183     struct nfcmrvl_i2c_drv_data *drv_data;
0184     struct nfcmrvl_platform_data config;
0185     int ret;
0186 
0187     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0188         nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
0189         return -ENODEV;
0190     }
0191 
0192     drv_data = devm_kzalloc(&client->dev, sizeof(*drv_data), GFP_KERNEL);
0193     if (!drv_data)
0194         return -ENOMEM;
0195 
0196     drv_data->i2c = client;
0197     drv_data->dev = &client->dev;
0198     drv_data->priv = NULL;
0199 
0200     i2c_set_clientdata(client, drv_data);
0201 
0202     pdata = client->dev.platform_data;
0203 
0204     if (!pdata && client->dev.of_node)
0205         if (nfcmrvl_i2c_parse_dt(client->dev.of_node, &config) == 0)
0206             pdata = &config;
0207 
0208     if (!pdata)
0209         return -EINVAL;
0210 
0211     /* Request the read IRQ */
0212     ret = devm_request_threaded_irq(&drv_data->i2c->dev, pdata->irq,
0213                     NULL, nfcmrvl_i2c_int_irq_thread_fn,
0214                     pdata->irq_polarity | IRQF_ONESHOT,
0215                     "nfcmrvl_i2c_int", drv_data);
0216     if (ret < 0) {
0217         nfc_err(&drv_data->i2c->dev,
0218             "Unable to register IRQ handler\n");
0219         return ret;
0220     }
0221 
0222     drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_I2C,
0223                           drv_data, &i2c_ops,
0224                           &drv_data->i2c->dev, pdata);
0225 
0226     if (IS_ERR(drv_data->priv))
0227         return PTR_ERR(drv_data->priv);
0228 
0229     drv_data->priv->support_fw_dnld = true;
0230 
0231     return 0;
0232 }
0233 
0234 static int nfcmrvl_i2c_remove(struct i2c_client *client)
0235 {
0236     struct nfcmrvl_i2c_drv_data *drv_data = i2c_get_clientdata(client);
0237 
0238     nfcmrvl_nci_unregister_dev(drv_data->priv);
0239 
0240     return 0;
0241 }
0242 
0243 
0244 static const struct of_device_id of_nfcmrvl_i2c_match[] __maybe_unused = {
0245     { .compatible = "marvell,nfc-i2c", },
0246     {},
0247 };
0248 MODULE_DEVICE_TABLE(of, of_nfcmrvl_i2c_match);
0249 
0250 static const struct i2c_device_id nfcmrvl_i2c_id_table[] = {
0251     { "nfcmrvl_i2c", 0 },
0252     {}
0253 };
0254 MODULE_DEVICE_TABLE(i2c, nfcmrvl_i2c_id_table);
0255 
0256 static struct i2c_driver nfcmrvl_i2c_driver = {
0257     .probe = nfcmrvl_i2c_probe,
0258     .id_table = nfcmrvl_i2c_id_table,
0259     .remove = nfcmrvl_i2c_remove,
0260     .driver = {
0261         .name       = "nfcmrvl_i2c",
0262         .of_match_table = of_match_ptr(of_nfcmrvl_i2c_match),
0263     },
0264 };
0265 
0266 module_i2c_driver(nfcmrvl_i2c_driver);
0267 
0268 MODULE_AUTHOR("Marvell International Ltd.");
0269 MODULE_DESCRIPTION("Marvell NFC-over-I2C driver");
0270 MODULE_LICENSE("GPL v2");