Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Novatek NT39016 TFT LCD panel driver |
| 4 | * |
| 5 | * Copyright (C) 2017, Maarten ter Huurne <maarten@treewalker.org> |
| 6 | * Copyright (C) 2019, Paul Cercueil <paul@crapouillou.net> |
| 7 | */ |
| 8 | |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 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> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/of_device.h> |
| 16 | #include <linux/regmap.h> |
| 17 | #include <linux/regulator/consumer.h> |
| 18 | #include <linux/spi/spi.h> |
| 19 | |
| 20 | #include <drm/drm_modes.h> |
| 21 | #include <drm/drm_panel.h> |
| 22 | |
| 23 | enum nt39016_regs { |
| 24 | NT39016_REG_SYSTEM, |
| 25 | NT39016_REG_TIMING, |
| 26 | NT39016_REG_OP, |
| 27 | NT39016_REG_DATA_IN, |
| 28 | NT39016_REG_SRC_TIMING_DELAY, |
| 29 | NT39016_REG_GATE_TIMING_DELAY, |
| 30 | NT39016_REG_RESERVED, |
| 31 | NT39016_REG_INITIAL_FUNC, |
| 32 | NT39016_REG_CONTRAST, |
| 33 | NT39016_REG_BRIGHTNESS, |
| 34 | NT39016_REG_HUE_SATURATION, |
| 35 | NT39016_REG_RB_SUBCONTRAST, |
| 36 | NT39016_REG_R_SUBBRIGHTNESS, |
| 37 | NT39016_REG_B_SUBBRIGHTNESS, |
| 38 | NT39016_REG_VCOMDC, |
| 39 | NT39016_REG_VCOMAC, |
| 40 | NT39016_REG_VGAM2, |
| 41 | NT39016_REG_VGAM34, |
| 42 | NT39016_REG_VGAM56, |
| 43 | NT39016_REG_VCOMDC_TRIM = 0x1e, |
| 44 | NT39016_REG_DISPLAY_MODE = 0x20, |
| 45 | }; |
| 46 | |
| 47 | #define NT39016_SYSTEM_RESET_N BIT(0) |
| 48 | #define NT39016_SYSTEM_STANDBY BIT(1) |
| 49 | |
| 50 | struct nt39016_panel_info { |
Paul Cercueil | fa36175 | 2020-04-08 11:58:29 +0200 | [diff] [blame] | 51 | const struct drm_display_mode *display_modes; |
| 52 | unsigned int num_modes; |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 53 | u16 width_mm, height_mm; |
| 54 | u32 bus_format, bus_flags; |
| 55 | }; |
| 56 | |
| 57 | struct nt39016 { |
| 58 | struct drm_panel drm_panel; |
| 59 | struct device *dev; |
| 60 | struct regmap *map; |
| 61 | struct regulator *supply; |
| 62 | const struct nt39016_panel_info *panel_info; |
| 63 | |
| 64 | struct gpio_desc *reset_gpio; |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static inline struct nt39016 *to_nt39016(struct drm_panel *panel) |
| 68 | { |
| 69 | return container_of(panel, struct nt39016, drm_panel); |
| 70 | } |
| 71 | |
| 72 | #define RV(REG, VAL) { .reg = (REG), .def = (VAL), .delay_us = 2 } |
| 73 | static const struct reg_sequence nt39016_panel_regs[] = { |
| 74 | RV(NT39016_REG_SYSTEM, 0x00), |
| 75 | RV(NT39016_REG_TIMING, 0x00), |
| 76 | RV(NT39016_REG_OP, 0x03), |
| 77 | RV(NT39016_REG_DATA_IN, 0xCC), |
| 78 | RV(NT39016_REG_SRC_TIMING_DELAY, 0x46), |
| 79 | RV(NT39016_REG_GATE_TIMING_DELAY, 0x05), |
| 80 | RV(NT39016_REG_RESERVED, 0x00), |
| 81 | RV(NT39016_REG_INITIAL_FUNC, 0x00), |
| 82 | RV(NT39016_REG_CONTRAST, 0x08), |
| 83 | RV(NT39016_REG_BRIGHTNESS, 0x40), |
| 84 | RV(NT39016_REG_HUE_SATURATION, 0x88), |
| 85 | RV(NT39016_REG_RB_SUBCONTRAST, 0x88), |
| 86 | RV(NT39016_REG_R_SUBBRIGHTNESS, 0x20), |
| 87 | RV(NT39016_REG_B_SUBBRIGHTNESS, 0x20), |
| 88 | RV(NT39016_REG_VCOMDC, 0x67), |
| 89 | RV(NT39016_REG_VCOMAC, 0xA4), |
| 90 | RV(NT39016_REG_VGAM2, 0x04), |
| 91 | RV(NT39016_REG_VGAM34, 0x24), |
| 92 | RV(NT39016_REG_VGAM56, 0x24), |
| 93 | RV(NT39016_REG_DISPLAY_MODE, 0x00), |
| 94 | }; |
| 95 | |
| 96 | #undef RV |
| 97 | |
| 98 | static const struct regmap_range nt39016_regmap_no_ranges[] = { |
| 99 | regmap_reg_range(0x13, 0x1D), |
| 100 | regmap_reg_range(0x1F, 0x1F), |
| 101 | }; |
| 102 | |
| 103 | static const struct regmap_access_table nt39016_regmap_access_table = { |
| 104 | .no_ranges = nt39016_regmap_no_ranges, |
| 105 | .n_no_ranges = ARRAY_SIZE(nt39016_regmap_no_ranges), |
| 106 | }; |
| 107 | |
| 108 | static const struct regmap_config nt39016_regmap_config = { |
| 109 | .reg_bits = 6, |
| 110 | .pad_bits = 2, |
| 111 | .val_bits = 8, |
| 112 | |
| 113 | .max_register = NT39016_REG_DISPLAY_MODE, |
| 114 | .wr_table = &nt39016_regmap_access_table, |
| 115 | .write_flag_mask = 0x02, |
| 116 | |
| 117 | .cache_type = REGCACHE_FLAT, |
| 118 | }; |
| 119 | |
| 120 | static int nt39016_prepare(struct drm_panel *drm_panel) |
| 121 | { |
| 122 | struct nt39016 *panel = to_nt39016(drm_panel); |
| 123 | int err; |
| 124 | |
| 125 | err = regulator_enable(panel->supply); |
| 126 | if (err) { |
| 127 | dev_err(panel->dev, "Failed to enable power supply: %d", err); |
| 128 | return err; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Reset the NT39016. |
| 133 | * The documentation says the reset pulse should be at least 40 us to |
| 134 | * pass the glitch filter, but when testing I see some resets fail and |
| 135 | * some succeed when using a 70 us delay, so we use 100 us instead. |
| 136 | */ |
| 137 | gpiod_set_value_cansleep(panel->reset_gpio, 1); |
| 138 | usleep_range(100, 1000); |
| 139 | gpiod_set_value_cansleep(panel->reset_gpio, 0); |
| 140 | udelay(2); |
| 141 | |
| 142 | /* Init all registers. */ |
| 143 | err = regmap_multi_reg_write(panel->map, nt39016_panel_regs, |
| 144 | ARRAY_SIZE(nt39016_panel_regs)); |
| 145 | if (err) { |
| 146 | dev_err(panel->dev, "Failed to init registers: %d", err); |
| 147 | goto err_disable_regulator; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | |
| 152 | err_disable_regulator: |
| 153 | regulator_disable(panel->supply); |
| 154 | return err; |
| 155 | } |
| 156 | |
| 157 | static int nt39016_unprepare(struct drm_panel *drm_panel) |
| 158 | { |
| 159 | struct nt39016 *panel = to_nt39016(drm_panel); |
| 160 | |
| 161 | gpiod_set_value_cansleep(panel->reset_gpio, 1); |
| 162 | |
| 163 | regulator_disable(panel->supply); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int nt39016_enable(struct drm_panel *drm_panel) |
| 169 | { |
| 170 | struct nt39016 *panel = to_nt39016(drm_panel); |
| 171 | int ret; |
| 172 | |
| 173 | ret = regmap_write(panel->map, NT39016_REG_SYSTEM, |
| 174 | NT39016_SYSTEM_RESET_N | NT39016_SYSTEM_STANDBY); |
| 175 | if (ret) { |
| 176 | dev_err(panel->dev, "Unable to enable panel: %d", ret); |
| 177 | return ret; |
| 178 | } |
| 179 | |
Paul Cercueil | 5b44f12 | 2020-08-11 02:22:36 +0200 | [diff] [blame^] | 180 | if (drm_panel->backlight) { |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 181 | /* Wait for the picture to be ready before enabling backlight */ |
| 182 | msleep(150); |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Paul Cercueil | 5b44f12 | 2020-08-11 02:22:36 +0200 | [diff] [blame^] | 185 | return 0; |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static int nt39016_disable(struct drm_panel *drm_panel) |
| 189 | { |
| 190 | struct nt39016 *panel = to_nt39016(drm_panel); |
| 191 | int err; |
| 192 | |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 193 | err = regmap_write(panel->map, NT39016_REG_SYSTEM, |
| 194 | NT39016_SYSTEM_RESET_N); |
| 195 | if (err) { |
| 196 | dev_err(panel->dev, "Unable to disable panel: %d", err); |
| 197 | return err; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Sam Ravnborg | 0ce8ddd | 2019-12-07 15:03:33 +0100 | [diff] [blame] | 203 | static int nt39016_get_modes(struct drm_panel *drm_panel, |
| 204 | struct drm_connector *connector) |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 205 | { |
| 206 | struct nt39016 *panel = to_nt39016(drm_panel); |
| 207 | const struct nt39016_panel_info *panel_info = panel->panel_info; |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 208 | struct drm_display_mode *mode; |
Paul Cercueil | fa36175 | 2020-04-08 11:58:29 +0200 | [diff] [blame] | 209 | unsigned int i; |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 210 | |
Paul Cercueil | fa36175 | 2020-04-08 11:58:29 +0200 | [diff] [blame] | 211 | for (i = 0; i < panel_info->num_modes; i++) { |
| 212 | mode = drm_mode_duplicate(connector->dev, |
| 213 | &panel_info->display_modes[i]); |
| 214 | if (!mode) |
| 215 | return -ENOMEM; |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 216 | |
Paul Cercueil | fa36175 | 2020-04-08 11:58:29 +0200 | [diff] [blame] | 217 | drm_mode_set_name(mode); |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 218 | |
Paul Cercueil | fa36175 | 2020-04-08 11:58:29 +0200 | [diff] [blame] | 219 | mode->type = DRM_MODE_TYPE_DRIVER; |
| 220 | if (panel_info->num_modes == 1) |
| 221 | mode->type |= DRM_MODE_TYPE_PREFERRED; |
| 222 | |
| 223 | drm_mode_probed_add(connector, mode); |
| 224 | } |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 225 | |
| 226 | connector->display_info.bpc = 8; |
| 227 | connector->display_info.width_mm = panel_info->width_mm; |
| 228 | connector->display_info.height_mm = panel_info->height_mm; |
| 229 | |
| 230 | drm_display_info_set_bus_formats(&connector->display_info, |
| 231 | &panel_info->bus_format, 1); |
| 232 | connector->display_info.bus_flags = panel_info->bus_flags; |
| 233 | |
Paul Cercueil | fa36175 | 2020-04-08 11:58:29 +0200 | [diff] [blame] | 234 | return panel_info->num_modes; |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static const struct drm_panel_funcs nt39016_funcs = { |
| 238 | .prepare = nt39016_prepare, |
| 239 | .unprepare = nt39016_unprepare, |
| 240 | .enable = nt39016_enable, |
| 241 | .disable = nt39016_disable, |
| 242 | .get_modes = nt39016_get_modes, |
| 243 | }; |
| 244 | |
| 245 | static int nt39016_probe(struct spi_device *spi) |
| 246 | { |
| 247 | struct device *dev = &spi->dev; |
| 248 | struct nt39016 *panel; |
| 249 | int err; |
| 250 | |
| 251 | panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL); |
| 252 | if (!panel) |
| 253 | return -ENOMEM; |
| 254 | |
| 255 | panel->dev = dev; |
| 256 | spi_set_drvdata(spi, panel); |
| 257 | |
| 258 | panel->panel_info = of_device_get_match_data(dev); |
| 259 | if (!panel->panel_info) |
| 260 | return -EINVAL; |
| 261 | |
| 262 | panel->supply = devm_regulator_get(dev, "power"); |
| 263 | if (IS_ERR(panel->supply)) { |
| 264 | dev_err(dev, "Failed to get power supply"); |
| 265 | return PTR_ERR(panel->supply); |
| 266 | } |
| 267 | |
| 268 | panel->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); |
| 269 | if (IS_ERR(panel->reset_gpio)) { |
| 270 | dev_err(dev, "Failed to get reset GPIO"); |
| 271 | return PTR_ERR(panel->reset_gpio); |
| 272 | } |
| 273 | |
| 274 | spi->bits_per_word = 8; |
| 275 | spi->mode = SPI_MODE_3 | SPI_3WIRE; |
| 276 | err = spi_setup(spi); |
| 277 | if (err) { |
| 278 | dev_err(dev, "Failed to setup SPI"); |
| 279 | return err; |
| 280 | } |
| 281 | |
| 282 | panel->map = devm_regmap_init_spi(spi, &nt39016_regmap_config); |
| 283 | if (IS_ERR(panel->map)) { |
| 284 | dev_err(dev, "Failed to init regmap"); |
| 285 | return PTR_ERR(panel->map); |
| 286 | } |
| 287 | |
Paul Cercueil | 5b44f12 | 2020-08-11 02:22:36 +0200 | [diff] [blame^] | 288 | err = drm_panel_of_backlight(&panel->drm_panel); |
| 289 | if (err) { |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 290 | if (err != -EPROBE_DEFER) |
| 291 | dev_err(dev, "Failed to get backlight handle"); |
| 292 | return err; |
| 293 | } |
| 294 | |
Laurent Pinchart | 9a2654c | 2019-09-04 16:28:03 +0300 | [diff] [blame] | 295 | drm_panel_init(&panel->drm_panel, dev, &nt39016_funcs, |
| 296 | DRM_MODE_CONNECTOR_DPI); |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 297 | |
Bernard Zhao | c3ee8c6 | 2020-08-01 20:02:13 +0800 | [diff] [blame] | 298 | drm_panel_add(&panel->drm_panel); |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int nt39016_remove(struct spi_device *spi) |
| 304 | { |
| 305 | struct nt39016 *panel = spi_get_drvdata(spi); |
| 306 | |
| 307 | drm_panel_remove(&panel->drm_panel); |
| 308 | |
| 309 | nt39016_disable(&panel->drm_panel); |
| 310 | nt39016_unprepare(&panel->drm_panel); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Paul Cercueil | fa36175 | 2020-04-08 11:58:29 +0200 | [diff] [blame] | 315 | static const struct drm_display_mode kd035g6_display_modes[] = { |
Paul Cercueil | 16b4511 | 2020-04-08 11:58:30 +0200 | [diff] [blame] | 316 | { /* 60 Hz */ |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 317 | .clock = 6000, |
| 318 | .hdisplay = 320, |
| 319 | .hsync_start = 320 + 10, |
| 320 | .hsync_end = 320 + 10 + 50, |
| 321 | .htotal = 320 + 10 + 50 + 20, |
| 322 | .vdisplay = 240, |
| 323 | .vsync_start = 240 + 5, |
| 324 | .vsync_end = 240 + 5 + 1, |
| 325 | .vtotal = 240 + 5 + 1 + 4, |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 326 | .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC, |
| 327 | }, |
Paul Cercueil | 16b4511 | 2020-04-08 11:58:30 +0200 | [diff] [blame] | 328 | { /* 50 Hz */ |
| 329 | .clock = 5400, |
| 330 | .hdisplay = 320, |
| 331 | .hsync_start = 320 + 42, |
| 332 | .hsync_end = 320 + 42 + 50, |
| 333 | .htotal = 320 + 42 + 50 + 20, |
| 334 | .vdisplay = 240, |
| 335 | .vsync_start = 240 + 5, |
| 336 | .vsync_end = 240 + 5 + 1, |
| 337 | .vtotal = 240 + 5 + 1 + 4, |
Paul Cercueil | 16b4511 | 2020-04-08 11:58:30 +0200 | [diff] [blame] | 338 | .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC, |
| 339 | }, |
Paul Cercueil | fa36175 | 2020-04-08 11:58:29 +0200 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | static const struct nt39016_panel_info kd035g6_info = { |
| 343 | .display_modes = kd035g6_display_modes, |
| 344 | .num_modes = ARRAY_SIZE(kd035g6_display_modes), |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 345 | .width_mm = 71, |
| 346 | .height_mm = 53, |
| 347 | .bus_format = MEDIA_BUS_FMT_RGB888_1X24, |
Sam Ravnborg | a4b1e1a | 2020-06-30 20:05:42 +0200 | [diff] [blame] | 348 | .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE, |
Paul Cercueil | ecdcbbb | 2019-06-03 17:25:55 +0200 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | static const struct of_device_id nt39016_of_match[] = { |
| 352 | { .compatible = "kingdisplay,kd035g6-54nt", .data = &kd035g6_info }, |
| 353 | { /* sentinel */ } |
| 354 | }; |
| 355 | MODULE_DEVICE_TABLE(of, nt39016_of_match); |
| 356 | |
| 357 | static struct spi_driver nt39016_driver = { |
| 358 | .driver = { |
| 359 | .name = "nt39016", |
| 360 | .of_match_table = nt39016_of_match, |
| 361 | }, |
| 362 | .probe = nt39016_probe, |
| 363 | .remove = nt39016_remove, |
| 364 | }; |
| 365 | |
| 366 | module_spi_driver(nt39016_driver); |
| 367 | |
| 368 | MODULE_AUTHOR("Maarten ter Huurne <maarten@treewalker.org>"); |
| 369 | MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>"); |
| 370 | MODULE_LICENSE("GPL v2"); |