malidp_drv.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  4. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  5. *
  6. * ARM Mali DP500/DP550/DP650 KMS/DRM driver
  7. */
  8. #include <linux/module.h>
  9. #include <linux/clk.h>
  10. #include <linux/component.h>
  11. #include <linux/of_device.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/of_reserved_mem.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/debugfs.h>
  17. #include <drm/drm_atomic.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_crtc.h>
  20. #include <drm/drm_drv.h>
  21. #include <drm/drm_fbdev_dma.h>
  22. #include <drm/drm_fourcc.h>
  23. #include <drm/drm_gem_dma_helper.h>
  24. #include <drm/drm_gem_framebuffer_helper.h>
  25. #include <drm/drm_managed.h>
  26. #include <drm/drm_modeset_helper.h>
  27. #include <drm/drm_module.h>
  28. #include <drm/drm_of.h>
  29. #include <drm/drm_probe_helper.h>
  30. #include <drm/drm_vblank.h>
  31. #include "malidp_drv.h"
  32. #include "malidp_mw.h"
  33. #include "malidp_regs.h"
  34. #include "malidp_hw.h"
  35. #define MALIDP_CONF_VALID_TIMEOUT 250
  36. #define AFBC_HEADER_SIZE 16
  37. #define AFBC_SUPERBLK_ALIGNMENT 128
  38. static void malidp_write_gamma_table(struct malidp_hw_device *hwdev,
  39. u32 data[MALIDP_COEFFTAB_NUM_COEFFS])
  40. {
  41. int i;
  42. /* Update all channels with a single gamma curve. */
  43. const u32 gamma_write_mask = GENMASK(18, 16);
  44. /*
  45. * Always write an entire table, so the address field in
  46. * DE_COEFFTAB_ADDR is 0 and we can use the gamma_write_mask bitmask
  47. * directly.
  48. */
  49. malidp_hw_write(hwdev, gamma_write_mask,
  50. hwdev->hw->map.coeffs_base + MALIDP_COEF_TABLE_ADDR);
  51. for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i)
  52. malidp_hw_write(hwdev, data[i],
  53. hwdev->hw->map.coeffs_base +
  54. MALIDP_COEF_TABLE_DATA);
  55. }
  56. static void malidp_atomic_commit_update_gamma(struct drm_crtc *crtc,
  57. struct drm_crtc_state *old_state)
  58. {
  59. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  60. struct malidp_hw_device *hwdev = malidp->dev;
  61. if (!crtc->state->color_mgmt_changed)
  62. return;
  63. if (!crtc->state->gamma_lut) {
  64. malidp_hw_clearbits(hwdev,
  65. MALIDP_DISP_FUNC_GAMMA,
  66. MALIDP_DE_DISPLAY_FUNC);
  67. } else {
  68. struct malidp_crtc_state *mc =
  69. to_malidp_crtc_state(crtc->state);
  70. if (!old_state->gamma_lut || (crtc->state->gamma_lut->base.id !=
  71. old_state->gamma_lut->base.id))
  72. malidp_write_gamma_table(hwdev, mc->gamma_coeffs);
  73. malidp_hw_setbits(hwdev, MALIDP_DISP_FUNC_GAMMA,
  74. MALIDP_DE_DISPLAY_FUNC);
  75. }
  76. }
  77. static
  78. void malidp_atomic_commit_update_coloradj(struct drm_crtc *crtc,
  79. struct drm_crtc_state *old_state)
  80. {
  81. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  82. struct malidp_hw_device *hwdev = malidp->dev;
  83. int i;
  84. if (!crtc->state->color_mgmt_changed)
  85. return;
  86. if (!crtc->state->ctm) {
  87. malidp_hw_clearbits(hwdev, MALIDP_DISP_FUNC_CADJ,
  88. MALIDP_DE_DISPLAY_FUNC);
  89. } else {
  90. struct malidp_crtc_state *mc =
  91. to_malidp_crtc_state(crtc->state);
  92. if (!old_state->ctm || (crtc->state->ctm->base.id !=
  93. old_state->ctm->base.id))
  94. for (i = 0; i < MALIDP_COLORADJ_NUM_COEFFS; ++i)
  95. malidp_hw_write(hwdev,
  96. mc->coloradj_coeffs[i],
  97. hwdev->hw->map.coeffs_base +
  98. MALIDP_COLOR_ADJ_COEF + 4 * i);
  99. malidp_hw_setbits(hwdev, MALIDP_DISP_FUNC_CADJ,
  100. MALIDP_DE_DISPLAY_FUNC);
  101. }
  102. }
  103. static void malidp_atomic_commit_se_config(struct drm_crtc *crtc,
  104. struct drm_crtc_state *old_state)
  105. {
  106. struct malidp_crtc_state *cs = to_malidp_crtc_state(crtc->state);
  107. struct malidp_crtc_state *old_cs = to_malidp_crtc_state(old_state);
  108. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  109. struct malidp_hw_device *hwdev = malidp->dev;
  110. struct malidp_se_config *s = &cs->scaler_config;
  111. struct malidp_se_config *old_s = &old_cs->scaler_config;
  112. u32 se_control = hwdev->hw->map.se_base +
  113. ((hwdev->hw->map.features & MALIDP_REGMAP_HAS_CLEARIRQ) ?
  114. 0x10 : 0xC);
  115. u32 layer_control = se_control + MALIDP_SE_LAYER_CONTROL;
  116. u32 scr = se_control + MALIDP_SE_SCALING_CONTROL;
  117. u32 val;
  118. /* Set SE_CONTROL */
  119. if (!s->scale_enable) {
  120. val = malidp_hw_read(hwdev, se_control);
  121. val &= ~MALIDP_SE_SCALING_EN;
  122. malidp_hw_write(hwdev, val, se_control);
  123. return;
  124. }
  125. hwdev->hw->se_set_scaling_coeffs(hwdev, s, old_s);
  126. val = malidp_hw_read(hwdev, se_control);
  127. val |= MALIDP_SE_SCALING_EN | MALIDP_SE_ALPHA_EN;
  128. val &= ~MALIDP_SE_ENH(MALIDP_SE_ENH_MASK);
  129. val |= s->enhancer_enable ? MALIDP_SE_ENH(3) : 0;
  130. val |= MALIDP_SE_RGBO_IF_EN;
  131. malidp_hw_write(hwdev, val, se_control);
  132. /* Set IN_SIZE & OUT_SIZE. */
  133. val = MALIDP_SE_SET_V_SIZE(s->input_h) |
  134. MALIDP_SE_SET_H_SIZE(s->input_w);
  135. malidp_hw_write(hwdev, val, layer_control + MALIDP_SE_L0_IN_SIZE);
  136. val = MALIDP_SE_SET_V_SIZE(s->output_h) |
  137. MALIDP_SE_SET_H_SIZE(s->output_w);
  138. malidp_hw_write(hwdev, val, layer_control + MALIDP_SE_L0_OUT_SIZE);
  139. /* Set phase regs. */
  140. malidp_hw_write(hwdev, s->h_init_phase, scr + MALIDP_SE_H_INIT_PH);
  141. malidp_hw_write(hwdev, s->h_delta_phase, scr + MALIDP_SE_H_DELTA_PH);
  142. malidp_hw_write(hwdev, s->v_init_phase, scr + MALIDP_SE_V_INIT_PH);
  143. malidp_hw_write(hwdev, s->v_delta_phase, scr + MALIDP_SE_V_DELTA_PH);
  144. }
  145. /*
  146. * set the "config valid" bit and wait until the hardware acts on it
  147. */
  148. static int malidp_set_and_wait_config_valid(struct drm_device *drm)
  149. {
  150. struct malidp_drm *malidp = drm_to_malidp(drm);
  151. struct malidp_hw_device *hwdev = malidp->dev;
  152. int ret;
  153. hwdev->hw->set_config_valid(hwdev, 1);
  154. /* don't wait for config_valid flag if we are in config mode */
  155. if (hwdev->hw->in_config_mode(hwdev)) {
  156. atomic_set(&malidp->config_valid, MALIDP_CONFIG_VALID_DONE);
  157. return 0;
  158. }
  159. ret = wait_event_interruptible_timeout(malidp->wq,
  160. atomic_read(&malidp->config_valid) == MALIDP_CONFIG_VALID_DONE,
  161. msecs_to_jiffies(MALIDP_CONF_VALID_TIMEOUT));
  162. return (ret > 0) ? 0 : -ETIMEDOUT;
  163. }
  164. static void malidp_atomic_commit_hw_done(struct drm_atomic_state *state)
  165. {
  166. struct drm_device *drm = state->dev;
  167. struct malidp_drm *malidp = drm_to_malidp(drm);
  168. int loop = 5;
  169. malidp->event = malidp->crtc.state->event;
  170. malidp->crtc.state->event = NULL;
  171. if (malidp->crtc.state->active) {
  172. /*
  173. * if we have an event to deliver to userspace, make sure
  174. * the vblank is enabled as we are sending it from the IRQ
  175. * handler.
  176. */
  177. if (malidp->event)
  178. drm_crtc_vblank_get(&malidp->crtc);
  179. /* only set config_valid if the CRTC is enabled */
  180. if (malidp_set_and_wait_config_valid(drm) < 0) {
  181. /*
  182. * make a loop around the second CVAL setting and
  183. * try 5 times before giving up.
  184. */
  185. while (loop--) {
  186. if (!malidp_set_and_wait_config_valid(drm))
  187. break;
  188. }
  189. DRM_DEBUG_DRIVER("timed out waiting for updated configuration\n");
  190. }
  191. } else if (malidp->event) {
  192. /* CRTC inactive means vblank IRQ is disabled, send event directly */
  193. spin_lock_irq(&drm->event_lock);
  194. drm_crtc_send_vblank_event(&malidp->crtc, malidp->event);
  195. malidp->event = NULL;
  196. spin_unlock_irq(&drm->event_lock);
  197. }
  198. drm_atomic_helper_commit_hw_done(state);
  199. }
  200. static void malidp_atomic_commit_tail(struct drm_atomic_state *state)
  201. {
  202. struct drm_device *drm = state->dev;
  203. struct malidp_drm *malidp = drm_to_malidp(drm);
  204. struct drm_crtc *crtc;
  205. struct drm_crtc_state *old_crtc_state;
  206. int i;
  207. bool fence_cookie = dma_fence_begin_signalling();
  208. pm_runtime_get_sync(drm->dev);
  209. /*
  210. * set config_valid to a special value to let IRQ handlers
  211. * know that we are updating registers
  212. */
  213. atomic_set(&malidp->config_valid, MALIDP_CONFIG_START);
  214. malidp->dev->hw->set_config_valid(malidp->dev, 0);
  215. drm_atomic_helper_commit_modeset_disables(drm, state);
  216. for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
  217. malidp_atomic_commit_update_gamma(crtc, old_crtc_state);
  218. malidp_atomic_commit_update_coloradj(crtc, old_crtc_state);
  219. malidp_atomic_commit_se_config(crtc, old_crtc_state);
  220. }
  221. drm_atomic_helper_commit_planes(drm, state, DRM_PLANE_COMMIT_ACTIVE_ONLY);
  222. malidp_mw_atomic_commit(drm, state);
  223. drm_atomic_helper_commit_modeset_enables(drm, state);
  224. malidp_atomic_commit_hw_done(state);
  225. dma_fence_end_signalling(fence_cookie);
  226. pm_runtime_put(drm->dev);
  227. drm_atomic_helper_cleanup_planes(drm, state);
  228. }
  229. static const struct drm_mode_config_helper_funcs malidp_mode_config_helpers = {
  230. .atomic_commit_tail = malidp_atomic_commit_tail,
  231. };
  232. static bool
  233. malidp_verify_afbc_framebuffer_caps(struct drm_device *dev,
  234. const struct drm_mode_fb_cmd2 *mode_cmd)
  235. {
  236. if (malidp_format_mod_supported(dev, mode_cmd->pixel_format,
  237. mode_cmd->modifier[0]) == false)
  238. return false;
  239. if (mode_cmd->offsets[0] != 0) {
  240. DRM_DEBUG_KMS("AFBC buffers' plane offset should be 0\n");
  241. return false;
  242. }
  243. switch (mode_cmd->modifier[0] & AFBC_SIZE_MASK) {
  244. case AFBC_SIZE_16X16:
  245. if ((mode_cmd->width % 16) || (mode_cmd->height % 16)) {
  246. DRM_DEBUG_KMS("AFBC buffers must be aligned to 16 pixels\n");
  247. return false;
  248. }
  249. break;
  250. default:
  251. DRM_DEBUG_KMS("Unsupported AFBC block size\n");
  252. return false;
  253. }
  254. return true;
  255. }
  256. static bool
  257. malidp_verify_afbc_framebuffer_size(struct drm_device *dev,
  258. struct drm_file *file,
  259. const struct drm_mode_fb_cmd2 *mode_cmd)
  260. {
  261. int n_superblocks = 0;
  262. const struct drm_format_info *info;
  263. struct drm_gem_object *objs = NULL;
  264. u32 afbc_superblock_size = 0, afbc_superblock_height = 0;
  265. u32 afbc_superblock_width = 0, afbc_size = 0;
  266. int bpp = 0;
  267. switch (mode_cmd->modifier[0] & AFBC_SIZE_MASK) {
  268. case AFBC_SIZE_16X16:
  269. afbc_superblock_height = 16;
  270. afbc_superblock_width = 16;
  271. break;
  272. default:
  273. DRM_DEBUG_KMS("AFBC superblock size is not supported\n");
  274. return false;
  275. }
  276. info = drm_get_format_info(dev, mode_cmd);
  277. n_superblocks = (mode_cmd->width / afbc_superblock_width) *
  278. (mode_cmd->height / afbc_superblock_height);
  279. bpp = malidp_format_get_bpp(info->format);
  280. afbc_superblock_size = (bpp * afbc_superblock_width * afbc_superblock_height)
  281. / BITS_PER_BYTE;
  282. afbc_size = ALIGN(n_superblocks * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
  283. afbc_size += n_superblocks * ALIGN(afbc_superblock_size, AFBC_SUPERBLK_ALIGNMENT);
  284. if ((mode_cmd->width * bpp) != (mode_cmd->pitches[0] * BITS_PER_BYTE)) {
  285. DRM_DEBUG_KMS("Invalid value of (pitch * BITS_PER_BYTE) (=%u) "
  286. "should be same as width (=%u) * bpp (=%u)\n",
  287. (mode_cmd->pitches[0] * BITS_PER_BYTE),
  288. mode_cmd->width, bpp);
  289. return false;
  290. }
  291. objs = drm_gem_object_lookup(file, mode_cmd->handles[0]);
  292. if (!objs) {
  293. DRM_DEBUG_KMS("Failed to lookup GEM object\n");
  294. return false;
  295. }
  296. if (objs->size < afbc_size) {
  297. DRM_DEBUG_KMS("buffer size (%zu) too small for AFBC buffer size = %u\n",
  298. objs->size, afbc_size);
  299. drm_gem_object_put(objs);
  300. return false;
  301. }
  302. drm_gem_object_put(objs);
  303. return true;
  304. }
  305. static bool
  306. malidp_verify_afbc_framebuffer(struct drm_device *dev, struct drm_file *file,
  307. const struct drm_mode_fb_cmd2 *mode_cmd)
  308. {
  309. if (malidp_verify_afbc_framebuffer_caps(dev, mode_cmd))
  310. return malidp_verify_afbc_framebuffer_size(dev, file, mode_cmd);
  311. return false;
  312. }
  313. static struct drm_framebuffer *
  314. malidp_fb_create(struct drm_device *dev, struct drm_file *file,
  315. const struct drm_mode_fb_cmd2 *mode_cmd)
  316. {
  317. if (mode_cmd->modifier[0]) {
  318. if (!malidp_verify_afbc_framebuffer(dev, file, mode_cmd))
  319. return ERR_PTR(-EINVAL);
  320. }
  321. return drm_gem_fb_create(dev, file, mode_cmd);
  322. }
  323. static const struct drm_mode_config_funcs malidp_mode_config_funcs = {
  324. .fb_create = malidp_fb_create,
  325. .atomic_check = drm_atomic_helper_check,
  326. .atomic_commit = drm_atomic_helper_commit,
  327. };
  328. static int malidp_init(struct drm_device *drm)
  329. {
  330. int ret;
  331. struct malidp_drm *malidp = drm_to_malidp(drm);
  332. struct malidp_hw_device *hwdev = malidp->dev;
  333. ret = drmm_mode_config_init(drm);
  334. if (ret)
  335. goto out;
  336. drm->mode_config.min_width = hwdev->min_line_size;
  337. drm->mode_config.min_height = hwdev->min_line_size;
  338. drm->mode_config.max_width = hwdev->max_line_size;
  339. drm->mode_config.max_height = hwdev->max_line_size;
  340. drm->mode_config.funcs = &malidp_mode_config_funcs;
  341. drm->mode_config.helper_private = &malidp_mode_config_helpers;
  342. ret = malidp_crtc_init(drm);
  343. if (ret)
  344. goto out;
  345. ret = malidp_mw_connector_init(drm);
  346. if (ret)
  347. goto out;
  348. out:
  349. return ret;
  350. }
  351. static int malidp_irq_init(struct platform_device *pdev)
  352. {
  353. int irq_de, irq_se, ret = 0;
  354. struct drm_device *drm = dev_get_drvdata(&pdev->dev);
  355. struct malidp_drm *malidp = drm_to_malidp(drm);
  356. struct malidp_hw_device *hwdev = malidp->dev;
  357. /* fetch the interrupts from DT */
  358. irq_de = platform_get_irq_byname(pdev, "DE");
  359. if (irq_de < 0) {
  360. DRM_ERROR("no 'DE' IRQ specified!\n");
  361. return irq_de;
  362. }
  363. irq_se = platform_get_irq_byname(pdev, "SE");
  364. if (irq_se < 0) {
  365. DRM_ERROR("no 'SE' IRQ specified!\n");
  366. return irq_se;
  367. }
  368. ret = malidp_de_irq_init(drm, irq_de);
  369. if (ret)
  370. return ret;
  371. ret = malidp_se_irq_init(drm, irq_se);
  372. if (ret) {
  373. malidp_de_irq_fini(hwdev);
  374. return ret;
  375. }
  376. return 0;
  377. }
  378. DEFINE_DRM_GEM_DMA_FOPS(fops);
  379. static int malidp_dumb_create(struct drm_file *file_priv,
  380. struct drm_device *drm,
  381. struct drm_mode_create_dumb *args)
  382. {
  383. struct malidp_drm *malidp = drm_to_malidp(drm);
  384. /* allocate for the worst case scenario, i.e. rotated buffers */
  385. u8 alignment = malidp_hw_get_pitch_align(malidp->dev, 1);
  386. args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), alignment);
  387. return drm_gem_dma_dumb_create_internal(file_priv, drm, args);
  388. }
  389. #ifdef CONFIG_DEBUG_FS
  390. static void malidp_error_stats_init(struct malidp_error_stats *error_stats)
  391. {
  392. error_stats->num_errors = 0;
  393. error_stats->last_error_status = 0;
  394. error_stats->last_error_vblank = -1;
  395. }
  396. void malidp_error(struct malidp_drm *malidp,
  397. struct malidp_error_stats *error_stats, u32 status,
  398. u64 vblank)
  399. {
  400. unsigned long irqflags;
  401. spin_lock_irqsave(&malidp->errors_lock, irqflags);
  402. error_stats->last_error_status = status;
  403. error_stats->last_error_vblank = vblank;
  404. error_stats->num_errors++;
  405. spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
  406. }
  407. static void malidp_error_stats_dump(const char *prefix,
  408. struct malidp_error_stats error_stats,
  409. struct seq_file *m)
  410. {
  411. seq_printf(m, "[%s] num_errors : %d\n", prefix,
  412. error_stats.num_errors);
  413. seq_printf(m, "[%s] last_error_status : 0x%08x\n", prefix,
  414. error_stats.last_error_status);
  415. seq_printf(m, "[%s] last_error_vblank : %lld\n", prefix,
  416. error_stats.last_error_vblank);
  417. }
  418. static int malidp_show_stats(struct seq_file *m, void *arg)
  419. {
  420. struct drm_device *drm = m->private;
  421. struct malidp_drm *malidp = drm_to_malidp(drm);
  422. unsigned long irqflags;
  423. struct malidp_error_stats de_errors, se_errors;
  424. spin_lock_irqsave(&malidp->errors_lock, irqflags);
  425. de_errors = malidp->de_errors;
  426. se_errors = malidp->se_errors;
  427. spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
  428. malidp_error_stats_dump("DE", de_errors, m);
  429. malidp_error_stats_dump("SE", se_errors, m);
  430. return 0;
  431. }
  432. static int malidp_debugfs_open(struct inode *inode, struct file *file)
  433. {
  434. return single_open(file, malidp_show_stats, inode->i_private);
  435. }
  436. static ssize_t malidp_debugfs_write(struct file *file, const char __user *ubuf,
  437. size_t len, loff_t *offp)
  438. {
  439. struct seq_file *m = file->private_data;
  440. struct drm_device *drm = m->private;
  441. struct malidp_drm *malidp = drm_to_malidp(drm);
  442. unsigned long irqflags;
  443. spin_lock_irqsave(&malidp->errors_lock, irqflags);
  444. malidp_error_stats_init(&malidp->de_errors);
  445. malidp_error_stats_init(&malidp->se_errors);
  446. spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
  447. return len;
  448. }
  449. static const struct file_operations malidp_debugfs_fops = {
  450. .owner = THIS_MODULE,
  451. .open = malidp_debugfs_open,
  452. .read = seq_read,
  453. .write = malidp_debugfs_write,
  454. .llseek = seq_lseek,
  455. .release = single_release,
  456. };
  457. static void malidp_debugfs_init(struct drm_minor *minor)
  458. {
  459. struct malidp_drm *malidp = drm_to_malidp(minor->dev);
  460. malidp_error_stats_init(&malidp->de_errors);
  461. malidp_error_stats_init(&malidp->se_errors);
  462. spin_lock_init(&malidp->errors_lock);
  463. debugfs_create_file("debug", S_IRUGO | S_IWUSR, minor->debugfs_root,
  464. minor->dev, &malidp_debugfs_fops);
  465. }
  466. #endif //CONFIG_DEBUG_FS
  467. static const struct drm_driver malidp_driver = {
  468. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  469. DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(malidp_dumb_create),
  470. #ifdef CONFIG_DEBUG_FS
  471. .debugfs_init = malidp_debugfs_init,
  472. #endif
  473. .fops = &fops,
  474. .name = "mali-dp",
  475. .desc = "ARM Mali Display Processor driver",
  476. .date = "20160106",
  477. .major = 1,
  478. .minor = 0,
  479. };
  480. static const struct of_device_id malidp_drm_of_match[] = {
  481. {
  482. .compatible = "arm,mali-dp500",
  483. .data = &malidp_device[MALIDP_500]
  484. },
  485. {
  486. .compatible = "arm,mali-dp550",
  487. .data = &malidp_device[MALIDP_550]
  488. },
  489. {
  490. .compatible = "arm,mali-dp650",
  491. .data = &malidp_device[MALIDP_650]
  492. },
  493. {},
  494. };
  495. MODULE_DEVICE_TABLE(of, malidp_drm_of_match);
  496. static bool malidp_is_compatible_hw_id(struct malidp_hw_device *hwdev,
  497. const struct of_device_id *dev_id)
  498. {
  499. u32 core_id;
  500. const char *compatstr_dp500 = "arm,mali-dp500";
  501. bool is_dp500;
  502. bool dt_is_dp500;
  503. /*
  504. * The DP500 CORE_ID register is in a different location, so check it
  505. * first. If the product id field matches, then this is DP500, otherwise
  506. * check the DP550/650 CORE_ID register.
  507. */
  508. core_id = malidp_hw_read(hwdev, MALIDP500_DC_BASE + MALIDP_DE_CORE_ID);
  509. /* Offset 0x18 will never read 0x500 on products other than DP500. */
  510. is_dp500 = (MALIDP_PRODUCT_ID(core_id) == 0x500);
  511. dt_is_dp500 = strnstr(dev_id->compatible, compatstr_dp500,
  512. sizeof(dev_id->compatible)) != NULL;
  513. if (is_dp500 != dt_is_dp500) {
  514. DRM_ERROR("Device-tree expects %s, but hardware %s DP500.\n",
  515. dev_id->compatible, is_dp500 ? "is" : "is not");
  516. return false;
  517. } else if (!dt_is_dp500) {
  518. u16 product_id;
  519. char buf[32];
  520. core_id = malidp_hw_read(hwdev,
  521. MALIDP550_DC_BASE + MALIDP_DE_CORE_ID);
  522. product_id = MALIDP_PRODUCT_ID(core_id);
  523. snprintf(buf, sizeof(buf), "arm,mali-dp%X", product_id);
  524. if (!strnstr(dev_id->compatible, buf,
  525. sizeof(dev_id->compatible))) {
  526. DRM_ERROR("Device-tree expects %s, but hardware is DP%03X.\n",
  527. dev_id->compatible, product_id);
  528. return false;
  529. }
  530. }
  531. return true;
  532. }
  533. static bool malidp_has_sufficient_address_space(const struct resource *res,
  534. const struct of_device_id *dev_id)
  535. {
  536. resource_size_t res_size = resource_size(res);
  537. const char *compatstr_dp500 = "arm,mali-dp500";
  538. if (!strnstr(dev_id->compatible, compatstr_dp500,
  539. sizeof(dev_id->compatible)))
  540. return res_size >= MALIDP550_ADDR_SPACE_SIZE;
  541. else if (res_size < MALIDP500_ADDR_SPACE_SIZE)
  542. return false;
  543. return true;
  544. }
  545. static ssize_t core_id_show(struct device *dev, struct device_attribute *attr,
  546. char *buf)
  547. {
  548. struct drm_device *drm = dev_get_drvdata(dev);
  549. struct malidp_drm *malidp = drm_to_malidp(drm);
  550. return sysfs_emit(buf, "%08x\n", malidp->core_id);
  551. }
  552. static DEVICE_ATTR_RO(core_id);
  553. static struct attribute *mali_dp_attrs[] = {
  554. &dev_attr_core_id.attr,
  555. NULL,
  556. };
  557. ATTRIBUTE_GROUPS(mali_dp);
  558. #define MAX_OUTPUT_CHANNELS 3
  559. static int malidp_runtime_pm_suspend(struct device *dev)
  560. {
  561. struct drm_device *drm = dev_get_drvdata(dev);
  562. struct malidp_drm *malidp = drm_to_malidp(drm);
  563. struct malidp_hw_device *hwdev = malidp->dev;
  564. /* we can only suspend if the hardware is in config mode */
  565. WARN_ON(!hwdev->hw->in_config_mode(hwdev));
  566. malidp_se_irq_fini(hwdev);
  567. malidp_de_irq_fini(hwdev);
  568. hwdev->pm_suspended = true;
  569. clk_disable_unprepare(hwdev->mclk);
  570. clk_disable_unprepare(hwdev->aclk);
  571. clk_disable_unprepare(hwdev->pclk);
  572. return 0;
  573. }
  574. static int malidp_runtime_pm_resume(struct device *dev)
  575. {
  576. struct drm_device *drm = dev_get_drvdata(dev);
  577. struct malidp_drm *malidp = drm_to_malidp(drm);
  578. struct malidp_hw_device *hwdev = malidp->dev;
  579. clk_prepare_enable(hwdev->pclk);
  580. clk_prepare_enable(hwdev->aclk);
  581. clk_prepare_enable(hwdev->mclk);
  582. hwdev->pm_suspended = false;
  583. malidp_de_irq_hw_init(hwdev);
  584. malidp_se_irq_hw_init(hwdev);
  585. return 0;
  586. }
  587. static int malidp_bind(struct device *dev)
  588. {
  589. struct resource *res;
  590. struct drm_device *drm;
  591. struct malidp_drm *malidp;
  592. struct malidp_hw_device *hwdev;
  593. struct platform_device *pdev = to_platform_device(dev);
  594. struct of_device_id const *dev_id;
  595. struct drm_encoder *encoder;
  596. /* number of lines for the R, G and B output */
  597. u8 output_width[MAX_OUTPUT_CHANNELS];
  598. int ret = 0, i;
  599. u32 version, out_depth = 0;
  600. malidp = devm_drm_dev_alloc(dev, &malidp_driver, typeof(*malidp), base);
  601. if (IS_ERR(malidp))
  602. return PTR_ERR(malidp);
  603. drm = &malidp->base;
  604. hwdev = drmm_kzalloc(drm, sizeof(*hwdev), GFP_KERNEL);
  605. if (!hwdev)
  606. return -ENOMEM;
  607. hwdev->hw = (struct malidp_hw *)of_device_get_match_data(dev);
  608. malidp->dev = hwdev;
  609. hwdev->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  610. if (IS_ERR(hwdev->regs))
  611. return PTR_ERR(hwdev->regs);
  612. hwdev->pclk = devm_clk_get(dev, "pclk");
  613. if (IS_ERR(hwdev->pclk))
  614. return PTR_ERR(hwdev->pclk);
  615. hwdev->aclk = devm_clk_get(dev, "aclk");
  616. if (IS_ERR(hwdev->aclk))
  617. return PTR_ERR(hwdev->aclk);
  618. hwdev->mclk = devm_clk_get(dev, "mclk");
  619. if (IS_ERR(hwdev->mclk))
  620. return PTR_ERR(hwdev->mclk);
  621. hwdev->pxlclk = devm_clk_get(dev, "pxlclk");
  622. if (IS_ERR(hwdev->pxlclk))
  623. return PTR_ERR(hwdev->pxlclk);
  624. /* Get the optional framebuffer memory resource */
  625. ret = of_reserved_mem_device_init(dev);
  626. if (ret && ret != -ENODEV)
  627. return ret;
  628. dev_set_drvdata(dev, drm);
  629. /* Enable power management */
  630. pm_runtime_enable(dev);
  631. /* Resume device to enable the clocks */
  632. if (pm_runtime_enabled(dev))
  633. pm_runtime_get_sync(dev);
  634. else
  635. malidp_runtime_pm_resume(dev);
  636. dev_id = of_match_device(malidp_drm_of_match, dev);
  637. if (!dev_id) {
  638. ret = -EINVAL;
  639. goto query_hw_fail;
  640. }
  641. if (!malidp_has_sufficient_address_space(res, dev_id)) {
  642. DRM_ERROR("Insufficient address space in device-tree.\n");
  643. ret = -EINVAL;
  644. goto query_hw_fail;
  645. }
  646. if (!malidp_is_compatible_hw_id(hwdev, dev_id)) {
  647. ret = -EINVAL;
  648. goto query_hw_fail;
  649. }
  650. ret = hwdev->hw->query_hw(hwdev);
  651. if (ret) {
  652. DRM_ERROR("Invalid HW configuration\n");
  653. goto query_hw_fail;
  654. }
  655. version = malidp_hw_read(hwdev, hwdev->hw->map.dc_base + MALIDP_DE_CORE_ID);
  656. DRM_INFO("found ARM Mali-DP%3x version r%dp%d\n", version >> 16,
  657. (version >> 12) & 0xf, (version >> 8) & 0xf);
  658. malidp->core_id = version;
  659. ret = of_property_read_u32(dev->of_node,
  660. "arm,malidp-arqos-value",
  661. &hwdev->arqos_value);
  662. if (ret)
  663. hwdev->arqos_value = 0x0;
  664. /* set the number of lines used for output of RGB data */
  665. ret = of_property_read_u8_array(dev->of_node,
  666. "arm,malidp-output-port-lines",
  667. output_width, MAX_OUTPUT_CHANNELS);
  668. if (ret)
  669. goto query_hw_fail;
  670. for (i = 0; i < MAX_OUTPUT_CHANNELS; i++)
  671. out_depth = (out_depth << 8) | (output_width[i] & 0xf);
  672. malidp_hw_write(hwdev, out_depth, hwdev->hw->map.out_depth_base);
  673. hwdev->output_color_depth = out_depth;
  674. atomic_set(&malidp->config_valid, MALIDP_CONFIG_VALID_INIT);
  675. init_waitqueue_head(&malidp->wq);
  676. ret = malidp_init(drm);
  677. if (ret < 0)
  678. goto query_hw_fail;
  679. /* Set the CRTC's port so that the encoder component can find it */
  680. malidp->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
  681. ret = component_bind_all(dev, drm);
  682. if (ret) {
  683. DRM_ERROR("Failed to bind all components\n");
  684. goto bind_fail;
  685. }
  686. /* We expect to have a maximum of two encoders one for the actual
  687. * display and a virtual one for the writeback connector
  688. */
  689. WARN_ON(drm->mode_config.num_encoder > 2);
  690. list_for_each_entry(encoder, &drm->mode_config.encoder_list, head) {
  691. encoder->possible_clones =
  692. (1 << drm->mode_config.num_encoder) - 1;
  693. }
  694. ret = malidp_irq_init(pdev);
  695. if (ret < 0)
  696. goto irq_init_fail;
  697. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  698. if (ret < 0) {
  699. DRM_ERROR("failed to initialise vblank\n");
  700. goto vblank_fail;
  701. }
  702. pm_runtime_put(dev);
  703. drm_mode_config_reset(drm);
  704. drm_kms_helper_poll_init(drm);
  705. ret = drm_dev_register(drm, 0);
  706. if (ret)
  707. goto register_fail;
  708. drm_fbdev_dma_setup(drm, 32);
  709. return 0;
  710. register_fail:
  711. drm_kms_helper_poll_fini(drm);
  712. pm_runtime_get_sync(dev);
  713. vblank_fail:
  714. malidp_se_irq_fini(hwdev);
  715. malidp_de_irq_fini(hwdev);
  716. irq_init_fail:
  717. drm_atomic_helper_shutdown(drm);
  718. component_unbind_all(dev, drm);
  719. bind_fail:
  720. of_node_put(malidp->crtc.port);
  721. malidp->crtc.port = NULL;
  722. query_hw_fail:
  723. pm_runtime_put(dev);
  724. if (pm_runtime_enabled(dev))
  725. pm_runtime_disable(dev);
  726. else
  727. malidp_runtime_pm_suspend(dev);
  728. dev_set_drvdata(dev, NULL);
  729. of_reserved_mem_device_release(dev);
  730. return ret;
  731. }
  732. static void malidp_unbind(struct device *dev)
  733. {
  734. struct drm_device *drm = dev_get_drvdata(dev);
  735. struct malidp_drm *malidp = drm_to_malidp(drm);
  736. struct malidp_hw_device *hwdev = malidp->dev;
  737. drm_dev_unregister(drm);
  738. drm_kms_helper_poll_fini(drm);
  739. pm_runtime_get_sync(dev);
  740. drm_atomic_helper_shutdown(drm);
  741. malidp_se_irq_fini(hwdev);
  742. malidp_de_irq_fini(hwdev);
  743. component_unbind_all(dev, drm);
  744. of_node_put(malidp->crtc.port);
  745. malidp->crtc.port = NULL;
  746. pm_runtime_put(dev);
  747. if (pm_runtime_enabled(dev))
  748. pm_runtime_disable(dev);
  749. else
  750. malidp_runtime_pm_suspend(dev);
  751. dev_set_drvdata(dev, NULL);
  752. of_reserved_mem_device_release(dev);
  753. }
  754. static const struct component_master_ops malidp_master_ops = {
  755. .bind = malidp_bind,
  756. .unbind = malidp_unbind,
  757. };
  758. static int malidp_compare_dev(struct device *dev, void *data)
  759. {
  760. struct device_node *np = data;
  761. return dev->of_node == np;
  762. }
  763. static int malidp_platform_probe(struct platform_device *pdev)
  764. {
  765. struct device_node *port;
  766. struct component_match *match = NULL;
  767. if (!pdev->dev.of_node)
  768. return -ENODEV;
  769. /* there is only one output port inside each device, find it */
  770. port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
  771. if (!port)
  772. return -ENODEV;
  773. drm_of_component_match_add(&pdev->dev, &match, malidp_compare_dev,
  774. port);
  775. of_node_put(port);
  776. return component_master_add_with_match(&pdev->dev, &malidp_master_ops,
  777. match);
  778. }
  779. static void malidp_platform_remove(struct platform_device *pdev)
  780. {
  781. component_master_del(&pdev->dev, &malidp_master_ops);
  782. }
  783. static void malidp_platform_shutdown(struct platform_device *pdev)
  784. {
  785. drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  786. }
  787. static int __maybe_unused malidp_pm_suspend(struct device *dev)
  788. {
  789. struct drm_device *drm = dev_get_drvdata(dev);
  790. return drm_mode_config_helper_suspend(drm);
  791. }
  792. static int __maybe_unused malidp_pm_resume(struct device *dev)
  793. {
  794. struct drm_device *drm = dev_get_drvdata(dev);
  795. drm_mode_config_helper_resume(drm);
  796. return 0;
  797. }
  798. static int __maybe_unused malidp_pm_suspend_late(struct device *dev)
  799. {
  800. if (!pm_runtime_status_suspended(dev)) {
  801. malidp_runtime_pm_suspend(dev);
  802. pm_runtime_set_suspended(dev);
  803. }
  804. return 0;
  805. }
  806. static int __maybe_unused malidp_pm_resume_early(struct device *dev)
  807. {
  808. malidp_runtime_pm_resume(dev);
  809. pm_runtime_set_active(dev);
  810. return 0;
  811. }
  812. static const struct dev_pm_ops malidp_pm_ops = {
  813. SET_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend, malidp_pm_resume) \
  814. SET_LATE_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend_late, malidp_pm_resume_early) \
  815. SET_RUNTIME_PM_OPS(malidp_runtime_pm_suspend, malidp_runtime_pm_resume, NULL)
  816. };
  817. static struct platform_driver malidp_platform_driver = {
  818. .probe = malidp_platform_probe,
  819. .remove_new = malidp_platform_remove,
  820. .shutdown = malidp_platform_shutdown,
  821. .driver = {
  822. .name = "mali-dp",
  823. .pm = &malidp_pm_ops,
  824. .of_match_table = malidp_drm_of_match,
  825. .dev_groups = mali_dp_groups,
  826. },
  827. };
  828. drm_module_platform_driver(malidp_platform_driver);
  829. MODULE_AUTHOR("Liviu Dudau <Liviu.Dudau@arm.com>");
  830. MODULE_DESCRIPTION("ARM Mali DP DRM driver");
  831. MODULE_LICENSE("GPL v2");