]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/nfc/nxp-nci/i2c.c
Merge branch 'fixes' of git://ftp.arm.linux.org.uk/~rmk/linux-arm
[karo-tx-linux.git] / drivers / nfc / nxp-nci / i2c.c
1 /*
2  * I2C link layer for the NXP NCI driver
3  *
4  * Copyright (C) 2014  NXP Semiconductors  All rights reserved.
5  * Copyright (C) 2012-2015  Intel Corporation. All rights reserved.
6  *
7  * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
8  * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
9  *
10  * Derived from PN544 device driver:
11  * Copyright (C) 2012  Intel Corporation. All rights reserved.
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms and conditions of the GNU General Public License,
15  * version 2, as published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, see <http://www.gnu.org/licenses/>.
24  */
25
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28 #include <linux/acpi.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/interrupt.h>
32 #include <linux/miscdevice.h>
33 #include <linux/module.h>
34 #include <linux/nfc.h>
35 #include <linux/gpio/consumer.h>
36 #include <linux/of_gpio.h>
37 #include <linux/of_irq.h>
38 #include <linux/platform_data/nxp-nci.h>
39 #include <linux/unaligned/access_ok.h>
40
41 #include <net/nfc/nfc.h>
42
43 #include "nxp-nci.h"
44
45 #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
46
47 #define NXP_NCI_I2C_MAX_PAYLOAD 32
48
49 struct nxp_nci_i2c_phy {
50         struct i2c_client *i2c_dev;
51         struct nci_dev *ndev;
52
53         unsigned int gpio_en;
54         unsigned int gpio_fw;
55         unsigned int gpio_irq;
56
57         int hard_fault; /*
58                          * < 0 if hardware error occurred (e.g. i2c err)
59                          * and prevents normal operation.
60                          */
61 };
62
63 static int nxp_nci_i2c_set_mode(void *phy_id,
64                                     enum nxp_nci_mode mode)
65 {
66         struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
67
68         gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
69         gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
70         usleep_range(10000, 15000);
71
72         if (mode == NXP_NCI_MODE_COLD)
73                 phy->hard_fault = 0;
74
75         return 0;
76 }
77
78 static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
79 {
80         int r;
81         struct nxp_nci_i2c_phy *phy = phy_id;
82         struct i2c_client *client = phy->i2c_dev;
83
84         if (phy->hard_fault != 0)
85                 return phy->hard_fault;
86
87         r = i2c_master_send(client, skb->data, skb->len);
88         if (r == -EREMOTEIO) {
89                 /* Retry, chip was in standby */
90                 usleep_range(110000, 120000);
91                 r = i2c_master_send(client, skb->data, skb->len);
92         }
93
94         if (r < 0) {
95                 nfc_err(&client->dev, "Error %d on I2C send\n", r);
96         } else if (r != skb->len) {
97                 nfc_err(&client->dev,
98                         "Invalid length sent: %u (expected %u)\n",
99                         r, skb->len);
100                 r = -EREMOTEIO;
101         } else {
102                 /* Success but return 0 and not number of bytes */
103                 r = 0;
104         }
105
106         return r;
107 }
108
109 static struct nxp_nci_phy_ops i2c_phy_ops = {
110         .set_mode = nxp_nci_i2c_set_mode,
111         .write = nxp_nci_i2c_write,
112 };
113
114 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
115                                struct sk_buff **skb)
116 {
117         struct i2c_client *client = phy->i2c_dev;
118         u16 header;
119         size_t frame_len;
120         int r;
121
122         r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
123         if (r < 0) {
124                 goto fw_read_exit;
125         } else if (r != NXP_NCI_FW_HDR_LEN) {
126                 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
127                 r = -EBADMSG;
128                 goto fw_read_exit;
129         }
130
131         frame_len = (get_unaligned_be16(&header) & NXP_NCI_FW_FRAME_LEN_MASK) +
132                     NXP_NCI_FW_CRC_LEN;
133
134         *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
135         if (*skb == NULL) {
136                 r = -ENOMEM;
137                 goto fw_read_exit;
138         }
139
140         memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
141
142         r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
143         if (r != frame_len) {
144                 nfc_err(&client->dev,
145                         "Invalid frame length: %u (expected %zu)\n",
146                         r, frame_len);
147                 r = -EBADMSG;
148                 goto fw_read_exit_free_skb;
149         }
150
151         return 0;
152
153 fw_read_exit_free_skb:
154         kfree_skb(*skb);
155 fw_read_exit:
156         return r;
157 }
158
159 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
160                                 struct sk_buff **skb)
161 {
162         struct nci_ctrl_hdr header; /* May actually be a data header */
163         struct i2c_client *client = phy->i2c_dev;
164         int r;
165
166         r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
167         if (r < 0) {
168                 goto nci_read_exit;
169         } else if (r != NCI_CTRL_HDR_SIZE) {
170                 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
171                 r = -EBADMSG;
172                 goto nci_read_exit;
173         }
174
175         *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
176         if (*skb == NULL) {
177                 r = -ENOMEM;
178                 goto nci_read_exit;
179         }
180
181         memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
182                NCI_CTRL_HDR_SIZE);
183
184         r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
185         if (r != header.plen) {
186                 nfc_err(&client->dev,
187                         "Invalid frame payload length: %u (expected %u)\n",
188                         r, header.plen);
189                 r = -EBADMSG;
190                 goto nci_read_exit_free_skb;
191         }
192
193         return 0;
194
195 nci_read_exit_free_skb:
196         kfree_skb(*skb);
197 nci_read_exit:
198         return r;
199 }
200
201 static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
202 {
203         struct nxp_nci_i2c_phy *phy = phy_id;
204         struct i2c_client *client;
205         struct nxp_nci_info *info;
206
207         struct sk_buff *skb = NULL;
208         int r = 0;
209
210         if (!phy || !phy->ndev)
211                 goto exit_irq_none;
212
213         client = phy->i2c_dev;
214
215         if (!client || irq != client->irq)
216                 goto exit_irq_none;
217
218         info = nci_get_drvdata(phy->ndev);
219
220         if (!info)
221                 goto exit_irq_none;
222
223         mutex_lock(&info->info_lock);
224
225         if (phy->hard_fault != 0)
226                 goto exit_irq_handled;
227
228         switch (info->mode) {
229         case NXP_NCI_MODE_NCI:
230                 r = nxp_nci_i2c_nci_read(phy, &skb);
231                 break;
232         case NXP_NCI_MODE_FW:
233                 r = nxp_nci_i2c_fw_read(phy, &skb);
234                 break;
235         case NXP_NCI_MODE_COLD:
236                 r = -EREMOTEIO;
237                 break;
238         }
239
240         if (r == -EREMOTEIO) {
241                 phy->hard_fault = r;
242                 skb = NULL;
243         } else if (r < 0) {
244                 nfc_err(&client->dev, "Read failed with error %d\n", r);
245                 goto exit_irq_handled;
246         }
247
248         switch (info->mode) {
249         case NXP_NCI_MODE_NCI:
250                 nci_recv_frame(phy->ndev, skb);
251                 break;
252         case NXP_NCI_MODE_FW:
253                 nxp_nci_fw_recv_frame(phy->ndev, skb);
254                 break;
255         case NXP_NCI_MODE_COLD:
256                 break;
257         }
258
259 exit_irq_handled:
260         mutex_unlock(&info->info_lock);
261         return IRQ_HANDLED;
262 exit_irq_none:
263         WARN_ON_ONCE(1);
264         return IRQ_NONE;
265 }
266
267 #ifdef CONFIG_OF
268
269 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
270 {
271         struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
272         struct device_node *pp;
273         int r;
274
275         pp = client->dev.of_node;
276         if (!pp)
277                 return -ENODEV;
278
279         r = of_get_named_gpio(pp, "enable-gpios", 0);
280         if (r == -EPROBE_DEFER)
281                 r = of_get_named_gpio(pp, "enable-gpios", 0);
282         if (r < 0) {
283                 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
284                 return r;
285         }
286         phy->gpio_en = r;
287
288         r = of_get_named_gpio(pp, "firmware-gpios", 0);
289         if (r == -EPROBE_DEFER)
290                 r = of_get_named_gpio(pp, "firmware-gpios", 0);
291         if (r < 0) {
292                 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
293                 return r;
294         }
295         phy->gpio_fw = r;
296
297         r = irq_of_parse_and_map(pp, 0);
298         if (r < 0) {
299                 nfc_err(&client->dev, "Unable to get irq, error: %d\n", r);
300                 return r;
301         }
302         client->irq = r;
303
304         return 0;
305 }
306
307 #else
308
309 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
310 {
311         return -ENODEV;
312 }
313
314 #endif
315
316 static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
317 {
318         struct i2c_client *client = phy->i2c_dev;
319         struct gpio_desc *gpiod_en, *gpiod_fw, *gpiod_irq;
320
321         gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
322         gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
323         gpiod_irq = devm_gpiod_get_index(&client->dev, NULL, 0, GPIOD_IN);
324
325         if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw) || IS_ERR(gpiod_irq)) {
326                 nfc_err(&client->dev, "No GPIOs\n");
327                 return -EINVAL;
328         }
329
330         client->irq = gpiod_to_irq(gpiod_irq);
331         if (client->irq < 0) {
332                 nfc_err(&client->dev, "No IRQ\n");
333                 return -EINVAL;
334         }
335
336         phy->gpio_en = desc_to_gpio(gpiod_en);
337         phy->gpio_fw = desc_to_gpio(gpiod_fw);
338         phy->gpio_irq = desc_to_gpio(gpiod_irq);
339
340         return 0;
341 }
342
343 static int nxp_nci_i2c_probe(struct i2c_client *client,
344                             const struct i2c_device_id *id)
345 {
346         struct nxp_nci_i2c_phy *phy;
347         struct nxp_nci_nfc_platform_data *pdata;
348         int r;
349
350         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
351                 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
352                 r = -ENODEV;
353                 goto probe_exit;
354         }
355
356         phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
357                            GFP_KERNEL);
358         if (!phy) {
359                 r = -ENOMEM;
360                 goto probe_exit;
361         }
362
363         phy->i2c_dev = client;
364         i2c_set_clientdata(client, phy);
365
366         pdata = client->dev.platform_data;
367
368         if (!pdata && client->dev.of_node) {
369                 r = nxp_nci_i2c_parse_devtree(client);
370                 if (r < 0) {
371                         nfc_err(&client->dev, "Failed to get DT data\n");
372                         goto probe_exit;
373                 }
374         } else if (pdata) {
375                 phy->gpio_en = pdata->gpio_en;
376                 phy->gpio_fw = pdata->gpio_fw;
377                 client->irq = pdata->irq;
378         } else if (ACPI_HANDLE(&client->dev)) {
379                 r = nxp_nci_i2c_acpi_config(phy);
380                 if (r < 0)
381                         goto probe_exit;
382                 goto nci_probe;
383         } else {
384                 nfc_err(&client->dev, "No platform data\n");
385                 r = -EINVAL;
386                 goto probe_exit;
387         }
388
389         r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
390                                   GPIOF_OUT_INIT_LOW, "nxp_nci_en");
391         if (r < 0)
392                 goto probe_exit;
393
394         r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
395                                   GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
396         if (r < 0)
397                 goto probe_exit;
398
399 nci_probe:
400         r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
401                           NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
402         if (r < 0)
403                 goto probe_exit;
404
405         r = request_threaded_irq(client->irq, NULL,
406                                  nxp_nci_i2c_irq_thread_fn,
407                                  IRQF_TRIGGER_RISING | IRQF_ONESHOT,
408                                  NXP_NCI_I2C_DRIVER_NAME, phy);
409         if (r < 0)
410                 nfc_err(&client->dev, "Unable to register IRQ handler\n");
411
412 probe_exit:
413         return r;
414 }
415
416 static int nxp_nci_i2c_remove(struct i2c_client *client)
417 {
418         struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
419
420         nxp_nci_remove(phy->ndev);
421         free_irq(client->irq, phy);
422
423         return 0;
424 }
425
426 static struct i2c_device_id nxp_nci_i2c_id_table[] = {
427         {"nxp-nci_i2c", 0},
428         {}
429 };
430 MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
431
432 static const struct of_device_id of_nxp_nci_i2c_match[] = {
433         { .compatible = "nxp,nxp-nci-i2c", },
434         {},
435 };
436 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
437
438 #ifdef CONFIG_ACPI
439 static struct acpi_device_id acpi_id[] = {
440         { "NXP7471" },
441         { },
442 };
443 MODULE_DEVICE_TABLE(acpi, acpi_id);
444 #endif
445
446 static struct i2c_driver nxp_nci_i2c_driver = {
447         .driver = {
448                    .name = NXP_NCI_I2C_DRIVER_NAME,
449                    .owner  = THIS_MODULE,
450                    .acpi_match_table = ACPI_PTR(acpi_id),
451                    .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
452                   },
453         .probe = nxp_nci_i2c_probe,
454         .id_table = nxp_nci_i2c_id_table,
455         .remove = nxp_nci_i2c_remove,
456 };
457
458 module_i2c_driver(nxp_nci_i2c_driver);
459
460 MODULE_LICENSE("GPL");
461 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
462 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
463 MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");