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