mcde_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Linus Walleij <[email protected]>
  4. * Parts of this file were based on the MCDE driver by Marcus Lorentzon
  5. * (C) ST-Ericsson SA 2013
  6. */
  7. /**
  8. * DOC: ST-Ericsson MCDE Driver
  9. *
  10. * The MCDE (short for multi-channel display engine) is a graphics
  11. * controller found in the Ux500 chipsets, such as NovaThor U8500.
  12. * It was initially conceptualized by ST Microelectronics for the
  13. * successor of the Nomadik line, STn8500 but productified in the
  14. * ST-Ericsson U8500 where is was used for mass-market deployments
  15. * in Android phones from Samsung and Sony Ericsson.
  16. *
  17. * It can do 1080p30 on SDTV CCIR656, DPI-2, DBI-2 or DSI for
  18. * panels with or without frame buffering and can convert most
  19. * input formats including most variants of RGB and YUV.
  20. *
  21. * The hardware has four display pipes, and the layout is a little
  22. * bit like this::
  23. *
  24. * Memory -> Overlay -> Channel -> FIFO -> 8 formatters -> DSI/DPI
  25. * External 0..5 0..3 A,B, 6 x DSI bridge
  26. * source 0..9 C0,C1 2 x DPI
  27. *
  28. * FIFOs A and B are for LCD and HDMI while FIFO CO/C1 are for
  29. * panels with embedded buffer.
  30. * 6 of the formatters are for DSI, 3 pairs for VID/CMD respectively.
  31. * 2 of the formatters are for DPI.
  32. *
  33. * Behind the formatters are the DSI or DPI ports that route to
  34. * the external pins of the chip. As there are 3 DSI ports and one
  35. * DPI port, it is possible to configure up to 4 display pipelines
  36. * (effectively using channels 0..3) for concurrent use.
  37. *
  38. * In the current DRM/KMS setup, we use one external source, one overlay,
  39. * one FIFO and one formatter which we connect to the simple DMA framebuffer
  40. * helpers. We then provide a bridge to the DSI port, and on the DSI port
  41. * bridge we connect hang a panel bridge or other bridge. This may be subject
  42. * to change as we exploit more of the hardware capabilities.
  43. *
  44. * TODO:
  45. *
  46. * - Enabled damaged rectangles using drm_plane_enable_fb_damage_clips()
  47. * so we can selectively just transmit the damaged area to a
  48. * command-only display.
  49. * - Enable mixing of more planes, possibly at the cost of moving away
  50. * from using the simple framebuffer pipeline.
  51. * - Enable output to bridges such as the AV8100 HDMI encoder from
  52. * the DSI bridge.
  53. */
  54. #include <linux/clk.h>
  55. #include <linux/component.h>
  56. #include <linux/dma-buf.h>
  57. #include <linux/irq.h>
  58. #include <linux/io.h>
  59. #include <linux/module.h>
  60. #include <linux/of_platform.h>
  61. #include <linux/platform_device.h>
  62. #include <linux/regulator/consumer.h>
  63. #include <linux/slab.h>
  64. #include <linux/delay.h>
  65. #include <drm/drm_atomic_helper.h>
  66. #include <drm/drm_bridge.h>
  67. #include <drm/drm_drv.h>
  68. #include <drm/drm_fb_dma_helper.h>
  69. #include <drm/drm_fb_helper.h>
  70. #include <drm/drm_gem.h>
  71. #include <drm/drm_gem_dma_helper.h>
  72. #include <drm/drm_gem_framebuffer_helper.h>
  73. #include <drm/drm_managed.h>
  74. #include <drm/drm_of.h>
  75. #include <drm/drm_probe_helper.h>
  76. #include <drm/drm_panel.h>
  77. #include <drm/drm_vblank.h>
  78. #include "mcde_drm.h"
  79. #define DRIVER_DESC "DRM module for MCDE"
  80. #define MCDE_PID 0x000001FC
  81. #define MCDE_PID_METALFIX_VERSION_SHIFT 0
  82. #define MCDE_PID_METALFIX_VERSION_MASK 0x000000FF
  83. #define MCDE_PID_DEVELOPMENT_VERSION_SHIFT 8
  84. #define MCDE_PID_DEVELOPMENT_VERSION_MASK 0x0000FF00
  85. #define MCDE_PID_MINOR_VERSION_SHIFT 16
  86. #define MCDE_PID_MINOR_VERSION_MASK 0x00FF0000
  87. #define MCDE_PID_MAJOR_VERSION_SHIFT 24
  88. #define MCDE_PID_MAJOR_VERSION_MASK 0xFF000000
  89. static const struct drm_mode_config_funcs mcde_mode_config_funcs = {
  90. .fb_create = drm_gem_fb_create_with_dirty,
  91. .atomic_check = drm_atomic_helper_check,
  92. .atomic_commit = drm_atomic_helper_commit,
  93. };
  94. static const struct drm_mode_config_helper_funcs mcde_mode_config_helpers = {
  95. /*
  96. * Using this function is necessary to commit atomic updates
  97. * that need the CRTC to be enabled before a commit, as is
  98. * the case with e.g. DSI displays.
  99. */
  100. .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
  101. };
  102. static irqreturn_t mcde_irq(int irq, void *data)
  103. {
  104. struct mcde *mcde = data;
  105. u32 val;
  106. val = readl(mcde->regs + MCDE_MISERR);
  107. mcde_display_irq(mcde);
  108. if (val)
  109. dev_info(mcde->dev, "some error IRQ\n");
  110. writel(val, mcde->regs + MCDE_RISERR);
  111. return IRQ_HANDLED;
  112. }
  113. static int mcde_modeset_init(struct drm_device *drm)
  114. {
  115. struct drm_mode_config *mode_config;
  116. struct mcde *mcde = to_mcde(drm);
  117. int ret;
  118. /*
  119. * If no other bridge was found, check if we have a DPI panel or
  120. * any other bridge connected directly to the MCDE DPI output.
  121. * If a DSI bridge is found, DSI will take precedence.
  122. *
  123. * TODO: more elaborate bridge selection if we have more than one
  124. * thing attached to the system.
  125. */
  126. if (!mcde->bridge) {
  127. struct drm_panel *panel;
  128. struct drm_bridge *bridge;
  129. ret = drm_of_find_panel_or_bridge(drm->dev->of_node,
  130. 0, 0, &panel, &bridge);
  131. if (ret) {
  132. dev_err(drm->dev,
  133. "Could not locate any output bridge or panel\n");
  134. return ret;
  135. }
  136. if (panel) {
  137. bridge = drm_panel_bridge_add_typed(panel,
  138. DRM_MODE_CONNECTOR_DPI);
  139. if (IS_ERR(bridge)) {
  140. dev_err(drm->dev,
  141. "Could not connect panel bridge\n");
  142. return PTR_ERR(bridge);
  143. }
  144. }
  145. mcde->dpi_output = true;
  146. mcde->bridge = bridge;
  147. mcde->flow_mode = MCDE_DPI_FORMATTER_FLOW;
  148. }
  149. mode_config = &drm->mode_config;
  150. mode_config->funcs = &mcde_mode_config_funcs;
  151. mode_config->helper_private = &mcde_mode_config_helpers;
  152. /* This hardware can do 1080p */
  153. mode_config->min_width = 1;
  154. mode_config->max_width = 1920;
  155. mode_config->min_height = 1;
  156. mode_config->max_height = 1080;
  157. ret = drm_vblank_init(drm, 1);
  158. if (ret) {
  159. dev_err(drm->dev, "failed to init vblank\n");
  160. return ret;
  161. }
  162. ret = mcde_display_init(drm);
  163. if (ret) {
  164. dev_err(drm->dev, "failed to init display\n");
  165. return ret;
  166. }
  167. /* Attach the bridge. */
  168. ret = drm_simple_display_pipe_attach_bridge(&mcde->pipe,
  169. mcde->bridge);
  170. if (ret) {
  171. dev_err(drm->dev, "failed to attach display output bridge\n");
  172. return ret;
  173. }
  174. drm_mode_config_reset(drm);
  175. drm_kms_helper_poll_init(drm);
  176. return 0;
  177. }
  178. DEFINE_DRM_GEM_DMA_FOPS(drm_fops);
  179. static const struct drm_driver mcde_drm_driver = {
  180. .driver_features =
  181. DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  182. .lastclose = drm_fb_helper_lastclose,
  183. .ioctls = NULL,
  184. .fops = &drm_fops,
  185. .name = "mcde",
  186. .desc = DRIVER_DESC,
  187. .date = "20180529",
  188. .major = 1,
  189. .minor = 0,
  190. .patchlevel = 0,
  191. DRM_GEM_DMA_DRIVER_OPS,
  192. };
  193. static int mcde_drm_bind(struct device *dev)
  194. {
  195. struct drm_device *drm = dev_get_drvdata(dev);
  196. int ret;
  197. ret = drmm_mode_config_init(drm);
  198. if (ret)
  199. return ret;
  200. ret = component_bind_all(drm->dev, drm);
  201. if (ret) {
  202. dev_err(dev, "can't bind component devices\n");
  203. return ret;
  204. }
  205. ret = mcde_modeset_init(drm);
  206. if (ret)
  207. goto unbind;
  208. ret = drm_dev_register(drm, 0);
  209. if (ret < 0)
  210. goto unbind;
  211. drm_fbdev_generic_setup(drm, 32);
  212. return 0;
  213. unbind:
  214. component_unbind_all(drm->dev, drm);
  215. return ret;
  216. }
  217. static void mcde_drm_unbind(struct device *dev)
  218. {
  219. struct drm_device *drm = dev_get_drvdata(dev);
  220. drm_dev_unregister(drm);
  221. drm_atomic_helper_shutdown(drm);
  222. component_unbind_all(drm->dev, drm);
  223. }
  224. static const struct component_master_ops mcde_drm_comp_ops = {
  225. .bind = mcde_drm_bind,
  226. .unbind = mcde_drm_unbind,
  227. };
  228. static struct platform_driver *const mcde_component_drivers[] = {
  229. &mcde_dsi_driver,
  230. };
  231. static int mcde_probe(struct platform_device *pdev)
  232. {
  233. struct device *dev = &pdev->dev;
  234. struct drm_device *drm;
  235. struct mcde *mcde;
  236. struct component_match *match = NULL;
  237. u32 pid;
  238. int irq;
  239. int ret;
  240. int i;
  241. mcde = devm_drm_dev_alloc(dev, &mcde_drm_driver, struct mcde, drm);
  242. if (IS_ERR(mcde))
  243. return PTR_ERR(mcde);
  244. drm = &mcde->drm;
  245. mcde->dev = dev;
  246. platform_set_drvdata(pdev, drm);
  247. /* First obtain and turn on the main power */
  248. mcde->epod = devm_regulator_get(dev, "epod");
  249. if (IS_ERR(mcde->epod)) {
  250. ret = PTR_ERR(mcde->epod);
  251. dev_err(dev, "can't get EPOD regulator\n");
  252. return ret;
  253. }
  254. ret = regulator_enable(mcde->epod);
  255. if (ret) {
  256. dev_err(dev, "can't enable EPOD regulator\n");
  257. return ret;
  258. }
  259. mcde->vana = devm_regulator_get(dev, "vana");
  260. if (IS_ERR(mcde->vana)) {
  261. ret = PTR_ERR(mcde->vana);
  262. dev_err(dev, "can't get VANA regulator\n");
  263. goto regulator_epod_off;
  264. }
  265. ret = regulator_enable(mcde->vana);
  266. if (ret) {
  267. dev_err(dev, "can't enable VANA regulator\n");
  268. goto regulator_epod_off;
  269. }
  270. /*
  271. * The vendor code uses ESRAM (onchip RAM) and need to activate
  272. * the v-esram34 regulator, but we don't use that yet
  273. */
  274. /* Clock the silicon so we can access the registers */
  275. mcde->mcde_clk = devm_clk_get(dev, "mcde");
  276. if (IS_ERR(mcde->mcde_clk)) {
  277. dev_err(dev, "unable to get MCDE main clock\n");
  278. ret = PTR_ERR(mcde->mcde_clk);
  279. goto regulator_off;
  280. }
  281. ret = clk_prepare_enable(mcde->mcde_clk);
  282. if (ret) {
  283. dev_err(dev, "failed to enable MCDE main clock\n");
  284. goto regulator_off;
  285. }
  286. dev_info(dev, "MCDE clk rate %lu Hz\n", clk_get_rate(mcde->mcde_clk));
  287. mcde->lcd_clk = devm_clk_get(dev, "lcd");
  288. if (IS_ERR(mcde->lcd_clk)) {
  289. dev_err(dev, "unable to get LCD clock\n");
  290. ret = PTR_ERR(mcde->lcd_clk);
  291. goto clk_disable;
  292. }
  293. mcde->hdmi_clk = devm_clk_get(dev, "hdmi");
  294. if (IS_ERR(mcde->hdmi_clk)) {
  295. dev_err(dev, "unable to get HDMI clock\n");
  296. ret = PTR_ERR(mcde->hdmi_clk);
  297. goto clk_disable;
  298. }
  299. mcde->regs = devm_platform_ioremap_resource(pdev, 0);
  300. if (IS_ERR(mcde->regs)) {
  301. dev_err(dev, "no MCDE regs\n");
  302. ret = -EINVAL;
  303. goto clk_disable;
  304. }
  305. irq = platform_get_irq(pdev, 0);
  306. if (irq < 0) {
  307. ret = irq;
  308. goto clk_disable;
  309. }
  310. ret = devm_request_irq(dev, irq, mcde_irq, 0, "mcde", mcde);
  311. if (ret) {
  312. dev_err(dev, "failed to request irq %d\n", ret);
  313. goto clk_disable;
  314. }
  315. /*
  316. * Check hardware revision, we only support U8500v2 version
  317. * as this was the only version used for mass market deployment,
  318. * but surely you can add more versions if you have them and
  319. * need them.
  320. */
  321. pid = readl(mcde->regs + MCDE_PID);
  322. dev_info(dev, "found MCDE HW revision %d.%d (dev %d, metal fix %d)\n",
  323. (pid & MCDE_PID_MAJOR_VERSION_MASK)
  324. >> MCDE_PID_MAJOR_VERSION_SHIFT,
  325. (pid & MCDE_PID_MINOR_VERSION_MASK)
  326. >> MCDE_PID_MINOR_VERSION_SHIFT,
  327. (pid & MCDE_PID_DEVELOPMENT_VERSION_MASK)
  328. >> MCDE_PID_DEVELOPMENT_VERSION_SHIFT,
  329. (pid & MCDE_PID_METALFIX_VERSION_MASK)
  330. >> MCDE_PID_METALFIX_VERSION_SHIFT);
  331. if (pid != 0x03000800) {
  332. dev_err(dev, "unsupported hardware revision\n");
  333. ret = -ENODEV;
  334. goto clk_disable;
  335. }
  336. /* Disable and clear any pending interrupts */
  337. mcde_display_disable_irqs(mcde);
  338. writel(0, mcde->regs + MCDE_IMSCERR);
  339. writel(0xFFFFFFFF, mcde->regs + MCDE_RISERR);
  340. /* Spawn child devices for the DSI ports */
  341. devm_of_platform_populate(dev);
  342. /* Create something that will match the subdrivers when we bind */
  343. for (i = 0; i < ARRAY_SIZE(mcde_component_drivers); i++) {
  344. struct device_driver *drv = &mcde_component_drivers[i]->driver;
  345. struct device *p = NULL, *d;
  346. while ((d = platform_find_device_by_driver(p, drv))) {
  347. put_device(p);
  348. component_match_add(dev, &match, component_compare_dev, d);
  349. p = d;
  350. }
  351. put_device(p);
  352. }
  353. if (!match) {
  354. dev_err(dev, "no matching components\n");
  355. ret = -ENODEV;
  356. goto clk_disable;
  357. }
  358. if (IS_ERR(match)) {
  359. dev_err(dev, "could not create component match\n");
  360. ret = PTR_ERR(match);
  361. goto clk_disable;
  362. }
  363. /*
  364. * Perform an invasive reset of the MCDE and all blocks by
  365. * cutting the power to the subsystem, then bring it back up
  366. * later when we enable the display as a result of
  367. * component_master_add_with_match().
  368. */
  369. ret = regulator_disable(mcde->epod);
  370. if (ret) {
  371. dev_err(dev, "can't disable EPOD regulator\n");
  372. return ret;
  373. }
  374. /* Wait 50 ms so we are sure we cut the power */
  375. usleep_range(50000, 70000);
  376. ret = component_master_add_with_match(&pdev->dev, &mcde_drm_comp_ops,
  377. match);
  378. if (ret) {
  379. dev_err(dev, "failed to add component master\n");
  380. /*
  381. * The EPOD regulator is already disabled at this point so some
  382. * special errorpath code is needed
  383. */
  384. clk_disable_unprepare(mcde->mcde_clk);
  385. regulator_disable(mcde->vana);
  386. return ret;
  387. }
  388. return 0;
  389. clk_disable:
  390. clk_disable_unprepare(mcde->mcde_clk);
  391. regulator_off:
  392. regulator_disable(mcde->vana);
  393. regulator_epod_off:
  394. regulator_disable(mcde->epod);
  395. return ret;
  396. }
  397. static int mcde_remove(struct platform_device *pdev)
  398. {
  399. struct drm_device *drm = platform_get_drvdata(pdev);
  400. struct mcde *mcde = to_mcde(drm);
  401. component_master_del(&pdev->dev, &mcde_drm_comp_ops);
  402. clk_disable_unprepare(mcde->mcde_clk);
  403. regulator_disable(mcde->vana);
  404. regulator_disable(mcde->epod);
  405. return 0;
  406. }
  407. static const struct of_device_id mcde_of_match[] = {
  408. {
  409. .compatible = "ste,mcde",
  410. },
  411. {},
  412. };
  413. static struct platform_driver mcde_driver = {
  414. .driver = {
  415. .name = "mcde",
  416. .of_match_table = of_match_ptr(mcde_of_match),
  417. },
  418. .probe = mcde_probe,
  419. .remove = mcde_remove,
  420. };
  421. static struct platform_driver *const component_drivers[] = {
  422. &mcde_dsi_driver,
  423. };
  424. static int __init mcde_drm_register(void)
  425. {
  426. int ret;
  427. if (drm_firmware_drivers_only())
  428. return -ENODEV;
  429. ret = platform_register_drivers(component_drivers,
  430. ARRAY_SIZE(component_drivers));
  431. if (ret)
  432. return ret;
  433. return platform_driver_register(&mcde_driver);
  434. }
  435. static void __exit mcde_drm_unregister(void)
  436. {
  437. platform_unregister_drivers(component_drivers,
  438. ARRAY_SIZE(component_drivers));
  439. platform_driver_unregister(&mcde_driver);
  440. }
  441. module_init(mcde_drm_register);
  442. module_exit(mcde_drm_unregister);
  443. MODULE_ALIAS("platform:mcde-drm");
  444. MODULE_DESCRIPTION(DRIVER_DESC);
  445. MODULE_AUTHOR("Linus Walleij <[email protected]>");
  446. MODULE_LICENSE("GPL");