omap_drv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_drv.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/wait.h>
  20. #include <drm/drm_atomic.h>
  21. #include <drm/drm_atomic_helper.h>
  22. #include <drm/drm_crtc_helper.h>
  23. #include <drm/drm_fb_helper.h>
  24. #include "omap_dmm_tiler.h"
  25. #include "omap_drv.h"
  26. #define DRIVER_NAME MODULE_NAME
  27. #define DRIVER_DESC "OMAP DRM"
  28. #define DRIVER_DATE "20110917"
  29. #define DRIVER_MAJOR 1
  30. #define DRIVER_MINOR 0
  31. #define DRIVER_PATCHLEVEL 0
  32. static int num_crtc = CONFIG_DRM_OMAP_NUM_CRTCS;
  33. MODULE_PARM_DESC(num_crtc, "Number of overlays to use as CRTCs");
  34. module_param(num_crtc, int, 0600);
  35. /*
  36. * mode config funcs
  37. */
  38. /* Notes about mapping DSS and DRM entities:
  39. * CRTC: overlay
  40. * encoder: manager.. with some extension to allow one primary CRTC
  41. * and zero or more video CRTC's to be mapped to one encoder?
  42. * connector: dssdev.. manager can be attached/detached from different
  43. * devices
  44. */
  45. static void omap_fb_output_poll_changed(struct drm_device *dev)
  46. {
  47. struct omap_drm_private *priv = dev->dev_private;
  48. DBG("dev=%p", dev);
  49. if (priv->fbdev)
  50. drm_fb_helper_hotplug_event(priv->fbdev);
  51. }
  52. struct omap_atomic_state_commit {
  53. struct work_struct work;
  54. struct drm_device *dev;
  55. struct drm_atomic_state *state;
  56. u32 crtcs;
  57. };
  58. static void omap_atomic_wait_for_completion(struct drm_device *dev,
  59. struct drm_atomic_state *old_state)
  60. {
  61. struct drm_crtc_state *old_crtc_state;
  62. struct drm_crtc *crtc;
  63. unsigned int i;
  64. int ret;
  65. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  66. if (!crtc->state->enable)
  67. continue;
  68. ret = omap_crtc_wait_pending(crtc);
  69. if (!ret)
  70. dev_warn(dev->dev,
  71. "atomic complete timeout (pipe %u)!\n", i);
  72. }
  73. }
  74. static void omap_atomic_complete(struct omap_atomic_state_commit *commit)
  75. {
  76. struct drm_device *dev = commit->dev;
  77. struct omap_drm_private *priv = dev->dev_private;
  78. struct drm_atomic_state *old_state = commit->state;
  79. /* Apply the atomic update. */
  80. dispc_runtime_get();
  81. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  82. drm_atomic_helper_commit_planes(dev, old_state, false);
  83. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  84. omap_atomic_wait_for_completion(dev, old_state);
  85. drm_atomic_helper_cleanup_planes(dev, old_state);
  86. dispc_runtime_put();
  87. drm_atomic_state_free(old_state);
  88. /* Complete the commit, wake up any waiter. */
  89. spin_lock(&priv->commit.lock);
  90. priv->commit.pending &= ~commit->crtcs;
  91. spin_unlock(&priv->commit.lock);
  92. wake_up_all(&priv->commit.wait);
  93. kfree(commit);
  94. }
  95. static void omap_atomic_work(struct work_struct *work)
  96. {
  97. struct omap_atomic_state_commit *commit =
  98. container_of(work, struct omap_atomic_state_commit, work);
  99. omap_atomic_complete(commit);
  100. }
  101. static bool omap_atomic_is_pending(struct omap_drm_private *priv,
  102. struct omap_atomic_state_commit *commit)
  103. {
  104. bool pending;
  105. spin_lock(&priv->commit.lock);
  106. pending = priv->commit.pending & commit->crtcs;
  107. spin_unlock(&priv->commit.lock);
  108. return pending;
  109. }
  110. static int omap_atomic_commit(struct drm_device *dev,
  111. struct drm_atomic_state *state, bool async)
  112. {
  113. struct omap_drm_private *priv = dev->dev_private;
  114. struct omap_atomic_state_commit *commit;
  115. unsigned long flags;
  116. unsigned int i;
  117. int ret;
  118. ret = drm_atomic_helper_prepare_planes(dev, state);
  119. if (ret)
  120. return ret;
  121. /* Allocate the commit object. */
  122. commit = kzalloc(sizeof(*commit), GFP_KERNEL);
  123. if (commit == NULL) {
  124. ret = -ENOMEM;
  125. goto error;
  126. }
  127. INIT_WORK(&commit->work, omap_atomic_work);
  128. commit->dev = dev;
  129. commit->state = state;
  130. /* Wait until all affected CRTCs have completed previous commits and
  131. * mark them as pending.
  132. */
  133. for (i = 0; i < dev->mode_config.num_crtc; ++i) {
  134. if (state->crtcs[i])
  135. commit->crtcs |= 1 << drm_crtc_index(state->crtcs[i]);
  136. }
  137. wait_event(priv->commit.wait, !omap_atomic_is_pending(priv, commit));
  138. spin_lock(&priv->commit.lock);
  139. priv->commit.pending |= commit->crtcs;
  140. spin_unlock(&priv->commit.lock);
  141. /* Keep track of all CRTC events to unlink them in preclose(). */
  142. spin_lock_irqsave(&dev->event_lock, flags);
  143. for (i = 0; i < dev->mode_config.num_crtc; ++i) {
  144. struct drm_crtc_state *cstate = state->crtc_states[i];
  145. if (cstate && cstate->event)
  146. list_add_tail(&cstate->event->base.link,
  147. &priv->commit.events);
  148. }
  149. spin_unlock_irqrestore(&dev->event_lock, flags);
  150. /* Swap the state, this is the point of no return. */
  151. drm_atomic_helper_swap_state(dev, state);
  152. if (async)
  153. schedule_work(&commit->work);
  154. else
  155. omap_atomic_complete(commit);
  156. return 0;
  157. error:
  158. drm_atomic_helper_cleanup_planes(dev, state);
  159. return ret;
  160. }
  161. static const struct drm_mode_config_funcs omap_mode_config_funcs = {
  162. .fb_create = omap_framebuffer_create,
  163. .output_poll_changed = omap_fb_output_poll_changed,
  164. .atomic_check = drm_atomic_helper_check,
  165. .atomic_commit = omap_atomic_commit,
  166. };
  167. static int get_connector_type(struct omap_dss_device *dssdev)
  168. {
  169. switch (dssdev->type) {
  170. case OMAP_DISPLAY_TYPE_HDMI:
  171. return DRM_MODE_CONNECTOR_HDMIA;
  172. case OMAP_DISPLAY_TYPE_DVI:
  173. return DRM_MODE_CONNECTOR_DVID;
  174. default:
  175. return DRM_MODE_CONNECTOR_Unknown;
  176. }
  177. }
  178. static bool channel_used(struct drm_device *dev, enum omap_channel channel)
  179. {
  180. struct omap_drm_private *priv = dev->dev_private;
  181. int i;
  182. for (i = 0; i < priv->num_crtcs; i++) {
  183. struct drm_crtc *crtc = priv->crtcs[i];
  184. if (omap_crtc_channel(crtc) == channel)
  185. return true;
  186. }
  187. return false;
  188. }
  189. static void omap_disconnect_dssdevs(void)
  190. {
  191. struct omap_dss_device *dssdev = NULL;
  192. for_each_dss_dev(dssdev)
  193. dssdev->driver->disconnect(dssdev);
  194. }
  195. static int omap_connect_dssdevs(void)
  196. {
  197. int r;
  198. struct omap_dss_device *dssdev = NULL;
  199. bool no_displays = true;
  200. for_each_dss_dev(dssdev) {
  201. r = dssdev->driver->connect(dssdev);
  202. if (r == -EPROBE_DEFER) {
  203. omap_dss_put_device(dssdev);
  204. goto cleanup;
  205. } else if (r) {
  206. dev_warn(dssdev->dev, "could not connect display: %s\n",
  207. dssdev->name);
  208. } else {
  209. no_displays = false;
  210. }
  211. }
  212. if (no_displays)
  213. return -EPROBE_DEFER;
  214. return 0;
  215. cleanup:
  216. /*
  217. * if we are deferring probe, we disconnect the devices we previously
  218. * connected
  219. */
  220. omap_disconnect_dssdevs();
  221. return r;
  222. }
  223. static int omap_modeset_create_crtc(struct drm_device *dev, int id,
  224. enum omap_channel channel)
  225. {
  226. struct omap_drm_private *priv = dev->dev_private;
  227. struct drm_plane *plane;
  228. struct drm_crtc *crtc;
  229. plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_PRIMARY);
  230. if (IS_ERR(plane))
  231. return PTR_ERR(plane);
  232. crtc = omap_crtc_init(dev, plane, channel, id);
  233. BUG_ON(priv->num_crtcs >= ARRAY_SIZE(priv->crtcs));
  234. priv->crtcs[id] = crtc;
  235. priv->num_crtcs++;
  236. priv->planes[id] = plane;
  237. priv->num_planes++;
  238. return 0;
  239. }
  240. static int omap_modeset_init_properties(struct drm_device *dev)
  241. {
  242. struct omap_drm_private *priv = dev->dev_private;
  243. if (priv->has_dmm) {
  244. dev->mode_config.rotation_property =
  245. drm_mode_create_rotation_property(dev,
  246. BIT(DRM_ROTATE_0) | BIT(DRM_ROTATE_90) |
  247. BIT(DRM_ROTATE_180) | BIT(DRM_ROTATE_270) |
  248. BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y));
  249. if (!dev->mode_config.rotation_property)
  250. return -ENOMEM;
  251. }
  252. priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
  253. if (!priv->zorder_prop)
  254. return -ENOMEM;
  255. return 0;
  256. }
  257. static int omap_modeset_init(struct drm_device *dev)
  258. {
  259. struct omap_drm_private *priv = dev->dev_private;
  260. struct omap_dss_device *dssdev = NULL;
  261. int num_ovls = dss_feat_get_num_ovls();
  262. int num_mgrs = dss_feat_get_num_mgrs();
  263. int num_crtcs;
  264. int i, id = 0;
  265. int ret;
  266. drm_mode_config_init(dev);
  267. omap_drm_irq_install(dev);
  268. ret = omap_modeset_init_properties(dev);
  269. if (ret < 0)
  270. return ret;
  271. /*
  272. * We usually don't want to create a CRTC for each manager, at least
  273. * not until we have a way to expose private planes to userspace.
  274. * Otherwise there would not be enough video pipes left for drm planes.
  275. * We use the num_crtc argument to limit the number of crtcs we create.
  276. */
  277. num_crtcs = min3(num_crtc, num_mgrs, num_ovls);
  278. dssdev = NULL;
  279. for_each_dss_dev(dssdev) {
  280. struct drm_connector *connector;
  281. struct drm_encoder *encoder;
  282. enum omap_channel channel;
  283. struct omap_overlay_manager *mgr;
  284. if (!omapdss_device_is_connected(dssdev))
  285. continue;
  286. encoder = omap_encoder_init(dev, dssdev);
  287. if (!encoder) {
  288. dev_err(dev->dev, "could not create encoder: %s\n",
  289. dssdev->name);
  290. return -ENOMEM;
  291. }
  292. connector = omap_connector_init(dev,
  293. get_connector_type(dssdev), dssdev, encoder);
  294. if (!connector) {
  295. dev_err(dev->dev, "could not create connector: %s\n",
  296. dssdev->name);
  297. return -ENOMEM;
  298. }
  299. BUG_ON(priv->num_encoders >= ARRAY_SIZE(priv->encoders));
  300. BUG_ON(priv->num_connectors >= ARRAY_SIZE(priv->connectors));
  301. priv->encoders[priv->num_encoders++] = encoder;
  302. priv->connectors[priv->num_connectors++] = connector;
  303. drm_mode_connector_attach_encoder(connector, encoder);
  304. /*
  305. * if we have reached the limit of the crtcs we are allowed to
  306. * create, let's not try to look for a crtc for this
  307. * panel/encoder and onwards, we will, of course, populate the
  308. * the possible_crtcs field for all the encoders with the final
  309. * set of crtcs we create
  310. */
  311. if (id == num_crtcs)
  312. continue;
  313. /*
  314. * get the recommended DISPC channel for this encoder. For now,
  315. * we only try to get create a crtc out of the recommended, the
  316. * other possible channels to which the encoder can connect are
  317. * not considered.
  318. */
  319. mgr = omapdss_find_mgr_from_display(dssdev);
  320. channel = mgr->id;
  321. /*
  322. * if this channel hasn't already been taken by a previously
  323. * allocated crtc, we create a new crtc for it
  324. */
  325. if (!channel_used(dev, channel)) {
  326. ret = omap_modeset_create_crtc(dev, id, channel);
  327. if (ret < 0) {
  328. dev_err(dev->dev,
  329. "could not create CRTC (channel %u)\n",
  330. channel);
  331. return ret;
  332. }
  333. id++;
  334. }
  335. }
  336. /*
  337. * we have allocated crtcs according to the need of the panels/encoders,
  338. * adding more crtcs here if needed
  339. */
  340. for (; id < num_crtcs; id++) {
  341. /* find a free manager for this crtc */
  342. for (i = 0; i < num_mgrs; i++) {
  343. if (!channel_used(dev, i))
  344. break;
  345. }
  346. if (i == num_mgrs) {
  347. /* this shouldn't really happen */
  348. dev_err(dev->dev, "no managers left for crtc\n");
  349. return -ENOMEM;
  350. }
  351. ret = omap_modeset_create_crtc(dev, id, i);
  352. if (ret < 0) {
  353. dev_err(dev->dev,
  354. "could not create CRTC (channel %u)\n", i);
  355. return ret;
  356. }
  357. }
  358. /*
  359. * Create normal planes for the remaining overlays:
  360. */
  361. for (; id < num_ovls; id++) {
  362. struct drm_plane *plane;
  363. plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_OVERLAY);
  364. if (IS_ERR(plane))
  365. return PTR_ERR(plane);
  366. BUG_ON(priv->num_planes >= ARRAY_SIZE(priv->planes));
  367. priv->planes[priv->num_planes++] = plane;
  368. }
  369. for (i = 0; i < priv->num_encoders; i++) {
  370. struct drm_encoder *encoder = priv->encoders[i];
  371. struct omap_dss_device *dssdev =
  372. omap_encoder_get_dssdev(encoder);
  373. struct omap_dss_device *output;
  374. output = omapdss_find_output_from_display(dssdev);
  375. /* figure out which crtc's we can connect the encoder to: */
  376. encoder->possible_crtcs = 0;
  377. for (id = 0; id < priv->num_crtcs; id++) {
  378. struct drm_crtc *crtc = priv->crtcs[id];
  379. enum omap_channel crtc_channel;
  380. crtc_channel = omap_crtc_channel(crtc);
  381. if (output->dispc_channel == crtc_channel) {
  382. encoder->possible_crtcs |= (1 << id);
  383. break;
  384. }
  385. }
  386. omap_dss_put_device(output);
  387. }
  388. DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
  389. priv->num_planes, priv->num_crtcs, priv->num_encoders,
  390. priv->num_connectors);
  391. dev->mode_config.min_width = 32;
  392. dev->mode_config.min_height = 32;
  393. /* note: eventually will need some cpu_is_omapXYZ() type stuff here
  394. * to fill in these limits properly on different OMAP generations..
  395. */
  396. dev->mode_config.max_width = 2048;
  397. dev->mode_config.max_height = 2048;
  398. dev->mode_config.funcs = &omap_mode_config_funcs;
  399. drm_mode_config_reset(dev);
  400. return 0;
  401. }
  402. static void omap_modeset_free(struct drm_device *dev)
  403. {
  404. drm_mode_config_cleanup(dev);
  405. }
  406. /*
  407. * drm ioctl funcs
  408. */
  409. static int ioctl_get_param(struct drm_device *dev, void *data,
  410. struct drm_file *file_priv)
  411. {
  412. struct omap_drm_private *priv = dev->dev_private;
  413. struct drm_omap_param *args = data;
  414. DBG("%p: param=%llu", dev, args->param);
  415. switch (args->param) {
  416. case OMAP_PARAM_CHIPSET_ID:
  417. args->value = priv->omaprev;
  418. break;
  419. default:
  420. DBG("unknown parameter %lld", args->param);
  421. return -EINVAL;
  422. }
  423. return 0;
  424. }
  425. static int ioctl_set_param(struct drm_device *dev, void *data,
  426. struct drm_file *file_priv)
  427. {
  428. struct drm_omap_param *args = data;
  429. switch (args->param) {
  430. default:
  431. DBG("unknown parameter %lld", args->param);
  432. return -EINVAL;
  433. }
  434. return 0;
  435. }
  436. static int ioctl_gem_new(struct drm_device *dev, void *data,
  437. struct drm_file *file_priv)
  438. {
  439. struct drm_omap_gem_new *args = data;
  440. VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
  441. args->size.bytes, args->flags);
  442. return omap_gem_new_handle(dev, file_priv, args->size,
  443. args->flags, &args->handle);
  444. }
  445. static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  446. struct drm_file *file_priv)
  447. {
  448. struct drm_omap_gem_cpu_prep *args = data;
  449. struct drm_gem_object *obj;
  450. int ret;
  451. VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
  452. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  453. if (!obj)
  454. return -ENOENT;
  455. ret = omap_gem_op_sync(obj, args->op);
  456. if (!ret)
  457. ret = omap_gem_op_start(obj, args->op);
  458. drm_gem_object_unreference_unlocked(obj);
  459. return ret;
  460. }
  461. static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  462. struct drm_file *file_priv)
  463. {
  464. struct drm_omap_gem_cpu_fini *args = data;
  465. struct drm_gem_object *obj;
  466. int ret;
  467. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  468. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  469. if (!obj)
  470. return -ENOENT;
  471. /* XXX flushy, flushy */
  472. ret = 0;
  473. if (!ret)
  474. ret = omap_gem_op_finish(obj, args->op);
  475. drm_gem_object_unreference_unlocked(obj);
  476. return ret;
  477. }
  478. static int ioctl_gem_info(struct drm_device *dev, void *data,
  479. struct drm_file *file_priv)
  480. {
  481. struct drm_omap_gem_info *args = data;
  482. struct drm_gem_object *obj;
  483. int ret = 0;
  484. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  485. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  486. if (!obj)
  487. return -ENOENT;
  488. args->size = omap_gem_mmap_size(obj);
  489. args->offset = omap_gem_mmap_offset(obj);
  490. drm_gem_object_unreference_unlocked(obj);
  491. return ret;
  492. }
  493. static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
  494. DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param, DRM_AUTH),
  495. DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  496. DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new, DRM_AUTH),
  497. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep, DRM_AUTH),
  498. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini, DRM_AUTH),
  499. DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info, DRM_AUTH),
  500. };
  501. /*
  502. * drm driver funcs
  503. */
  504. /**
  505. * load - setup chip and create an initial config
  506. * @dev: DRM device
  507. * @flags: startup flags
  508. *
  509. * The driver load routine has to do several things:
  510. * - initialize the memory manager
  511. * - allocate initial config memory
  512. * - setup the DRM framebuffer with the allocated memory
  513. */
  514. static int dev_load(struct drm_device *dev, unsigned long flags)
  515. {
  516. struct omap_drm_platform_data *pdata = dev->dev->platform_data;
  517. struct omap_drm_private *priv;
  518. unsigned int i;
  519. int ret;
  520. DBG("load: dev=%p", dev);
  521. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  522. if (!priv)
  523. return -ENOMEM;
  524. priv->omaprev = pdata->omaprev;
  525. dev->dev_private = priv;
  526. priv->wq = alloc_ordered_workqueue("omapdrm", 0);
  527. init_waitqueue_head(&priv->commit.wait);
  528. spin_lock_init(&priv->commit.lock);
  529. INIT_LIST_HEAD(&priv->commit.events);
  530. spin_lock_init(&priv->list_lock);
  531. INIT_LIST_HEAD(&priv->obj_list);
  532. omap_gem_init(dev);
  533. ret = omap_modeset_init(dev);
  534. if (ret) {
  535. dev_err(dev->dev, "omap_modeset_init failed: ret=%d\n", ret);
  536. dev->dev_private = NULL;
  537. kfree(priv);
  538. return ret;
  539. }
  540. /* Initialize vblank handling, start with all CRTCs disabled. */
  541. ret = drm_vblank_init(dev, priv->num_crtcs);
  542. if (ret)
  543. dev_warn(dev->dev, "could not init vblank\n");
  544. for (i = 0; i < priv->num_crtcs; i++)
  545. drm_crtc_vblank_off(priv->crtcs[i]);
  546. priv->fbdev = omap_fbdev_init(dev);
  547. if (!priv->fbdev) {
  548. dev_warn(dev->dev, "omap_fbdev_init failed\n");
  549. /* well, limp along without an fbdev.. maybe X11 will work? */
  550. }
  551. /* store off drm_device for use in pm ops */
  552. dev_set_drvdata(dev->dev, dev);
  553. drm_kms_helper_poll_init(dev);
  554. return 0;
  555. }
  556. static int dev_unload(struct drm_device *dev)
  557. {
  558. struct omap_drm_private *priv = dev->dev_private;
  559. DBG("unload: dev=%p", dev);
  560. drm_kms_helper_poll_fini(dev);
  561. if (priv->fbdev)
  562. omap_fbdev_free(dev);
  563. omap_modeset_free(dev);
  564. omap_gem_deinit(dev);
  565. destroy_workqueue(priv->wq);
  566. drm_vblank_cleanup(dev);
  567. omap_drm_irq_uninstall(dev);
  568. kfree(dev->dev_private);
  569. dev->dev_private = NULL;
  570. dev_set_drvdata(dev->dev, NULL);
  571. return 0;
  572. }
  573. static int dev_open(struct drm_device *dev, struct drm_file *file)
  574. {
  575. file->driver_priv = NULL;
  576. DBG("open: dev=%p, file=%p", dev, file);
  577. return 0;
  578. }
  579. /**
  580. * lastclose - clean up after all DRM clients have exited
  581. * @dev: DRM device
  582. *
  583. * Take care of cleaning up after all DRM clients have exited. In the
  584. * mode setting case, we want to restore the kernel's initial mode (just
  585. * in case the last client left us in a bad state).
  586. */
  587. static void dev_lastclose(struct drm_device *dev)
  588. {
  589. int i;
  590. /* we don't support vga_switcheroo.. so just make sure the fbdev
  591. * mode is active
  592. */
  593. struct omap_drm_private *priv = dev->dev_private;
  594. int ret;
  595. DBG("lastclose: dev=%p", dev);
  596. if (dev->mode_config.rotation_property) {
  597. /* need to restore default rotation state.. not sure
  598. * if there is a cleaner way to restore properties to
  599. * default state? Maybe a flag that properties should
  600. * automatically be restored to default state on
  601. * lastclose?
  602. */
  603. for (i = 0; i < priv->num_crtcs; i++) {
  604. drm_object_property_set_value(&priv->crtcs[i]->base,
  605. dev->mode_config.rotation_property, 0);
  606. }
  607. for (i = 0; i < priv->num_planes; i++) {
  608. drm_object_property_set_value(&priv->planes[i]->base,
  609. dev->mode_config.rotation_property, 0);
  610. }
  611. }
  612. if (priv->fbdev) {
  613. ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  614. if (ret)
  615. DBG("failed to restore crtc mode");
  616. }
  617. }
  618. static void dev_preclose(struct drm_device *dev, struct drm_file *file)
  619. {
  620. struct omap_drm_private *priv = dev->dev_private;
  621. struct drm_pending_event *event;
  622. unsigned long flags;
  623. DBG("preclose: dev=%p", dev);
  624. /*
  625. * Unlink all pending CRTC events to make sure they won't be queued up
  626. * by a pending asynchronous commit.
  627. */
  628. spin_lock_irqsave(&dev->event_lock, flags);
  629. list_for_each_entry(event, &priv->commit.events, link) {
  630. if (event->file_priv == file) {
  631. file->event_space += event->event->length;
  632. event->file_priv = NULL;
  633. }
  634. }
  635. spin_unlock_irqrestore(&dev->event_lock, flags);
  636. }
  637. static void dev_postclose(struct drm_device *dev, struct drm_file *file)
  638. {
  639. DBG("postclose: dev=%p, file=%p", dev, file);
  640. }
  641. static const struct vm_operations_struct omap_gem_vm_ops = {
  642. .fault = omap_gem_fault,
  643. .open = drm_gem_vm_open,
  644. .close = drm_gem_vm_close,
  645. };
  646. static const struct file_operations omapdriver_fops = {
  647. .owner = THIS_MODULE,
  648. .open = drm_open,
  649. .unlocked_ioctl = drm_ioctl,
  650. .release = drm_release,
  651. .mmap = omap_gem_mmap,
  652. .poll = drm_poll,
  653. .read = drm_read,
  654. .llseek = noop_llseek,
  655. };
  656. static struct drm_driver omap_drm_driver = {
  657. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
  658. .load = dev_load,
  659. .unload = dev_unload,
  660. .open = dev_open,
  661. .lastclose = dev_lastclose,
  662. .preclose = dev_preclose,
  663. .postclose = dev_postclose,
  664. .set_busid = drm_platform_set_busid,
  665. .get_vblank_counter = drm_vblank_no_hw_counter,
  666. .enable_vblank = omap_irq_enable_vblank,
  667. .disable_vblank = omap_irq_disable_vblank,
  668. #ifdef CONFIG_DEBUG_FS
  669. .debugfs_init = omap_debugfs_init,
  670. .debugfs_cleanup = omap_debugfs_cleanup,
  671. #endif
  672. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  673. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  674. .gem_prime_export = omap_gem_prime_export,
  675. .gem_prime_import = omap_gem_prime_import,
  676. .gem_free_object = omap_gem_free_object,
  677. .gem_vm_ops = &omap_gem_vm_ops,
  678. .dumb_create = omap_gem_dumb_create,
  679. .dumb_map_offset = omap_gem_dumb_map_offset,
  680. .dumb_destroy = drm_gem_dumb_destroy,
  681. .ioctls = ioctls,
  682. .num_ioctls = DRM_OMAP_NUM_IOCTLS,
  683. .fops = &omapdriver_fops,
  684. .name = DRIVER_NAME,
  685. .desc = DRIVER_DESC,
  686. .date = DRIVER_DATE,
  687. .major = DRIVER_MAJOR,
  688. .minor = DRIVER_MINOR,
  689. .patchlevel = DRIVER_PATCHLEVEL,
  690. };
  691. static int pdev_probe(struct platform_device *device)
  692. {
  693. int r;
  694. if (omapdss_is_initialized() == false)
  695. return -EPROBE_DEFER;
  696. omap_crtc_pre_init();
  697. r = omap_connect_dssdevs();
  698. if (r) {
  699. omap_crtc_pre_uninit();
  700. return r;
  701. }
  702. DBG("%s", device->name);
  703. return drm_platform_init(&omap_drm_driver, device);
  704. }
  705. static int pdev_remove(struct platform_device *device)
  706. {
  707. DBG("");
  708. drm_put_dev(platform_get_drvdata(device));
  709. omap_disconnect_dssdevs();
  710. omap_crtc_pre_uninit();
  711. return 0;
  712. }
  713. #ifdef CONFIG_PM_SLEEP
  714. static int omap_drm_suspend(struct device *dev)
  715. {
  716. struct drm_device *drm_dev = dev_get_drvdata(dev);
  717. drm_kms_helper_poll_disable(drm_dev);
  718. return 0;
  719. }
  720. static int omap_drm_resume(struct device *dev)
  721. {
  722. struct drm_device *drm_dev = dev_get_drvdata(dev);
  723. drm_kms_helper_poll_enable(drm_dev);
  724. return omap_gem_resume(dev);
  725. }
  726. #endif
  727. static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
  728. static struct platform_driver pdev = {
  729. .driver = {
  730. .name = DRIVER_NAME,
  731. .pm = &omapdrm_pm_ops,
  732. },
  733. .probe = pdev_probe,
  734. .remove = pdev_remove,
  735. };
  736. static int __init omap_drm_init(void)
  737. {
  738. int r;
  739. DBG("init");
  740. r = platform_driver_register(&omap_dmm_driver);
  741. if (r) {
  742. pr_err("DMM driver registration failed\n");
  743. return r;
  744. }
  745. r = platform_driver_register(&pdev);
  746. if (r) {
  747. pr_err("omapdrm driver registration failed\n");
  748. platform_driver_unregister(&omap_dmm_driver);
  749. return r;
  750. }
  751. return 0;
  752. }
  753. static void __exit omap_drm_fini(void)
  754. {
  755. DBG("fini");
  756. platform_driver_unregister(&pdev);
  757. platform_driver_unregister(&omap_dmm_driver);
  758. }
  759. /* need late_initcall() so we load after dss_driver's are loaded */
  760. late_initcall(omap_drm_init);
  761. module_exit(omap_drm_fini);
  762. MODULE_AUTHOR("Rob Clark <rob@ti.com>");
  763. MODULE_DESCRIPTION("OMAP DRM Display Driver");
  764. MODULE_ALIAS("platform:" DRIVER_NAME);
  765. MODULE_LICENSE("GPL v2");