ioexp.c revision 1.2 1 1.2 thorpej /* $NetBSD: ioexp.c,v 1.2 2018/06/16 21:22:13 thorpej 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.2 thorpej __KERNEL_RCSID(0, "$NetBSD: ioexp.c,v 1.2 2018/06/16 21:22:13 thorpej 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.2 thorpej int match_result;
90 1.1 nonaka
91 1.1 nonaka /* only for SL-C1000 */
92 1.1 nonaka if (!ZAURUS_ISC1000)
93 1.1 nonaka return 0;
94 1.1 nonaka
95 1.2 thorpej if (iic_use_direct_match(ia, cf, NULL, &match_result))
96 1.2 thorpej return match_result;
97 1.2 thorpej
98 1.2 thorpej /* indirect config - check typical address */
99 1.2 thorpej if (ia->ia_addr == IOEXP_ADDRESS)
100 1.2 thorpej return I2C_MATCH_ADDRESS_ONLY;
101 1.2 thorpej
102 1.1 nonaka return 0;
103 1.1 nonaka }
104 1.1 nonaka
105 1.1 nonaka static void
106 1.1 nonaka ioexp_attach(device_t parent, device_t self, void *aux)
107 1.1 nonaka {
108 1.1 nonaka struct ioexp_softc *sc = device_private(self);
109 1.1 nonaka struct i2c_attach_args *ia = aux;
110 1.1 nonaka
111 1.1 nonaka sc->sc_dev = self;
112 1.1 nonaka sc->sc_i2c = ia->ia_tag;
113 1.1 nonaka
114 1.1 nonaka aprint_normal(": GPIO controller\n");
115 1.1 nonaka aprint_naive("\n");
116 1.1 nonaka
117 1.1 nonaka sc->sc_output = output_init_value;
118 1.1 nonaka sc->sc_direction = direction_init_value;
119 1.1 nonaka
120 1.1 nonaka iic_acquire_bus(sc->sc_i2c, 0);
121 1.1 nonaka ioexp_write(sc, IOEXP_POLARITY, 0);
122 1.1 nonaka ioexp_write(sc, IOEXP_OUTPUT, sc->sc_output);
123 1.1 nonaka ioexp_write(sc, IOEXP_DIRECTION, sc->sc_direction);
124 1.1 nonaka iic_release_bus(sc->sc_i2c, 0);
125 1.1 nonaka
126 1.1 nonaka sc->sc_inited = 1;
127 1.1 nonaka }
128 1.1 nonaka
129 1.1 nonaka #if 0
130 1.1 nonaka static void
131 1.1 nonaka ioexp_gpio_pin_ctl(struct ioexp_softc *sc, uint8_t bit, int flags,
132 1.1 nonaka bool acquire_bus)
133 1.1 nonaka {
134 1.1 nonaka int error;
135 1.1 nonaka
136 1.1 nonaka if (acquire_bus) {
137 1.1 nonaka error = iic_acquire_bus(sc->sc_i2c, 0);
138 1.1 nonaka if (error) {
139 1.1 nonaka aprint_error_dev(sc->sc_dev,
140 1.1 nonaka "unable to acquire bus. error=%d\n", error);
141 1.1 nonaka return;
142 1.1 nonaka }
143 1.1 nonaka }
144 1.1 nonaka
145 1.1 nonaka switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
146 1.1 nonaka case GPIO_PIN_INPUT:
147 1.1 nonaka sc->sc_direction |= bit;
148 1.1 nonaka break;
149 1.1 nonaka case GPIO_PIN_OUTPUT:
150 1.1 nonaka sc->sc_direction &= ~bit;
151 1.1 nonaka break;
152 1.1 nonaka }
153 1.1 nonaka error = ioexp_write(sc, IOEXP_DIRECTION, sc->sc_direction);
154 1.1 nonaka if (error)
155 1.1 nonaka aprint_error_dev(sc->sc_dev,
156 1.1 nonaka "direction write failed. error=%d\n", error);
157 1.1 nonaka
158 1.1 nonaka if (acquire_bus)
159 1.1 nonaka iic_release_bus(sc->sc_i2c, 0);
160 1.1 nonaka }
161 1.1 nonaka #endif
162 1.1 nonaka
163 1.1 nonaka static void
164 1.1 nonaka ioexp_gpio_pin_write(struct ioexp_softc *sc, uint8_t bit, int level,
165 1.1 nonaka bool acquire_bus)
166 1.1 nonaka {
167 1.1 nonaka int error;
168 1.1 nonaka
169 1.1 nonaka if (acquire_bus) {
170 1.1 nonaka error = iic_acquire_bus(sc->sc_i2c, 0);
171 1.1 nonaka if (error) {
172 1.1 nonaka aprint_error_dev(sc->sc_dev,
173 1.1 nonaka "unable to acquire bus. error=%d\n", error);
174 1.1 nonaka return;
175 1.1 nonaka }
176 1.1 nonaka }
177 1.1 nonaka
178 1.1 nonaka if (level == GPIO_PIN_LOW)
179 1.1 nonaka sc->sc_output &= ~bit;
180 1.1 nonaka else
181 1.1 nonaka sc->sc_output |= bit;
182 1.1 nonaka error = ioexp_write(sc, IOEXP_OUTPUT, sc->sc_output);
183 1.1 nonaka if (error)
184 1.1 nonaka aprint_error_dev(sc->sc_dev,
185 1.1 nonaka "output write failed. error=%d\n", error);
186 1.1 nonaka
187 1.1 nonaka if (acquire_bus)
188 1.1 nonaka iic_release_bus(sc->sc_i2c, 0);
189 1.1 nonaka }
190 1.1 nonaka
191 1.1 nonaka static __inline uint8_t
192 1.1 nonaka ioexp_gpio_pin_get(struct ioexp_softc *sc, uint8_t bit)
193 1.1 nonaka {
194 1.1 nonaka
195 1.1 nonaka return sc->sc_output & bit;
196 1.1 nonaka }
197 1.1 nonaka
198 1.1 nonaka /*
199 1.1 nonaka * Turn the LCD background light and contrast signal on or off.
200 1.1 nonaka */
201 1.1 nonaka void
202 1.1 nonaka ioexp_set_backlight(int onoff, int cont)
203 1.1 nonaka {
204 1.1 nonaka struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);
205 1.1 nonaka
206 1.1 nonaka if (sc == NULL || !sc->sc_inited) {
207 1.1 nonaka #ifdef DEBUG
208 1.1 nonaka aprint_error("ioexp: %s: not attached or not inited\n",
209 1.1 nonaka __func__);
210 1.1 nonaka #endif
211 1.1 nonaka if (onoff)
212 1.1 nonaka output_init_value |= IOEXP_BACKLIGHT_ON;
213 1.1 nonaka else
214 1.1 nonaka output_init_value &= ~IOEXP_BACKLIGHT_ON;
215 1.1 nonaka /* BACKLIGHT_CONT is inverted */
216 1.1 nonaka if (cont)
217 1.1 nonaka output_init_value &= ~IOEXP_BACKLIGHT_CONT;
218 1.1 nonaka else
219 1.1 nonaka output_init_value |= IOEXP_BACKLIGHT_CONT;
220 1.1 nonaka return;
221 1.1 nonaka }
222 1.1 nonaka
223 1.1 nonaka if (sc != NULL) {
224 1.1 nonaka uint8_t bkreg = ioexp_gpio_pin_get(sc, IOEXP_BACKLIGHT_ON);
225 1.1 nonaka uint8_t contreg = ioexp_gpio_pin_get(sc, IOEXP_BACKLIGHT_CONT);
226 1.1 nonaka
227 1.1 nonaka if (onoff && !bkreg) {
228 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_ON,
229 1.1 nonaka GPIO_PIN_HIGH, true);
230 1.1 nonaka } else if (!onoff && bkreg) {
231 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_ON,
232 1.1 nonaka GPIO_PIN_LOW, true);
233 1.1 nonaka }
234 1.1 nonaka
235 1.1 nonaka /* BACKLIGHT_CONT is inverted */
236 1.1 nonaka if (cont && contreg) {
237 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_CONT,
238 1.1 nonaka GPIO_PIN_LOW, true);
239 1.1 nonaka } else if (!cont && !contreg) {
240 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_CONT,
241 1.1 nonaka GPIO_PIN_HIGH, true);
242 1.1 nonaka }
243 1.1 nonaka }
244 1.1 nonaka }
245 1.1 nonaka
246 1.1 nonaka /*
247 1.1 nonaka * Turn the infrared LED on or off (must be on while transmitting).
248 1.1 nonaka */
249 1.1 nonaka void
250 1.1 nonaka ioexp_set_irled(int onoff)
251 1.1 nonaka {
252 1.1 nonaka struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);
253 1.1 nonaka
254 1.1 nonaka if (sc == NULL || !sc->sc_inited) {
255 1.1 nonaka #ifdef DEBUG
256 1.1 nonaka aprint_error("ioexp: %s: not attached or not inited\n",
257 1.1 nonaka __func__);
258 1.1 nonaka #endif
259 1.1 nonaka /* IR_ON is inverted */
260 1.1 nonaka if (onoff)
261 1.1 nonaka output_init_value &= ~IOEXP_IR_ON;
262 1.1 nonaka else
263 1.1 nonaka output_init_value |= IOEXP_IR_ON;
264 1.1 nonaka return;
265 1.1 nonaka }
266 1.1 nonaka
267 1.1 nonaka if (sc != NULL) {
268 1.1 nonaka /* IR_ON is inverted */
269 1.1 nonaka uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_IR_ON);
270 1.1 nonaka if (onoff && reg) {
271 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_IR_ON, GPIO_PIN_LOW,
272 1.1 nonaka true);
273 1.1 nonaka } else if (!onoff && !reg) {
274 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_IR_ON, GPIO_PIN_HIGH,
275 1.1 nonaka true);
276 1.1 nonaka }
277 1.1 nonaka }
278 1.1 nonaka }
279 1.1 nonaka
280 1.1 nonaka /*
281 1.1 nonaka * Enable or disable the mic bias
282 1.1 nonaka */
283 1.1 nonaka void
284 1.1 nonaka ioexp_set_mic_bias(int onoff)
285 1.1 nonaka {
286 1.1 nonaka struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);
287 1.1 nonaka
288 1.1 nonaka if (sc == NULL || !sc->sc_inited) {
289 1.1 nonaka #ifdef DEBUG
290 1.1 nonaka aprint_error("ioexp: %s: not attached or not inited\n",
291 1.1 nonaka __func__);
292 1.1 nonaka #endif
293 1.1 nonaka if (onoff)
294 1.1 nonaka output_init_value |= IOEXP_MIC_BIAS;
295 1.1 nonaka else
296 1.1 nonaka output_init_value &= ~IOEXP_MIC_BIAS;
297 1.1 nonaka return;
298 1.1 nonaka }
299 1.1 nonaka
300 1.1 nonaka if (sc != NULL) {
301 1.1 nonaka uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_MIC_BIAS);
302 1.1 nonaka if (onoff && !reg) {
303 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_MIC_BIAS, GPIO_PIN_HIGH,
304 1.1 nonaka false);
305 1.1 nonaka } else if (!onoff && reg) {
306 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_MIC_BIAS, GPIO_PIN_LOW,
307 1.1 nonaka false);
308 1.1 nonaka }
309 1.1 nonaka }
310 1.1 nonaka }
311 1.1 nonaka
312 1.1 nonaka /*
313 1.1 nonaka * Turn on pullup resistor while not reading the remote control.
314 1.1 nonaka */
315 1.1 nonaka void
316 1.1 nonaka ioexp_akin_pullup(int onoff)
317 1.1 nonaka {
318 1.1 nonaka struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);
319 1.1 nonaka
320 1.1 nonaka if (sc == NULL || !sc->sc_inited) {
321 1.1 nonaka #ifdef DEBUG
322 1.1 nonaka aprint_error("ioexp: %s: not attached or not inited\n",
323 1.1 nonaka __func__);
324 1.1 nonaka #endif
325 1.1 nonaka if (onoff)
326 1.1 nonaka output_init_value |= IOEXP_AKIN_PULLUP;
327 1.1 nonaka else
328 1.1 nonaka output_init_value &= ~IOEXP_AKIN_PULLUP;
329 1.1 nonaka return;
330 1.1 nonaka }
331 1.1 nonaka
332 1.1 nonaka if (sc != NULL) {
333 1.1 nonaka uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_AKIN_PULLUP);
334 1.1 nonaka if (onoff && !reg) {
335 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_AKIN_PULLUP,
336 1.1 nonaka GPIO_PIN_HIGH, true);
337 1.1 nonaka } else if (!onoff && reg) {
338 1.1 nonaka ioexp_gpio_pin_write(sc, IOEXP_AKIN_PULLUP,
339 1.1 nonaka GPIO_PIN_LOW, true);
340 1.1 nonaka }
341 1.1 nonaka }
342 1.1 nonaka }
343