radeon_backlight.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Backlight code for ATI Radeon based graphic cards
  3. *
  4. * Copyright (c) 2000 Ani Joshi <ajoshi@kernel.crashing.org>
  5. * Copyright (c) 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  6. * Copyright (c) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include "radeonfb.h"
  13. #include <linux/backlight.h>
  14. #include <linux/slab.h>
  15. #ifdef CONFIG_PMAC_BACKLIGHT
  16. #include <asm/backlight.h>
  17. #endif
  18. #define MAX_RADEON_LEVEL 0xFF
  19. struct radeon_bl_privdata {
  20. struct radeonfb_info *rinfo;
  21. uint8_t negative;
  22. };
  23. static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata,
  24. int level)
  25. {
  26. int rlevel;
  27. /* Get and convert the value */
  28. /* No locking of bl_curve since we read a single value */
  29. rlevel = pdata->rinfo->info->bl_curve[level] *
  30. FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL;
  31. if (rlevel < 0)
  32. rlevel = 0;
  33. else if (rlevel > MAX_RADEON_LEVEL)
  34. rlevel = MAX_RADEON_LEVEL;
  35. if (pdata->negative)
  36. rlevel = MAX_RADEON_LEVEL - rlevel;
  37. return rlevel;
  38. }
  39. static int radeon_bl_update_status(struct backlight_device *bd)
  40. {
  41. struct radeon_bl_privdata *pdata = bl_get_data(bd);
  42. struct radeonfb_info *rinfo = pdata->rinfo;
  43. u32 lvds_gen_cntl, tmpPixclksCntl;
  44. int level;
  45. if (rinfo->mon1_type != MT_LCD)
  46. return 0;
  47. /* We turn off the LCD completely instead of just dimming the
  48. * backlight. This provides some greater power saving and the display
  49. * is useless without backlight anyway.
  50. */
  51. if (bd->props.power != FB_BLANK_UNBLANK ||
  52. bd->props.fb_blank != FB_BLANK_UNBLANK)
  53. level = 0;
  54. else
  55. level = bd->props.brightness;
  56. del_timer_sync(&rinfo->lvds_timer);
  57. radeon_engine_idle();
  58. lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
  59. if (level > 0) {
  60. lvds_gen_cntl &= ~LVDS_DISPLAY_DIS;
  61. if (!(lvds_gen_cntl & LVDS_BLON) || !(lvds_gen_cntl & LVDS_ON)) {
  62. lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_DIGON);
  63. lvds_gen_cntl |= LVDS_BLON | LVDS_EN;
  64. OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
  65. lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
  66. lvds_gen_cntl |=
  67. (radeon_bl_get_level_brightness(pdata, level) <<
  68. LVDS_BL_MOD_LEVEL_SHIFT);
  69. lvds_gen_cntl |= LVDS_ON;
  70. lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_BL_MOD_EN);
  71. rinfo->pending_lvds_gen_cntl = lvds_gen_cntl;
  72. mod_timer(&rinfo->lvds_timer,
  73. jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay));
  74. } else {
  75. lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
  76. lvds_gen_cntl |=
  77. (radeon_bl_get_level_brightness(pdata, level) <<
  78. LVDS_BL_MOD_LEVEL_SHIFT);
  79. OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
  80. }
  81. rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
  82. rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl
  83. & LVDS_STATE_MASK;
  84. } else {
  85. /* Asic bug, when turning off LVDS_ON, we have to make sure
  86. RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
  87. */
  88. tmpPixclksCntl = INPLL(PIXCLKS_CNTL);
  89. if (rinfo->is_mobility || rinfo->is_IGP)
  90. OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb);
  91. lvds_gen_cntl &= ~(LVDS_BL_MOD_LEVEL_MASK | LVDS_BL_MOD_EN);
  92. lvds_gen_cntl |= (radeon_bl_get_level_brightness(pdata, 0) <<
  93. LVDS_BL_MOD_LEVEL_SHIFT);
  94. lvds_gen_cntl |= LVDS_DISPLAY_DIS;
  95. OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
  96. udelay(100);
  97. lvds_gen_cntl &= ~(LVDS_ON | LVDS_EN);
  98. OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
  99. lvds_gen_cntl &= ~(LVDS_DIGON);
  100. rinfo->pending_lvds_gen_cntl = lvds_gen_cntl;
  101. mod_timer(&rinfo->lvds_timer,
  102. jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay));
  103. if (rinfo->is_mobility || rinfo->is_IGP)
  104. OUTPLL(PIXCLKS_CNTL, tmpPixclksCntl);
  105. }
  106. rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
  107. rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK);
  108. return 0;
  109. }
  110. static int radeon_bl_get_brightness(struct backlight_device *bd)
  111. {
  112. return bd->props.brightness;
  113. }
  114. static const struct backlight_ops radeon_bl_data = {
  115. .get_brightness = radeon_bl_get_brightness,
  116. .update_status = radeon_bl_update_status,
  117. };
  118. void radeonfb_bl_init(struct radeonfb_info *rinfo)
  119. {
  120. struct backlight_properties props;
  121. struct backlight_device *bd;
  122. struct radeon_bl_privdata *pdata;
  123. char name[12];
  124. if (rinfo->mon1_type != MT_LCD)
  125. return;
  126. #ifdef CONFIG_PMAC_BACKLIGHT
  127. if (!pmac_has_backlight_type("ati") &&
  128. !pmac_has_backlight_type("mnca"))
  129. return;
  130. #endif
  131. pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL);
  132. if (!pdata) {
  133. printk("radeonfb: Memory allocation failed\n");
  134. goto error;
  135. }
  136. snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node);
  137. memset(&props, 0, sizeof(struct backlight_properties));
  138. props.type = BACKLIGHT_RAW;
  139. props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
  140. bd = backlight_device_register(name, rinfo->info->dev, pdata,
  141. &radeon_bl_data, &props);
  142. if (IS_ERR(bd)) {
  143. rinfo->info->bl_dev = NULL;
  144. printk("radeonfb: Backlight registration failed\n");
  145. goto error;
  146. }
  147. pdata->rinfo = rinfo;
  148. /* Pardon me for that hack... maybe some day we can figure out in what
  149. * direction backlight should work on a given panel?
  150. */
  151. pdata->negative =
  152. (rinfo->family != CHIP_FAMILY_RV200 &&
  153. rinfo->family != CHIP_FAMILY_RV250 &&
  154. rinfo->family != CHIP_FAMILY_RV280 &&
  155. rinfo->family != CHIP_FAMILY_RV350);
  156. #ifdef CONFIG_PMAC_BACKLIGHT
  157. pdata->negative = pdata->negative ||
  158. of_machine_is_compatible("PowerBook4,3") ||
  159. of_machine_is_compatible("PowerBook6,3") ||
  160. of_machine_is_compatible("PowerBook6,5");
  161. #endif
  162. rinfo->info->bl_dev = bd;
  163. fb_bl_default_curve(rinfo->info, 0,
  164. 63 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL,
  165. 217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL);
  166. bd->props.brightness = bd->props.max_brightness;
  167. bd->props.power = FB_BLANK_UNBLANK;
  168. backlight_update_status(bd);
  169. printk("radeonfb: Backlight initialized (%s)\n", name);
  170. return;
  171. error:
  172. kfree(pdata);
  173. return;
  174. }
  175. void radeonfb_bl_exit(struct radeonfb_info *rinfo)
  176. {
  177. struct backlight_device *bd = rinfo->info->bl_dev;
  178. if (bd) {
  179. struct radeon_bl_privdata *pdata;
  180. pdata = bl_get_data(bd);
  181. backlight_device_unregister(bd);
  182. kfree(pdata);
  183. rinfo->info->bl_dev = NULL;
  184. printk("radeonfb: Backlight unloaded\n");
  185. }
  186. }