Merge branch 'fix/hda' into for-linus
[pandora-kernel.git] / drivers / i2c / busses / i2c-simtec.c
1 /*
2  * Copyright (C) 2005 Simtec Electronics
3  *      Ben Dooks <ben@simtec.co.uk>
4  *
5  * Simtec Generic I2C Controller
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27
28 #include <linux/i2c.h>
29 #include <linux/i2c-algo-bit.h>
30
31 #include <asm/io.h>
32
33 struct simtec_i2c_data {
34         struct resource         *ioarea;
35         void __iomem            *reg;
36         struct i2c_adapter       adap;
37         struct i2c_algo_bit_data bit;
38 };
39
40 #define CMD_SET_SDA     (1<<2)
41 #define CMD_SET_SCL     (1<<3)
42
43 #define STATE_SDA       (1<<0)
44 #define STATE_SCL       (1<<1)
45
46 /* i2c bit-bus functions */
47
48 static void simtec_i2c_setsda(void *pw, int state)
49 {
50         struct simtec_i2c_data *pd = pw;
51         writeb(CMD_SET_SDA | (state ? STATE_SDA : 0), pd->reg);
52 }
53
54 static void simtec_i2c_setscl(void *pw, int state)
55 {
56         struct simtec_i2c_data *pd = pw;
57         writeb(CMD_SET_SCL | (state ? STATE_SCL : 0), pd->reg);
58 }
59
60 static int simtec_i2c_getsda(void *pw)
61 {
62         struct simtec_i2c_data *pd = pw;
63         return readb(pd->reg) & STATE_SDA ? 1 : 0;
64 }
65
66 static int simtec_i2c_getscl(void *pw)
67 {
68         struct simtec_i2c_data *pd = pw;
69         return readb(pd->reg) & STATE_SCL ? 1 : 0;
70 }
71
72 /* device registration */
73
74 static int simtec_i2c_probe(struct platform_device *dev)
75 {
76         struct simtec_i2c_data *pd;
77         struct resource *res;
78         int size;
79         int ret;
80
81         pd = kzalloc(sizeof(struct simtec_i2c_data), GFP_KERNEL);
82         if (pd == NULL) {
83                 dev_err(&dev->dev, "cannot allocate private data\n");
84                 return -ENOMEM;
85         }
86
87         platform_set_drvdata(dev, pd);
88
89         res = platform_get_resource(dev, IORESOURCE_MEM, 0);
90         if (res == NULL) {
91                 dev_err(&dev->dev, "cannot find IO resource\n");
92                 ret = -ENOENT;
93                 goto err;
94         }
95
96         size = resource_size(res);
97
98         pd->ioarea = request_mem_region(res->start, size, dev->name);
99         if (pd->ioarea == NULL) {
100                 dev_err(&dev->dev, "cannot request IO\n");
101                 ret = -ENXIO;
102                 goto err;
103         }
104
105         pd->reg = ioremap(res->start, size);
106         if (pd->reg == NULL) {
107                 dev_err(&dev->dev, "cannot map IO\n");
108                 ret = -ENXIO;
109                 goto err_res;
110         }
111
112         /* setup the private data */
113
114         pd->adap.owner = THIS_MODULE;
115         pd->adap.algo_data = &pd->bit;
116         pd->adap.dev.parent = &dev->dev;
117
118         strlcpy(pd->adap.name, "Simtec I2C", sizeof(pd->adap.name));
119
120         pd->bit.data = pd;
121         pd->bit.setsda = simtec_i2c_setsda;
122         pd->bit.setscl = simtec_i2c_setscl;
123         pd->bit.getsda = simtec_i2c_getsda;
124         pd->bit.getscl = simtec_i2c_getscl;
125         pd->bit.timeout = HZ;
126         pd->bit.udelay = 20;
127
128         ret = i2c_bit_add_bus(&pd->adap);
129         if (ret)
130                 goto err_all;
131
132         return 0;
133
134  err_all:
135         iounmap(pd->reg);
136
137  err_res:
138         release_resource(pd->ioarea);
139         kfree(pd->ioarea);
140
141  err:
142         kfree(pd);
143         return ret;
144 }
145
146 static int simtec_i2c_remove(struct platform_device *dev)
147 {
148         struct simtec_i2c_data *pd = platform_get_drvdata(dev);
149
150         i2c_del_adapter(&pd->adap);
151
152         iounmap(pd->reg);
153         release_resource(pd->ioarea);
154         kfree(pd->ioarea);
155         kfree(pd);
156
157         return 0;
158 }
159
160
161 /* device driver */
162
163 /* work with hotplug and coldplug */
164 MODULE_ALIAS("platform:simtec-i2c");
165
166 static struct platform_driver simtec_i2c_driver = {
167         .driver         = {
168                 .name           = "simtec-i2c",
169                 .owner          = THIS_MODULE,
170         },
171         .probe          = simtec_i2c_probe,
172         .remove         = simtec_i2c_remove,
173 };
174
175 static int __init i2c_adap_simtec_init(void)
176 {
177         return platform_driver_register(&simtec_i2c_driver);
178 }
179
180 static void __exit i2c_adap_simtec_exit(void)
181 {
182         platform_driver_unregister(&simtec_i2c_driver);
183 }
184
185 module_init(i2c_adap_simtec_init);
186 module_exit(i2c_adap_simtec_exit);
187
188 MODULE_DESCRIPTION("Simtec Generic I2C Bus driver");
189 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
190 MODULE_LICENSE("GPL");