2 * I2C Link Layer for ST NCI NFC controller familly based Driver
3 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/gpio.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_gpio.h>
26 #include <linux/acpi.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/nfc.h>
33 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
36 #define ST_NCI_FRAME_HEADROOM 1
37 #define ST_NCI_FRAME_TAILROOM 0
39 #define ST_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
40 #define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
42 #define ST_NCI_DRIVER_NAME "st_nci"
43 #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
45 #define ST_NCI_GPIO_NAME_RESET "reset"
47 struct st_nci_i2c_phy {
48 struct i2c_client *i2c_dev;
49 struct llt_ndlc *ndlc;
53 unsigned int gpio_reset;
54 unsigned int irq_polarity;
56 struct st_nci_se_status se_status;
59 static int st_nci_i2c_enable(void *phy_id)
61 struct st_nci_i2c_phy *phy = phy_id;
63 gpio_set_value(phy->gpio_reset, 0);
64 usleep_range(10000, 15000);
65 gpio_set_value(phy->gpio_reset, 1);
66 usleep_range(80000, 85000);
68 if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
69 enable_irq(phy->i2c_dev->irq);
70 phy->irq_active = true;
76 static void st_nci_i2c_disable(void *phy_id)
78 struct st_nci_i2c_phy *phy = phy_id;
80 disable_irq_nosync(phy->i2c_dev->irq);
81 phy->irq_active = false;
85 * Writing a frame must not return the number of written bytes.
86 * It must return either zero for success, or <0 for error.
87 * In addition, it must not alter the skb
89 static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
92 struct st_nci_i2c_phy *phy = phy_id;
93 struct i2c_client *client = phy->i2c_dev;
95 if (phy->ndlc->hard_fault != 0)
96 return phy->ndlc->hard_fault;
98 r = i2c_master_send(client, skb->data, skb->len);
99 if (r < 0) { /* Retry, chip was in standby */
100 usleep_range(1000, 4000);
101 r = i2c_master_send(client, skb->data, skb->len);
115 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
117 * 0 : if received frame is complete
118 * -EREMOTEIO : i2c read error (fatal)
119 * -EBADMSG : frame was incorrect and discarded
120 * -ENOMEM : cannot allocate skb, frame dropped
122 static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
123 struct sk_buff **skb)
127 u8 buf[ST_NCI_I2C_MAX_SIZE];
128 struct i2c_client *client = phy->i2c_dev;
130 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
131 if (r < 0) { /* Retry, chip was in standby */
132 usleep_range(1000, 4000);
133 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
136 if (r != ST_NCI_I2C_MIN_SIZE)
139 len = be16_to_cpu(*(__be16 *) (buf + 2));
140 if (len > ST_NCI_I2C_MAX_SIZE) {
141 nfc_err(&client->dev, "invalid frame len\n");
145 *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
149 skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
150 skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
151 memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
156 r = i2c_master_recv(client, buf, len);
163 memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
169 * Reads an ndlc frame from the chip.
171 * On ST_NCI, IRQ goes in idle state when read starts.
173 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
175 struct st_nci_i2c_phy *phy = phy_id;
176 struct i2c_client *client;
177 struct sk_buff *skb = NULL;
180 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
185 client = phy->i2c_dev;
186 dev_dbg(&client->dev, "IRQ\n");
188 if (phy->ndlc->hard_fault)
191 if (!phy->ndlc->powered) {
192 st_nci_i2c_disable(phy);
196 r = st_nci_i2c_read(phy, &skb);
197 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
200 ndlc_recv(phy->ndlc, skb);
205 static struct nfc_phy_ops i2c_phy_ops = {
206 .write = st_nci_i2c_write,
207 .enable = st_nci_i2c_enable,
208 .disable = st_nci_i2c_disable,
211 static int st_nci_i2c_acpi_request_resources(struct i2c_client *client)
213 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
214 struct gpio_desc *gpiod_reset;
215 struct device *dev = &client->dev;
218 /* Get RESET GPIO from ACPI */
219 gpiod_reset = devm_gpiod_get_index(dev, ST_NCI_GPIO_NAME_RESET, 1,
221 if (IS_ERR(gpiod_reset)) {
222 nfc_err(dev, "Unable to get RESET GPIO\n");
226 phy->gpio_reset = desc_to_gpio(gpiod_reset);
228 phy->irq_polarity = irq_get_trigger_type(client->irq);
230 phy->se_status.is_ese_present = false;
231 phy->se_status.is_uicc_present = false;
233 if (device_property_present(dev, "ese-present")) {
234 device_property_read_u8(dev, "ese-present", &tmp);
235 phy->se_status.is_ese_present = tmp;
238 if (device_property_present(dev, "uicc-present")) {
239 device_property_read_u8(dev, "uicc-present", &tmp);
240 phy->se_status.is_uicc_present = tmp;
246 static int st_nci_i2c_of_request_resources(struct i2c_client *client)
248 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
249 struct device_node *pp;
253 pp = client->dev.of_node;
257 /* Get GPIO from device tree */
258 gpio = of_get_named_gpio(pp, "reset-gpios", 0);
260 nfc_err(&client->dev,
261 "Failed to retrieve reset-gpios from device tree\n");
265 /* GPIO request and configuration */
266 r = devm_gpio_request_one(&client->dev, gpio,
267 GPIOF_OUT_INIT_HIGH, ST_NCI_GPIO_NAME_RESET);
269 nfc_err(&client->dev, "Failed to request reset pin\n");
272 phy->gpio_reset = gpio;
274 phy->irq_polarity = irq_get_trigger_type(client->irq);
276 phy->se_status.is_ese_present =
277 of_property_read_bool(pp, "ese-present");
278 phy->se_status.is_uicc_present =
279 of_property_read_bool(pp, "uicc-present");
284 static int st_nci_i2c_probe(struct i2c_client *client,
285 const struct i2c_device_id *id)
287 struct st_nci_i2c_phy *phy;
290 dev_dbg(&client->dev, "%s\n", __func__);
291 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
293 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
294 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
298 phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy),
303 phy->i2c_dev = client;
305 i2c_set_clientdata(client, phy);
307 if (client->dev.of_node) {
308 r = st_nci_i2c_of_request_resources(client);
310 nfc_err(&client->dev, "No platform data\n");
313 } else if (ACPI_HANDLE(&client->dev)) {
314 r = st_nci_i2c_acpi_request_resources(client);
316 nfc_err(&client->dev, "Cannot get ACPI data\n");
320 nfc_err(&client->dev,
321 "st_nci platform resources not available\n");
325 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
326 ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
327 &phy->ndlc, &phy->se_status);
329 nfc_err(&client->dev, "Unable to register ndlc layer\n");
333 phy->irq_active = true;
334 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
335 st_nci_irq_thread_fn,
336 phy->irq_polarity | IRQF_ONESHOT,
337 ST_NCI_DRIVER_NAME, phy);
339 nfc_err(&client->dev, "Unable to register IRQ handler\n");
344 static int st_nci_i2c_remove(struct i2c_client *client)
346 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
348 dev_dbg(&client->dev, "%s\n", __func__);
350 ndlc_remove(phy->ndlc);
355 static struct i2c_device_id st_nci_i2c_id_table[] = {
356 {ST_NCI_DRIVER_NAME, 0},
359 MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
361 static const struct acpi_device_id st_nci_i2c_acpi_match[] = {
366 MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
368 static const struct of_device_id of_st_nci_i2c_match[] = {
369 { .compatible = "st,st21nfcb-i2c", },
370 { .compatible = "st,st21nfcb_i2c", },
371 { .compatible = "st,st21nfcc-i2c", },
374 MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
376 static struct i2c_driver st_nci_i2c_driver = {
378 .name = ST_NCI_I2C_DRIVER_NAME,
379 .of_match_table = of_match_ptr(of_st_nci_i2c_match),
380 .acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
382 .probe = st_nci_i2c_probe,
383 .id_table = st_nci_i2c_id_table,
384 .remove = st_nci_i2c_remove,
386 module_i2c_driver(st_nci_i2c_driver);
388 MODULE_LICENSE("GPL");
389 MODULE_DESCRIPTION(DRIVER_DESC);