1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * DRM driver for Ilitek ILI9225 panels 4 * 5 * Copyright 2017 David Lechner <david@lechnology.com> 6 * 7 * Some code copied from mipi-dbi.c 8 * Copyright 2016 Noralf Trønnes 9 */ 10 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_damage_helper.h> 21 #include <drm/drm_drv.h> 22 #include <drm/drm_fb_dma_helper.h> 23 #include <drm/drm_fb_helper.h> 24 #include <drm/drm_fourcc.h> 25 #include <drm/drm_framebuffer.h> 26 #include <drm/drm_gem_atomic_helper.h> 27 #include <drm/drm_gem_dma_helper.h> 28 #include <drm/drm_managed.h> 29 #include <drm/drm_mipi_dbi.h> 30 #include <drm/drm_rect.h> 31 32 #define ILI9225_DRIVER_READ_CODE 0x00 33 #define ILI9225_DRIVER_OUTPUT_CONTROL 0x01 34 #define ILI9225_LCD_AC_DRIVING_CONTROL 0x02 35 #define ILI9225_ENTRY_MODE 0x03 36 #define ILI9225_DISPLAY_CONTROL_1 0x07 37 #define ILI9225_BLANK_PERIOD_CONTROL_1 0x08 38 #define ILI9225_FRAME_CYCLE_CONTROL 0x0b 39 #define ILI9225_INTERFACE_CONTROL 0x0c 40 #define ILI9225_OSCILLATION_CONTROL 0x0f 41 #define ILI9225_POWER_CONTROL_1 0x10 42 #define ILI9225_POWER_CONTROL_2 0x11 43 #define ILI9225_POWER_CONTROL_3 0x12 44 #define ILI9225_POWER_CONTROL_4 0x13 45 #define ILI9225_POWER_CONTROL_5 0x14 46 #define ILI9225_VCI_RECYCLING 0x15 47 #define ILI9225_RAM_ADDRESS_SET_1 0x20 48 #define ILI9225_RAM_ADDRESS_SET_2 0x21 49 #define ILI9225_WRITE_DATA_TO_GRAM 0x22 50 #define ILI9225_SOFTWARE_RESET 0x28 51 #define ILI9225_GATE_SCAN_CONTROL 0x30 52 #define ILI9225_VERTICAL_SCROLL_1 0x31 53 #define ILI9225_VERTICAL_SCROLL_2 0x32 54 #define ILI9225_VERTICAL_SCROLL_3 0x33 55 #define ILI9225_PARTIAL_DRIVING_POS_1 0x34 56 #define ILI9225_PARTIAL_DRIVING_POS_2 0x35 57 #define ILI9225_HORIZ_WINDOW_ADDR_1 0x36 58 #define ILI9225_HORIZ_WINDOW_ADDR_2 0x37 59 #define ILI9225_VERT_WINDOW_ADDR_1 0x38 60 #define ILI9225_VERT_WINDOW_ADDR_2 0x39 61 #define ILI9225_GAMMA_CONTROL_1 0x50 62 #define ILI9225_GAMMA_CONTROL_2 0x51 63 #define ILI9225_GAMMA_CONTROL_3 0x52 64 #define ILI9225_GAMMA_CONTROL_4 0x53 65 #define ILI9225_GAMMA_CONTROL_5 0x54 66 #define ILI9225_GAMMA_CONTROL_6 0x55 67 #define ILI9225_GAMMA_CONTROL_7 0x56 68 #define ILI9225_GAMMA_CONTROL_8 0x57 69 #define ILI9225_GAMMA_CONTROL_9 0x58 70 #define ILI9225_GAMMA_CONTROL_10 0x59 71 72 static inline int ili9225_command(struct mipi_dbi *dbi, u8 cmd, u16 data) 73 { 74 u8 par[2] = { data >> 8, data & 0xff }; 75 76 return mipi_dbi_command_buf(dbi, cmd, par, 2); 77 } 78 79 static void ili9225_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect) 80 { 81 struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0); 82 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev); 83 unsigned int height = rect->y2 - rect->y1; 84 unsigned int width = rect->x2 - rect->x1; 85 struct mipi_dbi *dbi = &dbidev->dbi; 86 bool swap = dbi->swap_bytes; 87 u16 x_start, y_start; 88 u16 x1, x2, y1, y2; 89 int idx, ret = 0; 90 bool full; 91 void *tr; 92 93 if (!drm_dev_enter(fb->dev, &idx)) 94 return; 95 96 full = width == fb->width && height == fb->height; 97 98 DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect)); 99 100 if (!dbi->dc || !full || swap || 101 fb->format->format == DRM_FORMAT_XRGB8888) { 102 tr = dbidev->tx_buf; 103 ret = mipi_dbi_buf_copy(dbidev->tx_buf, fb, rect, swap); 104 if (ret) 105 goto err_msg; 106 } else { 107 tr = dma_obj->vaddr; 108 } 109 110 switch (dbidev->rotation) { 111 default: 112 x1 = rect->x1; 113 x2 = rect->x2 - 1; 114 y1 = rect->y1; 115 y2 = rect->y2 - 1; 116 x_start = x1; 117 y_start = y1; 118 break; 119 case 90: 120 x1 = rect->y1; 121 x2 = rect->y2 - 1; 122 y1 = fb->width - rect->x2; 123 y2 = fb->width - rect->x1 - 1; 124 x_start = x1; 125 y_start = y2; 126 break; 127 case 180: 128 x1 = fb->width - rect->x2; 129 x2 = fb->width - rect->x1 - 1; 130 y1 = fb->height - rect->y2; 131 y2 = fb->height - rect->y1 - 1; 132 x_start = x2; 133 y_start = y2; 134 break; 135 case 270: 136 x1 = fb->height - rect->y2; 137 x2 = fb->height - rect->y1 - 1; 138 y1 = rect->x1; 139 y2 = rect->x2 - 1; 140 x_start = x2; 141 y_start = y1; 142 break; 143 } 144 145 ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_1, x2); 146 ili9225_command(dbi, ILI9225_HORIZ_WINDOW_ADDR_2, x1); 147 ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_1, y2); 148 ili9225_command(dbi, ILI9225_VERT_WINDOW_ADDR_2, y1); 149 150 ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, x_start); 151 ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, y_start); 152 153 ret = mipi_dbi_command_buf(dbi, ILI9225_WRITE_DATA_TO_GRAM, tr, 154 width * height * 2); 155 err_msg: 156 if (ret) 157 dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret); 158 159 drm_dev_exit(idx); 160 } 161 162 static void ili9225_pipe_update(struct drm_simple_display_pipe *pipe, 163 struct drm_plane_state *old_state) 164 { 165 struct drm_plane_state *state = pipe->plane.state; 166 struct drm_rect rect; 167 168 if (!pipe->crtc.state->active) 169 return; 170 171 if (drm_atomic_helper_damage_merged(old_state, state, &rect)) 172 ili9225_fb_dirty(state->fb, &rect); 173 } 174 175 static void ili9225_pipe_enable(struct drm_simple_display_pipe *pipe, 176 struct drm_crtc_state *crtc_state, 177 struct drm_plane_state *plane_state) 178 { 179 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev); 180 struct drm_framebuffer *fb = plane_state->fb; 181 struct device *dev = pipe->crtc.dev->dev; 182 struct mipi_dbi *dbi = &dbidev->dbi; 183 struct drm_rect rect = { 184 .x1 = 0, 185 .x2 = fb->width, 186 .y1 = 0, 187 .y2 = fb->height, 188 }; 189 int ret, idx; 190 u8 am_id; 191 192 if (!drm_dev_enter(pipe->crtc.dev, &idx)) 193 return; 194 195 DRM_DEBUG_KMS("\n"); 196 197 mipi_dbi_hw_reset(dbi); 198 199 /* 200 * There don't seem to be two example init sequences that match, so 201 * using the one from the popular Arduino library for this display. 202 * https://github.com/Nkawu/TFT_22_ILI9225/blob/master/src/TFT_22_ILI9225.cpp 203 */ 204 205 ret = ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0000); 206 if (ret) { 207 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret); 208 goto out_exit; 209 } 210 ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0000); 211 ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x0000); 212 ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x0000); 213 ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x0000); 214 215 msleep(40); 216 217 ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0018); 218 ili9225_command(dbi, ILI9225_POWER_CONTROL_3, 0x6121); 219 ili9225_command(dbi, ILI9225_POWER_CONTROL_4, 0x006f); 220 ili9225_command(dbi, ILI9225_POWER_CONTROL_5, 0x495f); 221 ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0800); 222 223 msleep(10); 224 225 ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x103b); 226 227 msleep(50); 228 229 switch (dbidev->rotation) { 230 default: 231 am_id = 0x30; 232 break; 233 case 90: 234 am_id = 0x18; 235 break; 236 case 180: 237 am_id = 0x00; 238 break; 239 case 270: 240 am_id = 0x28; 241 break; 242 } 243 ili9225_command(dbi, ILI9225_DRIVER_OUTPUT_CONTROL, 0x011c); 244 ili9225_command(dbi, ILI9225_LCD_AC_DRIVING_CONTROL, 0x0100); 245 ili9225_command(dbi, ILI9225_ENTRY_MODE, 0x1000 | am_id); 246 ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000); 247 ili9225_command(dbi, ILI9225_BLANK_PERIOD_CONTROL_1, 0x0808); 248 ili9225_command(dbi, ILI9225_FRAME_CYCLE_CONTROL, 0x1100); 249 ili9225_command(dbi, ILI9225_INTERFACE_CONTROL, 0x0000); 250 ili9225_command(dbi, ILI9225_OSCILLATION_CONTROL, 0x0d01); 251 ili9225_command(dbi, ILI9225_VCI_RECYCLING, 0x0020); 252 ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_1, 0x0000); 253 ili9225_command(dbi, ILI9225_RAM_ADDRESS_SET_2, 0x0000); 254 255 ili9225_command(dbi, ILI9225_GATE_SCAN_CONTROL, 0x0000); 256 ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_1, 0x00db); 257 ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_2, 0x0000); 258 ili9225_command(dbi, ILI9225_VERTICAL_SCROLL_3, 0x0000); 259 ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_1, 0x00db); 260 ili9225_command(dbi, ILI9225_PARTIAL_DRIVING_POS_2, 0x0000); 261 262 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_1, 0x0000); 263 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_2, 0x0808); 264 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_3, 0x080a); 265 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_4, 0x000a); 266 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_5, 0x0a08); 267 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_6, 0x0808); 268 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_7, 0x0000); 269 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_8, 0x0a00); 270 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_9, 0x0710); 271 ili9225_command(dbi, ILI9225_GAMMA_CONTROL_10, 0x0710); 272 273 ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0012); 274 275 msleep(50); 276 277 ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x1017); 278 279 ili9225_fb_dirty(fb, &rect); 280 out_exit: 281 drm_dev_exit(idx); 282 } 283 284 static void ili9225_pipe_disable(struct drm_simple_display_pipe *pipe) 285 { 286 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev); 287 struct mipi_dbi *dbi = &dbidev->dbi; 288 289 DRM_DEBUG_KMS("\n"); 290 291 /* 292 * This callback is not protected by drm_dev_enter/exit since we want to 293 * turn off the display on regular driver unload. It's highly unlikely 294 * that the underlying SPI controller is gone should this be called after 295 * unplug. 296 */ 297 298 ili9225_command(dbi, ILI9225_DISPLAY_CONTROL_1, 0x0000); 299 msleep(50); 300 ili9225_command(dbi, ILI9225_POWER_CONTROL_2, 0x0007); 301 msleep(50); 302 ili9225_command(dbi, ILI9225_POWER_CONTROL_1, 0x0a02); 303 } 304 305 static int ili9225_dbi_command(struct mipi_dbi *dbi, u8 *cmd, u8 *par, 306 size_t num) 307 { 308 struct spi_device *spi = dbi->spi; 309 unsigned int bpw = 8; 310 u32 speed_hz; 311 int ret; 312 313 gpiod_set_value_cansleep(dbi->dc, 0); 314 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1); 315 ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1); 316 if (ret || !num) 317 return ret; 318 319 if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !dbi->swap_bytes) 320 bpw = 16; 321 322 gpiod_set_value_cansleep(dbi->dc, 1); 323 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num); 324 325 return mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num); 326 } 327 328 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = { 329 .enable = ili9225_pipe_enable, 330 .disable = ili9225_pipe_disable, 331 .update = ili9225_pipe_update, 332 }; 333 334 static const struct drm_display_mode ili9225_mode = { 335 DRM_SIMPLE_MODE(176, 220, 35, 44), 336 }; 337 338 DEFINE_DRM_GEM_DMA_FOPS(ili9225_fops); 339 340 static const struct drm_driver ili9225_driver = { 341 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 342 .fops = &ili9225_fops, 343 DRM_GEM_DMA_DRIVER_OPS_VMAP, 344 .name = "ili9225", 345 .desc = "Ilitek ILI9225", 346 .date = "20171106", 347 .major = 1, 348 .minor = 0, 349 }; 350 351 static const struct of_device_id ili9225_of_match[] = { 352 { .compatible = "vot,v220hf01a-t" }, 353 {}, 354 }; 355 MODULE_DEVICE_TABLE(of, ili9225_of_match); 356 357 static const struct spi_device_id ili9225_id[] = { 358 { "v220hf01a-t", 0 }, 359 { }, 360 }; 361 MODULE_DEVICE_TABLE(spi, ili9225_id); 362 363 static int ili9225_probe(struct spi_device *spi) 364 { 365 struct device *dev = &spi->dev; 366 struct mipi_dbi_dev *dbidev; 367 struct drm_device *drm; 368 struct mipi_dbi *dbi; 369 struct gpio_desc *rs; 370 u32 rotation = 0; 371 int ret; 372 373 dbidev = devm_drm_dev_alloc(dev, &ili9225_driver, 374 struct mipi_dbi_dev, drm); 375 if (IS_ERR(dbidev)) 376 return PTR_ERR(dbidev); 377 378 dbi = &dbidev->dbi; 379 drm = &dbidev->drm; 380 381 dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 382 if (IS_ERR(dbi->reset)) 383 return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n"); 384 385 rs = devm_gpiod_get(dev, "rs", GPIOD_OUT_LOW); 386 if (IS_ERR(rs)) 387 return dev_err_probe(dev, PTR_ERR(rs), "Failed to get GPIO 'rs'\n"); 388 389 device_property_read_u32(dev, "rotation", &rotation); 390 391 ret = mipi_dbi_spi_init(spi, dbi, rs); 392 if (ret) 393 return ret; 394 395 /* override the command function set in mipi_dbi_spi_init() */ 396 dbi->command = ili9225_dbi_command; 397 398 ret = mipi_dbi_dev_init(dbidev, &ili9225_pipe_funcs, &ili9225_mode, rotation); 399 if (ret) 400 return ret; 401 402 drm_mode_config_reset(drm); 403 404 ret = drm_dev_register(drm, 0); 405 if (ret) 406 return ret; 407 408 spi_set_drvdata(spi, drm); 409 410 drm_fbdev_generic_setup(drm, 0); 411 412 return 0; 413 } 414 415 static void ili9225_remove(struct spi_device *spi) 416 { 417 struct drm_device *drm = spi_get_drvdata(spi); 418 419 drm_dev_unplug(drm); 420 drm_atomic_helper_shutdown(drm); 421 } 422 423 static void ili9225_shutdown(struct spi_device *spi) 424 { 425 drm_atomic_helper_shutdown(spi_get_drvdata(spi)); 426 } 427 428 static struct spi_driver ili9225_spi_driver = { 429 .driver = { 430 .name = "ili9225", 431 .owner = THIS_MODULE, 432 .of_match_table = ili9225_of_match, 433 }, 434 .id_table = ili9225_id, 435 .probe = ili9225_probe, 436 .remove = ili9225_remove, 437 .shutdown = ili9225_shutdown, 438 }; 439 module_spi_driver(ili9225_spi_driver); 440 441 MODULE_DESCRIPTION("Ilitek ILI9225 DRM driver"); 442 MODULE_AUTHOR("David Lechner <david@lechnology.com>"); 443 MODULE_LICENSE("GPL"); 444