tilcdc_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Texas Instruments
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. */
  6. /* LCDC DRM driver, based on da8xx-fb */
  7. #include <linux/component.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/pinctrl/consumer.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <drm/clients/drm_client_setup.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_debugfs.h>
  16. #include <drm/drm_drv.h>
  17. #include <drm/drm_fbdev_dma.h>
  18. #include <drm/drm_fourcc.h>
  19. #include <drm/drm_gem_dma_helper.h>
  20. #include <drm/drm_gem_framebuffer_helper.h>
  21. #include <drm/drm_mm.h>
  22. #include <drm/drm_probe_helper.h>
  23. #include <drm/drm_vblank.h>
  24. #include "tilcdc_drv.h"
  25. #include "tilcdc_external.h"
  26. #include "tilcdc_panel.h"
  27. #include "tilcdc_regs.h"
  28. static LIST_HEAD(module_list);
  29. static const u32 tilcdc_rev1_formats[] = { DRM_FORMAT_RGB565 };
  30. static const u32 tilcdc_straight_formats[] = { DRM_FORMAT_RGB565,
  31. DRM_FORMAT_BGR888,
  32. DRM_FORMAT_XBGR8888 };
  33. static const u32 tilcdc_crossed_formats[] = { DRM_FORMAT_BGR565,
  34. DRM_FORMAT_RGB888,
  35. DRM_FORMAT_XRGB8888 };
  36. static const u32 tilcdc_legacy_formats[] = { DRM_FORMAT_RGB565,
  37. DRM_FORMAT_RGB888,
  38. DRM_FORMAT_XRGB8888 };
  39. void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
  40. const struct tilcdc_module_ops *funcs)
  41. {
  42. mod->name = name;
  43. mod->funcs = funcs;
  44. INIT_LIST_HEAD(&mod->list);
  45. list_add(&mod->list, &module_list);
  46. }
  47. void tilcdc_module_cleanup(struct tilcdc_module *mod)
  48. {
  49. list_del(&mod->list);
  50. }
  51. static int tilcdc_atomic_check(struct drm_device *dev,
  52. struct drm_atomic_state *state)
  53. {
  54. int ret;
  55. ret = drm_atomic_helper_check_modeset(dev, state);
  56. if (ret)
  57. return ret;
  58. ret = drm_atomic_helper_check_planes(dev, state);
  59. if (ret)
  60. return ret;
  61. /*
  62. * tilcdc ->atomic_check can update ->mode_changed if pixel format
  63. * changes, hence will we check modeset changes again.
  64. */
  65. ret = drm_atomic_helper_check_modeset(dev, state);
  66. if (ret)
  67. return ret;
  68. return ret;
  69. }
  70. static const struct drm_mode_config_funcs mode_config_funcs = {
  71. .fb_create = drm_gem_fb_create,
  72. .atomic_check = tilcdc_atomic_check,
  73. .atomic_commit = drm_atomic_helper_commit,
  74. };
  75. static void modeset_init(struct drm_device *dev)
  76. {
  77. struct tilcdc_drm_private *priv = dev->dev_private;
  78. struct tilcdc_module *mod;
  79. list_for_each_entry(mod, &module_list, list) {
  80. DBG("loading module: %s", mod->name);
  81. mod->funcs->modeset_init(mod, dev);
  82. }
  83. dev->mode_config.min_width = 0;
  84. dev->mode_config.min_height = 0;
  85. dev->mode_config.max_width = priv->max_width;
  86. dev->mode_config.max_height = 2048;
  87. dev->mode_config.funcs = &mode_config_funcs;
  88. }
  89. #ifdef CONFIG_CPU_FREQ
  90. static int cpufreq_transition(struct notifier_block *nb,
  91. unsigned long val, void *data)
  92. {
  93. struct tilcdc_drm_private *priv = container_of(nb,
  94. struct tilcdc_drm_private, freq_transition);
  95. if (val == CPUFREQ_POSTCHANGE)
  96. tilcdc_crtc_update_clk(priv->crtc);
  97. return 0;
  98. }
  99. #endif
  100. static irqreturn_t tilcdc_irq(int irq, void *arg)
  101. {
  102. struct drm_device *dev = arg;
  103. struct tilcdc_drm_private *priv = dev->dev_private;
  104. return tilcdc_crtc_irq(priv->crtc);
  105. }
  106. static int tilcdc_irq_install(struct drm_device *dev, unsigned int irq)
  107. {
  108. struct tilcdc_drm_private *priv = dev->dev_private;
  109. int ret;
  110. ret = request_irq(irq, tilcdc_irq, 0, dev->driver->name, dev);
  111. if (ret)
  112. return ret;
  113. priv->irq_enabled = true;
  114. return 0;
  115. }
  116. static void tilcdc_irq_uninstall(struct drm_device *dev)
  117. {
  118. struct tilcdc_drm_private *priv = dev->dev_private;
  119. if (!priv->irq_enabled)
  120. return;
  121. free_irq(priv->irq, dev);
  122. priv->irq_enabled = false;
  123. }
  124. /*
  125. * DRM operations:
  126. */
  127. static void tilcdc_fini(struct drm_device *dev)
  128. {
  129. struct tilcdc_drm_private *priv = dev->dev_private;
  130. #ifdef CONFIG_CPU_FREQ
  131. if (priv->freq_transition.notifier_call)
  132. cpufreq_unregister_notifier(&priv->freq_transition,
  133. CPUFREQ_TRANSITION_NOTIFIER);
  134. #endif
  135. if (priv->crtc)
  136. tilcdc_crtc_shutdown(priv->crtc);
  137. drm_dev_unregister(dev);
  138. drm_kms_helper_poll_fini(dev);
  139. drm_atomic_helper_shutdown(dev);
  140. tilcdc_irq_uninstall(dev);
  141. drm_mode_config_cleanup(dev);
  142. if (priv->clk)
  143. clk_put(priv->clk);
  144. if (priv->wq)
  145. destroy_workqueue(priv->wq);
  146. dev->dev_private = NULL;
  147. pm_runtime_disable(dev->dev);
  148. drm_dev_put(dev);
  149. }
  150. static int tilcdc_init(const struct drm_driver *ddrv, struct device *dev)
  151. {
  152. struct drm_device *ddev;
  153. struct platform_device *pdev = to_platform_device(dev);
  154. struct device_node *node = dev->of_node;
  155. struct tilcdc_drm_private *priv;
  156. u32 bpp = 0;
  157. int ret;
  158. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  159. if (!priv)
  160. return -ENOMEM;
  161. ddev = drm_dev_alloc(ddrv, dev);
  162. if (IS_ERR(ddev))
  163. return PTR_ERR(ddev);
  164. ddev->dev_private = priv;
  165. platform_set_drvdata(pdev, ddev);
  166. drm_mode_config_init(ddev);
  167. priv->is_componentized =
  168. tilcdc_get_external_components(dev, NULL) > 0;
  169. priv->wq = alloc_ordered_workqueue("tilcdc", 0);
  170. if (!priv->wq) {
  171. ret = -ENOMEM;
  172. goto put_drm;
  173. }
  174. priv->mmio = devm_platform_ioremap_resource(pdev, 0);
  175. if (IS_ERR(priv->mmio)) {
  176. dev_err(dev, "failed to request / ioremap\n");
  177. ret = PTR_ERR(priv->mmio);
  178. goto free_wq;
  179. }
  180. priv->clk = clk_get(dev, "fck");
  181. if (IS_ERR(priv->clk)) {
  182. dev_err(dev, "failed to get functional clock\n");
  183. ret = -ENODEV;
  184. goto free_wq;
  185. }
  186. pm_runtime_enable(dev);
  187. /* Determine LCD IP Version */
  188. pm_runtime_get_sync(dev);
  189. switch (tilcdc_read(ddev, LCDC_PID_REG)) {
  190. case 0x4c100102:
  191. priv->rev = 1;
  192. break;
  193. case 0x4f200800:
  194. case 0x4f201000:
  195. priv->rev = 2;
  196. break;
  197. default:
  198. dev_warn(dev, "Unknown PID Reg value 0x%08x, "
  199. "defaulting to LCD revision 1\n",
  200. tilcdc_read(ddev, LCDC_PID_REG));
  201. priv->rev = 1;
  202. break;
  203. }
  204. pm_runtime_put_sync(dev);
  205. if (priv->rev == 1) {
  206. DBG("Revision 1 LCDC supports only RGB565 format");
  207. priv->pixelformats = tilcdc_rev1_formats;
  208. priv->num_pixelformats = ARRAY_SIZE(tilcdc_rev1_formats);
  209. bpp = 16;
  210. } else {
  211. const char *str = "\0";
  212. of_property_read_string(node, "blue-and-red-wiring", &str);
  213. if (0 == strcmp(str, "crossed")) {
  214. DBG("Configured for crossed blue and red wires");
  215. priv->pixelformats = tilcdc_crossed_formats;
  216. priv->num_pixelformats =
  217. ARRAY_SIZE(tilcdc_crossed_formats);
  218. bpp = 32; /* Choose bpp with RGB support for fbdef */
  219. } else if (0 == strcmp(str, "straight")) {
  220. DBG("Configured for straight blue and red wires");
  221. priv->pixelformats = tilcdc_straight_formats;
  222. priv->num_pixelformats =
  223. ARRAY_SIZE(tilcdc_straight_formats);
  224. bpp = 16; /* Choose bpp with RGB support for fbdef */
  225. } else {
  226. DBG("Blue and red wiring '%s' unknown, use legacy mode",
  227. str);
  228. priv->pixelformats = tilcdc_legacy_formats;
  229. priv->num_pixelformats =
  230. ARRAY_SIZE(tilcdc_legacy_formats);
  231. bpp = 16; /* This is just a guess */
  232. }
  233. }
  234. if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
  235. priv->max_bandwidth = TILCDC_DEFAULT_MAX_BANDWIDTH;
  236. DBG("Maximum Bandwidth Value %d", priv->max_bandwidth);
  237. if (of_property_read_u32(node, "max-width", &priv->max_width)) {
  238. if (priv->rev == 1)
  239. priv->max_width = TILCDC_DEFAULT_MAX_WIDTH_V1;
  240. else
  241. priv->max_width = TILCDC_DEFAULT_MAX_WIDTH_V2;
  242. }
  243. DBG("Maximum Horizontal Pixel Width Value %dpixels", priv->max_width);
  244. if (of_property_read_u32(node, "max-pixelclock",
  245. &priv->max_pixelclock))
  246. priv->max_pixelclock = TILCDC_DEFAULT_MAX_PIXELCLOCK;
  247. DBG("Maximum Pixel Clock Value %dKHz", priv->max_pixelclock);
  248. ret = tilcdc_crtc_create(ddev);
  249. if (ret < 0) {
  250. dev_err(dev, "failed to create crtc\n");
  251. goto disable_pm;
  252. }
  253. modeset_init(ddev);
  254. #ifdef CONFIG_CPU_FREQ
  255. priv->freq_transition.notifier_call = cpufreq_transition;
  256. ret = cpufreq_register_notifier(&priv->freq_transition,
  257. CPUFREQ_TRANSITION_NOTIFIER);
  258. if (ret) {
  259. dev_err(dev, "failed to register cpufreq notifier\n");
  260. priv->freq_transition.notifier_call = NULL;
  261. goto destroy_crtc;
  262. }
  263. #endif
  264. if (priv->is_componentized) {
  265. ret = component_bind_all(dev, ddev);
  266. if (ret < 0)
  267. goto unregister_cpufreq_notif;
  268. ret = tilcdc_add_component_encoder(ddev);
  269. if (ret < 0)
  270. goto unbind_component;
  271. } else {
  272. ret = tilcdc_attach_external_device(ddev);
  273. if (ret)
  274. goto unregister_cpufreq_notif;
  275. }
  276. if (!priv->external_connector &&
  277. ((priv->num_encoders == 0) || (priv->num_connectors == 0))) {
  278. dev_err(dev, "no encoders/connectors found\n");
  279. ret = -EPROBE_DEFER;
  280. goto unbind_component;
  281. }
  282. ret = drm_vblank_init(ddev, 1);
  283. if (ret < 0) {
  284. dev_err(dev, "failed to initialize vblank\n");
  285. goto unbind_component;
  286. }
  287. ret = platform_get_irq(pdev, 0);
  288. if (ret < 0)
  289. goto unbind_component;
  290. priv->irq = ret;
  291. ret = tilcdc_irq_install(ddev, priv->irq);
  292. if (ret < 0) {
  293. dev_err(dev, "failed to install IRQ handler\n");
  294. goto unbind_component;
  295. }
  296. drm_mode_config_reset(ddev);
  297. drm_kms_helper_poll_init(ddev);
  298. ret = drm_dev_register(ddev, 0);
  299. if (ret)
  300. goto stop_poll;
  301. drm_client_setup_with_color_mode(ddev, bpp);
  302. return 0;
  303. stop_poll:
  304. drm_kms_helper_poll_fini(ddev);
  305. tilcdc_irq_uninstall(ddev);
  306. unbind_component:
  307. if (priv->is_componentized)
  308. component_unbind_all(dev, ddev);
  309. unregister_cpufreq_notif:
  310. #ifdef CONFIG_CPU_FREQ
  311. cpufreq_unregister_notifier(&priv->freq_transition,
  312. CPUFREQ_TRANSITION_NOTIFIER);
  313. destroy_crtc:
  314. #endif
  315. tilcdc_crtc_destroy(priv->crtc);
  316. disable_pm:
  317. pm_runtime_disable(dev);
  318. clk_put(priv->clk);
  319. free_wq:
  320. destroy_workqueue(priv->wq);
  321. put_drm:
  322. platform_set_drvdata(pdev, NULL);
  323. ddev->dev_private = NULL;
  324. drm_dev_put(ddev);
  325. return ret;
  326. }
  327. #if defined(CONFIG_DEBUG_FS)
  328. static const struct {
  329. const char *name;
  330. uint8_t rev;
  331. uint8_t save;
  332. uint32_t reg;
  333. } registers[] = {
  334. #define REG(rev, save, reg) { #reg, rev, save, reg }
  335. /* exists in revision 1: */
  336. REG(1, false, LCDC_PID_REG),
  337. REG(1, true, LCDC_CTRL_REG),
  338. REG(1, false, LCDC_STAT_REG),
  339. REG(1, true, LCDC_RASTER_CTRL_REG),
  340. REG(1, true, LCDC_RASTER_TIMING_0_REG),
  341. REG(1, true, LCDC_RASTER_TIMING_1_REG),
  342. REG(1, true, LCDC_RASTER_TIMING_2_REG),
  343. REG(1, true, LCDC_DMA_CTRL_REG),
  344. REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
  345. REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
  346. REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
  347. REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
  348. /* new in revision 2: */
  349. REG(2, false, LCDC_RAW_STAT_REG),
  350. REG(2, false, LCDC_MASKED_STAT_REG),
  351. REG(2, true, LCDC_INT_ENABLE_SET_REG),
  352. REG(2, false, LCDC_INT_ENABLE_CLR_REG),
  353. REG(2, false, LCDC_END_OF_INT_IND_REG),
  354. REG(2, true, LCDC_CLK_ENABLE_REG),
  355. #undef REG
  356. };
  357. #endif
  358. #ifdef CONFIG_DEBUG_FS
  359. static int tilcdc_regs_show(struct seq_file *m, void *arg)
  360. {
  361. struct drm_info_node *node = (struct drm_info_node *) m->private;
  362. struct drm_device *dev = node->minor->dev;
  363. struct tilcdc_drm_private *priv = dev->dev_private;
  364. unsigned i;
  365. pm_runtime_get_sync(dev->dev);
  366. seq_printf(m, "revision: %d\n", priv->rev);
  367. for (i = 0; i < ARRAY_SIZE(registers); i++)
  368. if (priv->rev >= registers[i].rev)
  369. seq_printf(m, "%s:\t %08x\n", registers[i].name,
  370. tilcdc_read(dev, registers[i].reg));
  371. pm_runtime_put_sync(dev->dev);
  372. return 0;
  373. }
  374. static int tilcdc_mm_show(struct seq_file *m, void *arg)
  375. {
  376. struct drm_info_node *node = (struct drm_info_node *) m->private;
  377. struct drm_device *dev = node->minor->dev;
  378. struct drm_printer p = drm_seq_file_printer(m);
  379. drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
  380. return 0;
  381. }
  382. static struct drm_info_list tilcdc_debugfs_list[] = {
  383. { "regs", tilcdc_regs_show, 0, NULL },
  384. { "mm", tilcdc_mm_show, 0, NULL },
  385. };
  386. static void tilcdc_debugfs_init(struct drm_minor *minor)
  387. {
  388. struct tilcdc_module *mod;
  389. drm_debugfs_create_files(tilcdc_debugfs_list,
  390. ARRAY_SIZE(tilcdc_debugfs_list),
  391. minor->debugfs_root, minor);
  392. list_for_each_entry(mod, &module_list, list)
  393. if (mod->funcs->debugfs_init)
  394. mod->funcs->debugfs_init(mod, minor);
  395. }
  396. #endif
  397. DEFINE_DRM_GEM_DMA_FOPS(fops);
  398. static const struct drm_driver tilcdc_driver = {
  399. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  400. DRM_GEM_DMA_DRIVER_OPS,
  401. DRM_FBDEV_DMA_DRIVER_OPS,
  402. #ifdef CONFIG_DEBUG_FS
  403. .debugfs_init = tilcdc_debugfs_init,
  404. #endif
  405. .fops = &fops,
  406. .name = "tilcdc",
  407. .desc = "TI LCD Controller DRM",
  408. .major = 1,
  409. .minor = 0,
  410. };
  411. /*
  412. * Power management:
  413. */
  414. static int tilcdc_pm_suspend(struct device *dev)
  415. {
  416. struct drm_device *ddev = dev_get_drvdata(dev);
  417. int ret = 0;
  418. ret = drm_mode_config_helper_suspend(ddev);
  419. /* Select sleep pin state */
  420. pinctrl_pm_select_sleep_state(dev);
  421. return ret;
  422. }
  423. static int tilcdc_pm_resume(struct device *dev)
  424. {
  425. struct drm_device *ddev = dev_get_drvdata(dev);
  426. /* Select default pin state */
  427. pinctrl_pm_select_default_state(dev);
  428. return drm_mode_config_helper_resume(ddev);
  429. }
  430. static DEFINE_SIMPLE_DEV_PM_OPS(tilcdc_pm_ops,
  431. tilcdc_pm_suspend, tilcdc_pm_resume);
  432. /*
  433. * Platform driver:
  434. */
  435. static int tilcdc_bind(struct device *dev)
  436. {
  437. return tilcdc_init(&tilcdc_driver, dev);
  438. }
  439. static void tilcdc_unbind(struct device *dev)
  440. {
  441. struct drm_device *ddev = dev_get_drvdata(dev);
  442. /* Check if a subcomponent has already triggered the unloading. */
  443. if (!ddev->dev_private)
  444. return;
  445. tilcdc_fini(ddev);
  446. dev_set_drvdata(dev, NULL);
  447. }
  448. static const struct component_master_ops tilcdc_comp_ops = {
  449. .bind = tilcdc_bind,
  450. .unbind = tilcdc_unbind,
  451. };
  452. static int tilcdc_pdev_probe(struct platform_device *pdev)
  453. {
  454. struct component_match *match = NULL;
  455. int ret;
  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. ret = tilcdc_get_external_components(&pdev->dev, &match);
  462. if (ret < 0)
  463. return ret;
  464. else if (ret == 0)
  465. return tilcdc_init(&tilcdc_driver, &pdev->dev);
  466. else
  467. return component_master_add_with_match(&pdev->dev,
  468. &tilcdc_comp_ops,
  469. match);
  470. }
  471. static void tilcdc_pdev_remove(struct platform_device *pdev)
  472. {
  473. int ret;
  474. ret = tilcdc_get_external_components(&pdev->dev, NULL);
  475. if (ret < 0)
  476. dev_err(&pdev->dev, "tilcdc_get_external_components() failed (%pe)\n",
  477. ERR_PTR(ret));
  478. else if (ret == 0)
  479. tilcdc_fini(platform_get_drvdata(pdev));
  480. else
  481. component_master_del(&pdev->dev, &tilcdc_comp_ops);
  482. }
  483. static void tilcdc_pdev_shutdown(struct platform_device *pdev)
  484. {
  485. drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  486. }
  487. static const struct of_device_id tilcdc_of_match[] = {
  488. { .compatible = "ti,am33xx-tilcdc", },
  489. { .compatible = "ti,da850-tilcdc", },
  490. { },
  491. };
  492. MODULE_DEVICE_TABLE(of, tilcdc_of_match);
  493. static struct platform_driver tilcdc_platform_driver = {
  494. .probe = tilcdc_pdev_probe,
  495. .remove = tilcdc_pdev_remove,
  496. .shutdown = tilcdc_pdev_shutdown,
  497. .driver = {
  498. .name = "tilcdc",
  499. .pm = pm_sleep_ptr(&tilcdc_pm_ops),
  500. .of_match_table = tilcdc_of_match,
  501. },
  502. };
  503. static int __init tilcdc_drm_init(void)
  504. {
  505. if (drm_firmware_drivers_only())
  506. return -ENODEV;
  507. DBG("init");
  508. tilcdc_panel_init();
  509. return platform_driver_register(&tilcdc_platform_driver);
  510. }
  511. static void __exit tilcdc_drm_fini(void)
  512. {
  513. DBG("fini");
  514. platform_driver_unregister(&tilcdc_platform_driver);
  515. tilcdc_panel_fini();
  516. }
  517. module_init(tilcdc_drm_init);
  518. module_exit(tilcdc_drm_fini);
  519. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  520. MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
  521. MODULE_LICENSE("GPL");