forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/nfc/nxp-nci/i2c.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * I2C link layer for the NXP NCI driver
34 *
....@@ -9,21 +10,7 @@
910 *
1011 * Derived from PN544 device driver:
1112 * 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/>.
2413 */
25
-
26
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2714
2815 #include <linux/acpi.h>
2916 #include <linux/delay.h>
....@@ -32,9 +19,6 @@
3219 #include <linux/module.h>
3320 #include <linux/nfc.h>
3421 #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>
3822 #include <asm/unaligned.h>
3923
4024 #include <net/nfc/nfc.h>
....@@ -49,8 +33,8 @@
4933 struct i2c_client *i2c_dev;
5034 struct nci_dev *ndev;
5135
52
- unsigned int gpio_en;
53
- unsigned int gpio_fw;
36
+ struct gpio_desc *gpiod_en;
37
+ struct gpio_desc *gpiod_fw;
5438
5539 int hard_fault; /*
5640 * < 0 if hardware error occurred (e.g. i2c err)
....@@ -63,8 +47,8 @@
6347 {
6448 struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
6549
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);
50
+ gpiod_set_value(phy->gpiod_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
51
+ gpiod_set_value(phy->gpiod_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
6852 usleep_range(10000, 15000);
6953
7054 if (mode == NXP_NCI_MODE_COLD)
....@@ -138,7 +122,9 @@
138122 skb_put_data(*skb, &header, NXP_NCI_FW_HDR_LEN);
139123
140124 r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
141
- if (r != frame_len) {
125
+ if (r < 0) {
126
+ goto fw_read_exit_free_skb;
127
+ } else if (r != frame_len) {
142128 nfc_err(&client->dev,
143129 "Invalid frame length: %u (expected %zu)\n",
144130 r, frame_len);
....@@ -178,8 +164,13 @@
178164
179165 skb_put_data(*skb, (void *)&header, NCI_CTRL_HDR_SIZE);
180166
167
+ if (!header.plen)
168
+ return 0;
169
+
181170 r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
182
- if (r != header.plen) {
171
+ if (r < 0) {
172
+ goto nci_read_exit_free_skb;
173
+ } else if (r != header.plen) {
183174 nfc_err(&client->dev,
184175 "Invalid frame payload length: %u (expected %u)\n",
185176 r, header.plen);
....@@ -263,116 +254,55 @@
263254 return IRQ_NONE;
264255 }
265256
266
-static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
267
-{
268
- struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
269
- struct device_node *pp;
270
- int r;
257
+static const struct acpi_gpio_params firmware_gpios = { 1, 0, false };
258
+static const struct acpi_gpio_params enable_gpios = { 2, 0, false };
271259
272
- pp = client->dev.of_node;
273
- if (!pp)
274
- return -ENODEV;
275
-
276
- r = of_get_named_gpio(pp, "enable-gpios", 0);
277
- if (r == -EPROBE_DEFER)
278
- r = of_get_named_gpio(pp, "enable-gpios", 0);
279
- if (r < 0) {
280
- nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
281
- return r;
282
- }
283
- phy->gpio_en = r;
284
-
285
- r = of_get_named_gpio(pp, "firmware-gpios", 0);
286
- if (r == -EPROBE_DEFER)
287
- r = of_get_named_gpio(pp, "firmware-gpios", 0);
288
- if (r < 0) {
289
- nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
290
- return r;
291
- }
292
- phy->gpio_fw = r;
293
-
294
- return 0;
295
-}
296
-
297
-static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
298
-{
299
- struct i2c_client *client = phy->i2c_dev;
300
- struct gpio_desc *gpiod_en, *gpiod_fw;
301
-
302
- gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
303
- gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
304
-
305
- if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
306
- nfc_err(&client->dev, "No GPIOs\n");
307
- return -EINVAL;
308
- }
309
-
310
- phy->gpio_en = desc_to_gpio(gpiod_en);
311
- phy->gpio_fw = desc_to_gpio(gpiod_fw);
312
-
313
- return 0;
314
-}
260
+static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
261
+ { "enable-gpios", &enable_gpios, 1 },
262
+ { "firmware-gpios", &firmware_gpios, 1 },
263
+ { }
264
+};
315265
316266 static int nxp_nci_i2c_probe(struct i2c_client *client,
317267 const struct i2c_device_id *id)
318268 {
269
+ struct device *dev = &client->dev;
319270 struct nxp_nci_i2c_phy *phy;
320
- struct nxp_nci_nfc_platform_data *pdata;
321271 int r;
322272
323273 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
324274 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
325
- r = -ENODEV;
326
- goto probe_exit;
275
+ return -ENODEV;
327276 }
328277
329278 phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
330279 GFP_KERNEL);
331
- if (!phy) {
332
- r = -ENOMEM;
333
- goto probe_exit;
334
- }
280
+ if (!phy)
281
+ return -ENOMEM;
335282
336283 phy->i2c_dev = client;
337284 i2c_set_clientdata(client, phy);
338285
339
- pdata = client->dev.platform_data;
286
+ r = devm_acpi_dev_add_driver_gpios(dev, acpi_nxp_nci_gpios);
287
+ if (r)
288
+ dev_dbg(dev, "Unable to add GPIO mapping table\n");
340289
341
- if (!pdata && client->dev.of_node) {
342
- r = nxp_nci_i2c_parse_devtree(client);
343
- if (r < 0) {
344
- nfc_err(&client->dev, "Failed to get DT data\n");
345
- goto probe_exit;
346
- }
347
- } else if (pdata) {
348
- phy->gpio_en = pdata->gpio_en;
349
- phy->gpio_fw = pdata->gpio_fw;
350
- } else if (ACPI_HANDLE(&client->dev)) {
351
- r = nxp_nci_i2c_acpi_config(phy);
352
- if (r < 0)
353
- goto probe_exit;
354
- goto nci_probe;
355
- } else {
356
- nfc_err(&client->dev, "No platform data\n");
357
- r = -EINVAL;
358
- goto probe_exit;
290
+ phy->gpiod_en = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
291
+ if (IS_ERR(phy->gpiod_en)) {
292
+ nfc_err(dev, "Failed to get EN gpio\n");
293
+ return PTR_ERR(phy->gpiod_en);
359294 }
360295
361
- r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
362
- GPIOF_OUT_INIT_LOW, "nxp_nci_en");
363
- if (r < 0)
364
- goto probe_exit;
296
+ phy->gpiod_fw = devm_gpiod_get(dev, "firmware", GPIOD_OUT_LOW);
297
+ if (IS_ERR(phy->gpiod_fw)) {
298
+ nfc_err(dev, "Failed to get FW gpio\n");
299
+ return PTR_ERR(phy->gpiod_fw);
300
+ }
365301
366
- r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
367
- GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
368
- if (r < 0)
369
- goto probe_exit;
370
-
371
-nci_probe:
372302 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
373303 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
374304 if (r < 0)
375
- goto probe_exit;
305
+ return r;
376306
377307 r = request_threaded_irq(client->irq, NULL,
378308 nxp_nci_i2c_irq_thread_fn,
....@@ -381,7 +311,6 @@
381311 if (r < 0)
382312 nfc_err(&client->dev, "Unable to register IRQ handler\n");
383313
384
-probe_exit:
385314 return r;
386315 }
387316
....@@ -403,14 +332,15 @@
403332
404333 static const struct of_device_id of_nxp_nci_i2c_match[] = {
405334 { .compatible = "nxp,nxp-nci-i2c", },
406
- {},
335
+ {}
407336 };
408337 MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
409338
410339 #ifdef CONFIG_ACPI
411
-static struct acpi_device_id acpi_id[] = {
340
+static const struct acpi_device_id acpi_id[] = {
341
+ { "NXP1001" },
412342 { "NXP7471" },
413
- { },
343
+ { }
414344 };
415345 MODULE_DEVICE_TABLE(acpi, acpi_id);
416346 #endif
....@@ -419,7 +349,7 @@
419349 .driver = {
420350 .name = NXP_NCI_I2C_DRIVER_NAME,
421351 .acpi_match_table = ACPI_PTR(acpi_id),
422
- .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
352
+ .of_match_table = of_nxp_nci_i2c_match,
423353 },
424354 .probe = nxp_nci_i2c_probe,
425355 .id_table = nxp_nci_i2c_id_table,