summaryrefslogtreecommitdiff
path: root/sys/arch/zaurus/dev/ioexp.c
blob: 223857d9dca20eb2addb08d63a3e5104038990c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*	$NetBSD: ioexp.c,v 1.2 2018/06/16 21:22:13 thorpej Exp $	*/

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by NONAKA Kimihiro.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ioexp.c,v 1.2 2018/06/16 21:22:13 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/gpio.h>

#include <dev/i2c/i2cvar.h>

#include <arm/xscale/pxa2x0reg.h>
#include <arm/xscale/pxa2x0var.h>
#include <arm/xscale/pxa2x0_i2c.h>

#include <zaurus/zaurus/zaurus_var.h>
#include <zaurus/dev/ioexpreg.h>
#include <zaurus/dev/ioexpvar.h>

#include "ioconf.h"

struct ioexp_softc {
	device_t	sc_dev;
	i2c_tag_t	sc_i2c;

	uint8_t		sc_output;
	uint8_t		sc_direction;

	int		sc_inited;
};

static int ioexp_match(device_t, cfdata_t, void *);
static void ioexp_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(ioexp, sizeof(struct ioexp_softc), 
    ioexp_match, ioexp_attach, NULL, NULL);

static uint8_t output_init_value = IOEXP_IR_ON | IOEXP_AKIN_PULLUP;
static uint8_t direction_init_value = 0;

static __inline int
ioexp_write(struct ioexp_softc *sc, uint8_t reg, uint8_t val)
{
	uint8_t cmd;
	uint8_t data;
	int error;

	cmd = reg;
	data = val;
	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, IOEXP_ADDRESS,
	    &cmd, 1, &data, 1, 0);
	return error;
}

static int
ioexp_match(device_t parent, cfdata_t cf, void *aux)
{
	struct i2c_attach_args *ia = aux;
	int match_result;

	/* only for SL-C1000 */
	if (!ZAURUS_ISC1000)
		return 0;

	if (iic_use_direct_match(ia, cf, NULL, &match_result))
		return match_result;
	
	/* indirect config - check typical address */
	if (ia->ia_addr == IOEXP_ADDRESS)
		return I2C_MATCH_ADDRESS_ONLY;

	return 0;
}

static void
ioexp_attach(device_t parent, device_t self, void *aux)
{
	struct ioexp_softc *sc = device_private(self);
	struct i2c_attach_args *ia = aux;

	sc->sc_dev = self;
	sc->sc_i2c = ia->ia_tag;

	aprint_normal(": GPIO controller\n");
	aprint_naive("\n");

	sc->sc_output = output_init_value;
	sc->sc_direction = direction_init_value;

	iic_acquire_bus(sc->sc_i2c, 0);
	ioexp_write(sc, IOEXP_POLARITY, 0);
	ioexp_write(sc, IOEXP_OUTPUT, sc->sc_output);
	ioexp_write(sc, IOEXP_DIRECTION, sc->sc_direction);
	iic_release_bus(sc->sc_i2c, 0);

	sc->sc_inited = 1;
}

#if 0
static void
ioexp_gpio_pin_ctl(struct ioexp_softc *sc, uint8_t bit, int flags,
    bool acquire_bus)
{
	int error;

	if (acquire_bus) {
		error = iic_acquire_bus(sc->sc_i2c, 0);
		if (error) {
			aprint_error_dev(sc->sc_dev,
			    "unable to acquire bus. error=%d\n", error);
			return;
		}
	}

	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
	case GPIO_PIN_INPUT:
		sc->sc_direction |= bit;
		break;
	case GPIO_PIN_OUTPUT:
		sc->sc_direction &= ~bit;
		break;
	}
	error = ioexp_write(sc, IOEXP_DIRECTION, sc->sc_direction);
	if (error)
		aprint_error_dev(sc->sc_dev,
		    "direction write failed. error=%d\n", error);

	if (acquire_bus)
		iic_release_bus(sc->sc_i2c, 0);
}
#endif

static void
ioexp_gpio_pin_write(struct ioexp_softc *sc, uint8_t bit, int level,
    bool acquire_bus)
{
	int error;

	if (acquire_bus) {
		error = iic_acquire_bus(sc->sc_i2c, 0);
		if (error) {
			aprint_error_dev(sc->sc_dev,
			    "unable to acquire bus. error=%d\n", error);
			return;
		}
	}

	if (level == GPIO_PIN_LOW)
		sc->sc_output &= ~bit;
	else
		sc->sc_output |= bit;
	error = ioexp_write(sc, IOEXP_OUTPUT, sc->sc_output);
	if (error)
		aprint_error_dev(sc->sc_dev,
		    "output write failed. error=%d\n", error);

	if (acquire_bus)
		iic_release_bus(sc->sc_i2c, 0);
}

