malidp_drv.c 25 KB

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