i2c.c 9.1 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_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. } else {
  144. phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
  145. *skb = alloc_skb(len, GFP_KERNEL);
  146. if (*skb == NULL) {
  147. r = -ENOMEM;
  148. goto flush;
  149. }
  150. skb_put_data(*skb, tmp, len);
  151. fdp_nci_i2c_dump_skb(&client->dev, "fdp_rd", *skb);
  152. fdp_nci_i2c_remove_len_lrc(*skb);
  153. }
  154. }
  155. return 0;
  156. flush:
  157. /* Flush the remaining data */
  158. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  159. r = -EREMOTEIO;
  160. return r;
  161. }
  162. static irqreturn_t fdp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
  163. {
  164. struct fdp_i2c_phy *phy = phy_id;
  165. struct i2c_client *client;
  166. struct sk_buff *skb;
  167. int r;
  168. if (!phy || irq != phy->i2c_dev->irq) {
  169. WARN_ON_ONCE(1);
  170. return IRQ_NONE;
  171. }
  172. client = phy->i2c_dev;
  173. dev_dbg(&client->dev, "%s\n", __func__);
  174. r = fdp_nci_i2c_read(phy, &skb);
  175. if (r == -EREMOTEIO)
  176. return IRQ_HANDLED;
  177. else if (r == -ENOMEM || r == -EBADMSG)
  178. return IRQ_HANDLED;
  179. if (skb != NULL)
  180. fdp_nci_recv_frame(phy->ndev, skb);
  181. return IRQ_HANDLED;
  182. }
  183. static void fdp_nci_i2c_read_device_properties(struct device *dev,
  184. u8 *clock_type, u32 *clock_freq,
  185. u8 **fw_vsc_cfg)
  186. {
  187. int r;
  188. u8 len;
  189. r = device_property_read_u8(dev, FDP_DP_CLOCK_TYPE_NAME, clock_type);
  190. if (r) {
  191. dev_dbg(dev, "Using default clock type");
  192. *clock_type = 0;
  193. }
  194. r = device_property_read_u32(dev, FDP_DP_CLOCK_FREQ_NAME, clock_freq);
  195. if (r) {
  196. dev_dbg(dev, "Using default clock frequency\n");
  197. *clock_freq = 26000;
  198. }
  199. if (device_property_present(dev, FDP_DP_FW_VSC_CFG_NAME)) {
  200. r = device_property_read_u8(dev, FDP_DP_FW_VSC_CFG_NAME,
  201. &len);
  202. if (r || len <= 0)
  203. goto vsc_read_err;
  204. /* Add 1 to the length to inclue the length byte itself */
  205. len++;
  206. *fw_vsc_cfg = devm_kmalloc_array(dev,
  207. len, sizeof(**fw_vsc_cfg),
  208. GFP_KERNEL);
  209. r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
  210. *fw_vsc_cfg, len);
  211. if (r) {
  212. devm_kfree(dev, *fw_vsc_cfg);
  213. goto vsc_read_err;
  214. }
  215. } else {
  216. vsc_read_err:
  217. dev_dbg(dev, "FW vendor specific commands not present\n");
  218. *fw_vsc_cfg = NULL;
  219. }
  220. dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
  221. *clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
  222. }
  223. static const struct acpi_gpio_params power_gpios = { 0, 0, false };
  224. static const struct acpi_gpio_mapping acpi_fdp_gpios[] = {
  225. { "power-gpios", &power_gpios, 1 },
  226. {},
  227. };
  228. static int fdp_nci_i2c_probe(struct i2c_client *client)
  229. {
  230. struct fdp_i2c_phy *phy;
  231. struct device *dev = &client->dev;
  232. u8 *fw_vsc_cfg;
  233. u8 clock_type;
  234. u32 clock_freq;
  235. int r = 0;
  236. dev_dbg(dev, "%s\n", __func__);
  237. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  238. nfc_err(dev, "No I2C_FUNC_I2C support\n");
  239. return -ENODEV;
  240. }
  241. /* Checking if we have an irq */
  242. if (client->irq <= 0) {
  243. nfc_err(dev, "IRQ not present\n");
  244. return -ENODEV;
  245. }
  246. phy = devm_kzalloc(dev, sizeof(struct fdp_i2c_phy), GFP_KERNEL);
  247. if (!phy)
  248. return -ENOMEM;
  249. phy->i2c_dev = client;
  250. phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
  251. i2c_set_clientdata(client, phy);
  252. r = devm_request_threaded_irq(dev, client->irq,
  253. NULL, fdp_nci_i2c_irq_thread_fn,
  254. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  255. FDP_I2C_DRIVER_NAME, phy);
  256. if (r < 0) {
  257. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  258. return r;
  259. }
  260. r = devm_acpi_dev_add_driver_gpios(dev, acpi_fdp_gpios);
  261. if (r)
  262. dev_dbg(dev, "Unable to add GPIO mapping table\n");
  263. /* Requesting the power gpio */
  264. phy->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
  265. if (IS_ERR(phy->power_gpio)) {
  266. nfc_err(dev, "Power GPIO request failed\n");
  267. return PTR_ERR(phy->power_gpio);
  268. }
  269. /* read device properties to get the clock and production settings */
  270. fdp_nci_i2c_read_device_properties(dev, &clock_type, &clock_freq,
  271. &fw_vsc_cfg);
  272. /* Call the NFC specific probe function */
  273. r = fdp_nci_probe(phy, &i2c_phy_ops, &phy->ndev,
  274. FDP_FRAME_HEADROOM, FDP_FRAME_TAILROOM,
  275. clock_type, clock_freq, fw_vsc_cfg);
  276. if (r < 0) {
  277. nfc_err(dev, "NCI probing error\n");
  278. return r;
  279. }
  280. dev_dbg(dev, "I2C driver loaded\n");
  281. return 0;
  282. }
  283. static int fdp_nci_i2c_remove(struct i2c_client *client)
  284. {
  285. struct fdp_i2c_phy *phy = i2c_get_clientdata(client);
  286. dev_dbg(&client->dev, "%s\n", __func__);
  287. fdp_nci_remove(phy->ndev);
  288. fdp_nci_i2c_disable(phy);
  289. return 0;
  290. }
  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. .probe_new = fdp_nci_i2c_probe,
  302. .remove = fdp_nci_i2c_remove,
  303. };
  304. module_i2c_driver(fdp_nci_i2c_driver);
  305. MODULE_LICENSE("GPL");
  306. MODULE_DESCRIPTION("I2C driver for Intel Fields Peak NFC controller");
  307. MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");