summaryrefslogtreecommitdiff
path: root/sys/arch/hpcarm/dev/nbpiic.c
blob: 40e627ef4f340c1dcdc8f2b3c339aac9187f2d28 (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
/*	$NetBSD: nbpiic.c,v 1.6 2022/06/18 22:11:00 andvar Exp $ */
/*
 * Copyright (c) 2011 KIYOHARA Takashi
 * All rights reserved.
 *
 * 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 AUTHOR ``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 AUTHOR 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: nbpiic.c,v 1.6 2022/06/18 22:11:00 andvar Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/mutex.h>

#include <machine/platid.h>
#include <machine/platid_mask.h>

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

#include <hpcarm/dev/nbppconvar.h>

#include <dev/i2c/i2cvar.h>

#include "nbppcon.h"

#define NBPIIC_SLAVE_ADDR	0x70

struct nbpiic_softc {
	struct pxa2x0_i2c_softc sc_pxa_i2c;

	struct i2c_controller sc_i2c;
	kmutex_t sc_lock;

	device_t sc_pcon;
	char sc_buf[16];
	int sc_idx;
};


static int pxaiic_match(device_t, cfdata_t, void *);
static void pxaiic_attach(device_t, device_t, void *);

static int nbpiic_intr(void *);
int nbpiic_poll(void *, int, char *);

/* functions for i2c_controller */
static int nbpiic_exec(void *cookie, i2c_op_t, i2c_addr_t, const void *, size_t,
    void *, size_t, int);

CFATTACH_DECL_NEW(pxaiic, sizeof(struct nbpiic_softc),
    pxaiic_match, pxaiic_attach, NULL, NULL);


/* ARGSUSED */
static int
pxaiic_match(device_t parent, cfdata_t match, void *aux)
{
	struct pxaip_attach_args *pxa = aux;

	if (strcmp(pxa->pxa_name, match->cf_name) != 0 ||
	    !platid_match(&platid, &platid_mask_MACH_PSIONTEKLOGIX_NETBOOK_PRO))
		return 0;

	pxa->pxa_size = PXA2X0_I2C_SIZE;
	return 1;
}

/* ARGSUSED */
static void
pxaiic_attach(device_t parent, device_t self, void *aux)
{
	struct nbpiic_softc *sc = device_private(self);
	struct pxaip_attach_args *pxa = aux;
	struct i2cbus_attach_args iba;
	void *ih;

	aprint_normal("\n");
	aprint_naive("\n");

	sc->sc_pxa_i2c.sc_dev = self;
	sc->sc_pxa_i2c.sc_iot = pxa->pxa_iot;
	sc->sc_pxa_i2c.sc_addr = pxa->pxa_addr;
	sc->sc_pxa_i2c.sc_size = pxa->pxa_size;
	sc->sc_pxa_i2c.sc_isar = NBPIIC_SLAVE_ADDR;
	sc->sc_pxa_i2c.sc_stat = PI2C_STAT_INIT;
	if (pxa2x0_i2c_attach_sub(&sc->sc_pxa_i2c)) {
		aprint_error_dev(self, "unable to attach PXA I2C\n");
		return;
	}

	/*
	 * Initialize mutex with IPL_HIGH.  Keyboard was connected to us.
	 * This is orthogonal to the lock held at the i2c layer; this
	 * is just to interlock us with the keyboard interrupt.
	 */
	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);

	ih = pxa2x0_intr_establish(pxa->pxa_intr, IPL_HIGH, nbpiic_intr, sc);
	if (ih == NULL) {
		aprint_error_dev(self, "intr_establish failed\n");
		return;
	}

	/* Initialize i2c_controller  */
	iic_tag_init(&sc->sc_i2c);
	sc->sc_i2c.ic_cookie = sc;
	sc->sc_i2c.ic_exec = nbpiic_exec;

	memset(&iba, 0, sizeof(iba));
	iba.iba_tag = &sc->sc_i2c;
	pxa2x0_i2c_open(&sc->sc_pxa_i2c);
	config_found(self, &iba, iicbus_print, CFARGS_NONE);

	sc->sc_pcon = device_find_by_xname("nbppcon0");

	/* Don't close I2C-bus.  We are slave device. */
}

static int
nbpiic_intr(void *arg)
{
	struct nbpiic_softc *sc = arg;
	struct pxa2x0_i2c_softc *pi2c = &sc->sc_pxa_i2c;
	int handled, len;

	handled = pxa2x0_i2c_intr_sub(pi2c, &len, &sc->sc_buf[sc->sc_idx]);
	if (len != 0)
		sc->sc_idx += len;
	if (pi2c->sc_stat == PI2C_STAT_STOP) {
#if NNBPPCON > 0
		nbppcon_intr(sc->sc_pcon, sc->sc_idx, sc->sc_buf);
#endif
		sc->sc_idx = 0;
		pi2c->sc_stat = PI2C_STAT_INIT;
	}

	return handled;
}

int
nbpiic_poll(void *cookie, int buflen, char *buf)
{
	struct nbpiic_softc *sc = cookie;
	int rv;

	if (sc->sc_idx > 0) {
		if (buflen < sc->sc_idx) {
			(void) pxa2x0_i2c_poll(&sc->sc_pxa_i2c,
			    sizeof(sc->sc_buf) - sc->sc_idx, buf + sc->sc_idx,
			    I2C_F_READ);
			sc->sc_idx = 0;
		} else
			memcpy(buf, sc->sc_buf, sc->sc_idx);
	}
	rv = pxa2x0_i2c_poll(&sc->sc_pxa_i2c,
	    buflen - sc->sc_idx, buf + sc->sc_idx, I2C_F_READ);
	sc->sc_idx = 0;

	return rv;
}

static int
nbpiic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
	    size_t cmdlen, void *vbuf, size_t buflen, int flags)
{
	struct nbpiic_softc *sc = cookie;
	int rv = -1;
	const u_char cmd = *(const u_char *)vcmd; 

	mutex_enter(&sc->sc_lock);

	if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1))
		rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c, addr, (u_char *)vbuf);

	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
		if (rv == 0)
			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
			    addr, (u_char *)vbuf);
	}

	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
printf("%s: read cmdlen=1, buflen=2: Ooops, maybe error...\n", __func__);
		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
		if (rv == 0)
			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
			    addr, &((u_char *)vbuf)[0]);
		if (rv == 0)
			rv = pxa2x0_i2c_read(&sc->sc_pxa_i2c,
			    addr, &((u_char *)vbuf)[1]);
	}

	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, *(u_char *)vbuf);

	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
		u_short v = (cmd << 8) | ((u_char *)vbuf)[0];

		rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c, addr, v);
	}

	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) {
printf("%s: write cmdlen=1, buflen=2: Ooops, maybe error...\n", __func__);
		rv = pxa2x0_i2c_write(&sc->sc_pxa_i2c, addr, cmd);
		if (rv == 0) {
			u_short v =
			    (((u_char *)vbuf)[0] << 8) | ((u_char *)vbuf)[1];

			rv = pxa2x0_i2c_write_2(&sc->sc_pxa_i2c, addr, v);
		}
	}

	/* Handle quick_read/quick_write ops - XXX Untested XXX */
	if ((cmdlen == 0) && (buflen == 0))
		rv = pxa2x0_i2c_quick(&sc->sc_pxa_i2c, addr,
			I2C_OP_READ_P(op) ? 1 : 0);

	mutex_exit(&sc->sc_lock);

	return rv;
}