Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
/* $NetBSD: sunxi_tcon.c,v 1.13 2021/08/20 20:25:27 andvar Exp $ */

/*-
 * Copyright (c) 2018 Manuel Bouyer <bouyer@antioche.eu.org>
 * All rights reserved.
 *
 * Copyright (c) 2014 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sunxi_tcon.c,v 1.13 2021/08/20 20:25:27 andvar Exp $");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/condvar.h>

#include <dev/fdt/fdtvar.h>
#include <dev/fdt/fdt_port.h>
#include <dev/fdt/panel_fdt.h>

#include <dev/videomode/videomode.h>

#include <arm/sunxi/sunxi_tconreg.h>
#include <arm/sunxi/sunxi_display.h>

#define DIVIDE(x,y)     (((x) + ((y) / 2)) / (y))

enum sunxi_tcon_type {
	TCON_A10 = 1,
};

struct sunxi_tcon_softc {
	device_t sc_dev;
	enum sunxi_tcon_type sc_type;
	int sc_phandle;
	bus_space_tag_t sc_bst;
	bus_space_handle_t sc_bsh;
	struct clk *sc_clk_ahb;
	struct clk *sc_clk_ch0;
	struct clk *sc_clk_ch1;
	struct fdtbus_reset *sc_rst, *sc_lvds_rst;
	unsigned int sc_output_type;
#define OUTPUT_HDMI 0
#define OUTPUT_LVDS 1
#define OUTPUT_VGA 2
	struct fdt_device_ports sc_ports;
	int sc_unit; /* tcon0 or tcon1 */
	struct fdt_endpoint *sc_in_ep;
	struct fdt_endpoint *sc_in_rep;
	struct fdt_endpoint *sc_out_ep;
};

static bus_space_handle_t tcon_mux_bsh;
static bool tcon_mux_inited = false;

static void sunxi_tcon_ep_connect(device_t, struct fdt_endpoint *, bool);
static int  sunxi_tcon_ep_activate(device_t, struct fdt_endpoint *, bool);
static int  sunxi_tcon_ep_enable(device_t, struct fdt_endpoint *, bool);
static int  sunxi_tcon0_set_video(struct sunxi_tcon_softc *);
static int  sunxi_tcon0_enable(struct sunxi_tcon_softc *, bool);
static int  sunxi_tcon1_enable(struct sunxi_tcon_softc *, bool);
void sunxi_tcon_dump_regs(int);

#define TCON_READ(sc, reg) \
    bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
#define TCON_WRITE(sc, reg, val) \
    bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "allwinner,sun4i-a10-tcon", .value = TCON_A10},
	{ .compat = "allwinner,sun7i-a20-tcon", .value = TCON_A10},
	DEVICE_COMPAT_EOL
};

static int	sunxi_tcon_match(device_t, cfdata_t, void *);
static void	sunxi_tcon_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(sunxi_tcon, sizeof(struct sunxi_tcon_softc),
	sunxi_tcon_match, sunxi_tcon_attach, NULL, NULL);

static int
sunxi_tcon_match(device_t parent, cfdata_t cf, void *aux)
{
	struct fdt_attach_args * const faa = aux;

	return of_compatible_match(faa->faa_phandle, compat_data);
}

static void
sunxi_tcon_attach(device_t parent, device_t self, void *aux)
{
	struct sunxi_tcon_softc *sc = device_private(self);
	struct fdt_attach_args * const faa = aux;
	const int phandle = faa->faa_phandle;
	bus_addr_t addr;
	bus_size_t size;

	sc->sc_dev = self;
	sc->sc_phandle = phandle;
	sc->sc_bst = faa->faa_bst;

	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
		aprint_error(": couldn't get registers\n");
	}
	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
		aprint_error(": couldn't map registers\n");
		return;
	}

	sc->sc_clk_ahb = fdtbus_clock_get(phandle, "ahb");
	sc->sc_clk_ch0 = fdtbus_clock_get(phandle, "tcon-ch0");
	sc->sc_clk_ch1 = fdtbus_clock_get(phandle, "tcon-ch1");

	if (sc->sc_clk_ahb == NULL || sc->sc_clk_ch0 == NULL
	    || sc->sc_clk_ch1 == NULL) {
		aprint_error(": couldn't get clocks\n");
		aprint_debug_dev(self, "clk ahb %s tcon-ch0 %s tcon-ch1 %s\n",
		    sc->sc_clk_ahb == NULL ? "missing" : "present",
		    sc->sc_clk_ch0 == NULL ? "missing" : "present",
		    sc->sc_clk_ch1 == NULL ? "missing" : "present");
		return;
	}

	sc->sc_rst = fdtbus_reset_get(phandle, "lcd");
	if (sc->sc_rst == NULL) {
		aprint_error(": couldn't get lcd reset\n");
		return;
	}

	sc->sc_lvds_rst = fdtbus_reset_get(phandle, "lvds");

	sc->sc_type =
	    of_compatible_lookup(faa->faa_phandle, compat_data)->value;

	aprint_naive("\n");
	aprint_normal(": LCD/TV timing controller (%s)\n", 
	    fdtbus_get_string(phandle, "name"));

	sc->sc_unit = -1;
	sc->sc_ports.dp_ep_connect = sunxi_tcon_ep_connect;
	sc->sc_ports.dp_ep_activate = sunxi_tcon_ep_activate;
	sc->sc_ports.dp_ep_enable = sunxi_tcon_ep_enable;
	fdt_ports_register(&sc->sc_ports, self, phandle, EP_OTHER);
}

