1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * I2C Link Layer for Samsung S3FWRN5 NCI based Driver
4  *
5  * Copyright (C) 2015 Samsung Electrnoics
6  * Robert Baldyga <r.baldyga@samsung.com>
7  */
8 
9 #include <linux/i2c.h>
10 #include <linux/gpio.h>
11 #include <linux/delay.h>
12 #include <linux/of_gpio.h>
13 #include <linux/of_irq.h>
14 #include <linux/module.h>
15 
16 #include <net/nfc/nfc.h>
17 
18 #include "s3fwrn5.h"
19 
20 #define S3FWRN5_I2C_DRIVER_NAME "s3fwrn5_i2c"
21 
22 #define S3FWRN5_I2C_MAX_PAYLOAD 32
23 #define S3FWRN5_EN_WAIT_TIME 150
24 
25 struct s3fwrn5_i2c_phy {
26 	struct i2c_client *i2c_dev;
27 	struct nci_dev *ndev;
28 
29 	int gpio_en;
30 	int gpio_fw_wake;
31 
32 	struct mutex mutex;
33 
34 	enum s3fwrn5_mode mode;
35 	unsigned int irq_skip:1;
36 };
37 
s3fwrn5_i2c_set_wake(void * phy_id,bool wake)38 static void s3fwrn5_i2c_set_wake(void *phy_id, bool wake)
39 {
40 	struct s3fwrn5_i2c_phy *phy = phy_id;
41 
42 	mutex_lock(&phy->mutex);
43 	gpio_set_value(phy->gpio_fw_wake, wake);
44 	msleep(S3FWRN5_EN_WAIT_TIME/2);
45 	mutex_unlock(&phy->mutex);
46 }
47 
s3fwrn5_i2c_set_mode(void * phy_id,enum s3fwrn5_mode mode)48 static void s3fwrn5_i2c_set_mode(void *phy_id, enum s3fwrn5_mode mode)
49 {
50 	struct s3fwrn5_i2c_phy *phy = phy_id;
51 
52 	mutex_lock(&phy->mutex);
53 
54 	if (phy->mode == mode)
55 		goto out;
56 
57 	phy->mode = mode;
58 
59 	gpio_set_value(phy->gpio_en, 1);
60 	gpio_set_value(phy->gpio_fw_wake, 0);
61 	if (mode == S3FWRN5_MODE_FW)
62 		gpio_set_value(phy->gpio_fw_wake, 1);
63 
64 	if (mode != S3FWRN5_MODE_COLD) {
65 		msleep(S3FWRN5_EN_WAIT_TIME);
66 		gpio_set_value(phy->gpio_en, 0);
67 		msleep(S3FWRN5_EN_WAIT_TIME/2);
68 	}
69 
70 	phy->irq_skip = true;
71 
72 out:
73 	mutex_unlock(&phy->mutex);
74 }
75 
s3fwrn5_i2c_get_mode(void * phy_id)76 static enum s3fwrn5_mode s3fwrn5_i2c_get_mode(void *phy_id)
77 {
78 	struct s3fwrn5_i2c_phy *phy = phy_id;
79 	enum s3fwrn5_mode mode;
80 
81 	mutex_lock(&phy->mutex);
82 
83 	mode = phy->mode;
84 
85 	mutex_unlock(&phy->mutex);
86 
87 	return mode;
88 }
89 
s3fwrn5_i2c_write(void * phy_id,struct sk_buff * skb)90 static int s3fwrn5_i2c_write(void *phy_id, struct sk_buff *skb)
91 {
92 	struct s3fwrn5_i2c_phy *phy = phy_id;
93 	int ret;
94 
95 	mutex_lock(&phy->mutex);
96 
97 	phy->irq_skip = false;
98 
99 	ret = i2c_master_send(phy->i2c_dev, skb->data, skb->len);
100 	if (ret == -EREMOTEIO) {
101 		/* Retry, chip was in standby */
102 		usleep_range(110000, 120000);
103 		ret  = i2c_master_send(phy->i2c_dev, skb->data, skb->len);
104 	}
105 
106 	mutex_unlock(&phy->mutex);
107 
108 	if (ret < 0)
109 		return ret;
110 
111 	if (ret != skb->len)
112 		return -EREMOTEIO;
113 
114 	return 0;
115 }
116 
117 static const struct s3fwrn5_phy_ops i2c_phy_ops = {
118 	.set_wake = s3fwrn5_i2c_set_wake,
119 	.set_mode = s3fwrn5_i2c_set_mode,
120 	.get_mode = s3fwrn5_i2c_get_mode,
121 	.write = s3fwrn5_i2c_write,
122 };
123 
s3fwrn5_i2c_read(struct s3fwrn5_i2c_phy * phy)124 static int s3fwrn5_i2c_read(struct s3fwrn5_i2c_phy *phy)
125 {
126 	struct sk_buff *skb;
127 	size_t hdr_size;
128 	size_t data_len;
129 	char hdr[4];
130 	int ret;
131 
132 	hdr_size = (phy->mode == S3FWRN5_MODE_NCI) ?
133 		NCI_CTRL_HDR_SIZE : S3FWRN5_FW_HDR_SIZE;
134 	ret = i2c_master_recv(phy->i2c_dev, hdr, hdr_size);
135 	if (ret < 0)
136 		return ret;
137 
138 	if (ret < hdr_size)
139 		return -EBADMSG;
140 
141 	data_len = (phy->mode == S3FWRN5_MODE_NCI) ?
142 		((struct nci_ctrl_hdr *)hdr)->plen :
143 		((struct s3fwrn5_fw_header *)hdr)->len;
144 
145 	skb = alloc_skb(hdr_size + data_len, GFP_KERNEL);
146 	if (!skb)
147 		return -ENOMEM;
148 
149 	skb_put_data(skb, hdr, hdr_size);
150 
151 	if (data_len == 0)
152 		goto out;
153 
154 	ret = i2c_master_recv(phy->i2c_dev, skb_put(skb, data_len), data_len);
155 	if (ret != data_len) {
156 		kfree_skb(skb);
157 		return -EBADMSG;
158 	}
159 
160 out:
161 	return s3fwrn5_recv_frame(phy->ndev, skb, phy->mode);
162 }
163 
s3fwrn5_i2c_irq_thread_fn(int irq,void * phy_id)164 static irqreturn_t s3fwrn5_i2c_irq_thread_fn(int irq, void *phy_id)
165 {
166 	struct s3fwrn5_i2c_phy *phy = phy_id;
167 
168 	if (!phy || !phy->ndev) {
169 		WARN_ON_ONCE(1);
170 		return IRQ_NONE;
171 	}
172 
173 	mutex_lock(&phy->mutex);
174 
175 	if (phy->irq_skip)
176 		goto out;
177 
178 	switch (phy->mode) {
179 	case S3FWRN5_MODE_NCI:
180 	case S3FWRN5_MODE_FW:
181 		s3fwrn5_i2c_read(phy);
182 		break;
183 	case S3FWRN5_MODE_COLD:
184 		break;
185 	}
186 
187 out:
188 	mutex_unlock(&phy->mutex);
189 
190 	return IRQ_HANDLED;
191 }
192 
s3fwrn5_i2c_parse_dt(struct i2c_client * client)193 static int s3fwrn5_i2c_parse_dt(struct i2c_client *client)
194 {
195 	struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
196 	struct device_node *np = client->dev.of_node;
197 
198 	if (!np)
199 		return -ENODEV;
200 
201 	phy->gpio_en = of_get_named_gpio(np, "en-gpios", 0);
202 	if (!gpio_is_valid(phy->gpio_en)) {
203 		/* Support also deprecated property */
204 		phy->gpio_en = of_get_named_gpio(np, "s3fwrn5,en-gpios", 0);
205 		if (!gpio_is_valid(phy->gpio_en))
206 			return -ENODEV;
207 	}
208 
209 	phy->gpio_fw_wake = of_get_named_gpio(np, "wake-gpios", 0);
210 	if (!gpio_is_valid(phy->gpio_fw_wake)) {
211 		/* Support also deprecated property */
212 		phy->gpio_fw_wake = of_get_named_gpio(np, "s3fwrn5,fw-gpios", 0);
213 		if (!gpio_is_valid(phy->gpio_fw_wake))
214 			return -ENODEV;
215 	}
216 
217 	return 0;
218 }
219 
s3fwrn5_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)220 static int s3fwrn5_i2c_probe(struct i2c_client *client,
221 				  const struct i2c_device_id *id)
222 {
223 	struct s3fwrn5_i2c_phy *phy;
224 	int ret;
225 
226 	phy = devm_kzalloc(&client->dev, sizeof(*phy), GFP_KERNEL);
227 	if (!phy)
228 		return -ENOMEM;
229 
230 	mutex_init(&phy->mutex);
231 	phy->mode = S3FWRN5_MODE_COLD;
232 	phy->irq_skip = true;
233 
234 	phy->i2c_dev = client;
235 	i2c_set_clientdata(client, phy);
236 
237 	ret = s3fwrn5_i2c_parse_dt(client);
238 	if (ret < 0)
239 		return ret;
240 
241 	ret = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_en,
242 		GPIOF_OUT_INIT_HIGH, "s3fwrn5_en");
243 	if (ret < 0)
244 		return ret;
245 
246 	ret = devm_gpio_request_one(&phy->i2c_dev->dev, phy->gpio_fw_wake,
247 		GPIOF_OUT_INIT_LOW, "s3fwrn5_fw_wake");
248 	if (ret < 0)
249 		return ret;
250 
251 	ret = s3fwrn5_probe(&phy->ndev, phy, &phy->i2c_dev->dev, &i2c_phy_ops,
252 		S3FWRN5_I2C_MAX_PAYLOAD);
253 	if (ret < 0)
254 		return ret;
255 
256 	ret = devm_request_threaded_irq(&client->dev, phy->i2c_dev->irq, NULL,
257 		s3fwrn5_i2c_irq_thread_fn, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
258 		S3FWRN5_I2C_DRIVER_NAME, phy);
259 	if (ret)
260 		s3fwrn5_remove(phy->ndev);
261 
262 	return ret;
263 }
264 
s3fwrn5_i2c_remove(struct i2c_client * client)265 static int s3fwrn5_i2c_remove(struct i2c_client *client)
266 {
267 	struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
268 
269 	s3fwrn5_remove(phy->ndev);
270 
271 	return 0;
272 }
273 
274 static const struct i2c_device_id s3fwrn5_i2c_id_table[] = {
275 	{S3FWRN5_I2C_DRIVER_NAME, 0},
276 	{}
277 };
278 MODULE_DEVICE_TABLE(i2c, s3fwrn5_i2c_id_table);
279 
280 static const struct of_device_id of_s3fwrn5_i2c_match[] = {
281 	{ .compatible = "samsung,s3fwrn5-i2c", },
282 	{}
283 };
284 MODULE_DEVICE_TABLE(of, of_s3fwrn5_i2c_match);
285 
286 static struct i2c_driver s3fwrn5_i2c_driver = {
287 	.driver = {
288 		.name = S3FWRN5_I2C_DRIVER_NAME,
289 		.of_match_table = of_match_ptr(of_s3fwrn5_i2c_match),
290 	},
291 	.probe = s3fwrn5_i2c_probe,
292 	.remove = s3fwrn5_i2c_remove,
293 	.id_table = s3fwrn5_i2c_id_table,
294 };
295 
296 module_i2c_driver(s3fwrn5_i2c_driver);
297 
298 MODULE_LICENSE("GPL");
299 MODULE_DESCRIPTION("I2C driver for Samsung S3FWRN5");
300 MODULE_AUTHOR("Robert Baldyga <r.baldyga@samsung.com>");
301