xref: /linux/drivers/nfc/st-nci/i2c.c (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * I2C Link Layer for ST NCI NFC controller familly based Driver
4  * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/acpi.h>
13 #include <linux/interrupt.h>
14 #include <linux/delay.h>
15 #include <linux/nfc.h>
16 #include <linux/of.h>
17 
18 #include "st-nci.h"
19 
20 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
21 
22 /* ndlc header */
23 #define ST_NCI_FRAME_HEADROOM 1
24 #define ST_NCI_FRAME_TAILROOM 0
25 
26 #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 */
28 
29 #define ST_NCI_DRIVER_NAME "st_nci"
30 #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
31 
32 struct st_nci_i2c_phy {
33 	struct i2c_client *i2c_dev;
34 	struct llt_ndlc *ndlc;
35 
36 	bool irq_active;
37 
38 	struct gpio_desc *gpiod_reset;
39 
40 	struct st_nci_se_status se_status;
41 };
42 
43 static int st_nci_i2c_enable(void *phy_id)
44 {
45 	struct st_nci_i2c_phy *phy = phy_id;
46 
47 	gpiod_set_value(phy->gpiod_reset, 0);
48 	usleep_range(10000, 15000);
49 	gpiod_set_value(phy->gpiod_reset, 1);
50 	usleep_range(80000, 85000);
51 
52 	if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
53 		enable_irq(phy->i2c_dev->irq);
54 		phy->irq_active = true;
55 	}
56 
57 	return 0;
58 }
59 
60 static void st_nci_i2c_disable(void *phy_id)
61 {
62 	struct st_nci_i2c_phy *phy = phy_id;
63 
64 	disable_irq_nosync(phy->i2c_dev->irq);
65 	phy->irq_active = false;
66 }
67 
68 /*
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  */
73 static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
74 {
75 	int r = -1;
76 	struct st_nci_i2c_phy *phy = phy_id;
77 	struct i2c_client *client = phy->i2c_dev;
78 
79 	if (phy->ndlc->hard_fault != 0)
80 		return phy->ndlc->hard_fault;
81 
82 	r = i2c_master_send(client, skb->data, skb->len);
83 	if (r < 0) {  /* Retry, chip was in standby */
84 		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 
95 	return r;
96 }
97 
98 /*
99  * Reads an ndlc frame and returns it in a newly allocated sk_buff.
100  * returns:
101  * 0 : if received frame is complete
102  * -EREMOTEIO : i2c read error (fatal)
103  * -EBADMSG : frame was incorrect and discarded
104  * -ENOMEM : cannot allocate skb, frame dropped
105  */
106 static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
107 				 struct sk_buff **skb)
108 {
109 	int r;
110 	u8 len;
111 	u8 buf[ST_NCI_I2C_MAX_SIZE];
112 	struct i2c_client *client = phy->i2c_dev;
113 
114 	r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
115 	if (r < 0) {  /* Retry, chip was in standby */
116 		usleep_range(1000, 4000);
117 		r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
118 	}
119 
120 	if (r != ST_NCI_I2C_MIN_SIZE)
121 		return -EREMOTEIO;
122 
123 	len = be16_to_cpu(*(__be16 *) (buf + 2));
124 	if (len > ST_NCI_I2C_MAX_SIZE) {
125 		nfc_err(&client->dev, "invalid frame len\n");
126 		return -EBADMSG;
127 	}
128 
129 	*skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
130 	if (*skb == NULL)
131 		return -ENOMEM;
132 
133 	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);
136 
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);
147 	memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
148 
149 	return 0;
150 }
151 
152 /*
153  * Reads an ndlc frame from the chip.
154  *
155  * On ST_NCI, IRQ goes in idle state when read starts.
156  */
157 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
158 {
159 	struct st_nci_i2c_phy *phy = phy_id;
160 	struct i2c_client *client;
161 	struct sk_buff *skb = NULL;
162 	int r;
163 
164 	if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
165 		WARN_ON_ONCE(1);
166 		return IRQ_NONE;
167 	}
168 
169 	client = phy->i2c_dev;
170 	dev_dbg(&client->dev, "IRQ\n");
171 
172 	if (phy->ndlc->hard_fault)
173 		return IRQ_HANDLED;
174 
175 	if (!phy->ndlc->powered) {
176 		st_nci_i2c_disable(phy);
177 		return IRQ_HANDLED;
178 	}
179 
180 	r = st_nci_i2c_read(phy, &skb);
181 	if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
182 		return IRQ_HANDLED;
183 
184 	ndlc_recv(phy->ndlc, skb);
185 
186 	return IRQ_HANDLED;
187 }
188 
189 static struct nfc_phy_ops i2c_phy_ops = {
190 	.write = st_nci_i2c_write,
191 	.enable = st_nci_i2c_enable,
192 	.disable = st_nci_i2c_disable,
193 };
194 
195 static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
196 
197 static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
198 	{ "reset-gpios", &reset_gpios, 1 },
199 	{},
200 };
201 
202 static int st_nci_i2c_probe(struct i2c_client *client,
203 				  const struct i2c_device_id *id)
204 {
205 	struct device *dev = &client->dev;
206 	struct st_nci_i2c_phy *phy;
207 	int r;
208 
209 	dev_dbg(&client->dev, "%s\n", __func__);
210 	dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
211 
212 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
213 		nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
214 		return -ENODEV;
215 	}
216 
217 	phy = devm_kzalloc(dev, sizeof(struct st_nci_i2c_phy), GFP_KERNEL);
218 	if (!phy)
219 		return -ENOMEM;
220 
221 	phy->i2c_dev = client;
222 
223 	i2c_set_clientdata(client, phy);
224 
225 	r = devm_acpi_dev_add_driver_gpios(dev, acpi_st_nci_gpios);
226 	if (r)
227 		dev_dbg(dev, "Unable to add GPIO mapping table\n");
228 
229 	/* Get RESET GPIO */
230 	phy->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
231 	if (IS_ERR(phy->gpiod_reset)) {
232 		nfc_err(dev, "Unable to get RESET GPIO\n");
233 		return -ENODEV;
234 	}
235 
236 	phy->se_status.is_ese_present =
237 				device_property_read_bool(dev, "ese-present");
238 	phy->se_status.is_uicc_present =
239 				device_property_read_bool(dev, "uicc-present");
240 
241 	r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
242 			ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
243 			&phy->ndlc, &phy->se_status);
244 	if (r < 0) {
245 		nfc_err(&client->dev, "Unable to register ndlc layer\n");
246 		return r;
247 	}
248 
249 	phy->irq_active = true;
250 	r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
251 				st_nci_irq_thread_fn,
252 				IRQF_ONESHOT,
253 				ST_NCI_DRIVER_NAME, phy);
254 	if (r < 0)
255 		nfc_err(&client->dev, "Unable to register IRQ handler\n");
256 
257 	return r;
258 }
259 
260 static int st_nci_i2c_remove(struct i2c_client *client)
261 {
262 	struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
263 
264 	dev_dbg(&client->dev, "%s\n", __func__);
265 
266 	ndlc_remove(phy->ndlc);
267 
268 	return 0;
269 }
270 
271 static const struct i2c_device_id st_nci_i2c_id_table[] = {
272 	{ST_NCI_DRIVER_NAME, 0},
273 	{}
274 };
275 MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
276 
277 static const struct acpi_device_id st_nci_i2c_acpi_match[] = {
278 	{"SMO2101"},
279 	{"SMO2102"},
280 	{}
281 };
282 MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
283 
284 static const struct of_device_id of_st_nci_i2c_match[] = {
285 	{ .compatible = "st,st21nfcb-i2c", },
286 	{ .compatible = "st,st21nfcb_i2c", },
287 	{ .compatible = "st,st21nfcc-i2c", },
288 	{}
289 };
290 MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
291 
292 static struct i2c_driver st_nci_i2c_driver = {
293 	.driver = {
294 		.name = ST_NCI_I2C_DRIVER_NAME,
295 		.of_match_table = of_match_ptr(of_st_nci_i2c_match),
296 		.acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
297 	},
298 	.probe = st_nci_i2c_probe,
299 	.id_table = st_nci_i2c_id_table,
300 	.remove = st_nci_i2c_remove,
301 };
302 module_i2c_driver(st_nci_i2c_driver);
303 
304 MODULE_LICENSE("GPL");
305 MODULE_DESCRIPTION(DRIVER_DESC);
306