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 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 18 | static const struct backlight_ops pmu_backlight_data; |
David Woodhouse | 4efd587 | 2006-09-29 01:58:37 -0700 | [diff] [blame] | 19 | static DEFINE_SPINLOCK(pmu_backlight_lock); |
Benjamin Herrenschmidt | fa19d63 | 2008-03-03 17:27:46 +1100 | [diff] [blame] | 20 | static int sleeping, uses_pmu_bl; |
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 | { |
Benjamin Herrenschmidt | 0094f2c | 2007-12-20 15:00:21 +1100 | [diff] [blame] | 25 | int i, flat, count, range = (max - min); |
Benjamin Herrenschmidt | d565dd3 | 2006-08-31 21:27:49 -0700 | [diff] [blame] | 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 | |
Benjamin Herrenschmidt | 0094f2c | 2007-12-20 15:00:21 +1100 | [diff] [blame] | 71 | static int __pmu_backlight_update_status(struct backlight_device *bd) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 72 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 73 | struct adb_request req; |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 74 | int level = bd->props.brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 75 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 76 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 77 | if (bd->props.power != FB_BLANK_UNBLANK || |
| 78 | bd->props.fb_blank != FB_BLANK_UNBLANK) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 79 | level = 0; |
| 80 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 81 | if (level > 0) { |
Benjamin Herrenschmidt | d565dd3 | 2006-08-31 21:27:49 -0700 | [diff] [blame] | 82 | int pmulevel = pmu_backlight_get_level_brightness(level); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 83 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 84 | pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel); |
| 85 | pmu_wait_complete(&req); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 86 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 87 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, |
| 88 | PMU_POW_BACKLIGHT | PMU_POW_ON); |
| 89 | pmu_wait_complete(&req); |
| 90 | } else { |
| 91 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, |
| 92 | PMU_POW_BACKLIGHT | PMU_POW_OFF); |
| 93 | pmu_wait_complete(&req); |
| 94 | } |
| 95 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Benjamin Herrenschmidt | 0094f2c | 2007-12-20 15:00:21 +1100 | [diff] [blame] | 99 | static int pmu_backlight_update_status(struct backlight_device *bd) |
| 100 | { |
| 101 | unsigned long flags; |
| 102 | int rc = 0; |
| 103 | |
| 104 | spin_lock_irqsave(&pmu_backlight_lock, flags); |
| 105 | /* Don't update brightness when sleeping */ |
| 106 | if (!sleeping) |
| 107 | rc = __pmu_backlight_update_status(bd); |
| 108 | spin_unlock_irqrestore(&pmu_backlight_lock, flags); |
| 109 | return rc; |
| 110 | } |
| 111 | |
| 112 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 113 | static const struct backlight_ops pmu_backlight_data = { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 114 | .update_status = pmu_backlight_update_status, |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 115 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 118 | #ifdef CONFIG_PM |
Benjamin Herrenschmidt | d565dd3 | 2006-08-31 21:27:49 -0700 | [diff] [blame] | 119 | void pmu_backlight_set_sleep(int sleep) |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 120 | { |
| 121 | unsigned long flags; |
| 122 | |
| 123 | spin_lock_irqsave(&pmu_backlight_lock, flags); |
Benjamin Herrenschmidt | d565dd3 | 2006-08-31 21:27:49 -0700 | [diff] [blame] | 124 | sleeping = sleep; |
Benjamin Herrenschmidt | fa19d63 | 2008-03-03 17:27:46 +1100 | [diff] [blame] | 125 | if (pmac_backlight && uses_pmu_bl) { |
Benjamin Herrenschmidt | 0094f2c | 2007-12-20 15:00:21 +1100 | [diff] [blame] | 126 | if (sleep) { |
| 127 | struct adb_request req; |
| 128 | |
| 129 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, |
| 130 | PMU_POW_BACKLIGHT | PMU_POW_OFF); |
| 131 | pmu_wait_complete(&req); |
| 132 | } else |
| 133 | __pmu_backlight_update_status(pmac_backlight); |
| 134 | } |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 135 | spin_unlock_irqrestore(&pmu_backlight_lock, flags); |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 136 | } |
Benjamin Herrenschmidt | d565dd3 | 2006-08-31 21:27:49 -0700 | [diff] [blame] | 137 | #endif /* CONFIG_PM */ |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 138 | |
| 139 | void __init pmu_backlight_init() |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 140 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 141 | struct backlight_properties props; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 142 | struct backlight_device *bd; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 143 | char name[10]; |
| 144 | int level, autosave; |
| 145 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 146 | /* Special case for the old PowerBook since I can't test on it */ |
| 147 | autosave = |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 148 | of_machine_is_compatible("AAPL,3400/2400") || |
| 149 | of_machine_is_compatible("AAPL,3500"); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 150 | |
| 151 | if (!autosave && |
| 152 | !pmac_has_backlight_type("pmu") && |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 153 | !of_machine_is_compatible("AAPL,PowerBook1998") && |
| 154 | !of_machine_is_compatible("PowerBook1,1")) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 155 | return; |
| 156 | |
Benjamin Herrenschmidt | d565dd3 | 2006-08-31 21:27:49 -0700 | [diff] [blame] | 157 | snprintf(name, sizeof(name), "pmubl"); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 158 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 159 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 160 | props.type = BACKLIGHT_PLATFORM; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 161 | props.max_brightness = FB_BACKLIGHT_LEVELS - 1; |
| 162 | bd = backlight_device_register(name, NULL, NULL, &pmu_backlight_data, |
| 163 | &props); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 164 | if (IS_ERR(bd)) { |
Benjamin Herrenschmidt | 0094f2c | 2007-12-20 15:00:21 +1100 | [diff] [blame] | 165 | printk(KERN_ERR "PMU Backlight registration failed\n"); |
| 166 | return; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 167 | } |
Benjamin Herrenschmidt | fa19d63 | 2008-03-03 17:27:46 +1100 | [diff] [blame] | 168 | uses_pmu_bl = 1; |
Benjamin Herrenschmidt | d565dd3 | 2006-08-31 21:27:49 -0700 | [diff] [blame] | 169 | pmu_backlight_init_curve(0x7F, 0x46, 0x0E); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 170 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 171 | level = bd->props.max_brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 172 | |
| 173 | if (autosave) { |
| 174 | /* read autosaved value if available */ |
| 175 | struct adb_request req; |
| 176 | pmu_request(&req, NULL, 2, 0xd9, 0); |
| 177 | pmu_wait_complete(&req); |
| 178 | |
Benjamin Herrenschmidt | d565dd3 | 2006-08-31 21:27:49 -0700 | [diff] [blame] | 179 | level = pmu_backlight_curve_lookup( |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 180 | (req.reply[0] >> 4) * |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 181 | bd->props.max_brightness / 15); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 184 | bd->props.brightness = level; |
| 185 | bd->props.power = FB_BLANK_UNBLANK; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 186 | backlight_update_status(bd); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 187 | |
Benjamin Herrenschmidt | 0094f2c | 2007-12-20 15:00:21 +1100 | [diff] [blame] | 188 | printk(KERN_INFO "PMU Backlight initialized (%s)\n", name); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 189 | } |