omap_drv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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_probe_helper.h>
  21. #include <drm/drm_vblank.h>
  22. #include "omap_dmm_tiler.h"
  23. #include "omap_drv.h"
  24. #include "omap_fbdev.h"
  25. #define DRIVER_NAME MODULE_NAME
  26. #define DRIVER_DESC "OMAP DRM"
  27. #define DRIVER_DATE "20110917"
  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_array(total_planes, sizeof(*states), GFP_KERNEL);
  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(sizeof(*state), GFP_KERNEL);
  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 = output->bridge;
  310. while (drm_bridge_get_next_bridge(bridge))
  311. bridge = drm_bridge_get_next_bridge(bridge);
  312. node = bridge->of_node;
  313. }
  314. return node ? of_alias_get_id(node, "display") : -ENODEV;
  315. }
  316. static int omap_modeset_init(struct drm_device *dev)
  317. {
  318. struct omap_drm_private *priv = dev->dev_private;
  319. int num_ovls = dispc_get_num_ovls(priv->dispc);
  320. int num_mgrs = dispc_get_num_mgrs(priv->dispc);
  321. unsigned int i;
  322. int ret;
  323. u32 plane_crtc_mask;
  324. if (!omapdss_stack_is_ready())
  325. return -EPROBE_DEFER;
  326. ret = omap_modeset_init_properties(dev);
  327. if (ret < 0)
  328. return ret;
  329. /*
  330. * This function creates exactly one connector, encoder, crtc,
  331. * and primary plane per each connected dss-device. Each
  332. * connector->encoder->crtc chain is expected to be separate
  333. * and each crtc is connect to a single dss-channel. If the
  334. * configuration does not match the expectations or exceeds
  335. * the available resources, the configuration is rejected.
  336. */
  337. ret = omap_connect_pipelines(dev);
  338. if (ret < 0)
  339. return ret;
  340. if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
  341. dev_err(dev->dev, "%s(): Too many connected displays\n",
  342. __func__);
  343. return -EINVAL;
  344. }
  345. /* Create all planes first. They can all be put to any CRTC. */
  346. plane_crtc_mask = (1 << priv->num_pipes) - 1;
  347. for (i = 0; i < num_ovls; i++) {
  348. enum drm_plane_type type = i < priv->num_pipes
  349. ? DRM_PLANE_TYPE_PRIMARY
  350. : DRM_PLANE_TYPE_OVERLAY;
  351. struct drm_plane *plane;
  352. if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
  353. return -EINVAL;
  354. plane = omap_plane_init(dev, i, type, plane_crtc_mask);
  355. if (IS_ERR(plane))
  356. return PTR_ERR(plane);
  357. priv->planes[priv->num_planes++] = plane;
  358. }
  359. /*
  360. * Create the encoders, attach the bridges and get the pipeline alias
  361. * IDs.
  362. */
  363. for (i = 0; i < priv->num_pipes; i++) {
  364. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  365. int id;
  366. pipe->encoder = omap_encoder_init(dev, pipe->output);
  367. if (!pipe->encoder)
  368. return -ENOMEM;
  369. if (pipe->output->bridge) {
  370. ret = drm_bridge_attach(pipe->encoder,
  371. pipe->output->bridge, NULL,
  372. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  373. if (ret < 0)
  374. return ret;
  375. }
  376. id = omap_display_id(pipe->output);
  377. pipe->alias_id = id >= 0 ? id : i;
  378. }
  379. /* Sort the pipelines by DT aliases. */
  380. sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
  381. omap_compare_pipelines, NULL);
  382. /*
  383. * Populate the pipeline lookup table by DISPC channel. Only one display
  384. * is allowed per channel.
  385. */
  386. for (i = 0; i < priv->num_pipes; ++i) {
  387. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  388. enum omap_channel channel = pipe->output->dispc_channel;
  389. if (WARN_ON(priv->channels[channel] != NULL))
  390. return -EINVAL;
  391. priv->channels[channel] = pipe;
  392. }
  393. /* Create the connectors and CRTCs. */
  394. for (i = 0; i < priv->num_pipes; i++) {
  395. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  396. struct drm_encoder *encoder = pipe->encoder;
  397. struct drm_crtc *crtc;
  398. pipe->connector = drm_bridge_connector_init(dev, encoder);
  399. if (IS_ERR(pipe->connector)) {
  400. dev_err(priv->dev,
  401. "unable to create bridge connector for %s\n",
  402. pipe->output->name);
  403. return PTR_ERR(pipe->connector);
  404. }
  405. drm_connector_attach_encoder(pipe->connector, encoder);
  406. crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
  407. if (IS_ERR(crtc))
  408. return PTR_ERR(crtc);
  409. encoder->possible_crtcs = 1 << i;
  410. pipe->crtc = crtc;
  411. }
  412. DBG("registered %u planes, %u crtcs/encoders/connectors\n",
  413. priv->num_planes, priv->num_pipes);
  414. dev->mode_config.min_width = 8;
  415. dev->mode_config.min_height = 2;
  416. /*
  417. * Note: these values are used for multiple independent things:
  418. * connector mode filtering, buffer sizes, crtc sizes...
  419. * Use big enough values here to cover all use cases, and do more
  420. * specific checking in the respective code paths.
  421. */
  422. dev->mode_config.max_width = 8192;
  423. dev->mode_config.max_height = 8192;
  424. /* We want the zpos to be normalized */
  425. dev->mode_config.normalize_zpos = true;
  426. dev->mode_config.funcs = &omap_mode_config_funcs;
  427. dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
  428. drm_mode_config_reset(dev);
  429. omap_drm_irq_install(dev);
  430. return 0;
  431. }
  432. static void omap_modeset_fini(struct drm_device *ddev)
  433. {
  434. omap_drm_irq_uninstall(ddev);
  435. drm_mode_config_cleanup(ddev);
  436. }
  437. /*
  438. * drm ioctl funcs
  439. */
  440. static int ioctl_get_param(struct drm_device *dev, void *data,
  441. struct drm_file *file_priv)
  442. {
  443. struct omap_drm_private *priv = dev->dev_private;
  444. struct drm_omap_param *args = data;
  445. DBG("%p: param=%llu", dev, args->param);
  446. switch (args->param) {
  447. case OMAP_PARAM_CHIPSET_ID:
  448. args->value = priv->omaprev;
  449. break;
  450. default:
  451. DBG("unknown parameter %lld", args->param);
  452. return -EINVAL;
  453. }
  454. return 0;
  455. }
  456. #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
  457. static int ioctl_gem_new(struct drm_device *dev, void *data,
  458. struct drm_file *file_priv)
  459. {
  460. struct drm_omap_gem_new *args = data;
  461. u32 flags = args->flags & OMAP_BO_USER_MASK;
  462. VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
  463. args->size.bytes, flags);
  464. return omap_gem_new_handle(dev, file_priv, args->size, flags,
  465. &args->handle);
  466. }
  467. static int ioctl_gem_info(struct drm_device *dev, void *data,
  468. struct drm_file *file_priv)
  469. {
  470. struct drm_omap_gem_info *args = data;
  471. struct drm_gem_object *obj;
  472. int ret = 0;
  473. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  474. obj = drm_gem_object_lookup(file_priv, args->handle);
  475. if (!obj)
  476. return -ENOENT;
  477. args->size = omap_gem_mmap_size(obj);
  478. args->offset = omap_gem_mmap_offset(obj);
  479. drm_gem_object_put(obj);
  480. return ret;
  481. }
  482. static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
  483. DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
  484. DRM_RENDER_ALLOW),
  485. DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
  486. DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
  487. DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
  488. DRM_RENDER_ALLOW),
  489. /* Deprecated, to be removed. */
  490. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
  491. DRM_RENDER_ALLOW),
  492. /* Deprecated, to be removed. */
  493. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
  494. DRM_RENDER_ALLOW),
  495. DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
  496. DRM_RENDER_ALLOW),
  497. };
  498. /*
  499. * drm driver funcs
  500. */
  501. static int dev_open(struct drm_device *dev, struct drm_file *file)
  502. {
  503. file->driver_priv = NULL;
  504. DBG("open: dev=%p, file=%p", dev, file);
  505. return 0;
  506. }
  507. DEFINE_DRM_GEM_FOPS(omapdriver_fops);
  508. static const struct drm_driver omap_drm_driver = {
  509. .driver_features = DRIVER_MODESET | DRIVER_GEM |
  510. DRIVER_ATOMIC | DRIVER_RENDER,
  511. .open = dev_open,
  512. #ifdef CONFIG_DEBUG_FS
  513. .debugfs_init = omap_debugfs_init,
  514. #endif
  515. .gem_prime_import = omap_gem_prime_import,
  516. .dumb_create = omap_gem_dumb_create,
  517. .dumb_map_offset = omap_gem_dumb_map_offset,
  518. .ioctls = ioctls,
  519. .num_ioctls = DRM_OMAP_NUM_IOCTLS,
  520. .fops = &omapdriver_fops,
  521. .name = DRIVER_NAME,
  522. .desc = DRIVER_DESC,
  523. .date = DRIVER_DATE,
  524. .major = DRIVER_MAJOR,
  525. .minor = DRIVER_MINOR,
  526. .patchlevel = DRIVER_PATCHLEVEL,
  527. };
  528. static const struct soc_device_attribute omapdrm_soc_devices[] = {
  529. { .family = "OMAP3", .data = (void *)0x3430 },
  530. { .family = "OMAP4", .data = (void *)0x4430 },
  531. { .family = "OMAP5", .data = (void *)0x5430 },
  532. { .family = "DRA7", .data = (void *)0x0752 },
  533. { /* sentinel */ }
  534. };
  535. static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
  536. {
  537. const struct soc_device_attribute *soc;
  538. struct dss_pdata *pdata = dev->platform_data;
  539. struct drm_device *ddev;
  540. int ret;
  541. DBG("%s", dev_name(dev));
  542. if (drm_firmware_drivers_only())
  543. return -ENODEV;
  544. /* Allocate and initialize the DRM device. */
  545. ddev = drm_dev_alloc(&omap_drm_driver, dev);
  546. if (IS_ERR(ddev))
  547. return PTR_ERR(ddev);
  548. priv->ddev = ddev;
  549. ddev->dev_private = priv;
  550. priv->dev = dev;
  551. priv->dss = pdata->dss;
  552. priv->dispc = dispc_get_dispc(priv->dss);
  553. priv->dss->mgr_ops_priv = priv;
  554. soc = soc_device_match(omapdrm_soc_devices);
  555. priv->omaprev = soc ? (uintptr_t)soc->data : 0;
  556. priv->wq = alloc_ordered_workqueue("omapdrm", 0);
  557. if (!priv->wq) {
  558. ret = -ENOMEM;
  559. goto err_alloc_workqueue;
  560. }
  561. mutex_init(&priv->list_lock);
  562. INIT_LIST_HEAD(&priv->obj_list);
  563. /* Get memory bandwidth limits */
  564. priv->max_bandwidth = dispc_get_memory_bandwidth_limit(priv->dispc);
  565. omap_gem_init(ddev);
  566. drm_mode_config_init(ddev);
  567. ret = omap_global_obj_init(ddev);
  568. if (ret)
  569. goto err_gem_deinit;
  570. ret = omap_hwoverlays_init(priv);
  571. if (ret)
  572. goto err_free_priv_obj;
  573. ret = omap_modeset_init(ddev);
  574. if (ret) {
  575. dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
  576. goto err_free_overlays;
  577. }
  578. /* Initialize vblank handling, start with all CRTCs disabled. */
  579. ret = drm_vblank_init(ddev, priv->num_pipes);
  580. if (ret) {
  581. dev_err(priv->dev, "could not init vblank\n");
  582. goto err_cleanup_modeset;
  583. }
  584. drm_kms_helper_poll_init(ddev);
  585. /*
  586. * Register the DRM device with the core and the connectors with
  587. * sysfs.
  588. */
  589. ret = drm_dev_register(ddev, 0);
  590. if (ret)
  591. goto err_cleanup_helpers;
  592. omap_fbdev_setup(ddev);
  593. return 0;
  594. err_cleanup_helpers:
  595. drm_kms_helper_poll_fini(ddev);
  596. err_cleanup_modeset:
  597. omap_modeset_fini(ddev);
  598. err_free_overlays:
  599. omap_hwoverlays_destroy(priv);
  600. err_free_priv_obj:
  601. omap_global_obj_fini(priv);
  602. err_gem_deinit:
  603. drm_mode_config_cleanup(ddev);
  604. omap_gem_deinit(ddev);
  605. destroy_workqueue(priv->wq);
  606. err_alloc_workqueue:
  607. omap_disconnect_pipelines(ddev);
  608. drm_dev_put(ddev);
  609. return ret;
  610. }
  611. static void omapdrm_cleanup(struct omap_drm_private *priv)
  612. {
  613. struct drm_device *ddev = priv->ddev;
  614. DBG("");
  615. drm_dev_unregister(ddev);
  616. drm_kms_helper_poll_fini(ddev);
  617. drm_atomic_helper_shutdown(ddev);
  618. omap_modeset_fini(ddev);
  619. omap_hwoverlays_destroy(priv);
  620. omap_global_obj_fini(priv);
  621. drm_mode_config_cleanup(ddev);
  622. omap_gem_deinit(ddev);
  623. destroy_workqueue(priv->wq);
  624. omap_disconnect_pipelines(ddev);
  625. drm_dev_put(ddev);
  626. }
  627. static int pdev_probe(struct platform_device *pdev)
  628. {
  629. struct omap_drm_private *priv;
  630. int ret;
  631. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  632. if (ret) {
  633. dev_err(&pdev->dev, "Failed to set the DMA mask\n");
  634. return ret;
  635. }
  636. /* Allocate and initialize the driver private structure. */
  637. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  638. if (!priv)
  639. return -ENOMEM;
  640. platform_set_drvdata(pdev, priv);
  641. ret = omapdrm_init(priv, &pdev->dev);
  642. if (ret < 0)
  643. kfree(priv);
  644. return ret;
  645. }
  646. static void pdev_remove(struct platform_device *pdev)
  647. {
  648. struct omap_drm_private *priv = platform_get_drvdata(pdev);
  649. omapdrm_cleanup(priv);
  650. kfree(priv);
  651. }
  652. static void pdev_shutdown(struct platform_device *pdev)
  653. {
  654. struct omap_drm_private *priv = platform_get_drvdata(pdev);
  655. drm_atomic_helper_shutdown(priv->ddev);
  656. }
  657. #ifdef CONFIG_PM_SLEEP
  658. static int omap_drm_suspend(struct device *dev)
  659. {
  660. struct omap_drm_private *priv = dev_get_drvdata(dev);
  661. struct drm_device *drm_dev = priv->ddev;
  662. return drm_mode_config_helper_suspend(drm_dev);
  663. }
  664. static int omap_drm_resume(struct device *dev)
  665. {
  666. struct omap_drm_private *priv = dev_get_drvdata(dev);
  667. struct drm_device *drm_dev = priv->ddev;
  668. drm_mode_config_helper_resume(drm_dev);
  669. return omap_gem_resume(drm_dev);
  670. }
  671. #endif
  672. static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
  673. static struct platform_driver pdev = {
  674. .driver = {
  675. .name = "omapdrm",
  676. .pm = &omapdrm_pm_ops,
  677. },
  678. .probe = pdev_probe,
  679. .remove_new = pdev_remove,
  680. .shutdown = pdev_shutdown,
  681. };
  682. static struct platform_driver * const drivers[] = {
  683. &omap_dmm_driver,
  684. &pdev,
  685. };
  686. static int __init omap_drm_init(void)
  687. {
  688. int r;
  689. DBG("init");
  690. r = omap_dss_init();
  691. if (r)
  692. return r;
  693. r = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  694. if (r) {
  695. omap_dss_exit();
  696. return r;
  697. }
  698. return 0;
  699. }
  700. static void __exit omap_drm_fini(void)
  701. {
  702. DBG("fini");
  703. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  704. omap_dss_exit();
  705. }
  706. module_init(omap_drm_init);
  707. module_exit(omap_drm_fini);
  708. MODULE_AUTHOR("Rob Clark <rob@ti.com>");
  709. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  710. MODULE_DESCRIPTION("OMAP DRM Display Driver");
  711. MODULE_ALIAS("platform:" DRIVER_NAME);
  712. MODULE_LICENSE("GPL v2");