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 	if (!mode)
271 		return -ENOMEM;
272 	drm_mode_set_name(mode);
273 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
274 
275 	mode->width_mm = vpanel->panel_type->width_mm;
276 	mode->height_mm = vpanel->panel_type->height_mm;
277 	drm_mode_probed_add(connector, mode);
278 
279 	return 1;
280 }
281 
282 static const struct drm_panel_funcs versatile_panel_drm_funcs = {
283 	.disable = versatile_panel_disable,
284 	.enable = versatile_panel_enable,
285 	.get_modes = versatile_panel_get_modes,
286 };
287 
versatile_panel_probe(struct platform_device * pdev)288 static int versatile_panel_probe(struct platform_device *pdev)
289 {
290 	struct device *dev = &pdev->dev;
291 	struct versatile_panel *vpanel;
292 	struct device *parent;
293 	struct regmap *map;
294 	int ret;
295 	u32 val;
296 	int i;
297 
298 	parent = dev->parent;
299 	if (!parent) {
300 		dev_err(dev, "no parent for versatile panel\n");
301 		return -ENODEV;
302 	}
303 	map = syscon_node_to_regmap(parent->of_node);
304 	if (IS_ERR(map)) {
305 		dev_err(dev, "no regmap for versatile panel parent\n");
306 		return PTR_ERR(map);
307 	}
308 
309 	vpanel = devm_kzalloc(dev, sizeof(*vpanel), GFP_KERNEL);
310 	if (!vpanel)
311 		return -ENOMEM;
312 
313 	ret = regmap_read(map, SYS_CLCD, &val);
314 	if (ret) {
315 		dev_err(dev, "cannot access syscon regs\n");
316 		return ret;
317 	}
318 
319 	val &= SYS_CLCD_CLCDID_MASK;
320 
321 	for (i = 0; i < ARRAY_SIZE(versatile_panels); i++) {
322 		const struct versatile_panel_type *pt;
323 
324 		pt = &versatile_panels[i];
325 		if (pt->magic == val) {
326 			vpanel->panel_type = pt;
327 			break;
328 		}
329 	}
330 
331 	/* No panel detected or VGA, let's leave this show */
332 	if (i == ARRAY_SIZE(versatile_panels)) {
333 		dev_info(dev, "no panel detected\n");
334 		return -ENODEV;
335 	}
336 
337 	dev_info(dev, "detected: %s\n", vpanel->panel_type->name);
338 	vpanel->dev = dev;
339 	vpanel->map = map;
340 
341 	/* Check if the panel is mounted on an IB2 daughterboard */
342 	if (vpanel->panel_type->ib2) {
343 		vpanel->ib2_map = syscon_regmap_lookup_by_compatible(
344 			"arm,versatile-ib2-syscon");
345 		if (IS_ERR(vpanel->ib2_map))
346 			vpanel->ib2_map = NULL;
347 		else
348 			dev_info(dev, "panel mounted on IB2 daughterboard\n");
349 	}
350 
351 	drm_panel_init(&vpanel->panel, dev, &versatile_panel_drm_funcs,
352 		       DRM_MODE_CONNECTOR_DPI);
353 
354 	drm_panel_add(&vpanel->panel);
355 
356 	return 0;
357 }
358 
359 static const struct of_device_id versatile_panel_match[] = {
360 	{ .compatible = "arm,versatile-tft-panel", },
361 	{},
362 };
363 MODULE_DEVICE_TABLE(of, versatile_panel_match);
364 
365 static struct platform_driver versatile_panel_driver = {
366 	.probe		= versatile_panel_probe,
367 	.driver		= {
368 		.name	= "versatile-tft-panel",
369 		.of_match_table = versatile_panel_match,
370 	},
371 };
372 module_platform_driver(versatile_panel_driver);
373 
374 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
375 MODULE_DESCRIPTION("ARM Versatile panel driver");
376 MODULE_LICENSE("GPL v2");
377