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/clk-provider.h> 12 #include <linux/clk.h> 13 #include <linux/delay.h> 14 #include <linux/dmi.h> 15 #include <linux/err.h> 16 #include <linux/errno.h> 17 #include <linux/i2c.h> 18 #include <linux/interrupt.h> 19 #include <linux/io.h> 20 #include <linux/kernel.h> 21 #include <linux/mfd/syscon.h> 22 #include <linux/module.h> 23 #include <linux/platform_device.h> 24 #include <linux/pm.h> 25 #include <linux/pm_runtime.h> 26 #include <linux/property.h> 27 #include <linux/regmap.h> 28 #include <linux/reset.h> 29 #include <linux/sched.h> 30 #include <linux/slab.h> 31 #include <linux/units.h> 32 33 #include "i2c-designware-core.h" 34 35 static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev) 36 { 37 return clk_get_rate(dev->clk) / HZ_PER_KHZ; 38 } 39 40 static int dw_i2c_get_parent_regmap(struct dw_i2c_dev *dev) 41 { 42 dev->map = dev_get_regmap(dev->dev->parent, NULL); 43 if (!dev->map) 44 return -ENODEV; 45 46 return 0; 47 } 48 49 static void dw_i2c_plat_pm_cleanup(struct dw_i2c_dev *dev) 50 { 51 pm_runtime_disable(dev->dev); 52 53 if (dev->shared_with_punit) 54 pm_runtime_put_noidle(dev->dev); 55 } 56 57 static int dw_i2c_plat_request_regs(struct dw_i2c_dev *dev) 58 { 59 struct platform_device *pdev = to_platform_device(dev->dev); 60 int ret; 61 62 if (device_is_compatible(dev->dev, "intel,xe-i2c")) 63 return dw_i2c_get_parent_regmap(dev); 64 65 switch (dev->flags & MODEL_MASK) { 66 case MODEL_WANGXUN_SP: 67 ret = dw_i2c_get_parent_regmap(dev); 68 break; 69 default: 70 dev->base = devm_platform_ioremap_resource(pdev, 0); 71 ret = PTR_ERR_OR_ZERO(dev->base); 72 break; 73 } 74 75 return ret; 76 } 77 78 static const struct dmi_system_id dw_i2c_hwmon_class_dmi[] = { 79 { 80 .ident = "Qtechnology QT5222", 81 .matches = { 82 DMI_MATCH(DMI_SYS_VENDOR, "Qtechnology"), 83 DMI_MATCH(DMI_PRODUCT_NAME, "QT5222"), 84 }, 85 }, 86 { } /* terminate list */ 87 }; 88 89 static const struct i2c_dw_semaphore_callbacks i2c_dw_semaphore_cb_table[] = { 90 #ifdef CONFIG_I2C_DESIGNWARE_BAYTRAIL 91 { 92 .probe = i2c_dw_baytrail_probe_lock_support, 93 }, 94 #endif 95 #ifdef CONFIG_I2C_DESIGNWARE_AMDPSP 96 { 97 .probe = i2c_dw_amdpsp_probe_lock_support, 98 }, 99 #endif 100 {} 101 }; 102 103 static int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev) 104 { 105 const struct i2c_dw_semaphore_callbacks *ptr; 106 int i = 0; 107 int ret; 108 109 dev->semaphore_idx = -1; 110 111 for (ptr = i2c_dw_semaphore_cb_table; ptr->probe; ptr++) { 112 ret = ptr->probe(dev); 113 if (ret) { 114 /* 115 * If there is no semaphore device attached to this 116 * controller, we shouldn't abort general i2c_controller 117 * probe. 118 */ 119 if (ret != -ENODEV) 120 return ret; 121 122 i++; 123 continue; 124 } 125 126 dev->semaphore_idx = i; 127 break; 128 } 129 130 return 0; 131 } 132 133 static int dw_i2c_plat_probe(struct platform_device *pdev) 134 { 135 u32 flags = (uintptr_t)device_get_match_data(&pdev->dev); 136 struct device *device = &pdev->dev; 137 struct i2c_adapter *adap; 138 struct dw_i2c_dev *dev; 139 int irq, ret; 140 141 irq = platform_get_irq_optional(pdev, 0); 142 if (irq == -ENXIO) 143 flags |= ACCESS_POLLING; 144 else if (irq < 0) 145 return irq; 146 147 dev = devm_kzalloc(device, sizeof(*dev), GFP_KERNEL); 148 if (!dev) 149 return -ENOMEM; 150 151 if (device_property_present(device, "wx,i2c-snps-model")) 152 flags = MODEL_WANGXUN_SP | ACCESS_POLLING; 153 154 dev->dev = device; 155 dev->irq = irq; 156 dev->flags = flags; 157 platform_set_drvdata(pdev, dev); 158 159 ret = dw_i2c_plat_request_regs(dev); 160 if (ret) 161 return ret; 162 163 dev->rst = devm_reset_control_get_optional_exclusive_deasserted(device, NULL); 164 if (IS_ERR(dev->rst)) 165 return dev_err_probe(device, PTR_ERR(dev->rst), "failed to acquire reset\n"); 166 167 ret = i2c_dw_fw_parse_and_configure(dev); 168 if (ret) 169 return ret; 170 171 ret = i2c_dw_probe_lock_support(dev); 172 if (ret) 173 return dev_err_probe(device, ret, "failed to probe lock support\n"); 174 175 i2c_dw_configure(dev); 176 177 /* Optional interface clock */ 178 dev->pclk = devm_clk_get_optional(device, "pclk"); 179 if (IS_ERR(dev->pclk)) 180 return dev_err_probe(device, PTR_ERR(dev->pclk), "failed to acquire pclk\n"); 181 182 dev->clk = devm_clk_get_optional(device, NULL); 183 if (IS_ERR(dev->clk)) 184 return dev_err_probe(device, PTR_ERR(dev->clk), "failed to acquire clock\n"); 185 186 ret = i2c_dw_prepare_clk(dev, true); 187 if (ret) 188 return ret; 189 190 if (dev->clk) { 191 struct i2c_timings *t = &dev->timings; 192 u64 clk_khz; 193 194 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz; 195 clk_khz = dev->get_clk_rate_khz(dev); 196 197 if (!dev->sda_hold_time && t->sda_hold_ns) 198 dev->sda_hold_time = 199 DIV_S64_ROUND_CLOSEST(clk_khz * t->sda_hold_ns, MICRO); 200 } 201 202 adap = &dev->adapter; 203 adap->owner = THIS_MODULE; 204 adap->class = dmi_check_system(dw_i2c_hwmon_class_dmi) ? 205 I2C_CLASS_HWMON : I2C_CLASS_DEPRECATED; 206 adap->nr = -1; 207 208 if (dev->flags & ACCESS_NO_IRQ_SUSPEND) 209 dev_pm_set_driver_flags(device, DPM_FLAG_SMART_PREPARE); 210 else 211 dev_pm_set_driver_flags(device, DPM_FLAG_SMART_PREPARE | DPM_FLAG_SMART_SUSPEND); 212 213 device_enable_async_suspend(device); 214 215 /* The code below assumes runtime PM to be disabled. */ 216 WARN_ON(pm_runtime_enabled(device)); 217 218 pm_runtime_set_autosuspend_delay(device, 1000); 219 pm_runtime_use_autosuspend(device); 220 pm_runtime_set_active(device); 221 222 if (dev->shared_with_punit) 223 pm_runtime_get_noresume(device); 224 225 pm_runtime_enable(device); 226 227 ret = i2c_dw_probe(dev); 228 if (ret) { 229 dw_i2c_plat_pm_cleanup(dev); 230 i2c_dw_prepare_clk(dev, false); 231 } 232 233 return ret; 234 } 235 236 static void dw_i2c_plat_remove(struct platform_device *pdev) 237 { 238 struct dw_i2c_dev *dev = platform_get_drvdata(pdev); 239 struct device *device = &pdev->dev; 240 241 pm_runtime_get_sync(device); 242 243 i2c_del_adapter(&dev->adapter); 244 245 i2c_dw_disable(dev); 246 247 pm_runtime_dont_use_autosuspend(device); 248 pm_runtime_put_noidle(device); 249 dw_i2c_plat_pm_cleanup(dev); 250 251 i2c_dw_prepare_clk(dev, false); 252 } 253 254 static const struct of_device_id dw_i2c_of_match[] = { 255 { .compatible = "mobileye,eyeq6lplus-i2c" }, 256 { .compatible = "mscc,ocelot-i2c" }, 257 { .compatible = "snps,designware-i2c" }, 258 {} 259 }; 260 MODULE_DEVICE_TABLE(of, dw_i2c_of_match); 261 262 static const struct acpi_device_id dw_i2c_acpi_match[] = { 263 { "80860F41", ACCESS_NO_IRQ_SUSPEND }, 264 { "808622C1", ACCESS_NO_IRQ_SUSPEND }, 265 { "AMD0010", ACCESS_INTR_MASK }, 266 { "AMDI0010", ACCESS_INTR_MASK }, 267 { "AMDI0019", ACCESS_INTR_MASK | ARBITRATION_SEMAPHORE }, 268 { "AMDI0510", 0 }, 269 { "APMC0D0F", 0 }, 270 { "FUJI200B", 0 }, 271 { "GOOG5000", 0 }, 272 { "HISI02A1", 0 }, 273 { "HISI02A2", 0 }, 274 { "HISI02A3", 0 }, 275 { "HJMC3001", 0 }, 276 { "HYGO0010", ACCESS_INTR_MASK }, 277 { "INT33C2", 0 }, 278 { "INT33C3", 0 }, 279 { "INT3432", 0 }, 280 { "INT3433", 0 }, 281 { "INTC10EF", 0 }, 282 {} 283 }; 284 MODULE_DEVICE_TABLE(acpi, dw_i2c_acpi_match); 285 286 static const struct platform_device_id dw_i2c_platform_ids[] = { 287 { "i2c_designware" }, 288 {} 289 }; 290 MODULE_DEVICE_TABLE(platform, dw_i2c_platform_ids); 291 292 static struct platform_driver dw_i2c_driver = { 293 .probe = dw_i2c_plat_probe, 294 .remove = dw_i2c_plat_remove, 295 .driver = { 296 .name = "i2c_designware", 297 .of_match_table = dw_i2c_of_match, 298 .acpi_match_table = dw_i2c_acpi_match, 299 .pm = pm_ptr(&i2c_dw_dev_pm_ops), 300 }, 301 .id_table = dw_i2c_platform_ids, 302 }; 303 304 static int __init dw_i2c_init_driver(void) 305 { 306 return platform_driver_register(&dw_i2c_driver); 307 } 308 subsys_initcall(dw_i2c_init_driver); 309 310 static void __exit dw_i2c_exit_driver(void) 311 { 312 platform_driver_unregister(&dw_i2c_driver); 313 } 314 module_exit(dw_i2c_exit_driver); 315 316 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>"); 317 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter"); 318 MODULE_LICENSE("GPL"); 319 MODULE_IMPORT_NS("I2C_DW"); 320 MODULE_IMPORT_NS("I2C_DW_COMMON"); 321