i2c.c 9.4 KB

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