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