summaryrefslogtreecommitdiffstats
path: root/sys/arm/amlogic/aml8726/aml8726_i2c.c
blob: c47bb6eec8006ebc25766742ce5810e76ea5fecf (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
/*-
 * Copyright 2013-2015 John Wehle <john@feith.com>
 * 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 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 AUTHOR 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.
 */

/*
 * Amlogic aml8726 I2C driver.
 *
 * Currently this implementation doesn't take full advantage of the hardware.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/rman.h>

#include <machine/bus.h>

#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <dev/iicbus/iiconf.h>
#include <dev/iicbus/iicbus.h>

#include "iicbb_if.h"


struct aml8726_iic_softc {
	device_t	dev;
	struct resource	*res[1];
	struct mtx	mtx;
	device_t	iicbb;
};

static struct resource_spec aml8726_iic_spec[] = {
	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
	{ -1, 0 }
};

#define	AML_I2C_LOCK(sc)		mtx_lock(&(sc)->mtx)
#define	AML_I2C_UNLOCK(sc)		mtx_unlock(&(sc)->mtx)
#define	AML_I2C_LOCK_INIT(sc)		\
    mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev),	\
    "i2c", MTX_DEF)
#define	AML_I2C_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->mtx);

#define	AML_I2C_CTRL_REG		0
#define	AML_I2C_MANUAL_SDA_I		(1 << 26)
#define	AML_I2C_MANUAL_SCL_I		(1 << 25)
#define	AML_I2C_MANUAL_SDA_O		(1 << 24)
#define	AML_I2C_MANUAL_SCL_O		(1 << 23)
#define	AML_I2C_MANUAL_EN		(1 << 22)

#define	CSR_WRITE_4(sc, reg, val)	bus_write_4((sc)->res[0], reg, (val))
#define	CSR_READ_4(sc, reg)		bus_read_4((sc)->res[0], reg)

static int
aml8726_iic_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (!ofw_bus_is_compatible(dev, "amlogic,meson6-i2c"))
		return (ENXIO);

	device_set_desc(dev, "Amlogic aml8726 I2C");

	return (BUS_PROBE_DEFAULT);
}

static int
aml8726_iic_attach(device_t dev)
{
	struct aml8726_iic_softc *sc = device_get_softc(dev);
	int error;

	sc->dev = dev;

	if (bus_alloc_resources(dev, aml8726_iic_spec, sc->res)) {
		device_printf(dev, "can not allocate resources for device\n");
		return (ENXIO);
	}

	AML_I2C_LOCK_INIT(sc);

	sc->iicbb = device_add_child(dev, "iicbb", -1);

	if (sc->iicbb == NULL) {
		device_printf(dev, "could not add iicbb\n");
		error = ENXIO;
		goto fail;
	}

	error = device_probe_and_attach(sc->iicbb);

	if (error) {
		device_printf(dev, "could not attach iicbb\n");
		goto fail;
	}

	return (0);

fail:
	AML_I2C_LOCK_DESTROY(sc);
	bus_release_resources(dev, aml8726_iic_spec, sc->res);

	return (error);
}

static int
aml8726_iic_detach(device_t dev)
{
	struct aml8726_iic_softc *sc = device_get_softc(dev);
	device_t child;

	/*
	 * Detach the children before recursively deleting
	 * in case a child has a pointer to a grandchild
	 * which is used by the child's detach routine.
	 *
	 * Remember the child before detaching so we can
	 * delete it (bus_generic_detach indirectly zeroes
	 * sc->child_dev).
	 */
	child = sc->iicbb;
	bus_generic_detach(dev);
	if (child)
		device_delete_child(dev, child);

	AML_I2C_LOCK_DESTROY(sc);

	bus_release_resources(dev, aml8726_iic_spec, sc->res);

	return (0);
}

static void
aml8726_iic_child_detached(device_t dev, device_t child)
{
	struct aml8726_iic_softc *sc = device_get_softc(dev);

	if (child == sc->iicbb)
		sc->iicbb = NULL;
}

