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