blob: d05502f67afd56bf64dde1a758036fdad89977f6 [file] [log] [blame]
Chris Morgan8c4e3042023-04-21 10:59:19 -05001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2023 Chris Morgan <macromorgan@hotmail.com>
4 */
5
6#include <abuf.h>
7#include <adc.h>
8#include <asm/io.h>
Chris Morgan3db220e2023-05-15 11:00:30 -05009#include <display.h>
Chris Morgan8c4e3042023-04-21 10:59:19 -050010#include <dm.h>
Chris Morgana824aa62023-05-15 11:00:29 -050011#include <dm/lists.h>
12#include <env.h>
13#include <fdt_support.h>
Chris Morgan8c4e3042023-04-21 10:59:19 -050014#include <linux/delay.h>
Chris Morgan3db220e2023-05-15 11:00:30 -050015#include <mipi_dsi.h>
Chris Morgana824aa62023-05-15 11:00:29 -050016#include <mmc.h>
Chris Morgan3db220e2023-05-15 11:00:30 -050017#include <panel.h>
Chris Morgan8c4e3042023-04-21 10:59:19 -050018#include <pwm.h>
Chris Morgan8c4e3042023-04-21 10:59:19 -050019#include <stdlib.h>
Chris Morgan3db220e2023-05-15 11:00:30 -050020#include <video_bridge.h>
Chris Morgan8c4e3042023-04-21 10:59:19 -050021
22#define GPIO0_BASE 0xfdd60000
Chris Morgan3db220e2023-05-15 11:00:30 -050023#define GPIO4_BASE 0xfe770000
Chris Morgana824aa62023-05-15 11:00:29 -050024#define GPIO_SWPORT_DR_L 0x0000
Chris Morgan8c4e3042023-04-21 10:59:19 -050025#define GPIO_SWPORT_DR_H 0x0004
Chris Morgana824aa62023-05-15 11:00:29 -050026#define GPIO_SWPORT_DDR_L 0x0008
Chris Morgan8c4e3042023-04-21 10:59:19 -050027#define GPIO_SWPORT_DDR_H 0x000c
Chris Morgan3db220e2023-05-15 11:00:30 -050028#define GPIO_A0 BIT(0)
Chris Morgana824aa62023-05-15 11:00:29 -050029#define GPIO_C5 BIT(5)
30#define GPIO_C6 BIT(6)
31#define GPIO_C7 BIT(7)
Chris Morgan8c4e3042023-04-21 10:59:19 -050032
33#define GPIO_WRITEMASK(bits) ((bits) << 16)
34
35#define DTB_DIR "rockchip/"
36
37struct rg3xx_model {
Chris Morgana824aa62023-05-15 11:00:29 -050038 const u16 adc_value;
Chris Morgan8c4e3042023-04-21 10:59:19 -050039 const char *board;
40 const char *board_name;
41 const char *fdtfile;
Chris Morgana63ff502024-01-02 09:46:48 -060042 const bool detect_panel;
Chris Morgan8c4e3042023-04-21 10:59:19 -050043};
44
45enum rgxx3_device_id {
46 RG353M,
47 RG353P,
48 RG353V,
Chris Morgan8c4e3042023-04-21 10:59:19 -050049 RG503,
Chris Morgan95db0c02024-01-02 09:46:53 -060050 RGB30,
51 RK2023,
52 RGARCD,
Chris Morgana824aa62023-05-15 11:00:29 -050053 /* Devices with duplicate ADC value */
54 RG353PS,
55 RG353VS,
Chris Morgan95db0c02024-01-02 09:46:53 -060056 RGARCS,
Chris Morgan8c4e3042023-04-21 10:59:19 -050057};
58
59static const struct rg3xx_model rg3xx_model_details[] = {
60 [RG353M] = {
Chris Morgana63ff502024-01-02 09:46:48 -060061 .adc_value = 517, /* Observed average from device */
62 .board = "rk3566-anbernic-rg353m",
63 .board_name = "RG353M",
64 /* Device is identical to RG353P. */
65 .fdtfile = DTB_DIR "rk3566-anbernic-rg353p.dtb",
66 .detect_panel = 1,
Chris Morgan8c4e3042023-04-21 10:59:19 -050067 },
68 [RG353P] = {
Chris Morgana63ff502024-01-02 09:46:48 -060069 .adc_value = 860, /* Documented value of 860 */
70 .board = "rk3566-anbernic-rg353p",
71 .board_name = "RG353P",
72 .fdtfile = DTB_DIR "rk3566-anbernic-rg353p.dtb",
73 .detect_panel = 1,
Chris Morgan8c4e3042023-04-21 10:59:19 -050074 },
75 [RG353V] = {
Chris Morgana63ff502024-01-02 09:46:48 -060076 .adc_value = 695, /* Observed average from device */
77 .board = "rk3566-anbernic-rg353v",
78 .board_name = "RG353V",
79 .fdtfile = DTB_DIR "rk3566-anbernic-rg353v.dtb",
80 .detect_panel = 1,
Chris Morgan8c4e3042023-04-21 10:59:19 -050081 },
Chris Morgan8c4e3042023-04-21 10:59:19 -050082 [RG503] = {
Chris Morgana63ff502024-01-02 09:46:48 -060083 .adc_value = 1023, /* Observed average from device */
84 .board = "rk3566-anbernic-rg503",
85 .board_name = "RG503",
86 .fdtfile = DTB_DIR "rk3566-anbernic-rg503.dtb",
87 .detect_panel = 0,
Chris Morgan8c4e3042023-04-21 10:59:19 -050088 },
Chris Morgan95db0c02024-01-02 09:46:53 -060089 [RGB30] = {
90 .adc_value = 383, /* Gathered from second hand information */
91 .board = "rk3566-powkiddy-rgb30",
92 .board_name = "RGB30",
93 .fdtfile = DTB_DIR "rk3566-powkiddy-rgb30.dtb",
94 .detect_panel = 0,
95 },
96 [RK2023] = {
97 .adc_value = 635, /* Observed average from device */
98 .board = "rk3566-powkiddy-rk2023",
99 .board_name = "RK2023",
100 .fdtfile = DTB_DIR "rk3566-powkiddy-rk2023.dtb",
101 .detect_panel = 0,
102 },
103 [RGARCD] = {
104 .adc_value = 183, /* Observed average from device */
105 .board = "rk3566-anbernic-rg-arc-d",
106 .board_name = "Anbernic RG ARC-D",
107 .fdtfile = DTB_DIR "rk3566-anbernic-rg-arc-d.dtb",
108 .detect_panel = 0,
109 },
Chris Morgana824aa62023-05-15 11:00:29 -0500110 /* Devices with duplicate ADC value */
111 [RG353PS] = {
Chris Morgana63ff502024-01-02 09:46:48 -0600112 .adc_value = 860, /* Observed average from device */
113 .board = "rk3566-anbernic-rg353ps",
114 .board_name = "RG353PS",
115 .fdtfile = DTB_DIR "rk3566-anbernic-rg353ps.dtb",
116 .detect_panel = 1,
Chris Morgana824aa62023-05-15 11:00:29 -0500117 },
118 [RG353VS] = {
Chris Morgana63ff502024-01-02 09:46:48 -0600119 .adc_value = 695, /* Gathered from second hand information */
120 .board = "rk3566-anbernic-rg353vs",
121 .board_name = "RG353VS",
122 .fdtfile = DTB_DIR "rk3566-anbernic-rg353vs.dtb",
123 .detect_panel = 1,
Chris Morgana824aa62023-05-15 11:00:29 -0500124 },
Chris Morgan95db0c02024-01-02 09:46:53 -0600125 [RGARCS] = {
126 .adc_value = 183, /* Observed average from device */
127 .board = "rk3566-anbernic-rg-arc-s",
128 .board_name = "Anbernic RG ARC-S",
129 .fdtfile = DTB_DIR "rk3566-anbernic-rg-arc-s.dtb",
130 .detect_panel = 0,
131 },
Chris Morgan8c4e3042023-04-21 10:59:19 -0500132};
133
Chris Morgan3db220e2023-05-15 11:00:30 -0500134struct rg353_panel {
135 const u16 id;
Chris Morgana63ff502024-01-02 09:46:48 -0600136 const char *panel_compat[2];
Chris Morgan3db220e2023-05-15 11:00:30 -0500137};
138
139static const struct rg353_panel rg353_panel_details[] = {
Chris Morgana63ff502024-01-02 09:46:48 -0600140 {
141 .id = 0x3052,
142 .panel_compat[0] = "anbernic,rg353p-panel",
143 .panel_compat[1] = "newvision,nv3051d",
144 },
145 {
146 .id = 0x3821,
147 .panel_compat[0] = "anbernic,rg353v-panel-v2",
148 .panel_compat[1] = NULL,
149 },
Chris Morgan3db220e2023-05-15 11:00:30 -0500150};
151
Chris Morgan8c4e3042023-04-21 10:59:19 -0500152/*
153 * Start LED very early so user knows device is on. Set color
Chris Morgana824aa62023-05-15 11:00:29 -0500154 * to red.
Chris Morgan8c4e3042023-04-21 10:59:19 -0500155 */
156void spl_board_init(void)
157{
Chris Morgana824aa62023-05-15 11:00:29 -0500158 /* Set GPIO0_C5, GPIO0_C6, and GPIO0_C7 to output. */
159 writel(GPIO_WRITEMASK(GPIO_C7 | GPIO_C6 | GPIO_C5) | \
160 (GPIO_C7 | GPIO_C6 | GPIO_C5),
Chris Morgan8c4e3042023-04-21 10:59:19 -0500161 (GPIO0_BASE + GPIO_SWPORT_DDR_H));
Chris Morgana824aa62023-05-15 11:00:29 -0500162 /* Set GPIO0_C5 and GPIO_C6 to 0 and GPIO0_C7 to 1. */
163 writel(GPIO_WRITEMASK(GPIO_C7 | GPIO_C6 | GPIO_C5) | GPIO_C7,
Chris Morgan8c4e3042023-04-21 10:59:19 -0500164 (GPIO0_BASE + GPIO_SWPORT_DR_H));
165}
166
Chris Morgan8c4e3042023-04-21 10:59:19 -0500167/*
168 * Buzz the buzzer so the user knows something is going on. Make it
169 * optional in case PWM is disabled.
170 */
171void __maybe_unused startup_buzz(void)
172{
173 struct udevice *dev;
174 int err;
175
176 err = uclass_get_device(UCLASS_PWM, 0, &dev);
177 if (err)
178 printf("pwm not found\n");
179
180 pwm_set_enable(dev, 0, 1);
181 mdelay(200);
182 pwm_set_enable(dev, 0, 0);
183}
184
Chris Morgan3db220e2023-05-15 11:00:30 -0500185/*
186 * Provide the bare minimum to identify the panel for the RG353
187 * series. Since we don't have a working framebuffer device, no
188 * need to init the panel; just identify it and provide the
189 * clocks so we know what to set the different clock values to.
190 */
191
192static const struct display_timing rg353_default_timing = {
193 .pixelclock.typ = 24150000,
194 .hactive.typ = 640,
195 .hfront_porch.typ = 40,
196 .hback_porch.typ = 80,
197 .hsync_len.typ = 2,
198 .vactive.typ = 480,
199 .vfront_porch.typ = 18,
200 .vback_porch.typ = 28,
201 .vsync_len.typ = 2,
202 .flags = DISPLAY_FLAGS_HSYNC_HIGH |
203 DISPLAY_FLAGS_VSYNC_HIGH,
204};
205
206static int anbernic_rg353_panel_get_timing(struct udevice *dev,
207 struct display_timing *timings)
208{
209 memcpy(timings, &rg353_default_timing, sizeof(*timings));
210
211 return 0;
212}
213
214static int anbernic_rg353_panel_probe(struct udevice *dev)
215{
216 struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
217
218 plat->lanes = 4;
219 plat->format = MIPI_DSI_FMT_RGB888;
220 plat->mode_flags = MIPI_DSI_MODE_VIDEO |
221 MIPI_DSI_MODE_VIDEO_BURST |
222 MIPI_DSI_MODE_EOT_PACKET |
223 MIPI_DSI_MODE_LPM;
224
225 return 0;
226}
227
228static const struct panel_ops anbernic_rg353_panel_ops = {
229 .get_display_timing = anbernic_rg353_panel_get_timing,
230};
231
232U_BOOT_DRIVER(anbernic_rg353_panel) = {
233 .name = "anbernic_rg353_panel",
234 .id = UCLASS_PANEL,
235 .ops = &anbernic_rg353_panel_ops,
236 .probe = anbernic_rg353_panel_probe,
237 .plat_auto = sizeof(struct mipi_dsi_panel_plat),
238};
239
240int rgxx3_detect_display(void)
241{
242 struct udevice *dev;
243 struct mipi_dsi_device *dsi;
244 struct mipi_dsi_panel_plat *mplat;
245 const struct rg353_panel *panel;
246 int ret = 0;
247 int i;
248 u8 panel_id[2];
249
250 /*
251 * Take panel out of reset status.
252 * Set GPIO4_A0 to output.
253 */
254 writel(GPIO_WRITEMASK(GPIO_A0) | GPIO_A0,
255 (GPIO4_BASE + GPIO_SWPORT_DDR_L));
256 /* Set GPIO4_A0 to 1. */
257 writel(GPIO_WRITEMASK(GPIO_A0) | GPIO_A0,
258 (GPIO4_BASE + GPIO_SWPORT_DR_L));
259
260 /* Probe the DSI controller. */
261 ret = uclass_get_device_by_name(UCLASS_VIDEO_BRIDGE,
262 "dsi@fe060000", &dev);
263 if (ret) {
264 printf("DSI host not probed: %d\n", ret);
265 return ret;
266 }
267
268 /* Probe the DSI panel. */
269 ret = device_bind_driver_to_node(dev, "anbernic_rg353_panel",
270 "anbernic_rg353_panel",
271 dev_ofnode(dev), NULL);
272 if (ret) {
273 printf("Failed to probe RG353 panel: %d\n", ret);
274 return ret;
275 }
276
277 /*
278 * Attach the DSI controller which will also probe and attach
279 * the DSIDPHY.
280 */
281 ret = video_bridge_attach(dev);
282 if (ret) {
283 printf("Failed to attach DSI controller: %d\n", ret);
284 return ret;
285 }
286
287 /*
288 * Get the panel which should have already been probed by the
289 * video_bridge_attach() function.
290 */
291 ret = uclass_first_device_err(UCLASS_PANEL, &dev);
292 if (ret) {
293 printf("Panel device error: %d\n", ret);
294 return ret;
295 }
296
297 /* Now call the panel via DSI commands to get the panel ID. */
298 mplat = dev_get_plat(dev);
299 dsi = mplat->device;
300 mipi_dsi_set_maximum_return_packet_size(dsi, sizeof(panel_id));
301 ret = mipi_dsi_dcs_read(dsi, MIPI_DCS_GET_DISPLAY_ID, &panel_id,
302 sizeof(panel_id));
303 if (ret < 0) {
304 printf("Unable to read panel ID: %d\n", ret);
305 return ret;
306 }
307
308 /* Get the correct panel compatible from the table. */
309 for (i = 0; i < ARRAY_SIZE(rg353_panel_details); i++) {
310 if (rg353_panel_details[i].id == ((panel_id[0] << 8) |
311 panel_id[1])) {
312 panel = &rg353_panel_details[i];
313 break;
314 }
315 }
316
317 if (!panel) {
318 printf("Unable to identify panel_id %x\n",
319 (panel_id[0] << 8) | panel_id[1]);
Chris Morgan3db220e2023-05-15 11:00:30 -0500320 return -EINVAL;
321 }
322
Chris Morgana63ff502024-01-02 09:46:48 -0600323 env_set("panel", panel->panel_compat[0]);
Chris Morgan3db220e2023-05-15 11:00:30 -0500324
325 return 0;
326}
327
Chris Morgan8c4e3042023-04-21 10:59:19 -0500328/* Detect which Anbernic RGXX3 device we are using so as to load the
329 * correct devicetree for Linux. Set an environment variable once
330 * found. The detection depends on the value of ADC channel 1, the
Chris Morgana824aa62023-05-15 11:00:29 -0500331 * presence of an eMMC on mmc0, and querying the DSI panel.
Chris Morgan8c4e3042023-04-21 10:59:19 -0500332 */
333int rgxx3_detect_device(void)
334{
335 u32 adc_info;
Chris Morgana824aa62023-05-15 11:00:29 -0500336 int ret, i;
Chris Morgan8c4e3042023-04-21 10:59:19 -0500337 int board_id = -ENXIO;
338 struct mmc *mmc;
339
340 ret = adc_channel_single_shot("saradc@fe720000", 1, &adc_info);
341 if (ret) {
342 printf("Read SARADC failed with error %d\n", ret);
343 return ret;
344 }
345
Chris Morgana824aa62023-05-15 11:00:29 -0500346 /*
347 * Get the correct device from the table. The ADC value is
348 * determined by a resistor on ADC channel 0. The hardware
349 * design calls for no more than a 1% variance on the
350 * resistor, so assume a +- value of 15 should be enough.
351 */
352 for (i = 0; i < ARRAY_SIZE(rg3xx_model_details); i++) {
353 u32 adc_min = rg3xx_model_details[i].adc_value - 15;
354 u32 adc_max = rg3xx_model_details[i].adc_value + 15;
355
356 if (adc_min < adc_info && adc_max > adc_info) {
357 board_id = i;
358 break;
359 }
360 }
Chris Morgan8c4e3042023-04-21 10:59:19 -0500361
362 /*
Chris Morgan95db0c02024-01-02 09:46:53 -0600363 * Try to access the eMMC on an RG353V, RG353P, or RG Arc D.
364 * If it's missing, it's an RG353VS, RG353PS, or RG Arc S.
365 * Note we could also check for a touchscreen at 0x1a on i2c2.
Chris Morgan8c4e3042023-04-21 10:59:19 -0500366 */
Chris Morgan95db0c02024-01-02 09:46:53 -0600367 if (board_id == RG353V || board_id == RG353P || board_id == RGARCD) {
Chris Morgan8c4e3042023-04-21 10:59:19 -0500368 mmc = find_mmc_device(0);
369 if (mmc) {
370 ret = mmc_init(mmc);
Chris Morgana824aa62023-05-15 11:00:29 -0500371 if (ret) {
372 if (board_id == RG353V)
373 board_id = RG353VS;
Chris Morgan95db0c02024-01-02 09:46:53 -0600374 else if (board_id == RG353P)
Chris Morgana824aa62023-05-15 11:00:29 -0500375 board_id = RG353PS;
Chris Morgan95db0c02024-01-02 09:46:53 -0600376 else
377 board_id = RGARCS;
Chris Morgana824aa62023-05-15 11:00:29 -0500378 }
Chris Morgan8c4e3042023-04-21 10:59:19 -0500379 }
380 }
381
382 if (board_id < 0)
383 return board_id;
384
385 env_set("board", rg3xx_model_details[board_id].board);
386 env_set("board_name",
387 rg3xx_model_details[board_id].board_name);
388 env_set("fdtfile", rg3xx_model_details[board_id].fdtfile);
389
Chris Morgana63ff502024-01-02 09:46:48 -0600390 /* Skip panel detection for when it is not needed. */
391 if (!rg3xx_model_details[board_id].detect_panel)
Chris Morgan3db220e2023-05-15 11:00:30 -0500392 return 0;
393
Chris Morgana63ff502024-01-02 09:46:48 -0600394 /* Warn but don't fail for errors in auto-detection of the panel. */
Chris Morgan3db220e2023-05-15 11:00:30 -0500395 ret = rgxx3_detect_display();
396 if (ret)
Chris Morgana63ff502024-01-02 09:46:48 -0600397 printf("Failed to detect panel type\n");
Chris Morgan3db220e2023-05-15 11:00:30 -0500398
Chris Morgan8c4e3042023-04-21 10:59:19 -0500399 return 0;
400}
401
402int rk_board_late_init(void)
403{
404 int ret;
405
Chris Morgan8c4e3042023-04-21 10:59:19 -0500406 ret = rgxx3_detect_device();
407 if (ret) {
408 printf("Unable to detect device type: %d\n", ret);
409 return ret;
410 }
411
Chris Morgana824aa62023-05-15 11:00:29 -0500412 /* Turn off red LED and turn on orange LED. */
413 writel(GPIO_WRITEMASK(GPIO_C7 | GPIO_C6 | GPIO_C5) | GPIO_C6,
414 (GPIO0_BASE + GPIO_SWPORT_DR_H));
415
Chris Morgan8c4e3042023-04-21 10:59:19 -0500416 if (IS_ENABLED(CONFIG_DM_PWM))
417 startup_buzz();
418
419 return 0;
420}
Chris Morgana824aa62023-05-15 11:00:29 -0500421
422int ft_board_setup(void *blob, struct bd_info *bd)
423{
Chris Morgana63ff502024-01-02 09:46:48 -0600424 const struct rg353_panel *panel = NULL;
425 int node, ret, i;
Chris Morgana824aa62023-05-15 11:00:29 -0500426 char *env;
427
428 /* No fixups necessary for the RG503 */
429 env = env_get("board_name");
430 if (env && (!strcmp(env, rg3xx_model_details[RG503].board_name)))
431 return 0;
432
433 /* Change the model name of the RG353M */
434 if (env && (!strcmp(env, rg3xx_model_details[RG353M].board_name)))
435 fdt_setprop(blob, 0, "model",
436 rg3xx_model_details[RG353M].board_name,
437 sizeof(rg3xx_model_details[RG353M].board_name));
438
Chris Morgana63ff502024-01-02 09:46:48 -0600439 env = env_get("panel");
440 if (!env) {
441 printf("Can't get panel env\n");
442 return 0;
443 }
444
Chris Morgan3db220e2023-05-15 11:00:30 -0500445 /*
446 * Check if the environment variable doesn't equal the panel.
447 * If it doesn't, update the devicetree to the correct panel.
448 */
449 node = fdt_path_offset(blob, "/dsi@fe060000/panel@0");
450 if (!(node > 0)) {
451 printf("Can't find the DSI node\n");
452 return -ENODEV;
453 }
454
Chris Morgan3db220e2023-05-15 11:00:30 -0500455 ret = fdt_node_check_compatible(blob, node, env);
456 if (ret < 0)
457 return -ENODEV;
458
459 /* Panels match, return 0. */
460 if (!ret)
461 return 0;
462
Chris Morgana63ff502024-01-02 09:46:48 -0600463 /* Panels don't match, search by first compatible value. */
464 for (i = 0; i < ARRAY_SIZE(rg353_panel_details); i++) {
465 if (!strcmp(env, rg353_panel_details[i].panel_compat[0])) {
466 panel = &rg353_panel_details[i];
467 break;
468 }
469 }
470
471 if (!panel) {
472 printf("Unable to identify panel by compat string\n");
473 return -ENODEV;
474 }
475
476 /* Set the compatible with the auto-detected values */
477 fdt_setprop_string(blob, node, "compatible", panel->panel_compat[0]);
478 if (panel->panel_compat[1])
479 fdt_appendprop_string(blob, node, "compatible",
480 panel->panel_compat[1]);
Chris Morgan3db220e2023-05-15 11:00:30 -0500481
Chris Morgana824aa62023-05-15 11:00:29 -0500482 return 0;
483}