Merge branch 'for-4.3/blkcg' of git://git.kernel.dk/linux-block
[linux-2.6-block.git] / drivers / nfc / st-nci / i2c.c
CommitLineData
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>
76b733d1 28#include <linux/platform_data/st-nci.h>
35630df6 29
35630df6
CR
30#include "ndlc.h"
31
30458aac 32#define DRIVER_DESC "NCI NFC driver for ST_NCI"
35630df6
CR
33
34/* ndlc header */
30458aac
CR
35#define ST_NCI_FRAME_HEADROOM 1
36#define ST_NCI_FRAME_TAILROOM 0
35630df6 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
43static struct i2c_device_id st_nci_i2c_id_table[] = {
44 {ST_NCI_DRIVER_NAME, 0},
35630df6
CR
45 {}
46};
ed06aeef 47MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
35630df6 48
ed06aeef 49struct 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) \
58do { \
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 64static 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 79static 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 91static 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:
e7723b33 121 * 0 : if received frame is complete
35630df6
CR
122 * -EREMOTEIO : i2c read error (fatal)
123 * -EBADMSG : frame was incorrect and discarded
e7723b33 124 * -ENOMEM : cannot allocate skb, frame dropped
35630df6 125 */
ed06aeef 126static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
35630df6
CR
127 struct sk_buff **skb)
128{
129 int r;
130 u8 len;
ed06aeef 131 u8 buf[ST_NCI_I2C_MAX_SIZE];
35630df6
CR
132 struct i2c_client *client = phy->i2c_dev;
133
ed06aeef 134 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
d4a41d10 135 if (r < 0) { /* Retry, chip was in standby */
35630df6 136 usleep_range(1000, 4000);
ed06aeef 137 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
ac633ba6
CR
138 }
139
ed06aeef 140 if (r != ST_NCI_I2C_MIN_SIZE)
35630df6 141 return -EREMOTEIO;
35630df6
CR
142
143 len = be16_to_cpu(*(__be16 *) (buf + 2));
ed06aeef 144 if (len > ST_NCI_I2C_MAX_SIZE) {
35630df6
CR
145 nfc_err(&client->dev, "invalid frame len\n");
146 return -EBADMSG;
147 }
148
ed06aeef 149 *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
35630df6
CR
150 if (*skb == NULL)
151 return -ENOMEM;
152
ed06aeef
CR
153 skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
154 skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
155 memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
35630df6
CR
156
157 if (!len)
158 return 0;
159
160 r = i2c_master_recv(client, buf, len);
161 if (r != len) {
162 kfree_skb(*skb);
163 return -EREMOTEIO;
164 }
165
166 skb_put(*skb, len);
ed06aeef 167 memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
35630df6
CR
168
169 I2C_DUMP_SKB("i2c frame read", *skb);
170
171 return 0;
172}
173
174/*
175 * Reads an ndlc frame from the chip.
176 *
30458aac 177 * On ST_NCI, IRQ goes in idle state when read starts.
35630df6 178 */
ed06aeef 179static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
35630df6 180{
ed06aeef 181 struct st_nci_i2c_phy *phy = phy_id;
35630df6
CR
182 struct i2c_client *client;
183 struct sk_buff *skb = NULL;
184 int r;
185
7274496f 186 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
35630df6
CR
187 WARN_ON_ONCE(1);
188 return IRQ_NONE;
189 }
190
191 client = phy->i2c_dev;
192 dev_dbg(&client->dev, "IRQ\n");
193
4294e320 194 if (phy->ndlc->hard_fault)
35630df6
CR
195 return IRQ_HANDLED;
196
183fe2d0 197 if (!phy->ndlc->powered) {
ed06aeef 198 st_nci_i2c_disable(phy);
35630df6
CR
199 return IRQ_HANDLED;
200 }
201
ed06aeef 202 r = st_nci_i2c_read(phy, &skb);
4294e320 203 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
35630df6 204 return IRQ_HANDLED;
35630df6
CR
205
206 ndlc_recv(phy->ndlc, skb);
207
208 return IRQ_HANDLED;
209}
210
211static struct nfc_phy_ops i2c_phy_ops = {
ed06aeef
CR
212 .write = st_nci_i2c_write,
213 .enable = st_nci_i2c_enable,
214 .disable = st_nci_i2c_disable,
35630df6
CR
215};
216
217#ifdef CONFIG_OF
ed06aeef 218static int st_nci_i2c_of_request_resources(struct i2c_client *client)
35630df6 219{
ed06aeef 220 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
35630df6
CR
221 struct device_node *pp;
222 int gpio;
223 int r;
224
225 pp = client->dev.of_node;
226 if (!pp)
227 return -ENODEV;
228
229 /* Get GPIO from device tree */
230 gpio = of_get_named_gpio(pp, "reset-gpios", 0);
231 if (gpio < 0) {
232 nfc_err(&client->dev,
233 "Failed to retrieve reset-gpios from device tree\n");
234 return gpio;
235 }
236
237 /* GPIO request and configuration */
56ee645e
CR
238 r = devm_gpio_request_one(&client->dev, gpio,
239 GPIOF_OUT_INIT_HIGH, "clf_reset");
35630df6
CR
240 if (r) {
241 nfc_err(&client->dev, "Failed to request reset pin\n");
18159624 242 return r;
35630df6 243 }
35630df6
CR
244 phy->gpio_reset = gpio;
245
a80d0cb6 246 phy->irq_polarity = irq_get_trigger_type(client->irq);
35630df6
CR
247
248 return 0;
249}
250#else
ed06aeef 251static int st_nci_i2c_of_request_resources(struct i2c_client *client)
35630df6
CR
252{
253 return -ENODEV;
254}
255#endif
256
ed06aeef 257static int st_nci_i2c_request_resources(struct i2c_client *client)
35630df6 258{
ed06aeef
CR
259 struct st_nci_nfc_platform_data *pdata;
260 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
35630df6 261 int r;
35630df6
CR
262
263 pdata = client->dev.platform_data;
264 if (pdata == NULL) {
265 nfc_err(&client->dev, "No platform data\n");
266 return -EINVAL;
267 }
268
269 /* store for later use */
35630df6
CR
270 phy->gpio_reset = pdata->gpio_reset;
271 phy->irq_polarity = pdata->irq_polarity;
272
56ee645e
CR
273 r = devm_gpio_request_one(&client->dev,
274 phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
35630df6
CR
275 if (r) {
276 pr_err("%s : reset gpio_request failed\n", __FILE__);
18159624 277 return r;
35630df6
CR
278 }
279
35630df6
CR
280 return 0;
281}
282
ed06aeef 283static int st_nci_i2c_probe(struct i2c_client *client,
35630df6
CR
284 const struct i2c_device_id *id)
285{
ed06aeef
CR
286 struct st_nci_i2c_phy *phy;
287 struct st_nci_nfc_platform_data *pdata;
35630df6
CR
288 int r;
289
290 dev_dbg(&client->dev, "%s\n", __func__);
291 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
292
293 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
294 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
295 return -ENODEV;
296 }
297
ed06aeef 298 phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy),
35630df6 299 GFP_KERNEL);
3590ebc0 300 if (!phy)
35630df6 301 return -ENOMEM;
35630df6
CR
302
303 phy->i2c_dev = client;
304
305 i2c_set_clientdata(client, phy);
306
307 pdata = client->dev.platform_data;
308 if (!pdata && client->dev.of_node) {
ed06aeef 309 r = st_nci_i2c_of_request_resources(client);
35630df6
CR
310 if (r) {
311 nfc_err(&client->dev, "No platform data\n");
312 return r;
313 }
314 } else if (pdata) {
ed06aeef 315 r = st_nci_i2c_request_resources(client);
35630df6
CR
316 if (r) {
317 nfc_err(&client->dev,
318 "Cannot get platform resources\n");
319 return r;
320 }
321 } else {
322 nfc_err(&client->dev,
30458aac 323 "st_nci platform resources not available\n");
35630df6
CR
324 return -ENODEV;
325 }
326
7274496f 327 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
30458aac 328 ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
7274496f
CR
329 &phy->ndlc);
330 if (r < 0) {
331 nfc_err(&client->dev, "Unable to register ndlc layer\n");
332 return r;
333 }
334
35630df6 335 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
ed06aeef 336 st_nci_irq_thread_fn,
35630df6 337 phy->irq_polarity | IRQF_ONESHOT,
ed06aeef 338 ST_NCI_DRIVER_NAME, phy);
7274496f 339 if (r < 0)
35630df6 340 nfc_err(&client->dev, "Unable to register IRQ handler\n");
35630df6 341
7274496f 342 return r;
35630df6
CR
343}
344
ed06aeef 345static int st_nci_i2c_remove(struct i2c_client *client)
35630df6 346{
ed06aeef 347 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
35630df6
CR
348
349 dev_dbg(&client->dev, "%s\n", __func__);
350
351 ndlc_remove(phy->ndlc);
352
35630df6
CR
353 return 0;
354}
355
d9b66918 356#ifdef CONFIG_OF
ed06aeef 357static const struct of_device_id of_st_nci_i2c_match[] = {
1a94cb60 358 { .compatible = "st,st21nfcb-i2c", },
35630df6 359 { .compatible = "st,st21nfcb_i2c", },
ed06aeef 360 { .compatible = "st,st21nfcc-i2c", },
35630df6
CR
361 {}
362};
ed06aeef 363MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
d9b66918 364#endif
35630df6 365
ed06aeef 366static struct i2c_driver st_nci_i2c_driver = {
35630df6
CR
367 .driver = {
368 .owner = THIS_MODULE,
ed06aeef
CR
369 .name = ST_NCI_I2C_DRIVER_NAME,
370 .of_match_table = of_match_ptr(of_st_nci_i2c_match),
35630df6 371 },
ed06aeef
CR
372 .probe = st_nci_i2c_probe,
373 .id_table = st_nci_i2c_id_table,
374 .remove = st_nci_i2c_remove,
35630df6
CR
375};
376
ed06aeef 377module_i2c_driver(st_nci_i2c_driver);
35630df6
CR
378
379MODULE_LICENSE("GPL");
380MODULE_DESCRIPTION(DRIVER_DESC);