xref: /freebsd/sys/arm/ti/ti_adc.c (revision 0050ea241584f931c6089f7b7a7aca3804131397)
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>
28855feb68SLuiz Otavio O Souza __FBSDID("$FreeBSD$");
29855feb68SLuiz Otavio O Souza 
30fbeb453bSOleksandr Tymoshenko #include "opt_evdev.h"
31fbeb453bSOleksandr Tymoshenko 
32855feb68SLuiz Otavio O Souza #include <sys/param.h>
33855feb68SLuiz Otavio O Souza #include <sys/systm.h>
34855feb68SLuiz Otavio O Souza #include <sys/bus.h>
35855feb68SLuiz Otavio O Souza 
36651f7544SOleksandr Tymoshenko #include <sys/conf.h>
37855feb68SLuiz Otavio O Souza #include <sys/kernel.h>
38855feb68SLuiz Otavio O Souza #include <sys/limits.h>
39855feb68SLuiz Otavio O Souza #include <sys/lock.h>
40855feb68SLuiz Otavio O Souza #include <sys/module.h>
41855feb68SLuiz Otavio O Souza #include <sys/mutex.h>
42651f7544SOleksandr Tymoshenko #include <sys/condvar.h>
43855feb68SLuiz Otavio O Souza #include <sys/resource.h>
44855feb68SLuiz Otavio O Souza #include <sys/rman.h>
45855feb68SLuiz Otavio O Souza #include <sys/sysctl.h>
46651f7544SOleksandr Tymoshenko #include <sys/selinfo.h>
47651f7544SOleksandr Tymoshenko #include <sys/poll.h>
48651f7544SOleksandr Tymoshenko #include <sys/uio.h>
49855feb68SLuiz Otavio O Souza 
50855feb68SLuiz Otavio O Souza #include <machine/bus.h>
51855feb68SLuiz Otavio O Souza 
52855feb68SLuiz Otavio O Souza #include <dev/ofw/openfirm.h>
53855feb68SLuiz Otavio O Souza #include <dev/ofw/ofw_bus.h>
54855feb68SLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h>
55855feb68SLuiz Otavio O Souza 
56a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
57fbeb453bSOleksandr Tymoshenko #include <dev/evdev/input.h>
58fbeb453bSOleksandr Tymoshenko #include <dev/evdev/evdev.h>
59fbeb453bSOleksandr Tymoshenko #endif
60fbeb453bSOleksandr Tymoshenko 
61*0050ea24SMichal Meloun #include <arm/ti/ti_sysc.h>
62855feb68SLuiz Otavio O Souza #include <arm/ti/ti_adcreg.h>
63855feb68SLuiz Otavio O Souza #include <arm/ti/ti_adcvar.h>
64855feb68SLuiz Otavio O Souza 
65651f7544SOleksandr Tymoshenko #undef	DEBUG_TSC
66651f7544SOleksandr Tymoshenko 
67651f7544SOleksandr Tymoshenko #define	DEFAULT_CHARGE_DELAY	0x400
68651f7544SOleksandr Tymoshenko #define	STEPDLY_OPEN		0x98
69651f7544SOleksandr Tymoshenko 
70651f7544SOleksandr Tymoshenko #define	ORDER_XP	0
71651f7544SOleksandr Tymoshenko #define	ORDER_XN	1
72651f7544SOleksandr Tymoshenko #define	ORDER_YP	2
73651f7544SOleksandr Tymoshenko #define	ORDER_YN	3
74651f7544SOleksandr Tymoshenko 
75f7eebb77SLuiz Otavio O Souza /* Define our 8 steps, one for each input channel. */
76855feb68SLuiz Otavio O Souza static struct ti_adc_input ti_adc_inputs[TI_ADC_NPINS] = {
77651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(1), .stepdelay = ADC_STEPDLY(1) },
78651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(2), .stepdelay = ADC_STEPDLY(2) },
79651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(3), .stepdelay = ADC_STEPDLY(3) },
80651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(4), .stepdelay = ADC_STEPDLY(4) },
81651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(5), .stepdelay = ADC_STEPDLY(5) },
82651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(6), .stepdelay = ADC_STEPDLY(6) },
83651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(7), .stepdelay = ADC_STEPDLY(7) },
84651f7544SOleksandr Tymoshenko 	{ .stepconfig = ADC_STEPCFG(8), .stepdelay = ADC_STEPDLY(8) },
85855feb68SLuiz Otavio O Souza };
86855feb68SLuiz Otavio O Souza 
87855feb68SLuiz Otavio O Souza static int ti_adc_samples[5] = { 0, 2, 4, 8, 16 };
88855feb68SLuiz Otavio O Souza 
89fbeb453bSOleksandr Tymoshenko static int ti_adc_detach(device_t dev);
90fbeb453bSOleksandr Tymoshenko 
91a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
92fbeb453bSOleksandr Tymoshenko static void
93fbeb453bSOleksandr Tymoshenko ti_adc_ev_report(struct ti_adc_softc *sc)
94fbeb453bSOleksandr Tymoshenko {
95fbeb453bSOleksandr Tymoshenko 
96fbeb453bSOleksandr Tymoshenko 	evdev_push_event(sc->sc_evdev, EV_ABS, ABS_X, sc->sc_x);
97fbeb453bSOleksandr Tymoshenko 	evdev_push_event(sc->sc_evdev, EV_ABS, ABS_Y, sc->sc_y);
98fbeb453bSOleksandr Tymoshenko 	evdev_push_event(sc->sc_evdev, EV_KEY, BTN_TOUCH, sc->sc_pen_down);
99fbeb453bSOleksandr Tymoshenko 	evdev_sync(sc->sc_evdev);
100fbeb453bSOleksandr Tymoshenko }
101fbeb453bSOleksandr Tymoshenko #endif /* EVDEV */
102fbeb453bSOleksandr Tymoshenko 
103855feb68SLuiz Otavio O Souza static void
104855feb68SLuiz Otavio O Souza ti_adc_enable(struct ti_adc_softc *sc)
105855feb68SLuiz Otavio O Souza {
106651f7544SOleksandr Tymoshenko 	uint32_t reg;
107855feb68SLuiz Otavio O Souza 
108855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
109855feb68SLuiz Otavio O Souza 
110855feb68SLuiz Otavio O Souza 	if (sc->sc_last_state == 1)
111855feb68SLuiz Otavio O Souza 		return;
112855feb68SLuiz Otavio O Souza 
113855feb68SLuiz Otavio O Souza 	/* Enable the FIFO0 threshold and the end of sequence interrupt. */
114855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_IRQENABLE_SET,
115651f7544SOleksandr Tymoshenko 	    ADC_IRQ_FIFO0_THRES | ADC_IRQ_FIFO1_THRES | ADC_IRQ_END_OF_SEQ);
116855feb68SLuiz Otavio O Souza 
117651f7544SOleksandr Tymoshenko 	reg = ADC_CTRL_STEP_WP | ADC_CTRL_STEP_ID;
118651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires > 0) {
119651f7544SOleksandr Tymoshenko 		reg |= ADC_CTRL_TSC_ENABLE;
120651f7544SOleksandr Tymoshenko 		switch (sc->sc_tsc_wires) {
121651f7544SOleksandr Tymoshenko 		case 4:
122651f7544SOleksandr Tymoshenko 			reg |= ADC_CTRL_TSC_4WIRE;
123651f7544SOleksandr Tymoshenko 			break;
124651f7544SOleksandr Tymoshenko 		case 5:
125651f7544SOleksandr Tymoshenko 			reg |= ADC_CTRL_TSC_5WIRE;
126651f7544SOleksandr Tymoshenko 			break;
127651f7544SOleksandr Tymoshenko 		case 8:
128651f7544SOleksandr Tymoshenko 			reg |= ADC_CTRL_TSC_8WIRE;
129651f7544SOleksandr Tymoshenko 			break;
130651f7544SOleksandr Tymoshenko 		default:
131651f7544SOleksandr Tymoshenko 			break;
132651f7544SOleksandr Tymoshenko 		}
133651f7544SOleksandr Tymoshenko 	}
134651f7544SOleksandr Tymoshenko 	reg |= ADC_CTRL_ENABLE;
135855feb68SLuiz Otavio O Souza 	/* Enable the ADC.  Run thru enabled steps, start the conversions. */
136651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_CTRL, reg);
137855feb68SLuiz Otavio O Souza 
138855feb68SLuiz Otavio O Souza 	sc->sc_last_state = 1;
139855feb68SLuiz Otavio O Souza }
140855feb68SLuiz Otavio O Souza 
141855feb68SLuiz Otavio O Souza static void
142855feb68SLuiz Otavio O Souza ti_adc_disable(struct ti_adc_softc *sc)
143855feb68SLuiz Otavio O Souza {
144855feb68SLuiz Otavio O Souza 	int count;
145855feb68SLuiz Otavio O Souza 	uint32_t data;
146855feb68SLuiz Otavio O Souza 
147855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
148855feb68SLuiz Otavio O Souza 
149855feb68SLuiz Otavio O Souza 	if (sc->sc_last_state == 0)
150855feb68SLuiz Otavio O Souza 		return;
151855feb68SLuiz Otavio O Souza 
152855feb68SLuiz Otavio O Souza 	/* Disable all the enabled steps. */
153855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_STEPENABLE, 0);
154855feb68SLuiz Otavio O Souza 
155855feb68SLuiz Otavio O Souza 	/* Disable the ADC. */
156855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_CTRL, ADC_READ4(sc, ADC_CTRL) & ~ADC_CTRL_ENABLE);
157855feb68SLuiz Otavio O Souza 
158855feb68SLuiz Otavio O Souza 	/* Disable the FIFO0 threshold and the end of sequence interrupt. */
159855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_IRQENABLE_CLR,
160651f7544SOleksandr Tymoshenko 	    ADC_IRQ_FIFO0_THRES | ADC_IRQ_FIFO1_THRES | ADC_IRQ_END_OF_SEQ);
161855feb68SLuiz Otavio O Souza 
162855feb68SLuiz Otavio O Souza 	/* ACK any pending interrupt. */
163855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_IRQSTATUS, ADC_READ4(sc, ADC_IRQSTATUS));
164855feb68SLuiz Otavio O Souza 
165855feb68SLuiz Otavio O Souza 	/* Drain the FIFO data. */
166855feb68SLuiz Otavio O Souza 	count = ADC_READ4(sc, ADC_FIFO0COUNT) & ADC_FIFO_COUNT_MSK;
167855feb68SLuiz Otavio O Souza 	while (count > 0) {
168855feb68SLuiz Otavio O Souza 		data = ADC_READ4(sc, ADC_FIFO0DATA);
169855feb68SLuiz Otavio O Souza 		count = ADC_READ4(sc, ADC_FIFO0COUNT) & ADC_FIFO_COUNT_MSK;
170855feb68SLuiz Otavio O Souza 	}
171855feb68SLuiz Otavio O Souza 
172651f7544SOleksandr Tymoshenko 	count = ADC_READ4(sc, ADC_FIFO1COUNT) & ADC_FIFO_COUNT_MSK;
173651f7544SOleksandr Tymoshenko 	while (count > 0) {
174651f7544SOleksandr Tymoshenko 		data = ADC_READ4(sc, ADC_FIFO1DATA);
175651f7544SOleksandr Tymoshenko 		count = ADC_READ4(sc, ADC_FIFO1COUNT) & ADC_FIFO_COUNT_MSK;
176651f7544SOleksandr Tymoshenko 	}
177651f7544SOleksandr Tymoshenko 
178855feb68SLuiz Otavio O Souza 	sc->sc_last_state = 0;
179855feb68SLuiz Otavio O Souza }
180855feb68SLuiz Otavio O Souza 
181855feb68SLuiz Otavio O Souza static int
182855feb68SLuiz Otavio O Souza ti_adc_setup(struct ti_adc_softc *sc)
183855feb68SLuiz Otavio O Souza {
184651f7544SOleksandr Tymoshenko 	int ain, i;
185855feb68SLuiz Otavio O Souza 	uint32_t enabled;
186855feb68SLuiz Otavio O Souza 
187855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
188855feb68SLuiz Otavio O Souza 
189855feb68SLuiz Otavio O Souza 	/* Check for enabled inputs. */
190651f7544SOleksandr Tymoshenko 	enabled = sc->sc_tsc_enabled;
191651f7544SOleksandr Tymoshenko 	for (i = 0; i < sc->sc_adc_nchannels; i++) {
192651f7544SOleksandr Tymoshenko 		ain = sc->sc_adc_channels[i];
193855feb68SLuiz Otavio O Souza 		if (ti_adc_inputs[ain].enable)
194855feb68SLuiz Otavio O Souza 			enabled |= (1U << (ain + 1));
195855feb68SLuiz Otavio O Souza 	}
196855feb68SLuiz Otavio O Souza 
197855feb68SLuiz Otavio O Souza 	/* Set the ADC global status. */
198855feb68SLuiz Otavio O Souza 	if (enabled != 0) {
199855feb68SLuiz Otavio O Souza 		ti_adc_enable(sc);
200855feb68SLuiz Otavio O Souza 		/* Update the enabled steps. */
201855feb68SLuiz Otavio O Souza 		if (enabled != ADC_READ4(sc, ADC_STEPENABLE))
202855feb68SLuiz Otavio O Souza 			ADC_WRITE4(sc, ADC_STEPENABLE, enabled);
203855feb68SLuiz Otavio O Souza 	} else
204855feb68SLuiz Otavio O Souza 		ti_adc_disable(sc);
205855feb68SLuiz Otavio O Souza 
206855feb68SLuiz Otavio O Souza 	return (0);
207855feb68SLuiz Otavio O Souza }
208855feb68SLuiz Otavio O Souza 
209855feb68SLuiz Otavio O Souza static void
210855feb68SLuiz Otavio O Souza ti_adc_input_setup(struct ti_adc_softc *sc, int32_t ain)
211855feb68SLuiz Otavio O Souza {
212855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
213855feb68SLuiz Otavio O Souza 	uint32_t reg, val;
214855feb68SLuiz Otavio O Souza 
215855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
216855feb68SLuiz Otavio O Souza 
217855feb68SLuiz Otavio O Souza 	input = &ti_adc_inputs[ain];
218855feb68SLuiz Otavio O Souza 	reg = input->stepconfig;
219855feb68SLuiz Otavio O Souza 	val = ADC_READ4(sc, reg);
220855feb68SLuiz Otavio O Souza 
221855feb68SLuiz Otavio O Souza 	/* Set single ended operation. */
222855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_DIFF_CNTRL;
223855feb68SLuiz Otavio O Souza 
224855feb68SLuiz Otavio O Souza 	/* Set the negative voltage reference. */
225855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_RFM_MSK;
226855feb68SLuiz Otavio O Souza 
227855feb68SLuiz Otavio O Souza 	/* Set the positive voltage reference. */
228855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_RFP_MSK;
229855feb68SLuiz Otavio O Souza 
230855feb68SLuiz Otavio O Souza 	/* Set the samples average. */
231855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_AVG_MSK;
232855feb68SLuiz Otavio O Souza 	val |= input->samples << ADC_STEP_AVG_SHIFT;
233855feb68SLuiz Otavio O Souza 
234855feb68SLuiz Otavio O Souza 	/* Select the desired input. */
235855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_INP_MSK;
236855feb68SLuiz Otavio O Souza 	val |= ain << ADC_STEP_INP_SHIFT;
237855feb68SLuiz Otavio O Souza 
238855feb68SLuiz Otavio O Souza 	/* Set the ADC to one-shot mode. */
239855feb68SLuiz Otavio O Souza 	val &= ~ADC_STEP_MODE_MSK;
240855feb68SLuiz Otavio O Souza 
241855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, reg, val);
242855feb68SLuiz Otavio O Souza }
243855feb68SLuiz Otavio O Souza 
244855feb68SLuiz Otavio O Souza static void
245855feb68SLuiz Otavio O Souza ti_adc_reset(struct ti_adc_softc *sc)
246855feb68SLuiz Otavio O Souza {
247651f7544SOleksandr Tymoshenko 	int ain, i;
248855feb68SLuiz Otavio O Souza 
249855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
250855feb68SLuiz Otavio O Souza 
251855feb68SLuiz Otavio O Souza 	/* Disable all the inputs. */
252651f7544SOleksandr Tymoshenko 	for (i = 0; i < sc->sc_adc_nchannels; i++) {
253651f7544SOleksandr Tymoshenko 		ain = sc->sc_adc_channels[i];
254855feb68SLuiz Otavio O Souza 		ti_adc_inputs[ain].enable = 0;
255855feb68SLuiz Otavio O Souza 	}
256651f7544SOleksandr Tymoshenko }
257855feb68SLuiz Otavio O Souza 
258855feb68SLuiz Otavio O Souza static int
259855feb68SLuiz Otavio O Souza ti_adc_clockdiv_proc(SYSCTL_HANDLER_ARGS)
260855feb68SLuiz Otavio O Souza {
261855feb68SLuiz Otavio O Souza 	int error, reg;
262855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
263855feb68SLuiz Otavio O Souza 
264855feb68SLuiz Otavio O Souza 	sc = (struct ti_adc_softc *)arg1;
265855feb68SLuiz Otavio O Souza 
266855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
267855feb68SLuiz Otavio O Souza 	reg = (int)ADC_READ4(sc, ADC_CLKDIV) + 1;
268855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
269855feb68SLuiz Otavio O Souza 
270855feb68SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
271855feb68SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
272855feb68SLuiz Otavio O Souza 		return (error);
273855feb68SLuiz Otavio O Souza 
274855feb68SLuiz Otavio O Souza 	/*
275855feb68SLuiz Otavio O Souza 	 * The actual written value is the prescaler setting - 1.
276855feb68SLuiz Otavio O Souza 	 * Enforce a minimum value of 10 (i.e. 9) which limits the maximum
277855feb68SLuiz Otavio O Souza 	 * ADC clock to ~2.4Mhz (CLK_M_OSC / 10).
278855feb68SLuiz Otavio O Souza 	 */
279855feb68SLuiz Otavio O Souza 	reg--;
280855feb68SLuiz Otavio O Souza 	if (reg < 9)
281855feb68SLuiz Otavio O Souza 		reg = 9;
282855feb68SLuiz Otavio O Souza 	if (reg > USHRT_MAX)
283855feb68SLuiz Otavio O Souza 		reg = USHRT_MAX;
284855feb68SLuiz Otavio O Souza 
285855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
286855feb68SLuiz Otavio O Souza 	/* Disable the ADC. */
287855feb68SLuiz Otavio O Souza 	ti_adc_disable(sc);
288855feb68SLuiz Otavio O Souza 	/* Update the ADC prescaler setting. */
289855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_CLKDIV, reg);
290855feb68SLuiz Otavio O Souza 	/* Enable the ADC again. */
291855feb68SLuiz Otavio O Souza 	ti_adc_setup(sc);
292855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
293855feb68SLuiz Otavio O Souza 
294855feb68SLuiz Otavio O Souza 	return (0);
295855feb68SLuiz Otavio O Souza }
296855feb68SLuiz Otavio O Souza 
297855feb68SLuiz Otavio O Souza static int
298855feb68SLuiz Otavio O Souza ti_adc_enable_proc(SYSCTL_HANDLER_ARGS)
299855feb68SLuiz Otavio O Souza {
300855feb68SLuiz Otavio O Souza 	int error;
301855feb68SLuiz Otavio O Souza 	int32_t enable;
302855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
303855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
304855feb68SLuiz Otavio O Souza 
305855feb68SLuiz Otavio O Souza 	input = (struct ti_adc_input *)arg1;
306855feb68SLuiz Otavio O Souza 	sc = input->sc;
307855feb68SLuiz Otavio O Souza 
308855feb68SLuiz Otavio O Souza 	enable = input->enable;
309855feb68SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &enable, sizeof(enable),
310855feb68SLuiz Otavio O Souza 	    req);
311855feb68SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
312855feb68SLuiz Otavio O Souza 		return (error);
313855feb68SLuiz Otavio O Souza 
314855feb68SLuiz Otavio O Souza 	if (enable)
315855feb68SLuiz Otavio O Souza 		enable = 1;
316855feb68SLuiz Otavio O Souza 
317855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
318855feb68SLuiz Otavio O Souza 	/* Setup the ADC as needed. */
319855feb68SLuiz Otavio O Souza 	if (input->enable != enable) {
320855feb68SLuiz Otavio O Souza 		input->enable = enable;
321855feb68SLuiz Otavio O Souza 		ti_adc_setup(sc);
322855feb68SLuiz Otavio O Souza 		if (input->enable == 0)
323855feb68SLuiz Otavio O Souza 			input->value = 0;
324855feb68SLuiz Otavio O Souza 	}
325855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
326855feb68SLuiz Otavio O Souza 
327855feb68SLuiz Otavio O Souza 	return (0);
328855feb68SLuiz Otavio O Souza }
329855feb68SLuiz Otavio O Souza 
330855feb68SLuiz Otavio O Souza static int
331855feb68SLuiz Otavio O Souza ti_adc_open_delay_proc(SYSCTL_HANDLER_ARGS)
332855feb68SLuiz Otavio O Souza {
333855feb68SLuiz Otavio O Souza 	int error, reg;
334855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
335855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
336855feb68SLuiz Otavio O Souza 
337855feb68SLuiz Otavio O Souza 	input = (struct ti_adc_input *)arg1;
338855feb68SLuiz Otavio O Souza 	sc = input->sc;
339855feb68SLuiz Otavio O Souza 
340855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
341855feb68SLuiz Otavio O Souza 	reg = (int)ADC_READ4(sc, input->stepdelay) & ADC_STEP_OPEN_DELAY;
342855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
343855feb68SLuiz Otavio O Souza 
344855feb68SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
345855feb68SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
346855feb68SLuiz Otavio O Souza 		return (error);
347855feb68SLuiz Otavio O Souza 
348855feb68SLuiz Otavio O Souza 	if (reg < 0)
349855feb68SLuiz Otavio O Souza 		reg = 0;
350855feb68SLuiz Otavio O Souza 
351855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
352855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, input->stepdelay, reg & ADC_STEP_OPEN_DELAY);
353855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
354855feb68SLuiz Otavio O Souza 
355855feb68SLuiz Otavio O Souza 	return (0);
356855feb68SLuiz Otavio O Souza }
357855feb68SLuiz Otavio O Souza 
358855feb68SLuiz Otavio O Souza static int
359855feb68SLuiz Otavio O Souza ti_adc_samples_avg_proc(SYSCTL_HANDLER_ARGS)
360855feb68SLuiz Otavio O Souza {
361855feb68SLuiz Otavio O Souza 	int error, samples, i;
362855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
363855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
364855feb68SLuiz Otavio O Souza 
365855feb68SLuiz Otavio O Souza 	input = (struct ti_adc_input *)arg1;
366855feb68SLuiz Otavio O Souza 	sc = input->sc;
367855feb68SLuiz Otavio O Souza 
368855feb68SLuiz Otavio O Souza 	if (input->samples > nitems(ti_adc_samples))
369855feb68SLuiz Otavio O Souza 		input->samples = nitems(ti_adc_samples);
370855feb68SLuiz Otavio O Souza 	samples = ti_adc_samples[input->samples];
371855feb68SLuiz Otavio O Souza 
372855feb68SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &samples, 0, req);
373855feb68SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
374855feb68SLuiz Otavio O Souza 		return (error);
375855feb68SLuiz Otavio O Souza 
376855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
377855feb68SLuiz Otavio O Souza 	if (samples != ti_adc_samples[input->samples]) {
378855feb68SLuiz Otavio O Souza 		input->samples = 0;
379855feb68SLuiz Otavio O Souza 		for (i = 0; i < nitems(ti_adc_samples); i++)
380855feb68SLuiz Otavio O Souza 			if (samples >= ti_adc_samples[i])
381855feb68SLuiz Otavio O Souza 				input->samples = i;
382855feb68SLuiz Otavio O Souza 		ti_adc_input_setup(sc, input->input);
383855feb68SLuiz Otavio O Souza 	}
384855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
385855feb68SLuiz Otavio O Souza 
386855feb68SLuiz Otavio O Souza 	return (error);
387855feb68SLuiz Otavio O Souza }
388855feb68SLuiz Otavio O Souza 
389855feb68SLuiz Otavio O Souza static void
390855feb68SLuiz Otavio O Souza ti_adc_read_data(struct ti_adc_softc *sc)
391855feb68SLuiz Otavio O Souza {
392855feb68SLuiz Otavio O Souza 	int count, ain;
393855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
394855feb68SLuiz Otavio O Souza 	uint32_t data;
395855feb68SLuiz Otavio O Souza 
396855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_ASSERT(sc);
397855feb68SLuiz Otavio O Souza 
398855feb68SLuiz Otavio O Souza 	/* Read the available data. */
399855feb68SLuiz Otavio O Souza 	count = ADC_READ4(sc, ADC_FIFO0COUNT) & ADC_FIFO_COUNT_MSK;
400855feb68SLuiz Otavio O Souza 	while (count > 0) {
401855feb68SLuiz Otavio O Souza 		data = ADC_READ4(sc, ADC_FIFO0DATA);
402855feb68SLuiz Otavio O Souza 		ain = (data & ADC_FIFO_STEP_ID_MSK) >> ADC_FIFO_STEP_ID_SHIFT;
403855feb68SLuiz Otavio O Souza 		input = &ti_adc_inputs[ain];
404855feb68SLuiz Otavio O Souza 		if (input->enable == 0)
405855feb68SLuiz Otavio O Souza 			input->value = 0;
406855feb68SLuiz Otavio O Souza 		else
407855feb68SLuiz Otavio O Souza 			input->value = (int32_t)(data & ADC_FIFO_DATA_MSK);
408855feb68SLuiz Otavio O Souza 		count = ADC_READ4(sc, ADC_FIFO0COUNT) & ADC_FIFO_COUNT_MSK;
409855feb68SLuiz Otavio O Souza 	}
410855feb68SLuiz Otavio O Souza }
411855feb68SLuiz Otavio O Souza 
412651f7544SOleksandr Tymoshenko static int
413651f7544SOleksandr Tymoshenko cmp_values(const void *a, const void *b)
414651f7544SOleksandr Tymoshenko {
415651f7544SOleksandr Tymoshenko 	const uint32_t *v1, *v2;
416651f7544SOleksandr Tymoshenko 	v1 = a;
417651f7544SOleksandr Tymoshenko 	v2 = b;
418651f7544SOleksandr Tymoshenko 	if (*v1 < *v2)
419651f7544SOleksandr Tymoshenko 		return -1;
420651f7544SOleksandr Tymoshenko 	if (*v1 > *v2)
421651f7544SOleksandr Tymoshenko 		return 1;
422651f7544SOleksandr Tymoshenko 
423651f7544SOleksandr Tymoshenko 	return (0);
424651f7544SOleksandr Tymoshenko }
425651f7544SOleksandr Tymoshenko 
426651f7544SOleksandr Tymoshenko static void
427651f7544SOleksandr Tymoshenko ti_adc_tsc_read_data(struct ti_adc_softc *sc)
428651f7544SOleksandr Tymoshenko {
429651f7544SOleksandr Tymoshenko 	int count;
430651f7544SOleksandr Tymoshenko 	uint32_t data[16];
431651f7544SOleksandr Tymoshenko 	uint32_t x, y;
432651f7544SOleksandr Tymoshenko 	int i, start, end;
433651f7544SOleksandr Tymoshenko 
434651f7544SOleksandr Tymoshenko 	TI_ADC_LOCK_ASSERT(sc);
435651f7544SOleksandr Tymoshenko 
436651f7544SOleksandr Tymoshenko 	/* Read the available data. */
437651f7544SOleksandr Tymoshenko 	count = ADC_READ4(sc, ADC_FIFO1COUNT) & ADC_FIFO_COUNT_MSK;
438651f7544SOleksandr Tymoshenko 	if (count == 0)
439651f7544SOleksandr Tymoshenko 		return;
440651f7544SOleksandr Tymoshenko 
441651f7544SOleksandr Tymoshenko 	i = 0;
442651f7544SOleksandr Tymoshenko 	while (count > 0) {
443651f7544SOleksandr Tymoshenko 		data[i++] = ADC_READ4(sc, ADC_FIFO1DATA) & ADC_FIFO_DATA_MSK;
444651f7544SOleksandr Tymoshenko 		count = ADC_READ4(sc, ADC_FIFO1COUNT) & ADC_FIFO_COUNT_MSK;
445651f7544SOleksandr Tymoshenko 	}
446651f7544SOleksandr Tymoshenko 
447651f7544SOleksandr Tymoshenko 	if (sc->sc_coord_readouts > 3) {
448651f7544SOleksandr Tymoshenko 		start = 1;
449651f7544SOleksandr Tymoshenko 		end = sc->sc_coord_readouts - 1;
450651f7544SOleksandr Tymoshenko 		qsort(data, sc->sc_coord_readouts,
451651f7544SOleksandr Tymoshenko 			sizeof(data[0]), &cmp_values);
452651f7544SOleksandr Tymoshenko 		qsort(&data[sc->sc_coord_readouts + 2],
453651f7544SOleksandr Tymoshenko 			sc->sc_coord_readouts,
454651f7544SOleksandr Tymoshenko 			sizeof(data[0]), &cmp_values);
455651f7544SOleksandr Tymoshenko 	}
456651f7544SOleksandr Tymoshenko 	else {
457651f7544SOleksandr Tymoshenko 		start = 0;
458651f7544SOleksandr Tymoshenko 		end = sc->sc_coord_readouts;
459651f7544SOleksandr Tymoshenko 	}
460651f7544SOleksandr Tymoshenko 
461651f7544SOleksandr Tymoshenko 	x = y = 0;
462651f7544SOleksandr Tymoshenko 	for (i = start; i < end; i++)
463651f7544SOleksandr Tymoshenko 		y += data[i];
464651f7544SOleksandr Tymoshenko 	y /= (end - start);
465651f7544SOleksandr Tymoshenko 
466651f7544SOleksandr Tymoshenko 	for (i = sc->sc_coord_readouts + 2 + start; i < sc->sc_coord_readouts + 2 + end; i++)
467651f7544SOleksandr Tymoshenko 		x += data[i];
468651f7544SOleksandr Tymoshenko 	x /= (end - start);
469651f7544SOleksandr Tymoshenko 
470651f7544SOleksandr Tymoshenko #ifdef DEBUG_TSC
471651f7544SOleksandr Tymoshenko 	device_printf(sc->sc_dev, "touchscreen x: %d, y: %d\n", x, y);
472651f7544SOleksandr Tymoshenko #endif
473fbeb453bSOleksandr Tymoshenko 
474a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
475fbeb453bSOleksandr Tymoshenko 	if ((sc->sc_x != x) || (sc->sc_y != y)) {
476fbeb453bSOleksandr Tymoshenko 		sc->sc_x = x;
477fbeb453bSOleksandr Tymoshenko 		sc->sc_y = y;
478fbeb453bSOleksandr Tymoshenko 		ti_adc_ev_report(sc);
479fbeb453bSOleksandr Tymoshenko 	}
480fbeb453bSOleksandr Tymoshenko #endif
481651f7544SOleksandr Tymoshenko }
482651f7544SOleksandr Tymoshenko 
483651f7544SOleksandr Tymoshenko static void
484651f7544SOleksandr Tymoshenko ti_adc_intr_locked(struct ti_adc_softc *sc, uint32_t status)
485651f7544SOleksandr Tymoshenko {
486651f7544SOleksandr Tymoshenko 	/* Read the available data. */
487651f7544SOleksandr Tymoshenko 	if (status & ADC_IRQ_FIFO0_THRES)
488651f7544SOleksandr Tymoshenko 		ti_adc_read_data(sc);
489651f7544SOleksandr Tymoshenko }
490651f7544SOleksandr Tymoshenko 
491651f7544SOleksandr Tymoshenko static void
492651f7544SOleksandr Tymoshenko ti_adc_tsc_intr_locked(struct ti_adc_softc *sc, uint32_t status)
493651f7544SOleksandr Tymoshenko {
494651f7544SOleksandr Tymoshenko 	/* Read the available data. */
495651f7544SOleksandr Tymoshenko 	if (status & ADC_IRQ_FIFO1_THRES)
496651f7544SOleksandr Tymoshenko 		ti_adc_tsc_read_data(sc);
497651f7544SOleksandr Tymoshenko 
498651f7544SOleksandr Tymoshenko }
499651f7544SOleksandr Tymoshenko 
500855feb68SLuiz Otavio O Souza static void
501855feb68SLuiz Otavio O Souza ti_adc_intr(void *arg)
502855feb68SLuiz Otavio O Souza {
503855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
504651f7544SOleksandr Tymoshenko 	uint32_t status, rawstatus;
505855feb68SLuiz Otavio O Souza 
506855feb68SLuiz Otavio O Souza 	sc = (struct ti_adc_softc *)arg;
507855feb68SLuiz Otavio O Souza 
508855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
509651f7544SOleksandr Tymoshenko 
510651f7544SOleksandr Tymoshenko 	rawstatus = ADC_READ4(sc, ADC_IRQSTATUS_RAW);
511651f7544SOleksandr Tymoshenko 	status = ADC_READ4(sc, ADC_IRQSTATUS);
512651f7544SOleksandr Tymoshenko 
513651f7544SOleksandr Tymoshenko 	if (rawstatus & ADC_IRQ_HW_PEN_ASYNC) {
514651f7544SOleksandr Tymoshenko 		sc->sc_pen_down = 1;
515651f7544SOleksandr Tymoshenko 		status |= ADC_IRQ_HW_PEN_ASYNC;
516651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_IRQENABLE_CLR,
517651f7544SOleksandr Tymoshenko 			ADC_IRQ_HW_PEN_ASYNC);
518a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
519fbeb453bSOleksandr Tymoshenko 		ti_adc_ev_report(sc);
520fbeb453bSOleksandr Tymoshenko #endif
521651f7544SOleksandr Tymoshenko 	}
522651f7544SOleksandr Tymoshenko 
523651f7544SOleksandr Tymoshenko 	if (rawstatus & ADC_IRQ_PEN_UP) {
524651f7544SOleksandr Tymoshenko 		sc->sc_pen_down = 0;
525651f7544SOleksandr Tymoshenko 		status |= ADC_IRQ_PEN_UP;
526a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
527fbeb453bSOleksandr Tymoshenko 		ti_adc_ev_report(sc);
528fbeb453bSOleksandr Tymoshenko #endif
529651f7544SOleksandr Tymoshenko 	}
530651f7544SOleksandr Tymoshenko 
531651f7544SOleksandr Tymoshenko 	if (status & ADC_IRQ_FIFO0_THRES)
532651f7544SOleksandr Tymoshenko 		ti_adc_intr_locked(sc, status);
533651f7544SOleksandr Tymoshenko 
534651f7544SOleksandr Tymoshenko 	if (status & ADC_IRQ_FIFO1_THRES)
535651f7544SOleksandr Tymoshenko 		ti_adc_tsc_intr_locked(sc, status);
536651f7544SOleksandr Tymoshenko 
537651f7544SOleksandr Tymoshenko 	if (status) {
538855feb68SLuiz Otavio O Souza 		/* ACK the interrupt. */
539855feb68SLuiz Otavio O Souza 		ADC_WRITE4(sc, ADC_IRQSTATUS, status);
540651f7544SOleksandr Tymoshenko 	}
541855feb68SLuiz Otavio O Souza 
542855feb68SLuiz Otavio O Souza 	/* Start the next conversion ? */
543855feb68SLuiz Otavio O Souza 	if (status & ADC_IRQ_END_OF_SEQ)
544855feb68SLuiz Otavio O Souza 		ti_adc_setup(sc);
545651f7544SOleksandr Tymoshenko 
546855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
547855feb68SLuiz Otavio O Souza }
548855feb68SLuiz Otavio O Souza 
549855feb68SLuiz Otavio O Souza static void
550855feb68SLuiz Otavio O Souza ti_adc_sysctl_init(struct ti_adc_softc *sc)
551855feb68SLuiz Otavio O Souza {
552855feb68SLuiz Otavio O Souza 	char pinbuf[3];
553855feb68SLuiz Otavio O Souza 	struct sysctl_ctx_list *ctx;
554855feb68SLuiz Otavio O Souza 	struct sysctl_oid *tree_node, *inp_node, *inpN_node;
555855feb68SLuiz Otavio O Souza 	struct sysctl_oid_list *tree, *inp_tree, *inpN_tree;
556651f7544SOleksandr Tymoshenko 	int ain, i;
557855feb68SLuiz Otavio O Souza 
558855feb68SLuiz Otavio O Souza 	/*
559855feb68SLuiz Otavio O Souza 	 * Add per-pin sysctl tree/handlers.
560855feb68SLuiz Otavio O Souza 	 */
561855feb68SLuiz Otavio O Souza 	ctx = device_get_sysctl_ctx(sc->sc_dev);
562855feb68SLuiz Otavio O Souza 	tree_node = device_get_sysctl_tree(sc->sc_dev);
563855feb68SLuiz Otavio O Souza 	tree = SYSCTL_CHILDREN(tree_node);
564855feb68SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clockdiv",
5657029da5cSPawel Biernacki 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,  sc, 0,
566855feb68SLuiz Otavio O Souza 	    ti_adc_clockdiv_proc, "IU", "ADC clock prescaler");
567855feb68SLuiz Otavio O Souza 	inp_node = SYSCTL_ADD_NODE(ctx, tree, OID_AUTO, "ain",
5687029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ADC inputs");
569855feb68SLuiz Otavio O Souza 	inp_tree = SYSCTL_CHILDREN(inp_node);
570855feb68SLuiz Otavio O Souza 
571651f7544SOleksandr Tymoshenko 	for (i = 0; i < sc->sc_adc_nchannels; i++) {
572651f7544SOleksandr Tymoshenko 		ain = sc->sc_adc_channels[i];
573855feb68SLuiz Otavio O Souza 
574855feb68SLuiz Otavio O Souza 		snprintf(pinbuf, sizeof(pinbuf), "%d", ain);
575855feb68SLuiz Otavio O Souza 		inpN_node = SYSCTL_ADD_NODE(ctx, inp_tree, OID_AUTO, pinbuf,
5767029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ADC input");
577855feb68SLuiz Otavio O Souza 		inpN_tree = SYSCTL_CHILDREN(inpN_node);
578855feb68SLuiz Otavio O Souza 
579855feb68SLuiz Otavio O Souza 		SYSCTL_ADD_PROC(ctx, inpN_tree, OID_AUTO, "enable",
5807029da5cSPawel Biernacki 		    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
5817029da5cSPawel Biernacki 		    &ti_adc_inputs[ain], 0,
582855feb68SLuiz Otavio O Souza 		    ti_adc_enable_proc, "IU", "Enable ADC input");
583855feb68SLuiz Otavio O Souza 		SYSCTL_ADD_PROC(ctx, inpN_tree, OID_AUTO, "open_delay",
5847029da5cSPawel Biernacki 		    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
5857029da5cSPawel Biernacki 		    &ti_adc_inputs[ain], 0,
586855feb68SLuiz Otavio O Souza 		    ti_adc_open_delay_proc, "IU", "ADC open delay");
587855feb68SLuiz Otavio O Souza 		SYSCTL_ADD_PROC(ctx, inpN_tree, OID_AUTO, "samples_avg",
5887029da5cSPawel Biernacki 		    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
5897029da5cSPawel Biernacki 		    &ti_adc_inputs[ain], 0,
590855feb68SLuiz Otavio O Souza 		    ti_adc_samples_avg_proc, "IU", "ADC samples average");
591855feb68SLuiz Otavio O Souza 		SYSCTL_ADD_INT(ctx, inpN_tree, OID_AUTO, "input",
592855feb68SLuiz Otavio O Souza 		    CTLFLAG_RD, &ti_adc_inputs[ain].value, 0,
593855feb68SLuiz Otavio O Souza 		    "Converted raw value for the ADC input");
594855feb68SLuiz Otavio O Souza 	}
595855feb68SLuiz Otavio O Souza }
596855feb68SLuiz Otavio O Souza 
597855feb68SLuiz Otavio O Souza static void
598855feb68SLuiz Otavio O Souza ti_adc_inputs_init(struct ti_adc_softc *sc)
599855feb68SLuiz Otavio O Souza {
600651f7544SOleksandr Tymoshenko 	int ain, i;
601855feb68SLuiz Otavio O Souza 	struct ti_adc_input *input;
602855feb68SLuiz Otavio O Souza 
603855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
604651f7544SOleksandr Tymoshenko 	for (i = 0; i < sc->sc_adc_nchannels; i++) {
605651f7544SOleksandr Tymoshenko 		ain = sc->sc_adc_channels[i];
606855feb68SLuiz Otavio O Souza 		input = &ti_adc_inputs[ain];
607855feb68SLuiz Otavio O Souza 		input->sc = sc;
608855feb68SLuiz Otavio O Souza 		input->input = ain;
609855feb68SLuiz Otavio O Souza 		input->value = 0;
610855feb68SLuiz Otavio O Souza 		input->enable = 0;
611855feb68SLuiz Otavio O Souza 		input->samples = 0;
612855feb68SLuiz Otavio O Souza 		ti_adc_input_setup(sc, ain);
613855feb68SLuiz Otavio O Souza 	}
614855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
615855feb68SLuiz Otavio O Souza }
616855feb68SLuiz Otavio O Souza 
617855feb68SLuiz Otavio O Souza static void
618651f7544SOleksandr Tymoshenko ti_adc_tsc_init(struct ti_adc_softc *sc)
619651f7544SOleksandr Tymoshenko {
620651f7544SOleksandr Tymoshenko 	int i, start_step, end_step;
621651f7544SOleksandr Tymoshenko 	uint32_t stepconfig, val;
622651f7544SOleksandr Tymoshenko 
623651f7544SOleksandr Tymoshenko 	TI_ADC_LOCK(sc);
624651f7544SOleksandr Tymoshenko 
625651f7544SOleksandr Tymoshenko 	/* X coordinates */
626651f7544SOleksandr Tymoshenko 	stepconfig = ADC_STEP_FIFO1 | (4 << ADC_STEP_AVG_SHIFT) |
627651f7544SOleksandr Tymoshenko 	    ADC_STEP_MODE_HW_ONESHOT | sc->sc_xp_bit;
628651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires == 4)
629651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(sc->sc_yp_inp) | sc->sc_xn_bit;
630651f7544SOleksandr Tymoshenko 	else if (sc->sc_tsc_wires == 5)
631651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(4) |
632651f7544SOleksandr Tymoshenko 			sc->sc_xn_bit | sc->sc_yn_bit | sc->sc_yp_bit;
633651f7544SOleksandr Tymoshenko 	else if (sc->sc_tsc_wires == 8)
634651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(sc->sc_yp_inp) | sc->sc_xn_bit;
635651f7544SOleksandr Tymoshenko 
636651f7544SOleksandr Tymoshenko 	start_step = ADC_STEPS - sc->sc_coord_readouts + 1;
637651f7544SOleksandr Tymoshenko 	end_step = start_step + sc->sc_coord_readouts - 1;
638651f7544SOleksandr Tymoshenko 	for (i = start_step; i <= end_step; i++) {
639651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_STEPCFG(i), stepconfig);
640651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_STEPDLY(i), STEPDLY_OPEN);
641651f7544SOleksandr Tymoshenko 	}
642651f7544SOleksandr Tymoshenko 
643651f7544SOleksandr Tymoshenko 	/* Y coordinates */
644651f7544SOleksandr Tymoshenko 	stepconfig = ADC_STEP_FIFO1 | (4 << ADC_STEP_AVG_SHIFT) |
645651f7544SOleksandr Tymoshenko 	    ADC_STEP_MODE_HW_ONESHOT | sc->sc_yn_bit |
646651f7544SOleksandr Tymoshenko 	    ADC_STEP_INM(8);
647651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires == 4)
648651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(sc->sc_xp_inp) | sc->sc_yp_bit;
649651f7544SOleksandr Tymoshenko 	else if (sc->sc_tsc_wires == 5)
650651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(4) |
651651f7544SOleksandr Tymoshenko 			sc->sc_xp_bit | sc->sc_xn_bit | sc->sc_yp_bit;
652651f7544SOleksandr Tymoshenko 	else if (sc->sc_tsc_wires == 8)
653651f7544SOleksandr Tymoshenko 		stepconfig |= ADC_STEP_INP(sc->sc_xp_inp) | sc->sc_yp_bit;
654651f7544SOleksandr Tymoshenko 
655651f7544SOleksandr Tymoshenko 	start_step = ADC_STEPS - (sc->sc_coord_readouts*2 + 2) + 1;
656651f7544SOleksandr Tymoshenko 	end_step = start_step + sc->sc_coord_readouts - 1;
657651f7544SOleksandr Tymoshenko 	for (i = start_step; i <= end_step; i++) {
658651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_STEPCFG(i), stepconfig);
659651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_STEPDLY(i), STEPDLY_OPEN);
660651f7544SOleksandr Tymoshenko 	}
661651f7544SOleksandr Tymoshenko 
662651f7544SOleksandr Tymoshenko 	/* Charge config */
663651f7544SOleksandr Tymoshenko 	val = ADC_READ4(sc, ADC_IDLECONFIG);
664651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_TC_CHARGE_STEPCONFIG, val);
665651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_TC_CHARGE_DELAY, sc->sc_charge_delay);
666651f7544SOleksandr Tymoshenko 
667651f7544SOleksandr Tymoshenko 	/* 2 steps for Z */
668651f7544SOleksandr Tymoshenko 	start_step = ADC_STEPS - (sc->sc_coord_readouts + 2) + 1;
669651f7544SOleksandr Tymoshenko 	stepconfig = ADC_STEP_FIFO1 | (4 << ADC_STEP_AVG_SHIFT) |
670651f7544SOleksandr Tymoshenko 	    ADC_STEP_MODE_HW_ONESHOT | sc->sc_yp_bit |
671651f7544SOleksandr Tymoshenko 	    sc->sc_xn_bit | ADC_STEP_INP(sc->sc_xp_inp) |
672651f7544SOleksandr Tymoshenko 	    ADC_STEP_INM(8);
673651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_STEPCFG(start_step), stepconfig);
674651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_STEPDLY(start_step), STEPDLY_OPEN);
675651f7544SOleksandr Tymoshenko 	start_step++;
676651f7544SOleksandr Tymoshenko 	stepconfig |= ADC_STEP_INP(sc->sc_yn_inp);
677651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_STEPCFG(start_step), stepconfig);
678651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_STEPDLY(start_step), STEPDLY_OPEN);
679651f7544SOleksandr Tymoshenko 
680651f7544SOleksandr Tymoshenko 	ADC_WRITE4(sc, ADC_FIFO1THRESHOLD, (sc->sc_coord_readouts*2 + 2) - 1);
681651f7544SOleksandr Tymoshenko 
682651f7544SOleksandr Tymoshenko 	sc->sc_tsc_enabled = 1;
683651f7544SOleksandr Tymoshenko 	start_step = ADC_STEPS - (sc->sc_coord_readouts*2 + 2) + 1;
684651f7544SOleksandr Tymoshenko 	end_step = ADC_STEPS;
685651f7544SOleksandr Tymoshenko 	for (i = start_step; i <= end_step; i++) {
686651f7544SOleksandr Tymoshenko 		sc->sc_tsc_enabled |= (1 << i);
687651f7544SOleksandr Tymoshenko 	}
688651f7544SOleksandr Tymoshenko 
689651f7544SOleksandr Tymoshenko 
690651f7544SOleksandr Tymoshenko 	TI_ADC_UNLOCK(sc);
691651f7544SOleksandr Tymoshenko }
692651f7544SOleksandr Tymoshenko 
693651f7544SOleksandr Tymoshenko static void
694855feb68SLuiz Otavio O Souza ti_adc_idlestep_init(struct ti_adc_softc *sc)
695855feb68SLuiz Otavio O Souza {
696855feb68SLuiz Otavio O Souza 	uint32_t val;
697855feb68SLuiz Otavio O Souza 
698651f7544SOleksandr Tymoshenko 	val = ADC_STEP_YNN_SW | ADC_STEP_INM(8) | ADC_STEP_INP(8) | ADC_STEP_YPN_SW;
699855feb68SLuiz Otavio O Souza 
700855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_IDLECONFIG, val);
701855feb68SLuiz Otavio O Souza }
702855feb68SLuiz Otavio O Souza 
703855feb68SLuiz Otavio O Souza static int
704651f7544SOleksandr Tymoshenko ti_adc_config_wires(struct ti_adc_softc *sc, int *wire_configs, int nwire_configs)
705651f7544SOleksandr Tymoshenko {
706651f7544SOleksandr Tymoshenko 	int i;
707651f7544SOleksandr Tymoshenko 	int wire, ai;
708651f7544SOleksandr Tymoshenko 
709651f7544SOleksandr Tymoshenko 	for (i = 0; i < nwire_configs; i++) {
710651f7544SOleksandr Tymoshenko 		wire = wire_configs[i] & 0xf;
711651f7544SOleksandr Tymoshenko 		ai = (wire_configs[i] >> 4) & 0xf;
712651f7544SOleksandr Tymoshenko 		switch (wire) {
713651f7544SOleksandr Tymoshenko 		case ORDER_XP:
714651f7544SOleksandr Tymoshenko 			sc->sc_xp_bit = ADC_STEP_XPP_SW;
715651f7544SOleksandr Tymoshenko 			sc->sc_xp_inp = ai;
716651f7544SOleksandr Tymoshenko 			break;
717651f7544SOleksandr Tymoshenko 		case ORDER_XN:
718651f7544SOleksandr Tymoshenko 			sc->sc_xn_bit = ADC_STEP_XNN_SW;
719651f7544SOleksandr Tymoshenko 			sc->sc_xn_inp = ai;
720651f7544SOleksandr Tymoshenko 			break;
721651f7544SOleksandr Tymoshenko 		case ORDER_YP:
722651f7544SOleksandr Tymoshenko 			sc->sc_yp_bit = ADC_STEP_YPP_SW;
723651f7544SOleksandr Tymoshenko 			sc->sc_yp_inp = ai;
724651f7544SOleksandr Tymoshenko 			break;
725651f7544SOleksandr Tymoshenko 		case ORDER_YN:
726651f7544SOleksandr Tymoshenko 			sc->sc_yn_bit = ADC_STEP_YNN_SW;
727651f7544SOleksandr Tymoshenko 			sc->sc_yn_inp = ai;
728651f7544SOleksandr Tymoshenko 			break;
729651f7544SOleksandr Tymoshenko 		default:
730651f7544SOleksandr Tymoshenko 			device_printf(sc->sc_dev, "Invalid wire config\n");
731651f7544SOleksandr Tymoshenko 			return (-1);
732651f7544SOleksandr Tymoshenko 		}
733651f7544SOleksandr Tymoshenko 	}
734651f7544SOleksandr Tymoshenko 	return (0);
735651f7544SOleksandr Tymoshenko }
736651f7544SOleksandr Tymoshenko 
737651f7544SOleksandr Tymoshenko static int
738855feb68SLuiz Otavio O Souza ti_adc_probe(device_t dev)
739855feb68SLuiz Otavio O Souza {
740855feb68SLuiz Otavio O Souza 
7415b03aba6SOleksandr Tymoshenko 	if (!ofw_bus_is_compatible(dev, "ti,am3359-tscadc"))
742855feb68SLuiz Otavio O Souza 		return (ENXIO);
743855feb68SLuiz Otavio O Souza 	device_set_desc(dev, "TI ADC controller");
744855feb68SLuiz Otavio O Souza 
745855feb68SLuiz Otavio O Souza 	return (BUS_PROBE_DEFAULT);
746855feb68SLuiz Otavio O Souza }
747855feb68SLuiz Otavio O Souza 
748855feb68SLuiz Otavio O Souza static int
749855feb68SLuiz Otavio O Souza ti_adc_attach(device_t dev)
750855feb68SLuiz Otavio O Souza {
751651f7544SOleksandr Tymoshenko 	int err, rid, i;
752855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
753651f7544SOleksandr Tymoshenko 	uint32_t rev, reg;
754651f7544SOleksandr Tymoshenko 	phandle_t node, child;
755651f7544SOleksandr Tymoshenko 	pcell_t cell;
756651f7544SOleksandr Tymoshenko 	int *channels;
757651f7544SOleksandr Tymoshenko 	int nwire_configs;
758651f7544SOleksandr Tymoshenko 	int *wire_configs;
759855feb68SLuiz Otavio O Souza 
760855feb68SLuiz Otavio O Souza 	sc = device_get_softc(dev);
761855feb68SLuiz Otavio O Souza 	sc->sc_dev = dev;
762855feb68SLuiz Otavio O Souza 
763651f7544SOleksandr Tymoshenko 	node = ofw_bus_get_node(dev);
764651f7544SOleksandr Tymoshenko 
765651f7544SOleksandr Tymoshenko 	sc->sc_tsc_wires = 0;
766651f7544SOleksandr Tymoshenko 	sc->sc_coord_readouts = 1;
767651f7544SOleksandr Tymoshenko 	sc->sc_x_plate_resistance = 0;
768651f7544SOleksandr Tymoshenko 	sc->sc_charge_delay = DEFAULT_CHARGE_DELAY;
769651f7544SOleksandr Tymoshenko 	/* Read "tsc" node properties */
770651f7544SOleksandr Tymoshenko 	child = ofw_bus_find_child(node, "tsc");
771d3dd0204SLuiz Otavio O Souza 	if (child != 0 && OF_hasprop(child, "ti,wires")) {
7729783ea5cSAndrew Turner 		if ((OF_getencprop(child, "ti,wires", &cell, sizeof(cell))) > 0)
7739783ea5cSAndrew Turner 			sc->sc_tsc_wires = cell;
7749783ea5cSAndrew Turner 		if ((OF_getencprop(child, "ti,coordinate-readouts", &cell,
7759783ea5cSAndrew Turner 		    sizeof(cell))) > 0)
7769783ea5cSAndrew Turner 			sc->sc_coord_readouts = cell;
7779783ea5cSAndrew Turner 		if ((OF_getencprop(child, "ti,x-plate-resistance", &cell,
7789783ea5cSAndrew Turner 		    sizeof(cell))) > 0)
7799783ea5cSAndrew Turner 			sc->sc_x_plate_resistance = cell;
7809783ea5cSAndrew Turner 		if ((OF_getencprop(child, "ti,charge-delay", &cell,
7819783ea5cSAndrew Turner 		    sizeof(cell))) > 0)
7829783ea5cSAndrew Turner 			sc->sc_charge_delay = cell;
783f7604b1bSOleksandr Tymoshenko 		nwire_configs = OF_getencprop_alloc_multi(child,
784f7604b1bSOleksandr Tymoshenko 		    "ti,wire-config", sizeof(*wire_configs),
785f7604b1bSOleksandr Tymoshenko 		    (void **)&wire_configs);
786651f7544SOleksandr Tymoshenko 		if (nwire_configs != sc->sc_tsc_wires) {
787651f7544SOleksandr Tymoshenko 			device_printf(sc->sc_dev,
788d3dd0204SLuiz Otavio O Souza 			    "invalid number of ti,wire-config: %d (should be %d)\n",
789651f7544SOleksandr Tymoshenko 			    nwire_configs, sc->sc_tsc_wires);
790bc90a48cSOleksandr Tymoshenko 			OF_prop_free(wire_configs);
791651f7544SOleksandr Tymoshenko 			return (EINVAL);
792651f7544SOleksandr Tymoshenko 		}
793651f7544SOleksandr Tymoshenko 		err = ti_adc_config_wires(sc, wire_configs, nwire_configs);
794bc90a48cSOleksandr Tymoshenko 		OF_prop_free(wire_configs);
795e5ee119aSLuiz Otavio O Souza 		if (err)
796651f7544SOleksandr Tymoshenko 			return (EINVAL);
797651f7544SOleksandr Tymoshenko 	}
798651f7544SOleksandr Tymoshenko 
799651f7544SOleksandr Tymoshenko 	/* Read "adc" node properties */
800651f7544SOleksandr Tymoshenko 	child = ofw_bus_find_child(node, "adc");
801651f7544SOleksandr Tymoshenko 	if (child != 0) {
802f7604b1bSOleksandr Tymoshenko 		sc->sc_adc_nchannels = OF_getencprop_alloc_multi(child,
803f7604b1bSOleksandr Tymoshenko 		    "ti,adc-channels", sizeof(*channels), (void **)&channels);
804651f7544SOleksandr Tymoshenko 		if (sc->sc_adc_nchannels > 0) {
805651f7544SOleksandr Tymoshenko 			for (i = 0; i < sc->sc_adc_nchannels; i++)
806651f7544SOleksandr Tymoshenko 				sc->sc_adc_channels[i] = channels[i];
807bc90a48cSOleksandr Tymoshenko 			OF_prop_free(channels);
808651f7544SOleksandr Tymoshenko 		}
809651f7544SOleksandr Tymoshenko 	}
810651f7544SOleksandr Tymoshenko 
811651f7544SOleksandr Tymoshenko 	/* Sanity check FDT data */
812651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires + sc->sc_adc_nchannels > TI_ADC_NPINS) {
813651f7544SOleksandr Tymoshenko 		device_printf(dev, "total number of chanels (%d) is larger than %d\n",
814651f7544SOleksandr Tymoshenko 		    sc->sc_tsc_wires + sc->sc_adc_nchannels, TI_ADC_NPINS);
815651f7544SOleksandr Tymoshenko 		return (ENXIO);
816651f7544SOleksandr Tymoshenko 	}
817e5ee119aSLuiz Otavio O Souza 
818855feb68SLuiz Otavio O Souza 	rid = 0;
819855feb68SLuiz Otavio O Souza 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
820855feb68SLuiz Otavio O Souza 	    RF_ACTIVE);
821855feb68SLuiz Otavio O Souza 	if (!sc->sc_mem_res) {
822855feb68SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate memory window\n");
823855feb68SLuiz Otavio O Souza 		return (ENXIO);
824855feb68SLuiz Otavio O Souza 	}
825855feb68SLuiz Otavio O Souza 
826651f7544SOleksandr Tymoshenko 	/* Activate the ADC_TSC module. */
827*0050ea24SMichal Meloun 	err = ti_sysc_clock_enable(device_get_parent(dev));
828651f7544SOleksandr Tymoshenko 	if (err)
829651f7544SOleksandr Tymoshenko 		return (err);
830651f7544SOleksandr Tymoshenko 
831855feb68SLuiz Otavio O Souza 	rid = 0;
832855feb68SLuiz Otavio O Souza 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
833855feb68SLuiz Otavio O Souza 	    RF_ACTIVE);
834855feb68SLuiz Otavio O Souza 	if (!sc->sc_irq_res) {
835855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
836855feb68SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate interrupt\n");
837855feb68SLuiz Otavio O Souza 		return (ENXIO);
838855feb68SLuiz Otavio O Souza 	}
839855feb68SLuiz Otavio O Souza 
840855feb68SLuiz Otavio O Souza 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
841855feb68SLuiz Otavio O Souza 	    NULL, ti_adc_intr, sc, &sc->sc_intrhand) != 0) {
842855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
843855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
844855feb68SLuiz Otavio O Souza 		device_printf(dev, "Unable to setup the irq handler.\n");
845855feb68SLuiz Otavio O Souza 		return (ENXIO);
846855feb68SLuiz Otavio O Souza 	}
847855feb68SLuiz Otavio O Souza 
848855feb68SLuiz Otavio O Souza 	/* Check the ADC revision. */
849*0050ea24SMichal Meloun 	rev = ADC_READ4(sc, ti_sysc_get_rev_address_offset_host(device_get_parent(dev)));
850855feb68SLuiz Otavio O Souza 	device_printf(dev,
851855feb68SLuiz Otavio O Souza 	    "scheme: %#x func: %#x rtl: %d rev: %d.%d custom rev: %d\n",
852855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_SCHEME_MSK) >> ADC_REV_SCHEME_SHIFT,
853855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_FUNC_MSK) >> ADC_REV_FUNC_SHIFT,
854855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_RTL_MSK) >> ADC_REV_RTL_SHIFT,
855855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_MAJOR_MSK) >> ADC_REV_MAJOR_SHIFT,
856855feb68SLuiz Otavio O Souza 	    rev & ADC_REV_MINOR_MSK,
857855feb68SLuiz Otavio O Souza 	    (rev & ADC_REV_CUSTOM_MSK) >> ADC_REV_CUSTOM_SHIFT);
858855feb68SLuiz Otavio O Souza 
859855feb68SLuiz Otavio O Souza 	reg = ADC_READ4(sc, ADC_CTRL);
860855feb68SLuiz Otavio O Souza 	ADC_WRITE4(sc, ADC_CTRL, reg | ADC_CTRL_STEP_WP | ADC_CTRL_STEP_ID);
861855feb68SLuiz Otavio O Souza 
862855feb68SLuiz Otavio O Souza 	/*
863651f7544SOleksandr Tymoshenko 	 * Set the ADC prescaler to 2400 if touchscreen is not enabled
864651f7544SOleksandr Tymoshenko 	 * and to 24 if it is.  This sets the ADC clock to ~10Khz and
865651f7544SOleksandr Tymoshenko 	 * ~1Mhz respectively (CLK_M_OSC / prescaler).
866855feb68SLuiz Otavio O Souza 	 */
867651f7544SOleksandr Tymoshenko 	if (sc->sc_tsc_wires)
868651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_CLKDIV, 24 - 1);
869651f7544SOleksandr Tymoshenko 	else
870651f7544SOleksandr Tymoshenko 		ADC_WRITE4(sc, ADC_CLKDIV, 2400 - 1);
871855feb68SLuiz Otavio O Souza 
872855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_INIT(sc);
873855feb68SLuiz Otavio O Souza 
874855feb68SLuiz Otavio O Souza 	ti_adc_idlestep_init(sc);
875855feb68SLuiz Otavio O Souza 	ti_adc_inputs_init(sc);
876855feb68SLuiz Otavio O Souza 	ti_adc_sysctl_init(sc);
877651f7544SOleksandr Tymoshenko 	ti_adc_tsc_init(sc);
878651f7544SOleksandr Tymoshenko 
879651f7544SOleksandr Tymoshenko 	TI_ADC_LOCK(sc);
880651f7544SOleksandr Tymoshenko 	ti_adc_setup(sc);
881651f7544SOleksandr Tymoshenko 	TI_ADC_UNLOCK(sc);
882855feb68SLuiz Otavio O Souza 
883a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
884fbeb453bSOleksandr Tymoshenko 	if (sc->sc_tsc_wires > 0) {
885fbeb453bSOleksandr Tymoshenko 		sc->sc_evdev = evdev_alloc();
886fbeb453bSOleksandr Tymoshenko 		evdev_set_name(sc->sc_evdev, device_get_desc(dev));
887fbeb453bSOleksandr Tymoshenko 		evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
888b763171bSOleksandr Tymoshenko 		evdev_set_id(sc->sc_evdev, BUS_VIRTUAL, 0, 0, 0);
889fbeb453bSOleksandr Tymoshenko 		evdev_support_prop(sc->sc_evdev, INPUT_PROP_DIRECT);
890fbeb453bSOleksandr Tymoshenko 		evdev_support_event(sc->sc_evdev, EV_SYN);
891fbeb453bSOleksandr Tymoshenko 		evdev_support_event(sc->sc_evdev, EV_ABS);
892fbeb453bSOleksandr Tymoshenko 		evdev_support_event(sc->sc_evdev, EV_KEY);
893fbeb453bSOleksandr Tymoshenko 
894fbeb453bSOleksandr Tymoshenko 		evdev_support_abs(sc->sc_evdev, ABS_X, 0, 0,
895fbeb453bSOleksandr Tymoshenko 		    ADC_MAX_VALUE, 0, 0, 0);
896fbeb453bSOleksandr Tymoshenko 		evdev_support_abs(sc->sc_evdev, ABS_Y, 0, 0,
897fbeb453bSOleksandr Tymoshenko 		    ADC_MAX_VALUE, 0, 0, 0);
898fbeb453bSOleksandr Tymoshenko 
899fbeb453bSOleksandr Tymoshenko 		evdev_support_key(sc->sc_evdev, BTN_TOUCH);
900fbeb453bSOleksandr Tymoshenko 
901fbeb453bSOleksandr Tymoshenko 		err = evdev_register(sc->sc_evdev);
902fbeb453bSOleksandr Tymoshenko 		if (err) {
903fbeb453bSOleksandr Tymoshenko 			device_printf(dev,
904fbeb453bSOleksandr Tymoshenko 			    "failed to register evdev: error=%d\n", err);
905fbeb453bSOleksandr Tymoshenko 			ti_adc_detach(dev);
906fbeb453bSOleksandr Tymoshenko 			return (err);
907fbeb453bSOleksandr Tymoshenko 		}
908fbeb453bSOleksandr Tymoshenko 
909fbeb453bSOleksandr Tymoshenko 		sc->sc_pen_down = 0;
910fbeb453bSOleksandr Tymoshenko 		sc->sc_x = -1;
911fbeb453bSOleksandr Tymoshenko 		sc->sc_y = -1;
912fbeb453bSOleksandr Tymoshenko 	}
913fbeb453bSOleksandr Tymoshenko #endif /* EVDEV */
914fbeb453bSOleksandr Tymoshenko 
915855feb68SLuiz Otavio O Souza 	return (0);
916855feb68SLuiz Otavio O Souza }
917855feb68SLuiz Otavio O Souza 
918855feb68SLuiz Otavio O Souza static int
919855feb68SLuiz Otavio O Souza ti_adc_detach(device_t dev)
920855feb68SLuiz Otavio O Souza {
921855feb68SLuiz Otavio O Souza 	struct ti_adc_softc *sc;
922855feb68SLuiz Otavio O Souza 
923855feb68SLuiz Otavio O Souza 	sc = device_get_softc(dev);
924855feb68SLuiz Otavio O Souza 
925855feb68SLuiz Otavio O Souza 	/* Turn off the ADC. */
926855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK(sc);
927855feb68SLuiz Otavio O Souza 	ti_adc_reset(sc);
928855feb68SLuiz Otavio O Souza 	ti_adc_setup(sc);
929fbeb453bSOleksandr Tymoshenko 
930a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
931fbeb453bSOleksandr Tymoshenko 	evdev_free(sc->sc_evdev);
932fbeb453bSOleksandr Tymoshenko #endif
933fbeb453bSOleksandr Tymoshenko 
934855feb68SLuiz Otavio O Souza 	TI_ADC_UNLOCK(sc);
935855feb68SLuiz Otavio O Souza 
936855feb68SLuiz Otavio O Souza 	TI_ADC_LOCK_DESTROY(sc);
937855feb68SLuiz Otavio O Souza 
938855feb68SLuiz Otavio O Souza 	if (sc->sc_intrhand)
939855feb68SLuiz Otavio O Souza 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
940855feb68SLuiz Otavio O Souza 	if (sc->sc_irq_res)
941855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
942855feb68SLuiz Otavio O Souza 	if (sc->sc_mem_res)
943855feb68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
944855feb68SLuiz Otavio O Souza 
945855feb68SLuiz Otavio O Souza 	return (bus_generic_detach(dev));
946855feb68SLuiz Otavio O Souza }
947855feb68SLuiz Otavio O Souza 
948855feb68SLuiz Otavio O Souza static device_method_t ti_adc_methods[] = {
949855feb68SLuiz Otavio O Souza 	DEVMETHOD(device_probe,		ti_adc_probe),
950855feb68SLuiz Otavio O Souza 	DEVMETHOD(device_attach,	ti_adc_attach),
951855feb68SLuiz Otavio O Souza 	DEVMETHOD(device_detach,	ti_adc_detach),
952855feb68SLuiz Otavio O Souza 
953855feb68SLuiz Otavio O Souza 	DEVMETHOD_END
954855feb68SLuiz Otavio O Souza };
955855feb68SLuiz Otavio O Souza 
956855feb68SLuiz Otavio O Souza static driver_t ti_adc_driver = {
957855feb68SLuiz Otavio O Souza 	"ti_adc",
958855feb68SLuiz Otavio O Souza 	ti_adc_methods,
959855feb68SLuiz Otavio O Souza 	sizeof(struct ti_adc_softc),
960855feb68SLuiz Otavio O Souza };
961855feb68SLuiz Otavio O Souza 
962855feb68SLuiz Otavio O Souza static devclass_t ti_adc_devclass;
963855feb68SLuiz Otavio O Souza 
964855feb68SLuiz Otavio O Souza DRIVER_MODULE(ti_adc, simplebus, ti_adc_driver, ti_adc_devclass, 0, 0);
965855feb68SLuiz Otavio O Souza MODULE_VERSION(ti_adc, 1);
966855feb68SLuiz Otavio O Souza MODULE_DEPEND(ti_adc, simplebus, 1, 1, 1);
967*0050ea24SMichal Meloun MODULE_DEPEND(ti_adc, ti_sysc, 1, 1, 1);
968a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
96915eefa5bSOleksandr Tymoshenko MODULE_DEPEND(ti_adc, evdev, 1, 1, 1);
97015eefa5bSOleksandr Tymoshenko #endif
971