st7586.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DRM driver for Sitronix ST7586 panels
  4. *
  5. * Copyright 2017 David Lechner <david@lechnology.com>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/module.h>
  10. #include <linux/property.h>
  11. #include <linux/spi/spi.h>
  12. #include <video/mipi_display.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_damage_helper.h>
  15. #include <drm/drm_drv.h>
  16. #include <drm/drm_fb_dma_helper.h>
  17. #include <drm/drm_fbdev_dma.h>
  18. #include <drm/drm_format_helper.h>
  19. #include <drm/drm_framebuffer.h>
  20. #include <drm/drm_gem_atomic_helper.h>
  21. #include <drm/drm_gem_dma_helper.h>
  22. #include <drm/drm_gem_framebuffer_helper.h>
  23. #include <drm/drm_managed.h>
  24. #include <drm/drm_mipi_dbi.h>
  25. #include <drm/drm_rect.h>
  26. /* controller-specific commands */
  27. #define ST7586_DISP_MODE_GRAY 0x38
  28. #define ST7586_DISP_MODE_MONO 0x39
  29. #define ST7586_ENABLE_DDRAM 0x3a
  30. #define ST7586_SET_DISP_DUTY 0xb0
  31. #define ST7586_SET_PART_DISP 0xb4
  32. #define ST7586_SET_NLINE_INV 0xb5
  33. #define ST7586_SET_VOP 0xc0
  34. #define ST7586_SET_BIAS_SYSTEM 0xc3
  35. #define ST7586_SET_BOOST_LEVEL 0xc4
  36. #define ST7586_SET_VOP_OFFSET 0xc7
  37. #define ST7586_ENABLE_ANALOG 0xd0
  38. #define ST7586_AUTO_READ_CTRL 0xd7
  39. #define ST7586_OTP_RW_CTRL 0xe0
  40. #define ST7586_OTP_CTRL_OUT 0xe1
  41. #define ST7586_OTP_READ 0xe3
  42. #define ST7586_DISP_CTRL_MX BIT(6)
  43. #define ST7586_DISP_CTRL_MY BIT(7)
  44. /*
  45. * The ST7586 controller has an unusual pixel format where 2bpp grayscale is
  46. * packed 3 pixels per byte with the first two pixels using 3 bits and the 3rd
  47. * pixel using only 2 bits.
  48. *
  49. * | D7 | D6 | D5 || | || 2bpp |
  50. * | (D4) | (D3) | (D2) || D1 | D0 || GRAY |
  51. * +------+------+------++------+------++------+
  52. * | 1 | 1 | 1 || 1 | 1 || 0 0 | black
  53. * | 1 | 0 | 0 || 1 | 0 || 0 1 | dark gray
  54. * | 0 | 1 | 0 || 0 | 1 || 1 0 | light gray
  55. * | 0 | 0 | 0 || 0 | 0 || 1 1 | white
  56. */
  57. static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 };
  58. static void st7586_xrgb8888_to_gray332(u8 *dst, void *vaddr,
  59. struct drm_framebuffer *fb,
  60. struct drm_rect *clip,
  61. struct drm_format_conv_state *fmtcnv_state)
  62. {
  63. size_t len = (clip->x2 - clip->x1) * (clip->y2 - clip->y1);
  64. unsigned int x, y;
  65. u8 *src, *buf, val;
  66. struct iosys_map dst_map, vmap;
  67. buf = kmalloc(len, GFP_KERNEL);
  68. if (!buf)
  69. return;
  70. iosys_map_set_vaddr(&dst_map, buf);
  71. iosys_map_set_vaddr(&vmap, vaddr);
  72. drm_fb_xrgb8888_to_gray8(&dst_map, NULL, &vmap, fb, clip, fmtcnv_state);
  73. src = buf;
  74. for (y = clip->y1; y < clip->y2; y++) {
  75. for (x = clip->x1; x < clip->x2; x += 3) {
  76. val = st7586_lookup[*src++ >> 6] << 5;
  77. val |= st7586_lookup[*src++ >> 6] << 2;
  78. val |= st7586_lookup[*src++ >> 6] >> 1;
  79. *dst++ = val;
  80. }
  81. }
  82. kfree(buf);
  83. }
  84. static int st7586_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb,
  85. struct drm_rect *clip, struct drm_format_conv_state *fmtcnv_state)
  86. {
  87. int ret;
  88. ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
  89. if (ret)
  90. return ret;
  91. st7586_xrgb8888_to_gray332(dst, src->vaddr, fb, clip, fmtcnv_state);
  92. drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
  93. return 0;
  94. }
  95. static void st7586_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb,
  96. struct drm_rect *rect, struct drm_format_conv_state *fmtcnv_state)
  97. {
  98. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev);
  99. struct mipi_dbi *dbi = &dbidev->dbi;
  100. int start, end, ret = 0;
  101. /* 3 pixels per byte, so grow clip to nearest multiple of 3 */
  102. rect->x1 = rounddown(rect->x1, 3);
  103. rect->x2 = roundup(rect->x2, 3);
  104. DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect));
  105. ret = st7586_buf_copy(dbidev->tx_buf, src, fb, rect, fmtcnv_state);
  106. if (ret)
  107. goto err_msg;
  108. /* Pixels are packed 3 per byte */
  109. start = rect->x1 / 3;
  110. end = rect->x2 / 3;
  111. mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS,
  112. (start >> 8) & 0xFF, start & 0xFF,
  113. (end >> 8) & 0xFF, (end - 1) & 0xFF);
  114. mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS,
  115. (rect->y1 >> 8) & 0xFF, rect->y1 & 0xFF,
  116. (rect->y2 >> 8) & 0xFF, (rect->y2 - 1) & 0xFF);
  117. ret = mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START,
  118. (u8 *)dbidev->tx_buf,
  119. (end - start) * (rect->y2 - rect->y1));
  120. err_msg:
  121. if (ret)
  122. dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret);
  123. }
  124. static void st7586_pipe_update(struct drm_simple_display_pipe *pipe,
  125. struct drm_plane_state *old_state)
  126. {
  127. struct drm_plane_state *state = pipe->plane.state;
  128. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
  129. struct drm_framebuffer *fb = state->fb;
  130. struct drm_rect rect;
  131. int idx;
  132. if (!pipe->crtc.state->active)
  133. return;
  134. if (!drm_dev_enter(fb->dev, &idx))
  135. return;
  136. if (drm_atomic_helper_damage_merged(old_state, state, &rect))
  137. st7586_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
  138. &shadow_plane_state->fmtcnv_state);
  139. drm_dev_exit(idx);
  140. }
  141. static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe,
  142. struct drm_crtc_state *crtc_state,
  143. struct drm_plane_state *plane_state)
  144. {
  145. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  146. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  147. struct drm_framebuffer *fb = plane_state->fb;
  148. struct mipi_dbi *dbi = &dbidev->dbi;
  149. struct drm_rect rect = {
  150. .x1 = 0,
  151. .x2 = fb->width,
  152. .y1 = 0,
  153. .y2 = fb->height,
  154. };
  155. int idx, ret;
  156. u8 addr_mode;
  157. if (!drm_dev_enter(pipe->crtc.dev, &idx))
  158. return;
  159. DRM_DEBUG_KMS("\n");
  160. ret = mipi_dbi_poweron_reset(dbidev);
  161. if (ret)
  162. goto out_exit;
  163. mipi_dbi_command(dbi, ST7586_AUTO_READ_CTRL, 0x9f);
  164. mipi_dbi_command(dbi, ST7586_OTP_RW_CTRL, 0x00);
  165. msleep(10);
  166. mipi_dbi_command(dbi, ST7586_OTP_READ);
  167. msleep(20);
  168. mipi_dbi_command(dbi, ST7586_OTP_CTRL_OUT);
  169. mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
  170. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
  171. msleep(50);
  172. mipi_dbi_command(dbi, ST7586_SET_VOP_OFFSET, 0x00);
  173. mipi_dbi_command(dbi, ST7586_SET_VOP, 0xe3, 0x00);
  174. mipi_dbi_command(dbi, ST7586_SET_BIAS_SYSTEM, 0x02);
  175. mipi_dbi_command(dbi, ST7586_SET_BOOST_LEVEL, 0x04);
  176. mipi_dbi_command(dbi, ST7586_ENABLE_ANALOG, 0x1d);
  177. mipi_dbi_command(dbi, ST7586_SET_NLINE_INV, 0x00);
  178. mipi_dbi_command(dbi, ST7586_DISP_MODE_GRAY);
  179. mipi_dbi_command(dbi, ST7586_ENABLE_DDRAM, 0x02);
  180. switch (dbidev->rotation) {
  181. default:
  182. addr_mode = 0x00;
  183. break;
  184. case 90:
  185. addr_mode = ST7586_DISP_CTRL_MY;
  186. break;
  187. case 180:
  188. addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY;
  189. break;
  190. case 270:
  191. addr_mode = ST7586_DISP_CTRL_MX;
  192. break;
  193. }
  194. mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
  195. mipi_dbi_command(dbi, ST7586_SET_DISP_DUTY, 0x7f);
  196. mipi_dbi_command(dbi, ST7586_SET_PART_DISP, 0xa0);
  197. mipi_dbi_command(dbi, MIPI_DCS_SET_PARTIAL_ROWS, 0x00, 0x00, 0x00, 0x77);
  198. mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE);
  199. msleep(100);
  200. st7586_fb_dirty(&shadow_plane_state->data[0], fb, &rect,
  201. &shadow_plane_state->fmtcnv_state);
  202. mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
  203. out_exit:
  204. drm_dev_exit(idx);
  205. }
  206. static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe)
  207. {
  208. struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
  209. /*
  210. * This callback is not protected by drm_dev_enter/exit since we want to
  211. * turn off the display on regular driver unload. It's highly unlikely
  212. * that the underlying SPI controller is gone should this be called after
  213. * unplug.
  214. */
  215. DRM_DEBUG_KMS("\n");
  216. mipi_dbi_command(&dbidev->dbi, MIPI_DCS_SET_DISPLAY_OFF);
  217. }
  218. static const u32 st7586_formats[] = {
  219. DRM_FORMAT_XRGB8888,
  220. };
  221. static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = {
  222. .mode_valid = mipi_dbi_pipe_mode_valid,
  223. .enable = st7586_pipe_enable,
  224. .disable = st7586_pipe_disable,
  225. .update = st7586_pipe_update,
  226. .begin_fb_access = mipi_dbi_pipe_begin_fb_access,
  227. .end_fb_access = mipi_dbi_pipe_end_fb_access,
  228. .reset_plane = mipi_dbi_pipe_reset_plane,
  229. .duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state,
  230. .destroy_plane_state = mipi_dbi_pipe_destroy_plane_state,
  231. };
  232. static const struct drm_display_mode st7586_mode = {
  233. DRM_SIMPLE_MODE(178, 128, 37, 27),
  234. };
  235. DEFINE_DRM_GEM_DMA_FOPS(st7586_fops);
  236. static const struct drm_driver st7586_driver = {
  237. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  238. .fops = &st7586_fops,
  239. DRM_GEM_DMA_DRIVER_OPS_VMAP,
  240. .debugfs_init = mipi_dbi_debugfs_init,
  241. .name = "st7586",
  242. .desc = "Sitronix ST7586",
  243. .date = "20170801",
  244. .major = 1,
  245. .minor = 0,
  246. };
  247. static const struct of_device_id st7586_of_match[] = {
  248. { .compatible = "lego,ev3-lcd" },
  249. {},
  250. };
  251. MODULE_DEVICE_TABLE(of, st7586_of_match);
  252. static const struct spi_device_id st7586_id[] = {
  253. { "ev3-lcd", 0 },
  254. { },
  255. };
  256. MODULE_DEVICE_TABLE(spi, st7586_id);
  257. static int st7586_probe(struct spi_device *spi)
  258. {
  259. struct device *dev = &spi->dev;
  260. struct mipi_dbi_dev *dbidev;
  261. struct drm_device *drm;
  262. struct mipi_dbi *dbi;
  263. struct gpio_desc *a0;
  264. u32 rotation = 0;
  265. size_t bufsize;
  266. int ret;
  267. dbidev = devm_drm_dev_alloc(dev, &st7586_driver,
  268. struct mipi_dbi_dev, drm);
  269. if (IS_ERR(dbidev))
  270. return PTR_ERR(dbidev);
  271. dbi = &dbidev->dbi;
  272. drm = &dbidev->drm;
  273. bufsize = (st7586_mode.vdisplay + 2) / 3 * st7586_mode.hdisplay;
  274. dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  275. if (IS_ERR(dbi->reset))
  276. return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
  277. a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW);
  278. if (IS_ERR(a0))
  279. return dev_err_probe(dev, PTR_ERR(a0), "Failed to get GPIO 'a0'\n");
  280. device_property_read_u32(dev, "rotation", &rotation);
  281. ret = mipi_dbi_spi_init(spi, dbi, a0);
  282. if (ret)
  283. return ret;
  284. /* Cannot read from this controller via SPI */
  285. dbi->read_commands = NULL;
  286. ret = mipi_dbi_dev_init_with_formats(dbidev, &st7586_pipe_funcs,
  287. st7586_formats, ARRAY_SIZE(st7586_formats),
  288. &st7586_mode, rotation, bufsize);
  289. if (ret)
  290. return ret;
  291. /*
  292. * we are using 8-bit data, so we are not actually swapping anything,
  293. * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the
  294. * right thing and not use 16-bit transfers (which results in swapped
  295. * bytes on little-endian systems and causes out of order data to be
  296. * sent to the display).
  297. */
  298. dbi->swap_bytes = true;
  299. drm_mode_config_reset(drm);
  300. ret = drm_dev_register(drm, 0);
  301. if (ret)
  302. return ret;
  303. spi_set_drvdata(spi, drm);
  304. drm_fbdev_dma_setup(drm, 0);
  305. return 0;
  306. }
  307. static void st7586_remove(struct spi_device *spi)
  308. {
  309. struct drm_device *drm = spi_get_drvdata(spi);
  310. drm_dev_unplug(drm);
  311. drm_atomic_helper_shutdown(drm);
  312. }
  313. static void st7586_shutdown(struct spi_device *spi)
  314. {
  315. drm_atomic_helper_shutdown(spi_get_drvdata(spi));
  316. }
  317. static struct spi_driver st7586_spi_driver = {
  318. .driver = {
  319. .name = "st7586",
  320. .of_match_table = st7586_of_match,
  321. },
  322. .id_table = st7586_id,
  323. .probe = st7586_probe,
  324. .remove = st7586_remove,
  325. .shutdown = st7586_shutdown,
  326. };
  327. module_spi_driver(st7586_spi_driver);
  328. MODULE_DESCRIPTION("Sitronix ST7586 DRM driver");
  329. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  330. MODULE_LICENSE("GPL");