Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Miscellaneous procedures for dealing with the PowerMac hardware. |
| 3 | * Contains support for the backlight. |
| 4 | * |
| 5 | * Copyright (C) 2000 Benjamin Herrenschmidt |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 6 | * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <linux/config.h> |
| 11 | #include <linux/kernel.h> |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 12 | #include <linux/fb.h> |
| 13 | #include <linux/backlight.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 14 | #include <asm/prom.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 15 | #include <asm/backlight.h> |
| 16 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 17 | #define OLD_BACKLIGHT_MAX 15 |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 18 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 19 | /* Protect the pmac_backlight variable */ |
| 20 | DEFINE_MUTEX(pmac_backlight_mutex); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 21 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 22 | /* Main backlight storage |
| 23 | * |
| 24 | * Backlight drivers in this variable are required to have the "props" |
| 25 | * attribute set and to have an update_status function. |
| 26 | * |
| 27 | * We can only store one backlight here, but since Apple laptops have only one |
| 28 | * internal display, it doesn't matter. Other backlight drivers can be used |
| 29 | * independently. |
| 30 | * |
| 31 | * Lock ordering: |
| 32 | * pmac_backlight_mutex (global, main backlight) |
| 33 | * pmac_backlight->sem (backlight class) |
| 34 | */ |
| 35 | struct backlight_device *pmac_backlight; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 36 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 37 | int pmac_has_backlight_type(const char *type) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 38 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 39 | struct device_node* bk_node = find_devices("backlight"); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 40 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 41 | if (bk_node) { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 42 | char *prop = get_property(bk_node, "backlight-control", NULL); |
| 43 | if (prop && strncmp(prop, type, strlen(type)) == 0) |
| 44 | return 1; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 45 | } |
| 46 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 50 | int pmac_backlight_curve_lookup(struct fb_info *info, int value) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 51 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 52 | int level = (FB_BACKLIGHT_LEVELS - 1); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 53 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 54 | if (info && info->bl_dev) { |
| 55 | int i, max = 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 56 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 57 | /* Look for biggest value */ |
| 58 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) |
| 59 | max = max((int)info->bl_curve[i], max); |
| 60 | |
| 61 | /* Look for nearest value */ |
| 62 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) { |
| 63 | int diff = abs(info->bl_curve[i] - value); |
| 64 | if (diff < max) { |
| 65 | max = diff; |
| 66 | level = i; |
| 67 | } |
| 68 | } |
| 69 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 70 | } |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 71 | |
| 72 | return level; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 73 | } |
| 74 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 75 | static void pmac_backlight_key(int direction) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 76 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 77 | mutex_lock(&pmac_backlight_mutex); |
| 78 | if (pmac_backlight) { |
| 79 | struct backlight_properties *props; |
| 80 | int brightness; |
| 81 | |
| 82 | down(&pmac_backlight->sem); |
| 83 | props = pmac_backlight->props; |
| 84 | |
| 85 | brightness = props->brightness + |
| 86 | ((direction?-1:1) * (props->max_brightness / 15)); |
| 87 | |
| 88 | if (brightness < 0) |
| 89 | brightness = 0; |
| 90 | else if (brightness > props->max_brightness) |
| 91 | brightness = props->max_brightness; |
| 92 | |
| 93 | props->brightness = brightness; |
| 94 | props->update_status(pmac_backlight); |
| 95 | |
| 96 | up(&pmac_backlight->sem); |
| 97 | } |
| 98 | mutex_unlock(&pmac_backlight_mutex); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 99 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 100 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 101 | void pmac_backlight_key_up() |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 102 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 103 | pmac_backlight_key(0); |
| 104 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 105 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 106 | void pmac_backlight_key_down() |
| 107 | { |
| 108 | pmac_backlight_key(1); |
| 109 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 110 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 111 | int pmac_backlight_set_legacy_brightness(int brightness) |
| 112 | { |
| 113 | int error = -ENXIO; |
| 114 | |
| 115 | mutex_lock(&pmac_backlight_mutex); |
| 116 | if (pmac_backlight) { |
| 117 | struct backlight_properties *props; |
| 118 | |
| 119 | down(&pmac_backlight->sem); |
| 120 | props = pmac_backlight->props; |
| 121 | props->brightness = brightness * |
| 122 | props->max_brightness / OLD_BACKLIGHT_MAX; |
| 123 | props->update_status(pmac_backlight); |
| 124 | up(&pmac_backlight->sem); |
| 125 | |
| 126 | error = 0; |
| 127 | } |
| 128 | mutex_unlock(&pmac_backlight_mutex); |
| 129 | |
| 130 | return error; |
| 131 | } |
| 132 | |
| 133 | int pmac_backlight_get_legacy_brightness() |
| 134 | { |
| 135 | int result = -ENXIO; |
| 136 | |
| 137 | mutex_lock(&pmac_backlight_mutex); |
| 138 | if (pmac_backlight) { |
| 139 | struct backlight_properties *props; |
| 140 | |
| 141 | down(&pmac_backlight->sem); |
| 142 | props = pmac_backlight->props; |
| 143 | result = props->brightness * |
| 144 | OLD_BACKLIGHT_MAX / props->max_brightness; |
| 145 | up(&pmac_backlight->sem); |
| 146 | } |
| 147 | mutex_unlock(&pmac_backlight_mutex); |
| 148 | |
| 149 | return result; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 150 | } |