1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * DRM driver for display panels connected to a Sitronix ST7715R or ST7735R 4 * display controller in SPI mode. 5 * 6 * Copyright 2017 David Lechner <david@lechnology.com> 7 * Copyright (C) 2019 Glider bvba 8 */ 9 10 #include <linux/backlight.h> 11 #include <linux/delay.h> 12 #include <linux/dma-buf.h> 13 #include <linux/gpio/consumer.h> 14 #include <linux/module.h> 15 #include <linux/property.h> 16 #include <linux/spi/spi.h> 17 #include <video/mipi_display.h> 18 19 #include <drm/clients/drm_client_setup.h> 20 #include <drm/drm_atomic_helper.h> 21 #include <drm/drm_drv.h> 22 #include <drm/drm_fbdev_dma.h> 23 #include <drm/drm_gem_atomic_helper.h> 24 #include <drm/drm_gem_dma_helper.h> 25 #include <drm/drm_managed.h> 26 #include <drm/drm_mipi_dbi.h> 27 #include <drm/drm_print.h> 28 29 #define ST7735R_FRMCTR1 0xb1 30 #define ST7735R_FRMCTR2 0xb2 31 #define ST7735R_FRMCTR3 0xb3 32 #define ST7735R_INVCTR 0xb4 33 #define ST7735R_PWCTR1 0xc0 34 #define ST7735R_PWCTR2 0xc1 35 #define ST7735R_PWCTR3 0xc2 36 #define ST7735R_PWCTR4 0xc3 37 #define ST7735R_PWCTR5 0xc4 38 #define ST7735R_VMCTR1 0xc5 39 #define ST7735R_GAMCTRP1 0xe0 40 #define ST7735R_GAMCTRN1 0xe1 41 42 #define ST7735R_MY BIT(7) 43 #define ST7735R_MX BIT(6) 44 #define ST7735R_MV BIT(5) 45 #define ST7735R_RGB BIT(3) 46 47 struct st7735r_cfg { 48 const struct drm_display_mode mode; 49 unsigned int left_offset; 50 unsigned int top_offset; 51 unsigned int write_only:1; 52 unsigned int rgb:1; /* RGB (vs. BGR) */ 53 }; 54 55 struct st7735r_device { 56 struct mipi_dbi_dev dbidev; /* Must be first for .release() */ 57 const struct st7735r_cfg *cfg; 58 59 struct drm_plane plane; 60 struct drm_crtc crtc; 61 struct drm_encoder encoder; 62 struct drm_connector connector; 63 }; 64 65 static struct st7735r_device *to_st7735r_device(struct drm_device *drm) 66 { 67 return container_of(drm_to_mipi_dbi_dev(drm), struct st7735r_device, dbidev); 68 } 69 70 static const u32 st7735r_plane_formats[] = { 71 DRM_MIPI_DBI_PLANE_FORMATS, 72 }; 73 74 static const u64 st7735r_plane_format_modifiers[] = { 75 DRM_MIPI_DBI_PLANE_FORMAT_MODIFIERS, 76 }; 77 78 static const struct drm_plane_helper_funcs st7735r_plane_helper_funcs = { 79 DRM_MIPI_DBI_PLANE_HELPER_FUNCS, 80 }; 81 82 static const struct drm_plane_funcs st7735r_plane_funcs = { 83 DRM_MIPI_DBI_PLANE_FUNCS, 84 .destroy = drm_plane_cleanup, 85 }; 86 87 static void st7735r_crtc_helper_atomic_enable(struct drm_crtc *crtc, 88 struct drm_atomic_state *state) 89 { 90 struct drm_device *drm = crtc->dev; 91 struct st7735r_device *st7735r = to_st7735r_device(drm); 92 struct mipi_dbi_dev *dbidev = &st7735r->dbidev; 93 struct mipi_dbi *dbi = &dbidev->dbi; 94 int ret, idx; 95 u8 addr_mode; 96 97 if (!drm_dev_enter(drm, &idx)) 98 return; 99 100 DRM_DEBUG_KMS("\n"); 101 102 ret = mipi_dbi_poweron_reset(dbidev); 103 if (ret) 104 goto out_exit; 105 106 msleep(150); 107 108 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE); 109 msleep(500); 110 111 mipi_dbi_command(dbi, ST7735R_FRMCTR1, 0x01, 0x2c, 0x2d); 112 mipi_dbi_command(dbi, ST7735R_FRMCTR2, 0x01, 0x2c, 0x2d); 113 mipi_dbi_command(dbi, ST7735R_FRMCTR3, 0x01, 0x2c, 0x2d, 0x01, 0x2c, 114 0x2d); 115 mipi_dbi_command(dbi, ST7735R_INVCTR, 0x07); 116 mipi_dbi_command(dbi, ST7735R_PWCTR1, 0xa2, 0x02, 0x84); 117 mipi_dbi_command(dbi, ST7735R_PWCTR2, 0xc5); 118 mipi_dbi_command(dbi, ST7735R_PWCTR3, 0x0a, 0x00); 119 mipi_dbi_command(dbi, ST7735R_PWCTR4, 0x8a, 0x2a); 120 mipi_dbi_command(dbi, ST7735R_PWCTR5, 0x8a, 0xee); 121 mipi_dbi_command(dbi, ST7735R_VMCTR1, 0x0e); 122 mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE); 123 switch (dbidev->rotation) { 124 default: 125 addr_mode = ST7735R_MX | ST7735R_MY; 126 break; 127 case 90: 128 addr_mode = ST7735R_MX | ST7735R_MV; 129 break; 130 case 180: 131 addr_mode = 0; 132 break; 133 case 270: 134 addr_mode = ST7735R_MY | ST7735R_MV; 135 break; 136 } 137 138 if (st7735r->cfg->rgb) 139 addr_mode |= ST7735R_RGB; 140 141 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode); 142 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, 143 MIPI_DCS_PIXEL_FMT_16BIT); 144 mipi_dbi_command(dbi, ST7735R_GAMCTRP1, 0x02, 0x1c, 0x07, 0x12, 0x37, 145 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01, 146 0x03, 0x10); 147 mipi_dbi_command(dbi, ST7735R_GAMCTRN1, 0x03, 0x1d, 0x07, 0x06, 0x2e, 148 0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00, 149 0x02, 0x10); 150 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON); 151 152 msleep(100); 153 154 mipi_dbi_command(dbi, MIPI_DCS_ENTER_NORMAL_MODE); 155 156 msleep(20); 157 158 backlight_enable(dbidev->backlight); 159 out_exit: 160 drm_dev_exit(idx); 161 } 162 163 static const struct drm_crtc_helper_funcs st7735r_crtc_helper_funcs = { 164 DRM_MIPI_DBI_CRTC_HELPER_FUNCS, 165 .atomic_enable = st7735r_crtc_helper_atomic_enable, 166 }; 167 168 static const struct drm_crtc_funcs st7735r_crtc_funcs = { 169 DRM_MIPI_DBI_CRTC_FUNCS, 170 .destroy = drm_crtc_cleanup, 171 }; 172 173 static const struct drm_encoder_funcs st7735r_encoder_funcs = { 174 .destroy = drm_encoder_cleanup, 175 }; 176 177 static const struct drm_connector_helper_funcs st7735r_connector_helper_funcs = { 178 DRM_MIPI_DBI_CONNECTOR_HELPER_FUNCS, 179 }; 180 181 static const struct drm_connector_funcs st7735r_connector_funcs = { 182 DRM_MIPI_DBI_CONNECTOR_FUNCS, 183 .destroy = drm_connector_cleanup, 184 }; 185 186 static const struct drm_mode_config_helper_funcs st7735r_mode_config_helper_funcs = { 187 DRM_MIPI_DBI_MODE_CONFIG_HELPER_FUNCS, 188 }; 189 190 static const struct drm_mode_config_funcs st7735r_mode_config_funcs = { 191 DRM_MIPI_DBI_MODE_CONFIG_FUNCS, 192 }; 193 194 static const struct st7735r_cfg jd_t18003_t01_cfg = { 195 .mode = { DRM_SIMPLE_MODE(128, 160, 28, 35) }, 196 /* Cannot read from Adafruit 1.8" display via SPI */ 197 .write_only = true, 198 }; 199 200 static const struct st7735r_cfg rh128128t_cfg = { 201 .mode = { DRM_SIMPLE_MODE(128, 128, 25, 26) }, 202 .left_offset = 2, 203 .top_offset = 3, 204 .rgb = true, 205 }; 206 207 DEFINE_DRM_GEM_DMA_FOPS(st7735r_fops); 208 209 static const struct drm_driver st7735r_driver = { 210 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 211 .fops = &st7735r_fops, 212 DRM_GEM_DMA_DRIVER_OPS_VMAP, 213 DRM_FBDEV_DMA_DRIVER_OPS, 214 .debugfs_init = mipi_dbi_debugfs_init, 215 .name = "st7735r", 216 .desc = "Sitronix ST7735R", 217 .major = 1, 218 .minor = 0, 219 }; 220 221 static const struct of_device_id st7735r_of_match[] = { 222 { .compatible = "jianda,jd-t18003-t01", .data = &jd_t18003_t01_cfg }, 223 { .compatible = "okaya,rh128128t", .data = &rh128128t_cfg }, 224 { }, 225 }; 226 MODULE_DEVICE_TABLE(of, st7735r_of_match); 227 228 static const struct spi_device_id st7735r_id[] = { 229 { "jd-t18003-t01", (uintptr_t)&jd_t18003_t01_cfg }, 230 { "rh128128t", (uintptr_t)&rh128128t_cfg }, 231 { }, 232 }; 233 MODULE_DEVICE_TABLE(spi, st7735r_id); 234 235 static int st7735r_probe(struct spi_device *spi) 236 { 237 struct device *dev = &spi->dev; 238 const struct st7735r_cfg *cfg; 239 struct mipi_dbi_dev *dbidev; 240 struct st7735r_device *st7735r; 241 struct drm_device *drm; 242 struct mipi_dbi *dbi; 243 struct gpio_desc *dc; 244 struct drm_plane *plane; 245 struct drm_crtc *crtc; 246 struct drm_encoder *encoder; 247 struct drm_connector *connector; 248 u32 rotation = 0; 249 int ret; 250 251 cfg = device_get_match_data(&spi->dev); 252 if (!cfg) 253 cfg = (void *)spi_get_device_id(spi)->driver_data; 254 255 st7735r = devm_drm_dev_alloc(dev, &st7735r_driver, struct st7735r_device, dbidev.drm); 256 if (IS_ERR(st7735r)) 257 return PTR_ERR(st7735r); 258 259 dbidev = &st7735r->dbidev; 260 st7735r->cfg = cfg; 261 262 dbi = &dbidev->dbi; 263 drm = &dbidev->drm; 264 265 dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 266 if (IS_ERR(dbi->reset)) 267 return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n"); 268 269 dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW); 270 if (IS_ERR(dc)) 271 return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n"); 272 273 dbidev->backlight = devm_of_find_backlight(dev); 274 if (IS_ERR(dbidev->backlight)) 275 return PTR_ERR(dbidev->backlight); 276 277 device_property_read_u32(dev, "rotation", &rotation); 278 279 ret = mipi_dbi_spi_init(spi, dbi, dc); 280 if (ret) 281 return ret; 282 283 if (cfg->write_only) 284 dbi->read_commands = NULL; 285 286 dbidev->left_offset = cfg->left_offset; 287 dbidev->top_offset = cfg->top_offset; 288 289 ret = drm_mipi_dbi_dev_init(dbidev, &cfg->mode, st7735r_plane_formats[0], rotation, 0); 290 if (ret) 291 return ret; 292 293 ret = drmm_mode_config_init(drm); 294 if (ret) 295 return ret; 296 297 drm->mode_config.min_width = dbidev->mode.hdisplay; 298 drm->mode_config.max_width = dbidev->mode.hdisplay; 299 drm->mode_config.min_height = dbidev->mode.vdisplay; 300 drm->mode_config.max_height = dbidev->mode.vdisplay; 301 drm->mode_config.funcs = &st7735r_mode_config_funcs; 302 drm->mode_config.preferred_depth = 16; 303 drm->mode_config.helper_private = &st7735r_mode_config_helper_funcs; 304 305 plane = &st7735r->plane; 306 ret = drm_universal_plane_init(drm, plane, 0, &st7735r_plane_funcs, 307 st7735r_plane_formats, ARRAY_SIZE(st7735r_plane_formats), 308 st7735r_plane_format_modifiers, 309 DRM_PLANE_TYPE_PRIMARY, NULL); 310 if (ret) 311 return ret; 312 drm_plane_helper_add(plane, &st7735r_plane_helper_funcs); 313 drm_plane_enable_fb_damage_clips(plane); 314 315 crtc = &st7735r->crtc; 316 ret = drm_crtc_init_with_planes(drm, crtc, plane, NULL, &st7735r_crtc_funcs, NULL); 317 if (ret) 318 return ret; 319 drm_crtc_helper_add(crtc, &st7735r_crtc_helper_funcs); 320 321 encoder = &st7735r->encoder; 322 ret = drm_encoder_init(drm, encoder, &st7735r_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL); 323 if (ret) 324 return ret; 325 encoder->possible_crtcs = drm_crtc_mask(crtc); 326 327 connector = &st7735r->connector; 328 ret = drm_connector_init(drm, connector, &st7735r_connector_funcs, 329 DRM_MODE_CONNECTOR_SPI); 330 if (ret) 331 return ret; 332 drm_connector_helper_add(connector, &st7735r_connector_helper_funcs); 333 334 ret = drm_connector_attach_encoder(connector, encoder); 335 if (ret) 336 return ret; 337 338 drm_mode_config_reset(drm); 339 340 ret = drm_dev_register(drm, 0); 341 if (ret) 342 return ret; 343 344 spi_set_drvdata(spi, drm); 345 346 drm_client_setup(drm, NULL); 347 348 return 0; 349 } 350 351 static void st7735r_remove(struct spi_device *spi) 352 { 353 struct drm_device *drm = spi_get_drvdata(spi); 354 355 drm_dev_unplug(drm); 356 drm_atomic_helper_shutdown(drm); 357 } 358 359 static void st7735r_shutdown(struct spi_device *spi) 360 { 361 drm_atomic_helper_shutdown(spi_get_drvdata(spi)); 362 } 363 364 static struct spi_driver st7735r_spi_driver = { 365 .driver = { 366 .name = "st7735r", 367 .of_match_table = st7735r_of_match, 368 }, 369 .id_table = st7735r_id, 370 .probe = st7735r_probe, 371 .remove = st7735r_remove, 372 .shutdown = st7735r_shutdown, 373 }; 374 module_spi_driver(st7735r_spi_driver); 375 376 MODULE_DESCRIPTION("Sitronix ST7735R DRM driver"); 377 MODULE_AUTHOR("David Lechner <david@lechnology.com>"); 378 MODULE_LICENSE("GPL"); 379