st7586.c 11 KB

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