2 * Synopsys DesignWare I2C adapter driver.
4 * Based on the TI DAVINCI I2C adapter driver.
6 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd.
10 * ----------------------------------------------------------------------------
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 * ----------------------------------------------------------------------------
24 #include <linux/acpi.h>
25 #include <linux/clk-provider.h>
26 #include <linux/clk.h>
27 #include <linux/delay.h>
28 #include <linux/dmi.h>
29 #include <linux/err.h>
30 #include <linux/errno.h>
31 #include <linux/i2c.h>
32 #include <linux/interrupt.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
37 #include <linux/platform_data/i2c-designware.h>
38 #include <linux/platform_device.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/property.h>
42 #include <linux/reset.h>
43 #include <linux/sched.h>
44 #include <linux/slab.h>
45 #include <linux/suspend.h>
47 #include "i2c-designware-core.h"
49 static u32
i2c_dw_get_clk_rate_khz(struct dw_i2c_dev
*dev
)
51 return clk_get_rate(dev
->clk
)/1000;
56 * The HCNT/LCNT information coming from ACPI should be the most accurate
57 * for given platform. However, some systems get it wrong. On such systems
58 * we get better results by calculating those based on the input clock.
60 static const struct dmi_system_id dw_i2c_no_acpi_params
[] = {
62 .ident
= "Dell Inspiron 7348",
64 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
65 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron 7348"),
71 static void dw_i2c_acpi_params(struct platform_device
*pdev
, char method
[],
72 u16
*hcnt
, u16
*lcnt
, u32
*sda_hold
)
74 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
75 acpi_handle handle
= ACPI_HANDLE(&pdev
->dev
);
76 union acpi_object
*obj
;
78 if (dmi_check_system(dw_i2c_no_acpi_params
))
81 if (ACPI_FAILURE(acpi_evaluate_object(handle
, method
, NULL
, &buf
)))
84 obj
= (union acpi_object
*)buf
.pointer
;
85 if (obj
->type
== ACPI_TYPE_PACKAGE
&& obj
->package
.count
== 3) {
86 const union acpi_object
*objs
= obj
->package
.elements
;
88 *hcnt
= (u16
)objs
[0].integer
.value
;
89 *lcnt
= (u16
)objs
[1].integer
.value
;
90 *sda_hold
= (u32
)objs
[2].integer
.value
;
96 static int dw_i2c_acpi_configure(struct platform_device
*pdev
)
98 struct dw_i2c_dev
*dev
= platform_get_drvdata(pdev
);
99 u32 ss_ht
= 0, fp_ht
= 0, hs_ht
= 0, fs_ht
= 0;
100 acpi_handle handle
= ACPI_HANDLE(&pdev
->dev
);
101 const struct acpi_device_id
*id
;
102 struct acpi_device
*adev
;
105 dev
->adapter
.nr
= -1;
106 dev
->tx_fifo_depth
= 32;
107 dev
->rx_fifo_depth
= 32;
110 * Try to get SDA hold time and *CNT values from an ACPI method for
111 * selected speed modes.
113 dw_i2c_acpi_params(pdev
, "SSCN", &dev
->ss_hcnt
, &dev
->ss_lcnt
, &ss_ht
);
114 dw_i2c_acpi_params(pdev
, "FPCN", &dev
->fp_hcnt
, &dev
->fp_lcnt
, &fp_ht
);
115 dw_i2c_acpi_params(pdev
, "HSCN", &dev
->hs_hcnt
, &dev
->hs_lcnt
, &hs_ht
);
116 dw_i2c_acpi_params(pdev
, "FMCN", &dev
->fs_hcnt
, &dev
->fs_lcnt
, &fs_ht
);
118 switch (dev
->clk_freq
) {
120 dev
->sda_hold_time
= ss_ht
;
123 dev
->sda_hold_time
= fp_ht
;
126 dev
->sda_hold_time
= hs_ht
;
130 dev
->sda_hold_time
= fs_ht
;
134 id
= acpi_match_device(pdev
->dev
.driver
->acpi_match_table
, &pdev
->dev
);
135 if (id
&& id
->driver_data
)
136 dev
->flags
|= (u32
)id
->driver_data
;
138 if (acpi_bus_get_device(handle
, &adev
))
142 * Cherrytrail I2C7 gets used for the PMIC which gets accessed
143 * through ACPI opregions during late suspend / early resume
146 uid
= adev
->pnp
.unique_id
;
147 if ((dev
->flags
& MODEL_CHERRYTRAIL
) && !strcmp(uid
, "7"))
148 dev
->pm_disabled
= true;
153 static const struct acpi_device_id dw_i2c_acpi_match
[] = {
159 { "808622C1", MODEL_CHERRYTRAIL
},
160 { "AMD0010", ACCESS_INTR_MASK
},
161 { "AMDI0010", ACCESS_INTR_MASK
},
168 MODULE_DEVICE_TABLE(acpi
, dw_i2c_acpi_match
);
170 static inline int dw_i2c_acpi_configure(struct platform_device
*pdev
)
176 static void i2c_dw_configure_master(struct dw_i2c_dev
*dev
)
178 dev
->functionality
= I2C_FUNC_10BIT_ADDR
| DW_IC_DEFAULT_FUNCTIONALITY
;
180 dev
->master_cfg
= DW_IC_CON_MASTER
| DW_IC_CON_SLAVE_DISABLE
|
181 DW_IC_CON_RESTART_EN
;
183 dev
->mode
= DW_IC_MASTER
;
185 switch (dev
->clk_freq
) {
187 dev
->master_cfg
|= DW_IC_CON_SPEED_STD
;
190 dev
->master_cfg
|= DW_IC_CON_SPEED_HIGH
;
193 dev
->master_cfg
|= DW_IC_CON_SPEED_FAST
;
197 static void i2c_dw_configure_slave(struct dw_i2c_dev
*dev
)
199 dev
->functionality
= I2C_FUNC_SLAVE
| DW_IC_DEFAULT_FUNCTIONALITY
;
201 dev
->slave_cfg
= DW_IC_CON_RX_FIFO_FULL_HLD_CTRL
|
202 DW_IC_CON_RESTART_EN
| DW_IC_CON_STOP_DET_IFADDRESSED
;
204 dev
->mode
= DW_IC_SLAVE
;
207 static void dw_i2c_set_fifo_size(struct dw_i2c_dev
*dev
, int id
)
209 u32 param
, tx_fifo_depth
, rx_fifo_depth
;
212 * Try to detect the FIFO depth if not set by interface driver,
213 * the depth could be from 2 to 256 from HW spec.
215 param
= i2c_dw_read_comp_param(dev
);
216 tx_fifo_depth
= ((param
>> 16) & 0xff) + 1;
217 rx_fifo_depth
= ((param
>> 8) & 0xff) + 1;
218 if (!dev
->tx_fifo_depth
) {
219 dev
->tx_fifo_depth
= tx_fifo_depth
;
220 dev
->rx_fifo_depth
= rx_fifo_depth
;
221 dev
->adapter
.nr
= id
;
222 } else if (tx_fifo_depth
>= 2) {
223 dev
->tx_fifo_depth
= min_t(u32
, dev
->tx_fifo_depth
,
225 dev
->rx_fifo_depth
= min_t(u32
, dev
->rx_fifo_depth
,
230 static void dw_i2c_plat_pm_cleanup(struct dw_i2c_dev
*dev
)
232 pm_runtime_disable(dev
->dev
);
234 if (dev
->pm_disabled
)
235 pm_runtime_put_noidle(dev
->dev
);
238 static int dw_i2c_plat_probe(struct platform_device
*pdev
)
240 struct dw_i2c_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
241 struct i2c_adapter
*adap
;
242 struct dw_i2c_dev
*dev
;
243 u32 acpi_speed
, ht
= 0;
244 struct resource
*mem
;
246 static const int supported_speeds
[] = {
247 0, 100000, 400000, 1000000, 3400000
250 irq
= platform_get_irq(pdev
, 0);
254 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct dw_i2c_dev
), GFP_KERNEL
);
258 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
259 dev
->base
= devm_ioremap_resource(&pdev
->dev
, mem
);
260 if (IS_ERR(dev
->base
))
261 return PTR_ERR(dev
->base
);
263 dev
->dev
= &pdev
->dev
;
265 platform_set_drvdata(pdev
, dev
);
267 dev
->rst
= devm_reset_control_get_optional_exclusive(&pdev
->dev
, NULL
);
268 if (IS_ERR(dev
->rst
)) {
269 if (PTR_ERR(dev
->rst
) == -EPROBE_DEFER
)
270 return -EPROBE_DEFER
;
272 reset_control_deassert(dev
->rst
);
276 dev
->clk_freq
= pdata
->i2c_scl_freq
;
278 device_property_read_u32(&pdev
->dev
, "i2c-sda-hold-time-ns",
280 device_property_read_u32(&pdev
->dev
, "i2c-sda-falling-time-ns",
281 &dev
->sda_falling_time
);
282 device_property_read_u32(&pdev
->dev
, "i2c-scl-falling-time-ns",
283 &dev
->scl_falling_time
);
284 device_property_read_u32(&pdev
->dev
, "clock-frequency",
288 acpi_speed
= i2c_acpi_find_bus_speed(&pdev
->dev
);
290 * Some DSTDs use a non standard speed, round down to the lowest
293 for (i
= 1; i
< ARRAY_SIZE(supported_speeds
); i
++) {
294 if (acpi_speed
< supported_speeds
[i
])
297 acpi_speed
= supported_speeds
[i
- 1];
300 * Find bus speed from the "clock-frequency" device property, ACPI
301 * or by using fast mode if neither is set.
303 if (acpi_speed
&& dev
->clk_freq
)
304 dev
->clk_freq
= min(dev
->clk_freq
, acpi_speed
);
305 else if (acpi_speed
|| dev
->clk_freq
)
306 dev
->clk_freq
= max(dev
->clk_freq
, acpi_speed
);
308 dev
->clk_freq
= 400000;
310 if (has_acpi_companion(&pdev
->dev
))
311 dw_i2c_acpi_configure(pdev
);
314 * Only standard mode at 100kHz, fast mode at 400kHz,
315 * fast mode plus at 1MHz and high speed mode at 3.4MHz are supported.
317 if (dev
->clk_freq
!= 100000 && dev
->clk_freq
!= 400000
318 && dev
->clk_freq
!= 1000000 && dev
->clk_freq
!= 3400000) {
320 "%d Hz is unsupported, only 100kHz, 400kHz, 1MHz and 3.4MHz are supported\n",
326 ret
= i2c_dw_probe_lock_support(dev
);
330 if (i2c_detect_slave_mode(&pdev
->dev
))
331 i2c_dw_configure_slave(dev
);
333 i2c_dw_configure_master(dev
);
335 dev
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
336 if (!i2c_dw_prepare_clk(dev
, true)) {
337 dev
->get_clk_rate_khz
= i2c_dw_get_clk_rate_khz
;
339 if (!dev
->sda_hold_time
&& ht
)
340 dev
->sda_hold_time
= div_u64(
341 (u64
)dev
->get_clk_rate_khz(dev
) * ht
+ 500000,
345 dw_i2c_set_fifo_size(dev
, pdev
->id
);
347 adap
= &dev
->adapter
;
348 adap
->owner
= THIS_MODULE
;
349 adap
->class = I2C_CLASS_DEPRECATED
;
350 ACPI_COMPANION_SET(&adap
->dev
, ACPI_COMPANION(&pdev
->dev
));
351 adap
->dev
.of_node
= pdev
->dev
.of_node
;
353 dev_pm_set_driver_flags(&pdev
->dev
,
354 DPM_FLAG_SMART_PREPARE
|
355 DPM_FLAG_SMART_SUSPEND
|
356 DPM_FLAG_LEAVE_SUSPENDED
);
358 /* The code below assumes runtime PM to be disabled. */
359 WARN_ON(pm_runtime_enabled(&pdev
->dev
));
361 pm_runtime_set_autosuspend_delay(&pdev
->dev
, 1000);
362 pm_runtime_use_autosuspend(&pdev
->dev
);
363 pm_runtime_set_active(&pdev
->dev
);
365 if (dev
->pm_disabled
)
366 pm_runtime_get_noresume(&pdev
->dev
);
368 pm_runtime_enable(&pdev
->dev
);
370 if (dev
->mode
== DW_IC_SLAVE
)
371 ret
= i2c_dw_probe_slave(dev
);
373 ret
= i2c_dw_probe(dev
);
381 dw_i2c_plat_pm_cleanup(dev
);
383 if (!IS_ERR_OR_NULL(dev
->rst
))
384 reset_control_assert(dev
->rst
);
388 static int dw_i2c_plat_remove(struct platform_device
*pdev
)
390 struct dw_i2c_dev
*dev
= platform_get_drvdata(pdev
);
392 pm_runtime_get_sync(&pdev
->dev
);
394 i2c_del_adapter(&dev
->adapter
);
398 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
399 pm_runtime_put_sync(&pdev
->dev
);
400 dw_i2c_plat_pm_cleanup(dev
);
402 if (!IS_ERR_OR_NULL(dev
->rst
))
403 reset_control_assert(dev
->rst
);
405 i2c_dw_remove_lock_support(dev
);
411 static const struct of_device_id dw_i2c_of_match
[] = {
412 { .compatible
= "snps,designware-i2c", },
415 MODULE_DEVICE_TABLE(of
, dw_i2c_of_match
);
418 #ifdef CONFIG_PM_SLEEP
419 static int dw_i2c_plat_prepare(struct device
*dev
)
422 * If the ACPI companion device object is present for this device, it
423 * may be accessed during suspend and resume of other devices via I2C
424 * operation regions, so tell the PM core and middle layers to avoid
425 * skipping system suspend/resume callbacks for it in that case.
427 return !has_acpi_companion(dev
);
430 static void dw_i2c_plat_complete(struct device
*dev
)
433 * The device can only be in runtime suspend at this point if it has not
434 * been resumed throughout the ending system suspend/resume cycle, so if
435 * the platform firmware might mess up with it, request the runtime PM
436 * framework to resume it.
438 if (pm_runtime_suspended(dev
) && pm_resume_via_firmware())
439 pm_request_resume(dev
);
442 #define dw_i2c_plat_prepare NULL
443 #define dw_i2c_plat_complete NULL
447 static int dw_i2c_plat_suspend(struct device
*dev
)
449 struct dw_i2c_dev
*i_dev
= dev_get_drvdata(dev
);
451 i_dev
->disable(i_dev
);
452 i2c_dw_prepare_clk(i_dev
, false);
457 static int dw_i2c_plat_resume(struct device
*dev
)
459 struct dw_i2c_dev
*i_dev
= dev_get_drvdata(dev
);
461 i2c_dw_prepare_clk(i_dev
, true);
467 static const struct dev_pm_ops dw_i2c_dev_pm_ops
= {
468 .prepare
= dw_i2c_plat_prepare
,
469 .complete
= dw_i2c_plat_complete
,
470 SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend
, dw_i2c_plat_resume
)
471 SET_RUNTIME_PM_OPS(dw_i2c_plat_suspend
, dw_i2c_plat_resume
, NULL
)
474 #define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
476 #define DW_I2C_DEV_PMOPS NULL
479 /* Work with hotplug and coldplug */
480 MODULE_ALIAS("platform:i2c_designware");
482 static struct platform_driver dw_i2c_driver
= {
483 .probe
= dw_i2c_plat_probe
,
484 .remove
= dw_i2c_plat_remove
,
486 .name
= "i2c_designware",
487 .of_match_table
= of_match_ptr(dw_i2c_of_match
),
488 .acpi_match_table
= ACPI_PTR(dw_i2c_acpi_match
),
489 .pm
= DW_I2C_DEV_PMOPS
,
493 static int __init
dw_i2c_init_driver(void)
495 return platform_driver_register(&dw_i2c_driver
);
497 subsys_initcall(dw_i2c_init_driver
);
499 static void __exit
dw_i2c_exit_driver(void)
501 platform_driver_unregister(&dw_i2c_driver
);
503 module_exit(dw_i2c_exit_driver
);
505 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
506 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
507 MODULE_LICENSE("GPL");