1 // SPDX-License-Identifier: GPL-2.0
3 * NewVision NV3052C IPS LCD panel driver
5 * Copyright (C) 2020, Paul Cercueil <paul@crapouillou.net>
6 * Copyright (C) 2022, Christophe Branchereau <cbranchereau@gmail.com>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/media-bus-format.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/spi/spi.h>
18 #include <video/mipi_display.h>
19 #include <drm/drm_mipi_dbi.h>
20 #include <drm/drm_modes.h>
21 #include <drm/drm_panel.h>
23 struct nv3052c_panel_info {
24 const struct drm_display_mode *display_modes;
25 unsigned int num_modes;
26 u16 width_mm, height_mm;
27 u32 bus_format, bus_flags;
32 struct drm_panel panel;
34 const struct nv3052c_panel_info *panel_info;
35 struct regulator *supply;
36 struct gpio_desc *reset_gpio;
44 static const struct nv3052c_reg nv3052c_panel_regs[] = {
236 static inline struct nv3052c *to_nv3052c(struct drm_panel *panel)
238 return container_of(panel, struct nv3052c, panel);
241 static int nv3052c_prepare(struct drm_panel *panel)
243 struct nv3052c *priv = to_nv3052c(panel);
244 struct mipi_dbi *dbi = &priv->dbi;
248 err = regulator_enable(priv->supply);
250 dev_err(priv->dev, "Failed to enable power supply: %d\n", err);
255 gpiod_set_value_cansleep(priv->reset_gpio, 1);
256 usleep_range(10, 1000);
257 gpiod_set_value_cansleep(priv->reset_gpio, 0);
258 usleep_range(5000, 20000);
260 for (i = 0; i < ARRAY_SIZE(nv3052c_panel_regs); i++) {
261 err = mipi_dbi_command(dbi, nv3052c_panel_regs[i].cmd,
262 nv3052c_panel_regs[i].val);
265 dev_err(priv->dev, "Unable to set register: %d\n", err);
266 goto err_disable_regulator;
270 err = mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
272 dev_err(priv->dev, "Unable to exit sleep mode: %d\n", err);
273 goto err_disable_regulator;
278 err_disable_regulator:
279 regulator_disable(priv->supply);
283 static int nv3052c_unprepare(struct drm_panel *panel)
285 struct nv3052c *priv = to_nv3052c(panel);
286 struct mipi_dbi *dbi = &priv->dbi;
289 err = mipi_dbi_command(dbi, MIPI_DCS_ENTER_SLEEP_MODE);
291 dev_err(priv->dev, "Unable to enter sleep mode: %d\n", err);
293 gpiod_set_value_cansleep(priv->reset_gpio, 1);
294 regulator_disable(priv->supply);
299 static int nv3052c_enable(struct drm_panel *panel)
301 struct nv3052c *priv = to_nv3052c(panel);
302 struct mipi_dbi *dbi = &priv->dbi;
305 err = mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
307 dev_err(priv->dev, "Unable to enable display: %d\n", err);
311 if (panel->backlight) {
312 /* Wait for the picture to be ready before enabling backlight */
319 static int nv3052c_disable(struct drm_panel *panel)
321 struct nv3052c *priv = to_nv3052c(panel);
322 struct mipi_dbi *dbi = &priv->dbi;
325 err = mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
327 dev_err(priv->dev, "Unable to disable display: %d\n", err);
334 static int nv3052c_get_modes(struct drm_panel *panel,
335 struct drm_connector *connector)
337 struct nv3052c *priv = to_nv3052c(panel);
338 const struct nv3052c_panel_info *panel_info = priv->panel_info;
339 struct drm_display_mode *mode;
342 for (i = 0; i < panel_info->num_modes; i++) {
343 mode = drm_mode_duplicate(connector->dev,
344 &panel_info->display_modes[i]);
348 drm_mode_set_name(mode);
350 mode->type = DRM_MODE_TYPE_DRIVER;
351 if (panel_info->num_modes == 1)
352 mode->type |= DRM_MODE_TYPE_PREFERRED;
354 drm_mode_probed_add(connector, mode);
357 connector->display_info.bpc = 8;
358 connector->display_info.width_mm = panel_info->width_mm;
359 connector->display_info.height_mm = panel_info->height_mm;
361 drm_display_info_set_bus_formats(&connector->display_info,
362 &panel_info->bus_format, 1);
363 connector->display_info.bus_flags = panel_info->bus_flags;
365 return panel_info->num_modes;
368 static const struct drm_panel_funcs nv3052c_funcs = {
369 .prepare = nv3052c_prepare,
370 .unprepare = nv3052c_unprepare,
371 .enable = nv3052c_enable,
372 .disable = nv3052c_disable,
373 .get_modes = nv3052c_get_modes,
376 static int nv3052c_probe(struct spi_device *spi)
378 struct device *dev = &spi->dev;
379 struct nv3052c *priv;
382 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
388 priv->panel_info = of_device_get_match_data(dev);
389 if (!priv->panel_info)
392 priv->supply = devm_regulator_get(dev, "power");
393 if (IS_ERR(priv->supply))
394 return dev_err_probe(dev, PTR_ERR(priv->supply), "Failed to get power supply\n");
396 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
397 if (IS_ERR(priv->reset_gpio))
398 return dev_err_probe(dev, PTR_ERR(priv->reset_gpio), "Failed to get reset GPIO\n");
400 err = mipi_dbi_spi_init(spi, &priv->dbi, NULL);
402 return dev_err_probe(dev, err, "MIPI DBI init failed\n");
404 priv->dbi.read_commands = NULL;
406 spi_set_drvdata(spi, priv);
408 drm_panel_init(&priv->panel, dev, &nv3052c_funcs,
409 DRM_MODE_CONNECTOR_DPI);
411 err = drm_panel_of_backlight(&priv->panel);
413 return dev_err_probe(dev, err, "Failed to attach backlight\n");
415 drm_panel_add(&priv->panel);
420 static void nv3052c_remove(struct spi_device *spi)
422 struct nv3052c *priv = spi_get_drvdata(spi);
424 drm_panel_remove(&priv->panel);
425 drm_panel_disable(&priv->panel);
426 drm_panel_unprepare(&priv->panel);
429 static const struct drm_display_mode ltk035c5444t_modes[] = {
433 .hsync_start = 640 + 96,
434 .hsync_end = 640 + 96 + 16,
435 .htotal = 640 + 96 + 16 + 48,
437 .vsync_start = 480 + 5,
438 .vsync_end = 480 + 5 + 2,
439 .vtotal = 480 + 5 + 2 + 13,
440 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
445 .hsync_start = 640 + 39,
446 .hsync_end = 640 + 39 + 2,
447 .htotal = 640 + 39 + 2 + 39,
449 .vsync_start = 480 + 5,
450 .vsync_end = 480 + 5 + 2,
451 .vtotal = 480 + 5 + 2 + 13,
452 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
456 static const struct nv3052c_panel_info ltk035c5444t_panel_info = {
457 .display_modes = ltk035c5444t_modes,
458 .num_modes = ARRAY_SIZE(ltk035c5444t_modes),
461 .bus_format = MEDIA_BUS_FMT_RGB888_1X24,
462 .bus_flags = DRM_BUS_FLAG_DE_HIGH | DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE,
465 static const struct of_device_id nv3052c_of_match[] = {
466 { .compatible = "leadtek,ltk035c5444t", .data = <k035c5444t_panel_info },
469 MODULE_DEVICE_TABLE(of, nv3052c_of_match);
471 static struct spi_driver nv3052c_driver = {
474 .of_match_table = nv3052c_of_match,
476 .probe = nv3052c_probe,
477 .remove = nv3052c_remove,
479 module_spi_driver(nv3052c_driver);
481 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
482 MODULE_AUTHOR("Christophe Branchereau <cbranchereau@gmail.com>");
483 MODULE_LICENSE("GPL v2");