i2c.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* -------------------------------------------------------------------------
  2. * Copyright (C) 2014-2016, Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  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. */
  15. #include <linux/module.h>
  16. #include <linux/acpi.h>
  17. #include <linux/i2c.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/nfc.h>
  20. #include <linux/delay.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <net/nfc/nfc.h>
  23. #include <net/nfc/nci_core.h>
  24. #include "fdp.h"
  25. #define FDP_I2C_DRIVER_NAME "fdp_nci_i2c"
  26. #define FDP_DP_POWER_GPIO_NAME "power"
  27. #define FDP_DP_CLOCK_TYPE_NAME "clock-type"
  28. #define FDP_DP_CLOCK_FREQ_NAME "clock-freq"
  29. #define FDP_DP_FW_VSC_CFG_NAME "fw-vsc-cfg"
  30. #define FDP_FRAME_HEADROOM 2
  31. #define FDP_FRAME_TAILROOM 1
  32. #define FDP_NCI_I2C_MIN_PAYLOAD 5
  33. #define FDP_NCI_I2C_MAX_PAYLOAD 261
  34. #define FDP_POWER_OFF 0
  35. #define FDP_POWER_ON 1
  36. #define fdp_nci_i2c_dump_skb(dev, prefix, skb) \
  37. print_hex_dump(KERN_DEBUG, prefix": ", DUMP_PREFIX_OFFSET, \
  38. 16, 1, (skb)->data, (skb)->len, 0)
  39. static void fdp_nci_i2c_reset(struct fdp_i2c_phy *phy)
  40. {
  41. /* Reset RST/WakeUP for at least 100 micro-second */
  42. gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_OFF);
  43. usleep_range(1000, 4000);
  44. gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_ON);
  45. usleep_range(10000, 14000);
  46. }
  47. static int fdp_nci_i2c_enable(void *phy_id)
  48. {
  49. struct fdp_i2c_phy *phy = phy_id;
  50. dev_dbg(&phy->i2c_dev->dev, "%s\n", __func__);
  51. fdp_nci_i2c_reset(phy);
  52. return 0;
  53. }
  54. static void fdp_nci_i2c_disable(void *phy_id)
  55. {
  56. struct fdp_i2c_phy *phy = phy_id;
  57. dev_dbg(&phy->i2c_dev->dev, "%s\n", __func__);
  58. fdp_nci_i2c_reset(phy);
  59. }
  60. static void fdp_nci_i2c_add_len_lrc(struct sk_buff *skb)
  61. {
  62. u8 lrc = 0;
  63. u16 len, i;
  64. /* Add length header */
  65. len = skb->len;
  66. *skb_push(skb, 1) = len & 0xff;
  67. *skb_push(skb, 1) = len >> 8;
  68. /* Compute and add lrc */
  69. for (i = 0; i < len + 2; i++)
  70. lrc ^= skb->data[i];
  71. *skb_put(skb, 1) = lrc;
  72. }
  73. static void fdp_nci_i2c_remove_len_lrc(struct sk_buff *skb)
  74. {
  75. skb_pull(skb, FDP_FRAME_HEADROOM);
  76. skb_trim(skb, skb->len - FDP_FRAME_TAILROOM);
  77. }
  78. static int fdp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
  79. {
  80. struct fdp_i2c_phy *phy = phy_id;
  81. struct i2c_client *client = phy->i2c_dev;
  82. int r;
  83. if (phy->hard_fault != 0)
  84. return phy->hard_fault;
  85. fdp_nci_i2c_add_len_lrc(skb);
  86. fdp_nci_i2c_dump_skb(&client->dev, "fdp_wr", skb);
  87. r = i2c_master_send(client, skb->data, skb->len);
  88. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  89. usleep_range(1000, 4000);
  90. r = i2c_master_send(client, skb->data, skb->len);
  91. }
  92. if (r < 0 || r != skb->len)
  93. dev_dbg(&client->dev, "%s: error err=%d len=%d\n",
  94. __func__, r, skb->len);
  95. if (r >= 0) {
  96. if (r != skb->len) {
  97. phy->hard_fault = r;
  98. r = -EREMOTEIO;
  99. } else {
  100. r = 0;
  101. }
  102. }
  103. fdp_nci_i2c_remove_len_lrc(skb);
  104. return r;
  105. }
  106. static struct nfc_phy_ops i2c_phy_ops = {
  107. .write = fdp_nci_i2c_write,
  108. .enable = fdp_nci_i2c_enable,
  109. .disable = fdp_nci_i2c_disable,
  110. };
  111. static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb)
  112. {
  113. int r, len;
  114. u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD], lrc, k;
  115. u16 i;
  116. struct i2c_client *client = phy->i2c_dev;
  117. *skb = NULL;
  118. /* Read the length packet and the data packet */
  119. for (k = 0; k < 2; k++) {
  120. len = phy->next_read_size;
  121. r = i2c_master_recv(client, tmp, len);
  122. if (r != len) {
  123. dev_dbg(&client->dev, "%s: i2c recv err: %d\n",
  124. __func__, r);
  125. goto flush;
  126. }
  127. /* Check packet integruty */
  128. for (lrc = i = 0; i < r; i++)
  129. lrc ^= tmp[i];
  130. /*
  131. * LRC check failed. This may due to transmission error or
  132. * desynchronization between driver and FDP. Drop the paquet
  133. * and force resynchronization
  134. */
  135. if (lrc) {
  136. dev_dbg(&client->dev, "%s: corrupted packet\n",
  137. __func__);
  138. phy->next_read_size = 5;
  139. goto flush;
  140. }
  141. /* Packet that contains a length */
  142. if (tmp[0] == 0 && tmp[1] == 0) {
  143. phy->next_read_size = (tmp[2] << 8) + tmp[3] + 3;
  144. } else {
  145. phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
  146. *skb = alloc_skb(len, GFP_KERNEL);
  147. if (*skb == NULL) {
  148. r = -ENOMEM;
  149. goto flush;
  150. }
  151. memcpy(skb_put(*skb, len), tmp, len);
  152. fdp_nci_i2c_dump_skb(&client->dev, "fdp_rd", *skb);
  153. fdp_nci_i2c_remove_len_lrc(*skb);
  154. }
  155. }
  156. return 0;
  157. flush:
  158. /* Flush the remaining data */
  159. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  160. r = -EREMOTEIO;
  161. return r;
  162. }
  163. static irqreturn_t fdp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
  164. {
  165. struct fdp_i2c_phy *phy = phy_id;
  166. struct i2c_client *client;
  167. struct sk_buff *skb;
  168. int r;
  169. if (!phy || irq != phy->i2c_dev->irq) {
  170. WARN_ON_ONCE(1);
  171. return IRQ_NONE;
  172. }
  173. client = phy->i2c_dev;
  174. dev_dbg(&client->dev, "%s\n", __func__);
  175. r = fdp_nci_i2c_read(phy, &skb);
  176. if (r == -EREMOTEIO)
  177. return IRQ_HANDLED;
  178. else if (r == -ENOMEM || r == -EBADMSG)
  179. return IRQ_HANDLED;
  180. if (skb != NULL)
  181. fdp_nci_recv_frame(phy->ndev, skb);
  182. return IRQ_HANDLED;
  183. }
  184. static void fdp_nci_i2c_read_device_properties(struct device *dev,
  185. u8 *clock_type, u32 *clock_freq,
  186. u8 **fw_vsc_cfg)
  187. {
  188. int r;
  189. u8 len;
  190. r = device_property_read_u8(dev, FDP_DP_CLOCK_TYPE_NAME, clock_type);
  191. if (r) {
  192. dev_dbg(dev, "Using default clock type");
  193. *clock_type = 0;
  194. }
  195. r = device_property_read_u32(dev, FDP_DP_CLOCK_FREQ_NAME, clock_freq);
  196. if (r) {
  197. dev_dbg(dev, "Using default clock frequency\n");
  198. *clock_freq = 26000;
  199. }
  200. if (device_property_present(dev, FDP_DP_FW_VSC_CFG_NAME)) {
  201. r = device_property_read_u8(dev, FDP_DP_FW_VSC_CFG_NAME,
  202. &len);
  203. if (r || len <= 0)
  204. goto vsc_read_err;
  205. /* Add 1 to the length to inclue the length byte itself */
  206. len++;
  207. *fw_vsc_cfg = devm_kmalloc(dev,
  208. len * sizeof(**fw_vsc_cfg),
  209. GFP_KERNEL);
  210. r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
  211. *fw_vsc_cfg, len);
  212. if (r) {
  213. devm_kfree(dev, fw_vsc_cfg);
  214. goto vsc_read_err;
  215. }
  216. } else {
  217. vsc_read_err:
  218. dev_dbg(dev, "FW vendor specific commands not present\n");
  219. *fw_vsc_cfg = NULL;
  220. }
  221. dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
  222. *clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
  223. }
  224. static int fdp_nci_i2c_probe(struct i2c_client *client,
  225. const struct i2c_device_id *id)
  226. {
  227. struct fdp_i2c_phy *phy;
  228. struct device *dev = &client->dev;
  229. u8 *fw_vsc_cfg;
  230. u8 clock_type;
  231. u32 clock_freq;
  232. int r = 0;
  233. dev_dbg(dev, "%s\n", __func__);
  234. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  235. nfc_err(dev, "No I2C_FUNC_I2C support\n");
  236. return -ENODEV;
  237. }
  238. phy = devm_kzalloc(dev, sizeof(struct fdp_i2c_phy),
  239. GFP_KERNEL);
  240. if (!phy)
  241. return -ENOMEM;
  242. phy->i2c_dev = client;
  243. phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
  244. i2c_set_clientdata(client, phy);
  245. /* Checking if we have an irq */
  246. if (client->irq <= 0) {
  247. dev_err(dev, "IRQ not present\n");
  248. return -ENODEV;
  249. }
  250. r = request_threaded_irq(client->irq, NULL, fdp_nci_i2c_irq_thread_fn,
  251. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  252. FDP_I2C_DRIVER_NAME, phy);
  253. if (r < 0) {
  254. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  255. return r;
  256. }
  257. /* Requesting the power gpio */
  258. phy->power_gpio = devm_gpiod_get(dev, FDP_DP_POWER_GPIO_NAME,
  259. GPIOD_OUT_LOW);
  260. if (IS_ERR(phy->power_gpio)) {
  261. nfc_err(dev, "Power GPIO request failed\n");
  262. return PTR_ERR(phy->power_gpio);
  263. }
  264. /* read device properties to get the clock and production settings */
  265. fdp_nci_i2c_read_device_properties(dev, &clock_type, &clock_freq,
  266. &fw_vsc_cfg);
  267. /* Call the NFC specific probe function */
  268. r = fdp_nci_probe(phy, &i2c_phy_ops, &phy->ndev,
  269. FDP_FRAME_HEADROOM, FDP_FRAME_TAILROOM,
  270. clock_type, clock_freq, fw_vsc_cfg);
  271. if (r < 0) {
  272. nfc_err(dev, "NCI probing error\n");
  273. return r;
  274. }
  275. dev_dbg(dev, "I2C driver loaded\n");
  276. return 0;
  277. }
  278. static int fdp_nci_i2c_remove(struct i2c_client *client)
  279. {
  280. struct fdp_i2c_phy *phy = i2c_get_clientdata(client);
  281. dev_dbg(&client->dev, "%s\n", __func__);
  282. fdp_nci_remove(phy->ndev);
  283. fdp_nci_i2c_disable(phy);
  284. return 0;
  285. }
  286. static struct i2c_device_id fdp_nci_i2c_id_table[] = {
  287. {"int339a", 0},
  288. {}
  289. };
  290. MODULE_DEVICE_TABLE(i2c, fdp_nci_i2c_id_table);
  291. static const struct acpi_device_id fdp_nci_i2c_acpi_match[] = {
  292. {"INT339A", 0},
  293. {}
  294. };
  295. MODULE_DEVICE_TABLE(acpi, fdp_nci_i2c_acpi_match);
  296. static struct i2c_driver fdp_nci_i2c_driver = {
  297. .driver = {
  298. .name = FDP_I2C_DRIVER_NAME,
  299. .acpi_match_table = ACPI_PTR(fdp_nci_i2c_acpi_match),
  300. },
  301. .id_table = fdp_nci_i2c_id_table,
  302. .probe = fdp_nci_i2c_probe,
  303. .remove = fdp_nci_i2c_remove,
  304. };
  305. module_i2c_driver(fdp_nci_i2c_driver);
  306. MODULE_LICENSE("GPL");
  307. MODULE_DESCRIPTION("I2C driver for Intel Fields Peak NFC controller");
  308. MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");