atmel_hlcdc_dc.c 21 KB

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