1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * DRM driver for Ilitek ILI9486 panels 4 * 5 * Copyright 2020 Kamlesh Gurudasani <kamlesh.gurudasani@gmail.com> 6 */ 7 8#include <linux/backlight.h> 9#include <linux/delay.h> 10#include <linux/gpio/consumer.h> 11#include <linux/module.h> 12#include <linux/property.h> 13#include <linux/spi/spi.h> 14 15#include <video/mipi_display.h> 16 17#include <drm/drm_atomic_helper.h> 18#include <drm/drm_drv.h> 19#include <drm/drm_fb_helper.h> 20#include <drm/drm_gem_cma_helper.h> 21#include <drm/drm_gem_framebuffer_helper.h> 22#include <drm/drm_managed.h> 23#include <drm/drm_mipi_dbi.h> 24#include <drm/drm_modeset_helper.h> 25 26#define ILI9486_ITFCTR1 0xb0 27#define ILI9486_PWCTRL1 0xc2 28#define ILI9486_VMCTRL1 0xc5 29#define ILI9486_PGAMCTRL 0xe0 30#define ILI9486_NGAMCTRL 0xe1 31#define ILI9486_DGAMCTRL 0xe2 32#define ILI9486_MADCTL_BGR BIT(3) 33#define ILI9486_MADCTL_MV BIT(5) 34#define ILI9486_MADCTL_MX BIT(6) 35#define ILI9486_MADCTL_MY BIT(7) 36 37/* 38 * The PiScreen/waveshare rpi-lcd-35 has a SPI to 16-bit parallel bus converter 39 * in front of the display controller. This means that 8-bit values have to be 40 * transferred as 16-bit. 41 */ 42static int waveshare_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par, 43 size_t num) 44{ 45 struct spi_device *spi = mipi->spi; 46 unsigned int bpw = 8; 47 void *data = par; 48 u32 speed_hz; 49 int i, ret; 50 __be16 *buf; 51 52 buf = kmalloc(32 * sizeof(u16), GFP_KERNEL); 53 if (!buf) 54 return -ENOMEM; 55 56 /* 57 * The displays are Raspberry Pi HATs and connected to the 8-bit only 58 * SPI controller, so 16-bit command and parameters need byte swapping 59 * before being transferred as 8-bit on the big endian SPI bus. 60 */ 61 buf[0] = cpu_to_be16(*cmd); 62 gpiod_set_value_cansleep(mipi->dc, 0); 63 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 2); 64 ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, buf, 2); 65 if (ret || !num) 66 goto free; 67 68 /* 8-bit configuration data, not 16-bit pixel data */ 69 if (num <= 32) { 70 for (i = 0; i < num; i++) 71 buf[i] = cpu_to_be16(par[i]); 72 num *= 2; 73 data = buf; 74 } 75 76 /* 77 * Check whether pixel data bytes needs to be swapped or not 78 */ 79 if (*cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes) 80 bpw = 16; 81 82 gpiod_set_value_cansleep(mipi->dc, 1); 83 speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num); 84 ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, data, num); 85 free: 86 kfree(buf); 87 88 return ret; 89} 90 91static void waveshare_enable(struct drm_simple_display_pipe *pipe, 92 struct drm_crtc_state *crtc_state, 93 struct drm_plane_state *plane_state) 94{ 95 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev); 96 struct mipi_dbi *dbi = &dbidev->dbi; 97 u8 addr_mode; 98 int ret, idx; 99 100 if (!drm_dev_enter(pipe->crtc.dev, &idx)) 101 return; 102 103 DRM_DEBUG_KMS("\n"); 104 105 ret = mipi_dbi_poweron_conditional_reset(dbidev); 106 if (ret < 0) 107 goto out_exit; 108 if (ret == 1) 109 goto out_enable; 110 111 mipi_dbi_command(dbi, ILI9486_ITFCTR1); 112 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE); 113 msleep(250); 114 115 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55); 116 117 mipi_dbi_command(dbi, ILI9486_PWCTRL1, 0x44); 118 119 mipi_dbi_command(dbi, ILI9486_VMCTRL1, 0x00, 0x00, 0x00, 0x00); 120 121 mipi_dbi_command(dbi, ILI9486_PGAMCTRL, 122 0x0F, 0x1F, 0x1C, 0x0C, 0x0F, 0x08, 0x48, 0x98, 123 0x37, 0x0A, 0x13, 0x04, 0x11, 0x0D, 0x0); 124 mipi_dbi_command(dbi, ILI9486_NGAMCTRL, 125 0x0F, 0x32, 0x2E, 0x0B, 0x0D, 0x05, 0x47, 0x75, 126 0x37, 0x06, 0x10, 0x03, 0x24, 0x20, 0x00); 127 mipi_dbi_command(dbi, ILI9486_DGAMCTRL, 128 0x0F, 0x32, 0x2E, 0x0B, 0x0D, 0x05, 0x47, 0x75, 129 0x37, 0x06, 0x10, 0x03, 0x24, 0x20, 0x00); 130 131 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON); 132 msleep(100); 133 134 out_enable: 135 switch (dbidev->rotation) { 136 case 90: 137 addr_mode = ILI9486_MADCTL_MY; 138 break; 139 case 180: 140 addr_mode = ILI9486_MADCTL_MV; 141 break; 142 case 270: 143 addr_mode = ILI9486_MADCTL_MX; 144 break; 145 default: 146 addr_mode = ILI9486_MADCTL_MV | ILI9486_MADCTL_MY | 147 ILI9486_MADCTL_MX; 148 break; 149 } 150 addr_mode |= ILI9486_MADCTL_BGR; 151 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode); 152 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state); 153 out_exit: 154 drm_dev_exit(idx); 155} 156 157static const struct drm_simple_display_pipe_funcs waveshare_pipe_funcs = { 158 .enable = waveshare_enable, 159 .disable = mipi_dbi_pipe_disable, 160 .update = mipi_dbi_pipe_update, 161 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb, 162}; 163 164static const struct drm_display_mode waveshare_mode = { 165 DRM_SIMPLE_MODE(480, 320, 73, 49), 166}; 167 168DEFINE_DRM_GEM_CMA_FOPS(ili9486_fops); 169 170static struct drm_driver ili9486_driver = { 171 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 172 .fops = &ili9486_fops, 173 DRM_GEM_CMA_DRIVER_OPS_VMAP, 174 .debugfs_init = mipi_dbi_debugfs_init, 175 .name = "ili9486", 176 .desc = "Ilitek ILI9486", 177 .date = "20200118", 178 .major = 1, 179 .minor = 0, 180}; 181 182static const struct of_device_id ili9486_of_match[] = { 183 { .compatible = "waveshare,rpi-lcd-35" }, 184 { .compatible = "ozzmaker,piscreen" }, 185 {}, 186}; 187MODULE_DEVICE_TABLE(of, ili9486_of_match); 188 189static const struct spi_device_id ili9486_id[] = { 190 { "ili9486", 0 }, 191 { } 192}; 193MODULE_DEVICE_TABLE(spi, ili9486_id); 194 195static int ili9486_probe(struct spi_device *spi) 196{ 197 struct device *dev = &spi->dev; 198 struct mipi_dbi_dev *dbidev; 199 struct drm_device *drm; 200 struct mipi_dbi *dbi; 201 struct gpio_desc *dc; 202 u32 rotation = 0; 203 int ret; 204 205 dbidev = devm_drm_dev_alloc(dev, &ili9486_driver, 206 struct mipi_dbi_dev, drm); 207 if (IS_ERR(dbidev)) 208 return PTR_ERR(dbidev); 209 210 dbi = &dbidev->dbi; 211 drm = &dbidev->drm; 212 213 dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 214 if (IS_ERR(dbi->reset)) { 215 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n"); 216 return PTR_ERR(dbi->reset); 217 } 218 219 dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW); 220 if (IS_ERR(dc)) { 221 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n"); 222 return PTR_ERR(dc); 223 } 224 225 dbidev->backlight = devm_of_find_backlight(dev); 226 if (IS_ERR(dbidev->backlight)) 227 return PTR_ERR(dbidev->backlight); 228 229 device_property_read_u32(dev, "rotation", &rotation); 230 231 ret = mipi_dbi_spi_init(spi, dbi, dc); 232 if (ret) 233 return ret; 234 235 dbi->command = waveshare_command; 236 dbi->read_commands = NULL; 237 238 ret = mipi_dbi_dev_init(dbidev, &waveshare_pipe_funcs, 239 &waveshare_mode, rotation); 240 if (ret) 241 return ret; 242 243 drm_mode_config_reset(drm); 244 245 ret = drm_dev_register(drm, 0); 246 if (ret) 247 return ret; 248 249 spi_set_drvdata(spi, drm); 250 251 drm_fbdev_generic_setup(drm, 0); 252 253 return 0; 254} 255 256static int ili9486_remove(struct spi_device *spi) 257{ 258 struct drm_device *drm = spi_get_drvdata(spi); 259 260 drm_dev_unplug(drm); 261 drm_atomic_helper_shutdown(drm); 262 263 return 0; 264} 265 266static void ili9486_shutdown(struct spi_device *spi) 267{ 268 drm_atomic_helper_shutdown(spi_get_drvdata(spi)); 269} 270 271static struct spi_driver ili9486_spi_driver = { 272 .driver = { 273 .name = "ili9486", 274 .of_match_table = ili9486_of_match, 275 }, 276 .id_table = ili9486_id, 277 .probe = ili9486_probe, 278 .remove = ili9486_remove, 279 .shutdown = ili9486_shutdown, 280}; 281module_spi_driver(ili9486_spi_driver); 282 283MODULE_DESCRIPTION("Ilitek ILI9486 DRM driver"); 284MODULE_AUTHOR("Kamlesh Gurudasani <kamlesh.gurudasani@gmail.com>"); 285MODULE_LICENSE("GPL"); 286