malidp_drv.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 <linux/debugfs.h>
  20. #include <drm/drmP.h>
  21. #include <drm/drm_atomic.h>
  22. #include <drm/drm_atomic_helper.h>
  23. #include <drm/drm_crtc.h>
  24. #include <drm/drm_crtc_helper.h>
  25. #include <drm/drm_fb_helper.h>
  26. #include <drm/drm_fb_cma_helper.h>
  27. #include <drm/drm_gem_cma_helper.h>
  28. #include <drm/drm_gem_framebuffer_helper.h>
  29. #include <drm/drm_modeset_helper.h>
  30. #include <drm/drm_of.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->dev_private;
  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->dev_private;
  168. malidp->event = malidp->crtc.state->event;
  169. malidp->crtc.state->event = NULL;
  170. if (malidp->crtc.state->active) {
  171. /*
  172. * if we have an event to deliver to userspace, make sure
  173. * the vblank is enabled as we are sending it from the IRQ
  174. * handler.
  175. */
  176. if (malidp->event)
  177. drm_crtc_vblank_get(&malidp->crtc);
  178. /* only set config_valid if the CRTC is enabled */
  179. if (malidp_set_and_wait_config_valid(drm) < 0)
  180. DRM_DEBUG_DRIVER("timed out waiting for updated configuration\n");
  181. } else if (malidp->event) {
  182. /* CRTC inactive means vblank IRQ is disabled, send event directly */
  183. spin_lock_irq(&drm->event_lock);
  184. drm_crtc_send_vblank_event(&malidp->crtc, malidp->event);
  185. malidp->event = NULL;
  186. spin_unlock_irq(&drm->event_lock);
  187. }
  188. drm_atomic_helper_commit_hw_done(state);
  189. }
  190. static void malidp_atomic_commit_tail(struct drm_atomic_state *state)
  191. {
  192. struct drm_device *drm = state->dev;
  193. struct malidp_drm *malidp = drm->dev_private;
  194. struct drm_crtc *crtc;
  195. struct drm_crtc_state *old_crtc_state;
  196. int i;
  197. pm_runtime_get_sync(drm->dev);
  198. /*
  199. * set config_valid to a special value to let IRQ handlers
  200. * know that we are updating registers
  201. */
  202. atomic_set(&malidp->config_valid, MALIDP_CONFIG_START);
  203. malidp->dev->hw->set_config_valid(malidp->dev, 0);
  204. drm_atomic_helper_commit_modeset_disables(drm, state);
  205. for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
  206. malidp_atomic_commit_update_gamma(crtc, old_crtc_state);
  207. malidp_atomic_commit_update_coloradj(crtc, old_crtc_state);
  208. malidp_atomic_commit_se_config(crtc, old_crtc_state);
  209. }
  210. drm_atomic_helper_commit_planes(drm, state, DRM_PLANE_COMMIT_ACTIVE_ONLY);
  211. malidp_mw_atomic_commit(drm, state);
  212. drm_atomic_helper_commit_modeset_enables(drm, state);
  213. malidp_atomic_commit_hw_done(state);
  214. pm_runtime_put(drm->dev);
  215. drm_atomic_helper_cleanup_planes(drm, state);
  216. }
  217. static const struct drm_mode_config_helper_funcs malidp_mode_config_helpers = {
  218. .atomic_commit_tail = malidp_atomic_commit_tail,
  219. };
  220. static bool
  221. malidp_verify_afbc_framebuffer_caps(struct drm_device *dev,
  222. const struct drm_mode_fb_cmd2 *mode_cmd)
  223. {
  224. const struct drm_format_info *info;
  225. if ((mode_cmd->modifier[0] >> 56) != DRM_FORMAT_MOD_VENDOR_ARM) {
  226. DRM_DEBUG_KMS("Unknown modifier (not Arm)\n");
  227. return false;
  228. }
  229. if (mode_cmd->modifier[0] &
  230. ~DRM_FORMAT_MOD_ARM_AFBC(AFBC_MOD_VALID_BITS)) {
  231. DRM_DEBUG_KMS("Unsupported modifiers\n");
  232. return false;
  233. }
  234. info = drm_get_format_info(dev, mode_cmd);
  235. if (!info) {
  236. DRM_DEBUG_KMS("Unable to get the format information\n");
  237. return false;
  238. }
  239. if (info->num_planes != 1) {
  240. DRM_DEBUG_KMS("AFBC buffers expect one plane\n");
  241. return false;
  242. }
  243. if (mode_cmd->offsets[0] != 0) {
  244. DRM_DEBUG_KMS("AFBC buffers' plane offset should be 0\n");
  245. return false;
  246. }
  247. switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
  248. case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
  249. if ((mode_cmd->width % 16) || (mode_cmd->height % 16)) {
  250. DRM_DEBUG_KMS("AFBC buffers must be aligned to 16 pixels\n");
  251. return false;
  252. }
  253. break;
  254. default:
  255. DRM_DEBUG_KMS("Unsupported AFBC block size\n");
  256. return false;
  257. }
  258. return true;
  259. }
  260. static bool
  261. malidp_verify_afbc_framebuffer_size(struct drm_device *dev,
  262. struct drm_file *file,
  263. const struct drm_mode_fb_cmd2 *mode_cmd)
  264. {
  265. int n_superblocks = 0;
  266. const struct drm_format_info *info;
  267. struct drm_gem_object *objs = NULL;
  268. u32 afbc_superblock_size = 0, afbc_superblock_height = 0;
  269. u32 afbc_superblock_width = 0, afbc_size = 0;
  270. switch (mode_cmd->modifier[0] & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
  271. case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
  272. afbc_superblock_height = 16;
  273. afbc_superblock_width = 16;
  274. break;
  275. default:
  276. DRM_DEBUG_KMS("AFBC superblock size is not supported\n");
  277. return false;
  278. }
  279. info = drm_get_format_info(dev, mode_cmd);
  280. n_superblocks = (mode_cmd->width / afbc_superblock_width) *
  281. (mode_cmd->height / afbc_superblock_height);
  282. afbc_superblock_size = info->cpp[0] * afbc_superblock_width *
  283. afbc_superblock_height;
  284. afbc_size = ALIGN(n_superblocks * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
  285. afbc_size += n_superblocks * ALIGN(afbc_superblock_size, AFBC_SUPERBLK_ALIGNMENT);
  286. if (mode_cmd->width * info->cpp[0] != mode_cmd->pitches[0]) {
  287. DRM_DEBUG_KMS("Invalid value of pitch (=%u) should be same as width (=%u) * cpp (=%u)\n",
  288. mode_cmd->pitches[0], mode_cmd->width, info->cpp[0]);
  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_unlocked(objs);
  300. return false;
  301. }
  302. drm_gem_object_put_unlocked(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. 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->dev_private;
  332. struct malidp_hw_device *hwdev = malidp->dev;
  333. drm_mode_config_init(drm);
  334. drm->mode_config.min_width = hwdev->min_line_size;
  335. drm->mode_config.min_height = hwdev->min_line_size;
  336. drm->mode_config.max_width = hwdev->max_line_size;
  337. drm->mode_config.max_height = hwdev->max_line_size;
  338. drm->mode_config.funcs = &malidp_mode_config_funcs;
  339. drm->mode_config.helper_private = &malidp_mode_config_helpers;
  340. ret = malidp_crtc_init(drm);
  341. if (ret)
  342. goto crtc_fail;
  343. ret = malidp_mw_connector_init(drm);
  344. if (ret)
  345. goto crtc_fail;
  346. return 0;
  347. crtc_fail:
  348. drm_mode_config_cleanup(drm);
  349. return ret;
  350. }
  351. static void malidp_fini(struct drm_device *drm)
  352. {
  353. drm_mode_config_cleanup(drm);
  354. }
  355. static int malidp_irq_init(struct platform_device *pdev)
  356. {
  357. int irq_de, irq_se, ret = 0;
  358. struct drm_device *drm = dev_get_drvdata(&pdev->dev);
  359. struct malidp_drm *malidp = drm->dev_private;
  360. struct malidp_hw_device *hwdev = malidp->dev;
  361. /* fetch the interrupts from DT */
  362. irq_de = platform_get_irq_byname(pdev, "DE");
  363. if (irq_de < 0) {
  364. DRM_ERROR("no 'DE' IRQ specified!\n");
  365. return irq_de;
  366. }
  367. irq_se = platform_get_irq_byname(pdev, "SE");
  368. if (irq_se < 0) {
  369. DRM_ERROR("no 'SE' IRQ specified!\n");
  370. return irq_se;
  371. }
  372. ret = malidp_de_irq_init(drm, irq_de);
  373. if (ret)
  374. return ret;
  375. ret = malidp_se_irq_init(drm, irq_se);
  376. if (ret) {
  377. malidp_de_irq_fini(hwdev);
  378. return ret;
  379. }
  380. return 0;
  381. }
  382. DEFINE_DRM_GEM_CMA_FOPS(fops);
  383. static int malidp_dumb_create(struct drm_file *file_priv,
  384. struct drm_device *drm,
  385. struct drm_mode_create_dumb *args)
  386. {
  387. struct malidp_drm *malidp = drm->dev_private;
  388. /* allocate for the worst case scenario, i.e. rotated buffers */
  389. u8 alignment = malidp_hw_get_pitch_align(malidp->dev, 1);
  390. args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), alignment);
  391. return drm_gem_cma_dumb_create_internal(file_priv, drm, args);
  392. }
  393. #ifdef CONFIG_DEBUG_FS
  394. static void malidp_error_stats_init(struct malidp_error_stats *error_stats)
  395. {
  396. error_stats->num_errors = 0;
  397. error_stats->last_error_status = 0;
  398. error_stats->last_error_vblank = -1;
  399. }
  400. void malidp_error(struct malidp_drm *malidp,
  401. struct malidp_error_stats *error_stats, u32 status,
  402. u64 vblank)
  403. {
  404. unsigned long irqflags;
  405. spin_lock_irqsave(&malidp->errors_lock, irqflags);
  406. error_stats->last_error_status = status;
  407. error_stats->last_error_vblank = vblank;
  408. error_stats->num_errors++;
  409. spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
  410. }
  411. void malidp_error_stats_dump(const char *prefix,
  412. struct malidp_error_stats error_stats,
  413. struct seq_file *m)
  414. {
  415. seq_printf(m, "[%s] num_errors : %d\n", prefix,
  416. error_stats.num_errors);
  417. seq_printf(m, "[%s] last_error_status : 0x%08x\n", prefix,
  418. error_stats.last_error_status);
  419. seq_printf(m, "[%s] last_error_vblank : %lld\n", prefix,
  420. error_stats.last_error_vblank);
  421. }
  422. static int malidp_show_stats(struct seq_file *m, void *arg)
  423. {
  424. struct drm_device *drm = m->private;
  425. struct malidp_drm *malidp = drm->dev_private;
  426. unsigned long irqflags;
  427. struct malidp_error_stats de_errors, se_errors;
  428. spin_lock_irqsave(&malidp->errors_lock, irqflags);
  429. de_errors = malidp->de_errors;
  430. se_errors = malidp->se_errors;
  431. spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
  432. malidp_error_stats_dump("DE", de_errors, m);
  433. malidp_error_stats_dump("SE", se_errors, m);
  434. return 0;
  435. }
  436. static int malidp_debugfs_open(struct inode *inode, struct file *file)
  437. {
  438. return single_open(file, malidp_show_stats, inode->i_private);
  439. }
  440. static ssize_t malidp_debugfs_write(struct file *file, const char __user *ubuf,
  441. size_t len, loff_t *offp)
  442. {
  443. struct seq_file *m = file->private_data;
  444. struct drm_device *drm = m->private;
  445. struct malidp_drm *malidp = drm->dev_private;
  446. unsigned long irqflags;
  447. spin_lock_irqsave(&malidp->errors_lock, irqflags);
  448. malidp_error_stats_init(&malidp->de_errors);
  449. malidp_error_stats_init(&malidp->se_errors);
  450. spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
  451. return len;
  452. }
  453. static const struct file_operations malidp_debugfs_fops = {
  454. .owner = THIS_MODULE,
  455. .open = malidp_debugfs_open,
  456. .read = seq_read,
  457. .write = malidp_debugfs_write,
  458. .llseek = seq_lseek,
  459. .release = single_release,
  460. };
  461. static int malidp_debugfs_init(struct drm_minor *minor)
  462. {
  463. struct malidp_drm *malidp = minor->dev->dev_private;
  464. struct dentry *dentry = NULL;
  465. malidp_error_stats_init(&malidp->de_errors);
  466. malidp_error_stats_init(&malidp->se_errors);
  467. spin_lock_init(&malidp->errors_lock);
  468. dentry = debugfs_create_file("debug",
  469. S_IRUGO | S_IWUSR,
  470. minor->debugfs_root, minor->dev,
  471. &malidp_debugfs_fops);
  472. if (!dentry) {
  473. DRM_ERROR("Cannot create debug file\n");
  474. return -ENOMEM;
  475. }
  476. return 0;
  477. }
  478. #endif //CONFIG_DEBUG_FS
  479. static struct drm_driver malidp_driver = {
  480. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC |
  481. DRIVER_PRIME,
  482. .gem_free_object_unlocked = drm_gem_cma_free_object,
  483. .gem_vm_ops = &drm_gem_cma_vm_ops,
  484. .dumb_create = malidp_dumb_create,
  485. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  486. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  487. .gem_prime_export = drm_gem_prime_export,
  488. .gem_prime_import = drm_gem_prime_import,
  489. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  490. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  491. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  492. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  493. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  494. #ifdef CONFIG_DEBUG_FS
  495. .debugfs_init = malidp_debugfs_init,
  496. #endif
  497. .fops = &fops,
  498. .name = "mali-dp",
  499. .desc = "ARM Mali Display Processor driver",
  500. .date = "20160106",
  501. .major = 1,
  502. .minor = 0,
  503. };
  504. static const struct of_device_id malidp_drm_of_match[] = {
  505. {
  506. .compatible = "arm,mali-dp500",
  507. .data = &malidp_device[MALIDP_500]
  508. },
  509. {
  510. .compatible = "arm,mali-dp550",
  511. .data = &malidp_device[MALIDP_550]
  512. },
  513. {
  514. .compatible = "arm,mali-dp650",
  515. .data = &malidp_device[MALIDP_650]
  516. },
  517. {},
  518. };
  519. MODULE_DEVICE_TABLE(of, malidp_drm_of_match);
  520. static bool malidp_is_compatible_hw_id(struct malidp_hw_device *hwdev,
  521. const struct of_device_id *dev_id)
  522. {
  523. u32 core_id;
  524. const char *compatstr_dp500 = "arm,mali-dp500";
  525. bool is_dp500;
  526. bool dt_is_dp500;
  527. /*
  528. * The DP500 CORE_ID register is in a different location, so check it
  529. * first. If the product id field matches, then this is DP500, otherwise
  530. * check the DP550/650 CORE_ID register.
  531. */
  532. core_id = malidp_hw_read(hwdev, MALIDP500_DC_BASE + MALIDP_DE_CORE_ID);
  533. /* Offset 0x18 will never read 0x500 on products other than DP500. */
  534. is_dp500 = (MALIDP_PRODUCT_ID(core_id) == 0x500);
  535. dt_is_dp500 = strnstr(dev_id->compatible, compatstr_dp500,
  536. sizeof(dev_id->compatible)) != NULL;
  537. if (is_dp500 != dt_is_dp500) {
  538. DRM_ERROR("Device-tree expects %s, but hardware %s DP500.\n",
  539. dev_id->compatible, is_dp500 ? "is" : "is not");
  540. return false;
  541. } else if (!dt_is_dp500) {
  542. u16 product_id;
  543. char buf[32];
  544. core_id = malidp_hw_read(hwdev,
  545. MALIDP550_DC_BASE + MALIDP_DE_CORE_ID);
  546. product_id = MALIDP_PRODUCT_ID(core_id);
  547. snprintf(buf, sizeof(buf), "arm,mali-dp%X", product_id);
  548. if (!strnstr(dev_id->compatible, buf,
  549. sizeof(dev_id->compatible))) {
  550. DRM_ERROR("Device-tree expects %s, but hardware is DP%03X.\n",
  551. dev_id->compatible, product_id);
  552. return false;
  553. }
  554. }
  555. return true;
  556. }
  557. static bool malidp_has_sufficient_address_space(const struct resource *res,
  558. const struct of_device_id *dev_id)
  559. {
  560. resource_size_t res_size = resource_size(res);
  561. const char *compatstr_dp500 = "arm,mali-dp500";
  562. if (!strnstr(dev_id->compatible, compatstr_dp500,
  563. sizeof(dev_id->compatible)))
  564. return res_size >= MALIDP550_ADDR_SPACE_SIZE;
  565. else if (res_size < MALIDP500_ADDR_SPACE_SIZE)
  566. return false;
  567. return true;
  568. }
  569. static ssize_t core_id_show(struct device *dev, struct device_attribute *attr,
  570. char *buf)
  571. {
  572. struct drm_device *drm = dev_get_drvdata(dev);
  573. struct malidp_drm *malidp = drm->dev_private;
  574. return snprintf(buf, PAGE_SIZE, "%08x\n", malidp->core_id);
  575. }
  576. DEVICE_ATTR_RO(core_id);
  577. static int malidp_init_sysfs(struct device *dev)
  578. {
  579. int ret = device_create_file(dev, &dev_attr_core_id);
  580. if (ret)
  581. DRM_ERROR("failed to create device file for core_id\n");
  582. return ret;
  583. }
  584. static void malidp_fini_sysfs(struct device *dev)
  585. {
  586. device_remove_file(dev, &dev_attr_core_id);
  587. }
  588. #define MAX_OUTPUT_CHANNELS 3
  589. static int malidp_runtime_pm_suspend(struct device *dev)
  590. {
  591. struct drm_device *drm = dev_get_drvdata(dev);
  592. struct malidp_drm *malidp = drm->dev_private;
  593. struct malidp_hw_device *hwdev = malidp->dev;
  594. /* we can only suspend if the hardware is in config mode */
  595. WARN_ON(!hwdev->hw->in_config_mode(hwdev));
  596. malidp_se_irq_fini(hwdev);
  597. malidp_de_irq_fini(hwdev);
  598. hwdev->pm_suspended = true;
  599. clk_disable_unprepare(hwdev->mclk);
  600. clk_disable_unprepare(hwdev->aclk);
  601. clk_disable_unprepare(hwdev->pclk);
  602. return 0;
  603. }
  604. static int malidp_runtime_pm_resume(struct device *dev)
  605. {
  606. struct drm_device *drm = dev_get_drvdata(dev);
  607. struct malidp_drm *malidp = drm->dev_private;
  608. struct malidp_hw_device *hwdev = malidp->dev;
  609. clk_prepare_enable(hwdev->pclk);
  610. clk_prepare_enable(hwdev->aclk);
  611. clk_prepare_enable(hwdev->mclk);
  612. hwdev->pm_suspended = false;
  613. malidp_de_irq_hw_init(hwdev);
  614. malidp_se_irq_hw_init(hwdev);
  615. return 0;
  616. }
  617. static int malidp_bind(struct device *dev)
  618. {
  619. struct resource *res;
  620. struct drm_device *drm;
  621. struct malidp_drm *malidp;
  622. struct malidp_hw_device *hwdev;
  623. struct platform_device *pdev = to_platform_device(dev);
  624. struct of_device_id const *dev_id;
  625. struct drm_encoder *encoder;
  626. /* number of lines for the R, G and B output */
  627. u8 output_width[MAX_OUTPUT_CHANNELS];
  628. int ret = 0, i;
  629. u32 version, out_depth = 0;
  630. malidp = devm_kzalloc(dev, sizeof(*malidp), GFP_KERNEL);
  631. if (!malidp)
  632. return -ENOMEM;
  633. hwdev = devm_kzalloc(dev, sizeof(*hwdev), GFP_KERNEL);
  634. if (!hwdev)
  635. return -ENOMEM;
  636. hwdev->hw = (struct malidp_hw *)of_device_get_match_data(dev);
  637. malidp->dev = hwdev;
  638. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  639. hwdev->regs = devm_ioremap_resource(dev, res);
  640. if (IS_ERR(hwdev->regs))
  641. return PTR_ERR(hwdev->regs);
  642. hwdev->pclk = devm_clk_get(dev, "pclk");
  643. if (IS_ERR(hwdev->pclk))
  644. return PTR_ERR(hwdev->pclk);
  645. hwdev->aclk = devm_clk_get(dev, "aclk");
  646. if (IS_ERR(hwdev->aclk))
  647. return PTR_ERR(hwdev->aclk);
  648. hwdev->mclk = devm_clk_get(dev, "mclk");
  649. if (IS_ERR(hwdev->mclk))
  650. return PTR_ERR(hwdev->mclk);
  651. hwdev->pxlclk = devm_clk_get(dev, "pxlclk");
  652. if (IS_ERR(hwdev->pxlclk))
  653. return PTR_ERR(hwdev->pxlclk);
  654. /* Get the optional framebuffer memory resource */
  655. ret = of_reserved_mem_device_init(dev);
  656. if (ret && ret != -ENODEV)
  657. return ret;
  658. drm = drm_dev_alloc(&malidp_driver, dev);
  659. if (IS_ERR(drm)) {
  660. ret = PTR_ERR(drm);
  661. goto alloc_fail;
  662. }
  663. drm->dev_private = malidp;
  664. dev_set_drvdata(dev, drm);
  665. /* Enable power management */
  666. pm_runtime_enable(dev);
  667. /* Resume device to enable the clocks */
  668. if (pm_runtime_enabled(dev))
  669. pm_runtime_get_sync(dev);
  670. else
  671. malidp_runtime_pm_resume(dev);
  672. dev_id = of_match_device(malidp_drm_of_match, dev);
  673. if (!dev_id) {
  674. ret = -EINVAL;
  675. goto query_hw_fail;
  676. }
  677. if (!malidp_has_sufficient_address_space(res, dev_id)) {
  678. DRM_ERROR("Insufficient address space in device-tree.\n");
  679. ret = -EINVAL;
  680. goto query_hw_fail;
  681. }
  682. if (!malidp_is_compatible_hw_id(hwdev, dev_id)) {
  683. ret = -EINVAL;
  684. goto query_hw_fail;
  685. }
  686. ret = hwdev->hw->query_hw(hwdev);
  687. if (ret) {
  688. DRM_ERROR("Invalid HW configuration\n");
  689. goto query_hw_fail;
  690. }
  691. version = malidp_hw_read(hwdev, hwdev->hw->map.dc_base + MALIDP_DE_CORE_ID);
  692. DRM_INFO("found ARM Mali-DP%3x version r%dp%d\n", version >> 16,
  693. (version >> 12) & 0xf, (version >> 8) & 0xf);
  694. malidp->core_id = version;
  695. /* set the number of lines used for output of RGB data */
  696. ret = of_property_read_u8_array(dev->of_node,
  697. "arm,malidp-output-port-lines",
  698. output_width, MAX_OUTPUT_CHANNELS);
  699. if (ret)
  700. goto query_hw_fail;
  701. for (i = 0; i < MAX_OUTPUT_CHANNELS; i++)
  702. out_depth = (out_depth << 8) | (output_width[i] & 0xf);
  703. malidp_hw_write(hwdev, out_depth, hwdev->hw->map.out_depth_base);
  704. hwdev->output_color_depth = out_depth;
  705. atomic_set(&malidp->config_valid, MALIDP_CONFIG_VALID_INIT);
  706. init_waitqueue_head(&malidp->wq);
  707. ret = malidp_init(drm);
  708. if (ret < 0)
  709. goto query_hw_fail;
  710. ret = malidp_init_sysfs(dev);
  711. if (ret)
  712. goto init_fail;
  713. /* Set the CRTC's port so that the encoder component can find it */
  714. malidp->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
  715. ret = component_bind_all(dev, drm);
  716. if (ret) {
  717. DRM_ERROR("Failed to bind all components\n");
  718. goto bind_fail;
  719. }
  720. /* We expect to have a maximum of two encoders one for the actual
  721. * display and a virtual one for the writeback connector
  722. */
  723. WARN_ON(drm->mode_config.num_encoder > 2);
  724. list_for_each_entry(encoder, &drm->mode_config.encoder_list, head) {
  725. encoder->possible_clones =
  726. (1 << drm->mode_config.num_encoder) - 1;
  727. }
  728. ret = malidp_irq_init(pdev);
  729. if (ret < 0)
  730. goto irq_init_fail;
  731. drm->irq_enabled = true;
  732. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  733. drm_crtc_vblank_reset(&malidp->crtc);
  734. if (ret < 0) {
  735. DRM_ERROR("failed to initialise vblank\n");
  736. goto vblank_fail;
  737. }
  738. pm_runtime_put(dev);
  739. drm_mode_config_reset(drm);
  740. drm_kms_helper_poll_init(drm);
  741. ret = drm_dev_register(drm, 0);
  742. if (ret)
  743. goto register_fail;
  744. drm_fbdev_generic_setup(drm, 32);
  745. return 0;
  746. register_fail:
  747. drm_kms_helper_poll_fini(drm);
  748. pm_runtime_get_sync(dev);
  749. vblank_fail:
  750. malidp_se_irq_fini(hwdev);
  751. malidp_de_irq_fini(hwdev);
  752. drm->irq_enabled = false;
  753. irq_init_fail:
  754. drm_atomic_helper_shutdown(drm);
  755. component_unbind_all(dev, drm);
  756. bind_fail:
  757. of_node_put(malidp->crtc.port);
  758. malidp->crtc.port = NULL;
  759. init_fail:
  760. malidp_fini_sysfs(dev);
  761. malidp_fini(drm);
  762. query_hw_fail:
  763. pm_runtime_put(dev);
  764. if (pm_runtime_enabled(dev))
  765. pm_runtime_disable(dev);
  766. else
  767. malidp_runtime_pm_suspend(dev);
  768. drm->dev_private = NULL;
  769. dev_set_drvdata(dev, NULL);
  770. drm_dev_put(drm);
  771. alloc_fail:
  772. of_reserved_mem_device_release(dev);
  773. return ret;
  774. }
  775. static void malidp_unbind(struct device *dev)
  776. {
  777. struct drm_device *drm = dev_get_drvdata(dev);
  778. struct malidp_drm *malidp = drm->dev_private;
  779. struct malidp_hw_device *hwdev = malidp->dev;
  780. drm_dev_unregister(drm);
  781. drm_kms_helper_poll_fini(drm);
  782. pm_runtime_get_sync(dev);
  783. drm_crtc_vblank_off(&malidp->crtc);
  784. malidp_se_irq_fini(hwdev);
  785. malidp_de_irq_fini(hwdev);
  786. drm->irq_enabled = false;
  787. drm_atomic_helper_shutdown(drm);
  788. component_unbind_all(dev, drm);
  789. of_node_put(malidp->crtc.port);
  790. malidp->crtc.port = NULL;
  791. malidp_fini_sysfs(dev);
  792. malidp_fini(drm);
  793. pm_runtime_put(dev);
  794. if (pm_runtime_enabled(dev))
  795. pm_runtime_disable(dev);
  796. else
  797. malidp_runtime_pm_suspend(dev);
  798. drm->dev_private = NULL;
  799. dev_set_drvdata(dev, NULL);
  800. drm_dev_put(drm);
  801. of_reserved_mem_device_release(dev);
  802. }
  803. static const struct component_master_ops malidp_master_ops = {
  804. .bind = malidp_bind,
  805. .unbind = malidp_unbind,
  806. };
  807. static int malidp_compare_dev(struct device *dev, void *data)
  808. {
  809. struct device_node *np = data;
  810. return dev->of_node == np;
  811. }
  812. static int malidp_platform_probe(struct platform_device *pdev)
  813. {
  814. struct device_node *port;
  815. struct component_match *match = NULL;
  816. if (!pdev->dev.of_node)
  817. return -ENODEV;
  818. /* there is only one output port inside each device, find it */
  819. port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
  820. if (!port)
  821. return -ENODEV;
  822. drm_of_component_match_add(&pdev->dev, &match, malidp_compare_dev,
  823. port);
  824. of_node_put(port);
  825. return component_master_add_with_match(&pdev->dev, &malidp_master_ops,
  826. match);
  827. }
  828. static int malidp_platform_remove(struct platform_device *pdev)
  829. {
  830. component_master_del(&pdev->dev, &malidp_master_ops);
  831. return 0;
  832. }
  833. static int __maybe_unused malidp_pm_suspend(struct device *dev)
  834. {
  835. struct drm_device *drm = dev_get_drvdata(dev);
  836. return drm_mode_config_helper_suspend(drm);
  837. }
  838. static int __maybe_unused malidp_pm_resume(struct device *dev)
  839. {
  840. struct drm_device *drm = dev_get_drvdata(dev);
  841. drm_mode_config_helper_resume(drm);
  842. return 0;
  843. }
  844. static int __maybe_unused malidp_pm_suspend_late(struct device *dev)
  845. {
  846. if (!pm_runtime_status_suspended(dev)) {
  847. malidp_runtime_pm_suspend(dev);
  848. pm_runtime_set_suspended(dev);
  849. }
  850. return 0;
  851. }
  852. static int __maybe_unused malidp_pm_resume_early(struct device *dev)
  853. {
  854. malidp_runtime_pm_resume(dev);
  855. pm_runtime_set_active(dev);
  856. return 0;
  857. }
  858. static const struct dev_pm_ops malidp_pm_ops = {
  859. SET_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend, malidp_pm_resume) \
  860. SET_LATE_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend_late, malidp_pm_resume_early) \
  861. SET_RUNTIME_PM_OPS(malidp_runtime_pm_suspend, malidp_runtime_pm_resume, NULL)
  862. };
  863. static struct platform_driver malidp_platform_driver = {
  864. .probe = malidp_platform_probe,
  865. .remove = malidp_platform_remove,
  866. .driver = {
  867. .name = "mali-dp",
  868. .pm = &malidp_pm_ops,
  869. .of_match_table = malidp_drm_of_match,
  870. },
  871. };
  872. module_platform_driver(malidp_platform_driver);
  873. MODULE_AUTHOR("Liviu Dudau <Liviu.Dudau@arm.com>");
  874. MODULE_DESCRIPTION("ARM Mali DP DRM driver");
  875. MODULE_LICENSE("GPL v2");