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