1 /*
2 * I2C Link Layer for ST NCI NFC controller familly based Driver
3 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/gpio.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_gpio.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/nfc.h>
28 #include <linux/platform_data/st-nci.h>
29
30 #include "st-nci.h"
31
32 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
33
34 /* ndlc header */
35 #define ST_NCI_FRAME_HEADROOM 1
36 #define ST_NCI_FRAME_TAILROOM 0
37
38 #define ST_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
39 #define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
40
41 #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
42
43 static struct i2c_device_id st_nci_i2c_id_table[] = {
44 {ST_NCI_DRIVER_NAME, 0},
45 {}
46 };
47 MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
48
49 struct st_nci_i2c_phy {
50 struct i2c_client *i2c_dev;
51 struct llt_ndlc *ndlc;
52
53 bool irq_active;
54
55 unsigned int gpio_reset;
56 unsigned int irq_polarity;
57
58 struct st_nci_se_status se_status;
59 };
60
st_nci_i2c_enable(void * phy_id)61 static int st_nci_i2c_enable(void *phy_id)
62 {
63 struct st_nci_i2c_phy *phy = phy_id;
64
65 gpio_set_value(phy->gpio_reset, 0);
66 usleep_range(10000, 15000);
67 gpio_set_value(phy->gpio_reset, 1);
68 usleep_range(80000, 85000);
69
70 if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
71 enable_irq(phy->i2c_dev->irq);
72 phy->irq_active = true;
73 }
74
75 return 0;
76 }
77
st_nci_i2c_disable(void * phy_id)78 static void st_nci_i2c_disable(void *phy_id)
79 {
80 struct st_nci_i2c_phy *phy = phy_id;
81
82 disable_irq_nosync(phy->i2c_dev->irq);
83 phy->irq_active = false;
84 }
85
86 /*
87 * Writing a frame must not return the number of written bytes.
88 * It must return either zero for success, or <0 for error.
89 * In addition, it must not alter the skb
90 */
st_nci_i2c_write(void * phy_id,struct sk_buff * skb)91 static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
92 {
93 int r = -1;
94 struct st_nci_i2c_phy *phy = phy_id;
95 struct i2c_client *client = phy->i2c_dev;
96
97 if (phy->ndlc->hard_fault != 0)
98 return phy->ndlc->hard_fault;
99
100 r = i2c_master_send(client, skb->data, skb->len);
101 if (r < 0) { /* Retry, chip was in standby */
102 usleep_range(1000, 4000);
103 r = i2c_master_send(client, skb->data, skb->len);
104 }
105
106 if (r >= 0) {
107 if (r != skb->len)
108 r = -EREMOTEIO;
109 else
110 r = 0;
111 }
112
113 return r;
114 }
115
116 /*
117 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
118 * returns:
119 * 0 : if received frame is complete
120 * -EREMOTEIO : i2c read error (fatal)
121 * -EBADMSG : frame was incorrect and discarded
122 * -ENOMEM : cannot allocate skb, frame dropped
123 */
st_nci_i2c_read(struct st_nci_i2c_phy * phy,struct sk_buff ** skb)124 static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
125 struct sk_buff **skb)
126 {
127 int r;
128 u8 len;
129 u8 buf[ST_NCI_I2C_MAX_SIZE];
130 struct i2c_client *client = phy->i2c_dev;
131
132 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
133 if (r < 0) { /* Retry, chip was in standby */
134 usleep_range(1000, 4000);
135 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
136 }
137
138 if (r != ST_NCI_I2C_MIN_SIZE)
139 return -EREMOTEIO;
140
141 len = be16_to_cpu(*(__be16 *) (buf + 2));
142 if (len > ST_NCI_I2C_MAX_SIZE) {
143 nfc_err(&client->dev, "invalid frame len\n");
144 return -EBADMSG;
145 }
146
147 *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
148 if (*skb == NULL)
149 return -ENOMEM;
150
151 skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
152 skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
153 memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
154
155 if (!len)
156 return 0;
157
158 r = i2c_master_recv(client, buf, len);
159 if (r != len) {
160 kfree_skb(*skb);
161 return -EREMOTEIO;
162 }
163
164 skb_put(*skb, len);
165 memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
166
167 return 0;
168 }
169
170 /*
171 * Reads an ndlc frame from the chip.
172 *
173 * On ST_NCI, IRQ goes in idle state when read starts.
174 */
st_nci_irq_thread_fn(int irq,void * phy_id)175 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
176 {
177 struct st_nci_i2c_phy *phy = phy_id;
178 struct i2c_client *client;
179 struct sk_buff *skb = NULL;
180 int r;
181
182 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
183 WARN_ON_ONCE(1);
184 return IRQ_NONE;
185 }
186
187 client = phy->i2c_dev;
188 dev_dbg(&client->dev, "IRQ\n");
189
190 if (phy->ndlc->hard_fault)
191 return IRQ_HANDLED;
192
193 if (!phy->ndlc->powered) {
194 st_nci_i2c_disable(phy);
195 return IRQ_HANDLED;
196 }
197
198 r = st_nci_i2c_read(phy, &skb);
199 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
200 return IRQ_HANDLED;
201
202 ndlc_recv(phy->ndlc, skb);
203
204 return IRQ_HANDLED;
205 }
206
207 static struct nfc_phy_ops i2c_phy_ops = {
208 .write = st_nci_i2c_write,
209 .enable = st_nci_i2c_enable,
210 .disable = st_nci_i2c_disable,
211 };
212
213 #ifdef CONFIG_OF
st_nci_i2c_of_request_resources(struct i2c_client * client)214 static int st_nci_i2c_of_request_resources(struct i2c_client *client)
215 {
216 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
217 struct device_node *pp;
218 int gpio;
219 int r;
220
221 pp = client->dev.of_node;
222 if (!pp)
223 return -ENODEV;
224
225 /* Get GPIO from device tree */
226 gpio = of_get_named_gpio(pp, "reset-gpios", 0);
227 if (gpio < 0) {
228 nfc_err(&client->dev,
229 "Failed to retrieve reset-gpios from device tree\n");
230 return gpio;
231 }
232
233 /* GPIO request and configuration */
234 r = devm_gpio_request_one(&client->dev, gpio,
235 GPIOF_OUT_INIT_HIGH, "clf_reset");
236 if (r) {
237 nfc_err(&client->dev, "Failed to request reset pin\n");
238 return r;
239 }
240 phy->gpio_reset = gpio;
241
242 phy->irq_polarity = irq_get_trigger_type(client->irq);
243
244 phy->se_status.is_ese_present =
245 of_property_read_bool(pp, "ese-present");
246 phy->se_status.is_uicc_present =
247 of_property_read_bool(pp, "uicc-present");
248
249 return 0;
250 }
251 #else
st_nci_i2c_of_request_resources(struct i2c_client * client)252 static int st_nci_i2c_of_request_resources(struct i2c_client *client)
253 {
254 return -ENODEV;
255 }
256 #endif
257
st_nci_i2c_request_resources(struct i2c_client * client)258 static int st_nci_i2c_request_resources(struct i2c_client *client)
259 {
260 struct st_nci_nfc_platform_data *pdata;
261 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
262 int r;
263
264 pdata = client->dev.platform_data;
265 if (pdata == NULL) {
266 nfc_err(&client->dev, "No platform data\n");
267 return -EINVAL;
268 }
269
270 /* store for later use */
271 phy->gpio_reset = pdata->gpio_reset;
272 phy->irq_polarity = pdata->irq_polarity;
273
274 r = devm_gpio_request_one(&client->dev,
275 phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
276 if (r) {
277 pr_err("%s : reset gpio_request failed\n", __FILE__);
278 return r;
279 }
280
281 phy->se_status.is_ese_present = pdata->is_ese_present;
282 phy->se_status.is_uicc_present = pdata->is_uicc_present;
283
284 return 0;
285 }
286
st_nci_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)287 static int st_nci_i2c_probe(struct i2c_client *client,
288 const struct i2c_device_id *id)
289 {
290 struct st_nci_i2c_phy *phy;
291 struct st_nci_nfc_platform_data *pdata;
292 int r;
293
294 dev_dbg(&client->dev, "%s\n", __func__);
295 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
296
297 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
298 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
299 return -ENODEV;
300 }
301
302 phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy),
303 GFP_KERNEL);
304 if (!phy)
305 return -ENOMEM;
306
307 phy->i2c_dev = client;
308
309 i2c_set_clientdata(client, phy);
310
311 pdata = client->dev.platform_data;
312 if (!pdata && client->dev.of_node) {
313 r = st_nci_i2c_of_request_resources(client);
314 if (r) {
315 nfc_err(&client->dev, "No platform data\n");
316 return r;
317 }
318 } else if (pdata) {
319 r = st_nci_i2c_request_resources(client);
320 if (r) {
321 nfc_err(&client->dev,
322 "Cannot get platform resources\n");
323 return r;
324 }
325 } else {
326 nfc_err(&client->dev,
327 "st_nci platform resources not available\n");
328 return -ENODEV;
329 }
330
331 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
332 ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
333 &phy->ndlc, &phy->se_status);
334 if (r < 0) {
335 nfc_err(&client->dev, "Unable to register ndlc layer\n");
336 return r;
337 }
338
339 phy->irq_active = true;
340 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
341 st_nci_irq_thread_fn,
342 phy->irq_polarity | IRQF_ONESHOT,
343 ST_NCI_DRIVER_NAME, phy);
344 if (r < 0)
345 nfc_err(&client->dev, "Unable to register IRQ handler\n");
346
347 return r;
348 }
349
st_nci_i2c_remove(struct i2c_client * client)350 static int st_nci_i2c_remove(struct i2c_client *client)
351 {
352 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
353
354 dev_dbg(&client->dev, "%s\n", __func__);
355
356 ndlc_remove(phy->ndlc);
357
358 return 0;
359 }
360
361 #ifdef CONFIG_OF
362 static const struct of_device_id of_st_nci_i2c_match[] = {
363 { .compatible = "st,st21nfcb-i2c", },
364 { .compatible = "st,st21nfcb_i2c", },
365 { .compatible = "st,st21nfcc-i2c", },
366 {}
367 };
368 MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
369 #endif
370
371 static struct i2c_driver st_nci_i2c_driver = {
372 .driver = {
373 .owner = THIS_MODULE,
374 .name = ST_NCI_I2C_DRIVER_NAME,
375 .of_match_table = of_match_ptr(of_st_nci_i2c_match),
376 },
377 .probe = st_nci_i2c_probe,
378 .id_table = st_nci_i2c_id_table,
379 .remove = st_nci_i2c_remove,
380 };
381
382 module_i2c_driver(st_nci_i2c_driver);
383
384 MODULE_LICENSE("GPL");
385 MODULE_DESCRIPTION(DRIVER_DESC);
386