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/drm_atomic_helper.h> 20 #include <drm/drm_drv.h> 21 #include <drm/drm_fb_helper.h> 22 #include <drm/drm_gem_cma_helper.h> 23 #include <drm/drm_gem_framebuffer_helper.h> 24 #include <drm/drm_mipi_dbi.h> 25 26 #define ST7735R_FRMCTR1 0xb1 27 #define ST7735R_FRMCTR2 0xb2 28 #define ST7735R_FRMCTR3 0xb3 29 #define ST7735R_INVCTR 0xb4 30 #define ST7735R_PWCTR1 0xc0 31 #define ST7735R_PWCTR2 0xc1 32 #define ST7735R_PWCTR3 0xc2 33 #define ST7735R_PWCTR4 0xc3 34 #define ST7735R_PWCTR5 0xc4 35 #define ST7735R_VMCTR1 0xc5 36 #define ST7735R_GAMCTRP1 0xe0 37 #define ST7735R_GAMCTRN1 0xe1 38 39 #define ST7735R_MY BIT(7) 40 #define ST7735R_MX BIT(6) 41 #define ST7735R_MV BIT(5) 42 #define ST7735R_RGB BIT(3) 43 44 struct st7735r_cfg { 45 const struct drm_display_mode mode; 46 unsigned int left_offset; 47 unsigned int top_offset; 48 unsigned int write_only:1; 49 unsigned int rgb:1; /* RGB (vs. BGR) */ 50 }; 51 52 struct st7735r_priv { 53 struct mipi_dbi_dev dbidev; /* Must be first for .release() */ 54 const struct st7735r_cfg *cfg; 55 }; 56 57 static void st7735r_pipe_enable(struct drm_simple_display_pipe *pipe, 58 struct drm_crtc_state *crtc_state, 59 struct drm_plane_state *plane_state) 60 { 61 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev); 62 struct st7735r_priv *priv = container_of(dbidev, struct st7735r_priv, 63 dbidev); 64 struct mipi_dbi *dbi = &dbidev->dbi; 65 int ret, idx; 66 u8 addr_mode; 67 68 if (!drm_dev_enter(pipe->crtc.dev, &idx)) 69 return; 70 71 DRM_DEBUG_KMS("\n"); 72 73 ret = mipi_dbi_poweron_reset(dbidev); 74 if (ret) 75 goto out_exit; 76 77 msleep(150); 78 79 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE); 80 msleep(500); 81 82 mipi_dbi_command(dbi, ST7735R_FRMCTR1, 0x01, 0x2c, 0x2d); 83 mipi_dbi_command(dbi, ST7735R_FRMCTR2, 0x01, 0x2c, 0x2d); 84 mipi_dbi_command(dbi, ST7735R_FRMCTR3, 0x01, 0x2c, 0x2d, 0x01, 0x2c, 85 0x2d); 86 mipi_dbi_command(dbi, ST7735R_INVCTR, 0x07); 87 mipi_dbi_command(dbi, ST7735R_PWCTR1, 0xa2, 0x02, 0x84); 88 mipi_dbi_command(dbi, ST7735R_PWCTR2, 0xc5); 89 mipi_dbi_command(dbi, ST7735R_PWCTR3, 0x0a, 0x00); 90 mipi_dbi_command(dbi, ST7735R_PWCTR4, 0x8a, 0x2a); 91 mipi_dbi_command(dbi, ST7735R_PWCTR5, 0x8a, 0xee); 92 mipi_dbi_command(dbi, ST7735R_VMCTR1, 0x0e); 93 mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE); 94 switch (dbidev->rotation) { 95 default: 96 addr_mode = ST7735R_MX | ST7735R_MY; 97 break; 98 case 90: 99 addr_mode = ST7735R_MX | ST7735R_MV; 100 break; 101 case 180: 102 addr_mode = 0; 103 break; 104 case 270: 105 addr_mode = ST7735R_MY | ST7735R_MV; 106 break; 107 } 108 109 if (priv->cfg->rgb) 110 addr_mode |= ST7735R_RGB; 111 112 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode); 113 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, 114 MIPI_DCS_PIXEL_FMT_16BIT); 115 mipi_dbi_command(dbi, ST7735R_GAMCTRP1, 0x02, 0x1c, 0x07, 0x12, 0x37, 116 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01, 117 0x03, 0x10); 118 mipi_dbi_command(dbi, ST7735R_GAMCTRN1, 0x03, 0x1d, 0x07, 0x06, 0x2e, 119 0x2c, 0x29, 0x2d, 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00, 120 0x02, 0x10); 121 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON); 122 123 msleep(100); 124 125 mipi_dbi_command(dbi, MIPI_DCS_ENTER_NORMAL_MODE); 126 127 msleep(20); 128 129 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state); 130 out_exit: 131 drm_dev_exit(idx); 132 } 133 134 static const struct drm_simple_display_pipe_funcs st7735r_pipe_funcs = { 135 .enable = st7735r_pipe_enable, 136 .disable = mipi_dbi_pipe_disable, 137 .update = mipi_dbi_pipe_update, 138 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb, 139 }; 140 141 static const struct st7735r_cfg jd_t18003_t01_cfg = { 142 .mode = { DRM_SIMPLE_MODE(128, 160, 28, 35) }, 143 /* Cannot read from Adafruit 1.8" display via SPI */ 144 .write_only = true, 145 }; 146 147 static const struct st7735r_cfg rh128128t_cfg = { 148 .mode = { DRM_SIMPLE_MODE(128, 128, 25, 26) }, 149 .left_offset = 2, 150 .top_offset = 3, 151 .rgb = true, 152 }; 153 154 DEFINE_DRM_GEM_CMA_FOPS(st7735r_fops); 155 156 static struct drm_driver st7735r_driver = { 157 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 158 .fops = &st7735r_fops, 159 .release = mipi_dbi_release, 160 DRM_GEM_CMA_VMAP_DRIVER_OPS, 161 .debugfs_init = mipi_dbi_debugfs_init, 162 .name = "st7735r", 163 .desc = "Sitronix ST7735R", 164 .date = "20171128", 165 .major = 1, 166 .minor = 0, 167 }; 168 169 static const struct of_device_id st7735r_of_match[] = { 170 { .compatible = "jianda,jd-t18003-t01", .data = &jd_t18003_t01_cfg }, 171 { .compatible = "okaya,rh128128t", .data = &rh128128t_cfg }, 172 { }, 173 }; 174 MODULE_DEVICE_TABLE(of, st7735r_of_match); 175 176 static const struct spi_device_id st7735r_id[] = { 177 { "jd-t18003-t01", (uintptr_t)&jd_t18003_t01_cfg }, 178 { }, 179 }; 180 MODULE_DEVICE_TABLE(spi, st7735r_id); 181 182 static int st7735r_probe(struct spi_device *spi) 183 { 184 struct device *dev = &spi->dev; 185 const struct st7735r_cfg *cfg; 186 struct mipi_dbi_dev *dbidev; 187 struct st7735r_priv *priv; 188 struct drm_device *drm; 189 struct mipi_dbi *dbi; 190 struct gpio_desc *dc; 191 u32 rotation = 0; 192 int ret; 193 194 cfg = device_get_match_data(&spi->dev); 195 if (!cfg) 196 cfg = (void *)spi_get_device_id(spi)->driver_data; 197 198 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 199 if (!priv) 200 return -ENOMEM; 201 202 dbidev = &priv->dbidev; 203 priv->cfg = cfg; 204 205 dbi = &dbidev->dbi; 206 drm = &dbidev->drm; 207 ret = devm_drm_dev_init(dev, drm, &st7735r_driver); 208 if (ret) { 209 kfree(dbidev); 210 return ret; 211 } 212 213 drm_mode_config_init(drm); 214 215 dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 216 if (IS_ERR(dbi->reset)) { 217 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n"); 218 return PTR_ERR(dbi->reset); 219 } 220 221 dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW); 222 if (IS_ERR(dc)) { 223 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n"); 224 return PTR_ERR(dc); 225 } 226 227 dbidev->backlight = devm_of_find_backlight(dev); 228 if (IS_ERR(dbidev->backlight)) 229 return PTR_ERR(dbidev->backlight); 230 231 device_property_read_u32(dev, "rotation", &rotation); 232 233 ret = mipi_dbi_spi_init(spi, dbi, dc); 234 if (ret) 235 return ret; 236 237 if (cfg->write_only) 238 dbi->read_commands = NULL; 239 240 dbidev->left_offset = cfg->left_offset; 241 dbidev->top_offset = cfg->top_offset; 242 243 ret = mipi_dbi_dev_init(dbidev, &st7735r_pipe_funcs, &cfg->mode, 244 rotation); 245 if (ret) 246 return ret; 247 248 drm_mode_config_reset(drm); 249 250 ret = drm_dev_register(drm, 0); 251 if (ret) 252 return ret; 253 254 spi_set_drvdata(spi, drm); 255 256 drm_fbdev_generic_setup(drm, 0); 257 258 return 0; 259 } 260 261 static int st7735r_remove(struct spi_device *spi) 262 { 263 struct drm_device *drm = spi_get_drvdata(spi); 264 265 drm_dev_unplug(drm); 266 drm_atomic_helper_shutdown(drm); 267 268 return 0; 269 } 270 271 static void st7735r_shutdown(struct spi_device *spi) 272 { 273 drm_atomic_helper_shutdown(spi_get_drvdata(spi)); 274 } 275 276 static struct spi_driver st7735r_spi_driver = { 277 .driver = { 278 .name = "st7735r", 279 .of_match_table = st7735r_of_match, 280 }, 281 .id_table = st7735r_id, 282 .probe = st7735r_probe, 283 .remove = st7735r_remove, 284 .shutdown = st7735r_shutdown, 285 }; 286 module_spi_driver(st7735r_spi_driver); 287 288 MODULE_DESCRIPTION("Sitronix ST7735R DRM driver"); 289 MODULE_AUTHOR("David Lechner <david@lechnology.com>"); 290 MODULE_LICENSE("GPL"); 291