blob: ccf6152ebb9f2680664146b7e9423d045733d848 [file] [log] [blame]
Thomas Gleixner46fe7772019-05-31 01:09:57 -07001// SPDX-License-Identifier: GPL-2.0-only
Christophe Ricard35630df2014-05-25 22:35:38 +02002/*
Christophe Ricarded06aeef2015-06-09 22:26:05 +02003 * I2C Link Layer for ST NCI NFC controller familly based Driver
4 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
Christophe Ricard35630df2014-05-25 22:35:38 +02005 */
6
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
Christophe Ricard35630df2014-05-25 22:35:38 +02009#include <linux/module.h>
10#include <linux/i2c.h>
Christophe Ricarded6a2f32015-12-23 23:45:09 +010011#include <linux/gpio/consumer.h>
Christophe Ricarded6a2f32015-12-23 23:45:09 +010012#include <linux/acpi.h>
Christophe Ricard35630df2014-05-25 22:35:38 +020013#include <linux/interrupt.h>
14#include <linux/delay.h>
15#include <linux/nfc.h>
Andy Shevchenkoa89e68f2017-06-19 13:08:54 +030016#include <linux/of.h>
Christophe Ricard35630df2014-05-25 22:35:38 +020017
Christophe Ricarde67e7e52015-10-25 22:54:17 +010018#include "st-nci.h"
Christophe Ricard35630df2014-05-25 22:35:38 +020019
Christophe Ricard30458aa2015-08-14 22:33:31 +020020#define DRIVER_DESC "NCI NFC driver for ST_NCI"
Christophe Ricard35630df2014-05-25 22:35:38 +020021
22/* ndlc header */
Christophe Ricard064d0042015-10-25 22:54:44 +010023#define ST_NCI_FRAME_HEADROOM 1
Christophe Ricard30458aa2015-08-14 22:33:31 +020024#define ST_NCI_FRAME_TAILROOM 0
Christophe Ricard35630df2014-05-25 22:35:38 +020025
Christophe Ricarded06aeef2015-06-09 22:26:05 +020026#define ST_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
27#define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
Christophe Ricard35630df2014-05-25 22:35:38 +020028
Andy Shevchenko61a04102017-06-19 13:08:52 +030029#define ST_NCI_DRIVER_NAME "st_nci"
Christophe Ricarded06aeef2015-06-09 22:26:05 +020030#define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
Christophe Ricard35630df2014-05-25 22:35:38 +020031
Christophe Ricarded06aeef2015-06-09 22:26:05 +020032struct st_nci_i2c_phy {
Christophe Ricard35630df2014-05-25 22:35:38 +020033 struct i2c_client *i2c_dev;
34 struct llt_ndlc *ndlc;
35
Christophe Ricardbb2496c2015-10-25 22:54:48 +010036 bool irq_active;
37
Andy Shevchenkoa89e68f2017-06-19 13:08:54 +030038 struct gpio_desc *gpiod_reset;
Christophe Ricard3648dc62015-10-25 22:54:39 +010039
40 struct st_nci_se_status se_status;
Christophe Ricard35630df2014-05-25 22:35:38 +020041};
42
Christophe Ricarded06aeef2015-06-09 22:26:05 +020043static int st_nci_i2c_enable(void *phy_id)
Christophe Ricard35630df2014-05-25 22:35:38 +020044{
Christophe Ricarded06aeef2015-06-09 22:26:05 +020045 struct st_nci_i2c_phy *phy = phy_id;
Christophe Ricard35630df2014-05-25 22:35:38 +020046
Andy Shevchenkoa89e68f2017-06-19 13:08:54 +030047 gpiod_set_value(phy->gpiod_reset, 0);
Christophe Ricard35630df2014-05-25 22:35:38 +020048 usleep_range(10000, 15000);
Andy Shevchenkoa89e68f2017-06-19 13:08:54 +030049 gpiod_set_value(phy->gpiod_reset, 1);
Christophe Ricard35630df2014-05-25 22:35:38 +020050 usleep_range(80000, 85000);
51
Christophe Ricardbb2496c2015-10-25 22:54:48 +010052 if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
Christophe Ricard05f09392015-06-06 13:16:51 +020053 enable_irq(phy->i2c_dev->irq);
Christophe Ricardbb2496c2015-10-25 22:54:48 +010054 phy->irq_active = true;
55 }
Christophe Ricard05f09392015-06-06 13:16:51 +020056
Christophe Ricard35630df2014-05-25 22:35:38 +020057 return 0;
58}
59
Christophe Ricarded06aeef2015-06-09 22:26:05 +020060static void st_nci_i2c_disable(void *phy_id)
Christophe Ricard35630df2014-05-25 22:35:38 +020061{
Christophe Ricarded06aeef2015-06-09 22:26:05 +020062 struct st_nci_i2c_phy *phy = phy_id;
Christophe Ricard35630df2014-05-25 22:35:38 +020063
Christophe Ricard05f09392015-06-06 13:16:51 +020064 disable_irq_nosync(phy->i2c_dev->irq);
Christophe Ricardbb2496c2015-10-25 22:54:48 +010065 phy->irq_active = false;
Christophe Ricard35630df2014-05-25 22:35:38 +020066}
67
Christophe Ricard35630df2014-05-25 22:35:38 +020068/*
69 * Writing a frame must not return the number of written bytes.
70 * It must return either zero for success, or <0 for error.
71 * In addition, it must not alter the skb
72 */
Christophe Ricarded06aeef2015-06-09 22:26:05 +020073static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
Christophe Ricard35630df2014-05-25 22:35:38 +020074{
Colin Ian King23ec8ea2019-07-02 14:16:42 +010075 int r;
Christophe Ricarded06aeef2015-06-09 22:26:05 +020076 struct st_nci_i2c_phy *phy = phy_id;
Christophe Ricard35630df2014-05-25 22:35:38 +020077 struct i2c_client *client = phy->i2c_dev;
78
Christophe Ricard4294e322014-09-13 10:28:47 +020079 if (phy->ndlc->hard_fault != 0)
80 return phy->ndlc->hard_fault;
Christophe Ricard35630df2014-05-25 22:35:38 +020081
82 r = i2c_master_send(client, skb->data, skb->len);
Christophe Ricardd4a41d12015-03-31 08:02:15 +020083 if (r < 0) { /* Retry, chip was in standby */
Christophe Ricard35630df2014-05-25 22:35:38 +020084 usleep_range(1000, 4000);
85 r = i2c_master_send(client, skb->data, skb->len);
86 }
87
88 if (r >= 0) {
89 if (r != skb->len)
90 r = -EREMOTEIO;
91 else
92 r = 0;
93 }
94
Christophe Ricard35630df2014-05-25 22:35:38 +020095 return r;
96}
97
98/*
99 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
100 * returns:
Christophe Ricarde7723b32015-08-14 22:33:32 +0200101 * 0 : if received frame is complete
Christophe Ricard35630df2014-05-25 22:35:38 +0200102 * -EREMOTEIO : i2c read error (fatal)
103 * -EBADMSG : frame was incorrect and discarded
Christophe Ricarde7723b32015-08-14 22:33:32 +0200104 * -ENOMEM : cannot allocate skb, frame dropped
Christophe Ricard35630df2014-05-25 22:35:38 +0200105 */
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200106static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
Christophe Ricard35630df2014-05-25 22:35:38 +0200107 struct sk_buff **skb)
108{
109 int r;
110 u8 len;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200111 u8 buf[ST_NCI_I2C_MAX_SIZE];
Christophe Ricard35630df2014-05-25 22:35:38 +0200112 struct i2c_client *client = phy->i2c_dev;
113
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200114 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
Christophe Ricardd4a41d12015-03-31 08:02:15 +0200115 if (r < 0) { /* Retry, chip was in standby */
Christophe Ricard35630df2014-05-25 22:35:38 +0200116 usleep_range(1000, 4000);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200117 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
Christophe Ricardac633ba2014-09-03 23:30:26 +0200118 }
119
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200120 if (r != ST_NCI_I2C_MIN_SIZE)
Christophe Ricard35630df2014-05-25 22:35:38 +0200121 return -EREMOTEIO;
Christophe Ricard35630df2014-05-25 22:35:38 +0200122
123 len = be16_to_cpu(*(__be16 *) (buf + 2));
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200124 if (len > ST_NCI_I2C_MAX_SIZE) {
Christophe Ricard35630df2014-05-25 22:35:38 +0200125 nfc_err(&client->dev, "invalid frame len\n");
126 return -EBADMSG;
127 }
128
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200129 *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
Christophe Ricard35630df2014-05-25 22:35:38 +0200130 if (*skb == NULL)
131 return -ENOMEM;
132
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200133 skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
134 skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
135 memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
Christophe Ricard35630df2014-05-25 22:35:38 +0200136
137 if (!len)
138 return 0;
139
140 r = i2c_master_recv(client, buf, len);
141 if (r != len) {
142 kfree_skb(*skb);
143 return -EREMOTEIO;
144 }
145
146 skb_put(*skb, len);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200147 memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
Christophe Ricard35630df2014-05-25 22:35:38 +0200148
Christophe Ricard35630df2014-05-25 22:35:38 +0200149 return 0;
150}
151
152/*
153 * Reads an ndlc frame from the chip.
154 *
Christophe Ricard30458aa2015-08-14 22:33:31 +0200155 * On ST_NCI, IRQ goes in idle state when read starts.
Christophe Ricard35630df2014-05-25 22:35:38 +0200156 */
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200157static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
Christophe Ricard35630df2014-05-25 22:35:38 +0200158{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200159 struct st_nci_i2c_phy *phy = phy_id;
Christophe Ricard35630df2014-05-25 22:35:38 +0200160 struct i2c_client *client;
161 struct sk_buff *skb = NULL;
162 int r;
163
Christophe Ricard72744962015-01-25 23:33:29 +0100164 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
Christophe Ricard35630df2014-05-25 22:35:38 +0200165 WARN_ON_ONCE(1);
166 return IRQ_NONE;
167 }
168
169 client = phy->i2c_dev;
170 dev_dbg(&client->dev, "IRQ\n");
171
Christophe Ricard4294e322014-09-13 10:28:47 +0200172 if (phy->ndlc->hard_fault)
Christophe Ricard35630df2014-05-25 22:35:38 +0200173 return IRQ_HANDLED;
174
Christophe Ricard183fe2d2015-06-06 13:16:50 +0200175 if (!phy->ndlc->powered) {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200176 st_nci_i2c_disable(phy);
Christophe Ricard35630df2014-05-25 22:35:38 +0200177 return IRQ_HANDLED;
178 }
179
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200180 r = st_nci_i2c_read(phy, &skb);
Christophe Ricard4294e322014-09-13 10:28:47 +0200181 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
Christophe Ricard35630df2014-05-25 22:35:38 +0200182 return IRQ_HANDLED;
Christophe Ricard35630df2014-05-25 22:35:38 +0200183
184 ndlc_recv(phy->ndlc, skb);
185
186 return IRQ_HANDLED;
187}
188
Krzysztof Kozlowski7a5e98d2021-07-24 23:47:36 +0200189static const struct nfc_phy_ops i2c_phy_ops = {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200190 .write = st_nci_i2c_write,
191 .enable = st_nci_i2c_enable,
192 .disable = st_nci_i2c_disable,
Christophe Ricard35630df2014-05-25 22:35:38 +0200193};
194
Andy Shevchenko85d23e72017-06-19 13:08:56 +0300195static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
196
197static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
198 { "reset-gpios", &reset_gpios, 1 },
199 {},
200};
201
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200202static int st_nci_i2c_probe(struct i2c_client *client,
Christophe Ricard35630df2014-05-25 22:35:38 +0200203 const struct i2c_device_id *id)
204{
Andy Shevchenko75719b22017-06-19 13:08:55 +0300205 struct device *dev = &client->dev;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200206 struct st_nci_i2c_phy *phy;
Christophe Ricard35630df2014-05-25 22:35:38 +0200207 int r;
208
Christophe Ricard35630df2014-05-25 22:35:38 +0200209 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
210 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
211 return -ENODEV;
212 }
213
Andy Shevchenkoc7451202017-06-19 13:08:57 +0300214 phy = devm_kzalloc(dev, sizeof(struct st_nci_i2c_phy), GFP_KERNEL);
Joe Perches3590ebc2015-04-07 00:17:00 -0700215 if (!phy)
Christophe Ricard35630df2014-05-25 22:35:38 +0200216 return -ENOMEM;
Christophe Ricard35630df2014-05-25 22:35:38 +0200217
218 phy->i2c_dev = client;
219
220 i2c_set_clientdata(client, phy);
221
Andy Shevchenkoc7451202017-06-19 13:08:57 +0300222 r = devm_acpi_dev_add_driver_gpios(dev, acpi_st_nci_gpios);
223 if (r)
224 dev_dbg(dev, "Unable to add GPIO mapping table\n");
225
226 /* Get RESET GPIO */
227 phy->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
228 if (IS_ERR(phy->gpiod_reset)) {
229 nfc_err(dev, "Unable to get RESET GPIO\n");
Christophe Ricard35630df2014-05-25 22:35:38 +0200230 return -ENODEV;
231 }
232
Andy Shevchenko75719b22017-06-19 13:08:55 +0300233 phy->se_status.is_ese_present =
234 device_property_read_bool(dev, "ese-present");
235 phy->se_status.is_uicc_present =
236 device_property_read_bool(dev, "uicc-present");
237
Christophe Ricard72744962015-01-25 23:33:29 +0100238 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
Christophe Ricard30458aa2015-08-14 22:33:31 +0200239 ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
Christophe Ricard3648dc62015-10-25 22:54:39 +0100240 &phy->ndlc, &phy->se_status);
Christophe Ricard72744962015-01-25 23:33:29 +0100241 if (r < 0) {
242 nfc_err(&client->dev, "Unable to register ndlc layer\n");
243 return r;
244 }
245
Christophe Ricardbb2496c2015-10-25 22:54:48 +0100246 phy->irq_active = true;
Christophe Ricard35630df2014-05-25 22:35:38 +0200247 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200248 st_nci_irq_thread_fn,
Andy Shevchenko1af9fea2017-06-19 13:08:53 +0300249 IRQF_ONESHOT,
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200250 ST_NCI_DRIVER_NAME, phy);
Christophe Ricard72744962015-01-25 23:33:29 +0100251 if (r < 0)
Christophe Ricard35630df2014-05-25 22:35:38 +0200252 nfc_err(&client->dev, "Unable to register IRQ handler\n");
Christophe Ricard35630df2014-05-25 22:35:38 +0200253
Christophe Ricard72744962015-01-25 23:33:29 +0100254 return r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200255}
256
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200257static int st_nci_i2c_remove(struct i2c_client *client)
Christophe Ricard35630df2014-05-25 22:35:38 +0200258{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200259 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard35630df2014-05-25 22:35:38 +0200260
Christophe Ricard35630df2014-05-25 22:35:38 +0200261 ndlc_remove(phy->ndlc);
262
Christophe Ricard35630df2014-05-25 22:35:38 +0200263 return 0;
264}
265
Arvind Yadav98455302017-08-21 22:33:59 +0530266static const struct i2c_device_id st_nci_i2c_id_table[] = {
Christophe Ricard32528972015-12-23 23:45:05 +0100267 {ST_NCI_DRIVER_NAME, 0},
268 {}
269};
270MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
271
Krzysztof Kozlowski255fcc72021-05-28 08:41:58 -0400272static const struct acpi_device_id st_nci_i2c_acpi_match[] __maybe_unused = {
Christophe Ricarded6a2f32015-12-23 23:45:09 +0100273 {"SMO2101"},
274 {"SMO2102"},
275 {}
276};
277MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
278
Krzysztof Kozlowski255fcc72021-05-28 08:41:58 -0400279static const struct of_device_id of_st_nci_i2c_match[] __maybe_unused = {
Christophe Ricard1a94cb62014-12-08 22:08:07 +0100280 { .compatible = "st,st21nfcb-i2c", },
Christophe Ricard35630df2014-05-25 22:35:38 +0200281 { .compatible = "st,st21nfcb_i2c", },
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200282 { .compatible = "st,st21nfcc-i2c", },
Christophe Ricard35630df2014-05-25 22:35:38 +0200283 {}
284};
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200285MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
Christophe Ricard35630df2014-05-25 22:35:38 +0200286
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200287static struct i2c_driver st_nci_i2c_driver = {
Christophe Ricard35630df2014-05-25 22:35:38 +0200288 .driver = {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200289 .name = ST_NCI_I2C_DRIVER_NAME,
290 .of_match_table = of_match_ptr(of_st_nci_i2c_match),
Christophe Ricarded6a2f32015-12-23 23:45:09 +0100291 .acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
Christophe Ricard35630df2014-05-25 22:35:38 +0200292 },
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200293 .probe = st_nci_i2c_probe,
294 .id_table = st_nci_i2c_id_table,
295 .remove = st_nci_i2c_remove,
Christophe Ricard35630df2014-05-25 22:35:38 +0200296};
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200297module_i2c_driver(st_nci_i2c_driver);
Christophe Ricard35630df2014-05-25 22:35:38 +0200298
299MODULE_LICENSE("GPL");
300MODULE_DESCRIPTION(DRIVER_DESC);