^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Backlight code for via-pmu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2001-2002 Benjamin Herrenschmidt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/adb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pmu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/prom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define MAX_PMU_LEVEL 0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static const struct backlight_ops pmu_backlight_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static DEFINE_SPINLOCK(pmu_backlight_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int sleeping, uses_pmu_bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static u8 bl_curve[FB_BACKLIGHT_LEVELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static void pmu_backlight_init_curve(u8 off, u8 min, u8 max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int i, flat, count, range = (max - min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) bl_curve[0] = off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) bl_curve[flat] = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) count = FB_BACKLIGHT_LEVELS * 15 / 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) for (i = 0; i < count; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) bl_curve[flat + i] = min + (range * (i + 1) / count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int pmu_backlight_curve_lookup(int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int level = (FB_BACKLIGHT_LEVELS - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int i, max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* Look for biggest value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) max = max((int)bl_curve[i], max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Look for nearest value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int diff = abs(bl_curve[i] - value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (diff < max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) max = diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) level = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int pmu_backlight_get_level_brightness(int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int pmulevel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* Get and convert the value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) pmulevel = bl_curve[level] * FB_BACKLIGHT_MAX / MAX_PMU_LEVEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (pmulevel < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pmulevel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) else if (pmulevel > MAX_PMU_LEVEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) pmulevel = MAX_PMU_LEVEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return pmulevel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int __pmu_backlight_update_status(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct adb_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int level = bd->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (bd->props.power != FB_BLANK_UNBLANK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) bd->props.fb_blank != FB_BLANK_UNBLANK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (level > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int pmulevel = pmu_backlight_get_level_brightness(level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) pmu_wait_complete(&req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) PMU_POW_BACKLIGHT | PMU_POW_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) pmu_wait_complete(&req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) PMU_POW_BACKLIGHT | PMU_POW_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pmu_wait_complete(&req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int pmu_backlight_update_status(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) spin_lock_irqsave(&pmu_backlight_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* Don't update brightness when sleeping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!sleeping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) rc = __pmu_backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) spin_unlock_irqrestore(&pmu_backlight_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static const struct backlight_ops pmu_backlight_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .update_status = pmu_backlight_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) void pmu_backlight_set_sleep(int sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) spin_lock_irqsave(&pmu_backlight_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) sleeping = sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (pmac_backlight && uses_pmu_bl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (sleep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct adb_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) PMU_POW_BACKLIGHT | PMU_POW_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pmu_wait_complete(&req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __pmu_backlight_update_status(pmac_backlight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) spin_unlock_irqrestore(&pmu_backlight_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) void __init pmu_backlight_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct backlight_device *bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) char name[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int level, autosave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Special case for the old PowerBook since I can't test on it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) autosave =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) of_machine_is_compatible("AAPL,3400/2400") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) of_machine_is_compatible("AAPL,3500");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!autosave &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) !pmac_has_backlight_type("pmu") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) !of_machine_is_compatible("AAPL,PowerBook1998") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) !of_machine_is_compatible("PowerBook1,1"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) snprintf(name, sizeof(name), "pmubl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) memset(&props, 0, sizeof(struct backlight_properties));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) props.type = BACKLIGHT_PLATFORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) bd = backlight_device_register(name, NULL, NULL, &pmu_backlight_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (IS_ERR(bd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) printk(KERN_ERR "PMU Backlight registration failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) uses_pmu_bl = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) pmu_backlight_init_curve(0x7F, 0x46, 0x0E);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) level = bd->props.max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (autosave) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /* read autosaved value if available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct adb_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) pmu_request(&req, NULL, 2, 0xd9, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pmu_wait_complete(&req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) level = pmu_backlight_curve_lookup(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) (req.reply[0] >> 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) bd->props.max_brightness / 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) bd->props.brightness = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) bd->props.power = FB_BLANK_UNBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) printk(KERN_INFO "PMU Backlight initialized (%s)\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }