atmel_hlcdc_plane.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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_h < state->src_h)
  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_h) - (256 * 4)) /
  296. state->crtc_h;
  297. factor++;
  298. max_memsize = ((factor * state->crtc_h) + (256 * 4)) /
  299. 2048;
  300. if (max_memsize > state->src_h)
  301. factor--;
  302. factor_reg |= (factor << 16) | 0x80000000;
  303. }
  304. atmel_hlcdc_layer_update_cfg(&plane->layer, 13, 0xffffffff,
  305. factor_reg);
  306. } else {
  307. atmel_hlcdc_layer_update_cfg(&plane->layer, 13, 0xffffffff, 0);
  308. }
  309. }
  310. static void
  311. atmel_hlcdc_plane_update_general_settings(struct atmel_hlcdc_plane *plane,
  312. struct atmel_hlcdc_plane_state *state)
  313. {
  314. const struct atmel_hlcdc_layer_cfg_layout *layout =
  315. &plane->layer.desc->layout;
  316. unsigned int cfg = ATMEL_HLCDC_LAYER_DMA;
  317. if (plane->base.type != DRM_PLANE_TYPE_PRIMARY) {
  318. cfg |= ATMEL_HLCDC_LAYER_OVR | ATMEL_HLCDC_LAYER_ITER2BL |
  319. ATMEL_HLCDC_LAYER_ITER;
  320. if (atmel_hlcdc_format_embeds_alpha(state->base.fb->pixel_format))
  321. cfg |= ATMEL_HLCDC_LAYER_LAEN;
  322. else
  323. cfg |= ATMEL_HLCDC_LAYER_GAEN |
  324. ATMEL_HLCDC_LAYER_GA(state->alpha);
  325. }
  326. atmel_hlcdc_layer_update_cfg(&plane->layer,
  327. ATMEL_HLCDC_LAYER_DMA_CFG_ID,
  328. ATMEL_HLCDC_LAYER_DMA_BLEN_MASK,
  329. ATMEL_HLCDC_LAYER_DMA_BLEN_INCR16);
  330. atmel_hlcdc_layer_update_cfg(&plane->layer, layout->general_config,
  331. ATMEL_HLCDC_LAYER_ITER2BL |
  332. ATMEL_HLCDC_LAYER_ITER |
  333. ATMEL_HLCDC_LAYER_GAEN |
  334. ATMEL_HLCDC_LAYER_GA_MASK |
  335. ATMEL_HLCDC_LAYER_LAEN |
  336. ATMEL_HLCDC_LAYER_OVR |
  337. ATMEL_HLCDC_LAYER_DMA, cfg);
  338. }
  339. static void atmel_hlcdc_plane_update_format(struct atmel_hlcdc_plane *plane,
  340. struct atmel_hlcdc_plane_state *state)
  341. {
  342. u32 cfg;
  343. int ret;
  344. ret = atmel_hlcdc_format_to_plane_mode(state->base.fb->pixel_format,
  345. &cfg);
  346. if (ret)
  347. return;
  348. if ((state->base.fb->pixel_format == DRM_FORMAT_YUV422 ||
  349. state->base.fb->pixel_format == DRM_FORMAT_NV61) &&
  350. (state->base.rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))))
  351. cfg |= ATMEL_HLCDC_YUV422ROT;
  352. atmel_hlcdc_layer_update_cfg(&plane->layer,
  353. ATMEL_HLCDC_LAYER_FORMAT_CFG_ID,
  354. 0xffffffff,
  355. cfg);
  356. /*
  357. * Rotation optimization is not working on RGB888 (rotation is still
  358. * working but without any optimization).
  359. */
  360. if (state->base.fb->pixel_format == DRM_FORMAT_RGB888)
  361. cfg = ATMEL_HLCDC_LAYER_DMA_ROTDIS;
  362. else
  363. cfg = 0;
  364. atmel_hlcdc_layer_update_cfg(&plane->layer,
  365. ATMEL_HLCDC_LAYER_DMA_CFG_ID,
  366. ATMEL_HLCDC_LAYER_DMA_ROTDIS, cfg);
  367. }
  368. static void atmel_hlcdc_plane_update_buffers(struct atmel_hlcdc_plane *plane,
  369. struct atmel_hlcdc_plane_state *state)
  370. {
  371. struct atmel_hlcdc_layer *layer = &plane->layer;
  372. const struct atmel_hlcdc_layer_cfg_layout *layout =
  373. &layer->desc->layout;
  374. int i;
  375. atmel_hlcdc_layer_update_set_fb(&plane->layer, state->base.fb,
  376. state->offsets);
  377. for (i = 0; i < state->nplanes; i++) {
  378. if (layout->xstride[i]) {
  379. atmel_hlcdc_layer_update_cfg(&plane->layer,
  380. layout->xstride[i],
  381. 0xffffffff,
  382. state->xstride[i]);
  383. }
  384. if (layout->pstride[i]) {
  385. atmel_hlcdc_layer_update_cfg(&plane->layer,
  386. layout->pstride[i],
  387. 0xffffffff,
  388. state->pstride[i]);
  389. }
  390. }
  391. }
  392. int
  393. atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state)
  394. {
  395. int disc_x = 0, disc_y = 0, disc_w = 0, disc_h = 0;
  396. const struct atmel_hlcdc_layer_cfg_layout *layout;
  397. struct atmel_hlcdc_plane_state *primary_state;
  398. struct drm_plane_state *primary_s;
  399. struct atmel_hlcdc_plane *primary;
  400. struct drm_plane *ovl;
  401. primary = drm_plane_to_atmel_hlcdc_plane(c_state->crtc->primary);
  402. layout = &primary->layer.desc->layout;
  403. if (!layout->disc_pos || !layout->disc_size)
  404. return 0;
  405. primary_s = drm_atomic_get_plane_state(c_state->state,
  406. &primary->base);
  407. if (IS_ERR(primary_s))
  408. return PTR_ERR(primary_s);
  409. primary_state = drm_plane_state_to_atmel_hlcdc_plane_state(primary_s);
  410. drm_atomic_crtc_state_for_each_plane(ovl, c_state) {
  411. struct atmel_hlcdc_plane_state *ovl_state;
  412. struct drm_plane_state *ovl_s;
  413. if (ovl == c_state->crtc->primary)
  414. continue;
  415. ovl_s = drm_atomic_get_plane_state(c_state->state, ovl);
  416. if (IS_ERR(ovl_s))
  417. return PTR_ERR(ovl_s);
  418. ovl_state = drm_plane_state_to_atmel_hlcdc_plane_state(ovl_s);
  419. if (!ovl_s->fb ||
  420. atmel_hlcdc_format_embeds_alpha(ovl_s->fb->pixel_format) ||
  421. ovl_state->alpha != 255)
  422. continue;
  423. /* TODO: implement a smarter hidden area detection */
  424. if (ovl_state->crtc_h * ovl_state->crtc_w < disc_h * disc_w)
  425. continue;
  426. disc_x = ovl_state->crtc_x;
  427. disc_y = ovl_state->crtc_y;
  428. disc_h = ovl_state->crtc_h;
  429. disc_w = ovl_state->crtc_w;
  430. }
  431. if (disc_x == primary_state->disc_x &&
  432. disc_y == primary_state->disc_y &&
  433. disc_w == primary_state->disc_w &&
  434. disc_h == primary_state->disc_h)
  435. return 0;
  436. primary_state->disc_x = disc_x;
  437. primary_state->disc_y = disc_y;
  438. primary_state->disc_w = disc_w;
  439. primary_state->disc_h = disc_h;
  440. primary_state->disc_updated = true;
  441. return 0;
  442. }
  443. static void
  444. atmel_hlcdc_plane_update_disc_area(struct atmel_hlcdc_plane *plane,
  445. struct atmel_hlcdc_plane_state *state)
  446. {
  447. const struct atmel_hlcdc_layer_cfg_layout *layout =
  448. &plane->layer.desc->layout;
  449. int disc_surface = 0;
  450. if (!state->disc_updated)
  451. return;
  452. disc_surface = state->disc_h * state->disc_w;
  453. atmel_hlcdc_layer_update_cfg(&plane->layer, layout->general_config,
  454. ATMEL_HLCDC_LAYER_DISCEN,
  455. disc_surface ? ATMEL_HLCDC_LAYER_DISCEN : 0);
  456. if (!disc_surface)
  457. return;
  458. atmel_hlcdc_layer_update_cfg(&plane->layer,
  459. layout->disc_pos,
  460. 0xffffffff,
  461. state->disc_x | (state->disc_y << 16));
  462. atmel_hlcdc_layer_update_cfg(&plane->layer,
  463. layout->disc_size,
  464. 0xffffffff,
  465. (state->disc_w - 1) |
  466. ((state->disc_h - 1) << 16));
  467. }
  468. static int atmel_hlcdc_plane_atomic_check(struct drm_plane *p,
  469. struct drm_plane_state *s)
  470. {
  471. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  472. struct atmel_hlcdc_plane_state *state =
  473. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  474. const struct atmel_hlcdc_layer_cfg_layout *layout =
  475. &plane->layer.desc->layout;
  476. struct drm_framebuffer *fb = state->base.fb;
  477. const struct drm_display_mode *mode;
  478. struct drm_crtc_state *crtc_state;
  479. unsigned int patched_crtc_w;
  480. unsigned int patched_crtc_h;
  481. unsigned int patched_src_w;
  482. unsigned int patched_src_h;
  483. unsigned int tmp;
  484. int x_offset = 0;
  485. int y_offset = 0;
  486. int hsub = 1;
  487. int vsub = 1;
  488. int i;
  489. if (!state->base.crtc || !fb)
  490. return 0;
  491. crtc_state = s->state->crtc_states[drm_crtc_index(s->crtc)];
  492. mode = &crtc_state->adjusted_mode;
  493. state->src_x = s->src_x;
  494. state->src_y = s->src_y;
  495. state->src_h = s->src_h;
  496. state->src_w = s->src_w;
  497. state->crtc_x = s->crtc_x;
  498. state->crtc_y = s->crtc_y;
  499. state->crtc_h = s->crtc_h;
  500. state->crtc_w = s->crtc_w;
  501. if ((state->src_x | state->src_y | state->src_w | state->src_h) &
  502. SUBPIXEL_MASK)
  503. return -EINVAL;
  504. state->src_x >>= 16;
  505. state->src_y >>= 16;
  506. state->src_w >>= 16;
  507. state->src_h >>= 16;
  508. state->nplanes = drm_format_num_planes(fb->pixel_format);
  509. if (state->nplanes > ATMEL_HLCDC_MAX_PLANES)
  510. return -EINVAL;
  511. /*
  512. * Swap width and size in case of 90 or 270 degrees rotation
  513. */
  514. if (state->base.rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
  515. tmp = state->crtc_w;
  516. state->crtc_w = state->crtc_h;
  517. state->crtc_h = tmp;
  518. tmp = state->src_w;
  519. state->src_w = state->src_h;
  520. state->src_h = tmp;
  521. }
  522. if (state->crtc_x + state->crtc_w > mode->hdisplay)
  523. patched_crtc_w = mode->hdisplay - state->crtc_x;
  524. else
  525. patched_crtc_w = state->crtc_w;
  526. if (state->crtc_x < 0) {
  527. patched_crtc_w += state->crtc_x;
  528. x_offset = -state->crtc_x;
  529. state->crtc_x = 0;
  530. }
  531. if (state->crtc_y + state->crtc_h > mode->vdisplay)
  532. patched_crtc_h = mode->vdisplay - state->crtc_y;
  533. else
  534. patched_crtc_h = state->crtc_h;
  535. if (state->crtc_y < 0) {
  536. patched_crtc_h += state->crtc_y;
  537. y_offset = -state->crtc_y;
  538. state->crtc_y = 0;
  539. }
  540. patched_src_w = DIV_ROUND_CLOSEST(patched_crtc_w * state->src_w,
  541. state->crtc_w);
  542. patched_src_h = DIV_ROUND_CLOSEST(patched_crtc_h * state->src_h,
  543. state->crtc_h);
  544. hsub = drm_format_horz_chroma_subsampling(fb->pixel_format);
  545. vsub = drm_format_vert_chroma_subsampling(fb->pixel_format);
  546. for (i = 0; i < state->nplanes; i++) {
  547. unsigned int offset = 0;
  548. int xdiv = i ? hsub : 1;
  549. int ydiv = i ? vsub : 1;
  550. state->bpp[i] = drm_format_plane_cpp(fb->pixel_format, i);
  551. if (!state->bpp[i])
  552. return -EINVAL;
  553. switch (state->base.rotation & DRM_ROTATE_MASK) {
  554. case BIT(DRM_ROTATE_90):
  555. offset = ((y_offset + state->src_y + patched_src_w - 1) /
  556. ydiv) * fb->pitches[i];
  557. offset += ((x_offset + state->src_x) / xdiv) *
  558. state->bpp[i];
  559. state->xstride[i] = ((patched_src_w - 1) / ydiv) *
  560. fb->pitches[i];
  561. state->pstride[i] = -fb->pitches[i] - state->bpp[i];
  562. break;
  563. case BIT(DRM_ROTATE_180):
  564. offset = ((y_offset + state->src_y + patched_src_h - 1) /
  565. ydiv) * fb->pitches[i];
  566. offset += ((x_offset + state->src_x + patched_src_w - 1) /
  567. xdiv) * state->bpp[i];
  568. state->xstride[i] = ((((patched_src_w - 1) / xdiv) - 1) *
  569. state->bpp[i]) - fb->pitches[i];
  570. state->pstride[i] = -2 * state->bpp[i];
  571. break;
  572. case BIT(DRM_ROTATE_270):
  573. offset = ((y_offset + state->src_y) / ydiv) *
  574. fb->pitches[i];
  575. offset += ((x_offset + state->src_x + patched_src_h - 1) /
  576. xdiv) * state->bpp[i];
  577. state->xstride[i] = -(((patched_src_w - 1) / ydiv) *
  578. fb->pitches[i]) -
  579. (2 * state->bpp[i]);
  580. state->pstride[i] = fb->pitches[i] - state->bpp[i];
  581. break;
  582. case BIT(DRM_ROTATE_0):
  583. default:
  584. offset = ((y_offset + state->src_y) / ydiv) *
  585. fb->pitches[i];
  586. offset += ((x_offset + state->src_x) / xdiv) *
  587. state->bpp[i];
  588. state->xstride[i] = fb->pitches[i] -
  589. ((patched_src_w / xdiv) *
  590. state->bpp[i]);
  591. state->pstride[i] = 0;
  592. break;
  593. }
  594. state->offsets[i] = offset + fb->offsets[i];
  595. }
  596. state->src_w = patched_src_w;
  597. state->src_h = patched_src_h;
  598. state->crtc_w = patched_crtc_w;
  599. state->crtc_h = patched_crtc_h;
  600. if (!layout->size &&
  601. (mode->hdisplay != state->crtc_w ||
  602. mode->vdisplay != state->crtc_h))
  603. return -EINVAL;
  604. if (plane->layer.desc->max_height &&
  605. state->crtc_h > plane->layer.desc->max_height)
  606. return -EINVAL;
  607. if (plane->layer.desc->max_width &&
  608. state->crtc_w > plane->layer.desc->max_width)
  609. return -EINVAL;
  610. if ((state->crtc_h != state->src_h || state->crtc_w != state->src_w) &&
  611. (!layout->memsize ||
  612. atmel_hlcdc_format_embeds_alpha(state->base.fb->pixel_format)))
  613. return -EINVAL;
  614. if (state->crtc_x < 0 || state->crtc_y < 0)
  615. return -EINVAL;
  616. if (state->crtc_w + state->crtc_x > mode->hdisplay ||
  617. state->crtc_h + state->crtc_y > mode->vdisplay)
  618. return -EINVAL;
  619. return 0;
  620. }
  621. static int atmel_hlcdc_plane_prepare_fb(struct drm_plane *p,
  622. const struct drm_plane_state *new_state)
  623. {
  624. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  625. if (!new_state->fb)
  626. return 0;
  627. return atmel_hlcdc_layer_update_start(&plane->layer);
  628. }
  629. static void atmel_hlcdc_plane_atomic_update(struct drm_plane *p,
  630. struct drm_plane_state *old_s)
  631. {
  632. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  633. struct atmel_hlcdc_plane_state *state =
  634. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  635. if (!p->state->crtc || !p->state->fb)
  636. return;
  637. atmel_hlcdc_plane_update_pos_and_size(plane, state);
  638. atmel_hlcdc_plane_update_general_settings(plane, state);
  639. atmel_hlcdc_plane_update_format(plane, state);
  640. atmel_hlcdc_plane_update_buffers(plane, state);
  641. atmel_hlcdc_plane_update_disc_area(plane, state);
  642. atmel_hlcdc_layer_update_commit(&plane->layer);
  643. }
  644. static void atmel_hlcdc_plane_atomic_disable(struct drm_plane *p,
  645. struct drm_plane_state *old_state)
  646. {
  647. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  648. atmel_hlcdc_layer_disable(&plane->layer);
  649. }
  650. static void atmel_hlcdc_plane_destroy(struct drm_plane *p)
  651. {
  652. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  653. if (plane->base.fb)
  654. drm_framebuffer_unreference(plane->base.fb);
  655. atmel_hlcdc_layer_cleanup(p->dev, &plane->layer);
  656. drm_plane_cleanup(p);
  657. devm_kfree(p->dev->dev, plane);
  658. }
  659. static int atmel_hlcdc_plane_atomic_set_property(struct drm_plane *p,
  660. struct drm_plane_state *s,
  661. struct drm_property *property,
  662. uint64_t val)
  663. {
  664. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  665. struct atmel_hlcdc_plane_properties *props = plane->properties;
  666. struct atmel_hlcdc_plane_state *state =
  667. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  668. if (property == props->alpha)
  669. state->alpha = val;
  670. else
  671. return -EINVAL;
  672. return 0;
  673. }
  674. static int atmel_hlcdc_plane_atomic_get_property(struct drm_plane *p,
  675. const struct drm_plane_state *s,
  676. struct drm_property *property,
  677. uint64_t *val)
  678. {
  679. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  680. struct atmel_hlcdc_plane_properties *props = plane->properties;
  681. const struct atmel_hlcdc_plane_state *state =
  682. container_of(s, const struct atmel_hlcdc_plane_state, base);
  683. if (property == props->alpha)
  684. *val = state->alpha;
  685. else
  686. return -EINVAL;
  687. return 0;
  688. }
  689. static void atmel_hlcdc_plane_init_properties(struct atmel_hlcdc_plane *plane,
  690. const struct atmel_hlcdc_layer_desc *desc,
  691. struct atmel_hlcdc_plane_properties *props)
  692. {
  693. struct regmap *regmap = plane->layer.hlcdc->regmap;
  694. if (desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
  695. desc->type == ATMEL_HLCDC_CURSOR_LAYER) {
  696. drm_object_attach_property(&plane->base.base,
  697. props->alpha, 255);
  698. /* Set default alpha value */
  699. regmap_update_bits(regmap,
  700. desc->regs_offset +
  701. ATMEL_HLCDC_LAYER_GENERAL_CFG(&plane->layer),
  702. ATMEL_HLCDC_LAYER_GA_MASK,
  703. ATMEL_HLCDC_LAYER_GA_MASK);
  704. }
  705. if (desc->layout.xstride && desc->layout.pstride)
  706. drm_object_attach_property(&plane->base.base,
  707. plane->base.dev->mode_config.rotation_property,
  708. BIT(DRM_ROTATE_0));
  709. if (desc->layout.csc) {
  710. /*
  711. * TODO: decare a "yuv-to-rgb-conv-factors" property to let
  712. * userspace modify these factors (using a BLOB property ?).
  713. */
  714. regmap_write(regmap,
  715. desc->regs_offset +
  716. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 0),
  717. 0x4c900091);
  718. regmap_write(regmap,
  719. desc->regs_offset +
  720. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 1),
  721. 0x7a5f5090);
  722. regmap_write(regmap,
  723. desc->regs_offset +
  724. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 2),
  725. 0x40040890);
  726. }
  727. }
  728. static struct drm_plane_helper_funcs atmel_hlcdc_layer_plane_helper_funcs = {
  729. .prepare_fb = atmel_hlcdc_plane_prepare_fb,
  730. .atomic_check = atmel_hlcdc_plane_atomic_check,
  731. .atomic_update = atmel_hlcdc_plane_atomic_update,
  732. .atomic_disable = atmel_hlcdc_plane_atomic_disable,
  733. };
  734. static void atmel_hlcdc_plane_reset(struct drm_plane *p)
  735. {
  736. struct atmel_hlcdc_plane_state *state;
  737. if (p->state) {
  738. state = drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  739. if (state->base.fb)
  740. drm_framebuffer_unreference(state->base.fb);
  741. kfree(state);
  742. p->state = NULL;
  743. }
  744. state = kzalloc(sizeof(*state), GFP_KERNEL);
  745. if (state) {
  746. state->alpha = 255;
  747. p->state = &state->base;
  748. p->state->plane = p;
  749. }
  750. }
  751. static struct drm_plane_state *
  752. atmel_hlcdc_plane_atomic_duplicate_state(struct drm_plane *p)
  753. {
  754. struct atmel_hlcdc_plane_state *state =
  755. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  756. struct atmel_hlcdc_plane_state *copy;
  757. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  758. if (!copy)
  759. return NULL;
  760. copy->disc_updated = false;
  761. if (copy->base.fb)
  762. drm_framebuffer_reference(copy->base.fb);
  763. return &copy->base;
  764. }
  765. static void atmel_hlcdc_plane_atomic_destroy_state(struct drm_plane *plane,
  766. struct drm_plane_state *s)
  767. {
  768. struct atmel_hlcdc_plane_state *state =
  769. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  770. if (s->fb)
  771. drm_framebuffer_unreference(s->fb);
  772. kfree(state);
  773. }
  774. static struct drm_plane_funcs layer_plane_funcs = {
  775. .update_plane = drm_atomic_helper_update_plane,
  776. .disable_plane = drm_atomic_helper_disable_plane,
  777. .set_property = drm_atomic_helper_plane_set_property,
  778. .destroy = atmel_hlcdc_plane_destroy,
  779. .reset = atmel_hlcdc_plane_reset,
  780. .atomic_duplicate_state = atmel_hlcdc_plane_atomic_duplicate_state,
  781. .atomic_destroy_state = atmel_hlcdc_plane_atomic_destroy_state,
  782. .atomic_set_property = atmel_hlcdc_plane_atomic_set_property,
  783. .atomic_get_property = atmel_hlcdc_plane_atomic_get_property,
  784. };
  785. static struct atmel_hlcdc_plane *
  786. atmel_hlcdc_plane_create(struct drm_device *dev,
  787. const struct atmel_hlcdc_layer_desc *desc,
  788. struct atmel_hlcdc_plane_properties *props)
  789. {
  790. struct atmel_hlcdc_plane *plane;
  791. enum drm_plane_type type;
  792. int ret;
  793. plane = devm_kzalloc(dev->dev, sizeof(*plane), GFP_KERNEL);
  794. if (!plane)
  795. return ERR_PTR(-ENOMEM);
  796. ret = atmel_hlcdc_layer_init(dev, &plane->layer, desc);
  797. if (ret)
  798. return ERR_PTR(ret);
  799. if (desc->type == ATMEL_HLCDC_BASE_LAYER)
  800. type = DRM_PLANE_TYPE_PRIMARY;
  801. else if (desc->type == ATMEL_HLCDC_CURSOR_LAYER)
  802. type = DRM_PLANE_TYPE_CURSOR;
  803. else
  804. type = DRM_PLANE_TYPE_OVERLAY;
  805. ret = drm_universal_plane_init(dev, &plane->base, 0,
  806. &layer_plane_funcs,
  807. desc->formats->formats,
  808. desc->formats->nformats, type);
  809. if (ret)
  810. return ERR_PTR(ret);
  811. drm_plane_helper_add(&plane->base,
  812. &atmel_hlcdc_layer_plane_helper_funcs);
  813. /* Set default property values*/
  814. atmel_hlcdc_plane_init_properties(plane, desc, props);
  815. return plane;
  816. }
  817. static struct atmel_hlcdc_plane_properties *
  818. atmel_hlcdc_plane_create_properties(struct drm_device *dev)
  819. {
  820. struct atmel_hlcdc_plane_properties *props;
  821. props = devm_kzalloc(dev->dev, sizeof(*props), GFP_KERNEL);
  822. if (!props)
  823. return ERR_PTR(-ENOMEM);
  824. props->alpha = drm_property_create_range(dev, 0, "alpha", 0, 255);
  825. if (!props->alpha)
  826. return ERR_PTR(-ENOMEM);
  827. dev->mode_config.rotation_property =
  828. drm_mode_create_rotation_property(dev,
  829. BIT(DRM_ROTATE_0) |
  830. BIT(DRM_ROTATE_90) |
  831. BIT(DRM_ROTATE_180) |
  832. BIT(DRM_ROTATE_270));
  833. if (!dev->mode_config.rotation_property)
  834. return ERR_PTR(-ENOMEM);
  835. return props;
  836. }
  837. struct atmel_hlcdc_planes *
  838. atmel_hlcdc_create_planes(struct drm_device *dev)
  839. {
  840. struct atmel_hlcdc_dc *dc = dev->dev_private;
  841. struct atmel_hlcdc_plane_properties *props;
  842. struct atmel_hlcdc_planes *planes;
  843. const struct atmel_hlcdc_layer_desc *descs = dc->desc->layers;
  844. int nlayers = dc->desc->nlayers;
  845. int i;
  846. planes = devm_kzalloc(dev->dev, sizeof(*planes), GFP_KERNEL);
  847. if (!planes)
  848. return ERR_PTR(-ENOMEM);
  849. for (i = 0; i < nlayers; i++) {
  850. if (descs[i].type == ATMEL_HLCDC_OVERLAY_LAYER)
  851. planes->noverlays++;
  852. }
  853. if (planes->noverlays) {
  854. planes->overlays = devm_kzalloc(dev->dev,
  855. planes->noverlays *
  856. sizeof(*planes->overlays),
  857. GFP_KERNEL);
  858. if (!planes->overlays)
  859. return ERR_PTR(-ENOMEM);
  860. }
  861. props = atmel_hlcdc_plane_create_properties(dev);
  862. if (IS_ERR(props))
  863. return ERR_CAST(props);
  864. planes->noverlays = 0;
  865. for (i = 0; i < nlayers; i++) {
  866. struct atmel_hlcdc_plane *plane;
  867. if (descs[i].type == ATMEL_HLCDC_PP_LAYER)
  868. continue;
  869. plane = atmel_hlcdc_plane_create(dev, &descs[i], props);
  870. if (IS_ERR(plane))
  871. return ERR_CAST(plane);
  872. plane->properties = props;
  873. switch (descs[i].type) {
  874. case ATMEL_HLCDC_BASE_LAYER:
  875. if (planes->primary)
  876. return ERR_PTR(-EINVAL);
  877. planes->primary = plane;
  878. break;
  879. case ATMEL_HLCDC_OVERLAY_LAYER:
  880. planes->overlays[planes->noverlays++] = plane;
  881. break;
  882. case ATMEL_HLCDC_CURSOR_LAYER:
  883. if (planes->cursor)
  884. return ERR_PTR(-EINVAL);
  885. planes->cursor = plane;
  886. break;
  887. default:
  888. break;
  889. }
  890. }
  891. return planes;
  892. }