]>
Commit | Line | Data |
---|---|---|
2373f6b9 DB |
1 | /* |
2 | * Synopsys DesignWare I2C adapter driver (master only). | |
3 | * | |
4 | * Based on the TI DAVINCI I2C adapter driver. | |
5 | * | |
6 | * Copyright (C) 2006 Texas Instruments. | |
7 | * Copyright (C) 2007 MontaVista Software Inc. | |
8 | * Copyright (C) 2009 Provigent Ltd. | |
9 | * | |
10 | * ---------------------------------------------------------------------------- | |
11 | * | |
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. | |
16 | * | |
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. | |
2373f6b9 DB |
21 | * ---------------------------------------------------------------------------- |
22 | * | |
23 | */ | |
24 | #include <linux/kernel.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/delay.h> | |
56d4b8a2 | 27 | #include <linux/dmi.h> |
2373f6b9 DB |
28 | #include <linux/i2c.h> |
29 | #include <linux/clk.h> | |
a445900c | 30 | #include <linux/clk-provider.h> |
2373f6b9 DB |
31 | #include <linux/errno.h> |
32 | #include <linux/sched.h> | |
33 | #include <linux/err.h> | |
34 | #include <linux/interrupt.h> | |
9803f868 | 35 | #include <linux/of.h> |
2373f6b9 | 36 | #include <linux/platform_device.h> |
3bf3b289 | 37 | #include <linux/pm.h> |
7272194e | 38 | #include <linux/pm_runtime.h> |
4c5301ab | 39 | #include <linux/property.h> |
2373f6b9 | 40 | #include <linux/io.h> |
ab809fd8 | 41 | #include <linux/reset.h> |
2373f6b9 | 42 | #include <linux/slab.h> |
b61b1415 | 43 | #include <linux/acpi.h> |
4bcfda09 | 44 | #include <linux/platform_data/i2c-designware.h> |
2373f6b9 DB |
45 | #include "i2c-designware-core.h" |
46 | ||
1d31b58f DB |
47 | static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev) |
48 | { | |
49 | return clk_get_rate(dev->clk)/1000; | |
50 | } | |
2373f6b9 | 51 | |
b61b1415 | 52 | #ifdef CONFIG_ACPI |
56d4b8a2 MW |
53 | /* |
54 | * The HCNT/LCNT information coming from ACPI should be the most accurate | |
55 | * for given platform. However, some systems get it wrong. On such systems | |
56 | * we get better results by calculating those based on the input clock. | |
57 | */ | |
58 | static const struct dmi_system_id dw_i2c_no_acpi_params[] = { | |
59 | { | |
60 | .ident = "Dell Inspiron 7348", | |
61 | .matches = { | |
62 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
63 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7348"), | |
64 | }, | |
65 | }, | |
66 | { } | |
67 | }; | |
68 | ||
57cd1e30 MW |
69 | static void dw_i2c_acpi_params(struct platform_device *pdev, char method[], |
70 | u16 *hcnt, u16 *lcnt, u32 *sda_hold) | |
71 | { | |
72 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; | |
73 | acpi_handle handle = ACPI_HANDLE(&pdev->dev); | |
74 | union acpi_object *obj; | |
75 | ||
56d4b8a2 MW |
76 | if (dmi_check_system(dw_i2c_no_acpi_params)) |
77 | return; | |
78 | ||
57cd1e30 MW |
79 | if (ACPI_FAILURE(acpi_evaluate_object(handle, method, NULL, &buf))) |
80 | return; | |
81 | ||
82 | obj = (union acpi_object *)buf.pointer; | |
83 | if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 3) { | |
84 | const union acpi_object *objs = obj->package.elements; | |
85 | ||
86 | *hcnt = (u16)objs[0].integer.value; | |
87 | *lcnt = (u16)objs[1].integer.value; | |
88 | if (sda_hold) | |
89 | *sda_hold = (u32)objs[2].integer.value; | |
90 | } | |
91 | ||
92 | kfree(buf.pointer); | |
93 | } | |
94 | ||
b61b1415 MW |
95 | static int dw_i2c_acpi_configure(struct platform_device *pdev) |
96 | { | |
97 | struct dw_i2c_dev *dev = platform_get_drvdata(pdev); | |
2d244c81 | 98 | const struct acpi_device_id *id; |
b61b1415 | 99 | |
b61b1415 | 100 | dev->adapter.nr = -1; |
b61b1415 MW |
101 | dev->tx_fifo_depth = 32; |
102 | dev->rx_fifo_depth = 32; | |
57cd1e30 MW |
103 | |
104 | /* | |
105 | * Try to get SDA hold time and *CNT values from an ACPI method if | |
106 | * it exists for both supported speed modes. | |
107 | */ | |
0b26c845 | 108 | dw_i2c_acpi_params(pdev, "SSCN", &dev->ss_hcnt, &dev->ss_lcnt, NULL); |
57cd1e30 | 109 | dw_i2c_acpi_params(pdev, "FMCN", &dev->fs_hcnt, &dev->fs_lcnt, |
0b26c845 | 110 | &dev->sda_hold_time); |
a92ec174 WV |
111 | dw_i2c_acpi_params(pdev, "FPCN", &dev->fp_hcnt, &dev->fp_lcnt, NULL); |
112 | dw_i2c_acpi_params(pdev, "HSCN", &dev->hs_hcnt, &dev->hs_lcnt, NULL); | |
57cd1e30 | 113 | |
2d244c81 XY |
114 | id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev); |
115 | if (id && id->driver_data) | |
116 | dev->accessor_flags |= (u32)id->driver_data; | |
117 | ||
b61b1415 MW |
118 | return 0; |
119 | } | |
120 | ||
121 | static const struct acpi_device_id dw_i2c_acpi_match[] = { | |
122 | { "INT33C2", 0 }, | |
123 | { "INT33C3", 0 }, | |
25b3dfc8 MW |
124 | { "INT3432", 0 }, |
125 | { "INT3433", 0 }, | |
5a7e6bd8 | 126 | { "80860F41", 0 }, |
0409516a | 127 | { "808622C1", 0 }, |
2d244c81 | 128 | { "AMD0010", ACCESS_INTR_MASK }, |
e4e666ba | 129 | { "AMDI0010", ACCESS_INTR_MASK }, |
90708ce2 | 130 | { "AMDI0510", 0 }, |
04a407f6 | 131 | { "APMC0D0F", 0 }, |
b61b1415 MW |
132 | { } |
133 | }; | |
134 | MODULE_DEVICE_TABLE(acpi, dw_i2c_acpi_match); | |
135 | #else | |
136 | static inline int dw_i2c_acpi_configure(struct platform_device *pdev) | |
137 | { | |
138 | return -ENODEV; | |
139 | } | |
140 | #endif | |
141 | ||
b33af11d SS |
142 | static int i2c_dw_plat_prepare_clk(struct dw_i2c_dev *i_dev, bool prepare) |
143 | { | |
144 | if (IS_ERR(i_dev->clk)) | |
145 | return PTR_ERR(i_dev->clk); | |
146 | ||
147 | if (prepare) | |
148 | return clk_prepare_enable(i_dev->clk); | |
149 | ||
150 | clk_disable_unprepare(i_dev->clk); | |
151 | return 0; | |
152 | } | |
153 | ||
8e598769 TH |
154 | static void dw_i2c_set_fifo_size(struct dw_i2c_dev *dev, int id) |
155 | { | |
156 | u32 param, tx_fifo_depth, rx_fifo_depth; | |
157 | ||
158 | /* | |
159 | * Try to detect the FIFO depth if not set by interface driver, | |
160 | * the depth could be from 2 to 256 from HW spec. | |
161 | */ | |
162 | param = i2c_dw_read_comp_param(dev); | |
163 | tx_fifo_depth = ((param >> 16) & 0xff) + 1; | |
164 | rx_fifo_depth = ((param >> 8) & 0xff) + 1; | |
165 | if (!dev->tx_fifo_depth) { | |
166 | dev->tx_fifo_depth = tx_fifo_depth; | |
167 | dev->rx_fifo_depth = rx_fifo_depth; | |
168 | dev->adapter.nr = id; | |
169 | } else if (tx_fifo_depth >= 2) { | |
170 | dev->tx_fifo_depth = min_t(u32, dev->tx_fifo_depth, | |
171 | tx_fifo_depth); | |
172 | dev->rx_fifo_depth = min_t(u32, dev->rx_fifo_depth, | |
173 | rx_fifo_depth); | |
174 | } | |
175 | } | |
176 | ||
6ad6fde3 | 177 | static int dw_i2c_plat_probe(struct platform_device *pdev) |
2373f6b9 | 178 | { |
4c5301ab | 179 | struct dw_i2c_platform_data *pdata = dev_get_platdata(&pdev->dev); |
2373f6b9 DB |
180 | struct dw_i2c_dev *dev; |
181 | struct i2c_adapter *adap; | |
1cb715ca | 182 | struct resource *mem; |
2373f6b9 | 183 | int irq, r; |
10f8e7fb | 184 | u32 acpi_speed, ht = 0; |
2373f6b9 | 185 | |
2373f6b9 | 186 | irq = platform_get_irq(pdev, 0); |
b20d3864 AB |
187 | if (irq < 0) |
188 | return irq; | |
2373f6b9 | 189 | |
1cb715ca AS |
190 | dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL); |
191 | if (!dev) | |
192 | return -ENOMEM; | |
2373f6b9 | 193 | |
3cc2d009 | 194 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
1cb715ca AS |
195 | dev->base = devm_ioremap_resource(&pdev->dev, mem); |
196 | if (IS_ERR(dev->base)) | |
197 | return PTR_ERR(dev->base); | |
2373f6b9 | 198 | |
1cb715ca | 199 | dev->dev = &pdev->dev; |
2373f6b9 DB |
200 | dev->irq = irq; |
201 | platform_set_drvdata(pdev, dev); | |
202 | ||
ab809fd8 ZG |
203 | dev->rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL); |
204 | if (IS_ERR(dev->rst)) { | |
205 | if (PTR_ERR(dev->rst) == -EPROBE_DEFER) | |
206 | return -EPROBE_DEFER; | |
207 | } else { | |
208 | reset_control_deassert(dev->rst); | |
209 | } | |
210 | ||
4c5301ab | 211 | if (pdata) { |
19c0a539 | 212 | dev->clk_freq = pdata->i2c_scl_freq; |
4bcfda09 | 213 | } else { |
4c5301ab MW |
214 | device_property_read_u32(&pdev->dev, "i2c-sda-hold-time-ns", |
215 | &ht); | |
216 | device_property_read_u32(&pdev->dev, "i2c-sda-falling-time-ns", | |
217 | &dev->sda_falling_time); | |
218 | device_property_read_u32(&pdev->dev, "i2c-scl-falling-time-ns", | |
219 | &dev->scl_falling_time); | |
220 | device_property_read_u32(&pdev->dev, "clock-frequency", | |
19c0a539 | 221 | &dev->clk_freq); |
4c5301ab MW |
222 | } |
223 | ||
10f8e7fb | 224 | acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev); |
973652db JN |
225 | /* |
226 | * Find bus speed from the "clock-frequency" device property, ACPI | |
227 | * or by using fast mode if neither is set. | |
228 | */ | |
229 | if (acpi_speed && dev->clk_freq) | |
230 | dev->clk_freq = min(dev->clk_freq, acpi_speed); | |
231 | else if (acpi_speed || dev->clk_freq) | |
232 | dev->clk_freq = max(dev->clk_freq, acpi_speed); | |
233 | else | |
234 | dev->clk_freq = 400000; | |
10f8e7fb | 235 | |
4c5301ab MW |
236 | if (has_acpi_companion(&pdev->dev)) |
237 | dw_i2c_acpi_configure(pdev); | |
238 | ||
239 | /* | |
d608c3d9 | 240 | * Only standard mode at 100kHz, fast mode at 400kHz, |
b6e67145 | 241 | * fast mode plus at 1MHz and high speed mode at 3.4MHz are supported. |
4c5301ab | 242 | */ |
d608c3d9 | 243 | if (dev->clk_freq != 100000 && dev->clk_freq != 400000 |
b6e67145 | 244 | && dev->clk_freq != 1000000 && dev->clk_freq != 3400000) { |
d608c3d9 | 245 | dev_err(&pdev->dev, |
b6e67145 | 246 | "Only 100kHz, 400kHz, 1MHz and 3.4MHz supported"); |
ab809fd8 ZG |
247 | r = -EINVAL; |
248 | goto exit_reset; | |
9803f868 CR |
249 | } |
250 | ||
894acb2f DB |
251 | r = i2c_dw_eval_lock_support(dev); |
252 | if (r) | |
ab809fd8 | 253 | goto exit_reset; |
894acb2f | 254 | |
f06122f0 | 255 | dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY; |
548e6695 WV |
256 | |
257 | dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE | | |
258 | DW_IC_CON_RESTART_EN; | |
259 | ||
b6e67145 WV |
260 | switch (dev->clk_freq) { |
261 | case 100000: | |
548e6695 | 262 | dev->master_cfg |= DW_IC_CON_SPEED_STD; |
b6e67145 WV |
263 | break; |
264 | case 3400000: | |
265 | dev->master_cfg |= DW_IC_CON_SPEED_HIGH; | |
266 | break; | |
267 | default: | |
548e6695 | 268 | dev->master_cfg |= DW_IC_CON_SPEED_FAST; |
b6e67145 | 269 | } |
2fa8326b | 270 | |
925ddb24 | 271 | dev->clk = devm_clk_get(&pdev->dev, NULL); |
b33af11d SS |
272 | if (!i2c_dw_plat_prepare_clk(dev, true)) { |
273 | dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz; | |
925ddb24 | 274 | |
b33af11d SS |
275 | if (!dev->sda_hold_time && ht) |
276 | dev->sda_hold_time = div_u64( | |
277 | (u64)dev->get_clk_rate_khz(dev) * ht + 500000, | |
278 | 1000000); | |
925ddb24 MW |
279 | } |
280 | ||
8e598769 | 281 | dw_i2c_set_fifo_size(dev, pdev->id); |
2373f6b9 DB |
282 | |
283 | adap = &dev->adapter; | |
2373f6b9 | 284 | adap->owner = THIS_MODULE; |
70fba830 | 285 | adap->class = I2C_CLASS_DEPRECATED; |
8eb5c87a | 286 | ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev)); |
af71100c | 287 | adap->dev.of_node = pdev->dev.of_node; |
2373f6b9 | 288 | |
894acb2f DB |
289 | if (dev->pm_runtime_disabled) { |
290 | pm_runtime_forbid(&pdev->dev); | |
291 | } else { | |
292 | pm_runtime_set_autosuspend_delay(&pdev->dev, 1000); | |
293 | pm_runtime_use_autosuspend(&pdev->dev); | |
294 | pm_runtime_set_active(&pdev->dev); | |
295 | pm_runtime_enable(&pdev->dev); | |
296 | } | |
7272194e | 297 | |
d55fc378 | 298 | r = i2c_dw_probe(dev); |
ab809fd8 ZG |
299 | if (r) |
300 | goto exit_probe; | |
36d48fb5 | 301 | |
e79e72c5 | 302 | return r; |
ab809fd8 ZG |
303 | |
304 | exit_probe: | |
305 | if (!dev->pm_runtime_disabled) | |
306 | pm_runtime_disable(&pdev->dev); | |
307 | exit_reset: | |
308 | if (!IS_ERR_OR_NULL(dev->rst)) | |
309 | reset_control_assert(dev->rst); | |
310 | return r; | |
2373f6b9 DB |
311 | } |
312 | ||
6ad6fde3 | 313 | static int dw_i2c_plat_remove(struct platform_device *pdev) |
2373f6b9 DB |
314 | { |
315 | struct dw_i2c_dev *dev = platform_get_drvdata(pdev); | |
2373f6b9 | 316 | |
7272194e MW |
317 | pm_runtime_get_sync(&pdev->dev); |
318 | ||
2373f6b9 | 319 | i2c_del_adapter(&dev->adapter); |
2373f6b9 | 320 | |
f3fa9f3d | 321 | i2c_dw_disable(dev); |
2373f6b9 | 322 | |
edfc3901 MW |
323 | pm_runtime_dont_use_autosuspend(&pdev->dev); |
324 | pm_runtime_put_sync(&pdev->dev); | |
e79e72c5 JN |
325 | if (!dev->pm_runtime_disabled) |
326 | pm_runtime_disable(&pdev->dev); | |
ab809fd8 ZG |
327 | if (!IS_ERR_OR_NULL(dev->rst)) |
328 | reset_control_assert(dev->rst); | |
7272194e | 329 | |
2373f6b9 DB |
330 | return 0; |
331 | } | |
332 | ||
af71100c RH |
333 | #ifdef CONFIG_OF |
334 | static const struct of_device_id dw_i2c_of_match[] = { | |
335 | { .compatible = "snps,designware-i2c", }, | |
336 | {}, | |
337 | }; | |
338 | MODULE_DEVICE_TABLE(of, dw_i2c_of_match); | |
339 | #endif | |
340 | ||
8503ff16 | 341 | #ifdef CONFIG_PM_SLEEP |
6ad6fde3 | 342 | static int dw_i2c_plat_prepare(struct device *dev) |
8503ff16 JZ |
343 | { |
344 | return pm_runtime_suspended(dev); | |
345 | } | |
346 | ||
6ad6fde3 | 347 | static void dw_i2c_plat_complete(struct device *dev) |
8503ff16 JZ |
348 | { |
349 | if (dev->power.direct_complete) | |
350 | pm_request_resume(dev); | |
351 | } | |
352 | #else | |
319d7f05 JN |
353 | #define dw_i2c_plat_prepare NULL |
354 | #define dw_i2c_plat_complete NULL | |
8503ff16 JZ |
355 | #endif |
356 | ||
1fc2fe20 | 357 | #ifdef CONFIG_PM |
6ad6fde3 | 358 | static int dw_i2c_plat_suspend(struct device *dev) |
3bf3b289 DS |
359 | { |
360 | struct platform_device *pdev = to_platform_device(dev); | |
361 | struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev); | |
362 | ||
f537295a | 363 | i2c_dw_disable(i_dev); |
b33af11d | 364 | i2c_dw_plat_prepare_clk(i_dev, false); |
3bf3b289 DS |
365 | |
366 | return 0; | |
367 | } | |
368 | ||
6ad6fde3 | 369 | static int dw_i2c_plat_resume(struct device *dev) |
3bf3b289 DS |
370 | { |
371 | struct platform_device *pdev = to_platform_device(dev); | |
372 | struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev); | |
373 | ||
b33af11d | 374 | i2c_dw_plat_prepare_clk(i_dev, true); |
894acb2f DB |
375 | |
376 | if (!i_dev->pm_runtime_disabled) | |
377 | i2c_dw_init(i_dev); | |
3bf3b289 DS |
378 | |
379 | return 0; | |
380 | } | |
3bf3b289 | 381 | |
8503ff16 | 382 | static const struct dev_pm_ops dw_i2c_dev_pm_ops = { |
6ad6fde3 JN |
383 | .prepare = dw_i2c_plat_prepare, |
384 | .complete = dw_i2c_plat_complete, | |
385 | SET_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume) | |
386 | SET_RUNTIME_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume, NULL) | |
8503ff16 JZ |
387 | }; |
388 | ||
389 | #define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops) | |
390 | #else | |
391 | #define DW_I2C_DEV_PMOPS NULL | |
392 | #endif | |
1fc2fe20 | 393 | |
2373f6b9 DB |
394 | /* work with hotplug and coldplug */ |
395 | MODULE_ALIAS("platform:i2c_designware"); | |
396 | ||
397 | static struct platform_driver dw_i2c_driver = { | |
6ad6fde3 JN |
398 | .probe = dw_i2c_plat_probe, |
399 | .remove = dw_i2c_plat_remove, | |
2373f6b9 DB |
400 | .driver = { |
401 | .name = "i2c_designware", | |
af71100c | 402 | .of_match_table = of_match_ptr(dw_i2c_of_match), |
b61b1415 | 403 | .acpi_match_table = ACPI_PTR(dw_i2c_acpi_match), |
8503ff16 | 404 | .pm = DW_I2C_DEV_PMOPS, |
2373f6b9 DB |
405 | }, |
406 | }; | |
407 | ||
408 | static int __init dw_i2c_init_driver(void) | |
409 | { | |
cccdcea1 | 410 | return platform_driver_register(&dw_i2c_driver); |
2373f6b9 | 411 | } |
10452280 | 412 | subsys_initcall(dw_i2c_init_driver); |
2373f6b9 DB |
413 | |
414 | static void __exit dw_i2c_exit_driver(void) | |
415 | { | |
416 | platform_driver_unregister(&dw_i2c_driver); | |
417 | } | |
418 | module_exit(dw_i2c_exit_driver); | |
419 | ||
420 | MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>"); | |
421 | MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter"); | |
422 | MODULE_LICENSE("GPL"); |