76a4cad41cec9b3affa4f903683a313d4620b389
[kvmfornfv.git] / kernel / drivers / nfc / st21nfcb / i2c.c
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
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
53         unsigned int gpio_reset;
54         unsigned int irq_polarity;
55
56         int powered;
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
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
108         if (phy->ndlc->hard_fault != 0)
109                 return phy->ndlc->hard_fault;
110
111         r = i2c_master_send(client, skb->data, skb->len);
112         if (r < 0) {  /* 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
150         r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
151         if (r < 0) {  /* Retry, chip was in standby */
152                 usleep_range(1000, 4000);
153                 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
154         }
155
156         if (r != ST21NFCB_NCI_I2C_MIN_SIZE)
157                 return -EREMOTEIO;
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
165         *skb = alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
166         if (*skb == NULL)
167                 return -ENOMEM;
168
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);
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);
183         memcpy((*skb)->data + ST21NFCB_NCI_I2C_MIN_SIZE, buf, len);
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 || !phy->ndlc || 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
210         if (phy->ndlc->hard_fault)
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);
219         if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
220                 return IRQ_HANDLED;
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 */
254         r = devm_gpio_request_one(&client->dev, gpio,
255                                 GPIOF_OUT_INIT_HIGH, "clf_reset");
256         if (r) {
257                 nfc_err(&client->dev, "Failed to request reset pin\n");
258                 return r;
259         }
260         phy->gpio_reset = gpio;
261
262         phy->irq_polarity = irq_get_trigger_type(client->irq);
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;
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 */
286         phy->gpio_reset = pdata->gpio_reset;
287         phy->irq_polarity = pdata->irq_polarity;
288
289         r = devm_gpio_request_one(&client->dev,
290                         phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
291         if (r) {
292                 pr_err("%s : reset gpio_request failed\n", __FILE__);
293                 return r;
294         }
295
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                 return -ENOMEM;
318
319         phy->i2c_dev = client;
320
321         i2c_set_clientdata(client, phy);
322
323         pdata = client->dev.platform_data;
324         if (!pdata && client->dev.of_node) {
325                 r = st21nfcb_nci_i2c_of_request_resources(client);
326                 if (r) {
327                         nfc_err(&client->dev, "No platform data\n");
328                         return r;
329                 }
330         } else if (pdata) {
331                 r = st21nfcb_nci_i2c_request_resources(client);
332                 if (r) {
333                         nfc_err(&client->dev,
334                                 "Cannot get platform resources\n");
335                         return r;
336                 }
337         } else {
338                 nfc_err(&client->dev,
339                         "st21nfcb platform resources not available\n");
340                 return -ENODEV;
341         }
342
343         r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
344                         ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
345                         &phy->ndlc);
346         if (r < 0) {
347                 nfc_err(&client->dev, "Unable to register ndlc layer\n");
348                 return r;
349         }
350
351         r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
352                                 st21nfcb_nci_irq_thread_fn,
353                                 phy->irq_polarity | IRQF_ONESHOT,
354                                 ST21NFCB_NCI_DRIVER_NAME, phy);
355         if (r < 0)
356                 nfc_err(&client->dev, "Unable to register IRQ handler\n");
357
358         return r;
359 }
360
361 static int st21nfcb_nci_i2c_remove(struct i2c_client *client)
362 {
363         struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
364
365         dev_dbg(&client->dev, "%s\n", __func__);
366
367         ndlc_remove(phy->ndlc);
368
369         if (phy->powered)
370                 st21nfcb_nci_i2c_disable(phy);
371
372         return 0;
373 }
374
375 #ifdef CONFIG_OF
376 static const struct of_device_id of_st21nfcb_i2c_match[] = {
377         { .compatible = "st,st21nfcb-i2c", },
378         { .compatible = "st,st21nfcb_i2c", },
379         {}
380 };
381 MODULE_DEVICE_TABLE(of, of_st21nfcb_i2c_match);
382 #endif
383
384 static struct i2c_driver st21nfcb_nci_i2c_driver = {
385         .driver = {
386                 .owner = THIS_MODULE,
387                 .name = ST21NFCB_NCI_I2C_DRIVER_NAME,
388                 .of_match_table = of_match_ptr(of_st21nfcb_i2c_match),
389         },
390         .probe = st21nfcb_nci_i2c_probe,
391         .id_table = st21nfcb_nci_i2c_id_table,
392         .remove = st21nfcb_nci_i2c_remove,
393 };
394
395 module_i2c_driver(st21nfcb_nci_i2c_driver);
396
397 MODULE_LICENSE("GPL");
398 MODULE_DESCRIPTION(DRIVER_DESC);