Bluetooth: hci_uart: Use generic functionality from Broadcom module
[linux/fpc-iii.git] / drivers / nfc / st21nfcb / i2c.c
blobeb886932d97278cfa044fb66a20dea49ffcaca6b
1 /*
2 * I2C Link Layer for ST21NFCB NCI based Driver
3 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
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.
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.
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/>.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
30 #include "ndlc.h"
32 #define DRIVER_DESC "NCI NFC driver for ST21NFCB"
34 /* ndlc header */
35 #define ST21NFCB_FRAME_HEADROOM 1
36 #define ST21NFCB_FRAME_TAILROOM 0
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 */
41 #define ST21NFCB_NCI_I2C_DRIVER_NAME "st21nfcb_nci_i2c"
43 static struct i2c_device_id st21nfcb_nci_i2c_id_table[] = {
44 {ST21NFCB_NCI_DRIVER_NAME, 0},
47 MODULE_DEVICE_TABLE(i2c, st21nfcb_nci_i2c_id_table);
49 struct st21nfcb_i2c_phy {
50 struct i2c_client *i2c_dev;
51 struct llt_ndlc *ndlc;
53 unsigned int gpio_reset;
54 unsigned int irq_polarity;
56 int powered;
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)
66 static int st21nfcb_nci_i2c_enable(void *phy_id)
68 struct st21nfcb_i2c_phy *phy = phy_id;
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);
76 return 0;
79 static void st21nfcb_nci_i2c_disable(void *phy_id)
81 struct st21nfcb_i2c_phy *phy = phy_id;
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);
90 static void st21nfcb_nci_remove_header(struct sk_buff *skb)
92 skb_pull(skb, ST21NFCB_FRAME_HEADROOM);
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
100 static int st21nfcb_nci_i2c_write(void *phy_id, struct sk_buff *skb)
102 int r = -1;
103 struct st21nfcb_i2c_phy *phy = phy_id;
104 struct i2c_client *client = phy->i2c_dev;
106 I2C_DUMP_SKB("st21nfcb_nci_i2c_write", skb);
108 if (phy->ndlc->hard_fault != 0)
109 return phy->ndlc->hard_fault;
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);
117 if (r >= 0) {
118 if (r != skb->len)
119 r = -EREMOTEIO;
120 else
121 r = 0;
124 st21nfcb_nci_remove_header(skb);
126 return r;
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
142 static int st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy *phy,
143 struct sk_buff **skb)
145 int r;
146 u8 len;
147 u8 buf[ST21NFCB_NCI_I2C_MAX_SIZE];
148 struct i2c_client *client = phy->i2c_dev;
150 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
151 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
152 usleep_range(1000, 4000);
153 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
156 if (r != ST21NFCB_NCI_I2C_MIN_SIZE)
157 return -EREMOTEIO;
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;
165 *skb = alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
166 if (*skb == NULL)
167 return -ENOMEM;
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);
173 if (!len)
174 return 0;
176 r = i2c_master_recv(client, buf, len);
177 if (r != len) {
178 kfree_skb(*skb);
179 return -EREMOTEIO;
182 skb_put(*skb, len);
183 memcpy((*skb)->data + ST21NFCB_NCI_I2C_MIN_SIZE, buf, len);
185 I2C_DUMP_SKB("i2c frame read", *skb);
187 return 0;
191 * Reads an ndlc frame from the chip.
193 * On ST21NFCB, IRQ goes in idle state when read starts.
195 static irqreturn_t st21nfcb_nci_irq_thread_fn(int irq, void *phy_id)
197 struct st21nfcb_i2c_phy *phy = phy_id;
198 struct i2c_client *client;
199 struct sk_buff *skb = NULL;
200 int r;
202 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
203 WARN_ON_ONCE(1);
204 return IRQ_NONE;
207 client = phy->i2c_dev;
208 dev_dbg(&client->dev, "IRQ\n");
210 if (phy->ndlc->hard_fault)
211 return IRQ_HANDLED;
213 if (!phy->powered) {
214 st21nfcb_nci_i2c_disable(phy);
215 return IRQ_HANDLED;
218 r = st21nfcb_nci_i2c_read(phy, &skb);
219 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
220 return IRQ_HANDLED;
222 ndlc_recv(phy->ndlc, skb);
224 return IRQ_HANDLED;
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,
233 #ifdef CONFIG_OF
234 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
236 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
237 struct device_node *pp;
238 int gpio;
239 int r;
241 pp = client->dev.of_node;
242 if (!pp)
243 return -ENODEV;
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;
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;
260 phy->gpio_reset = gpio;
262 phy->irq_polarity = irq_get_trigger_type(client->irq);
264 return 0;
266 #else
267 static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
269 return -ENODEV;
271 #endif
273 static int st21nfcb_nci_i2c_request_resources(struct i2c_client *client)
275 struct st21nfcb_nfc_platform_data *pdata;
276 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
277 int r;
279 pdata = client->dev.platform_data;
280 if (pdata == NULL) {
281 nfc_err(&client->dev, "No platform data\n");
282 return -EINVAL;
285 /* store for later use */
286 phy->gpio_reset = pdata->gpio_reset;
287 phy->irq_polarity = pdata->irq_polarity;
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;
296 return 0;
299 static int st21nfcb_nci_i2c_probe(struct i2c_client *client,
300 const struct i2c_device_id *id)
302 struct st21nfcb_i2c_phy *phy;
303 struct st21nfcb_nfc_platform_data *pdata;
304 int r;
306 dev_dbg(&client->dev, "%s\n", __func__);
307 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
309 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
310 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
311 return -ENODEV;
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;
322 phy->i2c_dev = client;
324 i2c_set_clientdata(client, phy);
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;
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;
340 } else {
341 nfc_err(&client->dev,
342 "st21nfcb platform resources not available\n");
343 return -ENODEV;
346 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
347 ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
348 &phy->ndlc);
349 if (r < 0) {
350 nfc_err(&client->dev, "Unable to register ndlc layer\n");
351 return r;
354 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
355 st21nfcb_nci_irq_thread_fn,
356 phy->irq_polarity | IRQF_ONESHOT,
357 ST21NFCB_NCI_DRIVER_NAME, phy);
358 if (r < 0)
359 nfc_err(&client->dev, "Unable to register IRQ handler\n");
361 return r;
364 static int st21nfcb_nci_i2c_remove(struct i2c_client *client)
366 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
368 dev_dbg(&client->dev, "%s\n", __func__);
370 ndlc_remove(phy->ndlc);
372 if (phy->powered)
373 st21nfcb_nci_i2c_disable(phy);
375 return 0;
378 #ifdef CONFIG_OF
379 static const struct of_device_id of_st21nfcb_i2c_match[] = {
380 { .compatible = "st,st21nfcb-i2c", },
381 { .compatible = "st,st21nfcb_i2c", },
384 MODULE_DEVICE_TABLE(of, of_st21nfcb_i2c_match);
385 #endif
387 static struct i2c_driver st21nfcb_nci_i2c_driver = {
388 .driver = {
389 .owner = THIS_MODULE,
390 .name = ST21NFCB_NCI_I2C_DRIVER_NAME,
391 .of_match_table = of_match_ptr(of_st21nfcb_i2c_match),
393 .probe = st21nfcb_nci_i2c_probe,
394 .id_table = st21nfcb_nci_i2c_id_table,
395 .remove = st21nfcb_nci_i2c_remove,
398 module_i2c_driver(st21nfcb_nci_i2c_driver);
400 MODULE_LICENSE("GPL");
401 MODULE_DESCRIPTION(DRIVER_DESC);