1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Panel driver for the ARM Versatile family reference designs from
4  * ARM Limited.
5  *
6  * Author:
7  * Linus Walleij <linus.wallei@linaro.org>
8  *
9  * On the Versatile AB, these panels come mounted on daughterboards
10  * named "IB1" or "IB2" (Interface Board 1 & 2 respectively.) They
11  * are documented in ARM DUI 0225D Appendix C and D. These daughter
12  * boards support TFT display panels.
13  *
14  * - The IB1 is a passive board where the display connector defines a
15  *   few wires for encoding the display type for autodetection,
16  *   suitable display settings can then be looked up from this setting.
17  *   The magic bits can be read out from the system controller.
18  *
19  * - The IB2 is a more complex board intended for GSM phone development
20  *   with some logic and a control register, which needs to be accessed
21  *   and the board display needs to be turned on explicitly.
22  *
23  * On the Versatile PB, a special CLCD adaptor board is available
24  * supporting the same displays as the Versatile AB, plus one more
25  * Epson QCIF display.
26  *
27  */
28 #include <drm/drmP.h>
29 #include <drm/drm_panel.h>
30 
31 #include <linux/bitops.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/mfd/syscon.h>
35 #include <linux/module.h>
36 #include <linux/platform_device.h>
37 #include <linux/regmap.h>
38 
39 #include <video/of_videomode.h>
40 #include <video/videomode.h>
41 
42 /*
43  * This configuration register in the Versatile and RealView
44  * family is uniformly present but appears more and more
45  * unutilized starting with the RealView series.
46  */
47 #define SYS_CLCD			0x50
48 
49 /* The Versatile can detect the connected panel type */
50 #define SYS_CLCD_CLCDID_MASK		(BIT(8)|BIT(9)|BIT(10)|BIT(11)|BIT(12))
51 #define SYS_CLCD_ID_SANYO_3_8		(0x00 << 8)
52 #define SYS_CLCD_ID_SHARP_8_4		(0x01 << 8)
53 #define SYS_CLCD_ID_EPSON_2_2		(0x02 << 8)
54 #define SYS_CLCD_ID_SANYO_2_5		(0x07 << 8)
55 #define SYS_CLCD_ID_VGA			(0x1f << 8)
56 
57 /* IB2 control register for the Versatile daughterboard */
58 #define IB2_CTRL			0x00
59 #define IB2_CTRL_LCD_SD			BIT(1) /* 1 = shut down LCD */
60 #define IB2_CTRL_LCD_BL_ON		BIT(0)
61 #define IB2_CTRL_LCD_MASK		(BIT(0)|BIT(1))
62 
63 /**
64  * struct versatile_panel_type - lookup struct for the supported panels
65  */
66 struct versatile_panel_type {
67 	/**
68 	 * @name: the name of this panel
69 	 */
70 	const char *name;
71 	/**
72 	 * @magic: the magic value from the detection register
73 	 */
74 	u32 magic;
75 	/**
76 	 * @mode: the DRM display mode for this panel
77 	 */
78 	struct drm_display_mode mode;
79 	/**
80 	 * @bus_flags: the DRM bus flags for this panel e.g. inverted clock
81 	 */
82 	u32 bus_flags;
83 	/**
84 	 * @width_mm: the panel width in mm
85 	 */
86 	u32 width_mm;
87 	/**
88 	 * @height_mm: the panel height in mm
89 	 */
90 	u32 height_mm;
91 	/**
92 	 * @ib2: the panel may be connected on an IB2 daughterboard
93 	 */
94 	bool ib2;
95 };
96 
97 /**
98  * struct versatile_panel - state container for the Versatile panels
99  */
100 struct versatile_panel {
101 	/**
102 	 * @dev: the container device
103 	 */
104 	struct device *dev;
105 	/**
106 	 * @panel: the DRM panel instance for this device
107 	 */
108 	struct drm_panel panel;
109 	/**
110 	 * @panel_type: the Versatile panel type as detected
111 	 */
112 	const struct versatile_panel_type *panel_type;
113 	/**
114 	 * @map: map to the parent syscon where the main register reside
115 	 */
116 	struct regmap *map;
117 	/**
118 	 * @ib2_map: map to the IB2 syscon, if applicable
119 	 */
120 	struct regmap *ib2_map;
121 };
122 
123 static const struct versatile_panel_type versatile_panels[] = {
124 	/*
125 	 * Sanyo TM38QV67A02A - 3.8 inch QVGA (320x240) Color TFT
126 	 * found on the Versatile AB IB1 connector or the Versatile
127 	 * PB adaptor board connector.
128 	 */
129 	{
130 		.name = "Sanyo TM38QV67A02A",
131 		.magic = SYS_CLCD_ID_SANYO_3_8,
132 		.width_mm = 79,
133 		.height_mm = 54,
134 		.mode = {
135 			.clock = 10000,
136 			.hdisplay = 320,
137 			.hsync_start = 320 + 6,
138 			.hsync_end = 320 + 6 + 6,
139 			.htotal = 320 + 6 + 6 + 6,
140 			.vdisplay = 240,
141 			.vsync_start = 240 + 5,
142 			.vsync_end = 240 + 5 + 6,
143 			.vtotal = 240 + 5 + 6 + 5,
144 			.vrefresh = 116,
145 			.flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC,
146 		},
147 	},
148 	/*
149 	 * Sharp LQ084V1DG21 640x480 VGA Color TFT module
150 	 * found on the Versatile AB IB1 connector or the Versatile
151 	 * PB adaptor board connector.
152 	 */
153 	{
154 		.name = "Sharp LQ084V1DG21",
155 		.magic = SYS_CLCD_ID_SHARP_8_4,
156 		.width_mm = 171,
157 		.height_mm = 130,
158 		.mode = {
159 			.clock = 25000,
160 			.hdisplay = 640,
161 			.hsync_start = 640 + 24,
162 			.hsync_end = 640 + 24 + 96,
163 			.htotal = 640 + 24 + 96 + 24,
164 			.vdisplay = 480,
165 			.vsync_start = 480 + 11,
166 			.vsync_end = 480 + 11 + 2,
167 			.vtotal = 480 + 11 + 2 + 32,
168 			.vrefresh = 60,
169 			.flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC,
170 		},
171 	},
172 	/*
173 	 * Epson L2F50113T00 - 2.2 inch QCIF 176x220 Color TFT
174 	 * found on the Versatile PB adaptor board connector.
175 	 */
176 	{
177 		.name = "Epson L2F50113T00",
178 		.magic = SYS_CLCD_ID_EPSON_2_2,
179 		.width_mm = 34,
180 		.height_mm = 45,
181 		.mode = {
182 			.clock = 62500,
183 			.hdisplay = 176,
184 			.hsync_start = 176 + 2,
185 			.hsync_end = 176 + 2 + 3,
186 			.htotal = 176 + 2 + 3 + 3,
187 			.vdisplay = 220,
188 			.vsync_start = 220 + 0,
189 			.vsync_end = 220 + 0 + 2,
190 			.vtotal = 220 + 0 + 2 + 1,
191 			.vrefresh = 390,
192 			.flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC,
193 		},
194 		.bus_flags = DRM_BUS_FLAG_PIXDATA_NEGEDGE,
195 	},
196 	/*
197 	 * Sanyo ALR252RGT 240x320 portrait display found on the
198 	 * Versatile AB IB2 daughterboard for GSM prototyping.
199 	 */
200 	{
201 		.name = "Sanyo ALR252RGT",
202 		.magic = SYS_CLCD_ID_SANYO_2_5,
203 		.width_mm = 37,
204 		.height_mm = 50,
205 		.mode = {
206 			.clock = 5400,
207 			.hdisplay = 240,
208 			.hsync_start = 240 + 10,
209 			.hsync_end = 240 + 10 + 10,
210 			.htotal = 240 + 10 + 10 + 20,
211 			.vdisplay = 320,
212 			.vsync_start = 320 + 2,
213 			.vsync_end = 320 + 2 + 2,
214 			.vtotal = 320 + 2 + 2 + 2,
215 			.vrefresh = 116,
216 			.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
217 		},
218 		.bus_flags = DRM_BUS_FLAG_PIXDATA_NEGEDGE,
219 		.ib2 = true,
220 	},
221 };
222 
223 static inline struct versatile_panel *
224 to_versatile_panel(struct drm_panel *panel)
225 {
226 	return container_of(panel, struct versatile_panel, panel);
227 }
228 
229 static int versatile_panel_disable(struct drm_panel *panel)
230 {
231 	struct versatile_panel *vpanel = to_versatile_panel(panel);
232 
233 	/* If we're on an IB2 daughterboard, turn off display */
234 	if (vpanel->ib2_map) {
235 		dev_dbg(vpanel->dev, "disable IB2 display\n");
236 		regmap_update_bits(vpanel->ib2_map,
237 				   IB2_CTRL,
238 				   IB2_CTRL_LCD_MASK,
239 				   IB2_CTRL_LCD_SD);
240 	}
241 
242 	return 0;
243 }
244 
245 static int versatile_panel_enable(struct drm_panel *panel)
246 {
247 	struct versatile_panel *vpanel = to_versatile_panel(panel);
248 
249 	/* If we're on an IB2 daughterboard, turn on display */
250 	if (vpanel->ib2_map) {
251 		dev_dbg(vpanel->dev, "enable IB2 display\n");
252 		regmap_update_bits(vpanel->ib2_map,
253 				   IB2_CTRL,
254 				   IB2_CTRL_LCD_MASK,
255 				   IB2_CTRL_LCD_BL_ON);
256 	}
257 
258 	return 0;
259 }
260 
261 static int versatile_panel_get_modes(struct drm_panel *panel)
262 {
263 	struct drm_connector *connector = panel->connector;
264 	struct versatile_panel *vpanel = to_versatile_panel(panel);
265 	struct drm_display_mode *mode;
266 
267 	strncpy(connector->display_info.name, vpanel->panel_type->name,
268 		DRM_DISPLAY_INFO_LEN);
269 	connector->display_info.width_mm = vpanel->panel_type->width_mm;
270 	connector->display_info.height_mm = vpanel->panel_type->height_mm;
271 	connector->display_info.bus_flags = vpanel->panel_type->bus_flags;
272 
273 	mode = drm_mode_duplicate(panel->drm, &vpanel->panel_type->mode);
274 	drm_mode_set_name(mode);
275 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
276 
277 	mode->width_mm = vpanel->panel_type->width_mm;
278 	mode->height_mm = vpanel->panel_type->height_mm;
279 	drm_mode_probed_add(connector, mode);
280 
281 	return 1;
282 }
283 
284 static const struct drm_panel_funcs versatile_panel_drm_funcs = {
285 	.disable = versatile_panel_disable,
286 	.enable = versatile_panel_enable,
287 	.get_modes = versatile_panel_get_modes,
288 };
289 
290 static int versatile_panel_probe(struct platform_device *pdev)
291 {
292 	struct device *dev = &pdev->dev;
293 	struct versatile_panel *vpanel;
294 	struct device *parent;
295 	struct regmap *map;
296 	int ret;
297 	u32 val;
298 	int i;
299 
300 	parent = dev->parent;
301 	if (!parent) {
302 		dev_err(dev, "no parent for versatile panel\n");
303 		return -ENODEV;
304 	}
305 	map = syscon_node_to_regmap(parent->of_node);
306 	if (IS_ERR(map)) {
307 		dev_err(dev, "no regmap for versatile panel parent\n");
308 		return PTR_ERR(map);
309 	}
310 
311 	vpanel = devm_kzalloc(dev, sizeof(*vpanel), GFP_KERNEL);
312 	if (!vpanel)
313 		return -ENOMEM;
314 
315 	ret = regmap_read(map, SYS_CLCD, &val);
316 	if (ret) {
317 		dev_err(dev, "cannot access syscon regs\n");
318 		return ret;
319 	}
320 
321 	val &= SYS_CLCD_CLCDID_MASK;
322 
323 	for (i = 0; i < ARRAY_SIZE(versatile_panels); i++) {
324 		const struct versatile_panel_type *pt;
325 
326 		pt = &versatile_panels[i];
327 		if (pt->magic == val) {
328 			vpanel->panel_type = pt;
329 			break;
330 		}
331 	}
332 
333 	/* No panel detected or VGA, let's leave this show */
334 	if (i == ARRAY_SIZE(versatile_panels)) {
335 		dev_info(dev, "no panel detected\n");
336 		return -ENODEV;
337 	}
338 
339 	dev_info(dev, "detected: %s\n", vpanel->panel_type->name);
340 	vpanel->dev = dev;
341 	vpanel->map = map;
342 
343 	/* Check if the panel is mounted on an IB2 daughterboard */
344 	if (vpanel->panel_type->ib2) {
345 		vpanel->ib2_map = syscon_regmap_lookup_by_compatible(
346 			"arm,versatile-ib2-syscon");
347 		if (IS_ERR(vpanel->ib2_map))
348 			vpanel->ib2_map = NULL;
349 		else
350 			dev_info(dev, "panel mounted on IB2 daughterboard\n");
351 	}
352 
353 	drm_panel_init(&vpanel->panel);
354 	vpanel->panel.dev = dev;
355 	vpanel->panel.funcs = &versatile_panel_drm_funcs;
356 
357 	return drm_panel_add(&vpanel->panel);
358 }
359 
360 static const struct of_device_id versatile_panel_match[] = {
361 	{ .compatible = "arm,versatile-tft-panel", },
362 	{},
363 };
364 MODULE_DEVICE_TABLE(of, versatile_panel_match);
365 
366 static struct platform_driver versatile_panel_driver = {
367 	.probe		= versatile_panel_probe,
368 	.driver		= {
369 		.name	= "versatile-tft-panel",
370 		.of_match_table = versatile_panel_match,
371 	},
372 };
373 module_platform_driver(versatile_panel_driver);
374 
375 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
376 MODULE_DESCRIPTION("ARM Versatile panel driver");
377 MODULE_LICENSE("GPL v2");
378