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