ACPI / APD: Add APM X-Gene ACPI I2C device support
[deliverable/linux.git] / drivers / i2c / busses / i2c-designware-platdrv.c
CommitLineData
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>
2373f6b9
DB
39#include <linux/io.h>
40#include <linux/slab.h>
b61b1415 41#include <linux/acpi.h>
4bcfda09 42#include <linux/platform_data/i2c-designware.h>
2373f6b9
DB
43#include "i2c-designware-core.h"
44
1d31b58f
DB
45static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev)
46{
47 return clk_get_rate(dev->clk)/1000;
48}
2373f6b9 49
b61b1415 50#ifdef CONFIG_ACPI
56d4b8a2
MW
51/*
52 * The HCNT/LCNT information coming from ACPI should be the most accurate
53 * for given platform. However, some systems get it wrong. On such systems
54 * we get better results by calculating those based on the input clock.
55 */
56static const struct dmi_system_id dw_i2c_no_acpi_params[] = {
57 {
58 .ident = "Dell Inspiron 7348",
59 .matches = {
60 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
61 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7348"),
62 },
63 },
64 { }
65};
66
57cd1e30
MW
67static void dw_i2c_acpi_params(struct platform_device *pdev, char method[],
68 u16 *hcnt, u16 *lcnt, u32 *sda_hold)
69{
70 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
71 acpi_handle handle = ACPI_HANDLE(&pdev->dev);
72 union acpi_object *obj;
73
56d4b8a2
MW
74 if (dmi_check_system(dw_i2c_no_acpi_params))
75 return;
76
57cd1e30
MW
77 if (ACPI_FAILURE(acpi_evaluate_object(handle, method, NULL, &buf)))
78 return;
79
80 obj = (union acpi_object *)buf.pointer;
81 if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 3) {
82 const union acpi_object *objs = obj->package.elements;
83
84 *hcnt = (u16)objs[0].integer.value;
85 *lcnt = (u16)objs[1].integer.value;
86 if (sda_hold)
87 *sda_hold = (u32)objs[2].integer.value;
88 }
89
90 kfree(buf.pointer);
91}
92
b61b1415
MW
93static int dw_i2c_acpi_configure(struct platform_device *pdev)
94{
95 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
b61b1415 96
b61b1415 97 dev->adapter.nr = -1;
b61b1415
MW
98 dev->tx_fifo_depth = 32;
99 dev->rx_fifo_depth = 32;
57cd1e30
MW
100
101 /*
102 * Try to get SDA hold time and *CNT values from an ACPI method if
103 * it exists for both supported speed modes.
104 */
0b26c845 105 dw_i2c_acpi_params(pdev, "SSCN", &dev->ss_hcnt, &dev->ss_lcnt, NULL);
57cd1e30 106 dw_i2c_acpi_params(pdev, "FMCN", &dev->fs_hcnt, &dev->fs_lcnt,
0b26c845 107 &dev->sda_hold_time);
57cd1e30 108
b61b1415
MW
109 return 0;
110}
111
112static const struct acpi_device_id dw_i2c_acpi_match[] = {
113 { "INT33C2", 0 },
114 { "INT33C3", 0 },
25b3dfc8
MW
115 { "INT3432", 0 },
116 { "INT3433", 0 },
5a7e6bd8 117 { "80860F41", 0 },
0409516a 118 { "808622C1", 0 },
3eddad96 119 { "AMD0010", 0 },
b61b1415
MW
120 { }
121};
122MODULE_DEVICE_TABLE(acpi, dw_i2c_acpi_match);
123#else
124static inline int dw_i2c_acpi_configure(struct platform_device *pdev)
125{
126 return -ENODEV;
127}
128#endif
129
6ad6fde3 130static int dw_i2c_plat_probe(struct platform_device *pdev)
2373f6b9
DB
131{
132 struct dw_i2c_dev *dev;
133 struct i2c_adapter *adap;
1cb715ca 134 struct resource *mem;
4bcfda09 135 struct dw_i2c_platform_data *pdata;
2373f6b9 136 int irq, r;
925ddb24 137 u32 clk_freq, ht = 0;
2373f6b9 138
2373f6b9 139 irq = platform_get_irq(pdev, 0);
b20d3864
AB
140 if (irq < 0)
141 return irq;
2373f6b9 142
1cb715ca
AS
143 dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
144 if (!dev)
145 return -ENOMEM;
2373f6b9 146
3cc2d009 147 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1cb715ca
AS
148 dev->base = devm_ioremap_resource(&pdev->dev, mem);
149 if (IS_ERR(dev->base))
150 return PTR_ERR(dev->base);
2373f6b9 151
1cb715ca 152 dev->dev = &pdev->dev;
2373f6b9
DB
153 dev->irq = irq;
154 platform_set_drvdata(pdev, dev);
155
8e5f6b2a
RB
156 /* fast mode by default because of legacy reasons */
157 clk_freq = 400000;
158
ca5b74d2 159 if (has_acpi_companion(&pdev->dev)) {
925ddb24
MW
160 dw_i2c_acpi_configure(pdev);
161 } else if (pdev->dev.of_node) {
9803f868
CR
162 of_property_read_u32(pdev->dev.of_node,
163 "i2c-sda-hold-time-ns", &ht);
6468276b
RB
164
165 of_property_read_u32(pdev->dev.of_node,
166 "i2c-sda-falling-time-ns",
167 &dev->sda_falling_time);
168 of_property_read_u32(pdev->dev.of_node,
169 "i2c-scl-falling-time-ns",
170 &dev->scl_falling_time);
8e5f6b2a
RB
171
172 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
173 &clk_freq);
174
175 /* Only standard mode at 100kHz and fast mode at 400kHz
176 * are supported.
177 */
178 if (clk_freq != 100000 && clk_freq != 400000) {
179 dev_err(&pdev->dev, "Only 100kHz and 400kHz supported");
180 return -EINVAL;
181 }
4bcfda09
TR
182 } else {
183 pdata = dev_get_platdata(&pdev->dev);
184 if (pdata)
185 clk_freq = pdata->i2c_scl_freq;
9803f868
CR
186 }
187
894acb2f
DB
188 r = i2c_dw_eval_lock_support(dev);
189 if (r)
190 return r;
191
2fa8326b
DB
192 dev->functionality =
193 I2C_FUNC_I2C |
194 I2C_FUNC_10BIT_ADDR |
195 I2C_FUNC_SMBUS_BYTE |
196 I2C_FUNC_SMBUS_BYTE_DATA |
197 I2C_FUNC_SMBUS_WORD_DATA |
198 I2C_FUNC_SMBUS_I2C_BLOCK;
8e5f6b2a
RB
199 if (clk_freq == 100000)
200 dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
201 DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_STD;
202 else
203 dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
204 DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
2fa8326b 205
925ddb24
MW
206 dev->clk = devm_clk_get(&pdev->dev, NULL);
207 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
208 if (IS_ERR(dev->clk))
209 return PTR_ERR(dev->clk);
210 clk_prepare_enable(dev->clk);
211
212 if (!dev->sda_hold_time && ht) {
213 u32 ic_clk = dev->get_clk_rate_khz(dev);
214
215 dev->sda_hold_time = div_u64((u64)ic_clk * ht + 500000,
216 1000000);
217 }
218
219 if (!dev->tx_fifo_depth) {
f3fa9f3d 220 u32 param1 = i2c_dw_read_comp_param(dev);
2373f6b9
DB
221
222 dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
223 dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
b61b1415 224 dev->adapter.nr = pdev->id;
2373f6b9 225 }
2373f6b9
DB
226
227 adap = &dev->adapter;
2373f6b9 228 adap->owner = THIS_MODULE;
70fba830 229 adap->class = I2C_CLASS_DEPRECATED;
8eb5c87a 230 ACPI_COMPANION_SET(&adap->dev, ACPI_COMPANION(&pdev->dev));
af71100c 231 adap->dev.of_node = pdev->dev.of_node;
2373f6b9 232
894acb2f
DB
233 if (dev->pm_runtime_disabled) {
234 pm_runtime_forbid(&pdev->dev);
235 } else {
236 pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
237 pm_runtime_use_autosuspend(&pdev->dev);
238 pm_runtime_set_active(&pdev->dev);
239 pm_runtime_enable(&pdev->dev);
240 }
7272194e 241
d55fc378 242 r = i2c_dw_probe(dev);
36d48fb5 243 if (r) {
36d48fb5
WS
244 pm_runtime_disable(&pdev->dev);
245 return r;
246 }
247
2373f6b9 248 return 0;
2373f6b9
DB
249}
250
6ad6fde3 251static int dw_i2c_plat_remove(struct platform_device *pdev)
2373f6b9
DB
252{
253 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
2373f6b9 254
7272194e
MW
255 pm_runtime_get_sync(&pdev->dev);
256
2373f6b9 257 i2c_del_adapter(&dev->adapter);
2373f6b9 258
f3fa9f3d 259 i2c_dw_disable(dev);
2373f6b9 260
edfc3901
MW
261 pm_runtime_dont_use_autosuspend(&pdev->dev);
262 pm_runtime_put_sync(&pdev->dev);
7272194e
MW
263 pm_runtime_disable(&pdev->dev);
264
2373f6b9
DB
265 return 0;
266}
267
af71100c
RH
268#ifdef CONFIG_OF
269static const struct of_device_id dw_i2c_of_match[] = {
270 { .compatible = "snps,designware-i2c", },
271 {},
272};
273MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
274#endif
275
8503ff16 276#ifdef CONFIG_PM_SLEEP
6ad6fde3 277static int dw_i2c_plat_prepare(struct device *dev)
8503ff16
JZ
278{
279 return pm_runtime_suspended(dev);
280}
281
6ad6fde3 282static void dw_i2c_plat_complete(struct device *dev)
8503ff16
JZ
283{
284 if (dev->power.direct_complete)
285 pm_request_resume(dev);
286}
287#else
319d7f05
JN
288#define dw_i2c_plat_prepare NULL
289#define dw_i2c_plat_complete NULL
8503ff16
JZ
290#endif
291
1fc2fe20 292#ifdef CONFIG_PM
6ad6fde3 293static int dw_i2c_plat_suspend(struct device *dev)
3bf3b289
DS
294{
295 struct platform_device *pdev = to_platform_device(dev);
296 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
297
f537295a 298 i2c_dw_disable(i_dev);
e1fac69f 299 clk_disable_unprepare(i_dev->clk);
3bf3b289
DS
300
301 return 0;
302}
303
6ad6fde3 304static int dw_i2c_plat_resume(struct device *dev)
3bf3b289
DS
305{
306 struct platform_device *pdev = to_platform_device(dev);
307 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
308
e1fac69f 309 clk_prepare_enable(i_dev->clk);
894acb2f
DB
310
311 if (!i_dev->pm_runtime_disabled)
312 i2c_dw_init(i_dev);
3bf3b289
DS
313
314 return 0;
315}
3bf3b289 316
8503ff16 317static const struct dev_pm_ops dw_i2c_dev_pm_ops = {
6ad6fde3
JN
318 .prepare = dw_i2c_plat_prepare,
319 .complete = dw_i2c_plat_complete,
320 SET_SYSTEM_SLEEP_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume)
321 SET_RUNTIME_PM_OPS(dw_i2c_plat_suspend, dw_i2c_plat_resume, NULL)
8503ff16
JZ
322};
323
324#define DW_I2C_DEV_PMOPS (&dw_i2c_dev_pm_ops)
325#else
326#define DW_I2C_DEV_PMOPS NULL
327#endif
1fc2fe20 328
2373f6b9
DB
329/* work with hotplug and coldplug */
330MODULE_ALIAS("platform:i2c_designware");
331
332static struct platform_driver dw_i2c_driver = {
6ad6fde3
JN
333 .probe = dw_i2c_plat_probe,
334 .remove = dw_i2c_plat_remove,
2373f6b9
DB
335 .driver = {
336 .name = "i2c_designware",
af71100c 337 .of_match_table = of_match_ptr(dw_i2c_of_match),
b61b1415 338 .acpi_match_table = ACPI_PTR(dw_i2c_acpi_match),
8503ff16 339 .pm = DW_I2C_DEV_PMOPS,
2373f6b9
DB
340 },
341};
342
343static int __init dw_i2c_init_driver(void)
344{
cccdcea1 345 return platform_driver_register(&dw_i2c_driver);
2373f6b9 346}
10452280 347subsys_initcall(dw_i2c_init_driver);
2373f6b9
DB
348
349static void __exit dw_i2c_exit_driver(void)
350{
351 platform_driver_unregister(&dw_i2c_driver);
352}
353module_exit(dw_i2c_exit_driver);
354
355MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
356MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
357MODULE_LICENSE("GPL");
This page took 0.306923 seconds and 5 git commands to generate.