1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell NFC-over-I2C driver: I2C interface related functions
5 * Copyright (C) 2015, Marvell International Ltd.
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/i2c.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/nfc.h>
13 #include <linux/gpio.h>
14 #include <linux/delay.h>
15 #include <linux/of_irq.h>
16 #include <linux/of_gpio.h>
17 #include <net/nfc/nci.h>
18 #include <net/nfc/nci_core.h>
21 struct nfcmrvl_i2c_drv_data {
24 struct i2c_client *i2c;
25 struct nfcmrvl_private *priv;
28 static int nfcmrvl_i2c_read(struct nfcmrvl_i2c_drv_data *drv_data,
32 struct nci_ctrl_hdr nci_hdr;
34 /* Read NCI header to know the payload size */
35 ret = i2c_master_recv(drv_data->i2c, (u8 *)&nci_hdr, NCI_CTRL_HDR_SIZE);
36 if (ret != NCI_CTRL_HDR_SIZE) {
37 nfc_err(&drv_data->i2c->dev, "cannot read NCI header\n");
41 *skb = nci_skb_alloc(drv_data->priv->ndev,
42 nci_hdr.plen + NCI_CTRL_HDR_SIZE, GFP_KERNEL);
46 /* Copy NCI header into the SKB */
47 skb_put_data(*skb, &nci_hdr, NCI_CTRL_HDR_SIZE);
50 /* Read the NCI payload */
51 ret = i2c_master_recv(drv_data->i2c,
52 skb_put(*skb, nci_hdr.plen),
55 if (ret != nci_hdr.plen) {
56 nfc_err(&drv_data->i2c->dev,
57 "Invalid frame payload length: %u (expected %u)\n",
67 static irqreturn_t nfcmrvl_i2c_int_irq_thread_fn(int irq, void *drv_data_ptr)
69 struct nfcmrvl_i2c_drv_data *drv_data = drv_data_ptr;
70 struct sk_buff *skb = NULL;
76 if (test_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags))
79 ret = nfcmrvl_i2c_read(drv_data, &skb);
83 set_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags);
87 nfc_err(&drv_data->i2c->dev, "read failed %d\n", ret);
90 if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
91 nfc_err(&drv_data->i2c->dev, "corrupted RX packet\n");
97 static int nfcmrvl_i2c_nci_open(struct nfcmrvl_private *priv)
99 struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
107 static int nfcmrvl_i2c_nci_close(struct nfcmrvl_private *priv)
112 static int nfcmrvl_i2c_nci_send(struct nfcmrvl_private *priv,
115 struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
118 if (test_bit(NFCMRVL_PHY_ERROR, &priv->flags))
121 ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
123 /* Retry if chip was in standby */
124 if (ret == -EREMOTEIO) {
125 nfc_info(drv_data->dev, "chip may sleep, retry\n");
126 usleep_range(6000, 10000);
127 ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
131 if (ret != skb->len) {
132 nfc_err(drv_data->dev,
133 "Invalid length sent: %u (expected %u)\n",
144 static void nfcmrvl_i2c_nci_update_config(struct nfcmrvl_private *priv,
149 static struct nfcmrvl_if_ops i2c_ops = {
150 .nci_open = nfcmrvl_i2c_nci_open,
151 .nci_close = nfcmrvl_i2c_nci_close,
152 .nci_send = nfcmrvl_i2c_nci_send,
153 .nci_update_config = nfcmrvl_i2c_nci_update_config,
156 static int nfcmrvl_i2c_parse_dt(struct device_node *node,
157 struct nfcmrvl_platform_data *pdata)
161 ret = nfcmrvl_parse_dt(node, pdata);
163 pr_err("Failed to get generic entries\n");
167 if (of_find_property(node, "i2c-int-falling", NULL))
168 pdata->irq_polarity = IRQF_TRIGGER_FALLING;
170 pdata->irq_polarity = IRQF_TRIGGER_RISING;
172 ret = irq_of_parse_and_map(node, 0);
174 pr_err("Unable to get irq, error: %d\n", ret);
182 static int nfcmrvl_i2c_probe(struct i2c_client *client,
183 const struct i2c_device_id *id)
185 struct nfcmrvl_i2c_drv_data *drv_data;
186 struct nfcmrvl_platform_data *pdata;
187 struct nfcmrvl_platform_data config;
190 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
191 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
195 drv_data = devm_kzalloc(&client->dev, sizeof(*drv_data), GFP_KERNEL);
199 drv_data->i2c = client;
200 drv_data->dev = &client->dev;
201 drv_data->priv = NULL;
203 i2c_set_clientdata(client, drv_data);
205 pdata = client->dev.platform_data;
207 if (!pdata && client->dev.of_node)
208 if (nfcmrvl_i2c_parse_dt(client->dev.of_node, &config) == 0)
214 /* Request the read IRQ */
215 ret = devm_request_threaded_irq(&drv_data->i2c->dev, pdata->irq,
216 NULL, nfcmrvl_i2c_int_irq_thread_fn,
217 pdata->irq_polarity | IRQF_ONESHOT,
218 "nfcmrvl_i2c_int", drv_data);
220 nfc_err(&drv_data->i2c->dev,
221 "Unable to register IRQ handler\n");
225 drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_I2C,
227 &drv_data->i2c->dev, pdata);
229 if (IS_ERR(drv_data->priv))
230 return PTR_ERR(drv_data->priv);
232 drv_data->priv->support_fw_dnld = true;
237 static int nfcmrvl_i2c_remove(struct i2c_client *client)
239 struct nfcmrvl_i2c_drv_data *drv_data = i2c_get_clientdata(client);
241 nfcmrvl_nci_unregister_dev(drv_data->priv);
247 static const struct of_device_id of_nfcmrvl_i2c_match[] __maybe_unused = {
248 { .compatible = "marvell,nfc-i2c", },
251 MODULE_DEVICE_TABLE(of, of_nfcmrvl_i2c_match);
253 static const struct i2c_device_id nfcmrvl_i2c_id_table[] = {
254 { "nfcmrvl_i2c", 0 },
257 MODULE_DEVICE_TABLE(i2c, nfcmrvl_i2c_id_table);
259 static struct i2c_driver nfcmrvl_i2c_driver = {
260 .probe = nfcmrvl_i2c_probe,
261 .id_table = nfcmrvl_i2c_id_table,
262 .remove = nfcmrvl_i2c_remove,
264 .name = "nfcmrvl_i2c",
265 .of_match_table = of_match_ptr(of_nfcmrvl_i2c_match),
269 module_i2c_driver(nfcmrvl_i2c_driver);
271 MODULE_AUTHOR("Marvell International Ltd.");
272 MODULE_DESCRIPTION("Marvell NFC-over-I2C driver");
273 MODULE_LICENSE("GPL v2");