void
sunxi_tcon_doreset(void)
{
	device_t dev;
	struct sunxi_tcon_softc *sc;
	for (int i = 0;;i++) {
		dev = device_find_by_driver_unit("sunxitcon", i);
		if (dev == NULL)
			return;
		sc = device_private(dev);

		if (clk_disable(sc->sc_clk_ahb) != 0) {
			aprint_error_dev(dev, ": couldn't disable ahb clock\n");
			return;
		}
		if (clk_disable(sc->sc_clk_ch0) != 0) {
			aprint_error_dev(dev, ": couldn't disable ch0 clock\n");
			return;
		}

		if (clk_disable(sc->sc_clk_ch1) != 0) {
			aprint_error_dev(dev, ": couldn't disable ch1 clock\n");
			return;
		}

		if (fdtbus_reset_assert(sc->sc_rst) != 0) {
			aprint_error_dev(dev, ": couldn't assert lcd reset\n");
			return;
		}
		if (sc->sc_lvds_rst != NULL) {
			if (fdtbus_reset_assert(sc->sc_lvds_rst) != 0) {
				aprint_error_dev(dev,
				    ": couldn't assert lvds reset\n");
				return;
			}
		}
		delay(1);
		if (fdtbus_reset_deassert(sc->sc_rst) != 0) {
			aprint_error_dev(dev,
			    ": couldn't de-assert lcd reset\n");
			return;
		}
		if (sc->sc_lvds_rst != NULL) {
			if (fdtbus_reset_deassert(sc->sc_lvds_rst) != 0) {
				aprint_error_dev(dev,
				    ": couldn't de-assert lvds reset\n");
				return;
			}
		}

		if (clk_enable(sc->sc_clk_ahb) != 0) {
			aprint_error_dev(dev, ": couldn't enable ahb clock\n");
			return;
		}

		TCON_WRITE(sc, SUNXI_TCON_GINT0_REG, 0);
		TCON_WRITE(sc, SUNXI_TCON_GINT1_REG,
		    __SHIFTIN(0x20, SUNXI_TCON_GINT1_TCON0_LINENO));
		TCON_WRITE(sc, SUNXI_TCON0_DCLK_REG, 0xf0000000);
		TCON_WRITE(sc, SUNXI_TCON0_LVDS_IF_REG, 0x0);
		TCON_WRITE(sc, SUNXI_TCON0_CTL_REG, 0);
		TCON_WRITE(sc, SUNXI_TCON0_IO_TRI_REG, 0xffffffff);
		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, 0);
		TCON_WRITE(sc, SUNXI_TCON1_IO_TRI_REG, 0xffffffff);
		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, 0);

		/* clock needed for the mux in unit 0 */
		if (sc->sc_unit != 0) {
			if (clk_disable(sc->sc_clk_ahb) != 0) {
				aprint_error_dev(dev,
				    ": couldn't disable ahb clock\n");
				return;
			}
		}
	}
}

