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