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