dc.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  1. /*
  2. * Copyright (C) 2012 Avionic Design GmbH
  3. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/iommu.h>
  12. #include <linux/of_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/reset.h>
  15. #include <soc/tegra/pmc.h>
  16. #include "dc.h"
  17. #include "drm.h"
  18. #include "gem.h"
  19. #include "hub.h"
  20. #include "plane.h"
  21. #include <drm/drm_atomic.h>
  22. #include <drm/drm_atomic_helper.h>
  23. #include <drm/drm_plane_helper.h>
  24. static void tegra_dc_stats_reset(struct tegra_dc_stats *stats)
  25. {
  26. stats->frames = 0;
  27. stats->vblank = 0;
  28. stats->underflow = 0;
  29. stats->overflow = 0;
  30. }
  31. /* Reads the active copy of a register. */
  32. static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
  33. {
  34. u32 value;
  35. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  36. value = tegra_dc_readl(dc, offset);
  37. tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
  38. return value;
  39. }
  40. static inline unsigned int tegra_plane_offset(struct tegra_plane *plane,
  41. unsigned int offset)
  42. {
  43. if (offset >= 0x500 && offset <= 0x638) {
  44. offset = 0x000 + (offset - 0x500);
  45. return plane->offset + offset;
  46. }
  47. if (offset >= 0x700 && offset <= 0x719) {
  48. offset = 0x180 + (offset - 0x700);
  49. return plane->offset + offset;
  50. }
  51. if (offset >= 0x800 && offset <= 0x839) {
  52. offset = 0x1c0 + (offset - 0x800);
  53. return plane->offset + offset;
  54. }
  55. dev_WARN(plane->dc->dev, "invalid offset: %x\n", offset);
  56. return plane->offset + offset;
  57. }
  58. static inline u32 tegra_plane_readl(struct tegra_plane *plane,
  59. unsigned int offset)
  60. {
  61. return tegra_dc_readl(plane->dc, tegra_plane_offset(plane, offset));
  62. }
  63. static inline void tegra_plane_writel(struct tegra_plane *plane, u32 value,
  64. unsigned int offset)
  65. {
  66. tegra_dc_writel(plane->dc, value, tegra_plane_offset(plane, offset));
  67. }
  68. bool tegra_dc_has_output(struct tegra_dc *dc, struct device *dev)
  69. {
  70. struct device_node *np = dc->dev->of_node;
  71. struct of_phandle_iterator it;
  72. int err;
  73. of_for_each_phandle(&it, err, np, "nvidia,outputs", NULL, 0)
  74. if (it.node == dev->of_node)
  75. return true;
  76. return false;
  77. }
  78. /*
  79. * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
  80. * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
  81. * Latching happens mmediately if the display controller is in STOP mode or
  82. * on the next frame boundary otherwise.
  83. *
  84. * Triple-buffered registers have three copies: ASSEMBLY, ARM and ACTIVE. The
  85. * ASSEMBLY copy is latched into the ARM copy immediately after *_UPDATE bits
  86. * are written. When the *_ACT_REQ bits are written, the ARM copy is latched
  87. * into the ACTIVE copy, either immediately if the display controller is in
  88. * STOP mode, or at the next frame boundary otherwise.
  89. */
  90. void tegra_dc_commit(struct tegra_dc *dc)
  91. {
  92. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  93. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  94. }
  95. static inline u32 compute_dda_inc(unsigned int in, unsigned int out, bool v,
  96. unsigned int bpp)
  97. {
  98. fixed20_12 outf = dfixed_init(out);
  99. fixed20_12 inf = dfixed_init(in);
  100. u32 dda_inc;
  101. int max;
  102. if (v)
  103. max = 15;
  104. else {
  105. switch (bpp) {
  106. case 2:
  107. max = 8;
  108. break;
  109. default:
  110. WARN_ON_ONCE(1);
  111. /* fallthrough */
  112. case 4:
  113. max = 4;
  114. break;
  115. }
  116. }
  117. outf.full = max_t(u32, outf.full - dfixed_const(1), dfixed_const(1));
  118. inf.full -= dfixed_const(1);
  119. dda_inc = dfixed_div(inf, outf);
  120. dda_inc = min_t(u32, dda_inc, dfixed_const(max));
  121. return dda_inc;
  122. }
  123. static inline u32 compute_initial_dda(unsigned int in)
  124. {
  125. fixed20_12 inf = dfixed_init(in);
  126. return dfixed_frac(inf);
  127. }
  128. static void tegra_plane_setup_blending_legacy(struct tegra_plane *plane)
  129. {
  130. u32 background[3] = {
  131. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  132. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  133. BLEND_WEIGHT1(0) | BLEND_WEIGHT0(0) | BLEND_COLOR_KEY_NONE,
  134. };
  135. u32 foreground = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255) |
  136. BLEND_COLOR_KEY_NONE;
  137. u32 blendnokey = BLEND_WEIGHT1(255) | BLEND_WEIGHT0(255);
  138. struct tegra_plane_state *state;
  139. u32 blending[2];
  140. unsigned int i;
  141. /* disable blending for non-overlapping case */
  142. tegra_plane_writel(plane, blendnokey, DC_WIN_BLEND_NOKEY);
  143. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_1WIN);
  144. state = to_tegra_plane_state(plane->base.state);
  145. if (state->opaque) {
  146. /*
  147. * Since custom fix-weight blending isn't utilized and weight
  148. * of top window is set to max, we can enforce dependent
  149. * blending which in this case results in transparent bottom
  150. * window if top window is opaque and if top window enables
  151. * alpha blending, then bottom window is getting alpha value
  152. * of 1 minus the sum of alpha components of the overlapping
  153. * plane.
  154. */
  155. background[0] |= BLEND_CONTROL_DEPENDENT;
  156. background[1] |= BLEND_CONTROL_DEPENDENT;
  157. /*
  158. * The region where three windows overlap is the intersection
  159. * of the two regions where two windows overlap. It contributes
  160. * to the area if all of the windows on top of it have an alpha
  161. * component.
  162. */
  163. switch (state->base.normalized_zpos) {
  164. case 0:
  165. if (state->blending[0].alpha &&
  166. state->blending[1].alpha)
  167. background[2] |= BLEND_CONTROL_DEPENDENT;
  168. break;
  169. case 1:
  170. background[2] |= BLEND_CONTROL_DEPENDENT;
  171. break;
  172. }
  173. } else {
  174. /*
  175. * Enable alpha blending if pixel format has an alpha
  176. * component.
  177. */
  178. foreground |= BLEND_CONTROL_ALPHA;
  179. /*
  180. * If any of the windows on top of this window is opaque, it
  181. * will completely conceal this window within that area. If
  182. * top window has an alpha component, it is blended over the
  183. * bottom window.
  184. */
  185. for (i = 0; i < 2; i++) {
  186. if (state->blending[i].alpha &&
  187. state->blending[i].top)
  188. background[i] |= BLEND_CONTROL_DEPENDENT;
  189. }
  190. switch (state->base.normalized_zpos) {
  191. case 0:
  192. if (state->blending[0].alpha &&
  193. state->blending[1].alpha)
  194. background[2] |= BLEND_CONTROL_DEPENDENT;
  195. break;
  196. case 1:
  197. /*
  198. * When both middle and topmost windows have an alpha,
  199. * these windows a mixed together and then the result
  200. * is blended over the bottom window.
  201. */
  202. if (state->blending[0].alpha &&
  203. state->blending[0].top)
  204. background[2] |= BLEND_CONTROL_ALPHA;
  205. if (state->blending[1].alpha &&
  206. state->blending[1].top)
  207. background[2] |= BLEND_CONTROL_ALPHA;
  208. break;
  209. }
  210. }
  211. switch (state->base.normalized_zpos) {
  212. case 0:
  213. tegra_plane_writel(plane, background[0], DC_WIN_BLEND_2WIN_X);
  214. tegra_plane_writel(plane, background[1], DC_WIN_BLEND_2WIN_Y);
  215. tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
  216. break;
  217. case 1:
  218. /*
  219. * If window B / C is topmost, then X / Y registers are
  220. * matching the order of blending[...] state indices,
  221. * otherwise a swap is required.
  222. */
  223. if (!state->blending[0].top && state->blending[1].top) {
  224. blending[0] = foreground;
  225. blending[1] = background[1];
  226. } else {
  227. blending[0] = background[0];
  228. blending[1] = foreground;
  229. }
  230. tegra_plane_writel(plane, blending[0], DC_WIN_BLEND_2WIN_X);
  231. tegra_plane_writel(plane, blending[1], DC_WIN_BLEND_2WIN_Y);
  232. tegra_plane_writel(plane, background[2], DC_WIN_BLEND_3WIN_XY);
  233. break;
  234. case 2:
  235. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_X);
  236. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_2WIN_Y);
  237. tegra_plane_writel(plane, foreground, DC_WIN_BLEND_3WIN_XY);
  238. break;
  239. }
  240. }
  241. static void tegra_plane_setup_blending(struct tegra_plane *plane,
  242. const struct tegra_dc_window *window)
  243. {
  244. u32 value;
  245. value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
  246. BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
  247. BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
  248. tegra_plane_writel(plane, value, DC_WIN_BLEND_MATCH_SELECT);
  249. value = BLEND_FACTOR_DST_ALPHA_ZERO | BLEND_FACTOR_SRC_ALPHA_K2 |
  250. BLEND_FACTOR_DST_COLOR_NEG_K1_TIMES_SRC |
  251. BLEND_FACTOR_SRC_COLOR_K1_TIMES_SRC;
  252. tegra_plane_writel(plane, value, DC_WIN_BLEND_NOMATCH_SELECT);
  253. value = K2(255) | K1(255) | WINDOW_LAYER_DEPTH(255 - window->zpos);
  254. tegra_plane_writel(plane, value, DC_WIN_BLEND_LAYER_CONTROL);
  255. }
  256. static bool
  257. tegra_plane_use_horizontal_filtering(struct tegra_plane *plane,
  258. const struct tegra_dc_window *window)
  259. {
  260. struct tegra_dc *dc = plane->dc;
  261. if (window->src.w == window->dst.w)
  262. return false;
  263. if (plane->index == 0 && dc->soc->has_win_a_without_filters)
  264. return false;
  265. return true;
  266. }
  267. static bool
  268. tegra_plane_use_vertical_filtering(struct tegra_plane *plane,
  269. const struct tegra_dc_window *window)
  270. {
  271. struct tegra_dc *dc = plane->dc;
  272. if (window->src.h == window->dst.h)
  273. return false;
  274. if (plane->index == 0 && dc->soc->has_win_a_without_filters)
  275. return false;
  276. if (plane->index == 2 && dc->soc->has_win_c_without_vert_filter)
  277. return false;
  278. return true;
  279. }
  280. static void tegra_dc_setup_window(struct tegra_plane *plane,
  281. const struct tegra_dc_window *window)
  282. {
  283. unsigned h_offset, v_offset, h_size, v_size, h_dda, v_dda, bpp;
  284. struct tegra_dc *dc = plane->dc;
  285. bool yuv, planar;
  286. u32 value;
  287. /*
  288. * For YUV planar modes, the number of bytes per pixel takes into
  289. * account only the luma component and therefore is 1.
  290. */
  291. yuv = tegra_plane_format_is_yuv(window->format, &planar);
  292. if (!yuv)
  293. bpp = window->bits_per_pixel / 8;
  294. else
  295. bpp = planar ? 1 : 2;
  296. tegra_plane_writel(plane, window->format, DC_WIN_COLOR_DEPTH);
  297. tegra_plane_writel(plane, window->swap, DC_WIN_BYTE_SWAP);
  298. value = V_POSITION(window->dst.y) | H_POSITION(window->dst.x);
  299. tegra_plane_writel(plane, value, DC_WIN_POSITION);
  300. value = V_SIZE(window->dst.h) | H_SIZE(window->dst.w);
  301. tegra_plane_writel(plane, value, DC_WIN_SIZE);
  302. h_offset = window->src.x * bpp;
  303. v_offset = window->src.y;
  304. h_size = window->src.w * bpp;
  305. v_size = window->src.h;
  306. value = V_PRESCALED_SIZE(v_size) | H_PRESCALED_SIZE(h_size);
  307. tegra_plane_writel(plane, value, DC_WIN_PRESCALED_SIZE);
  308. /*
  309. * For DDA computations the number of bytes per pixel for YUV planar
  310. * modes needs to take into account all Y, U and V components.
  311. */
  312. if (yuv && planar)
  313. bpp = 2;
  314. h_dda = compute_dda_inc(window->src.w, window->dst.w, false, bpp);
  315. v_dda = compute_dda_inc(window->src.h, window->dst.h, true, bpp);
  316. value = V_DDA_INC(v_dda) | H_DDA_INC(h_dda);
  317. tegra_plane_writel(plane, value, DC_WIN_DDA_INC);
  318. h_dda = compute_initial_dda(window->src.x);
  319. v_dda = compute_initial_dda(window->src.y);
  320. tegra_plane_writel(plane, h_dda, DC_WIN_H_INITIAL_DDA);
  321. tegra_plane_writel(plane, v_dda, DC_WIN_V_INITIAL_DDA);
  322. tegra_plane_writel(plane, 0, DC_WIN_UV_BUF_STRIDE);
  323. tegra_plane_writel(plane, 0, DC_WIN_BUF_STRIDE);
  324. tegra_plane_writel(plane, window->base[0], DC_WINBUF_START_ADDR);
  325. if (yuv && planar) {
  326. tegra_plane_writel(plane, window->base[1], DC_WINBUF_START_ADDR_U);
  327. tegra_plane_writel(plane, window->base[2], DC_WINBUF_START_ADDR_V);
  328. value = window->stride[1] << 16 | window->stride[0];
  329. tegra_plane_writel(plane, value, DC_WIN_LINE_STRIDE);
  330. } else {
  331. tegra_plane_writel(plane, window->stride[0], DC_WIN_LINE_STRIDE);
  332. }
  333. if (window->bottom_up)
  334. v_offset += window->src.h - 1;
  335. tegra_plane_writel(plane, h_offset, DC_WINBUF_ADDR_H_OFFSET);
  336. tegra_plane_writel(plane, v_offset, DC_WINBUF_ADDR_V_OFFSET);
  337. if (dc->soc->supports_block_linear) {
  338. unsigned long height = window->tiling.value;
  339. switch (window->tiling.mode) {
  340. case TEGRA_BO_TILING_MODE_PITCH:
  341. value = DC_WINBUF_SURFACE_KIND_PITCH;
  342. break;
  343. case TEGRA_BO_TILING_MODE_TILED:
  344. value = DC_WINBUF_SURFACE_KIND_TILED;
  345. break;
  346. case TEGRA_BO_TILING_MODE_BLOCK:
  347. value = DC_WINBUF_SURFACE_KIND_BLOCK_HEIGHT(height) |
  348. DC_WINBUF_SURFACE_KIND_BLOCK;
  349. break;
  350. }
  351. tegra_plane_writel(plane, value, DC_WINBUF_SURFACE_KIND);
  352. } else {
  353. switch (window->tiling.mode) {
  354. case TEGRA_BO_TILING_MODE_PITCH:
  355. value = DC_WIN_BUFFER_ADDR_MODE_LINEAR_UV |
  356. DC_WIN_BUFFER_ADDR_MODE_LINEAR;
  357. break;
  358. case TEGRA_BO_TILING_MODE_TILED:
  359. value = DC_WIN_BUFFER_ADDR_MODE_TILE_UV |
  360. DC_WIN_BUFFER_ADDR_MODE_TILE;
  361. break;
  362. case TEGRA_BO_TILING_MODE_BLOCK:
  363. /*
  364. * No need to handle this here because ->atomic_check
  365. * will already have filtered it out.
  366. */
  367. break;
  368. }
  369. tegra_plane_writel(plane, value, DC_WIN_BUFFER_ADDR_MODE);
  370. }
  371. value = WIN_ENABLE;
  372. if (yuv) {
  373. /* setup default colorspace conversion coefficients */
  374. tegra_plane_writel(plane, 0x00f0, DC_WIN_CSC_YOF);
  375. tegra_plane_writel(plane, 0x012a, DC_WIN_CSC_KYRGB);
  376. tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KUR);
  377. tegra_plane_writel(plane, 0x0198, DC_WIN_CSC_KVR);
  378. tegra_plane_writel(plane, 0x039b, DC_WIN_CSC_KUG);
  379. tegra_plane_writel(plane, 0x032f, DC_WIN_CSC_KVG);
  380. tegra_plane_writel(plane, 0x0204, DC_WIN_CSC_KUB);
  381. tegra_plane_writel(plane, 0x0000, DC_WIN_CSC_KVB);
  382. value |= CSC_ENABLE;
  383. } else if (window->bits_per_pixel < 24) {
  384. value |= COLOR_EXPAND;
  385. }
  386. if (window->bottom_up)
  387. value |= V_DIRECTION;
  388. if (tegra_plane_use_horizontal_filtering(plane, window)) {
  389. /*
  390. * Enable horizontal 6-tap filter and set filtering
  391. * coefficients to the default values defined in TRM.
  392. */
  393. tegra_plane_writel(plane, 0x00008000, DC_WIN_H_FILTER_P(0));
  394. tegra_plane_writel(plane, 0x3e087ce1, DC_WIN_H_FILTER_P(1));
  395. tegra_plane_writel(plane, 0x3b117ac1, DC_WIN_H_FILTER_P(2));
  396. tegra_plane_writel(plane, 0x591b73aa, DC_WIN_H_FILTER_P(3));
  397. tegra_plane_writel(plane, 0x57256d9a, DC_WIN_H_FILTER_P(4));
  398. tegra_plane_writel(plane, 0x552f668b, DC_WIN_H_FILTER_P(5));
  399. tegra_plane_writel(plane, 0x73385e8b, DC_WIN_H_FILTER_P(6));
  400. tegra_plane_writel(plane, 0x72435583, DC_WIN_H_FILTER_P(7));
  401. tegra_plane_writel(plane, 0x714c4c8b, DC_WIN_H_FILTER_P(8));
  402. tegra_plane_writel(plane, 0x70554393, DC_WIN_H_FILTER_P(9));
  403. tegra_plane_writel(plane, 0x715e389b, DC_WIN_H_FILTER_P(10));
  404. tegra_plane_writel(plane, 0x71662faa, DC_WIN_H_FILTER_P(11));
  405. tegra_plane_writel(plane, 0x536d25ba, DC_WIN_H_FILTER_P(12));
  406. tegra_plane_writel(plane, 0x55731bca, DC_WIN_H_FILTER_P(13));
  407. tegra_plane_writel(plane, 0x387a11d9, DC_WIN_H_FILTER_P(14));
  408. tegra_plane_writel(plane, 0x3c7c08f1, DC_WIN_H_FILTER_P(15));
  409. value |= H_FILTER;
  410. }
  411. if (tegra_plane_use_vertical_filtering(plane, window)) {
  412. unsigned int i, k;
  413. /*
  414. * Enable vertical 2-tap filter and set filtering
  415. * coefficients to the default values defined in TRM.
  416. */
  417. for (i = 0, k = 128; i < 16; i++, k -= 8)
  418. tegra_plane_writel(plane, k, DC_WIN_V_FILTER_P(i));
  419. value |= V_FILTER;
  420. }
  421. tegra_plane_writel(plane, value, DC_WIN_WIN_OPTIONS);
  422. if (dc->soc->has_legacy_blending)
  423. tegra_plane_setup_blending_legacy(plane);
  424. else
  425. tegra_plane_setup_blending(plane, window);
  426. }
  427. static const u32 tegra20_primary_formats[] = {
  428. DRM_FORMAT_ARGB4444,
  429. DRM_FORMAT_ARGB1555,
  430. DRM_FORMAT_RGB565,
  431. DRM_FORMAT_RGBA5551,
  432. DRM_FORMAT_ABGR8888,
  433. DRM_FORMAT_ARGB8888,
  434. /* non-native formats */
  435. DRM_FORMAT_XRGB1555,
  436. DRM_FORMAT_RGBX5551,
  437. DRM_FORMAT_XBGR8888,
  438. DRM_FORMAT_XRGB8888,
  439. };
  440. static const u64 tegra20_modifiers[] = {
  441. DRM_FORMAT_MOD_LINEAR,
  442. DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED,
  443. DRM_FORMAT_MOD_INVALID
  444. };
  445. static const u32 tegra114_primary_formats[] = {
  446. DRM_FORMAT_ARGB4444,
  447. DRM_FORMAT_ARGB1555,
  448. DRM_FORMAT_RGB565,
  449. DRM_FORMAT_RGBA5551,
  450. DRM_FORMAT_ABGR8888,
  451. DRM_FORMAT_ARGB8888,
  452. /* new on Tegra114 */
  453. DRM_FORMAT_ABGR4444,
  454. DRM_FORMAT_ABGR1555,
  455. DRM_FORMAT_BGRA5551,
  456. DRM_FORMAT_XRGB1555,
  457. DRM_FORMAT_RGBX5551,
  458. DRM_FORMAT_XBGR1555,
  459. DRM_FORMAT_BGRX5551,
  460. DRM_FORMAT_BGR565,
  461. DRM_FORMAT_BGRA8888,
  462. DRM_FORMAT_RGBA8888,
  463. DRM_FORMAT_XRGB8888,
  464. DRM_FORMAT_XBGR8888,
  465. };
  466. static const u32 tegra124_primary_formats[] = {
  467. DRM_FORMAT_ARGB4444,
  468. DRM_FORMAT_ARGB1555,
  469. DRM_FORMAT_RGB565,
  470. DRM_FORMAT_RGBA5551,
  471. DRM_FORMAT_ABGR8888,
  472. DRM_FORMAT_ARGB8888,
  473. /* new on Tegra114 */
  474. DRM_FORMAT_ABGR4444,
  475. DRM_FORMAT_ABGR1555,
  476. DRM_FORMAT_BGRA5551,
  477. DRM_FORMAT_XRGB1555,
  478. DRM_FORMAT_RGBX5551,
  479. DRM_FORMAT_XBGR1555,
  480. DRM_FORMAT_BGRX5551,
  481. DRM_FORMAT_BGR565,
  482. DRM_FORMAT_BGRA8888,
  483. DRM_FORMAT_RGBA8888,
  484. DRM_FORMAT_XRGB8888,
  485. DRM_FORMAT_XBGR8888,
  486. /* new on Tegra124 */
  487. DRM_FORMAT_RGBX8888,
  488. DRM_FORMAT_BGRX8888,
  489. };
  490. static const u64 tegra124_modifiers[] = {
  491. DRM_FORMAT_MOD_LINEAR,
  492. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0),
  493. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1),
  494. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2),
  495. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3),
  496. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4),
  497. DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5),
  498. DRM_FORMAT_MOD_INVALID
  499. };
  500. static int tegra_plane_atomic_check(struct drm_plane *plane,
  501. struct drm_plane_state *state)
  502. {
  503. struct tegra_plane_state *plane_state = to_tegra_plane_state(state);
  504. unsigned int rotation = DRM_MODE_ROTATE_0 | DRM_MODE_REFLECT_Y;
  505. struct tegra_bo_tiling *tiling = &plane_state->tiling;
  506. struct tegra_plane *tegra = to_tegra_plane(plane);
  507. struct tegra_dc *dc = to_tegra_dc(state->crtc);
  508. int err;
  509. /* no need for further checks if the plane is being disabled */
  510. if (!state->crtc)
  511. return 0;
  512. err = tegra_plane_format(state->fb->format->format,
  513. &plane_state->format,
  514. &plane_state->swap);
  515. if (err < 0)
  516. return err;
  517. /*
  518. * Tegra20 and Tegra30 are special cases here because they support
  519. * only variants of specific formats with an alpha component, but not
  520. * the corresponding opaque formats. However, the opaque formats can
  521. * be emulated by disabling alpha blending for the plane.
  522. */
  523. if (dc->soc->has_legacy_blending) {
  524. err = tegra_plane_setup_legacy_state(tegra, plane_state);
  525. if (err < 0)
  526. return err;
  527. }
  528. err = tegra_fb_get_tiling(state->fb, tiling);
  529. if (err < 0)
  530. return err;
  531. if (tiling->mode == TEGRA_BO_TILING_MODE_BLOCK &&
  532. !dc->soc->supports_block_linear) {
  533. DRM_ERROR("hardware doesn't support block linear mode\n");
  534. return -EINVAL;
  535. }
  536. rotation = drm_rotation_simplify(state->rotation, rotation);
  537. if (rotation & DRM_MODE_REFLECT_Y)
  538. plane_state->bottom_up = true;
  539. else
  540. plane_state->bottom_up = false;
  541. /*
  542. * Tegra doesn't support different strides for U and V planes so we
  543. * error out if the user tries to display a framebuffer with such a
  544. * configuration.
  545. */
  546. if (state->fb->format->num_planes > 2) {
  547. if (state->fb->pitches[2] != state->fb->pitches[1]) {
  548. DRM_ERROR("unsupported UV-plane configuration\n");
  549. return -EINVAL;
  550. }
  551. }
  552. err = tegra_plane_state_add(tegra, state);
  553. if (err < 0)
  554. return err;
  555. return 0;
  556. }
  557. static void tegra_plane_atomic_disable(struct drm_plane *plane,
  558. struct drm_plane_state *old_state)
  559. {
  560. struct tegra_plane *p = to_tegra_plane(plane);
  561. u32 value;
  562. /* rien ne va plus */
  563. if (!old_state || !old_state->crtc)
  564. return;
  565. value = tegra_plane_readl(p, DC_WIN_WIN_OPTIONS);
  566. value &= ~WIN_ENABLE;
  567. tegra_plane_writel(p, value, DC_WIN_WIN_OPTIONS);
  568. }
  569. static void tegra_plane_atomic_update(struct drm_plane *plane,
  570. struct drm_plane_state *old_state)
  571. {
  572. struct tegra_plane_state *state = to_tegra_plane_state(plane->state);
  573. struct drm_framebuffer *fb = plane->state->fb;
  574. struct tegra_plane *p = to_tegra_plane(plane);
  575. struct tegra_dc_window window;
  576. unsigned int i;
  577. /* rien ne va plus */
  578. if (!plane->state->crtc || !plane->state->fb)
  579. return;
  580. if (!plane->state->visible)
  581. return tegra_plane_atomic_disable(plane, old_state);
  582. memset(&window, 0, sizeof(window));
  583. window.src.x = plane->state->src.x1 >> 16;
  584. window.src.y = plane->state->src.y1 >> 16;
  585. window.src.w = drm_rect_width(&plane->state->src) >> 16;
  586. window.src.h = drm_rect_height(&plane->state->src) >> 16;
  587. window.dst.x = plane->state->dst.x1;
  588. window.dst.y = plane->state->dst.y1;
  589. window.dst.w = drm_rect_width(&plane->state->dst);
  590. window.dst.h = drm_rect_height(&plane->state->dst);
  591. window.bits_per_pixel = fb->format->cpp[0] * 8;
  592. window.bottom_up = tegra_fb_is_bottom_up(fb) || state->bottom_up;
  593. /* copy from state */
  594. window.zpos = plane->state->normalized_zpos;
  595. window.tiling = state->tiling;
  596. window.format = state->format;
  597. window.swap = state->swap;
  598. for (i = 0; i < fb->format->num_planes; i++) {
  599. struct tegra_bo *bo = tegra_fb_get_plane(fb, i);
  600. window.base[i] = bo->paddr + fb->offsets[i];
  601. /*
  602. * Tegra uses a shared stride for UV planes. Framebuffers are
  603. * already checked for this in the tegra_plane_atomic_check()
  604. * function, so it's safe to ignore the V-plane pitch here.
  605. */
  606. if (i < 2)
  607. window.stride[i] = fb->pitches[i];
  608. }
  609. tegra_dc_setup_window(p, &window);
  610. }
  611. static const struct drm_plane_helper_funcs tegra_plane_helper_funcs = {
  612. .atomic_check = tegra_plane_atomic_check,
  613. .atomic_disable = tegra_plane_atomic_disable,
  614. .atomic_update = tegra_plane_atomic_update,
  615. };
  616. static unsigned long tegra_plane_get_possible_crtcs(struct drm_device *drm)
  617. {
  618. /*
  619. * Ideally this would use drm_crtc_mask(), but that would require the
  620. * CRTC to already be in the mode_config's list of CRTCs. However, it
  621. * will only be added to that list in the drm_crtc_init_with_planes()
  622. * (in tegra_dc_init()), which in turn requires registration of these
  623. * planes. So we have ourselves a nice little chicken and egg problem
  624. * here.
  625. *
  626. * We work around this by manually creating the mask from the number
  627. * of CRTCs that have been registered, and should therefore always be
  628. * the same as drm_crtc_index() after registration.
  629. */
  630. return 1 << drm->mode_config.num_crtc;
  631. }
  632. static struct drm_plane *tegra_primary_plane_create(struct drm_device *drm,
  633. struct tegra_dc *dc)
  634. {
  635. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  636. enum drm_plane_type type = DRM_PLANE_TYPE_PRIMARY;
  637. struct tegra_plane *plane;
  638. unsigned int num_formats;
  639. const u64 *modifiers;
  640. const u32 *formats;
  641. int err;
  642. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  643. if (!plane)
  644. return ERR_PTR(-ENOMEM);
  645. /* Always use window A as primary window */
  646. plane->offset = 0xa00;
  647. plane->index = 0;
  648. plane->dc = dc;
  649. num_formats = dc->soc->num_primary_formats;
  650. formats = dc->soc->primary_formats;
  651. modifiers = dc->soc->modifiers;
  652. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  653. &tegra_plane_funcs, formats,
  654. num_formats, modifiers, type, NULL);
  655. if (err < 0) {
  656. kfree(plane);
  657. return ERR_PTR(err);
  658. }
  659. drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
  660. drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
  661. err = drm_plane_create_rotation_property(&plane->base,
  662. DRM_MODE_ROTATE_0,
  663. DRM_MODE_ROTATE_0 |
  664. DRM_MODE_REFLECT_Y);
  665. if (err < 0)
  666. dev_err(dc->dev, "failed to create rotation property: %d\n",
  667. err);
  668. return &plane->base;
  669. }
  670. static const u32 tegra_cursor_plane_formats[] = {
  671. DRM_FORMAT_RGBA8888,
  672. };
  673. static int tegra_cursor_atomic_check(struct drm_plane *plane,
  674. struct drm_plane_state *state)
  675. {
  676. struct tegra_plane *tegra = to_tegra_plane(plane);
  677. int err;
  678. /* no need for further checks if the plane is being disabled */
  679. if (!state->crtc)
  680. return 0;
  681. /* scaling not supported for cursor */
  682. if ((state->src_w >> 16 != state->crtc_w) ||
  683. (state->src_h >> 16 != state->crtc_h))
  684. return -EINVAL;
  685. /* only square cursors supported */
  686. if (state->src_w != state->src_h)
  687. return -EINVAL;
  688. if (state->crtc_w != 32 && state->crtc_w != 64 &&
  689. state->crtc_w != 128 && state->crtc_w != 256)
  690. return -EINVAL;
  691. err = tegra_plane_state_add(tegra, state);
  692. if (err < 0)
  693. return err;
  694. return 0;
  695. }
  696. static void tegra_cursor_atomic_update(struct drm_plane *plane,
  697. struct drm_plane_state *old_state)
  698. {
  699. struct tegra_bo *bo = tegra_fb_get_plane(plane->state->fb, 0);
  700. struct tegra_dc *dc = to_tegra_dc(plane->state->crtc);
  701. struct drm_plane_state *state = plane->state;
  702. u32 value = CURSOR_CLIP_DISPLAY;
  703. /* rien ne va plus */
  704. if (!plane->state->crtc || !plane->state->fb)
  705. return;
  706. switch (state->crtc_w) {
  707. case 32:
  708. value |= CURSOR_SIZE_32x32;
  709. break;
  710. case 64:
  711. value |= CURSOR_SIZE_64x64;
  712. break;
  713. case 128:
  714. value |= CURSOR_SIZE_128x128;
  715. break;
  716. case 256:
  717. value |= CURSOR_SIZE_256x256;
  718. break;
  719. default:
  720. WARN(1, "cursor size %ux%u not supported\n", state->crtc_w,
  721. state->crtc_h);
  722. return;
  723. }
  724. value |= (bo->paddr >> 10) & 0x3fffff;
  725. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR);
  726. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  727. value = (bo->paddr >> 32) & 0x3;
  728. tegra_dc_writel(dc, value, DC_DISP_CURSOR_START_ADDR_HI);
  729. #endif
  730. /* enable cursor and set blend mode */
  731. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  732. value |= CURSOR_ENABLE;
  733. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  734. value = tegra_dc_readl(dc, DC_DISP_BLEND_CURSOR_CONTROL);
  735. value &= ~CURSOR_DST_BLEND_MASK;
  736. value &= ~CURSOR_SRC_BLEND_MASK;
  737. value |= CURSOR_MODE_NORMAL;
  738. value |= CURSOR_DST_BLEND_NEG_K1_TIMES_SRC;
  739. value |= CURSOR_SRC_BLEND_K1_TIMES_SRC;
  740. value |= CURSOR_ALPHA;
  741. tegra_dc_writel(dc, value, DC_DISP_BLEND_CURSOR_CONTROL);
  742. /* position the cursor */
  743. value = (state->crtc_y & 0x3fff) << 16 | (state->crtc_x & 0x3fff);
  744. tegra_dc_writel(dc, value, DC_DISP_CURSOR_POSITION);
  745. }
  746. static void tegra_cursor_atomic_disable(struct drm_plane *plane,
  747. struct drm_plane_state *old_state)
  748. {
  749. struct tegra_dc *dc;
  750. u32 value;
  751. /* rien ne va plus */
  752. if (!old_state || !old_state->crtc)
  753. return;
  754. dc = to_tegra_dc(old_state->crtc);
  755. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  756. value &= ~CURSOR_ENABLE;
  757. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  758. }
  759. static const struct drm_plane_helper_funcs tegra_cursor_plane_helper_funcs = {
  760. .atomic_check = tegra_cursor_atomic_check,
  761. .atomic_update = tegra_cursor_atomic_update,
  762. .atomic_disable = tegra_cursor_atomic_disable,
  763. };
  764. static struct drm_plane *tegra_dc_cursor_plane_create(struct drm_device *drm,
  765. struct tegra_dc *dc)
  766. {
  767. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  768. struct tegra_plane *plane;
  769. unsigned int num_formats;
  770. const u32 *formats;
  771. int err;
  772. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  773. if (!plane)
  774. return ERR_PTR(-ENOMEM);
  775. /*
  776. * This index is kind of fake. The cursor isn't a regular plane, but
  777. * its update and activation request bits in DC_CMD_STATE_CONTROL do
  778. * use the same programming. Setting this fake index here allows the
  779. * code in tegra_add_plane_state() to do the right thing without the
  780. * need to special-casing the cursor plane.
  781. */
  782. plane->index = 6;
  783. plane->dc = dc;
  784. num_formats = ARRAY_SIZE(tegra_cursor_plane_formats);
  785. formats = tegra_cursor_plane_formats;
  786. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  787. &tegra_plane_funcs, formats,
  788. num_formats, NULL,
  789. DRM_PLANE_TYPE_CURSOR, NULL);
  790. if (err < 0) {
  791. kfree(plane);
  792. return ERR_PTR(err);
  793. }
  794. drm_plane_helper_add(&plane->base, &tegra_cursor_plane_helper_funcs);
  795. return &plane->base;
  796. }
  797. static const u32 tegra20_overlay_formats[] = {
  798. DRM_FORMAT_ARGB4444,
  799. DRM_FORMAT_ARGB1555,
  800. DRM_FORMAT_RGB565,
  801. DRM_FORMAT_RGBA5551,
  802. DRM_FORMAT_ABGR8888,
  803. DRM_FORMAT_ARGB8888,
  804. /* non-native formats */
  805. DRM_FORMAT_XRGB1555,
  806. DRM_FORMAT_RGBX5551,
  807. DRM_FORMAT_XBGR8888,
  808. DRM_FORMAT_XRGB8888,
  809. /* planar formats */
  810. DRM_FORMAT_UYVY,
  811. DRM_FORMAT_YUYV,
  812. DRM_FORMAT_YUV420,
  813. DRM_FORMAT_YUV422,
  814. };
  815. static const u32 tegra114_overlay_formats[] = {
  816. DRM_FORMAT_ARGB4444,
  817. DRM_FORMAT_ARGB1555,
  818. DRM_FORMAT_RGB565,
  819. DRM_FORMAT_RGBA5551,
  820. DRM_FORMAT_ABGR8888,
  821. DRM_FORMAT_ARGB8888,
  822. /* new on Tegra114 */
  823. DRM_FORMAT_ABGR4444,
  824. DRM_FORMAT_ABGR1555,
  825. DRM_FORMAT_BGRA5551,
  826. DRM_FORMAT_XRGB1555,
  827. DRM_FORMAT_RGBX5551,
  828. DRM_FORMAT_XBGR1555,
  829. DRM_FORMAT_BGRX5551,
  830. DRM_FORMAT_BGR565,
  831. DRM_FORMAT_BGRA8888,
  832. DRM_FORMAT_RGBA8888,
  833. DRM_FORMAT_XRGB8888,
  834. DRM_FORMAT_XBGR8888,
  835. /* planar formats */
  836. DRM_FORMAT_UYVY,
  837. DRM_FORMAT_YUYV,
  838. DRM_FORMAT_YUV420,
  839. DRM_FORMAT_YUV422,
  840. };
  841. static const u32 tegra124_overlay_formats[] = {
  842. DRM_FORMAT_ARGB4444,
  843. DRM_FORMAT_ARGB1555,
  844. DRM_FORMAT_RGB565,
  845. DRM_FORMAT_RGBA5551,
  846. DRM_FORMAT_ABGR8888,
  847. DRM_FORMAT_ARGB8888,
  848. /* new on Tegra114 */
  849. DRM_FORMAT_ABGR4444,
  850. DRM_FORMAT_ABGR1555,
  851. DRM_FORMAT_BGRA5551,
  852. DRM_FORMAT_XRGB1555,
  853. DRM_FORMAT_RGBX5551,
  854. DRM_FORMAT_XBGR1555,
  855. DRM_FORMAT_BGRX5551,
  856. DRM_FORMAT_BGR565,
  857. DRM_FORMAT_BGRA8888,
  858. DRM_FORMAT_RGBA8888,
  859. DRM_FORMAT_XRGB8888,
  860. DRM_FORMAT_XBGR8888,
  861. /* new on Tegra124 */
  862. DRM_FORMAT_RGBX8888,
  863. DRM_FORMAT_BGRX8888,
  864. /* planar formats */
  865. DRM_FORMAT_UYVY,
  866. DRM_FORMAT_YUYV,
  867. DRM_FORMAT_YUV420,
  868. DRM_FORMAT_YUV422,
  869. };
  870. static struct drm_plane *tegra_dc_overlay_plane_create(struct drm_device *drm,
  871. struct tegra_dc *dc,
  872. unsigned int index,
  873. bool cursor)
  874. {
  875. unsigned long possible_crtcs = tegra_plane_get_possible_crtcs(drm);
  876. struct tegra_plane *plane;
  877. unsigned int num_formats;
  878. enum drm_plane_type type;
  879. const u32 *formats;
  880. int err;
  881. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  882. if (!plane)
  883. return ERR_PTR(-ENOMEM);
  884. plane->offset = 0xa00 + 0x200 * index;
  885. plane->index = index;
  886. plane->dc = dc;
  887. num_formats = dc->soc->num_overlay_formats;
  888. formats = dc->soc->overlay_formats;
  889. if (!cursor)
  890. type = DRM_PLANE_TYPE_OVERLAY;
  891. else
  892. type = DRM_PLANE_TYPE_CURSOR;
  893. err = drm_universal_plane_init(drm, &plane->base, possible_crtcs,
  894. &tegra_plane_funcs, formats,
  895. num_formats, NULL, type, NULL);
  896. if (err < 0) {
  897. kfree(plane);
  898. return ERR_PTR(err);
  899. }
  900. drm_plane_helper_add(&plane->base, &tegra_plane_helper_funcs);
  901. drm_plane_create_zpos_property(&plane->base, plane->index, 0, 255);
  902. err = drm_plane_create_rotation_property(&plane->base,
  903. DRM_MODE_ROTATE_0,
  904. DRM_MODE_ROTATE_0 |
  905. DRM_MODE_REFLECT_Y);
  906. if (err < 0)
  907. dev_err(dc->dev, "failed to create rotation property: %d\n",
  908. err);
  909. return &plane->base;
  910. }
  911. static struct drm_plane *tegra_dc_add_shared_planes(struct drm_device *drm,
  912. struct tegra_dc *dc)
  913. {
  914. struct drm_plane *plane, *primary = NULL;
  915. unsigned int i, j;
  916. for (i = 0; i < dc->soc->num_wgrps; i++) {
  917. const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
  918. if (wgrp->dc == dc->pipe) {
  919. for (j = 0; j < wgrp->num_windows; j++) {
  920. unsigned int index = wgrp->windows[j];
  921. plane = tegra_shared_plane_create(drm, dc,
  922. wgrp->index,
  923. index);
  924. if (IS_ERR(plane))
  925. return plane;
  926. /*
  927. * Choose the first shared plane owned by this
  928. * head as the primary plane.
  929. */
  930. if (!primary) {
  931. plane->type = DRM_PLANE_TYPE_PRIMARY;
  932. primary = plane;
  933. }
  934. }
  935. }
  936. }
  937. return primary;
  938. }
  939. static struct drm_plane *tegra_dc_add_planes(struct drm_device *drm,
  940. struct tegra_dc *dc)
  941. {
  942. struct drm_plane *planes[2], *primary;
  943. unsigned int planes_num;
  944. unsigned int i;
  945. int err;
  946. primary = tegra_primary_plane_create(drm, dc);
  947. if (IS_ERR(primary))
  948. return primary;
  949. if (dc->soc->supports_cursor)
  950. planes_num = 2;
  951. else
  952. planes_num = 1;
  953. for (i = 0; i < planes_num; i++) {
  954. planes[i] = tegra_dc_overlay_plane_create(drm, dc, 1 + i,
  955. false);
  956. if (IS_ERR(planes[i])) {
  957. err = PTR_ERR(planes[i]);
  958. while (i--)
  959. tegra_plane_funcs.destroy(planes[i]);
  960. tegra_plane_funcs.destroy(primary);
  961. return ERR_PTR(err);
  962. }
  963. }
  964. return primary;
  965. }
  966. static void tegra_dc_destroy(struct drm_crtc *crtc)
  967. {
  968. drm_crtc_cleanup(crtc);
  969. }
  970. static void tegra_crtc_reset(struct drm_crtc *crtc)
  971. {
  972. struct tegra_dc_state *state;
  973. if (crtc->state)
  974. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  975. kfree(crtc->state);
  976. crtc->state = NULL;
  977. state = kzalloc(sizeof(*state), GFP_KERNEL);
  978. if (state) {
  979. crtc->state = &state->base;
  980. crtc->state->crtc = crtc;
  981. }
  982. drm_crtc_vblank_reset(crtc);
  983. }
  984. static struct drm_crtc_state *
  985. tegra_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
  986. {
  987. struct tegra_dc_state *state = to_dc_state(crtc->state);
  988. struct tegra_dc_state *copy;
  989. copy = kmalloc(sizeof(*copy), GFP_KERNEL);
  990. if (!copy)
  991. return NULL;
  992. __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->base);
  993. copy->clk = state->clk;
  994. copy->pclk = state->pclk;
  995. copy->div = state->div;
  996. copy->planes = state->planes;
  997. return &copy->base;
  998. }
  999. static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
  1000. struct drm_crtc_state *state)
  1001. {
  1002. __drm_atomic_helper_crtc_destroy_state(state);
  1003. kfree(state);
  1004. }
  1005. #define DEBUGFS_REG32(_name) { .name = #_name, .offset = _name }
  1006. static const struct debugfs_reg32 tegra_dc_regs[] = {
  1007. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT),
  1008. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_CNTRL),
  1009. DEBUGFS_REG32(DC_CMD_GENERAL_INCR_SYNCPT_ERROR),
  1010. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT),
  1011. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_CNTRL),
  1012. DEBUGFS_REG32(DC_CMD_WIN_A_INCR_SYNCPT_ERROR),
  1013. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT),
  1014. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_CNTRL),
  1015. DEBUGFS_REG32(DC_CMD_WIN_B_INCR_SYNCPT_ERROR),
  1016. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT),
  1017. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_CNTRL),
  1018. DEBUGFS_REG32(DC_CMD_WIN_C_INCR_SYNCPT_ERROR),
  1019. DEBUGFS_REG32(DC_CMD_CONT_SYNCPT_VSYNC),
  1020. DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND_OPTION0),
  1021. DEBUGFS_REG32(DC_CMD_DISPLAY_COMMAND),
  1022. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE),
  1023. DEBUGFS_REG32(DC_CMD_DISPLAY_POWER_CONTROL),
  1024. DEBUGFS_REG32(DC_CMD_INT_STATUS),
  1025. DEBUGFS_REG32(DC_CMD_INT_MASK),
  1026. DEBUGFS_REG32(DC_CMD_INT_ENABLE),
  1027. DEBUGFS_REG32(DC_CMD_INT_TYPE),
  1028. DEBUGFS_REG32(DC_CMD_INT_POLARITY),
  1029. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE1),
  1030. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE2),
  1031. DEBUGFS_REG32(DC_CMD_SIGNAL_RAISE3),
  1032. DEBUGFS_REG32(DC_CMD_STATE_ACCESS),
  1033. DEBUGFS_REG32(DC_CMD_STATE_CONTROL),
  1034. DEBUGFS_REG32(DC_CMD_DISPLAY_WINDOW_HEADER),
  1035. DEBUGFS_REG32(DC_CMD_REG_ACT_CONTROL),
  1036. DEBUGFS_REG32(DC_COM_CRC_CONTROL),
  1037. DEBUGFS_REG32(DC_COM_CRC_CHECKSUM),
  1038. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(0)),
  1039. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(1)),
  1040. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(2)),
  1041. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_ENABLE(3)),
  1042. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(0)),
  1043. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(1)),
  1044. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(2)),
  1045. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_POLARITY(3)),
  1046. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(0)),
  1047. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(1)),
  1048. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(2)),
  1049. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_DATA(3)),
  1050. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(0)),
  1051. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(1)),
  1052. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(2)),
  1053. DEBUGFS_REG32(DC_COM_PIN_INPUT_ENABLE(3)),
  1054. DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(0)),
  1055. DEBUGFS_REG32(DC_COM_PIN_INPUT_DATA(1)),
  1056. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(0)),
  1057. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(1)),
  1058. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(2)),
  1059. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(3)),
  1060. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(4)),
  1061. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(5)),
  1062. DEBUGFS_REG32(DC_COM_PIN_OUTPUT_SELECT(6)),
  1063. DEBUGFS_REG32(DC_COM_PIN_MISC_CONTROL),
  1064. DEBUGFS_REG32(DC_COM_PIN_PM0_CONTROL),
  1065. DEBUGFS_REG32(DC_COM_PIN_PM0_DUTY_CYCLE),
  1066. DEBUGFS_REG32(DC_COM_PIN_PM1_CONTROL),
  1067. DEBUGFS_REG32(DC_COM_PIN_PM1_DUTY_CYCLE),
  1068. DEBUGFS_REG32(DC_COM_SPI_CONTROL),
  1069. DEBUGFS_REG32(DC_COM_SPI_START_BYTE),
  1070. DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_AB),
  1071. DEBUGFS_REG32(DC_COM_HSPI_WRITE_DATA_CD),
  1072. DEBUGFS_REG32(DC_COM_HSPI_CS_DC),
  1073. DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_A),
  1074. DEBUGFS_REG32(DC_COM_SCRATCH_REGISTER_B),
  1075. DEBUGFS_REG32(DC_COM_GPIO_CTRL),
  1076. DEBUGFS_REG32(DC_COM_GPIO_DEBOUNCE_COUNTER),
  1077. DEBUGFS_REG32(DC_COM_CRC_CHECKSUM_LATCHED),
  1078. DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS0),
  1079. DEBUGFS_REG32(DC_DISP_DISP_SIGNAL_OPTIONS1),
  1080. DEBUGFS_REG32(DC_DISP_DISP_WIN_OPTIONS),
  1081. DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY),
  1082. DEBUGFS_REG32(DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER),
  1083. DEBUGFS_REG32(DC_DISP_DISP_TIMING_OPTIONS),
  1084. DEBUGFS_REG32(DC_DISP_REF_TO_SYNC),
  1085. DEBUGFS_REG32(DC_DISP_SYNC_WIDTH),
  1086. DEBUGFS_REG32(DC_DISP_BACK_PORCH),
  1087. DEBUGFS_REG32(DC_DISP_ACTIVE),
  1088. DEBUGFS_REG32(DC_DISP_FRONT_PORCH),
  1089. DEBUGFS_REG32(DC_DISP_H_PULSE0_CONTROL),
  1090. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_A),
  1091. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_B),
  1092. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_C),
  1093. DEBUGFS_REG32(DC_DISP_H_PULSE0_POSITION_D),
  1094. DEBUGFS_REG32(DC_DISP_H_PULSE1_CONTROL),
  1095. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_A),
  1096. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_B),
  1097. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_C),
  1098. DEBUGFS_REG32(DC_DISP_H_PULSE1_POSITION_D),
  1099. DEBUGFS_REG32(DC_DISP_H_PULSE2_CONTROL),
  1100. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_A),
  1101. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_B),
  1102. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_C),
  1103. DEBUGFS_REG32(DC_DISP_H_PULSE2_POSITION_D),
  1104. DEBUGFS_REG32(DC_DISP_V_PULSE0_CONTROL),
  1105. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_A),
  1106. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_B),
  1107. DEBUGFS_REG32(DC_DISP_V_PULSE0_POSITION_C),
  1108. DEBUGFS_REG32(DC_DISP_V_PULSE1_CONTROL),
  1109. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_A),
  1110. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_B),
  1111. DEBUGFS_REG32(DC_DISP_V_PULSE1_POSITION_C),
  1112. DEBUGFS_REG32(DC_DISP_V_PULSE2_CONTROL),
  1113. DEBUGFS_REG32(DC_DISP_V_PULSE2_POSITION_A),
  1114. DEBUGFS_REG32(DC_DISP_V_PULSE3_CONTROL),
  1115. DEBUGFS_REG32(DC_DISP_V_PULSE3_POSITION_A),
  1116. DEBUGFS_REG32(DC_DISP_M0_CONTROL),
  1117. DEBUGFS_REG32(DC_DISP_M1_CONTROL),
  1118. DEBUGFS_REG32(DC_DISP_DI_CONTROL),
  1119. DEBUGFS_REG32(DC_DISP_PP_CONTROL),
  1120. DEBUGFS_REG32(DC_DISP_PP_SELECT_A),
  1121. DEBUGFS_REG32(DC_DISP_PP_SELECT_B),
  1122. DEBUGFS_REG32(DC_DISP_PP_SELECT_C),
  1123. DEBUGFS_REG32(DC_DISP_PP_SELECT_D),
  1124. DEBUGFS_REG32(DC_DISP_DISP_CLOCK_CONTROL),
  1125. DEBUGFS_REG32(DC_DISP_DISP_INTERFACE_CONTROL),
  1126. DEBUGFS_REG32(DC_DISP_DISP_COLOR_CONTROL),
  1127. DEBUGFS_REG32(DC_DISP_SHIFT_CLOCK_OPTIONS),
  1128. DEBUGFS_REG32(DC_DISP_DATA_ENABLE_OPTIONS),
  1129. DEBUGFS_REG32(DC_DISP_SERIAL_INTERFACE_OPTIONS),
  1130. DEBUGFS_REG32(DC_DISP_LCD_SPI_OPTIONS),
  1131. DEBUGFS_REG32(DC_DISP_BORDER_COLOR),
  1132. DEBUGFS_REG32(DC_DISP_COLOR_KEY0_LOWER),
  1133. DEBUGFS_REG32(DC_DISP_COLOR_KEY0_UPPER),
  1134. DEBUGFS_REG32(DC_DISP_COLOR_KEY1_LOWER),
  1135. DEBUGFS_REG32(DC_DISP_COLOR_KEY1_UPPER),
  1136. DEBUGFS_REG32(DC_DISP_CURSOR_FOREGROUND),
  1137. DEBUGFS_REG32(DC_DISP_CURSOR_BACKGROUND),
  1138. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR),
  1139. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_NS),
  1140. DEBUGFS_REG32(DC_DISP_CURSOR_POSITION),
  1141. DEBUGFS_REG32(DC_DISP_CURSOR_POSITION_NS),
  1142. DEBUGFS_REG32(DC_DISP_INIT_SEQ_CONTROL),
  1143. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_A),
  1144. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_B),
  1145. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_C),
  1146. DEBUGFS_REG32(DC_DISP_SPI_INIT_SEQ_DATA_D),
  1147. DEBUGFS_REG32(DC_DISP_DC_MCCIF_FIFOCTRL),
  1148. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0A_HYST),
  1149. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY0B_HYST),
  1150. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1A_HYST),
  1151. DEBUGFS_REG32(DC_DISP_MCCIF_DISPLAY1B_HYST),
  1152. DEBUGFS_REG32(DC_DISP_DAC_CRT_CTRL),
  1153. DEBUGFS_REG32(DC_DISP_DISP_MISC_CONTROL),
  1154. DEBUGFS_REG32(DC_DISP_SD_CONTROL),
  1155. DEBUGFS_REG32(DC_DISP_SD_CSC_COEFF),
  1156. DEBUGFS_REG32(DC_DISP_SD_LUT(0)),
  1157. DEBUGFS_REG32(DC_DISP_SD_LUT(1)),
  1158. DEBUGFS_REG32(DC_DISP_SD_LUT(2)),
  1159. DEBUGFS_REG32(DC_DISP_SD_LUT(3)),
  1160. DEBUGFS_REG32(DC_DISP_SD_LUT(4)),
  1161. DEBUGFS_REG32(DC_DISP_SD_LUT(5)),
  1162. DEBUGFS_REG32(DC_DISP_SD_LUT(6)),
  1163. DEBUGFS_REG32(DC_DISP_SD_LUT(7)),
  1164. DEBUGFS_REG32(DC_DISP_SD_LUT(8)),
  1165. DEBUGFS_REG32(DC_DISP_SD_FLICKER_CONTROL),
  1166. DEBUGFS_REG32(DC_DISP_DC_PIXEL_COUNT),
  1167. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(0)),
  1168. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(1)),
  1169. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(2)),
  1170. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(3)),
  1171. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(4)),
  1172. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(5)),
  1173. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(6)),
  1174. DEBUGFS_REG32(DC_DISP_SD_HISTOGRAM(7)),
  1175. DEBUGFS_REG32(DC_DISP_SD_BL_TF(0)),
  1176. DEBUGFS_REG32(DC_DISP_SD_BL_TF(1)),
  1177. DEBUGFS_REG32(DC_DISP_SD_BL_TF(2)),
  1178. DEBUGFS_REG32(DC_DISP_SD_BL_TF(3)),
  1179. DEBUGFS_REG32(DC_DISP_SD_BL_CONTROL),
  1180. DEBUGFS_REG32(DC_DISP_SD_HW_K_VALUES),
  1181. DEBUGFS_REG32(DC_DISP_SD_MAN_K_VALUES),
  1182. DEBUGFS_REG32(DC_DISP_CURSOR_START_ADDR_HI),
  1183. DEBUGFS_REG32(DC_DISP_BLEND_CURSOR_CONTROL),
  1184. DEBUGFS_REG32(DC_WIN_WIN_OPTIONS),
  1185. DEBUGFS_REG32(DC_WIN_BYTE_SWAP),
  1186. DEBUGFS_REG32(DC_WIN_BUFFER_CONTROL),
  1187. DEBUGFS_REG32(DC_WIN_COLOR_DEPTH),
  1188. DEBUGFS_REG32(DC_WIN_POSITION),
  1189. DEBUGFS_REG32(DC_WIN_SIZE),
  1190. DEBUGFS_REG32(DC_WIN_PRESCALED_SIZE),
  1191. DEBUGFS_REG32(DC_WIN_H_INITIAL_DDA),
  1192. DEBUGFS_REG32(DC_WIN_V_INITIAL_DDA),
  1193. DEBUGFS_REG32(DC_WIN_DDA_INC),
  1194. DEBUGFS_REG32(DC_WIN_LINE_STRIDE),
  1195. DEBUGFS_REG32(DC_WIN_BUF_STRIDE),
  1196. DEBUGFS_REG32(DC_WIN_UV_BUF_STRIDE),
  1197. DEBUGFS_REG32(DC_WIN_BUFFER_ADDR_MODE),
  1198. DEBUGFS_REG32(DC_WIN_DV_CONTROL),
  1199. DEBUGFS_REG32(DC_WIN_BLEND_NOKEY),
  1200. DEBUGFS_REG32(DC_WIN_BLEND_1WIN),
  1201. DEBUGFS_REG32(DC_WIN_BLEND_2WIN_X),
  1202. DEBUGFS_REG32(DC_WIN_BLEND_2WIN_Y),
  1203. DEBUGFS_REG32(DC_WIN_BLEND_3WIN_XY),
  1204. DEBUGFS_REG32(DC_WIN_HP_FETCH_CONTROL),
  1205. DEBUGFS_REG32(DC_WINBUF_START_ADDR),
  1206. DEBUGFS_REG32(DC_WINBUF_START_ADDR_NS),
  1207. DEBUGFS_REG32(DC_WINBUF_START_ADDR_U),
  1208. DEBUGFS_REG32(DC_WINBUF_START_ADDR_U_NS),
  1209. DEBUGFS_REG32(DC_WINBUF_START_ADDR_V),
  1210. DEBUGFS_REG32(DC_WINBUF_START_ADDR_V_NS),
  1211. DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET),
  1212. DEBUGFS_REG32(DC_WINBUF_ADDR_H_OFFSET_NS),
  1213. DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET),
  1214. DEBUGFS_REG32(DC_WINBUF_ADDR_V_OFFSET_NS),
  1215. DEBUGFS_REG32(DC_WINBUF_UFLOW_STATUS),
  1216. DEBUGFS_REG32(DC_WINBUF_AD_UFLOW_STATUS),
  1217. DEBUGFS_REG32(DC_WINBUF_BD_UFLOW_STATUS),
  1218. DEBUGFS_REG32(DC_WINBUF_CD_UFLOW_STATUS),
  1219. };
  1220. static int tegra_dc_show_regs(struct seq_file *s, void *data)
  1221. {
  1222. struct drm_info_node *node = s->private;
  1223. struct tegra_dc *dc = node->info_ent->data;
  1224. unsigned int i;
  1225. int err = 0;
  1226. drm_modeset_lock(&dc->base.mutex, NULL);
  1227. if (!dc->base.state->active) {
  1228. err = -EBUSY;
  1229. goto unlock;
  1230. }
  1231. for (i = 0; i < ARRAY_SIZE(tegra_dc_regs); i++) {
  1232. unsigned int offset = tegra_dc_regs[i].offset;
  1233. seq_printf(s, "%-40s %#05x %08x\n", tegra_dc_regs[i].name,
  1234. offset, tegra_dc_readl(dc, offset));
  1235. }
  1236. unlock:
  1237. drm_modeset_unlock(&dc->base.mutex);
  1238. return err;
  1239. }
  1240. static int tegra_dc_show_crc(struct seq_file *s, void *data)
  1241. {
  1242. struct drm_info_node *node = s->private;
  1243. struct tegra_dc *dc = node->info_ent->data;
  1244. int err = 0;
  1245. u32 value;
  1246. drm_modeset_lock(&dc->base.mutex, NULL);
  1247. if (!dc->base.state->active) {
  1248. err = -EBUSY;
  1249. goto unlock;
  1250. }
  1251. value = DC_COM_CRC_CONTROL_ACTIVE_DATA | DC_COM_CRC_CONTROL_ENABLE;
  1252. tegra_dc_writel(dc, value, DC_COM_CRC_CONTROL);
  1253. tegra_dc_commit(dc);
  1254. drm_crtc_wait_one_vblank(&dc->base);
  1255. drm_crtc_wait_one_vblank(&dc->base);
  1256. value = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM);
  1257. seq_printf(s, "%08x\n", value);
  1258. tegra_dc_writel(dc, 0, DC_COM_CRC_CONTROL);
  1259. unlock:
  1260. drm_modeset_unlock(&dc->base.mutex);
  1261. return err;
  1262. }
  1263. static int tegra_dc_show_stats(struct seq_file *s, void *data)
  1264. {
  1265. struct drm_info_node *node = s->private;
  1266. struct tegra_dc *dc = node->info_ent->data;
  1267. seq_printf(s, "frames: %lu\n", dc->stats.frames);
  1268. seq_printf(s, "vblank: %lu\n", dc->stats.vblank);
  1269. seq_printf(s, "underflow: %lu\n", dc->stats.underflow);
  1270. seq_printf(s, "overflow: %lu\n", dc->stats.overflow);
  1271. return 0;
  1272. }
  1273. static struct drm_info_list debugfs_files[] = {
  1274. { "regs", tegra_dc_show_regs, 0, NULL },
  1275. { "crc", tegra_dc_show_crc, 0, NULL },
  1276. { "stats", tegra_dc_show_stats, 0, NULL },
  1277. };
  1278. static int tegra_dc_late_register(struct drm_crtc *crtc)
  1279. {
  1280. unsigned int i, count = ARRAY_SIZE(debugfs_files);
  1281. struct drm_minor *minor = crtc->dev->primary;
  1282. struct dentry *root;
  1283. struct tegra_dc *dc = to_tegra_dc(crtc);
  1284. int err;
  1285. #ifdef CONFIG_DEBUG_FS
  1286. root = crtc->debugfs_entry;
  1287. #else
  1288. root = NULL;
  1289. #endif
  1290. dc->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  1291. GFP_KERNEL);
  1292. if (!dc->debugfs_files)
  1293. return -ENOMEM;
  1294. for (i = 0; i < count; i++)
  1295. dc->debugfs_files[i].data = dc;
  1296. err = drm_debugfs_create_files(dc->debugfs_files, count, root, minor);
  1297. if (err < 0)
  1298. goto free;
  1299. return 0;
  1300. free:
  1301. kfree(dc->debugfs_files);
  1302. dc->debugfs_files = NULL;
  1303. return err;
  1304. }
  1305. static void tegra_dc_early_unregister(struct drm_crtc *crtc)
  1306. {
  1307. unsigned int count = ARRAY_SIZE(debugfs_files);
  1308. struct drm_minor *minor = crtc->dev->primary;
  1309. struct tegra_dc *dc = to_tegra_dc(crtc);
  1310. drm_debugfs_remove_files(dc->debugfs_files, count, minor);
  1311. kfree(dc->debugfs_files);
  1312. dc->debugfs_files = NULL;
  1313. }
  1314. static u32 tegra_dc_get_vblank_counter(struct drm_crtc *crtc)
  1315. {
  1316. struct tegra_dc *dc = to_tegra_dc(crtc);
  1317. /* XXX vblank syncpoints don't work with nvdisplay yet */
  1318. if (dc->syncpt && !dc->soc->has_nvdisplay)
  1319. return host1x_syncpt_read(dc->syncpt);
  1320. /* fallback to software emulated VBLANK counter */
  1321. return (u32)drm_crtc_vblank_count(&dc->base);
  1322. }
  1323. static int tegra_dc_enable_vblank(struct drm_crtc *crtc)
  1324. {
  1325. struct tegra_dc *dc = to_tegra_dc(crtc);
  1326. u32 value;
  1327. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  1328. value |= VBLANK_INT;
  1329. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1330. return 0;
  1331. }
  1332. static void tegra_dc_disable_vblank(struct drm_crtc *crtc)
  1333. {
  1334. struct tegra_dc *dc = to_tegra_dc(crtc);
  1335. u32 value;
  1336. value = tegra_dc_readl(dc, DC_CMD_INT_MASK);
  1337. value &= ~VBLANK_INT;
  1338. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1339. }
  1340. static const struct drm_crtc_funcs tegra_crtc_funcs = {
  1341. .page_flip = drm_atomic_helper_page_flip,
  1342. .set_config = drm_atomic_helper_set_config,
  1343. .destroy = tegra_dc_destroy,
  1344. .reset = tegra_crtc_reset,
  1345. .atomic_duplicate_state = tegra_crtc_atomic_duplicate_state,
  1346. .atomic_destroy_state = tegra_crtc_atomic_destroy_state,
  1347. .late_register = tegra_dc_late_register,
  1348. .early_unregister = tegra_dc_early_unregister,
  1349. .get_vblank_counter = tegra_dc_get_vblank_counter,
  1350. .enable_vblank = tegra_dc_enable_vblank,
  1351. .disable_vblank = tegra_dc_disable_vblank,
  1352. };
  1353. static int tegra_dc_set_timings(struct tegra_dc *dc,
  1354. struct drm_display_mode *mode)
  1355. {
  1356. unsigned int h_ref_to_sync = 1;
  1357. unsigned int v_ref_to_sync = 1;
  1358. unsigned long value;
  1359. if (!dc->soc->has_nvdisplay) {
  1360. tegra_dc_writel(dc, 0x0, DC_DISP_DISP_TIMING_OPTIONS);
  1361. value = (v_ref_to_sync << 16) | h_ref_to_sync;
  1362. tegra_dc_writel(dc, value, DC_DISP_REF_TO_SYNC);
  1363. }
  1364. value = ((mode->vsync_end - mode->vsync_start) << 16) |
  1365. ((mode->hsync_end - mode->hsync_start) << 0);
  1366. tegra_dc_writel(dc, value, DC_DISP_SYNC_WIDTH);
  1367. value = ((mode->vtotal - mode->vsync_end) << 16) |
  1368. ((mode->htotal - mode->hsync_end) << 0);
  1369. tegra_dc_writel(dc, value, DC_DISP_BACK_PORCH);
  1370. value = ((mode->vsync_start - mode->vdisplay) << 16) |
  1371. ((mode->hsync_start - mode->hdisplay) << 0);
  1372. tegra_dc_writel(dc, value, DC_DISP_FRONT_PORCH);
  1373. value = (mode->vdisplay << 16) | mode->hdisplay;
  1374. tegra_dc_writel(dc, value, DC_DISP_ACTIVE);
  1375. return 0;
  1376. }
  1377. /**
  1378. * tegra_dc_state_setup_clock - check clock settings and store them in atomic
  1379. * state
  1380. * @dc: display controller
  1381. * @crtc_state: CRTC atomic state
  1382. * @clk: parent clock for display controller
  1383. * @pclk: pixel clock
  1384. * @div: shift clock divider
  1385. *
  1386. * Returns:
  1387. * 0 on success or a negative error-code on failure.
  1388. */
  1389. int tegra_dc_state_setup_clock(struct tegra_dc *dc,
  1390. struct drm_crtc_state *crtc_state,
  1391. struct clk *clk, unsigned long pclk,
  1392. unsigned int div)
  1393. {
  1394. struct tegra_dc_state *state = to_dc_state(crtc_state);
  1395. if (!clk_has_parent(dc->clk, clk))
  1396. return -EINVAL;
  1397. state->clk = clk;
  1398. state->pclk = pclk;
  1399. state->div = div;
  1400. return 0;
  1401. }
  1402. static void tegra_dc_commit_state(struct tegra_dc *dc,
  1403. struct tegra_dc_state *state)
  1404. {
  1405. u32 value;
  1406. int err;
  1407. err = clk_set_parent(dc->clk, state->clk);
  1408. if (err < 0)
  1409. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  1410. /*
  1411. * Outputs may not want to change the parent clock rate. This is only
  1412. * relevant to Tegra20 where only a single display PLL is available.
  1413. * Since that PLL would typically be used for HDMI, an internal LVDS
  1414. * panel would need to be driven by some other clock such as PLL_P
  1415. * which is shared with other peripherals. Changing the clock rate
  1416. * should therefore be avoided.
  1417. */
  1418. if (state->pclk > 0) {
  1419. err = clk_set_rate(state->clk, state->pclk);
  1420. if (err < 0)
  1421. dev_err(dc->dev,
  1422. "failed to set clock rate to %lu Hz\n",
  1423. state->pclk);
  1424. }
  1425. DRM_DEBUG_KMS("rate: %lu, div: %u\n", clk_get_rate(dc->clk),
  1426. state->div);
  1427. DRM_DEBUG_KMS("pclk: %lu\n", state->pclk);
  1428. if (!dc->soc->has_nvdisplay) {
  1429. value = SHIFT_CLK_DIVIDER(state->div) | PIXEL_CLK_DIVIDER_PCD1;
  1430. tegra_dc_writel(dc, value, DC_DISP_DISP_CLOCK_CONTROL);
  1431. }
  1432. err = clk_set_rate(dc->clk, state->pclk);
  1433. if (err < 0)
  1434. dev_err(dc->dev, "failed to set clock %pC to %lu Hz: %d\n",
  1435. dc->clk, state->pclk, err);
  1436. }
  1437. static void tegra_dc_stop(struct tegra_dc *dc)
  1438. {
  1439. u32 value;
  1440. /* stop the display controller */
  1441. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  1442. value &= ~DISP_CTRL_MODE_MASK;
  1443. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  1444. tegra_dc_commit(dc);
  1445. }
  1446. static bool tegra_dc_idle(struct tegra_dc *dc)
  1447. {
  1448. u32 value;
  1449. value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
  1450. return (value & DISP_CTRL_MODE_MASK) == 0;
  1451. }
  1452. static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
  1453. {
  1454. timeout = jiffies + msecs_to_jiffies(timeout);
  1455. while (time_before(jiffies, timeout)) {
  1456. if (tegra_dc_idle(dc))
  1457. return 0;
  1458. usleep_range(1000, 2000);
  1459. }
  1460. dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
  1461. return -ETIMEDOUT;
  1462. }
  1463. static void tegra_crtc_atomic_disable(struct drm_crtc *crtc,
  1464. struct drm_crtc_state *old_state)
  1465. {
  1466. struct tegra_dc *dc = to_tegra_dc(crtc);
  1467. u32 value;
  1468. if (!tegra_dc_idle(dc)) {
  1469. tegra_dc_stop(dc);
  1470. /*
  1471. * Ignore the return value, there isn't anything useful to do
  1472. * in case this fails.
  1473. */
  1474. tegra_dc_wait_idle(dc, 100);
  1475. }
  1476. /*
  1477. * This should really be part of the RGB encoder driver, but clearing
  1478. * these bits has the side-effect of stopping the display controller.
  1479. * When that happens no VBLANK interrupts will be raised. At the same
  1480. * time the encoder is disabled before the display controller, so the
  1481. * above code is always going to timeout waiting for the controller
  1482. * to go idle.
  1483. *
  1484. * Given the close coupling between the RGB encoder and the display
  1485. * controller doing it here is still kind of okay. None of the other
  1486. * encoder drivers require these bits to be cleared.
  1487. *
  1488. * XXX: Perhaps given that the display controller is switched off at
  1489. * this point anyway maybe clearing these bits isn't even useful for
  1490. * the RGB encoder?
  1491. */
  1492. if (dc->rgb) {
  1493. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  1494. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  1495. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  1496. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  1497. }
  1498. tegra_dc_stats_reset(&dc->stats);
  1499. drm_crtc_vblank_off(crtc);
  1500. spin_lock_irq(&crtc->dev->event_lock);
  1501. if (crtc->state->event) {
  1502. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  1503. crtc->state->event = NULL;
  1504. }
  1505. spin_unlock_irq(&crtc->dev->event_lock);
  1506. pm_runtime_put_sync(dc->dev);
  1507. }
  1508. static void tegra_crtc_atomic_enable(struct drm_crtc *crtc,
  1509. struct drm_crtc_state *old_state)
  1510. {
  1511. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  1512. struct tegra_dc_state *state = to_dc_state(crtc->state);
  1513. struct tegra_dc *dc = to_tegra_dc(crtc);
  1514. u32 value;
  1515. pm_runtime_get_sync(dc->dev);
  1516. /* initialize display controller */
  1517. if (dc->syncpt) {
  1518. u32 syncpt = host1x_syncpt_id(dc->syncpt), enable;
  1519. if (dc->soc->has_nvdisplay)
  1520. enable = 1 << 31;
  1521. else
  1522. enable = 1 << 8;
  1523. value = SYNCPT_CNTRL_NO_STALL;
  1524. tegra_dc_writel(dc, value, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
  1525. value = enable | syncpt;
  1526. tegra_dc_writel(dc, value, DC_CMD_CONT_SYNCPT_VSYNC);
  1527. }
  1528. if (dc->soc->has_nvdisplay) {
  1529. value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
  1530. DSC_OBUF_UF_INT;
  1531. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  1532. value = DSC_TO_UF_INT | DSC_BBUF_UF_INT | DSC_RBUF_UF_INT |
  1533. DSC_OBUF_UF_INT | SD3_BUCKET_WALK_DONE_INT |
  1534. HEAD_UF_INT | MSF_INT | REG_TMOUT_INT |
  1535. REGION_CRC_INT | V_PULSE2_INT | V_PULSE3_INT |
  1536. VBLANK_INT | FRAME_END_INT;
  1537. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  1538. value = SD3_BUCKET_WALK_DONE_INT | HEAD_UF_INT | VBLANK_INT |
  1539. FRAME_END_INT;
  1540. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  1541. value = HEAD_UF_INT | REG_TMOUT_INT | FRAME_END_INT;
  1542. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1543. tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
  1544. } else {
  1545. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1546. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1547. tegra_dc_writel(dc, value, DC_CMD_INT_TYPE);
  1548. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1549. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1550. tegra_dc_writel(dc, value, DC_CMD_INT_POLARITY);
  1551. /* initialize timer */
  1552. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(0x20) |
  1553. WINDOW_B_THRESHOLD(0x20) | WINDOW_C_THRESHOLD(0x20);
  1554. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY);
  1555. value = CURSOR_THRESHOLD(0) | WINDOW_A_THRESHOLD(1) |
  1556. WINDOW_B_THRESHOLD(1) | WINDOW_C_THRESHOLD(1);
  1557. tegra_dc_writel(dc, value, DC_DISP_DISP_MEM_HIGH_PRIORITY_TIMER);
  1558. value = VBLANK_INT | WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1559. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1560. tegra_dc_writel(dc, value, DC_CMD_INT_ENABLE);
  1561. value = WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT |
  1562. WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT;
  1563. tegra_dc_writel(dc, value, DC_CMD_INT_MASK);
  1564. }
  1565. if (dc->soc->supports_background_color)
  1566. tegra_dc_writel(dc, 0, DC_DISP_BLEND_BACKGROUND_COLOR);
  1567. else
  1568. tegra_dc_writel(dc, 0, DC_DISP_BORDER_COLOR);
  1569. /* apply PLL and pixel clock changes */
  1570. tegra_dc_commit_state(dc, state);
  1571. /* program display mode */
  1572. tegra_dc_set_timings(dc, mode);
  1573. /* interlacing isn't supported yet, so disable it */
  1574. if (dc->soc->supports_interlacing) {
  1575. value = tegra_dc_readl(dc, DC_DISP_INTERLACE_CONTROL);
  1576. value &= ~INTERLACE_ENABLE;
  1577. tegra_dc_writel(dc, value, DC_DISP_INTERLACE_CONTROL);
  1578. }
  1579. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  1580. value &= ~DISP_CTRL_MODE_MASK;
  1581. value |= DISP_CTRL_MODE_C_DISPLAY;
  1582. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  1583. if (!dc->soc->has_nvdisplay) {
  1584. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  1585. value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  1586. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  1587. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  1588. }
  1589. /* enable underflow reporting and display red for missing pixels */
  1590. if (dc->soc->has_nvdisplay) {
  1591. value = UNDERFLOW_MODE_RED | UNDERFLOW_REPORT_ENABLE;
  1592. tegra_dc_writel(dc, value, DC_COM_RG_UNDERFLOW);
  1593. }
  1594. tegra_dc_commit(dc);
  1595. drm_crtc_vblank_on(crtc);
  1596. }
  1597. static void tegra_crtc_atomic_begin(struct drm_crtc *crtc,
  1598. struct drm_crtc_state *old_crtc_state)
  1599. {
  1600. unsigned long flags;
  1601. if (crtc->state->event) {
  1602. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  1603. if (drm_crtc_vblank_get(crtc) != 0)
  1604. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  1605. else
  1606. drm_crtc_arm_vblank_event(crtc, crtc->state->event);
  1607. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  1608. crtc->state->event = NULL;
  1609. }
  1610. }
  1611. static void tegra_crtc_atomic_flush(struct drm_crtc *crtc,
  1612. struct drm_crtc_state *old_crtc_state)
  1613. {
  1614. struct tegra_dc_state *state = to_dc_state(crtc->state);
  1615. struct tegra_dc *dc = to_tegra_dc(crtc);
  1616. u32 value;
  1617. value = state->planes << 8 | GENERAL_UPDATE;
  1618. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  1619. value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
  1620. value = state->planes | GENERAL_ACT_REQ;
  1621. tegra_dc_writel(dc, value, DC_CMD_STATE_CONTROL);
  1622. value = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
  1623. }
  1624. static const struct drm_crtc_helper_funcs tegra_crtc_helper_funcs = {
  1625. .atomic_begin = tegra_crtc_atomic_begin,
  1626. .atomic_flush = tegra_crtc_atomic_flush,
  1627. .atomic_enable = tegra_crtc_atomic_enable,
  1628. .atomic_disable = tegra_crtc_atomic_disable,
  1629. };
  1630. static irqreturn_t tegra_dc_irq(int irq, void *data)
  1631. {
  1632. struct tegra_dc *dc = data;
  1633. unsigned long status;
  1634. status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
  1635. tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
  1636. if (status & FRAME_END_INT) {
  1637. /*
  1638. dev_dbg(dc->dev, "%s(): frame end\n", __func__);
  1639. */
  1640. dc->stats.frames++;
  1641. }
  1642. if (status & VBLANK_INT) {
  1643. /*
  1644. dev_dbg(dc->dev, "%s(): vertical blank\n", __func__);
  1645. */
  1646. drm_crtc_handle_vblank(&dc->base);
  1647. dc->stats.vblank++;
  1648. }
  1649. if (status & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT)) {
  1650. /*
  1651. dev_dbg(dc->dev, "%s(): underflow\n", __func__);
  1652. */
  1653. dc->stats.underflow++;
  1654. }
  1655. if (status & (WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT)) {
  1656. /*
  1657. dev_dbg(dc->dev, "%s(): overflow\n", __func__);
  1658. */
  1659. dc->stats.overflow++;
  1660. }
  1661. if (status & HEAD_UF_INT) {
  1662. dev_dbg_ratelimited(dc->dev, "%s(): head underflow\n", __func__);
  1663. dc->stats.underflow++;
  1664. }
  1665. return IRQ_HANDLED;
  1666. }
  1667. static int tegra_dc_init(struct host1x_client *client)
  1668. {
  1669. struct drm_device *drm = dev_get_drvdata(client->parent);
  1670. unsigned long flags = HOST1X_SYNCPT_CLIENT_MANAGED;
  1671. struct tegra_dc *dc = host1x_client_to_dc(client);
  1672. struct tegra_drm *tegra = drm->dev_private;
  1673. struct drm_plane *primary = NULL;
  1674. struct drm_plane *cursor = NULL;
  1675. int err;
  1676. /*
  1677. * XXX do not register DCs with no window groups because we cannot
  1678. * assign a primary plane to them, which in turn will cause KMS to
  1679. * crash.
  1680. */
  1681. if (dc->soc->wgrps) {
  1682. bool has_wgrps = false;
  1683. unsigned int i;
  1684. for (i = 0; i < dc->soc->num_wgrps; i++) {
  1685. const struct tegra_windowgroup_soc *wgrp = &dc->soc->wgrps[i];
  1686. if (wgrp->dc == dc->pipe && wgrp->num_windows > 0) {
  1687. has_wgrps = true;
  1688. break;
  1689. }
  1690. }
  1691. if (!has_wgrps)
  1692. return 0;
  1693. }
  1694. dc->syncpt = host1x_syncpt_request(client, flags);
  1695. if (!dc->syncpt)
  1696. dev_warn(dc->dev, "failed to allocate syncpoint\n");
  1697. dc->group = host1x_client_iommu_attach(client, true);
  1698. if (IS_ERR(dc->group)) {
  1699. err = PTR_ERR(dc->group);
  1700. dev_err(client->dev, "failed to attach to domain: %d\n", err);
  1701. return err;
  1702. }
  1703. if (dc->soc->wgrps)
  1704. primary = tegra_dc_add_shared_planes(drm, dc);
  1705. else
  1706. primary = tegra_dc_add_planes(drm, dc);
  1707. if (IS_ERR(primary)) {
  1708. err = PTR_ERR(primary);
  1709. goto cleanup;
  1710. }
  1711. if (dc->soc->supports_cursor) {
  1712. cursor = tegra_dc_cursor_plane_create(drm, dc);
  1713. if (IS_ERR(cursor)) {
  1714. err = PTR_ERR(cursor);
  1715. goto cleanup;
  1716. }
  1717. } else {
  1718. /* dedicate one overlay to mouse cursor */
  1719. cursor = tegra_dc_overlay_plane_create(drm, dc, 2, true);
  1720. if (IS_ERR(cursor)) {
  1721. err = PTR_ERR(cursor);
  1722. goto cleanup;
  1723. }
  1724. }
  1725. err = drm_crtc_init_with_planes(drm, &dc->base, primary, cursor,
  1726. &tegra_crtc_funcs, NULL);
  1727. if (err < 0)
  1728. goto cleanup;
  1729. drm_crtc_helper_add(&dc->base, &tegra_crtc_helper_funcs);
  1730. /*
  1731. * Keep track of the minimum pitch alignment across all display
  1732. * controllers.
  1733. */
  1734. if (dc->soc->pitch_align > tegra->pitch_align)
  1735. tegra->pitch_align = dc->soc->pitch_align;
  1736. err = tegra_dc_rgb_init(drm, dc);
  1737. if (err < 0 && err != -ENODEV) {
  1738. dev_err(dc->dev, "failed to initialize RGB output: %d\n", err);
  1739. goto cleanup;
  1740. }
  1741. err = devm_request_irq(dc->dev, dc->irq, tegra_dc_irq, 0,
  1742. dev_name(dc->dev), dc);
  1743. if (err < 0) {
  1744. dev_err(dc->dev, "failed to request IRQ#%u: %d\n", dc->irq,
  1745. err);
  1746. goto cleanup;
  1747. }
  1748. return 0;
  1749. cleanup:
  1750. if (!IS_ERR_OR_NULL(cursor))
  1751. drm_plane_cleanup(cursor);
  1752. if (!IS_ERR(primary))
  1753. drm_plane_cleanup(primary);
  1754. host1x_client_iommu_detach(client, dc->group);
  1755. host1x_syncpt_free(dc->syncpt);
  1756. return err;
  1757. }
  1758. static int tegra_dc_exit(struct host1x_client *client)
  1759. {
  1760. struct tegra_dc *dc = host1x_client_to_dc(client);
  1761. int err;
  1762. devm_free_irq(dc->dev, dc->irq, dc);
  1763. err = tegra_dc_rgb_exit(dc);
  1764. if (err) {
  1765. dev_err(dc->dev, "failed to shutdown RGB output: %d\n", err);
  1766. return err;
  1767. }
  1768. host1x_client_iommu_detach(client, dc->group);
  1769. host1x_syncpt_free(dc->syncpt);
  1770. return 0;
  1771. }
  1772. static const struct host1x_client_ops dc_client_ops = {
  1773. .init = tegra_dc_init,
  1774. .exit = tegra_dc_exit,
  1775. };
  1776. static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
  1777. .supports_background_color = false,
  1778. .supports_interlacing = false,
  1779. .supports_cursor = false,
  1780. .supports_block_linear = false,
  1781. .has_legacy_blending = true,
  1782. .pitch_align = 8,
  1783. .has_powergate = false,
  1784. .coupled_pm = true,
  1785. .has_nvdisplay = false,
  1786. .num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
  1787. .primary_formats = tegra20_primary_formats,
  1788. .num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
  1789. .overlay_formats = tegra20_overlay_formats,
  1790. .modifiers = tegra20_modifiers,
  1791. .has_win_a_without_filters = true,
  1792. .has_win_c_without_vert_filter = true,
  1793. };
  1794. static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
  1795. .supports_background_color = false,
  1796. .supports_interlacing = false,
  1797. .supports_cursor = false,
  1798. .supports_block_linear = false,
  1799. .has_legacy_blending = true,
  1800. .pitch_align = 8,
  1801. .has_powergate = false,
  1802. .coupled_pm = false,
  1803. .has_nvdisplay = false,
  1804. .num_primary_formats = ARRAY_SIZE(tegra20_primary_formats),
  1805. .primary_formats = tegra20_primary_formats,
  1806. .num_overlay_formats = ARRAY_SIZE(tegra20_overlay_formats),
  1807. .overlay_formats = tegra20_overlay_formats,
  1808. .modifiers = tegra20_modifiers,
  1809. .has_win_a_without_filters = false,
  1810. .has_win_c_without_vert_filter = false,
  1811. };
  1812. static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
  1813. .supports_background_color = false,
  1814. .supports_interlacing = false,
  1815. .supports_cursor = false,
  1816. .supports_block_linear = false,
  1817. .has_legacy_blending = true,
  1818. .pitch_align = 64,
  1819. .has_powergate = true,
  1820. .coupled_pm = false,
  1821. .has_nvdisplay = false,
  1822. .num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
  1823. .primary_formats = tegra114_primary_formats,
  1824. .num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
  1825. .overlay_formats = tegra114_overlay_formats,
  1826. .modifiers = tegra20_modifiers,
  1827. .has_win_a_without_filters = false,
  1828. .has_win_c_without_vert_filter = false,
  1829. };
  1830. static const struct tegra_dc_soc_info tegra124_dc_soc_info = {
  1831. .supports_background_color = true,
  1832. .supports_interlacing = true,
  1833. .supports_cursor = true,
  1834. .supports_block_linear = true,
  1835. .has_legacy_blending = false,
  1836. .pitch_align = 64,
  1837. .has_powergate = true,
  1838. .coupled_pm = false,
  1839. .has_nvdisplay = false,
  1840. .num_primary_formats = ARRAY_SIZE(tegra124_primary_formats),
  1841. .primary_formats = tegra124_primary_formats,
  1842. .num_overlay_formats = ARRAY_SIZE(tegra124_overlay_formats),
  1843. .overlay_formats = tegra124_overlay_formats,
  1844. .modifiers = tegra124_modifiers,
  1845. .has_win_a_without_filters = false,
  1846. .has_win_c_without_vert_filter = false,
  1847. };
  1848. static const struct tegra_dc_soc_info tegra210_dc_soc_info = {
  1849. .supports_background_color = true,
  1850. .supports_interlacing = true,
  1851. .supports_cursor = true,
  1852. .supports_block_linear = true,
  1853. .has_legacy_blending = false,
  1854. .pitch_align = 64,
  1855. .has_powergate = true,
  1856. .coupled_pm = false,
  1857. .has_nvdisplay = false,
  1858. .num_primary_formats = ARRAY_SIZE(tegra114_primary_formats),
  1859. .primary_formats = tegra114_primary_formats,
  1860. .num_overlay_formats = ARRAY_SIZE(tegra114_overlay_formats),
  1861. .overlay_formats = tegra114_overlay_formats,
  1862. .modifiers = tegra124_modifiers,
  1863. .has_win_a_without_filters = false,
  1864. .has_win_c_without_vert_filter = false,
  1865. };
  1866. static const struct tegra_windowgroup_soc tegra186_dc_wgrps[] = {
  1867. {
  1868. .index = 0,
  1869. .dc = 0,
  1870. .windows = (const unsigned int[]) { 0 },
  1871. .num_windows = 1,
  1872. }, {
  1873. .index = 1,
  1874. .dc = 1,
  1875. .windows = (const unsigned int[]) { 1 },
  1876. .num_windows = 1,
  1877. }, {
  1878. .index = 2,
  1879. .dc = 1,
  1880. .windows = (const unsigned int[]) { 2 },
  1881. .num_windows = 1,
  1882. }, {
  1883. .index = 3,
  1884. .dc = 2,
  1885. .windows = (const unsigned int[]) { 3 },
  1886. .num_windows = 1,
  1887. }, {
  1888. .index = 4,
  1889. .dc = 2,
  1890. .windows = (const unsigned int[]) { 4 },
  1891. .num_windows = 1,
  1892. }, {
  1893. .index = 5,
  1894. .dc = 2,
  1895. .windows = (const unsigned int[]) { 5 },
  1896. .num_windows = 1,
  1897. },
  1898. };
  1899. static const struct tegra_dc_soc_info tegra186_dc_soc_info = {
  1900. .supports_background_color = true,
  1901. .supports_interlacing = true,
  1902. .supports_cursor = true,
  1903. .supports_block_linear = true,
  1904. .has_legacy_blending = false,
  1905. .pitch_align = 64,
  1906. .has_powergate = false,
  1907. .coupled_pm = false,
  1908. .has_nvdisplay = true,
  1909. .wgrps = tegra186_dc_wgrps,
  1910. .num_wgrps = ARRAY_SIZE(tegra186_dc_wgrps),
  1911. };
  1912. static const struct tegra_windowgroup_soc tegra194_dc_wgrps[] = {
  1913. {
  1914. .index = 0,
  1915. .dc = 0,
  1916. .windows = (const unsigned int[]) { 0 },
  1917. .num_windows = 1,
  1918. }, {
  1919. .index = 1,
  1920. .dc = 1,
  1921. .windows = (const unsigned int[]) { 1 },
  1922. .num_windows = 1,
  1923. }, {
  1924. .index = 2,
  1925. .dc = 1,
  1926. .windows = (const unsigned int[]) { 2 },
  1927. .num_windows = 1,
  1928. }, {
  1929. .index = 3,
  1930. .dc = 2,
  1931. .windows = (const unsigned int[]) { 3 },
  1932. .num_windows = 1,
  1933. }, {
  1934. .index = 4,
  1935. .dc = 2,
  1936. .windows = (const unsigned int[]) { 4 },
  1937. .num_windows = 1,
  1938. }, {
  1939. .index = 5,
  1940. .dc = 2,
  1941. .windows = (const unsigned int[]) { 5 },
  1942. .num_windows = 1,
  1943. },
  1944. };
  1945. static const struct tegra_dc_soc_info tegra194_dc_soc_info = {
  1946. .supports_background_color = true,
  1947. .supports_interlacing = true,
  1948. .supports_cursor = true,
  1949. .supports_block_linear = true,
  1950. .has_legacy_blending = false,
  1951. .pitch_align = 64,
  1952. .has_powergate = false,
  1953. .coupled_pm = false,
  1954. .has_nvdisplay = true,
  1955. .wgrps = tegra194_dc_wgrps,
  1956. .num_wgrps = ARRAY_SIZE(tegra194_dc_wgrps),
  1957. };
  1958. static const struct of_device_id tegra_dc_of_match[] = {
  1959. {
  1960. .compatible = "nvidia,tegra194-dc",
  1961. .data = &tegra194_dc_soc_info,
  1962. }, {
  1963. .compatible = "nvidia,tegra186-dc",
  1964. .data = &tegra186_dc_soc_info,
  1965. }, {
  1966. .compatible = "nvidia,tegra210-dc",
  1967. .data = &tegra210_dc_soc_info,
  1968. }, {
  1969. .compatible = "nvidia,tegra124-dc",
  1970. .data = &tegra124_dc_soc_info,
  1971. }, {
  1972. .compatible = "nvidia,tegra114-dc",
  1973. .data = &tegra114_dc_soc_info,
  1974. }, {
  1975. .compatible = "nvidia,tegra30-dc",
  1976. .data = &tegra30_dc_soc_info,
  1977. }, {
  1978. .compatible = "nvidia,tegra20-dc",
  1979. .data = &tegra20_dc_soc_info,
  1980. }, {
  1981. /* sentinel */
  1982. }
  1983. };
  1984. MODULE_DEVICE_TABLE(of, tegra_dc_of_match);
  1985. static int tegra_dc_parse_dt(struct tegra_dc *dc)
  1986. {
  1987. struct device_node *np;
  1988. u32 value = 0;
  1989. int err;
  1990. err = of_property_read_u32(dc->dev->of_node, "nvidia,head", &value);
  1991. if (err < 0) {
  1992. dev_err(dc->dev, "missing \"nvidia,head\" property\n");
  1993. /*
  1994. * If the nvidia,head property isn't present, try to find the
  1995. * correct head number by looking up the position of this
  1996. * display controller's node within the device tree. Assuming
  1997. * that the nodes are ordered properly in the DTS file and
  1998. * that the translation into a flattened device tree blob
  1999. * preserves that ordering this will actually yield the right
  2000. * head number.
  2001. *
  2002. * If those assumptions don't hold, this will still work for
  2003. * cases where only a single display controller is used.
  2004. */
  2005. for_each_matching_node(np, tegra_dc_of_match) {
  2006. if (np == dc->dev->of_node) {
  2007. of_node_put(np);
  2008. break;
  2009. }
  2010. value++;
  2011. }
  2012. }
  2013. dc->pipe = value;
  2014. return 0;
  2015. }
  2016. static int tegra_dc_match_by_pipe(struct device *dev, void *data)
  2017. {
  2018. struct tegra_dc *dc = dev_get_drvdata(dev);
  2019. unsigned int pipe = (unsigned long)data;
  2020. return dc->pipe == pipe;
  2021. }
  2022. static int tegra_dc_couple(struct tegra_dc *dc)
  2023. {
  2024. /*
  2025. * On Tegra20, DC1 requires DC0 to be taken out of reset in order to
  2026. * be enabled, otherwise CPU hangs on writing to CMD_DISPLAY_COMMAND /
  2027. * POWER_CONTROL registers during CRTC enabling.
  2028. */
  2029. if (dc->soc->coupled_pm && dc->pipe == 1) {
  2030. u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
  2031. struct device_link *link;
  2032. struct device *partner;
  2033. partner = driver_find_device(dc->dev->driver, NULL, NULL,
  2034. tegra_dc_match_by_pipe);
  2035. if (!partner)
  2036. return -EPROBE_DEFER;
  2037. link = device_link_add(dc->dev, partner, flags);
  2038. if (!link) {
  2039. dev_err(dc->dev, "failed to link controllers\n");
  2040. return -EINVAL;
  2041. }
  2042. dev_dbg(dc->dev, "coupled to %s\n", dev_name(partner));
  2043. }
  2044. return 0;
  2045. }
  2046. static int tegra_dc_probe(struct platform_device *pdev)
  2047. {
  2048. struct resource *regs;
  2049. struct tegra_dc *dc;
  2050. int err;
  2051. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  2052. if (!dc)
  2053. return -ENOMEM;
  2054. dc->soc = of_device_get_match_data(&pdev->dev);
  2055. INIT_LIST_HEAD(&dc->list);
  2056. dc->dev = &pdev->dev;
  2057. err = tegra_dc_parse_dt(dc);
  2058. if (err < 0)
  2059. return err;
  2060. err = tegra_dc_couple(dc);
  2061. if (err < 0)
  2062. return err;
  2063. dc->clk = devm_clk_get(&pdev->dev, NULL);
  2064. if (IS_ERR(dc->clk)) {
  2065. dev_err(&pdev->dev, "failed to get clock\n");
  2066. return PTR_ERR(dc->clk);
  2067. }
  2068. dc->rst = devm_reset_control_get(&pdev->dev, "dc");
  2069. if (IS_ERR(dc->rst)) {
  2070. dev_err(&pdev->dev, "failed to get reset\n");
  2071. return PTR_ERR(dc->rst);
  2072. }
  2073. /* assert reset and disable clock */
  2074. err = clk_prepare_enable(dc->clk);
  2075. if (err < 0)
  2076. return err;
  2077. usleep_range(2000, 4000);
  2078. err = reset_control_assert(dc->rst);
  2079. if (err < 0)
  2080. return err;
  2081. usleep_range(2000, 4000);
  2082. clk_disable_unprepare(dc->clk);
  2083. if (dc->soc->has_powergate) {
  2084. if (dc->pipe == 0)
  2085. dc->powergate = TEGRA_POWERGATE_DIS;
  2086. else
  2087. dc->powergate = TEGRA_POWERGATE_DISB;
  2088. tegra_powergate_power_off(dc->powergate);
  2089. }
  2090. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2091. dc->regs = devm_ioremap_resource(&pdev->dev, regs);
  2092. if (IS_ERR(dc->regs))
  2093. return PTR_ERR(dc->regs);
  2094. dc->irq = platform_get_irq(pdev, 0);
  2095. if (dc->irq < 0) {
  2096. dev_err(&pdev->dev, "failed to get IRQ\n");
  2097. return -ENXIO;
  2098. }
  2099. err = tegra_dc_rgb_probe(dc);
  2100. if (err < 0 && err != -ENODEV) {
  2101. dev_err(&pdev->dev, "failed to probe RGB output: %d\n", err);
  2102. return err;
  2103. }
  2104. platform_set_drvdata(pdev, dc);
  2105. pm_runtime_enable(&pdev->dev);
  2106. INIT_LIST_HEAD(&dc->client.list);
  2107. dc->client.ops = &dc_client_ops;
  2108. dc->client.dev = &pdev->dev;
  2109. err = host1x_client_register(&dc->client);
  2110. if (err < 0) {
  2111. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  2112. err);
  2113. return err;
  2114. }
  2115. return 0;
  2116. }
  2117. static int tegra_dc_remove(struct platform_device *pdev)
  2118. {
  2119. struct tegra_dc *dc = platform_get_drvdata(pdev);
  2120. int err;
  2121. err = host1x_client_unregister(&dc->client);
  2122. if (err < 0) {
  2123. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  2124. err);
  2125. return err;
  2126. }
  2127. err = tegra_dc_rgb_remove(dc);
  2128. if (err < 0) {
  2129. dev_err(&pdev->dev, "failed to remove RGB output: %d\n", err);
  2130. return err;
  2131. }
  2132. pm_runtime_disable(&pdev->dev);
  2133. return 0;
  2134. }
  2135. #ifdef CONFIG_PM
  2136. static int tegra_dc_suspend(struct device *dev)
  2137. {
  2138. struct tegra_dc *dc = dev_get_drvdata(dev);
  2139. int err;
  2140. err = reset_control_assert(dc->rst);
  2141. if (err < 0) {
  2142. dev_err(dev, "failed to assert reset: %d\n", err);
  2143. return err;
  2144. }
  2145. if (dc->soc->has_powergate)
  2146. tegra_powergate_power_off(dc->powergate);
  2147. clk_disable_unprepare(dc->clk);
  2148. return 0;
  2149. }
  2150. static int tegra_dc_resume(struct device *dev)
  2151. {
  2152. struct tegra_dc *dc = dev_get_drvdata(dev);
  2153. int err;
  2154. if (dc->soc->has_powergate) {
  2155. err = tegra_powergate_sequence_power_up(dc->powergate, dc->clk,
  2156. dc->rst);
  2157. if (err < 0) {
  2158. dev_err(dev, "failed to power partition: %d\n", err);
  2159. return err;
  2160. }
  2161. } else {
  2162. err = clk_prepare_enable(dc->clk);
  2163. if (err < 0) {
  2164. dev_err(dev, "failed to enable clock: %d\n", err);
  2165. return err;
  2166. }
  2167. err = reset_control_deassert(dc->rst);
  2168. if (err < 0) {
  2169. dev_err(dev, "failed to deassert reset: %d\n", err);
  2170. return err;
  2171. }
  2172. }
  2173. return 0;
  2174. }
  2175. #endif
  2176. static const struct dev_pm_ops tegra_dc_pm_ops = {
  2177. SET_RUNTIME_PM_OPS(tegra_dc_suspend, tegra_dc_resume, NULL)
  2178. };
  2179. struct platform_driver tegra_dc_driver = {
  2180. .driver = {
  2181. .name = "tegra-dc",
  2182. .of_match_table = tegra_dc_of_match,
  2183. .pm = &tegra_dc_pm_ops,
  2184. },
  2185. .probe = tegra_dc_probe,
  2186. .remove = tegra_dc_remove,
  2187. };