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