i2c-designware-platdrv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_data/i2c-designware.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/reset.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/suspend.h>
  33. #include "i2c-designware-core.h"
  34. static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
  35. {
  36. return clk_get_rate(dev->clk)/1000;
  37. }
  38. #ifdef CONFIG_ACPI
  39. /*
  40. * The HCNT/LCNT information coming from ACPI should be the most accurate
  41. * for given platform. However, some systems get it wrong. On such systems
  42. * we get better results by calculating those based on the input clock.
  43. */
  44. static const struct dmi_system_id dw_i2c_no_acpi_params[] = {
  45. {
  46. .ident = "Dell Inspiron 7348",
  47. .matches = {
  48. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  49. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7348"),
  50. },
  51. },
  52. { }
  53. };
  54. static void dw_i2c_acpi_params(struct platform_device *pdev, char method[],
  55. u16 *hcnt, u16 *lcnt, u32 *sda_hold)
  56. {
  57. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  58. acpi_handle handle = ACPI_HANDLE(&pdev->dev);
  59. union acpi_object *obj;
  60. if (dmi_check_system(dw_i2c_no_acpi_params))
  61. return;
  62. if (ACPI_FAILURE(acpi_evaluate_object(handle, method, NULL, &buf)))
  63. return;
  64. obj = (union acpi_object *)buf.pointer;
  65. if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 3) {
  66. const union acpi_object *objs = obj->package.elements;
  67. *hcnt = (u16)objs[0].integer.value;
  68. *lcnt = (u16)objs[1].integer.value;
  69. *sda_hold = (u32)objs[2].integer.value;
  70. }
  71. kfree(buf.pointer);
  72. }
  73. static int dw_i2c_acpi_configure(struct platform_device *pdev)
  74. {
  75. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  76. struct i2c_timings *t = &dev->timings;
  77. u32 ss_ht = 0, fp_ht = 0, hs_ht = 0, fs_ht = 0;
  78. acpi_handle handle = ACPI_HANDLE(&pdev->dev);
  79. const struct acpi_device_id *id;
  80. struct acpi_device *adev;
  81. const char *uid;
  82. dev->adapter.nr = -1;
  83. dev->tx_fifo_depth = 32;
  84. dev->rx_fifo_depth = 32;
  85. /*
  86. * Try to get SDA hold time and *CNT values from an ACPI method for
  87. * selected speed modes.
  88. */
  89. dw_i2c_acpi_params(pdev, "SSCN", &dev->ss_hcnt, &dev->ss_lcnt, &ss_ht);
  90. dw_i2c_acpi_params(pdev, "FPCN", &dev->fp_hcnt, &dev->fp_lcnt, &fp_ht);
  91. dw_i2c_acpi_params(pdev, "HSCN", &dev->hs_hcnt, &dev->hs_lcnt, &hs_ht);
  92. dw_i2c_acpi_params(pdev, "FMCN", &dev->fs_hcnt, &dev->fs_lcnt, &fs_ht);
  93. switch (t->bus_freq_hz) {
  94. case 100000:
  95. dev->sda_hold_time = ss_ht;
  96. break;
  97. case 1000000:
  98. dev->sda_hold_time = fp_ht;
  99. break;
  100. case 3400000:
  101. dev->sda_hold_time = hs_ht;
  102. break;
  103. case 400000:
  104. default:
  105. dev->sda_hold_time = fs_ht;
  106. break;
  107. }
  108. id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
  109. if (id && id->driver_data)
  110. dev->flags |= (u32)id->driver_data;
  111. if (acpi_bus_get_device(handle, &adev))
  112. return -ENODEV;
  113. /*
  114. * Cherrytrail I2C7 gets used for the PMIC which gets accessed
  115. * through ACPI opregions during late suspend / early resume
  116. * disable pm for it.
  117. */
  118. uid = adev->pnp.unique_id;
  119. if ((dev->flags & MODEL_CHERRYTRAIL) && !strcmp(uid, "7"))
  120. dev->pm_disabled = true;
  121. return 0;
  122. }
  123. static const struct acpi_device_id dw_i2c_acpi_match[] = {
  124. { "INT33C2", 0 },
  125. { "INT33C3", 0 },
  126. { "INT3432", 0 },
  127. { "INT3433", 0 },
  128. { "80860F41", 0 },
  129. { "808622C1", MODEL_CHERRYTRAIL },
  130. { "AMD0010", ACCESS_INTR_MASK },
  131. { "AMDI0010", ACCESS_INTR_MASK },
  132. { "AMDI0510", 0 },
  133. { "APMC0D0F", 0 },
  134. { "HISI02A1", 0 },
  135. { "HISI02A2", 0 },
  136. { }
  137. };
  138. MODULE_DEVICE_TABLE(acpi, dw_i2c_acpi_match);
  139. #else
  140. static inline int dw_i2c_acpi_configure(struct platform_device *pdev)
  141. {
  142. return -ENODEV;
  143. }
  144. #endif
  145. static void i2c_dw_configure_master(struct dw_i2c_dev *dev)
  146. {
  147. struct i2c_timings *t = &dev->timings;
  148. dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY;
  149. dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  150. DW_IC_CON_RESTART_EN;
  151. dev->mode = DW_IC_MASTER;
  152. switch (t->bus_freq_hz) {
  153. case 100000:
  154. dev->master_cfg |= DW_IC_CON_SPEED_STD;
  155. break;
  156. case 3400000:
  157. dev->master_cfg |= DW_IC_CON_SPEED_HIGH;
  158. break;
  159. default:
  160. dev->master_cfg |= DW_IC_CON_SPEED_FAST;
  161. }
  162. }
  163. static void i2c_dw_configure_slave(struct dw_i2c_dev *dev)
  164. {
  165. dev->functionality = I2C_FUNC_SLAVE | DW_IC_DEFAULT_FUNCTIONALITY;
  166. dev->slave_cfg = DW_IC_CON_RX_FIFO_FULL_HLD_CTRL |
  167. DW_IC_CON_RESTART_EN | DW_IC_CON_STOP_DET_IFADDRESSED;
  168. dev->mode = DW_IC_SLAVE;
  169. }
  170. static void dw_i2c_set_fifo_size(struct dw_i2c_dev *dev, int id)
  171. {
  172. u32 param, tx_fifo_depth, rx_fifo_depth;
  173. /*
  174. * Try to detect the FIFO depth if not set by interface driver,
  175. * the depth could be from 2 to 256 from HW spec.
  176. */
  177. param = i2c_dw_read_comp_param(dev);
  178. tx_fifo_depth = ((param >> 16) & 0xff) + 1;
  179. rx_fifo_depth = ((param >> 8) & 0xff) + 1;
  180. if (!dev->tx_fifo_depth) {
  181. dev->tx_fifo_depth = tx_fifo_depth;
  182. dev->rx_fifo_depth = rx_fifo_depth;
  183. dev->adapter.nr = id;
  184. } else if (tx_fifo_depth >= 2) {
  185. dev->tx_fifo_depth = min_t(u32, dev->tx_fifo_depth,
  186. tx_fifo_depth);
  187. dev->rx_fifo_depth = min_t(u32, dev->rx_fifo_depth,
  188. rx_fifo_depth);
  189. }
  190. }
  191. static void dw_i2c_plat_pm_cleanup(struct dw_i2c_dev *dev)
  192. {
  193. pm_runtime_disable(dev->dev);
  194. if (dev->pm_disabled)
  195. pm_runtime_put_noidle(dev->dev);
  196. }
  197. static int dw_i2c_plat_probe(struct platform_device *pdev)
  198. {
  199. struct dw_i2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
  200. struct i2c_adapter *adap;
  201. struct dw_i2c_dev *dev;
  202. struct i2c_timings *t;
  203. u32 acpi_speed;
  204. struct resource *mem;
  205. int i, irq, ret;
  206. static const int supported_speeds[] = {
  207. 0, 100000, 400000, 1000000, 3400000
  208. };
  209. irq = platform_get_irq(pdev, 0);
  210. if (irq < 0)
  211. return irq;
  212. dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
  213. if (!dev)
  214. return -ENOMEM;
  215. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  216. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  217. if (IS_ERR(dev->base))
  218. return PTR_ERR(dev->base);
  219. dev->dev = &pdev->dev;
  220. dev->irq = irq;
  221. platform_set_drvdata(pdev, dev);
  222. dev->rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
  223. if (IS_ERR(dev->rst)) {
  224. if (PTR_ERR(dev->rst) == -EPROBE_DEFER)
  225. return -EPROBE_DEFER;
  226. } else {
  227. reset_control_deassert(dev->rst);
  228. }
  229. t = &dev->timings;
  230. if (pdata)
  231. t->bus_freq_hz = pdata->i2c_scl_freq;
  232. else
  233. i2c_parse_fw_timings(&pdev->dev, t, false);
  234. acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev);
  235. /*
  236. * Some DSTDs use a non standard speed, round down to the lowest
  237. * standard speed.
  238. */
  239. for (i = 1; i < ARRAY_SIZE(supported_speeds); i++) {
  240. if (acpi_speed < supported_speeds[i])
  241. break;
  242. }
  243. acpi_speed = supported_speeds[i - 1];
  244. /*
  245. * Find bus speed from the "clock-frequency" device property, ACPI
  246. * or by using fast mode if neither is set.
  247. */
  248. if (acpi_speed && t->bus_freq_hz)
  249. t->bus_freq_hz = min(t->bus_freq_hz, acpi_speed);
  250. else if (acpi_speed || t->bus_freq_hz)
  251. t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed);
  252. else
  253. t->bus_freq_hz = 400000;
  254. if (has_acpi_companion(&pdev->dev))
  255. dw_i2c_acpi_configure(pdev);
  256. /*
  257. * Only standard mode at 100kHz, fast mode at 400kHz,
  258. * fast mode plus at 1MHz and high speed mode at 3.4MHz are supported.
  259. */
  260. if (t->bus_freq_hz != 100000 && t->bus_freq_hz != 400000 &&
  261. t->bus_freq_hz != 1000000 && t->bus_freq_hz != 3400000) {
  262. dev_err(&pdev->dev,
  263. "%d Hz is unsupported, only 100kHz, 400kHz, 1MHz and 3.4MHz are supported\n",
  264. t->bus_freq_hz);
  265. ret = -EINVAL;
  266. goto exit_reset;
  267. }
  268. ret = i2c_dw_probe_lock_support(dev);
  269. if (ret)
  270. goto exit_reset;
  271. if (i2c_detect_slave_mode(&pdev->dev))
  272. i2c_dw_configure_slave(dev);
  273. else
  274. i2c_dw_configure_master(dev);
  275. dev->clk = devm_clk_get(&pdev->dev, NULL);
  276. if (!i2c_dw_prepare_clk(dev, true)) {
  277. u64 clk_khz;
  278. dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
  279. clk_khz = dev->get_clk_rate_khz(dev);
  280. if (!dev->sda_hold_time && t->sda_hold_ns)
  281. dev->sda_hold_time =
  282. div_u64(clk_khz * t->sda_hold_ns + 500000, 1000000);
  283. }
  284. dw_i2c_set_fifo_size(dev, pdev->id);
  285. adap = &dev->adapter;
  286. adap->owner = THIS_MODULE;
  287. adap->class = I2C_CLASS_DEPRECATED;
  288. ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
  289. adap->dev.of_node = pdev->dev.of_node;
  290. dev_pm_set_driver_flags(&pdev->dev,
  291. DPM_FLAG_SMART_PREPARE |
  292. DPM_FLAG_SMART_SUSPEND |
  293. DPM_FLAG_LEAVE_SUSPENDED);
  294. /* The code below assumes runtime PM to be disabled. */
  295. WARN_ON(pm_runtime_enabled(&pdev->dev));
  296. pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
  297. pm_runtime_use_autosuspend(&pdev->dev);
  298. pm_runtime_set_active(&pdev->dev);
  299. if (dev->pm_disabled)
  300. pm_runtime_get_noresume(&pdev->dev);
  301. pm_runtime_enable(&pdev->dev);
  302. if (dev->mode == DW_IC_SLAVE)
  303. ret = i2c_dw_probe_slave(dev);
  304. else
  305. ret = i2c_dw_probe(dev);
  306. if (ret)
  307. goto exit_probe;
  308. return ret;
  309. exit_probe:
  310. dw_i2c_plat_pm_cleanup(dev);
  311. exit_reset:
  312. if (!IS_ERR_OR_NULL(dev->rst))
  313. reset_control_assert(dev->rst);
  314. return ret;
  315. }
  316. static int dw_i2c_plat_remove(struct platform_device *pdev)
  317. {
  318. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  319. pm_runtime_get_sync(&pdev->dev);
  320. i2c_del_adapter(&dev->adapter);
  321. dev->disable(dev);
  322. pm_runtime_dont_use_autosuspend(&pdev->dev);
  323. pm_runtime_put_sync(&pdev->dev);
  324. dw_i2c_plat_pm_cleanup(dev);
  325. if (!IS_ERR_OR_NULL(dev->rst))
  326. reset_control_assert(dev->rst);
  327. i2c_dw_remove_lock_support(dev);
  328. return 0;
  329. }
  330. #ifdef CONFIG_OF
  331. static const struct of_device_id dw_i2c_of_match[] = {
  332. { .compatible = "snps,designware-i2c", },
  333. {},
  334. };
  335. MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
  336. #endif
  337. #ifdef CONFIG_PM_SLEEP
  338. static int dw_i2c_plat_prepare(struct device *dev)
  339. {
  340. /*
  341. * If the ACPI companion device object is present for this device, it
  342. * may be accessed during suspend and resume of other devices via I2C
  343. * operation regions, so tell the PM core and middle layers to avoid
  344. * skipping system suspend/resume callbacks for it in that case.
  345. */
  346. return !has_acpi_companion(dev);
  347. }
  348. static void dw_i2c_plat_complete(struct device *dev)
  349. {
  350. /*
  351. * The device can only be in runtime suspend at this point if it has not
  352. * been resumed throughout the ending system suspend/resume cycle, so if
  353. * the platform firmware might mess up with it, request the runtime PM
  354. * framework to resume it.
  355. */
  356. if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
  357. pm_request_resume(dev);
  358. }
  359. #else
  360. #define dw_i2c_plat_prepare NULL
  361. #define dw_i2c_plat_complete NULL
  362. #endif
  363. #ifdef CONFIG_PM
  364. static int dw_i2c_plat_suspend(struct device *dev)
  365. {
  366. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  367. if (i_dev->pm_disabled)
  368. return 0;
  369. i_dev->disable(i_dev);
  370. i2c_dw_prepare_clk(i_dev, false);
  371. return 0;
  372. }
  373. static int dw_i2c_plat_resume(struct device *dev)
  374. {
  375. struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
  376. if (!i_dev->pm_disabled)
  377. i2c_dw_prepare_clk(i_dev, true);
  378. i_dev->init(i_dev);
  379. return 0;
  380. }
  381. static const struct dev_pm_ops dw_i2c_dev_pm_ops = {
  382. .prepare = dw_i2c_plat_prepare,
  383. .complete = dw_i2c_plat_complete,
  384. SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume)
  385. SET_RUNTIME_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume, NULL)
  386. };
  387. #define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
  388. #else
  389. #define DW_I2C_DEV_PMOPS NULL
  390. #endif
  391. /* Work with hotplug and coldplug */
  392. MODULE_ALIAS("platform:i2c_designware");
  393. static struct platform_driver dw_i2c_driver = {
  394. .probe = dw_i2c_plat_probe,
  395. .remove = dw_i2c_plat_remove,
  396. .driver = {
  397. .name = "i2c_designware",
  398. .of_match_table = of_match_ptr(dw_i2c_of_match),
  399. .acpi_match_table = ACPI_PTR(dw_i2c_acpi_match),
  400. .pm = DW_I2C_DEV_PMOPS,
  401. },
  402. };
  403. static int __init dw_i2c_init_driver(void)
  404. {
  405. return platform_driver_register(&dw_i2c_driver);
  406. }
  407. subsys_initcall(dw_i2c_init_driver);
  408. static void __exit dw_i2c_exit_driver(void)
  409. {
  410. platform_driver_unregister(&dw_i2c_driver);
  411. }
  412. module_exit(dw_i2c_exit_driver);
  413. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  414. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
  415. MODULE_LICENSE("GPL");