intel_panel.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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: drm device
  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_device *dev,
  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, 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_device *dev)
  336. {
  337. struct drm_i915_private *dev_priv = to_i915(dev);
  338. /* Assume that the BIOS does not lie through the OpRegion... */
  339. if (!i915.panel_ignore_lid && dev_priv->opregion.lid_state) {
  340. return *dev_priv->opregion.lid_state & 0x1 ?
  341. connector_status_connected :
  342. connector_status_disconnected;
  343. }
  344. switch (i915.panel_ignore_lid) {
  345. case -2:
  346. return connector_status_connected;
  347. case -1:
  348. return connector_status_disconnected;
  349. default:
  350. return connector_status_unknown;
  351. }
  352. }
  353. /**
  354. * scale - scale values from one range to another
  355. *
  356. * @source_val: value in range [@source_min..@source_max]
  357. *
  358. * Return @source_val in range [@source_min..@source_max] scaled to range
  359. * [@target_min..@target_max].
  360. */
  361. static uint32_t scale(uint32_t source_val,
  362. uint32_t source_min, uint32_t source_max,
  363. uint32_t target_min, uint32_t target_max)
  364. {
  365. uint64_t target_val;
  366. WARN_ON(source_min > source_max);
  367. WARN_ON(target_min > target_max);
  368. /* defensive */
  369. source_val = clamp(source_val, source_min, source_max);
  370. /* avoid overflows */
  371. target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
  372. (target_max - target_min), source_max - source_min);
  373. target_val += target_min;
  374. return target_val;
  375. }
  376. /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
  377. static inline u32 scale_user_to_hw(struct intel_connector *connector,
  378. u32 user_level, u32 user_max)
  379. {
  380. struct intel_panel *panel = &connector->panel;
  381. return scale(user_level, 0, user_max,
  382. panel->backlight.min, panel->backlight.max);
  383. }
  384. /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
  385. * to [hw_min..hw_max]. */
  386. static inline u32 clamp_user_to_hw(struct intel_connector *connector,
  387. u32 user_level, u32 user_max)
  388. {
  389. struct intel_panel *panel = &connector->panel;
  390. u32 hw_level;
  391. hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
  392. hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
  393. return hw_level;
  394. }
  395. /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
  396. static inline u32 scale_hw_to_user(struct intel_connector *connector,
  397. u32 hw_level, u32 user_max)
  398. {
  399. struct intel_panel *panel = &connector->panel;
  400. return scale(hw_level, panel->backlight.min, panel->backlight.max,
  401. 0, user_max);
  402. }
  403. static u32 intel_panel_compute_brightness(struct intel_connector *connector,
  404. u32 val)
  405. {
  406. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  407. struct intel_panel *panel = &connector->panel;
  408. WARN_ON(panel->backlight.max == 0);
  409. if (i915.invert_brightness < 0)
  410. return val;
  411. if (i915.invert_brightness > 0 ||
  412. dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
  413. return panel->backlight.max - val;
  414. }
  415. return val;
  416. }
  417. static u32 lpt_get_backlight(struct intel_connector *connector)
  418. {
  419. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  420. return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
  421. }
  422. static u32 pch_get_backlight(struct intel_connector *connector)
  423. {
  424. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  425. return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  426. }
  427. static u32 i9xx_get_backlight(struct intel_connector *connector)
  428. {
  429. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  430. struct intel_panel *panel = &connector->panel;
  431. u32 val;
  432. val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  433. if (INTEL_INFO(dev_priv)->gen < 4)
  434. val >>= 1;
  435. if (panel->backlight.combination_mode) {
  436. u8 lbpc;
  437. pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
  438. val *= lbpc;
  439. }
  440. return val;
  441. }
  442. static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
  443. {
  444. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  445. return 0;
  446. return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
  447. }
  448. static u32 vlv_get_backlight(struct intel_connector *connector)
  449. {
  450. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  451. enum pipe pipe = intel_get_pipe_from_connector(connector);
  452. return _vlv_get_backlight(dev_priv, pipe);
  453. }
  454. static u32 bxt_get_backlight(struct intel_connector *connector)
  455. {
  456. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  457. struct intel_panel *panel = &connector->panel;
  458. return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
  459. }
  460. static u32 pwm_get_backlight(struct intel_connector *connector)
  461. {
  462. struct intel_panel *panel = &connector->panel;
  463. int duty_ns;
  464. duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
  465. return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
  466. }
  467. static u32 intel_panel_get_backlight(struct intel_connector *connector)
  468. {
  469. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  470. struct intel_panel *panel = &connector->panel;
  471. u32 val = 0;
  472. mutex_lock(&dev_priv->backlight_lock);
  473. if (panel->backlight.enabled) {
  474. val = panel->backlight.get(connector);
  475. val = intel_panel_compute_brightness(connector, val);
  476. }
  477. mutex_unlock(&dev_priv->backlight_lock);
  478. DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
  479. return val;
  480. }
  481. static void lpt_set_backlight(struct intel_connector *connector, u32 level)
  482. {
  483. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  484. u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  485. I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
  486. }
  487. static void pch_set_backlight(struct intel_connector *connector, u32 level)
  488. {
  489. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  490. u32 tmp;
  491. tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  492. I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
  493. }
  494. static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
  495. {
  496. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  497. struct intel_panel *panel = &connector->panel;
  498. u32 tmp, mask;
  499. WARN_ON(panel->backlight.max == 0);
  500. if (panel->backlight.combination_mode) {
  501. u8 lbpc;
  502. lbpc = level * 0xfe / panel->backlight.max + 1;
  503. level /= lbpc;
  504. pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
  505. }
  506. if (IS_GEN4(dev_priv)) {
  507. mask = BACKLIGHT_DUTY_CYCLE_MASK;
  508. } else {
  509. level <<= 1;
  510. mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
  511. }
  512. tmp = I915_READ(BLC_PWM_CTL) & ~mask;
  513. I915_WRITE(BLC_PWM_CTL, tmp | level);
  514. }
  515. static void vlv_set_backlight(struct intel_connector *connector, u32 level)
  516. {
  517. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  518. enum pipe pipe = intel_get_pipe_from_connector(connector);
  519. u32 tmp;
  520. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  521. return;
  522. tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  523. I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
  524. }
  525. static void bxt_set_backlight(struct intel_connector *connector, u32 level)
  526. {
  527. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  528. struct intel_panel *panel = &connector->panel;
  529. I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
  530. }
  531. static void pwm_set_backlight(struct intel_connector *connector, u32 level)
  532. {
  533. struct intel_panel *panel = &connector->panel;
  534. int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
  535. pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
  536. }
  537. static void
  538. intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
  539. {
  540. struct intel_panel *panel = &connector->panel;
  541. DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
  542. level = intel_panel_compute_brightness(connector, level);
  543. panel->backlight.set(connector, level);
  544. }
  545. /* set backlight brightness to level in range [0..max], scaling wrt hw min */
  546. static void intel_panel_set_backlight(struct intel_connector *connector,
  547. u32 user_level, u32 user_max)
  548. {
  549. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  550. struct intel_panel *panel = &connector->panel;
  551. u32 hw_level;
  552. if (!panel->backlight.present)
  553. return;
  554. mutex_lock(&dev_priv->backlight_lock);
  555. WARN_ON(panel->backlight.max == 0);
  556. hw_level = scale_user_to_hw(connector, user_level, user_max);
  557. panel->backlight.level = hw_level;
  558. if (panel->backlight.enabled)
  559. intel_panel_actually_set_backlight(connector, hw_level);
  560. mutex_unlock(&dev_priv->backlight_lock);
  561. }
  562. /* set backlight brightness to level in range [0..max], assuming hw min is
  563. * respected.
  564. */
  565. void intel_panel_set_backlight_acpi(struct intel_connector *connector,
  566. u32 user_level, u32 user_max)
  567. {
  568. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  569. struct intel_panel *panel = &connector->panel;
  570. enum pipe pipe = intel_get_pipe_from_connector(connector);
  571. u32 hw_level;
  572. /*
  573. * INVALID_PIPE may occur during driver init because
  574. * connection_mutex isn't held across the entire backlight
  575. * setup + modeset readout, and the BIOS can issue the
  576. * requests at any time.
  577. */
  578. if (!panel->backlight.present || pipe == INVALID_PIPE)
  579. return;
  580. mutex_lock(&dev_priv->backlight_lock);
  581. WARN_ON(panel->backlight.max == 0);
  582. hw_level = clamp_user_to_hw(connector, user_level, user_max);
  583. panel->backlight.level = hw_level;
  584. if (panel->backlight.device)
  585. panel->backlight.device->props.brightness =
  586. scale_hw_to_user(connector,
  587. panel->backlight.level,
  588. panel->backlight.device->props.max_brightness);
  589. if (panel->backlight.enabled)
  590. intel_panel_actually_set_backlight(connector, hw_level);
  591. mutex_unlock(&dev_priv->backlight_lock);
  592. }
  593. static void lpt_disable_backlight(struct intel_connector *connector)
  594. {
  595. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  596. u32 tmp;
  597. intel_panel_actually_set_backlight(connector, 0);
  598. /*
  599. * Although we don't support or enable CPU PWM with LPT/SPT based
  600. * systems, it may have been enabled prior to loading the
  601. * driver. Disable to avoid warnings on LCPLL disable.
  602. *
  603. * This needs rework if we need to add support for CPU PWM on PCH split
  604. * platforms.
  605. */
  606. tmp = I915_READ(BLC_PWM_CPU_CTL2);
  607. if (tmp & BLM_PWM_ENABLE) {
  608. DRM_DEBUG_KMS("cpu backlight was enabled, disabling\n");
  609. I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
  610. }
  611. tmp = I915_READ(BLC_PWM_PCH_CTL1);
  612. I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
  613. }
  614. static void pch_disable_backlight(struct intel_connector *connector)
  615. {
  616. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  617. u32 tmp;
  618. intel_panel_actually_set_backlight(connector, 0);
  619. tmp = I915_READ(BLC_PWM_CPU_CTL2);
  620. I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
  621. tmp = I915_READ(BLC_PWM_PCH_CTL1);
  622. I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
  623. }
  624. static void i9xx_disable_backlight(struct intel_connector *connector)
  625. {
  626. intel_panel_actually_set_backlight(connector, 0);
  627. }
  628. static void i965_disable_backlight(struct intel_connector *connector)
  629. {
  630. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  631. u32 tmp;
  632. intel_panel_actually_set_backlight(connector, 0);
  633. tmp = I915_READ(BLC_PWM_CTL2);
  634. I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
  635. }
  636. static void vlv_disable_backlight(struct intel_connector *connector)
  637. {
  638. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  639. enum pipe pipe = intel_get_pipe_from_connector(connector);
  640. u32 tmp;
  641. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  642. return;
  643. intel_panel_actually_set_backlight(connector, 0);
  644. tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
  645. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
  646. }
  647. static void bxt_disable_backlight(struct intel_connector *connector)
  648. {
  649. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  650. struct intel_panel *panel = &connector->panel;
  651. u32 tmp, val;
  652. intel_panel_actually_set_backlight(connector, 0);
  653. tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  654. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
  655. tmp & ~BXT_BLC_PWM_ENABLE);
  656. if (panel->backlight.controller == 1) {
  657. val = I915_READ(UTIL_PIN_CTL);
  658. val &= ~UTIL_PIN_ENABLE;
  659. I915_WRITE(UTIL_PIN_CTL, val);
  660. }
  661. }
  662. static void pwm_disable_backlight(struct intel_connector *connector)
  663. {
  664. struct intel_panel *panel = &connector->panel;
  665. /* Disable the backlight */
  666. pwm_config(panel->backlight.pwm, 0, CRC_PMIC_PWM_PERIOD_NS);
  667. usleep_range(2000, 3000);
  668. pwm_disable(panel->backlight.pwm);
  669. }
  670. void intel_panel_disable_backlight(struct intel_connector *connector)
  671. {
  672. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  673. struct intel_panel *panel = &connector->panel;
  674. if (!panel->backlight.present)
  675. return;
  676. /*
  677. * Do not disable backlight on the vga_switcheroo path. When switching
  678. * away from i915, the other client may depend on i915 to handle the
  679. * backlight. This will leave the backlight on unnecessarily when
  680. * another client is not activated.
  681. */
  682. if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
  683. DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
  684. return;
  685. }
  686. mutex_lock(&dev_priv->backlight_lock);
  687. if (panel->backlight.device)
  688. panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
  689. panel->backlight.enabled = false;
  690. panel->backlight.disable(connector);
  691. mutex_unlock(&dev_priv->backlight_lock);
  692. }
  693. static void lpt_enable_backlight(struct intel_connector *connector)
  694. {
  695. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  696. struct intel_panel *panel = &connector->panel;
  697. u32 pch_ctl1, pch_ctl2, schicken;
  698. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  699. if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
  700. DRM_DEBUG_KMS("pch backlight already enabled\n");
  701. pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
  702. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  703. }
  704. if (HAS_PCH_LPT(dev_priv)) {
  705. schicken = I915_READ(SOUTH_CHICKEN2);
  706. if (panel->backlight.alternate_pwm_increment)
  707. schicken |= LPT_PWM_GRANULARITY;
  708. else
  709. schicken &= ~LPT_PWM_GRANULARITY;
  710. I915_WRITE(SOUTH_CHICKEN2, schicken);
  711. } else {
  712. schicken = I915_READ(SOUTH_CHICKEN1);
  713. if (panel->backlight.alternate_pwm_increment)
  714. schicken |= SPT_PWM_GRANULARITY;
  715. else
  716. schicken &= ~SPT_PWM_GRANULARITY;
  717. I915_WRITE(SOUTH_CHICKEN1, schicken);
  718. }
  719. pch_ctl2 = panel->backlight.max << 16;
  720. I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
  721. pch_ctl1 = 0;
  722. if (panel->backlight.active_low_pwm)
  723. pch_ctl1 |= BLM_PCH_POLARITY;
  724. /* After LPT, override is the default. */
  725. if (HAS_PCH_LPT(dev_priv))
  726. pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
  727. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  728. POSTING_READ(BLC_PWM_PCH_CTL1);
  729. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
  730. /* This won't stick until the above enable. */
  731. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  732. }
  733. static void pch_enable_backlight(struct intel_connector *connector)
  734. {
  735. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  736. struct intel_panel *panel = &connector->panel;
  737. enum pipe pipe = intel_get_pipe_from_connector(connector);
  738. enum transcoder cpu_transcoder =
  739. intel_pipe_to_cpu_transcoder(dev_priv, pipe);
  740. u32 cpu_ctl2, pch_ctl1, pch_ctl2;
  741. cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
  742. if (cpu_ctl2 & BLM_PWM_ENABLE) {
  743. DRM_DEBUG_KMS("cpu backlight already enabled\n");
  744. cpu_ctl2 &= ~BLM_PWM_ENABLE;
  745. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
  746. }
  747. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  748. if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
  749. DRM_DEBUG_KMS("pch backlight already enabled\n");
  750. pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
  751. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  752. }
  753. if (cpu_transcoder == TRANSCODER_EDP)
  754. cpu_ctl2 = BLM_TRANSCODER_EDP;
  755. else
  756. cpu_ctl2 = BLM_PIPE(cpu_transcoder);
  757. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
  758. POSTING_READ(BLC_PWM_CPU_CTL2);
  759. I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
  760. /* This won't stick until the above enable. */
  761. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  762. pch_ctl2 = panel->backlight.max << 16;
  763. I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
  764. pch_ctl1 = 0;
  765. if (panel->backlight.active_low_pwm)
  766. pch_ctl1 |= BLM_PCH_POLARITY;
  767. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
  768. POSTING_READ(BLC_PWM_PCH_CTL1);
  769. I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
  770. }
  771. static void i9xx_enable_backlight(struct intel_connector *connector)
  772. {
  773. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  774. struct intel_panel *panel = &connector->panel;
  775. u32 ctl, freq;
  776. ctl = I915_READ(BLC_PWM_CTL);
  777. if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
  778. DRM_DEBUG_KMS("backlight already enabled\n");
  779. I915_WRITE(BLC_PWM_CTL, 0);
  780. }
  781. freq = panel->backlight.max;
  782. if (panel->backlight.combination_mode)
  783. freq /= 0xff;
  784. ctl = freq << 17;
  785. if (panel->backlight.combination_mode)
  786. ctl |= BLM_LEGACY_MODE;
  787. if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
  788. ctl |= BLM_POLARITY_PNV;
  789. I915_WRITE(BLC_PWM_CTL, ctl);
  790. POSTING_READ(BLC_PWM_CTL);
  791. /* XXX: combine this into above write? */
  792. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  793. /*
  794. * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
  795. * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
  796. * that has backlight.
  797. */
  798. if (IS_GEN2(dev_priv))
  799. I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
  800. }
  801. static void i965_enable_backlight(struct intel_connector *connector)
  802. {
  803. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  804. struct intel_panel *panel = &connector->panel;
  805. enum pipe pipe = intel_get_pipe_from_connector(connector);
  806. u32 ctl, ctl2, freq;
  807. ctl2 = I915_READ(BLC_PWM_CTL2);
  808. if (ctl2 & BLM_PWM_ENABLE) {
  809. DRM_DEBUG_KMS("backlight already enabled\n");
  810. ctl2 &= ~BLM_PWM_ENABLE;
  811. I915_WRITE(BLC_PWM_CTL2, ctl2);
  812. }
  813. freq = panel->backlight.max;
  814. if (panel->backlight.combination_mode)
  815. freq /= 0xff;
  816. ctl = freq << 16;
  817. I915_WRITE(BLC_PWM_CTL, ctl);
  818. ctl2 = BLM_PIPE(pipe);
  819. if (panel->backlight.combination_mode)
  820. ctl2 |= BLM_COMBINATION_MODE;
  821. if (panel->backlight.active_low_pwm)
  822. ctl2 |= BLM_POLARITY_I965;
  823. I915_WRITE(BLC_PWM_CTL2, ctl2);
  824. POSTING_READ(BLC_PWM_CTL2);
  825. I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
  826. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  827. }
  828. static void vlv_enable_backlight(struct intel_connector *connector)
  829. {
  830. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  831. struct intel_panel *panel = &connector->panel;
  832. enum pipe pipe = intel_get_pipe_from_connector(connector);
  833. u32 ctl, ctl2;
  834. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  835. return;
  836. ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
  837. if (ctl2 & BLM_PWM_ENABLE) {
  838. DRM_DEBUG_KMS("backlight already enabled\n");
  839. ctl2 &= ~BLM_PWM_ENABLE;
  840. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
  841. }
  842. ctl = panel->backlight.max << 16;
  843. I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
  844. /* XXX: combine this into above write? */
  845. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  846. ctl2 = 0;
  847. if (panel->backlight.active_low_pwm)
  848. ctl2 |= BLM_POLARITY_I965;
  849. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
  850. POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
  851. I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
  852. }
  853. static void bxt_enable_backlight(struct intel_connector *connector)
  854. {
  855. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  856. struct intel_panel *panel = &connector->panel;
  857. enum pipe pipe = intel_get_pipe_from_connector(connector);
  858. u32 pwm_ctl, val;
  859. /* Controller 1 uses the utility pin. */
  860. if (panel->backlight.controller == 1) {
  861. val = I915_READ(UTIL_PIN_CTL);
  862. if (val & UTIL_PIN_ENABLE) {
  863. DRM_DEBUG_KMS("util pin already enabled\n");
  864. val &= ~UTIL_PIN_ENABLE;
  865. I915_WRITE(UTIL_PIN_CTL, val);
  866. }
  867. val = 0;
  868. if (panel->backlight.util_pin_active_low)
  869. val |= UTIL_PIN_POLARITY;
  870. I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
  871. UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
  872. }
  873. pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  874. if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
  875. DRM_DEBUG_KMS("backlight already enabled\n");
  876. pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
  877. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
  878. pwm_ctl);
  879. }
  880. I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
  881. panel->backlight.max);
  882. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  883. pwm_ctl = 0;
  884. if (panel->backlight.active_low_pwm)
  885. pwm_ctl |= BXT_BLC_PWM_POLARITY;
  886. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
  887. POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  888. I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
  889. pwm_ctl | BXT_BLC_PWM_ENABLE);
  890. }
  891. static void pwm_enable_backlight(struct intel_connector *connector)
  892. {
  893. struct intel_panel *panel = &connector->panel;
  894. pwm_enable(panel->backlight.pwm);
  895. intel_panel_actually_set_backlight(connector, panel->backlight.level);
  896. }
  897. void intel_panel_enable_backlight(struct intel_connector *connector)
  898. {
  899. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  900. struct intel_panel *panel = &connector->panel;
  901. enum pipe pipe = intel_get_pipe_from_connector(connector);
  902. if (!panel->backlight.present)
  903. return;
  904. DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
  905. mutex_lock(&dev_priv->backlight_lock);
  906. WARN_ON(panel->backlight.max == 0);
  907. if (panel->backlight.level <= panel->backlight.min) {
  908. panel->backlight.level = panel->backlight.max;
  909. if (panel->backlight.device)
  910. panel->backlight.device->props.brightness =
  911. scale_hw_to_user(connector,
  912. panel->backlight.level,
  913. panel->backlight.device->props.max_brightness);
  914. }
  915. panel->backlight.enable(connector);
  916. panel->backlight.enabled = true;
  917. if (panel->backlight.device)
  918. panel->backlight.device->props.power = FB_BLANK_UNBLANK;
  919. mutex_unlock(&dev_priv->backlight_lock);
  920. }
  921. #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
  922. static int intel_backlight_device_update_status(struct backlight_device *bd)
  923. {
  924. struct intel_connector *connector = bl_get_data(bd);
  925. struct intel_panel *panel = &connector->panel;
  926. struct drm_device *dev = connector->base.dev;
  927. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  928. DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
  929. bd->props.brightness, bd->props.max_brightness);
  930. intel_panel_set_backlight(connector, bd->props.brightness,
  931. bd->props.max_brightness);
  932. /*
  933. * Allow flipping bl_power as a sub-state of enabled. Sadly the
  934. * backlight class device does not make it easy to to differentiate
  935. * between callbacks for brightness and bl_power, so our backlight_power
  936. * callback needs to take this into account.
  937. */
  938. if (panel->backlight.enabled) {
  939. if (panel->backlight.power) {
  940. bool enable = bd->props.power == FB_BLANK_UNBLANK &&
  941. bd->props.brightness != 0;
  942. panel->backlight.power(connector, enable);
  943. }
  944. } else {
  945. bd->props.power = FB_BLANK_POWERDOWN;
  946. }
  947. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  948. return 0;
  949. }
  950. static int intel_backlight_device_get_brightness(struct backlight_device *bd)
  951. {
  952. struct intel_connector *connector = bl_get_data(bd);
  953. struct drm_device *dev = connector->base.dev;
  954. struct drm_i915_private *dev_priv = to_i915(dev);
  955. u32 hw_level;
  956. int ret;
  957. intel_runtime_pm_get(dev_priv);
  958. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  959. hw_level = intel_panel_get_backlight(connector);
  960. ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
  961. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  962. intel_runtime_pm_put(dev_priv);
  963. return ret;
  964. }
  965. static const struct backlight_ops intel_backlight_device_ops = {
  966. .update_status = intel_backlight_device_update_status,
  967. .get_brightness = intel_backlight_device_get_brightness,
  968. };
  969. int intel_backlight_device_register(struct intel_connector *connector)
  970. {
  971. struct intel_panel *panel = &connector->panel;
  972. struct backlight_properties props;
  973. if (WARN_ON(panel->backlight.device))
  974. return -ENODEV;
  975. if (!panel->backlight.present)
  976. return 0;
  977. WARN_ON(panel->backlight.max == 0);
  978. memset(&props, 0, sizeof(props));
  979. props.type = BACKLIGHT_RAW;
  980. /*
  981. * Note: Everything should work even if the backlight device max
  982. * presented to the userspace is arbitrarily chosen.
  983. */
  984. props.max_brightness = panel->backlight.max;
  985. props.brightness = scale_hw_to_user(connector,
  986. panel->backlight.level,
  987. props.max_brightness);
  988. if (panel->backlight.enabled)
  989. props.power = FB_BLANK_UNBLANK;
  990. else
  991. props.power = FB_BLANK_POWERDOWN;
  992. /*
  993. * Note: using the same name independent of the connector prevents
  994. * registration of multiple backlight devices in the driver.
  995. */
  996. panel->backlight.device =
  997. backlight_device_register("intel_backlight",
  998. connector->base.kdev,
  999. connector,
  1000. &intel_backlight_device_ops, &props);
  1001. if (IS_ERR(panel->backlight.device)) {
  1002. DRM_ERROR("Failed to register backlight: %ld\n",
  1003. PTR_ERR(panel->backlight.device));
  1004. panel->backlight.device = NULL;
  1005. return -ENODEV;
  1006. }
  1007. DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
  1008. connector->base.name);
  1009. return 0;
  1010. }
  1011. void intel_backlight_device_unregister(struct intel_connector *connector)
  1012. {
  1013. struct intel_panel *panel = &connector->panel;
  1014. if (panel->backlight.device) {
  1015. backlight_device_unregister(panel->backlight.device);
  1016. panel->backlight.device = NULL;
  1017. }
  1018. }
  1019. #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
  1020. /*
  1021. * BXT: PWM clock frequency = 19.2 MHz.
  1022. */
  1023. static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1024. {
  1025. return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
  1026. }
  1027. /*
  1028. * SPT: This value represents the period of the PWM stream in clock periods
  1029. * multiplied by 16 (default increment) or 128 (alternate increment selected in
  1030. * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
  1031. */
  1032. static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1033. {
  1034. struct intel_panel *panel = &connector->panel;
  1035. u32 mul;
  1036. if (panel->backlight.alternate_pwm_increment)
  1037. mul = 128;
  1038. else
  1039. mul = 16;
  1040. return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
  1041. }
  1042. /*
  1043. * LPT: This value represents the period of the PWM stream in clock periods
  1044. * multiplied by 128 (default increment) or 16 (alternate increment, selected in
  1045. * LPT SOUTH_CHICKEN2 register bit 5).
  1046. */
  1047. static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1048. {
  1049. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1050. struct intel_panel *panel = &connector->panel;
  1051. u32 mul, clock;
  1052. if (panel->backlight.alternate_pwm_increment)
  1053. mul = 16;
  1054. else
  1055. mul = 128;
  1056. if (HAS_PCH_LPT_H(dev_priv))
  1057. clock = MHz(135); /* LPT:H */
  1058. else
  1059. clock = MHz(24); /* LPT:LP */
  1060. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
  1061. }
  1062. /*
  1063. * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
  1064. * display raw clocks multiplied by 128.
  1065. */
  1066. static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1067. {
  1068. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1069. return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
  1070. }
  1071. /*
  1072. * Gen2: This field determines the number of time base events (display core
  1073. * clock frequency/32) in total for a complete cycle of modulated backlight
  1074. * control.
  1075. *
  1076. * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
  1077. * divided by 32.
  1078. */
  1079. static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1080. {
  1081. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1082. int clock;
  1083. if (IS_PINEVIEW(dev_priv))
  1084. clock = KHz(dev_priv->rawclk_freq);
  1085. else
  1086. clock = KHz(dev_priv->cdclk_freq);
  1087. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
  1088. }
  1089. /*
  1090. * Gen4: This value represents the period of the PWM stream in display core
  1091. * clocks ([DevCTG] HRAW clocks) multiplied by 128.
  1092. *
  1093. */
  1094. static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1095. {
  1096. struct drm_device *dev = connector->base.dev;
  1097. struct drm_i915_private *dev_priv = to_i915(dev);
  1098. int clock;
  1099. if (IS_G4X(dev_priv))
  1100. clock = KHz(dev_priv->rawclk_freq);
  1101. else
  1102. clock = KHz(dev_priv->cdclk_freq);
  1103. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
  1104. }
  1105. /*
  1106. * VLV: This value represents the period of the PWM stream in display core
  1107. * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
  1108. * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
  1109. */
  1110. static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
  1111. {
  1112. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1113. int mul, clock;
  1114. if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
  1115. if (IS_CHERRYVIEW(dev_priv))
  1116. clock = KHz(19200);
  1117. else
  1118. clock = MHz(25);
  1119. mul = 16;
  1120. } else {
  1121. clock = KHz(dev_priv->rawclk_freq);
  1122. mul = 128;
  1123. }
  1124. return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
  1125. }
  1126. static u32 get_backlight_max_vbt(struct intel_connector *connector)
  1127. {
  1128. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1129. struct intel_panel *panel = &connector->panel;
  1130. u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
  1131. u32 pwm;
  1132. if (!panel->backlight.hz_to_pwm) {
  1133. DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
  1134. return 0;
  1135. }
  1136. if (pwm_freq_hz) {
  1137. DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
  1138. pwm_freq_hz);
  1139. } else {
  1140. pwm_freq_hz = 200;
  1141. DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
  1142. pwm_freq_hz);
  1143. }
  1144. pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
  1145. if (!pwm) {
  1146. DRM_DEBUG_KMS("backlight frequency conversion failed\n");
  1147. return 0;
  1148. }
  1149. return pwm;
  1150. }
  1151. /*
  1152. * Note: The setup hooks can't assume pipe is set!
  1153. */
  1154. static u32 get_backlight_min_vbt(struct intel_connector *connector)
  1155. {
  1156. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1157. struct intel_panel *panel = &connector->panel;
  1158. int min;
  1159. WARN_ON(panel->backlight.max == 0);
  1160. /*
  1161. * XXX: If the vbt value is 255, it makes min equal to max, which leads
  1162. * to problems. There are such machines out there. Either our
  1163. * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
  1164. * against this by letting the minimum be at most (arbitrarily chosen)
  1165. * 25% of the max.
  1166. */
  1167. min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
  1168. if (min != dev_priv->vbt.backlight.min_brightness) {
  1169. DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
  1170. dev_priv->vbt.backlight.min_brightness, min);
  1171. }
  1172. /* vbt value is a coefficient in range [0..255] */
  1173. return scale(min, 0, 255, 0, panel->backlight.max);
  1174. }
  1175. static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1176. {
  1177. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1178. struct intel_panel *panel = &connector->panel;
  1179. u32 pch_ctl1, pch_ctl2, val;
  1180. bool alt;
  1181. if (HAS_PCH_LPT(dev_priv))
  1182. alt = I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
  1183. else
  1184. alt = I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
  1185. panel->backlight.alternate_pwm_increment = alt;
  1186. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  1187. panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
  1188. pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
  1189. panel->backlight.max = pch_ctl2 >> 16;
  1190. if (!panel->backlight.max)
  1191. panel->backlight.max = get_backlight_max_vbt(connector);
  1192. if (!panel->backlight.max)
  1193. return -ENODEV;
  1194. panel->backlight.min = get_backlight_min_vbt(connector);
  1195. val = lpt_get_backlight(connector);
  1196. val = intel_panel_compute_brightness(connector, val);
  1197. panel->backlight.level = clamp(val, panel->backlight.min,
  1198. panel->backlight.max);
  1199. panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
  1200. return 0;
  1201. }
  1202. static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1203. {
  1204. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1205. struct intel_panel *panel = &connector->panel;
  1206. u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
  1207. pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
  1208. panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
  1209. pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
  1210. panel->backlight.max = pch_ctl2 >> 16;
  1211. if (!panel->backlight.max)
  1212. panel->backlight.max = get_backlight_max_vbt(connector);
  1213. if (!panel->backlight.max)
  1214. return -ENODEV;
  1215. panel->backlight.min = get_backlight_min_vbt(connector);
  1216. val = pch_get_backlight(connector);
  1217. val = intel_panel_compute_brightness(connector, val);
  1218. panel->backlight.level = clamp(val, panel->backlight.min,
  1219. panel->backlight.max);
  1220. cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
  1221. panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
  1222. (pch_ctl1 & BLM_PCH_PWM_ENABLE);
  1223. return 0;
  1224. }
  1225. static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1226. {
  1227. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1228. struct intel_panel *panel = &connector->panel;
  1229. u32 ctl, val;
  1230. ctl = I915_READ(BLC_PWM_CTL);
  1231. if (IS_GEN2(dev_priv) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
  1232. panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
  1233. if (IS_PINEVIEW(dev_priv))
  1234. panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
  1235. panel->backlight.max = ctl >> 17;
  1236. if (!panel->backlight.max) {
  1237. panel->backlight.max = get_backlight_max_vbt(connector);
  1238. panel->backlight.max >>= 1;
  1239. }
  1240. if (!panel->backlight.max)
  1241. return -ENODEV;
  1242. if (panel->backlight.combination_mode)
  1243. panel->backlight.max *= 0xff;
  1244. panel->backlight.min = get_backlight_min_vbt(connector);
  1245. val = i9xx_get_backlight(connector);
  1246. val = intel_panel_compute_brightness(connector, val);
  1247. panel->backlight.level = clamp(val, panel->backlight.min,
  1248. panel->backlight.max);
  1249. panel->backlight.enabled = val != 0;
  1250. return 0;
  1251. }
  1252. static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1253. {
  1254. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1255. struct intel_panel *panel = &connector->panel;
  1256. u32 ctl, ctl2, val;
  1257. ctl2 = I915_READ(BLC_PWM_CTL2);
  1258. panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
  1259. panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
  1260. ctl = I915_READ(BLC_PWM_CTL);
  1261. panel->backlight.max = ctl >> 16;
  1262. if (!panel->backlight.max)
  1263. panel->backlight.max = get_backlight_max_vbt(connector);
  1264. if (!panel->backlight.max)
  1265. return -ENODEV;
  1266. if (panel->backlight.combination_mode)
  1267. panel->backlight.max *= 0xff;
  1268. panel->backlight.min = get_backlight_min_vbt(connector);
  1269. val = i9xx_get_backlight(connector);
  1270. val = intel_panel_compute_brightness(connector, val);
  1271. panel->backlight.level = clamp(val, panel->backlight.min,
  1272. panel->backlight.max);
  1273. panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
  1274. return 0;
  1275. }
  1276. static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
  1277. {
  1278. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1279. struct intel_panel *panel = &connector->panel;
  1280. u32 ctl, ctl2, val;
  1281. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  1282. return -ENODEV;
  1283. ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
  1284. panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
  1285. ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
  1286. panel->backlight.max = ctl >> 16;
  1287. if (!panel->backlight.max)
  1288. panel->backlight.max = get_backlight_max_vbt(connector);
  1289. if (!panel->backlight.max)
  1290. return -ENODEV;
  1291. panel->backlight.min = get_backlight_min_vbt(connector);
  1292. val = _vlv_get_backlight(dev_priv, pipe);
  1293. val = intel_panel_compute_brightness(connector, val);
  1294. panel->backlight.level = clamp(val, panel->backlight.min,
  1295. panel->backlight.max);
  1296. panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
  1297. return 0;
  1298. }
  1299. static int
  1300. bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
  1301. {
  1302. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1303. struct intel_panel *panel = &connector->panel;
  1304. u32 pwm_ctl, val;
  1305. panel->backlight.controller = dev_priv->vbt.backlight.controller;
  1306. pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
  1307. /* Controller 1 uses the utility pin. */
  1308. if (panel->backlight.controller == 1) {
  1309. val = I915_READ(UTIL_PIN_CTL);
  1310. panel->backlight.util_pin_active_low =
  1311. val & UTIL_PIN_POLARITY;
  1312. }
  1313. panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
  1314. panel->backlight.max =
  1315. I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
  1316. if (!panel->backlight.max)
  1317. panel->backlight.max = get_backlight_max_vbt(connector);
  1318. if (!panel->backlight.max)
  1319. return -ENODEV;
  1320. val = bxt_get_backlight(connector);
  1321. val = intel_panel_compute_brightness(connector, val);
  1322. panel->backlight.level = clamp(val, panel->backlight.min,
  1323. panel->backlight.max);
  1324. panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
  1325. return 0;
  1326. }
  1327. static int pwm_setup_backlight(struct intel_connector *connector,
  1328. enum pipe pipe)
  1329. {
  1330. struct drm_device *dev = connector->base.dev;
  1331. struct intel_panel *panel = &connector->panel;
  1332. int retval;
  1333. /* Get the PWM chip for backlight control */
  1334. panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
  1335. if (IS_ERR(panel->backlight.pwm)) {
  1336. DRM_ERROR("Failed to own the pwm chip\n");
  1337. panel->backlight.pwm = NULL;
  1338. return -ENODEV;
  1339. }
  1340. /*
  1341. * FIXME: pwm_apply_args() should be removed when switching to
  1342. * the atomic PWM API.
  1343. */
  1344. pwm_apply_args(panel->backlight.pwm);
  1345. retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
  1346. CRC_PMIC_PWM_PERIOD_NS);
  1347. if (retval < 0) {
  1348. DRM_ERROR("Failed to configure the pwm chip\n");
  1349. pwm_put(panel->backlight.pwm);
  1350. panel->backlight.pwm = NULL;
  1351. return retval;
  1352. }
  1353. panel->backlight.min = 0; /* 0% */
  1354. panel->backlight.max = 100; /* 100% */
  1355. panel->backlight.level = DIV_ROUND_UP(
  1356. pwm_get_duty_cycle(panel->backlight.pwm) * 100,
  1357. CRC_PMIC_PWM_PERIOD_NS);
  1358. panel->backlight.enabled = panel->backlight.level != 0;
  1359. return 0;
  1360. }
  1361. int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
  1362. {
  1363. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  1364. struct intel_connector *intel_connector = to_intel_connector(connector);
  1365. struct intel_panel *panel = &intel_connector->panel;
  1366. int ret;
  1367. if (!dev_priv->vbt.backlight.present) {
  1368. if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
  1369. DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
  1370. } else {
  1371. DRM_DEBUG_KMS("no backlight present per VBT\n");
  1372. return 0;
  1373. }
  1374. }
  1375. /* ensure intel_panel has been initialized first */
  1376. if (WARN_ON(!panel->backlight.setup))
  1377. return -ENODEV;
  1378. /* set level and max in panel struct */
  1379. mutex_lock(&dev_priv->backlight_lock);
  1380. ret = panel->backlight.setup(intel_connector, pipe);
  1381. mutex_unlock(&dev_priv->backlight_lock);
  1382. if (ret) {
  1383. DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
  1384. connector->name);
  1385. return ret;
  1386. }
  1387. panel->backlight.present = true;
  1388. DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
  1389. connector->name,
  1390. enableddisabled(panel->backlight.enabled),
  1391. panel->backlight.level, panel->backlight.max);
  1392. return 0;
  1393. }
  1394. void intel_panel_destroy_backlight(struct drm_connector *connector)
  1395. {
  1396. struct intel_connector *intel_connector = to_intel_connector(connector);
  1397. struct intel_panel *panel = &intel_connector->panel;
  1398. /* dispose of the pwm */
  1399. if (panel->backlight.pwm)
  1400. pwm_put(panel->backlight.pwm);
  1401. panel->backlight.present = false;
  1402. }
  1403. /* Set up chip specific backlight functions */
  1404. static void
  1405. intel_panel_init_backlight_funcs(struct intel_panel *panel)
  1406. {
  1407. struct intel_connector *connector =
  1408. container_of(panel, struct intel_connector, panel);
  1409. struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
  1410. if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
  1411. intel_dp_aux_init_backlight_funcs(connector) == 0)
  1412. return;
  1413. if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
  1414. intel_dsi_dcs_init_backlight_funcs(connector) == 0)
  1415. return;
  1416. if (IS_GEN9_LP(dev_priv)) {
  1417. panel->backlight.setup = bxt_setup_backlight;
  1418. panel->backlight.enable = bxt_enable_backlight;
  1419. panel->backlight.disable = bxt_disable_backlight;
  1420. panel->backlight.set = bxt_set_backlight;
  1421. panel->backlight.get = bxt_get_backlight;
  1422. panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
  1423. } else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_SPT(dev_priv) ||
  1424. HAS_PCH_KBP(dev_priv)) {
  1425. panel->backlight.setup = lpt_setup_backlight;
  1426. panel->backlight.enable = lpt_enable_backlight;
  1427. panel->backlight.disable = lpt_disable_backlight;
  1428. panel->backlight.set = lpt_set_backlight;
  1429. panel->backlight.get = lpt_get_backlight;
  1430. if (HAS_PCH_LPT(dev_priv))
  1431. panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
  1432. else
  1433. panel->backlight.hz_to_pwm = spt_hz_to_pwm;
  1434. } else if (HAS_PCH_SPLIT(dev_priv)) {
  1435. panel->backlight.setup = pch_setup_backlight;
  1436. panel->backlight.enable = pch_enable_backlight;
  1437. panel->backlight.disable = pch_disable_backlight;
  1438. panel->backlight.set = pch_set_backlight;
  1439. panel->backlight.get = pch_get_backlight;
  1440. panel->backlight.hz_to_pwm = pch_hz_to_pwm;
  1441. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1442. if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
  1443. panel->backlight.setup = pwm_setup_backlight;
  1444. panel->backlight.enable = pwm_enable_backlight;
  1445. panel->backlight.disable = pwm_disable_backlight;
  1446. panel->backlight.set = pwm_set_backlight;
  1447. panel->backlight.get = pwm_get_backlight;
  1448. } else {
  1449. panel->backlight.setup = vlv_setup_backlight;
  1450. panel->backlight.enable = vlv_enable_backlight;
  1451. panel->backlight.disable = vlv_disable_backlight;
  1452. panel->backlight.set = vlv_set_backlight;
  1453. panel->backlight.get = vlv_get_backlight;
  1454. panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
  1455. }
  1456. } else if (IS_GEN4(dev_priv)) {
  1457. panel->backlight.setup = i965_setup_backlight;
  1458. panel->backlight.enable = i965_enable_backlight;
  1459. panel->backlight.disable = i965_disable_backlight;
  1460. panel->backlight.set = i9xx_set_backlight;
  1461. panel->backlight.get = i9xx_get_backlight;
  1462. panel->backlight.hz_to_pwm = i965_hz_to_pwm;
  1463. } else {
  1464. panel->backlight.setup = i9xx_setup_backlight;
  1465. panel->backlight.enable = i9xx_enable_backlight;
  1466. panel->backlight.disable = i9xx_disable_backlight;
  1467. panel->backlight.set = i9xx_set_backlight;
  1468. panel->backlight.get = i9xx_get_backlight;
  1469. panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
  1470. }
  1471. }
  1472. int intel_panel_init(struct intel_panel *panel,
  1473. struct drm_display_mode *fixed_mode,
  1474. struct drm_display_mode *downclock_mode)
  1475. {
  1476. intel_panel_init_backlight_funcs(panel);
  1477. panel->fixed_mode = fixed_mode;
  1478. panel->downclock_mode = downclock_mode;
  1479. return 0;
  1480. }
  1481. void intel_panel_fini(struct intel_panel *panel)
  1482. {
  1483. struct intel_connector *intel_connector =
  1484. container_of(panel, struct intel_connector, panel);
  1485. if (panel->fixed_mode)
  1486. drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
  1487. if (panel->downclock_mode)
  1488. drm_mode_destroy(intel_connector->base.dev,
  1489. panel->downclock_mode);
  1490. }