GNU Linux-libre 4.9.328-gnu1
[releases.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 <asm/unaligned.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
56         int hard_fault; /*
57                          * < 0 if hardware error occurred (e.g. i2c err)
58                          * and prevents normal operation.
59                          */
60 };
61
62 static int nxp_nci_i2c_set_mode(void *phy_id,
63                                     enum nxp_nci_mode mode)
64 {
65         struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
66
67         gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
68         gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
69         usleep_range(10000, 15000);
70
71         if (mode == NXP_NCI_MODE_COLD)
72                 phy->hard_fault = 0;
73
74         return 0;
75 }
76
77 static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
78 {
79         int r;
80         struct nxp_nci_i2c_phy *phy = phy_id;
81         struct i2c_client *client = phy->i2c_dev;
82
83         if (phy->hard_fault != 0)
84                 return phy->hard_fault;
85
86         r = i2c_master_send(client, skb->data, skb->len);
87         if (r < 0) {
88                 /* Retry, chip was in standby */
89                 usleep_range(110000, 120000);
90                 r = i2c_master_send(client, skb->data, skb->len);
91         }
92
93         if (r < 0) {
94                 nfc_err(&client->dev, "Error %d on I2C send\n", r);
95         } else if (r != skb->len) {
96                 nfc_err(&client->dev,
97                         "Invalid length sent: %u (expected %u)\n",
98                         r, skb->len);
99                 r = -EREMOTEIO;
100         } else {
101                 /* Success but return 0 and not number of bytes */
102                 r = 0;
103         }
104
105         return r;
106 }
107
108 static const struct nxp_nci_phy_ops i2c_phy_ops = {
109         .set_mode = nxp_nci_i2c_set_mode,
110         .write = nxp_nci_i2c_write,
111 };
112
113 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
114                                struct sk_buff **skb)
115 {
116         struct i2c_client *client = phy->i2c_dev;
117         u16 header;
118         size_t frame_len;
119         int r;
120
121         r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
122         if (r < 0) {
123                 goto fw_read_exit;
124         } else if (r != NXP_NCI_FW_HDR_LEN) {
125                 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
126                 r = -EBADMSG;
127                 goto fw_read_exit;
128         }
129
130         frame_len = (get_unaligned_be16(&header) & NXP_NCI_FW_FRAME_LEN_MASK) +
131                     NXP_NCI_FW_CRC_LEN;
132
133         *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
134         if (*skb == NULL) {
135                 r = -ENOMEM;
136                 goto fw_read_exit;
137         }
138
139         memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
140
141         r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
142         if (r < 0) {
143                 goto fw_read_exit_free_skb;
144         } else if (r != frame_len) {
145                 nfc_err(&client->dev,
146                         "Invalid frame length: %u (expected %zu)\n",
147                         r, frame_len);
148                 r = -EBADMSG;
149                 goto fw_read_exit_free_skb;
150         }
151
152         return 0;
153
154 fw_read_exit_free_skb:
155         kfree_skb(*skb);
156 fw_read_exit:
157         return r;
158 }
159
160 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
161                                 struct sk_buff **skb)
162 {
163         struct nci_ctrl_hdr header; /* May actually be a data header */
164         struct i2c_client *client = phy->i2c_dev;
165         int r;
166
167         r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
168         if (r < 0) {
169                 goto nci_read_exit;
170         } else if (r != NCI_CTRL_HDR_SIZE) {
171                 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
172                 r = -EBADMSG;
173                 goto nci_read_exit;
174         }
175
176         *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
177         if (*skb == NULL) {
178                 r = -ENOMEM;
179                 goto nci_read_exit;
180         }
181
182         memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
183                NCI_CTRL_HDR_SIZE);
184
185         if (!header.plen)
186                 return 0;
187
188         r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
189         if (r < 0) {
190                 goto nci_read_exit_free_skb;
191         } else if (r != header.plen) {
192                 nfc_err(&client->dev,
193                         "Invalid frame payload length: %u (expected %u)\n",
194                         r, header.plen);
195                 r = -EBADMSG;
196                 goto nci_read_exit_free_skb;
197         }
198
199         return 0;
200
201 nci_read_exit_free_skb:
202         kfree_skb(*skb);
203 nci_read_exit:
204         return r;
205 }
206
207 static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
208 {
209         struct nxp_nci_i2c_phy *phy = phy_id;
210         struct i2c_client *client;
211         struct nxp_nci_info *info;
212
213         struct sk_buff *skb = NULL;
214         int r = 0;
215
216         if (!phy || !phy->ndev)
217                 goto exit_irq_none;
218
219         client = phy->i2c_dev;
220
221         if (!client || irq != client->irq)
222                 goto exit_irq_none;
223
224         info = nci_get_drvdata(phy->ndev);
225
226         if (!info)
227                 goto exit_irq_none;
228
229         mutex_lock(&info->info_lock);
230
231         if (phy->hard_fault != 0)
232                 goto exit_irq_handled;
233
234         switch (info->mode) {
235         case NXP_NCI_MODE_NCI:
236                 r = nxp_nci_i2c_nci_read(phy, &skb);
237                 break;
238         case NXP_NCI_MODE_FW:
239                 r = nxp_nci_i2c_fw_read(phy, &skb);
240                 break;
241         case NXP_NCI_MODE_COLD:
242                 r = -EREMOTEIO;
243                 break;
244         }
245
246         if (r == -EREMOTEIO) {
247                 phy->hard_fault = r;
248                 if (info->mode == NXP_NCI_MODE_FW)
249                         nxp_nci_fw_recv_frame(phy->ndev, NULL);
250         }
251         if (r < 0) {
252                 nfc_err(&client->dev, "Read failed with error %d\n", r);
253                 goto exit_irq_handled;
254         }
255
256         switch (info->mode) {
257         case NXP_NCI_MODE_NCI:
258                 nci_recv_frame(phy->ndev, skb);
259                 break;
260         case NXP_NCI_MODE_FW:
261                 nxp_nci_fw_recv_frame(phy->ndev, skb);
262                 break;
263         case NXP_NCI_MODE_COLD:
264                 break;
265         }
266
267 exit_irq_handled:
268         mutex_unlock(&info->info_lock);
269         return IRQ_HANDLED;
270 exit_irq_none:
271         WARN_ON_ONCE(1);
272         return IRQ_NONE;
273 }
274
275 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
276 {
277         struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
278         struct device_node *pp;
279         int r;
280
281         pp = client->dev.of_node;
282         if (!pp)
283                 return -ENODEV;
284
285         r = of_get_named_gpio(pp, "enable-gpios", 0);
286         if (r == -EPROBE_DEFER)
287                 r = of_get_named_gpio(pp, "enable-gpios", 0);
288         if (r < 0) {
289                 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
290                 return r;
291         }
292         phy->gpio_en = r;
293
294         r = of_get_named_gpio(pp, "firmware-gpios", 0);
295         if (r == -EPROBE_DEFER)
296                 r = of_get_named_gpio(pp, "firmware-gpios", 0);
297         if (r < 0) {
298                 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
299                 return r;
300         }
301         phy->gpio_fw = r;
302
303         return 0;
304 }
305
306 static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
307 {
308         struct i2c_client *client = phy->i2c_dev;
309         struct gpio_desc *gpiod_en, *gpiod_fw;
310
311         gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
312         gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
313
314         if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
315                 nfc_err(&client->dev, "No GPIOs\n");
316                 return -EINVAL;
317         }
318
319         phy->gpio_en = desc_to_gpio(gpiod_en);
320         phy->gpio_fw = desc_to_gpio(gpiod_fw);
321
322         return 0;
323 }
324
325 static int nxp_nci_i2c_probe(struct i2c_client *client,
326                             const struct i2c_device_id *id)
327 {
328         struct nxp_nci_i2c_phy *phy;
329         struct nxp_nci_nfc_platform_data *pdata;
330         int r;
331
332         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
333                 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
334                 r = -ENODEV;
335                 goto probe_exit;
336         }
337
338         phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
339                            GFP_KERNEL);
340         if (!phy) {
341                 r = -ENOMEM;
342                 goto probe_exit;
343         }
344
345         phy->i2c_dev = client;
346         i2c_set_clientdata(client, phy);
347
348         pdata = client->dev.platform_data;
349
350         if (!pdata && client->dev.of_node) {
351                 r = nxp_nci_i2c_parse_devtree(client);
352                 if (r < 0) {
353                         nfc_err(&client->dev, "Failed to get DT data\n");
354                         goto probe_exit;
355                 }
356         } else if (pdata) {
357                 phy->gpio_en = pdata->gpio_en;
358                 phy->gpio_fw = pdata->gpio_fw;
359         } else if (ACPI_HANDLE(&client->dev)) {
360                 r = nxp_nci_i2c_acpi_config(phy);
361                 if (r < 0)
362                         goto probe_exit;
363                 goto nci_probe;
364         } else {
365                 nfc_err(&client->dev, "No platform data\n");
366                 r = -EINVAL;
367                 goto probe_exit;
368         }
369
370         r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
371                                   GPIOF_OUT_INIT_LOW, "nxp_nci_en");
372         if (r < 0)
373                 goto probe_exit;
374
375         r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
376                                   GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
377         if (r < 0)
378                 goto probe_exit;
379
380 nci_probe:
381         r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
382                           NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
383         if (r < 0)
384                 goto probe_exit;
385
386         r = request_threaded_irq(client->irq, NULL,
387                                  nxp_nci_i2c_irq_thread_fn,
388                                  IRQF_TRIGGER_RISING | IRQF_ONESHOT,
389                                  NXP_NCI_I2C_DRIVER_NAME, phy);
390         if (r < 0)
391                 nfc_err(&client->dev, "Unable to register IRQ handler\n");
392
393 probe_exit:
394         return r;
395 }
396
397 static int nxp_nci_i2c_remove(struct i2c_client *client)
398 {
399         struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
400
401         nxp_nci_remove(phy->ndev);
402         free_irq(client->irq, phy);
403
404         return 0;
405 }
406
407 static struct i2c_device_id nxp_nci_i2c_id_table[] = {
408         {"nxp-nci_i2c", 0},
409         {}
410 };
411 MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
412
413 static const struct of_device_id of_nxp_nci_i2c_match[] = {
414         { .compatible = "nxp,nxp-nci-i2c", },
415         {},
416 };
417 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
418
419 #ifdef CONFIG_ACPI
420 static struct acpi_device_id acpi_id[] = {
421         { "NXP7471" },
422         { },
423 };
424 MODULE_DEVICE_TABLE(acpi, acpi_id);
425 #endif
426
427 static struct i2c_driver nxp_nci_i2c_driver = {
428         .driver = {
429                    .name = NXP_NCI_I2C_DRIVER_NAME,
430                    .acpi_match_table = ACPI_PTR(acpi_id),
431                    .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
432                   },
433         .probe = nxp_nci_i2c_probe,
434         .id_table = nxp_nci_i2c_id_table,
435         .remove = nxp_nci_i2c_remove,
436 };
437
438 module_i2c_driver(nxp_nci_i2c_driver);
439
440 MODULE_LICENSE("GPL");
441 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
442 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
443 MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");