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