1/*	$NetBSD: ziic.c,v 1.6 2021/08/07 16:19:08 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by NONAKA Kimihiro.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: ziic.c,v 1.6 2021/08/07 16:19:08 thorpej Exp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/device.h>
38#include <sys/bus.h>
39#include <sys/mutex.h>
40
41#include <dev/i2c/i2cvar.h>
42
43#include <arm/xscale/pxa2x0reg.h>
44#include <arm/xscale/pxa2x0var.h>
45#include <arm/xscale/pxa2x0_i2c.h>
46
47#ifdef PXAIIC_DEBUG
48#define	DPRINTF(s)	printf s
49#else
50#define	DPRINTF(s)	do { } while (/*CONSTCOND*/0)
51#endif
52
53struct pxaiic_softc {
54	struct pxa2x0_i2c_softc	sc_pxa_i2c;
55	void *			sc_ih;
56
57	struct i2c_controller	sc_i2c;
58};
59
60static int pxaiic_match(device_t, cfdata_t, void *);
61static void pxaiic_attach(device_t, device_t, void *);
62
63CFATTACH_DECL_NEW(pxaiic, sizeof(struct pxaiic_softc),
64    pxaiic_match, pxaiic_attach, NULL, NULL);
65
66static int pxaiic_acquire_bus(void *, int);
67static void pxaiic_release_bus(void *, int);
68static int pxaiic_send_start(void *, int);
69static int pxaiic_send_stop(void *, int);
70static int pxaiic_initiate_xfer(void *, uint16_t, int);
71static int pxaiic_read_byte(void *, uint8_t *, int);
72static int pxaiic_write_byte(void *, uint8_t, int);
73
74static int
75pxaiic_match(device_t parent, cfdata_t cf, void *aux)
76{
77	struct pxaip_attach_args *pxa = aux;
78
79	if (strcmp(cf->cf_name, pxa->pxa_name))
80		return 0;
81
82	pxa->pxa_addr = PXA2X0_I2C_BASE;
83	pxa->pxa_size = PXA2X0_I2C_SIZE;
84	return 1;
85}
86
87static void
88pxaiic_attach(device_t parent, device_t self, void *aux)
89{
90	struct pxaiic_softc *sc = device_private(self);
91	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
92	struct pxaip_attach_args *pxa = aux;
93	struct i2cbus_attach_args iba;
94
95	aprint_normal(": I2C controller\n");
96	aprint_naive("\n");
97
98	psc->sc_dev = self;
99	psc->sc_iot = pxa->pxa_iot;
100	psc->sc_addr = pxa->pxa_addr;
101	psc->sc_size = pxa->pxa_size;
102	psc->sc_flags = 0;
103	if (pxa2x0_i2c_attach_sub(psc)) {
104		aprint_error_dev(self, "unable to attach PXA I2C controller\n");
105		return;
106	}
107
108#if 0
109	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_TTY);
110
111	sc->sc_ih = pxa2x0_intr_establish(PXA2X0_INT_I2C, IPL_TTY,
112	    pxa2x0_i2c_intr, &psc);
113	if (sc->sc_ih == NULL) {
114		aprint_error_dev(self, "unable to establish intr\n");
115		return;	/* XXX: mutex_destroy, bus_space_unmap */
116	}
117#endif
118
119	/* Initialize i2c_controller */
120	iic_tag_init(&sc->sc_i2c);
121	sc->sc_i2c.ic_cookie = sc;
122	sc->sc_i2c.ic_acquire_bus = pxaiic_acquire_bus;
123	sc->sc_i2c.ic_release_bus = pxaiic_release_bus;
124	sc->sc_i2c.ic_send_start = pxaiic_send_start;
125	sc->sc_i2c.ic_send_stop = pxaiic_send_stop;
126	sc->sc_i2c.ic_initiate_xfer = pxaiic_initiate_xfer;
127	sc->sc_i2c.ic_read_byte = pxaiic_read_byte;
128	sc->sc_i2c.ic_write_byte = pxaiic_write_byte;
129
130	memset(&iba, 0, sizeof(iba));
131	iba.iba_tag = &sc->sc_i2c;
132	config_found(psc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
133}
134
135static int
136pxaiic_acquire_bus(void *cookie, int flags)
137{
138	struct pxaiic_softc *sc = cookie;
139	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
140
141	pxa2x0_i2c_open(psc);
142
143	return 0;
144}
145
146static void
147pxaiic_release_bus(void *cookie, int flags)
148{
149	struct pxaiic_softc *sc = cookie;
150	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
151
152	pxa2x0_i2c_close(psc);
153}
154
155static int
156pxaiic_send_start(void *cookie, int flags)
157{
158	struct pxaiic_softc *sc = cookie;
159	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
160
161	return pxa2x0_i2c_send_start(psc, flags);
162}
163
164static int
165pxaiic_send_stop(void *cookie, int flags)
166{
167	struct pxaiic_softc *sc = cookie;
168	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
169
170	return pxa2x0_i2c_send_stop(psc, flags);
171}
172
173static int
174pxaiic_initiate_xfer(void *cookie, uint16_t addr, int flags)
175{
176	struct pxaiic_softc *sc = cookie;
177	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
178
179	return pxa2x0_i2c_initiate_xfer(psc, addr, flags);
180}
181
182static int
183pxaiic_read_byte(void *cookie, uint8_t *bytep, int flags)
184{
185	struct pxaiic_softc *sc = cookie;
186	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
187
188	return pxa2x0_i2c_read_byte(psc, bytep, flags);
189}
190
191static int
192pxaiic_write_byte(void *cookie, uint8_t byte, int flags)
193{
194	struct pxaiic_softc *sc = cookie;
195	struct pxa2x0_i2c_softc *psc = &sc->sc_pxa_i2c;
196
197	return pxa2x0_i2c_write_byte(psc, byte, flags);
198}
199