atmel_hlcdc_dc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 Traphandler
  4. * Copyright (C) 2014 Free Electrons
  5. * Copyright (C) 2014 Atmel
  6. *
  7. * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  8. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/irq.h>
  12. #include <linux/irqchip.h>
  13. #include <linux/mfd/atmel-hlcdc.h>
  14. #include <linux/module.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/platform_device.h>
  17. #include <drm/drm_atomic.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_drv.h>
  20. #include <drm/drm_fbdev_dma.h>
  21. #include <drm/drm_gem_dma_helper.h>
  22. #include <drm/drm_gem_framebuffer_helper.h>
  23. #include <drm/drm_module.h>
  24. #include <drm/drm_probe_helper.h>
  25. #include <drm/drm_vblank.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. .ops = &atmel_hlcdc_ops,
  56. };
  57. static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
  58. {
  59. .name = "base",
  60. .formats = &atmel_hlcdc_plane_rgb_formats,
  61. .regs_offset = 0x40,
  62. .id = 0,
  63. .type = ATMEL_HLCDC_BASE_LAYER,
  64. .cfgs_offset = 0x2c,
  65. .layout = {
  66. .xstride = { 2 },
  67. .default_color = 3,
  68. .general_config = 4,
  69. .disc_pos = 5,
  70. .disc_size = 6,
  71. },
  72. .clut_offset = 0x400,
  73. },
  74. {
  75. .name = "overlay1",
  76. .formats = &atmel_hlcdc_plane_rgb_formats,
  77. .regs_offset = 0x100,
  78. .id = 1,
  79. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  80. .cfgs_offset = 0x2c,
  81. .layout = {
  82. .pos = 2,
  83. .size = 3,
  84. .xstride = { 4 },
  85. .pstride = { 5 },
  86. .default_color = 6,
  87. .chroma_key = 7,
  88. .chroma_key_mask = 8,
  89. .general_config = 9,
  90. },
  91. .clut_offset = 0x800,
  92. },
  93. {
  94. .name = "high-end-overlay",
  95. .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
  96. .regs_offset = 0x280,
  97. .id = 2,
  98. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  99. .cfgs_offset = 0x4c,
  100. .layout = {
  101. .pos = 2,
  102. .size = 3,
  103. .memsize = 4,
  104. .xstride = { 5, 7 },
  105. .pstride = { 6, 8 },
  106. .default_color = 9,
  107. .chroma_key = 10,
  108. .chroma_key_mask = 11,
  109. .general_config = 12,
  110. .scaler_config = 13,
  111. .csc = 14,
  112. },
  113. .clut_offset = 0x1000,
  114. },
  115. {
  116. .name = "cursor",
  117. .formats = &atmel_hlcdc_plane_rgb_formats,
  118. .regs_offset = 0x340,
  119. .id = 3,
  120. .type = ATMEL_HLCDC_CURSOR_LAYER,
  121. .max_width = 128,
  122. .max_height = 128,
  123. .cfgs_offset = 0x2c,
  124. .layout = {
  125. .pos = 2,
  126. .size = 3,
  127. .xstride = { 4 },
  128. .default_color = 6,
  129. .chroma_key = 7,
  130. .chroma_key_mask = 8,
  131. .general_config = 9,
  132. },
  133. .clut_offset = 0x1400,
  134. },
  135. };
  136. static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9x5 = {
  137. .min_width = 0,
  138. .min_height = 0,
  139. .max_width = 800,
  140. .max_height = 600,
  141. .max_spw = 0x3f,
  142. .max_vpw = 0x3f,
  143. .max_hpw = 0xff,
  144. .conflicting_output_formats = true,
  145. .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9x5_layers),
  146. .layers = atmel_hlcdc_at91sam9x5_layers,
  147. .ops = &atmel_hlcdc_ops,
  148. };
  149. static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
  150. {
  151. .name = "base",
  152. .formats = &atmel_hlcdc_plane_rgb_formats,
  153. .regs_offset = 0x40,
  154. .id = 0,
  155. .type = ATMEL_HLCDC_BASE_LAYER,
  156. .cfgs_offset = 0x2c,
  157. .layout = {
  158. .xstride = { 2 },
  159. .default_color = 3,
  160. .general_config = 4,
  161. .disc_pos = 5,
  162. .disc_size = 6,
  163. },
  164. .clut_offset = 0x600,
  165. },
  166. {
  167. .name = "overlay1",
  168. .formats = &atmel_hlcdc_plane_rgb_formats,
  169. .regs_offset = 0x140,
  170. .id = 1,
  171. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  172. .cfgs_offset = 0x2c,
  173. .layout = {
  174. .pos = 2,
  175. .size = 3,
  176. .xstride = { 4 },
  177. .pstride = { 5 },
  178. .default_color = 6,
  179. .chroma_key = 7,
  180. .chroma_key_mask = 8,
  181. .general_config = 9,
  182. },
  183. .clut_offset = 0xa00,
  184. },
  185. {
  186. .name = "overlay2",
  187. .formats = &atmel_hlcdc_plane_rgb_formats,
  188. .regs_offset = 0x240,
  189. .id = 2,
  190. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  191. .cfgs_offset = 0x2c,
  192. .layout = {
  193. .pos = 2,
  194. .size = 3,
  195. .xstride = { 4 },
  196. .pstride = { 5 },
  197. .default_color = 6,
  198. .chroma_key = 7,
  199. .chroma_key_mask = 8,
  200. .general_config = 9,
  201. },
  202. .clut_offset = 0xe00,
  203. },
  204. {
  205. .name = "high-end-overlay",
  206. .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
  207. .regs_offset = 0x340,
  208. .id = 3,
  209. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  210. .cfgs_offset = 0x4c,
  211. .layout = {
  212. .pos = 2,
  213. .size = 3,
  214. .memsize = 4,
  215. .xstride = { 5, 7 },
  216. .pstride = { 6, 8 },
  217. .default_color = 9,
  218. .chroma_key = 10,
  219. .chroma_key_mask = 11,
  220. .general_config = 12,
  221. .scaler_config = 13,
  222. .phicoeffs = {
  223. .x = 17,
  224. .y = 33,
  225. },
  226. .csc = 14,
  227. },
  228. .clut_offset = 0x1200,
  229. },
  230. {
  231. .name = "cursor",
  232. .formats = &atmel_hlcdc_plane_rgb_formats,
  233. .regs_offset = 0x440,
  234. .id = 4,
  235. .type = ATMEL_HLCDC_CURSOR_LAYER,
  236. .max_width = 128,
  237. .max_height = 128,
  238. .cfgs_offset = 0x2c,
  239. .layout = {
  240. .pos = 2,
  241. .size = 3,
  242. .xstride = { 4 },
  243. .pstride = { 5 },
  244. .default_color = 6,
  245. .chroma_key = 7,
  246. .chroma_key_mask = 8,
  247. .general_config = 9,
  248. .scaler_config = 13,
  249. },
  250. .clut_offset = 0x1600,
  251. },
  252. };
  253. static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d3 = {
  254. .min_width = 0,
  255. .min_height = 0,
  256. .max_width = 2048,
  257. .max_height = 2048,
  258. .max_spw = 0x3f,
  259. .max_vpw = 0x3f,
  260. .max_hpw = 0x1ff,
  261. .conflicting_output_formats = true,
  262. .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d3_layers),
  263. .layers = atmel_hlcdc_sama5d3_layers,
  264. .ops = &atmel_hlcdc_ops,
  265. };
  266. static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers[] = {
  267. {
  268. .name = "base",
  269. .formats = &atmel_hlcdc_plane_rgb_formats,
  270. .regs_offset = 0x40,
  271. .id = 0,
  272. .type = ATMEL_HLCDC_BASE_LAYER,
  273. .cfgs_offset = 0x2c,
  274. .layout = {
  275. .xstride = { 2 },
  276. .default_color = 3,
  277. .general_config = 4,
  278. .disc_pos = 5,
  279. .disc_size = 6,
  280. },
  281. .clut_offset = 0x600,
  282. },
  283. {
  284. .name = "overlay1",
  285. .formats = &atmel_hlcdc_plane_rgb_formats,
  286. .regs_offset = 0x140,
  287. .id = 1,
  288. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  289. .cfgs_offset = 0x2c,
  290. .layout = {
  291. .pos = 2,
  292. .size = 3,
  293. .xstride = { 4 },
  294. .pstride = { 5 },
  295. .default_color = 6,
  296. .chroma_key = 7,
  297. .chroma_key_mask = 8,
  298. .general_config = 9,
  299. },
  300. .clut_offset = 0xa00,
  301. },
  302. {
  303. .name = "overlay2",
  304. .formats = &atmel_hlcdc_plane_rgb_formats,
  305. .regs_offset = 0x240,
  306. .id = 2,
  307. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  308. .cfgs_offset = 0x2c,
  309. .layout = {
  310. .pos = 2,
  311. .size = 3,
  312. .xstride = { 4 },
  313. .pstride = { 5 },
  314. .default_color = 6,
  315. .chroma_key = 7,
  316. .chroma_key_mask = 8,
  317. .general_config = 9,
  318. },
  319. .clut_offset = 0xe00,
  320. },
  321. {
  322. .name = "high-end-overlay",
  323. .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
  324. .regs_offset = 0x340,
  325. .id = 3,
  326. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  327. .cfgs_offset = 0x4c,
  328. .layout = {
  329. .pos = 2,
  330. .size = 3,
  331. .memsize = 4,
  332. .xstride = { 5, 7 },
  333. .pstride = { 6, 8 },
  334. .default_color = 9,
  335. .chroma_key = 10,
  336. .chroma_key_mask = 11,
  337. .general_config = 12,
  338. .scaler_config = 13,
  339. .phicoeffs = {
  340. .x = 17,
  341. .y = 33,
  342. },
  343. .csc = 14,
  344. },
  345. .clut_offset = 0x1200,
  346. },
  347. };
  348. static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d4 = {
  349. .min_width = 0,
  350. .min_height = 0,
  351. .max_width = 2048,
  352. .max_height = 2048,
  353. .max_spw = 0xff,
  354. .max_vpw = 0xff,
  355. .max_hpw = 0x3ff,
  356. .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d4_layers),
  357. .layers = atmel_hlcdc_sama5d4_layers,
  358. .ops = &atmel_hlcdc_ops,
  359. };
  360. static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sam9x60_layers[] = {
  361. {
  362. .name = "base",
  363. .formats = &atmel_hlcdc_plane_rgb_formats,
  364. .regs_offset = 0x60,
  365. .id = 0,
  366. .type = ATMEL_HLCDC_BASE_LAYER,
  367. .cfgs_offset = 0x2c,
  368. .layout = {
  369. .xstride = { 2 },
  370. .default_color = 3,
  371. .general_config = 4,
  372. .disc_pos = 5,
  373. .disc_size = 6,
  374. },
  375. .clut_offset = 0x600,
  376. },
  377. {
  378. .name = "overlay1",
  379. .formats = &atmel_hlcdc_plane_rgb_formats,
  380. .regs_offset = 0x160,
  381. .id = 1,
  382. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  383. .cfgs_offset = 0x2c,
  384. .layout = {
  385. .pos = 2,
  386. .size = 3,
  387. .xstride = { 4 },
  388. .pstride = { 5 },
  389. .default_color = 6,
  390. .chroma_key = 7,
  391. .chroma_key_mask = 8,
  392. .general_config = 9,
  393. },
  394. .clut_offset = 0xa00,
  395. },
  396. {
  397. .name = "overlay2",
  398. .formats = &atmel_hlcdc_plane_rgb_formats,
  399. .regs_offset = 0x260,
  400. .id = 2,
  401. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  402. .cfgs_offset = 0x2c,
  403. .layout = {
  404. .pos = 2,
  405. .size = 3,
  406. .xstride = { 4 },
  407. .pstride = { 5 },
  408. .default_color = 6,
  409. .chroma_key = 7,
  410. .chroma_key_mask = 8,
  411. .general_config = 9,
  412. },
  413. .clut_offset = 0xe00,
  414. },
  415. {
  416. .name = "high-end-overlay",
  417. .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
  418. .regs_offset = 0x360,
  419. .id = 3,
  420. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  421. .cfgs_offset = 0x4c,
  422. .layout = {
  423. .pos = 2,
  424. .size = 3,
  425. .memsize = 4,
  426. .xstride = { 5, 7 },
  427. .pstride = { 6, 8 },
  428. .default_color = 9,
  429. .chroma_key = 10,
  430. .chroma_key_mask = 11,
  431. .general_config = 12,
  432. .scaler_config = 13,
  433. .phicoeffs = {
  434. .x = 17,
  435. .y = 33,
  436. },
  437. .csc = 14,
  438. },
  439. .clut_offset = 0x1200,
  440. },
  441. };
  442. static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sam9x60 = {
  443. .min_width = 0,
  444. .min_height = 0,
  445. .max_width = 2048,
  446. .max_height = 2048,
  447. .max_spw = 0xff,
  448. .max_vpw = 0xff,
  449. .max_hpw = 0x3ff,
  450. .fixed_clksrc = true,
  451. .nlayers = ARRAY_SIZE(atmel_hlcdc_sam9x60_layers),
  452. .layers = atmel_hlcdc_sam9x60_layers,
  453. .ops = &atmel_hlcdc_ops,
  454. };
  455. static const struct atmel_hlcdc_layer_desc atmel_xlcdc_sam9x75_layers[] = {
  456. {
  457. .name = "base",
  458. .formats = &atmel_hlcdc_plane_rgb_formats,
  459. .regs_offset = 0x60,
  460. .id = 0,
  461. .type = ATMEL_HLCDC_BASE_LAYER,
  462. .cfgs_offset = 0x1c,
  463. .layout = {
  464. .xstride = { 2 },
  465. .default_color = 3,
  466. .general_config = 4,
  467. .disc_pos = 5,
  468. .disc_size = 6,
  469. },
  470. .clut_offset = 0x700,
  471. },
  472. {
  473. .name = "overlay1",
  474. .formats = &atmel_hlcdc_plane_rgb_formats,
  475. .regs_offset = 0x160,
  476. .id = 1,
  477. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  478. .cfgs_offset = 0x1c,
  479. .layout = {
  480. .pos = 2,
  481. .size = 3,
  482. .xstride = { 4 },
  483. .pstride = { 5 },
  484. .default_color = 6,
  485. .chroma_key = 7,
  486. .chroma_key_mask = 8,
  487. .general_config = 9,
  488. },
  489. .clut_offset = 0xb00,
  490. },
  491. {
  492. .name = "overlay2",
  493. .formats = &atmel_hlcdc_plane_rgb_formats,
  494. .regs_offset = 0x260,
  495. .id = 2,
  496. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  497. .cfgs_offset = 0x1c,
  498. .layout = {
  499. .pos = 2,
  500. .size = 3,
  501. .xstride = { 4 },
  502. .pstride = { 5 },
  503. .default_color = 6,
  504. .chroma_key = 7,
  505. .chroma_key_mask = 8,
  506. .general_config = 9,
  507. },
  508. .clut_offset = 0xf00,
  509. },
  510. {
  511. .name = "high-end-overlay",
  512. .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
  513. .regs_offset = 0x360,
  514. .id = 3,
  515. .type = ATMEL_HLCDC_OVERLAY_LAYER,
  516. .cfgs_offset = 0x30,
  517. .layout = {
  518. .pos = 2,
  519. .size = 3,
  520. .memsize = 4,
  521. .xstride = { 5, 7 },
  522. .pstride = { 6, 8 },
  523. .default_color = 9,
  524. .chroma_key = 10,
  525. .chroma_key_mask = 11,
  526. .general_config = 12,
  527. .csc = 16,
  528. .scaler_config = 23,
  529. .vxs_config = 30,
  530. .hxs_config = 31,
  531. },
  532. .clut_offset = 0x1300,
  533. },
  534. };
  535. static const struct atmel_hlcdc_dc_desc atmel_xlcdc_dc_sam9x75 = {
  536. .min_width = 0,
  537. .min_height = 0,
  538. .max_width = 2048,
  539. .max_height = 2048,
  540. .max_spw = 0x3ff,
  541. .max_vpw = 0x3ff,
  542. .max_hpw = 0x3ff,
  543. .fixed_clksrc = true,
  544. .is_xlcdc = true,
  545. .nlayers = ARRAY_SIZE(atmel_xlcdc_sam9x75_layers),
  546. .layers = atmel_xlcdc_sam9x75_layers,
  547. .ops = &atmel_xlcdc_ops,
  548. };
  549. static const struct of_device_id atmel_hlcdc_of_match[] = {
  550. {
  551. .compatible = "atmel,at91sam9n12-hlcdc",
  552. .data = &atmel_hlcdc_dc_at91sam9n12,
  553. },
  554. {
  555. .compatible = "atmel,at91sam9x5-hlcdc",
  556. .data = &atmel_hlcdc_dc_at91sam9x5,
  557. },
  558. {
  559. .compatible = "atmel,sama5d2-hlcdc",
  560. .data = &atmel_hlcdc_dc_sama5d4,
  561. },
  562. {
  563. .compatible = "atmel,sama5d3-hlcdc",
  564. .data = &atmel_hlcdc_dc_sama5d3,
  565. },
  566. {
  567. .compatible = "atmel,sama5d4-hlcdc",
  568. .data = &atmel_hlcdc_dc_sama5d4,
  569. },
  570. {
  571. .compatible = "microchip,sam9x60-hlcdc",
  572. .data = &atmel_hlcdc_dc_sam9x60,
  573. },
  574. {
  575. .compatible = "microchip,sam9x75-xlcdc",
  576. .data = &atmel_xlcdc_dc_sam9x75,
  577. },
  578. { /* sentinel */ },
  579. };
  580. MODULE_DEVICE_TABLE(of, atmel_hlcdc_of_match);
  581. enum drm_mode_status
  582. atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
  583. const struct drm_display_mode *mode)
  584. {
  585. int vfront_porch = mode->vsync_start - mode->vdisplay;
  586. int vback_porch = mode->vtotal - mode->vsync_end;
  587. int vsync_len = mode->vsync_end - mode->vsync_start;
  588. int hfront_porch = mode->hsync_start - mode->hdisplay;
  589. int hback_porch = mode->htotal - mode->hsync_end;
  590. int hsync_len = mode->hsync_end - mode->hsync_start;
  591. if (hsync_len > dc->desc->max_spw + 1 || hsync_len < 1)
  592. return MODE_HSYNC;
  593. if (vsync_len > dc->desc->max_spw + 1 || vsync_len < 1)
  594. return MODE_VSYNC;
  595. if (hfront_porch > dc->desc->max_hpw + 1 || hfront_porch < 1 ||
  596. hback_porch > dc->desc->max_hpw + 1 || hback_porch < 1 ||
  597. mode->hdisplay < 1)
  598. return MODE_H_ILLEGAL;
  599. if (vfront_porch > dc->desc->max_vpw + 1 || vfront_porch < 1 ||
  600. vback_porch > dc->desc->max_vpw || vback_porch < 0 ||
  601. mode->vdisplay < 1)
  602. return MODE_V_ILLEGAL;
  603. return MODE_OK;
  604. }
  605. static void atmel_hlcdc_layer_irq(struct atmel_hlcdc_layer *layer)
  606. {
  607. if (!layer)
  608. return;
  609. if (layer->desc->type == ATMEL_HLCDC_BASE_LAYER ||
  610. layer->desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
  611. layer->desc->type == ATMEL_HLCDC_CURSOR_LAYER)
  612. atmel_hlcdc_plane_irq(atmel_hlcdc_layer_to_plane(layer));
  613. }
  614. static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
  615. {
  616. struct drm_device *dev = data;
  617. struct atmel_hlcdc_dc *dc = dev->dev_private;
  618. unsigned long status;
  619. unsigned int imr, isr;
  620. int i;
  621. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
  622. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
  623. status = imr & isr;
  624. if (!status)
  625. return IRQ_NONE;
  626. if (status & ATMEL_HLCDC_SOF)
  627. atmel_hlcdc_crtc_irq(dc->crtc);
  628. for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
  629. if (ATMEL_HLCDC_LAYER_STATUS(i) & status)
  630. atmel_hlcdc_layer_irq(dc->layers[i]);
  631. }
  632. return IRQ_HANDLED;
  633. }
  634. static void atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
  635. {
  636. struct atmel_hlcdc_dc *dc = dev->dev_private;
  637. unsigned int cfg = 0;
  638. int i;
  639. /* Enable interrupts on activated layers */
  640. for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
  641. if (dc->layers[i])
  642. cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
  643. }
  644. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
  645. }
  646. static void atmel_hlcdc_dc_irq_disable(struct drm_device *dev)
  647. {
  648. struct atmel_hlcdc_dc *dc = dev->dev_private;
  649. unsigned int isr;
  650. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
  651. regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
  652. }
  653. static int atmel_hlcdc_dc_irq_install(struct drm_device *dev, unsigned int irq)
  654. {
  655. int ret;
  656. atmel_hlcdc_dc_irq_disable(dev);
  657. ret = devm_request_irq(dev->dev, irq, atmel_hlcdc_dc_irq_handler, 0,
  658. dev->driver->name, dev);
  659. if (ret)
  660. return ret;
  661. atmel_hlcdc_dc_irq_postinstall(dev);
  662. return 0;
  663. }
  664. static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
  665. {
  666. atmel_hlcdc_dc_irq_disable(dev);
  667. }
  668. static const struct drm_mode_config_funcs mode_config_funcs = {
  669. .fb_create = drm_gem_fb_create,
  670. .atomic_check = drm_atomic_helper_check,
  671. .atomic_commit = drm_atomic_helper_commit,
  672. };
  673. static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
  674. {
  675. struct atmel_hlcdc_dc *dc = dev->dev_private;
  676. int ret;
  677. drm_mode_config_init(dev);
  678. ret = atmel_hlcdc_create_outputs(dev);
  679. if (ret) {
  680. dev_err(dev->dev, "failed to create HLCDC outputs: %d\n", ret);
  681. return ret;
  682. }
  683. ret = atmel_hlcdc_create_planes(dev);
  684. if (ret) {
  685. dev_err(dev->dev, "failed to create planes: %d\n", ret);
  686. return ret;
  687. }
  688. ret = atmel_hlcdc_crtc_create(dev);
  689. if (ret) {
  690. dev_err(dev->dev, "failed to create crtc\n");
  691. return ret;
  692. }
  693. dev->mode_config.min_width = dc->desc->min_width;
  694. dev->mode_config.min_height = dc->desc->min_height;
  695. dev->mode_config.max_width = dc->desc->max_width;
  696. dev->mode_config.max_height = dc->desc->max_height;
  697. dev->mode_config.funcs = &mode_config_funcs;
  698. dev->mode_config.async_page_flip = true;
  699. return 0;
  700. }
  701. static int atmel_hlcdc_dc_load(struct drm_device *dev)
  702. {
  703. struct platform_device *pdev = to_platform_device(dev->dev);
  704. const struct of_device_id *match;
  705. struct atmel_hlcdc_dc *dc;
  706. int ret;
  707. match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
  708. if (!match) {
  709. dev_err(&pdev->dev, "invalid compatible string\n");
  710. return -ENODEV;
  711. }
  712. if (!match->data) {
  713. dev_err(&pdev->dev, "invalid hlcdc description\n");
  714. return -EINVAL;
  715. }
  716. dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
  717. if (!dc)
  718. return -ENOMEM;
  719. dc->desc = match->data;
  720. dc->hlcdc = dev_get_drvdata(dev->dev->parent);
  721. dev->dev_private = dc;
  722. ret = clk_prepare_enable(dc->hlcdc->periph_clk);
  723. if (ret) {
  724. dev_err(dev->dev, "failed to enable periph_clk\n");
  725. return ret;
  726. }
  727. pm_runtime_enable(dev->dev);
  728. ret = drm_vblank_init(dev, 1);
  729. if (ret < 0) {
  730. dev_err(dev->dev, "failed to initialize vblank\n");
  731. goto err_periph_clk_disable;
  732. }
  733. ret = atmel_hlcdc_dc_modeset_init(dev);
  734. if (ret < 0) {
  735. dev_err(dev->dev, "failed to initialize mode setting\n");
  736. goto err_periph_clk_disable;
  737. }
  738. drm_mode_config_reset(dev);
  739. pm_runtime_get_sync(dev->dev);
  740. ret = atmel_hlcdc_dc_irq_install(dev, dc->hlcdc->irq);
  741. pm_runtime_put_sync(dev->dev);
  742. if (ret < 0) {
  743. dev_err(dev->dev, "failed to install IRQ handler\n");
  744. goto err_periph_clk_disable;
  745. }
  746. platform_set_drvdata(pdev, dev);
  747. drm_kms_helper_poll_init(dev);
  748. return 0;
  749. err_periph_clk_disable:
  750. pm_runtime_disable(dev->dev);
  751. clk_disable_unprepare(dc->hlcdc->periph_clk);
  752. return ret;
  753. }
  754. static void atmel_hlcdc_dc_unload(struct drm_device *dev)
  755. {
  756. struct atmel_hlcdc_dc *dc = dev->dev_private;
  757. drm_kms_helper_poll_fini(dev);
  758. drm_atomic_helper_shutdown(dev);
  759. drm_mode_config_cleanup(dev);
  760. pm_runtime_get_sync(dev->dev);
  761. atmel_hlcdc_dc_irq_uninstall(dev);
  762. pm_runtime_put_sync(dev->dev);
  763. dev->dev_private = NULL;
  764. pm_runtime_disable(dev->dev);
  765. clk_disable_unprepare(dc->hlcdc->periph_clk);
  766. }
  767. DEFINE_DRM_GEM_DMA_FOPS(fops);
  768. static const struct drm_driver atmel_hlcdc_dc_driver = {
  769. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  770. DRM_GEM_DMA_DRIVER_OPS,
  771. .fops = &fops,
  772. .name = "atmel-hlcdc",
  773. .desc = "Atmel HLCD Controller DRM",
  774. .date = "20141504",
  775. .major = 1,
  776. .minor = 0,
  777. };
  778. static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
  779. {
  780. struct drm_device *ddev;
  781. int ret;
  782. ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
  783. if (IS_ERR(ddev))
  784. return PTR_ERR(ddev);
  785. ret = atmel_hlcdc_dc_load(ddev);
  786. if (ret)
  787. goto err_put;
  788. ret = drm_dev_register(ddev, 0);
  789. if (ret)
  790. goto err_unload;
  791. drm_fbdev_dma_setup(ddev, 24);
  792. return 0;
  793. err_unload:
  794. atmel_hlcdc_dc_unload(ddev);
  795. err_put:
  796. drm_dev_put(ddev);
  797. return ret;
  798. }
  799. static void atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
  800. {
  801. struct drm_device *ddev = platform_get_drvdata(pdev);
  802. drm_dev_unregister(ddev);
  803. atmel_hlcdc_dc_unload(ddev);
  804. drm_dev_put(ddev);
  805. }
  806. static void atmel_hlcdc_dc_drm_shutdown(struct platform_device *pdev)
  807. {
  808. drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  809. }
  810. static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
  811. {
  812. struct drm_device *drm_dev = dev_get_drvdata(dev);
  813. struct atmel_hlcdc_dc *dc = drm_dev->dev_private;
  814. struct regmap *regmap = dc->hlcdc->regmap;
  815. struct drm_atomic_state *state;
  816. state = drm_atomic_helper_suspend(drm_dev);
  817. if (IS_ERR(state))
  818. return PTR_ERR(state);
  819. dc->suspend.state = state;
  820. regmap_read(regmap, ATMEL_HLCDC_IMR, &dc->suspend.imr);
  821. regmap_write(regmap, ATMEL_HLCDC_IDR, dc->suspend.imr);
  822. clk_disable_unprepare(dc->hlcdc->periph_clk);
  823. return 0;
  824. }
  825. static int atmel_hlcdc_dc_drm_resume(struct device *dev)
  826. {
  827. struct drm_device *drm_dev = dev_get_drvdata(dev);
  828. struct atmel_hlcdc_dc *dc = drm_dev->dev_private;
  829. clk_prepare_enable(dc->hlcdc->periph_clk);
  830. regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, dc->suspend.imr);
  831. return drm_atomic_helper_resume(drm_dev, dc->suspend.state);
  832. }
  833. static DEFINE_SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
  834. atmel_hlcdc_dc_drm_suspend,
  835. atmel_hlcdc_dc_drm_resume);
  836. static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
  837. { .compatible = "atmel,hlcdc-display-controller" },
  838. { },
  839. };
  840. static struct platform_driver atmel_hlcdc_dc_platform_driver = {
  841. .probe = atmel_hlcdc_dc_drm_probe,
  842. .remove_new = atmel_hlcdc_dc_drm_remove,
  843. .shutdown = atmel_hlcdc_dc_drm_shutdown,
  844. .driver = {
  845. .name = "atmel-hlcdc-display-controller",
  846. .pm = pm_sleep_ptr(&atmel_hlcdc_dc_drm_pm_ops),
  847. .of_match_table = atmel_hlcdc_dc_of_match,
  848. },
  849. };
  850. drm_module_platform_driver(atmel_hlcdc_dc_platform_driver);
  851. MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
  852. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  853. MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
  854. MODULE_LICENSE("GPL");
  855. MODULE_ALIAS("platform:atmel-hlcdc-dc");