intel_panel.c 53 KB

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