usb: dwc3: pci: Add GPIO lookup table on platforms without ACPI GPIO resources
[linux/fpc-iii.git] / drivers / nfc / nxp-nci / i2c.c
blobba695e392c3b7b1c4d85c9e877d1234c23502caf
1 /*
2 * I2C link layer for the NXP NCI driver
4 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
5 * Copyright (C) 2012-2015 Intel Corporation. All rights reserved.
7 * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
8 * Authors: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
10 * Derived from PN544 device driver:
11 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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/>.
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
40 #include <net/nfc/nfc.h>
42 #include "nxp-nci.h"
44 #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
46 #define NXP_NCI_I2C_MAX_PAYLOAD 32
48 struct nxp_nci_i2c_phy {
49 struct i2c_client *i2c_dev;
50 struct nci_dev *ndev;
52 unsigned int gpio_en;
53 unsigned int gpio_fw;
55 int hard_fault; /*
56 * < 0 if hardware error occurred (e.g. i2c err)
57 * and prevents normal operation.
61 static int nxp_nci_i2c_set_mode(void *phy_id,
62 enum nxp_nci_mode mode)
64 struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
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);
70 if (mode == NXP_NCI_MODE_COLD)
71 phy->hard_fault = 0;
73 return 0;
76 static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
78 int r;
79 struct nxp_nci_i2c_phy *phy = phy_id;
80 struct i2c_client *client = phy->i2c_dev;
82 if (phy->hard_fault != 0)
83 return phy->hard_fault;
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);
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;
104 return r;
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,
112 static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
113 struct sk_buff **skb)
115 struct i2c_client *client = phy->i2c_dev;
116 u16 header;
117 size_t frame_len;
118 int r;
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;
129 frame_len = (be16_to_cpu(header) & NXP_NCI_FW_FRAME_LEN_MASK) +
130 NXP_NCI_FW_CRC_LEN;
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;
138 skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN);
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;
149 return 0;
151 fw_read_exit_free_skb:
152 kfree_skb(*skb);
153 fw_read_exit:
154 return r;
157 static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
158 struct sk_buff **skb)
160 struct nci_ctrl_hdr header; /* May actually be a data header */
161 struct i2c_client *client = phy->i2c_dev;
162 int r;
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;
173 *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
174 if (*skb == NULL) {
175 r = -ENOMEM;
176 goto nci_read_exit;
179 skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE);
181 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
182 if (r != header.plen) {
183 nfc_err(&client->dev,
184 "Invalid frame payload length: %u (expected %u)\n",
185 r, header.plen);
186 r = -EBADMSG;
187 goto nci_read_exit_free_skb;
190 return 0;
192 nci_read_exit_free_skb:
193 kfree_skb(*skb);
194 nci_read_exit:
195 return r;
198 static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
200 struct nxp_nci_i2c_phy *phy = phy_id;
201 struct i2c_client *client;
202 struct nxp_nci_info *info;
204 struct sk_buff *skb = NULL;
205 int r = 0;
207 if (!phy || !phy->ndev)
208 goto exit_irq_none;
210 client = phy->i2c_dev;
212 if (!client || irq != client->irq)
213 goto exit_irq_none;
215 info = nci_get_drvdata(phy->ndev);
217 if (!info)
218 goto exit_irq_none;
220 mutex_lock(&info->info_lock);
222 if (phy->hard_fault != 0)
223 goto exit_irq_handled;
225 switch (info->mode) {
226 case NXP_NCI_MODE_NCI:
227 r = nxp_nci_i2c_nci_read(phy, &skb);
228 break;
229 case NXP_NCI_MODE_FW:
230 r = nxp_nci_i2c_fw_read(phy, &skb);
231 break;
232 case NXP_NCI_MODE_COLD:
233 r = -EREMOTEIO;
234 break;
237 if (r == -EREMOTEIO) {
238 phy->hard_fault = r;
239 skb = NULL;
240 } else if (r < 0) {
241 nfc_err(&client->dev, "Read failed with error %d\n", r);
242 goto exit_irq_handled;
245 switch (info->mode) {
246 case NXP_NCI_MODE_NCI:
247 nci_recv_frame(phy->ndev, skb);
248 break;
249 case NXP_NCI_MODE_FW:
250 nxp_nci_fw_recv_frame(phy->ndev, skb);
251 break;
252 case NXP_NCI_MODE_COLD:
253 break;
256 exit_irq_handled:
257 mutex_unlock(&info->info_lock);
258 return IRQ_HANDLED;
259 exit_irq_none:
260 WARN_ON_ONCE(1);
261 return IRQ_NONE;
264 static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
266 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
267 struct device_node *pp;
268 int r;
270 pp = client->dev.of_node;
271 if (!pp)
272 return -ENODEV;
274 r = of_get_named_gpio(pp, "enable-gpios", 0);
275 if (r == -EPROBE_DEFER)
276 r = of_get_named_gpio(pp, "enable-gpios", 0);
277 if (r < 0) {
278 nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
279 return r;
281 phy->gpio_en = r;
283 r = of_get_named_gpio(pp, "firmware-gpios", 0);
284 if (r == -EPROBE_DEFER)
285 r = of_get_named_gpio(pp, "firmware-gpios", 0);
286 if (r < 0) {
287 nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
288 return r;
290 phy->gpio_fw = r;
292 return 0;
295 static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
297 struct i2c_client *client = phy->i2c_dev;
298 struct gpio_desc *gpiod_en, *gpiod_fw;
300 gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
301 gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
303 if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
304 nfc_err(&client->dev, "No GPIOs\n");
305 return -EINVAL;
308 phy->gpio_en = desc_to_gpio(gpiod_en);
309 phy->gpio_fw = desc_to_gpio(gpiod_fw);
311 return 0;
314 static int nxp_nci_i2c_probe(struct i2c_client *client,
315 const struct i2c_device_id *id)
317 struct nxp_nci_i2c_phy *phy;
318 struct nxp_nci_nfc_platform_data *pdata;
319 int r;
321 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
322 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
323 r = -ENODEV;
324 goto probe_exit;
327 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
328 GFP_KERNEL);
329 if (!phy) {
330 r = -ENOMEM;
331 goto probe_exit;
334 phy->i2c_dev = client;
335 i2c_set_clientdata(client, phy);
337 pdata = client->dev.platform_data;
339 if (!pdata && client->dev.of_node) {
340 r = nxp_nci_i2c_parse_devtree(client);
341 if (r < 0) {
342 nfc_err(&client->dev, "Failed to get DT data\n");
343 goto probe_exit;
345 } else if (pdata) {
346 phy->gpio_en = pdata->gpio_en;
347 phy->gpio_fw = pdata->gpio_fw;
348 } else if (ACPI_HANDLE(&client->dev)) {
349 r = nxp_nci_i2c_acpi_config(phy);
350 if (r < 0)
351 goto probe_exit;
352 goto nci_probe;
353 } else {
354 nfc_err(&client->dev, "No platform data\n");
355 r = -EINVAL;
356 goto probe_exit;
359 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
360 GPIOF_OUT_INIT_LOW, "nxp_nci_en");
361 if (r < 0)
362 goto probe_exit;
364 r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
365 GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
366 if (r < 0)
367 goto probe_exit;
369 nci_probe:
370 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
371 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
372 if (r < 0)
373 goto probe_exit;
375 r = request_threaded_irq(client->irq, NULL,
376 nxp_nci_i2c_irq_thread_fn,
377 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
378 NXP_NCI_I2C_DRIVER_NAME, phy);
379 if (r < 0)
380 nfc_err(&client->dev, "Unable to register IRQ handler\n");
382 probe_exit:
383 return r;
386 static int nxp_nci_i2c_remove(struct i2c_client *client)
388 struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
390 nxp_nci_remove(phy->ndev);
391 free_irq(client->irq, phy);
393 return 0;
396 static const struct i2c_device_id nxp_nci_i2c_id_table[] = {
397 {"nxp-nci_i2c", 0},
400 MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
402 static const struct of_device_id of_nxp_nci_i2c_match[] = {
403 { .compatible = "nxp,nxp-nci-i2c", },
406 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
408 #ifdef CONFIG_ACPI
409 static struct acpi_device_id acpi_id[] = {
410 { "NXP7471" },
411 { },
413 MODULE_DEVICE_TABLE(acpi, acpi_id);
414 #endif
416 static struct i2c_driver nxp_nci_i2c_driver = {
417 .driver = {
418 .name = NXP_NCI_I2C_DRIVER_NAME,
419 .acpi_match_table = ACPI_PTR(acpi_id),
420 .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
422 .probe = nxp_nci_i2c_probe,
423 .id_table = nxp_nci_i2c_id_table,
424 .remove = nxp_nci_i2c_remove,
427 module_i2c_driver(nxp_nci_i2c_driver);
429 MODULE_LICENSE("GPL");
430 MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
431 MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
432 MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");