blob: 3b7d58c2fe8598fd658ccc7aac5c9d5a944900ea [file] [log] [blame]
Andy Shevchenko15c566f2018-08-10 13:26:49 +03001// SPDX-License-Identifier: GPL-2.0-or-later
Dirk Brandewie2373f6b2011-10-29 10:57:23 +01002/*
Luis Oliveira5b6d7212017-06-22 11:17:33 +01003 * Synopsys DesignWare I2C adapter driver.
Dirk Brandewie2373f6b2011-10-29 10:57:23 +01004 *
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.
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010010 */
Luis Oliveirae393f672017-06-14 11:43:21 +010011#include <linux/acpi.h>
12#include <linux/clk-provider.h>
13#include <linux/clk.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010014#include <linux/delay.h>
Mika Westerberg56d4b8a2015-09-24 12:06:54 +030015#include <linux/dmi.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010016#include <linux/err.h>
Luis Oliveirae393f672017-06-14 11:43:21 +010017#include <linux/errno.h>
18#include <linux/i2c.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010019#include <linux/interrupt.h>
Luis Oliveirae393f672017-06-14 11:43:21 +010020#include <linux/io.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
Christian Ruppert9803f862013-06-26 10:55:06 +020023#include <linux/of.h>
Luis Oliveirae393f672017-06-14 11:43:21 +010024#include <linux/platform_data/i2c-designware.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010025#include <linux/platform_device.h>
Deepak Sikri3bf3b282012-02-24 17:01:15 +053026#include <linux/pm.h>
Mika Westerberg72721942013-01-17 12:31:06 +020027#include <linux/pm_runtime.h>
Mika Westerberg4c5301a2015-11-30 17:11:44 +020028#include <linux/property.h>
Zhangfei Gaoab809fd2016-12-27 22:22:40 +080029#include <linux/reset.h>
Luis Oliveirae393f672017-06-14 11:43:21 +010030#include <linux/sched.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010031#include <linux/slab.h>
Rafael J. Wysocki02e45642018-01-03 01:37:34 +010032#include <linux/suspend.h>
Luis Oliveirae393f672017-06-14 11:43:21 +010033
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010034#include "i2c-designware-core.h"
35
Dirk Brandewie1d31b582011-10-06 11:26:30 -070036static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
37{
38 return clk_get_rate(dev->clk)/1000;
39}
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010040
Mika Westerbergb61b1412013-01-17 12:31:07 +020041#ifdef CONFIG_ACPI
Mika Westerberg56d4b8a2015-09-24 12:06:54 +030042/*
43 * The HCNT/LCNT information coming from ACPI should be the most accurate
44 * for given platform. However, some systems get it wrong. On such systems
45 * we get better results by calculating those based on the input clock.
46 */
47static const struct dmi_system_id dw_i2c_no_acpi_params[] = {
48 {
49 .ident = "Dell Inspiron 7348",
50 .matches = {
51 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
52 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7348"),
53 },
54 },
55 { }
56};
57
Mika Westerberg57cd1e32013-08-19 15:07:54 +030058static void dw_i2c_acpi_params(struct platform_device *pdev, char method[],
59 u16 *hcnt, u16 *lcnt, u32 *sda_hold)
60{
61 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
62 acpi_handle handle = ACPI_HANDLE(&pdev->dev);
63 union acpi_object *obj;
64
Mika Westerberg56d4b8a2015-09-24 12:06:54 +030065 if (dmi_check_system(dw_i2c_no_acpi_params))
66 return;
67
Mika Westerberg57cd1e32013-08-19 15:07:54 +030068 if (ACPI_FAILURE(acpi_evaluate_object(handle, method, NULL, &buf)))
69 return;
70
71 obj = (union acpi_object *)buf.pointer;
72 if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 3) {
73 const union acpi_object *objs = obj->package.elements;
74
75 *hcnt = (u16)objs[0].integer.value;
76 *lcnt = (u16)objs[1].integer.value;
chin.yew.tan@intel.combd698d22017-03-28 16:48:02 +080077 *sda_hold = (u32)objs[2].integer.value;
Mika Westerberg57cd1e32013-08-19 15:07:54 +030078 }
79
80 kfree(buf.pointer);
81}
82
Mika Westerbergb61b1412013-01-17 12:31:07 +020083static int dw_i2c_acpi_configure(struct platform_device *pdev)
84{
85 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +030086 struct i2c_timings *t = &dev->timings;
Jan Kiszkaad258fb2017-05-22 07:46:55 +020087 u32 ss_ht = 0, fp_ht = 0, hs_ht = 0, fs_ht = 0;
Mika Westerbergb61b1412013-01-17 12:31:07 +020088
Mika Westerbergb61b1412013-01-17 12:31:07 +020089 dev->tx_fifo_depth = 32;
90 dev->rx_fifo_depth = 32;
Mika Westerberg57cd1e32013-08-19 15:07:54 +030091
92 /*
chin.yew.tan@intel.combd698d22017-03-28 16:48:02 +080093 * Try to get SDA hold time and *CNT values from an ACPI method for
94 * selected speed modes.
Mika Westerberg57cd1e32013-08-19 15:07:54 +030095 */
Ard Biesheuvel9d640842017-05-19 09:56:40 +010096 dw_i2c_acpi_params(pdev, "SSCN", &dev->ss_hcnt, &dev->ss_lcnt, &ss_ht);
97 dw_i2c_acpi_params(pdev, "FPCN", &dev->fp_hcnt, &dev->fp_lcnt, &fp_ht);
98 dw_i2c_acpi_params(pdev, "HSCN", &dev->hs_hcnt, &dev->hs_lcnt, &hs_ht);
99 dw_i2c_acpi_params(pdev, "FMCN", &dev->fs_hcnt, &dev->fs_lcnt, &fs_ht);
100
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300101 switch (t->bus_freq_hz) {
chin.yew.tan@intel.combd698d22017-03-28 16:48:02 +0800102 case 100000:
Ard Biesheuvel9d640842017-05-19 09:56:40 +0100103 dev->sda_hold_time = ss_ht;
chin.yew.tan@intel.combd698d22017-03-28 16:48:02 +0800104 break;
105 case 1000000:
Ard Biesheuvel9d640842017-05-19 09:56:40 +0100106 dev->sda_hold_time = fp_ht;
chin.yew.tan@intel.combd698d22017-03-28 16:48:02 +0800107 break;
108 case 3400000:
Ard Biesheuvel9d640842017-05-19 09:56:40 +0100109 dev->sda_hold_time = hs_ht;
chin.yew.tan@intel.combd698d22017-03-28 16:48:02 +0800110 break;
111 case 400000:
112 default:
Ard Biesheuvel9d640842017-05-19 09:56:40 +0100113 dev->sda_hold_time = fs_ht;
chin.yew.tan@intel.combd698d22017-03-28 16:48:02 +0800114 break;
115 }
Mika Westerberg57cd1e32013-08-19 15:07:54 +0300116
Mika Westerbergb61b1412013-01-17 12:31:07 +0200117 return 0;
118}
119
120static const struct acpi_device_id dw_i2c_acpi_match[] = {
121 { "INT33C2", 0 },
122 { "INT33C3", 0 },
Mika Westerberg25b3dfc2013-11-12 11:57:30 +0200123 { "INT3432", 0 },
124 { "INT3433", 0 },
Hans de Goedeb30f2f62018-10-06 10:25:39 +0200125 { "80860F41", ACCESS_NO_IRQ_SUSPEND },
126 { "808622C1", ACCESS_NO_IRQ_SUSPEND | MODEL_CHERRYTRAIL },
Xiangliang Yu2d244c82015-12-11 20:02:53 +0800127 { "AMD0010", ACCESS_INTR_MASK },
Xiangliang Yue4e666b2016-03-10 19:34:52 +0800128 { "AMDI0010", ACCESS_INTR_MASK },
Suravee Suthikulpanit90708ce2015-12-15 15:55:53 -0600129 { "AMDI0510", 0 },
Loc Ho04a407f2015-12-10 14:19:17 -0700130 { "APMC0D0F", 0 },
Hanjun Guo58dd8ab2017-04-22 11:23:44 +0800131 { "HISI02A1", 0 },
132 { "HISI02A2", 0 },
Hanjun Guodec0a812020-02-03 09:36:07 +0800133 { "HISI02A3", 0 },
Mika Westerbergb61b1412013-01-17 12:31:07 +0200134 { }
135};
136MODULE_DEVICE_TABLE(acpi, dw_i2c_acpi_match);
137#else
138static inline int dw_i2c_acpi_configure(struct platform_device *pdev)
139{
140 return -ENODEV;
141}
142#endif
143
Alexandre Belloni96742772018-08-31 17:11:09 +0200144#ifdef CONFIG_OF
Alexandre Belloni1bb39952018-08-31 17:11:12 +0200145#define MSCC_ICPU_CFG_TWI_DELAY 0x0
146#define MSCC_ICPU_CFG_TWI_DELAY_ENABLE BIT(0)
147#define MSCC_ICPU_CFG_TWI_SPIKE_FILTER 0x4
148
149static int mscc_twi_set_sda_hold_time(struct dw_i2c_dev *dev)
150{
151 writel((dev->sda_hold_time << 1) | MSCC_ICPU_CFG_TWI_DELAY_ENABLE,
152 dev->ext + MSCC_ICPU_CFG_TWI_DELAY);
153
154 return 0;
155}
156
157static int dw_i2c_of_configure(struct platform_device *pdev)
158{
159 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
160 struct resource *mem;
161
162 switch (dev->flags & MODEL_MASK) {
163 case MODEL_MSCC_OCELOT:
164 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
165 dev->ext = devm_ioremap_resource(&pdev->dev, mem);
166 if (!IS_ERR(dev->ext))
167 dev->set_sda_hold_time = mscc_twi_set_sda_hold_time;
168 break;
169 default:
170 break;
171 }
172
173 return 0;
174}
175
Alexandre Belloni96742772018-08-31 17:11:09 +0200176static const struct of_device_id dw_i2c_of_match[] = {
177 { .compatible = "snps,designware-i2c", },
Alexandre Belloni1bb39952018-08-31 17:11:12 +0200178 { .compatible = "mscc,ocelot-i2c", .data = (void *)MODEL_MSCC_OCELOT },
Alexandre Belloni96742772018-08-31 17:11:09 +0200179 {},
180};
181MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
Alexandre Belloni1bb39952018-08-31 17:11:12 +0200182#else
183static inline int dw_i2c_of_configure(struct platform_device *pdev)
184{
185 return -ENODEV;
186}
Alexandre Belloni96742772018-08-31 17:11:09 +0200187#endif
188
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100189static void i2c_dw_configure_master(struct dw_i2c_dev *dev)
190{
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300191 struct i2c_timings *t = &dev->timings;
192
Luis Oliveira5b6d7212017-06-22 11:17:33 +0100193 dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY;
194
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100195 dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
196 DW_IC_CON_RESTART_EN;
197
Luis Oliveira5b6d7212017-06-22 11:17:33 +0100198 dev->mode = DW_IC_MASTER;
199
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300200 switch (t->bus_freq_hz) {
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100201 case 100000:
202 dev->master_cfg |= DW_IC_CON_SPEED_STD;
203 break;
204 case 3400000:
205 dev->master_cfg |= DW_IC_CON_SPEED_HIGH;
206 break;
207 default:
208 dev->master_cfg |= DW_IC_CON_SPEED_FAST;
209 }
210}
211
Luis Oliveira5b6d7212017-06-22 11:17:33 +0100212static void i2c_dw_configure_slave(struct dw_i2c_dev *dev)
213{
214 dev->functionality = I2C_FUNC_SLAVE | DW_IC_DEFAULT_FUNCTIONALITY;
215
216 dev->slave_cfg = DW_IC_CON_RX_FIFO_FULL_HLD_CTRL |
Jarkko Nikula4e2d93d2017-08-09 15:24:44 +0300217 DW_IC_CON_RESTART_EN | DW_IC_CON_STOP_DET_IFADDRESSED;
Luis Oliveira5b6d7212017-06-22 11:17:33 +0100218
219 dev->mode = DW_IC_SLAVE;
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600220}
221
Hans de Goede77f33812019-03-12 15:55:53 +0100222static void dw_i2c_set_fifo_size(struct dw_i2c_dev *dev)
Tin Huynh8e598762016-12-14 16:23:58 +0700223{
224 u32 param, tx_fifo_depth, rx_fifo_depth;
225
226 /*
227 * Try to detect the FIFO depth if not set by interface driver,
228 * the depth could be from 2 to 256 from HW spec.
229 */
230 param = i2c_dw_read_comp_param(dev);
231 tx_fifo_depth = ((param >> 16) & 0xff) + 1;
232 rx_fifo_depth = ((param >> 8) & 0xff) + 1;
233 if (!dev->tx_fifo_depth) {
234 dev->tx_fifo_depth = tx_fifo_depth;
235 dev->rx_fifo_depth = rx_fifo_depth;
Tin Huynh8e598762016-12-14 16:23:58 +0700236 } else if (tx_fifo_depth >= 2) {
237 dev->tx_fifo_depth = min_t(u32, dev->tx_fifo_depth,
238 tx_fifo_depth);
239 dev->rx_fifo_depth = min_t(u32, dev->rx_fifo_depth,
240 rx_fifo_depth);
241 }
242}
243
Rafael J. Wysocki126dbc62017-09-25 23:10:06 +0200244static void dw_i2c_plat_pm_cleanup(struct dw_i2c_dev *dev)
245{
246 pm_runtime_disable(dev->dev);
247
Hans de Goede9cbeeca2018-09-05 21:51:31 +0200248 if (dev->shared_with_punit)
Rafael J. Wysocki126dbc62017-09-25 23:10:06 +0200249 pm_runtime_put_noidle(dev->dev);
250}
251
Jarkko Nikula6ad6fde2015-08-31 17:31:32 +0300252static int dw_i2c_plat_probe(struct platform_device *pdev)
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100253{
Mika Westerberg4c5301a2015-11-30 17:11:44 +0200254 struct dw_i2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100255 struct i2c_adapter *adap;
Luis Oliveirae393f672017-06-14 11:43:21 +0100256 struct dw_i2c_dev *dev;
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300257 struct i2c_timings *t;
258 u32 acpi_speed;
Luis Oliveirae393f672017-06-14 11:43:21 +0100259 struct resource *mem;
Hans de Goede231d0692017-08-29 14:08:35 +0200260 int i, irq, ret;
Colin Ian King4ce8e882017-09-21 23:30:07 +0100261 static const int supported_speeds[] = {
262 0, 100000, 400000, 1000000, 3400000
263 };
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100264
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100265 irq = platform_get_irq(pdev, 0);
Alexey Brodkinb20d3862015-03-09 12:03:12 +0300266 if (irq < 0)
267 return irq;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100268
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000269 dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
270 if (!dev)
271 return -ENOMEM;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100272
Wolfram Sang3cc2d002013-05-10 10:16:54 +0200273 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000274 dev->base = devm_ioremap_resource(&pdev->dev, mem);
275 if (IS_ERR(dev->base))
276 return PTR_ERR(dev->base);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100277
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000278 dev->dev = &pdev->dev;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100279 dev->irq = irq;
280 platform_set_drvdata(pdev, dev);
281
Zhangfei Gaoab809fd2016-12-27 22:22:40 +0800282 dev->rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
Andy Shevchenkoa6af48e2019-08-19 13:31:30 +0300283 if (IS_ERR(dev->rst))
284 return PTR_ERR(dev->rst);
285
286 reset_control_deassert(dev->rst);
Zhangfei Gaoab809fd2016-12-27 22:22:40 +0800287
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300288 t = &dev->timings;
289 if (pdata)
290 t->bus_freq_hz = pdata->i2c_scl_freq;
291 else
292 i2c_parse_fw_timings(&pdev->dev, t, false);
Mika Westerberg4c5301a2015-11-30 17:11:44 +0200293
Jarkko Nikula10f8e7f2016-08-12 17:02:54 +0300294 acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev);
Hans de Goede231d0692017-08-29 14:08:35 +0200295 /*
296 * Some DSTDs use a non standard speed, round down to the lowest
297 * standard speed.
298 */
299 for (i = 1; i < ARRAY_SIZE(supported_speeds); i++) {
300 if (acpi_speed < supported_speeds[i])
301 break;
302 }
303 acpi_speed = supported_speeds[i - 1];
304
Jarkko Nikula973652d2016-11-10 13:37:20 +0200305 /*
306 * Find bus speed from the "clock-frequency" device property, ACPI
307 * or by using fast mode if neither is set.
308 */
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300309 if (acpi_speed && t->bus_freq_hz)
310 t->bus_freq_hz = min(t->bus_freq_hz, acpi_speed);
311 else if (acpi_speed || t->bus_freq_hz)
312 t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed);
Jarkko Nikula973652d2016-11-10 13:37:20 +0200313 else
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300314 t->bus_freq_hz = 400000;
Jarkko Nikula10f8e7f2016-08-12 17:02:54 +0300315
Alexandre Belloni1732c222018-08-31 17:11:08 +0200316 dev->flags |= (uintptr_t)device_get_match_data(&pdev->dev);
317
Alexandre Belloni1bb39952018-08-31 17:11:12 +0200318 if (pdev->dev.of_node)
319 dw_i2c_of_configure(pdev);
320
Mika Westerberg4c5301a2015-11-30 17:11:44 +0200321 if (has_acpi_companion(&pdev->dev))
322 dw_i2c_acpi_configure(pdev);
323
324 /*
Weifeng Voond608c3d2016-08-12 17:02:49 +0300325 * Only standard mode at 100kHz, fast mode at 400kHz,
Weifeng Voonb6e67142016-08-12 17:02:51 +0300326 * fast mode plus at 1MHz and high speed mode at 3.4MHz are supported.
Mika Westerberg4c5301a2015-11-30 17:11:44 +0200327 */
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300328 if (t->bus_freq_hz != 100000 && t->bus_freq_hz != 400000 &&
329 t->bus_freq_hz != 1000000 && t->bus_freq_hz != 3400000) {
Weifeng Voond608c3d2016-08-12 17:02:49 +0300330 dev_err(&pdev->dev,
Hans de Goede22acc372017-07-13 15:45:01 +0200331 "%d Hz is unsupported, only 100kHz, 400kHz, 1MHz and 3.4MHz are supported\n",
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300332 t->bus_freq_hz);
Luis Oliveirae393f672017-06-14 11:43:21 +0100333 ret = -EINVAL;
Zhangfei Gaoab809fd2016-12-27 22:22:40 +0800334 goto exit_reset;
Christian Ruppert9803f862013-06-26 10:55:06 +0200335 }
336
Luis Oliveirae393f672017-06-14 11:43:21 +0100337 ret = i2c_dw_probe_lock_support(dev);
338 if (ret)
Zhangfei Gaoab809fd2016-12-27 22:22:40 +0800339 goto exit_reset;
David Box894acb22015-01-15 01:12:17 -0800340
Luis Oliveira5b6d7212017-06-22 11:17:33 +0100341 if (i2c_detect_slave_mode(&pdev->dev))
342 i2c_dw_configure_slave(dev);
343 else
344 i2c_dw_configure_master(dev);
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700345
Phil Edworthyc62ebb32019-02-28 13:52:10 +0000346 /* Optional interface clock */
347 dev->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
Andy Shevchenko71dc2972019-08-19 13:24:23 +0300348 if (IS_ERR(dev->pclk)) {
349 ret = PTR_ERR(dev->pclk);
350 goto exit_reset;
351 }
Phil Edworthyc62ebb32019-02-28 13:52:10 +0000352
Mika Westerberg925ddb22014-09-30 13:04:54 +0300353 dev->clk = devm_clk_get(&pdev->dev, NULL);
Phil Reid0326f9f2017-11-02 10:40:26 +0800354 if (!i2c_dw_prepare_clk(dev, true)) {
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300355 u64 clk_khz;
Mika Westerberg925ddb22014-09-30 13:04:54 +0300356
Andy Shevchenkoe3ea52b2018-07-25 17:39:26 +0300357 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
358 clk_khz = dev->get_clk_rate_khz(dev);
359
360 if (!dev->sda_hold_time && t->sda_hold_ns)
361 dev->sda_hold_time =
362 div_u64(clk_khz * t->sda_hold_ns + 500000, 1000000);
Mika Westerberg925ddb22014-09-30 13:04:54 +0300363 }
364
Hans de Goede77f33812019-03-12 15:55:53 +0100365 dw_i2c_set_fifo_size(dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100366
367 adap = &dev->adapter;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100368 adap->owner = THIS_MODULE;
Wolfram Sang70fba832014-07-10 13:46:26 +0200369 adap->class = I2C_CLASS_DEPRECATED;
Dustin Byford8eb5c872015-10-23 12:27:07 -0700370 ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
Rob Herringaf711002011-11-08 14:43:47 -0600371 adap->dev.of_node = pdev->dev.of_node;
Hans de Goedecd86d142019-03-12 15:55:54 +0100372 adap->nr = -1;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100373
Rafael J. Wysocki02e45642018-01-03 01:37:34 +0100374 dev_pm_set_driver_flags(&pdev->dev,
375 DPM_FLAG_SMART_PREPARE |
376 DPM_FLAG_SMART_SUSPEND |
377 DPM_FLAG_LEAVE_SUSPENDED);
Rafael J. Wysocki422cb782018-01-03 01:35:54 +0100378
Rafael J. Wysocki126dbc62017-09-25 23:10:06 +0200379 /* The code below assumes runtime PM to be disabled. */
380 WARN_ON(pm_runtime_enabled(&pdev->dev));
381
382 pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
383 pm_runtime_use_autosuspend(&pdev->dev);
384 pm_runtime_set_active(&pdev->dev);
385
Hans de Goede9cbeeca2018-09-05 21:51:31 +0200386 if (dev->shared_with_punit)
Rafael J. Wysocki126dbc62017-09-25 23:10:06 +0200387 pm_runtime_get_noresume(&pdev->dev);
388
389 pm_runtime_enable(&pdev->dev);
Mika Westerberg72721942013-01-17 12:31:06 +0200390
Luis Oliveira5b6d7212017-06-22 11:17:33 +0100391 if (dev->mode == DW_IC_SLAVE)
392 ret = i2c_dw_probe_slave(dev);
393 else
394 ret = i2c_dw_probe(dev);
395
Luis Oliveirae393f672017-06-14 11:43:21 +0100396 if (ret)
Zhangfei Gaoab809fd2016-12-27 22:22:40 +0800397 goto exit_probe;
Wolfram Sang36d48fb2015-10-09 10:39:24 +0100398
Luis Oliveirae393f672017-06-14 11:43:21 +0100399 return ret;
Zhangfei Gaoab809fd2016-12-27 22:22:40 +0800400
401exit_probe:
Rafael J. Wysocki126dbc62017-09-25 23:10:06 +0200402 dw_i2c_plat_pm_cleanup(dev);
Zhangfei Gaoab809fd2016-12-27 22:22:40 +0800403exit_reset:
Andy Shevchenkoa6af48e2019-08-19 13:31:30 +0300404 reset_control_assert(dev->rst);
Luis Oliveirae393f672017-06-14 11:43:21 +0100405 return ret;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100406}
407
Jarkko Nikula6ad6fde2015-08-31 17:31:32 +0300408static int dw_i2c_plat_remove(struct platform_device *pdev)
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100409{
410 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100411
Mika Westerberg72721942013-01-17 12:31:06 +0200412 pm_runtime_get_sync(&pdev->dev);
413
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100414 i2c_del_adapter(&dev->adapter);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100415
Luis Oliveira90312352017-06-14 11:43:23 +0100416 dev->disable(dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100417
Mika Westerbergedfc3902015-06-17 12:08:38 +0300418 pm_runtime_dont_use_autosuspend(&pdev->dev);
419 pm_runtime_put_sync(&pdev->dev);
Rafael J. Wysocki126dbc62017-09-25 23:10:06 +0200420 dw_i2c_plat_pm_cleanup(dev);
421
Andy Shevchenkoa6af48e2019-08-19 13:31:30 +0300422 reset_control_assert(dev->rst);
Mika Westerberg72721942013-01-17 12:31:06 +0200423
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100424 return 0;
425}
426
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800427#ifdef CONFIG_PM_SLEEP
Jarkko Nikula6ad6fde2015-08-31 17:31:32 +0300428static int dw_i2c_plat_prepare(struct device *dev)
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800429{
Rafael J. Wysocki422cb782018-01-03 01:35:54 +0100430 /*
431 * If the ACPI companion device object is present for this device, it
432 * may be accessed during suspend and resume of other devices via I2C
433 * operation regions, so tell the PM core and middle layers to avoid
434 * skipping system suspend/resume callbacks for it in that case.
435 */
436 return !has_acpi_companion(dev);
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800437}
438
Jarkko Nikula6ad6fde2015-08-31 17:31:32 +0300439static void dw_i2c_plat_complete(struct device *dev)
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800440{
Rafael J. Wysocki02e45642018-01-03 01:37:34 +0100441 /*
442 * The device can only be in runtime suspend at this point if it has not
443 * been resumed throughout the ending system suspend/resume cycle, so if
444 * the platform firmware might mess up with it, request the runtime PM
445 * framework to resume it.
446 */
447 if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800448 pm_request_resume(dev);
449}
450#else
Jarkko Nikula319d7f02015-10-21 10:09:17 +0300451#define dw_i2c_plat_prepare NULL
452#define dw_i2c_plat_complete NULL
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800453#endif
454
Mika Westerberg1fc2fe22014-05-15 17:37:23 +0300455#ifdef CONFIG_PM
Rafael J. Wysocki54152772017-09-25 01:30:51 +0200456static int dw_i2c_plat_suspend(struct device *dev)
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530457{
Masahiro Yamada9242e722017-07-28 01:16:24 +0900458 struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530459
Hans de Goede27515412019-02-22 14:08:40 +0100460 i_dev->suspended = true;
461
Hans de Goede9cbeeca2018-09-05 21:51:31 +0200462 if (i_dev->shared_with_punit)
Hans de Goede9d9a1522018-08-29 15:06:31 +0200463 return 0;
464
Luis Oliveira90312352017-06-14 11:43:23 +0100465 i_dev->disable(i_dev);
Phil Reid0326f9f2017-11-02 10:40:26 +0800466 i2c_dw_prepare_clk(i_dev, false);
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530467
468 return 0;
469}
470
Jarkko Nikula6ad6fde2015-08-31 17:31:32 +0300471static int dw_i2c_plat_resume(struct device *dev)
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530472{
Masahiro Yamada9242e722017-07-28 01:16:24 +0900473 struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530474
Hans de Goede9cbeeca2018-09-05 21:51:31 +0200475 if (!i_dev->shared_with_punit)
Hans de Goede9d9a1522018-08-29 15:06:31 +0200476 i2c_dw_prepare_clk(i_dev, true);
477
Luis Oliveira90312352017-06-14 11:43:23 +0100478 i_dev->init(i_dev);
Hans de Goede27515412019-02-22 14:08:40 +0100479 i_dev->suspended = false;
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530480
481 return 0;
482}
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530483
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800484static const struct dev_pm_ops dw_i2c_dev_pm_ops = {
Jarkko Nikula6ad6fde2015-08-31 17:31:32 +0300485 .prepare = dw_i2c_plat_prepare,
486 .complete = dw_i2c_plat_complete,
Rafael J. Wysocki54152772017-09-25 01:30:51 +0200487 SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume)
488 SET_RUNTIME_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume, NULL)
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800489};
490
491#define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
492#else
493#define DW_I2C_DEV_PMOPS NULL
494#endif
Mika Westerberg1fc2fe22014-05-15 17:37:23 +0300495
Luis Oliveirae393f672017-06-14 11:43:21 +0100496/* Work with hotplug and coldplug */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100497MODULE_ALIAS("platform:i2c_designware");
498
499static struct platform_driver dw_i2c_driver = {
Jarkko Nikula6ad6fde2015-08-31 17:31:32 +0300500 .probe = dw_i2c_plat_probe,
501 .remove = dw_i2c_plat_remove,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100502 .driver = {
503 .name = "i2c_designware",
Rob Herringaf711002011-11-08 14:43:47 -0600504 .of_match_table = of_match_ptr(dw_i2c_of_match),
Mika Westerbergb61b1412013-01-17 12:31:07 +0200505 .acpi_match_table = ACPI_PTR(dw_i2c_acpi_match),
Jisheng Zhang8503ff12015-05-20 22:33:13 +0800506 .pm = DW_I2C_DEV_PMOPS,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100507 },
508};
509
510static int __init dw_i2c_init_driver(void)
511{
Wolfram Sangcccdcea2013-10-08 22:35:33 +0200512 return platform_driver_register(&dw_i2c_driver);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100513}
Pratyush Anand10452282012-02-29 12:27:46 +0530514subsys_initcall(dw_i2c_init_driver);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100515
516static void __exit dw_i2c_exit_driver(void)
517{
518 platform_driver_unregister(&dw_i2c_driver);
519}
520module_exit(dw_i2c_exit_driver);
521
522MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
523MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
524MODULE_LICENSE("GPL");