static void
sunxi_tcon_ep_connect(device_t self, struct fdt_endpoint *ep, bool connect)
{
	struct sunxi_tcon_softc *sc = device_private(self);
	struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
	int rep_idx = fdt_endpoint_index(rep);

	KASSERT(device_is_a(self, "sunxitcon"));
	if (!connect) {
		aprint_error_dev(self, "endpoint disconnect not supported\n");
		return;
	}

	if (fdt_endpoint_port_index(ep) == 0) {
		bool do_print = (sc->sc_unit == -1);
		/*
		 * one of our input endpoints has been connected.
		 * the remote id is our unit number
		 */
		if (sc->sc_unit != -1 && rep_idx != -1 &&
		    sc->sc_unit != rep_idx) {
			aprint_error_dev(self, ": remote id %d doesn't match"
			    " discovered unit number %d\n",
			    rep_idx, sc->sc_unit);
			return;
		}
		if (!device_is_a(fdt_endpoint_device(rep), "sunxidebe")) {
			aprint_error_dev(self,
			    ": input %d connected to unknown device\n",
			    fdt_endpoint_index(ep));
			return;
		}

		if (rep_idx != -1)
			sc->sc_unit = rep_idx;
		else {
			/* assume only one tcon */
			sc->sc_unit = 0;
		}
		if (do_print)
			aprint_verbose_dev(self, "tcon unit %d\n", sc->sc_unit);
		if (!tcon_mux_inited && sc->sc_unit == 0) {
			/* the mux register is only in LCD0 */
			if (clk_enable(sc->sc_clk_ahb) != 0) {
				aprint_error_dev(self,
				    "couldn't enable ahb clock\n");
				return;
			}
			bus_space_subregion(sc->sc_bst, sc->sc_bsh,
			    SUNXI_TCON_MUX_CTL_REG, 4, &tcon_mux_bsh);
			tcon_mux_inited = true;
			bus_space_write_4(sc->sc_bst, tcon_mux_bsh, 0,
			    __SHIFTIN(SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC_CLOSE,
			    SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC));
		}
	} else if (fdt_endpoint_port_index(ep) == 1) {
		device_t rep_dev = fdt_endpoint_device(rep);
		switch(fdt_endpoint_index(ep)) {
		case 0:
			break;
		case 1:
			if (!device_is_a(rep_dev, "sunxihdmi")) {
				aprint_error_dev(self,
				    ": output 1 connected to unknown device\n");
				return;
			}
			break;
		default:
			break;
		}
	}
}

static int
sunxi_tcon_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
{
	struct sunxi_tcon_softc *sc = device_private(dev);
	struct fdt_endpoint *in_ep, *out_ep;
	int outi;
	int error = ENODEV;

	KASSERT(device_is_a(dev, "sunxitcon"));
	/* our input is activated by debe, we activate our output */
	if (fdt_endpoint_port_index(ep) != SUNXI_PORT_INPUT) {
		panic("sunxi_tcon_ep_activate: port %d",
		    fdt_endpoint_port_index(ep));
	}

	if (!activate)
		return EOPNOTSUPP;

	if (clk_enable(sc->sc_clk_ahb) != 0) {
		aprint_error_dev(dev, "couldn't enable ahb clock\n");
		return EIO;
	}
	sc->sc_in_ep = ep;
	sc->sc_in_rep = fdt_endpoint_remote(ep);
	/* check that our other input is not active */
	switch (fdt_endpoint_index(ep)) {
	case 0:
		in_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
		    SUNXI_PORT_INPUT, 1);
		break;
	case 1:
		in_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
		    SUNXI_PORT_INPUT, 0);
		break;
	default:
		in_ep = NULL;
		panic("sunxi_tcon_ep_activate: input index %d",
		    fdt_endpoint_index(ep));
	}
	if (in_ep != NULL) {
		if (fdt_endpoint_is_active(in_ep))
			return EBUSY;
	}
	/* try output 0 (RGB/LVDS) first, then output 1 (HDMI) if it fails */
	for (outi = 0; outi < 2; outi++) {
		out_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
		    SUNXI_PORT_OUTPUT, outi);
		if (out_ep == NULL)
			continue;
		error = fdt_endpoint_activate(out_ep, activate);
		if (error == 0) {
			struct fdt_endpoint *rep = fdt_endpoint_remote(out_ep);
			aprint_verbose_dev(dev, "output to %s\n",
			    device_xname(fdt_endpoint_device(rep)));
			sc->sc_out_ep = out_ep;
			if (outi == 0)
				return sunxi_tcon0_set_video(sc);
			/* XXX should check VGA here */
			sc->sc_output_type = OUTPUT_HDMI;
			return 0;
		}
	}
	if (out_ep == NULL) {
		aprint_error_dev(dev, "no output endpoint\n");
		return ENODEV;
	}
	return error;
}

