xref: /openbmc/linux/drivers/nfc/st-nci/i2c.c (revision 3648dc6d27f648b8e3ce9b48874627a833d53c3a)
1 /*
2  * I2C Link Layer for ST NCI NFC controller familly based Driver
3  * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
4  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/gpio.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_gpio.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/nfc.h>
28 #include <linux/platform_data/st-nci.h>
29 
30 #include "st-nci.h"
31 
32 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
33 
34 /* ndlc header */
35 #define ST_NCI_FRAME_HEADROOM	1
36 #define ST_NCI_FRAME_TAILROOM 0
37 
38 #define ST_NCI_I2C_MIN_SIZE 4   /* PCB(1) + NCI Packet header(3) */
39 #define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
40 
41 #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
42 
43 static struct i2c_device_id st_nci_i2c_id_table[] = {
44 	{ST_NCI_DRIVER_NAME, 0},
45 	{}
46 };
47 MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
48 
49 struct st_nci_i2c_phy {
50 	struct i2c_client *i2c_dev;
51 	struct llt_ndlc *ndlc;
52 
53 	unsigned int gpio_reset;
54 	unsigned int irq_polarity;
55 
56 	struct st_nci_se_status se_status;
57 };
58 
59 #define I2C_DUMP_SKB(info, skb)					\
60 do {								\
61 	pr_debug("%s:\n", info);				\
62 	print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET,	\
63 		       16, 1, (skb)->data, (skb)->len, 0);	\
64 } while (0)
65 
66 static int st_nci_i2c_enable(void *phy_id)
67 {
68 	struct st_nci_i2c_phy *phy = phy_id;
69 
70 	gpio_set_value(phy->gpio_reset, 0);
71 	usleep_range(10000, 15000);
72 	gpio_set_value(phy->gpio_reset, 1);
73 	usleep_range(80000, 85000);
74 
75 	if (phy->ndlc->powered == 0)
76 		enable_irq(phy->i2c_dev->irq);
77 
78 	return 0;
79 }
80 
81 static void st_nci_i2c_disable(void *phy_id)
82 {
83 	struct st_nci_i2c_phy *phy = phy_id;
84 
85 	disable_irq_nosync(phy->i2c_dev->irq);
86 }
87 
88 /*
89  * Writing a frame must not return the number of written bytes.
90  * It must return either zero for success, or <0 for error.
91  * In addition, it must not alter the skb
92  */
93 static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
94 {
95 	int r = -1;
96 	struct st_nci_i2c_phy *phy = phy_id;
97 	struct i2c_client *client = phy->i2c_dev;
98 
99 	I2C_DUMP_SKB("st_nci_i2c_write", skb);
100 
101 	if (phy->ndlc->hard_fault != 0)
102 		return phy->ndlc->hard_fault;
103 
104 	r = i2c_master_send(client, skb->data, skb->len);
105 	if (r < 0) {  /* Retry, chip was in standby */
106 		usleep_range(1000, 4000);
107 		r = i2c_master_send(client, skb->data, skb->len);
108 	}
109 
110 	if (r >= 0) {
111 		if (r != skb->len)
112 			r = -EREMOTEIO;
113 		else
114 			r = 0;
115 	}
116 
117 	return r;
118 }
119 
120 /*
121  * Reads an ndlc frame and returns it in a newly allocated sk_buff.
122  * returns:
123  * 0 : if received frame is complete
124  * -EREMOTEIO : i2c read error (fatal)
125  * -EBADMSG : frame was incorrect and discarded
126  * -ENOMEM : cannot allocate skb, frame dropped
127  */
128 static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
129 				 struct sk_buff **skb)
130 {
131 	int r;
132 	u8 len;
133 	u8 buf[ST_NCI_I2C_MAX_SIZE];
134 	struct i2c_client *client = phy->i2c_dev;
135 
136 	r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
137 	if (r < 0) {  /* Retry, chip was in standby */
138 		usleep_range(1000, 4000);
139 		r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
140 	}
141 
142 	if (r != ST_NCI_I2C_MIN_SIZE)
143 		return -EREMOTEIO;
144 
145 	len = be16_to_cpu(*(__be16 *) (buf + 2));
146 	if (len > ST_NCI_I2C_MAX_SIZE) {
147 		nfc_err(&client->dev, "invalid frame len\n");
148 		return -EBADMSG;
149 	}
150 
151 	*skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
152 	if (*skb == NULL)
153 		return -ENOMEM;
154 
155 	skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
156 	skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
157 	memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
158 
159 	if (!len)
160 		return 0;
161 
162 	r = i2c_master_recv(client, buf, len);
163 	if (r != len) {
164 		kfree_skb(*skb);
165 		return -EREMOTEIO;
166 	}
167 
168 	skb_put(*skb, len);
169 	memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
170 
171 	I2C_DUMP_SKB("i2c frame read", *skb);
172 
173 	return 0;
174 }
175 
176 /*
177  * Reads an ndlc frame from the chip.
178  *
179  * On ST_NCI, IRQ goes in idle state when read starts.
180  */
181 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
182 {
183 	struct st_nci_i2c_phy *phy = phy_id;
184 	struct i2c_client *client;
185 	struct sk_buff *skb = NULL;
186 	int r;
187 
188 	if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
189 		WARN_ON_ONCE(1);
190 		return IRQ_NONE;
191 	}
192 
193 	client = phy->i2c_dev;
194 	dev_dbg(&client->dev, "IRQ\n");
195 
196 	if (phy->ndlc->hard_fault)
197 		return IRQ_HANDLED;
198 
199 	if (!phy->ndlc->powered) {
200 		st_nci_i2c_disable(phy);
201 		return IRQ_HANDLED;
202 	}
203 
204 	r = st_nci_i2c_read(phy, &skb);
205 	if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
206 		return IRQ_HANDLED;
207 
208 	ndlc_recv(phy->ndlc, skb);
209 
210 	return IRQ_HANDLED;
211 }
212 
213 static struct nfc_phy_ops i2c_phy_ops = {
214 	.write = st_nci_i2c_write,
215 	.enable = st_nci_i2c_enable,
216 	.disable = st_nci_i2c_disable,
217 };
218 
219 #ifdef CONFIG_OF
220 static int st_nci_i2c_of_request_resources(struct i2c_client *client)
221 {
222 	struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
223 	struct device_node *pp;
224 	int gpio;
225 	int r;
226 
227 	pp = client->dev.of_node;
228 	if (!pp)
229 		return -ENODEV;
230 
231 	/* Get GPIO from device tree */
232 	gpio = of_get_named_gpio(pp, "reset-gpios", 0);
233 	if (gpio < 0) {
234 		nfc_err(&client->dev,
235 			"Failed to retrieve reset-gpios from device tree\n");
236 		return gpio;
237 	}
238 
239 	/* GPIO request and configuration */
240 	r = devm_gpio_request_one(&client->dev, gpio,
241 				GPIOF_OUT_INIT_HIGH, "clf_reset");
242 	if (r) {
243 		nfc_err(&client->dev, "Failed to request reset pin\n");
244 		return r;
245 	}
246 	phy->gpio_reset = gpio;
247 
248 	phy->irq_polarity = irq_get_trigger_type(client->irq);
249 
250 	phy->se_status.is_ese_present =
251 				of_property_read_bool(pp, "ese-present");
252 	phy->se_status.is_uicc_present =
253 				of_property_read_bool(pp, "uicc-present");
254 
255 	return 0;
256 }
257 #else
258 static int st_nci_i2c_of_request_resources(struct i2c_client *client)
259 {
260 	return -ENODEV;
261 }
262 #endif
263 
264 static int st_nci_i2c_request_resources(struct i2c_client *client)
265 {
266 	struct st_nci_nfc_platform_data *pdata;
267 	struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
268 	int r;
269 
270 	pdata = client->dev.platform_data;
271 	if (pdata == NULL) {
272 		nfc_err(&client->dev, "No platform data\n");
273 		return -EINVAL;
274 	}
275 
276 	/* store for later use */
277 	phy->gpio_reset = pdata->gpio_reset;
278 	phy->irq_polarity = pdata->irq_polarity;
279 
280 	r = devm_gpio_request_one(&client->dev,
281 			phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
282 	if (r) {
283 		pr_err("%s : reset gpio_request failed\n", __FILE__);
284 		return r;
285 	}
286 
287 	phy->se_status.is_ese_present = pdata->is_ese_present;
288 	phy->se_status.is_uicc_present = pdata->is_uicc_present;
289 
290 	return 0;
291 }
292 
293 static int st_nci_i2c_probe(struct i2c_client *client,
294 				  const struct i2c_device_id *id)
295 {
296 	struct st_nci_i2c_phy *phy;
297 	struct st_nci_nfc_platform_data *pdata;
298 	int r;
299 
300 	dev_dbg(&client->dev, "%s\n", __func__);
301 	dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
302 
303 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
304 		nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
305 		return -ENODEV;
306 	}
307 
308 	phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy),
309 			   GFP_KERNEL);
310 	if (!phy)
311 		return -ENOMEM;
312 
313 	phy->i2c_dev = client;
314 
315 	i2c_set_clientdata(client, phy);
316 
317 	pdata = client->dev.platform_data;
318 	if (!pdata && client->dev.of_node) {
319 		r = st_nci_i2c_of_request_resources(client);
320 		if (r) {
321 			nfc_err(&client->dev, "No platform data\n");
322 			return r;
323 		}
324 	} else if (pdata) {
325 		r = st_nci_i2c_request_resources(client);
326 		if (r) {
327 			nfc_err(&client->dev,
328 				"Cannot get platform resources\n");
329 			return r;
330 		}
331 	} else {
332 		nfc_err(&client->dev,
333 			"st_nci platform resources not available\n");
334 		return -ENODEV;
335 	}
336 
337 	r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
338 			ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
339 			&phy->ndlc, &phy->se_status);
340 	if (r < 0) {
341 		nfc_err(&client->dev, "Unable to register ndlc layer\n");
342 		return r;
343 	}
344 
345 	r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
346 				st_nci_irq_thread_fn,
347 				phy->irq_polarity | IRQF_ONESHOT,
348 				ST_NCI_DRIVER_NAME, phy);
349 	if (r < 0)
350 		nfc_err(&client->dev, "Unable to register IRQ handler\n");
351 
352 	return r;
353 }
354 
355 static int st_nci_i2c_remove(struct i2c_client *client)
356 {
357 	struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
358 
359 	dev_dbg(&client->dev, "%s\n", __func__);
360 
361 	ndlc_remove(phy->ndlc);
362 
363 	return 0;
364 }
365 
366 #ifdef CONFIG_OF
367 static const struct of_device_id of_st_nci_i2c_match[] = {
368 	{ .compatible = "st,st21nfcb-i2c", },
369 	{ .compatible = "st,st21nfcb_i2c", },
370 	{ .compatible = "st,st21nfcc-i2c", },
371 	{}
372 };
373 MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
374 #endif
375 
376 static struct i2c_driver st_nci_i2c_driver = {
377 	.driver = {
378 		.owner = THIS_MODULE,
379 		.name = ST_NCI_I2C_DRIVER_NAME,
380 		.of_match_table = of_match_ptr(of_st_nci_i2c_match),
381 	},
382 	.probe = st_nci_i2c_probe,
383 	.id_table = st_nci_i2c_id_table,
384 	.remove = st_nci_i2c_remove,
385 };
386 
387 module_i2c_driver(st_nci_i2c_driver);
388 
389 MODULE_LICENSE("GPL");
390 MODULE_DESCRIPTION(DRIVER_DESC);
391