atmel_hlcdc_dc.c 20 KB

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