1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Based on Synopsys DesignWare I2C adapter driver.
4 *
5 * Copyright (C) 2025 Advanced Micro Devices, Inc.
6 */
7
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/soc/amd/isp4_misc.h>
12
13 #include "i2c-designware-core.h"
14
15 #define DRV_NAME "amd_isp_i2c_designware"
16 #define AMD_ISP_I2C_INPUT_CLK 100 /* Mhz */
17
amd_isp_dw_i2c_plat_pm_cleanup(struct dw_i2c_dev * i2c_dev)18 static void amd_isp_dw_i2c_plat_pm_cleanup(struct dw_i2c_dev *i2c_dev)
19 {
20 pm_runtime_disable(i2c_dev->dev);
21
22 if (i2c_dev->shared_with_punit)
23 pm_runtime_put_noidle(i2c_dev->dev);
24 }
25
amd_isp_dw_i2c_get_clk_rate(struct dw_i2c_dev * i2c_dev)26 static inline u32 amd_isp_dw_i2c_get_clk_rate(struct dw_i2c_dev *i2c_dev)
27 {
28 return AMD_ISP_I2C_INPUT_CLK * 1000;
29 }
30
amd_isp_dw_i2c_plat_probe(struct platform_device * pdev)31 static int amd_isp_dw_i2c_plat_probe(struct platform_device *pdev)
32 {
33 struct dw_i2c_dev *isp_i2c_dev;
34 struct i2c_adapter *adap;
35 int ret;
36
37 isp_i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*isp_i2c_dev), GFP_KERNEL);
38 if (!isp_i2c_dev)
39 return -ENOMEM;
40 isp_i2c_dev->dev = &pdev->dev;
41
42 pdev->dev.init_name = DRV_NAME;
43
44 /*
45 * Use the polling mode to send/receive the data, because
46 * no IRQ connection from ISP I2C
47 */
48 isp_i2c_dev->flags |= ACCESS_POLLING;
49 platform_set_drvdata(pdev, isp_i2c_dev);
50
51 isp_i2c_dev->base = devm_platform_ioremap_resource(pdev, 0);
52 if (IS_ERR(isp_i2c_dev->base))
53 return dev_err_probe(&pdev->dev, PTR_ERR(isp_i2c_dev->base),
54 "failed to get IOMEM resource\n");
55
56 isp_i2c_dev->get_clk_rate_khz = amd_isp_dw_i2c_get_clk_rate;
57 ret = i2c_dw_fw_parse_and_configure(isp_i2c_dev);
58 if (ret)
59 return dev_err_probe(&pdev->dev, ret,
60 "failed to parse i2c dw fwnode and configure\n");
61
62 i2c_dw_configure(isp_i2c_dev);
63
64 adap = &isp_i2c_dev->adapter;
65 adap->owner = THIS_MODULE;
66 scnprintf(adap->name, sizeof(adap->name), AMDISP_I2C_ADAP_NAME);
67 ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
68 adap->dev.of_node = pdev->dev.of_node;
69 /* use dynamically allocated adapter id */
70 adap->nr = -1;
71
72 if (isp_i2c_dev->flags & ACCESS_NO_IRQ_SUSPEND)
73 dev_pm_set_driver_flags(&pdev->dev,
74 DPM_FLAG_SMART_PREPARE);
75 else
76 dev_pm_set_driver_flags(&pdev->dev,
77 DPM_FLAG_SMART_PREPARE |
78 DPM_FLAG_SMART_SUSPEND);
79
80 device_enable_async_suspend(&pdev->dev);
81
82 if (isp_i2c_dev->shared_with_punit)
83 pm_runtime_get_noresume(&pdev->dev);
84
85 pm_runtime_enable(&pdev->dev);
86 pm_runtime_get_sync(&pdev->dev);
87
88 ret = i2c_dw_probe(isp_i2c_dev);
89 if (ret) {
90 dev_err_probe(&pdev->dev, ret, "i2c_dw_probe failed\n");
91 goto error_release_rpm;
92 }
93
94 pm_runtime_put_sync(&pdev->dev);
95
96 return 0;
97
98 error_release_rpm:
99 amd_isp_dw_i2c_plat_pm_cleanup(isp_i2c_dev);
100 pm_runtime_put_sync(&pdev->dev);
101 return ret;
102 }
103
amd_isp_dw_i2c_plat_remove(struct platform_device * pdev)104 static void amd_isp_dw_i2c_plat_remove(struct platform_device *pdev)
105 {
106 struct dw_i2c_dev *isp_i2c_dev = platform_get_drvdata(pdev);
107
108 pm_runtime_get_sync(&pdev->dev);
109
110 i2c_del_adapter(&isp_i2c_dev->adapter);
111
112 i2c_dw_disable(isp_i2c_dev);
113
114 pm_runtime_put_sync(&pdev->dev);
115 amd_isp_dw_i2c_plat_pm_cleanup(isp_i2c_dev);
116 }
117
amd_isp_dw_i2c_plat_prepare(struct device * dev)118 static int amd_isp_dw_i2c_plat_prepare(struct device *dev)
119 {
120 /*
121 * If the ACPI companion device object is present for this device, it
122 * may be accessed during suspend and resume of other devices via I2C
123 * operation regions, so tell the PM core and middle layers to avoid
124 * skipping system suspend/resume callbacks for it in that case.
125 */
126 return !has_acpi_companion(dev);
127 }
128
amd_isp_dw_i2c_plat_runtime_suspend(struct device * dev)129 static int amd_isp_dw_i2c_plat_runtime_suspend(struct device *dev)
130 {
131 struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
132
133 if (i_dev->shared_with_punit)
134 return 0;
135
136 i2c_dw_disable(i_dev);
137 i2c_dw_prepare_clk(i_dev, false);
138
139 return 0;
140 }
141
amd_isp_dw_i2c_plat_suspend(struct device * dev)142 static int amd_isp_dw_i2c_plat_suspend(struct device *dev)
143 {
144 struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
145 int ret;
146
147 if (!i_dev)
148 return -ENODEV;
149
150 ret = amd_isp_dw_i2c_plat_runtime_suspend(dev);
151 if (!ret)
152 i2c_mark_adapter_suspended(&i_dev->adapter);
153
154 return ret;
155 }
156
amd_isp_dw_i2c_plat_runtime_resume(struct device * dev)157 static int amd_isp_dw_i2c_plat_runtime_resume(struct device *dev)
158 {
159 struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
160
161 if (!i_dev)
162 return -ENODEV;
163
164 if (!i_dev->shared_with_punit)
165 i2c_dw_prepare_clk(i_dev, true);
166 if (i_dev->init)
167 i_dev->init(i_dev);
168
169 return 0;
170 }
171
amd_isp_dw_i2c_plat_resume(struct device * dev)172 static int amd_isp_dw_i2c_plat_resume(struct device *dev)
173 {
174 struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
175
176 amd_isp_dw_i2c_plat_runtime_resume(dev);
177 i2c_mark_adapter_resumed(&i_dev->adapter);
178
179 return 0;
180 }
181
182 static const struct dev_pm_ops amd_isp_dw_i2c_dev_pm_ops = {
183 .prepare = pm_sleep_ptr(amd_isp_dw_i2c_plat_prepare),
184 LATE_SYSTEM_SLEEP_PM_OPS(amd_isp_dw_i2c_plat_suspend, amd_isp_dw_i2c_plat_resume)
185 RUNTIME_PM_OPS(amd_isp_dw_i2c_plat_runtime_suspend, amd_isp_dw_i2c_plat_runtime_resume, NULL)
186 };
187
188 /* Work with hotplug and coldplug */
189 MODULE_ALIAS("platform:amd_isp_i2c_designware");
190
191 static struct platform_driver amd_isp_dw_i2c_driver = {
192 .probe = amd_isp_dw_i2c_plat_probe,
193 .remove = amd_isp_dw_i2c_plat_remove,
194 .driver = {
195 .name = DRV_NAME,
196 .pm = pm_ptr(&amd_isp_dw_i2c_dev_pm_ops),
197 },
198 };
199 module_platform_driver(amd_isp_dw_i2c_driver);
200
201 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter in AMD ISP");
202 MODULE_IMPORT_NS("I2C_DW");
203 MODULE_IMPORT_NS("I2C_DW_COMMON");
204 MODULE_AUTHOR("Venkata Narendra Kumar Gutta <vengutta@amd.com>");
205 MODULE_AUTHOR("Pratap Nirujogi <pratap.nirujogi@amd.com>");
206 MODULE_AUTHOR("Bin Du <bin.du@amd.com>");
207 MODULE_LICENSE("GPL");
208