i2c-designware-pcidrv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Synopsys DesignWare I2C adapter driver (master only).
  4. *
  5. * Based on the TI DAVINCI I2C adapter driver.
  6. *
  7. * Copyright (C) 2006 Texas Instruments.
  8. * Copyright (C) 2007 MontaVista Software Inc.
  9. * Copyright (C) 2009 Provigent Ltd.
  10. * Copyright (C) 2011, 2015, 2016 Intel Corporation.
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/errno.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include "i2c-designware-core.h"
  27. #include "i2c-ccgx-ucsi.h"
  28. #define DRIVER_NAME "i2c-designware-pci"
  29. enum dw_pci_ctl_id_t {
  30. medfield,
  31. merrifield,
  32. baytrail,
  33. cherrytrail,
  34. haswell,
  35. elkhartlake,
  36. navi_amd,
  37. };
  38. /*
  39. * This is a legacy structure to describe the hardware counters
  40. * to configure signal timings on the bus. For Device Tree platforms
  41. * one should use the respective properties and for ACPI there is
  42. * a set of ACPI methods that provide these counters. No new
  43. * platform should use this structure.
  44. */
  45. struct dw_scl_sda_cfg {
  46. u16 ss_hcnt;
  47. u16 fs_hcnt;
  48. u16 ss_lcnt;
  49. u16 fs_lcnt;
  50. u32 sda_hold;
  51. };
  52. struct dw_pci_controller {
  53. u32 bus_num;
  54. u32 flags;
  55. struct dw_scl_sda_cfg *scl_sda_cfg;
  56. int (*setup)(struct pci_dev *pdev, struct dw_pci_controller *c);
  57. u32 (*get_clk_rate_khz)(struct dw_i2c_dev *dev);
  58. };
  59. /* Merrifield HCNT/LCNT/SDA hold time */
  60. static struct dw_scl_sda_cfg mrfld_config = {
  61. .ss_hcnt = 0x2f8,
  62. .fs_hcnt = 0x87,
  63. .ss_lcnt = 0x37b,
  64. .fs_lcnt = 0x10a,
  65. };
  66. /* BayTrail HCNT/LCNT/SDA hold time */
  67. static struct dw_scl_sda_cfg byt_config = {
  68. .ss_hcnt = 0x200,
  69. .fs_hcnt = 0x55,
  70. .ss_lcnt = 0x200,
  71. .fs_lcnt = 0x99,
  72. .sda_hold = 0x6,
  73. };
  74. /* Haswell HCNT/LCNT/SDA hold time */
  75. static struct dw_scl_sda_cfg hsw_config = {
  76. .ss_hcnt = 0x01b0,
  77. .fs_hcnt = 0x48,
  78. .ss_lcnt = 0x01fb,
  79. .fs_lcnt = 0xa0,
  80. .sda_hold = 0x9,
  81. };
  82. /* NAVI-AMD HCNT/LCNT/SDA hold time */
  83. static struct dw_scl_sda_cfg navi_amd_config = {
  84. .ss_hcnt = 0x1ae,
  85. .ss_lcnt = 0x23a,
  86. .sda_hold = 0x9,
  87. };
  88. static u32 mfld_get_clk_rate_khz(struct dw_i2c_dev *dev)
  89. {
  90. return 25000;
  91. }
  92. static int mfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
  93. {
  94. struct dw_i2c_dev *dev = dev_get_drvdata(&pdev->dev);
  95. switch (pdev->device) {
  96. case 0x0817:
  97. dev->timings.bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ;
  98. fallthrough;
  99. case 0x0818:
  100. case 0x0819:
  101. c->bus_num = pdev->device - 0x817 + 3;
  102. return 0;
  103. case 0x082C:
  104. case 0x082D:
  105. case 0x082E:
  106. c->bus_num = pdev->device - 0x82C + 0;
  107. return 0;
  108. }
  109. return -ENODEV;
  110. }
  111. static int mrfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
  112. {
  113. /*
  114. * On Intel Merrifield the user visible i2c buses are enumerated
  115. * [1..7]. So, we add 1 to shift the default range. Besides that the
  116. * first PCI slot provides 4 functions, that's why we have to add 0 to
  117. * the first slot and 4 to the next one.
  118. */
  119. switch (PCI_SLOT(pdev->devfn)) {
  120. case 8:
  121. c->bus_num = PCI_FUNC(pdev->devfn) + 0 + 1;
  122. return 0;
  123. case 9:
  124. c->bus_num = PCI_FUNC(pdev->devfn) + 4 + 1;
  125. return 0;
  126. }
  127. return -ENODEV;
  128. }
  129. static u32 ehl_get_clk_rate_khz(struct dw_i2c_dev *dev)
  130. {
  131. return 100000;
  132. }
  133. static u32 navi_amd_get_clk_rate_khz(struct dw_i2c_dev *dev)
  134. {
  135. return 100000;
  136. }
  137. static int navi_amd_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
  138. {
  139. struct dw_i2c_dev *dev = dev_get_drvdata(&pdev->dev);
  140. dev->flags |= MODEL_AMD_NAVI_GPU;
  141. dev->timings.bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ;
  142. return 0;
  143. }
  144. static struct dw_pci_controller dw_pci_controllers[] = {
  145. [medfield] = {
  146. .bus_num = -1,
  147. .setup = mfld_setup,
  148. .get_clk_rate_khz = mfld_get_clk_rate_khz,
  149. },
  150. [merrifield] = {
  151. .bus_num = -1,
  152. .scl_sda_cfg = &mrfld_config,
  153. .setup = mrfld_setup,
  154. },
  155. [baytrail] = {
  156. .bus_num = -1,
  157. .scl_sda_cfg = &byt_config,
  158. },
  159. [haswell] = {
  160. .bus_num = -1,
  161. .scl_sda_cfg = &hsw_config,
  162. },
  163. [cherrytrail] = {
  164. .bus_num = -1,
  165. .scl_sda_cfg = &byt_config,
  166. },
  167. [elkhartlake] = {
  168. .bus_num = -1,
  169. .get_clk_rate_khz = ehl_get_clk_rate_khz,
  170. },
  171. [navi_amd] = {
  172. .bus_num = -1,
  173. .scl_sda_cfg = &navi_amd_config,
  174. .setup = navi_amd_setup,
  175. .get_clk_rate_khz = navi_amd_get_clk_rate_khz,
  176. },
  177. };
  178. static int __maybe_unused i2c_dw_pci_runtime_suspend(struct device *dev)
  179. {
  180. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  181. i_dev->disable(i_dev);
  182. return 0;
  183. }
  184. static int __maybe_unused i2c_dw_pci_suspend(struct device *dev)
  185. {
  186. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  187. i2c_mark_adapter_suspended(&i_dev->adapter);
  188. return i2c_dw_pci_runtime_suspend(dev);
  189. }
  190. static int __maybe_unused i2c_dw_pci_runtime_resume(struct device *dev)
  191. {
  192. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  193. return i_dev->init(i_dev);
  194. }
  195. static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
  196. {
  197. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  198. int ret;
  199. ret = i2c_dw_pci_runtime_resume(dev);
  200. i2c_mark_adapter_resumed(&i_dev->adapter);
  201. return ret;
  202. }
  203. static const struct dev_pm_ops i2c_dw_pm_ops = {
  204. SET_SYSTEM_SLEEP_PM_OPS(i2c_dw_pci_suspend, i2c_dw_pci_resume)
  205. SET_RUNTIME_PM_OPS(i2c_dw_pci_runtime_suspend, i2c_dw_pci_runtime_resume, NULL)
  206. };
  207. static const struct property_entry dgpu_properties[] = {
  208. /* USB-C doesn't power the system */
  209. PROPERTY_ENTRY_U8("scope", POWER_SUPPLY_SCOPE_DEVICE),
  210. {}
  211. };
  212. static const struct software_node dgpu_node = {
  213. .properties = dgpu_properties,
  214. };
  215. static int i2c_dw_pci_probe(struct pci_dev *pdev,
  216. const struct pci_device_id *id)
  217. {
  218. struct dw_i2c_dev *dev;
  219. struct i2c_adapter *adap;
  220. int r;
  221. struct dw_pci_controller *controller;
  222. struct dw_scl_sda_cfg *cfg;
  223. struct i2c_timings *t;
  224. if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers))
  225. return dev_err_probe(&pdev->dev, -EINVAL,
  226. "Invalid driver data %ld\n",
  227. id->driver_data);
  228. controller = &dw_pci_controllers[id->driver_data];
  229. r = pcim_enable_device(pdev);
  230. if (r)
  231. return dev_err_probe(&pdev->dev, r,
  232. "Failed to enable I2C PCI device\n");
  233. pci_set_master(pdev);
  234. r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
  235. if (r)
  236. return dev_err_probe(&pdev->dev, r,
  237. "I/O memory remapping failed\n");
  238. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  239. if (!dev)
  240. return -ENOMEM;
  241. r = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
  242. if (r < 0)
  243. return r;
  244. dev->get_clk_rate_khz = controller->get_clk_rate_khz;
  245. dev->base = pcim_iomap_table(pdev)[0];
  246. dev->dev = &pdev->dev;
  247. dev->irq = pci_irq_vector(pdev, 0);
  248. dev->flags |= controller->flags;
  249. t = &dev->timings;
  250. i2c_parse_fw_timings(&pdev->dev, t, false);
  251. pci_set_drvdata(pdev, dev);
  252. if (controller->setup) {
  253. r = controller->setup(pdev, controller);
  254. if (r) {
  255. pci_free_irq_vectors(pdev);
  256. return r;
  257. }
  258. }
  259. i2c_dw_adjust_bus_speed(dev);
  260. if (has_acpi_companion(&pdev->dev))
  261. i2c_dw_acpi_configure(&pdev->dev);
  262. r = i2c_dw_validate_speed(dev);
  263. if (r) {
  264. pci_free_irq_vectors(pdev);
  265. return r;
  266. }
  267. i2c_dw_configure(dev);
  268. if (controller->scl_sda_cfg) {
  269. cfg = controller->scl_sda_cfg;
  270. dev->ss_hcnt = cfg->ss_hcnt;
  271. dev->fs_hcnt = cfg->fs_hcnt;
  272. dev->ss_lcnt = cfg->ss_lcnt;
  273. dev->fs_lcnt = cfg->fs_lcnt;
  274. dev->sda_hold_time = cfg->sda_hold;
  275. }
  276. adap = &dev->adapter;
  277. adap->owner = THIS_MODULE;
  278. adap->class = 0;
  279. ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
  280. adap->nr = controller->bus_num;
  281. r = i2c_dw_probe(dev);
  282. if (r) {
  283. pci_free_irq_vectors(pdev);
  284. return r;
  285. }
  286. if ((dev->flags & MODEL_MASK) == MODEL_AMD_NAVI_GPU) {
  287. dev->slave = i2c_new_ccgx_ucsi(&dev->adapter, dev->irq, &dgpu_node);
  288. if (IS_ERR(dev->slave))
  289. return dev_err_probe(dev->dev, PTR_ERR(dev->slave),
  290. "register UCSI failed\n");
  291. }
  292. pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
  293. pm_runtime_use_autosuspend(&pdev->dev);
  294. pm_runtime_put_autosuspend(&pdev->dev);
  295. pm_runtime_allow(&pdev->dev);
  296. return 0;
  297. }
  298. static void i2c_dw_pci_remove(struct pci_dev *pdev)
  299. {
  300. struct dw_i2c_dev *dev = pci_get_drvdata(pdev);
  301. dev->disable(dev);
  302. pm_runtime_forbid(&pdev->dev);
  303. pm_runtime_get_noresume(&pdev->dev);
  304. i2c_del_adapter(&dev->adapter);
  305. devm_free_irq(&pdev->dev, dev->irq, dev);
  306. pci_free_irq_vectors(pdev);
  307. }
  308. static const struct pci_device_id i2_designware_pci_ids[] = {
  309. /* Medfield */
  310. { PCI_VDEVICE(INTEL, 0x0817), medfield },
  311. { PCI_VDEVICE(INTEL, 0x0818), medfield },
  312. { PCI_VDEVICE(INTEL, 0x0819), medfield },
  313. { PCI_VDEVICE(INTEL, 0x082C), medfield },
  314. { PCI_VDEVICE(INTEL, 0x082D), medfield },
  315. { PCI_VDEVICE(INTEL, 0x082E), medfield },
  316. /* Merrifield */
  317. { PCI_VDEVICE(INTEL, 0x1195), merrifield },
  318. { PCI_VDEVICE(INTEL, 0x1196), merrifield },
  319. /* Baytrail */
  320. { PCI_VDEVICE(INTEL, 0x0F41), baytrail },
  321. { PCI_VDEVICE(INTEL, 0x0F42), baytrail },
  322. { PCI_VDEVICE(INTEL, 0x0F43), baytrail },
  323. { PCI_VDEVICE(INTEL, 0x0F44), baytrail },
  324. { PCI_VDEVICE(INTEL, 0x0F45), baytrail },
  325. { PCI_VDEVICE(INTEL, 0x0F46), baytrail },
  326. { PCI_VDEVICE(INTEL, 0x0F47), baytrail },
  327. /* Haswell */
  328. { PCI_VDEVICE(INTEL, 0x9c61), haswell },
  329. { PCI_VDEVICE(INTEL, 0x9c62), haswell },
  330. /* Braswell / Cherrytrail */
  331. { PCI_VDEVICE(INTEL, 0x22C1), cherrytrail },
  332. { PCI_VDEVICE(INTEL, 0x22C2), cherrytrail },
  333. { PCI_VDEVICE(INTEL, 0x22C3), cherrytrail },
  334. { PCI_VDEVICE(INTEL, 0x22C4), cherrytrail },
  335. { PCI_VDEVICE(INTEL, 0x22C5), cherrytrail },
  336. { PCI_VDEVICE(INTEL, 0x22C6), cherrytrail },
  337. { PCI_VDEVICE(INTEL, 0x22C7), cherrytrail },
  338. /* Elkhart Lake (PSE I2C) */
  339. { PCI_VDEVICE(INTEL, 0x4bb9), elkhartlake },
  340. { PCI_VDEVICE(INTEL, 0x4bba), elkhartlake },
  341. { PCI_VDEVICE(INTEL, 0x4bbb), elkhartlake },
  342. { PCI_VDEVICE(INTEL, 0x4bbc), elkhartlake },
  343. { PCI_VDEVICE(INTEL, 0x4bbd), elkhartlake },
  344. { PCI_VDEVICE(INTEL, 0x4bbe), elkhartlake },
  345. { PCI_VDEVICE(INTEL, 0x4bbf), elkhartlake },
  346. { PCI_VDEVICE(INTEL, 0x4bc0), elkhartlake },
  347. /* AMD NAVI */
  348. { PCI_VDEVICE(ATI, 0x7314), navi_amd },
  349. { PCI_VDEVICE(ATI, 0x73a4), navi_amd },
  350. { PCI_VDEVICE(ATI, 0x73e4), navi_amd },
  351. { PCI_VDEVICE(ATI, 0x73c4), navi_amd },
  352. { PCI_VDEVICE(ATI, 0x7444), navi_amd },
  353. { PCI_VDEVICE(ATI, 0x7464), navi_amd },
  354. { 0,}
  355. };
  356. MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
  357. static struct pci_driver dw_i2c_driver = {
  358. .name = DRIVER_NAME,
  359. .id_table = i2_designware_pci_ids,
  360. .probe = i2c_dw_pci_probe,
  361. .remove = i2c_dw_pci_remove,
  362. .driver = {
  363. .pm = &i2c_dw_pm_ops,
  364. },
  365. };
  366. module_pci_driver(dw_i2c_driver);
  367. /* Work with hotplug and coldplug */
  368. MODULE_ALIAS("i2c_designware-pci");
  369. MODULE_AUTHOR("Baruch Siach <[email protected]>");
  370. MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
  371. MODULE_LICENSE("GPL");