static int
aml8726_iic_callback(device_t dev, int index, caddr_t data)
{

	return (0);
}

static int
aml8726_iic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
{
	struct aml8726_iic_softc *sc = device_get_softc(dev);

	AML_I2C_LOCK(sc);

	CSR_WRITE_4(sc, AML_I2C_CTRL_REG,
	    (CSR_READ_4(sc, AML_I2C_CTRL_REG) | AML_I2C_MANUAL_SDA_O |
	    AML_I2C_MANUAL_SCL_O | AML_I2C_MANUAL_EN));

	AML_I2C_UNLOCK(sc);

	/* Wait for 10 usec */
	DELAY(10);

	return (IIC_ENOADDR);
}

static int
aml8726_iic_getscl(device_t dev)
{
	struct aml8726_iic_softc *sc = device_get_softc(dev);

	return (CSR_READ_4(sc, AML_I2C_CTRL_REG) & AML_I2C_MANUAL_SCL_I);
}

static int
aml8726_iic_getsda(device_t dev)
{
	struct aml8726_iic_softc *sc = device_get_softc(dev);

	return (CSR_READ_4(sc, AML_I2C_CTRL_REG) & AML_I2C_MANUAL_SDA_I);
}

static void
aml8726_iic_setscl(device_t dev, int val)
{
	struct aml8726_iic_softc *sc = device_get_softc(dev);

	AML_I2C_LOCK(sc);

	CSR_WRITE_4(sc, AML_I2C_CTRL_REG, ((CSR_READ_4(sc, AML_I2C_CTRL_REG) &
	    ~AML_I2C_MANUAL_SCL_O) | (val ? AML_I2C_MANUAL_SCL_O : 0) |
	    AML_I2C_MANUAL_EN));

	AML_I2C_UNLOCK(sc);
}

static void
aml8726_iic_setsda(device_t dev, int val)
{
	struct aml8726_iic_softc *sc = device_get_softc(dev);

	AML_I2C_LOCK(sc);

	CSR_WRITE_4(sc, AML_I2C_CTRL_REG, ((CSR_READ_4(sc, AML_I2C_CTRL_REG) &
	    ~AML_I2C_MANUAL_SDA_O) | (val ? AML_I2C_MANUAL_SDA_O : 0) |
	    AML_I2C_MANUAL_EN));

	AML_I2C_UNLOCK(sc);
}

static device_method_t aml8726_iic_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		aml8726_iic_probe),
	DEVMETHOD(device_attach,	aml8726_iic_attach),
	DEVMETHOD(device_detach,	aml8726_iic_detach),

	/* bus interface */
	DEVMETHOD(bus_child_detached,	aml8726_iic_child_detached),
	DEVMETHOD(bus_print_child,	bus_generic_print_child),
	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),

	/* IICBB interface */
	DEVMETHOD(iicbb_callback,	aml8726_iic_callback),
	DEVMETHOD(iicbb_reset,		aml8726_iic_reset),

	DEVMETHOD(iicbb_getscl,		aml8726_iic_getscl),
	DEVMETHOD(iicbb_getsda,		aml8726_iic_getsda),
	DEVMETHOD(iicbb_setscl,		aml8726_iic_setscl),
	DEVMETHOD(iicbb_setsda,		aml8726_iic_setsda),

	DEVMETHOD_END
};

static driver_t aml8726_iic_driver = {
	"aml8726_iic",
	aml8726_iic_methods,
	sizeof(struct aml8726_iic_softc),
};

static devclass_t aml8726_iic_devclass;

DRIVER_MODULE(aml8726_iic, simplebus, aml8726_iic_driver,
    aml8726_iic_devclass, 0, 0);
DRIVER_MODULE(iicbb, aml8726_iic, iicbb_driver, iicbb_devclass, 0, 0);
MODULE_DEPEND(aml8726_iic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
MODULE_VERSION(aml8726_iic, 1);
OpenPOWER on IntegriCloud