ioexp.c revision 1.1 1 1.1 nonaka /* $NetBSD: ioexp.c,v 1.1 2011/06/19 16:20:09 nonaka Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*-
4 1.1 nonaka * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 nonaka * All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * This code is derived from software contributed to The NetBSD Foundation
8 1.1 nonaka * by NONAKA Kimihiro.
9 1.1 nonaka *
10 1.1 nonaka * Redistribution and use in source and binary forms, with or without
11 1.1 nonaka * modification, are permitted provided that the following conditions
12 1.1 nonaka * are met:
13 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
14 1.1 nonaka * notice, this list of conditions and the following disclaimer.
15 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
17 1.1 nonaka * documentation and/or other materials provided with the distribution.
18 1.1 nonaka *
19 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 nonaka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 nonaka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 nonaka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 nonaka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 nonaka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 nonaka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 nonaka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 nonaka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 nonaka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 nonaka * POSSIBILITY OF SUCH DAMAGE.
30 1.1 nonaka */
31 1.1 nonaka
32 1.1 nonaka #include <sys/cdefs.h>
33 1.1 nonaka __KERNEL_RCSID(0, "$NetBSD: ioexp.c,v 1.1 2011/06/19 16:20:09 nonaka Exp $");
34 1.1 nonaka
35 1.1 nonaka #include <sys/param.h>
36 1.1 nonaka #include <sys/systm.h>
37 1.1 nonaka #include <sys/device.h>
38 1.1 nonaka #include <sys/gpio.h>
39 1.1 nonaka
40 1.1 nonaka #include <dev/i2c/i2cvar.h>
41 1.1 nonaka
42 1.1 nonaka #include <arm/xscale/pxa2x0reg.h>
43 1.1 nonaka #include <arm/xscale/pxa2x0var.h>
44 1.1 nonaka #include <arm/xscale/pxa2x0_i2c.h>
45 1.1 nonaka
46 1.1 nonaka #include <zaurus/zaurus/zaurus_var.h>
47 1.1 nonaka #include <zaurus/dev/ioexpreg.h>
48 1.1 nonaka #include <zaurus/dev/ioexpvar.h>
49 1.1 nonaka
50 1.1 nonaka #include "ioconf.h"
51 1.1 nonaka
52 1.1 nonaka struct ioexp_softc {
53 1.1 nonaka device_t sc_dev;
54 1.1 nonaka i2c_tag_t sc_i2c;
55 1.1 nonaka
56 1.1 nonaka uint8_t sc_output;
57 1.1 nonaka uint8_t sc_direction;
58 1.1 nonaka
59 1.1 nonaka int sc_inited;
60 1.1 nonaka };
61 1.1 nonaka
62 1.1 nonaka static int ioexp_match(device_t, cfdata_t, void *);
63 1.1 nonaka static void ioexp_attach(device_t, device_t, void *);
64 1.1 nonaka
65 1.1 nonaka CFATTACH_DECL_NEW(ioexp, sizeof(struct ioexp_softc),
66 1.1 nonaka ioexp_match, ioexp_attach, NULL, NULL);
67 1.1 nonaka
68 1.1 nonaka static uint8_t output_init_value = IOEXP_IR_ON | IOEXP_AKIN_PULLUP;
69 1.1 nonaka static uint8_t direction_init_value = 0;
70 1.1 nonaka
71 1.1 nonaka static __inline int
72 1.1 nonaka ioexp_write(struct ioexp_softc *sc, uint8_t reg, uint8_t val)
73 1.1 nonaka {
74 1.1 nonaka uint8_t cmd;
75 1.1 nonaka uint8_t data;
76 1.1 nonaka int error;
77 1.1 nonaka
78 1.1 nonaka cmd = reg;
79 1.1 nonaka data = val;
80 1.1 nonaka error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, IOEXP_ADDRESS,
81 1.1 nonaka &cmd, 1, &data, 1, 0);
82 1.1 nonaka return error;
83 1.1 nonaka }
84 1.1 nonaka
85 1.1 nonaka static int
86 1.1 nonaka ioexp_match(device_t parent, cfdata_t cf, void *aux)
87 1.1 nonaka {
88 1.1 nonaka struct i2c_attach_args *ia = aux;
89 1.1 nonaka
90 1.1 nonaka /* only for SL-C1000 */
91 1.1 nonaka if (!ZAURUS_ISC1000)
92 1.1 nonaka return 0;
93 1.1 nonaka
94 1.1 nonaka if (ia->ia_name) {
95 1.1 nonaka /* direct config - check name */
96 1.1 nonaka if (strcmp(ia->ia_name, "ioexp") == 0)
97 1.1 nonaka return 1;
98 1.1 nonaka } else {
99 1.1 nonaka /* indirect config - check typical address */
100 1.1 nonaka if (ia->ia_addr == IOEXP_ADDRESS)
101 1.1 nonaka return 1;
102 1.1 nonaka }
103 1.1 nonaka return 0;
104 1.1 nonaka }
105 1.1 nonaka
106 1.1 nonaka static void
107 1.1 nonaka ioexp_attach(device_t parent, device_t self, void *aux)
108 1.1 nonaka {
109 1.1 nonaka struct ioexp_softc *sc = device_private(self);
110 1.1 nonaka struct i2c_attach_args *ia = aux;
111 1.1 nonaka
112 1.1 nonaka sc->sc_dev = self;
113 1.1 nonaka sc->sc_i2c = ia->ia_tag;
114 1.1 nonaka
115 1.1 nonaka aprint_normal(": GPIO controller\n");
116 1.1 nonaka aprint_naive("\n");
117 1.1 nonaka
118 1.1 nonaka sc->sc_output = output_init_value;
119 1.1 nonaka sc->sc_direction = direction_init_value;
120 1.1 nonaka
121 1.1 nonaka iic_acquire_bus(sc->sc_i2c, 0);
122 1.1 nonaka ioexp_write(sc, IOEXP_POLARITY, 0);
123 1.1 nonaka ioexp_write(sc, IOEXP_OUTPUT, sc->sc_output);
124 1.1 nonaka ioexp_write(sc, IOEXP_DIRECTION, sc->sc_direction);
125 1.1 nonaka iic_release_bus(sc->sc_i2c, 0);
126 1.1 nonaka
127 1.1 nonaka sc->sc_inited = 1;
128 1.1 nonaka }
129 1.1 nonaka
130 1.1 nonaka #if 0
131 1.1 nonaka static void
132 1.1 nonaka ioexp_gpio_pin_ctl(struct ioexp_softc *sc, uint8_t bit, int flags,
133 1.1 nonaka bool acquire_bus)
134 1.1 nonaka {
135 1.1 nonaka int error;
136 1.1 nonaka
137 1.1 nonaka if (acquire_bus) {
138 1.1 nonaka error = iic_acquire_bus(sc->sc_i2c, 0);
139 1.1 nonaka if (error) {
140 1.1 nonaka aprint_error_dev(sc->sc_dev,
141 1.1 nonaka "unable to acquire bus. error=%d\n", error);
142 1.1 nonaka return;
143 1.1 nonaka }
144 1.1 nonaka }
145 1.1 nonaka
146 1.1 nonaka switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
147 1.1 nonaka case GPIO_PIN_INPUT:
148 1.1 nonaka sc->sc_direction |= bit;
149 1.1 nonaka break;
150 1.1 nonaka case GPIO_PIN_OUTPUT:
151 1.1 nonaka sc->sc_direction &= ~bit;
152 1.1 nonaka break;
153 1.1 nonaka }
154 1.1 nonaka error = ioexp_write(sc, IOEXP_DIRECTION, sc->sc_direction);
155 1.1 nonaka if (error)
156 1.1 nonaka aprint_error_dev(sc->sc_dev,
157 1.1 nonaka "direction write failed. error=%d\n", error);
158 1.1 nonaka
159 1.1 nonaka if (acquire_bus)
160 1.1 nonaka iic_release_bus(sc->sc_i2c, 0);
161 1.1 nonaka }
162 1.1 nonaka #endif
163 1.1 nonaka
164 1.1 nonaka static void
165 1.1 nonaka ioexp_gpio_pin_write(struct ioexp_softc *sc, uint8_t bit, int level,
166 1.1 nonaka bool acquire_bus)
167 1.1 nonaka {
168 1.1 nonaka int error;
169 1.1 nonaka
170 1.1 nonaka if (acquire_bus) {
171 1.1 nonaka error = iic_acquire_bus(sc->sc_i2c, 0);
172 1.1 nonaka if (error) {
173 1.1 nonaka aprint_error_dev(sc->sc_dev,
174 1.1 nonaka "unable to acquire bus. error=%d\n", error);
175 1.1 nonaka return;
176 1.1 nonaka }
177 1.1 nonaka }
178 1.1 nonaka
179 1.1 nonaka if (level == GPIO_PIN_LOW)
180 1.1 nonaka sc->sc_output &= ~bit;
181 1.1 nonaka else
182 1.1 nonaka sc->sc_output |= bit;
183 1.1 nonaka error = ioexp_write(sc, IOEXP_OUTPUT, sc->sc_output);
184 1.1 nonaka if (error)
185 1.1 nonaka aprint_error_dev(sc->sc_dev,
186 1.1 nonaka "output write failed. error=%d\n", error);
187 1.1 nonaka
188 1.1 nonaka if (acquire_bus)
189 1.1 nonaka iic_release_bus(sc->sc_i2c, 0);
190 1.1 nonaka }
191 1.1 nonaka
192 1.1 nonaka static __inline uint8_t
193 1.1 nonaka ioexp_gpio_pin_get(struct ioexp_softc *sc, uint8_t bit)
194 1.1 nonaka {
195 1.1 nonaka
196 1.1 nonaka return sc->sc_output & bit;
197 1.1 nonaka }
198 1.1 nonaka
199 1.1 nonaka /*
200 1.1 nonaka * Turn the LCD background light and contrast signal on or off.
201 1.1 nonaka */
202 1.1 nonaka void
203 1.1 nonaka ioexp_set_backlight(int onoff, int cont)
204 1.1 nonaka {
205 1.1 nonaka struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);
206 1.1 nonaka
207 1.1 nonaka if (sc == NULL || !sc->sc_inited) {
208 1.1 nonaka #ifdef DEBUG
209 1.1 nonaka aprint_error("ioexp: %s: not attached or not inited\n",
210 1.1 nonaka __func__);
211 1.1 nonaka #endif
212 1.1 nonaka if (onoff)
213 1.1 nonaka output_init_value |= IOEXP_BACKLIGHT_ON;
214 1.1 nonaka else
215 1.1 nonaka output_init_value &= ~IOEXP_BACKLIGHT_ON;
216 1.1 nonaka /* BACKLIGHT_CONT is inverted */
217 1.1 nonaka if (cont)
218 1.1 nonaka output_init_value &= ~IOEXP_BACKLIGHT_CONT;
219 1.1 nonaka else
220 1.1 nonaka output_init_value |= IOEXP_BACKLIGHT_CONT;
221 1.1 nonaka return;
222 1.1 nonaka }
223 1.1 nonaka
224 1.1 nonaka if (sc != NULL) {
225 1.1 nonaka uint8_t bkreg = ioexp_gpio_pin_get(sc, IOEXP_BACKLIGHT_ON);
226 1.1 nonaka uint8_t contreg = ioexp_gpio_pin_get(sc, IOEXP_BACKLIGHT_CONT);
227 1.1 nonaka
228 1.1 nonaka if (onoff && !bkreg) {
229 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_ON,
230 1.1 nonaka GPIO_PIN_HIGH, true);
231 1.1 nonaka } else if (!onoff && bkreg) {
232 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_ON,
233 1.1 nonaka GPIO_PIN_LOW, true);
234 1.1 nonaka }
235 1.1 nonaka
236 1.1 nonaka /* BACKLIGHT_CONT is inverted */
237 1.1 nonaka if (cont && contreg) {
238 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_CONT,
239 1.1 nonaka GPIO_PIN_LOW, true);
240 1.1 nonaka } else if (!cont && !contreg) {
241 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_CONT,
242 1.1 nonaka GPIO_PIN_HIGH, true);
243 1.1 nonaka }
244 1.1 nonaka }
245 1.1 nonaka }
246 1.1 nonaka
247 1.1 nonaka /*
248 1.1 nonaka * Turn the infrared LED on or off (must be on while transmitting).
249 1.1 nonaka */
250 1.1 nonaka void
251 1.1 nonaka ioexp_set_irled(int onoff)
252 1.1 nonaka {
253 1.1 nonaka struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);
254 1.1 nonaka
255 1.1 nonaka if (sc == NULL || !sc->sc_inited) {
256 1.1 nonaka #ifdef DEBUG
257 1.1 nonaka aprint_error("ioexp: %s: not attached or not inited\n",
258 1.1 nonaka __func__);
259 1.1 nonaka #endif
260 1.1 nonaka /* IR_ON is inverted */
261 1.1 nonaka if (onoff)
262 1.1 nonaka output_init_value &= ~IOEXP_IR_ON;
263 1.1 nonaka else
264 1.1 nonaka output_init_value |= IOEXP_IR_ON;
265 1.1 nonaka return;
266 1.1 nonaka }
267 1.1 nonaka
268 1.1 nonaka if (sc != NULL) {
269 1.1 nonaka /* IR_ON is inverted */
270 1.1 nonaka uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_IR_ON);
271 1.1 nonaka if (onoff && reg) {
272 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_IR_ON, GPIO_PIN_LOW,
273 1.1 nonaka true);
274 1.1 nonaka } else if (!onoff && !reg) {
275 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_IR_ON, GPIO_PIN_HIGH,
276 1.1 nonaka true);
277 1.1 nonaka }
278 1.1 nonaka }
279 1.1 nonaka }
280 1.1 nonaka
281 1.1 nonaka /*
282 1.1 nonaka * Enable or disable the mic bias
283 1.1 nonaka */
284 1.1 nonaka void
285 1.1 nonaka ioexp_set_mic_bias(int onoff)
286 1.1 nonaka {
287 1.1 nonaka struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);
288 1.1 nonaka
289 1.1 nonaka if (sc == NULL || !sc->sc_inited) {
290 1.1 nonaka #ifdef DEBUG
291 1.1 nonaka aprint_error("ioexp: %s: not attached or not inited\n",
292 1.1 nonaka __func__);
293 1.1 nonaka #endif
294 1.1 nonaka if (onoff)
295 1.1 nonaka output_init_value |= IOEXP_MIC_BIAS;
296 1.1 nonaka else
297 1.1 nonaka output_init_value &= ~IOEXP_MIC_BIAS;
298 1.1 nonaka return;
299 1.1 nonaka }
300 1.1 nonaka
301 1.1 nonaka if (sc != NULL) {
302 1.1 nonaka uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_MIC_BIAS);
303 1.1 nonaka if (onoff && !reg) {
304 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_MIC_BIAS, GPIO_PIN_HIGH,
305 1.1 nonaka false);
306 1.1 nonaka } else if (!onoff && reg) {
307 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_MIC_BIAS, GPIO_PIN_LOW,
308 1.1 nonaka false);
309 1.1 nonaka }
310 1.1 nonaka }
311 1.1 nonaka }
312 1.1 nonaka
313 1.1 nonaka /*
314 1.1 nonaka * Turn on pullup resistor while not reading the remote control.
315 1.1 nonaka */
316 1.1 nonaka void
317 1.1 nonaka ioexp_akin_pullup(int onoff)
318 1.1 nonaka {
319 1.1 nonaka struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);
320 1.1 nonaka
321 1.1 nonaka if (sc == NULL || !sc->sc_inited) {
322 1.1 nonaka #ifdef DEBUG
323 1.1 nonaka aprint_error("ioexp: %s: not attached or not inited\n",
324 1.1 nonaka __func__);
325 1.1 nonaka #endif
326 1.1 nonaka if (onoff)
327 1.1 nonaka output_init_value |= IOEXP_AKIN_PULLUP;
328 1.1 nonaka else
329 1.1 nonaka output_init_value &= ~IOEXP_AKIN_PULLUP;
330 1.1 nonaka return;
331 1.1 nonaka }
332 1.1 nonaka
333 1.1 nonaka if (sc != NULL) {
334 1.1 nonaka uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_AKIN_PULLUP);
335 1.1 nonaka if (onoff && !reg) {
336 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_AKIN_PULLUP,
337 1.1 nonaka GPIO_PIN_HIGH, true);
338 1.1 nonaka } else if (!onoff && reg) {
339 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_AKIN_PULLUP,
340 1.1 nonaka GPIO_PIN_LOW, true);
341 1.1 nonaka }
342 1.1 nonaka }
343 1.1 nonaka }
344