atmel_hlcdc_dc.c 20 KB

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