i2c-nvidia-gpu.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <[email protected]>
  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 <asm/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_master_xfer(struct i2c_adapter *adap,
  141. struct i2c_msg *msgs, int num)
  142. {
  143. struct gpu_i2c_dev *i2cd = i2c_get_adapdata(adap);
  144. int status, status2;
  145. bool send_stop = true;
  146. int i, j;
  147. /*
  148. * The controller supports maximum 4 byte read due to known
  149. * limitation of sending STOP after every read.
  150. */
  151. pm_runtime_get_sync(i2cd->dev);
  152. for (i = 0; i < num; i++) {
  153. if (msgs[i].flags & I2C_M_RD) {
  154. /* program client address before starting read */
  155. writel(msgs[i].addr, i2cd->regs + I2C_MST_ADDR);
  156. /* gpu_i2c_read has implicit start */
  157. status = gpu_i2c_read(i2cd, msgs[i].buf, msgs[i].len);
  158. if (status < 0)
  159. goto exit;
  160. } else {
  161. u8 addr = i2c_8bit_addr_from_msg(msgs + i);
  162. status = gpu_i2c_start(i2cd);
  163. if (status < 0) {
  164. if (i == 0)
  165. send_stop = false;
  166. goto exit;
  167. }
  168. status = gpu_i2c_write(i2cd, addr);
  169. if (status < 0)
  170. goto exit;
  171. for (j = 0; j < msgs[i].len; j++) {
  172. status = gpu_i2c_write(i2cd, msgs[i].buf[j]);
  173. if (status < 0)
  174. goto exit;
  175. }
  176. }
  177. }
  178. send_stop = false;
  179. status = gpu_i2c_stop(i2cd);
  180. if (status < 0)
  181. goto exit;
  182. status = i;
  183. exit:
  184. if (send_stop) {
  185. status2 = gpu_i2c_stop(i2cd);
  186. if (status2 < 0)
  187. dev_err(i2cd->dev, "i2c stop failed %d\n", status2);
  188. }
  189. pm_runtime_mark_last_busy(i2cd->dev);
  190. pm_runtime_put_autosuspend(i2cd->dev);
  191. return status;
  192. }
  193. static const struct i2c_adapter_quirks gpu_i2c_quirks = {
  194. .max_read_len = 4,
  195. .max_comb_2nd_msg_len = 4,
  196. .flags = I2C_AQ_COMB_WRITE_THEN_READ,
  197. };
  198. static u32 gpu_i2c_functionality(struct i2c_adapter *adap)
  199. {
  200. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  201. }
  202. static const struct i2c_algorithm gpu_i2c_algorithm = {
  203. .master_xfer = gpu_i2c_master_xfer,
  204. .functionality = gpu_i2c_functionality,
  205. };
  206. /*
  207. * This driver is for Nvidia GPU cards with USB Type-C interface.
  208. * We want to identify the cards using vendor ID and class code only
  209. * to avoid dependency of adding product id for any new card which
  210. * requires this driver.
  211. * Currently there is no class code defined for UCSI device over PCI
  212. * so using UNKNOWN class for now and it will be updated when UCSI
  213. * over PCI gets a class code.
  214. * There is no other NVIDIA cards with UNKNOWN class code. Even if the
  215. * driver gets loaded for an undesired card then eventually i2c_read()
  216. * (initiated from UCSI i2c_client) will timeout or UCSI commands will
  217. * timeout.
  218. */
  219. #define PCI_CLASS_SERIAL_UNKNOWN 0x0c80
  220. static const struct pci_device_id gpu_i2c_ids[] = {
  221. { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  222. PCI_CLASS_SERIAL_UNKNOWN << 8, 0xffffff00},
  223. { }
  224. };
  225. MODULE_DEVICE_TABLE(pci, gpu_i2c_ids);
  226. static const struct property_entry ccgx_props[] = {
  227. /* Use FW built for NVIDIA GPU only */
  228. PROPERTY_ENTRY_STRING("firmware-name", "nvidia,gpu"),
  229. /* USB-C doesn't power the system */
  230. PROPERTY_ENTRY_U8("scope", POWER_SUPPLY_SCOPE_DEVICE),
  231. { }
  232. };
  233. static const struct software_node ccgx_node = {
  234. .properties = ccgx_props,
  235. };
  236. static int gpu_i2c_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  237. {
  238. struct device *dev = &pdev->dev;
  239. struct gpu_i2c_dev *i2cd;
  240. int status;
  241. i2cd = devm_kzalloc(dev, sizeof(*i2cd), GFP_KERNEL);
  242. if (!i2cd)
  243. return -ENOMEM;
  244. i2cd->dev = dev;
  245. dev_set_drvdata(dev, i2cd);
  246. status = pcim_enable_device(pdev);
  247. if (status < 0)
  248. return dev_err_probe(dev, status, "pcim_enable_device failed\n");
  249. pci_set_master(pdev);
  250. i2cd->regs = pcim_iomap(pdev, 0, 0);
  251. if (!i2cd->regs)
  252. return dev_err_probe(dev, -ENOMEM, "pcim_iomap failed\n");
  253. status = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  254. if (status < 0)
  255. return dev_err_probe(dev, status, "pci_alloc_irq_vectors err\n");
  256. gpu_enable_i2c_bus(i2cd);
  257. i2c_set_adapdata(&i2cd->adapter, i2cd);
  258. i2cd->adapter.owner = THIS_MODULE;
  259. strscpy(i2cd->adapter.name, "NVIDIA GPU I2C adapter",
  260. sizeof(i2cd->adapter.name));
  261. i2cd->adapter.algo = &gpu_i2c_algorithm;
  262. i2cd->adapter.quirks = &gpu_i2c_quirks;
  263. i2cd->adapter.dev.parent = dev;
  264. status = i2c_add_adapter(&i2cd->adapter);
  265. if (status < 0)
  266. goto free_irq_vectors;
  267. i2cd->ccgx_client = i2c_new_ccgx_ucsi(&i2cd->adapter, pdev->irq, &ccgx_node);
  268. if (IS_ERR(i2cd->ccgx_client)) {
  269. status = dev_err_probe(dev, PTR_ERR(i2cd->ccgx_client), "register UCSI failed\n");
  270. goto del_adapter;
  271. }
  272. pm_runtime_set_autosuspend_delay(dev, 3000);
  273. pm_runtime_use_autosuspend(dev);
  274. pm_runtime_put_autosuspend(dev);
  275. pm_runtime_allow(dev);
  276. return 0;
  277. del_adapter:
  278. i2c_del_adapter(&i2cd->adapter);
  279. free_irq_vectors:
  280. pci_free_irq_vectors(pdev);
  281. return status;
  282. }
  283. static void gpu_i2c_remove(struct pci_dev *pdev)
  284. {
  285. struct gpu_i2c_dev *i2cd = pci_get_drvdata(pdev);
  286. pm_runtime_get_noresume(i2cd->dev);
  287. i2c_del_adapter(&i2cd->adapter);
  288. pci_free_irq_vectors(pdev);
  289. }
  290. #define gpu_i2c_suspend NULL
  291. static __maybe_unused int gpu_i2c_resume(struct device *dev)
  292. {
  293. struct gpu_i2c_dev *i2cd = dev_get_drvdata(dev);
  294. gpu_enable_i2c_bus(i2cd);
  295. /*
  296. * Runtime resume ccgx client so that it can see for any
  297. * connector change event. Old ccg firmware has known
  298. * issue of not triggering interrupt when a device is
  299. * connected to runtime resume the controller.
  300. */
  301. pm_request_resume(&i2cd->ccgx_client->dev);
  302. return 0;
  303. }
  304. static UNIVERSAL_DEV_PM_OPS(gpu_i2c_driver_pm, gpu_i2c_suspend, gpu_i2c_resume,
  305. NULL);
  306. static struct pci_driver gpu_i2c_driver = {
  307. .name = "nvidia-gpu",
  308. .id_table = gpu_i2c_ids,
  309. .probe = gpu_i2c_probe,
  310. .remove = gpu_i2c_remove,
  311. .driver = {
  312. .pm = &gpu_i2c_driver_pm,
  313. },
  314. };
  315. module_pci_driver(gpu_i2c_driver);
  316. MODULE_AUTHOR("Ajay Gupta <[email protected]>");
  317. MODULE_DESCRIPTION("Nvidia GPU I2C controller Driver");
  318. MODULE_LICENSE("GPL v2");