pull master
[hh.org.git] / drivers / l3 / l3-bit-sa1100.c
blobc8244b035f829115085292a8ff00a913ef075b13
1 /*
2 * linux/drivers/l3/l3-bit-sa1100.c
4 * Copyright (C) 2001 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This is a combined I2C and L3 bus driver.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/ioport.h>
15 #include <linux/delay.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/i2c.h>
19 #include <linux/i2c-algo-bit.h>
20 #include <linux/l3/algo-bit.h>
22 #include <asm/system.h>
23 #include <asm/hardware.h>
24 #include <asm/mach-types.h>
25 #include <asm/arch/assabet.h>
27 #define NAME "l3-bit-sa1100-gpio"
29 struct bit_data {
30 unsigned int sda;
31 unsigned int scl;
32 unsigned int l3_mode;
35 static int getsda(void *data)
37 struct bit_data *bits = data;
39 return GPLR & bits->sda;
42 #ifdef CONFIG_I2C_BIT_SA1100_GPIO
43 static void i2c_setsda(void *data, int state)
45 struct bit_data *bits = data;
46 unsigned long flags;
48 local_irq_save(flags);
49 if (state)
50 GPDR &= ~bits->sda;
51 else {
52 GPCR = bits->sda;
53 GPDR |= bits->sda;
55 local_irq_restore(flags);
58 static void i2c_setscl(void *data, int state)
60 struct bit_data *bits = data;
61 unsigned long flags;
63 local_irq_save(flags);
64 if (state)
65 GPDR &= ~bits->scl;
66 else {
67 GPCR = bits->scl;
68 GPDR |= bits->scl;
70 local_irq_restore(flags);
73 static int i2c_getscl(void *data)
75 struct bit_data *bits = data;
77 return GPLR & bits->scl;
80 static struct i2c_algo_bit_data i2c_bit_data = {
81 .setsda = i2c_setsda,
82 .setscl = i2c_setscl,
83 .getsda = getsda,
84 .getscl = i2c_getscl,
85 .udelay = 10,
86 .mdelay = 10,
87 .timeout = 100,
90 static struct i2c_adapter i2c_adapter = {
91 .algo_data = &i2c_bit_data,
94 #define LOCK &i2c_adapter.bus_lock
96 static int __init i2c_init(struct bit_data *bits)
98 i2c_bit_data.data = bits;
99 return i2c_bit_add_bus(&i2c_adapter);
102 static void i2c_exit(void)
104 i2c_bit_del_bus(&i2c_adapter);
107 #else
108 static DECLARE_MUTEX(l3_lock);
109 #define LOCK &l3_lock
110 #define i2c_init(bits) (0)
111 #define i2c_exit() do { } while (0)
112 #endif
114 #ifdef CONFIG_L3_BIT_SA1100_GPIO
116 * iPAQs need the clock line driven hard high and low.
118 static void l3_setscl(void *data, int state)
120 struct bit_data *bits = data;
121 unsigned long flags;
123 local_irq_save(flags);
124 if (state)
125 GPSR = bits->scl;
126 else
127 GPCR = bits->scl;
128 GPDR |= bits->scl;
129 local_irq_restore(flags);
132 static void l3_setsda(void *data, int state)
134 struct bit_data *bits = data;
136 if (state)
137 GPSR = bits->sda;
138 else
139 GPCR = bits->sda;
142 static void l3_setdir(void *data, int in)
144 struct bit_data *bits = data;
145 unsigned long flags;
147 local_irq_save(flags);
148 if (in)
149 GPDR &= ~bits->sda;
150 else
151 GPDR |= bits->sda;
152 local_irq_restore(flags);
155 static void l3_setmode(void *data, int state)
157 struct bit_data *bits = data;
159 if (state)
160 GPSR = bits->l3_mode;
161 else
162 GPCR = bits->l3_mode;
165 static struct l3_algo_bit_data l3_bit_data = {
166 .data = NULL,
167 .setdat = l3_setsda,
168 .setclk = l3_setscl,
169 .setmode = l3_setmode,
170 .setdir = l3_setdir,
171 .getdat = getsda,
172 .data_hold = 1,
173 .data_setup = 1,
174 .clock_high = 1,
175 .mode_hold = 1,
176 .mode_setup = 1,
179 static struct l3_adapter l3_adapter = {
180 .owner = THIS_MODULE,
181 .name = NAME,
182 .algo_data = &l3_bit_data,
183 .lock = LOCK,
186 static int __init l3_init(struct bit_data *bits)
188 l3_bit_data.data = bits;
189 return l3_bit_add_bus(&l3_adapter);
192 static void __exit l3_exit(void)
194 l3_bit_del_bus(&l3_adapter);
196 #else
197 #define l3_init(bits) (0)
198 #define l3_exit() do { } while (0)
199 #endif
201 static struct bit_data bit_data;
203 static int __init bus_init(void)
205 struct bit_data *bit = &bit_data;
206 unsigned long flags;
207 int ret;
209 if (machine_is_assabet() || machine_is_pangolin()) {
210 bit->sda = GPIO_GPIO15;
211 bit->scl = GPIO_GPIO18;
212 bit->l3_mode = GPIO_GPIO17;
215 if (machine_is_h3600() || machine_is_h3100()) {
216 bit->sda = GPIO_GPIO14;
217 bit->scl = GPIO_GPIO16;
218 bit->l3_mode = GPIO_GPIO15;
221 if (machine_is_stork()) {
222 bit->sda = GPIO_GPIO15;
223 bit->scl = GPIO_GPIO18;
224 bit->l3_mode = GPIO_GPIO17;
227 if (!bit->sda)
228 return -ENODEV;
231 * Default level for L3 mode is low.
232 * We set SCL and SDA high (i2c idle state).
234 local_irq_save(flags);
235 GPDR &= ~(bit->scl | bit->sda);
236 GPCR = bit->l3_mode | bit->scl | bit->sda;
237 GPDR |= bit->l3_mode;
238 local_irq_restore(flags);
240 if (machine_is_assabet()) {
242 * Release reset on UCB1300, ADI7171 and UDA1341. We
243 * need to do this here so that we can communicate on
244 * the I2C/L3 buses.
246 ASSABET_BCR_set(ASSABET_BCR_CODEC_RST);
247 mdelay(1);
248 ASSABET_BCR_clear(ASSABET_BCR_CODEC_RST);
249 mdelay(1);
250 ASSABET_BCR_set(ASSABET_BCR_CODEC_RST);
253 ret = i2c_init(bit);
254 if (ret == 0 && bit->l3_mode) {
255 ret = l3_init(bit);
256 if (ret)
257 i2c_exit();
260 return ret;
263 static void __exit bus_exit(void)
265 l3_exit();
266 i2c_exit();
269 module_init(bus_init);
270 module_exit(bus_exit);