atmel_hlcdc_plane.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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 = drm_atomic_get_existing_crtc_state(s->state, 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 & DRM_ROTATE_MASK) {
  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. const struct drm_plane_state *new_state)
  621. {
  622. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  623. if (!new_state->fb)
  624. return 0;
  625. return atmel_hlcdc_layer_update_start(&plane->layer);
  626. }
  627. static void atmel_hlcdc_plane_atomic_update(struct drm_plane *p,
  628. struct drm_plane_state *old_s)
  629. {
  630. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  631. struct atmel_hlcdc_plane_state *state =
  632. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  633. if (!p->state->crtc || !p->state->fb)
  634. return;
  635. atmel_hlcdc_plane_update_pos_and_size(plane, state);
  636. atmel_hlcdc_plane_update_general_settings(plane, state);
  637. atmel_hlcdc_plane_update_format(plane, state);
  638. atmel_hlcdc_plane_update_buffers(plane, state);
  639. atmel_hlcdc_plane_update_disc_area(plane, state);
  640. atmel_hlcdc_layer_update_commit(&plane->layer);
  641. }
  642. static void atmel_hlcdc_plane_atomic_disable(struct drm_plane *p,
  643. struct drm_plane_state *old_state)
  644. {
  645. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  646. atmel_hlcdc_layer_disable(&plane->layer);
  647. }
  648. static void atmel_hlcdc_plane_destroy(struct drm_plane *p)
  649. {
  650. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  651. if (plane->base.fb)
  652. drm_framebuffer_unreference(plane->base.fb);
  653. atmel_hlcdc_layer_cleanup(p->dev, &plane->layer);
  654. drm_plane_cleanup(p);
  655. devm_kfree(p->dev->dev, plane);
  656. }
  657. static int atmel_hlcdc_plane_atomic_set_property(struct drm_plane *p,
  658. struct drm_plane_state *s,
  659. struct drm_property *property,
  660. uint64_t val)
  661. {
  662. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  663. struct atmel_hlcdc_plane_properties *props = plane->properties;
  664. struct atmel_hlcdc_plane_state *state =
  665. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  666. if (property == props->alpha)
  667. state->alpha = val;
  668. else
  669. return -EINVAL;
  670. return 0;
  671. }
  672. static int atmel_hlcdc_plane_atomic_get_property(struct drm_plane *p,
  673. const struct drm_plane_state *s,
  674. struct drm_property *property,
  675. uint64_t *val)
  676. {
  677. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  678. struct atmel_hlcdc_plane_properties *props = plane->properties;
  679. const struct atmel_hlcdc_plane_state *state =
  680. container_of(s, const struct atmel_hlcdc_plane_state, base);
  681. if (property == props->alpha)
  682. *val = state->alpha;
  683. else
  684. return -EINVAL;
  685. return 0;
  686. }
  687. static void atmel_hlcdc_plane_init_properties(struct atmel_hlcdc_plane *plane,
  688. const struct atmel_hlcdc_layer_desc *desc,
  689. struct atmel_hlcdc_plane_properties *props)
  690. {
  691. struct regmap *regmap = plane->layer.hlcdc->regmap;
  692. if (desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
  693. desc->type == ATMEL_HLCDC_CURSOR_LAYER) {
  694. drm_object_attach_property(&plane->base.base,
  695. props->alpha, 255);
  696. /* Set default alpha value */
  697. regmap_update_bits(regmap,
  698. desc->regs_offset +
  699. ATMEL_HLCDC_LAYER_GENERAL_CFG(&plane->layer),
  700. ATMEL_HLCDC_LAYER_GA_MASK,
  701. ATMEL_HLCDC_LAYER_GA_MASK);
  702. }
  703. if (desc->layout.xstride && desc->layout.pstride)
  704. drm_object_attach_property(&plane->base.base,
  705. plane->base.dev->mode_config.rotation_property,
  706. BIT(DRM_ROTATE_0));
  707. if (desc->layout.csc) {
  708. /*
  709. * TODO: decare a "yuv-to-rgb-conv-factors" property to let
  710. * userspace modify these factors (using a BLOB property ?).
  711. */
  712. regmap_write(regmap,
  713. desc->regs_offset +
  714. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 0),
  715. 0x4c900091);
  716. regmap_write(regmap,
  717. desc->regs_offset +
  718. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 1),
  719. 0x7a5f5090);
  720. regmap_write(regmap,
  721. desc->regs_offset +
  722. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 2),
  723. 0x40040890);
  724. }
  725. }
  726. static struct drm_plane_helper_funcs atmel_hlcdc_layer_plane_helper_funcs = {
  727. .prepare_fb = atmel_hlcdc_plane_prepare_fb,
  728. .atomic_check = atmel_hlcdc_plane_atomic_check,
  729. .atomic_update = atmel_hlcdc_plane_atomic_update,
  730. .atomic_disable = atmel_hlcdc_plane_atomic_disable,
  731. };
  732. static void atmel_hlcdc_plane_reset(struct drm_plane *p)
  733. {
  734. struct atmel_hlcdc_plane_state *state;
  735. if (p->state) {
  736. state = drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  737. if (state->base.fb)
  738. drm_framebuffer_unreference(state->base.fb);
  739. kfree(state);
  740. p->state = NULL;
  741. }
  742. state = kzalloc(sizeof(*state), GFP_KERNEL);
  743. if (state) {
  744. state->alpha = 255;
  745. p->state = &state->base;
  746. p->state->plane = p;
  747. }
  748. }
  749. static struct drm_plane_state *
  750. atmel_hlcdc_plane_atomic_duplicate_state(struct drm_plane *p)
  751. {
  752. struct atmel_hlcdc_plane_state *state =
  753. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  754. struct atmel_hlcdc_plane_state *copy;
  755. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  756. if (!copy)
  757. return NULL;
  758. copy->disc_updated = false;
  759. if (copy->base.fb)
  760. drm_framebuffer_reference(copy->base.fb);
  761. return &copy->base;
  762. }
  763. static void atmel_hlcdc_plane_atomic_destroy_state(struct drm_plane *plane,
  764. struct drm_plane_state *s)
  765. {
  766. struct atmel_hlcdc_plane_state *state =
  767. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  768. if (s->fb)
  769. drm_framebuffer_unreference(s->fb);
  770. kfree(state);
  771. }
  772. static struct drm_plane_funcs layer_plane_funcs = {
  773. .update_plane = drm_atomic_helper_update_plane,
  774. .disable_plane = drm_atomic_helper_disable_plane,
  775. .set_property = drm_atomic_helper_plane_set_property,
  776. .destroy = atmel_hlcdc_plane_destroy,
  777. .reset = atmel_hlcdc_plane_reset,
  778. .atomic_duplicate_state = atmel_hlcdc_plane_atomic_duplicate_state,
  779. .atomic_destroy_state = atmel_hlcdc_plane_atomic_destroy_state,
  780. .atomic_set_property = atmel_hlcdc_plane_atomic_set_property,
  781. .atomic_get_property = atmel_hlcdc_plane_atomic_get_property,
  782. };
  783. static struct atmel_hlcdc_plane *
  784. atmel_hlcdc_plane_create(struct drm_device *dev,
  785. const struct atmel_hlcdc_layer_desc *desc,
  786. struct atmel_hlcdc_plane_properties *props)
  787. {
  788. struct atmel_hlcdc_plane *plane;
  789. enum drm_plane_type type;
  790. int ret;
  791. plane = devm_kzalloc(dev->dev, sizeof(*plane), GFP_KERNEL);
  792. if (!plane)
  793. return ERR_PTR(-ENOMEM);
  794. ret = atmel_hlcdc_layer_init(dev, &plane->layer, desc);
  795. if (ret)
  796. return ERR_PTR(ret);
  797. if (desc->type == ATMEL_HLCDC_BASE_LAYER)
  798. type = DRM_PLANE_TYPE_PRIMARY;
  799. else if (desc->type == ATMEL_HLCDC_CURSOR_LAYER)
  800. type = DRM_PLANE_TYPE_CURSOR;
  801. else
  802. type = DRM_PLANE_TYPE_OVERLAY;
  803. ret = drm_universal_plane_init(dev, &plane->base, 0,
  804. &layer_plane_funcs,
  805. desc->formats->formats,
  806. desc->formats->nformats, type, NULL);
  807. if (ret)
  808. return ERR_PTR(ret);
  809. drm_plane_helper_add(&plane->base,
  810. &atmel_hlcdc_layer_plane_helper_funcs);
  811. /* Set default property values*/
  812. atmel_hlcdc_plane_init_properties(plane, desc, props);
  813. return plane;
  814. }
  815. static struct atmel_hlcdc_plane_properties *
  816. atmel_hlcdc_plane_create_properties(struct drm_device *dev)
  817. {
  818. struct atmel_hlcdc_plane_properties *props;
  819. props = devm_kzalloc(dev->dev, sizeof(*props), GFP_KERNEL);
  820. if (!props)
  821. return ERR_PTR(-ENOMEM);
  822. props->alpha = drm_property_create_range(dev, 0, "alpha", 0, 255);
  823. if (!props->alpha)
  824. return ERR_PTR(-ENOMEM);
  825. dev->mode_config.rotation_property =
  826. drm_mode_create_rotation_property(dev,
  827. BIT(DRM_ROTATE_0) |
  828. BIT(DRM_ROTATE_90) |
  829. BIT(DRM_ROTATE_180) |
  830. BIT(DRM_ROTATE_270));
  831. if (!dev->mode_config.rotation_property)
  832. return ERR_PTR(-ENOMEM);
  833. return props;
  834. }
  835. struct atmel_hlcdc_planes *
  836. atmel_hlcdc_create_planes(struct drm_device *dev)
  837. {
  838. struct atmel_hlcdc_dc *dc = dev->dev_private;
  839. struct atmel_hlcdc_plane_properties *props;
  840. struct atmel_hlcdc_planes *planes;
  841. const struct atmel_hlcdc_layer_desc *descs = dc->desc->layers;
  842. int nlayers = dc->desc->nlayers;
  843. int i;
  844. planes = devm_kzalloc(dev->dev, sizeof(*planes), GFP_KERNEL);
  845. if (!planes)
  846. return ERR_PTR(-ENOMEM);
  847. for (i = 0; i < nlayers; i++) {
  848. if (descs[i].type == ATMEL_HLCDC_OVERLAY_LAYER)
  849. planes->noverlays++;
  850. }
  851. if (planes->noverlays) {
  852. planes->overlays = devm_kzalloc(dev->dev,
  853. planes->noverlays *
  854. sizeof(*planes->overlays),
  855. GFP_KERNEL);
  856. if (!planes->overlays)
  857. return ERR_PTR(-ENOMEM);
  858. }
  859. props = atmel_hlcdc_plane_create_properties(dev);
  860. if (IS_ERR(props))
  861. return ERR_CAST(props);
  862. planes->noverlays = 0;
  863. for (i = 0; i < nlayers; i++) {
  864. struct atmel_hlcdc_plane *plane;
  865. if (descs[i].type == ATMEL_HLCDC_PP_LAYER)
  866. continue;
  867. plane = atmel_hlcdc_plane_create(dev, &descs[i], props);
  868. if (IS_ERR(plane))
  869. return ERR_CAST(plane);
  870. plane->properties = props;
  871. switch (descs[i].type) {
  872. case ATMEL_HLCDC_BASE_LAYER:
  873. if (planes->primary)
  874. return ERR_PTR(-EINVAL);
  875. planes->primary = plane;
  876. break;
  877. case ATMEL_HLCDC_OVERLAY_LAYER:
  878. planes->overlays[planes->noverlays++] = plane;
  879. break;
  880. case ATMEL_HLCDC_CURSOR_LAYER:
  881. if (planes->cursor)
  882. return ERR_PTR(-EINVAL);
  883. planes->cursor = plane;
  884. break;
  885. default:
  886. break;
  887. }
  888. }
  889. return planes;
  890. }