Ondrej Jirman | 55453d4 | 2023-05-25 14:34:35 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2022 Ondrej Jirman <megi@xff.cz> |
| 4 | */ |
Ondrej Jirman | 55453d4 | 2023-05-25 14:34:35 +0200 | [diff] [blame] | 5 | #include <backlight.h> |
| 6 | #include <dm.h> |
| 7 | #include <mipi_dsi.h> |
| 8 | #include <panel.h> |
| 9 | #include <asm/gpio.h> |
| 10 | #include <dm/device_compat.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <power/regulator.h> |
| 13 | |
| 14 | struct hx8394_panel_priv { |
| 15 | struct udevice *reg_vcc; |
| 16 | struct udevice *reg_iovcc; |
| 17 | struct gpio_desc reset; |
| 18 | struct udevice *backlight; |
| 19 | }; |
| 20 | |
| 21 | static const struct display_timing default_timing = { |
| 22 | .pixelclock.typ = 74250000, |
| 23 | .hactive.typ = 720, |
| 24 | .hfront_porch.typ = 40, |
| 25 | .hback_porch.typ = 40, |
| 26 | .hsync_len.typ = 46, |
| 27 | .vactive.typ = 1440, |
| 28 | .vfront_porch.typ = 7, |
| 29 | .vback_porch.typ = 9, |
| 30 | .vsync_len.typ = 7, |
| 31 | .flags = DISPLAY_FLAGS_VSYNC_LOW | DISPLAY_FLAGS_HSYNC_LOW, |
| 32 | }; |
| 33 | |
| 34 | #define dsi_dcs_write_seq(device, seq...) do { \ |
| 35 | static const u8 d[] = { seq }; \ |
| 36 | int ret; \ |
| 37 | ret = mipi_dsi_dcs_write_buffer(device, d, ARRAY_SIZE(d)); \ |
| 38 | if (ret < 0) \ |
| 39 | return ret; \ |
| 40 | } while (0) |
| 41 | |
| 42 | static int hx8394_init_sequence(struct udevice *dev) |
| 43 | { |
| 44 | struct mipi_dsi_panel_plat *plat = dev_get_plat(dev); |
| 45 | struct mipi_dsi_device *device = plat->device; |
| 46 | int ret; |
| 47 | |
| 48 | dsi_dcs_write_seq(device, 0xb9, 0xff, 0x83, 0x94); |
| 49 | dsi_dcs_write_seq(device, 0xb1, 0x48, 0x11, 0x71, 0x09, 0x32, 0x24, |
| 50 | 0x71, 0x31, 0x55, 0x30); |
| 51 | dsi_dcs_write_seq(device, 0xba, 0x63, 0x03, 0x68, 0x6b, 0xb2, 0xc0); |
| 52 | dsi_dcs_write_seq(device, 0xb2, 0x00, 0x80, 0x78, 0x0c, 0x07); |
| 53 | dsi_dcs_write_seq(device, 0xb4, 0x12, 0x63, 0x12, 0x63, 0x12, 0x63, |
| 54 | 0x01, 0x0c, 0x7c, 0x55, 0x00, 0x3f, 0x12, 0x6b, 0x12, |
| 55 | 0x6b, 0x12, 0x6b, 0x01, 0x0c, 0x7c); |
| 56 | dsi_dcs_write_seq(device, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x1c, |
| 57 | 0x00, 0x00, 0x32, 0x10, 0x09, 0x00, 0x09, 0x32, 0x15, |
| 58 | 0xad, 0x05, 0xad, 0x32, 0x00, 0x00, 0x00, 0x00, 0x37, |
| 59 | 0x03, 0x0b, 0x0b, 0x37, 0x00, 0x00, 0x00, 0x0c, 0x40); |
| 60 | dsi_dcs_write_seq(device, 0xd5, 0x19, 0x19, 0x18, 0x18, 0x1b, 0x1b, |
| 61 | 0x1a, 0x1a, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 62 | 0x07, 0x20, 0x21, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, |
| 63 | 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x24, 0x25, 0x18, |
| 64 | 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, |
| 65 | 0x18, 0x18); |
| 66 | dsi_dcs_write_seq(device, 0xd6, 0x18, 0x18, 0x19, 0x19, 0x1b, 0x1b, |
| 67 | 0x1a, 0x1a, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, |
| 68 | 0x00, 0x25, 0x24, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, |
| 69 | 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x21, 0x20, 0x18, |
| 70 | 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, |
| 71 | 0x18, 0x18); |
| 72 | dsi_dcs_write_seq(device, 0xe0, 0x00, 0x04, 0x0c, 0x12, 0x14, 0x18, |
| 73 | 0x1a, 0x18, 0x31, 0x3f, 0x4d, 0x4c, 0x54, 0x65, 0x6b, |
| 74 | 0x70, 0x7f, 0x82, 0x7e, 0x8a, 0x99, 0x4a, 0x48, 0x49, |
| 75 | 0x4b, 0x4a, 0x4c, 0x4b, 0x7f, 0x00, 0x04, 0x0c, 0x11, |
| 76 | 0x13, 0x17, 0x1a, 0x18, 0x31, 0x3f, 0x4d, 0x4c, 0x54, |
| 77 | 0x65, 0x6b, 0x70, 0x7f, 0x82, 0x7e, 0x8a, 0x99, 0x4a, |
| 78 | 0x48, 0x49, 0x4b, 0x4a, 0x4c, 0x4b, 0x7f); |
| 79 | dsi_dcs_write_seq(device, 0xcc, 0x0b); |
| 80 | dsi_dcs_write_seq(device, 0xc0, 0x1f, 0x31); |
| 81 | dsi_dcs_write_seq(device, 0xb6, 0x7d, 0x7d); |
| 82 | dsi_dcs_write_seq(device, 0xd4, 0x02); |
| 83 | dsi_dcs_write_seq(device, 0xbd, 0x01); |
| 84 | dsi_dcs_write_seq(device, 0xb1, 0x00); |
| 85 | dsi_dcs_write_seq(device, 0xbd, 0x00); |
| 86 | dsi_dcs_write_seq(device, 0xc6, 0xed); |
| 87 | |
| 88 | ret = mipi_dsi_dcs_exit_sleep_mode(device); |
| 89 | if (ret) |
| 90 | return ret; |
| 91 | |
| 92 | /* Panel is operational 120 msec after reset */ |
| 93 | mdelay(120); |
| 94 | |
| 95 | ret = mipi_dsi_dcs_set_display_on(device); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int hx8394_panel_enable_backlight(struct udevice *dev) |
| 103 | { |
| 104 | struct mipi_dsi_panel_plat *plat = dev_get_plat(dev); |
| 105 | struct mipi_dsi_device *device = plat->device; |
| 106 | struct hx8394_panel_priv *priv = dev_get_priv(dev); |
| 107 | int ret; |
| 108 | |
| 109 | ret = mipi_dsi_attach(device); |
| 110 | if (ret < 0) { |
| 111 | printf("mipi_dsi_attach failed %d\n", ret); |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | ret = hx8394_init_sequence(dev); |
| 116 | if (ret) { |
| 117 | printf("hx8394_init_sequence failed %d\n", ret); |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | if (priv->backlight) { |
| 122 | ret = backlight_enable(priv->backlight); |
| 123 | if (ret) { |
| 124 | printf("backlight enabled failed %d\n", ret); |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | backlight_set_brightness(priv->backlight, 60); |
| 129 | } |
| 130 | |
| 131 | mdelay(10); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int hx8394_panel_get_display_timing(struct udevice *dev, |
| 137 | struct display_timing *timings) |
| 138 | { |
| 139 | memcpy(timings, &default_timing, sizeof(*timings)); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int hx8394_panel_of_to_plat(struct udevice *dev) |
| 145 | { |
| 146 | struct hx8394_panel_priv *priv = dev_get_priv(dev); |
| 147 | int ret; |
| 148 | |
| 149 | if (CONFIG_IS_ENABLED(DM_REGULATOR)) { |
| 150 | ret = device_get_supply_regulator(dev, "vcc-supply", |
| 151 | &priv->reg_vcc); |
| 152 | if (ret && ret != -ENOENT) { |
| 153 | dev_err(dev, "Warning: cannot get vcc supply\n"); |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | ret = device_get_supply_regulator(dev, "iovcc-supply", |
| 158 | &priv->reg_iovcc); |
| 159 | if (ret && ret != -ENOENT) { |
| 160 | dev_err(dev, "Warning: cannot get iovcc supply\n"); |
| 161 | return ret; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | ret = uclass_get_device_by_phandle(UCLASS_PANEL_BACKLIGHT, dev, |
| 166 | "backlight", &priv->backlight); |
| 167 | if (ret) |
| 168 | dev_warn(dev, "failed to get backlight\n"); |
| 169 | |
| 170 | ret = gpio_request_by_name(dev, "reset-gpios", 0, &priv->reset, |
| 171 | GPIOD_IS_OUT); |
| 172 | if (ret) { |
| 173 | dev_err(dev, "warning: cannot get reset GPIO (%d)\n", ret); |
| 174 | if (ret != -ENOENT) |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int hx8394_panel_probe(struct udevice *dev) |
| 182 | { |
| 183 | struct hx8394_panel_priv *priv = dev_get_priv(dev); |
| 184 | struct mipi_dsi_panel_plat *plat = dev_get_plat(dev); |
| 185 | int ret; |
| 186 | |
| 187 | dm_gpio_set_value(&priv->reset, true); |
| 188 | |
| 189 | if (CONFIG_IS_ENABLED(DM_REGULATOR)) { |
| 190 | dev_dbg(dev, "enable vcc '%s'\n", priv->reg_vcc->name); |
| 191 | ret = regulator_set_enable(priv->reg_vcc, true); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | |
| 195 | dev_dbg(dev, "enable iovcc '%s'\n", priv->reg_iovcc->name); |
| 196 | ret = regulator_set_enable(priv->reg_iovcc, true); |
| 197 | if (ret) { |
| 198 | regulator_set_enable(priv->reg_vcc, false); |
| 199 | return ret; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | mdelay(5); |
| 204 | dm_gpio_set_value(&priv->reset, false); |
| 205 | |
| 206 | mdelay(180); |
| 207 | |
| 208 | /* fill characteristics of DSI data link */ |
| 209 | plat->lanes = 4; |
| 210 | plat->format = MIPI_DSI_FMT_RGB888; |
| 211 | plat->mode_flags = MIPI_DSI_MODE_VIDEO | |
| 212 | MIPI_DSI_MODE_VIDEO_BURST; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static const struct panel_ops hx8394_panel_ops = { |
| 218 | .enable_backlight = hx8394_panel_enable_backlight, |
| 219 | .get_display_timing = hx8394_panel_get_display_timing, |
| 220 | }; |
| 221 | |
| 222 | static const struct udevice_id hx8394_panel_ids[] = { |
| 223 | { .compatible = "hannstar,hsd060bhw4" }, |
| 224 | { } |
| 225 | }; |
| 226 | |
| 227 | U_BOOT_DRIVER(hx8394_panel) = { |
| 228 | .name = "hx8394_panel", |
| 229 | .id = UCLASS_PANEL, |
| 230 | .of_match = hx8394_panel_ids, |
| 231 | .ops = &hx8394_panel_ops, |
| 232 | .of_to_plat = hx8394_panel_of_to_plat, |
| 233 | .probe = hx8394_panel_probe, |
| 234 | .plat_auto = sizeof(struct mipi_dsi_panel_plat), |
| 235 | .priv_auto = sizeof(struct hx8394_panel_priv), |
| 236 | }; |