omap_drv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. /* 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. dispc_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. bool no_displays = true;
  204. for_each_dss_dev(dssdev) {
  205. r = dssdev->driver->connect(dssdev);
  206. if (r == -EPROBE_DEFER) {
  207. omap_dss_put_device(dssdev);
  208. goto cleanup;
  209. } else if (r) {
  210. dev_warn(dssdev->dev, "could not connect display: %s\n",
  211. dssdev->name);
  212. } else {
  213. no_displays = false;
  214. }
  215. }
  216. if (no_displays)
  217. return -EPROBE_DEFER;
  218. return 0;
  219. cleanup:
  220. /*
  221. * if we are deferring probe, we disconnect the devices we previously
  222. * connected
  223. */
  224. omap_disconnect_dssdevs();
  225. return r;
  226. }
  227. static int omap_modeset_create_crtc(struct drm_device *dev, int id,
  228. enum omap_channel channel,
  229. u32 possible_crtcs)
  230. {
  231. struct omap_drm_private *priv = dev->dev_private;
  232. struct drm_plane *plane;
  233. struct drm_crtc *crtc;
  234. plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_PRIMARY,
  235. possible_crtcs);
  236. if (IS_ERR(plane))
  237. return PTR_ERR(plane);
  238. crtc = omap_crtc_init(dev, plane, channel, id);
  239. BUG_ON(priv->num_crtcs >= ARRAY_SIZE(priv->crtcs));
  240. priv->crtcs[id] = crtc;
  241. priv->num_crtcs++;
  242. priv->planes[id] = plane;
  243. priv->num_planes++;
  244. return 0;
  245. }
  246. static int omap_modeset_init_properties(struct drm_device *dev)
  247. {
  248. struct omap_drm_private *priv = dev->dev_private;
  249. priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
  250. if (!priv->zorder_prop)
  251. return -ENOMEM;
  252. return 0;
  253. }
  254. static int omap_modeset_init(struct drm_device *dev)
  255. {
  256. struct omap_drm_private *priv = dev->dev_private;
  257. struct omap_dss_device *dssdev = NULL;
  258. int num_ovls = dss_feat_get_num_ovls();
  259. int num_mgrs = dss_feat_get_num_mgrs();
  260. int num_crtcs;
  261. int i, id = 0;
  262. int ret;
  263. u32 possible_crtcs;
  264. drm_mode_config_init(dev);
  265. ret = omap_modeset_init_properties(dev);
  266. if (ret < 0)
  267. return ret;
  268. /*
  269. * We usually don't want to create a CRTC for each manager, at least
  270. * not until we have a way to expose private planes to userspace.
  271. * Otherwise there would not be enough video pipes left for drm planes.
  272. * We use the num_crtc argument to limit the number of crtcs we create.
  273. */
  274. num_crtcs = min3(num_crtc, num_mgrs, num_ovls);
  275. possible_crtcs = (1 << num_crtcs) - 1;
  276. dssdev = NULL;
  277. for_each_dss_dev(dssdev) {
  278. struct drm_connector *connector;
  279. struct drm_encoder *encoder;
  280. enum omap_channel channel;
  281. struct omap_dss_device *out;
  282. if (!omapdss_device_is_connected(dssdev))
  283. continue;
  284. encoder = omap_encoder_init(dev, dssdev);
  285. if (!encoder) {
  286. dev_err(dev->dev, "could not create encoder: %s\n",
  287. dssdev->name);
  288. return -ENOMEM;
  289. }
  290. connector = omap_connector_init(dev,
  291. get_connector_type(dssdev), dssdev, encoder);
  292. if (!connector) {
  293. dev_err(dev->dev, "could not create connector: %s\n",
  294. dssdev->name);
  295. return -ENOMEM;
  296. }
  297. BUG_ON(priv->num_encoders >= ARRAY_SIZE(priv->encoders));
  298. BUG_ON(priv->num_connectors >= ARRAY_SIZE(priv->connectors));
  299. priv->encoders[priv->num_encoders++] = encoder;
  300. priv->connectors[priv->num_connectors++] = connector;
  301. drm_mode_connector_attach_encoder(connector, encoder);
  302. /*
  303. * if we have reached the limit of the crtcs we are allowed to
  304. * create, let's not try to look for a crtc for this
  305. * panel/encoder and onwards, we will, of course, populate the
  306. * the possible_crtcs field for all the encoders with the final
  307. * set of crtcs we create
  308. */
  309. if (id == num_crtcs)
  310. continue;
  311. /*
  312. * get the recommended DISPC channel for this encoder. For now,
  313. * we only try to get create a crtc out of the recommended, the
  314. * other possible channels to which the encoder can connect are
  315. * not considered.
  316. */
  317. out = omapdss_find_output_from_display(dssdev);
  318. channel = out->dispc_channel;
  319. omap_dss_put_device(out);
  320. /*
  321. * if this channel hasn't already been taken by a previously
  322. * allocated crtc, we create a new crtc for it
  323. */
  324. if (!channel_used(dev, channel)) {
  325. ret = omap_modeset_create_crtc(dev, id, channel,
  326. possible_crtcs);
  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. possible_crtcs);
  353. if (ret < 0) {
  354. dev_err(dev->dev,
  355. "could not create CRTC (channel %u)\n", i);
  356. return ret;
  357. }
  358. }
  359. /*
  360. * Create normal planes for the remaining overlays:
  361. */
  362. for (; id < num_ovls; id++) {
  363. struct drm_plane *plane;
  364. plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_OVERLAY,
  365. possible_crtcs);
  366. if (IS_ERR(plane))
  367. return PTR_ERR(plane);
  368. BUG_ON(priv->num_planes >= ARRAY_SIZE(priv->planes));
  369. priv->planes[priv->num_planes++] = plane;
  370. }
  371. for (i = 0; i < priv->num_encoders; i++) {
  372. struct drm_encoder *encoder = priv->encoders[i];
  373. struct omap_dss_device *dssdev =
  374. omap_encoder_get_dssdev(encoder);
  375. struct omap_dss_device *output;
  376. output = omapdss_find_output_from_display(dssdev);
  377. /* figure out which crtc's we can connect the encoder to: */
  378. encoder->possible_crtcs = 0;
  379. for (id = 0; id < priv->num_crtcs; id++) {
  380. struct drm_crtc *crtc = priv->crtcs[id];
  381. enum omap_channel crtc_channel;
  382. crtc_channel = omap_crtc_channel(crtc);
  383. if (output->dispc_channel == crtc_channel) {
  384. encoder->possible_crtcs |= (1 << id);
  385. break;
  386. }
  387. }
  388. omap_dss_put_device(output);
  389. }
  390. DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
  391. priv->num_planes, priv->num_crtcs, priv->num_encoders,
  392. priv->num_connectors);
  393. dev->mode_config.min_width = 32;
  394. dev->mode_config.min_height = 32;
  395. /* note: eventually will need some cpu_is_omapXYZ() type stuff here
  396. * to fill in these limits properly on different OMAP generations..
  397. */
  398. dev->mode_config.max_width = 2048;
  399. dev->mode_config.max_height = 2048;
  400. dev->mode_config.funcs = &omap_mode_config_funcs;
  401. drm_mode_config_reset(dev);
  402. omap_drm_irq_install(dev);
  403. return 0;
  404. }
  405. /*
  406. * drm ioctl funcs
  407. */
  408. static int ioctl_get_param(struct drm_device *dev, void *data,
  409. struct drm_file *file_priv)
  410. {
  411. struct omap_drm_private *priv = dev->dev_private;
  412. struct drm_omap_param *args = data;
  413. DBG("%p: param=%llu", dev, args->param);
  414. switch (args->param) {
  415. case OMAP_PARAM_CHIPSET_ID:
  416. args->value = priv->omaprev;
  417. break;
  418. default:
  419. DBG("unknown parameter %lld", args->param);
  420. return -EINVAL;
  421. }
  422. return 0;
  423. }
  424. static int ioctl_set_param(struct drm_device *dev, void *data,
  425. struct drm_file *file_priv)
  426. {
  427. struct drm_omap_param *args = data;
  428. switch (args->param) {
  429. default:
  430. DBG("unknown parameter %lld", args->param);
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
  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. u32 flags = args->flags & OMAP_BO_USER_MASK;
  441. VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
  442. args->size.bytes, flags);
  443. return omap_gem_new_handle(dev, file_priv, args->size, flags,
  444. &args->handle);
  445. }
  446. static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  447. struct drm_file *file_priv)
  448. {
  449. struct drm_omap_gem_cpu_prep *args = data;
  450. struct drm_gem_object *obj;
  451. int ret;
  452. VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
  453. obj = drm_gem_object_lookup(file_priv, args->handle);
  454. if (!obj)
  455. return -ENOENT;
  456. ret = omap_gem_op_sync(obj, args->op);
  457. if (!ret)
  458. ret = omap_gem_op_start(obj, args->op);
  459. drm_gem_object_unreference_unlocked(obj);
  460. return ret;
  461. }
  462. static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  463. struct drm_file *file_priv)
  464. {
  465. struct drm_omap_gem_cpu_fini *args = data;
  466. struct drm_gem_object *obj;
  467. int ret;
  468. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  469. obj = drm_gem_object_lookup(file_priv, args->handle);
  470. if (!obj)
  471. return -ENOENT;
  472. /* XXX flushy, flushy */
  473. ret = 0;
  474. if (!ret)
  475. ret = omap_gem_op_finish(obj, args->op);
  476. drm_gem_object_unreference_unlocked(obj);
  477. return ret;
  478. }
  479. static int ioctl_gem_info(struct drm_device *dev, void *data,
  480. struct drm_file *file_priv)
  481. {
  482. struct drm_omap_gem_info *args = data;
  483. struct drm_gem_object *obj;
  484. int ret = 0;
  485. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  486. obj = drm_gem_object_lookup(file_priv, args->handle);
  487. if (!obj)
  488. return -ENOENT;
  489. args->size = omap_gem_mmap_size(obj);
  490. args->offset = omap_gem_mmap_offset(obj);
  491. drm_gem_object_unreference_unlocked(obj);
  492. return ret;
  493. }
  494. static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
  495. DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param, DRM_AUTH),
  496. DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  497. DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new, DRM_AUTH),
  498. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep, DRM_AUTH),
  499. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini, DRM_AUTH),
  500. DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info, DRM_AUTH),
  501. };
  502. /*
  503. * drm driver funcs
  504. */
  505. static int dev_open(struct drm_device *dev, struct drm_file *file)
  506. {
  507. file->driver_priv = NULL;
  508. DBG("open: dev=%p, file=%p", dev, file);
  509. return 0;
  510. }
  511. /**
  512. * lastclose - clean up after all DRM clients have exited
  513. * @dev: DRM device
  514. *
  515. * Take care of cleaning up after all DRM clients have exited. In the
  516. * mode setting case, we want to restore the kernel's initial mode (just
  517. * in case the last client left us in a bad state).
  518. */
  519. static void dev_lastclose(struct drm_device *dev)
  520. {
  521. int i;
  522. /* we don't support vga_switcheroo.. so just make sure the fbdev
  523. * mode is active
  524. */
  525. struct omap_drm_private *priv = dev->dev_private;
  526. int ret;
  527. DBG("lastclose: dev=%p", dev);
  528. /* need to restore default rotation state.. not sure
  529. * if there is a cleaner way to restore properties to
  530. * default state? Maybe a flag that properties should
  531. * automatically be restored to default state on
  532. * lastclose?
  533. */
  534. for (i = 0; i < priv->num_crtcs; i++) {
  535. struct drm_crtc *crtc = priv->crtcs[i];
  536. if (!crtc->primary->rotation_property)
  537. continue;
  538. drm_object_property_set_value(&crtc->base,
  539. crtc->primary->rotation_property,
  540. DRM_ROTATE_0);
  541. }
  542. for (i = 0; i < priv->num_planes; i++) {
  543. struct drm_plane *plane = priv->planes[i];
  544. if (!plane->rotation_property)
  545. continue;
  546. drm_object_property_set_value(&plane->base,
  547. plane->rotation_property,
  548. DRM_ROTATE_0);
  549. }
  550. if (priv->fbdev) {
  551. ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  552. if (ret)
  553. DBG("failed to restore crtc mode");
  554. }
  555. }
  556. static const struct vm_operations_struct omap_gem_vm_ops = {
  557. .fault = omap_gem_fault,
  558. .open = drm_gem_vm_open,
  559. .close = drm_gem_vm_close,
  560. };
  561. static const struct file_operations omapdriver_fops = {
  562. .owner = THIS_MODULE,
  563. .open = drm_open,
  564. .unlocked_ioctl = drm_ioctl,
  565. .release = drm_release,
  566. .mmap = omap_gem_mmap,
  567. .poll = drm_poll,
  568. .read = drm_read,
  569. .llseek = noop_llseek,
  570. };
  571. static struct drm_driver omap_drm_driver = {
  572. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
  573. DRIVER_ATOMIC,
  574. .open = dev_open,
  575. .lastclose = dev_lastclose,
  576. .get_vblank_counter = drm_vblank_no_hw_counter,
  577. .enable_vblank = omap_irq_enable_vblank,
  578. .disable_vblank = omap_irq_disable_vblank,
  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->omaprev = pdata->omaprev;
  622. priv->wq = alloc_ordered_workqueue("omapdrm", 0);
  623. init_waitqueue_head(&priv->commit.wait);
  624. spin_lock_init(&priv->commit.lock);
  625. spin_lock_init(&priv->list_lock);
  626. INIT_LIST_HEAD(&priv->obj_list);
  627. /* Allocate and initialize the DRM device. */
  628. ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
  629. if (IS_ERR(ddev)) {
  630. ret = PTR_ERR(ddev);
  631. goto err_free_priv;
  632. }
  633. ddev->dev_private = priv;
  634. platform_set_drvdata(pdev, ddev);
  635. omap_gem_init(ddev);
  636. ret = omap_modeset_init(ddev);
  637. if (ret) {
  638. dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
  639. goto err_free_drm_dev;
  640. }
  641. /* Initialize vblank handling, start with all CRTCs disabled. */
  642. ret = drm_vblank_init(ddev, priv->num_crtcs);
  643. if (ret) {
  644. dev_err(&pdev->dev, "could not init vblank\n");
  645. goto err_cleanup_modeset;
  646. }
  647. for (i = 0; i < priv->num_crtcs; i++)
  648. drm_crtc_vblank_off(priv->crtcs[i]);
  649. priv->fbdev = omap_fbdev_init(ddev);
  650. drm_kms_helper_poll_init(ddev);
  651. /*
  652. * Register the DRM device with the core and the connectors with
  653. * sysfs.
  654. */
  655. ret = drm_dev_register(ddev, 0);
  656. if (ret)
  657. goto err_cleanup_helpers;
  658. return 0;
  659. err_cleanup_helpers:
  660. drm_kms_helper_poll_fini(ddev);
  661. if (priv->fbdev)
  662. omap_fbdev_free(ddev);
  663. err_cleanup_modeset:
  664. drm_mode_config_cleanup(ddev);
  665. omap_drm_irq_uninstall(ddev);
  666. err_free_drm_dev:
  667. omap_gem_deinit(ddev);
  668. drm_dev_unref(ddev);
  669. err_free_priv:
  670. destroy_workqueue(priv->wq);
  671. kfree(priv);
  672. err_disconnect_dssdevs:
  673. omap_disconnect_dssdevs();
  674. err_crtc_uninit:
  675. omap_crtc_pre_uninit();
  676. return ret;
  677. }
  678. static int pdev_remove(struct platform_device *pdev)
  679. {
  680. struct drm_device *ddev = platform_get_drvdata(pdev);
  681. struct omap_drm_private *priv = ddev->dev_private;
  682. DBG("");
  683. drm_dev_unregister(ddev);
  684. drm_kms_helper_poll_fini(ddev);
  685. if (priv->fbdev)
  686. omap_fbdev_free(ddev);
  687. drm_mode_config_cleanup(ddev);
  688. omap_drm_irq_uninstall(ddev);
  689. omap_gem_deinit(ddev);
  690. drm_dev_unref(ddev);
  691. destroy_workqueue(priv->wq);
  692. kfree(priv);
  693. omap_disconnect_dssdevs();
  694. omap_crtc_pre_uninit();
  695. return 0;
  696. }
  697. #ifdef CONFIG_PM_SLEEP
  698. static int omap_drm_suspend_all_displays(void)
  699. {
  700. struct omap_dss_device *dssdev = NULL;
  701. for_each_dss_dev(dssdev) {
  702. if (!dssdev->driver)
  703. continue;
  704. if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
  705. dssdev->driver->disable(dssdev);
  706. dssdev->activate_after_resume = true;
  707. } else {
  708. dssdev->activate_after_resume = false;
  709. }
  710. }
  711. return 0;
  712. }
  713. static int omap_drm_resume_all_displays(void)
  714. {
  715. struct omap_dss_device *dssdev = NULL;
  716. for_each_dss_dev(dssdev) {
  717. if (!dssdev->driver)
  718. continue;
  719. if (dssdev->activate_after_resume) {
  720. dssdev->driver->enable(dssdev);
  721. dssdev->activate_after_resume = false;
  722. }
  723. }
  724. return 0;
  725. }
  726. static int omap_drm_suspend(struct device *dev)
  727. {
  728. struct drm_device *drm_dev = dev_get_drvdata(dev);
  729. drm_kms_helper_poll_disable(drm_dev);
  730. drm_modeset_lock_all(drm_dev);
  731. omap_drm_suspend_all_displays();
  732. drm_modeset_unlock_all(drm_dev);
  733. return 0;
  734. }
  735. static int omap_drm_resume(struct device *dev)
  736. {
  737. struct drm_device *drm_dev = dev_get_drvdata(dev);
  738. drm_modeset_lock_all(drm_dev);
  739. omap_drm_resume_all_displays();
  740. drm_modeset_unlock_all(drm_dev);
  741. drm_kms_helper_poll_enable(drm_dev);
  742. return omap_gem_resume(dev);
  743. }
  744. #endif
  745. static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
  746. static struct platform_driver pdev = {
  747. .driver = {
  748. .name = DRIVER_NAME,
  749. .pm = &omapdrm_pm_ops,
  750. },
  751. .probe = pdev_probe,
  752. .remove = pdev_remove,
  753. };
  754. static struct platform_driver * const drivers[] = {
  755. &omap_dmm_driver,
  756. &pdev,
  757. };
  758. static int __init omap_drm_init(void)
  759. {
  760. DBG("init");
  761. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  762. }
  763. static void __exit omap_drm_fini(void)
  764. {
  765. DBG("fini");
  766. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  767. }
  768. /* need late_initcall() so we load after dss_driver's are loaded */
  769. late_initcall(omap_drm_init);
  770. module_exit(omap_drm_fini);
  771. MODULE_AUTHOR("Rob Clark <rob@ti.com>");
  772. MODULE_DESCRIPTION("OMAP DRM Display Driver");
  773. MODULE_ALIAS("platform:" DRIVER_NAME);
  774. MODULE_LICENSE("GPL v2");