blob: 865d4bddb7f917c6b2dbf72cbb76ddaacf3cd53a [file] [log] [blame]
Nikhil M Jain94272742023-01-31 15:35:17 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2023 Texas Instruments Incorporated - https://www.ti.com/
4 * Nikhil M Jain, n-jain1@ti.com
5 *
6 * based on the linux tidss driver, which is
7 *
8 * (C) Copyright 2018 Texas Instruments Incorporated - https://www.ti.com/
9 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
10 */
11
Nikhil M Jain94272742023-01-31 15:35:17 +053012#include <dm.h>
13#include <clk.h>
14#include <log.h>
15#include <video.h>
16#include <errno.h>
17#include <panel.h>
18#include <reset.h>
19#include <malloc.h>
20#include <fdtdec.h>
21#include <syscon.h>
22#include <regmap.h>
23#include <cpu_func.h>
24#include <media_bus_format.h>
25
26#include <asm/io.h>
27#include <asm/cache.h>
28#include <asm/utils.h>
29#include <asm/bitops.h>
30
31#include <dm/devres.h>
32#include <dm/of_access.h>
33#include <dm/device_compat.h>
34#include <dm/device-internal.h>
35
36#include <linux/bug.h>
37#include <linux/err.h>
38#include <linux/delay.h>
39#include <linux/iopoll.h>
40
41#include "tidss_drv.h"
42#include "tidss_regs.h"
43
44DECLARE_GLOBAL_DATA_PTR;
45
46/* Panel parameters */
47enum {
48 LCD_MAX_WIDTH = 1920,
49 LCD_MAX_HEIGHT = 1200,
50 LCD_MAX_LOG2_BPP = VIDEO_BPP32,
51};
52
53static const u16 *dss_common_regmap;
54
55static const u16 tidss_am62x_common_regs[DSS_COMMON_REG_TABLE_LEN] = {
56 [DSS_REVISION_OFF] = 0x4,
57 [DSS_SYSCONFIG_OFF] = 0x8,
58 [DSS_SYSSTATUS_OFF] = 0x20,
59 [DSS_IRQ_EOI_OFF] = 0x24,
60 [DSS_IRQSTATUS_RAW_OFF] = 0x28,
61 [DSS_IRQSTATUS_OFF] = 0x2c,
62 [DSS_IRQENABLE_SET_OFF] = 0x30,
63 [DSS_IRQENABLE_CLR_OFF] = 0x40,
64 [DSS_VID_IRQENABLE_OFF] = 0x44,
65 [DSS_VID_IRQSTATUS_OFF] = 0x58,
66 [DSS_VP_IRQENABLE_OFF] = 0x70,
67 [DSS_VP_IRQSTATUS_OFF] = 0x7c,
68
69 [WB_IRQENABLE_OFF] = 0x88,
70 [WB_IRQSTATUS_OFF] = 0x8c,
71
72 [DSS_GLOBAL_MFLAG_ATTRIBUTE_OFF] = 0x90,
73 [DSS_GLOBAL_OUTPUT_ENABLE_OFF] = 0x94,
74 [DSS_GLOBAL_BUFFER_OFF] = 0x98,
75 [DSS_CBA_CFG_OFF] = 0x9c,
76 [DSS_DBG_CONTROL_OFF] = 0xa0,
77 [DSS_DBG_STATUS_OFF] = 0xa4,
78 [DSS_CLKGATING_DISABLE_OFF] = 0xa8,
79 [DSS_SECURE_DISABLE_OFF] = 0xac,
80};
81
82/* TIDSS AM62x Features */
83const struct dss_features dss_am625_feats = {
84 .max_pclk_khz = {
85 [DSS_VP_DPI] = 165000,
86 [DSS_VP_OLDI] = 165000,
87 },
88
89 .subrev = DSS_AM625,
90
91 .common = "common",
92 .common_regs = tidss_am62x_common_regs,
93
94 .num_vps = 2,
95 .vp_name = { "vp1", "vp2" },
96 .ovr_name = { "ovr1", "ovr2" },
97 .vpclk_name = { "vp1", "vp2" },
98 .vp_bus_type = { DSS_VP_OLDI, DSS_VP_DPI },
99
100 .vp_feat = { .color = {
101 .has_ctm = true,
102 .gamma_size = 256,
103 .gamma_type = TIDSS_GAMMA_8BIT,
104 },
105 },
106
107 .num_planes = 2,
108 /* note: vid is plane_id 0 and vidl1 is plane_id 1 */
Devarsh Thakkar217809d2024-01-24 14:37:11 +0530109 .vid_name = { "vidl1", "vid" },
Nikhil M Jain94272742023-01-31 15:35:17 +0530110 .vid_lite = { true, false },
111 .vid_order = { 1, 0 },
112};
113
114/* Wrapper functions to write and read TI_DSS registers */
115static void dss_write(struct tidss_drv_priv *priv, u16 reg, u32 val)
116{
117 writel(val, priv->base_common + reg);
118}
119
120static u32 dss_read(struct tidss_drv_priv *priv, u16 reg)
121{
122 return readl(priv->base_common + reg);
123}
124
125static void dss_vid_write(struct tidss_drv_priv *priv, u32 hw_plane, u16 reg, u32 val)
126{
127 void __iomem *base = priv->base_vid[hw_plane];
128
129 writel(val, base + reg);
130}
131
132static u32 dss_vid_read(struct tidss_drv_priv *priv, u32 hw_plane, u16 reg)
133{
134 void __iomem *base = priv->base_vid[hw_plane];
135
136 return readl(base + reg);
137}
138
139static void dss_ovr_write(struct tidss_drv_priv *priv, u32 hw_videoport,
140 u16 reg, u32 val)
141{
142 void __iomem *base = priv->base_ovr[hw_videoport];
143
144 writel(val, base + reg);
145}
146
147static u32 dss_ovr_read(struct tidss_drv_priv *priv, u32 hw_videoport, u16 reg)
148{
149 void __iomem *base = priv->base_ovr[hw_videoport];
150
151 return readl(base + reg);
152}
153
154static void dss_vp_write(struct tidss_drv_priv *priv, u32 hw_videoport,
155 u16 reg, u32 val)
156{
157 void __iomem *base = priv->base_vp[hw_videoport];
158
159 writel(val, base + reg);
160}
161
162static u32 dss_vp_read(struct tidss_drv_priv *priv, u32 hw_videoport, u16 reg)
163{
164 void __iomem *base = priv->base_vp[hw_videoport];
165
166 return readl(base + reg);
167}
168
169/* generate mask on a register */
170static u32 FLD_MASK(u32 start, u32 end)
171{
172 return ((1 << (start - end + 1)) - 1) << end;
173}
174
175/* set the given val in specified range */
176static u32 FLD_VAL(u32 val, u32 start, u32 end)
177{
178 return (val << end) & FLD_MASK(start, end);
179}
180
181/* return the value in the specified range */
182static u32 FLD_GET(u32 val, u32 start, u32 end)
183{
184 return (val & FLD_MASK(start, end)) >> end;
185}
186
187/* modify the value of the specified range */
188static u32 FLD_MOD(u32 orig, u32 val, u32 start, u32 end)
189{
190 return (orig & ~FLD_MASK(start, end)) | FLD_VAL(val, start, end);
191}
192
193/* read and modify common register region of DSS*/
194__maybe_unused
195static u32 REG_GET(struct tidss_drv_priv *priv, u32 idx, u32 start, u32 end)
196{
197 return FLD_GET(dss_read(priv, idx), start, end);
198}
199
200static void REG_FLD_MOD(struct tidss_drv_priv *priv, u32 idx, u32 val,
201 u32 start, u32 end)
202{
203 dss_write(priv, idx, FLD_MOD(dss_read(priv, idx), val,
204 start, end));
205}
206
207/* read and modify planes vid1 and vid2 register of DSS*/
208static u32 VID_REG_GET(struct tidss_drv_priv *priv, u32 hw_plane, u32 idx,
209 u32 start, u32 end)
210{
211 return FLD_GET(dss_vid_read(priv, hw_plane, idx), start, end);
212}
213
214static void VID_REG_FLD_MOD(struct tidss_drv_priv *priv, u32 hw_plane, u32 idx,
215 u32 val, u32 start, u32 end)
216{
217 dss_vid_write(priv, hw_plane, idx,
218 FLD_MOD(dss_vid_read(priv, hw_plane, idx),
219 val, start, end));
220}
221
222/* read and modify port vid1 and vid2 registers of DSS*/
223__maybe_unused
224static u32 VP_REG_GET(struct tidss_drv_priv *priv, u32 vp, u32 idx,
225 u32 start, u32 end)
226{
227 return FLD_GET(dss_vp_read(priv, vp, idx), start, end);
228}
229
230static void VP_REG_FLD_MOD(struct tidss_drv_priv *priv, u32 vp, u32 idx, u32 val,
231 u32 start, u32 end)
232{
233 dss_vp_write(priv, vp, idx, FLD_MOD(dss_vp_read(priv, vp, idx),
234 val, start, end));
235}
236
237/* read and modify overlay ovr1 and ovr2 registers of DSS*/
238__maybe_unused
239static u32 OVR_REG_GET(struct tidss_drv_priv *priv, u32 ovr, u32 idx,
240 u32 start, u32 end)
241{
242 return FLD_GET(dss_ovr_read(priv, ovr, idx), start, end);
243}
244
245static void OVR_REG_FLD_MOD(struct tidss_drv_priv *priv, u32 ovr, u32 idx,
246 u32 val, u32 start, u32 end)
247{
248 dss_ovr_write(priv, ovr, idx, FLD_MOD(dss_ovr_read(priv, ovr, idx),
249 val, start, end));
250}
251
252static void dss_oldi_tx_power(struct tidss_drv_priv *priv, bool power)
253{
254 u32 val;
255
256 if (WARN_ON(!priv->oldi_io_ctrl))
257 return;
258
259 if (priv->feat->subrev == DSS_AM625) {
260 if (power) {
261 switch (priv->oldi_mode) {
262 case OLDI_SINGLE_LINK_SINGLE_MODE:
263 /* Power down OLDI TX 1 */
264 val = OLDI1_PWRDN_TX;
265 break;
266 case OLDI_DUAL_LINK:
267 /* No Power down */
268 val = 0;
269 break;
270 default:
271 /* Power down both the OLDI TXes */
272 val = OLDI_BANDGAP_PWR | OLDI0_PWRDN_TX | OLDI1_PWRDN_TX;
273 break;
274 }
275 } else {
276 val = OLDI_BANDGAP_PWR | OLDI0_PWRDN_TX | OLDI1_PWRDN_TX;
277 }
278 regmap_update_bits(priv->oldi_io_ctrl, OLDI_PD_CTRL,
279 OLDI_BANDGAP_PWR | OLDI0_PWRDN_TX | OLDI1_PWRDN_TX, val);
280 }
281}
282
283static void dss_set_num_datalines(struct tidss_drv_priv *priv,
284 u32 hw_videoport)
285{
286 int v;
287 u32 num_lines = priv->bus_format->data_width;
288
289 switch (num_lines) {
290 case 12:
291 v = 0; break;
292 case 16:
293 v = 1; break;
294 case 18:
295 v = 2; break;
296 case 24:
297 v = 3; break;
298 case 30:
299 v = 4; break;
300 case 36:
301 v = 5; break;
302 default:
303 WARN_ON(1);
304 v = 3;
305 }
306
307 VP_REG_FLD_MOD(priv, hw_videoport, DSS_VP_CONTROL, v, 10, 8);
308}
309
310static void dss_enable_oldi(struct tidss_drv_priv *priv, u32 hw_videoport)
311{
312 u32 oldi_cfg = 0;
313 u32 oldi_reset_bit = BIT(5 + hw_videoport);
314 int count = 0;
315
316 /*
317 * For the moment MASTERSLAVE, and SRC bits of DSS_VP_DSS_OLDI_CFG are
318 * set statically to 0.
319 */
320
321 if (priv->bus_format->data_width == 24)
322 oldi_cfg |= BIT(8); /* MSB */
323 else if (priv->bus_format->data_width != 18)
324 dev_warn(priv->dev, "%s: %d port width not supported\n",
325 __func__, priv->bus_format->data_width);
326
327 oldi_cfg |= BIT(7); /* DEPOL */
328
329 oldi_cfg = FLD_MOD(oldi_cfg, priv->bus_format->oldi_mode_reg_val, 3, 1);
330
331 oldi_cfg |= BIT(12); /* SOFTRST */
332
333 oldi_cfg |= BIT(0); /* ENABLE */
334
335 switch (priv->oldi_mode) {
336 case OLDI_MODE_OFF:
337 oldi_cfg &= ~BIT(0); /* DISABLE */
338 break;
339
340 case OLDI_SINGLE_LINK_SINGLE_MODE:
341 /* All configuration is done for this mode. */
342 break;
343
344 case OLDI_SINGLE_LINK_DUPLICATE_MODE:
345 oldi_cfg |= BIT(5); /* DUPLICATE MODE */
346 break;
347
348 case OLDI_DUAL_LINK:
349 oldi_cfg |= BIT(11); /* DUALMODESYNC */
350 oldi_cfg |= BIT(3); /* data-mapping field also indicates dual-link mode */
351 break;
352
353 default:
354 dev_warn(priv->dev, "%s: Incorrect oldi mode. Returning.\n",
355 __func__);
356 return;
357 }
358
359 dss_vp_write(priv, hw_videoport, DSS_VP_DSS_OLDI_CFG, oldi_cfg);
360
361 while (!(oldi_reset_bit & dss_read(priv, DSS_SYSSTATUS)) &&
362 count < 10000)
363 count++;
364
365 if (!(oldi_reset_bit & dss_read(priv, DSS_SYSSTATUS)))
366 dev_warn(priv->dev, "%s: timeout waiting OLDI reset done\n",
367 __func__);
368}
369
370static const struct dss_color_lut dss_vp_gamma_default_lut[] = {
371 { .red = 0, .green = 0, .blue = 0, },
372 { .red = U16_MAX, .green = U16_MAX, .blue = U16_MAX, },
373};
374
375static void dss_vp_write_gamma_table(struct tidss_drv_priv *priv,
376 u32 hw_videoport)
377{
378 u32 *table = priv->vp_data[hw_videoport].gamma_table;
379 u32 hwlen = priv->feat->vp_feat.color.gamma_size;
380 unsigned int i;
381
382 dev_dbg(priv->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
383
384 for (i = 0; i < hwlen; ++i) {
385 u32 v = table[i];
386
387 v |= i << 24;
388
389 dss_vp_write(priv, hw_videoport, DSS_VP_GAMMA_TABLE, v);
390 }
391}
392
393static void dss_vp_set_gamma(struct tidss_drv_priv *priv,
394 u32 hw_videoport, const struct dss_color_lut *lut,
395 unsigned int length)
396{
397 u32 *table = priv->vp_data[hw_videoport].gamma_table;
398 u32 hwlen = priv->feat->vp_feat.color.gamma_size;
399 u32 hwbits;
400 unsigned int i;
401
402 dev_dbg(priv->dev, "%s: hw_videoport %d, lut len %u, hw len %u\n",
403 __func__, hw_videoport, length, hwlen);
404
405 if (priv->feat->vp_feat.color.gamma_type == TIDSS_GAMMA_10BIT)
406 hwbits = 10;
407 else
408 hwbits = 8;
409
410 lut = dss_vp_gamma_default_lut;
411 length = ARRAY_SIZE(dss_vp_gamma_default_lut);
412
413 for (i = 0; i < length - 1; ++i) {
414 unsigned int first = i * (hwlen - 1) / (length - 1);
415 unsigned int last = (i + 1) * (hwlen - 1) / (length - 1);
416 unsigned int w = last - first;
417 u16 r, g, b;
418 unsigned int j;
419
420 if (w == 0)
421 continue;
422
423 for (j = 0; j <= w; j++) {
424 r = (lut[i].red * (w - j) + lut[i + 1].red * j) / w;
425 g = (lut[i].green * (w - j) + lut[i + 1].green * j) / w;
426 b = (lut[i].blue * (w - j) + lut[i + 1].blue * j) / w;
427
428 r >>= 16 - hwbits;
429 g >>= 16 - hwbits;
430 b >>= 16 - hwbits;
431
432 table[first + j] = (r << (hwbits * 2)) |
433 (g << hwbits) | b;
434 }
435 }
436
437 dss_vp_write_gamma_table(priv, hw_videoport);
438}
439
440void dss_vp_enable(struct tidss_drv_priv *priv, u32 hw_videoport, struct display_timing *timing)
441{
442 bool align, onoff, rf, ieo, ipc, ihs, ivs;
443 u32 hsw, hfp, hbp, vsw, vfp, vbp;
444
445 dss_set_num_datalines(priv, hw_videoport);
446
447 /* panel parameters to set clocks for video port*/
448 hfp = timing->hfront_porch.typ;
449 hsw = timing->hsync_len.typ;
450 hbp = timing->hback_porch.typ;
451 vfp = timing->vfront_porch.typ;
452 vsw = timing->vsync_len.typ;
453 vbp = timing->vback_porch.typ;
454
455 dss_vp_write(priv, hw_videoport, DSS_VP_TIMING_H,
456 FLD_VAL(hsw - 1, 7, 0) |
457 FLD_VAL(hfp - 1, 19, 8) | FLD_VAL(hbp - 1, 31, 20));
458
459 dss_vp_write(priv, hw_videoport, DSS_VP_TIMING_V,
460 FLD_VAL(vsw - 1, 7, 0) |
461 FLD_VAL(vfp, 19, 8) | FLD_VAL(vbp, 31, 20));
462
463 ivs = !!(timing->flags & (1 << 3));
464
465 ihs = !!(timing->flags & (1 << 1));
466
467 ieo = 0;
468
469 ipc = 0;
470
471 /* always use the 'rf' setting */
472 onoff = true;
473
474 rf = true;
475
476 /* always use aligned syncs */
477 align = true;
478
479 /* always use DE_HIGH for OLDI */
480 if (priv->feat->vp_bus_type[hw_videoport] == DSS_VP_OLDI)
481 ieo = false;
482
483 dss_vp_write(priv, hw_videoport, DSS_VP_POL_FREQ,
484 FLD_VAL(align, 18, 18) |
485 FLD_VAL(onoff, 17, 17) |
486 FLD_VAL(rf, 16, 16) |
487 FLD_VAL(ieo, 15, 15) |
488 FLD_VAL(ipc, 14, 14) |
489 FLD_VAL(ihs, 13, 13) |
490 FLD_VAL(ivs, 12, 12));
491
492 dss_vp_write(priv, hw_videoport, DSS_VP_SIZE_SCREEN,
493 FLD_VAL(timing->hactive.typ - 1, 11, 0) |
494 FLD_VAL(timing->vactive.typ - 1, 27, 16));
495
496 VP_REG_FLD_MOD(priv, hw_videoport, DSS_VP_CONTROL, 1, 0, 0);
497}
498
499enum c8_to_c12_mode { C8_TO_C12_REPLICATE, C8_TO_C12_MAX, C8_TO_C12_MIN };
500
501static u16 c8_to_c12(u8 c8, enum c8_to_c12_mode mode)
502{
503 u16 c12;
504
505 c12 = c8 << 4;
506
507 switch (mode) {
508 case C8_TO_C12_REPLICATE:
509 /* Copy c8 4 MSB to 4 LSB for full scale c12 */
510 c12 |= c8 >> 4;
511 break;
512 case C8_TO_C12_MAX:
513 c12 |= 0xF;
514 break;
515 default:
516 case C8_TO_C12_MIN:
517 break;
518 }
519
520 return c12;
521}
522
523static u64 argb8888_to_argb12121212(u32 argb8888, enum c8_to_c12_mode m)
524{
525 u8 a, r, g, b;
526 u64 v;
527
528 a = (argb8888 >> 24) & 0xff;
529 r = (argb8888 >> 16) & 0xff;
530 g = (argb8888 >> 8) & 0xff;
531 b = (argb8888 >> 0) & 0xff;
532
533 v = ((u64)c8_to_c12(a, m) << 36) | ((u64)c8_to_c12(r, m) << 24) |
534 ((u64)c8_to_c12(g, m) << 12) | (u64)c8_to_c12(b, m);
535
536 return v;
537}
538
539static void dss_vp_set_default_color(struct tidss_drv_priv *priv,
540 u32 hw_videoport, u32 default_color)
541{
542 u64 v;
543
544 v = argb8888_to_argb12121212(default_color, C8_TO_C12_REPLICATE);
545 dss_ovr_write(priv, hw_videoport,
546 DSS_OVR_DEFAULT_COLOR, v & 0xffffffff);
547 dss_ovr_write(priv, hw_videoport,
548 DSS_OVR_DEFAULT_COLOR2, (v >> 32) & 0xffff);
549}
550
551int dss_vp_enable_clk(struct tidss_drv_priv *priv, u32 hw_videoport)
552{
553 int ret = clk_prepare_enable(&priv->vp_clk[hw_videoport]);
554
555 if (ret)
556 dev_dbg(priv->dev, "%s: enabling clk failed: %d\n", __func__,
557 ret);
558
559 return ret;
560}
561
562void dss_vp_prepare(struct tidss_drv_priv *priv, u32 hw_videoport)
563{
564 dss_vp_set_gamma(priv, hw_videoport, NULL, 0);
565 dss_vp_set_default_color(priv, 0, 0);
566
567 if (priv->feat->vp_bus_type[hw_videoport] == DSS_VP_OLDI) {
568 dss_oldi_tx_power(priv, true);
569 dss_enable_oldi(priv, hw_videoport);
570 }
571}
572
573static
574unsigned int dss_pclk_diff(unsigned long rate, unsigned long real_rate)
575{
576 int r = rate / 100, rr = real_rate / 100;
577
578 return (unsigned int)(abs(((rr - r) * 100) / r));
579}
580
581int dss_vp_set_clk_rate(struct tidss_drv_priv *priv, u32 hw_videoport,
582 unsigned long rate)
583{
584 int r;
585 unsigned long new_rate;
586
587 /*
588 * For AM625 OLDI video ports, the requested pixel clock needs to take into account the
589 * serial clock required for the serialization of DPI signals into LVDS signals. The
590 * incoming pixel clock on the OLDI video port gets divided by 7 whenever OLDI enable bit
591 * gets set.
592 */
593 if (priv->feat->vp_bus_type[hw_videoport] == DSS_VP_OLDI &&
594 priv->feat->subrev == DSS_AM625)
595 rate *= 7;
596
597 r = clk_set_rate(&priv->vp_clk[hw_videoport], rate);
598
599 new_rate = clk_get_rate(&priv->vp_clk[hw_videoport]);
600
601 if (dss_pclk_diff(rate, new_rate) > 5)
602 dev_warn(priv->dev,
603 "vp%d: Clock rate %lu differs over 5%% from requested %lu\n",
604 hw_videoport, new_rate, rate);
605
606 dev_dbg(priv->dev, "vp%d: new rate %lu Hz (requested %lu Hz)\n",
607 hw_videoport, clk_get_rate(&priv->vp_clk[hw_videoport]), rate);
608 return 0;
609}
610
611static void dss_ovr_set_plane(struct tidss_drv_priv *priv,
612 u32 hw_plane, u32 hw_ovr,
613 u32 x, u32 y, u32 layer)
614{
615 OVR_REG_FLD_MOD(priv, hw_ovr, DSS_OVR_ATTRIBUTES(layer),
616 0x1, 4, 1);
617 OVR_REG_FLD_MOD(priv, hw_ovr, DSS_OVR_ATTRIBUTES(layer),
618 x, 17, 6);
619 OVR_REG_FLD_MOD(priv, hw_ovr, DSS_OVR_ATTRIBUTES(layer),
620 y, 30, 19);
621}
622
623void dss_ovr_enable_layer(struct tidss_drv_priv *priv,
624 u32 hw_ovr, u32 layer, bool enable)
625{
626 OVR_REG_FLD_MOD(priv, hw_ovr, DSS_OVR_ATTRIBUTES(layer),
627 !!enable, 0, 0);
628}
629
630static void dss_vid_csc_enable(struct tidss_drv_priv *priv, u32 hw_plane,
631 bool enable)
632{
633 VID_REG_FLD_MOD(priv, hw_plane, DSS_VID_ATTRIBUTES, !!enable, 9, 9);
634}
635
636int dss_plane_setup(struct tidss_drv_priv *priv, u32 hw_plane, u32 hw_videoport)
637{
638 VID_REG_FLD_MOD(priv, hw_plane, DSS_VID_ATTRIBUTES,
639 priv->pixel_format, 6, 1);
640
641 dss_vid_write(priv, hw_plane, DSS_VID_PICTURE_SIZE,
642 ((LCD_MAX_WIDTH - 1) | ((LCD_MAX_HEIGHT - 1) << 16)));
643
644 dss_vid_csc_enable(priv, hw_plane, false);
645
646 dss_vid_write(priv, hw_plane, DSS_VID_GLOBAL_ALPHA, 0xFF);
647
648 VID_REG_FLD_MOD(priv, hw_plane, DSS_VID_ATTRIBUTES, 1, 28, 28);
649 return 0;
650}
651
652int dss_plane_enable(struct tidss_drv_priv *priv, u32 hw_plane, bool enable)
653{
654 VID_REG_FLD_MOD(priv, hw_plane, DSS_VID_ATTRIBUTES, !!enable, 0, 0);
655
656 return 0;
657}
658
659static u32 dss_vid_get_fifo_size(struct tidss_drv_priv *priv, u32 hw_plane)
660{
661 return VID_REG_GET(priv, hw_plane, DSS_VID_BUF_SIZE_STATUS, 15, 0);
662}
663
664static void dss_vid_set_mflag_threshold(struct tidss_drv_priv *priv,
665 u32 hw_plane, u32 low, u32 high)
666{
667 dss_vid_write(priv, hw_plane, DSS_VID_MFLAG_THRESHOLD,
668 FLD_VAL(high, 31, 16) | FLD_VAL(low, 15, 0));
669}
670
671static
672void dss_vid_set_buf_threshold(struct tidss_drv_priv *priv,
673 u32 hw_plane, u32 low, u32 high)
674{
675 dss_vid_write(priv, hw_plane, DSS_VID_BUF_THRESHOLD,
676 FLD_VAL(high, 31, 16) | FLD_VAL(low, 15, 0));
677}
678
679static void dss_plane_init(struct tidss_drv_priv *priv)
680{
681 unsigned int hw_plane;
682 u32 cba_lo_pri = 1;
683 u32 cba_hi_pri = 0;
684
685 REG_FLD_MOD(priv, DSS_CBA_CFG, cba_lo_pri, 2, 0);
686 REG_FLD_MOD(priv, DSS_CBA_CFG, cba_hi_pri, 5, 3);
687
688 /* MFLAG_CTRL = ENABLED */
689 REG_FLD_MOD(priv, DSS_GLOBAL_MFLAG_ATTRIBUTE, 2, 1, 0);
690 /* MFLAG_START = MFLAGNORMALSTARTMODE */
691 REG_FLD_MOD(priv, DSS_GLOBAL_MFLAG_ATTRIBUTE, 0, 6, 6);
692
693 for (hw_plane = 0; hw_plane < priv->feat->num_planes; hw_plane++) {
694 u32 size = dss_vid_get_fifo_size(priv, hw_plane);
695 u32 thr_low, thr_high;
696 u32 mflag_low, mflag_high;
697 u32 preload;
698
699 thr_high = size - 1;
700 thr_low = size / 2;
701
702 mflag_high = size * 2 / 3;
703 mflag_low = size / 3;
704
705 preload = thr_low;
706
707 dev_dbg(priv->dev,
708 "%s: bufsize %u, buf_threshold %u/%u, mflag threshold %u/%u preload %u\n",
709 priv->feat->vid_name[hw_plane],
710 size,
711 thr_high, thr_low,
712 mflag_high, mflag_low,
713 preload);
714
715 dss_vid_set_buf_threshold(priv, hw_plane,
716 thr_low, thr_high);
717 dss_vid_set_mflag_threshold(priv, hw_plane,
718 mflag_low, mflag_high);
719
720 dss_vid_write(priv, hw_plane, DSS_VID_PRELOAD, preload);
721
722 /* Prefech up to PRELOAD value */
723 VID_REG_FLD_MOD(priv, hw_plane, DSS_VID_ATTRIBUTES, 0,
724 19, 19);
725 }
726}
727
728static void dss_vp_init(struct tidss_drv_priv *priv)
729{
730 unsigned int i;
731
732 /* Enable the gamma Shadow bit-field for all VPs*/
733 for (i = 0; i < priv->feat->num_vps; i++)
734 VP_REG_FLD_MOD(priv, i, DSS_VP_CONFIG, 1, 2, 2);
735}
736
737static int dss_init_am65x_oldi_io_ctrl(struct udevice *dev,
738 struct tidss_drv_priv *priv)
739{
740 struct udevice *syscon;
741 struct regmap *regmap;
742 int ret = 0;
743
744 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, "ti,am65x-oldi-io-ctrl",
745 &syscon);
746 if (ret) {
747 debug("unable to find ti,am65x-oldi-io-ctrl syscon device (%d)\n", ret);
748 return ret;
749 }
750
751 /* get grf-reg base address */
752 regmap = syscon_get_regmap(syscon);
753 if (!regmap) {
754 debug("unable to find rockchip grf regmap\n");
755 return -ENODEV;
756 }
757 priv->oldi_io_ctrl = regmap;
758 return 0;
759}
760
761static int tidss_drv_probe(struct udevice *dev)
762{
763 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
764 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
765 struct tidss_drv_priv *priv = dev_get_priv(dev);
766 struct udevice *panel = NULL;
767 struct display_timing timings;
768 unsigned int i;
769 int ret = 0;
770 const char *mode;
771
772 priv->dev = dev;
773
774 priv->feat = &dss_am625_feats;
775
776 /*
777 * set your plane format based on your bmp image
778 * Supported 24bpp and 32bpp bmpimages
779 */
780
781 priv->pixel_format = DSS_FORMAT_XRGB8888;
782
783 dss_common_regmap = priv->feat->common_regs;
784
785 ret = uclass_first_device_err(UCLASS_PANEL, &panel);
786 if (ret) {
787 if (ret != -ENODEV)
788 dev_err(dev, "panel device error %d\n", ret);
789 return ret;
790 }
791
792 ret = panel_get_display_timing(panel, &timings);
793 if (ret) {
794 ret = ofnode_decode_panel_timing(dev_ofnode(panel),
795 &timings);
796 if (ret) {
797 dev_err(dev, "decode display timing error %d\n", ret);
798 return ret;
799 }
800 }
801
802 mode = ofnode_read_string(dev_ofnode(panel), "data-mapping");
803 if (!mode) {
804 debug("%s: Could not read mode property\n", dev->name);
805 return -EINVAL;
806 }
807
808 uc_priv->bpix = VIDEO_BPP32;
809
810 if (!strcmp(mode, "vesa-24"))
811 priv->bus_format = &dss_bus_formats[7];
812 else
813 priv->bus_format = &dss_bus_formats[8];
814
815 /* Common address */
Devarsh Thakkar217809d2024-01-24 14:37:11 +0530816 priv->base_common = dev_remap_addr_name(dev, priv->feat->common);
Nikhil M Jain94272742023-01-31 15:35:17 +0530817 if (!priv->base_common)
818 return -EINVAL;
819
820 /* plane address setup and enable */
821 for (i = 0; i < priv->feat->num_planes; i++) {
Devarsh Thakkar217809d2024-01-24 14:37:11 +0530822 priv->base_vid[i] = dev_remap_addr_name(dev, priv->feat->vid_name[i]);
Nikhil M Jain94272742023-01-31 15:35:17 +0530823 if (!priv->base_vid[i])
824 return -EINVAL;
825 }
826
827 dss_vid_write(priv, 0, DSS_VID_BA_0, uc_plat->base & 0xffffffff);
828 dss_vid_write(priv, 0, DSS_VID_BA_EXT_0, (u64)uc_plat->base >> 32);
829 dss_vid_write(priv, 0, DSS_VID_BA_1, uc_plat->base & 0xffffffff);
830 dss_vid_write(priv, 0, DSS_VID_BA_EXT_1, (u64)uc_plat->base >> 32);
831
832 ret = dss_plane_setup(priv, 0, 0);
833 if (ret) {
834 dss_plane_enable(priv, 0, false);
835 return ret;
836 }
837
838 dss_plane_enable(priv, 0, true);
839 dss_plane_init(priv);
840
841 /* video port address clocks and enable */
842 for (i = 0; i < priv->feat->num_vps; i++) {
Devarsh Thakkar217809d2024-01-24 14:37:11 +0530843 priv->base_ovr[i] = dev_remap_addr_name(dev, priv->feat->ovr_name[i]);
844 priv->base_vp[i] = dev_remap_addr_name(dev, priv->feat->vp_name[i]);
Nikhil M Jain94272742023-01-31 15:35:17 +0530845 }
846
847 ret = clk_get_by_name(dev, "vp1", &priv->vp_clk[0]);
848 if (ret) {
849 dev_err(dev, "video port %d clock enable error %d\n", i, ret);
850 return ret;
851 }
852
853 dss_ovr_set_plane(priv, 1, 0, 0, 0, 0);
854 dss_ovr_enable_layer(priv, 0, 0, true);
855
856 /* Video Port cloks */
857 dss_vp_enable_clk(priv, 0);
858
859 dss_vp_set_clk_rate(priv, 0, timings.pixelclock.typ * 1000);
860
861 priv->oldi_mode = OLDI_MODE_OFF;
862 uc_priv->xsize = timings.hactive.typ;
863 uc_priv->ysize = timings.vactive.typ;
864 if (priv->feat->subrev == DSS_AM65X || priv->feat->subrev == DSS_AM625) {
865 priv->oldi_mode = OLDI_DUAL_LINK;
866 if (priv->oldi_mode) {
867 ret = dss_init_am65x_oldi_io_ctrl(dev, priv);
868 if (ret)
869 return ret;
870 }
871 }
872
873 dss_vp_prepare(priv, 0);
874 dss_vp_enable(priv, 0, &timings);
875 dss_vp_init(priv);
876
877 ret = clk_get_by_name(dev, "fck", &priv->fclk);
878 if (ret) {
879 dev_err(dev, "peripheral clock get error %d\n", ret);
880 return ret;
881 }
882
883 ret = clk_enable(&priv->fclk);
884 if (ret) {
885 dev_err(dev, "peripheral clock enable error %d\n", ret);
886 return ret;
887 }
888
889 if (IS_ERR(&priv->fclk)) {
890 dev_err(dev, "%s: Failed to get fclk: %ld\n",
891 __func__, PTR_ERR(&priv->fclk));
892 return PTR_ERR(&priv->fclk);
893 }
894
895 dev_dbg(dev, "DSS fclk %lu Hz\n", clk_get_rate(&priv->fclk));
896
897 video_set_flush_dcache(dev, true);
898 return 0;
899}
900
901static int tidss_drv_remove(struct udevice *dev)
902{
Nikhil M Jain44ceae62023-07-27 12:01:26 +0530903 if (CONFIG_IS_ENABLED(VIDEO_REMOVE)) {
904 struct tidss_drv_priv *priv = dev_get_priv(dev);
Nikhil M Jain94272742023-01-31 15:35:17 +0530905
Nikhil M Jain44ceae62023-07-27 12:01:26 +0530906 VP_REG_FLD_MOD(priv, 0, DSS_VP_CONTROL, 0, 0, 0);
907 }
Nikhil M Jain94272742023-01-31 15:35:17 +0530908 return 0;
909}
910
911static int tidss_drv_bind(struct udevice *dev)
912{
913 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
914
915 uc_plat->size = ((LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
916 (1 << LCD_MAX_LOG2_BPP)) >> 3) + 0x20;
917 return 0;
918}
919
920static const struct udevice_id tidss_drv_ids[] = {
921 { .compatible = "ti,am625-dss" },
922 { }
923};
924
925U_BOOT_DRIVER(tidss_drv) = {
926 .name = "tidss_drv",
927 .id = UCLASS_VIDEO,
928 .of_match = tidss_drv_ids,
929 .bind = tidss_drv_bind,
930 .probe = tidss_drv_probe,
931 .remove = tidss_drv_remove,
932 .priv_auto = sizeof(struct tidss_drv_priv),
Nikhil M Jain44ceae62023-07-27 12:01:26 +0530933#if CONFIG_IS_ENABLED(VIDEO_REMOVE)
Nikhil M Jain94272742023-01-31 15:35:17 +0530934 .flags = DM_FLAG_OS_PREPARE,
Nikhil M Jain44ceae62023-07-27 12:01:26 +0530935#else
936 .flags = DM_FLAG_OS_PREPARE | DM_FLAG_LEAVE_PD_ON,
937#endif
Nikhil M Jain94272742023-01-31 15:35:17 +0530938};