st7586.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * DRM driver for Sitronix ST7586 panels
  3. *
  4. * Copyright 2017 David Lechner <david@lechnology.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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. #include <drm/drm_fb_helper.h>
  19. #include <drm/drm_gem_framebuffer_helper.h>
  20. #include <drm/tinydrm/mipi-dbi.h>
  21. #include <drm/tinydrm/tinydrm-helpers.h>
  22. /* controller-specific commands */
  23. #define ST7586_DISP_MODE_GRAY 0x38
  24. #define ST7586_DISP_MODE_MONO 0x39
  25. #define ST7586_ENABLE_DDRAM 0x3a
  26. #define ST7586_SET_DISP_DUTY 0xb0
  27. #define ST7586_SET_PART_DISP 0xb4
  28. #define ST7586_SET_NLINE_INV 0xb5
  29. #define ST7586_SET_VOP 0xc0
  30. #define ST7586_SET_BIAS_SYSTEM 0xc3
  31. #define ST7586_SET_BOOST_LEVEL 0xc4
  32. #define ST7586_SET_VOP_OFFSET 0xc7
  33. #define ST7586_ENABLE_ANALOG 0xd0
  34. #define ST7586_AUTO_READ_CTRL 0xd7
  35. #define ST7586_OTP_RW_CTRL 0xe0
  36. #define ST7586_OTP_CTRL_OUT 0xe1
  37. #define ST7586_OTP_READ 0xe3
  38. #define ST7586_DISP_CTRL_MX BIT(6)
  39. #define ST7586_DISP_CTRL_MY BIT(7)
  40. /*
  41. * The ST7586 controller has an unusual pixel format where 2bpp grayscale is
  42. * packed 3 pixels per byte with the first two pixels using 3 bits and the 3rd
  43. * pixel using only 2 bits.
  44. *
  45. * | D7 | D6 | D5 || | || 2bpp |
  46. * | (D4) | (D3) | (D2) || D1 | D0 || GRAY |
  47. * +------+------+------++------+------++------+
  48. * | 1 | 1 | 1 || 1 | 1 || 0 0 | black
  49. * | 1 | 0 | 0 || 1 | 0 || 0 1 | dark gray
  50. * | 0 | 1 | 0 || 0 | 1 || 1 0 | light gray
  51. * | 0 | 0 | 0 || 0 | 0 || 1 1 | white
  52. */
  53. static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 };
  54. static void st7586_xrgb8888_to_gray332(u8 *dst, void *vaddr,
  55. struct drm_framebuffer *fb,
  56. struct drm_clip_rect *clip)
  57. {
  58. size_t len = (clip->x2 - clip->x1) * (clip->y2 - clip->y1);
  59. unsigned int x, y;
  60. u8 *src, *buf, val;
  61. buf = kmalloc(len, GFP_KERNEL);
  62. if (!buf)
  63. return;
  64. tinydrm_xrgb8888_to_gray8(buf, vaddr, fb, clip);
  65. src = buf;
  66. for (y = clip->y1; y < clip->y2; y++) {
  67. for (x = clip->x1; x < clip->x2; x += 3) {
  68. val = st7586_lookup[*src++ >> 6] << 5;
  69. val |= st7586_lookup[*src++ >> 6] << 2;
  70. val |= st7586_lookup[*src++ >> 6] >> 1;
  71. *dst++ = val;
  72. }
  73. }
  74. kfree(buf);
  75. }
  76. static int st7586_buf_copy(void *dst, struct drm_framebuffer *fb,
  77. struct drm_clip_rect *clip)
  78. {
  79. struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  80. struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
  81. void *src = cma_obj->vaddr;
  82. int ret = 0;
  83. if (import_attach) {
  84. ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
  85. DMA_FROM_DEVICE);
  86. if (ret)
  87. return ret;
  88. }
  89. st7586_xrgb8888_to_gray332(dst, src, fb, clip);
  90. if (import_attach)
  91. ret = dma_buf_end_cpu_access(import_attach->dmabuf,
  92. DMA_FROM_DEVICE);
  93. return ret;
  94. }
  95. static int st7586_fb_dirty(struct drm_framebuffer *fb,
  96. struct drm_file *file_priv, unsigned int flags,
  97. unsigned int color, struct drm_clip_rect *clips,
  98. unsigned int num_clips)
  99. {
  100. struct tinydrm_device *tdev = fb->dev->dev_private;
  101. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  102. struct drm_clip_rect clip;
  103. int start, end;
  104. int ret = 0;
  105. if (!mipi->enabled)
  106. return 0;
  107. tinydrm_merge_clips(&clip, clips, num_clips, flags, fb->width,
  108. fb->height);
  109. /* 3 pixels per byte, so grow clip to nearest multiple of 3 */
  110. clip.x1 = rounddown(clip.x1, 3);
  111. clip.x2 = roundup(clip.x2, 3);
  112. DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
  113. clip.x1, clip.x2, clip.y1, clip.y2);
  114. ret = st7586_buf_copy(mipi->tx_buf, fb, &clip);
  115. if (ret)
  116. return ret;
  117. /* Pixels are packed 3 per byte */
  118. start = clip.x1 / 3;
  119. end = clip.x2 / 3;
  120. mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS,
  121. (start >> 8) & 0xFF, start & 0xFF,
  122. (end >> 8) & 0xFF, (end - 1) & 0xFF);
  123. mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS,
  124. (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
  125. (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
  126. ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
  127. (u8 *)mipi->tx_buf,
  128. (end - start) * (clip.y2 - clip.y1));
  129. return ret;
  130. }
  131. static const struct drm_framebuffer_funcs st7586_fb_funcs = {
  132. .destroy = drm_gem_fb_destroy,
  133. .create_handle = drm_gem_fb_create_handle,
  134. .dirty = tinydrm_fb_dirty,
  135. };
  136. static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe,
  137. struct drm_crtc_state *crtc_state,
  138. struct drm_plane_state *plane_state)
  139. {
  140. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  141. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  142. int ret;
  143. u8 addr_mode;
  144. DRM_DEBUG_KMS("\n");
  145. ret = mipi_dbi_poweron_reset(mipi);
  146. if (ret)
  147. return;
  148. mipi_dbi_command(mipi, ST7586_AUTO_READ_CTRL, 0x9f);
  149. mipi_dbi_command(mipi, ST7586_OTP_RW_CTRL, 0x00);
  150. msleep(10);
  151. mipi_dbi_command(mipi, ST7586_OTP_READ);
  152. msleep(20);
  153. mipi_dbi_command(mipi, ST7586_OTP_CTRL_OUT);
  154. mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
  155. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
  156. msleep(50);
  157. mipi_dbi_command(mipi, ST7586_SET_VOP_OFFSET, 0x00);
  158. mipi_dbi_command(mipi, ST7586_SET_VOP, 0xe3, 0x00);
  159. mipi_dbi_command(mipi, ST7586_SET_BIAS_SYSTEM, 0x02);
  160. mipi_dbi_command(mipi, ST7586_SET_BOOST_LEVEL, 0x04);
  161. mipi_dbi_command(mipi, ST7586_ENABLE_ANALOG, 0x1d);
  162. mipi_dbi_command(mipi, ST7586_SET_NLINE_INV, 0x00);
  163. mipi_dbi_command(mipi, ST7586_DISP_MODE_GRAY);
  164. mipi_dbi_command(mipi, ST7586_ENABLE_DDRAM, 0x02);
  165. switch (mipi->rotation) {
  166. default:
  167. addr_mode = 0x00;
  168. break;
  169. case 90:
  170. addr_mode = ST7586_DISP_CTRL_MY;
  171. break;
  172. case 180:
  173. addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY;
  174. break;
  175. case 270:
  176. addr_mode = ST7586_DISP_CTRL_MX;
  177. break;
  178. }
  179. mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
  180. mipi_dbi_command(mipi, ST7586_SET_DISP_DUTY, 0x7f);
  181. mipi_dbi_command(mipi, ST7586_SET_PART_DISP, 0xa0);
  182. mipi_dbi_command(mipi, MIPI_DCS_SET_PARTIAL_AREA, 0x00, 0x00, 0x00, 0x77);
  183. mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE);
  184. msleep(100);
  185. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
  186. mipi_dbi_enable_flush(mipi, crtc_state, plane_state);
  187. }
  188. static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe)
  189. {
  190. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  191. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  192. DRM_DEBUG_KMS("\n");
  193. if (!mipi->enabled)
  194. return;
  195. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
  196. mipi->enabled = false;
  197. }
  198. static const u32 st7586_formats[] = {
  199. DRM_FORMAT_XRGB8888,
  200. };
  201. static int st7586_init(struct device *dev, struct mipi_dbi *mipi,
  202. const struct drm_simple_display_pipe_funcs *pipe_funcs,
  203. struct drm_driver *driver, const struct drm_display_mode *mode,
  204. unsigned int rotation)
  205. {
  206. size_t bufsize = (mode->vdisplay + 2) / 3 * mode->hdisplay;
  207. struct tinydrm_device *tdev = &mipi->tinydrm;
  208. int ret;
  209. mutex_init(&mipi->cmdlock);
  210. mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
  211. if (!mipi->tx_buf)
  212. return -ENOMEM;
  213. ret = devm_tinydrm_init(dev, tdev, &st7586_fb_funcs, driver);
  214. if (ret)
  215. return ret;
  216. tdev->fb_dirty = st7586_fb_dirty;
  217. ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
  218. DRM_MODE_CONNECTOR_VIRTUAL,
  219. st7586_formats,
  220. ARRAY_SIZE(st7586_formats),
  221. mode, rotation);
  222. if (ret)
  223. return ret;
  224. tdev->drm->mode_config.preferred_depth = 32;
  225. mipi->rotation = rotation;
  226. drm_mode_config_reset(tdev->drm);
  227. DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
  228. tdev->drm->mode_config.preferred_depth, rotation);
  229. return 0;
  230. }
  231. static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = {
  232. .enable = st7586_pipe_enable,
  233. .disable = st7586_pipe_disable,
  234. .update = tinydrm_display_pipe_update,
  235. .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
  236. };
  237. static const struct drm_display_mode st7586_mode = {
  238. TINYDRM_MODE(178, 128, 37, 27),
  239. };
  240. DEFINE_DRM_GEM_CMA_FOPS(st7586_fops);
  241. static struct drm_driver st7586_driver = {
  242. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  243. DRIVER_ATOMIC,
  244. .fops = &st7586_fops,
  245. TINYDRM_GEM_DRIVER_OPS,
  246. .debugfs_init = mipi_dbi_debugfs_init,
  247. .name = "st7586",
  248. .desc = "Sitronix ST7586",
  249. .date = "20170801",
  250. .major = 1,
  251. .minor = 0,
  252. };
  253. static const struct of_device_id st7586_of_match[] = {
  254. { .compatible = "lego,ev3-lcd" },
  255. {},
  256. };
  257. MODULE_DEVICE_TABLE(of, st7586_of_match);
  258. static const struct spi_device_id st7586_id[] = {
  259. { "ev3-lcd", 0 },
  260. { },
  261. };
  262. MODULE_DEVICE_TABLE(spi, st7586_id);
  263. static int st7586_probe(struct spi_device *spi)
  264. {
  265. struct device *dev = &spi->dev;
  266. struct mipi_dbi *mipi;
  267. struct gpio_desc *a0;
  268. u32 rotation = 0;
  269. int ret;
  270. mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
  271. if (!mipi)
  272. return -ENOMEM;
  273. mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  274. if (IS_ERR(mipi->reset)) {
  275. DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
  276. return PTR_ERR(mipi->reset);
  277. }
  278. a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW);
  279. if (IS_ERR(a0)) {
  280. DRM_DEV_ERROR(dev, "Failed to get gpio 'a0'\n");
  281. return PTR_ERR(a0);
  282. }
  283. device_property_read_u32(dev, "rotation", &rotation);
  284. ret = mipi_dbi_spi_init(spi, mipi, a0);
  285. if (ret)
  286. return ret;
  287. /* Cannot read from this controller via SPI */
  288. mipi->read_commands = NULL;
  289. /*
  290. * we are using 8-bit data, so we are not actually swapping anything,
  291. * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the
  292. * right thing and not use 16-bit transfers (which results in swapped
  293. * bytes on little-endian systems and causes out of order data to be
  294. * sent to the display).
  295. */
  296. mipi->swap_bytes = true;
  297. ret = st7586_init(&spi->dev, mipi, &st7586_pipe_funcs, &st7586_driver,
  298. &st7586_mode, rotation);
  299. if (ret)
  300. return ret;
  301. spi_set_drvdata(spi, mipi);
  302. return devm_tinydrm_register(&mipi->tinydrm);
  303. }
  304. static void st7586_shutdown(struct spi_device *spi)
  305. {
  306. struct mipi_dbi *mipi = spi_get_drvdata(spi);
  307. tinydrm_shutdown(&mipi->tinydrm);
  308. }
  309. static struct spi_driver st7586_spi_driver = {
  310. .driver = {
  311. .name = "st7586",
  312. .owner = THIS_MODULE,
  313. .of_match_table = st7586_of_match,
  314. },
  315. .id_table = st7586_id,
  316. .probe = st7586_probe,
  317. .shutdown = st7586_shutdown,
  318. };
  319. module_spi_driver(st7586_spi_driver);
  320. MODULE_DESCRIPTION("Sitronix ST7586 DRM driver");
  321. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  322. MODULE_LICENSE("GPL");