intel_panel.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  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. intel_pipe_to_cpu_transcoder(dev_priv, pipe);
  739. u32 cpu_ctl2, pch_ctl1, pch_ctl2;
  740. cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
  741. if (cpu_ctl2 & BLM_PWM_ENABLE) {
  742. DRM_DEBUG_KMS("cpu backlight already enabled\n");
  743. cpu_ctl2 &= ~BLM_PWM_ENABLE;
  744. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
  745. }
  746. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  747. if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
  748. DRM_DEBUG_KMS("pch backlight already enabled\n");
  749. pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
  750. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  751. }
  752. if (cpu_transcoder == TRANSCODER_EDP)
  753. cpu_ctl2 = BLM_TRANSCODER_EDP;
  754. else
  755. cpu_ctl2 = BLM_PIPE(cpu_transcoder);
  756. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
  757. POSTING_READ(BLC_PWM_CPU_CTL2);
  758. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
  759. /* This won't stick until the above enable. */
  760. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  761. pch_ctl2 = panel->backlight.max << 16;
  762. I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
  763. pch_ctl1 = 0;
  764. if (panel->backlight.active_low_pwm)
  765. pch_ctl1 |= BLM_PCH_POLARITY;
  766. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  767. POSTING_READ(BLC_PWM_PCH_CTL1);
  768. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
  769. }
  770. static void i9xx_enable_backlight(struct intel_connector *connector)
  771. {
  772. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  773. struct intel_panel *panel = &connector->panel;
  774. u32 ctl, freq;
  775. ctl = I915_READ(BLC_PWM_CTL);
  776. if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
  777. DRM_DEBUG_KMS("backlight already enabled\n");
  778. I915_WRITE(BLC_PWM_CTL, 0);
  779. }
  780. freq = panel->backlight.max;
  781. if (panel->backlight.combination_mode)
  782. freq /= 0xff;
  783. ctl = freq << 17;
  784. if (panel->backlight.combination_mode)
  785. ctl |= BLM_LEGACY_MODE;
  786. if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
  787. ctl |= BLM_POLARITY_PNV;
  788. I915_WRITE(BLC_PWM_CTL, ctl);
  789. POSTING_READ(BLC_PWM_CTL);
  790. /* XXX: combine this into above write? */
  791. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  792. /*
  793. * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
  794. * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
  795. * that has backlight.
  796. */
  797. if (IS_GEN2(dev_priv))
  798. I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
  799. }
  800. static void i965_enable_backlight(struct intel_connector *connector)
  801. {
  802. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  803. struct intel_panel *panel = &connector->panel;
  804. enum pipe pipe = intel_get_pipe_from_connector(connector);
  805. u32 ctl, ctl2, freq;
  806. ctl2 = I915_READ(BLC_PWM_CTL2);
  807. if (ctl2 & BLM_PWM_ENABLE) {
  808. DRM_DEBUG_KMS("backlight already enabled\n");
  809. ctl2 &= ~BLM_PWM_ENABLE;
  810. I915_WRITE(BLC_PWM_CTL2, ctl2);
  811. }
  812. freq = panel->backlight.max;
  813. if (panel->backlight.combination_mode)
  814. freq /= 0xff;
  815. ctl = freq << 16;
  816. I915_WRITE(BLC_PWM_CTL, ctl);
  817. ctl2 = BLM_PIPE(pipe);
  818. if (panel->backlight.combination_mode)
  819. ctl2 |= BLM_COMBINATION_MODE;
  820. if (panel->backlight.active_low_pwm)
  821. ctl2 |= BLM_POLARITY_I965;
  822. I915_WRITE(BLC_PWM_CTL2, ctl2);
  823. POSTING_READ(BLC_PWM_CTL2);
  824. I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
  825. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  826. }
  827. static void vlv_enable_backlight(struct intel_connector *connector)
  828. {
  829. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  830. struct intel_panel *panel = &connector->panel;
  831. enum pipe pipe = intel_get_pipe_from_connector(connector);
  832. u32 ctl, ctl2;
  833. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  834. return;
  835. ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
  836. if (ctl2 & BLM_PWM_ENABLE) {
  837. DRM_DEBUG_KMS("backlight already enabled\n");
  838. ctl2 &= ~BLM_PWM_ENABLE;
  839. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
  840. }
  841. ctl = panel->backlight.max << 16;
  842. I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
  843. /* XXX: combine this into above write? */
  844. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  845. ctl2 = 0;
  846. if (panel->backlight.active_low_pwm)
  847. ctl2 |= BLM_POLARITY_I965;
  848. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
  849. POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
  850. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
  851. }
  852. static void bxt_enable_backlight(struct intel_connector *connector)
  853. {
  854. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  855. struct intel_panel *panel = &connector->panel;
  856. enum pipe pipe = intel_get_pipe_from_connector(connector);
  857. u32 pwm_ctl, val;
  858. /* Controller 1 uses the utility pin. */
  859. if (panel->backlight.controller == 1) {
  860. val = I915_READ(UTIL_PIN_CTL);
  861. if (val & UTIL_PIN_ENABLE) {
  862. DRM_DEBUG_KMS("util pin already enabled\n");
  863. val &= ~UTIL_PIN_ENABLE;
  864. I915_WRITE(UTIL_PIN_CTL, val);
  865. }
  866. val = 0;
  867. if (panel->backlight.util_pin_active_low)
  868. val |= UTIL_PIN_POLARITY;
  869. I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
  870. UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
  871. }
  872. pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  873. if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
  874. DRM_DEBUG_KMS("backlight already enabled\n");
  875. pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
  876. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
  877. pwm_ctl);
  878. }
  879. I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
  880. panel->backlight.max);
  881. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  882. pwm_ctl = 0;
  883. if (panel->backlight.active_low_pwm)
  884. pwm_ctl |= BXT_BLC_PWM_POLARITY;
  885. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
  886. POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  887. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
  888. pwm_ctl | BXT_BLC_PWM_ENABLE);
  889. }
  890. static void pwm_enable_backlight(struct intel_connector *connector)
  891. {
  892. struct intel_panel *panel = &connector->panel;
  893. pwm_enable(panel->backlight.pwm);
  894. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  895. }
  896. void intel_panel_enable_backlight(struct intel_connector *connector)
  897. {
  898. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  899. struct intel_panel *panel = &connector->panel;
  900. enum pipe pipe = intel_get_pipe_from_connector(connector);
  901. if (!panel->backlight.present)
  902. return;
  903. DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
  904. mutex_lock(&dev_priv->backlight_lock);
  905. WARN_ON(panel->backlight.max == 0);
  906. if (panel->backlight.level <= panel->backlight.min) {
  907. panel->backlight.level = panel->backlight.max;
  908. if (panel->backlight.device)
  909. panel->backlight.device->props.brightness =
  910. scale_hw_to_user(connector,
  911. panel->backlight.level,
  912. panel->backlight.device->props.max_brightness);
  913. }
  914. panel->backlight.enable(connector);
  915. panel->backlight.enabled = true;
  916. if (panel->backlight.device)
  917. panel->backlight.device->props.power = FB_BLANK_UNBLANK;
  918. mutex_unlock(&dev_priv->backlight_lock);
  919. }
  920. #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
  921. static int intel_backlight_device_update_status(struct backlight_device *bd)
  922. {
  923. struct intel_connector *connector = bl_get_data(bd);
  924. struct intel_panel *panel = &connector->panel;
  925. struct drm_device *dev = connector->base.dev;
  926. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  927. DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
  928. bd->props.brightness, bd->props.max_brightness);
  929. intel_panel_set_backlight(connector, bd->props.brightness,
  930. bd->props.max_brightness);
  931. /*
  932. * Allow flipping bl_power as a sub-state of enabled. Sadly the
  933. * backlight class device does not make it easy to to differentiate
  934. * between callbacks for brightness and bl_power, so our backlight_power
  935. * callback needs to take this into account.
  936. */
  937. if (panel->backlight.enabled) {
  938. if (panel->backlight.power) {
  939. bool enable = bd->props.power == FB_BLANK_UNBLANK &&
  940. bd->props.brightness != 0;
  941. panel->backlight.power(connector, enable);
  942. }
  943. } else {
  944. bd->props.power = FB_BLANK_POWERDOWN;
  945. }
  946. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  947. return 0;
  948. }
  949. static int intel_backlight_device_get_brightness(struct backlight_device *bd)
  950. {
  951. struct intel_connector *connector = bl_get_data(bd);
  952. struct drm_device *dev = connector->base.dev;
  953. struct drm_i915_private *dev_priv = to_i915(dev);
  954. u32 hw_level;
  955. int ret;
  956. intel_runtime_pm_get(dev_priv);
  957. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  958. hw_level = intel_panel_get_backlight(connector);
  959. ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
  960. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  961. intel_runtime_pm_put(dev_priv);
  962. return ret;
  963. }
  964. static const struct backlight_ops intel_backlight_device_ops = {
  965. .update_status = intel_backlight_device_update_status,
  966. .get_brightness = intel_backlight_device_get_brightness,
  967. };
  968. int intel_backlight_device_register(struct intel_connector *connector)
  969. {
  970. struct intel_panel *panel = &connector->panel;
  971. struct backlight_properties props;
  972. if (WARN_ON(panel->backlight.device))
  973. return -ENODEV;
  974. if (!panel->backlight.present)
  975. return 0;
  976. WARN_ON(panel->backlight.max == 0);
  977. memset(&props, 0, sizeof(props));
  978. props.type = BACKLIGHT_RAW;
  979. /*
  980. * Note: Everything should work even if the backlight device max
  981. * presented to the userspace is arbitrarily chosen.
  982. */
  983. props.max_brightness = panel->backlight.max;
  984. props.brightness = scale_hw_to_user(connector,
  985. panel->backlight.level,
  986. props.max_brightness);
  987. if (panel->backlight.enabled)
  988. props.power = FB_BLANK_UNBLANK;
  989. else
  990. props.power = FB_BLANK_POWERDOWN;
  991. /*
  992. * Note: using the same name independent of the connector prevents
  993. * registration of multiple backlight devices in the driver.
  994. */
  995. panel->backlight.device =
  996. backlight_device_register("intel_backlight",
  997. connector->base.kdev,
  998. connector,
  999. &intel_backlight_device_ops, &props);
  1000. if (IS_ERR(panel->backlight.device)) {
  1001. DRM_ERROR("Failed to register backlight: %ld\n",
  1002. PTR_ERR(panel->backlight.device));
  1003. panel->backlight.device = NULL;
  1004. return -ENODEV;
  1005. }
  1006. DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
  1007. connector->base.name);
  1008. return 0;
  1009. }
  1010. void intel_backlight_device_unregister(struct intel_connector *connector)
  1011. {
  1012. struct intel_panel *panel = &connector->panel;
  1013. if (panel->backlight.device) {
  1014. backlight_device_unregister(panel->backlight.device);
  1015. panel->backlight.device = NULL;
  1016. }
  1017. }
  1018. #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
  1019. /*
  1020. * BXT: PWM clock frequency = 19.2 MHz.
  1021. */
  1022. static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1023. {
  1024. return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
  1025. }
  1026. /*
  1027. * SPT: This value represents the period of the PWM stream in clock periods
  1028. * multiplied by 16 (default increment) or 128 (alternate increment selected in
  1029. * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
  1030. */
  1031. static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1032. {
  1033. struct intel_panel *panel = &connector->panel;
  1034. u32 mul;
  1035. if (panel->backlight.alternate_pwm_increment)
  1036. mul = 128;
  1037. else
  1038. mul = 16;
  1039. return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
  1040. }
  1041. /*
  1042. * LPT: This value represents the period of the PWM stream in clock periods
  1043. * multiplied by 128 (default increment) or 16 (alternate increment, selected in
  1044. * LPT SOUTH_CHICKEN2 register bit 5).
  1045. */
  1046. static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1047. {
  1048. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1049. struct intel_panel *panel = &connector->panel;
  1050. u32 mul, clock;
  1051. if (panel->backlight.alternate_pwm_increment)
  1052. mul = 16;
  1053. else
  1054. mul = 128;
  1055. if (HAS_PCH_LPT_H(dev_priv))
  1056. clock = MHz(135); /* LPT:H */
  1057. else
  1058. clock = MHz(24); /* LPT:LP */
  1059. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
  1060. }
  1061. /*
  1062. * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
  1063. * display raw clocks multiplied by 128.
  1064. */
  1065. static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1066. {
  1067. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1068. return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
  1069. }
  1070. /*
  1071. * Gen2: This field determines the number of time base events (display core
  1072. * clock frequency/32) in total for a complete cycle of modulated backlight
  1073. * control.
  1074. *
  1075. * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
  1076. * divided by 32.
  1077. */
  1078. static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1079. {
  1080. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1081. int clock;
  1082. if (IS_PINEVIEW(dev_priv))
  1083. clock = KHz(dev_priv->rawclk_freq);
  1084. else
  1085. clock = KHz(dev_priv->cdclk_freq);
  1086. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
  1087. }
  1088. /*
  1089. * Gen4: This value represents the period of the PWM stream in display core
  1090. * clocks ([DevCTG] HRAW clocks) multiplied by 128.
  1091. *
  1092. */
  1093. static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1094. {
  1095. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1096. int clock;
  1097. if (IS_G4X(dev_priv))
  1098. clock = KHz(dev_priv->rawclk_freq);
  1099. else
  1100. clock = KHz(dev_priv->cdclk_freq);
  1101. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
  1102. }
  1103. /*
  1104. * VLV: This value represents the period of the PWM stream in display core
  1105. * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
  1106. * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
  1107. */
  1108. static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1109. {
  1110. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1111. int mul, clock;
  1112. if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
  1113. if (IS_CHERRYVIEW(dev_priv))
  1114. clock = KHz(19200);
  1115. else
  1116. clock = MHz(25);
  1117. mul = 16;
  1118. } else {
  1119. clock = KHz(dev_priv->rawclk_freq);
  1120. mul = 128;
  1121. }
  1122. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
  1123. }
  1124. static u32 get_backlight_max_vbt(struct intel_connector *connector)
  1125. {
  1126. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1127. struct intel_panel *panel = &connector->panel;
  1128. u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
  1129. u32 pwm;
  1130. if (!panel->backlight.hz_to_pwm) {
  1131. DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
  1132. return 0;
  1133. }
  1134. if (pwm_freq_hz) {
  1135. DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
  1136. pwm_freq_hz);
  1137. } else {
  1138. pwm_freq_hz = 200;
  1139. DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
  1140. pwm_freq_hz);
  1141. }
  1142. pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
  1143. if (!pwm) {
  1144. DRM_DEBUG_KMS("backlight frequency conversion failed\n");
  1145. return 0;
  1146. }
  1147. return pwm;
  1148. }
  1149. /*
  1150. * Note: The setup hooks can't assume pipe is set!
  1151. */
  1152. static u32 get_backlight_min_vbt(struct intel_connector *connector)
  1153. {
  1154. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1155. struct intel_panel *panel = &connector->panel;
  1156. int min;
  1157. WARN_ON(panel->backlight.max == 0);
  1158. /*
  1159. * XXX: If the vbt value is 255, it makes min equal to max, which leads
  1160. * to problems. There are such machines out there. Either our
  1161. * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
  1162. * against this by letting the minimum be at most (arbitrarily chosen)
  1163. * 25% of the max.
  1164. */
  1165. min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
  1166. if (min != dev_priv->vbt.backlight.min_brightness) {
  1167. DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
  1168. dev_priv->vbt.backlight.min_brightness, min);
  1169. }
  1170. /* vbt value is a coefficient in range [0..255] */
  1171. return scale(min, 0, 255, 0, panel->backlight.max);
  1172. }
  1173. static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1174. {
  1175. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1176. struct intel_panel *panel = &connector->panel;
  1177. u32 pch_ctl1, pch_ctl2, val;
  1178. bool alt;
  1179. if (HAS_PCH_LPT(dev_priv))
  1180. alt = I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
  1181. else
  1182. alt = I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
  1183. panel->backlight.alternate_pwm_increment = alt;
  1184. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  1185. panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
  1186. pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
  1187. panel->backlight.max = pch_ctl2 >> 16;
  1188. if (!panel->backlight.max)
  1189. panel->backlight.max = get_backlight_max_vbt(connector);
  1190. if (!panel->backlight.max)
  1191. return -ENODEV;
  1192. panel->backlight.min = get_backlight_min_vbt(connector);
  1193. val = lpt_get_backlight(connector);
  1194. val = intel_panel_compute_brightness(connector, val);
  1195. panel->backlight.level = clamp(val, panel->backlight.min,
  1196. panel->backlight.max);
  1197. panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
  1198. return 0;
  1199. }
  1200. static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1201. {
  1202. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1203. struct intel_panel *panel = &connector->panel;
  1204. u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
  1205. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  1206. panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
  1207. pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
  1208. panel->backlight.max = pch_ctl2 >> 16;
  1209. if (!panel->backlight.max)
  1210. panel->backlight.max = get_backlight_max_vbt(connector);
  1211. if (!panel->backlight.max)
  1212. return -ENODEV;
  1213. panel->backlight.min = get_backlight_min_vbt(connector);
  1214. val = pch_get_backlight(connector);
  1215. val = intel_panel_compute_brightness(connector, val);
  1216. panel->backlight.level = clamp(val, panel->backlight.min,
  1217. panel->backlight.max);
  1218. cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
  1219. panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
  1220. (pch_ctl1 & BLM_PCH_PWM_ENABLE);
  1221. return 0;
  1222. }
  1223. static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1224. {
  1225. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1226. struct intel_panel *panel = &connector->panel;
  1227. u32 ctl, val;
  1228. ctl = I915_READ(BLC_PWM_CTL);
  1229. if (IS_GEN2(dev_priv) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
  1230. panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
  1231. if (IS_PINEVIEW(dev_priv))
  1232. panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
  1233. panel->backlight.max = ctl >> 17;
  1234. if (!panel->backlight.max) {
  1235. panel->backlight.max = get_backlight_max_vbt(connector);
  1236. panel->backlight.max >>= 1;
  1237. }
  1238. if (!panel->backlight.max)
  1239. return -ENODEV;
  1240. if (panel->backlight.combination_mode)
  1241. panel->backlight.max *= 0xff;
  1242. panel->backlight.min = get_backlight_min_vbt(connector);
  1243. val = i9xx_get_backlight(connector);
  1244. val = intel_panel_compute_brightness(connector, val);
  1245. panel->backlight.level = clamp(val, panel->backlight.min,
  1246. panel->backlight.max);
  1247. panel->backlight.enabled = val != 0;
  1248. return 0;
  1249. }
  1250. static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1251. {
  1252. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1253. struct intel_panel *panel = &connector->panel;
  1254. u32 ctl, ctl2, val;
  1255. ctl2 = I915_READ(BLC_PWM_CTL2);
  1256. panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
  1257. panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
  1258. ctl = I915_READ(BLC_PWM_CTL);
  1259. panel->backlight.max = ctl >> 16;
  1260. if (!panel->backlight.max)
  1261. panel->backlight.max = get_backlight_max_vbt(connector);
  1262. if (!panel->backlight.max)
  1263. return -ENODEV;
  1264. if (panel->backlight.combination_mode)
  1265. panel->backlight.max *= 0xff;
  1266. panel->backlight.min = get_backlight_min_vbt(connector);
  1267. val = i9xx_get_backlight(connector);
  1268. val = intel_panel_compute_brightness(connector, val);
  1269. panel->backlight.level = clamp(val, panel->backlight.min,
  1270. panel->backlight.max);
  1271. panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
  1272. return 0;
  1273. }
  1274. static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
  1275. {
  1276. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1277. struct intel_panel *panel = &connector->panel;
  1278. u32 ctl, ctl2, val;
  1279. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  1280. return -ENODEV;
  1281. ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
  1282. panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
  1283. ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
  1284. panel->backlight.max = ctl >> 16;
  1285. if (!panel->backlight.max)
  1286. panel->backlight.max = get_backlight_max_vbt(connector);
  1287. if (!panel->backlight.max)
  1288. return -ENODEV;
  1289. panel->backlight.min = get_backlight_min_vbt(connector);
  1290. val = _vlv_get_backlight(dev_priv, pipe);
  1291. val = intel_panel_compute_brightness(connector, val);
  1292. panel->backlight.level = clamp(val, panel->backlight.min,
  1293. panel->backlight.max);
  1294. panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
  1295. return 0;
  1296. }
  1297. static int
  1298. bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1299. {
  1300. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1301. struct intel_panel *panel = &connector->panel;
  1302. u32 pwm_ctl, val;
  1303. panel->backlight.controller = dev_priv->vbt.backlight.controller;
  1304. pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  1305. /* Controller 1 uses the utility pin. */
  1306. if (panel->backlight.controller == 1) {
  1307. val = I915_READ(UTIL_PIN_CTL);
  1308. panel->backlight.util_pin_active_low =
  1309. val & UTIL_PIN_POLARITY;
  1310. }
  1311. panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
  1312. panel->backlight.max =
  1313. I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
  1314. if (!panel->backlight.max)
  1315. panel->backlight.max = get_backlight_max_vbt(connector);
  1316. if (!panel->backlight.max)
  1317. return -ENODEV;
  1318. val = bxt_get_backlight(connector);
  1319. val = intel_panel_compute_brightness(connector, val);
  1320. panel->backlight.level = clamp(val, panel->backlight.min,
  1321. panel->backlight.max);
  1322. panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
  1323. return 0;
  1324. }
  1325. static int pwm_setup_backlight(struct intel_connector *connector,
  1326. enum pipe pipe)
  1327. {
  1328. struct drm_device *dev = connector->base.dev;
  1329. struct intel_panel *panel = &connector->panel;
  1330. int retval;
  1331. /* Get the PWM chip for backlight control */
  1332. panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
  1333. if (IS_ERR(panel->backlight.pwm)) {
  1334. DRM_ERROR("Failed to own the pwm chip\n");
  1335. panel->backlight.pwm = NULL;
  1336. return -ENODEV;
  1337. }
  1338. /*
  1339. * FIXME: pwm_apply_args() should be removed when switching to
  1340. * the atomic PWM API.
  1341. */
  1342. pwm_apply_args(panel->backlight.pwm);
  1343. retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
  1344. CRC_PMIC_PWM_PERIOD_NS);
  1345. if (retval < 0) {
  1346. DRM_ERROR("Failed to configure the pwm chip\n");
  1347. pwm_put(panel->backlight.pwm);
  1348. panel->backlight.pwm = NULL;
  1349. return retval;
  1350. }
  1351. panel->backlight.min = 0; /* 0% */
  1352. panel->backlight.max = 100; /* 100% */
  1353. panel->backlight.level = DIV_ROUND_UP(
  1354. pwm_get_duty_cycle(panel->backlight.pwm) * 100,
  1355. CRC_PMIC_PWM_PERIOD_NS);
  1356. panel->backlight.enabled = panel->backlight.level != 0;
  1357. return 0;
  1358. }
  1359. int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
  1360. {
  1361. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  1362. struct intel_connector *intel_connector = to_intel_connector(connector);
  1363. struct intel_panel *panel = &intel_connector->panel;
  1364. int ret;
  1365. if (!dev_priv->vbt.backlight.present) {
  1366. if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
  1367. DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
  1368. } else {
  1369. DRM_DEBUG_KMS("no backlight present per VBT\n");
  1370. return 0;
  1371. }
  1372. }
  1373. /* ensure intel_panel has been initialized first */
  1374. if (WARN_ON(!panel->backlight.setup))
  1375. return -ENODEV;
  1376. /* set level and max in panel struct */
  1377. mutex_lock(&dev_priv->backlight_lock);
  1378. ret = panel->backlight.setup(intel_connector, pipe);
  1379. mutex_unlock(&dev_priv->backlight_lock);
  1380. if (ret) {
  1381. DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
  1382. connector->name);
  1383. return ret;
  1384. }
  1385. panel->backlight.present = true;
  1386. DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
  1387. connector->name,
  1388. enableddisabled(panel->backlight.enabled),
  1389. panel->backlight.level, panel->backlight.max);
  1390. return 0;
  1391. }
  1392. void intel_panel_destroy_backlight(struct drm_connector *connector)
  1393. {
  1394. struct intel_connector *intel_connector = to_intel_connector(connector);
  1395. struct intel_panel *panel = &intel_connector->panel;
  1396. /* dispose of the pwm */
  1397. if (panel->backlight.pwm)
  1398. pwm_put(panel->backlight.pwm);
  1399. panel->backlight.present = false;
  1400. }
  1401. /* Set up chip specific backlight functions */
  1402. static void
  1403. intel_panel_init_backlight_funcs(struct intel_panel *panel)
  1404. {
  1405. struct intel_connector *connector =
  1406. container_of(panel, struct intel_connector, panel);
  1407. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1408. if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
  1409. intel_dp_aux_init_backlight_funcs(connector) == 0)
  1410. return;
  1411. if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
  1412. intel_dsi_dcs_init_backlight_funcs(connector) == 0)
  1413. return;
  1414. if (IS_GEN9_LP(dev_priv)) {
  1415. panel->backlight.setup = bxt_setup_backlight;
  1416. panel->backlight.enable = bxt_enable_backlight;
  1417. panel->backlight.disable = bxt_disable_backlight;
  1418. panel->backlight.set = bxt_set_backlight;
  1419. panel->backlight.get = bxt_get_backlight;
  1420. panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
  1421. } else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_SPT(dev_priv) ||
  1422. HAS_PCH_KBP(dev_priv)) {
  1423. panel->backlight.setup = lpt_setup_backlight;
  1424. panel->backlight.enable = lpt_enable_backlight;
  1425. panel->backlight.disable = lpt_disable_backlight;
  1426. panel->backlight.set = lpt_set_backlight;
  1427. panel->backlight.get = lpt_get_backlight;
  1428. if (HAS_PCH_LPT(dev_priv))
  1429. panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
  1430. else
  1431. panel->backlight.hz_to_pwm = spt_hz_to_pwm;
  1432. } else if (HAS_PCH_SPLIT(dev_priv)) {
  1433. panel->backlight.setup = pch_setup_backlight;
  1434. panel->backlight.enable = pch_enable_backlight;
  1435. panel->backlight.disable = pch_disable_backlight;
  1436. panel->backlight.set = pch_set_backlight;
  1437. panel->backlight.get = pch_get_backlight;
  1438. panel->backlight.hz_to_pwm = pch_hz_to_pwm;
  1439. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1440. if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
  1441. panel->backlight.setup = pwm_setup_backlight;
  1442. panel->backlight.enable = pwm_enable_backlight;
  1443. panel->backlight.disable = pwm_disable_backlight;
  1444. panel->backlight.set = pwm_set_backlight;
  1445. panel->backlight.get = pwm_get_backlight;
  1446. } else {
  1447. panel->backlight.setup = vlv_setup_backlight;
  1448. panel->backlight.enable = vlv_enable_backlight;
  1449. panel->backlight.disable = vlv_disable_backlight;
  1450. panel->backlight.set = vlv_set_backlight;
  1451. panel->backlight.get = vlv_get_backlight;
  1452. panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
  1453. }
  1454. } else if (IS_GEN4(dev_priv)) {
  1455. panel->backlight.setup = i965_setup_backlight;
  1456. panel->backlight.enable = i965_enable_backlight;
  1457. panel->backlight.disable = i965_disable_backlight;
  1458. panel->backlight.set = i9xx_set_backlight;
  1459. panel->backlight.get = i9xx_get_backlight;
  1460. panel->backlight.hz_to_pwm = i965_hz_to_pwm;
  1461. } else {
  1462. panel->backlight.setup = i9xx_setup_backlight;
  1463. panel->backlight.enable = i9xx_enable_backlight;
  1464. panel->backlight.disable = i9xx_disable_backlight;
  1465. panel->backlight.set = i9xx_set_backlight;
  1466. panel->backlight.get = i9xx_get_backlight;
  1467. panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
  1468. }
  1469. }
  1470. int intel_panel_init(struct intel_panel *panel,
  1471. struct drm_display_mode *fixed_mode,
  1472. struct drm_display_mode *downclock_mode)
  1473. {
  1474. intel_panel_init_backlight_funcs(panel);
  1475. panel->fixed_mode = fixed_mode;
  1476. panel->downclock_mode = downclock_mode;
  1477. return 0;
  1478. }
  1479. void intel_panel_fini(struct intel_panel *panel)
  1480. {
  1481. struct intel_connector *intel_connector =
  1482. container_of(panel, struct intel_connector, panel);
  1483. if (panel->fixed_mode)
  1484. drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
  1485. if (panel->downclock_mode)
  1486. drm_mode_destroy(intel_connector->base.dev,
  1487. panel->downclock_mode);
  1488. }