blob: cbd968f013c70db9bc60ec9ed766a2ea9fa22a54 [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 sk_buff *skb = NULL;
161 int r;
162
Christophe Ricard72744962015-01-25 23:33:29 +0100163 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
Christophe Ricard35630df2014-05-25 22:35:38 +0200164 WARN_ON_ONCE(1);
165 return IRQ_NONE;
166 }
167
Christophe Ricard4294e322014-09-13 10:28:47 +0200168 if (phy->ndlc->hard_fault)
Christophe Ricard35630df2014-05-25 22:35:38 +0200169 return IRQ_HANDLED;
170
Christophe Ricard183fe2d2015-06-06 13:16:50 +0200171 if (!phy->ndlc->powered) {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200172 st_nci_i2c_disable(phy);
Christophe Ricard35630df2014-05-25 22:35:38 +0200173 return IRQ_HANDLED;
174 }
175
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200176 r = st_nci_i2c_read(phy, &skb);
Christophe Ricard4294e322014-09-13 10:28:47 +0200177 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
Christophe Ricard35630df2014-05-25 22:35:38 +0200178 return IRQ_HANDLED;
Christophe Ricard35630df2014-05-25 22:35:38 +0200179
180 ndlc_recv(phy->ndlc, skb);
181
182 return IRQ_HANDLED;
183}
184
Krzysztof Kozlowski7a5e98d2021-07-24 23:47:36 +0200185static const struct nfc_phy_ops i2c_phy_ops = {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200186 .write = st_nci_i2c_write,
187 .enable = st_nci_i2c_enable,
188 .disable = st_nci_i2c_disable,
Christophe Ricard35630df2014-05-25 22:35:38 +0200189};
190
Andy Shevchenko85d23e72017-06-19 13:08:56 +0300191static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
192
193static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
194 { "reset-gpios", &reset_gpios, 1 },
195 {},
196};
197
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200198static int st_nci_i2c_probe(struct i2c_client *client,
Christophe Ricard35630df2014-05-25 22:35:38 +0200199 const struct i2c_device_id *id)
200{
Andy Shevchenko75719b22017-06-19 13:08:55 +0300201 struct device *dev = &client->dev;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200202 struct st_nci_i2c_phy *phy;
Christophe Ricard35630df2014-05-25 22:35:38 +0200203 int r;
204
Christophe Ricard35630df2014-05-25 22:35:38 +0200205 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
206 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
207 return -ENODEV;
208 }
209
Andy Shevchenkoc7451202017-06-19 13:08:57 +0300210 phy = devm_kzalloc(dev, sizeof(struct st_nci_i2c_phy), GFP_KERNEL);
Joe Perches3590ebc2015-04-07 00:17:00 -0700211 if (!phy)
Christophe Ricard35630df2014-05-25 22:35:38 +0200212 return -ENOMEM;
Christophe Ricard35630df2014-05-25 22:35:38 +0200213
214 phy->i2c_dev = client;
215
216 i2c_set_clientdata(client, phy);
217
Andy Shevchenkoc7451202017-06-19 13:08:57 +0300218 r = devm_acpi_dev_add_driver_gpios(dev, acpi_st_nci_gpios);
219 if (r)
220 dev_dbg(dev, "Unable to add GPIO mapping table\n");
221
222 /* Get RESET GPIO */
223 phy->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
224 if (IS_ERR(phy->gpiod_reset)) {
225 nfc_err(dev, "Unable to get RESET GPIO\n");
Christophe Ricard35630df2014-05-25 22:35:38 +0200226 return -ENODEV;
227 }
228
Andy Shevchenko75719b22017-06-19 13:08:55 +0300229 phy->se_status.is_ese_present =
230 device_property_read_bool(dev, "ese-present");
231 phy->se_status.is_uicc_present =
232 device_property_read_bool(dev, "uicc-present");
233
Christophe Ricard72744962015-01-25 23:33:29 +0100234 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
Christophe Ricard30458aa2015-08-14 22:33:31 +0200235 ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
Christophe Ricard3648dc62015-10-25 22:54:39 +0100236 &phy->ndlc, &phy->se_status);
Christophe Ricard72744962015-01-25 23:33:29 +0100237 if (r < 0) {
238 nfc_err(&client->dev, "Unable to register ndlc layer\n");
239 return r;
240 }
241
Christophe Ricardbb2496c2015-10-25 22:54:48 +0100242 phy->irq_active = true;
Christophe Ricard35630df2014-05-25 22:35:38 +0200243 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200244 st_nci_irq_thread_fn,
Andy Shevchenko1af9fea2017-06-19 13:08:53 +0300245 IRQF_ONESHOT,
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200246 ST_NCI_DRIVER_NAME, phy);
Christophe Ricard72744962015-01-25 23:33:29 +0100247 if (r < 0)
Christophe Ricard35630df2014-05-25 22:35:38 +0200248 nfc_err(&client->dev, "Unable to register IRQ handler\n");
Christophe Ricard35630df2014-05-25 22:35:38 +0200249
Christophe Ricard72744962015-01-25 23:33:29 +0100250 return r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200251}
252
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200253static int st_nci_i2c_remove(struct i2c_client *client)
Christophe Ricard35630df2014-05-25 22:35:38 +0200254{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200255 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard35630df2014-05-25 22:35:38 +0200256
Christophe Ricard35630df2014-05-25 22:35:38 +0200257 ndlc_remove(phy->ndlc);
258
Christophe Ricard35630df2014-05-25 22:35:38 +0200259 return 0;
260}
261
Arvind Yadav98455302017-08-21 22:33:59 +0530262static const struct i2c_device_id st_nci_i2c_id_table[] = {
Christophe Ricard32528972015-12-23 23:45:05 +0100263 {ST_NCI_DRIVER_NAME, 0},
264 {}
265};
266MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
267
Krzysztof Kozlowski255fcc72021-05-28 08:41:58 -0400268static const struct acpi_device_id st_nci_i2c_acpi_match[] __maybe_unused = {
Christophe Ricarded6a2f32015-12-23 23:45:09 +0100269 {"SMO2101"},
270 {"SMO2102"},
271 {}
272};
273MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
274
Krzysztof Kozlowski255fcc72021-05-28 08:41:58 -0400275static const struct of_device_id of_st_nci_i2c_match[] __maybe_unused = {
Christophe Ricard1a94cb62014-12-08 22:08:07 +0100276 { .compatible = "st,st21nfcb-i2c", },
Christophe Ricard35630df2014-05-25 22:35:38 +0200277 { .compatible = "st,st21nfcb_i2c", },
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200278 { .compatible = "st,st21nfcc-i2c", },
Christophe Ricard35630df2014-05-25 22:35:38 +0200279 {}
280};
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200281MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
Christophe Ricard35630df2014-05-25 22:35:38 +0200282
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200283static struct i2c_driver st_nci_i2c_driver = {
Christophe Ricard35630df2014-05-25 22:35:38 +0200284 .driver = {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200285 .name = ST_NCI_I2C_DRIVER_NAME,
286 .of_match_table = of_match_ptr(of_st_nci_i2c_match),
Christophe Ricarded6a2f32015-12-23 23:45:09 +0100287 .acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
Christophe Ricard35630df2014-05-25 22:35:38 +0200288 },
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200289 .probe = st_nci_i2c_probe,
290 .id_table = st_nci_i2c_id_table,
291 .remove = st_nci_i2c_remove,
Christophe Ricard35630df2014-05-25 22:35:38 +0200292};
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200293module_i2c_driver(st_nci_i2c_driver);
Christophe Ricard35630df2014-05-25 22:35:38 +0200294
295MODULE_LICENSE("GPL");
296MODULE_DESCRIPTION(DRIVER_DESC);