i2c.c revision ac633ba6acb94a11b09a7ec417c72f65c6308b7a
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, ST21NFCB_NCI_I2C_MIN_SIZE); 168 if (r == -EREMOTEIO) { /* Retry, chip was in standby */ 169 usleep_range(1000, 4000); 170 r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE); 171 } 172 173 if (r != ST21NFCB_NCI_I2C_MIN_SIZE) { 174 nfc_err(&client->dev, "cannot read ndlc & nci header\n"); 175 return -EREMOTEIO; 176 } 177 178 len = be16_to_cpu(*(__be16 *) (buf + 2)); 179 if (len > ST21NFCB_NCI_I2C_MAX_SIZE) { 180 nfc_err(&client->dev, "invalid frame len\n"); 181 return -EBADMSG; 182 } 183 184 *skb = alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE + len, GFP_KERNEL); 185 if (*skb == NULL) 186 return -ENOMEM; 187 188 skb_reserve(*skb, ST21NFCB_NCI_I2C_MIN_SIZE); 189 skb_put(*skb, ST21NFCB_NCI_I2C_MIN_SIZE); 190 memcpy((*skb)->data, buf, ST21NFCB_NCI_I2C_MIN_SIZE); 191 192 if (!len) 193 return 0; 194 195 r = i2c_master_recv(client, buf, len); 196 if (r != len) { 197 kfree_skb(*skb); 198 return -EREMOTEIO; 199 } 200 201 skb_put(*skb, len); 202 memcpy((*skb)->data + ST21NFCB_NCI_I2C_MIN_SIZE, buf, len); 203 204 I2C_DUMP_SKB("i2c frame read", *skb); 205 206 return 0; 207} 208 209/* 210 * Reads an ndlc frame from the chip. 211 * 212 * On ST21NFCB, IRQ goes in idle state when read starts. 213 */ 214static irqreturn_t st21nfcb_nci_irq_thread_fn(int irq, void *phy_id) 215{ 216 struct st21nfcb_i2c_phy *phy = phy_id; 217 struct i2c_client *client; 218 struct sk_buff *skb = NULL; 219 int r; 220 221 if (!phy || irq != phy->i2c_dev->irq) { 222 WARN_ON_ONCE(1); 223 return IRQ_NONE; 224 } 225 226 client = phy->i2c_dev; 227 dev_dbg(&client->dev, "IRQ\n"); 228 229 if (phy->hard_fault) 230 return IRQ_HANDLED; 231 232 if (!phy->powered) { 233 st21nfcb_nci_i2c_disable(phy); 234 return IRQ_HANDLED; 235 } 236 237 r = st21nfcb_nci_i2c_read(phy, &skb); 238 if (r == -EREMOTEIO) { 239 phy->hard_fault = r; 240 ndlc_recv(phy->ndlc, NULL); 241 return IRQ_HANDLED; 242 } else if (r == -ENOMEM || r == -EBADMSG) { 243 return IRQ_HANDLED; 244 } 245 246 ndlc_recv(phy->ndlc, skb); 247 248 return IRQ_HANDLED; 249} 250 251static struct nfc_phy_ops i2c_phy_ops = { 252 .write = st21nfcb_nci_i2c_write, 253 .enable = st21nfcb_nci_i2c_enable, 254 .disable = st21nfcb_nci_i2c_disable, 255}; 256 257#ifdef CONFIG_OF 258static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client) 259{ 260 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client); 261 struct device_node *pp; 262 int gpio; 263 int r; 264 265 pp = client->dev.of_node; 266 if (!pp) 267 return -ENODEV; 268 269 /* Get GPIO from device tree */ 270 gpio = of_get_named_gpio(pp, "reset-gpios", 0); 271 if (gpio < 0) { 272 nfc_err(&client->dev, 273 "Failed to retrieve reset-gpios from device tree\n"); 274 return gpio; 275 } 276 277 /* GPIO request and configuration */ 278 r = devm_gpio_request_one(&client->dev, gpio, 279 GPIOF_OUT_INIT_HIGH, "clf_reset"); 280 if (r) { 281 nfc_err(&client->dev, "Failed to request reset pin\n"); 282 return -ENODEV; 283 } 284 phy->gpio_reset = gpio; 285 286 /* IRQ */ 287 r = irq_of_parse_and_map(pp, 0); 288 if (r < 0) { 289 nfc_err(&client->dev, "Unable to get irq, error: %d\n", r); 290 return r; 291 } 292 293 phy->irq_polarity = irq_get_trigger_type(r); 294 client->irq = r; 295 296 return 0; 297} 298#else 299static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client) 300{ 301 return -ENODEV; 302} 303#endif 304 305static int st21nfcb_nci_i2c_request_resources(struct i2c_client *client) 306{ 307 struct st21nfcb_nfc_platform_data *pdata; 308 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client); 309 int r; 310 int irq; 311 312 pdata = client->dev.platform_data; 313 if (pdata == NULL) { 314 nfc_err(&client->dev, "No platform data\n"); 315 return -EINVAL; 316 } 317 318 /* store for later use */ 319 phy->gpio_irq = pdata->gpio_irq; 320 phy->gpio_reset = pdata->gpio_reset; 321 phy->irq_polarity = pdata->irq_polarity; 322 323 r = devm_gpio_request_one(&client->dev, phy->gpio_irq, 324 GPIOF_IN, "clf_irq"); 325 if (r) { 326 pr_err("%s : gpio_request failed\n", __FILE__); 327 return -ENODEV; 328 } 329 330 r = devm_gpio_request_one(&client->dev, 331 phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset"); 332 if (r) { 333 pr_err("%s : reset gpio_request failed\n", __FILE__); 334 return -ENODEV; 335 } 336 337 /* IRQ */ 338 irq = gpio_to_irq(phy->gpio_irq); 339 if (irq < 0) { 340 nfc_err(&client->dev, 341 "Unable to get irq number for GPIO %d error %d\n", 342 phy->gpio_irq, r); 343 return -ENODEV; 344 } 345 client->irq = irq; 346 347 return 0; 348} 349 350static int st21nfcb_nci_i2c_probe(struct i2c_client *client, 351 const struct i2c_device_id *id) 352{ 353 struct st21nfcb_i2c_phy *phy; 354 struct st21nfcb_nfc_platform_data *pdata; 355 int r; 356 357 dev_dbg(&client->dev, "%s\n", __func__); 358 dev_dbg(&client->dev, "IRQ: %d\n", client->irq); 359 360 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 361 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); 362 return -ENODEV; 363 } 364 365 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfcb_i2c_phy), 366 GFP_KERNEL); 367 if (!phy) { 368 nfc_err(&client->dev, 369 "Cannot allocate memory for st21nfcb i2c phy.\n"); 370 return -ENOMEM; 371 } 372 373 phy->i2c_dev = client; 374 375 i2c_set_clientdata(client, phy); 376 377 pdata = client->dev.platform_data; 378 if (!pdata && client->dev.of_node) { 379 r = st21nfcb_nci_i2c_of_request_resources(client); 380 if (r) { 381 nfc_err(&client->dev, "No platform data\n"); 382 return r; 383 } 384 } else if (pdata) { 385 r = st21nfcb_nci_i2c_request_resources(client); 386 if (r) { 387 nfc_err(&client->dev, 388 "Cannot get platform resources\n"); 389 return r; 390 } 391 } else { 392 nfc_err(&client->dev, 393 "st21nfcb platform resources not available\n"); 394 return -ENODEV; 395 } 396 397 r = devm_request_threaded_irq(&client->dev, client->irq, NULL, 398 st21nfcb_nci_irq_thread_fn, 399 phy->irq_polarity | IRQF_ONESHOT, 400 ST21NFCB_NCI_DRIVER_NAME, phy); 401 if (r < 0) { 402 nfc_err(&client->dev, "Unable to register IRQ handler\n"); 403 return r; 404 } 405 406 return ndlc_probe(phy, &i2c_phy_ops, &client->dev, 407 ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM, 408 &phy->ndlc); 409} 410 411static int st21nfcb_nci_i2c_remove(struct i2c_client *client) 412{ 413 struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client); 414 415 dev_dbg(&client->dev, "%s\n", __func__); 416 417 ndlc_remove(phy->ndlc); 418 419 if (phy->powered) 420 st21nfcb_nci_i2c_disable(phy); 421 422 return 0; 423} 424 425static const struct of_device_id of_st21nfcb_i2c_match[] = { 426 { .compatible = "st,st21nfcb_i2c", }, 427 {} 428}; 429 430static struct i2c_driver st21nfcb_nci_i2c_driver = { 431 .driver = { 432 .owner = THIS_MODULE, 433 .name = ST21NFCB_NCI_I2C_DRIVER_NAME, 434 .of_match_table = of_match_ptr(of_st21nfcb_i2c_match), 435 }, 436 .probe = st21nfcb_nci_i2c_probe, 437 .id_table = st21nfcb_nci_i2c_id_table, 438 .remove = st21nfcb_nci_i2c_remove, 439}; 440 441module_i2c_driver(st21nfcb_nci_i2c_driver); 442 443MODULE_LICENSE("GPL"); 444MODULE_DESCRIPTION(DRIVER_DESC); 445