1// SPDX-License-Identifier: GPL-2.0-only
2
3/*
4 * Versatile family (ARM reference designs) handling for the PL11x.
5 * This is based on code and know-how in the previous frame buffer
6 * driver in drivers/video/fbdev/amba-clcd.c:
7 * Copyright (C) 2001 ARM Limited, by David A Rusling
8 * Updated to 2.5 by Deep Blue Solutions Ltd.
9 * Major contributions and discoveries by Russell King.
10 */
11
12#include <linux/bitops.h>
13#include <linux/device.h>
14#include <linux/mfd/syscon.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_platform.h>
18#include <linux/regmap.h>
19#include <linux/vexpress.h>
20
21#include "pl111_versatile.h"
22#include "pl111_drm.h"
23
24static struct regmap *versatile_syscon_map;
25
26/*
27 * We detect the different syscon types from the compatible strings.
28 */
29enum versatile_clcd {
30	INTEGRATOR_IMPD1,
31	INTEGRATOR_CLCD_CM,
32	VERSATILE_CLCD,
33	REALVIEW_CLCD_EB,
34	REALVIEW_CLCD_PB1176,
35	REALVIEW_CLCD_PB11MP,
36	REALVIEW_CLCD_PBA8,
37	REALVIEW_CLCD_PBX,
38	VEXPRESS_CLCD_V2M,
39};
40
41static const struct of_device_id versatile_clcd_of_match[] = {
42	{
43		.compatible = "arm,core-module-integrator",
44		.data = (void *)INTEGRATOR_CLCD_CM,
45	},
46	{
47		.compatible = "arm,versatile-sysreg",
48		.data = (void *)VERSATILE_CLCD,
49	},
50	{
51		.compatible = "arm,realview-eb-syscon",
52		.data = (void *)REALVIEW_CLCD_EB,
53	},
54	{
55		.compatible = "arm,realview-pb1176-syscon",
56		.data = (void *)REALVIEW_CLCD_PB1176,
57	},
58	{
59		.compatible = "arm,realview-pb11mp-syscon",
60		.data = (void *)REALVIEW_CLCD_PB11MP,
61	},
62	{
63		.compatible = "arm,realview-pba8-syscon",
64		.data = (void *)REALVIEW_CLCD_PBA8,
65	},
66	{
67		.compatible = "arm,realview-pbx-syscon",
68		.data = (void *)REALVIEW_CLCD_PBX,
69	},
70	{
71		.compatible = "arm,vexpress-muxfpga",
72		.data = (void *)VEXPRESS_CLCD_V2M,
73	},
74	{},
75};
76
77static const struct of_device_id impd1_clcd_of_match[] = {
78	{
79		.compatible = "arm,im-pd1-syscon",
80		.data = (void *)INTEGRATOR_IMPD1,
81	},
82	{},
83};
84
85/*
86 * Core module CLCD control on the Integrator/CP, bits
87 * 8 thru 19 of the CM_CONTROL register controls a bunch
88 * of CLCD settings.
89 */
90#define INTEGRATOR_HDR_CTRL_OFFSET	0x0C
91#define INTEGRATOR_CLCD_LCDBIASEN	BIT(8)
92#define INTEGRATOR_CLCD_LCDBIASUP	BIT(9)
93#define INTEGRATOR_CLCD_LCDBIASDN	BIT(10)
94/* Bits 11,12,13 controls the LCD or VGA bridge type */
95#define INTEGRATOR_CLCD_LCDMUX_LCD24	BIT(11)
96#define INTEGRATOR_CLCD_LCDMUX_SHARP	(BIT(11)|BIT(12))
97#define INTEGRATOR_CLCD_LCDMUX_VGA555	BIT(13)
98#define INTEGRATOR_CLCD_LCDMUX_VGA24	(BIT(11)|BIT(12)|BIT(13))
99#define INTEGRATOR_CLCD_LCD0_EN		BIT(14)
100#define INTEGRATOR_CLCD_LCD1_EN		BIT(15)
101/* R/L flip on Sharp */
102#define INTEGRATOR_CLCD_LCD_STATIC1	BIT(16)
103/* U/D flip on Sharp */
104#define INTEGRATOR_CLCD_LCD_STATIC2	BIT(17)
105/* No connection on Sharp */
106#define INTEGRATOR_CLCD_LCD_STATIC	BIT(18)
107/* 0 = 24bit VGA, 1 = 18bit VGA */
108#define INTEGRATOR_CLCD_LCD_N24BITEN	BIT(19)
109
110#define INTEGRATOR_CLCD_MASK		GENMASK(19, 8)
111
112static void pl111_integrator_enable(struct drm_device *drm, u32 format)
113{
114	u32 val;
115
116	dev_info(drm->dev, "enable Integrator CLCD connectors\n");
117
118	/* FIXME: really needed? */
119	val = INTEGRATOR_CLCD_LCD_STATIC1 | INTEGRATOR_CLCD_LCD_STATIC2 |
120		INTEGRATOR_CLCD_LCD0_EN | INTEGRATOR_CLCD_LCD1_EN;
121
122	switch (format) {
123	case DRM_FORMAT_XBGR8888:
124	case DRM_FORMAT_XRGB8888:
125		/* 24bit formats */
126		val |= INTEGRATOR_CLCD_LCDMUX_VGA24;
127		break;
128	case DRM_FORMAT_XBGR1555:
129	case DRM_FORMAT_XRGB1555:
130		/* Pseudocolor, RGB555, BGR555 */
131		val |= INTEGRATOR_CLCD_LCDMUX_VGA555;
132		break;
133	default:
134		dev_err(drm->dev, "unhandled format on Integrator 0x%08x\n",
135			format);
136		break;
137	}
138
139	regmap_update_bits(versatile_syscon_map,
140			   INTEGRATOR_HDR_CTRL_OFFSET,
141			   INTEGRATOR_CLCD_MASK,
142			   val);
143}
144
145#define IMPD1_CTRL_OFFSET	0x18
146#define IMPD1_CTRL_DISP_LCD	(0 << 0)
147#define IMPD1_CTRL_DISP_VGA	(1 << 0)
148#define IMPD1_CTRL_DISP_LCD1	(2 << 0)
149#define IMPD1_CTRL_DISP_ENABLE	(1 << 2)
150#define IMPD1_CTRL_DISP_MASK	(7 << 0)
151
152static void pl111_impd1_enable(struct drm_device *drm, u32 format)
153{
154	u32 val;
155
156	dev_info(drm->dev, "enable IM-PD1 CLCD connectors\n");
157	val = IMPD1_CTRL_DISP_VGA | IMPD1_CTRL_DISP_ENABLE;
158
159	regmap_update_bits(versatile_syscon_map,
160			   IMPD1_CTRL_OFFSET,
161			   IMPD1_CTRL_DISP_MASK,
162			   val);
163}
164
165static void pl111_impd1_disable(struct drm_device *drm)
166{
167	dev_info(drm->dev, "disable IM-PD1 CLCD connectors\n");
168
169	regmap_update_bits(versatile_syscon_map,
170			   IMPD1_CTRL_OFFSET,
171			   IMPD1_CTRL_DISP_MASK,
172			   0);
173}
174
175/*
176 * This configuration register in the Versatile and RealView
177 * family is uniformly present but appears more and more
178 * unutilized starting with the RealView series.
179 */
180#define SYS_CLCD			0x50
181#define SYS_CLCD_MODE_MASK		(BIT(0)|BIT(1))
182#define SYS_CLCD_MODE_888		0
183#define SYS_CLCD_MODE_5551		BIT(0)
184#define SYS_CLCD_MODE_565_R_LSB		BIT(1)
185#define SYS_CLCD_MODE_565_B_LSB		(BIT(0)|BIT(1))
186#define SYS_CLCD_CONNECTOR_MASK		(BIT(2)|BIT(3)|BIT(4)|BIT(5))
187#define SYS_CLCD_NLCDIOON		BIT(2)
188#define SYS_CLCD_VDDPOSSWITCH		BIT(3)
189#define SYS_CLCD_PWR3V5SWITCH		BIT(4)
190#define SYS_CLCD_VDDNEGSWITCH		BIT(5)
191
192static void pl111_versatile_disable(struct drm_device *drm)
193{
194	dev_info(drm->dev, "disable Versatile CLCD connectors\n");
195	regmap_update_bits(versatile_syscon_map,
196			   SYS_CLCD,
197			   SYS_CLCD_CONNECTOR_MASK,
198			   0);
199}
200
201static void pl111_versatile_enable(struct drm_device *drm, u32 format)
202{
203	u32 val = 0;
204
205	dev_info(drm->dev, "enable Versatile CLCD connectors\n");
206
207	switch (format) {
208	case DRM_FORMAT_ABGR8888:
209	case DRM_FORMAT_XBGR8888:
210	case DRM_FORMAT_ARGB8888:
211	case DRM_FORMAT_XRGB8888:
212		val |= SYS_CLCD_MODE_888;
213		break;
214	case DRM_FORMAT_BGR565:
215		val |= SYS_CLCD_MODE_565_R_LSB;
216		break;
217	case DRM_FORMAT_RGB565:
218		val |= SYS_CLCD_MODE_565_B_LSB;
219		break;
220	case DRM_FORMAT_ABGR1555:
221	case DRM_FORMAT_XBGR1555:
222	case DRM_FORMAT_ARGB1555:
223	case DRM_FORMAT_XRGB1555:
224		val |= SYS_CLCD_MODE_5551;
225		break;
226	default:
227		dev_err(drm->dev, "unhandled format on Versatile 0x%08x\n",
228			format);
229		break;
230	}
231
232	/* Set up the MUX */
233	regmap_update_bits(versatile_syscon_map,
234			   SYS_CLCD,
235			   SYS_CLCD_MODE_MASK,
236			   val);
237
238	/* Then enable the display */
239	regmap_update_bits(versatile_syscon_map,
240			   SYS_CLCD,
241			   SYS_CLCD_CONNECTOR_MASK,
242			   SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH);
243}
244
245static void pl111_realview_clcd_disable(struct drm_device *drm)
246{
247	dev_info(drm->dev, "disable RealView CLCD connectors\n");
248	regmap_update_bits(versatile_syscon_map,
249			   SYS_CLCD,
250			   SYS_CLCD_CONNECTOR_MASK,
251			   0);
252}
253
254static void pl111_realview_clcd_enable(struct drm_device *drm, u32 format)
255{
256	dev_info(drm->dev, "enable RealView CLCD connectors\n");
257	regmap_update_bits(versatile_syscon_map,
258			   SYS_CLCD,
259			   SYS_CLCD_CONNECTOR_MASK,
260			   SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH);
261}
262
263/* PL110 pixel formats for Integrator, vanilla PL110 */
264static const u32 pl110_integrator_pixel_formats[] = {
265	DRM_FORMAT_ABGR8888,
266	DRM_FORMAT_XBGR8888,
267	DRM_FORMAT_ARGB8888,
268	DRM_FORMAT_XRGB8888,
269	DRM_FORMAT_ABGR1555,
270	DRM_FORMAT_XBGR1555,
271	DRM_FORMAT_ARGB1555,
272	DRM_FORMAT_XRGB1555,
273};
274
275/* Extended PL110 pixel formats for Integrator and Versatile */
276static const u32 pl110_versatile_pixel_formats[] = {
277	DRM_FORMAT_ABGR8888,
278	DRM_FORMAT_XBGR8888,
279	DRM_FORMAT_ARGB8888,
280	DRM_FORMAT_XRGB8888,
281	DRM_FORMAT_BGR565, /* Uses external PLD */
282	DRM_FORMAT_RGB565, /* Uses external PLD */
283	DRM_FORMAT_ABGR1555,
284	DRM_FORMAT_XBGR1555,
285	DRM_FORMAT_ARGB1555,
286	DRM_FORMAT_XRGB1555,
287};
288
289static const u32 pl111_realview_pixel_formats[] = {
290	DRM_FORMAT_ABGR8888,
291	DRM_FORMAT_XBGR8888,
292	DRM_FORMAT_ARGB8888,
293	DRM_FORMAT_XRGB8888,
294	DRM_FORMAT_BGR565,
295	DRM_FORMAT_RGB565,
296	DRM_FORMAT_ABGR1555,
297	DRM_FORMAT_XBGR1555,
298	DRM_FORMAT_ARGB1555,
299	DRM_FORMAT_XRGB1555,
300	DRM_FORMAT_ABGR4444,
301	DRM_FORMAT_XBGR4444,
302	DRM_FORMAT_ARGB4444,
303	DRM_FORMAT_XRGB4444,
304};
305
306/*
307 * The Integrator variant is a PL110 with a bunch of broken, or not
308 * yet implemented features
309 */
310static const struct pl111_variant_data pl110_integrator = {
311	.name = "PL110 Integrator",
312	.is_pl110 = true,
313	.broken_clockdivider = true,
314	.broken_vblank = true,
315	.formats = pl110_integrator_pixel_formats,
316	.nformats = ARRAY_SIZE(pl110_integrator_pixel_formats),
317	.fb_bpp = 16,
318};
319
320/*
321 * The IM-PD1 variant is a PL110 with a bunch of broken, or not
322 * yet implemented features
323 */
324static const struct pl111_variant_data pl110_impd1 = {
325	.name = "PL110 IM-PD1",
326	.is_pl110 = true,
327	.broken_clockdivider = true,
328	.broken_vblank = true,
329	.formats = pl110_integrator_pixel_formats,
330	.nformats = ARRAY_SIZE(pl110_integrator_pixel_formats),
331	.fb_bpp = 16,
332};
333
334/*
335 * This is the in-between PL110 variant found in the ARM Versatile,
336 * supporting RGB565/BGR565
337 */
338static const struct pl111_variant_data pl110_versatile = {
339	.name = "PL110 Versatile",
340	.is_pl110 = true,
341	.external_bgr = true,
342	.formats = pl110_versatile_pixel_formats,
343	.nformats = ARRAY_SIZE(pl110_versatile_pixel_formats),
344	.fb_bpp = 16,
345};
346
347/*
348 * RealView PL111 variant, the only real difference from the vanilla
349 * PL111 is that we select 16bpp framebuffer by default to be able
350 * to get 1024x768 without saturating the memory bus.
351 */
352static const struct pl111_variant_data pl111_realview = {
353	.name = "PL111 RealView",
354	.formats = pl111_realview_pixel_formats,
355	.nformats = ARRAY_SIZE(pl111_realview_pixel_formats),
356	.fb_bpp = 16,
357};
358
359/*
360 * Versatile Express PL111 variant, again we just push the maximum
361 * BPP to 16 to be able to get 1024x768 without saturating the memory
362 * bus. The clockdivider also seems broken on the Versatile Express.
363 */
364static const struct pl111_variant_data pl111_vexpress = {
365	.name = "PL111 Versatile Express",
366	.formats = pl111_realview_pixel_formats,
367	.nformats = ARRAY_SIZE(pl111_realview_pixel_formats),
368	.fb_bpp = 16,
369	.broken_clockdivider = true,
370};
371
372#define VEXPRESS_FPGAMUX_MOTHERBOARD		0x00
373#define VEXPRESS_FPGAMUX_DAUGHTERBOARD_1	0x01
374#define VEXPRESS_FPGAMUX_DAUGHTERBOARD_2	0x02
375
376static int pl111_vexpress_clcd_init(struct device *dev, struct device_node *np,
377				    struct pl111_drm_dev_private *priv)
378{
379	struct platform_device *pdev;
380	struct device_node *root;
381	struct device_node *child;
382	struct device_node *ct_clcd = NULL;
383	struct regmap *map;
384	bool has_coretile_clcd = false;
385	bool has_coretile_hdlcd = false;
386	bool mux_motherboard = true;
387	u32 val;
388	int ret;
389
390	if (!IS_ENABLED(CONFIG_VEXPRESS_CONFIG))
391		return -ENODEV;
392
393	/*
394	 * Check if we have a CLCD or HDLCD on the core tile by checking if a
395	 * CLCD or HDLCD is available in the root of the device tree.
396	 */
397	root = of_find_node_by_path("/");
398	if (!root)
399		return -EINVAL;
400
401	for_each_available_child_of_node(root, child) {
402		if (of_device_is_compatible(child, "arm,pl111")) {
403			has_coretile_clcd = true;
404			ct_clcd = child;
405			of_node_put(child);
406			break;
407		}
408		if (of_device_is_compatible(child, "arm,hdlcd")) {
409			has_coretile_hdlcd = true;
410			of_node_put(child);
411			break;
412		}
413	}
414
415	of_node_put(root);
416
417	/*
418	 * If there is a coretile HDLCD and it has a driver,
419	 * do not mux the CLCD on the motherboard to the DVI.
420	 */
421	if (has_coretile_hdlcd && IS_ENABLED(CONFIG_DRM_HDLCD))
422		mux_motherboard = false;
423
424	/*
425	 * On the Vexpress CA9 we let the CLCD on the coretile
426	 * take precedence, so also in this case do not mux the
427	 * motherboard to the DVI.
428	 */
429	if (has_coretile_clcd)
430		mux_motherboard = false;
431
432	if (mux_motherboard) {
433		dev_info(dev, "DVI muxed to motherboard CLCD\n");
434		val = VEXPRESS_FPGAMUX_MOTHERBOARD;
435	} else if (ct_clcd == dev->of_node) {
436		dev_info(dev,
437			 "DVI muxed to daughterboard 1 (core tile) CLCD\n");
438		val = VEXPRESS_FPGAMUX_DAUGHTERBOARD_1;
439	} else {
440		dev_info(dev, "core tile graphics present\n");
441		dev_info(dev, "this device will be deactivated\n");
442		return -ENODEV;
443	}
444
445	/* Call into deep Vexpress configuration API */
446	pdev = of_find_device_by_node(np);
447	if (!pdev) {
448		dev_err(dev, "can't find the sysreg device, deferring\n");
449		return -EPROBE_DEFER;
450	}
451
452	map = devm_regmap_init_vexpress_config(&pdev->dev);
453	if (IS_ERR(map)) {
454		platform_device_put(pdev);
455		return PTR_ERR(map);
456	}
457
458	ret = regmap_write(map, 0, val);
459	platform_device_put(pdev);
460	if (ret) {
461		dev_err(dev, "error setting DVI muxmode\n");
462		return -ENODEV;
463	}
464
465	priv->variant = &pl111_vexpress;
466	dev_info(dev, "initializing Versatile Express PL111\n");
467
468	return 0;
469}
470
471int pl111_versatile_init(struct device *dev, struct pl111_drm_dev_private *priv)
472{
473	const struct of_device_id *clcd_id;
474	enum versatile_clcd versatile_clcd_type;
475	struct device_node *np;
476	struct regmap *map;
477
478	np = of_find_matching_node_and_match(NULL, versatile_clcd_of_match,
479					     &clcd_id);
480	if (!np) {
481		/* Non-ARM reference designs, just bail out */
482		return 0;
483	}
484
485	versatile_clcd_type = (enum versatile_clcd)clcd_id->data;
486
487	/* Versatile Express special handling */
488	if (versatile_clcd_type == VEXPRESS_CLCD_V2M) {
489		int ret = pl111_vexpress_clcd_init(dev, np, priv);
490		of_node_put(np);
491		if (ret)
492			dev_err(dev, "Versatile Express init failed - %d", ret);
493		return ret;
494	}
495
496	/*
497	 * On the Integrator, check if we should use the IM-PD1 instead,
498	 * if we find it, it will take precedence. This is on the Integrator/AP
499	 * which only has this option for PL110 graphics.
500	 */
501	 if (versatile_clcd_type == INTEGRATOR_CLCD_CM) {
502		np = of_find_matching_node_and_match(NULL, impd1_clcd_of_match,
503						     &clcd_id);
504		if (np)
505			versatile_clcd_type = (enum versatile_clcd)clcd_id->data;
506	}
507
508	map = syscon_node_to_regmap(np);
509	of_node_put(np);
510	if (IS_ERR(map)) {
511		dev_err(dev, "no Versatile syscon regmap\n");
512		return PTR_ERR(map);
513	}
514
515	switch (versatile_clcd_type) {
516	case INTEGRATOR_CLCD_CM:
517		versatile_syscon_map = map;
518		priv->variant = &pl110_integrator;
519		priv->variant_display_enable = pl111_integrator_enable;
520		dev_info(dev, "set up callbacks for Integrator PL110\n");
521		break;
522	case INTEGRATOR_IMPD1:
523		versatile_syscon_map = map;
524		priv->variant = &pl110_impd1;
525		priv->variant_display_enable = pl111_impd1_enable;
526		priv->variant_display_disable = pl111_impd1_disable;
527		dev_info(dev, "set up callbacks for IM-PD1 PL110\n");
528		break;
529	case VERSATILE_CLCD:
530		versatile_syscon_map = map;
531		/* This can do RGB565 with external PLD */
532		priv->variant = &pl110_versatile;
533		priv->variant_display_enable = pl111_versatile_enable;
534		priv->variant_display_disable = pl111_versatile_disable;
535		/*
536		 * The Versatile has a variant halfway between PL110
537		 * and PL111 where these two registers have already been
538		 * swapped.
539		 */
540		priv->ienb = CLCD_PL111_IENB;
541		priv->ctrl = CLCD_PL111_CNTL;
542		dev_info(dev, "set up callbacks for Versatile PL110\n");
543		break;
544	case REALVIEW_CLCD_EB:
545	case REALVIEW_CLCD_PB1176:
546	case REALVIEW_CLCD_PB11MP:
547	case REALVIEW_CLCD_PBA8:
548	case REALVIEW_CLCD_PBX:
549		versatile_syscon_map = map;
550		priv->variant = &pl111_realview;
551		priv->variant_display_enable = pl111_realview_clcd_enable;
552		priv->variant_display_disable = pl111_realview_clcd_disable;
553		dev_info(dev, "set up callbacks for RealView PL111\n");
554		break;
555	default:
556		dev_info(dev, "unknown Versatile system controller\n");
557		break;
558	}
559
560	return 0;
561}
562EXPORT_SYMBOL_GPL(pl111_versatile_init);
563