1
2
3
4
5
6
7
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>
19#include "nfcmrvl.h"
20
21struct nfcmrvl_i2c_drv_data {
22 unsigned long flags;
23 struct device *dev;
24 struct i2c_client *i2c;
25 struct nfcmrvl_private *priv;
26};
27
28static int nfcmrvl_i2c_read(struct nfcmrvl_i2c_drv_data *drv_data,
29 struct sk_buff **skb)
30{
31 int ret;
32 struct nci_ctrl_hdr nci_hdr;
33
34
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");
38 return -EBADMSG;
39 }
40
41 *skb = nci_skb_alloc(drv_data->priv->ndev,
42 nci_hdr.plen + NCI_CTRL_HDR_SIZE, GFP_KERNEL);
43 if (!*skb)
44 return -ENOMEM;
45
46
47 skb_put_data(*skb, &nci_hdr, NCI_CTRL_HDR_SIZE);
48
49 if (nci_hdr.plen) {
50
51 ret = i2c_master_recv(drv_data->i2c,
52 skb_put(*skb, nci_hdr.plen),
53 nci_hdr.plen);
54
55 if (ret != nci_hdr.plen) {
56 nfc_err(&drv_data->i2c->dev,
57 "Invalid frame payload length: %u (expected %u)\n",
58 ret, nci_hdr.plen);
59 kfree_skb(*skb);
60 return -EBADMSG;
61 }
62 }
63
64 return 0;
65}
66
67static irqreturn_t nfcmrvl_i2c_int_irq_thread_fn(int irq, void *drv_data_ptr)
68{
69 struct nfcmrvl_i2c_drv_data *drv_data = drv_data_ptr;
70 struct sk_buff *skb = NULL;
71 int ret;
72
73 if (!drv_data->priv)
74 return IRQ_HANDLED;
75
76 if (test_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags))
77 return IRQ_HANDLED;
78
79 ret = nfcmrvl_i2c_read(drv_data, &skb);
80
81 switch (ret) {
82 case -EREMOTEIO:
83 set_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags);
84 break;
85 case -ENOMEM:
86 case -EBADMSG:
87 nfc_err(&drv_data->i2c->dev, "read failed %d\n", ret);
88 break;
89 default:
90 if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
91 nfc_err(&drv_data->i2c->dev, "corrupted RX packet\n");
92 break;
93 }
94 return IRQ_HANDLED;
95}
96
97static int nfcmrvl_i2c_nci_open(struct nfcmrvl_private *priv)
98{
99 struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
100
101 if (!drv_data)
102 return -ENODEV;
103
104 return 0;
105}
106
107static int nfcmrvl_i2c_nci_close(struct nfcmrvl_private *priv)
108{
109 return 0;
110}
111
112static int nfcmrvl_i2c_nci_send(struct nfcmrvl_private *priv,
113 struct sk_buff *skb)
114{
115 struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data;
116 int ret;
117
118 if (test_bit(NFCMRVL_PHY_ERROR, &priv->flags))
119 return -EREMOTEIO;
120
121 ret = i2c_master_send(drv_data->i2c, skb->data, skb->len);
122
123
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);
128 }
129
130 if (ret >= 0) {
131 if (ret != skb->len) {
132 nfc_err(drv_data->dev,
133 "Invalid length sent: %u (expected %u)\n",
134 ret, skb->len);
135 ret = -EREMOTEIO;
136 } else
137 ret = 0;
138 kfree_skb(skb);
139 }
140
141 return ret;
142}
143
144static void nfcmrvl_i2c_nci_update_config(struct nfcmrvl_private *priv,
145 const void *param)
146{
147}
148
149static 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,
154};
155
156static int nfcmrvl_i2c_parse_dt(struct device_node *node,
157 struct nfcmrvl_platform_data *pdata)
158{
159 int ret;
160
161 ret = nfcmrvl_parse_dt(node, pdata);
162 if (ret < 0) {
163 pr_err("Failed to get generic entries\n");
164 return ret;
165 }
166
167 if (of_find_property(node, "i2c-int-falling", NULL))
168 pdata->irq_polarity = IRQF_TRIGGER_FALLING;
169 else
170 pdata->irq_polarity = IRQF_TRIGGER_RISING;
171
172 ret = irq_of_parse_and_map(node, 0);
173 if (ret < 0) {
174 pr_err("Unable to get irq, error: %d\n", ret);
175 return ret;
176 }
177 pdata->irq = ret;
178
179 return 0;
180}
181
182static int nfcmrvl_i2c_probe(struct i2c_client *client,
183 const struct i2c_device_id *id)
184{
185 struct nfcmrvl_i2c_drv_data *drv_data;
186 struct nfcmrvl_platform_data *pdata;
187 struct nfcmrvl_platform_data config;
188 int ret;
189
190 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
191 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
192 return -ENODEV;
193 }
194
195 drv_data = devm_kzalloc(&client->dev, sizeof(*drv_data), GFP_KERNEL);
196 if (!drv_data)
197 return -ENOMEM;
198
199 drv_data->i2c = client;
200 drv_data->dev = &client->dev;
201 drv_data->priv = NULL;
202
203 i2c_set_clientdata(client, drv_data);
204
205 pdata = client->dev.platform_data;
206
207 if (!pdata && client->dev.of_node)
208 if (nfcmrvl_i2c_parse_dt(client->dev.of_node, &config) == 0)
209 pdata = &config;
210
211 if (!pdata)
212 return -EINVAL;
213
214
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);
219 if (ret < 0) {
220 nfc_err(&drv_data->i2c->dev,
221 "Unable to register IRQ handler\n");
222 return ret;
223 }
224
225 drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_I2C,
226 drv_data, &i2c_ops,
227 &drv_data->i2c->dev, pdata);
228
229 if (IS_ERR(drv_data->priv))
230 return PTR_ERR(drv_data->priv);
231
232 drv_data->priv->support_fw_dnld = true;
233
234 return 0;
235}
236
237static int nfcmrvl_i2c_remove(struct i2c_client *client)
238{
239 struct nfcmrvl_i2c_drv_data *drv_data = i2c_get_clientdata(client);
240
241 nfcmrvl_nci_unregister_dev(drv_data->priv);
242
243 return 0;
244}
245
246
247static const struct of_device_id of_nfcmrvl_i2c_match[] __maybe_unused = {
248 { .compatible = "marvell,nfc-i2c", },
249 {},
250};
251MODULE_DEVICE_TABLE(of, of_nfcmrvl_i2c_match);
252
253static const struct i2c_device_id nfcmrvl_i2c_id_table[] = {
254 { "nfcmrvl_i2c", 0 },
255 {}
256};
257MODULE_DEVICE_TABLE(i2c, nfcmrvl_i2c_id_table);
258
259static struct i2c_driver nfcmrvl_i2c_driver = {
260 .probe = nfcmrvl_i2c_probe,
261 .id_table = nfcmrvl_i2c_id_table,
262 .remove = nfcmrvl_i2c_remove,
263 .driver = {
264 .name = "nfcmrvl_i2c",
265 .of_match_table = of_match_ptr(of_nfcmrvl_i2c_match),
266 },
267};
268
269module_i2c_driver(nfcmrvl_i2c_driver);
270
271MODULE_AUTHOR("Marvell International Ltd.");
272MODULE_DESCRIPTION("Marvell NFC-over-I2C driver");
273MODULE_LICENSE("GPL v2");
274