i2c-designware-platdrv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Synopsys DesignWare I2C adapter driver.
  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. */
  11. #include <linux/acpi.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/dmi.h>
  16. #include <linux/err.h>
  17. #include <linux/errno.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/property.h>
  29. #include <linux/regmap.h>
  30. #include <linux/reset.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/suspend.h>
  34. #include <linux/units.h>
  35. #include "i2c-designware-core.h"
  36. static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
  37. {
  38. return clk_get_rate(dev->clk) / KILO;
  39. }
  40. #ifdef CONFIG_ACPI
  41. static const struct acpi_device_id dw_i2c_acpi_match[] = {
  42. { "INT33C2", 0 },
  43. { "INT33C3", 0 },
  44. { "INT3432", 0 },
  45. { "INT3433", 0 },
  46. { "80860F41", ACCESS_NO_IRQ_SUSPEND },
  47. { "808622C1", ACCESS_NO_IRQ_SUSPEND },
  48. { "AMD0010", ACCESS_INTR_MASK },
  49. { "AMDI0010", ACCESS_INTR_MASK },
  50. { "AMDI0019", ACCESS_INTR_MASK | ARBITRATION_SEMAPHORE },
  51. { "AMDI0510", 0 },
  52. { "APMC0D0F", 0 },
  53. { "HISI02A1", 0 },
  54. { "HISI02A2", 0 },
  55. { "HISI02A3", 0 },
  56. { "HYGO0010", ACCESS_INTR_MASK },
  57. { }
  58. };
  59. MODULE_DEVICE_TABLE(acpi, dw_i2c_acpi_match);
  60. #endif
  61. #ifdef CONFIG_OF
  62. #define BT1_I2C_CTL 0x100
  63. #define BT1_I2C_CTL_ADDR_MASK GENMASK(7, 0)
  64. #define BT1_I2C_CTL_WR BIT(8)
  65. #define BT1_I2C_CTL_GO BIT(31)
  66. #define BT1_I2C_DI 0x104
  67. #define BT1_I2C_DO 0x108
  68. static int bt1_i2c_read(void *context, unsigned int reg, unsigned int *val)
  69. {
  70. struct dw_i2c_dev *dev = context;
  71. int ret;
  72. /*
  73. * Note these methods shouldn't ever fail because the system controller
  74. * registers are memory mapped. We check the return value just in case.
  75. */
  76. ret = regmap_write(dev->sysmap, BT1_I2C_CTL,
  77. BT1_I2C_CTL_GO | (reg & BT1_I2C_CTL_ADDR_MASK));
  78. if (ret)
  79. return ret;
  80. return regmap_read(dev->sysmap, BT1_I2C_DO, val);
  81. }
  82. static int bt1_i2c_write(void *context, unsigned int reg, unsigned int val)
  83. {
  84. struct dw_i2c_dev *dev = context;
  85. int ret;
  86. ret = regmap_write(dev->sysmap, BT1_I2C_DI, val);
  87. if (ret)
  88. return ret;
  89. return regmap_write(dev->sysmap, BT1_I2C_CTL,
  90. BT1_I2C_CTL_GO | BT1_I2C_CTL_WR | (reg & BT1_I2C_CTL_ADDR_MASK));
  91. }
  92. static struct regmap_config bt1_i2c_cfg = {
  93. .reg_bits = 32,
  94. .val_bits = 32,
  95. .reg_stride = 4,
  96. .fast_io = true,
  97. .reg_read = bt1_i2c_read,
  98. .reg_write = bt1_i2c_write,
  99. .max_register = DW_IC_COMP_TYPE,
  100. };
  101. static int bt1_i2c_request_regs(struct dw_i2c_dev *dev)
  102. {
  103. dev->sysmap = syscon_node_to_regmap(dev->dev->of_node->parent);
  104. if (IS_ERR(dev->sysmap))
  105. return PTR_ERR(dev->sysmap);
  106. dev->map = devm_regmap_init(dev->dev, NULL, dev, &bt1_i2c_cfg);
  107. return PTR_ERR_OR_ZERO(dev->map);
  108. }
  109. #define MSCC_ICPU_CFG_TWI_DELAY 0x0
  110. #define MSCC_ICPU_CFG_TWI_DELAY_ENABLE BIT(0)
  111. #define MSCC_ICPU_CFG_TWI_SPIKE_FILTER 0x4
  112. static int mscc_twi_set_sda_hold_time(struct dw_i2c_dev *dev)
  113. {
  114. writel((dev->sda_hold_time << 1) | MSCC_ICPU_CFG_TWI_DELAY_ENABLE,
  115. dev->ext + MSCC_ICPU_CFG_TWI_DELAY);
  116. return 0;
  117. }
  118. static int dw_i2c_of_configure(struct platform_device *pdev)
  119. {
  120. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  121. switch (dev->flags & MODEL_MASK) {
  122. case MODEL_MSCC_OCELOT:
  123. dev->ext = devm_platform_ioremap_resource(pdev, 1);
  124. if (!IS_ERR(dev->ext))
  125. dev->set_sda_hold_time = mscc_twi_set_sda_hold_time;
  126. break;
  127. default:
  128. break;
  129. }
  130. return 0;
  131. }
  132. static const struct of_device_id dw_i2c_of_match[] = {
  133. { .compatible = "snps,designware-i2c", },
  134. { .compatible = "mscc,ocelot-i2c", .data = (void *)MODEL_MSCC_OCELOT },
  135. { .compatible = "baikal,bt1-sys-i2c", .data = (void *)MODEL_BAIKAL_BT1 },
  136. {},
  137. };
  138. MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
  139. #else
  140. static int bt1_i2c_request_regs(struct dw_i2c_dev *dev)
  141. {
  142. return -ENODEV;
  143. }
  144. static inline int dw_i2c_of_configure(struct platform_device *pdev)
  145. {
  146. return -ENODEV;
  147. }
  148. #endif
  149. static void dw_i2c_plat_pm_cleanup(struct dw_i2c_dev *dev)
  150. {
  151. pm_runtime_disable(dev->dev);
  152. if (dev->shared_with_punit)
  153. pm_runtime_put_noidle(dev->dev);
  154. }
  155. static int dw_i2c_plat_request_regs(struct dw_i2c_dev *dev)
  156. {
  157. struct platform_device *pdev = to_platform_device(dev->dev);
  158. int ret;
  159. switch (dev->flags & MODEL_MASK) {
  160. case MODEL_BAIKAL_BT1:
  161. ret = bt1_i2c_request_regs(dev);
  162. break;
  163. default:
  164. dev->base = devm_platform_ioremap_resource(pdev, 0);
  165. ret = PTR_ERR_OR_ZERO(dev->base);
  166. break;
  167. }
  168. return ret;
  169. }
  170. static const struct dmi_system_id dw_i2c_hwmon_class_dmi[] = {
  171. {
  172. .ident = "Qtechnology QT5222",
  173. .matches = {
  174. DMI_MATCH(DMI_SYS_VENDOR, "Qtechnology"),
  175. DMI_MATCH(DMI_PRODUCT_NAME, "QT5222"),
  176. },
  177. },
  178. { } /* terminate list */
  179. };
  180. static const struct i2c_dw_semaphore_callbacks i2c_dw_semaphore_cb_table[] = {
  181. #ifdef CONFIG_I2C_DESIGNWARE_BAYTRAIL
  182. {
  183. .probe = i2c_dw_baytrail_probe_lock_support,
  184. },
  185. #endif
  186. #ifdef CONFIG_I2C_DESIGNWARE_AMDPSP
  187. {
  188. .probe = i2c_dw_amdpsp_probe_lock_support,
  189. .remove = i2c_dw_amdpsp_remove_lock_support,
  190. },
  191. #endif
  192. {}
  193. };
  194. static int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev)
  195. {
  196. const struct i2c_dw_semaphore_callbacks *ptr;
  197. int i = 0;
  198. int ret;
  199. ptr = i2c_dw_semaphore_cb_table;
  200. dev->semaphore_idx = -1;
  201. while (ptr->probe) {
  202. ret = ptr->probe(dev);
  203. if (ret) {
  204. /*
  205. * If there is no semaphore device attached to this
  206. * controller, we shouldn't abort general i2c_controller
  207. * probe.
  208. */
  209. if (ret != -ENODEV)
  210. return ret;
  211. i++;
  212. ptr++;
  213. continue;
  214. }
  215. dev->semaphore_idx = i;
  216. break;
  217. }
  218. return 0;
  219. }
  220. static void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev)
  221. {
  222. if (dev->semaphore_idx < 0)
  223. return;
  224. if (i2c_dw_semaphore_cb_table[dev->semaphore_idx].remove)
  225. i2c_dw_semaphore_cb_table[dev->semaphore_idx].remove(dev);
  226. }
  227. static int dw_i2c_plat_probe(struct platform_device *pdev)
  228. {
  229. struct i2c_adapter *adap;
  230. struct dw_i2c_dev *dev;
  231. struct i2c_timings *t;
  232. int irq, ret;
  233. irq = platform_get_irq(pdev, 0);
  234. if (irq < 0)
  235. return irq;
  236. dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
  237. if (!dev)
  238. return -ENOMEM;
  239. dev->flags = (uintptr_t)device_get_match_data(&pdev->dev);
  240. dev->dev = &pdev->dev;
  241. dev->irq = irq;
  242. platform_set_drvdata(pdev, dev);
  243. ret = dw_i2c_plat_request_regs(dev);
  244. if (ret)
  245. return ret;
  246. dev->rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
  247. if (IS_ERR(dev->rst))
  248. return PTR_ERR(dev->rst);
  249. reset_control_deassert(dev->rst);
  250. t = &dev->timings;
  251. i2c_parse_fw_timings(&pdev->dev, t, false);
  252. i2c_dw_adjust_bus_speed(dev);
  253. if (pdev->dev.of_node)
  254. dw_i2c_of_configure(pdev);
  255. if (has_acpi_companion(&pdev->dev))
  256. i2c_dw_acpi_configure(&pdev->dev);
  257. ret = i2c_dw_validate_speed(dev);
  258. if (ret)
  259. goto exit_reset;
  260. ret = i2c_dw_probe_lock_support(dev);
  261. if (ret)
  262. goto exit_reset;
  263. i2c_dw_configure(dev);
  264. /* Optional interface clock */
  265. dev->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
  266. if (IS_ERR(dev->pclk)) {
  267. ret = PTR_ERR(dev->pclk);
  268. goto exit_reset;
  269. }
  270. dev->clk = devm_clk_get_optional(&pdev->dev, NULL);
  271. if (IS_ERR(dev->clk)) {
  272. ret = PTR_ERR(dev->clk);
  273. goto exit_reset;
  274. }
  275. ret = i2c_dw_prepare_clk(dev, true);
  276. if (ret)
  277. goto exit_reset;
  278. if (dev->clk) {
  279. u64 clk_khz;
  280. dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
  281. clk_khz = dev->get_clk_rate_khz(dev);
  282. if (!dev->sda_hold_time && t->sda_hold_ns)
  283. dev->sda_hold_time =
  284. DIV_S64_ROUND_CLOSEST(clk_khz * t->sda_hold_ns, MICRO);
  285. }
  286. adap = &dev->adapter;
  287. adap->owner = THIS_MODULE;
  288. adap->class = dmi_check_system(dw_i2c_hwmon_class_dmi) ?
  289. I2C_CLASS_HWMON : I2C_CLASS_DEPRECATED;
  290. ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
  291. adap->dev.of_node = pdev->dev.of_node;
  292. adap->nr = -1;
  293. if (dev->flags & ACCESS_NO_IRQ_SUSPEND) {
  294. dev_pm_set_driver_flags(&pdev->dev,
  295. DPM_FLAG_SMART_PREPARE);
  296. } else {
  297. dev_pm_set_driver_flags(&pdev->dev,
  298. DPM_FLAG_SMART_PREPARE |
  299. DPM_FLAG_SMART_SUSPEND);
  300. }
  301. device_enable_async_suspend(&pdev->dev);
  302. /* The code below assumes runtime PM to be disabled. */
  303. WARN_ON(pm_runtime_enabled(&pdev->dev));
  304. pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
  305. pm_runtime_use_autosuspend(&pdev->dev);
  306. pm_runtime_set_active(&pdev->dev);
  307. if (dev->shared_with_punit)
  308. pm_runtime_get_noresume(&pdev->dev);
  309. pm_runtime_enable(&pdev->dev);
  310. ret = i2c_dw_probe(dev);
  311. if (ret)
  312. goto exit_probe;
  313. return ret;
  314. exit_probe:
  315. dw_i2c_plat_pm_cleanup(dev);
  316. exit_reset:
  317. reset_control_assert(dev->rst);
  318. return ret;
  319. }
  320. static int dw_i2c_plat_remove(struct platform_device *pdev)
  321. {
  322. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  323. pm_runtime_get_sync(&pdev->dev);
  324. i2c_del_adapter(&dev->adapter);
  325. dev->disable(dev);
  326. pm_runtime_dont_use_autosuspend(&pdev->dev);
  327. pm_runtime_put_sync(&pdev->dev);
  328. dw_i2c_plat_pm_cleanup(dev);
  329. i2c_dw_remove_lock_support(dev);
  330. reset_control_assert(dev->rst);
  331. return 0;
  332. }
  333. #ifdef CONFIG_PM_SLEEP
  334. static int dw_i2c_plat_prepare(struct device *dev)
  335. {
  336. /*
  337. * If the ACPI companion device object is present for this device, it
  338. * may be accessed during suspend and resume of other devices via I2C
  339. * operation regions, so tell the PM core and middle layers to avoid
  340. * skipping system suspend/resume callbacks for it in that case.
  341. */
  342. return !has_acpi_companion(dev);
  343. }
  344. #else
  345. #define dw_i2c_plat_prepare NULL
  346. #endif
  347. #ifdef CONFIG_PM
  348. static int dw_i2c_plat_runtime_suspend(struct device *dev)
  349. {
  350. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  351. if (i_dev->shared_with_punit)
  352. return 0;
  353. i_dev->disable(i_dev);
  354. i2c_dw_prepare_clk(i_dev, false);
  355. return 0;
  356. }
  357. static int __maybe_unused dw_i2c_plat_suspend(struct device *dev)
  358. {
  359. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  360. i2c_mark_adapter_suspended(&i_dev->adapter);
  361. return dw_i2c_plat_runtime_suspend(dev);
  362. }
  363. static int dw_i2c_plat_runtime_resume(struct device *dev)
  364. {
  365. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  366. if (!i_dev->shared_with_punit)
  367. i2c_dw_prepare_clk(i_dev, true);
  368. i_dev->init(i_dev);
  369. return 0;
  370. }
  371. static int __maybe_unused dw_i2c_plat_resume(struct device *dev)
  372. {
  373. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  374. dw_i2c_plat_runtime_resume(dev);
  375. i2c_mark_adapter_resumed(&i_dev->adapter);
  376. return 0;
  377. }
  378. static const struct dev_pm_ops dw_i2c_dev_pm_ops = {
  379. .prepare = dw_i2c_plat_prepare,
  380. SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume)
  381. SET_RUNTIME_PM_OPS(dw_i2c_plat_runtime_suspend, dw_i2c_plat_runtime_resume, NULL)
  382. };
  383. #define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
  384. #else
  385. #define DW_I2C_DEV_PMOPS NULL
  386. #endif
  387. /* Work with hotplug and coldplug */
  388. MODULE_ALIAS("platform:i2c_designware");
  389. static struct platform_driver dw_i2c_driver = {
  390. .probe = dw_i2c_plat_probe,
  391. .remove = dw_i2c_plat_remove,
  392. .driver = {
  393. .name = "i2c_designware",
  394. .of_match_table = of_match_ptr(dw_i2c_of_match),
  395. .acpi_match_table = ACPI_PTR(dw_i2c_acpi_match),
  396. .pm = DW_I2C_DEV_PMOPS,
  397. },
  398. };
  399. static int __init dw_i2c_init_driver(void)
  400. {
  401. return platform_driver_register(&dw_i2c_driver);
  402. }
  403. subsys_initcall(dw_i2c_init_driver);
  404. static void __exit dw_i2c_exit_driver(void)
  405. {
  406. platform_driver_unregister(&dw_i2c_driver);
  407. }
  408. module_exit(dw_i2c_exit_driver);
  409. MODULE_AUTHOR("Baruch Siach <[email protected]>");
  410. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
  411. MODULE_LICENSE("GPL");