blob: 1cec02f6c431a0af52f8360f262c2cdb6859e14e [file] [log] [blame]
Michael Hanselmann5474c122006-06-25 05:47:08 -07001/*
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
Richard Purdie599a52d2007-02-10 23:07:48 +000018static struct backlight_ops pmu_backlight_data;
David Woodhouse4efd5872006-09-29 01:58:37 -070019static DEFINE_SPINLOCK(pmu_backlight_lock);
Benjamin Herrenschmidtfa19d632008-03-03 17:27:46 +110020static int sleeping, uses_pmu_bl;
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070021static u8 bl_curve[FB_BACKLIGHT_LEVELS];
Michael Hanselmann5474c122006-06-25 05:47:08 -070022
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070023static void pmu_backlight_init_curve(u8 off, u8 min, u8 max)
24{
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +110025 int i, flat, count, range = (max - min);
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070026
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
37static 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
57static int pmu_backlight_get_level_brightness(int level)
Michael Hanselmann5474c122006-06-25 05:47:08 -070058{
59 int pmulevel;
60
61 /* Get and convert the value */
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070062 pmulevel = bl_curve[level] * FB_BACKLIGHT_MAX / MAX_PMU_LEVEL;
Michael Hanselmann5474c122006-06-25 05:47:08 -070063 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 Herrenschmidt0094f2c2007-12-20 15:00:21 +110071static int __pmu_backlight_update_status(struct backlight_device *bd)
Michael Hanselmann5474c122006-06-25 05:47:08 -070072{
Michael Hanselmann5474c122006-06-25 05:47:08 -070073 struct adb_request req;
Richard Purdie599a52d2007-02-10 23:07:48 +000074 int level = bd->props.brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -070075
Michael Hanselmann5474c122006-06-25 05:47:08 -070076
Richard Purdie599a52d2007-02-10 23:07:48 +000077 if (bd->props.power != FB_BLANK_UNBLANK ||
78 bd->props.fb_blank != FB_BLANK_UNBLANK)
Michael Hanselmann5474c122006-06-25 05:47:08 -070079 level = 0;
80
Michael Hanselmann4b755992006-07-30 03:04:19 -070081 if (level > 0) {
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070082 int pmulevel = pmu_backlight_get_level_brightness(level);
Michael Hanselmann5474c122006-06-25 05:47:08 -070083
Michael Hanselmann4b755992006-07-30 03:04:19 -070084 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel);
85 pmu_wait_complete(&req);
Michael Hanselmann5474c122006-06-25 05:47:08 -070086
Michael Hanselmann4b755992006-07-30 03:04:19 -070087 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 Hanselmann5474c122006-06-25 05:47:08 -070096 return 0;
97}
98
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +110099static 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
Michael Hanselmann5474c122006-06-25 05:47:08 -0700113static int pmu_backlight_get_brightness(struct backlight_device *bd)
114{
Richard Purdie599a52d2007-02-10 23:07:48 +0000115 return bd->props.brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700116}
117
Richard Purdie599a52d2007-02-10 23:07:48 +0000118static struct backlight_ops pmu_backlight_data = {
Michael Hanselmann5474c122006-06-25 05:47:08 -0700119 .get_brightness = pmu_backlight_get_brightness,
120 .update_status = pmu_backlight_update_status,
Richard Purdie599a52d2007-02-10 23:07:48 +0000121
Michael Hanselmann5474c122006-06-25 05:47:08 -0700122};
123
Michael Hanselmann4b755992006-07-30 03:04:19 -0700124#ifdef CONFIG_PM
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700125void pmu_backlight_set_sleep(int sleep)
Michael Hanselmann4b755992006-07-30 03:04:19 -0700126{
127 unsigned long flags;
128
129 spin_lock_irqsave(&pmu_backlight_lock, flags);
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700130 sleeping = sleep;
Benjamin Herrenschmidtfa19d632008-03-03 17:27:46 +1100131 if (pmac_backlight && uses_pmu_bl) {
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +1100132 if (sleep) {
133 struct adb_request req;
134
135 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
136 PMU_POW_BACKLIGHT | PMU_POW_OFF);
137 pmu_wait_complete(&req);
138 } else
139 __pmu_backlight_update_status(pmac_backlight);
140 }
Michael Hanselmann4b755992006-07-30 03:04:19 -0700141 spin_unlock_irqrestore(&pmu_backlight_lock, flags);
Michael Hanselmann4b755992006-07-30 03:04:19 -0700142}
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700143#endif /* CONFIG_PM */
Michael Hanselmann4b755992006-07-30 03:04:19 -0700144
145void __init pmu_backlight_init()
Michael Hanselmann5474c122006-06-25 05:47:08 -0700146{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500147 struct backlight_properties props;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700148 struct backlight_device *bd;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700149 char name[10];
150 int level, autosave;
151
Michael Hanselmann5474c122006-06-25 05:47:08 -0700152 /* Special case for the old PowerBook since I can't test on it */
153 autosave =
Grant Likely71a157e2010-02-01 21:34:14 -0700154 of_machine_is_compatible("AAPL,3400/2400") ||
155 of_machine_is_compatible("AAPL,3500");
Michael Hanselmann5474c122006-06-25 05:47:08 -0700156
157 if (!autosave &&
158 !pmac_has_backlight_type("pmu") &&
Grant Likely71a157e2010-02-01 21:34:14 -0700159 !of_machine_is_compatible("AAPL,PowerBook1998") &&
160 !of_machine_is_compatible("PowerBook1,1"))
Michael Hanselmann5474c122006-06-25 05:47:08 -0700161 return;
162
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700163 snprintf(name, sizeof(name), "pmubl");
Michael Hanselmann5474c122006-06-25 05:47:08 -0700164
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500165 memset(&props, 0, sizeof(struct backlight_properties));
166 props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
167 bd = backlight_device_register(name, NULL, NULL, &pmu_backlight_data,
168 &props);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700169 if (IS_ERR(bd)) {
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +1100170 printk(KERN_ERR "PMU Backlight registration failed\n");
171 return;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700172 }
Benjamin Herrenschmidtfa19d632008-03-03 17:27:46 +1100173 uses_pmu_bl = 1;
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700174 pmu_backlight_init_curve(0x7F, 0x46, 0x0E);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700175
Richard Purdie599a52d2007-02-10 23:07:48 +0000176 level = bd->props.max_brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700177
178 if (autosave) {
179 /* read autosaved value if available */
180 struct adb_request req;
181 pmu_request(&req, NULL, 2, 0xd9, 0);
182 pmu_wait_complete(&req);
183
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700184 level = pmu_backlight_curve_lookup(
Michael Hanselmann5474c122006-06-25 05:47:08 -0700185 (req.reply[0] >> 4) *
Richard Purdie599a52d2007-02-10 23:07:48 +0000186 bd->props.max_brightness / 15);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700187 }
188
Richard Purdie599a52d2007-02-10 23:07:48 +0000189 bd->props.brightness = level;
190 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +0000191 backlight_update_status(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700192
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +1100193 printk(KERN_INFO "PMU Backlight initialized (%s)\n", name);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700194}