arcpgu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ARC PGU DRM driver.
  4. *
  5. * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com)
  6. */
  7. #include <linux/clk.h>
  8. #include <drm/clients/drm_client_setup.h>
  9. #include <drm/drm_atomic_helper.h>
  10. #include <drm/drm_debugfs.h>
  11. #include <drm/drm_device.h>
  12. #include <drm/drm_drv.h>
  13. #include <drm/drm_edid.h>
  14. #include <drm/drm_fb_dma_helper.h>
  15. #include <drm/drm_fbdev_dma.h>
  16. #include <drm/drm_fourcc.h>
  17. #include <drm/drm_framebuffer.h>
  18. #include <drm/drm_gem_dma_helper.h>
  19. #include <drm/drm_gem_framebuffer_helper.h>
  20. #include <drm/drm_module.h>
  21. #include <drm/drm_of.h>
  22. #include <drm/drm_probe_helper.h>
  23. #include <drm/drm_simple_kms_helper.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/module.h>
  26. #include <linux/of_reserved_mem.h>
  27. #include <linux/platform_device.h>
  28. #define ARCPGU_REG_CTRL 0x00
  29. #define ARCPGU_REG_STAT 0x04
  30. #define ARCPGU_REG_FMT 0x10
  31. #define ARCPGU_REG_HSYNC 0x14
  32. #define ARCPGU_REG_VSYNC 0x18
  33. #define ARCPGU_REG_ACTIVE 0x1c
  34. #define ARCPGU_REG_BUF0_ADDR 0x40
  35. #define ARCPGU_REG_STRIDE 0x50
  36. #define ARCPGU_REG_START_SET 0x84
  37. #define ARCPGU_REG_ID 0x3FC
  38. #define ARCPGU_CTRL_ENABLE_MASK 0x02
  39. #define ARCPGU_CTRL_VS_POL_MASK 0x1
  40. #define ARCPGU_CTRL_VS_POL_OFST 0x3
  41. #define ARCPGU_CTRL_HS_POL_MASK 0x1
  42. #define ARCPGU_CTRL_HS_POL_OFST 0x4
  43. #define ARCPGU_MODE_XRGB8888 BIT(2)
  44. #define ARCPGU_STAT_BUSY_MASK 0x02
  45. struct arcpgu_drm_private {
  46. struct drm_device drm;
  47. void __iomem *regs;
  48. struct clk *clk;
  49. struct drm_simple_display_pipe pipe;
  50. struct drm_connector sim_conn;
  51. };
  52. #define dev_to_arcpgu(x) container_of(x, struct arcpgu_drm_private, drm)
  53. #define pipe_to_arcpgu_priv(x) container_of(x, struct arcpgu_drm_private, pipe)
  54. static inline void arc_pgu_write(struct arcpgu_drm_private *arcpgu,
  55. unsigned int reg, u32 value)
  56. {
  57. iowrite32(value, arcpgu->regs + reg);
  58. }
  59. static inline u32 arc_pgu_read(struct arcpgu_drm_private *arcpgu,
  60. unsigned int reg)
  61. {
  62. return ioread32(arcpgu->regs + reg);
  63. }
  64. #define XRES_DEF 640
  65. #define YRES_DEF 480
  66. #define XRES_MAX 8192
  67. #define YRES_MAX 8192
  68. static int arcpgu_drm_connector_get_modes(struct drm_connector *connector)
  69. {
  70. int count;
  71. count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
  72. drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
  73. return count;
  74. }
  75. static const struct drm_connector_helper_funcs
  76. arcpgu_drm_connector_helper_funcs = {
  77. .get_modes = arcpgu_drm_connector_get_modes,
  78. };
  79. static const struct drm_connector_funcs arcpgu_drm_connector_funcs = {
  80. .reset = drm_atomic_helper_connector_reset,
  81. .fill_modes = drm_helper_probe_single_connector_modes,
  82. .destroy = drm_connector_cleanup,
  83. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  84. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  85. };
  86. static int arcpgu_drm_sim_init(struct drm_device *drm, struct drm_connector *connector)
  87. {
  88. drm_connector_helper_add(connector, &arcpgu_drm_connector_helper_funcs);
  89. return drm_connector_init(drm, connector, &arcpgu_drm_connector_funcs,
  90. DRM_MODE_CONNECTOR_VIRTUAL);
  91. }
  92. #define ENCODE_PGU_XY(x, y) ((((x) - 1) << 16) | ((y) - 1))
  93. static const u32 arc_pgu_supported_formats[] = {
  94. DRM_FORMAT_RGB565,
  95. DRM_FORMAT_XRGB8888,
  96. DRM_FORMAT_ARGB8888,
  97. };
  98. static void arc_pgu_set_pxl_fmt(struct arcpgu_drm_private *arcpgu)
  99. {
  100. const struct drm_framebuffer *fb = arcpgu->pipe.plane.state->fb;
  101. uint32_t pixel_format = fb->format->format;
  102. u32 format = DRM_FORMAT_INVALID;
  103. int i;
  104. u32 reg_ctrl;
  105. for (i = 0; i < ARRAY_SIZE(arc_pgu_supported_formats); i++) {
  106. if (arc_pgu_supported_formats[i] == pixel_format)
  107. format = arc_pgu_supported_formats[i];
  108. }
  109. if (WARN_ON(format == DRM_FORMAT_INVALID))
  110. return;
  111. reg_ctrl = arc_pgu_read(arcpgu, ARCPGU_REG_CTRL);
  112. if (format == DRM_FORMAT_RGB565)
  113. reg_ctrl &= ~ARCPGU_MODE_XRGB8888;
  114. else
  115. reg_ctrl |= ARCPGU_MODE_XRGB8888;
  116. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL, reg_ctrl);
  117. }
  118. static enum drm_mode_status arc_pgu_mode_valid(struct drm_simple_display_pipe *pipe,
  119. const struct drm_display_mode *mode)
  120. {
  121. struct arcpgu_drm_private *arcpgu = pipe_to_arcpgu_priv(pipe);
  122. long rate, clk_rate = mode->clock * 1000;
  123. long diff = clk_rate / 200; /* +-0.5% allowed by HDMI spec */
  124. rate = clk_round_rate(arcpgu->clk, clk_rate);
  125. if ((max(rate, clk_rate) - min(rate, clk_rate) < diff) && (rate > 0))
  126. return MODE_OK;
  127. return MODE_NOCLOCK;
  128. }
  129. static void arc_pgu_mode_set(struct arcpgu_drm_private *arcpgu)
  130. {
  131. struct drm_display_mode *m = &arcpgu->pipe.crtc.state->adjusted_mode;
  132. u32 val;
  133. arc_pgu_write(arcpgu, ARCPGU_REG_FMT,
  134. ENCODE_PGU_XY(m->crtc_htotal, m->crtc_vtotal));
  135. arc_pgu_write(arcpgu, ARCPGU_REG_HSYNC,
  136. ENCODE_PGU_XY(m->crtc_hsync_start - m->crtc_hdisplay,
  137. m->crtc_hsync_end - m->crtc_hdisplay));
  138. arc_pgu_write(arcpgu, ARCPGU_REG_VSYNC,
  139. ENCODE_PGU_XY(m->crtc_vsync_start - m->crtc_vdisplay,
  140. m->crtc_vsync_end - m->crtc_vdisplay));
  141. arc_pgu_write(arcpgu, ARCPGU_REG_ACTIVE,
  142. ENCODE_PGU_XY(m->crtc_hblank_end - m->crtc_hblank_start,
  143. m->crtc_vblank_end - m->crtc_vblank_start));
  144. val = arc_pgu_read(arcpgu, ARCPGU_REG_CTRL);
  145. if (m->flags & DRM_MODE_FLAG_PVSYNC)
  146. val |= ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST;
  147. else
  148. val &= ~(ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST);
  149. if (m->flags & DRM_MODE_FLAG_PHSYNC)
  150. val |= ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST;
  151. else
  152. val &= ~(ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST);
  153. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL, val);
  154. arc_pgu_write(arcpgu, ARCPGU_REG_STRIDE, 0);
  155. arc_pgu_write(arcpgu, ARCPGU_REG_START_SET, 1);
  156. arc_pgu_set_pxl_fmt(arcpgu);
  157. clk_set_rate(arcpgu->clk, m->crtc_clock * 1000);
  158. }
  159. static void arc_pgu_enable(struct drm_simple_display_pipe *pipe,
  160. struct drm_crtc_state *crtc_state,
  161. struct drm_plane_state *plane_state)
  162. {
  163. struct arcpgu_drm_private *arcpgu = pipe_to_arcpgu_priv(pipe);
  164. arc_pgu_mode_set(arcpgu);
  165. clk_prepare_enable(arcpgu->clk);
  166. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
  167. arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) |
  168. ARCPGU_CTRL_ENABLE_MASK);
  169. }
  170. static void arc_pgu_disable(struct drm_simple_display_pipe *pipe)
  171. {
  172. struct arcpgu_drm_private *arcpgu = pipe_to_arcpgu_priv(pipe);
  173. clk_disable_unprepare(arcpgu->clk);
  174. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
  175. arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) &
  176. ~ARCPGU_CTRL_ENABLE_MASK);
  177. }
  178. static void arc_pgu_update(struct drm_simple_display_pipe *pipe,
  179. struct drm_plane_state *state)
  180. {
  181. struct arcpgu_drm_private *arcpgu;
  182. struct drm_gem_dma_object *gem;
  183. if (!pipe->plane.state->fb)
  184. return;
  185. arcpgu = pipe_to_arcpgu_priv(pipe);
  186. gem = drm_fb_dma_get_gem_obj(pipe->plane.state->fb, 0);
  187. arc_pgu_write(arcpgu, ARCPGU_REG_BUF0_ADDR, gem->dma_addr);
  188. }
  189. static const struct drm_simple_display_pipe_funcs arc_pgu_pipe_funcs = {
  190. .update = arc_pgu_update,
  191. .mode_valid = arc_pgu_mode_valid,
  192. .enable = arc_pgu_enable,
  193. .disable = arc_pgu_disable,
  194. };
  195. static const struct drm_mode_config_funcs arcpgu_drm_modecfg_funcs = {
  196. .fb_create = drm_gem_fb_create,
  197. .atomic_check = drm_atomic_helper_check,
  198. .atomic_commit = drm_atomic_helper_commit,
  199. };
  200. DEFINE_DRM_GEM_DMA_FOPS(arcpgu_drm_ops);
  201. static int arcpgu_load(struct arcpgu_drm_private *arcpgu)
  202. {
  203. struct platform_device *pdev = to_platform_device(arcpgu->drm.dev);
  204. struct device_node *encoder_node = NULL, *endpoint_node = NULL;
  205. struct drm_connector *connector = NULL;
  206. struct drm_device *drm = &arcpgu->drm;
  207. int ret;
  208. arcpgu->clk = devm_clk_get(drm->dev, "pxlclk");
  209. if (IS_ERR(arcpgu->clk))
  210. return PTR_ERR(arcpgu->clk);
  211. ret = drmm_mode_config_init(drm);
  212. if (ret)
  213. return ret;
  214. drm->mode_config.min_width = 0;
  215. drm->mode_config.min_height = 0;
  216. drm->mode_config.max_width = 1920;
  217. drm->mode_config.max_height = 1080;
  218. drm->mode_config.funcs = &arcpgu_drm_modecfg_funcs;
  219. arcpgu->regs = devm_platform_ioremap_resource(pdev, 0);
  220. if (IS_ERR(arcpgu->regs))
  221. return PTR_ERR(arcpgu->regs);
  222. dev_info(drm->dev, "arc_pgu ID: 0x%x\n",
  223. arc_pgu_read(arcpgu, ARCPGU_REG_ID));
  224. /* Get the optional framebuffer memory resource */
  225. ret = of_reserved_mem_device_init(drm->dev);
  226. if (ret && ret != -ENODEV)
  227. return ret;
  228. if (dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32)))
  229. return -ENODEV;
  230. /*
  231. * There is only one output port inside each device. It is linked with
  232. * encoder endpoint.
  233. */
  234. endpoint_node = of_graph_get_endpoint_by_regs(pdev->dev.of_node, 0, -1);
  235. if (endpoint_node) {
  236. encoder_node = of_graph_get_remote_port_parent(endpoint_node);
  237. of_node_put(endpoint_node);
  238. } else {
  239. connector = &arcpgu->sim_conn;
  240. dev_info(drm->dev, "no encoder found. Assumed virtual LCD on simulation platform\n");
  241. ret = arcpgu_drm_sim_init(drm, connector);
  242. if (ret < 0)
  243. return ret;
  244. }
  245. ret = drm_simple_display_pipe_init(drm, &arcpgu->pipe, &arc_pgu_pipe_funcs,
  246. arc_pgu_supported_formats,
  247. ARRAY_SIZE(arc_pgu_supported_formats),
  248. NULL, connector);
  249. if (ret)
  250. return ret;
  251. if (encoder_node) {
  252. /* Locate drm bridge from the hdmi encoder DT node */
  253. struct drm_bridge *bridge __free(drm_bridge_put) =
  254. of_drm_find_and_get_bridge(encoder_node);
  255. if (!bridge)
  256. return -EPROBE_DEFER;
  257. ret = drm_simple_display_pipe_attach_bridge(&arcpgu->pipe, bridge);
  258. if (ret)
  259. return ret;
  260. }
  261. drm_mode_config_reset(drm);
  262. drm_kms_helper_poll_init(drm);
  263. platform_set_drvdata(pdev, drm);
  264. return 0;
  265. }
  266. static int arcpgu_unload(struct drm_device *drm)
  267. {
  268. drm_kms_helper_poll_fini(drm);
  269. drm_atomic_helper_shutdown(drm);
  270. return 0;
  271. }
  272. #ifdef CONFIG_DEBUG_FS
  273. static int arcpgu_show_pxlclock(struct seq_file *m, void *arg)
  274. {
  275. struct drm_info_node *node = (struct drm_info_node *)m->private;
  276. struct drm_device *drm = node->minor->dev;
  277. struct arcpgu_drm_private *arcpgu = dev_to_arcpgu(drm);
  278. unsigned long clkrate = clk_get_rate(arcpgu->clk);
  279. unsigned long mode_clock = arcpgu->pipe.crtc.mode.crtc_clock * 1000;
  280. seq_printf(m, "hw : %lu\n", clkrate);
  281. seq_printf(m, "mode: %lu\n", mode_clock);
  282. return 0;
  283. }
  284. static struct drm_info_list arcpgu_debugfs_list[] = {
  285. { "clocks", arcpgu_show_pxlclock, 0 },
  286. };
  287. static void arcpgu_debugfs_init(struct drm_minor *minor)
  288. {
  289. drm_debugfs_create_files(arcpgu_debugfs_list,
  290. ARRAY_SIZE(arcpgu_debugfs_list),
  291. minor->debugfs_root, minor);
  292. }
  293. #endif
  294. static const struct drm_driver arcpgu_drm_driver = {
  295. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  296. .name = "arcpgu",
  297. .desc = "ARC PGU Controller",
  298. .major = 1,
  299. .minor = 0,
  300. .patchlevel = 0,
  301. .fops = &arcpgu_drm_ops,
  302. DRM_GEM_DMA_DRIVER_OPS,
  303. DRM_FBDEV_DMA_DRIVER_OPS,
  304. #ifdef CONFIG_DEBUG_FS
  305. .debugfs_init = arcpgu_debugfs_init,
  306. #endif
  307. };
  308. static int arcpgu_probe(struct platform_device *pdev)
  309. {
  310. struct arcpgu_drm_private *arcpgu;
  311. int ret;
  312. arcpgu = devm_drm_dev_alloc(&pdev->dev, &arcpgu_drm_driver,
  313. struct arcpgu_drm_private, drm);
  314. if (IS_ERR(arcpgu))
  315. return PTR_ERR(arcpgu);
  316. ret = arcpgu_load(arcpgu);
  317. if (ret)
  318. return ret;
  319. ret = drm_dev_register(&arcpgu->drm, 0);
  320. if (ret)
  321. goto err_unload;
  322. drm_client_setup_with_fourcc(&arcpgu->drm, DRM_FORMAT_RGB565);
  323. return 0;
  324. err_unload:
  325. arcpgu_unload(&arcpgu->drm);
  326. return ret;
  327. }
  328. static void arcpgu_remove(struct platform_device *pdev)
  329. {
  330. struct drm_device *drm = platform_get_drvdata(pdev);
  331. drm_dev_unregister(drm);
  332. arcpgu_unload(drm);
  333. }
  334. static const struct of_device_id arcpgu_of_table[] = {
  335. {.compatible = "snps,arcpgu"},
  336. {}
  337. };
  338. MODULE_DEVICE_TABLE(of, arcpgu_of_table);
  339. static struct platform_driver arcpgu_platform_driver = {
  340. .probe = arcpgu_probe,
  341. .remove = arcpgu_remove,
  342. .driver = {
  343. .name = "arcpgu",
  344. .of_match_table = arcpgu_of_table,
  345. },
  346. };
  347. drm_module_platform_driver(arcpgu_platform_driver);
  348. MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>");
  349. MODULE_DESCRIPTION("ARC PGU DRM driver");
  350. MODULE_LICENSE("GPL");