intel_panel.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. /*
  2. * Copyright © 2006-2010 Intel Corporation
  3. * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Eric Anholt <eric@anholt.net>
  26. * Dave Airlie <airlied@linux.ie>
  27. * Jesse Barnes <jesse.barnes@intel.com>
  28. * Chris Wilson <chris@chris-wilson.co.uk>
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/kernel.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/pwm.h>
  34. #include "intel_drv.h"
  35. #define CRC_PMIC_PWM_PERIOD_NS 21333
  36. void
  37. intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
  38. struct drm_display_mode *adjusted_mode)
  39. {
  40. drm_mode_copy(adjusted_mode, fixed_mode);
  41. drm_mode_set_crtcinfo(adjusted_mode, 0);
  42. }
  43. /**
  44. * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
  45. * @dev_priv: i915 device instance
  46. * @fixed_mode : panel native mode
  47. * @connector: LVDS/eDP connector
  48. *
  49. * Return downclock_avail
  50. * Find the reduced downclock for LVDS/eDP in EDID.
  51. */
  52. struct drm_display_mode *
  53. intel_find_panel_downclock(struct drm_i915_private *dev_priv,
  54. struct drm_display_mode *fixed_mode,
  55. struct drm_connector *connector)
  56. {
  57. struct drm_display_mode *scan, *tmp_mode;
  58. int temp_downclock;
  59. temp_downclock = fixed_mode->clock;
  60. tmp_mode = NULL;
  61. list_for_each_entry(scan, &connector->probed_modes, head) {
  62. /*
  63. * If one mode has the same resolution with the fixed_panel
  64. * mode while they have the different refresh rate, it means
  65. * that the reduced downclock is found. In such
  66. * case we can set the different FPx0/1 to dynamically select
  67. * between low and high frequency.
  68. */
  69. if (scan->hdisplay == fixed_mode->hdisplay &&
  70. scan->hsync_start == fixed_mode->hsync_start &&
  71. scan->hsync_end == fixed_mode->hsync_end &&
  72. scan->htotal == fixed_mode->htotal &&
  73. scan->vdisplay == fixed_mode->vdisplay &&
  74. scan->vsync_start == fixed_mode->vsync_start &&
  75. scan->vsync_end == fixed_mode->vsync_end &&
  76. scan->vtotal == fixed_mode->vtotal) {
  77. if (scan->clock < temp_downclock) {
  78. /*
  79. * The downclock is already found. But we
  80. * expect to find the lower downclock.
  81. */
  82. temp_downclock = scan->clock;
  83. tmp_mode = scan;
  84. }
  85. }
  86. }
  87. if (temp_downclock < fixed_mode->clock)
  88. return drm_mode_duplicate(&dev_priv->drm, tmp_mode);
  89. else
  90. return NULL;
  91. }
  92. /* adjusted_mode has been preset to be the panel's fixed mode */
  93. void
  94. intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
  95. struct intel_crtc_state *pipe_config,
  96. int fitting_mode)
  97. {
  98. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  99. int x = 0, y = 0, width = 0, height = 0;
  100. /* Native modes don't need fitting */
  101. if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
  102. adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
  103. goto done;
  104. switch (fitting_mode) {
  105. case DRM_MODE_SCALE_CENTER:
  106. width = pipe_config->pipe_src_w;
  107. height = pipe_config->pipe_src_h;
  108. x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
  109. y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
  110. break;
  111. case DRM_MODE_SCALE_ASPECT:
  112. /* Scale but preserve the aspect ratio */
  113. {
  114. u32 scaled_width = adjusted_mode->crtc_hdisplay
  115. * pipe_config->pipe_src_h;
  116. u32 scaled_height = pipe_config->pipe_src_w
  117. * adjusted_mode->crtc_vdisplay;
  118. if (scaled_width > scaled_height) { /* pillar */
  119. width = scaled_height / pipe_config->pipe_src_h;
  120. if (width & 1)
  121. width++;
  122. x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
  123. y = 0;
  124. height = adjusted_mode->crtc_vdisplay;
  125. } else if (scaled_width < scaled_height) { /* letter */
  126. height = scaled_width / pipe_config->pipe_src_w;
  127. if (height & 1)
  128. height++;
  129. y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
  130. x = 0;
  131. width = adjusted_mode->crtc_hdisplay;
  132. } else {
  133. x = y = 0;
  134. width = adjusted_mode->crtc_hdisplay;
  135. height = adjusted_mode->crtc_vdisplay;
  136. }
  137. }
  138. break;
  139. case DRM_MODE_SCALE_FULLSCREEN:
  140. x = y = 0;
  141. width = adjusted_mode->crtc_hdisplay;
  142. height = adjusted_mode->crtc_vdisplay;
  143. break;
  144. default:
  145. WARN(1, "bad panel fit mode: %d\n", fitting_mode);
  146. return;
  147. }
  148. done:
  149. pipe_config->pch_pfit.pos = (x << 16) | y;
  150. pipe_config->pch_pfit.size = (width << 16) | height;
  151. pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
  152. }
  153. static void
  154. centre_horizontally(struct drm_display_mode *adjusted_mode,
  155. int width)
  156. {
  157. u32 border, sync_pos, blank_width, sync_width;
  158. /* keep the hsync and hblank widths constant */
  159. sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
  160. blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
  161. sync_pos = (blank_width - sync_width + 1) / 2;
  162. border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
  163. border += border & 1; /* make the border even */
  164. adjusted_mode->crtc_hdisplay = width;
  165. adjusted_mode->crtc_hblank_start = width + border;
  166. adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
  167. adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
  168. adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
  169. }
  170. static void
  171. centre_vertically(struct drm_display_mode *adjusted_mode,
  172. int height)
  173. {
  174. u32 border, sync_pos, blank_width, sync_width;
  175. /* keep the vsync and vblank widths constant */
  176. sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
  177. blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
  178. sync_pos = (blank_width - sync_width + 1) / 2;
  179. border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
  180. adjusted_mode->crtc_vdisplay = height;
  181. adjusted_mode->crtc_vblank_start = height + border;
  182. adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
  183. adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
  184. adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
  185. }
  186. static inline u32 panel_fitter_scaling(u32 source, u32 target)
  187. {
  188. /*
  189. * Floating point operation is not supported. So the FACTOR
  190. * is defined, which can avoid the floating point computation
  191. * when calculating the panel ratio.
  192. */
  193. #define ACCURACY 12
  194. #define FACTOR (1 << ACCURACY)
  195. u32 ratio = source * FACTOR / target;
  196. return (FACTOR * ratio + FACTOR/2) / FACTOR;
  197. }
  198. static void i965_scale_aspect(struct intel_crtc_state *pipe_config,
  199. u32 *pfit_control)
  200. {
  201. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  202. u32 scaled_width = adjusted_mode->crtc_hdisplay *
  203. pipe_config->pipe_src_h;
  204. u32 scaled_height = pipe_config->pipe_src_w *
  205. adjusted_mode->crtc_vdisplay;
  206. /* 965+ is easy, it does everything in hw */
  207. if (scaled_width > scaled_height)
  208. *pfit_control |= PFIT_ENABLE |
  209. PFIT_SCALING_PILLAR;
  210. else if (scaled_width < scaled_height)
  211. *pfit_control |= PFIT_ENABLE |
  212. PFIT_SCALING_LETTER;
  213. else if (adjusted_mode->crtc_hdisplay != pipe_config->pipe_src_w)
  214. *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
  215. }
  216. static void i9xx_scale_aspect(struct intel_crtc_state *pipe_config,
  217. u32 *pfit_control, u32 *pfit_pgm_ratios,
  218. u32 *border)
  219. {
  220. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  221. u32 scaled_width = adjusted_mode->crtc_hdisplay *
  222. pipe_config->pipe_src_h;
  223. u32 scaled_height = pipe_config->pipe_src_w *
  224. adjusted_mode->crtc_vdisplay;
  225. u32 bits;
  226. /*
  227. * For earlier chips we have to calculate the scaling
  228. * ratio by hand and program it into the
  229. * PFIT_PGM_RATIO register
  230. */
  231. if (scaled_width > scaled_height) { /* pillar */
  232. centre_horizontally(adjusted_mode,
  233. scaled_height /
  234. pipe_config->pipe_src_h);
  235. *border = LVDS_BORDER_ENABLE;
  236. if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay) {
  237. bits = panel_fitter_scaling(pipe_config->pipe_src_h,
  238. adjusted_mode->crtc_vdisplay);
  239. *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
  240. bits << PFIT_VERT_SCALE_SHIFT);
  241. *pfit_control |= (PFIT_ENABLE |
  242. VERT_INTERP_BILINEAR |
  243. HORIZ_INTERP_BILINEAR);
  244. }
  245. } else if (scaled_width < scaled_height) { /* letter */
  246. centre_vertically(adjusted_mode,
  247. scaled_width /
  248. pipe_config->pipe_src_w);
  249. *border = LVDS_BORDER_ENABLE;
  250. if (pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
  251. bits = panel_fitter_scaling(pipe_config->pipe_src_w,
  252. adjusted_mode->crtc_hdisplay);
  253. *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
  254. bits << PFIT_VERT_SCALE_SHIFT);
  255. *pfit_control |= (PFIT_ENABLE |
  256. VERT_INTERP_BILINEAR |
  257. HORIZ_INTERP_BILINEAR);
  258. }
  259. } else {
  260. /* Aspects match, Let hw scale both directions */
  261. *pfit_control |= (PFIT_ENABLE |
  262. VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
  263. VERT_INTERP_BILINEAR |
  264. HORIZ_INTERP_BILINEAR);
  265. }
  266. }
  267. void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
  268. struct intel_crtc_state *pipe_config,
  269. int fitting_mode)
  270. {
  271. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  272. u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
  273. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  274. /* Native modes don't need fitting */
  275. if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
  276. adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
  277. goto out;
  278. switch (fitting_mode) {
  279. case DRM_MODE_SCALE_CENTER:
  280. /*
  281. * For centered modes, we have to calculate border widths &
  282. * heights and modify the values programmed into the CRTC.
  283. */
  284. centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
  285. centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
  286. border = LVDS_BORDER_ENABLE;
  287. break;
  288. case DRM_MODE_SCALE_ASPECT:
  289. /* Scale but preserve the aspect ratio */
  290. if (INTEL_GEN(dev_priv) >= 4)
  291. i965_scale_aspect(pipe_config, &pfit_control);
  292. else
  293. i9xx_scale_aspect(pipe_config, &pfit_control,
  294. &pfit_pgm_ratios, &border);
  295. break;
  296. case DRM_MODE_SCALE_FULLSCREEN:
  297. /*
  298. * Full scaling, even if it changes the aspect ratio.
  299. * Fortunately this is all done for us in hw.
  300. */
  301. if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay ||
  302. pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
  303. pfit_control |= PFIT_ENABLE;
  304. if (INTEL_GEN(dev_priv) >= 4)
  305. pfit_control |= PFIT_SCALING_AUTO;
  306. else
  307. pfit_control |= (VERT_AUTO_SCALE |
  308. VERT_INTERP_BILINEAR |
  309. HORIZ_AUTO_SCALE |
  310. HORIZ_INTERP_BILINEAR);
  311. }
  312. break;
  313. default:
  314. WARN(1, "bad panel fit mode: %d\n", fitting_mode);
  315. return;
  316. }
  317. /* 965+ wants fuzzy fitting */
  318. /* FIXME: handle multiple panels by failing gracefully */
  319. if (INTEL_GEN(dev_priv) >= 4)
  320. pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
  321. PFIT_FILTER_FUZZY);
  322. out:
  323. if ((pfit_control & PFIT_ENABLE) == 0) {
  324. pfit_control = 0;
  325. pfit_pgm_ratios = 0;
  326. }
  327. /* Make sure pre-965 set dither correctly for 18bpp panels. */
  328. if (INTEL_GEN(dev_priv) < 4 && pipe_config->pipe_bpp == 18)
  329. pfit_control |= PANEL_8TO6_DITHER_ENABLE;
  330. pipe_config->gmch_pfit.control = pfit_control;
  331. pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
  332. pipe_config->gmch_pfit.lvds_border_bits = border;
  333. }
  334. enum drm_connector_status
  335. intel_panel_detect(struct drm_i915_private *dev_priv)
  336. {
  337. /* Assume that the BIOS does not lie through the OpRegion... */
  338. if (!i915.panel_ignore_lid && dev_priv->opregion.lid_state) {
  339. return *dev_priv->opregion.lid_state & 0x1 ?
  340. connector_status_connected :
  341. connector_status_disconnected;
  342. }
  343. switch (i915.panel_ignore_lid) {
  344. case -2:
  345. return connector_status_connected;
  346. case -1:
  347. return connector_status_disconnected;
  348. default:
  349. return connector_status_unknown;
  350. }
  351. }
  352. /**
  353. * scale - scale values from one range to another
  354. *
  355. * @source_val: value in range [@source_min..@source_max]
  356. *
  357. * Return @source_val in range [@source_min..@source_max] scaled to range
  358. * [@target_min..@target_max].
  359. */
  360. static uint32_t scale(uint32_t source_val,
  361. uint32_t source_min, uint32_t source_max,
  362. uint32_t target_min, uint32_t target_max)
  363. {
  364. uint64_t target_val;
  365. WARN_ON(source_min > source_max);
  366. WARN_ON(target_min > target_max);
  367. /* defensive */
  368. source_val = clamp(source_val, source_min, source_max);
  369. /* avoid overflows */
  370. target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
  371. (target_max - target_min), source_max - source_min);
  372. target_val += target_min;
  373. return target_val;
  374. }
  375. /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
  376. static inline u32 scale_user_to_hw(struct intel_connector *connector,
  377. u32 user_level, u32 user_max)
  378. {
  379. struct intel_panel *panel = &connector->panel;
  380. return scale(user_level, 0, user_max,
  381. panel->backlight.min, panel->backlight.max);
  382. }
  383. /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
  384. * to [hw_min..hw_max]. */
  385. static inline u32 clamp_user_to_hw(struct intel_connector *connector,
  386. u32 user_level, u32 user_max)
  387. {
  388. struct intel_panel *panel = &connector->panel;
  389. u32 hw_level;
  390. hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
  391. hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
  392. return hw_level;
  393. }
  394. /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
  395. static inline u32 scale_hw_to_user(struct intel_connector *connector,
  396. u32 hw_level, u32 user_max)
  397. {
  398. struct intel_panel *panel = &connector->panel;
  399. return scale(hw_level, panel->backlight.min, panel->backlight.max,
  400. 0, user_max);
  401. }
  402. static u32 intel_panel_compute_brightness(struct intel_connector *connector,
  403. u32 val)
  404. {
  405. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  406. struct intel_panel *panel = &connector->panel;
  407. WARN_ON(panel->backlight.max == 0);
  408. if (i915.invert_brightness < 0)
  409. return val;
  410. if (i915.invert_brightness > 0 ||
  411. dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
  412. return panel->backlight.max - val;
  413. }
  414. return val;
  415. }
  416. static u32 lpt_get_backlight(struct intel_connector *connector)
  417. {
  418. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  419. return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
  420. }
  421. static u32 pch_get_backlight(struct intel_connector *connector)
  422. {
  423. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  424. return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  425. }
  426. static u32 i9xx_get_backlight(struct intel_connector *connector)
  427. {
  428. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  429. struct intel_panel *panel = &connector->panel;
  430. u32 val;
  431. val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  432. if (INTEL_INFO(dev_priv)->gen < 4)
  433. val >>= 1;
  434. if (panel->backlight.combination_mode) {
  435. u8 lbpc;
  436. pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
  437. val *= lbpc;
  438. }
  439. return val;
  440. }
  441. static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
  442. {
  443. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  444. return 0;
  445. return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
  446. }
  447. static u32 vlv_get_backlight(struct intel_connector *connector)
  448. {
  449. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  450. enum pipe pipe = intel_get_pipe_from_connector(connector);
  451. return _vlv_get_backlight(dev_priv, pipe);
  452. }
  453. static u32 bxt_get_backlight(struct intel_connector *connector)
  454. {
  455. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  456. struct intel_panel *panel = &connector->panel;
  457. return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
  458. }
  459. static u32 pwm_get_backlight(struct intel_connector *connector)
  460. {
  461. struct intel_panel *panel = &connector->panel;
  462. int duty_ns;
  463. duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
  464. return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
  465. }
  466. static u32 intel_panel_get_backlight(struct intel_connector *connector)
  467. {
  468. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  469. struct intel_panel *panel = &connector->panel;
  470. u32 val = 0;
  471. mutex_lock(&dev_priv->backlight_lock);
  472. if (panel->backlight.enabled) {
  473. val = panel->backlight.get(connector);
  474. val = intel_panel_compute_brightness(connector, val);
  475. }
  476. mutex_unlock(&dev_priv->backlight_lock);
  477. DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
  478. return val;
  479. }
  480. static void lpt_set_backlight(struct intel_connector *connector, u32 level)
  481. {
  482. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  483. u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  484. I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
  485. }
  486. static void pch_set_backlight(struct intel_connector *connector, u32 level)
  487. {
  488. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  489. u32 tmp;
  490. tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  491. I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
  492. }
  493. static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
  494. {
  495. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  496. struct intel_panel *panel = &connector->panel;
  497. u32 tmp, mask;
  498. WARN_ON(panel->backlight.max == 0);
  499. if (panel->backlight.combination_mode) {
  500. u8 lbpc;
  501. lbpc = level * 0xfe / panel->backlight.max + 1;
  502. level /= lbpc;
  503. pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
  504. }
  505. if (IS_GEN4(dev_priv)) {
  506. mask = BACKLIGHT_DUTY_CYCLE_MASK;
  507. } else {
  508. level <<= 1;
  509. mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
  510. }
  511. tmp = I915_READ(BLC_PWM_CTL) & ~mask;
  512. I915_WRITE(BLC_PWM_CTL, tmp | level);
  513. }
  514. static void vlv_set_backlight(struct intel_connector *connector, u32 level)
  515. {
  516. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  517. enum pipe pipe = intel_get_pipe_from_connector(connector);
  518. u32 tmp;
  519. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  520. return;
  521. tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  522. I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
  523. }
  524. static void bxt_set_backlight(struct intel_connector *connector, u32 level)
  525. {
  526. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  527. struct intel_panel *panel = &connector->panel;
  528. I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
  529. }
  530. static void pwm_set_backlight(struct intel_connector *connector, u32 level)
  531. {
  532. struct intel_panel *panel = &connector->panel;
  533. int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
  534. pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
  535. }
  536. static void
  537. intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
  538. {
  539. struct intel_panel *panel = &connector->panel;
  540. DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
  541. level = intel_panel_compute_brightness(connector, level);
  542. panel->backlight.set(connector, level);
  543. }
  544. /* set backlight brightness to level in range [0..max], scaling wrt hw min */
  545. static void intel_panel_set_backlight(struct intel_connector *connector,
  546. u32 user_level, u32 user_max)
  547. {
  548. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  549. struct intel_panel *panel = &connector->panel;
  550. u32 hw_level;
  551. if (!panel->backlight.present)
  552. return;
  553. mutex_lock(&dev_priv->backlight_lock);
  554. WARN_ON(panel->backlight.max == 0);
  555. hw_level = scale_user_to_hw(connector, user_level, user_max);
  556. panel->backlight.level = hw_level;
  557. if (panel->backlight.enabled)
  558. intel_panel_actually_set_backlight(connector, hw_level);
  559. mutex_unlock(&dev_priv->backlight_lock);
  560. }
  561. /* set backlight brightness to level in range [0..max], assuming hw min is
  562. * respected.
  563. */
  564. void intel_panel_set_backlight_acpi(struct intel_connector *connector,
  565. u32 user_level, u32 user_max)
  566. {
  567. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  568. struct intel_panel *panel = &connector->panel;
  569. enum pipe pipe = intel_get_pipe_from_connector(connector);
  570. u32 hw_level;
  571. /*
  572. * INVALID_PIPE may occur during driver init because
  573. * connection_mutex isn't held across the entire backlight
  574. * setup + modeset readout, and the BIOS can issue the
  575. * requests at any time.
  576. */
  577. if (!panel->backlight.present || pipe == INVALID_PIPE)
  578. return;
  579. mutex_lock(&dev_priv->backlight_lock);
  580. WARN_ON(panel->backlight.max == 0);
  581. hw_level = clamp_user_to_hw(connector, user_level, user_max);
  582. panel->backlight.level = hw_level;
  583. if (panel->backlight.device)
  584. panel->backlight.device->props.brightness =
  585. scale_hw_to_user(connector,
  586. panel->backlight.level,
  587. panel->backlight.device->props.max_brightness);
  588. if (panel->backlight.enabled)
  589. intel_panel_actually_set_backlight(connector, hw_level);
  590. mutex_unlock(&dev_priv->backlight_lock);
  591. }
  592. static void lpt_disable_backlight(struct intel_connector *connector)
  593. {
  594. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  595. u32 tmp;
  596. intel_panel_actually_set_backlight(connector, 0);
  597. /*
  598. * Although we don't support or enable CPU PWM with LPT/SPT based
  599. * systems, it may have been enabled prior to loading the
  600. * driver. Disable to avoid warnings on LCPLL disable.
  601. *
  602. * This needs rework if we need to add support for CPU PWM on PCH split
  603. * platforms.
  604. */
  605. tmp = I915_READ(BLC_PWM_CPU_CTL2);
  606. if (tmp & BLM_PWM_ENABLE) {
  607. DRM_DEBUG_KMS("cpu backlight was enabled, disabling\n");
  608. I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
  609. }
  610. tmp = I915_READ(BLC_PWM_PCH_CTL1);
  611. I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
  612. }
  613. static void pch_disable_backlight(struct intel_connector *connector)
  614. {
  615. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  616. u32 tmp;
  617. intel_panel_actually_set_backlight(connector, 0);
  618. tmp = I915_READ(BLC_PWM_CPU_CTL2);
  619. I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
  620. tmp = I915_READ(BLC_PWM_PCH_CTL1);
  621. I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
  622. }
  623. static void i9xx_disable_backlight(struct intel_connector *connector)
  624. {
  625. intel_panel_actually_set_backlight(connector, 0);
  626. }
  627. static void i965_disable_backlight(struct intel_connector *connector)
  628. {
  629. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  630. u32 tmp;
  631. intel_panel_actually_set_backlight(connector, 0);
  632. tmp = I915_READ(BLC_PWM_CTL2);
  633. I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
  634. }
  635. static void vlv_disable_backlight(struct intel_connector *connector)
  636. {
  637. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  638. enum pipe pipe = intel_get_pipe_from_connector(connector);
  639. u32 tmp;
  640. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  641. return;
  642. intel_panel_actually_set_backlight(connector, 0);
  643. tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
  644. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
  645. }
  646. static void bxt_disable_backlight(struct intel_connector *connector)
  647. {
  648. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  649. struct intel_panel *panel = &connector->panel;
  650. u32 tmp, val;
  651. intel_panel_actually_set_backlight(connector, 0);
  652. tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  653. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
  654. tmp & ~BXT_BLC_PWM_ENABLE);
  655. if (panel->backlight.controller == 1) {
  656. val = I915_READ(UTIL_PIN_CTL);
  657. val &= ~UTIL_PIN_ENABLE;
  658. I915_WRITE(UTIL_PIN_CTL, val);
  659. }
  660. }
  661. static void pwm_disable_backlight(struct intel_connector *connector)
  662. {
  663. struct intel_panel *panel = &connector->panel;
  664. /* Disable the backlight */
  665. pwm_config(panel->backlight.pwm, 0, CRC_PMIC_PWM_PERIOD_NS);
  666. usleep_range(2000, 3000);
  667. pwm_disable(panel->backlight.pwm);
  668. }
  669. void intel_panel_disable_backlight(struct intel_connector *connector)
  670. {
  671. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  672. struct intel_panel *panel = &connector->panel;
  673. if (!panel->backlight.present)
  674. return;
  675. /*
  676. * Do not disable backlight on the vga_switcheroo path. When switching
  677. * away from i915, the other client may depend on i915 to handle the
  678. * backlight. This will leave the backlight on unnecessarily when
  679. * another client is not activated.
  680. */
  681. if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
  682. DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
  683. return;
  684. }
  685. mutex_lock(&dev_priv->backlight_lock);
  686. if (panel->backlight.device)
  687. panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
  688. panel->backlight.enabled = false;
  689. panel->backlight.disable(connector);
  690. mutex_unlock(&dev_priv->backlight_lock);
  691. }
  692. static void lpt_enable_backlight(struct intel_connector *connector)
  693. {
  694. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  695. struct intel_panel *panel = &connector->panel;
  696. u32 pch_ctl1, pch_ctl2, schicken;
  697. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  698. if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
  699. DRM_DEBUG_KMS("pch backlight already enabled\n");
  700. pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
  701. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  702. }
  703. if (HAS_PCH_LPT(dev_priv)) {
  704. schicken = I915_READ(SOUTH_CHICKEN2);
  705. if (panel->backlight.alternate_pwm_increment)
  706. schicken |= LPT_PWM_GRANULARITY;
  707. else
  708. schicken &= ~LPT_PWM_GRANULARITY;
  709. I915_WRITE(SOUTH_CHICKEN2, schicken);
  710. } else {
  711. schicken = I915_READ(SOUTH_CHICKEN1);
  712. if (panel->backlight.alternate_pwm_increment)
  713. schicken |= SPT_PWM_GRANULARITY;
  714. else
  715. schicken &= ~SPT_PWM_GRANULARITY;
  716. I915_WRITE(SOUTH_CHICKEN1, schicken);
  717. }
  718. pch_ctl2 = panel->backlight.max << 16;
  719. I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
  720. pch_ctl1 = 0;
  721. if (panel->backlight.active_low_pwm)
  722. pch_ctl1 |= BLM_PCH_POLARITY;
  723. /* After LPT, override is the default. */
  724. if (HAS_PCH_LPT(dev_priv))
  725. pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
  726. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  727. POSTING_READ(BLC_PWM_PCH_CTL1);
  728. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
  729. /* This won't stick until the above enable. */
  730. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  731. }
  732. static void pch_enable_backlight(struct intel_connector *connector)
  733. {
  734. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  735. struct intel_panel *panel = &connector->panel;
  736. enum pipe pipe = intel_get_pipe_from_connector(connector);
  737. enum transcoder cpu_transcoder;
  738. u32 cpu_ctl2, pch_ctl1, pch_ctl2;
  739. if (!WARN_ON_ONCE(pipe == INVALID_PIPE))
  740. cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, pipe);
  741. else
  742. cpu_transcoder = TRANSCODER_EDP;
  743. cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
  744. if (cpu_ctl2 & BLM_PWM_ENABLE) {
  745. DRM_DEBUG_KMS("cpu backlight already enabled\n");
  746. cpu_ctl2 &= ~BLM_PWM_ENABLE;
  747. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
  748. }
  749. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  750. if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
  751. DRM_DEBUG_KMS("pch backlight already enabled\n");
  752. pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
  753. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  754. }
  755. if (cpu_transcoder == TRANSCODER_EDP)
  756. cpu_ctl2 = BLM_TRANSCODER_EDP;
  757. else
  758. cpu_ctl2 = BLM_PIPE(cpu_transcoder);
  759. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
  760. POSTING_READ(BLC_PWM_CPU_CTL2);
  761. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
  762. /* This won't stick until the above enable. */
  763. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  764. pch_ctl2 = panel->backlight.max << 16;
  765. I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
  766. pch_ctl1 = 0;
  767. if (panel->backlight.active_low_pwm)
  768. pch_ctl1 |= BLM_PCH_POLARITY;
  769. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  770. POSTING_READ(BLC_PWM_PCH_CTL1);
  771. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
  772. }
  773. static void i9xx_enable_backlight(struct intel_connector *connector)
  774. {
  775. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  776. struct intel_panel *panel = &connector->panel;
  777. u32 ctl, freq;
  778. ctl = I915_READ(BLC_PWM_CTL);
  779. if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
  780. DRM_DEBUG_KMS("backlight already enabled\n");
  781. I915_WRITE(BLC_PWM_CTL, 0);
  782. }
  783. freq = panel->backlight.max;
  784. if (panel->backlight.combination_mode)
  785. freq /= 0xff;
  786. ctl = freq << 17;
  787. if (panel->backlight.combination_mode)
  788. ctl |= BLM_LEGACY_MODE;
  789. if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
  790. ctl |= BLM_POLARITY_PNV;
  791. I915_WRITE(BLC_PWM_CTL, ctl);
  792. POSTING_READ(BLC_PWM_CTL);
  793. /* XXX: combine this into above write? */
  794. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  795. /*
  796. * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
  797. * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
  798. * that has backlight.
  799. */
  800. if (IS_GEN2(dev_priv))
  801. I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
  802. }
  803. static void i965_enable_backlight(struct intel_connector *connector)
  804. {
  805. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  806. struct intel_panel *panel = &connector->panel;
  807. enum pipe pipe = intel_get_pipe_from_connector(connector);
  808. u32 ctl, ctl2, freq;
  809. if (WARN_ON_ONCE(pipe == INVALID_PIPE))
  810. pipe = PIPE_A;
  811. ctl2 = I915_READ(BLC_PWM_CTL2);
  812. if (ctl2 & BLM_PWM_ENABLE) {
  813. DRM_DEBUG_KMS("backlight already enabled\n");
  814. ctl2 &= ~BLM_PWM_ENABLE;
  815. I915_WRITE(BLC_PWM_CTL2, ctl2);
  816. }
  817. freq = panel->backlight.max;
  818. if (panel->backlight.combination_mode)
  819. freq /= 0xff;
  820. ctl = freq << 16;
  821. I915_WRITE(BLC_PWM_CTL, ctl);
  822. ctl2 = BLM_PIPE(pipe);
  823. if (panel->backlight.combination_mode)
  824. ctl2 |= BLM_COMBINATION_MODE;
  825. if (panel->backlight.active_low_pwm)
  826. ctl2 |= BLM_POLARITY_I965;
  827. I915_WRITE(BLC_PWM_CTL2, ctl2);
  828. POSTING_READ(BLC_PWM_CTL2);
  829. I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
  830. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  831. }
  832. static void vlv_enable_backlight(struct intel_connector *connector)
  833. {
  834. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  835. struct intel_panel *panel = &connector->panel;
  836. enum pipe pipe = intel_get_pipe_from_connector(connector);
  837. u32 ctl, ctl2;
  838. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  839. return;
  840. ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
  841. if (ctl2 & BLM_PWM_ENABLE) {
  842. DRM_DEBUG_KMS("backlight already enabled\n");
  843. ctl2 &= ~BLM_PWM_ENABLE;
  844. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
  845. }
  846. ctl = panel->backlight.max << 16;
  847. I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
  848. /* XXX: combine this into above write? */
  849. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  850. ctl2 = 0;
  851. if (panel->backlight.active_low_pwm)
  852. ctl2 |= BLM_POLARITY_I965;
  853. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
  854. POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
  855. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
  856. }
  857. static void bxt_enable_backlight(struct intel_connector *connector)
  858. {
  859. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  860. struct intel_panel *panel = &connector->panel;
  861. enum pipe pipe = intel_get_pipe_from_connector(connector);
  862. u32 pwm_ctl, val;
  863. if (WARN_ON_ONCE(pipe == INVALID_PIPE))
  864. pipe = PIPE_A;
  865. /* Controller 1 uses the utility pin. */
  866. if (panel->backlight.controller == 1) {
  867. val = I915_READ(UTIL_PIN_CTL);
  868. if (val & UTIL_PIN_ENABLE) {
  869. DRM_DEBUG_KMS("util pin already enabled\n");
  870. val &= ~UTIL_PIN_ENABLE;
  871. I915_WRITE(UTIL_PIN_CTL, val);
  872. }
  873. val = 0;
  874. if (panel->backlight.util_pin_active_low)
  875. val |= UTIL_PIN_POLARITY;
  876. I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
  877. UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
  878. }
  879. pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  880. if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
  881. DRM_DEBUG_KMS("backlight already enabled\n");
  882. pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
  883. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
  884. pwm_ctl);
  885. }
  886. I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
  887. panel->backlight.max);
  888. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  889. pwm_ctl = 0;
  890. if (panel->backlight.active_low_pwm)
  891. pwm_ctl |= BXT_BLC_PWM_POLARITY;
  892. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
  893. POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  894. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
  895. pwm_ctl | BXT_BLC_PWM_ENABLE);
  896. }
  897. static void pwm_enable_backlight(struct intel_connector *connector)
  898. {
  899. struct intel_panel *panel = &connector->panel;
  900. pwm_enable(panel->backlight.pwm);
  901. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  902. }
  903. void intel_panel_enable_backlight(struct intel_connector *connector)
  904. {
  905. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  906. struct intel_panel *panel = &connector->panel;
  907. enum pipe pipe = intel_get_pipe_from_connector(connector);
  908. if (!panel->backlight.present)
  909. return;
  910. if (!WARN_ON_ONCE(pipe == INVALID_PIPE))
  911. DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
  912. mutex_lock(&dev_priv->backlight_lock);
  913. WARN_ON(panel->backlight.max == 0);
  914. if (panel->backlight.level <= panel->backlight.min) {
  915. panel->backlight.level = panel->backlight.max;
  916. if (panel->backlight.device)
  917. panel->backlight.device->props.brightness =
  918. scale_hw_to_user(connector,
  919. panel->backlight.level,
  920. panel->backlight.device->props.max_brightness);
  921. }
  922. panel->backlight.enable(connector);
  923. panel->backlight.enabled = true;
  924. if (panel->backlight.device)
  925. panel->backlight.device->props.power = FB_BLANK_UNBLANK;
  926. mutex_unlock(&dev_priv->backlight_lock);
  927. }
  928. #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
  929. static int intel_backlight_device_update_status(struct backlight_device *bd)
  930. {
  931. struct intel_connector *connector = bl_get_data(bd);
  932. struct intel_panel *panel = &connector->panel;
  933. struct drm_device *dev = connector->base.dev;
  934. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  935. DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
  936. bd->props.brightness, bd->props.max_brightness);
  937. intel_panel_set_backlight(connector, bd->props.brightness,
  938. bd->props.max_brightness);
  939. /*
  940. * Allow flipping bl_power as a sub-state of enabled. Sadly the
  941. * backlight class device does not make it easy to to differentiate
  942. * between callbacks for brightness and bl_power, so our backlight_power
  943. * callback needs to take this into account.
  944. */
  945. if (panel->backlight.enabled) {
  946. if (panel->backlight.power) {
  947. bool enable = bd->props.power == FB_BLANK_UNBLANK &&
  948. bd->props.brightness != 0;
  949. panel->backlight.power(connector, enable);
  950. }
  951. } else {
  952. bd->props.power = FB_BLANK_POWERDOWN;
  953. }
  954. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  955. return 0;
  956. }
  957. static int intel_backlight_device_get_brightness(struct backlight_device *bd)
  958. {
  959. struct intel_connector *connector = bl_get_data(bd);
  960. struct drm_device *dev = connector->base.dev;
  961. struct drm_i915_private *dev_priv = to_i915(dev);
  962. u32 hw_level;
  963. int ret;
  964. intel_runtime_pm_get(dev_priv);
  965. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  966. hw_level = intel_panel_get_backlight(connector);
  967. ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
  968. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  969. intel_runtime_pm_put(dev_priv);
  970. return ret;
  971. }
  972. static const struct backlight_ops intel_backlight_device_ops = {
  973. .update_status = intel_backlight_device_update_status,
  974. .get_brightness = intel_backlight_device_get_brightness,
  975. };
  976. int intel_backlight_device_register(struct intel_connector *connector)
  977. {
  978. struct intel_panel *panel = &connector->panel;
  979. struct backlight_properties props;
  980. if (WARN_ON(panel->backlight.device))
  981. return -ENODEV;
  982. if (!panel->backlight.present)
  983. return 0;
  984. WARN_ON(panel->backlight.max == 0);
  985. memset(&props, 0, sizeof(props));
  986. props.type = BACKLIGHT_RAW;
  987. /*
  988. * Note: Everything should work even if the backlight device max
  989. * presented to the userspace is arbitrarily chosen.
  990. */
  991. props.max_brightness = panel->backlight.max;
  992. props.brightness = scale_hw_to_user(connector,
  993. panel->backlight.level,
  994. props.max_brightness);
  995. if (panel->backlight.enabled)
  996. props.power = FB_BLANK_UNBLANK;
  997. else
  998. props.power = FB_BLANK_POWERDOWN;
  999. /*
  1000. * Note: using the same name independent of the connector prevents
  1001. * registration of multiple backlight devices in the driver.
  1002. */
  1003. panel->backlight.device =
  1004. backlight_device_register("intel_backlight",
  1005. connector->base.kdev,
  1006. connector,
  1007. &intel_backlight_device_ops, &props);
  1008. if (IS_ERR(panel->backlight.device)) {
  1009. DRM_ERROR("Failed to register backlight: %ld\n",
  1010. PTR_ERR(panel->backlight.device));
  1011. panel->backlight.device = NULL;
  1012. return -ENODEV;
  1013. }
  1014. DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
  1015. connector->base.name);
  1016. return 0;
  1017. }
  1018. void intel_backlight_device_unregister(struct intel_connector *connector)
  1019. {
  1020. struct intel_panel *panel = &connector->panel;
  1021. if (panel->backlight.device) {
  1022. backlight_device_unregister(panel->backlight.device);
  1023. panel->backlight.device = NULL;
  1024. }
  1025. }
  1026. #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
  1027. /*
  1028. * BXT: PWM clock frequency = 19.2 MHz.
  1029. */
  1030. static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1031. {
  1032. return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
  1033. }
  1034. /*
  1035. * SPT: This value represents the period of the PWM stream in clock periods
  1036. * multiplied by 16 (default increment) or 128 (alternate increment selected in
  1037. * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
  1038. */
  1039. static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1040. {
  1041. struct intel_panel *panel = &connector->panel;
  1042. u32 mul;
  1043. if (panel->backlight.alternate_pwm_increment)
  1044. mul = 128;
  1045. else
  1046. mul = 16;
  1047. return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
  1048. }
  1049. /*
  1050. * LPT: This value represents the period of the PWM stream in clock periods
  1051. * multiplied by 128 (default increment) or 16 (alternate increment, selected in
  1052. * LPT SOUTH_CHICKEN2 register bit 5).
  1053. */
  1054. static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1055. {
  1056. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1057. struct intel_panel *panel = &connector->panel;
  1058. u32 mul, clock;
  1059. if (panel->backlight.alternate_pwm_increment)
  1060. mul = 16;
  1061. else
  1062. mul = 128;
  1063. if (HAS_PCH_LPT_H(dev_priv))
  1064. clock = MHz(135); /* LPT:H */
  1065. else
  1066. clock = MHz(24); /* LPT:LP */
  1067. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
  1068. }
  1069. /*
  1070. * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
  1071. * display raw clocks multiplied by 128.
  1072. */
  1073. static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1074. {
  1075. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1076. return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
  1077. }
  1078. /*
  1079. * Gen2: This field determines the number of time base events (display core
  1080. * clock frequency/32) in total for a complete cycle of modulated backlight
  1081. * control.
  1082. *
  1083. * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
  1084. * divided by 32.
  1085. */
  1086. static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1087. {
  1088. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1089. int clock;
  1090. if (IS_PINEVIEW(dev_priv))
  1091. clock = KHz(dev_priv->rawclk_freq);
  1092. else
  1093. clock = KHz(dev_priv->cdclk.hw.cdclk);
  1094. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
  1095. }
  1096. /*
  1097. * Gen4: This value represents the period of the PWM stream in display core
  1098. * clocks ([DevCTG] HRAW clocks) multiplied by 128.
  1099. *
  1100. */
  1101. static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1102. {
  1103. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1104. int clock;
  1105. if (IS_G4X(dev_priv))
  1106. clock = KHz(dev_priv->rawclk_freq);
  1107. else
  1108. clock = KHz(dev_priv->cdclk.hw.cdclk);
  1109. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
  1110. }
  1111. /*
  1112. * VLV: This value represents the period of the PWM stream in display core
  1113. * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
  1114. * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
  1115. */
  1116. static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1117. {
  1118. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1119. int mul, clock;
  1120. if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
  1121. if (IS_CHERRYVIEW(dev_priv))
  1122. clock = KHz(19200);
  1123. else
  1124. clock = MHz(25);
  1125. mul = 16;
  1126. } else {
  1127. clock = KHz(dev_priv->rawclk_freq);
  1128. mul = 128;
  1129. }
  1130. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
  1131. }
  1132. static u32 get_backlight_max_vbt(struct intel_connector *connector)
  1133. {
  1134. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1135. struct intel_panel *panel = &connector->panel;
  1136. u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
  1137. u32 pwm;
  1138. if (!panel->backlight.hz_to_pwm) {
  1139. DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
  1140. return 0;
  1141. }
  1142. if (pwm_freq_hz) {
  1143. DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
  1144. pwm_freq_hz);
  1145. } else {
  1146. pwm_freq_hz = 200;
  1147. DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
  1148. pwm_freq_hz);
  1149. }
  1150. pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
  1151. if (!pwm) {
  1152. DRM_DEBUG_KMS("backlight frequency conversion failed\n");
  1153. return 0;
  1154. }
  1155. return pwm;
  1156. }
  1157. /*
  1158. * Note: The setup hooks can't assume pipe is set!
  1159. */
  1160. static u32 get_backlight_min_vbt(struct intel_connector *connector)
  1161. {
  1162. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1163. struct intel_panel *panel = &connector->panel;
  1164. int min;
  1165. WARN_ON(panel->backlight.max == 0);
  1166. /*
  1167. * XXX: If the vbt value is 255, it makes min equal to max, which leads
  1168. * to problems. There are such machines out there. Either our
  1169. * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
  1170. * against this by letting the minimum be at most (arbitrarily chosen)
  1171. * 25% of the max.
  1172. */
  1173. min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
  1174. if (min != dev_priv->vbt.backlight.min_brightness) {
  1175. DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
  1176. dev_priv->vbt.backlight.min_brightness, min);
  1177. }
  1178. /* vbt value is a coefficient in range [0..255] */
  1179. return scale(min, 0, 255, 0, panel->backlight.max);
  1180. }
  1181. static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1182. {
  1183. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1184. struct intel_panel *panel = &connector->panel;
  1185. u32 pch_ctl1, pch_ctl2, val;
  1186. bool alt;
  1187. if (HAS_PCH_LPT(dev_priv))
  1188. alt = I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
  1189. else
  1190. alt = I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
  1191. panel->backlight.alternate_pwm_increment = alt;
  1192. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  1193. panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
  1194. pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
  1195. panel->backlight.max = pch_ctl2 >> 16;
  1196. if (!panel->backlight.max)
  1197. panel->backlight.max = get_backlight_max_vbt(connector);
  1198. if (!panel->backlight.max)
  1199. return -ENODEV;
  1200. panel->backlight.min = get_backlight_min_vbt(connector);
  1201. val = lpt_get_backlight(connector);
  1202. val = intel_panel_compute_brightness(connector, val);
  1203. panel->backlight.level = clamp(val, panel->backlight.min,
  1204. panel->backlight.max);
  1205. panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
  1206. return 0;
  1207. }
  1208. static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1209. {
  1210. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1211. struct intel_panel *panel = &connector->panel;
  1212. u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
  1213. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  1214. panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
  1215. pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
  1216. panel->backlight.max = pch_ctl2 >> 16;
  1217. if (!panel->backlight.max)
  1218. panel->backlight.max = get_backlight_max_vbt(connector);
  1219. if (!panel->backlight.max)
  1220. return -ENODEV;
  1221. panel->backlight.min = get_backlight_min_vbt(connector);
  1222. val = pch_get_backlight(connector);
  1223. val = intel_panel_compute_brightness(connector, val);
  1224. panel->backlight.level = clamp(val, panel->backlight.min,
  1225. panel->backlight.max);
  1226. cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
  1227. panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
  1228. (pch_ctl1 & BLM_PCH_PWM_ENABLE);
  1229. return 0;
  1230. }
  1231. static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1232. {
  1233. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1234. struct intel_panel *panel = &connector->panel;
  1235. u32 ctl, val;
  1236. ctl = I915_READ(BLC_PWM_CTL);
  1237. if (IS_GEN2(dev_priv) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
  1238. panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
  1239. if (IS_PINEVIEW(dev_priv))
  1240. panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
  1241. panel->backlight.max = ctl >> 17;
  1242. if (!panel->backlight.max) {
  1243. panel->backlight.max = get_backlight_max_vbt(connector);
  1244. panel->backlight.max >>= 1;
  1245. }
  1246. if (!panel->backlight.max)
  1247. return -ENODEV;
  1248. if (panel->backlight.combination_mode)
  1249. panel->backlight.max *= 0xff;
  1250. panel->backlight.min = get_backlight_min_vbt(connector);
  1251. val = i9xx_get_backlight(connector);
  1252. val = intel_panel_compute_brightness(connector, val);
  1253. panel->backlight.level = clamp(val, panel->backlight.min,
  1254. panel->backlight.max);
  1255. panel->backlight.enabled = val != 0;
  1256. return 0;
  1257. }
  1258. static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1259. {
  1260. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1261. struct intel_panel *panel = &connector->panel;
  1262. u32 ctl, ctl2, val;
  1263. ctl2 = I915_READ(BLC_PWM_CTL2);
  1264. panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
  1265. panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
  1266. ctl = I915_READ(BLC_PWM_CTL);
  1267. panel->backlight.max = ctl >> 16;
  1268. if (!panel->backlight.max)
  1269. panel->backlight.max = get_backlight_max_vbt(connector);
  1270. if (!panel->backlight.max)
  1271. return -ENODEV;
  1272. if (panel->backlight.combination_mode)
  1273. panel->backlight.max *= 0xff;
  1274. panel->backlight.min = get_backlight_min_vbt(connector);
  1275. val = i9xx_get_backlight(connector);
  1276. val = intel_panel_compute_brightness(connector, val);
  1277. panel->backlight.level = clamp(val, panel->backlight.min,
  1278. panel->backlight.max);
  1279. panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
  1280. return 0;
  1281. }
  1282. static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
  1283. {
  1284. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1285. struct intel_panel *panel = &connector->panel;
  1286. u32 ctl, ctl2, val;
  1287. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  1288. return -ENODEV;
  1289. ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
  1290. panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
  1291. ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
  1292. panel->backlight.max = ctl >> 16;
  1293. if (!panel->backlight.max)
  1294. panel->backlight.max = get_backlight_max_vbt(connector);
  1295. if (!panel->backlight.max)
  1296. return -ENODEV;
  1297. panel->backlight.min = get_backlight_min_vbt(connector);
  1298. val = _vlv_get_backlight(dev_priv, pipe);
  1299. val = intel_panel_compute_brightness(connector, val);
  1300. panel->backlight.level = clamp(val, panel->backlight.min,
  1301. panel->backlight.max);
  1302. panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
  1303. return 0;
  1304. }
  1305. static int
  1306. bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1307. {
  1308. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1309. struct intel_panel *panel = &connector->panel;
  1310. u32 pwm_ctl, val;
  1311. panel->backlight.controller = dev_priv->vbt.backlight.controller;
  1312. pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  1313. /* Controller 1 uses the utility pin. */
  1314. if (panel->backlight.controller == 1) {
  1315. val = I915_READ(UTIL_PIN_CTL);
  1316. panel->backlight.util_pin_active_low =
  1317. val & UTIL_PIN_POLARITY;
  1318. }
  1319. panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
  1320. panel->backlight.max =
  1321. I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
  1322. if (!panel->backlight.max)
  1323. panel->backlight.max = get_backlight_max_vbt(connector);
  1324. if (!panel->backlight.max)
  1325. return -ENODEV;
  1326. val = bxt_get_backlight(connector);
  1327. val = intel_panel_compute_brightness(connector, val);
  1328. panel->backlight.level = clamp(val, panel->backlight.min,
  1329. panel->backlight.max);
  1330. panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
  1331. return 0;
  1332. }
  1333. static int pwm_setup_backlight(struct intel_connector *connector,
  1334. enum pipe pipe)
  1335. {
  1336. struct drm_device *dev = connector->base.dev;
  1337. struct intel_panel *panel = &connector->panel;
  1338. int retval;
  1339. /* Get the PWM chip for backlight control */
  1340. panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
  1341. if (IS_ERR(panel->backlight.pwm)) {
  1342. DRM_ERROR("Failed to own the pwm chip\n");
  1343. panel->backlight.pwm = NULL;
  1344. return -ENODEV;
  1345. }
  1346. /*
  1347. * FIXME: pwm_apply_args() should be removed when switching to
  1348. * the atomic PWM API.
  1349. */
  1350. pwm_apply_args(panel->backlight.pwm);
  1351. retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
  1352. CRC_PMIC_PWM_PERIOD_NS);
  1353. if (retval < 0) {
  1354. DRM_ERROR("Failed to configure the pwm chip\n");
  1355. pwm_put(panel->backlight.pwm);
  1356. panel->backlight.pwm = NULL;
  1357. return retval;
  1358. }
  1359. panel->backlight.min = 0; /* 0% */
  1360. panel->backlight.max = 100; /* 100% */
  1361. panel->backlight.level = DIV_ROUND_UP(
  1362. pwm_get_duty_cycle(panel->backlight.pwm) * 100,
  1363. CRC_PMIC_PWM_PERIOD_NS);
  1364. panel->backlight.enabled = panel->backlight.level != 0;
  1365. return 0;
  1366. }
  1367. int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
  1368. {
  1369. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  1370. struct intel_connector *intel_connector = to_intel_connector(connector);
  1371. struct intel_panel *panel = &intel_connector->panel;
  1372. int ret;
  1373. if (!dev_priv->vbt.backlight.present) {
  1374. if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
  1375. DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
  1376. } else {
  1377. DRM_DEBUG_KMS("no backlight present per VBT\n");
  1378. return 0;
  1379. }
  1380. }
  1381. /* ensure intel_panel has been initialized first */
  1382. if (WARN_ON(!panel->backlight.setup))
  1383. return -ENODEV;
  1384. /* set level and max in panel struct */
  1385. mutex_lock(&dev_priv->backlight_lock);
  1386. ret = panel->backlight.setup(intel_connector, pipe);
  1387. mutex_unlock(&dev_priv->backlight_lock);
  1388. if (ret) {
  1389. DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
  1390. connector->name);
  1391. return ret;
  1392. }
  1393. panel->backlight.present = true;
  1394. DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
  1395. connector->name,
  1396. enableddisabled(panel->backlight.enabled),
  1397. panel->backlight.level, panel->backlight.max);
  1398. return 0;
  1399. }
  1400. void intel_panel_destroy_backlight(struct drm_connector *connector)
  1401. {
  1402. struct intel_connector *intel_connector = to_intel_connector(connector);
  1403. struct intel_panel *panel = &intel_connector->panel;
  1404. /* dispose of the pwm */
  1405. if (panel->backlight.pwm)
  1406. pwm_put(panel->backlight.pwm);
  1407. panel->backlight.present = false;
  1408. }
  1409. /* Set up chip specific backlight functions */
  1410. static void
  1411. intel_panel_init_backlight_funcs(struct intel_panel *panel)
  1412. {
  1413. struct intel_connector *connector =
  1414. container_of(panel, struct intel_connector, panel);
  1415. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1416. if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
  1417. intel_dp_aux_init_backlight_funcs(connector) == 0)
  1418. return;
  1419. if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
  1420. intel_dsi_dcs_init_backlight_funcs(connector) == 0)
  1421. return;
  1422. if (IS_GEN9_LP(dev_priv)) {
  1423. panel->backlight.setup = bxt_setup_backlight;
  1424. panel->backlight.enable = bxt_enable_backlight;
  1425. panel->backlight.disable = bxt_disable_backlight;
  1426. panel->backlight.set = bxt_set_backlight;
  1427. panel->backlight.get = bxt_get_backlight;
  1428. panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
  1429. } else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_SPT(dev_priv) ||
  1430. HAS_PCH_KBP(dev_priv)) {
  1431. panel->backlight.setup = lpt_setup_backlight;
  1432. panel->backlight.enable = lpt_enable_backlight;
  1433. panel->backlight.disable = lpt_disable_backlight;
  1434. panel->backlight.set = lpt_set_backlight;
  1435. panel->backlight.get = lpt_get_backlight;
  1436. if (HAS_PCH_LPT(dev_priv))
  1437. panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
  1438. else
  1439. panel->backlight.hz_to_pwm = spt_hz_to_pwm;
  1440. } else if (HAS_PCH_SPLIT(dev_priv)) {
  1441. panel->backlight.setup = pch_setup_backlight;
  1442. panel->backlight.enable = pch_enable_backlight;
  1443. panel->backlight.disable = pch_disable_backlight;
  1444. panel->backlight.set = pch_set_backlight;
  1445. panel->backlight.get = pch_get_backlight;
  1446. panel->backlight.hz_to_pwm = pch_hz_to_pwm;
  1447. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1448. if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
  1449. panel->backlight.setup = pwm_setup_backlight;
  1450. panel->backlight.enable = pwm_enable_backlight;
  1451. panel->backlight.disable = pwm_disable_backlight;
  1452. panel->backlight.set = pwm_set_backlight;
  1453. panel->backlight.get = pwm_get_backlight;
  1454. } else {
  1455. panel->backlight.setup = vlv_setup_backlight;
  1456. panel->backlight.enable = vlv_enable_backlight;
  1457. panel->backlight.disable = vlv_disable_backlight;
  1458. panel->backlight.set = vlv_set_backlight;
  1459. panel->backlight.get = vlv_get_backlight;
  1460. panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
  1461. }
  1462. } else if (IS_GEN4(dev_priv)) {
  1463. panel->backlight.setup = i965_setup_backlight;
  1464. panel->backlight.enable = i965_enable_backlight;
  1465. panel->backlight.disable = i965_disable_backlight;
  1466. panel->backlight.set = i9xx_set_backlight;
  1467. panel->backlight.get = i9xx_get_backlight;
  1468. panel->backlight.hz_to_pwm = i965_hz_to_pwm;
  1469. } else {
  1470. panel->backlight.setup = i9xx_setup_backlight;
  1471. panel->backlight.enable = i9xx_enable_backlight;
  1472. panel->backlight.disable = i9xx_disable_backlight;
  1473. panel->backlight.set = i9xx_set_backlight;
  1474. panel->backlight.get = i9xx_get_backlight;
  1475. panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
  1476. }
  1477. }
  1478. int intel_panel_init(struct intel_panel *panel,
  1479. struct drm_display_mode *fixed_mode,
  1480. struct drm_display_mode *downclock_mode)
  1481. {
  1482. intel_panel_init_backlight_funcs(panel);
  1483. panel->fixed_mode = fixed_mode;
  1484. panel->downclock_mode = downclock_mode;
  1485. return 0;
  1486. }
  1487. void intel_panel_fini(struct intel_panel *panel)
  1488. {
  1489. struct intel_connector *intel_connector =
  1490. container_of(panel, struct intel_connector, panel);
  1491. if (panel->fixed_mode)
  1492. drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
  1493. if (panel->downclock_mode)
  1494. drm_mode_destroy(intel_connector->base.dev,
  1495. panel->downclock_mode);
  1496. }