blob: f2c8a560e2655958ba2c7042a3bd09a273837e28 [file] [log] [blame]
Thomas Gleixner46fe7772019-05-31 01:09:57 -07001// SPDX-License-Identifier: GPL-2.0-only
Clément Perrochaud6be88672015-03-09 11:12:05 +01002/*
3 * I2C link layer for the NXP NCI driver
4 *
5 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +03006 * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
Clément Perrochaud6be88672015-03-09 11:12:05 +01007 *
8 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +03009 * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
Clément Perrochaud6be88672015-03-09 11:12:05 +010010 *
11 * Derived from PN544 device driver:
12 * Copyright (C) 2012 Intel Corporation. All rights reserved.
Clément Perrochaud6be88672015-03-09 11:12:05 +010013 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +030017#include <linux/acpi.h>
Clément Perrochaud6be88672015-03-09 11:12:05 +010018#include <linux/delay.h>
19#include <linux/i2c.h>
20#include <linux/interrupt.h>
Clément Perrochaud6be88672015-03-09 11:12:05 +010021#include <linux/module.h>
22#include <linux/nfc.h>
Samuel Ortiz262e7192015-05-28 17:10:03 +020023#include <linux/gpio/consumer.h>
Guenter Roeck2eee74b2015-08-01 06:59:29 -070024#include <asm/unaligned.h>
Clément Perrochaud6be88672015-03-09 11:12:05 +010025
26#include <net/nfc/nfc.h>
27
28#include "nxp-nci.h"
29
30#define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
31
32#define NXP_NCI_I2C_MAX_PAYLOAD 32
33
34struct nxp_nci_i2c_phy {
35 struct i2c_client *i2c_dev;
36 struct nci_dev *ndev;
37
Andy Shevchenko43201762019-07-29 16:35:04 +030038 struct gpio_desc *gpiod_en;
39 struct gpio_desc *gpiod_fw;
Clément Perrochaud6be88672015-03-09 11:12:05 +010040
41 int hard_fault; /*
42 * < 0 if hardware error occurred (e.g. i2c err)
43 * and prevents normal operation.
44 */
45};
46
47static int nxp_nci_i2c_set_mode(void *phy_id,
48 enum nxp_nci_mode mode)
49{
50 struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
51
Andy Shevchenko43201762019-07-29 16:35:04 +030052 gpiod_set_value(phy->gpiod_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
53 gpiod_set_value(phy->gpiod_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
Clément Perrochaud6be88672015-03-09 11:12:05 +010054 usleep_range(10000, 15000);
55
56 if (mode == NXP_NCI_MODE_COLD)
57 phy->hard_fault = 0;
58
59 return 0;
60}
61
62static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
63{
64 int r;
65 struct nxp_nci_i2c_phy *phy = phy_id;
66 struct i2c_client *client = phy->i2c_dev;
67
68 if (phy->hard_fault != 0)
69 return phy->hard_fault;
70
71 r = i2c_master_send(client, skb->data, skb->len);
Fabio Estevam59df9bb2015-11-04 08:13:00 -020072 if (r < 0) {
Clément Perrochaud6be88672015-03-09 11:12:05 +010073 /* Retry, chip was in standby */
Nicholas Mc Guire96bd0b52017-01-22 13:28:39 +010074 msleep(110);
Clément Perrochaud6be88672015-03-09 11:12:05 +010075 r = i2c_master_send(client, skb->data, skb->len);
76 }
77
78 if (r < 0) {
79 nfc_err(&client->dev, "Error %d on I2C send\n", r);
80 } else if (r != skb->len) {
81 nfc_err(&client->dev,
82 "Invalid length sent: %u (expected %u)\n",
83 r, skb->len);
84 r = -EREMOTEIO;
85 } else {
86 /* Success but return 0 and not number of bytes */
87 r = 0;
88 }
89
90 return r;
91}
92
Julia Lawall7cf6d082015-10-11 13:24:13 +020093static const struct nxp_nci_phy_ops i2c_phy_ops = {
Clément Perrochaud6be88672015-03-09 11:12:05 +010094 .set_mode = nxp_nci_i2c_set_mode,
95 .write = nxp_nci_i2c_write,
96};
97
98static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
99 struct sk_buff **skb)
100{
101 struct i2c_client *client = phy->i2c_dev;
102 u16 header;
103 size_t frame_len;
104 int r;
105
106 r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
107 if (r < 0) {
108 goto fw_read_exit;
109 } else if (r != NXP_NCI_FW_HDR_LEN) {
110 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
111 r = -EBADMSG;
112 goto fw_read_exit;
113 }
114
Al Viro4ea20632017-04-17 00:42:22 +0200115 frame_len = (be16_to_cpu(header) & NXP_NCI_FW_FRAME_LEN_MASK) +
Clément Perrochaud6be88672015-03-09 11:12:05 +0100116 NXP_NCI_FW_CRC_LEN;
117
118 *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
119 if (*skb == NULL) {
120 r = -ENOMEM;
121 goto fw_read_exit;
122 }
123
Johannes Berg59ae1d12017-06-16 14:29:20 +0200124 skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN);
Clément Perrochaud6be88672015-03-09 11:12:05 +0100125
126 r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
127 if (r != frame_len) {
128 nfc_err(&client->dev,
129 "Invalid frame length: %u (expected %zu)\n",
130 r, frame_len);
131 r = -EBADMSG;
132 goto fw_read_exit_free_skb;
133 }
134
135 return 0;
136
137fw_read_exit_free_skb:
138 kfree_skb(*skb);
139fw_read_exit:
140 return r;
141}
142
143static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
144 struct sk_buff **skb)
145{
146 struct nci_ctrl_hdr header; /* May actually be a data header */
147 struct i2c_client *client = phy->i2c_dev;
148 int r;
149
150 r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
151 if (r < 0) {
152 goto nci_read_exit;
153 } else if (r != NCI_CTRL_HDR_SIZE) {
154 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
155 r = -EBADMSG;
156 goto nci_read_exit;
157 }
158
159 *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
160 if (*skb == NULL) {
161 r = -ENOMEM;
162 goto nci_read_exit;
163 }
164
Johannes Berg59ae1d12017-06-16 14:29:20 +0200165 skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE);
Clément Perrochaud6be88672015-03-09 11:12:05 +0100166
167 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
168 if (r != header.plen) {
169 nfc_err(&client->dev,
170 "Invalid frame payload length: %u (expected %u)\n",
171 r, header.plen);
172 r = -EBADMSG;
173 goto nci_read_exit_free_skb;
174 }
175
176 return 0;
177
178nci_read_exit_free_skb:
179 kfree_skb(*skb);
180nci_read_exit:
181 return r;
182}
183
184static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
185{
186 struct nxp_nci_i2c_phy *phy = phy_id;
187 struct i2c_client *client;
188 struct nxp_nci_info *info;
189
190 struct sk_buff *skb = NULL;
191 int r = 0;
192
193 if (!phy || !phy->ndev)
194 goto exit_irq_none;
195
196 client = phy->i2c_dev;
197
198 if (!client || irq != client->irq)
199 goto exit_irq_none;
200
201 info = nci_get_drvdata(phy->ndev);
202
203 if (!info)
204 goto exit_irq_none;
205
206 mutex_lock(&info->info_lock);
207
208 if (phy->hard_fault != 0)
209 goto exit_irq_handled;
210
211 switch (info->mode) {
212 case NXP_NCI_MODE_NCI:
213 r = nxp_nci_i2c_nci_read(phy, &skb);
214 break;
215 case NXP_NCI_MODE_FW:
216 r = nxp_nci_i2c_fw_read(phy, &skb);
217 break;
218 case NXP_NCI_MODE_COLD:
219 r = -EREMOTEIO;
220 break;
221 }
222
223 if (r == -EREMOTEIO) {
224 phy->hard_fault = r;
225 skb = NULL;
226 } else if (r < 0) {
227 nfc_err(&client->dev, "Read failed with error %d\n", r);
228 goto exit_irq_handled;
229 }
230
231 switch (info->mode) {
232 case NXP_NCI_MODE_NCI:
233 nci_recv_frame(phy->ndev, skb);
234 break;
235 case NXP_NCI_MODE_FW:
236 nxp_nci_fw_recv_frame(phy->ndev, skb);
237 break;
238 case NXP_NCI_MODE_COLD:
239 break;
240 }
241
242exit_irq_handled:
243 mutex_unlock(&info->info_lock);
244 return IRQ_HANDLED;
245exit_irq_none:
246 WARN_ON_ONCE(1);
247 return IRQ_NONE;
248}
249
Andy Shevchenko099d03f2019-07-29 16:35:05 +0300250static const struct acpi_gpio_params firmware_gpios = { 1, 0, false };
251static const struct acpi_gpio_params enable_gpios = { 2, 0, false };
252
253static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
254 { "enable-gpios", &enable_gpios, 1 },
255 { "firmware-gpios", &firmware_gpios, 1 },
256 { }
257};
258
Clément Perrochaud6be88672015-03-09 11:12:05 +0100259static int nxp_nci_i2c_probe(struct i2c_client *client,
260 const struct i2c_device_id *id)
261{
Andy Shevchenkoad0acfd2019-07-29 16:35:06 +0300262 struct device *dev = &client->dev;
Clément Perrochaud6be88672015-03-09 11:12:05 +0100263 struct nxp_nci_i2c_phy *phy;
Clément Perrochaud6be88672015-03-09 11:12:05 +0100264 int r;
265
266 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
267 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
Andy Shevchenko4f1cbf22019-07-29 16:35:07 +0300268 return -ENODEV;
Clément Perrochaud6be88672015-03-09 11:12:05 +0100269 }
270
271 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
272 GFP_KERNEL);
Andy Shevchenko4f1cbf22019-07-29 16:35:07 +0300273 if (!phy)
274 return -ENOMEM;
Clément Perrochaud6be88672015-03-09 11:12:05 +0100275
276 phy->i2c_dev = client;
277 i2c_set_clientdata(client, phy);
278
Andy Shevchenkoad0acfd2019-07-29 16:35:06 +0300279 r = devm_acpi_dev_add_driver_gpios(dev, acpi_nxp_nci_gpios);
280 if (r)
281 return r;
282
283 phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
284 if (IS_ERR(phy->gpiod_en)) {
285 nfc_err(dev, "Failed to get EN gpio\n");
286 return PTR_ERR(phy->gpiod_en);
287 }
288
289 phy->gpiod_fw = devm_gpiod_get(dev, "firmware", GPIOD_OUT_LOW);
290 if (IS_ERR(phy->gpiod_fw)) {
291 nfc_err(dev, "Failed to get FW gpio\n");
292 return PTR_ERR(phy->gpiod_fw);
Clément Perrochaud6be88672015-03-09 11:12:05 +0100293 }
294
Clément Perrochaud6be88672015-03-09 11:12:05 +0100295 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
296 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
297 if (r < 0)
Andy Shevchenko4f1cbf22019-07-29 16:35:07 +0300298 return r;
Clément Perrochaud6be88672015-03-09 11:12:05 +0100299
300 r = request_threaded_irq(client->irq, NULL,
301 nxp_nci_i2c_irq_thread_fn,
302 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
303 NXP_NCI_I2C_DRIVER_NAME, phy);
304 if (r < 0)
305 nfc_err(&client->dev, "Unable to register IRQ handler\n");
306
Clément Perrochaud6be88672015-03-09 11:12:05 +0100307 return r;
308}
309
310static int nxp_nci_i2c_remove(struct i2c_client *client)
311{
312 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
313
314 nxp_nci_remove(phy->ndev);
315 free_irq(client->irq, phy);
316
317 return 0;
318}
319
Arvind Yadav01e682a2017-08-21 22:33:55 +0530320static const struct i2c_device_id nxp_nci_i2c_id_table[] = {
Clément Perrochaud6be88672015-03-09 11:12:05 +0100321 {"nxp-nci_i2c", 0},
322 {}
323};
324MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
325
326static const struct of_device_id of_nxp_nci_i2c_match[] = {
327 { .compatible = "nxp,nxp-nci-i2c", },
328 {},
329};
330MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
331
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300332#ifdef CONFIG_ACPI
Andy Shevchenko52c2ea02019-07-29 16:35:08 +0300333static const struct acpi_device_id acpi_id[] = {
Andy Shevchenko1b14a372019-07-29 16:35:02 +0300334 { "NXP1001" },
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300335 { "NXP7471" },
336 { },
337};
338MODULE_DEVICE_TABLE(acpi, acpi_id);
339#endif
340
Clément Perrochaud6be88672015-03-09 11:12:05 +0100341static struct i2c_driver nxp_nci_i2c_driver = {
342 .driver = {
343 .name = NXP_NCI_I2C_DRIVER_NAME,
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300344 .acpi_match_table = ACPI_PTR(acpi_id),
Andy Shevchenkoda052082019-07-29 16:35:09 +0300345 .of_match_table = of_nxp_nci_i2c_match,
Clément Perrochaud6be88672015-03-09 11:12:05 +0100346 },
347 .probe = nxp_nci_i2c_probe,
348 .id_table = nxp_nci_i2c_id_table,
349 .remove = nxp_nci_i2c_remove,
350};
351
352module_i2c_driver(nxp_nci_i2c_driver);
353
354MODULE_LICENSE("GPL");
355MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
356MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300357MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");