atmel_hlcdc_dc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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,sama5d3-hlcdc",
  326. .data = &atmel_hlcdc_dc_sama5d3,
  327. },
  328. {
  329. .compatible = "atmel,sama5d4-hlcdc",
  330. .data = &atmel_hlcdc_dc_sama5d4,
  331. },
  332. { /* sentinel */ },
  333. };
  334. int atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
  335. struct drm_display_mode *mode)
  336. {
  337. int vfront_porch = mode->vsync_start - mode->vdisplay;
  338. int vback_porch = mode->vtotal - mode->vsync_end;
  339. int vsync_len = mode->vsync_end - mode->vsync_start;
  340. int hfront_porch = mode->hsync_start - mode->hdisplay;
  341. int hback_porch = mode->htotal - mode->hsync_end;
  342. int hsync_len = mode->hsync_end - mode->hsync_start;
  343. if (hsync_len > 0x40 || hsync_len < 1)
  344. return MODE_HSYNC;
  345. if (vsync_len > 0x40 || vsync_len < 1)
  346. return MODE_VSYNC;
  347. if (hfront_porch > 0x200 || hfront_porch < 1 ||
  348. hback_porch > 0x200 || hback_porch < 1 ||
  349. mode->hdisplay < 1)
  350. return MODE_H_ILLEGAL;
  351. if (vfront_porch > 0x40 || vfront_porch < 1 ||
  352. vback_porch > 0x40 || vback_porch < 0 ||
  353. mode->vdisplay < 1)
  354. return MODE_V_ILLEGAL;
  355. return MODE_OK;
  356. }
  357. static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
  358. {
  359. struct drm_device *dev = data;
  360. struct atmel_hlcdc_dc *dc = dev->dev_private;
  361. unsigned long status;
  362. unsigned int imr, isr;
  363. int i;
  364. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
  365. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
  366. status = imr & isr;
  367. if (!status)
  368. return IRQ_NONE;
  369. if (status & ATMEL_HLCDC_SOF)
  370. atmel_hlcdc_crtc_irq(dc->crtc);
  371. for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
  372. struct atmel_hlcdc_layer *layer = dc->layers[i];
  373. if (!(ATMEL_HLCDC_LAYER_STATUS(i) & status) || !layer)
  374. continue;
  375. atmel_hlcdc_layer_irq(layer);
  376. }
  377. return IRQ_HANDLED;
  378. }
  379. static struct drm_framebuffer *atmel_hlcdc_fb_create(struct drm_device *dev,
  380. struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd)
  381. {
  382. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  383. }
  384. static void atmel_hlcdc_fb_output_poll_changed(struct drm_device *dev)
  385. {
  386. struct atmel_hlcdc_dc *dc = dev->dev_private;
  387. if (dc->fbdev) {
  388. drm_fbdev_cma_hotplug_event(dc->fbdev);
  389. } else {
  390. dc->fbdev = drm_fbdev_cma_init(dev, 24,
  391. dev->mode_config.num_crtc,
  392. dev->mode_config.num_connector);
  393. if (IS_ERR(dc->fbdev))
  394. dc->fbdev = NULL;
  395. }
  396. }
  397. static const struct drm_mode_config_funcs mode_config_funcs = {
  398. .fb_create = atmel_hlcdc_fb_create,
  399. .output_poll_changed = atmel_hlcdc_fb_output_poll_changed,
  400. .atomic_check = drm_atomic_helper_check,
  401. .atomic_commit = drm_atomic_helper_commit,
  402. };
  403. static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
  404. {
  405. struct atmel_hlcdc_dc *dc = dev->dev_private;
  406. struct atmel_hlcdc_planes *planes;
  407. int ret;
  408. int i;
  409. drm_mode_config_init(dev);
  410. ret = atmel_hlcdc_create_outputs(dev);
  411. if (ret) {
  412. dev_err(dev->dev, "failed to create panel: %d\n", ret);
  413. return ret;
  414. }
  415. planes = atmel_hlcdc_create_planes(dev);
  416. if (IS_ERR(planes)) {
  417. dev_err(dev->dev, "failed to create planes\n");
  418. return PTR_ERR(planes);
  419. }
  420. dc->planes = planes;
  421. dc->layers[planes->primary->layer.desc->id] =
  422. &planes->primary->layer;
  423. if (planes->cursor)
  424. dc->layers[planes->cursor->layer.desc->id] =
  425. &planes->cursor->layer;
  426. for (i = 0; i < planes->noverlays; i++)
  427. dc->layers[planes->overlays[i]->layer.desc->id] =
  428. &planes->overlays[i]->layer;
  429. ret = atmel_hlcdc_crtc_create(dev);
  430. if (ret) {
  431. dev_err(dev->dev, "failed to create crtc\n");
  432. return ret;
  433. }
  434. dev->mode_config.min_width = dc->desc->min_width;
  435. dev->mode_config.min_height = dc->desc->min_height;
  436. dev->mode_config.max_width = dc->desc->max_width;
  437. dev->mode_config.max_height = dc->desc->max_height;
  438. dev->mode_config.funcs = &mode_config_funcs;
  439. return 0;
  440. }
  441. static int atmel_hlcdc_dc_load(struct drm_device *dev)
  442. {
  443. struct platform_device *pdev = to_platform_device(dev->dev);
  444. const struct of_device_id *match;
  445. struct atmel_hlcdc_dc *dc;
  446. int ret;
  447. match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
  448. if (!match) {
  449. dev_err(&pdev->dev, "invalid compatible string\n");
  450. return -ENODEV;
  451. }
  452. if (!match->data) {
  453. dev_err(&pdev->dev, "invalid hlcdc description\n");
  454. return -EINVAL;
  455. }
  456. dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
  457. if (!dc)
  458. return -ENOMEM;
  459. dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
  460. if (!dc->wq)
  461. return -ENOMEM;
  462. dc->desc = match->data;
  463. dc->hlcdc = dev_get_drvdata(dev->dev->parent);
  464. dev->dev_private = dc;
  465. ret = clk_prepare_enable(dc->hlcdc->periph_clk);
  466. if (ret) {
  467. dev_err(dev->dev, "failed to enable periph_clk\n");
  468. goto err_destroy_wq;
  469. }
  470. pm_runtime_enable(dev->dev);
  471. ret = drm_vblank_init(dev, 1);
  472. if (ret < 0) {
  473. dev_err(dev->dev, "failed to initialize vblank\n");
  474. goto err_periph_clk_disable;
  475. }
  476. ret = atmel_hlcdc_dc_modeset_init(dev);
  477. if (ret < 0) {
  478. dev_err(dev->dev, "failed to initialize mode setting\n");
  479. goto err_periph_clk_disable;
  480. }
  481. drm_mode_config_reset(dev);
  482. pm_runtime_get_sync(dev->dev);
  483. ret = drm_irq_install(dev, dc->hlcdc->irq);
  484. pm_runtime_put_sync(dev->dev);
  485. if (ret < 0) {
  486. dev_err(dev->dev, "failed to install IRQ handler\n");
  487. goto err_periph_clk_disable;
  488. }
  489. platform_set_drvdata(pdev, dev);
  490. drm_kms_helper_poll_init(dev);
  491. /* force connectors detection */
  492. drm_helper_hpd_irq_event(dev);
  493. return 0;
  494. err_periph_clk_disable:
  495. pm_runtime_disable(dev->dev);
  496. clk_disable_unprepare(dc->hlcdc->periph_clk);
  497. err_destroy_wq:
  498. destroy_workqueue(dc->wq);
  499. return ret;
  500. }
  501. static void atmel_hlcdc_dc_unload(struct drm_device *dev)
  502. {
  503. struct atmel_hlcdc_dc *dc = dev->dev_private;
  504. if (dc->fbdev)
  505. drm_fbdev_cma_fini(dc->fbdev);
  506. flush_workqueue(dc->wq);
  507. drm_kms_helper_poll_fini(dev);
  508. drm_mode_config_cleanup(dev);
  509. drm_vblank_cleanup(dev);
  510. pm_runtime_get_sync(dev->dev);
  511. drm_irq_uninstall(dev);
  512. pm_runtime_put_sync(dev->dev);
  513. dev->dev_private = NULL;
  514. pm_runtime_disable(dev->dev);
  515. clk_disable_unprepare(dc->hlcdc->periph_clk);
  516. destroy_workqueue(dc->wq);
  517. }
  518. static int atmel_hlcdc_dc_connector_plug_all(struct drm_device *dev)
  519. {
  520. struct drm_connector *connector, *failed;
  521. int ret;
  522. mutex_lock(&dev->mode_config.mutex);
  523. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  524. ret = drm_connector_register(connector);
  525. if (ret) {
  526. failed = connector;
  527. goto err;
  528. }
  529. }
  530. mutex_unlock(&dev->mode_config.mutex);
  531. return 0;
  532. err:
  533. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  534. if (failed == connector)
  535. break;
  536. drm_connector_unregister(connector);
  537. }
  538. mutex_unlock(&dev->mode_config.mutex);
  539. return ret;
  540. }
  541. static void atmel_hlcdc_dc_connector_unplug_all(struct drm_device *dev)
  542. {
  543. mutex_lock(&dev->mode_config.mutex);
  544. drm_connector_unplug_all(dev);
  545. mutex_unlock(&dev->mode_config.mutex);
  546. }
  547. static void atmel_hlcdc_dc_preclose(struct drm_device *dev,
  548. struct drm_file *file)
  549. {
  550. struct drm_crtc *crtc;
  551. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  552. atmel_hlcdc_crtc_cancel_page_flip(crtc, file);
  553. }
  554. static void atmel_hlcdc_dc_lastclose(struct drm_device *dev)
  555. {
  556. struct atmel_hlcdc_dc *dc = dev->dev_private;
  557. drm_fbdev_cma_restore_mode(dc->fbdev);
  558. }
  559. static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
  560. {
  561. struct atmel_hlcdc_dc *dc = dev->dev_private;
  562. unsigned int cfg = 0;
  563. int i;
  564. /* Enable interrupts on activated layers */
  565. for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
  566. if (dc->layers[i])
  567. cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
  568. }
  569. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
  570. return 0;
  571. }
  572. static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
  573. {
  574. struct atmel_hlcdc_dc *dc = dev->dev_private;
  575. unsigned int isr;
  576. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
  577. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
  578. }
  579. static int atmel_hlcdc_dc_enable_vblank(struct drm_device *dev,
  580. unsigned int pipe)
  581. {
  582. struct atmel_hlcdc_dc *dc = dev->dev_private;
  583. /* Enable SOF (Start Of Frame) interrupt for vblank counting */
  584. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, ATMEL_HLCDC_SOF);
  585. return 0;
  586. }
  587. static void atmel_hlcdc_dc_disable_vblank(struct drm_device *dev,
  588. unsigned int pipe)
  589. {
  590. struct atmel_hlcdc_dc *dc = dev->dev_private;
  591. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, ATMEL_HLCDC_SOF);
  592. }
  593. static const struct file_operations fops = {
  594. .owner = THIS_MODULE,
  595. .open = drm_open,
  596. .release = drm_release,
  597. .unlocked_ioctl = drm_ioctl,
  598. #ifdef CONFIG_COMPAT
  599. .compat_ioctl = drm_compat_ioctl,
  600. #endif
  601. .poll = drm_poll,
  602. .read = drm_read,
  603. .llseek = no_llseek,
  604. .mmap = drm_gem_cma_mmap,
  605. };
  606. static struct drm_driver atmel_hlcdc_dc_driver = {
  607. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
  608. DRIVER_MODESET | DRIVER_PRIME |
  609. DRIVER_ATOMIC,
  610. .preclose = atmel_hlcdc_dc_preclose,
  611. .lastclose = atmel_hlcdc_dc_lastclose,
  612. .irq_handler = atmel_hlcdc_dc_irq_handler,
  613. .irq_preinstall = atmel_hlcdc_dc_irq_uninstall,
  614. .irq_postinstall = atmel_hlcdc_dc_irq_postinstall,
  615. .irq_uninstall = atmel_hlcdc_dc_irq_uninstall,
  616. .get_vblank_counter = drm_vblank_no_hw_counter,
  617. .enable_vblank = atmel_hlcdc_dc_enable_vblank,
  618. .disable_vblank = atmel_hlcdc_dc_disable_vblank,
  619. .gem_free_object = drm_gem_cma_free_object,
  620. .gem_vm_ops = &drm_gem_cma_vm_ops,
  621. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  622. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  623. .gem_prime_import = drm_gem_prime_import,
  624. .gem_prime_export = drm_gem_prime_export,
  625. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  626. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  627. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  628. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  629. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  630. .dumb_create = drm_gem_cma_dumb_create,
  631. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  632. .dumb_destroy = drm_gem_dumb_destroy,
  633. .fops = &fops,
  634. .name = "atmel-hlcdc",
  635. .desc = "Atmel HLCD Controller DRM",
  636. .date = "20141504",
  637. .major = 1,
  638. .minor = 0,
  639. };
  640. static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
  641. {
  642. struct drm_device *ddev;
  643. int ret;
  644. ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
  645. if (!ddev)
  646. return -ENOMEM;
  647. ret = drm_dev_set_unique(ddev, dev_name(ddev->dev));
  648. if (ret)
  649. goto err_unref;
  650. ret = atmel_hlcdc_dc_load(ddev);
  651. if (ret)
  652. goto err_unref;
  653. ret = drm_dev_register(ddev, 0);
  654. if (ret)
  655. goto err_unload;
  656. ret = atmel_hlcdc_dc_connector_plug_all(ddev);
  657. if (ret)
  658. goto err_unregister;
  659. return 0;
  660. err_unregister:
  661. drm_dev_unregister(ddev);
  662. err_unload:
  663. atmel_hlcdc_dc_unload(ddev);
  664. err_unref:
  665. drm_dev_unref(ddev);
  666. return ret;
  667. }
  668. static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
  669. {
  670. struct drm_device *ddev = platform_get_drvdata(pdev);
  671. atmel_hlcdc_dc_connector_unplug_all(ddev);
  672. drm_dev_unregister(ddev);
  673. atmel_hlcdc_dc_unload(ddev);
  674. drm_dev_unref(ddev);
  675. return 0;
  676. }
  677. #ifdef CONFIG_PM_SLEEP
  678. static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
  679. {
  680. struct drm_device *drm_dev = dev_get_drvdata(dev);
  681. struct drm_crtc *crtc;
  682. if (pm_runtime_suspended(dev))
  683. return 0;
  684. drm_modeset_lock_all(drm_dev);
  685. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
  686. atmel_hlcdc_crtc_suspend(crtc);
  687. drm_modeset_unlock_all(drm_dev);
  688. return 0;
  689. }
  690. static int atmel_hlcdc_dc_drm_resume(struct device *dev)
  691. {
  692. struct drm_device *drm_dev = dev_get_drvdata(dev);
  693. struct drm_crtc *crtc;
  694. if (pm_runtime_suspended(dev))
  695. return 0;
  696. drm_modeset_lock_all(drm_dev);
  697. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
  698. atmel_hlcdc_crtc_resume(crtc);
  699. drm_modeset_unlock_all(drm_dev);
  700. return 0;
  701. }
  702. #endif
  703. static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
  704. atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
  705. static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
  706. { .compatible = "atmel,hlcdc-display-controller" },
  707. { },
  708. };
  709. static struct platform_driver atmel_hlcdc_dc_platform_driver = {
  710. .probe = atmel_hlcdc_dc_drm_probe,
  711. .remove = atmel_hlcdc_dc_drm_remove,
  712. .driver = {
  713. .name = "atmel-hlcdc-display-controller",
  714. .pm = &atmel_hlcdc_dc_drm_pm_ops,
  715. .of_match_table = atmel_hlcdc_dc_of_match,
  716. },
  717. };
  718. module_platform_driver(atmel_hlcdc_dc_platform_driver);
  719. MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
  720. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  721. MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
  722. MODULE_LICENSE("GPL");
  723. MODULE_ALIAS("platform:atmel-hlcdc-dc");