Linux Audio

Check our new training course

Loading...
v3.1
 
  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
 18static const struct backlight_ops pmu_backlight_data;
 19static DEFINE_SPINLOCK(pmu_backlight_lock);
 20static int sleeping, uses_pmu_bl;
 21static u8 bl_curve[FB_BACKLIGHT_LEVELS];
 22
 23static void pmu_backlight_init_curve(u8 off, u8 min, u8 max)
 24{
 25	int i, flat, count, range = (max - min);
 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
 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)
 58{
 59	int pmulevel;
 60
 61	/* Get and convert the value */
 62	pmulevel = bl_curve[level] * FB_BACKLIGHT_MAX / MAX_PMU_LEVEL;
 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
 71static int __pmu_backlight_update_status(struct backlight_device *bd)
 72{
 73	struct adb_request req;
 74	int level = bd->props.brightness;
 75
 76
 77	if (bd->props.power != FB_BLANK_UNBLANK ||
 78	    bd->props.fb_blank != FB_BLANK_UNBLANK)
 79		level = 0;
 80
 81	if (level > 0) {
 82		int pmulevel = pmu_backlight_get_level_brightness(level);
 83
 84		pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel);
 85		pmu_wait_complete(&req);
 86
 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
 96	return 0;
 97}
 98
 99static 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
113static int pmu_backlight_get_brightness(struct backlight_device *bd)
114{
115	return bd->props.brightness;
116}
117
118static const struct backlight_ops pmu_backlight_data = {
119	.get_brightness	= pmu_backlight_get_brightness,
120	.update_status	= pmu_backlight_update_status,
121
122};
123
124#ifdef CONFIG_PM
125void pmu_backlight_set_sleep(int sleep)
126{
127	unsigned long flags;
128
129	spin_lock_irqsave(&pmu_backlight_lock, flags);
130	sleeping = sleep;
131	if (pmac_backlight && uses_pmu_bl) {
132		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	}
141	spin_unlock_irqrestore(&pmu_backlight_lock, flags);
142}
143#endif /* CONFIG_PM */
144
145void __init pmu_backlight_init()
146{
147	struct backlight_properties props;
148	struct backlight_device *bd;
149	char name[10];
150	int level, autosave;
151
152	/* Special case for the old PowerBook since I can't test on it */
153	autosave =
154		of_machine_is_compatible("AAPL,3400/2400") ||
155		of_machine_is_compatible("AAPL,3500");
156
157	if (!autosave &&
158	    !pmac_has_backlight_type("pmu") &&
159	    !of_machine_is_compatible("AAPL,PowerBook1998") &&
160	    !of_machine_is_compatible("PowerBook1,1"))
161		return;
162
163	snprintf(name, sizeof(name), "pmubl");
164
165	memset(&props, 0, sizeof(struct backlight_properties));
166	props.type = BACKLIGHT_PLATFORM;
167	props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
168	bd = backlight_device_register(name, NULL, NULL, &pmu_backlight_data,
169				       &props);
170	if (IS_ERR(bd)) {
171		printk(KERN_ERR "PMU Backlight registration failed\n");
172		return;
173	}
174	uses_pmu_bl = 1;
175	pmu_backlight_init_curve(0x7F, 0x46, 0x0E);
176
177	level = bd->props.max_brightness;
178
179	if (autosave) {
180		/* read autosaved value if available */
181		struct adb_request req;
182		pmu_request(&req, NULL, 2, 0xd9, 0);
183		pmu_wait_complete(&req);
184
185		level = pmu_backlight_curve_lookup(
186				(req.reply[0] >> 4) *
187				bd->props.max_brightness / 15);
188	}
189
190	bd->props.brightness = level;
191	bd->props.power = FB_BLANK_UNBLANK;
192	backlight_update_status(bd);
193
194	printk(KERN_INFO "PMU Backlight initialized (%s)\n", name);
195}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  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/backlight.h>
 14#include <linux/pmu.h>
 15#include <asm/backlight.h>
 
 16
 17#define MAX_PMU_LEVEL 0xFF
 18
 19static const struct backlight_ops pmu_backlight_data;
 20static DEFINE_SPINLOCK(pmu_backlight_lock);
 21static int sleeping, uses_pmu_bl;
 22static u8 bl_curve[FB_BACKLIGHT_LEVELS];
 23
 24static void pmu_backlight_init_curve(u8 off, u8 min, u8 max)
 25{
 26	int i, flat, count, range = (max - min);
 27
 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)
 59{
 60	int pmulevel;
 61
 62	/* Get and convert the value */
 63	pmulevel = bl_curve[level] * FB_BACKLIGHT_MAX / MAX_PMU_LEVEL;
 64	if (pmulevel < 0)
 65		pmulevel = 0;
 66	else if (pmulevel > MAX_PMU_LEVEL)
 67		pmulevel = MAX_PMU_LEVEL;
 68
 69	return pmulevel;
 70}
 71
 72static int __pmu_backlight_update_status(struct backlight_device *bd)
 73{
 74	struct adb_request req;
 75	int level = backlight_get_brightness(bd);
 
 
 
 
 
 76
 77	if (level > 0) {
 78		int pmulevel = pmu_backlight_get_level_brightness(level);
 79
 80		pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel);
 81		pmu_wait_complete(&req);
 82
 83		pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
 84			PMU_POW_BACKLIGHT | PMU_POW_ON);
 85		pmu_wait_complete(&req);
 86	} else {
 87		pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
 88			PMU_POW_BACKLIGHT | PMU_POW_OFF);
 89		pmu_wait_complete(&req);
 90	}
 91
 92	return 0;
 93}
 94
 95static int pmu_backlight_update_status(struct backlight_device *bd)
 96{
 97	unsigned long flags;
 98	int rc = 0;
 99
100	spin_lock_irqsave(&pmu_backlight_lock, flags);
101	/* Don't update brightness when sleeping */
102	if (!sleeping)
103		rc = __pmu_backlight_update_status(bd);
104	spin_unlock_irqrestore(&pmu_backlight_lock, flags);
105	return rc;
106}
107
108
 
 
 
 
 
