malidp_drv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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_device *drm = state->dev;
  162. struct malidp_drm *malidp = drm->dev_private;
  163. malidp->event = malidp->crtc.state->event;
  164. malidp->crtc.state->event = NULL;
  165. if (malidp->crtc.state->active) {
  166. /*
  167. * if we have an event to deliver to userspace, make sure
  168. * the vblank is enabled as we are sending it from the IRQ
  169. * handler.
  170. */
  171. if (malidp->event)
  172. drm_crtc_vblank_get(&malidp->crtc);
  173. /* only set config_valid if the CRTC is enabled */
  174. if (malidp_set_and_wait_config_valid(drm) < 0)
  175. DRM_DEBUG_DRIVER("timed out waiting for updated configuration\n");
  176. } else if (malidp->event) {
  177. /* CRTC inactive means vblank IRQ is disabled, send event directly */
  178. spin_lock_irq(&drm->event_lock);
  179. drm_crtc_send_vblank_event(&malidp->crtc, malidp->event);
  180. malidp->event = NULL;
  181. spin_unlock_irq(&drm->event_lock);
  182. }
  183. drm_atomic_helper_commit_hw_done(state);
  184. }
  185. static void malidp_atomic_commit_tail(struct drm_atomic_state *state)
  186. {
  187. struct drm_device *drm = state->dev;
  188. struct drm_crtc *crtc;
  189. struct drm_crtc_state *old_crtc_state;
  190. int i;
  191. pm_runtime_get_sync(drm->dev);
  192. drm_atomic_helper_commit_modeset_disables(drm, state);
  193. for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
  194. malidp_atomic_commit_update_gamma(crtc, old_crtc_state);
  195. malidp_atomic_commit_update_coloradj(crtc, old_crtc_state);
  196. malidp_atomic_commit_se_config(crtc, old_crtc_state);
  197. }
  198. drm_atomic_helper_commit_planes(drm, state, 0);
  199. drm_atomic_helper_commit_modeset_enables(drm, state);
  200. malidp_atomic_commit_hw_done(state);
  201. pm_runtime_put(drm->dev);
  202. drm_atomic_helper_cleanup_planes(drm, state);
  203. }
  204. static const struct drm_mode_config_helper_funcs malidp_mode_config_helpers = {
  205. .atomic_commit_tail = malidp_atomic_commit_tail,
  206. };
  207. static const struct drm_mode_config_funcs malidp_mode_config_funcs = {
  208. .fb_create = drm_gem_fb_create,
  209. .output_poll_changed = drm_fb_helper_output_poll_changed,
  210. .atomic_check = drm_atomic_helper_check,
  211. .atomic_commit = drm_atomic_helper_commit,
  212. };
  213. static int malidp_init(struct drm_device *drm)
  214. {
  215. int ret;
  216. struct malidp_drm *malidp = drm->dev_private;
  217. struct malidp_hw_device *hwdev = malidp->dev;
  218. drm_mode_config_init(drm);
  219. drm->mode_config.min_width = hwdev->min_line_size;
  220. drm->mode_config.min_height = hwdev->min_line_size;
  221. drm->mode_config.max_width = hwdev->max_line_size;
  222. drm->mode_config.max_height = hwdev->max_line_size;
  223. drm->mode_config.funcs = &malidp_mode_config_funcs;
  224. drm->mode_config.helper_private = &malidp_mode_config_helpers;
  225. ret = malidp_crtc_init(drm);
  226. if (ret) {
  227. drm_mode_config_cleanup(drm);
  228. return ret;
  229. }
  230. return 0;
  231. }
  232. static void malidp_fini(struct drm_device *drm)
  233. {
  234. drm_atomic_helper_shutdown(drm);
  235. drm_mode_config_cleanup(drm);
  236. }
  237. static int malidp_irq_init(struct platform_device *pdev)
  238. {
  239. int irq_de, irq_se, ret = 0;
  240. struct drm_device *drm = dev_get_drvdata(&pdev->dev);
  241. /* fetch the interrupts from DT */
  242. irq_de = platform_get_irq_byname(pdev, "DE");
  243. if (irq_de < 0) {
  244. DRM_ERROR("no 'DE' IRQ specified!\n");
  245. return irq_de;
  246. }
  247. irq_se = platform_get_irq_byname(pdev, "SE");
  248. if (irq_se < 0) {
  249. DRM_ERROR("no 'SE' IRQ specified!\n");
  250. return irq_se;
  251. }
  252. ret = malidp_de_irq_init(drm, irq_de);
  253. if (ret)
  254. return ret;
  255. ret = malidp_se_irq_init(drm, irq_se);
  256. if (ret) {
  257. malidp_de_irq_fini(drm);
  258. return ret;
  259. }
  260. return 0;
  261. }
  262. DEFINE_DRM_GEM_CMA_FOPS(fops);
  263. static int malidp_dumb_create(struct drm_file *file_priv,
  264. struct drm_device *drm,
  265. struct drm_mode_create_dumb *args)
  266. {
  267. struct malidp_drm *malidp = drm->dev_private;
  268. /* allocate for the worst case scenario, i.e. rotated buffers */
  269. u8 alignment = malidp_hw_get_pitch_align(malidp->dev, 1);
  270. args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), alignment);
  271. return drm_gem_cma_dumb_create_internal(file_priv, drm, args);
  272. }
  273. static struct drm_driver malidp_driver = {
  274. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC |
  275. DRIVER_PRIME,
  276. .lastclose = drm_fb_helper_lastclose,
  277. .gem_free_object_unlocked = drm_gem_cma_free_object,
  278. .gem_vm_ops = &drm_gem_cma_vm_ops,
  279. .dumb_create = malidp_dumb_create,
  280. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  281. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  282. .gem_prime_export = drm_gem_prime_export,
  283. .gem_prime_import = drm_gem_prime_import,
  284. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  285. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  286. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  287. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  288. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  289. .fops = &fops,
  290. .name = "mali-dp",
  291. .desc = "ARM Mali Display Processor driver",
  292. .date = "20160106",
  293. .major = 1,
  294. .minor = 0,
  295. };
  296. static const struct of_device_id malidp_drm_of_match[] = {
  297. {
  298. .compatible = "arm,mali-dp500",
  299. .data = &malidp_device[MALIDP_500]
  300. },
  301. {
  302. .compatible = "arm,mali-dp550",
  303. .data = &malidp_device[MALIDP_550]
  304. },
  305. {
  306. .compatible = "arm,mali-dp650",
  307. .data = &malidp_device[MALIDP_650]
  308. },
  309. {},
  310. };
  311. MODULE_DEVICE_TABLE(of, malidp_drm_of_match);
  312. static bool malidp_is_compatible_hw_id(struct malidp_hw_device *hwdev,
  313. const struct of_device_id *dev_id)
  314. {
  315. u32 core_id;
  316. const char *compatstr_dp500 = "arm,mali-dp500";
  317. bool is_dp500;
  318. bool dt_is_dp500;
  319. /*
  320. * The DP500 CORE_ID register is in a different location, so check it
  321. * first. If the product id field matches, then this is DP500, otherwise
  322. * check the DP550/650 CORE_ID register.
  323. */
  324. core_id = malidp_hw_read(hwdev, MALIDP500_DC_BASE + MALIDP_DE_CORE_ID);
  325. /* Offset 0x18 will never read 0x500 on products other than DP500. */
  326. is_dp500 = (MALIDP_PRODUCT_ID(core_id) == 0x500);
  327. dt_is_dp500 = strnstr(dev_id->compatible, compatstr_dp500,
  328. sizeof(dev_id->compatible)) != NULL;
  329. if (is_dp500 != dt_is_dp500) {
  330. DRM_ERROR("Device-tree expects %s, but hardware %s DP500.\n",
  331. dev_id->compatible, is_dp500 ? "is" : "is not");
  332. return false;
  333. } else if (!dt_is_dp500) {
  334. u16 product_id;
  335. char buf[32];
  336. core_id = malidp_hw_read(hwdev,
  337. MALIDP550_DC_BASE + MALIDP_DE_CORE_ID);
  338. product_id = MALIDP_PRODUCT_ID(core_id);
  339. snprintf(buf, sizeof(buf), "arm,mali-dp%X", product_id);
  340. if (!strnstr(dev_id->compatible, buf,
  341. sizeof(dev_id->compatible))) {
  342. DRM_ERROR("Device-tree expects %s, but hardware is DP%03X.\n",
  343. dev_id->compatible, product_id);
  344. return false;
  345. }
  346. }
  347. return true;
  348. }
  349. static bool malidp_has_sufficient_address_space(const struct resource *res,
  350. const struct of_device_id *dev_id)
  351. {
  352. resource_size_t res_size = resource_size(res);
  353. const char *compatstr_dp500 = "arm,mali-dp500";
  354. if (!strnstr(dev_id->compatible, compatstr_dp500,
  355. sizeof(dev_id->compatible)))
  356. return res_size >= MALIDP550_ADDR_SPACE_SIZE;
  357. else if (res_size < MALIDP500_ADDR_SPACE_SIZE)
  358. return false;
  359. return true;
  360. }
  361. static ssize_t core_id_show(struct device *dev, struct device_attribute *attr,
  362. char *buf)
  363. {
  364. struct drm_device *drm = dev_get_drvdata(dev);
  365. struct malidp_drm *malidp = drm->dev_private;
  366. return snprintf(buf, PAGE_SIZE, "%08x\n", malidp->core_id);
  367. }
  368. DEVICE_ATTR_RO(core_id);
  369. static int malidp_init_sysfs(struct device *dev)
  370. {
  371. int ret = device_create_file(dev, &dev_attr_core_id);
  372. if (ret)
  373. DRM_ERROR("failed to create device file for core_id\n");
  374. return ret;
  375. }
  376. static void malidp_fini_sysfs(struct device *dev)
  377. {
  378. device_remove_file(dev, &dev_attr_core_id);
  379. }
  380. #define MAX_OUTPUT_CHANNELS 3
  381. static int malidp_runtime_pm_suspend(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. /* we can only suspend if the hardware is in config mode */
  387. WARN_ON(!hwdev->hw->in_config_mode(hwdev));
  388. hwdev->pm_suspended = true;
  389. clk_disable_unprepare(hwdev->mclk);
  390. clk_disable_unprepare(hwdev->aclk);
  391. clk_disable_unprepare(hwdev->pclk);
  392. return 0;
  393. }
  394. static int malidp_runtime_pm_resume(struct device *dev)
  395. {
  396. struct drm_device *drm = dev_get_drvdata(dev);
  397. struct malidp_drm *malidp = drm->dev_private;
  398. struct malidp_hw_device *hwdev = malidp->dev;
  399. clk_prepare_enable(hwdev->pclk);
  400. clk_prepare_enable(hwdev->aclk);
  401. clk_prepare_enable(hwdev->mclk);
  402. hwdev->pm_suspended = false;
  403. return 0;
  404. }
  405. static int malidp_bind(struct device *dev)
  406. {
  407. struct resource *res;
  408. struct drm_device *drm;
  409. struct malidp_drm *malidp;
  410. struct malidp_hw_device *hwdev;
  411. struct platform_device *pdev = to_platform_device(dev);
  412. struct of_device_id const *dev_id;
  413. /* number of lines for the R, G and B output */
  414. u8 output_width[MAX_OUTPUT_CHANNELS];
  415. int ret = 0, i;
  416. u32 version, out_depth = 0;
  417. malidp = devm_kzalloc(dev, sizeof(*malidp), GFP_KERNEL);
  418. if (!malidp)
  419. return -ENOMEM;
  420. hwdev = devm_kzalloc(dev, sizeof(*hwdev), GFP_KERNEL);
  421. if (!hwdev)
  422. return -ENOMEM;
  423. hwdev->hw = (struct malidp_hw *)of_device_get_match_data(dev);
  424. malidp->dev = hwdev;
  425. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  426. hwdev->regs = devm_ioremap_resource(dev, res);
  427. if (IS_ERR(hwdev->regs))
  428. return PTR_ERR(hwdev->regs);
  429. hwdev->pclk = devm_clk_get(dev, "pclk");
  430. if (IS_ERR(hwdev->pclk))
  431. return PTR_ERR(hwdev->pclk);
  432. hwdev->aclk = devm_clk_get(dev, "aclk");
  433. if (IS_ERR(hwdev->aclk))
  434. return PTR_ERR(hwdev->aclk);
  435. hwdev->mclk = devm_clk_get(dev, "mclk");
  436. if (IS_ERR(hwdev->mclk))
  437. return PTR_ERR(hwdev->mclk);
  438. hwdev->pxlclk = devm_clk_get(dev, "pxlclk");
  439. if (IS_ERR(hwdev->pxlclk))
  440. return PTR_ERR(hwdev->pxlclk);
  441. /* Get the optional framebuffer memory resource */
  442. ret = of_reserved_mem_device_init(dev);
  443. if (ret && ret != -ENODEV)
  444. return ret;
  445. drm = drm_dev_alloc(&malidp_driver, dev);
  446. if (IS_ERR(drm)) {
  447. ret = PTR_ERR(drm);
  448. goto alloc_fail;
  449. }
  450. drm->dev_private = malidp;
  451. dev_set_drvdata(dev, drm);
  452. /* Enable power management */
  453. pm_runtime_enable(dev);
  454. /* Resume device to enable the clocks */
  455. if (pm_runtime_enabled(dev))
  456. pm_runtime_get_sync(dev);
  457. else
  458. malidp_runtime_pm_resume(dev);
  459. dev_id = of_match_device(malidp_drm_of_match, dev);
  460. if (!dev_id) {
  461. ret = -EINVAL;
  462. goto query_hw_fail;
  463. }
  464. if (!malidp_has_sufficient_address_space(res, dev_id)) {
  465. DRM_ERROR("Insufficient address space in device-tree.\n");
  466. ret = -EINVAL;
  467. goto query_hw_fail;
  468. }
  469. if (!malidp_is_compatible_hw_id(hwdev, dev_id)) {
  470. ret = -EINVAL;
  471. goto query_hw_fail;
  472. }
  473. ret = hwdev->hw->query_hw(hwdev);
  474. if (ret) {
  475. DRM_ERROR("Invalid HW configuration\n");
  476. goto query_hw_fail;
  477. }
  478. version = malidp_hw_read(hwdev, hwdev->hw->map.dc_base + MALIDP_DE_CORE_ID);
  479. DRM_INFO("found ARM Mali-DP%3x version r%dp%d\n", version >> 16,
  480. (version >> 12) & 0xf, (version >> 8) & 0xf);
  481. malidp->core_id = version;
  482. /* set the number of lines used for output of RGB data */
  483. ret = of_property_read_u8_array(dev->of_node,
  484. "arm,malidp-output-port-lines",
  485. output_width, MAX_OUTPUT_CHANNELS);
  486. if (ret)
  487. goto query_hw_fail;
  488. for (i = 0; i < MAX_OUTPUT_CHANNELS; i++)
  489. out_depth = (out_depth << 8) | (output_width[i] & 0xf);
  490. malidp_hw_write(hwdev, out_depth, hwdev->hw->map.out_depth_base);
  491. atomic_set(&malidp->config_valid, 0);
  492. init_waitqueue_head(&malidp->wq);
  493. ret = malidp_init(drm);
  494. if (ret < 0)
  495. goto query_hw_fail;
  496. ret = malidp_init_sysfs(dev);
  497. if (ret)
  498. goto init_fail;
  499. /* Set the CRTC's port so that the encoder component can find it */
  500. malidp->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
  501. ret = component_bind_all(dev, drm);
  502. if (ret) {
  503. DRM_ERROR("Failed to bind all components\n");
  504. goto bind_fail;
  505. }
  506. ret = malidp_irq_init(pdev);
  507. if (ret < 0)
  508. goto irq_init_fail;
  509. drm->irq_enabled = true;
  510. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  511. if (ret < 0) {
  512. DRM_ERROR("failed to initialise vblank\n");
  513. goto vblank_fail;
  514. }
  515. pm_runtime_put(dev);
  516. drm_mode_config_reset(drm);
  517. ret = drm_fb_cma_fbdev_init(drm, 32, 0);
  518. if (ret)
  519. goto fbdev_fail;
  520. drm_kms_helper_poll_init(drm);
  521. ret = drm_dev_register(drm, 0);
  522. if (ret)
  523. goto register_fail;
  524. return 0;
  525. register_fail:
  526. drm_fb_cma_fbdev_fini(drm);
  527. drm_kms_helper_poll_fini(drm);
  528. fbdev_fail:
  529. pm_runtime_get_sync(dev);
  530. vblank_fail:
  531. malidp_se_irq_fini(drm);
  532. malidp_de_irq_fini(drm);
  533. drm->irq_enabled = false;
  534. irq_init_fail:
  535. component_unbind_all(dev, drm);
  536. bind_fail:
  537. of_node_put(malidp->crtc.port);
  538. malidp->crtc.port = NULL;
  539. init_fail:
  540. malidp_fini_sysfs(dev);
  541. malidp_fini(drm);
  542. query_hw_fail:
  543. pm_runtime_put(dev);
  544. if (pm_runtime_enabled(dev))
  545. pm_runtime_disable(dev);
  546. else
  547. malidp_runtime_pm_suspend(dev);
  548. drm->dev_private = NULL;
  549. dev_set_drvdata(dev, NULL);
  550. drm_dev_put(drm);
  551. alloc_fail:
  552. of_reserved_mem_device_release(dev);
  553. return ret;
  554. }
  555. static void malidp_unbind(struct device *dev)
  556. {
  557. struct drm_device *drm = dev_get_drvdata(dev);
  558. struct malidp_drm *malidp = drm->dev_private;
  559. drm_dev_unregister(drm);
  560. drm_fb_cma_fbdev_fini(drm);
  561. drm_kms_helper_poll_fini(drm);
  562. pm_runtime_get_sync(dev);
  563. drm_crtc_vblank_off(&malidp->crtc);
  564. malidp_se_irq_fini(drm);
  565. malidp_de_irq_fini(drm);
  566. drm->irq_enabled = false;
  567. component_unbind_all(dev, drm);
  568. of_node_put(malidp->crtc.port);
  569. malidp->crtc.port = NULL;
  570. malidp_fini_sysfs(dev);
  571. malidp_fini(drm);
  572. pm_runtime_put(dev);
  573. if (pm_runtime_enabled(dev))
  574. pm_runtime_disable(dev);
  575. else
  576. malidp_runtime_pm_suspend(dev);
  577. drm->dev_private = NULL;
  578. dev_set_drvdata(dev, NULL);
  579. drm_dev_put(drm);
  580. of_reserved_mem_device_release(dev);
  581. }
  582. static const struct component_master_ops malidp_master_ops = {
  583. .bind = malidp_bind,
  584. .unbind = malidp_unbind,
  585. };
  586. static int malidp_compare_dev(struct device *dev, void *data)
  587. {
  588. struct device_node *np = data;
  589. return dev->of_node == np;
  590. }
  591. static int malidp_platform_probe(struct platform_device *pdev)
  592. {
  593. struct device_node *port;
  594. struct component_match *match = NULL;
  595. if (!pdev->dev.of_node)
  596. return -ENODEV;
  597. /* there is only one output port inside each device, find it */
  598. port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
  599. if (!port)
  600. return -ENODEV;
  601. drm_of_component_match_add(&pdev->dev, &match, malidp_compare_dev,
  602. port);
  603. of_node_put(port);
  604. return component_master_add_with_match(&pdev->dev, &malidp_master_ops,
  605. match);
  606. }
  607. static int malidp_platform_remove(struct platform_device *pdev)
  608. {
  609. component_master_del(&pdev->dev, &malidp_master_ops);
  610. return 0;
  611. }
  612. static int __maybe_unused malidp_pm_suspend(struct device *dev)
  613. {
  614. struct drm_device *drm = dev_get_drvdata(dev);
  615. return drm_mode_config_helper_suspend(drm);
  616. }
  617. static int __maybe_unused malidp_pm_resume(struct device *dev)
  618. {
  619. struct drm_device *drm = dev_get_drvdata(dev);
  620. drm_mode_config_helper_resume(drm);
  621. return 0;
  622. }
  623. static const struct dev_pm_ops malidp_pm_ops = {
  624. SET_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend, malidp_pm_resume) \
  625. SET_RUNTIME_PM_OPS(malidp_runtime_pm_suspend, malidp_runtime_pm_resume, NULL)
  626. };
  627. static struct platform_driver malidp_platform_driver = {
  628. .probe = malidp_platform_probe,
  629. .remove = malidp_platform_remove,
  630. .driver = {
  631. .name = "mali-dp",
  632. .pm = &malidp_pm_ops,
  633. .of_match_table = malidp_drm_of_match,
  634. },
  635. };
  636. module_platform_driver(malidp_platform_driver);
  637. MODULE_AUTHOR("Liviu Dudau <Liviu.Dudau@arm.com>");
  638. MODULE_DESCRIPTION("ARM Mali DP DRM driver");
  639. MODULE_LICENSE("GPL v2");