xref: /freebsd/sys/arm/ti/ti_adc.c (revision d412c07617eb35435668b024bc2cecda05f57f1f)
1855feb68SLuiz Otavio O Souza /*-
2855feb68SLuiz Otavio O Souza  * Copyright 2014 Luiz Otavio O Souza <loos@freebsd.org>
3855feb68SLuiz Otavio O Souza  * All rights reserved.
4855feb68SLuiz Otavio O Souza  *
5855feb68SLuiz Otavio O Souza  * Redistribution and use in source and binary forms, with or without
6855feb68SLuiz Otavio O Souza  * modification, are permitted provided that the following conditions
7855feb68SLuiz Otavio O Souza  * are met:
8855feb68SLuiz Otavio O Souza  * 1. Redistributions of source code must retain the above copyright
9855feb68SLuiz Otavio O Souza  *    notice, this list of conditions and the following disclaimer.
10855feb68SLuiz Otavio O Souza  * 2. Redistributions in binary form must reproduce the above copyright
11855feb68SLuiz Otavio O Souza  *    notice, this list of conditions and the following disclaimer in the
12855feb68SLuiz Otavio O Souza  *    documentation and/or other materials provided with the distribution.
13855feb68SLuiz Otavio O Souza  *
14855feb68SLuiz Otavio O Souza  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15855feb68SLuiz Otavio O Souza  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16855feb68SLuiz Otavio O Souza  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17855feb68SLuiz Otavio O Souza  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18855feb68SLuiz Otavio O Souza  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19855feb68SLuiz Otavio O Souza  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20855feb68SLuiz Otavio O Souza  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21855feb68SLuiz Otavio O Souza  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22855feb68SLuiz Otavio O Souza  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23855feb68SLuiz Otavio O Souza  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24855feb68SLuiz Otavio O Souza  * SUCH DAMAGE.
25855feb68SLuiz Otavio O Souza  */
26855feb68SLuiz Otavio O Souza 
27855feb68SLuiz Otavio O Souza #include <sys/cdefs.h>
28fbeb453bSOleksandr Tymoshenko #include "opt_evdev.h"
29fbeb453bSOleksandr Tymoshenko 
30855feb68SLuiz Otavio O Souza #include <sys/param.h>
31855feb68SLuiz Otavio O Souza #include <sys/systm.h>
32855feb68SLuiz Otavio O Souza #include <sys/bus.h>
33855feb68SLuiz Otavio O Souza 
34651f7544SOleksandr Tymoshenko #include <sys/conf.h>
35855feb68SLuiz Otavio O Souza #include <sys/kernel.h>
36855feb68SLuiz Otavio O Souza #include <sys/limits.h>
37855feb68SLuiz Otavio O Souza #include <sys/lock.h>
38855feb68SLuiz Otavio O Souza #include <sys/module.h>
39855feb68SLuiz Otavio O Souza #include <sys/mutex.h>
40651f7544SOleksandr Tymoshenko #include <sys/condvar.h>
41855feb68SLuiz Otavio O Souza #include <sys/resource.h>
42855feb68SLuiz Otavio O Souza #include <sys/rman.h>
43855feb68SLuiz Otavio O Souza #include <sys/sysctl.h>
44651f7544SOleksandr Tymoshenko #include <sys/selinfo.h>
45651f7544SOleksandr Tymoshenko #include <sys/poll.h>
46651f7544SOleksandr Tymoshenko #include <sys/uio.h>
47855feb68SLuiz Otavio O Souza 
48855feb68SLuiz Otavio O Souza #include <machine/bus.h>
49855feb68SLuiz Otavio O Souza 
50855feb68SLuiz Otavio O Souza #include <dev/ofw/openfirm.h>
51855feb68SLuiz Otavio O Souza #include <dev/ofw/ofw_bus.h>
52855feb68SLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h>
53855feb68SLuiz Otavio O Souza 
54a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
55fbeb453bSOleksandr Tymoshenko #include <dev/evdev/input.h>
56fbeb453bSOleksandr Tymoshenko #include <dev/evdev/evdev.h>
57fbeb453bSOleksandr Tymoshenko #endif
58fbeb453bSOleksandr Tymoshenko 
590050ea24SMichal Meloun #include <arm/ti/ti_sysc.h>
60855feb68SLuiz Otavio O Souza #include <arm/ti/ti_adcreg.h>
61855feb68SLuiz Otavio O Souza #include <arm/ti/ti_adcvar.h>
62855feb68SLuiz Otavio O Souza 
63651f7544SOleksandr Tymoshenko #undef	DEBUG_TSC
64651f7544SOleksandr Tymoshenko 
65651f7544SOleksandr Tymoshenko #define	DEFAULT_CHARGE_DELAY	0x400
66651f7544SOleksandr Tymoshenko #define	STEPDLY_OPEN		0x98
67651f7544SOleksandr Tymoshenko 
68651f7544SOleksandr Tymoshenko #define	ORDER_XP	0
69651f7544SOleksandr Tymoshenko #define	ORDER_XN	1
70651f7544SOleksandr Tymoshenko #define	ORDER_YP	2
71651f7544SOleksandr Tymoshenko #define	ORDER_YN	3
72651f7544SOleksandr Tymoshenko 
73f7eebb77SLuiz Otavio O Souza /* Define our 8 steps, one for each input channel. */
74855feb68SLuiz Otavio O Souza static struct ti_adc_input ti_adc_inputs[TI_ADC_NPINS] = {
75651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(1), .stepdelay = ADC_STEPDLY(1) },
76651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(2), .stepdelay = ADC_STEPDLY(2) },
77651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(3), .stepdelay = ADC_STEPDLY(3) },
78651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(4), .stepdelay = ADC_STEPDLY(4) },
79651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(5), .stepdelay = ADC_STEPDLY(5) },
80651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(6), .stepdelay = ADC_STEPDLY(6) },
81651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(7), .stepdelay = ADC_STEPDLY(7) },
82651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(8), .stepdelay = ADC_STEPDLY(8) },
83855feb68SLuiz Otavio O Souza };
84855feb68SLuiz Otavio O Souza 
85855feb68SLuiz Otavio O Souza static int ti_adc_samples[5] = { 0, 2, 4, 8, 16 };
86855feb68SLuiz Otavio O Souza 
87fbeb453bSOleksandr Tymoshenko static int ti_adc_detach(device_t dev);
88fbeb453bSOleksandr Tymoshenko 
89a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
90fbeb453bSOleksandr Tymoshenko static void
ti_adc_ev_report(struct ti_adc_softc * sc)91fbeb453bSOleksandr Tymoshenko ti_adc_ev_report(struct ti_adc_softc *sc)
92fbeb453bSOleksandr Tymoshenko {
93fbeb453bSOleksandr Tymoshenko 
94fbeb453bSOleksandr Tymoshenko 	evdev_push_event(sc->sc_evdev, EV_ABS, ABS_X, sc->sc_x);
95fbeb453bSOleksandr Tymoshenko 	evdev_push_event(sc->sc_evdev, EV_ABS, ABS_Y, sc->sc_y);
96fbeb453bSOleksandr Tymoshenko 	evdev_push_event(sc->sc_evdev, EV_KEY, BTN_TOUCH, sc->sc_pen_down);
97fbeb453bSOleksandr Tymoshenko 	evdev_sync(sc->sc_evdev);
98fbeb453bSOleksandr Tymoshenko }
99fbeb453bSOleksandr Tymoshenko #endif /* EVDEV */
100fbeb453bSOleksandr Tymoshenko 
101855feb68SLuiz Otavio O Souza static void
ti_adc_enable(struct ti_adc_softc * sc)102855feb68SLuiz Otavio O Souza ti_adc_enable(struct ti_adc_softc *sc)
103855feb68SLuiz Otavio O Souza {
104651f7544SOleksandr Tymoshenko 	uint32_t reg;
105855feb68SLuiz Otavio O Souza 
106855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
107855feb68SLuiz Otavio O Souza 
108855feb68SLuiz Otavio O Souza 	if (sc->sc_last_state == 1)
109855feb68SLuiz Otavio O Souza 		return;
110855feb68SLuiz Otavio O Souza 
111855feb68SLuiz Otavio O Souza 	/* Enable the FIFO0 threshold and the end of sequence interrupt. */
112855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_IRQENABLE_SET,
113651f7544SOleksandr Tymoshenko 	    ADC_IRQ_FIFO0_THRES | ADC_IRQ_FIFO1_THRES | ADC_IRQ_END_OF_SEQ);
114855feb68SLuiz Otavio O Souza 
115651f7544SOleksandr Tymoshenko 	reg = ADC_CTRL_STEP_WP | ADC_CTRL_STEP_ID;
116651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires > 0) {
117651f7544SOleksandr Tymoshenko 		reg |= ADC_CTRL_TSC_ENABLE;
118651f7544SOleksandr Tymoshenko 		switch (sc->sc_tsc_wires) {
119651f7544SOleksandr Tymoshenko 		case 4:
120651f7544SOleksandr Tymoshenko 			reg |= ADC_CTRL_TSC_4WIRE;
121651f7544SOleksandr Tymoshenko 			break;
122651f7544SOleksandr Tymoshenko 		case 5:
123651f7544SOleksandr Tymoshenko 			reg |= ADC_CTRL_TSC_5WIRE;
124651f7544SOleksandr Tymoshenko 			break;
125651f7544SOleksandr Tymoshenko 		case 8:
126651f7544SOleksandr Tymoshenko 			reg |= ADC_CTRL_TSC_8WIRE;
127651f7544SOleksandr Tymoshenko 			break;
128651f7544SOleksandr Tymoshenko 		default:
129651f7544SOleksandr Tymoshenko 			break;
130651f7544SOleksandr Tymoshenko 		}
131651f7544SOleksandr Tymoshenko 	}
132651f7544SOleksandr Tymoshenko 	reg |= ADC_CTRL_ENABLE;
133855feb68SLuiz Otavio O Souza 	/* Enable the ADC.  Run thru enabled steps, start the conversions. */
134651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_CTRL, reg);
135855feb68SLuiz Otavio O Souza 
136855feb68SLuiz Otavio O Souza 	sc->sc_last_state = 1;
137855feb68SLuiz Otavio O Souza }
138855feb68SLuiz Otavio O Souza 
139855feb68SLuiz Otavio O Souza static void
ti_adc_disable(struct ti_adc_softc * sc)140855feb68SLuiz Otavio O Souza ti_adc_disable(struct ti_adc_softc *sc)
141855feb68SLuiz Otavio O Souza {
142855feb68SLuiz Otavio O Souza 	int count;
143855feb68SLuiz Otavio O Souza 
144855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
145855feb68SLuiz Otavio O Souza 
146855feb68SLuiz Otavio O Souza 	if (sc->sc_last_state == 0)
147855feb68SLuiz Otavio O Souza 		return;
148855feb68SLuiz Otavio O Souza 
149855feb68SLuiz Otavio O Souza 	/* Disable all the enabled steps. */
150855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_STEPENABLE, 0);
151855feb68SLuiz Otavio O Souza 
152855feb68SLuiz Otavio O Souza 	/* Disable the ADC. */
153855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_CTRL, ADC_READ4(sc, ADC_CTRL) & ~ADC_CTRL_ENABLE);
154855feb68SLuiz Otavio O Souza 
155855feb68SLuiz Otavio O Souza 	/* Disable the FIFO0 threshold and the end of sequence interrupt. */
156855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_IRQENABLE_CLR,
157651f7544SOleksandr Tymoshenko 	    ADC_IRQ_FIFO0_THRES | ADC_IRQ_FIFO1_THRES | ADC_IRQ_END_OF_SEQ);
158855feb68SLuiz Otavio O Souza 
159855feb68SLuiz Otavio O Souza 	/* ACK any pending interrupt. */
160855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_IRQSTATUS, ADC_READ4(sc, ADC_IRQSTATUS));
161855feb68SLuiz Otavio O Souza 
162855feb68SLuiz Otavio O Souza 	/* Drain the FIFO data. */
163855feb68SLuiz Otavio O Souza 	count = ADC_READ4(sc, ADC_FIFO0COUNT) & ADC_FIFO_COUNT_MSK;
164855feb68SLuiz Otavio O Souza 	while (count > 0) {
165ce71fb38SJohn Baldwin 		(void)ADC_READ4(sc, ADC_FIFO0DATA);
166855feb68SLuiz Otavio O Souza 		count = ADC_READ4(sc, ADC_FIFO0COUNT) & ADC_FIFO_COUNT_MSK;
167855feb68SLuiz Otavio O Souza 	}
168855feb68SLuiz Otavio O Souza 
169651f7544SOleksandr Tymoshenko 	count = ADC_READ4(sc, ADC_FIFO1COUNT) & ADC_FIFO_COUNT_MSK;
170651f7544SOleksandr Tymoshenko 	while (count > 0) {
171ce71fb38SJohn Baldwin 		(void)ADC_READ4(sc, ADC_FIFO1DATA);
172651f7544SOleksandr Tymoshenko 		count = ADC_READ4(sc, ADC_FIFO1COUNT) & ADC_FIFO_COUNT_MSK;
173651f7544SOleksandr Tymoshenko 	}
174651f7544SOleksandr Tymoshenko 
175855feb68SLuiz Otavio O Souza 	sc->sc_last_state = 0;
176855feb68SLuiz Otavio O Souza }
177855feb68SLuiz Otavio O Souza 
178855feb68SLuiz Otavio O Souza static int
ti_adc_setup(struct ti_adc_softc * sc)179855feb68SLuiz Otavio O Souza ti_adc_setup(struct ti_adc_softc *sc)
180855feb68SLuiz Otavio O Souza {
181651f7544SOleksandr Tymoshenko 	int ain, i;
182855feb68SLuiz Otavio O Souza 	uint32_t enabled;
183855feb68SLuiz Otavio O Souza 
184855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
185855feb68SLuiz Otavio O Souza 
186855feb68SLuiz Otavio O Souza 	/* Check for enabled inputs. */
187651f7544SOleksandr Tymoshenko 	enabled = sc->sc_tsc_enabled;
188651f7544SOleksandr Tymoshenko 	for (i = 0; i < sc->sc_adc_nchannels; i++) {
189651f7544SOleksandr Tymoshenko 		ain = sc->sc_adc_channels[i];
190855feb68SLuiz Otavio O Souza 		if (ti_adc_inputs[ain].enable)
191855feb68SLuiz Otavio O Souza 			enabled |= (1U << (ain + 1));
192855feb68SLuiz Otavio O Souza 	}
193855feb68SLuiz Otavio O Souza 
194855feb68SLuiz Otavio O Souza 	/* Set the ADC global status. */
195855feb68SLuiz Otavio O Souza 	if (enabled != 0) {
196855feb68SLuiz Otavio O Souza 		ti_adc_enable(sc);
197855feb68SLuiz Otavio O Souza 		/* Update the enabled steps. */
198855feb68SLuiz Otavio O Souza 		if (enabled != ADC_READ4(sc, ADC_STEPENABLE))
199855feb68SLuiz Otavio O Souza 			ADC_WRITE4(sc, ADC_STEPENABLE, enabled);
200855feb68SLuiz Otavio O Souza 	} else
201855feb68SLuiz Otavio O Souza 		ti_adc_disable(sc);
202855feb68SLuiz Otavio O Souza 
203855feb68SLuiz Otavio O Souza 	return (0);
204855feb68SLuiz Otavio O Souza }
205855feb68SLuiz Otavio O Souza 
206855feb68SLuiz Otavio O Souza static void
ti_adc_input_setup(struct ti_adc_softc * sc,int32_t ain)207855feb68SLuiz Otavio O Souza ti_adc_input_setup(struct ti_adc_softc *sc, int32_t ain)
208855feb68SLuiz Otavio O Souza {
209855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
210855feb68SLuiz Otavio O Souza 	uint32_t reg, val;
211855feb68SLuiz Otavio O Souza 
212855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
213855feb68SLuiz Otavio O Souza 
214855feb68SLuiz Otavio O Souza 	input = &ti_adc_inputs[ain];
215855feb68SLuiz Otavio O Souza 	reg = input->stepconfig;
216855feb68SLuiz Otavio O Souza 	val = ADC_READ4(sc, reg);
217855feb68SLuiz Otavio O Souza 
218855feb68SLuiz Otavio O Souza 	/* Set single ended operation. */
219855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_DIFF_CNTRL;
220855feb68SLuiz Otavio O Souza 
221855feb68SLuiz Otavio O Souza 	/* Set the negative voltage reference. */
222855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_RFM_MSK;
223855feb68SLuiz Otavio O Souza 
224855feb68SLuiz Otavio O Souza 	/* Set the positive voltage reference. */
225855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_RFP_MSK;
226855feb68SLuiz Otavio O Souza 
227855feb68SLuiz Otavio O Souza 	/* Set the samples average. */
228855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_AVG_MSK;
229855feb68SLuiz Otavio O Souza 	val |= input->samples << ADC_STEP_AVG_SHIFT;
230855feb68SLuiz Otavio O Souza 
231855feb68SLuiz Otavio O Souza 	/* Select the desired input. */
232855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_INP_MSK;
233855feb68SLuiz Otavio O Souza 	val |= ain << ADC_STEP_INP_SHIFT;
234855feb68SLuiz Otavio O Souza 
235855feb68SLuiz Otavio O Souza 	/* Set the ADC to one-shot mode. */
236855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_MODE_MSK;
237855feb68SLuiz Otavio O Souza 
238855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, reg, val);
239855feb68SLuiz Otavio O Souza }
240855feb68SLuiz Otavio O Souza 
241855feb68SLuiz Otavio O Souza static void
ti_adc_reset(struct ti_adc_softc * sc)242855feb68SLuiz Otavio O Souza ti_adc_reset(struct ti_adc_softc *sc)
243855feb68SLuiz Otavio O Souza {
244651f7544SOleksandr Tymoshenko 	int ain, i;
245855feb68SLuiz Otavio O Souza 
246855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
247855feb68SLuiz Otavio O Souza 
248855feb68SLuiz Otavio O Souza 	/* Disable all the inputs. */
249651f7544SOleksandr Tymoshenko 	for (i = 0; i < sc->sc_adc_nchannels; i++) {
250651f7544SOleksandr Tymoshenko 		ain = sc->sc_adc_channels[i];
251855feb68SLuiz Otavio O Souza 		ti_adc_inputs[ain].enable = 0;
252855feb68SLuiz Otavio O Souza 	}
253651f7544SOleksandr Tymoshenko }
254855feb68SLuiz Otavio O Souza 
255855feb68SLuiz Otavio O Souza static int
ti_adc_clockdiv_proc(SYSCTL_HANDLER_ARGS)256855feb68SLuiz Otavio O Souza ti_adc_clockdiv_proc(SYSCTL_HANDLER_ARGS)
257855feb68SLuiz Otavio O Souza {
258855feb68SLuiz Otavio O Souza 	int error, reg;
259855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
260855feb68SLuiz Otavio O Souza 
261855feb68SLuiz Otavio O Souza 	sc = (struct ti_adc_softc *)arg1;
262855feb68SLuiz Otavio O Souza 
263855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
264855feb68SLuiz Otavio O Souza 	reg = (int)ADC_READ4(sc, ADC_CLKDIV) + 1;
265855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
266855feb68SLuiz Otavio O Souza 
267855feb68SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
268855feb68SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
269855feb68SLuiz Otavio O Souza 		return (error);
270855feb68SLuiz Otavio O Souza 
271855feb68SLuiz Otavio O Souza 	/*
272855feb68SLuiz Otavio O Souza 	 * The actual written value is the prescaler setting - 1.
273855feb68SLuiz Otavio O Souza 	 * Enforce a minimum value of 10 (i.e. 9) which limits the maximum
274855feb68SLuiz Otavio O Souza 	 * ADC clock to ~2.4Mhz (CLK_M_OSC / 10).
275855feb68SLuiz Otavio O Souza 	 */
276855feb68SLuiz Otavio O Souza 	reg--;
277855feb68SLuiz Otavio O Souza 	if (reg < 9)
278855feb68SLuiz Otavio O Souza 		reg = 9;
279855feb68SLuiz Otavio O Souza 	if (reg > USHRT_MAX)
280855feb68SLuiz Otavio O Souza 		reg = USHRT_MAX;
281855feb68SLuiz Otavio O Souza 
282855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
283855feb68SLuiz Otavio O Souza 	/* Disable the ADC. */
284855feb68SLuiz Otavio O Souza 	ti_adc_disable(sc);
285855feb68SLuiz Otavio O Souza 	/* Update the ADC prescaler setting. */
286855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_CLKDIV, reg);
287855feb68SLuiz Otavio O Souza 	/* Enable the ADC again. */
288855feb68SLuiz Otavio O Souza 	ti_adc_setup(sc);
289855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
290855feb68SLuiz Otavio O Souza 
291855feb68SLuiz Otavio O Souza 	return (0);
292855feb68SLuiz Otavio O Souza }
293855feb68SLuiz Otavio O Souza 
294855feb68SLuiz Otavio O Souza static int
ti_adc_enable_proc(SYSCTL_HANDLER_ARGS)295855feb68SLuiz Otavio O Souza ti_adc_enable_proc(SYSCTL_HANDLER_ARGS)
296855feb68SLuiz Otavio O Souza {
297855feb68SLuiz Otavio O Souza 	int error;
298855feb68SLuiz Otavio O Souza 	int32_t enable;
299855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
300855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
301855feb68SLuiz Otavio O Souza 
302855feb68SLuiz Otavio O Souza 	input = (struct ti_adc_input *)arg1;
303855feb68SLuiz Otavio O Souza 	sc = input->sc;
304855feb68SLuiz Otavio O Souza 
305855feb68SLuiz Otavio O Souza 	enable = input->enable;
306855feb68SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &enable, sizeof(enable),
307855feb68SLuiz Otavio O Souza 	    req);
308855feb68SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
309855feb68SLuiz Otavio O Souza 		return (error);
310855feb68SLuiz Otavio O Souza 
311855feb68SLuiz Otavio O Souza 	if (enable)
312855feb68SLuiz Otavio O Souza 		enable = 1;
313855feb68SLuiz Otavio O Souza 
314855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
315855feb68SLuiz Otavio O Souza 	/* Setup the ADC as needed. */
316855feb68SLuiz Otavio O Souza 	if (input->enable != enable) {
317855feb68SLuiz Otavio O Souza 		input->enable = enable;
318855feb68SLuiz Otavio O Souza 		ti_adc_setup(sc);
319855feb68SLuiz Otavio O Souza 		if (input->enable == 0)
320855feb68SLuiz Otavio O Souza 			input->value = 0;
321855feb68SLuiz Otavio O Souza 	}
322855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
323855feb68SLuiz Otavio O Souza 
324855feb68SLuiz Otavio O Souza 	return (0);
325855feb68SLuiz Otavio O Souza }
326855feb68SLuiz Otavio O Souza 
327855feb68SLuiz Otavio O Souza static int
ti_adc_open_delay_proc(SYSCTL_HANDLER_ARGS)328855feb68SLuiz Otavio O Souza ti_adc_open_delay_proc(SYSCTL_HANDLER_ARGS)
329855feb68SLuiz Otavio O Souza {
330855feb68SLuiz Otavio O Souza 	int error, reg;
331855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
332855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
333855feb68SLuiz Otavio O Souza 
334855feb68SLuiz Otavio O Souza 	input = (struct ti_adc_input *)arg1;
335855feb68SLuiz Otavio O Souza 	sc = input->sc;
336855feb68SLuiz Otavio O Souza 
337855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
338855feb68SLuiz Otavio O Souza 	reg = (int)ADC_READ4(sc, input->stepdelay) & ADC_STEP_OPEN_DELAY;
339855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
340855feb68SLuiz Otavio O Souza 
341855feb68SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
342855feb68SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
343855feb68SLuiz Otavio O Souza 		return (error);
344855feb68SLuiz Otavio O Souza 
345855feb68SLuiz Otavio O Souza 	if (reg < 0)
346855feb68SLuiz Otavio O Souza 		reg = 0;
347855feb68SLuiz Otavio O Souza 
348855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
349855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, input->stepdelay, reg & ADC_STEP_OPEN_DELAY);
350855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
351855feb68SLuiz Otavio O Souza 
352855feb68SLuiz Otavio O Souza 	return (0);
353855feb68SLuiz Otavio O Souza }
354855feb68SLuiz Otavio O Souza 
355855feb68SLuiz Otavio O Souza static int
ti_adc_samples_avg_proc(SYSCTL_HANDLER_ARGS)356855feb68SLuiz Otavio O Souza ti_adc_samples_avg_proc(SYSCTL_HANDLER_ARGS)
357855feb68SLuiz Otavio O Souza {
358855feb68SLuiz Otavio O Souza 	int error, samples, i;
359855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
360855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
361855feb68SLuiz Otavio O Souza 
362855feb68SLuiz Otavio O Souza 	input = (struct ti_adc_input *)arg1;
363855feb68SLuiz Otavio O Souza 	sc = input->sc;
364855feb68SLuiz Otavio O Souza 
365855feb68SLuiz Otavio O Souza 	if (input->samples > nitems(ti_adc_samples))
366855feb68SLuiz Otavio O Souza 		input->samples = nitems(ti_adc_samples);
367855feb68SLuiz Otavio O Souza 	samples = ti_adc_samples[input->samples];
368855feb68SLuiz Otavio O Souza 
369855feb68SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &samples, 0, req);
370855feb68SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
371855feb68SLuiz Otavio O Souza 		return (error);
372855feb68SLuiz Otavio O Souza 
373855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
374855feb68SLuiz Otavio O Souza 	if (samples != ti_adc_samples[input->samples]) {
375855feb68SLuiz Otavio O Souza 		input->samples = 0;
376855feb68SLuiz Otavio O Souza 		for (i = 0; i < nitems(ti_adc_samples); i++)
377855feb68SLuiz Otavio O Souza 			if (samples >= ti_adc_samples[i])
378855feb68SLuiz Otavio O Souza 				input->samples = i;
379855feb68SLuiz Otavio O Souza 		ti_adc_input_setup(sc, input->input);
380855feb68SLuiz Otavio O Souza 	}
381855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
382855feb68SLuiz Otavio O Souza 
383855feb68SLuiz Otavio O Souza 	return (error);
384855feb68SLuiz Otavio O Souza }
385855feb68SLuiz Otavio O Souza 
386855feb68SLuiz Otavio O Souza static void
ti_adc_read_data(struct ti_adc_softc * sc)387855feb68SLuiz Otavio O Souza ti_adc_read_data(struct ti_adc_softc *sc)
388855feb68SLuiz Otavio O Souza {
389855feb68SLuiz Otavio O Souza 	int count, ain;
390855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
391855feb68SLuiz Otavio O Souza 	uint32_t data;
392855feb68SLuiz Otavio O Souza 
393855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
394855feb68SLuiz Otavio O Souza 
395855feb68SLuiz Otavio O Souza 	/* Read the available data. */
396855feb68SLuiz Otavio O Souza 	count = ADC_READ4(sc, ADC_FIFO0COUNT) & ADC_FIFO_COUNT_MSK;
397855feb68SLuiz Otavio O Souza 	while (count > 0) {
398855feb68SLuiz Otavio O Souza 		data = ADC_READ4(sc, ADC_FIFO0DATA);
399855feb68SLuiz Otavio O Souza 		ain = (data & ADC_FIFO_STEP_ID_MSK) >> ADC_FIFO_STEP_ID_SHIFT;
400855feb68SLuiz Otavio O Souza 		input = &ti_adc_inputs[ain];
401855feb68SLuiz Otavio O Souza 		if (input->enable == 0)
402855feb68SLuiz Otavio O Souza 			input->value = 0;
403855feb68SLuiz Otavio O Souza 		else
404855feb68SLuiz Otavio O Souza 			input->value = (int32_t)(data & ADC_FIFO_DATA_MSK);
405855feb68SLuiz Otavio O Souza 		count = ADC_READ4(sc, ADC_FIFO0COUNT) & ADC_FIFO_COUNT_MSK;
406855feb68SLuiz Otavio O Souza 	}
407855feb68SLuiz Otavio O Souza }
408855feb68SLuiz Otavio O Souza 
409651f7544SOleksandr Tymoshenko static int
cmp_values(const void * a,const void * b)410651f7544SOleksandr Tymoshenko cmp_values(const void *a, const void *b)
411651f7544SOleksandr Tymoshenko {
412651f7544SOleksandr Tymoshenko 	const uint32_t *v1, *v2;
413651f7544SOleksandr Tymoshenko 	v1 = a;
414651f7544SOleksandr Tymoshenko 	v2 = b;
415651f7544SOleksandr Tymoshenko 	if (*v1 < *v2)
416651f7544SOleksandr Tymoshenko 		return -1;
417651f7544SOleksandr Tymoshenko 	if (*v1 > *v2)
418651f7544SOleksandr Tymoshenko 		return 1;
419651f7544SOleksandr Tymoshenko 
420651f7544SOleksandr Tymoshenko 	return (0);
421651f7544SOleksandr Tymoshenko }
422651f7544SOleksandr Tymoshenko 
423651f7544SOleksandr Tymoshenko static void
ti_adc_tsc_read_data(struct ti_adc_softc * sc)424651f7544SOleksandr Tymoshenko ti_adc_tsc_read_data(struct ti_adc_softc *sc)
425651f7544SOleksandr Tymoshenko {
426651f7544SOleksandr Tymoshenko 	int count;
427651f7544SOleksandr Tymoshenko 	uint32_t data[16];
428651f7544SOleksandr Tymoshenko 	uint32_t x, y;
429651f7544SOleksandr Tymoshenko 	int i, start, end;
430651f7544SOleksandr Tymoshenko 
431651f7544SOleksandr Tymoshenko 	TI_ADC_LOCK_ASSERT(sc);
432651f7544SOleksandr Tymoshenko 
433651f7544SOleksandr Tymoshenko 	/* Read the available data. */
434651f7544SOleksandr Tymoshenko 	count = ADC_READ4(sc, ADC_FIFO1COUNT) & ADC_FIFO_COUNT_MSK;
435651f7544SOleksandr Tymoshenko 	if (count == 0)
436651f7544SOleksandr Tymoshenko 		return;
437651f7544SOleksandr Tymoshenko 
438651f7544SOleksandr Tymoshenko 	i = 0;
439651f7544SOleksandr Tymoshenko 	while (count > 0) {
440651f7544SOleksandr Tymoshenko 		data[i++] = ADC_READ4(sc, ADC_FIFO1DATA) & ADC_FIFO_DATA_MSK;
441651f7544SOleksandr Tymoshenko 		count = ADC_READ4(sc, ADC_FIFO1COUNT) & ADC_FIFO_COUNT_MSK;
442651f7544SOleksandr Tymoshenko 	}
443651f7544SOleksandr Tymoshenko 
444651f7544SOleksandr Tymoshenko 	if (sc->sc_coord_readouts > 3) {
445651f7544SOleksandr Tymoshenko 		start = 1;
446651f7544SOleksandr Tymoshenko 		end = sc->sc_coord_readouts - 1;
447651f7544SOleksandr Tymoshenko 		qsort(data, sc->sc_coord_readouts,
448651f7544SOleksandr Tymoshenko 			sizeof(data[0]), &cmp_values);
449651f7544SOleksandr Tymoshenko 		qsort(&data[sc->sc_coord_readouts + 2],
450651f7544SOleksandr Tymoshenko 			sc->sc_coord_readouts,
451651f7544SOleksandr Tymoshenko 			sizeof(data[0]), &cmp_values);
452651f7544SOleksandr Tymoshenko 	}
453651f7544SOleksandr Tymoshenko 	else {
454651f7544SOleksandr Tymoshenko 		start = 0;
455651f7544SOleksandr Tymoshenko 		end = sc->sc_coord_readouts;
456651f7544SOleksandr Tymoshenko 	}
457651f7544SOleksandr Tymoshenko 
458651f7544SOleksandr Tymoshenko 	x = y = 0;
459651f7544SOleksandr Tymoshenko 	for (i = start; i < end; i++)
460651f7544SOleksandr Tymoshenko 		y += data[i];
461651f7544SOleksandr Tymoshenko 	y /= (end - start);
462651f7544SOleksandr Tymoshenko 
463651f7544SOleksandr Tymoshenko 	for (i = sc->sc_coord_readouts + 2 + start; i < sc->sc_coord_readouts + 2 + end; i++)
464651f7544SOleksandr Tymoshenko 		x += data[i];
465651f7544SOleksandr Tymoshenko 	x /= (end - start);
466651f7544SOleksandr Tymoshenko 
467651f7544SOleksandr Tymoshenko #ifdef DEBUG_TSC
468651f7544SOleksandr Tymoshenko 	device_printf(sc->sc_dev, "touchscreen x: %d, y: %d\n", x, y);
469651f7544SOleksandr Tymoshenko #endif
470fbeb453bSOleksandr Tymoshenko 
471a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
472fbeb453bSOleksandr Tymoshenko 	if ((sc->sc_x != x) || (sc->sc_y != y)) {
473fbeb453bSOleksandr Tymoshenko 		sc->sc_x = x;
474fbeb453bSOleksandr Tymoshenko 		sc->sc_y = y;
475fbeb453bSOleksandr Tymoshenko 		ti_adc_ev_report(sc);
476fbeb453bSOleksandr Tymoshenko 	}
477fbeb453bSOleksandr Tymoshenko #endif
478651f7544SOleksandr Tymoshenko }
479651f7544SOleksandr Tymoshenko 
480651f7544SOleksandr Tymoshenko static void
ti_adc_intr_locked(struct ti_adc_softc * sc,uint32_t status)481651f7544SOleksandr Tymoshenko ti_adc_intr_locked(struct ti_adc_softc *sc, uint32_t status)
482651f7544SOleksandr Tymoshenko {
483651f7544SOleksandr Tymoshenko 	/* Read the available data. */
484651f7544SOleksandr Tymoshenko 	if (status & ADC_IRQ_FIFO0_THRES)
485651f7544SOleksandr Tymoshenko 		ti_adc_read_data(sc);
486651f7544SOleksandr Tymoshenko }
487651f7544SOleksandr Tymoshenko 
488651f7544SOleksandr Tymoshenko static void
ti_adc_tsc_intr_locked(struct ti_adc_softc * sc,uint32_t status)489651f7544SOleksandr Tymoshenko ti_adc_tsc_intr_locked(struct ti_adc_softc *sc, uint32_t status)
490651f7544SOleksandr Tymoshenko {
491651f7544SOleksandr Tymoshenko 	/* Read the available data. */
492651f7544SOleksandr Tymoshenko 	if (status & ADC_IRQ_FIFO1_THRES)
493651f7544SOleksandr Tymoshenko 		ti_adc_tsc_read_data(sc);
494651f7544SOleksandr Tymoshenko 
495651f7544SOleksandr Tymoshenko }
496651f7544SOleksandr Tymoshenko 
497855feb68SLuiz Otavio O Souza static void
ti_adc_intr(void * arg)498855feb68SLuiz Otavio O Souza ti_adc_intr(void *arg)
499855feb68SLuiz Otavio O Souza {
500855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
501651f7544SOleksandr Tymoshenko 	uint32_t status, rawstatus;
502855feb68SLuiz Otavio O Souza 
503855feb68SLuiz Otavio O Souza 	sc = (struct ti_adc_softc *)arg;
504855feb68SLuiz Otavio O Souza 
505855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
506651f7544SOleksandr Tymoshenko 
507651f7544SOleksandr Tymoshenko 	rawstatus = ADC_READ4(sc, ADC_IRQSTATUS_RAW);
508651f7544SOleksandr Tymoshenko 	status = ADC_READ4(sc, ADC_IRQSTATUS);
509651f7544SOleksandr Tymoshenko 
510651f7544SOleksandr Tymoshenko 	if (rawstatus & ADC_IRQ_HW_PEN_ASYNC) {
511651f7544SOleksandr Tymoshenko 		sc->sc_pen_down = 1;
512651f7544SOleksandr Tymoshenko 		status |= ADC_IRQ_HW_PEN_ASYNC;
513651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_IRQENABLE_CLR,
514651f7544SOleksandr Tymoshenko 			ADC_IRQ_HW_PEN_ASYNC);
515a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
516fbeb453bSOleksandr Tymoshenko 		ti_adc_ev_report(sc);
517fbeb453bSOleksandr Tymoshenko #endif
518651f7544SOleksandr Tymoshenko 	}
519651f7544SOleksandr Tymoshenko 
520651f7544SOleksandr Tymoshenko 	if (rawstatus & ADC_IRQ_PEN_UP) {
521651f7544SOleksandr Tymoshenko 		sc->sc_pen_down = 0;
522651f7544SOleksandr Tymoshenko 		status |= ADC_IRQ_PEN_UP;
523a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
524fbeb453bSOleksandr Tymoshenko 		ti_adc_ev_report(sc);
525fbeb453bSOleksandr Tymoshenko #endif
526651f7544SOleksandr Tymoshenko 	}
527651f7544SOleksandr Tymoshenko 
528651f7544SOleksandr Tymoshenko 	if (status & ADC_IRQ_FIFO0_THRES)
529651f7544SOleksandr Tymoshenko 		ti_adc_intr_locked(sc, status);
530651f7544SOleksandr Tymoshenko 
531651f7544SOleksandr Tymoshenko 	if (status & ADC_IRQ_FIFO1_THRES)
532651f7544SOleksandr Tymoshenko 		ti_adc_tsc_intr_locked(sc, status);
533651f7544SOleksandr Tymoshenko 
534651f7544SOleksandr Tymoshenko 	if (status) {
535855feb68SLuiz Otavio O Souza 		/* ACK the interrupt. */
536855feb68SLuiz Otavio O Souza 		ADC_WRITE4(sc, ADC_IRQSTATUS, status);
537651f7544SOleksandr Tymoshenko 	}
538855feb68SLuiz Otavio O Souza 
539855feb68SLuiz Otavio O Souza 	/* Start the next conversion ? */
540855feb68SLuiz Otavio O Souza 	if (status & ADC_IRQ_END_OF_SEQ)
541855feb68SLuiz Otavio O Souza 		ti_adc_setup(sc);
542651f7544SOleksandr Tymoshenko 
543855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
544855feb68SLuiz Otavio O Souza }
545855feb68SLuiz Otavio O Souza 
546855feb68SLuiz Otavio O Souza static void
ti_adc_sysctl_init(struct ti_adc_softc * sc)547855feb68SLuiz Otavio O Souza ti_adc_sysctl_init(struct ti_adc_softc *sc)
548855feb68SLuiz Otavio O Souza {
549855feb68SLuiz Otavio O Souza 	char pinbuf[3];
550855feb68SLuiz Otavio O Souza 	struct sysctl_ctx_list *ctx;
551855feb68SLuiz Otavio O Souza 	struct sysctl_oid *tree_node, *inp_node, *inpN_node;
552855feb68SLuiz Otavio O Souza 	struct sysctl_oid_list *tree, *inp_tree, *inpN_tree;
553651f7544SOleksandr Tymoshenko 	int ain, i;
554855feb68SLuiz Otavio O Souza 
555855feb68SLuiz Otavio O Souza 	/*
556855feb68SLuiz Otavio O Souza 	 * Add per-pin sysctl tree/handlers.
557855feb68SLuiz Otavio O Souza 	 */
558855feb68SLuiz Otavio O Souza 	ctx = device_get_sysctl_ctx(sc->sc_dev);
559855feb68SLuiz Otavio O Souza 	tree_node = device_get_sysctl_tree(sc->sc_dev);
560855feb68SLuiz Otavio O Souza 	tree = SYSCTL_CHILDREN(tree_node);
561855feb68SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clockdiv",
5627029da5cSPawel Biernacki 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,  sc, 0,
563855feb68SLuiz Otavio O Souza 	    ti_adc_clockdiv_proc, "IU", "ADC clock prescaler");
564855feb68SLuiz Otavio O Souza 	inp_node = SYSCTL_ADD_NODE(ctx, tree, OID_AUTO, "ain",
5657029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ADC inputs");
566855feb68SLuiz Otavio O Souza 	inp_tree = SYSCTL_CHILDREN(inp_node);
567855feb68SLuiz Otavio O Souza 
568651f7544SOleksandr Tymoshenko 	for (i = 0; i < sc->sc_adc_nchannels; i++) {
569651f7544SOleksandr Tymoshenko 		ain = sc->sc_adc_channels[i];
570855feb68SLuiz Otavio O Souza 
571855feb68SLuiz Otavio O Souza 		snprintf(pinbuf, sizeof(pinbuf), "%d", ain);
572855feb68SLuiz Otavio O Souza 		inpN_node = SYSCTL_ADD_NODE(ctx, inp_tree, OID_AUTO, pinbuf,
5737029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ADC input");
574855feb68SLuiz Otavio O Souza 		inpN_tree = SYSCTL_CHILDREN(inpN_node);
575855feb68SLuiz Otavio O Souza 
576855feb68SLuiz Otavio O Souza 		SYSCTL_ADD_PROC(ctx, inpN_tree, OID_AUTO, "enable",
5777029da5cSPawel Biernacki 		    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
5787029da5cSPawel Biernacki 		    &ti_adc_inputs[ain], 0,
579855feb68SLuiz Otavio O Souza 		    ti_adc_enable_proc, "IU", "Enable ADC input");
580855feb68SLuiz Otavio O Souza 		SYSCTL_ADD_PROC(ctx, inpN_tree, OID_AUTO, "open_delay",
5817029da5cSPawel Biernacki 		    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
5827029da5cSPawel Biernacki 		    &ti_adc_inputs[ain], 0,
583855feb68SLuiz Otavio O Souza 		    ti_adc_open_delay_proc, "IU", "ADC open delay");
584855feb68SLuiz Otavio O Souza 		SYSCTL_ADD_PROC(ctx, inpN_tree, OID_AUTO, "samples_avg",
5857029da5cSPawel Biernacki 		    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
5867029da5cSPawel Biernacki 		    &ti_adc_inputs[ain], 0,
587855feb68SLuiz Otavio O Souza 		    ti_adc_samples_avg_proc, "IU", "ADC samples average");
588855feb68SLuiz Otavio O Souza 		SYSCTL_ADD_INT(ctx, inpN_tree, OID_AUTO, "input",
589855feb68SLuiz Otavio O Souza 		    CTLFLAG_RD, &ti_adc_inputs[ain].value, 0,
590855feb68SLuiz Otavio O Souza 		    "Converted raw value for the ADC input");
591855feb68SLuiz Otavio O Souza 	}
592855feb68SLuiz Otavio O Souza }
593855feb68SLuiz Otavio O Souza 
594855feb68SLuiz Otavio O Souza static void
ti_adc_inputs_init(struct ti_adc_softc * sc)595855feb68SLuiz Otavio O Souza ti_adc_inputs_init(struct ti_adc_softc *sc)
596855feb68SLuiz Otavio O Souza {
597651f7544SOleksandr Tymoshenko 	int ain, i;
598855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
599855feb68SLuiz Otavio O Souza 
600855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
601651f7544SOleksandr Tymoshenko 	for (i = 0; i < sc->sc_adc_nchannels; i++) {
602651f7544SOleksandr Tymoshenko 		ain = sc->sc_adc_channels[i];
603855feb68SLuiz Otavio O Souza 		input = &ti_adc_inputs[ain];
604855feb68SLuiz Otavio O Souza 		input->sc = sc;
605855feb68SLuiz Otavio O Souza 		input->input = ain;
606855feb68SLuiz Otavio O Souza 		input->value = 0;
607855feb68SLuiz Otavio O Souza 		input->enable = 0;
608855feb68SLuiz Otavio O Souza 		input->samples = 0;
609855feb68SLuiz Otavio O Souza 		ti_adc_input_setup(sc, ain);
610855feb68SLuiz Otavio O Souza 	}
611855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
612855feb68SLuiz Otavio O Souza }
613855feb68SLuiz Otavio O Souza 
614855feb68SLuiz Otavio O Souza static void
ti_adc_tsc_init(struct ti_adc_softc * sc)615651f7544SOleksandr Tymoshenko ti_adc_tsc_init(struct ti_adc_softc *sc)
616651f7544SOleksandr Tymoshenko {
617651f7544SOleksandr Tymoshenko 	int i, start_step, end_step;
618651f7544SOleksandr Tymoshenko 	uint32_t stepconfig, val;
619651f7544SOleksandr Tymoshenko 
620651f7544SOleksandr Tymoshenko 	TI_ADC_LOCK(sc);
621651f7544SOleksandr Tymoshenko 
622651f7544SOleksandr Tymoshenko 	/* X coordinates */
623651f7544SOleksandr Tymoshenko 	stepconfig = ADC_STEP_FIFO1 | (4 << ADC_STEP_AVG_SHIFT) |
624651f7544SOleksandr Tymoshenko 	    ADC_STEP_MODE_HW_ONESHOT | sc->sc_xp_bit;
625651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires == 4)
626651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(sc->sc_yp_inp) | sc->sc_xn_bit;
627651f7544SOleksandr Tymoshenko 	else if (sc->sc_tsc_wires == 5)
628651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(4) |
629651f7544SOleksandr Tymoshenko 			sc->sc_xn_bit | sc->sc_yn_bit | sc->sc_yp_bit;
630651f7544SOleksandr Tymoshenko 	else if (sc->sc_tsc_wires == 8)
631651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(sc->sc_yp_inp) | sc->sc_xn_bit;
632651f7544SOleksandr Tymoshenko 
633651f7544SOleksandr Tymoshenko 	start_step = ADC_STEPS - sc->sc_coord_readouts + 1;
634651f7544SOleksandr Tymoshenko 	end_step = start_step + sc->sc_coord_readouts - 1;
635651f7544SOleksandr Tymoshenko 	for (i = start_step; i <= end_step; i++) {
636651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_STEPCFG(i), stepconfig);
637651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_STEPDLY(i), STEPDLY_OPEN);
638651f7544SOleksandr Tymoshenko 	}
639651f7544SOleksandr Tymoshenko 
640651f7544SOleksandr Tymoshenko 	/* Y coordinates */
641651f7544SOleksandr Tymoshenko 	stepconfig = ADC_STEP_FIFO1 | (4 << ADC_STEP_AVG_SHIFT) |
642651f7544SOleksandr Tymoshenko 	    ADC_STEP_MODE_HW_ONESHOT | sc->sc_yn_bit |
643651f7544SOleksandr Tymoshenko 	    ADC_STEP_INM(8);
644651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires == 4)
645651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(sc->sc_xp_inp) | sc->sc_yp_bit;
646651f7544SOleksandr Tymoshenko 	else if (sc->sc_tsc_wires == 5)
647651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(4) |
648651f7544SOleksandr Tymoshenko 			sc->sc_xp_bit | sc->sc_xn_bit | sc->sc_yp_bit;
649651f7544SOleksandr Tymoshenko 	else if (sc->sc_tsc_wires == 8)
650651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(sc->sc_xp_inp) | sc->sc_yp_bit;
651651f7544SOleksandr Tymoshenko 
652651f7544SOleksandr Tymoshenko 	start_step = ADC_STEPS - (sc->sc_coord_readouts*2 + 2) + 1;
653651f7544SOleksandr Tymoshenko 	end_step = start_step + sc->sc_coord_readouts - 1;
654651f7544SOleksandr Tymoshenko 	for (i = start_step; i <= end_step; i++) {
655651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_STEPCFG(i), stepconfig);
656651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_STEPDLY(i), STEPDLY_OPEN);
657651f7544SOleksandr Tymoshenko 	}
658651f7544SOleksandr Tymoshenko 
659651f7544SOleksandr Tymoshenko 	/* Charge config */
660651f7544SOleksandr Tymoshenko 	val = ADC_READ4(sc, ADC_IDLECONFIG);
661651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_TC_CHARGE_STEPCONFIG, val);
662651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_TC_CHARGE_DELAY, sc->sc_charge_delay);
663651f7544SOleksandr Tymoshenko 
664651f7544SOleksandr Tymoshenko 	/* 2 steps for Z */
665651f7544SOleksandr Tymoshenko 	start_step = ADC_STEPS - (sc->sc_coord_readouts + 2) + 1;
666651f7544SOleksandr Tymoshenko 	stepconfig = ADC_STEP_FIFO1 | (4 << ADC_STEP_AVG_SHIFT) |
667651f7544SOleksandr Tymoshenko 	    ADC_STEP_MODE_HW_ONESHOT | sc->sc_yp_bit |
668651f7544SOleksandr Tymoshenko 	    sc->sc_xn_bit | ADC_STEP_INP(sc->sc_xp_inp) |
669651f7544SOleksandr Tymoshenko 	    ADC_STEP_INM(8);
670651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_STEPCFG(start_step), stepconfig);
671651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_STEPDLY(start_step), STEPDLY_OPEN);
672651f7544SOleksandr Tymoshenko 	start_step++;
673651f7544SOleksandr Tymoshenko 	stepconfig |= ADC_STEP_INP(sc->sc_yn_inp);
674651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_STEPCFG(start_step), stepconfig);
675651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_STEPDLY(start_step), STEPDLY_OPEN);
676651f7544SOleksandr Tymoshenko 
677651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_FIFO1THRESHOLD, (sc->sc_coord_readouts*2 + 2) - 1);
678651f7544SOleksandr Tymoshenko 
679651f7544SOleksandr Tymoshenko 	sc->sc_tsc_enabled = 1;
680651f7544SOleksandr Tymoshenko 	start_step = ADC_STEPS - (sc->sc_coord_readouts*2 + 2) + 1;
681651f7544SOleksandr Tymoshenko 	end_step = ADC_STEPS;
682651f7544SOleksandr Tymoshenko 	for (i = start_step; i <= end_step; i++) {
683651f7544SOleksandr Tymoshenko 		sc->sc_tsc_enabled |= (1 << i);
684651f7544SOleksandr Tymoshenko 	}
685651f7544SOleksandr Tymoshenko 
686651f7544SOleksandr Tymoshenko 	TI_ADC_UNLOCK(sc);
687651f7544SOleksandr Tymoshenko }
688651f7544SOleksandr Tymoshenko 
689651f7544SOleksandr Tymoshenko static void
ti_adc_idlestep_init(struct ti_adc_softc * sc)690855feb68SLuiz Otavio O Souza ti_adc_idlestep_init(struct ti_adc_softc *sc)
691855feb68SLuiz Otavio O Souza {
692855feb68SLuiz Otavio O Souza 	uint32_t val;
693855feb68SLuiz Otavio O Souza 
694651f7544SOleksandr Tymoshenko 	val = ADC_STEP_YNN_SW | ADC_STEP_INM(8) | ADC_STEP_INP(8) | ADC_STEP_YPN_SW;
695855feb68SLuiz Otavio O Souza 
696855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_IDLECONFIG, val);
697855feb68SLuiz Otavio O Souza }
698855feb68SLuiz Otavio O Souza 
699855feb68SLuiz Otavio O Souza static int
ti_adc_config_wires(struct ti_adc_softc * sc,int * wire_configs,int nwire_configs)700651f7544SOleksandr Tymoshenko ti_adc_config_wires(struct ti_adc_softc *sc, int *wire_configs, int nwire_configs)
701651f7544SOleksandr Tymoshenko {
702651f7544SOleksandr Tymoshenko 	int i;
703651f7544SOleksandr Tymoshenko 	int wire, ai;
704651f7544SOleksandr Tymoshenko 
705651f7544SOleksandr Tymoshenko 	for (i = 0; i < nwire_configs; i++) {
706651f7544SOleksandr Tymoshenko 		wire = wire_configs[i] & 0xf;
707651f7544SOleksandr Tymoshenko 		ai = (wire_configs[i] >> 4) & 0xf;
708651f7544SOleksandr Tymoshenko 		switch (wire) {
709651f7544SOleksandr Tymoshenko 		case ORDER_XP:
710651f7544SOleksandr Tymoshenko 			sc->sc_xp_bit = ADC_STEP_XPP_SW;
711651f7544SOleksandr Tymoshenko 			sc->sc_xp_inp = ai;
712651f7544SOleksandr Tymoshenko 			break;
713651f7544SOleksandr Tymoshenko 		case ORDER_XN:
714651f7544SOleksandr Tymoshenko 			sc->sc_xn_bit = ADC_STEP_XNN_SW;
715651f7544SOleksandr Tymoshenko 			sc->sc_xn_inp = ai;
716651f7544SOleksandr Tymoshenko 			break;
717651f7544SOleksandr Tymoshenko 		case ORDER_YP:
718651f7544SOleksandr Tymoshenko 			sc->sc_yp_bit = ADC_STEP_YPP_SW;
719651f7544SOleksandr Tymoshenko 			sc->sc_yp_inp = ai;
720651f7544SOleksandr Tymoshenko 			break;
721651f7544SOleksandr Tymoshenko 		case ORDER_YN:
722651f7544SOleksandr Tymoshenko 			sc->sc_yn_bit = ADC_STEP_YNN_SW;
723651f7544SOleksandr Tymoshenko 			sc->sc_yn_inp = ai;
724651f7544SOleksandr Tymoshenko 			break;
725651f7544SOleksandr Tymoshenko 		default:
726651f7544SOleksandr Tymoshenko 			device_printf(sc->sc_dev, "Invalid wire config\n");
727651f7544SOleksandr Tymoshenko 			return (-1);
728651f7544SOleksandr Tymoshenko 		}
729651f7544SOleksandr Tymoshenko 	}
730651f7544SOleksandr Tymoshenko 	return (0);
731651f7544SOleksandr Tymoshenko }
732651f7544SOleksandr Tymoshenko 
733651f7544SOleksandr Tymoshenko static int
ti_adc_probe(device_t dev)734855feb68SLuiz Otavio O Souza ti_adc_probe(device_t dev)
735855feb68SLuiz Otavio O Souza {
736855feb68SLuiz Otavio O Souza 
7375b03aba6SOleksandr Tymoshenko 	if (!ofw_bus_is_compatible(dev, "ti,am3359-tscadc"))
738855feb68SLuiz Otavio O Souza 		return (ENXIO);
739855feb68SLuiz Otavio O Souza 	device_set_desc(dev, "TI ADC controller");
740855feb68SLuiz Otavio O Souza 
741855feb68SLuiz Otavio O Souza 	return (BUS_PROBE_DEFAULT);
742855feb68SLuiz Otavio O Souza }
743855feb68SLuiz Otavio O Souza 
744855feb68SLuiz Otavio O Souza static int
ti_adc_attach(device_t dev)745855feb68SLuiz Otavio O Souza ti_adc_attach(device_t dev)
746855feb68SLuiz Otavio O Souza {
747651f7544SOleksandr Tymoshenko 	int err, rid, i;
748855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
749651f7544SOleksandr Tymoshenko 	uint32_t rev, reg;
750651f7544SOleksandr Tymoshenko 	phandle_t node, child;
751651f7544SOleksandr Tymoshenko 	pcell_t cell;
752651f7544SOleksandr Tymoshenko 	int *channels;
753651f7544SOleksandr Tymoshenko 	int nwire_configs;
754651f7544SOleksandr Tymoshenko 	int *wire_configs;
755855feb68SLuiz Otavio O Souza 
756855feb68SLuiz Otavio O Souza 	sc = device_get_softc(dev);
757855feb68SLuiz Otavio O Souza 	sc->sc_dev = dev;
758855feb68SLuiz Otavio O Souza 
759651f7544SOleksandr Tymoshenko 	node = ofw_bus_get_node(dev);
760651f7544SOleksandr Tymoshenko 
761651f7544SOleksandr Tymoshenko 	sc->sc_tsc_wires = 0;
762651f7544SOleksandr Tymoshenko 	sc->sc_coord_readouts = 1;
763651f7544SOleksandr Tymoshenko 	sc->sc_x_plate_resistance = 0;
764651f7544SOleksandr Tymoshenko 	sc->sc_charge_delay = DEFAULT_CHARGE_DELAY;
765651f7544SOleksandr Tymoshenko 	/* Read "tsc" node properties */
766651f7544SOleksandr Tymoshenko 	child = ofw_bus_find_child(node, "tsc");
767d3dd0204SLuiz Otavio O Souza 	if (child != 0 && OF_hasprop(child, "ti,wires")) {
7689783ea5cSAndrew Turner 		if ((OF_getencprop(child, "ti,wires", &cell, sizeof(cell))) > 0)
7699783ea5cSAndrew Turner 			sc->sc_tsc_wires = cell;
7709783ea5cSAndrew Turner 		if ((OF_getencprop(child, "ti,coordinate-readouts", &cell,
7719783ea5cSAndrew Turner 		    sizeof(cell))) > 0)
7729783ea5cSAndrew Turner 			sc->sc_coord_readouts = cell;
7739783ea5cSAndrew Turner 		if ((OF_getencprop(child, "ti,x-plate-resistance", &cell,
7749783ea5cSAndrew Turner 		    sizeof(cell))) > 0)
7759783ea5cSAndrew Turner 			sc->sc_x_plate_resistance = cell;
7769783ea5cSAndrew Turner 		if ((OF_getencprop(child, "ti,charge-delay", &cell,
7779783ea5cSAndrew Turner 		    sizeof(cell))) > 0)
7789783ea5cSAndrew Turner 			sc->sc_charge_delay = cell;
779f7604b1bSOleksandr Tymoshenko 		nwire_configs = OF_getencprop_alloc_multi(child,
780f7604b1bSOleksandr Tymoshenko 		    "ti,wire-config", sizeof(*wire_configs),
781f7604b1bSOleksandr Tymoshenko 		    (void **)&wire_configs);
782651f7544SOleksandr Tymoshenko 		if (nwire_configs != sc->sc_tsc_wires) {
783651f7544SOleksandr Tymoshenko 			device_printf(sc->sc_dev,
784d3dd0204SLuiz Otavio O Souza 			    "invalid number of ti,wire-config: %d (should be %d)\n",
785651f7544SOleksandr Tymoshenko 			    nwire_configs, sc->sc_tsc_wires);
786bc90a48cSOleksandr Tymoshenko 			OF_prop_free(wire_configs);
787651f7544SOleksandr Tymoshenko 			return (EINVAL);
788651f7544SOleksandr Tymoshenko 		}
789651f7544SOleksandr Tymoshenko 		err = ti_adc_config_wires(sc, wire_configs, nwire_configs);
790bc90a48cSOleksandr Tymoshenko 		OF_prop_free(wire_configs);
791e5ee119aSLuiz Otavio O Souza 		if (err)
792651f7544SOleksandr Tymoshenko 			return (EINVAL);
793651f7544SOleksandr Tymoshenko 	}
794651f7544SOleksandr Tymoshenko 
795651f7544SOleksandr Tymoshenko 	/* Read "adc" node properties */
796651f7544SOleksandr Tymoshenko 	child = ofw_bus_find_child(node, "adc");
797651f7544SOleksandr Tymoshenko 	if (child != 0) {
798f7604b1bSOleksandr Tymoshenko 		sc->sc_adc_nchannels = OF_getencprop_alloc_multi(child,
799f7604b1bSOleksandr Tymoshenko 		    "ti,adc-channels", sizeof(*channels), (void **)&channels);
800651f7544SOleksandr Tymoshenko 		if (sc->sc_adc_nchannels > 0) {
801651f7544SOleksandr Tymoshenko 			for (i = 0; i < sc->sc_adc_nchannels; i++)
802651f7544SOleksandr Tymoshenko 				sc->sc_adc_channels[i] = channels[i];
803bc90a48cSOleksandr Tymoshenko 			OF_prop_free(channels);
804651f7544SOleksandr Tymoshenko 		}
805651f7544SOleksandr Tymoshenko 	}
806651f7544SOleksandr Tymoshenko 
807651f7544SOleksandr Tymoshenko 	/* Sanity check FDT data */
808651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires + sc->sc_adc_nchannels > TI_ADC_NPINS) {
80942dfad2eSGordon Bergling 		device_printf(dev, "total number of channels (%d) is larger than %d\n",
810651f7544SOleksandr Tymoshenko 		    sc->sc_tsc_wires + sc->sc_adc_nchannels, TI_ADC_NPINS);
811651f7544SOleksandr Tymoshenko 		return (ENXIO);
812651f7544SOleksandr Tymoshenko 	}
813e5ee119aSLuiz Otavio O Souza 
814855feb68SLuiz Otavio O Souza 	rid = 0;
815855feb68SLuiz Otavio O Souza 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
816855feb68SLuiz Otavio O Souza 	    RF_ACTIVE);
817855feb68SLuiz Otavio O Souza 	if (!sc->sc_mem_res) {
818855feb68SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate memory window\n");
819855feb68SLuiz Otavio O Souza 		return (ENXIO);
820855feb68SLuiz Otavio O Souza 	}
821855feb68SLuiz Otavio O Souza 
822651f7544SOleksandr Tymoshenko 	/* Activate the ADC_TSC module. */
8230050ea24SMichal Meloun 	err = ti_sysc_clock_enable(device_get_parent(dev));
824651f7544SOleksandr Tymoshenko 	if (err)
825651f7544SOleksandr Tymoshenko 		return (err);
826651f7544SOleksandr Tymoshenko 
827855feb68SLuiz Otavio O Souza 	rid = 0;
828855feb68SLuiz Otavio O Souza 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
829855feb68SLuiz Otavio O Souza 	    RF_ACTIVE);
830855feb68SLuiz Otavio O Souza 	if (!sc->sc_irq_res) {
831855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
832855feb68SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate interrupt\n");
833855feb68SLuiz Otavio O Souza 		return (ENXIO);
834855feb68SLuiz Otavio O Souza 	}
835855feb68SLuiz Otavio O Souza 
836855feb68SLuiz Otavio O Souza 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
837855feb68SLuiz Otavio O Souza 	    NULL, ti_adc_intr, sc, &sc->sc_intrhand) != 0) {
838855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
839855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
840855feb68SLuiz Otavio O Souza 		device_printf(dev, "Unable to setup the irq handler.\n");
841855feb68SLuiz Otavio O Souza 		return (ENXIO);
842855feb68SLuiz Otavio O Souza 	}
843855feb68SLuiz Otavio O Souza 
844855feb68SLuiz Otavio O Souza 	/* Check the ADC revision. */
8450050ea24SMichal Meloun 	rev = ADC_READ4(sc, ti_sysc_get_rev_address_offset_host(device_get_parent(dev)));
846855feb68SLuiz Otavio O Souza 	device_printf(dev,
847855feb68SLuiz Otavio O Souza 	    "scheme: %#x func: %#x rtl: %d rev: %d.%d custom rev: %d\n",
848855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_SCHEME_MSK) >> ADC_REV_SCHEME_SHIFT,
849855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_FUNC_MSK) >> ADC_REV_FUNC_SHIFT,
850855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_RTL_MSK) >> ADC_REV_RTL_SHIFT,
851855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_MAJOR_MSK) >> ADC_REV_MAJOR_SHIFT,
852855feb68SLuiz Otavio O Souza 	    rev & ADC_REV_MINOR_MSK,
853855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_CUSTOM_MSK) >> ADC_REV_CUSTOM_SHIFT);
854855feb68SLuiz Otavio O Souza 
855855feb68SLuiz Otavio O Souza 	reg = ADC_READ4(sc, ADC_CTRL);
856855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_CTRL, reg | ADC_CTRL_STEP_WP | ADC_CTRL_STEP_ID);
857855feb68SLuiz Otavio O Souza 
858855feb68SLuiz Otavio O Souza 	/*
859651f7544SOleksandr Tymoshenko 	 * Set the ADC prescaler to 2400 if touchscreen is not enabled
860651f7544SOleksandr Tymoshenko 	 * and to 24 if it is.  This sets the ADC clock to ~10Khz and
861651f7544SOleksandr Tymoshenko 	 * ~1Mhz respectively (CLK_M_OSC / prescaler).
862855feb68SLuiz Otavio O Souza 	 */
863651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires)
864651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_CLKDIV, 24 - 1);
865651f7544SOleksandr Tymoshenko 	else
866651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_CLKDIV, 2400 - 1);
867855feb68SLuiz Otavio O Souza 
868855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_INIT(sc);
869855feb68SLuiz Otavio O Souza 
870855feb68SLuiz Otavio O Souza 	ti_adc_idlestep_init(sc);
871855feb68SLuiz Otavio O Souza 	ti_adc_inputs_init(sc);
872855feb68SLuiz Otavio O Souza 	ti_adc_sysctl_init(sc);
873651f7544SOleksandr Tymoshenko 	ti_adc_tsc_init(sc);
874651f7544SOleksandr Tymoshenko 
875651f7544SOleksandr Tymoshenko 	TI_ADC_LOCK(sc);
876651f7544SOleksandr Tymoshenko 	ti_adc_setup(sc);
877651f7544SOleksandr Tymoshenko 	TI_ADC_UNLOCK(sc);
878855feb68SLuiz Otavio O Souza 
879a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
880fbeb453bSOleksandr Tymoshenko 	if (sc->sc_tsc_wires > 0) {
881fbeb453bSOleksandr Tymoshenko 		sc->sc_evdev = evdev_alloc();
882fbeb453bSOleksandr Tymoshenko 		evdev_set_name(sc->sc_evdev, device_get_desc(dev));
883fbeb453bSOleksandr Tymoshenko 		evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
884b763171bSOleksandr Tymoshenko 		evdev_set_id(sc->sc_evdev, BUS_VIRTUAL, 0, 0, 0);
885fbeb453bSOleksandr Tymoshenko 		evdev_support_prop(sc->sc_evdev, INPUT_PROP_DIRECT);
886fbeb453bSOleksandr Tymoshenko 		evdev_support_event(sc->sc_evdev, EV_SYN);
887fbeb453bSOleksandr Tymoshenko 		evdev_support_event(sc->sc_evdev, EV_ABS);
888fbeb453bSOleksandr Tymoshenko 		evdev_support_event(sc->sc_evdev, EV_KEY);
889fbeb453bSOleksandr Tymoshenko 
8905af73ad5SVladimir Kondratyev 		evdev_support_abs(sc->sc_evdev, ABS_X, 0,
891fbeb453bSOleksandr Tymoshenko 		    ADC_MAX_VALUE, 0, 0, 0);
8925af73ad5SVladimir Kondratyev 		evdev_support_abs(sc->sc_evdev, ABS_Y, 0,
893fbeb453bSOleksandr Tymoshenko 		    ADC_MAX_VALUE, 0, 0, 0);
894fbeb453bSOleksandr Tymoshenko 
895fbeb453bSOleksandr Tymoshenko 		evdev_support_key(sc->sc_evdev, BTN_TOUCH);
896fbeb453bSOleksandr Tymoshenko 
897fbeb453bSOleksandr Tymoshenko 		err = evdev_register(sc->sc_evdev);
898fbeb453bSOleksandr Tymoshenko 		if (err) {
899fbeb453bSOleksandr Tymoshenko 			device_printf(dev,
900fbeb453bSOleksandr Tymoshenko 			    "failed to register evdev: error=%d\n", err);
901fbeb453bSOleksandr Tymoshenko 			ti_adc_detach(dev);
902fbeb453bSOleksandr Tymoshenko 			return (err);
903fbeb453bSOleksandr Tymoshenko 		}
904fbeb453bSOleksandr Tymoshenko 
905fbeb453bSOleksandr Tymoshenko 		sc->sc_pen_down = 0;
906fbeb453bSOleksandr Tymoshenko 		sc->sc_x = -1;
907fbeb453bSOleksandr Tymoshenko 		sc->sc_y = -1;
908fbeb453bSOleksandr Tymoshenko 	}
909fbeb453bSOleksandr Tymoshenko #endif /* EVDEV */
910fbeb453bSOleksandr Tymoshenko 
911855feb68SLuiz Otavio O Souza 	return (0);
912855feb68SLuiz Otavio O Souza }
913855feb68SLuiz Otavio O Souza 
914855feb68SLuiz Otavio O Souza static int
ti_adc_detach(device_t dev)915855feb68SLuiz Otavio O Souza ti_adc_detach(device_t dev)
916855feb68SLuiz Otavio O Souza {
917855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
918*d412c076SJohn Baldwin 	int error;
919*d412c076SJohn Baldwin 
920*d412c076SJohn Baldwin 	error = bus_generic_detach(dev);
921*d412c076SJohn Baldwin 	if (error != 0)
922*d412c076SJohn Baldwin 		return (error);
923855feb68SLuiz Otavio O Souza 
924855feb68SLuiz Otavio O Souza 	sc = device_get_softc(dev);
925855feb68SLuiz Otavio O Souza 
926855feb68SLuiz Otavio O Souza 	/* Turn off the ADC. */
927855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
928855feb68SLuiz Otavio O Souza 	ti_adc_reset(sc);
929855feb68SLuiz Otavio O Souza 	ti_adc_setup(sc);
930fbeb453bSOleksandr Tymoshenko 
931a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
932fbeb453bSOleksandr Tymoshenko 	evdev_free(sc->sc_evdev);
933fbeb453bSOleksandr Tymoshenko #endif
934fbeb453bSOleksandr Tymoshenko 
935855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
936855feb68SLuiz Otavio O Souza 
937855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_DESTROY(sc);
938855feb68SLuiz Otavio O Souza 
939855feb68SLuiz Otavio O Souza 	if (sc->sc_intrhand)
940855feb68SLuiz Otavio O Souza 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
941855feb68SLuiz Otavio O Souza 	if (sc->sc_irq_res)
942855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
943855feb68SLuiz Otavio O Souza 	if (sc->sc_mem_res)
944855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
945855feb68SLuiz Otavio O Souza 
946*d412c076SJohn Baldwin 	return (0);
947855feb68SLuiz Otavio O Souza }
948855feb68SLuiz Otavio O Souza 
949855feb68SLuiz Otavio O Souza static device_method_t ti_adc_methods[] = {
950855feb68SLuiz Otavio O Souza 	DEVMETHOD(device_probe,		ti_adc_probe),
951855feb68SLuiz Otavio O Souza 	DEVMETHOD(device_attach,	ti_adc_attach),
952855feb68SLuiz Otavio O Souza 	DEVMETHOD(device_detach,	ti_adc_detach),
953855feb68SLuiz Otavio O Souza 
954855feb68SLuiz Otavio O Souza 	DEVMETHOD_END
955855feb68SLuiz Otavio O Souza };
956855feb68SLuiz Otavio O Souza 
957855feb68SLuiz Otavio O Souza static driver_t ti_adc_driver = {
958855feb68SLuiz Otavio O Souza 	"ti_adc",
959855feb68SLuiz Otavio O Souza 	ti_adc_methods,
960855feb68SLuiz Otavio O Souza 	sizeof(struct ti_adc_softc),
961855feb68SLuiz Otavio O Souza };
962855feb68SLuiz Otavio O Souza 
9638537e671SJohn Baldwin DRIVER_MODULE(ti_adc, simplebus, ti_adc_driver, 0, 0);
964855feb68SLuiz Otavio O Souza MODULE_VERSION(ti_adc, 1);
965855feb68SLuiz Otavio O Souza MODULE_DEPEND(ti_adc, simplebus, 1, 1, 1);
9660050ea24SMichal Meloun MODULE_DEPEND(ti_adc, ti_sysc, 1, 1, 1);
967a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
96815eefa5bSOleksandr Tymoshenko MODULE_DEPEND(ti_adc, evdev, 1, 1, 1);
96915eefa5bSOleksandr Tymoshenko #endif
970