1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 InforceComputing
4 * Copyright (C) 2016 Linaro Ltd
5 * Copyright (C) 2023 BayLibre, SAS
14 #include <linux/backlight.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/module.h>
19 #include <linux/regulator/consumer.h>
21 #include <video/mipi_display.h>
23 #include <drm/drm_mipi_dsi.h>
24 #include <drm/drm_modes.h>
25 #include <drm/drm_panel.h>
27 #define DSI_REG_MCAP 0xb0
28 #define DSI_REG_IS 0xb3 /* Interface Setting */
29 #define DSI_REG_IIS 0xb4 /* Interface ID Setting */
30 #define DSI_REG_CTRL 0xb6
38 const struct drm_display_mode *mode;
39 struct backlight_device *backlight;
40 struct drm_panel base;
41 struct gpio_desc *enable_gpio; /* Power IC supply enable */
42 struct gpio_desc *reset_gpio; /* External reset */
43 struct mipi_dsi_device *dsi;
44 struct regulator_bulk_data supplies[2];
47 static inline struct stk_panel *to_stk_panel(struct drm_panel *panel)
49 return container_of(panel, struct stk_panel, base);
52 static int stk_panel_init(struct stk_panel *stk)
54 struct mipi_dsi_device *dsi = stk->dsi;
55 struct mipi_dsi_multi_context dsi_ctx = {.dsi = dsi};
57 mipi_dsi_dcs_soft_reset_multi(&dsi_ctx);
58 mipi_dsi_msleep(&dsi_ctx, 5);
59 mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
60 mipi_dsi_msleep(&dsi_ctx, 120);
62 mipi_dsi_generic_write_seq_multi(&dsi_ctx, DSI_REG_MCAP, 0x04);
64 /* Interface setting, video mode */
65 mipi_dsi_generic_write_seq_multi(&dsi_ctx, DSI_REG_IS, 0x14, 0x08, 0x00, 0x22, 0x00);
66 mipi_dsi_generic_write_seq_multi(&dsi_ctx, DSI_REG_IIS, 0x0c, 0x00);
67 mipi_dsi_generic_write_seq_multi(&dsi_ctx, DSI_REG_CTRL, 0x3a, 0xd3);
69 mipi_dsi_dcs_set_display_brightness_multi(&dsi_ctx, 0x77);
71 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_CONTROL_DISPLAY,
72 MIPI_DCS_WRITE_MEMORY_START);
74 mipi_dsi_dcs_set_pixel_format_multi(&dsi_ctx, 0x77);
75 mipi_dsi_dcs_set_column_address_multi(&dsi_ctx, 0, stk->mode->hdisplay - 1);
76 mipi_dsi_dcs_set_page_address_multi(&dsi_ctx, 0, stk->mode->vdisplay - 1);
78 return dsi_ctx.accum_err;
81 static int stk_panel_on(struct stk_panel *stk)
83 struct mipi_dsi_device *dsi = stk->dsi;
84 struct mipi_dsi_multi_context dsi_ctx = {.dsi = dsi};
86 mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
88 mipi_dsi_msleep(&dsi_ctx, 20);
90 return dsi_ctx.accum_err;
93 static void stk_panel_off(struct stk_panel *stk)
95 struct mipi_dsi_device *dsi = stk->dsi;
96 struct mipi_dsi_multi_context dsi_ctx = {.dsi = dsi};
98 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
100 mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
101 mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
103 mipi_dsi_msleep(&dsi_ctx, 100);
106 static int stk_panel_unprepare(struct drm_panel *panel)
108 struct stk_panel *stk = to_stk_panel(panel);
111 regulator_bulk_disable(ARRAY_SIZE(stk->supplies), stk->supplies);
112 gpiod_set_value(stk->reset_gpio, 0);
113 gpiod_set_value(stk->enable_gpio, 1);
118 static int stk_panel_prepare(struct drm_panel *panel)
120 struct stk_panel *stk = to_stk_panel(panel);
123 gpiod_set_value(stk->reset_gpio, 0);
124 gpiod_set_value(stk->enable_gpio, 0);
125 ret = regulator_enable(stk->supplies[IOVCC].consumer);
130 ret = regulator_enable(stk->supplies[POWER].consumer);
135 gpiod_set_value(stk->enable_gpio, 1);
137 gpiod_set_value(stk->reset_gpio, 1);
139 ret = stk_panel_init(stk);
143 ret = stk_panel_on(stk);
150 regulator_disable(stk->supplies[POWER].consumer);
152 regulator_disable(stk->supplies[IOVCC].consumer);
153 gpiod_set_value(stk->reset_gpio, 0);
154 gpiod_set_value(stk->enable_gpio, 0);
159 static const struct drm_display_mode default_mode = {
162 .hsync_start = 1200 + 144,
163 .hsync_end = 1200 + 144 + 16,
164 .htotal = 1200 + 144 + 16 + 45,
166 .vsync_start = 1920 + 8,
167 .vsync_end = 1920 + 8 + 4,
168 .vtotal = 1920 + 8 + 4 + 4,
173 static int stk_panel_get_modes(struct drm_panel *panel,
174 struct drm_connector *connector)
176 struct drm_display_mode *mode;
178 mode = drm_mode_duplicate(connector->dev, &default_mode);
180 dev_err(panel->dev, "failed to add mode %ux%ux@%u\n",
181 default_mode.hdisplay, default_mode.vdisplay,
182 drm_mode_vrefresh(&default_mode));
186 drm_mode_set_name(mode);
187 drm_mode_probed_add(connector, mode);
188 connector->display_info.width_mm = default_mode.width_mm;
189 connector->display_info.height_mm = default_mode.height_mm;
193 static int dsi_dcs_bl_get_brightness(struct backlight_device *bl)
195 struct mipi_dsi_device *dsi = bl_get_data(bl);
199 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
200 ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
204 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
205 return brightness & 0xff;
208 static int dsi_dcs_bl_update_status(struct backlight_device *bl)
210 struct mipi_dsi_device *dsi = bl_get_data(bl);
211 struct mipi_dsi_multi_context dsi_ctx = {.dsi = dsi};
213 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
214 mipi_dsi_dcs_set_display_brightness_multi(&dsi_ctx, bl->props.brightness);
215 if (dsi_ctx.accum_err)
216 return dsi_ctx.accum_err;
218 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
219 return dsi_ctx.accum_err;
222 static const struct backlight_ops dsi_bl_ops = {
223 .update_status = dsi_dcs_bl_update_status,
224 .get_brightness = dsi_dcs_bl_get_brightness,
227 static struct backlight_device *
228 drm_panel_create_dsi_backlight(struct mipi_dsi_device *dsi)
230 struct device *dev = &dsi->dev;
231 struct backlight_properties props = {
232 .type = BACKLIGHT_RAW,
234 .max_brightness = 255,
237 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
238 &dsi_bl_ops, &props);
241 static const struct drm_panel_funcs stk_panel_funcs = {
242 .unprepare = stk_panel_unprepare,
243 .prepare = stk_panel_prepare,
244 .get_modes = stk_panel_get_modes,
247 static const struct of_device_id stk_of_match[] = {
248 { .compatible = "startek,kd070fhfid015", },
251 MODULE_DEVICE_TABLE(of, stk_of_match);
253 static int stk_panel_add(struct stk_panel *stk)
255 struct device *dev = &stk->dsi->dev;
258 stk->mode = &default_mode;
260 stk->supplies[IOVCC].supply = "iovcc";
261 stk->supplies[POWER].supply = "power";
262 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(stk->supplies), stk->supplies);
264 dev_err(dev, "regulator_bulk failed\n");
268 stk->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
269 if (IS_ERR(stk->reset_gpio)) {
270 ret = PTR_ERR(stk->reset_gpio);
271 dev_err(dev, "cannot get reset-gpios %d\n", ret);
275 stk->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
276 if (IS_ERR(stk->enable_gpio)) {
277 ret = PTR_ERR(stk->enable_gpio);
278 dev_err(dev, "cannot get enable-gpio %d\n", ret);
282 stk->backlight = drm_panel_create_dsi_backlight(stk->dsi);
283 if (IS_ERR(stk->backlight)) {
284 ret = PTR_ERR(stk->backlight);
285 dev_err(dev, "failed to register backlight %d\n", ret);
289 drm_panel_init(&stk->base, &stk->dsi->dev, &stk_panel_funcs,
290 DRM_MODE_CONNECTOR_DSI);
292 drm_panel_add(&stk->base);
297 static int stk_panel_probe(struct mipi_dsi_device *dsi)
299 struct stk_panel *stk;
303 dsi->format = MIPI_DSI_FMT_RGB888;
304 dsi->mode_flags = (MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_LPM);
306 stk = devm_kzalloc(&dsi->dev, sizeof(*stk), GFP_KERNEL);
310 mipi_dsi_set_drvdata(dsi, stk);
314 ret = stk_panel_add(stk);
318 ret = mipi_dsi_attach(dsi);
320 drm_panel_remove(&stk->base);
325 static void stk_panel_remove(struct mipi_dsi_device *dsi)
327 struct stk_panel *stk = mipi_dsi_get_drvdata(dsi);
330 err = mipi_dsi_detach(dsi);
332 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n",
335 drm_panel_remove(&stk->base);
338 static struct mipi_dsi_driver stk_panel_driver = {
340 .name = "panel-startek-kd070fhfid015",
341 .of_match_table = stk_of_match,
343 .probe = stk_panel_probe,
344 .remove = stk_panel_remove,
346 module_mipi_dsi_driver(stk_panel_driver);
349 MODULE_DESCRIPTION("STARTEK KD070FHFID015");
350 MODULE_LICENSE("GPL");