blob: 50ada02ae75d7deb4d9bd4af53b93d262653de73 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Michael Hanselmann5474c122006-06-25 05:47:08 -07002/*
3 * Backlight code for via-pmu
4 *
5 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
6 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
7 * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
8 *
9 */
10
11#include <asm/ptrace.h>
12#include <linux/adb.h>
13#include <linux/pmu.h>
14#include <asm/backlight.h>
15#include <asm/prom.h>
16
17#define MAX_PMU_LEVEL 0xFF
18
Lionel Debrouxacc24722010-11-16 14:14:02 +010019static const struct backlight_ops pmu_backlight_data;
David Woodhouse4efd5872006-09-29 01:58:37 -070020static DEFINE_SPINLOCK(pmu_backlight_lock);
Benjamin Herrenschmidtfa19d632008-03-03 17:27:46 +110021static int sleeping, uses_pmu_bl;
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070022static u8 bl_curve[FB_BACKLIGHT_LEVELS];
Michael Hanselmann5474c122006-06-25 05:47:08 -070023
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070024static void pmu_backlight_init_curve(u8 off, u8 min, u8 max)
25{
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +110026 int i, flat, count, range = (max - min);
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070027
28 bl_curve[0] = off;
29
30 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
31 bl_curve[flat] = min;
32
33 count = FB_BACKLIGHT_LEVELS * 15 / 16;
34 for (i = 0; i < count; ++i)
35 bl_curve[flat + i] = min + (range * (i + 1) / count);
36}
37
38static int pmu_backlight_curve_lookup(int value)
39{
40 int level = (FB_BACKLIGHT_LEVELS - 1);
41 int i, max = 0;
42
43 /* Look for biggest value */
44 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
45 max = max((int)bl_curve[i], max);
46
47 /* Look for nearest value */
48 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
49 int diff = abs(bl_curve[i] - value);
50 if (diff < max) {
51 max = diff;
52 level = i;
53 }
54 }
55 return level;
56}
57
58static int pmu_backlight_get_level_brightness(int level)
Michael Hanselmann5474c122006-06-25 05:47:08 -070059{
60 int pmulevel;
61
62 /* Get and convert the value */
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070063 pmulevel = bl_curve[level] * FB_BACKLIGHT_MAX / MAX_PMU_LEVEL;
Michael Hanselmann5474c122006-06-25 05:47:08 -070064 if (pmulevel < 0)
65 pmulevel = 0;
66 else if (pmulevel > MAX_PMU_LEVEL)
67 pmulevel = MAX_PMU_LEVEL;
68
69 return pmulevel;
70}
71
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +110072static int __pmu_backlight_update_status(struct backlight_device *bd)
Michael Hanselmann5474c122006-06-25 05:47:08 -070073{
Michael Hanselmann5474c122006-06-25 05:47:08 -070074 struct adb_request req;
Richard Purdie599a52d2007-02-10 23:07:48 +000075 int level = bd->props.brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -070076
Michael Hanselmann5474c122006-06-25 05:47:08 -070077
Richard Purdie599a52d2007-02-10 23:07:48 +000078 if (bd->props.power != FB_BLANK_UNBLANK ||
79 bd->props.fb_blank != FB_BLANK_UNBLANK)
Michael Hanselmann5474c122006-06-25 05:47:08 -070080 level = 0;
81
Michael Hanselmann4b755992006-07-30 03:04:19 -070082 if (level > 0) {
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -070083 int pmulevel = pmu_backlight_get_level_brightness(level);
Michael Hanselmann5474c122006-06-25 05:47:08 -070084
Michael Hanselmann4b755992006-07-30 03:04:19 -070085 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel);
86 pmu_wait_complete(&req);
Michael Hanselmann5474c122006-06-25 05:47:08 -070087
Michael Hanselmann4b755992006-07-30 03:04:19 -070088 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
89 PMU_POW_BACKLIGHT | PMU_POW_ON);
90 pmu_wait_complete(&req);
91 } else {
92 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
93 PMU_POW_BACKLIGHT | PMU_POW_OFF);
94 pmu_wait_complete(&req);
95 }
96
Michael Hanselmann5474c122006-06-25 05:47:08 -070097 return 0;
98}
99
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +1100100static int pmu_backlight_update_status(struct backlight_device *bd)
101{
102 unsigned long flags;
103 int rc = 0;
104
105 spin_lock_irqsave(&pmu_backlight_lock, flags);
106 /* Don't update brightness when sleeping */
107 if (!sleeping)
108 rc = __pmu_backlight_update_status(bd);
109 spin_unlock_irqrestore(&pmu_backlight_lock, flags);
110 return rc;
111}
112
113
Lionel Debrouxacc24722010-11-16 14:14:02 +0100114static const struct backlight_ops pmu_backlight_data = {
Michael Hanselmann5474c122006-06-25 05:47:08 -0700115 .update_status = pmu_backlight_update_status,
Richard Purdie599a52d2007-02-10 23:07:48 +0000116
Michael Hanselmann5474c122006-06-25 05:47:08 -0700117};
118
Michael Hanselmann4b755992006-07-30 03:04:19 -0700119#ifdef CONFIG_PM
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700120void pmu_backlight_set_sleep(int sleep)
Michael Hanselmann4b755992006-07-30 03:04:19 -0700121{
122 unsigned long flags;
123
124 spin_lock_irqsave(&pmu_backlight_lock, flags);
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700125 sleeping = sleep;
Benjamin Herrenschmidtfa19d632008-03-03 17:27:46 +1100126 if (pmac_backlight && uses_pmu_bl) {
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +1100127 if (sleep) {
128 struct adb_request req;
129
130 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
131 PMU_POW_BACKLIGHT | PMU_POW_OFF);
132 pmu_wait_complete(&req);
133 } else
134 __pmu_backlight_update_status(pmac_backlight);
135 }
Michael Hanselmann4b755992006-07-30 03:04:19 -0700136 spin_unlock_irqrestore(&pmu_backlight_lock, flags);
Michael Hanselmann4b755992006-07-30 03:04:19 -0700137}
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700138#endif /* CONFIG_PM */
Michael Hanselmann4b755992006-07-30 03:04:19 -0700139
Mathieu Malaterre00f7b292017-12-26 15:12:41 +0100140void __init pmu_backlight_init(void)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700141{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500142 struct backlight_properties props;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700143 struct backlight_device *bd;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700144 char name[10];
145 int level, autosave;
146
Michael Hanselmann5474c122006-06-25 05:47:08 -0700147 /* Special case for the old PowerBook since I can't test on it */
148 autosave =
Grant Likely71a157e2010-02-01 21:34:14 -0700149 of_machine_is_compatible("AAPL,3400/2400") ||
150 of_machine_is_compatible("AAPL,3500");
Michael Hanselmann5474c122006-06-25 05:47:08 -0700151
152 if (!autosave &&
153 !pmac_has_backlight_type("pmu") &&
Grant Likely71a157e2010-02-01 21:34:14 -0700154 !of_machine_is_compatible("AAPL,PowerBook1998") &&
155 !of_machine_is_compatible("PowerBook1,1"))
Michael Hanselmann5474c122006-06-25 05:47:08 -0700156 return;
157
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700158 snprintf(name, sizeof(name), "pmubl");
Michael Hanselmann5474c122006-06-25 05:47:08 -0700159
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500160 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700161 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500162 props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
163 bd = backlight_device_register(name, NULL, NULL, &pmu_backlight_data,
164 &props);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700165 if (IS_ERR(bd)) {
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +1100166 printk(KERN_ERR "PMU Backlight registration failed\n");
167 return;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700168 }
Benjamin Herrenschmidtfa19d632008-03-03 17:27:46 +1100169 uses_pmu_bl = 1;
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700170 pmu_backlight_init_curve(0x7F, 0x46, 0x0E);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700171
Richard Purdie599a52d2007-02-10 23:07:48 +0000172 level = bd->props.max_brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700173
174 if (autosave) {
175 /* read autosaved value if available */
176 struct adb_request req;
177 pmu_request(&req, NULL, 2, 0xd9, 0);
178 pmu_wait_complete(&req);
179
Benjamin Herrenschmidtd565dd32006-08-31 21:27:49 -0700180 level = pmu_backlight_curve_lookup(
Michael Hanselmann5474c122006-06-25 05:47:08 -0700181 (req.reply[0] >> 4) *
Richard Purdie599a52d2007-02-10 23:07:48 +0000182 bd->props.max_brightness / 15);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700183 }
184
Richard Purdie599a52d2007-02-10 23:07:48 +0000185 bd->props.brightness = level;
186 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +0000187 backlight_update_status(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700188
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +1100189 printk(KERN_INFO "PMU Backlight initialized (%s)\n", name);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700190}