rockchip_drm_drv.c 14 KB

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