static int
sunxi_tcon_ep_enable(device_t dev, struct fdt_endpoint *ep, bool enable)
{
	struct sunxi_tcon_softc *sc = device_private(dev);
	int error;
	KASSERT(device_is_a(dev, "sunxitcon"));
	switch (fdt_endpoint_port_index(ep)) {
	case SUNXI_PORT_INPUT:
		KASSERT(ep == sc->sc_in_ep);
		if (fdt_endpoint_index(sc->sc_out_ep) == 0) {
			/* tcon0 active */
			return sunxi_tcon0_enable(sc, enable);
		}
		/* propagate to our output, it will get back to us */
		return fdt_endpoint_enable(sc->sc_out_ep, enable);
	case SUNXI_PORT_OUTPUT:
		KASSERT(ep == sc->sc_out_ep);
		switch (fdt_endpoint_index(ep)) {
		case 0:
			panic("sunxi_tcon0_ep_enable");
		case 1:
			error = sunxi_tcon1_enable(sc, enable);
			break;
		default:
			panic("sunxi_tcon_ep_enable ep %d",
			    fdt_endpoint_index(ep));
			
		}
		break;
	default:
		panic("sunxi_tcon_ep_enable port %d", fdt_endpoint_port_index(ep));
	}
#if defined(SUNXI_TCON_DEBUG)
	sunxi_tcon_dump_regs(device_unit(dev));
#endif
	return error;
}

