]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/nfc/st-nci/i2c.c
NFC: st-nci: Covert to use GPIO descriptor
[karo-tx-linux.git] / drivers / nfc / st-nci / i2c.c
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/consumer.h>
23 #include <linux/acpi.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/nfc.h>
27 #include <linux/of.h>
28
29 #include "st-nci.h"
30
31 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
32
33 /* ndlc header */
34 #define ST_NCI_FRAME_HEADROOM 1
35 #define ST_NCI_FRAME_TAILROOM 0
36
37 #define ST_NCI_I2C_MIN_SIZE 4   /* PCB(1) + NCI Packet header(3) */
38 #define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
39
40 #define ST_NCI_DRIVER_NAME "st_nci"
41 #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
42
43 #define ST_NCI_GPIO_NAME_RESET "reset"
44
45 struct st_nci_i2c_phy {
46         struct i2c_client *i2c_dev;
47         struct llt_ndlc *ndlc;
48
49         bool irq_active;
50
51         struct gpio_desc *gpiod_reset;
52
53         struct st_nci_se_status se_status;
54 };
55
56 static int st_nci_i2c_enable(void *phy_id)
57 {
58         struct st_nci_i2c_phy *phy = phy_id;
59
60         gpiod_set_value(phy->gpiod_reset, 0);
61         usleep_range(10000, 15000);
62         gpiod_set_value(phy->gpiod_reset, 1);
63         usleep_range(80000, 85000);
64
65         if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
66                 enable_irq(phy->i2c_dev->irq);
67                 phy->irq_active = true;
68         }
69
70         return 0;
71 }
72
73 static void st_nci_i2c_disable(void *phy_id)
74 {
75         struct st_nci_i2c_phy *phy = phy_id;
76
77         disable_irq_nosync(phy->i2c_dev->irq);
78         phy->irq_active = false;
79 }
80
81 /*
82  * Writing a frame must not return the number of written bytes.
83  * It must return either zero for success, or <0 for error.
84  * In addition, it must not alter the skb
85  */
86 static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
87 {
88         int r = -1;
89         struct st_nci_i2c_phy *phy = phy_id;
90         struct i2c_client *client = phy->i2c_dev;
91
92         if (phy->ndlc->hard_fault != 0)
93                 return phy->ndlc->hard_fault;
94
95         r = i2c_master_send(client, skb->data, skb->len);
96         if (r < 0) {  /* Retry, chip was in standby */
97                 usleep_range(1000, 4000);
98                 r = i2c_master_send(client, skb->data, skb->len);
99         }
100
101         if (r >= 0) {
102                 if (r != skb->len)
103                         r = -EREMOTEIO;
104                 else
105                         r = 0;
106         }
107
108         return r;
109 }
110
111 /*
112  * Reads an ndlc frame and returns it in a newly allocated sk_buff.
113  * returns:
114  * 0 : if received frame is complete
115  * -EREMOTEIO : i2c read error (fatal)
116  * -EBADMSG : frame was incorrect and discarded
117  * -ENOMEM : cannot allocate skb, frame dropped
118  */
119 static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
120                                  struct sk_buff **skb)
121 {
122         int r;
123         u8 len;
124         u8 buf[ST_NCI_I2C_MAX_SIZE];
125         struct i2c_client *client = phy->i2c_dev;
126
127         r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
128         if (r < 0) {  /* Retry, chip was in standby */
129                 usleep_range(1000, 4000);
130                 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
131         }
132
133         if (r != ST_NCI_I2C_MIN_SIZE)
134                 return -EREMOTEIO;
135
136         len = be16_to_cpu(*(__be16 *) (buf + 2));
137         if (len > ST_NCI_I2C_MAX_SIZE) {
138                 nfc_err(&client->dev, "invalid frame len\n");
139                 return -EBADMSG;
140         }
141
142         *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
143         if (*skb == NULL)
144                 return -ENOMEM;
145
146         skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
147         skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
148         memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
149
150         if (!len)
151                 return 0;
152
153         r = i2c_master_recv(client, buf, len);
154         if (r != len) {
155                 kfree_skb(*skb);
156                 return -EREMOTEIO;
157         }
158
159         skb_put(*skb, len);
160         memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
161
162         return 0;
163 }
164
165 /*
166  * Reads an ndlc frame from the chip.
167  *
168  * On ST_NCI, IRQ goes in idle state when read starts.
169  */
170 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
171 {
172         struct st_nci_i2c_phy *phy = phy_id;
173         struct i2c_client *client;
174         struct sk_buff *skb = NULL;
175         int r;
176
177         if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
178                 WARN_ON_ONCE(1);
179                 return IRQ_NONE;
180         }
181
182         client = phy->i2c_dev;
183         dev_dbg(&client->dev, "IRQ\n");
184
185         if (phy->ndlc->hard_fault)
186                 return IRQ_HANDLED;
187
188         if (!phy->ndlc->powered) {
189                 st_nci_i2c_disable(phy);
190                 return IRQ_HANDLED;
191         }
192
193         r = st_nci_i2c_read(phy, &skb);
194         if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
195                 return IRQ_HANDLED;
196
197         ndlc_recv(phy->ndlc, skb);
198
199         return IRQ_HANDLED;
200 }
201
202 static struct nfc_phy_ops i2c_phy_ops = {
203         .write = st_nci_i2c_write,
204         .enable = st_nci_i2c_enable,
205         .disable = st_nci_i2c_disable,
206 };
207
208 static int st_nci_i2c_acpi_request_resources(struct i2c_client *client)
209 {
210         struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
211         struct device *dev = &client->dev;
212         u8 tmp;
213
214         /* Get RESET GPIO from ACPI */
215         phy->gpiod_reset = devm_gpiod_get_index(dev, ST_NCI_GPIO_NAME_RESET,
216                                                 1, GPIOD_OUT_HIGH);
217         if (IS_ERR(phy->gpiod_reset)) {
218                 nfc_err(dev, "Unable to get RESET GPIO\n");
219                 return -ENODEV;
220         }
221
222         phy->se_status.is_ese_present = false;
223         phy->se_status.is_uicc_present = false;
224
225         if (device_property_present(dev, "ese-present")) {
226                 device_property_read_u8(dev, "ese-present", &tmp);
227                 phy->se_status.is_ese_present = tmp;
228         }
229
230         if (device_property_present(dev, "uicc-present")) {
231                 device_property_read_u8(dev, "uicc-present", &tmp);
232                 phy->se_status.is_uicc_present = tmp;
233         }
234
235         return 0;
236 }
237
238 static int st_nci_i2c_of_request_resources(struct i2c_client *client)
239 {
240         struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
241         struct device *dev = &client->dev;
242         struct device_node *pp;
243
244         pp = client->dev.of_node;
245         if (!pp)
246                 return -ENODEV;
247
248         /* Get GPIO from device tree */
249         phy->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
250         if (IS_ERR(phy->gpiod_reset)) {
251                 nfc_err(dev, "Unable to get RESET GPIO\n");
252                 return PTR_ERR(phy->gpiod_reset);
253         }
254
255         phy->se_status.is_ese_present =
256                                 of_property_read_bool(pp, "ese-present");
257         phy->se_status.is_uicc_present =
258                                 of_property_read_bool(pp, "uicc-present");
259
260         return 0;
261 }
262
263 static int st_nci_i2c_probe(struct i2c_client *client,
264                                   const struct i2c_device_id *id)
265 {
266         struct st_nci_i2c_phy *phy;
267         int r;
268
269         dev_dbg(&client->dev, "%s\n", __func__);
270         dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
271
272         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
273                 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
274                 return -ENODEV;
275         }
276
277         phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy),
278                            GFP_KERNEL);
279         if (!phy)
280                 return -ENOMEM;
281
282         phy->i2c_dev = client;
283
284         i2c_set_clientdata(client, phy);
285
286         if (client->dev.of_node) {
287                 r = st_nci_i2c_of_request_resources(client);
288                 if (r) {
289                         nfc_err(&client->dev, "No platform data\n");
290                         return r;
291                 }
292         } else if (ACPI_HANDLE(&client->dev)) {
293                 r = st_nci_i2c_acpi_request_resources(client);
294                 if (r) {
295                         nfc_err(&client->dev, "Cannot get ACPI data\n");
296                         return r;
297                 }
298         } else {
299                 nfc_err(&client->dev,
300                         "st_nci platform resources not available\n");
301                 return -ENODEV;
302         }
303
304         r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
305                         ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
306                         &phy->ndlc, &phy->se_status);
307         if (r < 0) {
308                 nfc_err(&client->dev, "Unable to register ndlc layer\n");
309                 return r;
310         }
311
312         phy->irq_active = true;
313         r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
314                                 st_nci_irq_thread_fn,
315                                 IRQF_ONESHOT,
316                                 ST_NCI_DRIVER_NAME, phy);
317         if (r < 0)
318                 nfc_err(&client->dev, "Unable to register IRQ handler\n");
319
320         return r;
321 }
322
323 static int st_nci_i2c_remove(struct i2c_client *client)
324 {
325         struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
326
327         dev_dbg(&client->dev, "%s\n", __func__);
328
329         ndlc_remove(phy->ndlc);
330
331         return 0;
332 }
333
334 static struct i2c_device_id st_nci_i2c_id_table[] = {
335         {ST_NCI_DRIVER_NAME, 0},
336         {}
337 };
338 MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
339
340 static const struct acpi_device_id st_nci_i2c_acpi_match[] = {
341         {"SMO2101"},
342         {"SMO2102"},
343         {}
344 };
345 MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
346
347 static const struct of_device_id of_st_nci_i2c_match[] = {
348         { .compatible = "st,st21nfcb-i2c", },
349         { .compatible = "st,st21nfcb_i2c", },
350         { .compatible = "st,st21nfcc-i2c", },
351         {}
352 };
353 MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
354
355 static struct i2c_driver st_nci_i2c_driver = {
356         .driver = {
357                 .name = ST_NCI_I2C_DRIVER_NAME,
358                 .of_match_table = of_match_ptr(of_st_nci_i2c_match),
359                 .acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
360         },
361         .probe = st_nci_i2c_probe,
362         .id_table = st_nci_i2c_id_table,
363         .remove = st_nci_i2c_remove,
364 };
365 module_i2c_driver(st_nci_i2c_driver);
366
367 MODULE_LICENSE("GPL");
368 MODULE_DESCRIPTION(DRIVER_DESC);