109static const struct backlight_ops pmu_backlight_data = {
 
110	.update_status	= pmu_backlight_update_status,
111
112};
113
114#ifdef CONFIG_PM
115void pmu_backlight_set_sleep(int sleep)
116{
117	unsigned long flags;
118
119	spin_lock_irqsave(&pmu_backlight_lock, flags);
120	sleeping = sleep;
121	if (pmac_backlight && uses_pmu_bl) {
122		if (sleep) {
123			struct adb_request req;
124
125			pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
126				    PMU_POW_BACKLIGHT | PMU_POW_OFF);
127			pmu_wait_complete(&req);
128		} else
129			__pmu_backlight_update_status(pmac_backlight);
130	}
131	spin_unlock_irqrestore(&pmu_backlight_lock, flags);
132}
133#endif /* CONFIG_PM */
134
135void __init pmu_backlight_init(void)
136{
137	struct backlight_properties props;
138	struct backlight_device *bd;
139	char name[10];
140	int level, autosave;
141
142	/* Special case for the old PowerBook since I can't test on it */
143	autosave =
144		of_machine_is_compatible("AAPL,3400/2400") ||
145		of_machine_is_compatible("AAPL,3500");
146
147	if (!autosave &&
148	    !pmac_has_backlight_type("pmu") &&
149	    !of_machine_is_compatible("AAPL,PowerBook1998") &&
150	    !of_machine_is_compatible("PowerBook1,1"))
151		return;
152
153	snprintf(name, sizeof(name), "pmubl");
154
155	memset(&props, 0, sizeof(struct backlight_properties));
156	props.type = BACKLIGHT_PLATFORM;
157	props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
158	bd = backlight_device_register(name, NULL, NULL, &pmu_backlight_data,
159				       &props);
160	if (IS_ERR(bd)) {
161		printk(KERN_ERR "PMU Backlight registration failed\n");
162		return;
163	}
164	uses_pmu_bl = 1;
165	pmu_backlight_init_curve(0x7F, 0x46, 0x0E);
166
167	level = bd->props.max_brightness;
168
169	if (autosave) {
170		/* read autosaved value if available */
171		struct adb_request req;
172		pmu_request(&req, NULL, 2, 0xd9, 0);
173		pmu_wait_complete(&req);
174
175		level = pmu_backlight_curve_lookup(
176				(req.reply[0] >> 4) *
177				bd->props.max_brightness / 15);
178	}
179
180	bd->props.brightness = level;
181	bd->props.power = BACKLIGHT_POWER_ON;
182	backlight_update_status(bd);
183
184	printk(KERN_INFO "PMU Backlight initialized (%s)\n", name);
185}