blob: c53a3127f6940e0b09528aebd4dae4e169a119d8 [file] [log] [blame]
Dirk Brandewie2373f6b2011-10-29 10:57:23 +01001/*
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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * ----------------------------------------------------------------------------
26 *
27 */
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/delay.h>
31#include <linux/i2c.h>
32#include <linux/clk.h>
33#include <linux/errno.h>
34#include <linux/sched.h>
35#include <linux/err.h>
36#include <linux/interrupt.h>
Rob Herringaf711002011-11-08 14:43:47 -060037#include <linux/of_i2c.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010038#include <linux/platform_device.h>
Deepak Sikri3bf3b282012-02-24 17:01:15 +053039#include <linux/pm.h>
Mika Westerberg72721942013-01-17 12:31:06 +020040#include <linux/pm_runtime.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010041#include <linux/io.h>
42#include <linux/slab.h>
Mika Westerbergb61b1412013-01-17 12:31:07 +020043#include <linux/acpi.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010044#include "i2c-designware-core.h"
45
46static struct i2c_algorithm i2c_dw_algo = {
47 .master_xfer = i2c_dw_xfer,
48 .functionality = i2c_dw_func,
49};
Dirk Brandewie1d31b582011-10-06 11:26:30 -070050static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
51{
52 return clk_get_rate(dev->clk)/1000;
53}
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010054
Mika Westerbergb61b1412013-01-17 12:31:07 +020055#ifdef CONFIG_ACPI
56static int dw_i2c_acpi_configure(struct platform_device *pdev)
57{
58 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
59 struct acpi_device *adev;
60 int busno, ret;
61
62 if (!ACPI_HANDLE(&pdev->dev))
63 return -ENODEV;
64
65 ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
66 if (ret)
67 return -ENODEV;
68
69 dev->adapter.nr = -1;
70 if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &busno))
71 dev->adapter.nr = busno;
72
73 dev->tx_fifo_depth = 32;
74 dev->rx_fifo_depth = 32;
75 return 0;
76}
77
78static const struct acpi_device_id dw_i2c_acpi_match[] = {
79 { "INT33C2", 0 },
80 { "INT33C3", 0 },
81 { }
82};
83MODULE_DEVICE_TABLE(acpi, dw_i2c_acpi_match);
84#else
85static inline int dw_i2c_acpi_configure(struct platform_device *pdev)
86{
87 return -ENODEV;
88}
89#endif
90
Bill Pemberton0b255e92012-11-27 15:59:38 -050091static int dw_i2c_probe(struct platform_device *pdev)
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010092{
93 struct dw_i2c_dev *dev;
94 struct i2c_adapter *adap;
Andy Shevchenko1cb715c2013-04-10 00:36:36 +000095 struct resource *mem;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010096 int irq, r;
97
98 /* NOTE: driver uses the static register mapping */
99 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
100 if (!mem) {
101 dev_err(&pdev->dev, "no mem resource?\n");
102 return -EINVAL;
103 }
104
105 irq = platform_get_irq(pdev, 0);
106 if (irq < 0) {
107 dev_err(&pdev->dev, "no irq resource?\n");
108 return irq; /* -ENXIO */
109 }
110
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000111 dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
112 if (!dev)
113 return -ENOMEM;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100114
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000115 dev->base = devm_ioremap_resource(&pdev->dev, mem);
116 if (IS_ERR(dev->base))
117 return PTR_ERR(dev->base);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100118
119 init_completion(&dev->cmd_complete);
120 mutex_init(&dev->lock);
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000121 dev->dev = &pdev->dev;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100122 dev->irq = irq;
123 platform_set_drvdata(pdev, dev);
124
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000125 dev->clk = devm_clk_get(&pdev->dev, NULL);
Dirk Brandewie1d31b582011-10-06 11:26:30 -0700126 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
127
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000128 if (IS_ERR(dev->clk))
129 return PTR_ERR(dev->clk);
Viresh Kumare1fac692012-04-17 17:04:31 +0530130 clk_prepare_enable(dev->clk);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100131
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700132 dev->functionality =
133 I2C_FUNC_I2C |
134 I2C_FUNC_10BIT_ADDR |
135 I2C_FUNC_SMBUS_BYTE |
136 I2C_FUNC_SMBUS_BYTE_DATA |
137 I2C_FUNC_SMBUS_WORD_DATA |
138 I2C_FUNC_SMBUS_I2C_BLOCK;
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700139 dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
140 DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700141
Mika Westerbergb61b1412013-01-17 12:31:07 +0200142 /* Try first if we can configure the device from ACPI */
143 r = dw_i2c_acpi_configure(pdev);
144 if (r) {
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700145 u32 param1 = i2c_dw_read_comp_param(dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100146
147 dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
148 dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
Mika Westerbergb61b1412013-01-17 12:31:07 +0200149 dev->adapter.nr = pdev->id;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100150 }
151 r = i2c_dw_init(dev);
152 if (r)
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000153 return r;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100154
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700155 i2c_dw_disable_int(dev);
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000156 r = devm_request_irq(&pdev->dev, dev->irq, i2c_dw_isr, IRQF_SHARED,
157 pdev->name, dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100158 if (r) {
159 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000160 return r;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100161 }
162
163 adap = &dev->adapter;
164 i2c_set_adapdata(adap, dev);
165 adap->owner = THIS_MODULE;
166 adap->class = I2C_CLASS_HWMON;
167 strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
168 sizeof(adap->name));
169 adap->algo = &i2c_dw_algo;
170 adap->dev.parent = &pdev->dev;
Rob Herringaf711002011-11-08 14:43:47 -0600171 adap->dev.of_node = pdev->dev.of_node;
Mika Westerbergb61b1412013-01-17 12:31:07 +0200172 ACPI_HANDLE_SET(&adap->dev, ACPI_HANDLE(&pdev->dev));
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100173
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100174 r = i2c_add_numbered_adapter(adap);
175 if (r) {
176 dev_err(&pdev->dev, "failure adding adapter\n");
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000177 return r;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100178 }
Rob Herringaf711002011-11-08 14:43:47 -0600179 of_i2c_register_devices(adap);
Mika Westerbergb61b1412013-01-17 12:31:07 +0200180 acpi_i2c_register_devices(adap);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100181
Andy Shevchenko1cb715c2013-04-10 00:36:36 +0000182 /* Increase reference counter */
183 get_device(&pdev->dev);
184
Mika Westerberg72721942013-01-17 12:31:06 +0200185 pm_runtime_set_active(&pdev->dev);
186 pm_runtime_enable(&pdev->dev);
187 pm_runtime_put(&pdev->dev);
188
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100189 return 0;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100190}
191
Bill Pemberton0b255e92012-11-27 15:59:38 -0500192static int dw_i2c_remove(struct platform_device *pdev)
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100193{
194 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100195
Mika Westerberg72721942013-01-17 12:31:06 +0200196 pm_runtime_get_sync(&pdev->dev);
197
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100198 i2c_del_adapter(&dev->adapter);
199 put_device(&pdev->dev);
200
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700201 i2c_dw_disable(dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100202
Mika Westerberg72721942013-01-17 12:31:06 +0200203 pm_runtime_put(&pdev->dev);
204 pm_runtime_disable(&pdev->dev);
205
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100206 return 0;
207}
208
Rob Herringaf711002011-11-08 14:43:47 -0600209#ifdef CONFIG_OF
210static const struct of_device_id dw_i2c_of_match[] = {
211 { .compatible = "snps,designware-i2c", },
212 {},
213};
214MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
215#endif
216
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530217#ifdef CONFIG_PM
218static int dw_i2c_suspend(struct device *dev)
219{
220 struct platform_device *pdev = to_platform_device(dev);
221 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
222
Viresh Kumare1fac692012-04-17 17:04:31 +0530223 clk_disable_unprepare(i_dev->clk);
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530224
225 return 0;
226}
227
228static int dw_i2c_resume(struct device *dev)
229{
230 struct platform_device *pdev = to_platform_device(dev);
231 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
232
Viresh Kumare1fac692012-04-17 17:04:31 +0530233 clk_prepare_enable(i_dev->clk);
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530234 i2c_dw_init(i_dev);
235
236 return 0;
237}
238#endif
239
240static SIMPLE_DEV_PM_OPS(dw_i2c_dev_pm_ops, dw_i2c_suspend, dw_i2c_resume);
241
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100242/* work with hotplug and coldplug */
243MODULE_ALIAS("platform:i2c_designware");
244
245static struct platform_driver dw_i2c_driver = {
Bill Pemberton0b255e92012-11-27 15:59:38 -0500246 .remove = dw_i2c_remove,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100247 .driver = {
248 .name = "i2c_designware",
249 .owner = THIS_MODULE,
Rob Herringaf711002011-11-08 14:43:47 -0600250 .of_match_table = of_match_ptr(dw_i2c_of_match),
Mika Westerbergb61b1412013-01-17 12:31:07 +0200251 .acpi_match_table = ACPI_PTR(dw_i2c_acpi_match),
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530252 .pm = &dw_i2c_dev_pm_ops,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100253 },
254};
255
256static int __init dw_i2c_init_driver(void)
257{
258 return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
259}
Pratyush Anand10452282012-02-29 12:27:46 +0530260subsys_initcall(dw_i2c_init_driver);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100261
262static void __exit dw_i2c_exit_driver(void)
263{
264 platform_driver_unregister(&dw_i2c_driver);
265}
266module_exit(dw_i2c_exit_driver);
267
268MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
269MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
270MODULE_LICENSE("GPL");