static int
sunxi_tcon0_set_video(struct sunxi_tcon_softc *sc)
{
	const struct fdt_panel * panel;
	int32_t lcd_x, lcd_y;
	int32_t lcd_hbp, lcd_ht, lcd_vbp, lcd_vt;
	int32_t lcd_hspw, lcd_vspw, lcd_io_cfg0;
	uint32_t vblk, start_delay;
	uint32_t val;
	uint32_t best_div;
	int best_diff, best_clk_freq, clk_freq, lcd_dclk_freq;
	bool dualchan = false;
	static struct videomode mode;
	int error;

	panel = fdt_endpoint_get_data(fdt_endpoint_remote(sc->sc_out_ep));
	KASSERT(panel != NULL);
	KASSERT(panel->panel_type == PANEL_DUAL_LVDS ||
	    panel->panel_type == PANEL_LVDS);
	sc->sc_output_type = OUTPUT_LVDS;

	lcd_x = panel->panel_timing.hactive;
	lcd_y = panel->panel_timing.vactive;

	lcd_dclk_freq = panel->panel_timing.clock_freq;

	lcd_hbp = panel->panel_timing.hback_porch;
	lcd_hspw = panel->panel_timing.hsync_len;
	lcd_ht = panel->panel_timing.hfront_porch + lcd_hspw + lcd_x + lcd_hbp;

	lcd_vbp = panel->panel_timing.vback_porch;
	lcd_vspw = panel->panel_timing.vsync_len;
	lcd_vt = panel->panel_timing.vfront_porch + lcd_vspw + lcd_y + lcd_vbp;
	
	lcd_io_cfg0 = 0x10000000; /* XXX */

	if (panel->panel_type == PANEL_DUAL_LVDS)
		dualchan = true;

	vblk = lcd_vt - lcd_y;
	start_delay = (vblk >= 32) ? 30 : (vblk - 2);

	if (lcd_dclk_freq > 150000000) /* hardware limit ? */
		lcd_dclk_freq = 150000000;

	best_diff = INT_MAX;
	best_div = 0;
	best_clk_freq = 0;
	for (u_int div = 7; div <= 15; div++) {
		int dot_freq, diff;
		clk_freq = clk_round_rate(sc->sc_clk_ch0, lcd_dclk_freq * div);
		if (clk_freq == 0)
			continue;
		dot_freq = clk_freq / div;
		diff = abs(lcd_dclk_freq - dot_freq);
		if (best_diff > diff) {
			best_diff = diff;
			best_div = div;
			best_clk_freq = clk_freq;
			if (diff == 0)
				break;
		}
	}
	if (best_clk_freq == 0) {
		device_printf(sc->sc_dev,
		    ": failed to find params for dot clock %d\n",
		    lcd_dclk_freq);
		return EINVAL;
	}

	error = clk_set_rate(sc->sc_clk_ch0, best_clk_freq);
	if (error) {
		device_printf(sc->sc_dev,
		    ": failed to set ch0 clock to %d for %d: %d\n",
		    best_clk_freq, lcd_dclk_freq, error);
		panic("tcon0 set clk");
	}
	error = clk_enable(sc->sc_clk_ch0);
	if (error) {
		device_printf(sc->sc_dev,
		    ": failed to enable ch0 clock: %d\n", error);
		return EIO;
	}

	val = __SHIFTIN(start_delay, SUNXI_TCONx_CTL_START_DELAY);
	/*
	 * the DE selector selects the primary DEBE for this tcon:
	 * 0 selects debe0 for tcon0 and debe1 for tcon1
	 */
	val |= __SHIFTIN(SUNXI_TCONx_CTL_SRC_SEL_DE0,
			 SUNXI_TCONx_CTL_SRC_SEL);
	TCON_WRITE(sc, SUNXI_TCON0_CTL_REG, val);

	val =  (lcd_x - 1) << 16 |  (lcd_y - 1);
	TCON_WRITE(sc, SUNXI_TCON0_BASIC0_REG, val);
	val = (lcd_ht - 1) << 16 | (lcd_hbp - 1);
	TCON_WRITE(sc, SUNXI_TCON0_BASIC1_REG, val);
	val = (lcd_vt * 2) << 16 | (lcd_vbp - 1);
	TCON_WRITE(sc, SUNXI_TCON0_BASIC2_REG, val);
	val =  ((lcd_hspw > 0) ? (lcd_hspw - 1) : 0) << 16;
	val |= ((lcd_vspw > 0) ? (lcd_vspw - 1) : 0);
	TCON_WRITE(sc, SUNXI_TCON0_BASIC3_REG, val);

	val = 0;
	if (dualchan)
		val |= SUNXI_TCON0_LVDS_IF_DUALCHAN;
	if (panel->panel_lvds_format == LVDS_JEIDA_24)
		val |= SUNXI_TCON0_LVDS_IF_MODE_JEIDA;
	if (panel->panel_lvds_format == LVDS_JEIDA_18)
		val |= SUNXI_TCON0_LVDS_IF_18BITS;
	TCON_WRITE(sc, SUNXI_TCON0_LVDS_IF_REG, val);

	TCON_WRITE(sc, SUNXI_TCON0_IO_POL_REG, lcd_io_cfg0);
	TCON_WRITE(sc, SUNXI_TCON0_IO_TRI_REG, 0);
	TCON_WRITE(sc, SUNXI_TCON_GINT1_REG,
	    __SHIFTIN(start_delay + 2, SUNXI_TCON_GINT1_TCON0_LINENO));

	val = 0xf0000000;
	val &= ~SUNXI_TCON0_DCLK_DIV;
	val |= __SHIFTIN(best_div, SUNXI_TCON0_DCLK_DIV);
	TCON_WRITE(sc, SUNXI_TCON0_DCLK_REG, val);

	mode.dot_clock = lcd_dclk_freq;
	mode.hdisplay = lcd_x;
	mode.hsync_start = lcd_ht - lcd_hbp;
	mode.hsync_end = lcd_hspw + mode.hsync_start;
	mode.htotal = lcd_ht;
	mode.vdisplay = lcd_y;
	mode.vsync_start = lcd_vt - lcd_vbp;
	mode.vsync_end = lcd_vspw + mode.vsync_start;
	mode.vtotal = lcd_vt;
	mode.flags = 0;
	mode.name = NULL;

	sunxi_debe_set_videomode(fdt_endpoint_device(sc->sc_in_rep), &mode);

	/* XXX
	 * magic values here from linux. these are not documented
	 * in the A20 user manual, and other Allwiner LVDS-capable SoC
	 * documentation don't make sense with these values
	 */
	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA0);
	val |= 0x3F310000;
	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA0, val);
	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA0);
	val |= 1 << 22;
	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA0, val);
	delay(2);
	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA1);
	val |= (0x1f << 26 | 0x1f << 10);
	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA1, val);
	delay(2);
	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA1);
	val |= (0x1f << 16 | 0x1f << 0);
	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA1, val);
	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA0);
	val |= 1 << 22;
	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA0, val);
	return 0;
}

