blob: d8afc85420be0a524d31c9c4d430e4253bd02a59 [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>
43#include "i2c-designware-core.h"
44
45static struct i2c_algorithm i2c_dw_algo = {
46 .master_xfer = i2c_dw_xfer,
47 .functionality = i2c_dw_func,
48};
Dirk Brandewie1d31b582011-10-06 11:26:30 -070049static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
50{
51 return clk_get_rate(dev->clk)/1000;
52}
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010053
Bill Pemberton0b255e92012-11-27 15:59:38 -050054static int dw_i2c_probe(struct platform_device *pdev)
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010055{
56 struct dw_i2c_dev *dev;
57 struct i2c_adapter *adap;
58 struct resource *mem, *ioarea;
59 int irq, r;
60
61 /* NOTE: driver uses the static register mapping */
62 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
63 if (!mem) {
64 dev_err(&pdev->dev, "no mem resource?\n");
65 return -EINVAL;
66 }
67
68 irq = platform_get_irq(pdev, 0);
69 if (irq < 0) {
70 dev_err(&pdev->dev, "no irq resource?\n");
71 return irq; /* -ENXIO */
72 }
73
74 ioarea = request_mem_region(mem->start, resource_size(mem),
75 pdev->name);
76 if (!ioarea) {
77 dev_err(&pdev->dev, "I2C region already claimed\n");
78 return -EBUSY;
79 }
80
81 dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
82 if (!dev) {
83 r = -ENOMEM;
84 goto err_release_region;
85 }
86
87 init_completion(&dev->cmd_complete);
88 mutex_init(&dev->lock);
89 dev->dev = get_device(&pdev->dev);
90 dev->irq = irq;
91 platform_set_drvdata(pdev, dev);
92
93 dev->clk = clk_get(&pdev->dev, NULL);
Dirk Brandewie1d31b582011-10-06 11:26:30 -070094 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
95
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010096 if (IS_ERR(dev->clk)) {
97 r = -ENODEV;
98 goto err_free_mem;
99 }
Viresh Kumare1fac692012-04-17 17:04:31 +0530100 clk_prepare_enable(dev->clk);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100101
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700102 dev->functionality =
103 I2C_FUNC_I2C |
104 I2C_FUNC_10BIT_ADDR |
105 I2C_FUNC_SMBUS_BYTE |
106 I2C_FUNC_SMBUS_BYTE_DATA |
107 I2C_FUNC_SMBUS_WORD_DATA |
108 I2C_FUNC_SMBUS_I2C_BLOCK;
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700109 dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
110 DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700111
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100112 dev->base = ioremap(mem->start, resource_size(mem));
113 if (dev->base == NULL) {
114 dev_err(&pdev->dev, "failure mapping io resources\n");
115 r = -EBUSY;
116 goto err_unuse_clocks;
117 }
118 {
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700119 u32 param1 = i2c_dw_read_comp_param(dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100120
121 dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
122 dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
123 }
124 r = i2c_dw_init(dev);
125 if (r)
126 goto err_iounmap;
127
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700128 i2c_dw_disable_int(dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100129 r = request_irq(dev->irq, i2c_dw_isr, IRQF_DISABLED, pdev->name, dev);
130 if (r) {
131 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
132 goto err_iounmap;
133 }
134
135 adap = &dev->adapter;
136 i2c_set_adapdata(adap, dev);
137 adap->owner = THIS_MODULE;
138 adap->class = I2C_CLASS_HWMON;
139 strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
140 sizeof(adap->name));
141 adap->algo = &i2c_dw_algo;
142 adap->dev.parent = &pdev->dev;
Rob Herringaf711002011-11-08 14:43:47 -0600143 adap->dev.of_node = pdev->dev.of_node;
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100144
145 adap->nr = pdev->id;
146 r = i2c_add_numbered_adapter(adap);
147 if (r) {
148 dev_err(&pdev->dev, "failure adding adapter\n");
149 goto err_free_irq;
150 }
Rob Herringaf711002011-11-08 14:43:47 -0600151 of_i2c_register_devices(adap);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100152
Mika Westerberg72721942013-01-17 12:31:06 +0200153 pm_runtime_set_active(&pdev->dev);
154 pm_runtime_enable(&pdev->dev);
155 pm_runtime_put(&pdev->dev);
156
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100157 return 0;
158
159err_free_irq:
160 free_irq(dev->irq, dev);
161err_iounmap:
162 iounmap(dev->base);
163err_unuse_clocks:
Viresh Kumare1fac692012-04-17 17:04:31 +0530164 clk_disable_unprepare(dev->clk);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100165 clk_put(dev->clk);
166 dev->clk = NULL;
167err_free_mem:
168 platform_set_drvdata(pdev, NULL);
169 put_device(&pdev->dev);
170 kfree(dev);
171err_release_region:
172 release_mem_region(mem->start, resource_size(mem));
173
174 return r;
175}
176
Bill Pemberton0b255e92012-11-27 15:59:38 -0500177static int dw_i2c_remove(struct platform_device *pdev)
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100178{
179 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
180 struct resource *mem;
181
182 platform_set_drvdata(pdev, NULL);
Mika Westerberg72721942013-01-17 12:31:06 +0200183 pm_runtime_get_sync(&pdev->dev);
184
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100185 i2c_del_adapter(&dev->adapter);
186 put_device(&pdev->dev);
187
Viresh Kumare1fac692012-04-17 17:04:31 +0530188 clk_disable_unprepare(dev->clk);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100189 clk_put(dev->clk);
190 dev->clk = NULL;
191
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700192 i2c_dw_disable(dev);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100193 free_irq(dev->irq, dev);
194 kfree(dev);
195
Mika Westerberg72721942013-01-17 12:31:06 +0200196 pm_runtime_put(&pdev->dev);
197 pm_runtime_disable(&pdev->dev);
198
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100199 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 release_mem_region(mem->start, resource_size(mem));
201 return 0;
202}
203
Rob Herringaf711002011-11-08 14:43:47 -0600204#ifdef CONFIG_OF
205static const struct of_device_id dw_i2c_of_match[] = {
206 { .compatible = "snps,designware-i2c", },
207 {},
208};
209MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
210#endif
211
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530212#ifdef CONFIG_PM
213static int dw_i2c_suspend(struct device *dev)
214{
215 struct platform_device *pdev = to_platform_device(dev);
216 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
217
Viresh Kumare1fac692012-04-17 17:04:31 +0530218 clk_disable_unprepare(i_dev->clk);
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530219
220 return 0;
221}
222
223static int dw_i2c_resume(struct device *dev)
224{
225 struct platform_device *pdev = to_platform_device(dev);
226 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
227
Viresh Kumare1fac692012-04-17 17:04:31 +0530228 clk_prepare_enable(i_dev->clk);
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530229 i2c_dw_init(i_dev);
230
231 return 0;
232}
233#endif
234
235static SIMPLE_DEV_PM_OPS(dw_i2c_dev_pm_ops, dw_i2c_suspend, dw_i2c_resume);
236
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100237/* work with hotplug and coldplug */
238MODULE_ALIAS("platform:i2c_designware");
239
240static struct platform_driver dw_i2c_driver = {
Bill Pemberton0b255e92012-11-27 15:59:38 -0500241 .remove = dw_i2c_remove,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100242 .driver = {
243 .name = "i2c_designware",
244 .owner = THIS_MODULE,
Rob Herringaf711002011-11-08 14:43:47 -0600245 .of_match_table = of_match_ptr(dw_i2c_of_match),
Deepak Sikri3bf3b282012-02-24 17:01:15 +0530246 .pm = &dw_i2c_dev_pm_ops,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100247 },
248};
249
250static int __init dw_i2c_init_driver(void)
251{
252 return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
253}
Pratyush Anand10452282012-02-29 12:27:46 +0530254subsys_initcall(dw_i2c_init_driver);
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100255
256static void __exit dw_i2c_exit_driver(void)
257{
258 platform_driver_unregister(&dw_i2c_driver);
259}
260module_exit(dw_i2c_exit_driver);
261
262MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
263MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
264MODULE_LICENSE("GPL");