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