blob: c6a04a950225ef66713239854ab5750f0cd936f0 [file] [log] [blame]
Clément Perrochaud6be88672015-03-09 11:12:05 +01001/*
2 * I2C link layer for the NXP NCI driver
3 *
4 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +03005 * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
Clément Perrochaud6be88672015-03-09 11:12:05 +01006 *
7 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +03008 * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
Clément Perrochaud6be88672015-03-09 11:12:05 +01009 *
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
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +030028#include <linux/acpi.h>
Clément Perrochaud6be88672015-03-09 11:12:05 +010029#include <linux/delay.h>
30#include <linux/i2c.h>
31#include <linux/interrupt.h>
Clément Perrochaud6be88672015-03-09 11:12:05 +010032#include <linux/module.h>
33#include <linux/nfc.h>
Samuel Ortiz262e7192015-05-28 17:10:03 +020034#include <linux/gpio/consumer.h>
Clément Perrochaud6be88672015-03-09 11:12:05 +010035#include <linux/of_gpio.h>
36#include <linux/of_irq.h>
37#include <linux/platform_data/nxp-nci.h>
Guenter Roeck2eee74b2015-08-01 06:59:29 -070038#include <asm/unaligned.h>
Clément Perrochaud6be88672015-03-09 11:12:05 +010039
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
48struct 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
61static 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
76static 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);
Fabio Estevam59df9bb2015-11-04 08:13:00 -020086 if (r < 0) {
Clément Perrochaud6be88672015-03-09 11:12:05 +010087 /* Retry, chip was in standby */
Nicholas Mc Guire96bd0b52017-01-22 13:28:39 +010088 msleep(110);
Clément Perrochaud6be88672015-03-09 11:12:05 +010089 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
Julia Lawall7cf6d082015-10-11 13:24:13 +0200107static const struct nxp_nci_phy_ops i2c_phy_ops = {
Clément Perrochaud6be88672015-03-09 11:12:05 +0100108 .set_mode = nxp_nci_i2c_set_mode,
109 .write = nxp_nci_i2c_write,
110};
111
112static 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 = (get_unaligned_be16(&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 memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
139
140 r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
141 if (r != frame_len) {
142 nfc_err(&client->dev,
143 "Invalid frame length: %u (expected %zu)\n",
144 r, frame_len);
145 r = -EBADMSG;
146 goto fw_read_exit_free_skb;
147 }
148
149 return 0;
150
151fw_read_exit_free_skb:
152 kfree_skb(*skb);
153fw_read_exit:
154 return r;
155}
156
157static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
158 struct sk_buff **skb)
159{
160 struct nci_ctrl_hdr header; /* May actually be a data header */
161 struct i2c_client *client = phy->i2c_dev;
162 int r;
163
164 r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
165 if (r < 0) {
166 goto nci_read_exit;
167 } else if (r != NCI_CTRL_HDR_SIZE) {
168 nfc_err(&client->dev, "Incorrect header length: %u\n", r);
169 r = -EBADMSG;
170 goto nci_read_exit;
171 }
172
173 *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
174 if (*skb == NULL) {
175 r = -ENOMEM;
176 goto nci_read_exit;
177 }
178
179 memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
180 NCI_CTRL_HDR_SIZE);
181
182 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
183 if (r != header.plen) {
184 nfc_err(&client->dev,
185 "Invalid frame payload length: %u (expected %u)\n",
186 r, header.plen);
187 r = -EBADMSG;
188 goto nci_read_exit_free_skb;
189 }
190
191 return 0;
192
193nci_read_exit_free_skb:
194 kfree_skb(*skb);
195nci_read_exit:
196 return r;
197}
198
199static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
200{
201 struct nxp_nci_i2c_phy *phy = phy_id;
202 struct i2c_client *client;
203 struct nxp_nci_info *info;
204
205 struct sk_buff *skb = NULL;
206 int r = 0;
207
208 if (!phy || !phy->ndev)
209 goto exit_irq_none;
210
211 client = phy->i2c_dev;
212
213 if (!client || irq != client->irq)
214 goto exit_irq_none;
215
216 info = nci_get_drvdata(phy->ndev);
217
218 if (!info)
219 goto exit_irq_none;
220
221 mutex_lock(&info->info_lock);
222
223 if (phy->hard_fault != 0)
224 goto exit_irq_handled;
225
226 switch (info->mode) {
227 case NXP_NCI_MODE_NCI:
228 r = nxp_nci_i2c_nci_read(phy, &skb);
229 break;
230 case NXP_NCI_MODE_FW:
231 r = nxp_nci_i2c_fw_read(phy, &skb);
232 break;
233 case NXP_NCI_MODE_COLD:
234 r = -EREMOTEIO;
235 break;
236 }
237
238 if (r == -EREMOTEIO) {
239 phy->hard_fault = r;
240 skb = NULL;
241 } else if (r < 0) {
242 nfc_err(&client->dev, "Read failed with error %d\n", r);
243 goto exit_irq_handled;
244 }
245
246 switch (info->mode) {
247 case NXP_NCI_MODE_NCI:
248 nci_recv_frame(phy->ndev, skb);
249 break;
250 case NXP_NCI_MODE_FW:
251 nxp_nci_fw_recv_frame(phy->ndev, skb);
252 break;
253 case NXP_NCI_MODE_COLD:
254 break;
255 }
256
257exit_irq_handled:
258 mutex_unlock(&info->info_lock);
259 return IRQ_HANDLED;
260exit_irq_none:
261 WARN_ON_ONCE(1);
262 return IRQ_NONE;
263}
264
Clément Perrochaud6be88672015-03-09 11:12:05 +0100265static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
266{
267 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
268 struct device_node *pp;
269 int r;
270
271 pp = client->dev.of_node;
272 if (!pp)
273 return -ENODEV;
274
275 r = of_get_named_gpio(pp, "enable-gpios", 0);
276 if (r == -EPROBE_DEFER)
277 r = of_get_named_gpio(pp, "enable-gpios", 0);
278 if (r < 0) {
279 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
280 return r;
281 }
282 phy->gpio_en = r;
283
284 r = of_get_named_gpio(pp, "firmware-gpios", 0);
285 if (r == -EPROBE_DEFER)
286 r = of_get_named_gpio(pp, "firmware-gpios", 0);
287 if (r < 0) {
288 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
289 return r;
290 }
291 phy->gpio_fw = r;
292
Clément Perrochaud6be88672015-03-09 11:12:05 +0100293 return 0;
294}
295
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300296static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
297{
298 struct i2c_client *client = phy->i2c_dev;
Christophe Ricardbe103b72015-12-23 23:45:25 +0100299 struct gpio_desc *gpiod_en, *gpiod_fw;
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300300
Uwe Kleine-König3bfe7682015-06-12 09:04:55 +0200301 gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
302 gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300303
Christophe Ricardbe103b72015-12-23 23:45:25 +0100304 if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300305 nfc_err(&client->dev, "No GPIOs\n");
306 return -EINVAL;
307 }
308
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300309 phy->gpio_en = desc_to_gpio(gpiod_en);
310 phy->gpio_fw = desc_to_gpio(gpiod_fw);
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300311
312 return 0;
313}
314
Clément Perrochaud6be88672015-03-09 11:12:05 +0100315static int nxp_nci_i2c_probe(struct i2c_client *client,
316 const struct i2c_device_id *id)
317{
318 struct nxp_nci_i2c_phy *phy;
319 struct nxp_nci_nfc_platform_data *pdata;
320 int r;
321
322 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
323 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
324 r = -ENODEV;
325 goto probe_exit;
326 }
327
328 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
329 GFP_KERNEL);
330 if (!phy) {
331 r = -ENOMEM;
332 goto probe_exit;
333 }
334
335 phy->i2c_dev = client;
336 i2c_set_clientdata(client, phy);
337
338 pdata = client->dev.platform_data;
339
340 if (!pdata && client->dev.of_node) {
341 r = nxp_nci_i2c_parse_devtree(client);
342 if (r < 0) {
343 nfc_err(&client->dev, "Failed to get DT data\n");
344 goto probe_exit;
345 }
346 } else if (pdata) {
347 phy->gpio_en = pdata->gpio_en;
348 phy->gpio_fw = pdata->gpio_fw;
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300349 } else if (ACPI_HANDLE(&client->dev)) {
350 r = nxp_nci_i2c_acpi_config(phy);
351 if (r < 0)
352 goto probe_exit;
353 goto nci_probe;
Clément Perrochaud6be88672015-03-09 11:12:05 +0100354 } else {
355 nfc_err(&client->dev, "No platform data\n");
356 r = -EINVAL;
357 goto probe_exit;
358 }
359
360 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
361 GPIOF_OUT_INIT_LOW, "nxp_nci_en");
362 if (r < 0)
363 goto probe_exit;
364
365 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
366 GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
367 if (r < 0)
368 goto probe_exit;
369
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300370nci_probe:
Clément Perrochaud6be88672015-03-09 11:12:05 +0100371 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
372 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
373 if (r < 0)
374 goto probe_exit;
375
376 r = request_threaded_irq(client->irq, NULL,
377 nxp_nci_i2c_irq_thread_fn,
378 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
379 NXP_NCI_I2C_DRIVER_NAME, phy);
380 if (r < 0)
381 nfc_err(&client->dev, "Unable to register IRQ handler\n");
382
383probe_exit:
384 return r;
385}
386
387static int nxp_nci_i2c_remove(struct i2c_client *client)
388{
389 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
390
391 nxp_nci_remove(phy->ndev);
392 free_irq(client->irq, phy);
393
394 return 0;
395}
396
397static struct i2c_device_id nxp_nci_i2c_id_table[] = {
398 {"nxp-nci_i2c", 0},
399 {}
400};
401MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
402
403static const struct of_device_id of_nxp_nci_i2c_match[] = {
404 { .compatible = "nxp,nxp-nci-i2c", },
405 {},
406};
407MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
408
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300409#ifdef CONFIG_ACPI
410static struct acpi_device_id acpi_id[] = {
411 { "NXP7471" },
412 { },
413};
414MODULE_DEVICE_TABLE(acpi, acpi_id);
415#endif
416
Clément Perrochaud6be88672015-03-09 11:12:05 +0100417static struct i2c_driver nxp_nci_i2c_driver = {
418 .driver = {
419 .name = NXP_NCI_I2C_DRIVER_NAME,
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300420 .acpi_match_table = ACPI_PTR(acpi_id),
Clément Perrochaud6be88672015-03-09 11:12:05 +0100421 .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
422 },
423 .probe = nxp_nci_i2c_probe,
424 .id_table = nxp_nci_i2c_id_table,
425 .remove = nxp_nci_i2c_remove,
426};
427
428module_i2c_driver(nxp_nci_i2c_driver);
429
430MODULE_LICENSE("GPL");
431MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
432MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
Oleg Zhurakivskyy551e3062015-05-25 13:55:13 +0300433MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");