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