xref: /freebsd/sys/arm/allwinner/aw_ts.c (revision 7be8de4271d5cb5d441e2757912c1824f6c3dc3b)
1 /*-
2  * Copyright (c) 2016 Emmanuel Vadot <manu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Allwinner Touch Sreen driver
29  * Touch screen part is not done, only the thermal sensor part is.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/rman.h>
41 #include <sys/sysctl.h>
42 #include <machine/bus.h>
43 
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #define	READ(_sc, _r) bus_read_4((_sc)->res[0], (_r))
50 #define	WRITE(_sc, _r, _v) bus_write_4((_sc)->res[0], (_r), (_v))
51 
52 /* Control register 0 */
53 #define	TP_CTRL0	0x00
54 #define	 TP_CTRL0_TACQ(x)	((x & 0xFF) << 0)
55 #define	 TP_CTRL0_FS_DIV(x)	((x & 0xF) << 16)
56 #define	 TP_CTRL0_CLK_DIV(x)	((x & 0x3) << 20)
57 #define	 TP_CTRL0_CLK_SELECT(x)	((x & 0x1) << 22)
58 
59 /* Control register 1 */
60 #define	TP_CTRL1	0x04
61 #define	 TP_CTRL1_MODE_EN	(1 << 4)
62 
63 /* Control register 2 */
64 #define	TP_CTRL2	0x08
65 
66 /* Control register 3 */
67 #define	TP_CTRL3	0x0C
68 
69 /* Int/FIFO control register */
70 #define	TP_FIFOC	0x10
71 #define	 TP_FIFOC_TEMP_IRQ_ENABLE	(1 << 18)
72 
73 /* Int/FIFO status register */
74 #define	TP_FIFOS	0x14
75 #define	 TP_FIFOS_TEMP_IRQ_PENDING	(1 << 18)
76 
77 /* Temperature Period Register */
78 #define	TP_TPR		0x18
79 #define	 TP_TPR_TEMP_EN		(1 << 16)
80 #define	 TP_TPR_TEMP_PERIOD(x)	(x << 0)
81 
82 /* Common data register */
83 #define	TP_CDAT		0x1C
84 
85 /* Temperature data register */
86 #define	TEMP_DATA	0x20
87 
88 /* TP Data register*/
89 #define	TP_DATA		0x24
90 
91 /* TP IO config register */
92 #define	TP_IO_CONFIG	0x28
93 
94 /* TP IO port data register */
95 #define	TP_IO_DATA	0x2C
96 
97 struct aw_ts_softc {
98 	device_t		dev;
99 	struct resource *	res[2];
100 	void *			intrhand;
101 	int			temp_data;
102 	int			temp_offset;
103 	int			temp_step;
104 };
105 
106 static struct resource_spec aw_ts_spec[] = {
107 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
108 	{ SYS_RES_IRQ,		0,	RF_ACTIVE | RF_SHAREABLE },
109 	{ -1, 0 }
110 };
111 
112 #define	A10_TS	1
113 #define	A13_TS	2
114 
115 #define	AW_TS_TEMP_SYSCTL	1
116 
117 static struct ofw_compat_data compat_data[] = {
118 	{"allwinner,sun4i-a10-ts", A10_TS},
119 	{"allwinner,sun5i-a13-ts", A13_TS},
120 	{NULL,             0}
121 };
122 
123 static void
124 aw_ts_intr(void *arg)
125 {
126 	struct aw_ts_softc *sc;
127 	int val;
128 
129 	sc= (struct aw_ts_softc *)arg;
130 
131 	val = READ(sc, TP_FIFOS);
132 	if (val & TP_FIFOS_TEMP_IRQ_PENDING) {
133 		/* Convert the value to millicelsius then millikelvin */
134 		sc->temp_data = (READ(sc, TEMP_DATA) * sc->temp_step - sc->temp_offset)
135 			+ 273150;
136 	}
137 
138 	WRITE(sc, TP_FIFOS, val);
139 }
140 
141 static int
142 aw_ts_probe(device_t dev)
143 {
144 
145 	if (!ofw_bus_status_okay(dev))
146 		return (ENXIO);
147 
148 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
149 		return (ENXIO);
150 
151 	device_set_desc(dev, "Allwinner Touch Screen controller");
152 	return (BUS_PROBE_DEFAULT);
153 }
154 
155 static int
156 aw_ts_attach(device_t dev)
157 {
158 	struct aw_ts_softc *sc;
159 
160 	sc = device_get_softc(dev);
161 	sc->dev = dev;
162 
163 	if (bus_alloc_resources(dev, aw_ts_spec, sc->res) != 0) {
164 		device_printf(dev, "could not allocate memory resource\n");
165 		return (ENXIO);
166 	}
167 
168 	if (bus_setup_intr(dev, sc->res[1],
169 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, aw_ts_intr, sc,
170 	    &sc->intrhand)) {
171 		bus_release_resources(dev, aw_ts_spec, sc->res);
172 		device_printf(dev, "cannot setup interrupt handler\n");
173 		return (ENXIO);
174 	}
175 
176 	/*
177 	 * Thoses magic values were taken from linux which take them from
178 	 * the allwinner SDK or found them by deduction
179 	 */
180 	switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
181 	case A10_TS:
182 		sc->temp_offset = 257000;
183 		sc->temp_step = 133;
184 		break;
185 	case A13_TS:
186 		sc->temp_offset = 144700;
187 		sc->temp_step = 100;
188 		break;
189 	}
190 
191 	/* Enable clock and set divisers */
192 	WRITE(sc, TP_CTRL0, TP_CTRL0_CLK_SELECT(0) |
193 	  TP_CTRL0_CLK_DIV(2) |
194 	  TP_CTRL0_FS_DIV(7) |
195 	  TP_CTRL0_TACQ(63));
196 
197 	/* Enable TS module */
198 	WRITE(sc, TP_CTRL1, TP_CTRL1_MODE_EN);
199 
200 	/* Enable Temperature, period is ~2s */
201 	WRITE(sc, TP_TPR, TP_TPR_TEMP_EN | TP_TPR_TEMP_PERIOD(1953));
202 
203 	/* Enable temp irq */
204 	WRITE(sc, TP_FIFOC, TP_FIFOC_TEMP_IRQ_ENABLE);
205 
206 	/* Add sysctl */
207 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
208 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
209 	    OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD,
210 	    &sc->temp_data, 0, sysctl_handle_int,
211 	    "IK3", "CPU Temperature");
212 
213 	return (0);
214 }
215 
216 static device_method_t aw_ts_methods[] = {
217 	DEVMETHOD(device_probe, aw_ts_probe),
218 	DEVMETHOD(device_attach, aw_ts_attach),
219 
220 	DEVMETHOD_END
221 };
222 
223 static driver_t aw_ts_driver = {
224 	"aw_ts",
225 	aw_ts_methods,
226 	sizeof(struct aw_ts_softc),
227 };
228 static devclass_t aw_ts_devclass;
229 
230 DRIVER_MODULE(aw_ts, simplebus, aw_ts_driver, aw_ts_devclass, 0, 0);
231