malidp_drv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  3. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * ARM Mali DP500/DP550/DP650 KMS/DRM driver
  11. */
  12. #include <linux/module.h>
  13. #include <linux/clk.h>
  14. #include <linux/component.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of_graph.h>
  17. #include <linux/of_reserved_mem.h>
  18. #include <linux/pm_runtime.h>
  19. #include <drm/drmP.h>
  20. #include <drm/drm_atomic.h>
  21. #include <drm/drm_atomic_helper.h>
  22. #include <drm/drm_crtc.h>
  23. #include <drm/drm_crtc_helper.h>
  24. #include <drm/drm_fb_helper.h>
  25. #include <drm/drm_fb_cma_helper.h>
  26. #include <drm/drm_gem_cma_helper.h>
  27. #include <drm/drm_gem_framebuffer_helper.h>
  28. #include <drm/drm_modeset_helper.h>
  29. #include <drm/drm_of.h>
  30. #include "malidp_drv.h"
  31. #include "malidp_regs.h"
  32. #include "malidp_hw.h"
  33. #define MALIDP_CONF_VALID_TIMEOUT 250
  34. static void malidp_write_gamma_table(struct malidp_hw_device *hwdev,
  35. u32 data[MALIDP_COEFFTAB_NUM_COEFFS])
  36. {
  37. int i;
  38. /* Update all channels with a single gamma curve. */
  39. const u32 gamma_write_mask = GENMASK(18, 16);
  40. /*
  41. * Always write an entire table, so the address field in
  42. * DE_COEFFTAB_ADDR is 0 and we can use the gamma_write_mask bitmask
  43. * directly.
  44. */
  45. malidp_hw_write(hwdev, gamma_write_mask,
  46. hwdev->hw->map.coeffs_base + MALIDP_COEF_TABLE_ADDR);
  47. for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i)
  48. malidp_hw_write(hwdev, data[i],
  49. hwdev->hw->map.coeffs_base +
  50. MALIDP_COEF_TABLE_DATA);
  51. }
  52. static void malidp_atomic_commit_update_gamma(struct drm_crtc *crtc,
  53. struct drm_crtc_state *old_state)
  54. {
  55. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  56. struct malidp_hw_device *hwdev = malidp->dev;
  57. if (!crtc->state->color_mgmt_changed)
  58. return;
  59. if (!crtc->state->gamma_lut) {
  60. malidp_hw_clearbits(hwdev,
  61. MALIDP_DISP_FUNC_GAMMA,
  62. MALIDP_DE_DISPLAY_FUNC);
  63. } else {
  64. struct malidp_crtc_state *mc =
  65. to_malidp_crtc_state(crtc->state);
  66. if (!old_state->gamma_lut || (crtc->state->gamma_lut->base.id !=
  67. old_state->gamma_lut->base.id))
  68. malidp_write_gamma_table(hwdev, mc->gamma_coeffs);
  69. malidp_hw_setbits(hwdev, MALIDP_DISP_FUNC_GAMMA,
  70. MALIDP_DE_DISPLAY_FUNC);
  71. }
  72. }
  73. static
  74. void malidp_atomic_commit_update_coloradj(struct drm_crtc *crtc,
  75. struct drm_crtc_state *old_state)
  76. {
  77. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  78. struct malidp_hw_device *hwdev = malidp->dev;
  79. int i;
  80. if (!crtc->state->color_mgmt_changed)
  81. return;
  82. if (!crtc->state->ctm) {
  83. malidp_hw_clearbits(hwdev, MALIDP_DISP_FUNC_CADJ,
  84. MALIDP_DE_DISPLAY_FUNC);
  85. } else {
  86. struct malidp_crtc_state *mc =
  87. to_malidp_crtc_state(crtc->state);
  88. if (!old_state->ctm || (crtc->state->ctm->base.id !=
  89. old_state->ctm->base.id))
  90. for (i = 0; i < MALIDP_COLORADJ_NUM_COEFFS; ++i)
  91. malidp_hw_write(hwdev,
  92. mc->coloradj_coeffs[i],
  93. hwdev->hw->map.coeffs_base +
  94. MALIDP_COLOR_ADJ_COEF + 4 * i);
  95. malidp_hw_setbits(hwdev, MALIDP_DISP_FUNC_CADJ,
  96. MALIDP_DE_DISPLAY_FUNC);
  97. }
  98. }
  99. static void malidp_atomic_commit_se_config(struct drm_crtc *crtc,
  100. struct drm_crtc_state *old_state)
  101. {
  102. struct malidp_crtc_state *cs = to_malidp_crtc_state(crtc->state);
  103. struct malidp_crtc_state *old_cs = to_malidp_crtc_state(old_state);
  104. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  105. struct malidp_hw_device *hwdev = malidp->dev;
  106. struct malidp_se_config *s = &cs->scaler_config;
  107. struct malidp_se_config *old_s = &old_cs->scaler_config;
  108. u32 se_control = hwdev->hw->map.se_base +
  109. ((hwdev->hw->map.features & MALIDP_REGMAP_HAS_CLEARIRQ) ?
  110. 0x10 : 0xC);
  111. u32 layer_control = se_control + MALIDP_SE_LAYER_CONTROL;
  112. u32 scr = se_control + MALIDP_SE_SCALING_CONTROL;
  113. u32 val;
  114. /* Set SE_CONTROL */
  115. if (!s->scale_enable) {
  116. val = malidp_hw_read(hwdev, se_control);
  117. val &= ~MALIDP_SE_SCALING_EN;
  118. malidp_hw_write(hwdev, val, se_control);
  119. return;
  120. }
  121. hwdev->hw->se_set_scaling_coeffs(hwdev, s, old_s);
  122. val = malidp_hw_read(hwdev, se_control);
  123. val |= MALIDP_SE_SCALING_EN | MALIDP_SE_ALPHA_EN;
  124. val &= ~MALIDP_SE_ENH(MALIDP_SE_ENH_MASK);
  125. val |= s->enhancer_enable ? MALIDP_SE_ENH(3) : 0;
  126. val |= MALIDP_SE_RGBO_IF_EN;
  127. malidp_hw_write(hwdev, val, se_control);
  128. /* Set IN_SIZE & OUT_SIZE. */
  129. val = MALIDP_SE_SET_V_SIZE(s->input_h) |
  130. MALIDP_SE_SET_H_SIZE(s->input_w);
  131. malidp_hw_write(hwdev, val, layer_control + MALIDP_SE_L0_IN_SIZE);
  132. val = MALIDP_SE_SET_V_SIZE(s->output_h) |
  133. MALIDP_SE_SET_H_SIZE(s->output_w);
  134. malidp_hw_write(hwdev, val, layer_control + MALIDP_SE_L0_OUT_SIZE);
  135. /* Set phase regs. */
  136. malidp_hw_write(hwdev, s->h_init_phase, scr + MALIDP_SE_H_INIT_PH);
  137. malidp_hw_write(hwdev, s->h_delta_phase, scr + MALIDP_SE_H_DELTA_PH);
  138. malidp_hw_write(hwdev, s->v_init_phase, scr + MALIDP_SE_V_INIT_PH);
  139. malidp_hw_write(hwdev, s->v_delta_phase, scr + MALIDP_SE_V_DELTA_PH);
  140. }
  141. /*
  142. * set the "config valid" bit and wait until the hardware acts on it
  143. */
  144. static int malidp_set_and_wait_config_valid(struct drm_device *drm)
  145. {
  146. struct malidp_drm *malidp = drm->dev_private;
  147. struct malidp_hw_device *hwdev = malidp->dev;
  148. int ret;
  149. atomic_set(&malidp->config_valid, 0);
  150. hwdev->hw->set_config_valid(hwdev);
  151. /* don't wait for config_valid flag if we are in config mode */
  152. if (hwdev->hw->in_config_mode(hwdev))
  153. return 0;
  154. ret = wait_event_interruptible_timeout(malidp->wq,
  155. atomic_read(&malidp->config_valid) == 1,
  156. msecs_to_jiffies(MALIDP_CONF_VALID_TIMEOUT));
  157. return (ret > 0) ? 0 : -ETIMEDOUT;
  158. }
  159. static void malidp_atomic_commit_hw_done(struct drm_atomic_state *state)
  160. {
  161. struct drm_pending_vblank_event *event;
  162. struct drm_device *drm = state->dev;
  163. struct malidp_drm *malidp = drm->dev_private;
  164. if (malidp->crtc.enabled) {
  165. /* only set config_valid if the CRTC is enabled */
  166. if (malidp_set_and_wait_config_valid(drm))
  167. DRM_DEBUG_DRIVER("timed out waiting for updated configuration\n");
  168. }
  169. event = malidp->crtc.state->event;
  170. if (event) {
  171. malidp->crtc.state->event = NULL;
  172. spin_lock_irq(&drm->event_lock);
  173. if (drm_crtc_vblank_get(&malidp->crtc) == 0)
  174. drm_crtc_arm_vblank_event(&malidp->crtc, event);
  175. else
  176. drm_crtc_send_vblank_event(&malidp->crtc, event);
  177. spin_unlock_irq(&drm->event_lock);
  178. }
  179. drm_atomic_helper_commit_hw_done(state);
  180. }
  181. static void malidp_atomic_commit_tail(struct drm_atomic_state *state)
  182. {
  183. struct drm_device *drm = state->dev;
  184. struct drm_crtc *crtc;
  185. struct drm_crtc_state *old_crtc_state;
  186. int i;
  187. pm_runtime_get_sync(drm->dev);
  188. drm_atomic_helper_commit_modeset_disables(drm, state);
  189. for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
  190. malidp_atomic_commit_update_gamma(crtc, old_crtc_state);
  191. malidp_atomic_commit_update_coloradj(crtc, old_crtc_state);
  192. malidp_atomic_commit_se_config(crtc, old_crtc_state);
  193. }
  194. drm_atomic_helper_commit_planes(drm, state, 0);
  195. drm_atomic_helper_commit_modeset_enables(drm, state);
  196. malidp_atomic_commit_hw_done(state);
  197. drm_atomic_helper_wait_for_vblanks(drm, state);
  198. pm_runtime_put(drm->dev);
  199. drm_atomic_helper_cleanup_planes(drm, state);
  200. }
  201. static const struct drm_mode_config_helper_funcs malidp_mode_config_helpers = {
  202. .atomic_commit_tail = malidp_atomic_commit_tail,
  203. };
  204. static const struct drm_mode_config_funcs malidp_mode_config_funcs = {
  205. .fb_create = drm_gem_fb_create,
  206. .output_poll_changed = drm_fb_helper_output_poll_changed,
  207. .atomic_check = drm_atomic_helper_check,
  208. .atomic_commit = drm_atomic_helper_commit,
  209. };
  210. static int malidp_init(struct drm_device *drm)
  211. {
  212. int ret;
  213. struct malidp_drm *malidp = drm->dev_private;
  214. struct malidp_hw_device *hwdev = malidp->dev;
  215. drm_mode_config_init(drm);
  216. drm->mode_config.min_width = hwdev->min_line_size;
  217. drm->mode_config.min_height = hwdev->min_line_size;
  218. drm->mode_config.max_width = hwdev->max_line_size;
  219. drm->mode_config.max_height = hwdev->max_line_size;
  220. drm->mode_config.funcs = &malidp_mode_config_funcs;
  221. drm->mode_config.helper_private = &malidp_mode_config_helpers;
  222. ret = malidp_crtc_init(drm);
  223. if (ret) {
  224. drm_mode_config_cleanup(drm);
  225. return ret;
  226. }
  227. return 0;
  228. }
  229. static void malidp_fini(struct drm_device *drm)
  230. {
  231. malidp_de_planes_destroy(drm);
  232. drm_mode_config_cleanup(drm);
  233. }
  234. static int malidp_irq_init(struct platform_device *pdev)
  235. {
  236. int irq_de, irq_se, ret = 0;
  237. struct drm_device *drm = dev_get_drvdata(&pdev->dev);
  238. /* fetch the interrupts from DT */
  239. irq_de = platform_get_irq_byname(pdev, "DE");
  240. if (irq_de < 0) {
  241. DRM_ERROR("no 'DE' IRQ specified!\n");
  242. return irq_de;
  243. }
  244. irq_se = platform_get_irq_byname(pdev, "SE");
  245. if (irq_se < 0) {
  246. DRM_ERROR("no 'SE' IRQ specified!\n");
  247. return irq_se;
  248. }
  249. ret = malidp_de_irq_init(drm, irq_de);
  250. if (ret)
  251. return ret;
  252. ret = malidp_se_irq_init(drm, irq_se);
  253. if (ret) {
  254. malidp_de_irq_fini(drm);
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. DEFINE_DRM_GEM_CMA_FOPS(fops);
  260. static struct drm_driver malidp_driver = {
  261. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC |
  262. DRIVER_PRIME,
  263. .lastclose = drm_fb_helper_lastclose,
  264. .gem_free_object_unlocked = drm_gem_cma_free_object,
  265. .gem_vm_ops = &drm_gem_cma_vm_ops,
  266. .dumb_create = drm_gem_cma_dumb_create,
  267. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  268. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  269. .gem_prime_export = drm_gem_prime_export,
  270. .gem_prime_import = drm_gem_prime_import,
  271. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  272. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  273. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  274. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  275. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  276. .fops = &fops,
  277. .name = "mali-dp",
  278. .desc = "ARM Mali Display Processor driver",
  279. .date = "20160106",
  280. .major = 1,
  281. .minor = 0,
  282. };
  283. static const struct of_device_id malidp_drm_of_match[] = {
  284. {
  285. .compatible = "arm,mali-dp500",
  286. .data = &malidp_device[MALIDP_500]
  287. },
  288. {
  289. .compatible = "arm,mali-dp550",
  290. .data = &malidp_device[MALIDP_550]
  291. },
  292. {
  293. .compatible = "arm,mali-dp650",
  294. .data = &malidp_device[MALIDP_650]
  295. },
  296. {},
  297. };
  298. MODULE_DEVICE_TABLE(of, malidp_drm_of_match);
  299. static bool malidp_is_compatible_hw_id(struct malidp_hw_device *hwdev,
  300. const struct of_device_id *dev_id)
  301. {
  302. u32 core_id;
  303. const char *compatstr_dp500 = "arm,mali-dp500";
  304. bool is_dp500;
  305. bool dt_is_dp500;
  306. /*
  307. * The DP500 CORE_ID register is in a different location, so check it
  308. * first. If the product id field matches, then this is DP500, otherwise
  309. * check the DP550/650 CORE_ID register.
  310. */
  311. core_id = malidp_hw_read(hwdev, MALIDP500_DC_BASE + MALIDP_DE_CORE_ID);
  312. /* Offset 0x18 will never read 0x500 on products other than DP500. */
  313. is_dp500 = (MALIDP_PRODUCT_ID(core_id) == 0x500);
  314. dt_is_dp500 = strnstr(dev_id->compatible, compatstr_dp500,
  315. sizeof(dev_id->compatible)) != NULL;
  316. if (is_dp500 != dt_is_dp500) {
  317. DRM_ERROR("Device-tree expects %s, but hardware %s DP500.\n",
  318. dev_id->compatible, is_dp500 ? "is" : "is not");
  319. return false;
  320. } else if (!dt_is_dp500) {
  321. u16 product_id;
  322. char buf[32];
  323. core_id = malidp_hw_read(hwdev,
  324. MALIDP550_DC_BASE + MALIDP_DE_CORE_ID);
  325. product_id = MALIDP_PRODUCT_ID(core_id);
  326. snprintf(buf, sizeof(buf), "arm,mali-dp%X", product_id);
  327. if (!strnstr(dev_id->compatible, buf,
  328. sizeof(dev_id->compatible))) {
  329. DRM_ERROR("Device-tree expects %s, but hardware is DP%03X.\n",
  330. dev_id->compatible, product_id);
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. static bool malidp_has_sufficient_address_space(const struct resource *res,
  337. const struct of_device_id *dev_id)
  338. {
  339. resource_size_t res_size = resource_size(res);
  340. const char *compatstr_dp500 = "arm,mali-dp500";
  341. if (!strnstr(dev_id->compatible, compatstr_dp500,
  342. sizeof(dev_id->compatible)))
  343. return res_size >= MALIDP550_ADDR_SPACE_SIZE;
  344. else if (res_size < MALIDP500_ADDR_SPACE_SIZE)
  345. return false;
  346. return true;
  347. }
  348. static ssize_t core_id_show(struct device *dev, struct device_attribute *attr,
  349. char *buf)
  350. {
  351. struct drm_device *drm = dev_get_drvdata(dev);
  352. struct malidp_drm *malidp = drm->dev_private;
  353. return snprintf(buf, PAGE_SIZE, "%08x\n", malidp->core_id);
  354. }
  355. DEVICE_ATTR_RO(core_id);
  356. static int malidp_init_sysfs(struct device *dev)
  357. {
  358. int ret = device_create_file(dev, &dev_attr_core_id);
  359. if (ret)
  360. DRM_ERROR("failed to create device file for core_id\n");
  361. return ret;
  362. }
  363. static void malidp_fini_sysfs(struct device *dev)
  364. {
  365. device_remove_file(dev, &dev_attr_core_id);
  366. }
  367. #define MAX_OUTPUT_CHANNELS 3
  368. static int malidp_runtime_pm_suspend(struct device *dev)
  369. {
  370. struct drm_device *drm = dev_get_drvdata(dev);
  371. struct malidp_drm *malidp = drm->dev_private;
  372. struct malidp_hw_device *hwdev = malidp->dev;
  373. /* we can only suspend if the hardware is in config mode */
  374. WARN_ON(!hwdev->hw->in_config_mode(hwdev));
  375. hwdev->pm_suspended = true;
  376. clk_disable_unprepare(hwdev->mclk);
  377. clk_disable_unprepare(hwdev->aclk);
  378. clk_disable_unprepare(hwdev->pclk);
  379. return 0;
  380. }
  381. static int malidp_runtime_pm_resume(struct device *dev)
  382. {
  383. struct drm_device *drm = dev_get_drvdata(dev);
  384. struct malidp_drm *malidp = drm->dev_private;
  385. struct malidp_hw_device *hwdev = malidp->dev;
  386. clk_prepare_enable(hwdev->pclk);
  387. clk_prepare_enable(hwdev->aclk);
  388. clk_prepare_enable(hwdev->mclk);
  389. hwdev->pm_suspended = false;
  390. return 0;
  391. }
  392. static int malidp_bind(struct device *dev)
  393. {
  394. struct resource *res;
  395. struct drm_device *drm;
  396. struct malidp_drm *malidp;
  397. struct malidp_hw_device *hwdev;
  398. struct platform_device *pdev = to_platform_device(dev);
  399. struct of_device_id const *dev_id;
  400. /* number of lines for the R, G and B output */
  401. u8 output_width[MAX_OUTPUT_CHANNELS];
  402. int ret = 0, i;
  403. u32 version, out_depth = 0;
  404. malidp = devm_kzalloc(dev, sizeof(*malidp), GFP_KERNEL);
  405. if (!malidp)
  406. return -ENOMEM;
  407. hwdev = devm_kzalloc(dev, sizeof(*hwdev), GFP_KERNEL);
  408. if (!hwdev)
  409. return -ENOMEM;
  410. hwdev->hw = (struct malidp_hw *)of_device_get_match_data(dev);
  411. malidp->dev = hwdev;
  412. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  413. hwdev->regs = devm_ioremap_resource(dev, res);
  414. if (IS_ERR(hwdev->regs))
  415. return PTR_ERR(hwdev->regs);
  416. hwdev->pclk = devm_clk_get(dev, "pclk");
  417. if (IS_ERR(hwdev->pclk))
  418. return PTR_ERR(hwdev->pclk);
  419. hwdev->aclk = devm_clk_get(dev, "aclk");
  420. if (IS_ERR(hwdev->aclk))
  421. return PTR_ERR(hwdev->aclk);
  422. hwdev->mclk = devm_clk_get(dev, "mclk");
  423. if (IS_ERR(hwdev->mclk))
  424. return PTR_ERR(hwdev->mclk);
  425. hwdev->pxlclk = devm_clk_get(dev, "pxlclk");
  426. if (IS_ERR(hwdev->pxlclk))
  427. return PTR_ERR(hwdev->pxlclk);
  428. /* Get the optional framebuffer memory resource */
  429. ret = of_reserved_mem_device_init(dev);
  430. if (ret && ret != -ENODEV)
  431. return ret;
  432. drm = drm_dev_alloc(&malidp_driver, dev);
  433. if (IS_ERR(drm)) {
  434. ret = PTR_ERR(drm);
  435. goto alloc_fail;
  436. }
  437. drm->dev_private = malidp;
  438. dev_set_drvdata(dev, drm);
  439. /* Enable power management */
  440. pm_runtime_enable(dev);
  441. /* Resume device to enable the clocks */
  442. if (pm_runtime_enabled(dev))
  443. pm_runtime_get_sync(dev);
  444. else
  445. malidp_runtime_pm_resume(dev);
  446. dev_id = of_match_device(malidp_drm_of_match, dev);
  447. if (!dev_id) {
  448. ret = -EINVAL;
  449. goto query_hw_fail;
  450. }
  451. if (!malidp_has_sufficient_address_space(res, dev_id)) {
  452. DRM_ERROR("Insufficient address space in device-tree.\n");
  453. ret = -EINVAL;
  454. goto query_hw_fail;
  455. }
  456. if (!malidp_is_compatible_hw_id(hwdev, dev_id)) {
  457. ret = -EINVAL;
  458. goto query_hw_fail;
  459. }
  460. ret = hwdev->hw->query_hw(hwdev);
  461. if (ret) {
  462. DRM_ERROR("Invalid HW configuration\n");
  463. goto query_hw_fail;
  464. }
  465. version = malidp_hw_read(hwdev, hwdev->hw->map.dc_base + MALIDP_DE_CORE_ID);
  466. DRM_INFO("found ARM Mali-DP%3x version r%dp%d\n", version >> 16,
  467. (version >> 12) & 0xf, (version >> 8) & 0xf);
  468. malidp->core_id = version;
  469. /* set the number of lines used for output of RGB data */
  470. ret = of_property_read_u8_array(dev->of_node,
  471. "arm,malidp-output-port-lines",
  472. output_width, MAX_OUTPUT_CHANNELS);
  473. if (ret)
  474. goto query_hw_fail;
  475. for (i = 0; i < MAX_OUTPUT_CHANNELS; i++)
  476. out_depth = (out_depth << 8) | (output_width[i] & 0xf);
  477. malidp_hw_write(hwdev, out_depth, hwdev->hw->map.out_depth_base);
  478. atomic_set(&malidp->config_valid, 0);
  479. init_waitqueue_head(&malidp->wq);
  480. ret = malidp_init(drm);
  481. if (ret < 0)
  482. goto query_hw_fail;
  483. ret = malidp_init_sysfs(dev);
  484. if (ret)
  485. goto init_fail;
  486. /* Set the CRTC's port so that the encoder component can find it */
  487. malidp->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
  488. ret = component_bind_all(dev, drm);
  489. if (ret) {
  490. DRM_ERROR("Failed to bind all components\n");
  491. goto bind_fail;
  492. }
  493. ret = malidp_irq_init(pdev);
  494. if (ret < 0)
  495. goto irq_init_fail;
  496. drm->irq_enabled = true;
  497. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  498. if (ret < 0) {
  499. DRM_ERROR("failed to initialise vblank\n");
  500. goto vblank_fail;
  501. }
  502. pm_runtime_put(dev);
  503. drm_mode_config_reset(drm);
  504. ret = drm_fb_cma_fbdev_init(drm, 32, 0);
  505. if (ret)
  506. goto fbdev_fail;
  507. drm_kms_helper_poll_init(drm);
  508. ret = drm_dev_register(drm, 0);
  509. if (ret)
  510. goto register_fail;
  511. return 0;
  512. register_fail:
  513. drm_fb_cma_fbdev_fini(drm);
  514. drm_kms_helper_poll_fini(drm);
  515. fbdev_fail:
  516. pm_runtime_get_sync(dev);
  517. vblank_fail:
  518. malidp_se_irq_fini(drm);
  519. malidp_de_irq_fini(drm);
  520. drm->irq_enabled = false;
  521. irq_init_fail:
  522. component_unbind_all(dev, drm);
  523. bind_fail:
  524. of_node_put(malidp->crtc.port);
  525. malidp->crtc.port = NULL;
  526. init_fail:
  527. malidp_fini_sysfs(dev);
  528. malidp_fini(drm);
  529. query_hw_fail:
  530. pm_runtime_put(dev);
  531. if (pm_runtime_enabled(dev))
  532. pm_runtime_disable(dev);
  533. else
  534. malidp_runtime_pm_suspend(dev);
  535. drm->dev_private = NULL;
  536. dev_set_drvdata(dev, NULL);
  537. drm_dev_put(drm);
  538. alloc_fail:
  539. of_reserved_mem_device_release(dev);
  540. return ret;
  541. }
  542. static void malidp_unbind(struct device *dev)
  543. {
  544. struct drm_device *drm = dev_get_drvdata(dev);
  545. struct malidp_drm *malidp = drm->dev_private;
  546. drm_dev_unregister(drm);
  547. drm_fb_cma_fbdev_fini(drm);
  548. drm_kms_helper_poll_fini(drm);
  549. pm_runtime_get_sync(dev);
  550. malidp_se_irq_fini(drm);
  551. malidp_de_irq_fini(drm);
  552. component_unbind_all(dev, drm);
  553. of_node_put(malidp->crtc.port);
  554. malidp->crtc.port = NULL;
  555. malidp_fini_sysfs(dev);
  556. malidp_fini(drm);
  557. pm_runtime_put(dev);
  558. if (pm_runtime_enabled(dev))
  559. pm_runtime_disable(dev);
  560. else
  561. malidp_runtime_pm_suspend(dev);
  562. drm->dev_private = NULL;
  563. dev_set_drvdata(dev, NULL);
  564. drm_dev_put(drm);
  565. of_reserved_mem_device_release(dev);
  566. }
  567. static const struct component_master_ops malidp_master_ops = {
  568. .bind = malidp_bind,
  569. .unbind = malidp_unbind,
  570. };
  571. static int malidp_compare_dev(struct device *dev, void *data)
  572. {
  573. struct device_node *np = data;
  574. return dev->of_node == np;
  575. }
  576. static int malidp_platform_probe(struct platform_device *pdev)
  577. {
  578. struct device_node *port;
  579. struct component_match *match = NULL;
  580. if (!pdev->dev.of_node)
  581. return -ENODEV;
  582. /* there is only one output port inside each device, find it */
  583. port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
  584. if (!port)
  585. return -ENODEV;
  586. drm_of_component_match_add(&pdev->dev, &match, malidp_compare_dev,
  587. port);
  588. of_node_put(port);
  589. return component_master_add_with_match(&pdev->dev, &malidp_master_ops,
  590. match);
  591. }
  592. static int malidp_platform_remove(struct platform_device *pdev)
  593. {
  594. component_master_del(&pdev->dev, &malidp_master_ops);
  595. return 0;
  596. }
  597. static int __maybe_unused malidp_pm_suspend(struct device *dev)
  598. {
  599. struct drm_device *drm = dev_get_drvdata(dev);
  600. return drm_mode_config_helper_suspend(drm);
  601. }
  602. static int __maybe_unused malidp_pm_resume(struct device *dev)
  603. {
  604. struct drm_device *drm = dev_get_drvdata(dev);
  605. drm_mode_config_helper_resume(drm);
  606. return 0;
  607. }
  608. static const struct dev_pm_ops malidp_pm_ops = {
  609. SET_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend, malidp_pm_resume) \
  610. SET_RUNTIME_PM_OPS(malidp_runtime_pm_suspend, malidp_runtime_pm_resume, NULL)
  611. };
  612. static struct platform_driver malidp_platform_driver = {
  613. .probe = malidp_platform_probe,
  614. .remove = malidp_platform_remove,
  615. .driver = {
  616. .name = "mali-dp",
  617. .pm = &malidp_pm_ops,
  618. .of_match_table = malidp_drm_of_match,
  619. },
  620. };
  621. module_platform_driver(malidp_platform_driver);
  622. MODULE_AUTHOR("Liviu Dudau <Liviu.Dudau@arm.com>");
  623. MODULE_DESCRIPTION("ARM Mali DP DRM driver");
  624. MODULE_LICENSE("GPL v2");