static __inline uint8_t
ioexp_gpio_pin_get(struct ioexp_softc *sc, uint8_t bit)
{

	return sc->sc_output & bit;
}

/*
 * Turn the LCD background light and contrast signal on or off.
 */
void
ioexp_set_backlight(int onoff, int cont)
{
	struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);

	if (sc == NULL || !sc->sc_inited) {
#ifdef DEBUG
		aprint_error("ioexp: %s: not attached or not inited\n",
		    __func__);
#endif
		if (onoff)
			output_init_value |= IOEXP_BACKLIGHT_ON;
		else
			output_init_value &= ~IOEXP_BACKLIGHT_ON;
		/* BACKLIGHT_CONT is inverted */
		if (cont)
			output_init_value &= ~IOEXP_BACKLIGHT_CONT;
		else
			output_init_value |= IOEXP_BACKLIGHT_CONT;
		return;
	}

	if (sc != NULL) {
		uint8_t bkreg = ioexp_gpio_pin_get(sc, IOEXP_BACKLIGHT_ON);
		uint8_t contreg = ioexp_gpio_pin_get(sc, IOEXP_BACKLIGHT_CONT);

		if (onoff && !bkreg) {
			ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_ON,
			    GPIO_PIN_HIGH, true);
		} else if (!onoff && bkreg) {
			ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_ON,
			    GPIO_PIN_LOW, true);
		}

		/* BACKLIGHT_CONT is inverted */
		if (cont && contreg) {
			ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_CONT,
			    GPIO_PIN_LOW, true);
		} else if (!cont && !contreg) {
			ioexp_gpio_pin_write(sc, IOEXP_BACKLIGHT_CONT,
			    GPIO_PIN_HIGH, true);
		}
	}
}

/*
 * Turn the infrared LED on or off (must be on while transmitting).
 */
void
ioexp_set_irled(int onoff)
{
	struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);

	if (sc == NULL || !sc->sc_inited) {
#ifdef DEBUG
		aprint_error("ioexp: %s: not attached or not inited\n",
		    __func__);
#endif
		/* IR_ON is inverted */
		if (onoff)
			output_init_value &= ~IOEXP_IR_ON;
		else
			output_init_value |= IOEXP_IR_ON;
		return;
	}

	if (sc != NULL) {
		/* IR_ON is inverted */
		uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_IR_ON);
		if (onoff && reg) {
			ioexp_gpio_pin_write(sc, IOEXP_IR_ON, GPIO_PIN_LOW,
			    true);
		} else if (!onoff && !reg) {
			ioexp_gpio_pin_write(sc, IOEXP_IR_ON, GPIO_PIN_HIGH,
			    true);
		}
	}
}

/*
 * Enable or disable the mic bias
 */
void
ioexp_set_mic_bias(int onoff)
{
	struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);

	if (sc == NULL || !sc->sc_inited) {
#ifdef DEBUG
		aprint_error("ioexp: %s: not attached or not inited\n",
		    __func__);
#endif
		if (onoff)
			output_init_value |= IOEXP_MIC_BIAS;
		else
			output_init_value &= ~IOEXP_MIC_BIAS;
		return;
	}

	if (sc != NULL) {
		uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_MIC_BIAS);
		if (onoff && !reg) {
			ioexp_gpio_pin_write(sc, IOEXP_MIC_BIAS, GPIO_PIN_HIGH,
			    false);
		} else if (!onoff && reg) {
			ioexp_gpio_pin_write(sc, IOEXP_MIC_BIAS, GPIO_PIN_LOW,
			    false);
		}
	}
}

/*
 * Turn on pullup resistor while not reading the remote control.
 */
void
ioexp_akin_pullup(int onoff)
{
	struct ioexp_softc *sc = device_lookup_private(&ioexp_cd, 0);

	if (sc == NULL || !sc->sc_inited) {
#ifdef DEBUG
		aprint_error("ioexp: %s: not attached or not inited\n",
		    __func__);
#endif
		if (onoff)
			output_init_value |= IOEXP_AKIN_PULLUP;
		else
			output_init_value &= ~IOEXP_AKIN_PULLUP;
		return;
	}

	if (sc != NULL) {
		uint8_t reg = ioexp_gpio_pin_get(sc, IOEXP_AKIN_PULLUP);
		if (onoff && !reg) {
			ioexp_gpio_pin_write(sc, IOEXP_AKIN_PULLUP,
			    GPIO_PIN_HIGH, true);
		} else if (!onoff && reg) {
			ioexp_gpio_pin_write(sc, IOEXP_AKIN_PULLUP,
			    GPIO_PIN_LOW, true);
		}
	}
}