intel_panel.c 54 KB

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