[ARM] mv78xx0: wire i2c support
[linux-2.6] / arch / arm / mach-pnx4008 / i2c.c
1 /*
2  * I2C initialization for PNX4008.
3  *
4  * Author: Vitaly Wool <vitalywool@gmail.com>
5  *
6  * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
7  * the terms of the GNU General Public License version 2. This program
8  * is licensed "as is" without any warranty of any kind, whether express
9  * or implied.
10  */
11
12 #include <linux/clk.h>
13 #include <linux/i2c.h>
14 #include <linux/i2c-pnx.h>
15 #include <linux/platform_device.h>
16 #include <linux/err.h>
17 #include <mach/platform.h>
18 #include <mach/irqs.h>
19 #include <mach/i2c.h>
20
21 static int set_clock_run(struct platform_device *pdev)
22 {
23         struct clk *clk;
24         char name[10];
25         int retval = 0;
26
27         snprintf(name, 10, "i2c%d_ck", pdev->id);
28         clk = clk_get(&pdev->dev, name);
29         if (!IS_ERR(clk)) {
30                 clk_set_rate(clk, 1);
31                 clk_put(clk);
32         } else
33                 retval = -ENOENT;
34
35         return retval;
36 }
37
38 static int set_clock_stop(struct platform_device *pdev)
39 {
40         struct clk *clk;
41         char name[10];
42         int retval = 0;
43
44         snprintf(name, 10, "i2c%d_ck", pdev->id);
45         clk = clk_get(&pdev->dev, name);
46         if (!IS_ERR(clk)) {
47                 clk_set_rate(clk, 0);
48                 clk_put(clk);
49         } else
50                 retval = -ENOENT;
51
52         return retval;
53 }
54
55 static int i2c_pnx_suspend(struct platform_device *pdev, pm_message_t state)
56 {
57         int retval = 0;
58 #ifdef CONFIG_PM
59         retval = set_clock_run(pdev);
60 #endif
61         return retval;
62 }
63
64 static int i2c_pnx_resume(struct platform_device *pdev)
65 {
66         int retval = 0;
67 #ifdef CONFIG_PM
68         retval = set_clock_run(pdev);
69 #endif
70         return retval;
71 }
72
73 static u32 calculate_input_freq(struct platform_device *pdev)
74 {
75         return HCLK_MHZ;
76 }
77
78
79 static struct i2c_pnx_algo_data pnx_algo_data0 = {
80         .base = PNX4008_I2C1_BASE,
81         .irq = I2C_1_INT,
82 };
83
84 static struct i2c_pnx_algo_data pnx_algo_data1 = {
85         .base = PNX4008_I2C2_BASE,
86         .irq = I2C_2_INT,
87 };
88
89 static struct i2c_pnx_algo_data pnx_algo_data2 = {
90         .base = (PNX4008_USB_CONFIG_BASE + 0x300),
91         .irq = USB_I2C_INT,
92 };
93
94 static struct i2c_adapter pnx_adapter0 = {
95         .name = I2C_CHIP_NAME "0",
96         .algo_data = &pnx_algo_data0,
97 };
98 static struct i2c_adapter pnx_adapter1 = {
99         .name = I2C_CHIP_NAME "1",
100         .algo_data = &pnx_algo_data1,
101 };
102
103 static struct i2c_adapter pnx_adapter2 = {
104         .name = "USB-I2C",
105         .algo_data = &pnx_algo_data2,
106 };
107
108 static struct i2c_pnx_data i2c0_data = {
109         .suspend = i2c_pnx_suspend,
110         .resume = i2c_pnx_resume,
111         .calculate_input_freq = calculate_input_freq,
112         .set_clock_run = set_clock_run,
113         .set_clock_stop = set_clock_stop,
114         .adapter = &pnx_adapter0,
115 };
116
117 static struct i2c_pnx_data i2c1_data = {
118         .suspend = i2c_pnx_suspend,
119         .resume = i2c_pnx_resume,
120         .calculate_input_freq = calculate_input_freq,
121         .set_clock_run = set_clock_run,
122         .set_clock_stop = set_clock_stop,
123         .adapter = &pnx_adapter1,
124 };
125
126 static struct i2c_pnx_data i2c2_data = {
127         .suspend = i2c_pnx_suspend,
128         .resume = i2c_pnx_resume,
129         .calculate_input_freq = calculate_input_freq,
130         .set_clock_run = set_clock_run,
131         .set_clock_stop = set_clock_stop,
132         .adapter = &pnx_adapter2,
133 };
134
135 static struct platform_device i2c0_device = {
136         .name = "pnx-i2c",
137         .id = 0,
138         .dev = {
139                 .platform_data = &i2c0_data,
140         },
141 };
142
143 static struct platform_device i2c1_device = {
144         .name = "pnx-i2c",
145         .id = 1,
146         .dev = {
147                 .platform_data = &i2c1_data,
148         },
149 };
150
151 static struct platform_device i2c2_device = {
152         .name = "pnx-i2c",
153         .id = 2,
154         .dev = {
155                 .platform_data = &i2c2_data,
156         },
157 };
158
159 static struct platform_device *devices[] __initdata = {
160         &i2c0_device,
161         &i2c1_device,
162         &i2c2_device,
163 };
164
165 void __init pnx4008_register_i2c_devices(void)
166 {
167         platform_add_devices(devices, ARRAY_SIZE(devices));
168 }