st7586.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. mutex_lock(&tdev->dirty_lock);
  106. if (!mipi->enabled)
  107. goto out_unlock;
  108. /* fbdev can flush even when we're not interested */
  109. if (tdev->pipe.plane.fb != fb)
  110. goto out_unlock;
  111. tinydrm_merge_clips(&clip, clips, num_clips, flags, fb->width,
  112. fb->height);
  113. /* 3 pixels per byte, so grow clip to nearest multiple of 3 */
  114. clip.x1 = rounddown(clip.x1, 3);
  115. clip.x2 = roundup(clip.x2, 3);
  116. DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id,
  117. clip.x1, clip.x2, clip.y1, clip.y2);
  118. ret = st7586_buf_copy(mipi->tx_buf, fb, &clip);
  119. if (ret)
  120. goto out_unlock;
  121. /* Pixels are packed 3 per byte */
  122. start = clip.x1 / 3;
  123. end = clip.x2 / 3;
  124. mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS,
  125. (start >> 8) & 0xFF, start & 0xFF,
  126. (end >> 8) & 0xFF, (end - 1) & 0xFF);
  127. mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS,
  128. (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF,
  129. (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF);
  130. ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START,
  131. (u8 *)mipi->tx_buf,
  132. (end - start) * (clip.y2 - clip.y1));
  133. out_unlock:
  134. mutex_unlock(&tdev->dirty_lock);
  135. if (ret)
  136. dev_err_once(fb->dev->dev, "Failed to update display %d\n",
  137. ret);
  138. return ret;
  139. }
  140. static const struct drm_framebuffer_funcs st7586_fb_funcs = {
  141. .destroy = drm_gem_fb_destroy,
  142. .create_handle = drm_gem_fb_create_handle,
  143. .dirty = st7586_fb_dirty,
  144. };
  145. static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe,
  146. struct drm_crtc_state *crtc_state)
  147. {
  148. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  149. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  150. int ret;
  151. u8 addr_mode;
  152. DRM_DEBUG_KMS("\n");
  153. ret = mipi_dbi_poweron_reset(mipi);
  154. if (ret)
  155. return;
  156. mipi_dbi_command(mipi, ST7586_AUTO_READ_CTRL, 0x9f);
  157. mipi_dbi_command(mipi, ST7586_OTP_RW_CTRL, 0x00);
  158. msleep(10);
  159. mipi_dbi_command(mipi, ST7586_OTP_READ);
  160. msleep(20);
  161. mipi_dbi_command(mipi, ST7586_OTP_CTRL_OUT);
  162. mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE);
  163. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
  164. msleep(50);
  165. mipi_dbi_command(mipi, ST7586_SET_VOP_OFFSET, 0x00);
  166. mipi_dbi_command(mipi, ST7586_SET_VOP, 0xe3, 0x00);
  167. mipi_dbi_command(mipi, ST7586_SET_BIAS_SYSTEM, 0x02);
  168. mipi_dbi_command(mipi, ST7586_SET_BOOST_LEVEL, 0x04);
  169. mipi_dbi_command(mipi, ST7586_ENABLE_ANALOG, 0x1d);
  170. mipi_dbi_command(mipi, ST7586_SET_NLINE_INV, 0x00);
  171. mipi_dbi_command(mipi, ST7586_DISP_MODE_GRAY);
  172. mipi_dbi_command(mipi, ST7586_ENABLE_DDRAM, 0x02);
  173. switch (mipi->rotation) {
  174. default:
  175. addr_mode = 0x00;
  176. break;
  177. case 90:
  178. addr_mode = ST7586_DISP_CTRL_MY;
  179. break;
  180. case 180:
  181. addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY;
  182. break;
  183. case 270:
  184. addr_mode = ST7586_DISP_CTRL_MX;
  185. break;
  186. }
  187. mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
  188. mipi_dbi_command(mipi, ST7586_SET_DISP_DUTY, 0x7f);
  189. mipi_dbi_command(mipi, ST7586_SET_PART_DISP, 0xa0);
  190. mipi_dbi_command(mipi, MIPI_DCS_SET_PARTIAL_AREA, 0x00, 0x00, 0x00, 0x77);
  191. mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE);
  192. msleep(100);
  193. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON);
  194. mipi_dbi_enable_flush(mipi);
  195. }
  196. static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe)
  197. {
  198. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  199. struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev);
  200. DRM_DEBUG_KMS("\n");
  201. if (!mipi->enabled)
  202. return;
  203. mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF);
  204. mipi->enabled = false;
  205. }
  206. static const u32 st7586_formats[] = {
  207. DRM_FORMAT_XRGB8888,
  208. };
  209. static int st7586_init(struct device *dev, struct mipi_dbi *mipi,
  210. const struct drm_simple_display_pipe_funcs *pipe_funcs,
  211. struct drm_driver *driver, const struct drm_display_mode *mode,
  212. unsigned int rotation)
  213. {
  214. size_t bufsize = (mode->vdisplay + 2) / 3 * mode->hdisplay;
  215. struct tinydrm_device *tdev = &mipi->tinydrm;
  216. int ret;
  217. mutex_init(&mipi->cmdlock);
  218. mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL);
  219. if (!mipi->tx_buf)
  220. return -ENOMEM;
  221. ret = devm_tinydrm_init(dev, tdev, &st7586_fb_funcs, driver);
  222. if (ret)
  223. return ret;
  224. ret = tinydrm_display_pipe_init(tdev, pipe_funcs,
  225. DRM_MODE_CONNECTOR_VIRTUAL,
  226. st7586_formats,
  227. ARRAY_SIZE(st7586_formats),
  228. mode, rotation);
  229. if (ret)
  230. return ret;
  231. tdev->drm->mode_config.preferred_depth = 32;
  232. mipi->rotation = rotation;
  233. drm_mode_config_reset(tdev->drm);
  234. DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n",
  235. tdev->drm->mode_config.preferred_depth, rotation);
  236. return 0;
  237. }
  238. static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = {
  239. .enable = st7586_pipe_enable,
  240. .disable = st7586_pipe_disable,
  241. .update = tinydrm_display_pipe_update,
  242. .prepare_fb = tinydrm_display_pipe_prepare_fb,
  243. };
  244. static const struct drm_display_mode st7586_mode = {
  245. TINYDRM_MODE(178, 128, 37, 27),
  246. };
  247. DEFINE_DRM_GEM_CMA_FOPS(st7586_fops);
  248. static struct drm_driver st7586_driver = {
  249. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  250. DRIVER_ATOMIC,
  251. .fops = &st7586_fops,
  252. TINYDRM_GEM_DRIVER_OPS,
  253. .lastclose = drm_fb_helper_lastclose,
  254. .debugfs_init = mipi_dbi_debugfs_init,
  255. .name = "st7586",
  256. .desc = "Sitronix ST7586",
  257. .date = "20170801",
  258. .major = 1,
  259. .minor = 0,
  260. };
  261. static const struct of_device_id st7586_of_match[] = {
  262. { .compatible = "lego,ev3-lcd" },
  263. {},
  264. };
  265. MODULE_DEVICE_TABLE(of, st7586_of_match);
  266. static const struct spi_device_id st7586_id[] = {
  267. { "ev3-lcd", 0 },
  268. { },
  269. };
  270. MODULE_DEVICE_TABLE(spi, st7586_id);
  271. static int st7586_probe(struct spi_device *spi)
  272. {
  273. struct device *dev = &spi->dev;
  274. struct mipi_dbi *mipi;
  275. struct gpio_desc *a0;
  276. u32 rotation = 0;
  277. int ret;
  278. mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL);
  279. if (!mipi)
  280. return -ENOMEM;
  281. mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  282. if (IS_ERR(mipi->reset)) {
  283. DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
  284. return PTR_ERR(mipi->reset);
  285. }
  286. a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW);
  287. if (IS_ERR(a0)) {
  288. DRM_DEV_ERROR(dev, "Failed to get gpio 'a0'\n");
  289. return PTR_ERR(a0);
  290. }
  291. device_property_read_u32(dev, "rotation", &rotation);
  292. ret = mipi_dbi_spi_init(spi, mipi, a0);
  293. if (ret)
  294. return ret;
  295. /* Cannot read from this controller via SPI */
  296. mipi->read_commands = NULL;
  297. /*
  298. * we are using 8-bit data, so we are not actually swapping anything,
  299. * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the
  300. * right thing and not use 16-bit transfers (which results in swapped
  301. * bytes on little-endian systems and causes out of order data to be
  302. * sent to the display).
  303. */
  304. mipi->swap_bytes = true;
  305. ret = st7586_init(&spi->dev, mipi, &st7586_pipe_funcs, &st7586_driver,
  306. &st7586_mode, rotation);
  307. if (ret)
  308. return ret;
  309. spi_set_drvdata(spi, mipi);
  310. return devm_tinydrm_register(&mipi->tinydrm);
  311. }
  312. static void st7586_shutdown(struct spi_device *spi)
  313. {
  314. struct mipi_dbi *mipi = spi_get_drvdata(spi);
  315. tinydrm_shutdown(&mipi->tinydrm);
  316. }
  317. static struct spi_driver st7586_spi_driver = {
  318. .driver = {
  319. .name = "st7586",
  320. .owner = THIS_MODULE,
  321. .of_match_table = st7586_of_match,
  322. },
  323. .id_table = st7586_id,
  324. .probe = st7586_probe,
  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");