omap_drv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
  4. * Author: Rob Clark <rob@ti.com>
  5. */
  6. #include <linux/dma-mapping.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/of.h>
  9. #include <linux/sort.h>
  10. #include <linux/sys_soc.h>
  11. #include <drm/drm_atomic.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_bridge.h>
  14. #include <drm/drm_bridge_connector.h>
  15. #include <drm/drm_drv.h>
  16. #include <drm/drm_file.h>
  17. #include <drm/drm_ioctl.h>
  18. #include <drm/drm_panel.h>
  19. #include <drm/drm_prime.h>
  20. #include <drm/drm_print.h>
  21. #include <drm/drm_probe_helper.h>
  22. #include <drm/drm_vblank.h>
  23. #include "omap_dmm_tiler.h"
  24. #include "omap_drv.h"
  25. #include "omap_fbdev.h"
  26. #define DRIVER_NAME MODULE_NAME
  27. #define DRIVER_DESC "OMAP DRM"
  28. #define DRIVER_MAJOR 1
  29. #define DRIVER_MINOR 0
  30. #define DRIVER_PATCHLEVEL 0
  31. /*
  32. * mode config funcs
  33. */
  34. /* Notes about mapping DSS and DRM entities:
  35. * CRTC: overlay
  36. * encoder: manager.. with some extension to allow one primary CRTC
  37. * and zero or more video CRTC's to be mapped to one encoder?
  38. * connector: dssdev.. manager can be attached/detached from different
  39. * devices
  40. */
  41. static void omap_atomic_wait_for_completion(struct drm_device *dev,
  42. struct drm_atomic_state *old_state)
  43. {
  44. struct drm_crtc_state *new_crtc_state;
  45. struct drm_crtc *crtc;
  46. unsigned int i;
  47. int ret;
  48. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  49. if (!new_crtc_state->active)
  50. continue;
  51. ret = omap_crtc_wait_pending(crtc);
  52. if (!ret)
  53. dev_warn(dev->dev,
  54. "atomic complete timeout (pipe %u)!\n", i);
  55. }
  56. }
  57. static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
  58. {
  59. struct drm_device *dev = old_state->dev;
  60. struct omap_drm_private *priv = dev->dev_private;
  61. dispc_runtime_get(priv->dispc);
  62. /* Apply the atomic update. */
  63. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  64. if (priv->omaprev != 0x3430) {
  65. /* With the current dss dispc implementation we have to enable
  66. * the new modeset before we can commit planes. The dispc ovl
  67. * configuration relies on the video mode configuration been
  68. * written into the HW when the ovl configuration is
  69. * calculated.
  70. *
  71. * This approach is not ideal because after a mode change the
  72. * plane update is executed only after the first vblank
  73. * interrupt. The dispc implementation should be fixed so that
  74. * it is able use uncommitted drm state information.
  75. */
  76. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  77. omap_atomic_wait_for_completion(dev, old_state);
  78. drm_atomic_helper_commit_planes(dev, old_state, 0);
  79. drm_atomic_helper_commit_hw_done(old_state);
  80. } else {
  81. /*
  82. * OMAP3 DSS seems to have issues with the work-around above,
  83. * resulting in endless sync losts if a crtc is enabled without
  84. * a plane. For now, skip the WA for OMAP3.
  85. */
  86. drm_atomic_helper_commit_planes(dev, old_state, 0);
  87. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  88. drm_atomic_helper_commit_hw_done(old_state);
  89. }
  90. /*
  91. * Wait for completion of the page flips to ensure that old buffers
  92. * can't be touched by the hardware anymore before cleaning up planes.
  93. */
  94. omap_atomic_wait_for_completion(dev, old_state);
  95. drm_atomic_helper_cleanup_planes(dev, old_state);
  96. dispc_runtime_put(priv->dispc);
  97. }
  98. static int drm_atomic_state_normalized_zpos_cmp(const void *a, const void *b)
  99. {
  100. const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
  101. const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
  102. if (sa->normalized_zpos != sb->normalized_zpos)
  103. return sa->normalized_zpos - sb->normalized_zpos;
  104. else
  105. return sa->plane->base.id - sb->plane->base.id;
  106. }
  107. /*
  108. * This replaces the drm_atomic_normalize_zpos to handle the dual overlay case.
  109. *
  110. * Since both halves need to be 'appear' side by side the zpos is
  111. * recalculated when dealing with dual overlay cases so that the other
  112. * planes zpos is consistent.
  113. */
  114. static int omap_atomic_update_normalize_zpos(struct drm_device *dev,
  115. struct drm_atomic_state *state)
  116. {
  117. struct drm_crtc *crtc;
  118. struct drm_crtc_state *old_state, *new_state;
  119. struct drm_plane *plane;
  120. int c, i, n, inc;
  121. int total_planes = dev->mode_config.num_total_plane;
  122. struct drm_plane_state **states;
  123. int ret = 0;
  124. states = kmalloc_objs(*states, total_planes);
  125. if (!states)
  126. return -ENOMEM;
  127. for_each_oldnew_crtc_in_state(state, crtc, old_state, new_state, c) {
  128. if (old_state->plane_mask == new_state->plane_mask &&
  129. !new_state->zpos_changed)
  130. continue;
  131. /* Reset plane increment and index value for every crtc */
  132. n = 0;
  133. /*
  134. * Normalization process might create new states for planes
  135. * which normalized_zpos has to be recalculated.
  136. */
  137. drm_for_each_plane_mask(plane, dev, new_state->plane_mask) {
  138. struct drm_plane_state *plane_state =
  139. drm_atomic_get_plane_state(new_state->state,
  140. plane);
  141. if (IS_ERR(plane_state)) {
  142. ret = PTR_ERR(plane_state);
  143. goto done;
  144. }
  145. states[n++] = plane_state;
  146. }
  147. sort(states, n, sizeof(*states),
  148. drm_atomic_state_normalized_zpos_cmp, NULL);
  149. for (i = 0, inc = 0; i < n; i++) {
  150. plane = states[i]->plane;
  151. states[i]->normalized_zpos = i + inc;
  152. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] updated normalized zpos value %d\n",
  153. plane->base.id, plane->name,
  154. states[i]->normalized_zpos);
  155. if (is_omap_plane_dual_overlay(states[i]))
  156. inc++;
  157. }
  158. new_state->zpos_changed = true;
  159. }
  160. done:
  161. kfree(states);
  162. return ret;
  163. }
  164. static int omap_atomic_check(struct drm_device *dev,
  165. struct drm_atomic_state *state)
  166. {
  167. int ret;
  168. ret = drm_atomic_helper_check(dev, state);
  169. if (ret)
  170. return ret;
  171. if (dev->mode_config.normalize_zpos) {
  172. ret = omap_atomic_update_normalize_zpos(dev, state);
  173. if (ret)
  174. return ret;
  175. }
  176. return 0;
  177. }
  178. static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
  179. .atomic_commit_tail = omap_atomic_commit_tail,
  180. };
  181. static const struct drm_mode_config_funcs omap_mode_config_funcs = {
  182. .fb_create = omap_framebuffer_create,
  183. .atomic_check = omap_atomic_check,
  184. .atomic_commit = drm_atomic_helper_commit,
  185. };
  186. /* Global/shared object state funcs */
  187. /*
  188. * This is a helper that returns the private state currently in operation.
  189. * Note that this would return the "old_state" if called in the atomic check
  190. * path, and the "new_state" after the atomic swap has been done.
  191. */
  192. struct omap_global_state *
  193. omap_get_existing_global_state(struct omap_drm_private *priv)
  194. {
  195. return to_omap_global_state(priv->glob_obj.state);
  196. }
  197. /*
  198. * This acquires the modeset lock set aside for global state, creates
  199. * a new duplicated private object state.
  200. */
  201. struct omap_global_state *__must_check
  202. omap_get_global_state(struct drm_atomic_state *s)
  203. {
  204. struct omap_drm_private *priv = s->dev->dev_private;
  205. struct drm_private_state *priv_state;
  206. priv_state = drm_atomic_get_private_obj_state(s, &priv->glob_obj);
  207. if (IS_ERR(priv_state))
  208. return ERR_CAST(priv_state);
  209. return to_omap_global_state(priv_state);
  210. }
  211. static struct drm_private_state *
  212. omap_global_duplicate_state(struct drm_private_obj *obj)
  213. {
  214. struct omap_global_state *state;
  215. state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
  216. if (!state)
  217. return NULL;
  218. __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
  219. return &state->base;
  220. }
  221. static void omap_global_destroy_state(struct drm_private_obj *obj,
  222. struct drm_private_state *state)
  223. {
  224. struct omap_global_state *omap_state = to_omap_global_state(state);
  225. kfree(omap_state);
  226. }
  227. static const struct drm_private_state_funcs omap_global_state_funcs = {
  228. .atomic_duplicate_state = omap_global_duplicate_state,
  229. .atomic_destroy_state = omap_global_destroy_state,
  230. };
  231. static int omap_global_obj_init(struct drm_device *dev)
  232. {
  233. struct omap_drm_private *priv = dev->dev_private;
  234. struct omap_global_state *state;
  235. state = kzalloc_obj(*state);
  236. if (!state)
  237. return -ENOMEM;
  238. drm_atomic_private_obj_init(dev, &priv->glob_obj, &state->base,
  239. &omap_global_state_funcs);
  240. return 0;
  241. }
  242. static void omap_global_obj_fini(struct omap_drm_private *priv)
  243. {
  244. drm_atomic_private_obj_fini(&priv->glob_obj);
  245. }
  246. static void omap_disconnect_pipelines(struct drm_device *ddev)
  247. {
  248. struct omap_drm_private *priv = ddev->dev_private;
  249. unsigned int i;
  250. for (i = 0; i < priv->num_pipes; i++) {
  251. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  252. omapdss_device_disconnect(priv->dss, pipe->output);
  253. omapdss_device_put(pipe->output);
  254. pipe->output = NULL;
  255. }
  256. memset(&priv->channels, 0, sizeof(priv->channels));
  257. priv->num_pipes = 0;
  258. }
  259. static int omap_connect_pipelines(struct drm_device *ddev)
  260. {
  261. struct omap_drm_private *priv = ddev->dev_private;
  262. struct omap_dss_device *output = NULL;
  263. int r;
  264. for_each_dss_output(output) {
  265. r = omapdss_device_connect(priv->dss, output);
  266. if (r == -EPROBE_DEFER) {
  267. omapdss_device_put(output);
  268. return r;
  269. } else if (r) {
  270. dev_warn(output->dev, "could not connect output %s\n",
  271. output->name);
  272. } else {
  273. struct omap_drm_pipeline *pipe;
  274. pipe = &priv->pipes[priv->num_pipes++];
  275. pipe->output = omapdss_device_get(output);
  276. if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
  277. /* To balance the 'for_each_dss_output' loop */
  278. omapdss_device_put(output);
  279. break;
  280. }
  281. }
  282. }
  283. return 0;
  284. }
  285. static int omap_compare_pipelines(const void *a, const void *b)
  286. {
  287. const struct omap_drm_pipeline *pipe1 = a;
  288. const struct omap_drm_pipeline *pipe2 = b;
  289. if (pipe1->alias_id > pipe2->alias_id)
  290. return 1;
  291. else if (pipe1->alias_id < pipe2->alias_id)
  292. return -1;
  293. return 0;
  294. }
  295. static int omap_modeset_init_properties(struct drm_device *dev)
  296. {
  297. struct omap_drm_private *priv = dev->dev_private;
  298. unsigned int num_planes = dispc_get_num_ovls(priv->dispc);
  299. priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
  300. num_planes - 1);
  301. if (!priv->zorder_prop)
  302. return -ENOMEM;
  303. return 0;
  304. }
  305. static int omap_display_id(struct omap_dss_device *output)
  306. {
  307. struct device_node *node = NULL;
  308. if (output->bridge) {
  309. struct drm_bridge *bridge __free(drm_bridge_put) =
  310. drm_bridge_chain_get_last_bridge(output->bridge->encoder);
  311. node = bridge->of_node;
  312. }
  313. return node ? of_alias_get_id(node, "display") : -ENODEV;
  314. }
  315. static int omap_modeset_init(struct drm_device *dev)
  316. {
  317. struct omap_drm_private *priv = dev->dev_private;
  318. int num_ovls = dispc_get_num_ovls(priv->dispc);
  319. int num_mgrs = dispc_get_num_mgrs(priv->dispc);
  320. unsigned int i;
  321. int ret;
  322. u32 plane_crtc_mask;
  323. if (!omapdss_stack_is_ready())
  324. return -EPROBE_DEFER;
  325. ret = omap_modeset_init_properties(dev);
  326. if (ret < 0)
  327. return ret;
  328. /*
  329. * This function creates exactly one connector, encoder, crtc,
  330. * and primary plane per each connected dss-device. Each
  331. * connector->encoder->crtc chain is expected to be separate
  332. * and each crtc is connect to a single dss-channel. If the
  333. * configuration does not match the expectations or exceeds
  334. * the available resources, the configuration is rejected.
  335. */
  336. ret = omap_connect_pipelines(dev);
  337. if (ret < 0)
  338. return ret;
  339. if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
  340. dev_err(dev->dev, "%s(): Too many connected displays\n",
  341. __func__);
  342. return -EINVAL;
  343. }
  344. /* Create all planes first. They can all be put to any CRTC. */
  345. plane_crtc_mask = (1 << priv->num_pipes) - 1;
  346. for (i = 0; i < num_ovls; i++) {
  347. enum drm_plane_type type = i < priv->num_pipes
  348. ? DRM_PLANE_TYPE_PRIMARY
  349. : DRM_PLANE_TYPE_OVERLAY;
  350. struct drm_plane *plane;
  351. if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
  352. return -EINVAL;
  353. plane = omap_plane_init(dev, i, type, plane_crtc_mask);
  354. if (IS_ERR(plane))
  355. return PTR_ERR(plane);
  356. priv->planes[priv->num_planes++] = plane;
  357. }
  358. /*
  359. * Create the encoders, attach the bridges and get the pipeline alias
  360. * IDs.
  361. */
  362. for (i = 0; i < priv->num_pipes; i++) {
  363. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  364. int id;
  365. pipe->encoder = omap_encoder_init(dev, pipe->output);
  366. if (!pipe->encoder)
  367. return -ENOMEM;
  368. if (pipe->output->bridge) {
  369. ret = drm_bridge_attach(pipe->encoder,
  370. pipe->output->bridge, NULL,
  371. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  372. if (ret < 0)
  373. return ret;
  374. }
  375. id = omap_display_id(pipe->output);
  376. pipe->alias_id = id >= 0 ? id : i;
  377. }
  378. /* Sort the pipelines by DT aliases. */
  379. sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
  380. omap_compare_pipelines, NULL);
  381. /*
  382. * Populate the pipeline lookup table by DISPC channel. Only one display
  383. * is allowed per channel.
  384. */
  385. for (i = 0; i < priv->num_pipes; ++i) {
  386. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  387. enum omap_channel channel = pipe->output->dispc_channel;
  388. if (WARN_ON(priv->channels[channel] != NULL))
  389. return -EINVAL;
  390. priv->channels[channel] = pipe;
  391. }
  392. /* Create the connectors and CRTCs. */
  393. for (i = 0; i < priv->num_pipes; i++) {
  394. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  395. struct drm_encoder *encoder = pipe->encoder;
  396. struct drm_crtc *crtc;
  397. pipe->connector = drm_bridge_connector_init(dev, encoder);
  398. if (IS_ERR(pipe->connector)) {
  399. dev_err(priv->dev,
  400. "unable to create bridge connector for %s\n",
  401. pipe->output->name);
  402. return PTR_ERR(pipe->connector);
  403. }
  404. drm_connector_attach_encoder(pipe->connector, encoder);
  405. crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
  406. if (IS_ERR(crtc))
  407. return PTR_ERR(crtc);
  408. encoder->possible_crtcs = 1 << i;
  409. pipe->crtc = crtc;
  410. }
  411. DBG("registered %u planes, %u crtcs/encoders/connectors\n",
  412. priv->num_planes, priv->num_pipes);
  413. dev->mode_config.min_width = 8;
  414. dev->mode_config.min_height = 2;
  415. /*
  416. * Note: these values are used for multiple independent things:
  417. * connector mode filtering, buffer sizes, crtc sizes...
  418. * Use big enough values here to cover all use cases, and do more
  419. * specific checking in the respective code paths.
  420. */
  421. dev->mode_config.max_width = 8192;
  422. dev->mode_config.max_height = 8192;
  423. /* We want the zpos to be normalized */
  424. dev->mode_config.normalize_zpos = true;
  425. dev->mode_config.funcs = &omap_mode_config_funcs;
  426. dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
  427. drm_mode_config_reset(dev);
  428. omap_drm_irq_install(dev);
  429. return 0;
  430. }
  431. static void omap_modeset_fini(struct drm_device *ddev)
  432. {
  433. omap_drm_irq_uninstall(ddev);
  434. drm_mode_config_cleanup(ddev);
  435. }
  436. /*
  437. * drm ioctl funcs
  438. */
  439. static int ioctl_get_param(struct drm_device *dev, void *data,
  440. struct drm_file *file_priv)
  441. {
  442. struct omap_drm_private *priv = dev->dev_private;
  443. struct drm_omap_param *args = data;
  444. DBG("%p: param=%llu", dev, args->param);
  445. switch (args->param) {
  446. case OMAP_PARAM_CHIPSET_ID:
  447. args->value = priv->omaprev;
  448. break;
  449. default:
  450. DBG("unknown parameter %lld", args->param);
  451. return -EINVAL;
  452. }
  453. return 0;
  454. }
  455. #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
  456. static int ioctl_gem_new(struct drm_device *dev, void *data,
  457. struct drm_file *file_priv)
  458. {
  459. struct drm_omap_gem_new *args = data;
  460. u32 flags = args->flags & OMAP_BO_USER_MASK;
  461. VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
  462. args->size.bytes, flags);
  463. return omap_gem_new_handle(dev, file_priv, args->size, flags,
  464. &args->handle);
  465. }
  466. static int ioctl_gem_info(struct drm_device *dev, void *data,
  467. struct drm_file *file_priv)
  468. {
  469. struct drm_omap_gem_info *args = data;
  470. struct drm_gem_object *obj;
  471. int ret = 0;
  472. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  473. obj = drm_gem_object_lookup(file_priv, args->handle);
  474. if (!obj)
  475. return -ENOENT;
  476. args->size = omap_gem_mmap_size(obj);
  477. args->offset = omap_gem_mmap_offset(obj);
  478. drm_gem_object_put(obj);
  479. return ret;
  480. }
  481. static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
  482. DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
  483. DRM_RENDER_ALLOW),
  484. DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
  485. DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
  486. DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
  487. DRM_RENDER_ALLOW),
  488. /* Deprecated, to be removed. */
  489. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
  490. DRM_RENDER_ALLOW),
  491. /* Deprecated, to be removed. */
  492. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
  493. DRM_RENDER_ALLOW),
  494. DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
  495. DRM_RENDER_ALLOW),
  496. };
  497. /*
  498. * drm driver funcs
  499. */
  500. static int dev_open(struct drm_device *dev, struct drm_file *file)
  501. {
  502. file->driver_priv = NULL;
  503. DBG("open: dev=%p, file=%p", dev, file);
  504. return 0;
  505. }
  506. DEFINE_DRM_GEM_FOPS(omapdriver_fops);
  507. static const struct drm_driver omap_drm_driver = {
  508. .driver_features = DRIVER_MODESET | DRIVER_GEM |
  509. DRIVER_ATOMIC | DRIVER_RENDER,
  510. .open = dev_open,
  511. #ifdef CONFIG_DEBUG_FS
  512. .debugfs_init = omap_debugfs_init,
  513. #endif
  514. .gem_prime_import = omap_gem_prime_import,
  515. .dumb_create = omap_gem_dumb_create,
  516. .dumb_map_offset = omap_gem_dumb_map_offset,
  517. OMAP_FBDEV_DRIVER_OPS,
  518. .ioctls = ioctls,
  519. .num_ioctls = DRM_OMAP_NUM_IOCTLS,
  520. .fops = &omapdriver_fops,
  521. .name = DRIVER_NAME,
  522. .desc = DRIVER_DESC,
  523. .major = DRIVER_MAJOR,
  524. .minor = DRIVER_MINOR,
  525. .patchlevel = DRIVER_PATCHLEVEL,
  526. };
  527. static const struct soc_device_attribute omapdrm_soc_devices[] = {
  528. { .family = "OMAP3", .data = (void *)0x3430 },
  529. { .family = "OMAP4", .data = (void *)0x4430 },
  530. { .family = "OMAP5", .data = (void *)0x5430 },
  531. { .family = "DRA7", .data = (void *)0x0752 },
  532. { /* sentinel */ }
  533. };
  534. static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
  535. {
  536. const struct soc_device_attribute *soc;
  537. struct dss_pdata *pdata = dev->platform_data;
  538. struct drm_device *ddev;
  539. int ret;
  540. DBG("%s", dev_name(dev));
  541. if (drm_firmware_drivers_only())
  542. return -ENODEV;
  543. /* Allocate and initialize the DRM device. */
  544. ddev = drm_dev_alloc(&omap_drm_driver, dev);
  545. if (IS_ERR(ddev))
  546. return PTR_ERR(ddev);
  547. priv->ddev = ddev;
  548. ddev->dev_private = priv;
  549. priv->dev = dev;
  550. priv->dss = pdata->dss;
  551. priv->dispc = dispc_get_dispc(priv->dss);
  552. priv->dss->mgr_ops_priv = priv;
  553. soc = soc_device_match(omapdrm_soc_devices);
  554. priv->omaprev = soc ? (uintptr_t)soc->data : 0;
  555. priv->wq = alloc_ordered_workqueue("omapdrm", 0);
  556. if (!priv->wq) {
  557. ret = -ENOMEM;
  558. goto err_alloc_workqueue;
  559. }
  560. mutex_init(&priv->list_lock);
  561. INIT_LIST_HEAD(&priv->obj_list);
  562. /* Get memory bandwidth limits */
  563. priv->max_bandwidth = dispc_get_memory_bandwidth_limit(priv->dispc);
  564. omap_gem_init(ddev);
  565. drm_mode_config_init(ddev);
  566. ret = omap_global_obj_init(ddev);
  567. if (ret)
  568. goto err_gem_deinit;
  569. ret = omap_hwoverlays_init(priv);
  570. if (ret)
  571. goto err_free_priv_obj;
  572. ret = omap_modeset_init(ddev);
  573. if (ret) {
  574. dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
  575. goto err_free_overlays;
  576. }
  577. /* Initialize vblank handling, start with all CRTCs disabled. */
  578. ret = drm_vblank_init(ddev, priv->num_pipes);
  579. if (ret) {
  580. dev_err(priv->dev, "could not init vblank\n");
  581. goto err_cleanup_modeset;
  582. }
  583. drm_kms_helper_poll_init(ddev);
  584. /*
  585. * Register the DRM device with the core and the connectors with
  586. * sysfs.
  587. */
  588. ret = drm_dev_register(ddev, 0);
  589. if (ret)
  590. goto err_cleanup_helpers;
  591. omap_fbdev_setup(ddev);
  592. return 0;
  593. err_cleanup_helpers:
  594. drm_kms_helper_poll_fini(ddev);
  595. err_cleanup_modeset:
  596. omap_modeset_fini(ddev);
  597. err_free_overlays:
  598. omap_hwoverlays_destroy(priv);
  599. err_free_priv_obj:
  600. omap_global_obj_fini(priv);
  601. err_gem_deinit:
  602. drm_mode_config_cleanup(ddev);
  603. omap_gem_deinit(ddev);
  604. destroy_workqueue(priv->wq);
  605. err_alloc_workqueue:
  606. omap_disconnect_pipelines(ddev);
  607. drm_dev_put(ddev);
  608. return ret;
  609. }
  610. static void omapdrm_cleanup(struct omap_drm_private *priv)
  611. {
  612. struct drm_device *ddev = priv->ddev;
  613. DBG("");
  614. drm_dev_unregister(ddev);
  615. drm_kms_helper_poll_fini(ddev);
  616. drm_atomic_helper_shutdown(ddev);
  617. omap_modeset_fini(ddev);
  618. omap_hwoverlays_destroy(priv);
  619. omap_global_obj_fini(priv);
  620. drm_mode_config_cleanup(ddev);
  621. omap_gem_deinit(ddev);
  622. destroy_workqueue(priv->wq);
  623. omap_disconnect_pipelines(ddev);
  624. drm_dev_put(ddev);
  625. }
  626. static int pdev_probe(struct platform_device *pdev)
  627. {
  628. struct omap_drm_private *priv;
  629. int ret;
  630. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  631. if (ret) {
  632. dev_err(&pdev->dev, "Failed to set the DMA mask\n");
  633. return ret;
  634. }
  635. /* Allocate and initialize the driver private structure. */
  636. priv = kzalloc_obj(*priv);
  637. if (!priv)
  638. return -ENOMEM;
  639. platform_set_drvdata(pdev, priv);
  640. ret = omapdrm_init(priv, &pdev->dev);
  641. if (ret < 0)
  642. kfree(priv);
  643. return ret;
  644. }
  645. static void pdev_remove(struct platform_device *pdev)
  646. {
  647. struct omap_drm_private *priv = platform_get_drvdata(pdev);
  648. omapdrm_cleanup(priv);
  649. kfree(priv);
  650. }
  651. static void pdev_shutdown(struct platform_device *pdev)
  652. {
  653. struct omap_drm_private *priv = platform_get_drvdata(pdev);
  654. drm_atomic_helper_shutdown(priv->ddev);
  655. }
  656. #ifdef CONFIG_PM_SLEEP
  657. static int omap_drm_suspend(struct device *dev)
  658. {
  659. struct omap_drm_private *priv = dev_get_drvdata(dev);
  660. struct drm_device *drm_dev = priv->ddev;
  661. return drm_mode_config_helper_suspend(drm_dev);
  662. }
  663. static int omap_drm_resume(struct device *dev)
  664. {
  665. struct omap_drm_private *priv = dev_get_drvdata(dev);
  666. struct drm_device *drm_dev = priv->ddev;
  667. drm_mode_config_helper_resume(drm_dev);
  668. return omap_gem_resume(drm_dev);
  669. }
  670. #endif
  671. static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
  672. static struct platform_driver pdev = {
  673. .driver = {
  674. .name = "omapdrm",
  675. .pm = &omapdrm_pm_ops,
  676. },
  677. .probe = pdev_probe,
  678. .remove = pdev_remove,
  679. .shutdown = pdev_shutdown,
  680. };
  681. static struct platform_driver * const drivers[] = {
  682. &omap_dmm_driver,
  683. &pdev,
  684. };
  685. static int __init omap_drm_init(void)
  686. {
  687. int r;
  688. DBG("init");
  689. r = omap_dss_init();
  690. if (r)
  691. return r;
  692. r = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  693. if (r) {
  694. omap_dss_exit();
  695. return r;
  696. }
  697. return 0;
  698. }
  699. static void __exit omap_drm_fini(void)
  700. {
  701. DBG("fini");
  702. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  703. omap_dss_exit();
  704. }
  705. module_init(omap_drm_init);
  706. module_exit(omap_drm_fini);
  707. MODULE_AUTHOR("Rob Clark <rob@ti.com>");
  708. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  709. MODULE_DESCRIPTION("OMAP DRM Display Driver");
  710. MODULE_ALIAS("platform:" DRIVER_NAME);
  711. MODULE_LICENSE("GPL v2");