static int
sunxi_tcon0_enable(struct sunxi_tcon_softc *sc, bool enable)
{
	uint32_t val;
	int error;

	/* turn on/off backlight and lcd  */
	error = fdt_endpoint_enable(sc->sc_out_ep, enable);
	if (error)
		return error;

	/* and finally disable or enable the tcon */
	error = fdt_endpoint_enable(sc->sc_in_ep, enable);
	if (error)
		return error;
	delay(20000);
	if (enable) {
		if ((error = clk_enable(sc->sc_clk_ch0)) != 0) {
			device_printf(sc->sc_dev,
			    ": couldn't enable ch0 clock\n");
			return error;
		}
		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
		val |= SUNXI_TCON_GCTL_EN;
		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
		val = TCON_READ(sc, SUNXI_TCON0_CTL_REG);
		val |= SUNXI_TCONx_CTL_EN;
		TCON_WRITE(sc, SUNXI_TCON0_CTL_REG, val);
		val = TCON_READ(sc, SUNXI_TCON0_LVDS_IF_REG);
		val |= SUNXI_TCON0_LVDS_IF_EN;
		TCON_WRITE(sc, SUNXI_TCON0_LVDS_IF_REG, val);
		TCON_WRITE(sc, SUNXI_TCON0_IO_TRI_REG, 0);
	} else {
		TCON_WRITE(sc, SUNXI_TCON0_IO_TRI_REG, 0xffffffff);
		val = TCON_READ(sc, SUNXI_TCON0_LVDS_IF_REG);
		val &= ~SUNXI_TCON0_LVDS_IF_EN;
		TCON_WRITE(sc, SUNXI_TCON0_LVDS_IF_REG, val);
		val = TCON_READ(sc, SUNXI_TCON0_CTL_REG);
		val &= ~SUNXI_TCONx_CTL_EN;
		TCON_WRITE(sc, SUNXI_TCON0_CTL_REG, val);
		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
		val &= ~SUNXI_TCON_GCTL_EN;
		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
		if ((error = clk_disable(sc->sc_clk_ch0)) != 0) {
			device_printf(sc->sc_dev,
			    ": couldn't disable ch0 clock\n");
			return error;
		}
	}
#ifdef SUNXI_TCON_DEBUG
	sunxi_tcon_dump_regs(device_unit(sc->sc_dev));
#endif
	return 0;
}

static int
sunxi_tcon1_enable(struct sunxi_tcon_softc *sc, bool enable)
{
	uint32_t val;
	int error;

	KASSERT((sc->sc_output_type == OUTPUT_HDMI) || 
		    (sc->sc_output_type == OUTPUT_VGA));

	fdt_endpoint_enable(sc->sc_in_ep, enable);
	delay(20000);
	if (enable) {
		if ((error = clk_enable(sc->sc_clk_ch1)) != 0) {
			device_printf(sc->sc_dev,
			    ": couldn't enable ch1 clock\n");
			return error;
		}
		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
		val |= SUNXI_TCON_GCTL_EN;
		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
		val = TCON_READ(sc, SUNXI_TCON1_CTL_REG);
		val |= SUNXI_TCONx_CTL_EN;
		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, val);
		if (sc->sc_output_type == OUTPUT_VGA) {
			TCON_WRITE(sc, SUNXI_TCON1_IO_TRI_REG, 0x0cffffff);
		} else
			TCON_WRITE(sc, SUNXI_TCON1_IO_TRI_REG, 0);
	} else {
		TCON_WRITE(sc, SUNXI_TCON1_IO_TRI_REG, 0xffffffff);
		val = TCON_READ(sc, SUNXI_TCON1_CTL_REG);
		val &= ~SUNXI_TCONx_CTL_EN;
		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, val);
		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
		val &= ~SUNXI_TCON_GCTL_EN;
		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
		if ((error = clk_disable(sc->sc_clk_ch1)) != 0) {
			device_printf(sc->sc_dev,
			    ": couldn't disable ch1 clock\n");
			return error;
		}
	}

	KASSERT(tcon_mux_inited);
	val = bus_space_read_4(sc->sc_bst, tcon_mux_bsh, 0);
#ifdef SUNXI_TCON_DEBUG
	printf("sunxi_tcon1_enable(%d) %d val 0x%x", sc->sc_unit, enable, val);
#endif
	val &= ~ SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC;
	switch(sc->sc_unit) {
	case 0:
		val |= __SHIFTIN(SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC_LCDC0_TCON1,
		    SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC);
		break;
	case 1:
		val |= __SHIFTIN(SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC_LCDC1_TCON1,
		    SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC);
		break;
	default:
		panic("tcon: invalid unid %d\n", sc->sc_unit);
	}
#ifdef SUNXI_TCON_DEBUG
	printf(" -> 0x%x", val);
#endif
	bus_space_write_4(sc->sc_bst, tcon_mux_bsh, 0, val);
#ifdef SUNXI_TCON_DEBUG
	printf(": 0x%" PRIxBSH " 0x%" PRIxBSH " 0x%x 0x%x\n", sc->sc_bsh,
	    tcon_mux_bsh, bus_space_read_4(sc->sc_bst, tcon_mux_bsh, 0),
	    TCON_READ(sc, SUNXI_TCON_MUX_CTL_REG));
#endif
	return 0;
}

