omap_drv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. priv->dispc_ops->runtime_get();
  81. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  82. /* With the current dss dispc implementation we have to enable
  83. * the new modeset before we can commit planes. The dispc ovl
  84. * configuration relies on the video mode configuration been
  85. * written into the HW when the ovl configuration is
  86. * calculated.
  87. *
  88. * This approach is not ideal because after a mode change the
  89. * plane update is executed only after the first vblank
  90. * interrupt. The dispc implementation should be fixed so that
  91. * it is able use uncommitted drm state information.
  92. */
  93. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  94. omap_atomic_wait_for_completion(dev, old_state);
  95. drm_atomic_helper_commit_planes(dev, old_state, 0);
  96. omap_atomic_wait_for_completion(dev, old_state);
  97. drm_atomic_helper_cleanup_planes(dev, old_state);
  98. priv->dispc_ops->runtime_put();
  99. drm_atomic_state_put(old_state);
  100. /* Complete the commit, wake up any waiter. */
  101. spin_lock(&priv->commit.lock);
  102. priv->commit.pending &= ~commit->crtcs;
  103. spin_unlock(&priv->commit.lock);
  104. wake_up_all(&priv->commit.wait);
  105. kfree(commit);
  106. }
  107. static void omap_atomic_work(struct work_struct *work)
  108. {
  109. struct omap_atomic_state_commit *commit =
  110. container_of(work, struct omap_atomic_state_commit, work);
  111. omap_atomic_complete(commit);
  112. }
  113. static bool omap_atomic_is_pending(struct omap_drm_private *priv,
  114. struct omap_atomic_state_commit *commit)
  115. {
  116. bool pending;
  117. spin_lock(&priv->commit.lock);
  118. pending = priv->commit.pending & commit->crtcs;
  119. spin_unlock(&priv->commit.lock);
  120. return pending;
  121. }
  122. static int omap_atomic_commit(struct drm_device *dev,
  123. struct drm_atomic_state *state, bool nonblock)
  124. {
  125. struct omap_drm_private *priv = dev->dev_private;
  126. struct omap_atomic_state_commit *commit;
  127. struct drm_crtc *crtc;
  128. struct drm_crtc_state *crtc_state;
  129. int i, ret;
  130. ret = drm_atomic_helper_prepare_planes(dev, state);
  131. if (ret)
  132. return ret;
  133. /* Allocate the commit object. */
  134. commit = kzalloc(sizeof(*commit), GFP_KERNEL);
  135. if (commit == NULL) {
  136. ret = -ENOMEM;
  137. goto error;
  138. }
  139. INIT_WORK(&commit->work, omap_atomic_work);
  140. commit->dev = dev;
  141. commit->state = state;
  142. /* Wait until all affected CRTCs have completed previous commits and
  143. * mark them as pending.
  144. */
  145. for_each_crtc_in_state(state, crtc, crtc_state, i)
  146. commit->crtcs |= drm_crtc_mask(crtc);
  147. wait_event(priv->commit.wait, !omap_atomic_is_pending(priv, commit));
  148. spin_lock(&priv->commit.lock);
  149. priv->commit.pending |= commit->crtcs;
  150. spin_unlock(&priv->commit.lock);
  151. /* Swap the state, this is the point of no return. */
  152. drm_atomic_helper_swap_state(state, true);
  153. drm_atomic_state_get(state);
  154. if (nonblock)
  155. schedule_work(&commit->work);
  156. else
  157. omap_atomic_complete(commit);
  158. return 0;
  159. error:
  160. drm_atomic_helper_cleanup_planes(dev, state);
  161. return ret;
  162. }
  163. static const struct drm_mode_config_funcs omap_mode_config_funcs = {
  164. .fb_create = omap_framebuffer_create,
  165. .output_poll_changed = omap_fb_output_poll_changed,
  166. .atomic_check = drm_atomic_helper_check,
  167. .atomic_commit = omap_atomic_commit,
  168. };
  169. static int get_connector_type(struct omap_dss_device *dssdev)
  170. {
  171. switch (dssdev->type) {
  172. case OMAP_DISPLAY_TYPE_HDMI:
  173. return DRM_MODE_CONNECTOR_HDMIA;
  174. case OMAP_DISPLAY_TYPE_DVI:
  175. return DRM_MODE_CONNECTOR_DVID;
  176. case OMAP_DISPLAY_TYPE_DSI:
  177. return DRM_MODE_CONNECTOR_DSI;
  178. default:
  179. return DRM_MODE_CONNECTOR_Unknown;
  180. }
  181. }
  182. static bool channel_used(struct drm_device *dev, enum omap_channel channel)
  183. {
  184. struct omap_drm_private *priv = dev->dev_private;
  185. int i;
  186. for (i = 0; i < priv->num_crtcs; i++) {
  187. struct drm_crtc *crtc = priv->crtcs[i];
  188. if (omap_crtc_channel(crtc) == channel)
  189. return true;
  190. }
  191. return false;
  192. }
  193. static void omap_disconnect_dssdevs(void)
  194. {
  195. struct omap_dss_device *dssdev = NULL;
  196. for_each_dss_dev(dssdev)
  197. dssdev->driver->disconnect(dssdev);
  198. }
  199. static int omap_connect_dssdevs(void)
  200. {
  201. int r;
  202. struct omap_dss_device *dssdev = NULL;
  203. if (!omapdss_stack_is_ready())
  204. return -EPROBE_DEFER;
  205. for_each_dss_dev(dssdev) {
  206. r = dssdev->driver->connect(dssdev);
  207. if (r == -EPROBE_DEFER) {
  208. omap_dss_put_device(dssdev);
  209. goto cleanup;
  210. } else if (r) {
  211. dev_warn(dssdev->dev, "could not connect display: %s\n",
  212. dssdev->name);
  213. }
  214. }
  215. return 0;
  216. cleanup:
  217. /*
  218. * if we are deferring probe, we disconnect the devices we previously
  219. * connected
  220. */
  221. omap_disconnect_dssdevs();
  222. return r;
  223. }
  224. static int omap_modeset_create_crtc(struct drm_device *dev, int id,
  225. enum omap_channel channel,
  226. u32 possible_crtcs)
  227. {
  228. struct omap_drm_private *priv = dev->dev_private;
  229. struct drm_plane *plane;
  230. struct drm_crtc *crtc;
  231. plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_PRIMARY,
  232. possible_crtcs);
  233. if (IS_ERR(plane))
  234. return PTR_ERR(plane);
  235. crtc = omap_crtc_init(dev, plane, channel, id);
  236. BUG_ON(priv->num_crtcs >= ARRAY_SIZE(priv->crtcs));
  237. priv->crtcs[id] = crtc;
  238. priv->num_crtcs++;
  239. priv->planes[id] = plane;
  240. priv->num_planes++;
  241. return 0;
  242. }
  243. static int omap_modeset_init_properties(struct drm_device *dev)
  244. {
  245. struct omap_drm_private *priv = dev->dev_private;
  246. priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
  247. if (!priv->zorder_prop)
  248. return -ENOMEM;
  249. return 0;
  250. }
  251. static int omap_modeset_init(struct drm_device *dev)
  252. {
  253. struct omap_drm_private *priv = dev->dev_private;
  254. struct omap_dss_device *dssdev = NULL;
  255. int num_ovls = priv->dispc_ops->get_num_ovls();
  256. int num_mgrs = priv->dispc_ops->get_num_mgrs();
  257. int num_crtcs;
  258. int i, id = 0;
  259. int ret;
  260. u32 possible_crtcs;
  261. drm_mode_config_init(dev);
  262. ret = omap_modeset_init_properties(dev);
  263. if (ret < 0)
  264. return ret;
  265. /*
  266. * We usually don't want to create a CRTC for each manager, at least
  267. * not until we have a way to expose private planes to userspace.
  268. * Otherwise there would not be enough video pipes left for drm planes.
  269. * We use the num_crtc argument to limit the number of crtcs we create.
  270. */
  271. num_crtcs = min3(num_crtc, num_mgrs, num_ovls);
  272. possible_crtcs = (1 << num_crtcs) - 1;
  273. dssdev = NULL;
  274. for_each_dss_dev(dssdev) {
  275. struct drm_connector *connector;
  276. struct drm_encoder *encoder;
  277. enum omap_channel channel;
  278. struct omap_dss_device *out;
  279. if (!omapdss_device_is_connected(dssdev))
  280. continue;
  281. encoder = omap_encoder_init(dev, dssdev);
  282. if (!encoder) {
  283. dev_err(dev->dev, "could not create encoder: %s\n",
  284. dssdev->name);
  285. return -ENOMEM;
  286. }
  287. connector = omap_connector_init(dev,
  288. get_connector_type(dssdev), dssdev, encoder);
  289. if (!connector) {
  290. dev_err(dev->dev, "could not create connector: %s\n",
  291. dssdev->name);
  292. return -ENOMEM;
  293. }
  294. BUG_ON(priv->num_encoders >= ARRAY_SIZE(priv->encoders));
  295. BUG_ON(priv->num_connectors >= ARRAY_SIZE(priv->connectors));
  296. priv->encoders[priv->num_encoders++] = encoder;
  297. priv->connectors[priv->num_connectors++] = connector;
  298. drm_mode_connector_attach_encoder(connector, encoder);
  299. /*
  300. * if we have reached the limit of the crtcs we are allowed to
  301. * create, let's not try to look for a crtc for this
  302. * panel/encoder and onwards, we will, of course, populate the
  303. * the possible_crtcs field for all the encoders with the final
  304. * set of crtcs we create
  305. */
  306. if (id == num_crtcs)
  307. continue;
  308. /*
  309. * get the recommended DISPC channel for this encoder. For now,
  310. * we only try to get create a crtc out of the recommended, the
  311. * other possible channels to which the encoder can connect are
  312. * not considered.
  313. */
  314. out = omapdss_find_output_from_display(dssdev);
  315. channel = out->dispc_channel;
  316. omap_dss_put_device(out);
  317. /*
  318. * if this channel hasn't already been taken by a previously
  319. * allocated crtc, we create a new crtc for it
  320. */
  321. if (!channel_used(dev, channel)) {
  322. ret = omap_modeset_create_crtc(dev, id, channel,
  323. possible_crtcs);
  324. if (ret < 0) {
  325. dev_err(dev->dev,
  326. "could not create CRTC (channel %u)\n",
  327. channel);
  328. return ret;
  329. }
  330. id++;
  331. }
  332. }
  333. /*
  334. * we have allocated crtcs according to the need of the panels/encoders,
  335. * adding more crtcs here if needed
  336. */
  337. for (; id < num_crtcs; id++) {
  338. /* find a free manager for this crtc */
  339. for (i = 0; i < num_mgrs; i++) {
  340. if (!channel_used(dev, i))
  341. break;
  342. }
  343. if (i == num_mgrs) {
  344. /* this shouldn't really happen */
  345. dev_err(dev->dev, "no managers left for crtc\n");
  346. return -ENOMEM;
  347. }
  348. ret = omap_modeset_create_crtc(dev, id, i,
  349. possible_crtcs);
  350. if (ret < 0) {
  351. dev_err(dev->dev,
  352. "could not create CRTC (channel %u)\n", i);
  353. return ret;
  354. }
  355. }
  356. /*
  357. * Create normal planes for the remaining overlays:
  358. */
  359. for (; id < num_ovls; id++) {
  360. struct drm_plane *plane;
  361. plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_OVERLAY,
  362. possible_crtcs);
  363. if (IS_ERR(plane))
  364. return PTR_ERR(plane);
  365. BUG_ON(priv->num_planes >= ARRAY_SIZE(priv->planes));
  366. priv->planes[priv->num_planes++] = plane;
  367. }
  368. for (i = 0; i < priv->num_encoders; i++) {
  369. struct drm_encoder *encoder = priv->encoders[i];
  370. struct omap_dss_device *dssdev =
  371. omap_encoder_get_dssdev(encoder);
  372. struct omap_dss_device *output;
  373. output = omapdss_find_output_from_display(dssdev);
  374. /* figure out which crtc's we can connect the encoder to: */
  375. encoder->possible_crtcs = 0;
  376. for (id = 0; id < priv->num_crtcs; id++) {
  377. struct drm_crtc *crtc = priv->crtcs[id];
  378. enum omap_channel crtc_channel;
  379. crtc_channel = omap_crtc_channel(crtc);
  380. if (output->dispc_channel == crtc_channel) {
  381. encoder->possible_crtcs |= (1 << id);
  382. break;
  383. }
  384. }
  385. omap_dss_put_device(output);
  386. }
  387. DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
  388. priv->num_planes, priv->num_crtcs, priv->num_encoders,
  389. priv->num_connectors);
  390. dev->mode_config.min_width = 8;
  391. dev->mode_config.min_height = 2;
  392. /* note: eventually will need some cpu_is_omapXYZ() type stuff here
  393. * to fill in these limits properly on different OMAP generations..
  394. */
  395. dev->mode_config.max_width = 2048;
  396. dev->mode_config.max_height = 2048;
  397. dev->mode_config.funcs = &omap_mode_config_funcs;
  398. drm_mode_config_reset(dev);
  399. omap_drm_irq_install(dev);
  400. return 0;
  401. }
  402. /*
  403. * drm ioctl funcs
  404. */
  405. static int ioctl_get_param(struct drm_device *dev, void *data,
  406. struct drm_file *file_priv)
  407. {
  408. struct omap_drm_private *priv = dev->dev_private;
  409. struct drm_omap_param *args = data;
  410. DBG("%p: param=%llu", dev, args->param);
  411. switch (args->param) {
  412. case OMAP_PARAM_CHIPSET_ID:
  413. args->value = priv->omaprev;
  414. break;
  415. default:
  416. DBG("unknown parameter %lld", args->param);
  417. return -EINVAL;
  418. }
  419. return 0;
  420. }
  421. static int ioctl_set_param(struct drm_device *dev, void *data,
  422. struct drm_file *file_priv)
  423. {
  424. struct drm_omap_param *args = data;
  425. switch (args->param) {
  426. default:
  427. DBG("unknown parameter %lld", args->param);
  428. return -EINVAL;
  429. }
  430. return 0;
  431. }
  432. #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
  433. static int ioctl_gem_new(struct drm_device *dev, void *data,
  434. struct drm_file *file_priv)
  435. {
  436. struct drm_omap_gem_new *args = data;
  437. u32 flags = args->flags & OMAP_BO_USER_MASK;
  438. VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
  439. args->size.bytes, flags);
  440. return omap_gem_new_handle(dev, file_priv, args->size, flags,
  441. &args->handle);
  442. }
  443. static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  444. struct drm_file *file_priv)
  445. {
  446. struct drm_omap_gem_cpu_prep *args = data;
  447. struct drm_gem_object *obj;
  448. int ret;
  449. VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
  450. obj = drm_gem_object_lookup(file_priv, args->handle);
  451. if (!obj)
  452. return -ENOENT;
  453. ret = omap_gem_op_sync(obj, args->op);
  454. if (!ret)
  455. ret = omap_gem_op_start(obj, args->op);
  456. drm_gem_object_unreference_unlocked(obj);
  457. return ret;
  458. }
  459. static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  460. struct drm_file *file_priv)
  461. {
  462. struct drm_omap_gem_cpu_fini *args = data;
  463. struct drm_gem_object *obj;
  464. int ret;
  465. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  466. obj = drm_gem_object_lookup(file_priv, args->handle);
  467. if (!obj)
  468. return -ENOENT;
  469. /* XXX flushy, flushy */
  470. ret = 0;
  471. if (!ret)
  472. ret = omap_gem_op_finish(obj, args->op);
  473. drm_gem_object_unreference_unlocked(obj);
  474. return ret;
  475. }
  476. static int ioctl_gem_info(struct drm_device *dev, void *data,
  477. struct drm_file *file_priv)
  478. {
  479. struct drm_omap_gem_info *args = data;
  480. struct drm_gem_object *obj;
  481. int ret = 0;
  482. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  483. obj = drm_gem_object_lookup(file_priv, args->handle);
  484. if (!obj)
  485. return -ENOENT;
  486. args->size = omap_gem_mmap_size(obj);
  487. args->offset = omap_gem_mmap_offset(obj);
  488. drm_gem_object_unreference_unlocked(obj);
  489. return ret;
  490. }
  491. static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
  492. DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
  493. DRM_AUTH | DRM_RENDER_ALLOW),
  494. DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
  495. DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
  496. DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
  497. DRM_AUTH | DRM_RENDER_ALLOW),
  498. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep,
  499. DRM_AUTH | DRM_RENDER_ALLOW),
  500. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini,
  501. DRM_AUTH | DRM_RENDER_ALLOW),
  502. DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
  503. DRM_AUTH | DRM_RENDER_ALLOW),
  504. };
  505. /*
  506. * drm driver funcs
  507. */
  508. static int dev_open(struct drm_device *dev, struct drm_file *file)
  509. {
  510. file->driver_priv = NULL;
  511. DBG("open: dev=%p, file=%p", dev, file);
  512. return 0;
  513. }
  514. /**
  515. * lastclose - clean up after all DRM clients have exited
  516. * @dev: DRM device
  517. *
  518. * Take care of cleaning up after all DRM clients have exited. In the
  519. * mode setting case, we want to restore the kernel's initial mode (just
  520. * in case the last client left us in a bad state).
  521. */
  522. static void dev_lastclose(struct drm_device *dev)
  523. {
  524. int i;
  525. /* we don't support vga_switcheroo.. so just make sure the fbdev
  526. * mode is active
  527. */
  528. struct omap_drm_private *priv = dev->dev_private;
  529. int ret;
  530. DBG("lastclose: dev=%p", dev);
  531. /* need to restore default rotation state.. not sure
  532. * if there is a cleaner way to restore properties to
  533. * default state? Maybe a flag that properties should
  534. * automatically be restored to default state on
  535. * lastclose?
  536. */
  537. for (i = 0; i < priv->num_crtcs; i++) {
  538. struct drm_crtc *crtc = priv->crtcs[i];
  539. if (!crtc->primary->rotation_property)
  540. continue;
  541. drm_object_property_set_value(&crtc->base,
  542. crtc->primary->rotation_property,
  543. DRM_ROTATE_0);
  544. }
  545. for (i = 0; i < priv->num_planes; i++) {
  546. struct drm_plane *plane = priv->planes[i];
  547. if (!plane->rotation_property)
  548. continue;
  549. drm_object_property_set_value(&plane->base,
  550. plane->rotation_property,
  551. DRM_ROTATE_0);
  552. }
  553. if (priv->fbdev) {
  554. ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  555. if (ret)
  556. DBG("failed to restore crtc mode");
  557. }
  558. }
  559. static const struct vm_operations_struct omap_gem_vm_ops = {
  560. .fault = omap_gem_fault,
  561. .open = drm_gem_vm_open,
  562. .close = drm_gem_vm_close,
  563. };
  564. static const struct file_operations omapdriver_fops = {
  565. .owner = THIS_MODULE,
  566. .open = drm_open,
  567. .unlocked_ioctl = drm_ioctl,
  568. .release = drm_release,
  569. .mmap = omap_gem_mmap,
  570. .poll = drm_poll,
  571. .read = drm_read,
  572. .llseek = noop_llseek,
  573. };
  574. static struct drm_driver omap_drm_driver = {
  575. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
  576. DRIVER_ATOMIC | DRIVER_RENDER,
  577. .open = dev_open,
  578. .lastclose = dev_lastclose,
  579. #ifdef CONFIG_DEBUG_FS
  580. .debugfs_init = omap_debugfs_init,
  581. #endif
  582. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  583. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  584. .gem_prime_export = omap_gem_prime_export,
  585. .gem_prime_import = omap_gem_prime_import,
  586. .gem_free_object = omap_gem_free_object,
  587. .gem_vm_ops = &omap_gem_vm_ops,
  588. .dumb_create = omap_gem_dumb_create,
  589. .dumb_map_offset = omap_gem_dumb_map_offset,
  590. .dumb_destroy = drm_gem_dumb_destroy,
  591. .ioctls = ioctls,
  592. .num_ioctls = DRM_OMAP_NUM_IOCTLS,
  593. .fops = &omapdriver_fops,
  594. .name = DRIVER_NAME,
  595. .desc = DRIVER_DESC,
  596. .date = DRIVER_DATE,
  597. .major = DRIVER_MAJOR,
  598. .minor = DRIVER_MINOR,
  599. .patchlevel = DRIVER_PATCHLEVEL,
  600. };
  601. static int pdev_probe(struct platform_device *pdev)
  602. {
  603. struct omap_drm_platform_data *pdata = pdev->dev.platform_data;
  604. struct omap_drm_private *priv;
  605. struct drm_device *ddev;
  606. unsigned int i;
  607. int ret;
  608. DBG("%s", pdev->name);
  609. if (omapdss_is_initialized() == false)
  610. return -EPROBE_DEFER;
  611. omap_crtc_pre_init();
  612. ret = omap_connect_dssdevs();
  613. if (ret)
  614. goto err_crtc_uninit;
  615. /* Allocate and initialize the driver private structure. */
  616. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  617. if (!priv) {
  618. ret = -ENOMEM;
  619. goto err_disconnect_dssdevs;
  620. }
  621. priv->dispc_ops = dispc_get_ops();
  622. priv->omaprev = pdata->omaprev;
  623. priv->wq = alloc_ordered_workqueue("omapdrm", 0);
  624. init_waitqueue_head(&priv->commit.wait);
  625. spin_lock_init(&priv->commit.lock);
  626. spin_lock_init(&priv->list_lock);
  627. INIT_LIST_HEAD(&priv->obj_list);
  628. /* Allocate and initialize the DRM device. */
  629. ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
  630. if (IS_ERR(ddev)) {
  631. ret = PTR_ERR(ddev);
  632. goto err_free_priv;
  633. }
  634. ddev->dev_private = priv;
  635. platform_set_drvdata(pdev, ddev);
  636. omap_gem_init(ddev);
  637. ret = omap_modeset_init(ddev);
  638. if (ret) {
  639. dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
  640. goto err_free_drm_dev;
  641. }
  642. /* Initialize vblank handling, start with all CRTCs disabled. */
  643. ret = drm_vblank_init(ddev, priv->num_crtcs);
  644. if (ret) {
  645. dev_err(&pdev->dev, "could not init vblank\n");
  646. goto err_cleanup_modeset;
  647. }
  648. for (i = 0; i < priv->num_crtcs; i++)
  649. drm_crtc_vblank_off(priv->crtcs[i]);
  650. priv->fbdev = omap_fbdev_init(ddev);
  651. drm_kms_helper_poll_init(ddev);
  652. /*
  653. * Register the DRM device with the core and the connectors with
  654. * sysfs.
  655. */
  656. ret = drm_dev_register(ddev, 0);
  657. if (ret)
  658. goto err_cleanup_helpers;
  659. return 0;
  660. err_cleanup_helpers:
  661. drm_kms_helper_poll_fini(ddev);
  662. if (priv->fbdev)
  663. omap_fbdev_free(ddev);
  664. err_cleanup_modeset:
  665. drm_mode_config_cleanup(ddev);
  666. omap_drm_irq_uninstall(ddev);
  667. err_free_drm_dev:
  668. omap_gem_deinit(ddev);
  669. drm_dev_unref(ddev);
  670. err_free_priv:
  671. destroy_workqueue(priv->wq);
  672. kfree(priv);
  673. err_disconnect_dssdevs:
  674. omap_disconnect_dssdevs();
  675. err_crtc_uninit:
  676. omap_crtc_pre_uninit();
  677. return ret;
  678. }
  679. static int pdev_remove(struct platform_device *pdev)
  680. {
  681. struct drm_device *ddev = platform_get_drvdata(pdev);
  682. struct omap_drm_private *priv = ddev->dev_private;
  683. DBG("");
  684. drm_dev_unregister(ddev);
  685. drm_kms_helper_poll_fini(ddev);
  686. if (priv->fbdev)
  687. omap_fbdev_free(ddev);
  688. drm_mode_config_cleanup(ddev);
  689. omap_drm_irq_uninstall(ddev);
  690. omap_gem_deinit(ddev);
  691. drm_dev_unref(ddev);
  692. destroy_workqueue(priv->wq);
  693. kfree(priv);
  694. omap_disconnect_dssdevs();
  695. omap_crtc_pre_uninit();
  696. return 0;
  697. }
  698. #ifdef CONFIG_PM_SLEEP
  699. static int omap_drm_suspend_all_displays(void)
  700. {
  701. struct omap_dss_device *dssdev = NULL;
  702. for_each_dss_dev(dssdev) {
  703. if (!dssdev->driver)
  704. continue;
  705. if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
  706. dssdev->driver->disable(dssdev);
  707. dssdev->activate_after_resume = true;
  708. } else {
  709. dssdev->activate_after_resume = false;
  710. }
  711. }
  712. return 0;
  713. }
  714. static int omap_drm_resume_all_displays(void)
  715. {
  716. struct omap_dss_device *dssdev = NULL;
  717. for_each_dss_dev(dssdev) {
  718. if (!dssdev->driver)
  719. continue;
  720. if (dssdev->activate_after_resume) {
  721. dssdev->driver->enable(dssdev);
  722. dssdev->activate_after_resume = false;
  723. }
  724. }
  725. return 0;
  726. }
  727. static int omap_drm_suspend(struct device *dev)
  728. {
  729. struct drm_device *drm_dev = dev_get_drvdata(dev);
  730. drm_kms_helper_poll_disable(drm_dev);
  731. drm_modeset_lock_all(drm_dev);
  732. omap_drm_suspend_all_displays();
  733. drm_modeset_unlock_all(drm_dev);
  734. return 0;
  735. }
  736. static int omap_drm_resume(struct device *dev)
  737. {
  738. struct drm_device *drm_dev = dev_get_drvdata(dev);
  739. drm_modeset_lock_all(drm_dev);
  740. omap_drm_resume_all_displays();
  741. drm_modeset_unlock_all(drm_dev);
  742. drm_kms_helper_poll_enable(drm_dev);
  743. return omap_gem_resume(dev);
  744. }
  745. #endif
  746. static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
  747. static struct platform_driver pdev = {
  748. .driver = {
  749. .name = DRIVER_NAME,
  750. .pm = &omapdrm_pm_ops,
  751. },
  752. .probe = pdev_probe,
  753. .remove = pdev_remove,
  754. };
  755. static struct platform_driver * const drivers[] = {
  756. &omap_dmm_driver,
  757. &pdev,
  758. };
  759. static int __init omap_drm_init(void)
  760. {
  761. DBG("init");
  762. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  763. }
  764. static void __exit omap_drm_fini(void)
  765. {
  766. DBG("fini");
  767. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  768. }
  769. /* need late_initcall() so we load after dss_driver's are loaded */
  770. late_initcall(omap_drm_init);
  771. module_exit(omap_drm_fini);
  772. MODULE_AUTHOR("Rob Clark <rob@ti.com>");
  773. MODULE_DESCRIPTION("OMAP DRM Display Driver");
  774. MODULE_ALIAS("platform:" DRIVER_NAME);
  775. MODULE_LICENSE("GPL v2");