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