tilcdc_drv.c 15 KB

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