18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Backlight code for ATI Radeon based graphic cards 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2000 Ani Joshi <ajoshi@kernel.crashing.org> 68c2ecf20Sopenharmony_ci * Copyright (c) 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org> 78c2ecf20Sopenharmony_ci * Copyright (c) 2006 Michael Hanselmann <linux-kernel@hansmi.ch> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include "radeonfb.h" 118c2ecf20Sopenharmony_ci#include <linux/backlight.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#ifdef CONFIG_PMAC_BACKLIGHT 158c2ecf20Sopenharmony_ci#include <asm/backlight.h> 168c2ecf20Sopenharmony_ci#endif 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define MAX_RADEON_LEVEL 0xFF 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct radeon_bl_privdata { 218c2ecf20Sopenharmony_ci struct radeonfb_info *rinfo; 228c2ecf20Sopenharmony_ci uint8_t negative; 238c2ecf20Sopenharmony_ci}; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata, 268c2ecf20Sopenharmony_ci int level) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci int rlevel; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci /* Get and convert the value */ 318c2ecf20Sopenharmony_ci /* No locking of bl_curve since we read a single value */ 328c2ecf20Sopenharmony_ci rlevel = pdata->rinfo->info->bl_curve[level] * 338c2ecf20Sopenharmony_ci FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci if (rlevel < 0) 368c2ecf20Sopenharmony_ci rlevel = 0; 378c2ecf20Sopenharmony_ci else if (rlevel > MAX_RADEON_LEVEL) 388c2ecf20Sopenharmony_ci rlevel = MAX_RADEON_LEVEL; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci if (pdata->negative) 418c2ecf20Sopenharmony_ci rlevel = MAX_RADEON_LEVEL - rlevel; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci return rlevel; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic int radeon_bl_update_status(struct backlight_device *bd) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci struct radeon_bl_privdata *pdata = bl_get_data(bd); 498c2ecf20Sopenharmony_ci struct radeonfb_info *rinfo = pdata->rinfo; 508c2ecf20Sopenharmony_ci u32 lvds_gen_cntl, tmpPixclksCntl; 518c2ecf20Sopenharmony_ci int level; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci if (rinfo->mon1_type != MT_LCD) 548c2ecf20Sopenharmony_ci return 0; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci /* We turn off the LCD completely instead of just dimming the 578c2ecf20Sopenharmony_ci * backlight. This provides some greater power saving and the display 588c2ecf20Sopenharmony_ci * is useless without backlight anyway. 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_ci if (bd->props.power != FB_BLANK_UNBLANK || 618c2ecf20Sopenharmony_ci bd->props.fb_blank != FB_BLANK_UNBLANK) 628c2ecf20Sopenharmony_ci level = 0; 638c2ecf20Sopenharmony_ci else 648c2ecf20Sopenharmony_ci level = bd->props.brightness; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci del_timer_sync(&rinfo->lvds_timer); 678c2ecf20Sopenharmony_ci radeon_engine_idle(); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci lvds_gen_cntl = INREG(LVDS_GEN_CNTL); 708c2ecf20Sopenharmony_ci if (level > 0) { 718c2ecf20Sopenharmony_ci lvds_gen_cntl &= ~LVDS_DISPLAY_DIS; 728c2ecf20Sopenharmony_ci if (!(lvds_gen_cntl & LVDS_BLON) || !(lvds_gen_cntl & LVDS_ON)) { 738c2ecf20Sopenharmony_ci lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_DIGON); 748c2ecf20Sopenharmony_ci lvds_gen_cntl |= LVDS_BLON | LVDS_EN; 758c2ecf20Sopenharmony_ci OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); 768c2ecf20Sopenharmony_ci lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK; 778c2ecf20Sopenharmony_ci lvds_gen_cntl |= 788c2ecf20Sopenharmony_ci (radeon_bl_get_level_brightness(pdata, level) << 798c2ecf20Sopenharmony_ci LVDS_BL_MOD_LEVEL_SHIFT); 808c2ecf20Sopenharmony_ci lvds_gen_cntl |= LVDS_ON; 818c2ecf20Sopenharmony_ci lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_BL_MOD_EN); 828c2ecf20Sopenharmony_ci rinfo->pending_lvds_gen_cntl = lvds_gen_cntl; 838c2ecf20Sopenharmony_ci mod_timer(&rinfo->lvds_timer, 848c2ecf20Sopenharmony_ci jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay)); 858c2ecf20Sopenharmony_ci } else { 868c2ecf20Sopenharmony_ci lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK; 878c2ecf20Sopenharmony_ci lvds_gen_cntl |= 888c2ecf20Sopenharmony_ci (radeon_bl_get_level_brightness(pdata, level) << 898c2ecf20Sopenharmony_ci LVDS_BL_MOD_LEVEL_SHIFT); 908c2ecf20Sopenharmony_ci OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); 918c2ecf20Sopenharmony_ci } 928c2ecf20Sopenharmony_ci rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK; 938c2ecf20Sopenharmony_ci rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl 948c2ecf20Sopenharmony_ci & LVDS_STATE_MASK; 958c2ecf20Sopenharmony_ci } else { 968c2ecf20Sopenharmony_ci /* Asic bug, when turning off LVDS_ON, we have to make sure 978c2ecf20Sopenharmony_ci RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off 988c2ecf20Sopenharmony_ci */ 998c2ecf20Sopenharmony_ci tmpPixclksCntl = INPLL(PIXCLKS_CNTL); 1008c2ecf20Sopenharmony_ci if (rinfo->is_mobility || rinfo->is_IGP) 1018c2ecf20Sopenharmony_ci OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb); 1028c2ecf20Sopenharmony_ci lvds_gen_cntl &= ~(LVDS_BL_MOD_LEVEL_MASK | LVDS_BL_MOD_EN); 1038c2ecf20Sopenharmony_ci lvds_gen_cntl |= (radeon_bl_get_level_brightness(pdata, 0) << 1048c2ecf20Sopenharmony_ci LVDS_BL_MOD_LEVEL_SHIFT); 1058c2ecf20Sopenharmony_ci lvds_gen_cntl |= LVDS_DISPLAY_DIS; 1068c2ecf20Sopenharmony_ci OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); 1078c2ecf20Sopenharmony_ci udelay(100); 1088c2ecf20Sopenharmony_ci lvds_gen_cntl &= ~(LVDS_ON | LVDS_EN); 1098c2ecf20Sopenharmony_ci OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); 1108c2ecf20Sopenharmony_ci lvds_gen_cntl &= ~(LVDS_DIGON); 1118c2ecf20Sopenharmony_ci rinfo->pending_lvds_gen_cntl = lvds_gen_cntl; 1128c2ecf20Sopenharmony_ci mod_timer(&rinfo->lvds_timer, 1138c2ecf20Sopenharmony_ci jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay)); 1148c2ecf20Sopenharmony_ci if (rinfo->is_mobility || rinfo->is_IGP) 1158c2ecf20Sopenharmony_ci OUTPLL(PIXCLKS_CNTL, tmpPixclksCntl); 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK; 1188c2ecf20Sopenharmony_ci rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic const struct backlight_ops radeon_bl_data = { 1248c2ecf20Sopenharmony_ci .update_status = radeon_bl_update_status, 1258c2ecf20Sopenharmony_ci}; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_civoid radeonfb_bl_init(struct radeonfb_info *rinfo) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci struct backlight_properties props; 1308c2ecf20Sopenharmony_ci struct backlight_device *bd; 1318c2ecf20Sopenharmony_ci struct radeon_bl_privdata *pdata; 1328c2ecf20Sopenharmony_ci char name[12]; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci if (rinfo->mon1_type != MT_LCD) 1358c2ecf20Sopenharmony_ci return; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci#ifdef CONFIG_PMAC_BACKLIGHT 1388c2ecf20Sopenharmony_ci if (!pmac_has_backlight_type("ati") && 1398c2ecf20Sopenharmony_ci !pmac_has_backlight_type("mnca")) 1408c2ecf20Sopenharmony_ci return; 1418c2ecf20Sopenharmony_ci#endif 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL); 1448c2ecf20Sopenharmony_ci if (!pdata) { 1458c2ecf20Sopenharmony_ci printk("radeonfb: Memory allocation failed\n"); 1468c2ecf20Sopenharmony_ci goto error; 1478c2ecf20Sopenharmony_ci } 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci memset(&props, 0, sizeof(struct backlight_properties)); 1528c2ecf20Sopenharmony_ci props.type = BACKLIGHT_RAW; 1538c2ecf20Sopenharmony_ci props.max_brightness = FB_BACKLIGHT_LEVELS - 1; 1548c2ecf20Sopenharmony_ci bd = backlight_device_register(name, rinfo->info->dev, pdata, 1558c2ecf20Sopenharmony_ci &radeon_bl_data, &props); 1568c2ecf20Sopenharmony_ci if (IS_ERR(bd)) { 1578c2ecf20Sopenharmony_ci rinfo->info->bl_dev = NULL; 1588c2ecf20Sopenharmony_ci printk("radeonfb: Backlight registration failed\n"); 1598c2ecf20Sopenharmony_ci goto error; 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci pdata->rinfo = rinfo; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* Pardon me for that hack... maybe some day we can figure out in what 1658c2ecf20Sopenharmony_ci * direction backlight should work on a given panel? 1668c2ecf20Sopenharmony_ci */ 1678c2ecf20Sopenharmony_ci pdata->negative = 1688c2ecf20Sopenharmony_ci (rinfo->family != CHIP_FAMILY_RV200 && 1698c2ecf20Sopenharmony_ci rinfo->family != CHIP_FAMILY_RV250 && 1708c2ecf20Sopenharmony_ci rinfo->family != CHIP_FAMILY_RV280 && 1718c2ecf20Sopenharmony_ci rinfo->family != CHIP_FAMILY_RV350); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci#ifdef CONFIG_PMAC_BACKLIGHT 1748c2ecf20Sopenharmony_ci pdata->negative = pdata->negative || 1758c2ecf20Sopenharmony_ci of_machine_is_compatible("PowerBook4,3") || 1768c2ecf20Sopenharmony_ci of_machine_is_compatible("PowerBook6,3") || 1778c2ecf20Sopenharmony_ci of_machine_is_compatible("PowerBook6,5"); 1788c2ecf20Sopenharmony_ci#endif 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci rinfo->info->bl_dev = bd; 1818c2ecf20Sopenharmony_ci fb_bl_default_curve(rinfo->info, 0, 1828c2ecf20Sopenharmony_ci 63 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL, 1838c2ecf20Sopenharmony_ci 217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci bd->props.brightness = bd->props.max_brightness; 1868c2ecf20Sopenharmony_ci bd->props.power = FB_BLANK_UNBLANK; 1878c2ecf20Sopenharmony_ci backlight_update_status(bd); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci printk("radeonfb: Backlight initialized (%s)\n", name); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci return; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cierror: 1948c2ecf20Sopenharmony_ci kfree(pdata); 1958c2ecf20Sopenharmony_ci return; 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_civoid radeonfb_bl_exit(struct radeonfb_info *rinfo) 1998c2ecf20Sopenharmony_ci{ 2008c2ecf20Sopenharmony_ci struct backlight_device *bd = rinfo->info->bl_dev; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci if (bd) { 2038c2ecf20Sopenharmony_ci struct radeon_bl_privdata *pdata; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci pdata = bl_get_data(bd); 2068c2ecf20Sopenharmony_ci backlight_device_unregister(bd); 2078c2ecf20Sopenharmony_ci kfree(pdata); 2088c2ecf20Sopenharmony_ci rinfo->info->bl_dev = NULL; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci printk("radeonfb: Backlight unloaded\n"); 2118c2ecf20Sopenharmony_ci } 2128c2ecf20Sopenharmony_ci} 213