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; |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 19 | static spinlock_t pmu_backlight_lock; |
| 20 | static int sleeping; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 21 | |
| 22 | static int pmu_backlight_get_level_brightness(struct fb_info *info, |
| 23 | int level) |
| 24 | { |
| 25 | int pmulevel; |
| 26 | |
| 27 | /* Get and convert the value */ |
| 28 | mutex_lock(&info->bl_mutex); |
| 29 | pmulevel = info->bl_curve[level] * FB_BACKLIGHT_MAX / MAX_PMU_LEVEL; |
| 30 | mutex_unlock(&info->bl_mutex); |
| 31 | |
| 32 | if (pmulevel < 0) |
| 33 | pmulevel = 0; |
| 34 | else if (pmulevel > MAX_PMU_LEVEL) |
| 35 | pmulevel = MAX_PMU_LEVEL; |
| 36 | |
| 37 | return pmulevel; |
| 38 | } |
| 39 | |
| 40 | static int pmu_backlight_update_status(struct backlight_device *bd) |
| 41 | { |
| 42 | struct fb_info *info = class_get_devdata(&bd->class_dev); |
| 43 | struct adb_request req; |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 44 | unsigned long flags; |
| 45 | int level = bd->props->brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 46 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 47 | spin_lock_irqsave(&pmu_backlight_lock, flags); |
| 48 | |
| 49 | /* Don't update brightness when sleeping */ |
| 50 | if (sleeping) |
| 51 | goto out; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 52 | |
| 53 | if (bd->props->power != FB_BLANK_UNBLANK || |
| 54 | bd->props->fb_blank != FB_BLANK_UNBLANK) |
| 55 | level = 0; |
| 56 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 57 | if (level > 0) { |
| 58 | int pmulevel = pmu_backlight_get_level_brightness(info, level); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 59 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 60 | pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel); |
| 61 | pmu_wait_complete(&req); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 62 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 63 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, |
| 64 | PMU_POW_BACKLIGHT | PMU_POW_ON); |
| 65 | pmu_wait_complete(&req); |
| 66 | } else { |
| 67 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, |
| 68 | PMU_POW_BACKLIGHT | PMU_POW_OFF); |
| 69 | pmu_wait_complete(&req); |
| 70 | } |
| 71 | |
| 72 | out: |
| 73 | spin_unlock_irqrestore(&pmu_backlight_lock, flags); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int pmu_backlight_get_brightness(struct backlight_device *bd) |
| 79 | { |
| 80 | return bd->props->brightness; |
| 81 | } |
| 82 | |
| 83 | static struct backlight_properties pmu_backlight_data = { |
| 84 | .owner = THIS_MODULE, |
| 85 | .get_brightness = pmu_backlight_get_brightness, |
| 86 | .update_status = pmu_backlight_update_status, |
| 87 | .max_brightness = (FB_BACKLIGHT_LEVELS - 1), |
| 88 | }; |
| 89 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 90 | #ifdef CONFIG_PM |
| 91 | static int pmu_backlight_sleep_call(struct pmu_sleep_notifier *self, int when) |
| 92 | { |
| 93 | unsigned long flags; |
| 94 | |
| 95 | spin_lock_irqsave(&pmu_backlight_lock, flags); |
| 96 | |
| 97 | switch (when) { |
| 98 | case PBOOK_SLEEP_REQUEST: |
| 99 | sleeping = 1; |
| 100 | break; |
| 101 | case PBOOK_WAKE: |
| 102 | sleeping = 0; |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | spin_unlock_irqrestore(&pmu_backlight_lock, flags); |
| 107 | |
| 108 | return PBOOK_SLEEP_OK; |
| 109 | } |
| 110 | |
| 111 | static struct pmu_sleep_notifier pmu_backlight_sleep_notif = { |
| 112 | .notifier_call = pmu_backlight_sleep_call, |
| 113 | }; |
| 114 | #endif |
| 115 | |
| 116 | void __init pmu_backlight_init() |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 117 | { |
| 118 | struct backlight_device *bd; |
| 119 | struct fb_info *info; |
| 120 | char name[10]; |
| 121 | int level, autosave; |
| 122 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 123 | /* Special case for the old PowerBook since I can't test on it */ |
| 124 | autosave = |
| 125 | machine_is_compatible("AAPL,3400/2400") || |
| 126 | machine_is_compatible("AAPL,3500"); |
| 127 | |
| 128 | if (!autosave && |
| 129 | !pmac_has_backlight_type("pmu") && |
| 130 | !machine_is_compatible("AAPL,PowerBook1998") && |
| 131 | !machine_is_compatible("PowerBook1,1")) |
| 132 | return; |
| 133 | |
| 134 | /* Actually, this is a hack, but I don't know of a better way |
| 135 | * to get the first framebuffer device. |
| 136 | */ |
| 137 | info = registered_fb[0]; |
| 138 | if (!info) { |
| 139 | printk("pmubl: No framebuffer found\n"); |
| 140 | goto error; |
| 141 | } |
| 142 | |
| 143 | snprintf(name, sizeof(name), "pmubl%d", info->node); |
| 144 | |
| 145 | bd = backlight_device_register(name, info, &pmu_backlight_data); |
| 146 | if (IS_ERR(bd)) { |
| 147 | printk("pmubl: Backlight registration failed\n"); |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | mutex_lock(&info->bl_mutex); |
| 152 | info->bl_dev = bd; |
| 153 | fb_bl_default_curve(info, 0x7F, 0x46, 0x0E); |
| 154 | mutex_unlock(&info->bl_mutex); |
| 155 | |
| 156 | level = pmu_backlight_data.max_brightness; |
| 157 | |
| 158 | if (autosave) { |
| 159 | /* read autosaved value if available */ |
| 160 | struct adb_request req; |
| 161 | pmu_request(&req, NULL, 2, 0xd9, 0); |
| 162 | pmu_wait_complete(&req); |
| 163 | |
| 164 | mutex_lock(&info->bl_mutex); |
| 165 | level = pmac_backlight_curve_lookup(info, |
| 166 | (req.reply[0] >> 4) * |
| 167 | pmu_backlight_data.max_brightness / 15); |
| 168 | mutex_unlock(&info->bl_mutex); |
| 169 | } |
| 170 | |
| 171 | up(&bd->sem); |
| 172 | bd->props->brightness = level; |
| 173 | bd->props->power = FB_BLANK_UNBLANK; |
| 174 | bd->props->update_status(bd); |
| 175 | down(&bd->sem); |
| 176 | |
| 177 | mutex_lock(&pmac_backlight_mutex); |
| 178 | if (!pmac_backlight) |
| 179 | pmac_backlight = bd; |
| 180 | mutex_unlock(&pmac_backlight_mutex); |
| 181 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 182 | #ifdef CONFIG_PM |
| 183 | pmu_register_sleep_notifier(&pmu_backlight_sleep_notif); |
| 184 | #endif |
| 185 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 186 | printk("pmubl: Backlight initialized (%s)\n", name); |
| 187 | |
| 188 | return; |
| 189 | |
| 190 | error: |
| 191 | return; |
| 192 | } |