Prepare v2023.10
[platform/kernel/u-boot.git] / drivers / video / simple_panel.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <backlight.h>
9 #include <dm.h>
10 #include <log.h>
11 #include <mipi_dsi.h>
12 #include <panel.h>
13 #include <asm/gpio.h>
14 #include <power/regulator.h>
15
16 struct simple_panel_priv {
17         struct udevice *reg;
18         struct udevice *backlight;
19         struct gpio_desc enable;
20 };
21
22 /* List of supported DSI panels */
23 enum {
24         PANEL_NON_DSI,
25         PANASONIC_VVX10F004B00,
26 };
27
28 static const struct mipi_dsi_panel_plat panasonic_vvx10f004b00 = {
29         .mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
30                       MIPI_DSI_CLOCK_NON_CONTINUOUS,
31         .format = MIPI_DSI_FMT_RGB888,
32         .lanes = 4,
33 };
34
35 static int simple_panel_enable_backlight(struct udevice *dev)
36 {
37         struct simple_panel_priv *priv = dev_get_priv(dev);
38         int ret;
39
40         debug("%s: start, backlight = '%s'\n", __func__, priv->backlight->name);
41         dm_gpio_set_value(&priv->enable, 1);
42         ret = backlight_enable(priv->backlight);
43         debug("%s: done, ret = %d\n", __func__, ret);
44         if (ret)
45                 return ret;
46
47         return 0;
48 }
49
50 static int simple_panel_set_backlight(struct udevice *dev, int percent)
51 {
52         struct simple_panel_priv *priv = dev_get_priv(dev);
53         int ret;
54
55         debug("%s: start, backlight = '%s'\n", __func__, priv->backlight->name);
56         dm_gpio_set_value(&priv->enable, 1);
57         ret = backlight_set_brightness(priv->backlight, percent);
58         debug("%s: done, ret = %d\n", __func__, ret);
59         if (ret)
60                 return ret;
61
62         return 0;
63 }
64
65 static int simple_panel_get_display_timing(struct udevice *dev,
66                                            struct display_timing *timings)
67 {
68         const void *blob = gd->fdt_blob;
69
70         return fdtdec_decode_display_timing(blob, dev_of_offset(dev),
71                                             0, timings);
72 }
73
74 static int simple_panel_of_to_plat(struct udevice *dev)
75 {
76         struct simple_panel_priv *priv = dev_get_priv(dev);
77         int ret;
78
79         if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
80                 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
81                                                    "power-supply", &priv->reg);
82                 if (ret) {
83                         debug("%s: Warning: cannot get power supply: ret=%d\n",
84                               __func__, ret);
85                         if (ret != -ENOENT)
86                                 return ret;
87                 }
88         }
89
90         ret = uclass_get_device_by_phandle(UCLASS_PANEL_BACKLIGHT, dev,
91                                                    "backlight", &priv->backlight);
92         if (ret) {
93                 debug("%s: Cannot get backlight: ret=%d\n", __func__, ret);
94                 if (ret != -ENOENT)
95                         return log_ret(ret);
96         }
97
98         ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
99                                    GPIOD_IS_OUT);
100         if (ret) {
101                 debug("%s: Warning: cannot get enable GPIO: ret=%d\n",
102                       __func__, ret);
103                 if (ret != -ENOENT)
104                         return log_ret(ret);
105         }
106
107         return 0;
108 }
109
110 static int simple_panel_probe(struct udevice *dev)
111 {
112         struct simple_panel_priv *priv = dev_get_priv(dev);
113         struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
114         const u32 dsi_data = dev_get_driver_data(dev);
115         int ret;
116
117         if (CONFIG_IS_ENABLED(DM_REGULATOR) && priv->reg) {
118                 debug("%s: Enable regulator '%s'\n", __func__, priv->reg->name);
119                 ret = regulator_set_enable(priv->reg, true);
120                 if (ret)
121                         return ret;
122         }
123
124         switch (dsi_data) {
125         case PANASONIC_VVX10F004B00:
126                 memcpy(plat, &panasonic_vvx10f004b00,
127                        sizeof(panasonic_vvx10f004b00));
128                 break;
129         case PANEL_NON_DSI:
130         default:
131                 break;
132         }
133
134         return 0;
135 }
136
137 static const struct panel_ops simple_panel_ops = {
138         .enable_backlight       = simple_panel_enable_backlight,
139         .set_backlight          = simple_panel_set_backlight,
140         .get_display_timing     = simple_panel_get_display_timing,
141 };
142
143 static const struct udevice_id simple_panel_ids[] = {
144         { .compatible = "simple-panel" },
145         { .compatible = "auo,b133xtn01" },
146         { .compatible = "auo,b116xw03" },
147         { .compatible = "auo,b133htn01" },
148         { .compatible = "boe,nv140fhmn49" },
149         { .compatible = "lg,lb070wv8" },
150         { .compatible = "sharp,lq123p1jx31" },
151         { .compatible = "boe,nv101wxmn51" },
152         { .compatible = "panasonic,vvx10f004b00",
153           .data = PANASONIC_VVX10F004B00 },
154         { }
155 };
156
157 U_BOOT_DRIVER(simple_panel) = {
158         .name           = "simple_panel",
159         .id             = UCLASS_PANEL,
160         .of_match       = simple_panel_ids,
161         .ops            = &simple_panel_ops,
162         .of_to_plat     = simple_panel_of_to_plat,
163         .probe          = simple_panel_probe,
164         .priv_auto      = sizeof(struct simple_panel_priv),
165         .plat_auto      = sizeof(struct mipi_dsi_panel_plat),
166 };