void
sunxi_tcon1_set_videomode(device_t dev, const struct videomode *mode)
{
	struct sunxi_tcon_softc *sc = device_private(dev);
	uint32_t val;
	int error;

	KASSERT(device_is_a(dev, "sunxitcon"));
	KASSERT((sc->sc_output_type == OUTPUT_HDMI) || 
		    (sc->sc_output_type == OUTPUT_VGA));

	sunxi_debe_set_videomode(fdt_endpoint_device(sc->sc_in_rep), mode);
	if (mode) {
		const u_int interlace_p = !!(mode->flags & VID_INTERLACE);
		const u_int phsync_p = !!(mode->flags & VID_PHSYNC);
		const u_int pvsync_p = !!(mode->flags & VID_PVSYNC);
		const u_int hspw = mode->hsync_end - mode->hsync_start;
		const u_int hbp = mode->htotal - mode->hsync_start;
		const u_int vspw = mode->vsync_end - mode->vsync_start;
		const u_int vbp = mode->vtotal - mode->vsync_start;
		const u_int vblank_len =
		    ((mode->vtotal << interlace_p) >> 1) - mode->vdisplay - 2;
		const u_int start_delay =
		    vblank_len >= 32 ? 30 : vblank_len - 2;

		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
		val |= SUNXI_TCON_GCTL_IO_MAP_SEL;
		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);

		/* enable */
		val = SUNXI_TCONx_CTL_EN;
		if (interlace_p)
			val |= SUNXI_TCONx_CTL_INTERLACE_EN;
		val |= __SHIFTIN(start_delay, SUNXI_TCONx_CTL_START_DELAY);
#ifdef SUNXI_TCON1_BLUEDATA
		val |= __SHIFTIN(SUNXI_TCONx_CTL_SRC_SEL_BLUEDATA,
				 SUNXI_TCONx_CTL_SRC_SEL);
#else
		/*
		 * the DE selector selects the primary DEBE for this tcon:
		 * 0 selects debe0 for tcon0 and debe1 for tcon1
		 */
		val |= __SHIFTIN(SUNXI_TCONx_CTL_SRC_SEL_DE0,
				 SUNXI_TCONx_CTL_SRC_SEL);
#endif
		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, val);

		/* Source width/height */
		TCON_WRITE(sc, SUNXI_TCON1_BASIC0_REG,
		    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
		/* Scaler width/height */
		TCON_WRITE(sc, SUNXI_TCON1_BASIC1_REG,
		    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
		/* Output width/height */
		TCON_WRITE(sc, SUNXI_TCON1_BASIC2_REG,
		    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
		/* Horizontal total + back porch */
		TCON_WRITE(sc, SUNXI_TCON1_BASIC3_REG,
		    ((mode->htotal - 1) << 16) | (hbp - 1));
		/* Vertical total + back porch */
		u_int vtotal = mode->vtotal * 2;
		if (interlace_p) {
			u_int framerate =
			    DIVIDE(DIVIDE(mode->dot_clock * 1000, mode->htotal),
			    mode->vtotal);
			u_int clk = mode->htotal * (mode->vtotal * 2 + 1) *
			    framerate;
			if ((clk / 2) == mode->dot_clock * 1000)
				vtotal += 1;
		}
		TCON_WRITE(sc, SUNXI_TCON1_BASIC4_REG,
		    (vtotal << 16) | (vbp - 1));

		/* Sync */
		TCON_WRITE(sc, SUNXI_TCON1_BASIC5_REG,
		    ((hspw - 1) << 16) | (vspw - 1));
		/* Polarity */
		val = SUNXI_TCON_IO_POL_IO2_INV;
		if (phsync_p)
			val |= SUNXI_TCON_IO_POL_PHSYNC;
		if (pvsync_p)
			val |= SUNXI_TCON_IO_POL_PVSYNC;
		TCON_WRITE(sc, SUNXI_TCON1_IO_POL_REG, val);

		TCON_WRITE(sc, SUNXI_TCON_GINT1_REG,
		    __SHIFTIN(start_delay + 2, SUNXI_TCON_GINT1_TCON1_LINENO));

		/* Setup LCDx CH1 PLL */
		error = clk_set_rate(sc->sc_clk_ch1, mode->dot_clock * 1000);
		if (error) {
			device_printf(sc->sc_dev,
			    ": failed to set ch1 clock to %d: %d\n",
			    mode->dot_clock, error);
		}
		error = clk_enable(sc->sc_clk_ch1);
		if (error) {
			device_printf(sc->sc_dev,
			    ": failed to enable ch1 clock: %d\n",
			    error);
		}
	} else {
		/* disable */
		val = TCON_READ(sc, SUNXI_TCON1_CTL_REG);
		val &= ~SUNXI_TCONx_CTL_EN;
		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, val);
		error = clk_disable(sc->sc_clk_ch1);
		if (error) {
			device_printf(sc->sc_dev,
			    ": failed to disable ch1 clock: %d\n",
			    error);
		}
	}
}

