atmel_hlcdc_dc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * Copyright (C) 2014 Traphandler
  3. * Copyright (C) 2014 Free Electrons
  4. * Copyright (C) 2014 Atmel
  5. *
  6. * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  7. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/irq.h>
  23. #include <linux/irqchip.h>
  24. #include <linux/module.h>
  25. #include <linux/pm_runtime.h>
  26. #include "atmel_hlcdc_dc.h"
  27. #define ATMEL_HLCDC_LAYER_IRQS_OFFSET 8
  28. static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9n12_layers[] = {
  29. {
  30. .name = "base",
  31. .formats = &atmel_hlcdc_plane_rgb_formats,
  32. .regs_offset = 0x40,
  33. .id = 0,
  34. .type = ATMEL_HLCDC_BASE_LAYER,
  35. .nconfigs = 5,
  36. .layout = {
  37. .xstride = { 2 },
  38. .default_color = 3,
  39. .general_config = 4,
  40. },
  41. },
  42. };
  43. static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9n12 = {
  44. .min_width = 0,
  45. .min_height = 0,
  46. .max_width = 1280,
  47. .max_height = 860,
  48. .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9n12_layers),
  49. .layers = atmel_hlcdc_at91sam9n12_layers,
  50. };
  51. static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
  52. {
  53. .name = "base",
  54. .formats = &atmel_hlcdc_plane_rgb_formats,
  55. .regs_offset = 0x40,
  56. .id = 0,
  57. .type = ATMEL_HLCDC_BASE_LAYER,
  58. .nconfigs = 5,
  59. .layout = {
  60. .xstride = { 2 },
  61. .default_color = 3,
  62. .general_config = 4,
  63. .disc_pos = 5,
  64. .disc_size = 6,
  65. },
  66. },
  67. {
  68. .name = "overlay1",
  69. .formats = &atmel_hlcdc_plane_rgb_formats,
  70. .regs_offset = 0x100,
  71. .id = 1,
  72. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  73. .nconfigs = 10,
  74. .layout = {
  75. .pos = 2,
  76. .size = 3,
  77. .xstride = { 4 },
  78. .pstride = { 5 },
  79. .default_color = 6,
  80. .chroma_key = 7,
  81. .chroma_key_mask = 8,
  82. .general_config = 9,
  83. },
  84. },
  85. {
  86. .name = "high-end-overlay",
  87. .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
  88. .regs_offset = 0x280,
  89. .id = 2,
  90. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  91. .nconfigs = 17,
  92. .layout = {
  93. .pos = 2,
  94. .size = 3,
  95. .memsize = 4,
  96. .xstride = { 5, 7 },
  97. .pstride = { 6, 8 },
  98. .default_color = 9,
  99. .chroma_key = 10,
  100. .chroma_key_mask = 11,
  101. .general_config = 12,
  102. .csc = 14,
  103. },
  104. },
  105. {
  106. .name = "cursor",
  107. .formats = &atmel_hlcdc_plane_rgb_formats,
  108. .regs_offset = 0x340,
  109. .id = 3,
  110. .type = ATMEL_HLCDC_CURSOR_LAYER,
  111. .nconfigs = 10,
  112. .max_width = 128,
  113. .max_height = 128,
  114. .layout = {
  115. .pos = 2,
  116. .size = 3,
  117. .xstride = { 4 },
  118. .default_color = 6,
  119. .chroma_key = 7,
  120. .chroma_key_mask = 8,
  121. .general_config = 9,
  122. },
  123. },
  124. };
  125. static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9x5 = {
  126. .min_width = 0,
  127. .min_height = 0,
  128. .max_width = 800,
  129. .max_height = 600,
  130. .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9x5_layers),
  131. .layers = atmel_hlcdc_at91sam9x5_layers,
  132. };
  133. static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
  134. {
  135. .name = "base",
  136. .formats = &atmel_hlcdc_plane_rgb_formats,
  137. .regs_offset = 0x40,
  138. .id = 0,
  139. .type = ATMEL_HLCDC_BASE_LAYER,
  140. .nconfigs = 7,
  141. .layout = {
  142. .xstride = { 2 },
  143. .default_color = 3,
  144. .general_config = 4,
  145. .disc_pos = 5,
  146. .disc_size = 6,
  147. },
  148. },
  149. {
  150. .name = "overlay1",
  151. .formats = &atmel_hlcdc_plane_rgb_formats,
  152. .regs_offset = 0x140,
  153. .id = 1,
  154. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  155. .nconfigs = 10,
  156. .layout = {
  157. .pos = 2,
  158. .size = 3,
  159. .xstride = { 4 },
  160. .pstride = { 5 },
  161. .default_color = 6,
  162. .chroma_key = 7,
  163. .chroma_key_mask = 8,
  164. .general_config = 9,
  165. },
  166. },
  167. {
  168. .name = "overlay2",
  169. .formats = &atmel_hlcdc_plane_rgb_formats,
  170. .regs_offset = 0x240,
  171. .id = 2,
  172. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  173. .nconfigs = 10,
  174. .layout = {
  175. .pos = 2,
  176. .size = 3,
  177. .xstride = { 4 },
  178. .pstride = { 5 },
  179. .default_color = 6,
  180. .chroma_key = 7,
  181. .chroma_key_mask = 8,
  182. .general_config = 9,
  183. },
  184. },
  185. {
  186. .name = "high-end-overlay",
  187. .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
  188. .regs_offset = 0x340,
  189. .id = 3,
  190. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  191. .nconfigs = 42,
  192. .layout = {
  193. .pos = 2,
  194. .size = 3,
  195. .memsize = 4,
  196. .xstride = { 5, 7 },
  197. .pstride = { 6, 8 },
  198. .default_color = 9,
  199. .chroma_key = 10,
  200. .chroma_key_mask = 11,
  201. .general_config = 12,
  202. .csc = 14,
  203. },
  204. },
  205. {
  206. .name = "cursor",
  207. .formats = &atmel_hlcdc_plane_rgb_formats,
  208. .regs_offset = 0x440,
  209. .id = 4,
  210. .type = ATMEL_HLCDC_CURSOR_LAYER,
  211. .nconfigs = 10,
  212. .max_width = 128,
  213. .max_height = 128,
  214. .layout = {
  215. .pos = 2,
  216. .size = 3,
  217. .xstride = { 4 },
  218. .pstride = { 5 },
  219. .default_color = 6,
  220. .chroma_key = 7,
  221. .chroma_key_mask = 8,
  222. .general_config = 9,
  223. },
  224. },
  225. };
  226. static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d3 = {
  227. .min_width = 0,
  228. .min_height = 0,
  229. .max_width = 2048,
  230. .max_height = 2048,
  231. .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d3_layers),
  232. .layers = atmel_hlcdc_sama5d3_layers,
  233. };
  234. static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers[] = {
  235. {
  236. .name = "base",
  237. .formats = &atmel_hlcdc_plane_rgb_formats,
  238. .regs_offset = 0x40,
  239. .id = 0,
  240. .type = ATMEL_HLCDC_BASE_LAYER,
  241. .nconfigs = 7,
  242. .layout = {
  243. .xstride = { 2 },
  244. .default_color = 3,
  245. .general_config = 4,
  246. .disc_pos = 5,
  247. .disc_size = 6,
  248. },
  249. },
  250. {
  251. .name = "overlay1",
  252. .formats = &atmel_hlcdc_plane_rgb_formats,
  253. .regs_offset = 0x140,
  254. .id = 1,
  255. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  256. .nconfigs = 10,
  257. .layout = {
  258. .pos = 2,
  259. .size = 3,
  260. .xstride = { 4 },
  261. .pstride = { 5 },
  262. .default_color = 6,
  263. .chroma_key = 7,
  264. .chroma_key_mask = 8,
  265. .general_config = 9,
  266. },
  267. },
  268. {
  269. .name = "overlay2",
  270. .formats = &atmel_hlcdc_plane_rgb_formats,
  271. .regs_offset = 0x240,
  272. .id = 2,
  273. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  274. .nconfigs = 10,
  275. .layout = {
  276. .pos = 2,
  277. .size = 3,
  278. .xstride = { 4 },
  279. .pstride = { 5 },
  280. .default_color = 6,
  281. .chroma_key = 7,
  282. .chroma_key_mask = 8,
  283. .general_config = 9,
  284. },
  285. },
  286. {
  287. .name = "high-end-overlay",
  288. .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
  289. .regs_offset = 0x340,
  290. .id = 3,
  291. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  292. .nconfigs = 42,
  293. .layout = {
  294. .pos = 2,
  295. .size = 3,
  296. .memsize = 4,
  297. .xstride = { 5, 7 },
  298. .pstride = { 6, 8 },
  299. .default_color = 9,
  300. .chroma_key = 10,
  301. .chroma_key_mask = 11,
  302. .general_config = 12,
  303. .csc = 14,
  304. },
  305. },
  306. };
  307. static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d4 = {
  308. .min_width = 0,
  309. .min_height = 0,
  310. .max_width = 2048,
  311. .max_height = 2048,
  312. .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d4_layers),
  313. .layers = atmel_hlcdc_sama5d4_layers,
  314. };
  315. static const struct of_device_id atmel_hlcdc_of_match[] = {
  316. {
  317. .compatible = "atmel,at91sam9n12-hlcdc",
  318. .data = &atmel_hlcdc_dc_at91sam9n12,
  319. },
  320. {
  321. .compatible = "atmel,at91sam9x5-hlcdc",
  322. .data = &atmel_hlcdc_dc_at91sam9x5,
  323. },
  324. {
  325. .compatible = "atmel,sama5d2-hlcdc",
  326. .data = &atmel_hlcdc_dc_sama5d4,
  327. },
  328. {
  329. .compatible = "atmel,sama5d3-hlcdc",
  330. .data = &atmel_hlcdc_dc_sama5d3,
  331. },
  332. {
  333. .compatible = "atmel,sama5d4-hlcdc",
  334. .data = &atmel_hlcdc_dc_sama5d4,
  335. },
  336. { /* sentinel */ },
  337. };
  338. MODULE_DEVICE_TABLE(of, atmel_hlcdc_of_match);
  339. int atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
  340. struct drm_display_mode *mode)
  341. {
  342. int vfront_porch = mode->vsync_start - mode->vdisplay;
  343. int vback_porch = mode->vtotal - mode->vsync_end;
  344. int vsync_len = mode->vsync_end - mode->vsync_start;
  345. int hfront_porch = mode->hsync_start - mode->hdisplay;
  346. int hback_porch = mode->htotal - mode->hsync_end;
  347. int hsync_len = mode->hsync_end - mode->hsync_start;
  348. if (hsync_len > 0x40 || hsync_len < 1)
  349. return MODE_HSYNC;
  350. if (vsync_len > 0x40 || vsync_len < 1)
  351. return MODE_VSYNC;
  352. if (hfront_porch > 0x200 || hfront_porch < 1 ||
  353. hback_porch > 0x200 || hback_porch < 1 ||
  354. mode->hdisplay < 1)
  355. return MODE_H_ILLEGAL;
  356. if (vfront_porch > 0x40 || vfront_porch < 1 ||
  357. vback_porch > 0x40 || vback_porch < 0 ||
  358. mode->vdisplay < 1)
  359. return MODE_V_ILLEGAL;
  360. return MODE_OK;
  361. }
  362. static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
  363. {
  364. struct drm_device *dev = data;
  365. struct atmel_hlcdc_dc *dc = dev->dev_private;
  366. unsigned long status;
  367. unsigned int imr, isr;
  368. int i;
  369. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
  370. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
  371. status = imr & isr;
  372. if (!status)
  373. return IRQ_NONE;
  374. if (status & ATMEL_HLCDC_SOF)
  375. atmel_hlcdc_crtc_irq(dc->crtc);
  376. for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
  377. struct atmel_hlcdc_layer *layer = dc->layers[i];
  378. if (!(ATMEL_HLCDC_LAYER_STATUS(i) & status) || !layer)
  379. continue;
  380. atmel_hlcdc_layer_irq(layer);
  381. }
  382. return IRQ_HANDLED;
  383. }
  384. static struct drm_framebuffer *atmel_hlcdc_fb_create(struct drm_device *dev,
  385. struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
  386. {
  387. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  388. }
  389. static void atmel_hlcdc_fb_output_poll_changed(struct drm_device *dev)
  390. {
  391. struct atmel_hlcdc_dc *dc = dev->dev_private;
  392. if (dc->fbdev) {
  393. drm_fbdev_cma_hotplug_event(dc->fbdev);
  394. } else {
  395. dc->fbdev = drm_fbdev_cma_init(dev, 24,
  396. dev->mode_config.num_crtc,
  397. dev->mode_config.num_connector);
  398. if (IS_ERR(dc->fbdev))
  399. dc->fbdev = NULL;
  400. }
  401. }
  402. static const struct drm_mode_config_funcs mode_config_funcs = {
  403. .fb_create = atmel_hlcdc_fb_create,
  404. .output_poll_changed = atmel_hlcdc_fb_output_poll_changed,
  405. .atomic_check = drm_atomic_helper_check,
  406. .atomic_commit = drm_atomic_helper_commit,
  407. };
  408. static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
  409. {
  410. struct atmel_hlcdc_dc *dc = dev->dev_private;
  411. struct atmel_hlcdc_planes *planes;
  412. int ret;
  413. int i;
  414. drm_mode_config_init(dev);
  415. ret = atmel_hlcdc_create_outputs(dev);
  416. if (ret) {
  417. dev_err(dev->dev, "failed to create panel: %d\n", ret);
  418. return ret;
  419. }
  420. planes = atmel_hlcdc_create_planes(dev);
  421. if (IS_ERR(planes)) {
  422. dev_err(dev->dev, "failed to create planes\n");
  423. return PTR_ERR(planes);
  424. }
  425. dc->planes = planes;
  426. dc->layers[planes->primary->layer.desc->id] =
  427. &planes->primary->layer;
  428. if (planes->cursor)
  429. dc->layers[planes->cursor->layer.desc->id] =
  430. &planes->cursor->layer;
  431. for (i = 0; i < planes->noverlays; i++)
  432. dc->layers[planes->overlays[i]->layer.desc->id] =
  433. &planes->overlays[i]->layer;
  434. ret = atmel_hlcdc_crtc_create(dev);
  435. if (ret) {
  436. dev_err(dev->dev, "failed to create crtc\n");
  437. return ret;
  438. }
  439. dev->mode_config.min_width = dc->desc->min_width;
  440. dev->mode_config.min_height = dc->desc->min_height;
  441. dev->mode_config.max_width = dc->desc->max_width;
  442. dev->mode_config.max_height = dc->desc->max_height;
  443. dev->mode_config.funcs = &mode_config_funcs;
  444. return 0;
  445. }
  446. static int atmel_hlcdc_dc_load(struct drm_device *dev)
  447. {
  448. struct platform_device *pdev = to_platform_device(dev->dev);
  449. const struct of_device_id *match;
  450. struct atmel_hlcdc_dc *dc;
  451. int ret;
  452. match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
  453. if (!match) {
  454. dev_err(&pdev->dev, "invalid compatible string\n");
  455. return -ENODEV;
  456. }
  457. if (!match->data) {
  458. dev_err(&pdev->dev, "invalid hlcdc description\n");
  459. return -EINVAL;
  460. }
  461. dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
  462. if (!dc)
  463. return -ENOMEM;
  464. dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
  465. if (!dc->wq)
  466. return -ENOMEM;
  467. dc->desc = match->data;
  468. dc->hlcdc = dev_get_drvdata(dev->dev->parent);
  469. dev->dev_private = dc;
  470. ret = clk_prepare_enable(dc->hlcdc->periph_clk);
  471. if (ret) {
  472. dev_err(dev->dev, "failed to enable periph_clk\n");
  473. goto err_destroy_wq;
  474. }
  475. pm_runtime_enable(dev->dev);
  476. ret = drm_vblank_init(dev, 1);
  477. if (ret < 0) {
  478. dev_err(dev->dev, "failed to initialize vblank\n");
  479. goto err_periph_clk_disable;
  480. }
  481. ret = atmel_hlcdc_dc_modeset_init(dev);
  482. if (ret < 0) {
  483. dev_err(dev->dev, "failed to initialize mode setting\n");
  484. goto err_periph_clk_disable;
  485. }
  486. drm_mode_config_reset(dev);
  487. pm_runtime_get_sync(dev->dev);
  488. ret = drm_irq_install(dev, dc->hlcdc->irq);
  489. pm_runtime_put_sync(dev->dev);
  490. if (ret < 0) {
  491. dev_err(dev->dev, "failed to install IRQ handler\n");
  492. goto err_periph_clk_disable;
  493. }
  494. platform_set_drvdata(pdev, dev);
  495. drm_kms_helper_poll_init(dev);
  496. /* force connectors detection */
  497. drm_helper_hpd_irq_event(dev);
  498. return 0;
  499. err_periph_clk_disable:
  500. pm_runtime_disable(dev->dev);
  501. clk_disable_unprepare(dc->hlcdc->periph_clk);
  502. err_destroy_wq:
  503. destroy_workqueue(dc->wq);
  504. return ret;
  505. }
  506. static void atmel_hlcdc_dc_unload(struct drm_device *dev)
  507. {
  508. struct atmel_hlcdc_dc *dc = dev->dev_private;
  509. if (dc->fbdev)
  510. drm_fbdev_cma_fini(dc->fbdev);
  511. flush_workqueue(dc->wq);
  512. drm_kms_helper_poll_fini(dev);
  513. drm_mode_config_cleanup(dev);
  514. drm_vblank_cleanup(dev);
  515. pm_runtime_get_sync(dev->dev);
  516. drm_irq_uninstall(dev);
  517. pm_runtime_put_sync(dev->dev);
  518. dev->dev_private = NULL;
  519. pm_runtime_disable(dev->dev);
  520. clk_disable_unprepare(dc->hlcdc->periph_clk);
  521. destroy_workqueue(dc->wq);
  522. }
  523. static int atmel_hlcdc_dc_connector_plug_all(struct drm_device *dev)
  524. {
  525. struct drm_connector *connector, *failed;
  526. int ret;
  527. mutex_lock(&dev->mode_config.mutex);
  528. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  529. ret = drm_connector_register(connector);
  530. if (ret) {
  531. failed = connector;
  532. goto err;
  533. }
  534. }
  535. mutex_unlock(&dev->mode_config.mutex);
  536. return 0;
  537. err:
  538. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  539. if (failed == connector)
  540. break;
  541. drm_connector_unregister(connector);
  542. }
  543. mutex_unlock(&dev->mode_config.mutex);
  544. return ret;
  545. }
  546. static void atmel_hlcdc_dc_connector_unplug_all(struct drm_device *dev)
  547. {
  548. mutex_lock(&dev->mode_config.mutex);
  549. drm_connector_unplug_all(dev);
  550. mutex_unlock(&dev->mode_config.mutex);
  551. }
  552. static void atmel_hlcdc_dc_lastclose(struct drm_device *dev)
  553. {
  554. struct atmel_hlcdc_dc *dc = dev->dev_private;
  555. drm_fbdev_cma_restore_mode(dc->fbdev);
  556. }
  557. static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
  558. {
  559. struct atmel_hlcdc_dc *dc = dev->dev_private;
  560. unsigned int cfg = 0;
  561. int i;
  562. /* Enable interrupts on activated layers */
  563. for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
  564. if (dc->layers[i])
  565. cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
  566. }
  567. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
  568. return 0;
  569. }
  570. static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
  571. {
  572. struct atmel_hlcdc_dc *dc = dev->dev_private;
  573. unsigned int isr;
  574. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
  575. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
  576. }
  577. static int atmel_hlcdc_dc_enable_vblank(struct drm_device *dev,
  578. unsigned int pipe)
  579. {
  580. struct atmel_hlcdc_dc *dc = dev->dev_private;
  581. /* Enable SOF (Start Of Frame) interrupt for vblank counting */
  582. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, ATMEL_HLCDC_SOF);
  583. return 0;
  584. }
  585. static void atmel_hlcdc_dc_disable_vblank(struct drm_device *dev,
  586. unsigned int pipe)
  587. {
  588. struct atmel_hlcdc_dc *dc = dev->dev_private;
  589. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, ATMEL_HLCDC_SOF);
  590. }
  591. static const struct file_operations fops = {
  592. .owner = THIS_MODULE,
  593. .open = drm_open,
  594. .release = drm_release,
  595. .unlocked_ioctl = drm_ioctl,
  596. #ifdef CONFIG_COMPAT
  597. .compat_ioctl = drm_compat_ioctl,
  598. #endif
  599. .poll = drm_poll,
  600. .read = drm_read,
  601. .llseek = no_llseek,
  602. .mmap = drm_gem_cma_mmap,
  603. };
  604. static struct drm_driver atmel_hlcdc_dc_driver = {
  605. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
  606. DRIVER_MODESET | DRIVER_PRIME |
  607. DRIVER_ATOMIC,
  608. .lastclose = atmel_hlcdc_dc_lastclose,
  609. .irq_handler = atmel_hlcdc_dc_irq_handler,
  610. .irq_preinstall = atmel_hlcdc_dc_irq_uninstall,
  611. .irq_postinstall = atmel_hlcdc_dc_irq_postinstall,
  612. .irq_uninstall = atmel_hlcdc_dc_irq_uninstall,
  613. .get_vblank_counter = drm_vblank_no_hw_counter,
  614. .enable_vblank = atmel_hlcdc_dc_enable_vblank,
  615. .disable_vblank = atmel_hlcdc_dc_disable_vblank,
  616. .gem_free_object = drm_gem_cma_free_object,
  617. .gem_vm_ops = &drm_gem_cma_vm_ops,
  618. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  619. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  620. .gem_prime_import = drm_gem_prime_import,
  621. .gem_prime_export = drm_gem_prime_export,
  622. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  623. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  624. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  625. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  626. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  627. .dumb_create = drm_gem_cma_dumb_create,
  628. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  629. .dumb_destroy = drm_gem_dumb_destroy,
  630. .fops = &fops,
  631. .name = "atmel-hlcdc",
  632. .desc = "Atmel HLCD Controller DRM",
  633. .date = "20141504",
  634. .major = 1,
  635. .minor = 0,
  636. };
  637. static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
  638. {
  639. struct drm_device *ddev;
  640. int ret;
  641. ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
  642. if (!ddev)
  643. return -ENOMEM;
  644. ret = atmel_hlcdc_dc_load(ddev);
  645. if (ret)
  646. goto err_unref;
  647. ret = drm_dev_register(ddev, 0);
  648. if (ret)
  649. goto err_unload;
  650. ret = atmel_hlcdc_dc_connector_plug_all(ddev);
  651. if (ret)
  652. goto err_unregister;
  653. return 0;
  654. err_unregister:
  655. drm_dev_unregister(ddev);
  656. err_unload:
  657. atmel_hlcdc_dc_unload(ddev);
  658. err_unref:
  659. drm_dev_unref(ddev);
  660. return ret;
  661. }
  662. static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
  663. {
  664. struct drm_device *ddev = platform_get_drvdata(pdev);
  665. atmel_hlcdc_dc_connector_unplug_all(ddev);
  666. drm_dev_unregister(ddev);
  667. atmel_hlcdc_dc_unload(ddev);
  668. drm_dev_unref(ddev);
  669. return 0;
  670. }
  671. #ifdef CONFIG_PM_SLEEP
  672. static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
  673. {
  674. struct drm_device *drm_dev = dev_get_drvdata(dev);
  675. struct drm_crtc *crtc;
  676. if (pm_runtime_suspended(dev))
  677. return 0;
  678. drm_modeset_lock_all(drm_dev);
  679. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
  680. atmel_hlcdc_crtc_suspend(crtc);
  681. drm_modeset_unlock_all(drm_dev);
  682. return 0;
  683. }
  684. static int atmel_hlcdc_dc_drm_resume(struct device *dev)
  685. {
  686. struct drm_device *drm_dev = dev_get_drvdata(dev);
  687. struct drm_crtc *crtc;
  688. if (pm_runtime_suspended(dev))
  689. return 0;
  690. drm_modeset_lock_all(drm_dev);
  691. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
  692. atmel_hlcdc_crtc_resume(crtc);
  693. drm_modeset_unlock_all(drm_dev);
  694. return 0;
  695. }
  696. #endif
  697. static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
  698. atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
  699. static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
  700. { .compatible = "atmel,hlcdc-display-controller" },
  701. { },
  702. };
  703. static struct platform_driver atmel_hlcdc_dc_platform_driver = {
  704. .probe = atmel_hlcdc_dc_drm_probe,
  705. .remove = atmel_hlcdc_dc_drm_remove,
  706. .driver = {
  707. .name = "atmel-hlcdc-display-controller",
  708. .pm = &atmel_hlcdc_dc_drm_pm_ops,
  709. .of_match_table = atmel_hlcdc_dc_of_match,
  710. },
  711. };
  712. module_platform_driver(atmel_hlcdc_dc_platform_driver);
  713. MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
  714. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  715. MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
  716. MODULE_LICENSE("GPL");
  717. MODULE_ALIAS("platform:atmel-hlcdc-dc");