tilcdc_drv.c 15 KB

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