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/nfc.h>
12 #include <linux/delay.h>
13 #include <linux/of_irq.h>
14 #include <net/nfc/nci.h>
15 #include <net/nfc/nci_core.h>
18 struct nfcmrvl_i2c_drv_data {
21 struct i2c_client *i2c;
22 struct nfcmrvl_private *priv;
25 static int nfcmrvl_i2c_read(struct nfcmrvl_i2c_drv_data *drv_data,
29 struct nci_ctrl_hdr nci_hdr;
31 /* Read NCI header to know the payload size */
32 ret = i2c_master_recv(drv_data->i2c, (u8 *)&nci_hdr, NCI_CTRL_HDR_SIZE);
33 if (ret != NCI_CTRL_HDR_SIZE) {
34 nfc_err(&drv_data->i2c->dev, "cannot read NCI header\n");
38 *skb = nci_skb_alloc(drv_data->priv->ndev,
39 nci_hdr.plen + NCI_CTRL_HDR_SIZE, GFP_KERNEL);
43 /* Copy NCI header into the SKB */
44 skb_put_data(*skb, &nci_hdr, NCI_CTRL_HDR_SIZE);
47 /* Read the NCI payload */
48 ret = i2c_master_recv(drv_data->i2c,
49 skb_put(*skb, nci_hdr.plen),
52 if (ret != nci_hdr.plen) {
53 nfc_err(&drv_data->i2c->dev,
54 "Invalid frame payload length: %u (expected %u)\n",
64 static irqreturn_t nfcmrvl_i2c_int_irq_thread_fn(int irq, void *drv_data_ptr)
66 struct nfcmrvl_i2c_drv_data *drv_data = drv_data_ptr;
67 struct sk_buff *skb = NULL;
73 if (test_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags))
76 ret = nfcmrvl_i2c_read(drv_data, &skb);
80 set_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags);
84 nfc_err(&drv_data->i2c->dev, "read failed %d\n", ret);
87 if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
88 nfc_err(&drv_data->i2c->dev, "corrupted RX packet\n");
94 static int nfcmrvl_i2c_nci_open(struct nfcmrvl_private *priv)
96 struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
104 static int nfcmrvl_i2c_nci_close(struct nfcmrvl_private *priv)
109 static int nfcmrvl_i2c_nci_send(struct nfcmrvl_private *priv,
112 struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
115 if (test_bit(NFCMRVL_PHY_ERROR, &priv->flags))
118 ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
120 /* Retry if chip was in standby */
121 if (ret == -EREMOTEIO) {
122 nfc_info(drv_data->dev, "chip may sleep, retry\n");
123 usleep_range(6000, 10000);
124 ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
128 if (ret != skb->len) {
129 nfc_err(drv_data->dev,
130 "Invalid length sent: %u (expected %u)\n",
141 static void nfcmrvl_i2c_nci_update_config(struct nfcmrvl_private *priv,
146 static const struct nfcmrvl_if_ops i2c_ops = {
147 .nci_open = nfcmrvl_i2c_nci_open,
148 .nci_close = nfcmrvl_i2c_nci_close,
149 .nci_send = nfcmrvl_i2c_nci_send,
150 .nci_update_config = nfcmrvl_i2c_nci_update_config,
153 static int nfcmrvl_i2c_parse_dt(struct device_node *node,
154 struct nfcmrvl_platform_data *pdata)
158 ret = nfcmrvl_parse_dt(node, pdata);
160 pr_err("Failed to get generic entries\n");
164 if (of_find_property(node, "i2c-int-falling", NULL))
165 pdata->irq_polarity = IRQF_TRIGGER_FALLING;
167 pdata->irq_polarity = IRQF_TRIGGER_RISING;
169 ret = irq_of_parse_and_map(node, 0);
171 pr_err("Unable to get irq, error: %d\n", ret);
179 static int nfcmrvl_i2c_probe(struct i2c_client *client,
180 const struct i2c_device_id *id)
182 const struct nfcmrvl_platform_data *pdata;
183 struct nfcmrvl_i2c_drv_data *drv_data;
184 struct nfcmrvl_platform_data config;
187 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
188 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
192 drv_data = devm_kzalloc(&client->dev, sizeof(*drv_data), GFP_KERNEL);
196 drv_data->i2c = client;
197 drv_data->dev = &client->dev;
198 drv_data->priv = NULL;
200 i2c_set_clientdata(client, drv_data);
202 pdata = client->dev.platform_data;
204 if (!pdata && client->dev.of_node)
205 if (nfcmrvl_i2c_parse_dt(client->dev.of_node, &config) == 0)
211 /* Request the read IRQ */
212 ret = devm_request_threaded_irq(&drv_data->i2c->dev, pdata->irq,
213 NULL, nfcmrvl_i2c_int_irq_thread_fn,
214 pdata->irq_polarity | IRQF_ONESHOT,
215 "nfcmrvl_i2c_int", drv_data);
217 nfc_err(&drv_data->i2c->dev,
218 "Unable to register IRQ handler\n");
222 drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_I2C,
224 &drv_data->i2c->dev, pdata);
226 if (IS_ERR(drv_data->priv))
227 return PTR_ERR(drv_data->priv);
229 drv_data->priv->support_fw_dnld = true;
234 static int nfcmrvl_i2c_remove(struct i2c_client *client)
236 struct nfcmrvl_i2c_drv_data *drv_data = i2c_get_clientdata(client);
238 nfcmrvl_nci_unregister_dev(drv_data->priv);
244 static const struct of_device_id of_nfcmrvl_i2c_match[] __maybe_unused = {
245 { .compatible = "marvell,nfc-i2c", },
248 MODULE_DEVICE_TABLE(of, of_nfcmrvl_i2c_match);
250 static const struct i2c_device_id nfcmrvl_i2c_id_table[] = {
251 { "nfcmrvl_i2c", 0 },
254 MODULE_DEVICE_TABLE(i2c, nfcmrvl_i2c_id_table);
256 static struct i2c_driver nfcmrvl_i2c_driver = {
257 .probe = nfcmrvl_i2c_probe,
258 .id_table = nfcmrvl_i2c_id_table,
259 .remove = nfcmrvl_i2c_remove,
261 .name = "nfcmrvl_i2c",
262 .of_match_table = of_match_ptr(of_nfcmrvl_i2c_match),
266 module_i2c_driver(nfcmrvl_i2c_driver);
268 MODULE_AUTHOR("Marvell International Ltd.");
269 MODULE_DESCRIPTION("Marvell NFC-over-I2C driver");
270 MODULE_LICENSE("GPL v2");