123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/acpi.h>
- #include <linux/delay.h>
- #include <linux/i2c.h>
- #include <linux/interrupt.h>
- #include <linux/miscdevice.h>
- #include <linux/module.h>
- #include <linux/nfc.h>
- #include <linux/gpio/consumer.h>
- #include <linux/of_gpio.h>
- #include <linux/of_irq.h>
- #include <linux/platform_data/nxp-nci.h>
- #include <linux/unaligned/access_ok.h>
- #include <net/nfc/nfc.h>
- #include "nxp-nci.h"
- #define NXP_NCI_I2C_DRIVER_NAME "nxp-nci_i2c"
- #define NXP_NCI_I2C_MAX_PAYLOAD 32
- struct nxp_nci_i2c_phy {
- struct i2c_client *i2c_dev;
- struct nci_dev *ndev;
- unsigned int gpio_en;
- unsigned int gpio_fw;
- int hard_fault;
- };
- static int nxp_nci_i2c_set_mode(void *phy_id,
- enum nxp_nci_mode mode)
- {
- struct nxp_nci_i2c_phy *phy = (struct nxp_nci_i2c_phy *) phy_id;
- gpio_set_value(phy->gpio_fw, (mode == NXP_NCI_MODE_FW) ? 1 : 0);
- gpio_set_value(phy->gpio_en, (mode != NXP_NCI_MODE_COLD) ? 1 : 0);
- usleep_range(10000, 15000);
- if (mode == NXP_NCI_MODE_COLD)
- phy->hard_fault = 0;
- return 0;
- }
- static int nxp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
- {
- int r;
- struct nxp_nci_i2c_phy *phy = phy_id;
- struct i2c_client *client = phy->i2c_dev;
- if (phy->hard_fault != 0)
- return phy->hard_fault;
- r = i2c_master_send(client, skb->data, skb->len);
- if (r < 0) {
-
- usleep_range(110000, 120000);
- r = i2c_master_send(client, skb->data, skb->len);
- }
- if (r < 0) {
- nfc_err(&client->dev, "Error %d on I2C send\n", r);
- } else if (r != skb->len) {
- nfc_err(&client->dev,
- "Invalid length sent: %u (expected %u)\n",
- r, skb->len);
- r = -EREMOTEIO;
- } else {
-
- r = 0;
- }
- return r;
- }
- static const struct nxp_nci_phy_ops i2c_phy_ops = {
- .set_mode = nxp_nci_i2c_set_mode,
- .write = nxp_nci_i2c_write,
- };
- static int nxp_nci_i2c_fw_read(struct nxp_nci_i2c_phy *phy,
- struct sk_buff **skb)
- {
- struct i2c_client *client = phy->i2c_dev;
- u16 header;
- size_t frame_len;
- int r;
- r = i2c_master_recv(client, (u8 *) &header, NXP_NCI_FW_HDR_LEN);
- if (r < 0) {
- goto fw_read_exit;
- } else if (r != NXP_NCI_FW_HDR_LEN) {
- nfc_err(&client->dev, "Incorrect header length: %u\n", r);
- r = -EBADMSG;
- goto fw_read_exit;
- }
- frame_len = (get_unaligned_be16(&header) & NXP_NCI_FW_FRAME_LEN_MASK) +
- NXP_NCI_FW_CRC_LEN;
- *skb = alloc_skb(NXP_NCI_FW_HDR_LEN + frame_len, GFP_KERNEL);
- if (*skb == NULL) {
- r = -ENOMEM;
- goto fw_read_exit;
- }
- memcpy(skb_put(*skb, NXP_NCI_FW_HDR_LEN), &header, NXP_NCI_FW_HDR_LEN);
- r = i2c_master_recv(client, skb_put(*skb, frame_len), frame_len);
- if (r != frame_len) {
- nfc_err(&client->dev,
- "Invalid frame length: %u (expected %zu)\n",
- r, frame_len);
- r = -EBADMSG;
- goto fw_read_exit_free_skb;
- }
- return 0;
- fw_read_exit_free_skb:
- kfree_skb(*skb);
- fw_read_exit:
- return r;
- }
- static int nxp_nci_i2c_nci_read(struct nxp_nci_i2c_phy *phy,
- struct sk_buff **skb)
- {
- struct nci_ctrl_hdr header;
- struct i2c_client *client = phy->i2c_dev;
- int r;
- r = i2c_master_recv(client, (u8 *) &header, NCI_CTRL_HDR_SIZE);
- if (r < 0) {
- goto nci_read_exit;
- } else if (r != NCI_CTRL_HDR_SIZE) {
- nfc_err(&client->dev, "Incorrect header length: %u\n", r);
- r = -EBADMSG;
- goto nci_read_exit;
- }
- *skb = alloc_skb(NCI_CTRL_HDR_SIZE + header.plen, GFP_KERNEL);
- if (*skb == NULL) {
- r = -ENOMEM;
- goto nci_read_exit;
- }
- memcpy(skb_put(*skb, NCI_CTRL_HDR_SIZE), (void *) &header,
- NCI_CTRL_HDR_SIZE);
- r = i2c_master_recv(client, skb_put(*skb, header.plen), header.plen);
- if (r != header.plen) {
- nfc_err(&client->dev,
- "Invalid frame payload length: %u (expected %u)\n",
- r, header.plen);
- r = -EBADMSG;
- goto nci_read_exit_free_skb;
- }
- return 0;
- nci_read_exit_free_skb:
- kfree_skb(*skb);
- nci_read_exit:
- return r;
- }
- static irqreturn_t nxp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
- {
- struct nxp_nci_i2c_phy *phy = phy_id;
- struct i2c_client *client;
- struct nxp_nci_info *info;
- struct sk_buff *skb = NULL;
- int r = 0;
- if (!phy || !phy->ndev)
- goto exit_irq_none;
- client = phy->i2c_dev;
- if (!client || irq != client->irq)
- goto exit_irq_none;
- info = nci_get_drvdata(phy->ndev);
- if (!info)
- goto exit_irq_none;
- mutex_lock(&info->info_lock);
- if (phy->hard_fault != 0)
- goto exit_irq_handled;
- switch (info->mode) {
- case NXP_NCI_MODE_NCI:
- r = nxp_nci_i2c_nci_read(phy, &skb);
- break;
- case NXP_NCI_MODE_FW:
- r = nxp_nci_i2c_fw_read(phy, &skb);
- break;
- case NXP_NCI_MODE_COLD:
- r = -EREMOTEIO;
- break;
- }
- if (r == -EREMOTEIO) {
- phy->hard_fault = r;
- skb = NULL;
- } else if (r < 0) {
- nfc_err(&client->dev, "Read failed with error %d\n", r);
- goto exit_irq_handled;
- }
- switch (info->mode) {
- case NXP_NCI_MODE_NCI:
- nci_recv_frame(phy->ndev, skb);
- break;
- case NXP_NCI_MODE_FW:
- nxp_nci_fw_recv_frame(phy->ndev, skb);
- break;
- case NXP_NCI_MODE_COLD:
- break;
- }
- exit_irq_handled:
- mutex_unlock(&info->info_lock);
- return IRQ_HANDLED;
- exit_irq_none:
- WARN_ON_ONCE(1);
- return IRQ_NONE;
- }
- static int nxp_nci_i2c_parse_devtree(struct i2c_client *client)
- {
- struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
- struct device_node *pp;
- int r;
- pp = client->dev.of_node;
- if (!pp)
- return -ENODEV;
- r = of_get_named_gpio(pp, "enable-gpios", 0);
- if (r == -EPROBE_DEFER)
- r = of_get_named_gpio(pp, "enable-gpios", 0);
- if (r < 0) {
- nfc_err(&client->dev, "Failed to get EN gpio, error: %d\n", r);
- return r;
- }
- phy->gpio_en = r;
- r = of_get_named_gpio(pp, "firmware-gpios", 0);
- if (r == -EPROBE_DEFER)
- r = of_get_named_gpio(pp, "firmware-gpios", 0);
- if (r < 0) {
- nfc_err(&client->dev, "Failed to get FW gpio, error: %d\n", r);
- return r;
- }
- phy->gpio_fw = r;
- return 0;
- }
- static int nxp_nci_i2c_acpi_config(struct nxp_nci_i2c_phy *phy)
- {
- struct i2c_client *client = phy->i2c_dev;
- struct gpio_desc *gpiod_en, *gpiod_fw;
- gpiod_en = devm_gpiod_get_index(&client->dev, NULL, 2, GPIOD_OUT_LOW);
- gpiod_fw = devm_gpiod_get_index(&client->dev, NULL, 1, GPIOD_OUT_LOW);
- if (IS_ERR(gpiod_en) || IS_ERR(gpiod_fw)) {
- nfc_err(&client->dev, "No GPIOs\n");
- return -EINVAL;
- }
- phy->gpio_en = desc_to_gpio(gpiod_en);
- phy->gpio_fw = desc_to_gpio(gpiod_fw);
- return 0;
- }
- static int nxp_nci_i2c_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- struct nxp_nci_i2c_phy *phy;
- struct nxp_nci_nfc_platform_data *pdata;
- int r;
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
- nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
- r = -ENODEV;
- goto probe_exit;
- }
- phy = devm_kzalloc(&client->dev, sizeof(struct nxp_nci_i2c_phy),
- GFP_KERNEL);
- if (!phy) {
- r = -ENOMEM;
- goto probe_exit;
- }
- phy->i2c_dev = client;
- i2c_set_clientdata(client, phy);
- pdata = client->dev.platform_data;
- if (!pdata && client->dev.of_node) {
- r = nxp_nci_i2c_parse_devtree(client);
- if (r < 0) {
- nfc_err(&client->dev, "Failed to get DT data\n");
- goto probe_exit;
- }
- } else if (pdata) {
- phy->gpio_en = pdata->gpio_en;
- phy->gpio_fw = pdata->gpio_fw;
- } else if (ACPI_HANDLE(&client->dev)) {
- r = nxp_nci_i2c_acpi_config(phy);
- if (r < 0)
- goto probe_exit;
- goto nci_probe;
- } else {
- nfc_err(&client->dev, "No platform data\n");
- r = -EINVAL;
- goto probe_exit;
- }
- r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
- GPIOF_OUT_INIT_LOW, "nxp_nci_en");
- if (r < 0)
- goto probe_exit;
- r = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw,
- GPIOF_OUT_INIT_LOW, "nxp_nci_fw");
- if (r < 0)
- goto probe_exit;
- nci_probe:
- r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
- NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
- if (r < 0)
- goto probe_exit;
- r = request_threaded_irq(client->irq, NULL,
- nxp_nci_i2c_irq_thread_fn,
- IRQF_TRIGGER_RISING | IRQF_ONESHOT,
- NXP_NCI_I2C_DRIVER_NAME, phy);
- if (r < 0)
- nfc_err(&client->dev, "Unable to register IRQ handler\n");
- probe_exit:
- return r;
- }
- static int nxp_nci_i2c_remove(struct i2c_client *client)
- {
- struct nxp_nci_i2c_phy *phy = i2c_get_clientdata(client);
- nxp_nci_remove(phy->ndev);
- free_irq(client->irq, phy);
- return 0;
- }
- static struct i2c_device_id nxp_nci_i2c_id_table[] = {
- {"nxp-nci_i2c", 0},
- {}
- };
- MODULE_DEVICE_TABLE(i2c, nxp_nci_i2c_id_table);
- static const struct of_device_id of_nxp_nci_i2c_match[] = {
- { .compatible = "nxp,nxp-nci-i2c", },
- {},
- };
- MODULE_DEVICE_TABLE(of, of_nxp_nci_i2c_match);
- #ifdef CONFIG_ACPI
- static struct acpi_device_id acpi_id[] = {
- { "NXP7471" },
- { },
- };
- MODULE_DEVICE_TABLE(acpi, acpi_id);
- #endif
- static struct i2c_driver nxp_nci_i2c_driver = {
- .driver = {
- .name = NXP_NCI_I2C_DRIVER_NAME,
- .acpi_match_table = ACPI_PTR(acpi_id),
- .of_match_table = of_match_ptr(of_nxp_nci_i2c_match),
- },
- .probe = nxp_nci_i2c_probe,
- .id_table = nxp_nci_i2c_id_table,
- .remove = nxp_nci_i2c_remove,
- };
- module_i2c_driver(nxp_nci_i2c_driver);
- MODULE_LICENSE("GPL");
- MODULE_DESCRIPTION("I2C driver for NXP NCI NFC controllers");
- MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");
- MODULE_AUTHOR("Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>");
|