i2c-nvidia-gpu.c 9.1 KB

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