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