1 /*
2  * Synopsys DesignWare I2C adapter driver.
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.
21  * ----------------------------------------------------------------------------
22  *
23  */
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>
33 #include <linux/io.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/of.h>
37 #include <linux/platform_data/i2c-designware.h>
38 #include <linux/platform_device.h>
39 #include <linux/pm.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>
46 
47 #include "i2c-designware-core.h"
48 
49 static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
50 {
51 	return clk_get_rate(dev->clk)/1000;
52 }
53 
54 #ifdef CONFIG_ACPI
55 /*
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.
59  */
60 static const struct dmi_system_id dw_i2c_no_acpi_params[] = {
61 	{
62 		.ident = "Dell Inspiron 7348",
63 		.matches = {
64 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
65 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7348"),
66 		},
67 	},
68 	{ }
69 };
70 
71 static void dw_i2c_acpi_params(struct platform_device *pdev, char method[],
72 			       u16 *hcnt, u16 *lcnt, u32 *sda_hold)
73 {
74 	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
75 	acpi_handle handle = ACPI_HANDLE(&pdev->dev);
76 	union acpi_object *obj;
77 
78 	if (dmi_check_system(dw_i2c_no_acpi_params))
79 		return;
80 
81 	if (ACPI_FAILURE(acpi_evaluate_object(handle, method, NULL, &buf)))
82 		return;
83 
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;
87 
88 		*hcnt = (u16)objs[0].integer.value;
89 		*lcnt = (u16)objs[1].integer.value;
90 		*sda_hold = (u32)objs[2].integer.value;
91 	}
92 
93 	kfree(buf.pointer);
94 }
95 
96 static int dw_i2c_acpi_configure(struct platform_device *pdev)
97 {
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;
103 	const char *uid;
104 
105 	dev->adapter.nr = -1;
106 	dev->tx_fifo_depth = 32;
107 	dev->rx_fifo_depth = 32;
108 
109 	/*
110 	 * Try to get SDA hold time and *CNT values from an ACPI method for
111 	 * selected speed modes.
112 	 */
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);
117 
118 	switch (dev->clk_freq) {
119 	case 100000:
120 		dev->sda_hold_time = ss_ht;
121 		break;
122 	case 1000000:
123 		dev->sda_hold_time = fp_ht;
124 		break;
125 	case 3400000:
126 		dev->sda_hold_time = hs_ht;
127 		break;
128 	case 400000:
129 	default:
130 		dev->sda_hold_time = fs_ht;
131 		break;
132 	}
133 
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;
137 
138 	if (acpi_bus_get_device(handle, &adev))
139 		return -ENODEV;
140 
141 	/*
142 	 * Cherrytrail I2C7 gets used for the PMIC which gets accessed
143 	 * through ACPI opregions during late suspend / early resume
144 	 * disable pm for it.
145 	 */
146 	uid = adev->pnp.unique_id;
147 	if ((dev->flags & MODEL_CHERRYTRAIL) && !strcmp(uid, "7"))
148 		dev->pm_disabled = true;
149 
150 	return 0;
151 }
152 
153 static const struct acpi_device_id dw_i2c_acpi_match[] = {
154 	{ "INT33C2", 0 },
155 	{ "INT33C3", 0 },
156 	{ "INT3432", 0 },
157 	{ "INT3433", 0 },
158 	{ "80860F41", 0 },
159 	{ "808622C1", MODEL_CHERRYTRAIL },
160 	{ "AMD0010", ACCESS_INTR_MASK },
161 	{ "AMDI0010", ACCESS_INTR_MASK },
162 	{ "AMDI0510", 0 },
163 	{ "APMC0D0F", 0 },
164 	{ "HISI02A1", 0 },
165 	{ "HISI02A2", 0 },
166 	{ }
167 };
168 MODULE_DEVICE_TABLE(acpi, dw_i2c_acpi_match);
169 #else
170 static inline int dw_i2c_acpi_configure(struct platform_device *pdev)
171 {
172 	return -ENODEV;
173 }
174 #endif
175 
176 static void i2c_dw_configure_master(struct dw_i2c_dev *dev)
177 {
178 	dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY;
179 
180 	dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
181 			  DW_IC_CON_RESTART_EN;
182 
183 	dev->mode = DW_IC_MASTER;
184 
185 	switch (dev->clk_freq) {
186 	case 100000:
187 		dev->master_cfg |= DW_IC_CON_SPEED_STD;
188 		break;
189 	case 3400000:
190 		dev->master_cfg |= DW_IC_CON_SPEED_HIGH;
191 		break;
192 	default:
193 		dev->master_cfg |= DW_IC_CON_SPEED_FAST;
194 	}
195 }
196 
197 static void i2c_dw_configure_slave(struct dw_i2c_dev *dev)
198 {
199 	dev->functionality = I2C_FUNC_SLAVE | DW_IC_DEFAULT_FUNCTIONALITY;
200 
201 	dev->slave_cfg = DW_IC_CON_RX_FIFO_FULL_HLD_CTRL |
202 			 DW_IC_CON_RESTART_EN | DW_IC_CON_STOP_DET_IFADDRESSED;
203 
204 	dev->mode = DW_IC_SLAVE;
205 }
206 
207 static void dw_i2c_set_fifo_size(struct dw_i2c_dev *dev, int id)
208 {
209 	u32 param, tx_fifo_depth, rx_fifo_depth;
210 
211 	/*
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.
214 	 */
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,
224 				tx_fifo_depth);
225 		dev->rx_fifo_depth = min_t(u32, dev->rx_fifo_depth,
226 				rx_fifo_depth);
227 	}
228 }
229 
230 static void dw_i2c_plat_pm_cleanup(struct dw_i2c_dev *dev)
231 {
232 	pm_runtime_disable(dev->dev);
233 
234 	if (dev->pm_disabled)
235 		pm_runtime_put_noidle(dev->dev);
236 }
237 
238 static int dw_i2c_plat_probe(struct platform_device *pdev)
239 {
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;
245 	int i, irq, ret;
246 	static const int supported_speeds[] = {
247 		0, 100000, 400000, 1000000, 3400000
248 	};
249 
250 	irq = platform_get_irq(pdev, 0);
251 	if (irq < 0)
252 		return irq;
253 
254 	dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
255 	if (!dev)
256 		return -ENOMEM;
257 
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);
262 
263 	dev->dev = &pdev->dev;
264 	dev->irq = irq;
265 	platform_set_drvdata(pdev, dev);
266 
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;
271 	} else {
272 		reset_control_deassert(dev->rst);
273 	}
274 
275 	if (pdata) {
276 		dev->clk_freq = pdata->i2c_scl_freq;
277 	} else {
278 		device_property_read_u32(&pdev->dev, "i2c-sda-hold-time-ns",
279 					 &ht);
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",
285 					 &dev->clk_freq);
286 	}
287 
288 	acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev);
289 	/*
290 	 * Some DSTDs use a non standard speed, round down to the lowest
291 	 * standard speed.
292 	 */
293 	for (i = 1; i < ARRAY_SIZE(supported_speeds); i++) {
294 		if (acpi_speed < supported_speeds[i])
295 			break;
296 	}
297 	acpi_speed = supported_speeds[i - 1];
298 
299 	/*
300 	 * Find bus speed from the "clock-frequency" device property, ACPI
301 	 * or by using fast mode if neither is set.
302 	 */
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);
307 	else
308 		dev->clk_freq = 400000;
309 
310 	if (has_acpi_companion(&pdev->dev))
311 		dw_i2c_acpi_configure(pdev);
312 
313 	/*
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.
316 	 */
317 	if (dev->clk_freq != 100000 && dev->clk_freq != 400000
318 	    && dev->clk_freq != 1000000 && dev->clk_freq != 3400000) {
319 		dev_err(&pdev->dev,
320 			"%d Hz is unsupported, only 100kHz, 400kHz, 1MHz and 3.4MHz are supported\n",
321 			dev->clk_freq);
322 		ret = -EINVAL;
323 		goto exit_reset;
324 	}
325 
326 	ret = i2c_dw_probe_lock_support(dev);
327 	if (ret)
328 		goto exit_reset;
329 
330 	if (i2c_detect_slave_mode(&pdev->dev))
331 		i2c_dw_configure_slave(dev);
332 	else
333 		i2c_dw_configure_master(dev);
334 
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;
338 
339 		if (!dev->sda_hold_time && ht)
340 			dev->sda_hold_time = div_u64(
341 				(u64)dev->get_clk_rate_khz(dev) * ht + 500000,
342 				1000000);
343 	}
344 
345 	dw_i2c_set_fifo_size(dev, pdev->id);
346 
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;
352 
353 	dev_pm_set_driver_flags(&pdev->dev,
354 				DPM_FLAG_SMART_PREPARE |
355 				DPM_FLAG_SMART_SUSPEND |
356 				DPM_FLAG_LEAVE_SUSPENDED);
357 
358 	/* The code below assumes runtime PM to be disabled. */
359 	WARN_ON(pm_runtime_enabled(&pdev->dev));
360 
361 	pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
362 	pm_runtime_use_autosuspend(&pdev->dev);
363 	pm_runtime_set_active(&pdev->dev);
364 
365 	if (dev->pm_disabled)
366 		pm_runtime_get_noresume(&pdev->dev);
367 
368 	pm_runtime_enable(&pdev->dev);
369 
370 	if (dev->mode == DW_IC_SLAVE)
371 		ret = i2c_dw_probe_slave(dev);
372 	else
373 		ret = i2c_dw_probe(dev);
374 
375 	if (ret)
376 		goto exit_probe;
377 
378 	return ret;
379 
380 exit_probe:
381 	dw_i2c_plat_pm_cleanup(dev);
382 exit_reset:
383 	if (!IS_ERR_OR_NULL(dev->rst))
384 		reset_control_assert(dev->rst);
385 	return ret;
386 }
387 
388 static int dw_i2c_plat_remove(struct platform_device *pdev)
389 {
390 	struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
391 
392 	pm_runtime_get_sync(&pdev->dev);
393 
394 	i2c_del_adapter(&dev->adapter);
395 
396 	dev->disable(dev);
397 
398 	pm_runtime_dont_use_autosuspend(&pdev->dev);
399 	pm_runtime_put_sync(&pdev->dev);
400 	dw_i2c_plat_pm_cleanup(dev);
401 
402 	if (!IS_ERR_OR_NULL(dev->rst))
403 		reset_control_assert(dev->rst);
404 
405 	i2c_dw_remove_lock_support(dev);
406 
407 	return 0;
408 }
409 
410 #ifdef CONFIG_OF
411 static const struct of_device_id dw_i2c_of_match[] = {
412 	{ .compatible = "snps,designware-i2c", },
413 	{},
414 };
415 MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
416 #endif
417 
418 #ifdef CONFIG_PM_SLEEP
419 static int dw_i2c_plat_prepare(struct device *dev)
420 {
421 	/*
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.
426 	 */
427 	return !has_acpi_companion(dev);
428 }
429 
430 static void dw_i2c_plat_complete(struct device *dev)
431 {
432 	/*
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.
437 	 */
438 	if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
439 		pm_request_resume(dev);
440 }
441 #else
442 #define dw_i2c_plat_prepare	NULL
443 #define dw_i2c_plat_complete	NULL
444 #endif
445 
446 #ifdef CONFIG_PM
447 static int dw_i2c_plat_suspend(struct device *dev)
448 {
449 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
450 
451 	i_dev->disable(i_dev);
452 	i2c_dw_prepare_clk(i_dev, false);
453 
454 	return 0;
455 }
456 
457 static int dw_i2c_plat_resume(struct device *dev)
458 {
459 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
460 
461 	i2c_dw_prepare_clk(i_dev, true);
462 	i_dev->init(i_dev);
463 
464 	return 0;
465 }
466 
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)
472 };
473 
474 #define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
475 #else
476 #define DW_I2C_DEV_PMOPS NULL
477 #endif
478 
479 /* Work with hotplug and coldplug */
480 MODULE_ALIAS("platform:i2c_designware");
481 
482 static struct platform_driver dw_i2c_driver = {
483 	.probe = dw_i2c_plat_probe,
484 	.remove = dw_i2c_plat_remove,
485 	.driver		= {
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,
490 	},
491 };
492 
493 static int __init dw_i2c_init_driver(void)
494 {
495 	return platform_driver_register(&dw_i2c_driver);
496 }
497 subsys_initcall(dw_i2c_init_driver);
498 
499 static void __exit dw_i2c_exit_driver(void)
500 {
501 	platform_driver_unregister(&dw_i2c_driver);
502 }
503 module_exit(dw_i2c_exit_driver);
504 
505 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
506 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
507 MODULE_LICENSE("GPL");
508