i2c-nvidia-gpu.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Nvidia GPU I2C controller Driver
  4. *
  5. * Copyright (C) 2018 NVIDIA Corporation. All rights reserved.
  6. * Author: Ajay Gupta <ajayg@nvidia.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/i2c.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/unaligned.h>
  19. #include "i2c-ccgx-ucsi.h"
  20. /* I2C definitions */
  21. #define I2C_MST_CNTL 0x00
  22. #define I2C_MST_CNTL_GEN_START BIT(0)
  23. #define I2C_MST_CNTL_GEN_STOP BIT(1)
  24. #define I2C_MST_CNTL_CMD_READ (1 << 2)
  25. #define I2C_MST_CNTL_CMD_WRITE (2 << 2)
  26. #define I2C_MST_CNTL_BURST_SIZE_SHIFT 6
  27. #define I2C_MST_CNTL_GEN_NACK BIT(28)
  28. #define I2C_MST_CNTL_STATUS GENMASK(30, 29)
  29. #define I2C_MST_CNTL_STATUS_OKAY (0 << 29)
  30. #define I2C_MST_CNTL_STATUS_NO_ACK (1 << 29)
  31. #define I2C_MST_CNTL_STATUS_TIMEOUT (2 << 29)
  32. #define I2C_MST_CNTL_STATUS_BUS_BUSY (3 << 29)
  33. #define I2C_MST_CNTL_CYCLE_TRIGGER BIT(31)
  34. #define I2C_MST_ADDR 0x04
  35. #define I2C_MST_I2C0_TIMING 0x08
  36. #define I2C_MST_I2C0_TIMING_SCL_PERIOD_100KHZ 0x10e
  37. #define I2C_MST_I2C0_TIMING_TIMEOUT_CLK_CNT 16
  38. #define I2C_MST_I2C0_TIMING_TIMEOUT_CLK_CNT_MAX 255
  39. #define I2C_MST_I2C0_TIMING_TIMEOUT_CHECK BIT(24)
  40. #define I2C_MST_DATA 0x0c
  41. #define I2C_MST_HYBRID_PADCTL 0x20
  42. #define I2C_MST_HYBRID_PADCTL_MODE_I2C BIT(0)
  43. #define I2C_MST_HYBRID_PADCTL_I2C_SCL_INPUT_RCV BIT(14)
  44. #define I2C_MST_HYBRID_PADCTL_I2C_SDA_INPUT_RCV BIT(15)
  45. struct gpu_i2c_dev {
  46. struct device *dev;
  47. void __iomem *regs;
  48. struct i2c_adapter adapter;
  49. struct i2c_board_info *gpu_ccgx_ucsi;
  50. struct i2c_client *ccgx_client;
  51. };
  52. static void gpu_enable_i2c_bus(struct gpu_i2c_dev *i2cd)
  53. {
  54. u32 val;
  55. /* enable I2C */
  56. val = readl(i2cd->regs + I2C_MST_HYBRID_PADCTL);
  57. val |= I2C_MST_HYBRID_PADCTL_MODE_I2C |
  58. I2C_MST_HYBRID_PADCTL_I2C_SCL_INPUT_RCV |
  59. I2C_MST_HYBRID_PADCTL_I2C_SDA_INPUT_RCV;
  60. writel(val, i2cd->regs + I2C_MST_HYBRID_PADCTL);
  61. /* enable 100KHZ mode */
  62. val = I2C_MST_I2C0_TIMING_SCL_PERIOD_100KHZ;
  63. val |= (I2C_MST_I2C0_TIMING_TIMEOUT_CLK_CNT_MAX
  64. << I2C_MST_I2C0_TIMING_TIMEOUT_CLK_CNT);
  65. val |= I2C_MST_I2C0_TIMING_TIMEOUT_CHECK;
  66. writel(val, i2cd->regs + I2C_MST_I2C0_TIMING);
  67. }
  68. static int gpu_i2c_check_status(struct gpu_i2c_dev *i2cd)
  69. {
  70. u32 val;
  71. int ret;
  72. ret = readl_poll_timeout(i2cd->regs + I2C_MST_CNTL, val,
  73. !(val & I2C_MST_CNTL_CYCLE_TRIGGER) ||
  74. (val & I2C_MST_CNTL_STATUS) != I2C_MST_CNTL_STATUS_BUS_BUSY,
  75. 500, 1000 * USEC_PER_MSEC);
  76. if (ret) {
  77. dev_err(i2cd->dev, "i2c timeout error %x\n", val);
  78. return -ETIMEDOUT;
  79. }
  80. val = readl(i2cd->regs + I2C_MST_CNTL);
  81. switch (val & I2C_MST_CNTL_STATUS) {
  82. case I2C_MST_CNTL_STATUS_OKAY:
  83. return 0;
  84. case I2C_MST_CNTL_STATUS_NO_ACK:
  85. return -ENXIO;
  86. case I2C_MST_CNTL_STATUS_TIMEOUT:
  87. return -ETIMEDOUT;
  88. default:
  89. return 0;
  90. }
  91. }
  92. static int gpu_i2c_read(struct gpu_i2c_dev *i2cd, u8 *data, u16 len)
  93. {
  94. int status;
  95. u32 val;
  96. val = I2C_MST_CNTL_GEN_START | I2C_MST_CNTL_CMD_READ |
  97. (len << I2C_MST_CNTL_BURST_SIZE_SHIFT) |
  98. I2C_MST_CNTL_CYCLE_TRIGGER | I2C_MST_CNTL_GEN_NACK;
  99. writel(val, i2cd->regs + I2C_MST_CNTL);
  100. status = gpu_i2c_check_status(i2cd);
  101. if (status < 0)
  102. return status;
  103. val = readl(i2cd->regs + I2C_MST_DATA);
  104. switch (len) {
  105. case 1:
  106. data[0] = val;
  107. break;
  108. case 2:
  109. put_unaligned_be16(val, data);
  110. break;
  111. case 3:
  112. put_unaligned_be24(val, data);
  113. break;
  114. case 4:
  115. put_unaligned_be32(val, data);
  116. break;
  117. default:
  118. break;
  119. }
  120. return status;
  121. }
  122. static int gpu_i2c_start(struct gpu_i2c_dev *i2cd)
  123. {
  124. writel(I2C_MST_CNTL_GEN_START, i2cd->regs + I2C_MST_CNTL);
  125. return gpu_i2c_check_status(i2cd);
  126. }
  127. static int gpu_i2c_stop(struct gpu_i2c_dev *i2cd)
  128. {
  129. writel(I2C_MST_CNTL_GEN_STOP, i2cd->regs + I2C_MST_CNTL);
  130. return gpu_i2c_check_status(i2cd);
  131. }
  132. static int gpu_i2c_write(struct gpu_i2c_dev *i2cd, u8 data)
  133. {
  134. u32 val;
  135. writel(data, i2cd->regs + I2C_MST_DATA);
  136. val = I2C_MST_CNTL_CMD_WRITE | (1 << I2C_MST_CNTL_BURST_SIZE_SHIFT);
  137. writel(val, i2cd->regs + I2C_MST_CNTL);
  138. return gpu_i2c_check_status(i2cd);
  139. }
  140. static int gpu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  141. {
  142. struct gpu_i2c_dev *i2cd = i2c_get_adapdata(adap);
  143. int status, status2;
  144. bool send_stop = true;
  145. int i, j;
  146. /*
  147. * The controller supports maximum 4 byte read due to known
  148. * limitation of sending STOP after every read.
  149. */
  150. pm_runtime_get_sync(i2cd->dev);
  151. for (i = 0; i < num; i++) {
  152. if (msgs[i].flags & I2C_M_RD) {
  153. /* program client address before starting read */
  154. writel(msgs[i].addr, i2cd->regs + I2C_MST_ADDR);
  155. /* gpu_i2c_read has implicit start */
  156. status = gpu_i2c_read(i2cd, msgs[i].buf, msgs[i].len);
  157. if (status < 0)
  158. goto exit;
  159. } else {
  160. u8 addr = i2c_8bit_addr_from_msg(msgs + i);
  161. status = gpu_i2c_start(i2cd);
  162. if (status < 0) {
  163. if (i == 0)
  164. send_stop = false;
  165. goto exit;
  166. }
  167. status = gpu_i2c_write(i2cd, addr);
  168. if (status < 0)
  169. goto exit;
  170. for (j = 0; j < msgs[i].len; j++) {
  171. status = gpu_i2c_write(i2cd, msgs[i].buf[j]);
  172. if (status < 0)
  173. goto exit;
  174. }
  175. }
  176. }
  177. send_stop = false;
  178. status = gpu_i2c_stop(i2cd);
  179. if (status < 0)
  180. goto exit;
  181. status = i;
  182. exit:
  183. if (send_stop) {
  184. status2 = gpu_i2c_stop(i2cd);
  185. if (status2 < 0)
  186. dev_err(i2cd->dev, "i2c stop failed %d\n", status2);
  187. }
  188. pm_runtime_mark_last_busy(i2cd->dev);
  189. pm_runtime_put_autosuspend(i2cd->dev);
  190. return status;
  191. }
  192. static const struct i2c_adapter_quirks gpu_i2c_quirks = {
  193. .max_read_len = 4,
  194. .max_comb_2nd_msg_len = 4,
  195. .flags = I2C_AQ_COMB_WRITE_THEN_READ,
  196. };
  197. static u32 gpu_i2c_functionality(struct i2c_adapter *adap)
  198. {
  199. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  200. }
  201. static const struct i2c_algorithm gpu_i2c_algorithm = {
  202. .xfer = gpu_i2c_xfer,
  203. .functionality = gpu_i2c_functionality,
  204. };
  205. /*
  206. * This driver is for Nvidia GPU cards with USB Type-C interface.
  207. * We want to identify the cards using vendor ID and class code only
  208. * to avoid dependency of adding product id for any new card which
  209. * requires this driver.
  210. * Currently there is no class code defined for UCSI device over PCI
  211. * so using UNKNOWN class for now and it will be updated when UCSI
  212. * over PCI gets a class code.
  213. * There is no other NVIDIA cards with UNKNOWN class code. Even if the
  214. * driver gets loaded for an undesired card then eventually i2c_read()
  215. * (initiated from UCSI i2c_client) will timeout or UCSI commands will
  216. * timeout.
  217. */
  218. #define PCI_CLASS_SERIAL_UNKNOWN 0x0c80
  219. static const struct pci_device_id gpu_i2c_ids[] = {
  220. { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  221. PCI_CLASS_SERIAL_UNKNOWN << 8, 0xffffff00},
  222. { }
  223. };
  224. MODULE_DEVICE_TABLE(pci, gpu_i2c_ids);
  225. static const struct property_entry ccgx_props[] = {
  226. /* Use FW built for NVIDIA GPU only */
  227. PROPERTY_ENTRY_STRING("firmware-name", "nvidia,gpu"),
  228. /* USB-C doesn't power the system */
  229. PROPERTY_ENTRY_U8("scope", POWER_SUPPLY_SCOPE_DEVICE),
  230. { }
  231. };
  232. static const struct software_node ccgx_node = {
  233. .properties = ccgx_props,
  234. };
  235. static int gpu_i2c_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  236. {
  237. struct device *dev = &pdev->dev;
  238. struct gpu_i2c_dev *i2cd;
  239. int status;
  240. i2cd = devm_kzalloc(dev, sizeof(*i2cd), GFP_KERNEL);
  241. if (!i2cd)
  242. return -ENOMEM;
  243. i2cd->dev = dev;
  244. dev_set_drvdata(dev, i2cd);
  245. status = pcim_enable_device(pdev);
  246. if (status < 0)
  247. return dev_err_probe(dev, status, "pcim_enable_device failed\n");
  248. pci_set_master(pdev);
  249. i2cd->regs = pcim_iomap(pdev, 0, 0);
  250. if (!i2cd->regs)
  251. return dev_err_probe(dev, -ENOMEM, "pcim_iomap failed\n");
  252. status = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  253. if (status < 0)
  254. return dev_err_probe(dev, status, "pci_alloc_irq_vectors err\n");
  255. gpu_enable_i2c_bus(i2cd);
  256. i2c_set_adapdata(&i2cd->adapter, i2cd);
  257. i2cd->adapter.owner = THIS_MODULE;
  258. strscpy(i2cd->adapter.name, "NVIDIA GPU I2C adapter",
  259. sizeof(i2cd->adapter.name));
  260. i2cd->adapter.algo = &gpu_i2c_algorithm;
  261. i2cd->adapter.quirks = &gpu_i2c_quirks;
  262. i2cd->adapter.dev.parent = dev;
  263. status = i2c_add_adapter(&i2cd->adapter);
  264. if (status < 0)
  265. goto free_irq_vectors;
  266. i2cd->ccgx_client = i2c_new_ccgx_ucsi(&i2cd->adapter, pdev->irq, &ccgx_node);
  267. if (IS_ERR(i2cd->ccgx_client)) {
  268. status = dev_err_probe(dev, PTR_ERR(i2cd->ccgx_client), "register UCSI failed\n");
  269. goto del_adapter;
  270. }
  271. pm_runtime_set_autosuspend_delay(dev, 3000);
  272. pm_runtime_use_autosuspend(dev);
  273. pm_runtime_put_autosuspend(dev);
  274. pm_runtime_allow(dev);
  275. return 0;
  276. del_adapter:
  277. i2c_del_adapter(&i2cd->adapter);
  278. free_irq_vectors:
  279. pci_free_irq_vectors(pdev);
  280. return status;
  281. }
  282. static void gpu_i2c_remove(struct pci_dev *pdev)
  283. {
  284. struct gpu_i2c_dev *i2cd = pci_get_drvdata(pdev);
  285. pm_runtime_get_noresume(i2cd->dev);
  286. i2c_del_adapter(&i2cd->adapter);
  287. pci_free_irq_vectors(pdev);
  288. }
  289. #define gpu_i2c_suspend NULL
  290. static __maybe_unused int gpu_i2c_resume(struct device *dev)
  291. {
  292. struct gpu_i2c_dev *i2cd = dev_get_drvdata(dev);
  293. gpu_enable_i2c_bus(i2cd);
  294. /*
  295. * Runtime resume ccgx client so that it can see for any
  296. * connector change event. Old ccg firmware has known
  297. * issue of not triggering interrupt when a device is
  298. * connected to runtime resume the controller.
  299. */
  300. pm_request_resume(&i2cd->ccgx_client->dev);
  301. return 0;
  302. }
  303. static UNIVERSAL_DEV_PM_OPS(gpu_i2c_driver_pm, gpu_i2c_suspend, gpu_i2c_resume,
  304. NULL);
  305. static struct pci_driver gpu_i2c_driver = {
  306. .name = "nvidia-gpu",
  307. .id_table = gpu_i2c_ids,
  308. .probe = gpu_i2c_probe,
  309. .remove = gpu_i2c_remove,
  310. .driver = {
  311. .pm = &gpu_i2c_driver_pm,
  312. },
  313. };
  314. module_pci_driver(gpu_i2c_driver);
  315. MODULE_AUTHOR("Ajay Gupta <ajayg@nvidia.com>");
  316. MODULE_DESCRIPTION("Nvidia GPU I2C controller Driver");
  317. MODULE_LICENSE("GPL v2");