intel_panel.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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/moduleparam.h>
  32. #include "intel_drv.h"
  33. #define PCI_LBPC 0xf4 /* legacy/combination backlight modes */
  34. void
  35. intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
  36. struct drm_display_mode *adjusted_mode)
  37. {
  38. drm_mode_copy(adjusted_mode, fixed_mode);
  39. drm_mode_set_crtcinfo(adjusted_mode, 0);
  40. }
  41. /* adjusted_mode has been preset to be the panel's fixed mode */
  42. void
  43. intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
  44. struct intel_crtc_config *pipe_config,
  45. int fitting_mode)
  46. {
  47. struct drm_display_mode *adjusted_mode;
  48. int x, y, width, height;
  49. adjusted_mode = &pipe_config->adjusted_mode;
  50. x = y = width = height = 0;
  51. /* Native modes don't need fitting */
  52. if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
  53. adjusted_mode->vdisplay == pipe_config->pipe_src_h)
  54. goto done;
  55. switch (fitting_mode) {
  56. case DRM_MODE_SCALE_CENTER:
  57. width = pipe_config->pipe_src_w;
  58. height = pipe_config->pipe_src_h;
  59. x = (adjusted_mode->hdisplay - width + 1)/2;
  60. y = (adjusted_mode->vdisplay - height + 1)/2;
  61. break;
  62. case DRM_MODE_SCALE_ASPECT:
  63. /* Scale but preserve the aspect ratio */
  64. {
  65. u32 scaled_width = adjusted_mode->hdisplay
  66. * pipe_config->pipe_src_h;
  67. u32 scaled_height = pipe_config->pipe_src_w
  68. * adjusted_mode->vdisplay;
  69. if (scaled_width > scaled_height) { /* pillar */
  70. width = scaled_height / pipe_config->pipe_src_h;
  71. if (width & 1)
  72. width++;
  73. x = (adjusted_mode->hdisplay - width + 1) / 2;
  74. y = 0;
  75. height = adjusted_mode->vdisplay;
  76. } else if (scaled_width < scaled_height) { /* letter */
  77. height = scaled_width / pipe_config->pipe_src_w;
  78. if (height & 1)
  79. height++;
  80. y = (adjusted_mode->vdisplay - height + 1) / 2;
  81. x = 0;
  82. width = adjusted_mode->hdisplay;
  83. } else {
  84. x = y = 0;
  85. width = adjusted_mode->hdisplay;
  86. height = adjusted_mode->vdisplay;
  87. }
  88. }
  89. break;
  90. case DRM_MODE_SCALE_FULLSCREEN:
  91. x = y = 0;
  92. width = adjusted_mode->hdisplay;
  93. height = adjusted_mode->vdisplay;
  94. break;
  95. default:
  96. WARN(1, "bad panel fit mode: %d\n", fitting_mode);
  97. return;
  98. }
  99. done:
  100. pipe_config->pch_pfit.pos = (x << 16) | y;
  101. pipe_config->pch_pfit.size = (width << 16) | height;
  102. pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
  103. }
  104. static void
  105. centre_horizontally(struct drm_display_mode *mode,
  106. int width)
  107. {
  108. u32 border, sync_pos, blank_width, sync_width;
  109. /* keep the hsync and hblank widths constant */
  110. sync_width = mode->crtc_hsync_end - mode->crtc_hsync_start;
  111. blank_width = mode->crtc_hblank_end - mode->crtc_hblank_start;
  112. sync_pos = (blank_width - sync_width + 1) / 2;
  113. border = (mode->hdisplay - width + 1) / 2;
  114. border += border & 1; /* make the border even */
  115. mode->crtc_hdisplay = width;
  116. mode->crtc_hblank_start = width + border;
  117. mode->crtc_hblank_end = mode->crtc_hblank_start + blank_width;
  118. mode->crtc_hsync_start = mode->crtc_hblank_start + sync_pos;
  119. mode->crtc_hsync_end = mode->crtc_hsync_start + sync_width;
  120. }
  121. static void
  122. centre_vertically(struct drm_display_mode *mode,
  123. int height)
  124. {
  125. u32 border, sync_pos, blank_width, sync_width;
  126. /* keep the vsync and vblank widths constant */
  127. sync_width = mode->crtc_vsync_end - mode->crtc_vsync_start;
  128. blank_width = mode->crtc_vblank_end - mode->crtc_vblank_start;
  129. sync_pos = (blank_width - sync_width + 1) / 2;
  130. border = (mode->vdisplay - height + 1) / 2;
  131. mode->crtc_vdisplay = height;
  132. mode->crtc_vblank_start = height + border;
  133. mode->crtc_vblank_end = mode->crtc_vblank_start + blank_width;
  134. mode->crtc_vsync_start = mode->crtc_vblank_start + sync_pos;
  135. mode->crtc_vsync_end = mode->crtc_vsync_start + sync_width;
  136. }
  137. static inline u32 panel_fitter_scaling(u32 source, u32 target)
  138. {
  139. /*
  140. * Floating point operation is not supported. So the FACTOR
  141. * is defined, which can avoid the floating point computation
  142. * when calculating the panel ratio.
  143. */
  144. #define ACCURACY 12
  145. #define FACTOR (1 << ACCURACY)
  146. u32 ratio = source * FACTOR / target;
  147. return (FACTOR * ratio + FACTOR/2) / FACTOR;
  148. }
  149. static void i965_scale_aspect(struct intel_crtc_config *pipe_config,
  150. u32 *pfit_control)
  151. {
  152. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  153. u32 scaled_width = adjusted_mode->hdisplay *
  154. pipe_config->pipe_src_h;
  155. u32 scaled_height = pipe_config->pipe_src_w *
  156. adjusted_mode->vdisplay;
  157. /* 965+ is easy, it does everything in hw */
  158. if (scaled_width > scaled_height)
  159. *pfit_control |= PFIT_ENABLE |
  160. PFIT_SCALING_PILLAR;
  161. else if (scaled_width < scaled_height)
  162. *pfit_control |= PFIT_ENABLE |
  163. PFIT_SCALING_LETTER;
  164. else if (adjusted_mode->hdisplay != pipe_config->pipe_src_w)
  165. *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
  166. }
  167. static void i9xx_scale_aspect(struct intel_crtc_config *pipe_config,
  168. u32 *pfit_control, u32 *pfit_pgm_ratios,
  169. u32 *border)
  170. {
  171. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  172. u32 scaled_width = adjusted_mode->hdisplay *
  173. pipe_config->pipe_src_h;
  174. u32 scaled_height = pipe_config->pipe_src_w *
  175. adjusted_mode->vdisplay;
  176. u32 bits;
  177. /*
  178. * For earlier chips we have to calculate the scaling
  179. * ratio by hand and program it into the
  180. * PFIT_PGM_RATIO register
  181. */
  182. if (scaled_width > scaled_height) { /* pillar */
  183. centre_horizontally(adjusted_mode,
  184. scaled_height /
  185. pipe_config->pipe_src_h);
  186. *border = LVDS_BORDER_ENABLE;
  187. if (pipe_config->pipe_src_h != adjusted_mode->vdisplay) {
  188. bits = panel_fitter_scaling(pipe_config->pipe_src_h,
  189. adjusted_mode->vdisplay);
  190. *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
  191. bits << PFIT_VERT_SCALE_SHIFT);
  192. *pfit_control |= (PFIT_ENABLE |
  193. VERT_INTERP_BILINEAR |
  194. HORIZ_INTERP_BILINEAR);
  195. }
  196. } else if (scaled_width < scaled_height) { /* letter */
  197. centre_vertically(adjusted_mode,
  198. scaled_width /
  199. pipe_config->pipe_src_w);
  200. *border = LVDS_BORDER_ENABLE;
  201. if (pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
  202. bits = panel_fitter_scaling(pipe_config->pipe_src_w,
  203. adjusted_mode->hdisplay);
  204. *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
  205. bits << PFIT_VERT_SCALE_SHIFT);
  206. *pfit_control |= (PFIT_ENABLE |
  207. VERT_INTERP_BILINEAR |
  208. HORIZ_INTERP_BILINEAR);
  209. }
  210. } else {
  211. /* Aspects match, Let hw scale both directions */
  212. *pfit_control |= (PFIT_ENABLE |
  213. VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
  214. VERT_INTERP_BILINEAR |
  215. HORIZ_INTERP_BILINEAR);
  216. }
  217. }
  218. void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
  219. struct intel_crtc_config *pipe_config,
  220. int fitting_mode)
  221. {
  222. struct drm_device *dev = intel_crtc->base.dev;
  223. u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
  224. struct drm_display_mode *adjusted_mode;
  225. adjusted_mode = &pipe_config->adjusted_mode;
  226. /* Native modes don't need fitting */
  227. if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
  228. adjusted_mode->vdisplay == pipe_config->pipe_src_h)
  229. goto out;
  230. switch (fitting_mode) {
  231. case DRM_MODE_SCALE_CENTER:
  232. /*
  233. * For centered modes, we have to calculate border widths &
  234. * heights and modify the values programmed into the CRTC.
  235. */
  236. centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
  237. centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
  238. border = LVDS_BORDER_ENABLE;
  239. break;
  240. case DRM_MODE_SCALE_ASPECT:
  241. /* Scale but preserve the aspect ratio */
  242. if (INTEL_INFO(dev)->gen >= 4)
  243. i965_scale_aspect(pipe_config, &pfit_control);
  244. else
  245. i9xx_scale_aspect(pipe_config, &pfit_control,
  246. &pfit_pgm_ratios, &border);
  247. break;
  248. case DRM_MODE_SCALE_FULLSCREEN:
  249. /*
  250. * Full scaling, even if it changes the aspect ratio.
  251. * Fortunately this is all done for us in hw.
  252. */
  253. if (pipe_config->pipe_src_h != adjusted_mode->vdisplay ||
  254. pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
  255. pfit_control |= PFIT_ENABLE;
  256. if (INTEL_INFO(dev)->gen >= 4)
  257. pfit_control |= PFIT_SCALING_AUTO;
  258. else
  259. pfit_control |= (VERT_AUTO_SCALE |
  260. VERT_INTERP_BILINEAR |
  261. HORIZ_AUTO_SCALE |
  262. HORIZ_INTERP_BILINEAR);
  263. }
  264. break;
  265. default:
  266. WARN(1, "bad panel fit mode: %d\n", fitting_mode);
  267. return;
  268. }
  269. /* 965+ wants fuzzy fitting */
  270. /* FIXME: handle multiple panels by failing gracefully */
  271. if (INTEL_INFO(dev)->gen >= 4)
  272. pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
  273. PFIT_FILTER_FUZZY);
  274. out:
  275. if ((pfit_control & PFIT_ENABLE) == 0) {
  276. pfit_control = 0;
  277. pfit_pgm_ratios = 0;
  278. }
  279. /* Make sure pre-965 set dither correctly for 18bpp panels. */
  280. if (INTEL_INFO(dev)->gen < 4 && pipe_config->pipe_bpp == 18)
  281. pfit_control |= PANEL_8TO6_DITHER_ENABLE;
  282. pipe_config->gmch_pfit.control = pfit_control;
  283. pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
  284. pipe_config->gmch_pfit.lvds_border_bits = border;
  285. }
  286. static int is_backlight_combination_mode(struct drm_device *dev)
  287. {
  288. struct drm_i915_private *dev_priv = dev->dev_private;
  289. if (IS_GEN4(dev))
  290. return I915_READ(BLC_PWM_CTL2) & BLM_COMBINATION_MODE;
  291. if (IS_GEN2(dev))
  292. return I915_READ(BLC_PWM_CTL) & BLM_LEGACY_MODE;
  293. return 0;
  294. }
  295. /* XXX: query mode clock or hardware clock and program max PWM appropriately
  296. * when it's 0.
  297. */
  298. static u32 i915_read_blc_pwm_ctl(struct drm_device *dev, enum pipe pipe)
  299. {
  300. struct drm_i915_private *dev_priv = dev->dev_private;
  301. u32 val;
  302. WARN_ON_SMP(!spin_is_locked(&dev_priv->backlight.lock));
  303. /* Restore the CTL value if it lost, e.g. GPU reset */
  304. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  305. val = I915_READ(BLC_PWM_PCH_CTL2);
  306. if (dev_priv->regfile.saveBLC_PWM_CTL2 == 0) {
  307. dev_priv->regfile.saveBLC_PWM_CTL2 = val;
  308. } else if (val == 0) {
  309. val = dev_priv->regfile.saveBLC_PWM_CTL2;
  310. I915_WRITE(BLC_PWM_PCH_CTL2, val);
  311. }
  312. } else if (IS_VALLEYVIEW(dev)) {
  313. val = I915_READ(VLV_BLC_PWM_CTL(pipe));
  314. if (dev_priv->regfile.saveBLC_PWM_CTL == 0) {
  315. dev_priv->regfile.saveBLC_PWM_CTL = val;
  316. dev_priv->regfile.saveBLC_PWM_CTL2 =
  317. I915_READ(VLV_BLC_PWM_CTL2(pipe));
  318. } else if (val == 0) {
  319. val = dev_priv->regfile.saveBLC_PWM_CTL;
  320. I915_WRITE(VLV_BLC_PWM_CTL(pipe), val);
  321. I915_WRITE(VLV_BLC_PWM_CTL2(pipe),
  322. dev_priv->regfile.saveBLC_PWM_CTL2);
  323. }
  324. if (!val)
  325. val = 0x0f42ffff;
  326. } else {
  327. val = I915_READ(BLC_PWM_CTL);
  328. if (dev_priv->regfile.saveBLC_PWM_CTL == 0) {
  329. dev_priv->regfile.saveBLC_PWM_CTL = val;
  330. if (INTEL_INFO(dev)->gen >= 4)
  331. dev_priv->regfile.saveBLC_PWM_CTL2 =
  332. I915_READ(BLC_PWM_CTL2);
  333. } else if (val == 0) {
  334. val = dev_priv->regfile.saveBLC_PWM_CTL;
  335. I915_WRITE(BLC_PWM_CTL, val);
  336. if (INTEL_INFO(dev)->gen >= 4)
  337. I915_WRITE(BLC_PWM_CTL2,
  338. dev_priv->regfile.saveBLC_PWM_CTL2);
  339. }
  340. }
  341. return val;
  342. }
  343. static u32 intel_panel_get_max_backlight(struct drm_device *dev,
  344. enum pipe pipe)
  345. {
  346. u32 max;
  347. max = i915_read_blc_pwm_ctl(dev, pipe);
  348. if (HAS_PCH_SPLIT(dev)) {
  349. max >>= 16;
  350. } else {
  351. if (INTEL_INFO(dev)->gen < 4)
  352. max >>= 17;
  353. else
  354. max >>= 16;
  355. if (is_backlight_combination_mode(dev))
  356. max *= 0xff;
  357. }
  358. DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max);
  359. return max;
  360. }
  361. static int i915_panel_invert_brightness;
  362. MODULE_PARM_DESC(invert_brightness, "Invert backlight brightness "
  363. "(-1 force normal, 0 machine defaults, 1 force inversion), please "
  364. "report PCI device ID, subsystem vendor and subsystem device ID "
  365. "to dri-devel@lists.freedesktop.org, if your machine needs it. "
  366. "It will then be included in an upcoming module version.");
  367. module_param_named(invert_brightness, i915_panel_invert_brightness, int, 0600);
  368. static u32 intel_panel_compute_brightness(struct drm_device *dev,
  369. enum pipe pipe, u32 val)
  370. {
  371. struct drm_i915_private *dev_priv = dev->dev_private;
  372. if (i915_panel_invert_brightness < 0)
  373. return val;
  374. if (i915_panel_invert_brightness > 0 ||
  375. dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
  376. u32 max = intel_panel_get_max_backlight(dev, pipe);
  377. if (max)
  378. return max - val;
  379. }
  380. return val;
  381. }
  382. static u32 intel_panel_get_backlight(struct drm_device *dev,
  383. enum pipe pipe)
  384. {
  385. struct drm_i915_private *dev_priv = dev->dev_private;
  386. u32 val;
  387. unsigned long flags;
  388. int reg;
  389. spin_lock_irqsave(&dev_priv->backlight.lock, flags);
  390. if (HAS_PCH_SPLIT(dev)) {
  391. val = I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  392. } else {
  393. if (IS_VALLEYVIEW(dev))
  394. reg = VLV_BLC_PWM_CTL(pipe);
  395. else
  396. reg = BLC_PWM_CTL;
  397. val = I915_READ(reg) & BACKLIGHT_DUTY_CYCLE_MASK;
  398. if (INTEL_INFO(dev)->gen < 4)
  399. val >>= 1;
  400. if (is_backlight_combination_mode(dev)) {
  401. u8 lbpc;
  402. pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
  403. val *= lbpc;
  404. }
  405. }
  406. val = intel_panel_compute_brightness(dev, pipe, val);
  407. spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
  408. DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
  409. return val;
  410. }
  411. static void intel_pch_panel_set_backlight(struct drm_device *dev, u32 level)
  412. {
  413. struct drm_i915_private *dev_priv = dev->dev_private;
  414. u32 val = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  415. I915_WRITE(BLC_PWM_CPU_CTL, val | level);
  416. }
  417. static void intel_panel_actually_set_backlight(struct drm_device *dev,
  418. enum pipe pipe, u32 level)
  419. {
  420. struct drm_i915_private *dev_priv = dev->dev_private;
  421. u32 tmp;
  422. int reg;
  423. DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
  424. level = intel_panel_compute_brightness(dev, pipe, level);
  425. if (HAS_PCH_SPLIT(dev))
  426. return intel_pch_panel_set_backlight(dev, level);
  427. if (is_backlight_combination_mode(dev)) {
  428. u32 max = intel_panel_get_max_backlight(dev, pipe);
  429. u8 lbpc;
  430. /* we're screwed, but keep behaviour backwards compatible */
  431. if (!max)
  432. max = 1;
  433. lbpc = level * 0xfe / max + 1;
  434. level /= lbpc;
  435. pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
  436. }
  437. if (IS_VALLEYVIEW(dev))
  438. reg = VLV_BLC_PWM_CTL(pipe);
  439. else
  440. reg = BLC_PWM_CTL;
  441. tmp = I915_READ(reg);
  442. if (INTEL_INFO(dev)->gen < 4)
  443. level <<= 1;
  444. tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK;
  445. I915_WRITE(reg, tmp | level);
  446. }
  447. /* set backlight brightness to level in range [0..max] */
  448. void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
  449. u32 max)
  450. {
  451. struct drm_device *dev = connector->base.dev;
  452. struct drm_i915_private *dev_priv = dev->dev_private;
  453. enum pipe pipe = intel_get_pipe_from_connector(connector);
  454. u32 freq;
  455. unsigned long flags;
  456. if (pipe == INVALID_PIPE)
  457. return;
  458. spin_lock_irqsave(&dev_priv->backlight.lock, flags);
  459. freq = intel_panel_get_max_backlight(dev, pipe);
  460. if (!freq) {
  461. /* we are screwed, bail out */
  462. goto out;
  463. }
  464. /* scale to hardware, but be careful to not overflow */
  465. if (freq < max)
  466. level = level * freq / max;
  467. else
  468. level = freq / max * level;
  469. dev_priv->backlight.level = level;
  470. if (dev_priv->backlight.device)
  471. dev_priv->backlight.device->props.brightness = level;
  472. if (dev_priv->backlight.enabled)
  473. intel_panel_actually_set_backlight(dev, pipe, level);
  474. out:
  475. spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
  476. }
  477. void intel_panel_disable_backlight(struct intel_connector *connector)
  478. {
  479. struct drm_device *dev = connector->base.dev;
  480. struct drm_i915_private *dev_priv = dev->dev_private;
  481. enum pipe pipe = intel_get_pipe_from_connector(connector);
  482. unsigned long flags;
  483. if (pipe == INVALID_PIPE)
  484. return;
  485. /*
  486. * Do not disable backlight on the vgaswitcheroo path. When switching
  487. * away from i915, the other client may depend on i915 to handle the
  488. * backlight. This will leave the backlight on unnecessarily when
  489. * another client is not activated.
  490. */
  491. if (dev->switch_power_state == DRM_SWITCH_POWER_CHANGING) {
  492. DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
  493. return;
  494. }
  495. spin_lock_irqsave(&dev_priv->backlight.lock, flags);
  496. dev_priv->backlight.enabled = false;
  497. intel_panel_actually_set_backlight(dev, pipe, 0);
  498. if (INTEL_INFO(dev)->gen >= 4) {
  499. uint32_t reg, tmp;
  500. if (HAS_PCH_SPLIT(dev))
  501. reg = BLC_PWM_CPU_CTL2;
  502. else if (IS_VALLEYVIEW(dev))
  503. reg = VLV_BLC_PWM_CTL2(pipe);
  504. else
  505. reg = BLC_PWM_CTL2;
  506. I915_WRITE(reg, I915_READ(reg) & ~BLM_PWM_ENABLE);
  507. if (HAS_PCH_SPLIT(dev)) {
  508. tmp = I915_READ(BLC_PWM_PCH_CTL1);
  509. tmp &= ~BLM_PCH_PWM_ENABLE;
  510. I915_WRITE(BLC_PWM_PCH_CTL1, tmp);
  511. }
  512. }
  513. spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
  514. }
  515. void intel_panel_enable_backlight(struct intel_connector *connector)
  516. {
  517. struct drm_device *dev = connector->base.dev;
  518. struct drm_i915_private *dev_priv = dev->dev_private;
  519. enum pipe pipe = intel_get_pipe_from_connector(connector);
  520. enum transcoder cpu_transcoder =
  521. intel_pipe_to_cpu_transcoder(dev_priv, pipe);
  522. unsigned long flags;
  523. if (pipe == INVALID_PIPE)
  524. return;
  525. DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
  526. spin_lock_irqsave(&dev_priv->backlight.lock, flags);
  527. if (dev_priv->backlight.level == 0) {
  528. dev_priv->backlight.level = intel_panel_get_max_backlight(dev,
  529. pipe);
  530. if (dev_priv->backlight.device)
  531. dev_priv->backlight.device->props.brightness =
  532. dev_priv->backlight.level;
  533. }
  534. if (INTEL_INFO(dev)->gen >= 4) {
  535. uint32_t reg, tmp;
  536. if (HAS_PCH_SPLIT(dev))
  537. reg = BLC_PWM_CPU_CTL2;
  538. else if (IS_VALLEYVIEW(dev))
  539. reg = VLV_BLC_PWM_CTL2(pipe);
  540. else
  541. reg = BLC_PWM_CTL2;
  542. tmp = I915_READ(reg);
  543. /* Note that this can also get called through dpms changes. And
  544. * we don't track the backlight dpms state, hence check whether
  545. * we have to do anything first. */
  546. if (tmp & BLM_PWM_ENABLE)
  547. goto set_level;
  548. if (INTEL_INFO(dev)->num_pipes == 3)
  549. tmp &= ~BLM_PIPE_SELECT_IVB;
  550. else
  551. tmp &= ~BLM_PIPE_SELECT;
  552. if (cpu_transcoder == TRANSCODER_EDP)
  553. tmp |= BLM_TRANSCODER_EDP;
  554. else
  555. tmp |= BLM_PIPE(cpu_transcoder);
  556. tmp &= ~BLM_PWM_ENABLE;
  557. I915_WRITE(reg, tmp);
  558. POSTING_READ(reg);
  559. I915_WRITE(reg, tmp | BLM_PWM_ENABLE);
  560. if (HAS_PCH_SPLIT(dev) &&
  561. !(dev_priv->quirks & QUIRK_NO_PCH_PWM_ENABLE)) {
  562. tmp = I915_READ(BLC_PWM_PCH_CTL1);
  563. tmp |= BLM_PCH_PWM_ENABLE;
  564. tmp &= ~BLM_PCH_OVERRIDE_ENABLE;
  565. I915_WRITE(BLC_PWM_PCH_CTL1, tmp);
  566. }
  567. }
  568. set_level:
  569. /* Call below after setting BLC_PWM_CPU_CTL2 and BLC_PWM_PCH_CTL1.
  570. * BLC_PWM_CPU_CTL may be cleared to zero automatically when these
  571. * registers are set.
  572. */
  573. dev_priv->backlight.enabled = true;
  574. intel_panel_actually_set_backlight(dev, pipe,
  575. dev_priv->backlight.level);
  576. spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
  577. }
  578. /* FIXME: use VBT vals to init PWM_CTL and PWM_CTL2 correctly */
  579. static void intel_panel_init_backlight_regs(struct drm_device *dev)
  580. {
  581. struct drm_i915_private *dev_priv = dev->dev_private;
  582. if (IS_VALLEYVIEW(dev)) {
  583. enum pipe pipe;
  584. for_each_pipe(pipe) {
  585. u32 cur_val = I915_READ(VLV_BLC_PWM_CTL(pipe));
  586. /* Skip if the modulation freq is already set */
  587. if (cur_val & ~BACKLIGHT_DUTY_CYCLE_MASK)
  588. continue;
  589. cur_val &= BACKLIGHT_DUTY_CYCLE_MASK;
  590. I915_WRITE(VLV_BLC_PWM_CTL(pipe), (0xf42 << 16) |
  591. cur_val);
  592. }
  593. }
  594. }
  595. static void intel_panel_init_backlight(struct drm_device *dev)
  596. {
  597. struct drm_i915_private *dev_priv = dev->dev_private;
  598. intel_panel_init_backlight_regs(dev);
  599. dev_priv->backlight.level = intel_panel_get_backlight(dev, 0);
  600. dev_priv->backlight.enabled = dev_priv->backlight.level != 0;
  601. }
  602. enum drm_connector_status
  603. intel_panel_detect(struct drm_device *dev)
  604. {
  605. struct drm_i915_private *dev_priv = dev->dev_private;
  606. /* Assume that the BIOS does not lie through the OpRegion... */
  607. if (!i915_panel_ignore_lid && dev_priv->opregion.lid_state) {
  608. return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
  609. connector_status_connected :
  610. connector_status_disconnected;
  611. }
  612. switch (i915_panel_ignore_lid) {
  613. case -2:
  614. return connector_status_connected;
  615. case -1:
  616. return connector_status_disconnected;
  617. default:
  618. return connector_status_unknown;
  619. }
  620. }
  621. #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
  622. static int intel_panel_update_status(struct backlight_device *bd)
  623. {
  624. struct intel_connector *connector = bl_get_data(bd);
  625. struct drm_device *dev = connector->base.dev;
  626. mutex_lock(&dev->mode_config.mutex);
  627. DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
  628. bd->props.brightness, bd->props.max_brightness);
  629. intel_panel_set_backlight(connector, bd->props.brightness,
  630. bd->props.max_brightness);
  631. mutex_unlock(&dev->mode_config.mutex);
  632. return 0;
  633. }
  634. static int intel_panel_get_brightness(struct backlight_device *bd)
  635. {
  636. struct intel_connector *connector = bl_get_data(bd);
  637. struct drm_device *dev = connector->base.dev;
  638. enum pipe pipe;
  639. mutex_lock(&dev->mode_config.mutex);
  640. pipe = intel_get_pipe_from_connector(connector);
  641. mutex_unlock(&dev->mode_config.mutex);
  642. if (pipe == INVALID_PIPE)
  643. return 0;
  644. return intel_panel_get_backlight(connector->base.dev, pipe);
  645. }
  646. static const struct backlight_ops intel_panel_bl_ops = {
  647. .update_status = intel_panel_update_status,
  648. .get_brightness = intel_panel_get_brightness,
  649. };
  650. int intel_panel_setup_backlight(struct drm_connector *connector)
  651. {
  652. struct drm_device *dev = connector->dev;
  653. struct drm_i915_private *dev_priv = dev->dev_private;
  654. struct backlight_properties props;
  655. unsigned long flags;
  656. intel_panel_init_backlight(dev);
  657. if (WARN_ON(dev_priv->backlight.device))
  658. return -ENODEV;
  659. memset(&props, 0, sizeof(props));
  660. props.type = BACKLIGHT_RAW;
  661. props.brightness = dev_priv->backlight.level;
  662. spin_lock_irqsave(&dev_priv->backlight.lock, flags);
  663. props.max_brightness = intel_panel_get_max_backlight(dev, 0);
  664. spin_unlock_irqrestore(&dev_priv->backlight.lock, flags);
  665. if (props.max_brightness == 0) {
  666. DRM_DEBUG_DRIVER("Failed to get maximum backlight value\n");
  667. return -ENODEV;
  668. }
  669. dev_priv->backlight.device =
  670. backlight_device_register("intel_backlight",
  671. connector->kdev,
  672. to_intel_connector(connector),
  673. &intel_panel_bl_ops, &props);
  674. if (IS_ERR(dev_priv->backlight.device)) {
  675. DRM_ERROR("Failed to register backlight: %ld\n",
  676. PTR_ERR(dev_priv->backlight.device));
  677. dev_priv->backlight.device = NULL;
  678. return -ENODEV;
  679. }
  680. return 0;
  681. }
  682. void intel_panel_destroy_backlight(struct drm_device *dev)
  683. {
  684. struct drm_i915_private *dev_priv = dev->dev_private;
  685. if (dev_priv->backlight.device) {
  686. backlight_device_unregister(dev_priv->backlight.device);
  687. dev_priv->backlight.device = NULL;
  688. }
  689. }
  690. #else
  691. int intel_panel_setup_backlight(struct drm_connector *connector)
  692. {
  693. intel_panel_init_backlight(connector->dev);
  694. return 0;
  695. }
  696. void intel_panel_destroy_backlight(struct drm_device *dev)
  697. {
  698. return;
  699. }
  700. #endif
  701. int intel_panel_init(struct intel_panel *panel,
  702. struct drm_display_mode *fixed_mode)
  703. {
  704. panel->fixed_mode = fixed_mode;
  705. return 0;
  706. }
  707. void intel_panel_fini(struct intel_panel *panel)
  708. {
  709. struct intel_connector *intel_connector =
  710. container_of(panel, struct intel_connector, panel);
  711. if (panel->fixed_mode)
  712. drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
  713. }