rockchip_drm_drv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author:Mark Yao <mark.yao@rock-chips.com>
  4. *
  5. * based on exynos_drm_drv.c
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <asm/dma-iommu.h>
  17. #include <drm/drmP.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_fb_helper.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/component.h>
  24. #include "rockchip_drm_drv.h"
  25. #include "rockchip_drm_fb.h"
  26. #include "rockchip_drm_fbdev.h"
  27. #include "rockchip_drm_gem.h"
  28. #define DRIVER_NAME "rockchip"
  29. #define DRIVER_DESC "RockChip Soc DRM"
  30. #define DRIVER_DATE "20140818"
  31. #define DRIVER_MAJOR 1
  32. #define DRIVER_MINOR 0
  33. /*
  34. * Attach a (component) device to the shared drm dma mapping from master drm
  35. * device. This is used by the VOPs to map GEM buffers to a common DMA
  36. * mapping.
  37. */
  38. int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
  39. struct device *dev)
  40. {
  41. struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
  42. int ret;
  43. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  44. if (ret)
  45. return ret;
  46. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  47. return arm_iommu_attach_device(dev, mapping);
  48. }
  49. EXPORT_SYMBOL_GPL(rockchip_drm_dma_attach_device);
  50. void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
  51. struct device *dev)
  52. {
  53. arm_iommu_detach_device(dev);
  54. }
  55. EXPORT_SYMBOL_GPL(rockchip_drm_dma_detach_device);
  56. int rockchip_register_crtc_funcs(struct drm_device *dev,
  57. const struct rockchip_crtc_funcs *crtc_funcs,
  58. int pipe)
  59. {
  60. struct rockchip_drm_private *priv = dev->dev_private;
  61. if (pipe > ROCKCHIP_MAX_CRTC)
  62. return -EINVAL;
  63. priv->crtc_funcs[pipe] = crtc_funcs;
  64. return 0;
  65. }
  66. EXPORT_SYMBOL_GPL(rockchip_register_crtc_funcs);
  67. void rockchip_unregister_crtc_funcs(struct drm_device *dev, int pipe)
  68. {
  69. struct rockchip_drm_private *priv = dev->dev_private;
  70. if (pipe > ROCKCHIP_MAX_CRTC)
  71. return;
  72. priv->crtc_funcs[pipe] = NULL;
  73. }
  74. EXPORT_SYMBOL_GPL(rockchip_unregister_crtc_funcs);
  75. static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
  76. int pipe)
  77. {
  78. struct drm_crtc *crtc;
  79. int i = 0;
  80. list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
  81. if (i++ == pipe)
  82. return crtc;
  83. return NULL;
  84. }
  85. static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
  86. {
  87. struct rockchip_drm_private *priv = dev->dev_private;
  88. struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
  89. if (crtc && priv->crtc_funcs[pipe] &&
  90. priv->crtc_funcs[pipe]->enable_vblank)
  91. return priv->crtc_funcs[pipe]->enable_vblank(crtc);
  92. return 0;
  93. }
  94. static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
  95. {
  96. struct rockchip_drm_private *priv = dev->dev_private;
  97. struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
  98. if (crtc && priv->crtc_funcs[pipe] &&
  99. priv->crtc_funcs[pipe]->enable_vblank)
  100. priv->crtc_funcs[pipe]->disable_vblank(crtc);
  101. }
  102. static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
  103. {
  104. struct rockchip_drm_private *private;
  105. struct dma_iommu_mapping *mapping;
  106. struct device *dev = drm_dev->dev;
  107. struct drm_connector *connector;
  108. int ret;
  109. private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
  110. if (!private)
  111. return -ENOMEM;
  112. drm_dev->dev_private = private;
  113. drm_mode_config_init(drm_dev);
  114. rockchip_drm_mode_config_init(drm_dev);
  115. dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
  116. GFP_KERNEL);
  117. if (!dev->dma_parms) {
  118. ret = -ENOMEM;
  119. goto err_config_cleanup;
  120. }
  121. /* TODO(djkurtz): fetch the mapping start/size from somewhere */
  122. mapping = arm_iommu_create_mapping(&platform_bus_type, 0x00000000,
  123. SZ_2G);
  124. if (IS_ERR(mapping)) {
  125. ret = PTR_ERR(mapping);
  126. goto err_config_cleanup;
  127. }
  128. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  129. if (ret)
  130. goto err_release_mapping;
  131. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  132. ret = arm_iommu_attach_device(dev, mapping);
  133. if (ret)
  134. goto err_release_mapping;
  135. /* Try to bind all sub drivers. */
  136. ret = component_bind_all(dev, drm_dev);
  137. if (ret)
  138. goto err_detach_device;
  139. /*
  140. * All components are now added, we can publish the connector sysfs
  141. * entries to userspace. This will generate hotplug events and so
  142. * userspace will expect to be able to access DRM at this point.
  143. */
  144. list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
  145. head) {
  146. ret = drm_connector_register(connector);
  147. if (ret) {
  148. dev_err(drm_dev->dev,
  149. "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
  150. connector->base.id,
  151. connector->name, ret);
  152. goto err_unbind;
  153. }
  154. }
  155. /* init kms poll for handling hpd */
  156. drm_kms_helper_poll_init(drm_dev);
  157. /*
  158. * enable drm irq mode.
  159. * - with irq_enabled = true, we can use the vblank feature.
  160. */
  161. drm_dev->irq_enabled = true;
  162. ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
  163. if (ret)
  164. goto err_kms_helper_poll_fini;
  165. /*
  166. * with vblank_disable_allowed = true, vblank interrupt will be disabled
  167. * by drm timer once a current process gives up ownership of
  168. * vblank event.(after drm_vblank_put function is called)
  169. */
  170. drm_dev->vblank_disable_allowed = true;
  171. ret = rockchip_drm_fbdev_init(drm_dev);
  172. if (ret)
  173. goto err_vblank_cleanup;
  174. return 0;
  175. err_vblank_cleanup:
  176. drm_vblank_cleanup(drm_dev);
  177. err_kms_helper_poll_fini:
  178. drm_kms_helper_poll_fini(drm_dev);
  179. err_unbind:
  180. component_unbind_all(dev, drm_dev);
  181. err_detach_device:
  182. arm_iommu_detach_device(dev);
  183. err_release_mapping:
  184. arm_iommu_release_mapping(dev->archdata.mapping);
  185. err_config_cleanup:
  186. drm_mode_config_cleanup(drm_dev);
  187. drm_dev->dev_private = NULL;
  188. return ret;
  189. }
  190. static int rockchip_drm_unload(struct drm_device *drm_dev)
  191. {
  192. struct device *dev = drm_dev->dev;
  193. rockchip_drm_fbdev_fini(drm_dev);
  194. drm_vblank_cleanup(drm_dev);
  195. drm_kms_helper_poll_fini(drm_dev);
  196. component_unbind_all(dev, drm_dev);
  197. arm_iommu_detach_device(dev);
  198. arm_iommu_release_mapping(dev->archdata.mapping);
  199. drm_mode_config_cleanup(drm_dev);
  200. drm_dev->dev_private = NULL;
  201. return 0;
  202. }
  203. void rockchip_drm_lastclose(struct drm_device *dev)
  204. {
  205. struct rockchip_drm_private *priv = dev->dev_private;
  206. drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
  207. }
  208. static const struct file_operations rockchip_drm_driver_fops = {
  209. .owner = THIS_MODULE,
  210. .open = drm_open,
  211. .mmap = rockchip_gem_mmap,
  212. .poll = drm_poll,
  213. .read = drm_read,
  214. .unlocked_ioctl = drm_ioctl,
  215. #ifdef CONFIG_COMPAT
  216. .compat_ioctl = drm_compat_ioctl,
  217. #endif
  218. .release = drm_release,
  219. };
  220. const struct vm_operations_struct rockchip_drm_vm_ops = {
  221. .open = drm_gem_vm_open,
  222. .close = drm_gem_vm_close,
  223. };
  224. static struct drm_driver rockchip_drm_driver = {
  225. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
  226. .load = rockchip_drm_load,
  227. .unload = rockchip_drm_unload,
  228. .lastclose = rockchip_drm_lastclose,
  229. .get_vblank_counter = drm_vblank_count,
  230. .enable_vblank = rockchip_drm_crtc_enable_vblank,
  231. .disable_vblank = rockchip_drm_crtc_disable_vblank,
  232. .gem_vm_ops = &rockchip_drm_vm_ops,
  233. .gem_free_object = rockchip_gem_free_object,
  234. .dumb_create = rockchip_gem_dumb_create,
  235. .dumb_map_offset = rockchip_gem_dumb_map_offset,
  236. .dumb_destroy = drm_gem_dumb_destroy,
  237. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  238. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  239. .gem_prime_import = drm_gem_prime_import,
  240. .gem_prime_export = drm_gem_prime_export,
  241. .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
  242. .gem_prime_vmap = rockchip_gem_prime_vmap,
  243. .gem_prime_vunmap = rockchip_gem_prime_vunmap,
  244. .gem_prime_mmap = rockchip_gem_mmap_buf,
  245. .fops = &rockchip_drm_driver_fops,
  246. .name = DRIVER_NAME,
  247. .desc = DRIVER_DESC,
  248. .date = DRIVER_DATE,
  249. .major = DRIVER_MAJOR,
  250. .minor = DRIVER_MINOR,
  251. };
  252. #ifdef CONFIG_PM_SLEEP
  253. static int rockchip_drm_sys_suspend(struct device *dev)
  254. {
  255. struct drm_device *drm = dev_get_drvdata(dev);
  256. struct drm_connector *connector;
  257. if (!drm)
  258. return 0;
  259. drm_modeset_lock_all(drm);
  260. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  261. int old_dpms = connector->dpms;
  262. if (connector->funcs->dpms)
  263. connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
  264. /* Set the old mode back to the connector for resume */
  265. connector->dpms = old_dpms;
  266. }
  267. drm_modeset_unlock_all(drm);
  268. return 0;
  269. }
  270. static int rockchip_drm_sys_resume(struct device *dev)
  271. {
  272. struct drm_device *drm = dev_get_drvdata(dev);
  273. struct drm_connector *connector;
  274. enum drm_connector_status status;
  275. bool changed = false;
  276. if (!drm)
  277. return 0;
  278. drm_modeset_lock_all(drm);
  279. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  280. int desired_mode = connector->dpms;
  281. /*
  282. * at suspend time, we save dpms to connector->dpms,
  283. * restore the old_dpms, and at current time, the connector
  284. * dpms status must be DRM_MODE_DPMS_OFF.
  285. */
  286. connector->dpms = DRM_MODE_DPMS_OFF;
  287. /*
  288. * If the connector has been disconnected during suspend,
  289. * disconnect it from the encoder and leave it off. We'll notify
  290. * userspace at the end.
  291. */
  292. if (desired_mode == DRM_MODE_DPMS_ON) {
  293. status = connector->funcs->detect(connector, true);
  294. if (status == connector_status_disconnected) {
  295. connector->encoder = NULL;
  296. connector->status = status;
  297. changed = true;
  298. continue;
  299. }
  300. }
  301. if (connector->funcs->dpms)
  302. connector->funcs->dpms(connector, desired_mode);
  303. }
  304. drm_modeset_unlock_all(drm);
  305. drm_helper_resume_force_mode(drm);
  306. if (changed)
  307. drm_kms_helper_hotplug_event(drm);
  308. return 0;
  309. }
  310. #endif
  311. static const struct dev_pm_ops rockchip_drm_pm_ops = {
  312. SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
  313. rockchip_drm_sys_resume)
  314. };
  315. /*
  316. * @node: device tree node containing encoder input ports
  317. * @encoder: drm_encoder
  318. */
  319. int rockchip_drm_encoder_get_mux_id(struct device_node *node,
  320. struct drm_encoder *encoder)
  321. {
  322. struct device_node *ep;
  323. struct drm_crtc *crtc = encoder->crtc;
  324. struct of_endpoint endpoint;
  325. struct device_node *port;
  326. int ret;
  327. if (!node || !crtc)
  328. return -EINVAL;
  329. for_each_endpoint_of_node(node, ep) {
  330. port = of_graph_get_remote_port(ep);
  331. of_node_put(port);
  332. if (port == crtc->port) {
  333. ret = of_graph_parse_endpoint(ep, &endpoint);
  334. of_node_put(ep);
  335. return ret ?: endpoint.id;
  336. }
  337. }
  338. return -EINVAL;
  339. }
  340. EXPORT_SYMBOL_GPL(rockchip_drm_encoder_get_mux_id);
  341. static int compare_of(struct device *dev, void *data)
  342. {
  343. struct device_node *np = data;
  344. return dev->of_node == np;
  345. }
  346. static void rockchip_add_endpoints(struct device *dev,
  347. struct component_match **match,
  348. struct device_node *port)
  349. {
  350. struct device_node *ep, *remote;
  351. for_each_child_of_node(port, ep) {
  352. remote = of_graph_get_remote_port_parent(ep);
  353. if (!remote || !of_device_is_available(remote)) {
  354. of_node_put(remote);
  355. continue;
  356. } else if (!of_device_is_available(remote->parent)) {
  357. dev_warn(dev, "parent device of %s is not available\n",
  358. remote->full_name);
  359. of_node_put(remote);
  360. continue;
  361. }
  362. component_match_add(dev, match, compare_of, remote);
  363. of_node_put(remote);
  364. }
  365. }
  366. static int rockchip_drm_bind(struct device *dev)
  367. {
  368. struct drm_device *drm;
  369. int ret;
  370. drm = drm_dev_alloc(&rockchip_drm_driver, dev);
  371. if (!drm)
  372. return -ENOMEM;
  373. ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
  374. if (ret)
  375. goto err_free;
  376. ret = drm_dev_register(drm, 0);
  377. if (ret)
  378. goto err_free;
  379. dev_set_drvdata(dev, drm);
  380. return 0;
  381. err_free:
  382. drm_dev_unref(drm);
  383. return ret;
  384. }
  385. static void rockchip_drm_unbind(struct device *dev)
  386. {
  387. struct drm_device *drm = dev_get_drvdata(dev);
  388. drm_dev_unregister(drm);
  389. drm_dev_unref(drm);
  390. dev_set_drvdata(dev, NULL);
  391. }
  392. static const struct component_master_ops rockchip_drm_ops = {
  393. .bind = rockchip_drm_bind,
  394. .unbind = rockchip_drm_unbind,
  395. };
  396. static int rockchip_drm_platform_probe(struct platform_device *pdev)
  397. {
  398. struct device *dev = &pdev->dev;
  399. struct component_match *match = NULL;
  400. struct device_node *np = dev->of_node;
  401. struct device_node *port;
  402. int i;
  403. if (!np)
  404. return -ENODEV;
  405. /*
  406. * Bind the crtc ports first, so that
  407. * drm_of_find_possible_crtcs called from encoder .bind callbacks
  408. * works as expected.
  409. */
  410. for (i = 0;; i++) {
  411. port = of_parse_phandle(np, "ports", i);
  412. if (!port)
  413. break;
  414. if (!of_device_is_available(port->parent)) {
  415. of_node_put(port);
  416. continue;
  417. }
  418. component_match_add(dev, &match, compare_of, port->parent);
  419. of_node_put(port);
  420. }
  421. if (i == 0) {
  422. dev_err(dev, "missing 'ports' property\n");
  423. return -ENODEV;
  424. }
  425. if (!match) {
  426. dev_err(dev, "No available vop found for display-subsystem.\n");
  427. return -ENODEV;
  428. }
  429. /*
  430. * For each bound crtc, bind the encoders attached to its
  431. * remote endpoint.
  432. */
  433. for (i = 0;; i++) {
  434. port = of_parse_phandle(np, "ports", i);
  435. if (!port)
  436. break;
  437. if (!of_device_is_available(port->parent)) {
  438. of_node_put(port);
  439. continue;
  440. }
  441. rockchip_add_endpoints(dev, &match, port);
  442. of_node_put(port);
  443. }
  444. return component_master_add_with_match(dev, &rockchip_drm_ops, match);
  445. }
  446. static int rockchip_drm_platform_remove(struct platform_device *pdev)
  447. {
  448. component_master_del(&pdev->dev, &rockchip_drm_ops);
  449. return 0;
  450. }
  451. static const struct of_device_id rockchip_drm_dt_ids[] = {
  452. { .compatible = "rockchip,display-subsystem", },
  453. { /* sentinel */ },
  454. };
  455. MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
  456. static struct platform_driver rockchip_drm_platform_driver = {
  457. .probe = rockchip_drm_platform_probe,
  458. .remove = rockchip_drm_platform_remove,
  459. .driver = {
  460. .owner = THIS_MODULE,
  461. .name = "rockchip-drm",
  462. .of_match_table = rockchip_drm_dt_ids,
  463. .pm = &rockchip_drm_pm_ops,
  464. },
  465. };
  466. module_platform_driver(rockchip_drm_platform_driver);
  467. MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
  468. MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
  469. MODULE_LICENSE("GPL v2");