/* check if this tcon is the console chosen by firmware */
bool
sunxi_tcon_is_console(device_t dev, const char *pipeline)
{
	struct sunxi_tcon_softc *sc = device_private(dev);
	char p[64];
	char *e, *n = p;
	bool is_console = false;

	KASSERT(device_is_a(dev, "sunxitcon"));
	strncpy(p, pipeline, sizeof(p) - 1);
	p[sizeof(p) - 1] = '\0';

	/*
	 * pipeline is like "de_be0-lcd0-hdmi"
	 * of "de_be0-lcd1".
	 * In the first case check output type
	 * In the second check tcon unit number
	 */
	 n = p;
	 e = strsep(&n, "-");
	 if (e == NULL || memcmp(e, "de_be", 5) != 0)
		goto bad;
	 e = strsep(&n, "-");
	 if (e == NULL)
		goto bad;
	 if (n == NULL) {
		/* second case */
		if (strcmp(e, "lcd0") == 0) {
			if (sc->sc_unit == 0)
				is_console = true;
		 } else if (strcmp(e, "lcd1") == 0) {
			if (sc->sc_unit == 1)
				is_console = true;
		} else
			goto bad;
		return is_console;
	}
	/* first case */
	if (strcmp(n, "hdmi") == 0) {
		if (sc->sc_output_type == OUTPUT_HDMI)
			is_console = true;
		return is_console;
	}
bad:
	aprint_error("warning: can't parse pipeline %s\n", pipeline);
	return is_console;
}

#if defined(DDB) || defined(SUNXI_TCON_DEBUG)
void
sunxi_tcon_dump_regs(int u)
{
	static const struct {
		const char *name;
		uint16_t reg;
	} regs[] = {
		{ "TCON0_BASIC0_REG", SUNXI_TCON0_BASIC0_REG },
		{ "TCON0_BASIC1_REG", SUNXI_TCON0_BASIC1_REG },
		{ "TCON0_BASIC2_REG", SUNXI_TCON0_BASIC2_REG },
		{ "TCON0_BASIC3_REG", SUNXI_TCON0_BASIC3_REG },
		{ "TCON0_CTL_REG", SUNXI_TCON0_CTL_REG },
		{ "TCON0_DCLK_REG", SUNXI_TCON0_DCLK_REG },
		{ "TCON0_IO_POL_REG", SUNXI_TCON0_IO_POL_REG },
		{ "TCON0_IO_TRI_REG", SUNXI_TCON0_IO_TRI_REG },
		{ "TCON0_LVDS_IF_REG", SUNXI_TCON0_LVDS_IF_REG },
		{ "TCON1_BASIC0_REG", SUNXI_TCON1_BASIC0_REG },
		{ "TCON1_BASIC1_REG", SUNXI_TCON1_BASIC1_REG },
		{ "TCON1_BASIC2_REG", SUNXI_TCON1_BASIC2_REG },
		{ "TCON1_BASIC3_REG", SUNXI_TCON1_BASIC3_REG },
		{ "TCON1_BASIC4_REG", SUNXI_TCON1_BASIC4_REG },
		{ "TCON1_BASIC5_REG", SUNXI_TCON1_BASIC5_REG },
		{ "TCON1_CTL_REG", SUNXI_TCON1_CTL_REG },
		{ "TCON1_IO_POL_REG", SUNXI_TCON1_IO_POL_REG },
		{ "TCON1_IO_TRI_REG", SUNXI_TCON1_IO_TRI_REG },
		{ "TCON_GCTL_REG", SUNXI_TCON_GCTL_REG },
		{ "TCON_GINT0_REG", SUNXI_TCON_GINT0_REG },
		{ "TCON_GINT1_REG", SUNXI_TCON_GINT1_REG },
		{ "TCON_MUX_CTL_REG", SUNXI_TCON_MUX_CTL_REG },
	};
	struct sunxi_tcon_softc *sc;
	device_t dev;

	dev = device_find_by_driver_unit("sunxitcon", u);
	if (dev == NULL)
		return;
	sc = device_private(dev);

	for (int i = 0; i < __arraycount(regs); i++) {
		printf("%s: 0x%08x\n", regs[i].name,
		    TCON_READ(sc, regs[i].reg));
	}
}
#endif