tilcdc_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * Copyright (C) 2012 Texas Instruments
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /* LCDC DRM driver, based on da8xx-fb */
  18. #include "tilcdc_drv.h"
  19. #include "tilcdc_regs.h"
  20. #include "tilcdc_tfp410.h"
  21. #include "tilcdc_slave.h"
  22. #include "drm_fb_helper.h"
  23. static LIST_HEAD(module_list);
  24. void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
  25. const struct tilcdc_module_ops *funcs)
  26. {
  27. mod->name = name;
  28. mod->funcs = funcs;
  29. INIT_LIST_HEAD(&mod->list);
  30. list_add(&mod->list, &module_list);
  31. }
  32. void tilcdc_module_cleanup(struct tilcdc_module *mod)
  33. {
  34. list_del(&mod->list);
  35. }
  36. static struct of_device_id tilcdc_of_match[];
  37. static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
  38. struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd)
  39. {
  40. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  41. }
  42. static void tilcdc_fb_output_poll_changed(struct drm_device *dev)
  43. {
  44. struct tilcdc_drm_private *priv = dev->dev_private;
  45. if (priv->fbdev)
  46. drm_fbdev_cma_hotplug_event(priv->fbdev);
  47. }
  48. static const struct drm_mode_config_funcs mode_config_funcs = {
  49. .fb_create = tilcdc_fb_create,
  50. .output_poll_changed = tilcdc_fb_output_poll_changed,
  51. };
  52. static int modeset_init(struct drm_device *dev)
  53. {
  54. struct tilcdc_drm_private *priv = dev->dev_private;
  55. struct tilcdc_module *mod;
  56. drm_mode_config_init(dev);
  57. priv->crtc = tilcdc_crtc_create(dev);
  58. list_for_each_entry(mod, &module_list, list) {
  59. DBG("loading module: %s", mod->name);
  60. mod->funcs->modeset_init(mod, dev);
  61. }
  62. if ((priv->num_encoders = 0) || (priv->num_connectors == 0)) {
  63. /* oh nos! */
  64. dev_err(dev->dev, "no encoders/connectors found\n");
  65. return -ENXIO;
  66. }
  67. dev->mode_config.min_width = 0;
  68. dev->mode_config.min_height = 0;
  69. dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc);
  70. dev->mode_config.max_height = 2048;
  71. dev->mode_config.funcs = &mode_config_funcs;
  72. return 0;
  73. }
  74. #ifdef CONFIG_CPU_FREQ
  75. static int cpufreq_transition(struct notifier_block *nb,
  76. unsigned long val, void *data)
  77. {
  78. struct tilcdc_drm_private *priv = container_of(nb,
  79. struct tilcdc_drm_private, freq_transition);
  80. if (val == CPUFREQ_POSTCHANGE) {
  81. if (priv->lcd_fck_rate != clk_get_rate(priv->clk)) {
  82. priv->lcd_fck_rate = clk_get_rate(priv->clk);
  83. tilcdc_crtc_update_clk(priv->crtc);
  84. }
  85. }
  86. return 0;
  87. }
  88. #endif
  89. /*
  90. * DRM operations:
  91. */
  92. static int tilcdc_unload(struct drm_device *dev)
  93. {
  94. struct tilcdc_drm_private *priv = dev->dev_private;
  95. struct tilcdc_module *mod, *cur;
  96. drm_kms_helper_poll_fini(dev);
  97. drm_mode_config_cleanup(dev);
  98. drm_vblank_cleanup(dev);
  99. pm_runtime_get_sync(dev->dev);
  100. drm_irq_uninstall(dev);
  101. pm_runtime_put_sync(dev->dev);
  102. #ifdef CONFIG_CPU_FREQ
  103. cpufreq_unregister_notifier(&priv->freq_transition,
  104. CPUFREQ_TRANSITION_NOTIFIER);
  105. #endif
  106. if (priv->clk)
  107. clk_put(priv->clk);
  108. if (priv->mmio)
  109. iounmap(priv->mmio);
  110. flush_workqueue(priv->wq);
  111. destroy_workqueue(priv->wq);
  112. dev->dev_private = NULL;
  113. pm_runtime_disable(dev->dev);
  114. list_for_each_entry_safe(mod, cur, &module_list, list) {
  115. DBG("destroying module: %s", mod->name);
  116. mod->funcs->destroy(mod);
  117. }
  118. kfree(priv);
  119. return 0;
  120. }
  121. static int tilcdc_load(struct drm_device *dev, unsigned long flags)
  122. {
  123. struct platform_device *pdev = dev->platformdev;
  124. struct device_node *node = pdev->dev.of_node;
  125. struct tilcdc_drm_private *priv;
  126. struct resource *res;
  127. int ret;
  128. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  129. if (!priv) {
  130. dev_err(dev->dev, "failed to allocate private data\n");
  131. return -ENOMEM;
  132. }
  133. dev->dev_private = priv;
  134. priv->wq = alloc_ordered_workqueue("tilcdc", 0);
  135. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  136. if (!res) {
  137. dev_err(dev->dev, "failed to get memory resource\n");
  138. ret = -EINVAL;
  139. goto fail;
  140. }
  141. priv->mmio = ioremap_nocache(res->start, resource_size(res));
  142. if (!priv->mmio) {
  143. dev_err(dev->dev, "failed to ioremap\n");
  144. ret = -ENOMEM;
  145. goto fail;
  146. }
  147. priv->clk = clk_get(dev->dev, "fck");
  148. if (IS_ERR(priv->clk)) {
  149. dev_err(dev->dev, "failed to get functional clock\n");
  150. ret = -ENODEV;
  151. goto fail;
  152. }
  153. priv->disp_clk = clk_get(dev->dev, "dpll_disp_ck");
  154. if (IS_ERR(priv->clk)) {
  155. dev_err(dev->dev, "failed to get display clock\n");
  156. ret = -ENODEV;
  157. goto fail;
  158. }
  159. #ifdef CONFIG_CPU_FREQ
  160. priv->lcd_fck_rate = clk_get_rate(priv->clk);
  161. priv->freq_transition.notifier_call = cpufreq_transition;
  162. ret = cpufreq_register_notifier(&priv->freq_transition,
  163. CPUFREQ_TRANSITION_NOTIFIER);
  164. if (ret) {
  165. dev_err(dev->dev, "failed to register cpufreq notifier\n");
  166. goto fail;
  167. }
  168. #endif
  169. if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
  170. priv->max_bandwidth = 1280 * 1024 * 60;
  171. pm_runtime_enable(dev->dev);
  172. /* Determine LCD IP Version */
  173. pm_runtime_get_sync(dev->dev);
  174. switch (tilcdc_read(dev, LCDC_PID_REG)) {
  175. case 0x4c100102:
  176. priv->rev = 1;
  177. break;
  178. case 0x4f200800:
  179. case 0x4f201000:
  180. priv->rev = 2;
  181. break;
  182. default:
  183. dev_warn(dev->dev, "Unknown PID Reg value 0x%08x, "
  184. "defaulting to LCD revision 1\n",
  185. tilcdc_read(dev, LCDC_PID_REG));
  186. priv->rev = 1;
  187. break;
  188. }
  189. pm_runtime_put_sync(dev->dev);
  190. ret = modeset_init(dev);
  191. if (ret < 0) {
  192. dev_err(dev->dev, "failed to initialize mode setting\n");
  193. goto fail;
  194. }
  195. ret = drm_vblank_init(dev, 1);
  196. if (ret < 0) {
  197. dev_err(dev->dev, "failed to initialize vblank\n");
  198. goto fail;
  199. }
  200. pm_runtime_get_sync(dev->dev);
  201. ret = drm_irq_install(dev);
  202. pm_runtime_put_sync(dev->dev);
  203. if (ret < 0) {
  204. dev_err(dev->dev, "failed to install IRQ handler\n");
  205. goto fail;
  206. }
  207. platform_set_drvdata(pdev, dev);
  208. priv->fbdev = drm_fbdev_cma_init(dev, 16,
  209. dev->mode_config.num_crtc,
  210. dev->mode_config.num_connector);
  211. drm_kms_helper_poll_init(dev);
  212. return 0;
  213. fail:
  214. tilcdc_unload(dev);
  215. return ret;
  216. }
  217. static void tilcdc_preclose(struct drm_device *dev, struct drm_file *file)
  218. {
  219. struct tilcdc_drm_private *priv = dev->dev_private;
  220. tilcdc_crtc_cancel_page_flip(priv->crtc, file);
  221. }
  222. static void tilcdc_lastclose(struct drm_device *dev)
  223. {
  224. struct tilcdc_drm_private *priv = dev->dev_private;
  225. drm_fbdev_cma_restore_mode(priv->fbdev);
  226. }
  227. static irqreturn_t tilcdc_irq(DRM_IRQ_ARGS)
  228. {
  229. struct drm_device *dev = arg;
  230. struct tilcdc_drm_private *priv = dev->dev_private;
  231. return tilcdc_crtc_irq(priv->crtc);
  232. }
  233. static void tilcdc_irq_preinstall(struct drm_device *dev)
  234. {
  235. tilcdc_clear_irqstatus(dev, 0xffffffff);
  236. }
  237. static int tilcdc_irq_postinstall(struct drm_device *dev)
  238. {
  239. struct tilcdc_drm_private *priv = dev->dev_private;
  240. /* enable FIFO underflow irq: */
  241. if (priv->rev == 1) {
  242. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_UNDERFLOW_INT_ENA);
  243. } else {
  244. tilcdc_set(dev, LCDC_INT_ENABLE_SET_REG, LCDC_V2_UNDERFLOW_INT_ENA);
  245. }
  246. return 0;
  247. }
  248. static void tilcdc_irq_uninstall(struct drm_device *dev)
  249. {
  250. struct tilcdc_drm_private *priv = dev->dev_private;
  251. /* disable irqs that we might have enabled: */
  252. if (priv->rev == 1) {
  253. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
  254. LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
  255. tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_V1_END_OF_FRAME_INT_ENA);
  256. } else {
  257. tilcdc_clear(dev, LCDC_INT_ENABLE_SET_REG,
  258. LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
  259. LCDC_V2_END_OF_FRAME0_INT_ENA | LCDC_V2_END_OF_FRAME1_INT_ENA |
  260. LCDC_FRAME_DONE);
  261. }
  262. }
  263. static void enable_vblank(struct drm_device *dev, bool enable)
  264. {
  265. struct tilcdc_drm_private *priv = dev->dev_private;
  266. u32 reg, mask;
  267. if (priv->rev == 1) {
  268. reg = LCDC_DMA_CTRL_REG;
  269. mask = LCDC_V1_END_OF_FRAME_INT_ENA;
  270. } else {
  271. reg = LCDC_INT_ENABLE_SET_REG;
  272. mask = LCDC_V2_END_OF_FRAME0_INT_ENA |
  273. LCDC_V2_END_OF_FRAME1_INT_ENA | LCDC_FRAME_DONE;
  274. }
  275. if (enable)
  276. tilcdc_set(dev, reg, mask);
  277. else
  278. tilcdc_clear(dev, reg, mask);
  279. }
  280. static int tilcdc_enable_vblank(struct drm_device *dev, int crtc)
  281. {
  282. enable_vblank(dev, true);
  283. return 0;
  284. }
  285. static void tilcdc_disable_vblank(struct drm_device *dev, int crtc)
  286. {
  287. enable_vblank(dev, false);
  288. }
  289. #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PM_SLEEP)
  290. static const struct {
  291. const char *name;
  292. uint8_t rev;
  293. uint8_t save;
  294. uint32_t reg;
  295. } registers[] = {
  296. #define REG(rev, save, reg) { #reg, rev, save, reg }
  297. /* exists in revision 1: */
  298. REG(1, false, LCDC_PID_REG),
  299. REG(1, true, LCDC_CTRL_REG),
  300. REG(1, false, LCDC_STAT_REG),
  301. REG(1, true, LCDC_RASTER_CTRL_REG),
  302. REG(1, true, LCDC_RASTER_TIMING_0_REG),
  303. REG(1, true, LCDC_RASTER_TIMING_1_REG),
  304. REG(1, true, LCDC_RASTER_TIMING_2_REG),
  305. REG(1, true, LCDC_DMA_CTRL_REG),
  306. REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
  307. REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
  308. REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
  309. REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
  310. /* new in revision 2: */
  311. REG(2, false, LCDC_RAW_STAT_REG),
  312. REG(2, false, LCDC_MASKED_STAT_REG),
  313. REG(2, false, LCDC_INT_ENABLE_SET_REG),
  314. REG(2, false, LCDC_INT_ENABLE_CLR_REG),
  315. REG(2, false, LCDC_END_OF_INT_IND_REG),
  316. REG(2, true, LCDC_CLK_ENABLE_REG),
  317. REG(2, true, LCDC_INT_ENABLE_SET_REG),
  318. #undef REG
  319. };
  320. #endif
  321. #ifdef CONFIG_DEBUG_FS
  322. static int tilcdc_regs_show(struct seq_file *m, void *arg)
  323. {
  324. struct drm_info_node *node = (struct drm_info_node *) m->private;
  325. struct drm_device *dev = node->minor->dev;
  326. struct tilcdc_drm_private *priv = dev->dev_private;
  327. unsigned i;
  328. pm_runtime_get_sync(dev->dev);
  329. seq_printf(m, "revision: %d\n", priv->rev);
  330. for (i = 0; i < ARRAY_SIZE(registers); i++)
  331. if (priv->rev >= registers[i].rev)
  332. seq_printf(m, "%s:\t %08x\n", registers[i].name,
  333. tilcdc_read(dev, registers[i].reg));
  334. pm_runtime_put_sync(dev->dev);
  335. return 0;
  336. }
  337. static int tilcdc_mm_show(struct seq_file *m, void *arg)
  338. {
  339. struct drm_info_node *node = (struct drm_info_node *) m->private;
  340. struct drm_device *dev = node->minor->dev;
  341. return drm_mm_dump_table(m, dev->mm_private);
  342. }
  343. static struct drm_info_list tilcdc_debugfs_list[] = {
  344. { "regs", tilcdc_regs_show, 0 },
  345. { "mm", tilcdc_mm_show, 0 },
  346. { "fb", drm_fb_cma_debugfs_show, 0 },
  347. };
  348. static int tilcdc_debugfs_init(struct drm_minor *minor)
  349. {
  350. struct drm_device *dev = minor->dev;
  351. struct tilcdc_module *mod;
  352. int ret;
  353. ret = drm_debugfs_create_files(tilcdc_debugfs_list,
  354. ARRAY_SIZE(tilcdc_debugfs_list),
  355. minor->debugfs_root, minor);
  356. list_for_each_entry(mod, &module_list, list)
  357. if (mod->funcs->debugfs_init)
  358. mod->funcs->debugfs_init(mod, minor);
  359. if (ret) {
  360. dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
  361. return ret;
  362. }
  363. return ret;
  364. }
  365. static void tilcdc_debugfs_cleanup(struct drm_minor *minor)
  366. {
  367. struct tilcdc_module *mod;
  368. drm_debugfs_remove_files(tilcdc_debugfs_list,
  369. ARRAY_SIZE(tilcdc_debugfs_list), minor);
  370. list_for_each_entry(mod, &module_list, list)
  371. if (mod->funcs->debugfs_cleanup)
  372. mod->funcs->debugfs_cleanup(mod, minor);
  373. }
  374. #endif
  375. static const struct file_operations fops = {
  376. .owner = THIS_MODULE,
  377. .open = drm_open,
  378. .release = drm_release,
  379. .unlocked_ioctl = drm_ioctl,
  380. #ifdef CONFIG_COMPAT
  381. .compat_ioctl = drm_compat_ioctl,
  382. #endif
  383. .poll = drm_poll,
  384. .read = drm_read,
  385. .fasync = drm_fasync,
  386. .llseek = no_llseek,
  387. .mmap = drm_gem_cma_mmap,
  388. };
  389. static struct drm_driver tilcdc_driver = {
  390. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET,
  391. .load = tilcdc_load,
  392. .unload = tilcdc_unload,
  393. .preclose = tilcdc_preclose,
  394. .lastclose = tilcdc_lastclose,
  395. .irq_handler = tilcdc_irq,
  396. .irq_preinstall = tilcdc_irq_preinstall,
  397. .irq_postinstall = tilcdc_irq_postinstall,
  398. .irq_uninstall = tilcdc_irq_uninstall,
  399. .get_vblank_counter = drm_vblank_count,
  400. .enable_vblank = tilcdc_enable_vblank,
  401. .disable_vblank = tilcdc_disable_vblank,
  402. .gem_free_object = drm_gem_cma_free_object,
  403. .gem_vm_ops = &drm_gem_cma_vm_ops,
  404. .dumb_create = drm_gem_cma_dumb_create,
  405. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  406. .dumb_destroy = drm_gem_cma_dumb_destroy,
  407. #ifdef CONFIG_DEBUG_FS
  408. .debugfs_init = tilcdc_debugfs_init,
  409. .debugfs_cleanup = tilcdc_debugfs_cleanup,
  410. #endif
  411. .fops = &fops,
  412. .name = "tilcdc",
  413. .desc = "TI LCD Controller DRM",
  414. .date = "20121205",
  415. .major = 1,
  416. .minor = 0,
  417. };
  418. /*
  419. * Power management:
  420. */
  421. #ifdef CONFIG_PM_SLEEP
  422. static int tilcdc_pm_suspend(struct device *dev)
  423. {
  424. struct drm_device *ddev = dev_get_drvdata(dev);
  425. struct tilcdc_drm_private *priv = ddev->dev_private;
  426. unsigned i, n = 0;
  427. drm_kms_helper_poll_disable(ddev);
  428. /* Save register state: */
  429. for (i = 0; i < ARRAY_SIZE(registers); i++)
  430. if (registers[i].save && (priv->rev >= registers[i].rev))
  431. priv->saved_register[n++] = tilcdc_read(ddev, registers[i].reg);
  432. return 0;
  433. }
  434. static int tilcdc_pm_resume(struct device *dev)
  435. {
  436. struct drm_device *ddev = dev_get_drvdata(dev);
  437. struct tilcdc_drm_private *priv = ddev->dev_private;
  438. unsigned i, n = 0;
  439. /* Restore register state: */
  440. for (i = 0; i < ARRAY_SIZE(registers); i++)
  441. if (registers[i].save && (priv->rev >= registers[i].rev))
  442. tilcdc_write(ddev, registers[i].reg, priv->saved_register[n++]);
  443. drm_kms_helper_poll_enable(ddev);
  444. return 0;
  445. }
  446. #endif
  447. static const struct dev_pm_ops tilcdc_pm_ops = {
  448. SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
  449. };
  450. /*
  451. * Platform driver:
  452. */
  453. static int tilcdc_pdev_probe(struct platform_device *pdev)
  454. {
  455. /* bail out early if no DT data: */
  456. if (!pdev->dev.of_node) {
  457. dev_err(&pdev->dev, "device-tree data is missing\n");
  458. return -ENXIO;
  459. }
  460. return drm_platform_init(&tilcdc_driver, pdev);
  461. }
  462. static int tilcdc_pdev_remove(struct platform_device *pdev)
  463. {
  464. drm_platform_exit(&tilcdc_driver, pdev);
  465. return 0;
  466. }
  467. static struct of_device_id tilcdc_of_match[] = {
  468. { .compatible = "ti,am33xx-tilcdc", },
  469. { },
  470. };
  471. MODULE_DEVICE_TABLE(of, tilcdc_of_match);
  472. static struct platform_driver tilcdc_platform_driver = {
  473. .probe = tilcdc_pdev_probe,
  474. .remove = tilcdc_pdev_remove,
  475. .driver = {
  476. .owner = THIS_MODULE,
  477. .name = "tilcdc",
  478. .pm = &tilcdc_pm_ops,
  479. .of_match_table = tilcdc_of_match,
  480. },
  481. };
  482. static int __init tilcdc_drm_init(void)
  483. {
  484. DBG("init");
  485. tilcdc_tfp410_init();
  486. tilcdc_slave_init();
  487. return platform_driver_register(&tilcdc_platform_driver);
  488. }
  489. static void __exit tilcdc_drm_fini(void)
  490. {
  491. DBG("fini");
  492. tilcdc_tfp410_fini();
  493. tilcdc_slave_fini();
  494. platform_driver_unregister(&tilcdc_platform_driver);
  495. }
  496. late_initcall(tilcdc_drm_init);
  497. module_exit(tilcdc_drm_fini);
  498. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  499. MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
  500. MODULE_LICENSE("GPL");