ili9486.c 7.0 KB

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