st7586.c 11 KB

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