atmel_hlcdc_plane.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * Copyright (C) 2014 Free Electrons
  3. * Copyright (C) 2014 Atmel
  4. *
  5. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "atmel_hlcdc_dc.h"
  20. /**
  21. * Atmel HLCDC Plane state structure.
  22. *
  23. * @base: DRM plane state
  24. * @crtc_x: x position of the plane relative to the CRTC
  25. * @crtc_y: y position of the plane relative to the CRTC
  26. * @crtc_w: visible width of the plane
  27. * @crtc_h: visible height of the plane
  28. * @src_x: x buffer position
  29. * @src_y: y buffer position
  30. * @src_w: buffer width
  31. * @src_h: buffer height
  32. * @alpha: alpha blending of the plane
  33. * @bpp: bytes per pixel deduced from pixel_format
  34. * @offsets: offsets to apply to the GEM buffers
  35. * @xstride: value to add to the pixel pointer between each line
  36. * @pstride: value to add to the pixel pointer between each pixel
  37. * @nplanes: number of planes (deduced from pixel_format)
  38. */
  39. struct atmel_hlcdc_plane_state {
  40. struct drm_plane_state base;
  41. int crtc_x;
  42. int crtc_y;
  43. unsigned int crtc_w;
  44. unsigned int crtc_h;
  45. uint32_t src_x;
  46. uint32_t src_y;
  47. uint32_t src_w;
  48. uint32_t src_h;
  49. u8 alpha;
  50. bool disc_updated;
  51. int disc_x;
  52. int disc_y;
  53. int disc_w;
  54. int disc_h;
  55. /* These fields are private and should not be touched */
  56. int bpp[ATMEL_HLCDC_MAX_PLANES];
  57. unsigned int offsets[ATMEL_HLCDC_MAX_PLANES];
  58. int xstride[ATMEL_HLCDC_MAX_PLANES];
  59. int pstride[ATMEL_HLCDC_MAX_PLANES];
  60. int nplanes;
  61. };
  62. static inline struct atmel_hlcdc_plane_state *
  63. drm_plane_state_to_atmel_hlcdc_plane_state(struct drm_plane_state *s)
  64. {
  65. return container_of(s, struct atmel_hlcdc_plane_state, base);
  66. }
  67. #define SUBPIXEL_MASK 0xffff
  68. static uint32_t rgb_formats[] = {
  69. DRM_FORMAT_XRGB4444,
  70. DRM_FORMAT_ARGB4444,
  71. DRM_FORMAT_RGBA4444,
  72. DRM_FORMAT_ARGB1555,
  73. DRM_FORMAT_RGB565,
  74. DRM_FORMAT_RGB888,
  75. DRM_FORMAT_XRGB8888,
  76. DRM_FORMAT_ARGB8888,
  77. DRM_FORMAT_RGBA8888,
  78. };
  79. struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_formats = {
  80. .formats = rgb_formats,
  81. .nformats = ARRAY_SIZE(rgb_formats),
  82. };
  83. static uint32_t rgb_and_yuv_formats[] = {
  84. DRM_FORMAT_XRGB4444,
  85. DRM_FORMAT_ARGB4444,
  86. DRM_FORMAT_RGBA4444,
  87. DRM_FORMAT_ARGB1555,
  88. DRM_FORMAT_RGB565,
  89. DRM_FORMAT_RGB888,
  90. DRM_FORMAT_XRGB8888,
  91. DRM_FORMAT_ARGB8888,
  92. DRM_FORMAT_RGBA8888,
  93. DRM_FORMAT_AYUV,
  94. DRM_FORMAT_YUYV,
  95. DRM_FORMAT_UYVY,
  96. DRM_FORMAT_YVYU,
  97. DRM_FORMAT_VYUY,
  98. DRM_FORMAT_NV21,
  99. DRM_FORMAT_NV61,
  100. DRM_FORMAT_YUV422,
  101. DRM_FORMAT_YUV420,
  102. };
  103. struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_and_yuv_formats = {
  104. .formats = rgb_and_yuv_formats,
  105. .nformats = ARRAY_SIZE(rgb_and_yuv_formats),
  106. };
  107. static int atmel_hlcdc_format_to_plane_mode(u32 format, u32 *mode)
  108. {
  109. switch (format) {
  110. case DRM_FORMAT_XRGB4444:
  111. *mode = ATMEL_HLCDC_XRGB4444_MODE;
  112. break;
  113. case DRM_FORMAT_ARGB4444:
  114. *mode = ATMEL_HLCDC_ARGB4444_MODE;
  115. break;
  116. case DRM_FORMAT_RGBA4444:
  117. *mode = ATMEL_HLCDC_RGBA4444_MODE;
  118. break;
  119. case DRM_FORMAT_RGB565:
  120. *mode = ATMEL_HLCDC_RGB565_MODE;
  121. break;
  122. case DRM_FORMAT_RGB888:
  123. *mode = ATMEL_HLCDC_RGB888_MODE;
  124. break;
  125. case DRM_FORMAT_ARGB1555:
  126. *mode = ATMEL_HLCDC_ARGB1555_MODE;
  127. break;
  128. case DRM_FORMAT_XRGB8888:
  129. *mode = ATMEL_HLCDC_XRGB8888_MODE;
  130. break;
  131. case DRM_FORMAT_ARGB8888:
  132. *mode = ATMEL_HLCDC_ARGB8888_MODE;
  133. break;
  134. case DRM_FORMAT_RGBA8888:
  135. *mode = ATMEL_HLCDC_RGBA8888_MODE;
  136. break;
  137. case DRM_FORMAT_AYUV:
  138. *mode = ATMEL_HLCDC_AYUV_MODE;
  139. break;
  140. case DRM_FORMAT_YUYV:
  141. *mode = ATMEL_HLCDC_YUYV_MODE;
  142. break;
  143. case DRM_FORMAT_UYVY:
  144. *mode = ATMEL_HLCDC_UYVY_MODE;
  145. break;
  146. case DRM_FORMAT_YVYU:
  147. *mode = ATMEL_HLCDC_YVYU_MODE;
  148. break;
  149. case DRM_FORMAT_VYUY:
  150. *mode = ATMEL_HLCDC_VYUY_MODE;
  151. break;
  152. case DRM_FORMAT_NV21:
  153. *mode = ATMEL_HLCDC_NV21_MODE;
  154. break;
  155. case DRM_FORMAT_NV61:
  156. *mode = ATMEL_HLCDC_NV61_MODE;
  157. break;
  158. case DRM_FORMAT_YUV420:
  159. *mode = ATMEL_HLCDC_YUV420_MODE;
  160. break;
  161. case DRM_FORMAT_YUV422:
  162. *mode = ATMEL_HLCDC_YUV422_MODE;
  163. break;
  164. default:
  165. return -ENOTSUPP;
  166. }
  167. return 0;
  168. }
  169. static bool atmel_hlcdc_format_embeds_alpha(u32 format)
  170. {
  171. int i;
  172. for (i = 0; i < sizeof(format); i++) {
  173. char tmp = (format >> (8 * i)) & 0xff;
  174. if (tmp == 'A')
  175. return true;
  176. }
  177. return false;
  178. }
  179. static u32 heo_downscaling_xcoef[] = {
  180. 0x11343311,
  181. 0x000000f7,
  182. 0x1635300c,
  183. 0x000000f9,
  184. 0x1b362c08,
  185. 0x000000fb,
  186. 0x1f372804,
  187. 0x000000fe,
  188. 0x24382400,
  189. 0x00000000,
  190. 0x28371ffe,
  191. 0x00000004,
  192. 0x2c361bfb,
  193. 0x00000008,
  194. 0x303516f9,
  195. 0x0000000c,
  196. };
  197. static u32 heo_downscaling_ycoef[] = {
  198. 0x00123737,
  199. 0x00173732,
  200. 0x001b382d,
  201. 0x001f3928,
  202. 0x00243824,
  203. 0x0028391f,
  204. 0x002d381b,
  205. 0x00323717,
  206. };
  207. static u32 heo_upscaling_xcoef[] = {
  208. 0xf74949f7,
  209. 0x00000000,
  210. 0xf55f33fb,
  211. 0x000000fe,
  212. 0xf5701efe,
  213. 0x000000ff,
  214. 0xf87c0dff,
  215. 0x00000000,
  216. 0x00800000,
  217. 0x00000000,
  218. 0x0d7cf800,
  219. 0x000000ff,
  220. 0x1e70f5ff,
  221. 0x000000fe,
  222. 0x335ff5fe,
  223. 0x000000fb,
  224. };
  225. static u32 heo_upscaling_ycoef[] = {
  226. 0x00004040,
  227. 0x00075920,
  228. 0x00056f0c,
  229. 0x00027b03,
  230. 0x00008000,
  231. 0x00037b02,
  232. 0x000c6f05,
  233. 0x00205907,
  234. };
  235. static void
  236. atmel_hlcdc_plane_update_pos_and_size(struct atmel_hlcdc_plane *plane,
  237. struct atmel_hlcdc_plane_state *state)
  238. {
  239. const struct atmel_hlcdc_layer_cfg_layout *layout =
  240. &plane->layer.desc->layout;
  241. if (layout->size)
  242. atmel_hlcdc_layer_update_cfg(&plane->layer,
  243. layout->size,
  244. 0xffffffff,
  245. (state->crtc_w - 1) |
  246. ((state->crtc_h - 1) << 16));
  247. if (layout->memsize)
  248. atmel_hlcdc_layer_update_cfg(&plane->layer,
  249. layout->memsize,
  250. 0xffffffff,
  251. (state->src_w - 1) |
  252. ((state->src_h - 1) << 16));
  253. if (layout->pos)
  254. atmel_hlcdc_layer_update_cfg(&plane->layer,
  255. layout->pos,
  256. 0xffffffff,
  257. state->crtc_x |
  258. (state->crtc_y << 16));
  259. /* TODO: rework the rescaling part */
  260. if (state->crtc_w != state->src_w || state->crtc_h != state->src_h) {
  261. u32 factor_reg = 0;
  262. if (state->crtc_w != state->src_w) {
  263. int i;
  264. u32 factor;
  265. u32 *coeff_tab = heo_upscaling_xcoef;
  266. u32 max_memsize;
  267. if (state->crtc_w < state->src_w)
  268. coeff_tab = heo_downscaling_xcoef;
  269. for (i = 0; i < ARRAY_SIZE(heo_upscaling_xcoef); i++)
  270. atmel_hlcdc_layer_update_cfg(&plane->layer,
  271. 17 + i,
  272. 0xffffffff,
  273. coeff_tab[i]);
  274. factor = ((8 * 256 * state->src_w) - (256 * 4)) /
  275. state->crtc_w;
  276. factor++;
  277. max_memsize = ((factor * state->crtc_w) + (256 * 4)) /
  278. 2048;
  279. if (max_memsize > state->src_w)
  280. factor--;
  281. factor_reg |= factor | 0x80000000;
  282. }
  283. if (state->crtc_h != state->src_h) {
  284. int i;
  285. u32 factor;
  286. u32 *coeff_tab = heo_upscaling_ycoef;
  287. u32 max_memsize;
  288. if (state->crtc_w < state->src_w)
  289. coeff_tab = heo_downscaling_ycoef;
  290. for (i = 0; i < ARRAY_SIZE(heo_upscaling_ycoef); i++)
  291. atmel_hlcdc_layer_update_cfg(&plane->layer,
  292. 33 + i,
  293. 0xffffffff,
  294. coeff_tab[i]);
  295. factor = ((8 * 256 * state->src_w) - (256 * 4)) /
  296. state->crtc_w;
  297. factor++;
  298. max_memsize = ((factor * state->crtc_w) + (256 * 4)) /
  299. 2048;
  300. if (max_memsize > state->src_w)
  301. factor--;
  302. factor_reg |= (factor << 16) | 0x80000000;
  303. }
  304. atmel_hlcdc_layer_update_cfg(&plane->layer, 13, 0xffffffff,
  305. factor_reg);
  306. }
  307. }
  308. static void
  309. atmel_hlcdc_plane_update_general_settings(struct atmel_hlcdc_plane *plane,
  310. struct atmel_hlcdc_plane_state *state)
  311. {
  312. const struct atmel_hlcdc_layer_cfg_layout *layout =
  313. &plane->layer.desc->layout;
  314. unsigned int cfg = ATMEL_HLCDC_LAYER_DMA;
  315. if (plane->base.type != DRM_PLANE_TYPE_PRIMARY) {
  316. cfg |= ATMEL_HLCDC_LAYER_OVR | ATMEL_HLCDC_LAYER_ITER2BL |
  317. ATMEL_HLCDC_LAYER_ITER;
  318. if (atmel_hlcdc_format_embeds_alpha(state->base.fb->pixel_format))
  319. cfg |= ATMEL_HLCDC_LAYER_LAEN;
  320. else
  321. cfg |= ATMEL_HLCDC_LAYER_GAEN |
  322. ATMEL_HLCDC_LAYER_GA(state->alpha);
  323. }
  324. atmel_hlcdc_layer_update_cfg(&plane->layer,
  325. ATMEL_HLCDC_LAYER_DMA_CFG_ID,
  326. ATMEL_HLCDC_LAYER_DMA_BLEN_MASK,
  327. ATMEL_HLCDC_LAYER_DMA_BLEN_INCR16);
  328. atmel_hlcdc_layer_update_cfg(&plane->layer, layout->general_config,
  329. ATMEL_HLCDC_LAYER_ITER2BL |
  330. ATMEL_HLCDC_LAYER_ITER |
  331. ATMEL_HLCDC_LAYER_GAEN |
  332. ATMEL_HLCDC_LAYER_GA_MASK |
  333. ATMEL_HLCDC_LAYER_LAEN |
  334. ATMEL_HLCDC_LAYER_OVR |
  335. ATMEL_HLCDC_LAYER_DMA, cfg);
  336. }
  337. static void atmel_hlcdc_plane_update_format(struct atmel_hlcdc_plane *plane,
  338. struct atmel_hlcdc_plane_state *state)
  339. {
  340. u32 cfg;
  341. int ret;
  342. ret = atmel_hlcdc_format_to_plane_mode(state->base.fb->pixel_format,
  343. &cfg);
  344. if (ret)
  345. return;
  346. if ((state->base.fb->pixel_format == DRM_FORMAT_YUV422 ||
  347. state->base.fb->pixel_format == DRM_FORMAT_NV61) &&
  348. (state->base.rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))))
  349. cfg |= ATMEL_HLCDC_YUV422ROT;
  350. atmel_hlcdc_layer_update_cfg(&plane->layer,
  351. ATMEL_HLCDC_LAYER_FORMAT_CFG_ID,
  352. 0xffffffff,
  353. cfg);
  354. /*
  355. * Rotation optimization is not working on RGB888 (rotation is still
  356. * working but without any optimization).
  357. */
  358. if (state->base.fb->pixel_format == DRM_FORMAT_RGB888)
  359. cfg = ATMEL_HLCDC_LAYER_DMA_ROTDIS;
  360. else
  361. cfg = 0;
  362. atmel_hlcdc_layer_update_cfg(&plane->layer,
  363. ATMEL_HLCDC_LAYER_DMA_CFG_ID,
  364. ATMEL_HLCDC_LAYER_DMA_ROTDIS, cfg);
  365. }
  366. static void atmel_hlcdc_plane_update_buffers(struct atmel_hlcdc_plane *plane,
  367. struct atmel_hlcdc_plane_state *state)
  368. {
  369. struct atmel_hlcdc_layer *layer = &plane->layer;
  370. const struct atmel_hlcdc_layer_cfg_layout *layout =
  371. &layer->desc->layout;
  372. int i;
  373. atmel_hlcdc_layer_update_set_fb(&plane->layer, state->base.fb,
  374. state->offsets);
  375. for (i = 0; i < state->nplanes; i++) {
  376. if (layout->xstride[i]) {
  377. atmel_hlcdc_layer_update_cfg(&plane->layer,
  378. layout->xstride[i],
  379. 0xffffffff,
  380. state->xstride[i]);
  381. }
  382. if (layout->pstride[i]) {
  383. atmel_hlcdc_layer_update_cfg(&plane->layer,
  384. layout->pstride[i],
  385. 0xffffffff,
  386. state->pstride[i]);
  387. }
  388. }
  389. }
  390. int
  391. atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state)
  392. {
  393. int disc_x = 0, disc_y = 0, disc_w = 0, disc_h = 0;
  394. const struct atmel_hlcdc_layer_cfg_layout *layout;
  395. struct atmel_hlcdc_plane_state *primary_state;
  396. struct drm_plane_state *primary_s;
  397. struct atmel_hlcdc_plane *primary;
  398. struct drm_plane *ovl;
  399. primary = drm_plane_to_atmel_hlcdc_plane(c_state->crtc->primary);
  400. layout = &primary->layer.desc->layout;
  401. if (!layout->disc_pos || !layout->disc_size)
  402. return 0;
  403. primary_s = drm_atomic_get_plane_state(c_state->state,
  404. &primary->base);
  405. if (IS_ERR(primary_s))
  406. return PTR_ERR(primary_s);
  407. primary_state = drm_plane_state_to_atmel_hlcdc_plane_state(primary_s);
  408. drm_atomic_crtc_state_for_each_plane(ovl, c_state) {
  409. struct atmel_hlcdc_plane_state *ovl_state;
  410. struct drm_plane_state *ovl_s;
  411. if (ovl == c_state->crtc->primary)
  412. continue;
  413. ovl_s = drm_atomic_get_plane_state(c_state->state, ovl);
  414. if (IS_ERR(ovl_s))
  415. return PTR_ERR(ovl_s);
  416. ovl_state = drm_plane_state_to_atmel_hlcdc_plane_state(ovl_s);
  417. if (!ovl_s->fb ||
  418. atmel_hlcdc_format_embeds_alpha(ovl_s->fb->pixel_format) ||
  419. ovl_state->alpha != 255)
  420. continue;
  421. /* TODO: implement a smarter hidden area detection */
  422. if (ovl_state->crtc_h * ovl_state->crtc_w < disc_h * disc_w)
  423. continue;
  424. disc_x = ovl_state->crtc_x;
  425. disc_y = ovl_state->crtc_y;
  426. disc_h = ovl_state->crtc_h;
  427. disc_w = ovl_state->crtc_w;
  428. }
  429. if (disc_x == primary_state->disc_x &&
  430. disc_y == primary_state->disc_y &&
  431. disc_w == primary_state->disc_w &&
  432. disc_h == primary_state->disc_h)
  433. return 0;
  434. primary_state->disc_x = disc_x;
  435. primary_state->disc_y = disc_y;
  436. primary_state->disc_w = disc_w;
  437. primary_state->disc_h = disc_h;
  438. primary_state->disc_updated = true;
  439. return 0;
  440. }
  441. static void
  442. atmel_hlcdc_plane_update_disc_area(struct atmel_hlcdc_plane *plane,
  443. struct atmel_hlcdc_plane_state *state)
  444. {
  445. const struct atmel_hlcdc_layer_cfg_layout *layout =
  446. &plane->layer.desc->layout;
  447. int disc_surface = 0;
  448. if (!state->disc_updated)
  449. return;
  450. disc_surface = state->disc_h * state->disc_w;
  451. atmel_hlcdc_layer_update_cfg(&plane->layer, layout->general_config,
  452. ATMEL_HLCDC_LAYER_DISCEN,
  453. disc_surface ? ATMEL_HLCDC_LAYER_DISCEN : 0);
  454. if (!disc_surface)
  455. return;
  456. atmel_hlcdc_layer_update_cfg(&plane->layer,
  457. layout->disc_pos,
  458. 0xffffffff,
  459. state->disc_x | (state->disc_y << 16));
  460. atmel_hlcdc_layer_update_cfg(&plane->layer,
  461. layout->disc_size,
  462. 0xffffffff,
  463. (state->disc_w - 1) |
  464. ((state->disc_h - 1) << 16));
  465. }
  466. static int atmel_hlcdc_plane_atomic_check(struct drm_plane *p,
  467. struct drm_plane_state *s)
  468. {
  469. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  470. struct atmel_hlcdc_plane_state *state =
  471. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  472. const struct atmel_hlcdc_layer_cfg_layout *layout =
  473. &plane->layer.desc->layout;
  474. struct drm_framebuffer *fb = state->base.fb;
  475. const struct drm_display_mode *mode;
  476. struct drm_crtc_state *crtc_state;
  477. unsigned int patched_crtc_w;
  478. unsigned int patched_crtc_h;
  479. unsigned int patched_src_w;
  480. unsigned int patched_src_h;
  481. unsigned int tmp;
  482. int x_offset = 0;
  483. int y_offset = 0;
  484. int hsub = 1;
  485. int vsub = 1;
  486. int i;
  487. if (!state->base.crtc || !fb)
  488. return 0;
  489. crtc_state = s->state->crtc_states[drm_crtc_index(s->crtc)];
  490. mode = &crtc_state->adjusted_mode;
  491. state->src_x = s->src_x;
  492. state->src_y = s->src_y;
  493. state->src_h = s->src_h;
  494. state->src_w = s->src_w;
  495. state->crtc_x = s->crtc_x;
  496. state->crtc_y = s->crtc_y;
  497. state->crtc_h = s->crtc_h;
  498. state->crtc_w = s->crtc_w;
  499. if ((state->src_x | state->src_y | state->src_w | state->src_h) &
  500. SUBPIXEL_MASK)
  501. return -EINVAL;
  502. state->src_x >>= 16;
  503. state->src_y >>= 16;
  504. state->src_w >>= 16;
  505. state->src_h >>= 16;
  506. state->nplanes = drm_format_num_planes(fb->pixel_format);
  507. if (state->nplanes > ATMEL_HLCDC_MAX_PLANES)
  508. return -EINVAL;
  509. /*
  510. * Swap width and size in case of 90 or 270 degrees rotation
  511. */
  512. if (state->base.rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
  513. tmp = state->crtc_w;
  514. state->crtc_w = state->crtc_h;
  515. state->crtc_h = tmp;
  516. tmp = state->src_w;
  517. state->src_w = state->src_h;
  518. state->src_h = tmp;
  519. }
  520. if (state->crtc_x + state->crtc_w > mode->hdisplay)
  521. patched_crtc_w = mode->hdisplay - state->crtc_x;
  522. else
  523. patched_crtc_w = state->crtc_w;
  524. if (state->crtc_x < 0) {
  525. patched_crtc_w += state->crtc_x;
  526. x_offset = -state->crtc_x;
  527. state->crtc_x = 0;
  528. }
  529. if (state->crtc_y + state->crtc_h > mode->vdisplay)
  530. patched_crtc_h = mode->vdisplay - state->crtc_y;
  531. else
  532. patched_crtc_h = state->crtc_h;
  533. if (state->crtc_y < 0) {
  534. patched_crtc_h += state->crtc_y;
  535. y_offset = -state->crtc_y;
  536. state->crtc_y = 0;
  537. }
  538. patched_src_w = DIV_ROUND_CLOSEST(patched_crtc_w * state->src_w,
  539. state->crtc_w);
  540. patched_src_h = DIV_ROUND_CLOSEST(patched_crtc_h * state->src_h,
  541. state->crtc_h);
  542. hsub = drm_format_horz_chroma_subsampling(fb->pixel_format);
  543. vsub = drm_format_vert_chroma_subsampling(fb->pixel_format);
  544. for (i = 0; i < state->nplanes; i++) {
  545. unsigned int offset = 0;
  546. int xdiv = i ? hsub : 1;
  547. int ydiv = i ? vsub : 1;
  548. state->bpp[i] = drm_format_plane_cpp(fb->pixel_format, i);
  549. if (!state->bpp[i])
  550. return -EINVAL;
  551. switch (state->base.rotation & 0xf) {
  552. case BIT(DRM_ROTATE_90):
  553. offset = ((y_offset + state->src_y + patched_src_w - 1) /
  554. ydiv) * fb->pitches[i];
  555. offset += ((x_offset + state->src_x) / xdiv) *
  556. state->bpp[i];
  557. state->xstride[i] = ((patched_src_w - 1) / ydiv) *
  558. fb->pitches[i];
  559. state->pstride[i] = -fb->pitches[i] - state->bpp[i];
  560. break;
  561. case BIT(DRM_ROTATE_180):
  562. offset = ((y_offset + state->src_y + patched_src_h - 1) /
  563. ydiv) * fb->pitches[i];
  564. offset += ((x_offset + state->src_x + patched_src_w - 1) /
  565. xdiv) * state->bpp[i];
  566. state->xstride[i] = ((((patched_src_w - 1) / xdiv) - 1) *
  567. state->bpp[i]) - fb->pitches[i];
  568. state->pstride[i] = -2 * state->bpp[i];
  569. break;
  570. case BIT(DRM_ROTATE_270):
  571. offset = ((y_offset + state->src_y) / ydiv) *
  572. fb->pitches[i];
  573. offset += ((x_offset + state->src_x + patched_src_h - 1) /
  574. xdiv) * state->bpp[i];
  575. state->xstride[i] = -(((patched_src_w - 1) / ydiv) *
  576. fb->pitches[i]) -
  577. (2 * state->bpp[i]);
  578. state->pstride[i] = fb->pitches[i] - state->bpp[i];
  579. break;
  580. case BIT(DRM_ROTATE_0):
  581. default:
  582. offset = ((y_offset + state->src_y) / ydiv) *
  583. fb->pitches[i];
  584. offset += ((x_offset + state->src_x) / xdiv) *
  585. state->bpp[i];
  586. state->xstride[i] = fb->pitches[i] -
  587. ((patched_src_w / xdiv) *
  588. state->bpp[i]);
  589. state->pstride[i] = 0;
  590. break;
  591. }
  592. state->offsets[i] = offset + fb->offsets[i];
  593. }
  594. state->src_w = patched_src_w;
  595. state->src_h = patched_src_h;
  596. state->crtc_w = patched_crtc_w;
  597. state->crtc_h = patched_crtc_h;
  598. if (!layout->size &&
  599. (mode->hdisplay != state->crtc_w ||
  600. mode->vdisplay != state->crtc_h))
  601. return -EINVAL;
  602. if (plane->layer.desc->max_height &&
  603. state->crtc_h > plane->layer.desc->max_height)
  604. return -EINVAL;
  605. if (plane->layer.desc->max_width &&
  606. state->crtc_w > plane->layer.desc->max_width)
  607. return -EINVAL;
  608. if ((state->crtc_h != state->src_h || state->crtc_w != state->src_w) &&
  609. (!layout->memsize ||
  610. atmel_hlcdc_format_embeds_alpha(state->base.fb->pixel_format)))
  611. return -EINVAL;
  612. if (state->crtc_x < 0 || state->crtc_y < 0)
  613. return -EINVAL;
  614. if (state->crtc_w + state->crtc_x > mode->hdisplay ||
  615. state->crtc_h + state->crtc_y > mode->vdisplay)
  616. return -EINVAL;
  617. return 0;
  618. }
  619. static int atmel_hlcdc_plane_prepare_fb(struct drm_plane *p,
  620. struct drm_framebuffer *fb,
  621. const struct drm_plane_state *new_state)
  622. {
  623. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  624. return atmel_hlcdc_layer_update_start(&plane->layer);
  625. }
  626. static void atmel_hlcdc_plane_atomic_update(struct drm_plane *p,
  627. struct drm_plane_state *old_s)
  628. {
  629. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  630. struct atmel_hlcdc_plane_state *state =
  631. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  632. if (!p->state->crtc || !p->state->fb)
  633. return;
  634. atmel_hlcdc_plane_update_pos_and_size(plane, state);
  635. atmel_hlcdc_plane_update_general_settings(plane, state);
  636. atmel_hlcdc_plane_update_format(plane, state);
  637. atmel_hlcdc_plane_update_buffers(plane, state);
  638. atmel_hlcdc_plane_update_disc_area(plane, state);
  639. atmel_hlcdc_layer_update_commit(&plane->layer);
  640. }
  641. static void atmel_hlcdc_plane_atomic_disable(struct drm_plane *p,
  642. struct drm_plane_state *old_state)
  643. {
  644. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  645. atmel_hlcdc_layer_disable(&plane->layer);
  646. }
  647. static void atmel_hlcdc_plane_destroy(struct drm_plane *p)
  648. {
  649. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  650. if (plane->base.fb)
  651. drm_framebuffer_unreference(plane->base.fb);
  652. atmel_hlcdc_layer_cleanup(p->dev, &plane->layer);
  653. drm_plane_cleanup(p);
  654. devm_kfree(p->dev->dev, plane);
  655. }
  656. static int atmel_hlcdc_plane_atomic_set_property(struct drm_plane *p,
  657. struct drm_plane_state *s,
  658. struct drm_property *property,
  659. uint64_t val)
  660. {
  661. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  662. struct atmel_hlcdc_plane_properties *props = plane->properties;
  663. struct atmel_hlcdc_plane_state *state =
  664. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  665. if (property == props->alpha)
  666. state->alpha = val;
  667. else
  668. return -EINVAL;
  669. return 0;
  670. }
  671. static int atmel_hlcdc_plane_atomic_get_property(struct drm_plane *p,
  672. const struct drm_plane_state *s,
  673. struct drm_property *property,
  674. uint64_t *val)
  675. {
  676. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  677. struct atmel_hlcdc_plane_properties *props = plane->properties;
  678. const struct atmel_hlcdc_plane_state *state =
  679. container_of(s, const struct atmel_hlcdc_plane_state, base);
  680. if (property == props->alpha)
  681. *val = state->alpha;
  682. else
  683. return -EINVAL;
  684. return 0;
  685. }
  686. static void atmel_hlcdc_plane_init_properties(struct atmel_hlcdc_plane *plane,
  687. const struct atmel_hlcdc_layer_desc *desc,
  688. struct atmel_hlcdc_plane_properties *props)
  689. {
  690. struct regmap *regmap = plane->layer.hlcdc->regmap;
  691. if (desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
  692. desc->type == ATMEL_HLCDC_CURSOR_LAYER) {
  693. drm_object_attach_property(&plane->base.base,
  694. props->alpha, 255);
  695. /* Set default alpha value */
  696. regmap_update_bits(regmap,
  697. desc->regs_offset +
  698. ATMEL_HLCDC_LAYER_GENERAL_CFG(&plane->layer),
  699. ATMEL_HLCDC_LAYER_GA_MASK,
  700. ATMEL_HLCDC_LAYER_GA_MASK);
  701. }
  702. if (desc->layout.xstride && desc->layout.pstride)
  703. drm_object_attach_property(&plane->base.base,
  704. plane->base.dev->mode_config.rotation_property,
  705. BIT(DRM_ROTATE_0));
  706. if (desc->layout.csc) {
  707. /*
  708. * TODO: decare a "yuv-to-rgb-conv-factors" property to let
  709. * userspace modify these factors (using a BLOB property ?).
  710. */
  711. regmap_write(regmap,
  712. desc->regs_offset +
  713. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 0),
  714. 0x4c900091);
  715. regmap_write(regmap,
  716. desc->regs_offset +
  717. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 1),
  718. 0x7a5f5090);
  719. regmap_write(regmap,
  720. desc->regs_offset +
  721. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 2),
  722. 0x40040890);
  723. }
  724. }
  725. static struct drm_plane_helper_funcs atmel_hlcdc_layer_plane_helper_funcs = {
  726. .prepare_fb = atmel_hlcdc_plane_prepare_fb,
  727. .atomic_check = atmel_hlcdc_plane_atomic_check,
  728. .atomic_update = atmel_hlcdc_plane_atomic_update,
  729. .atomic_disable = atmel_hlcdc_plane_atomic_disable,
  730. };
  731. static void atmel_hlcdc_plane_reset(struct drm_plane *p)
  732. {
  733. struct atmel_hlcdc_plane_state *state;
  734. if (p->state) {
  735. state = drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  736. if (state->base.fb)
  737. drm_framebuffer_unreference(state->base.fb);
  738. kfree(state);
  739. p->state = NULL;
  740. }
  741. state = kzalloc(sizeof(*state), GFP_KERNEL);
  742. if (state) {
  743. state->alpha = 255;
  744. p->state = &state->base;
  745. p->state->plane = p;
  746. }
  747. }
  748. static struct drm_plane_state *
  749. atmel_hlcdc_plane_atomic_duplicate_state(struct drm_plane *p)
  750. {
  751. struct atmel_hlcdc_plane_state *state =
  752. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  753. struct atmel_hlcdc_plane_state *copy;
  754. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  755. if (!copy)
  756. return NULL;
  757. copy->disc_updated = false;
  758. if (copy->base.fb)
  759. drm_framebuffer_reference(copy->base.fb);
  760. return &copy->base;
  761. }
  762. static void atmel_hlcdc_plane_atomic_destroy_state(struct drm_plane *plane,
  763. struct drm_plane_state *s)
  764. {
  765. struct atmel_hlcdc_plane_state *state =
  766. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  767. if (s->fb)
  768. drm_framebuffer_unreference(s->fb);
  769. kfree(state);
  770. }
  771. static struct drm_plane_funcs layer_plane_funcs = {
  772. .update_plane = drm_atomic_helper_update_plane,
  773. .disable_plane = drm_atomic_helper_disable_plane,
  774. .set_property = drm_atomic_helper_plane_set_property,
  775. .destroy = atmel_hlcdc_plane_destroy,
  776. .reset = atmel_hlcdc_plane_reset,
  777. .atomic_duplicate_state = atmel_hlcdc_plane_atomic_duplicate_state,
  778. .atomic_destroy_state = atmel_hlcdc_plane_atomic_destroy_state,
  779. .atomic_set_property = atmel_hlcdc_plane_atomic_set_property,
  780. .atomic_get_property = atmel_hlcdc_plane_atomic_get_property,
  781. };
  782. static struct atmel_hlcdc_plane *
  783. atmel_hlcdc_plane_create(struct drm_device *dev,
  784. const struct atmel_hlcdc_layer_desc *desc,
  785. struct atmel_hlcdc_plane_properties *props)
  786. {
  787. struct atmel_hlcdc_plane *plane;
  788. enum drm_plane_type type;
  789. int ret;
  790. plane = devm_kzalloc(dev->dev, sizeof(*plane), GFP_KERNEL);
  791. if (!plane)
  792. return ERR_PTR(-ENOMEM);
  793. ret = atmel_hlcdc_layer_init(dev, &plane->layer, desc);
  794. if (ret)
  795. return ERR_PTR(ret);
  796. if (desc->type == ATMEL_HLCDC_BASE_LAYER)
  797. type = DRM_PLANE_TYPE_PRIMARY;
  798. else if (desc->type == ATMEL_HLCDC_CURSOR_LAYER)
  799. type = DRM_PLANE_TYPE_CURSOR;
  800. else
  801. type = DRM_PLANE_TYPE_OVERLAY;
  802. ret = drm_universal_plane_init(dev, &plane->base, 0,
  803. &layer_plane_funcs,
  804. desc->formats->formats,
  805. desc->formats->nformats, type);
  806. if (ret)
  807. return ERR_PTR(ret);
  808. drm_plane_helper_add(&plane->base,
  809. &atmel_hlcdc_layer_plane_helper_funcs);
  810. /* Set default property values*/
  811. atmel_hlcdc_plane_init_properties(plane, desc, props);
  812. return plane;
  813. }
  814. static struct atmel_hlcdc_plane_properties *
  815. atmel_hlcdc_plane_create_properties(struct drm_device *dev)
  816. {
  817. struct atmel_hlcdc_plane_properties *props;
  818. props = devm_kzalloc(dev->dev, sizeof(*props), GFP_KERNEL);
  819. if (!props)
  820. return ERR_PTR(-ENOMEM);
  821. props->alpha = drm_property_create_range(dev, 0, "alpha", 0, 255);
  822. if (!props->alpha)
  823. return ERR_PTR(-ENOMEM);
  824. dev->mode_config.rotation_property =
  825. drm_mode_create_rotation_property(dev,
  826. BIT(DRM_ROTATE_0) |
  827. BIT(DRM_ROTATE_90) |
  828. BIT(DRM_ROTATE_180) |
  829. BIT(DRM_ROTATE_270));
  830. if (!dev->mode_config.rotation_property)
  831. return ERR_PTR(-ENOMEM);
  832. return props;
  833. }
  834. struct atmel_hlcdc_planes *
  835. atmel_hlcdc_create_planes(struct drm_device *dev)
  836. {
  837. struct atmel_hlcdc_dc *dc = dev->dev_private;
  838. struct atmel_hlcdc_plane_properties *props;
  839. struct atmel_hlcdc_planes *planes;
  840. const struct atmel_hlcdc_layer_desc *descs = dc->desc->layers;
  841. int nlayers = dc->desc->nlayers;
  842. int i;
  843. planes = devm_kzalloc(dev->dev, sizeof(*planes), GFP_KERNEL);
  844. if (!planes)
  845. return ERR_PTR(-ENOMEM);
  846. for (i = 0; i < nlayers; i++) {
  847. if (descs[i].type == ATMEL_HLCDC_OVERLAY_LAYER)
  848. planes->noverlays++;
  849. }
  850. if (planes->noverlays) {
  851. planes->overlays = devm_kzalloc(dev->dev,
  852. planes->noverlays *
  853. sizeof(*planes->overlays),
  854. GFP_KERNEL);
  855. if (!planes->overlays)
  856. return ERR_PTR(-ENOMEM);
  857. }
  858. props = atmel_hlcdc_plane_create_properties(dev);
  859. if (IS_ERR(props))
  860. return ERR_CAST(props);
  861. planes->noverlays = 0;
  862. for (i = 0; i < nlayers; i++) {
  863. struct atmel_hlcdc_plane *plane;
  864. if (descs[i].type == ATMEL_HLCDC_PP_LAYER)
  865. continue;
  866. plane = atmel_hlcdc_plane_create(dev, &descs[i], props);
  867. if (IS_ERR(plane))
  868. return ERR_CAST(plane);
  869. plane->properties = props;
  870. switch (descs[i].type) {
  871. case ATMEL_HLCDC_BASE_LAYER:
  872. if (planes->primary)
  873. return ERR_PTR(-EINVAL);
  874. planes->primary = plane;
  875. break;
  876. case ATMEL_HLCDC_OVERLAY_LAYER:
  877. planes->overlays[planes->noverlays++] = plane;
  878. break;
  879. case ATMEL_HLCDC_CURSOR_LAYER:
  880. if (planes->cursor)
  881. return ERR_PTR(-EINVAL);
  882. planes->cursor = plane;
  883. break;
  884. default:
  885. break;
  886. }
  887. }
  888. return planes;
  889. }