1 /*
2  *  Backlight Driver for HP Jornada 680
3  *
4  *  Copyright (c) 2005 Andriy Skulysh
5  *
6  *  Based on Sharp's Corgi Backlight Driver
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License.  See the file "COPYING" in the main directory of this archive
10  * for more details.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/spinlock.h>
18 #include <linux/fb.h>
19 #include <linux/backlight.h>
20 
21 #include <cpu/dac.h>
22 #include <mach/hp6xx.h>
23 #include <asm/hd64461.h>
24 
25 #define HP680_MAX_INTENSITY 255
26 #define HP680_DEFAULT_INTENSITY 10
27 
28 static int hp680bl_suspended;
29 static int current_intensity;
30 static DEFINE_SPINLOCK(bl_lock);
31 
32 static void hp680bl_send_intensity(struct backlight_device *bd)
33 {
34 	unsigned long flags;
35 	u16 v;
36 	int intensity = backlight_get_brightness(bd);
37 
38 	if (hp680bl_suspended)
39 		intensity = 0;
40 
41 	spin_lock_irqsave(&bl_lock, flags);
42 	if (intensity && current_intensity == 0) {
43 		sh_dac_enable(DAC_LCD_BRIGHTNESS);
44 		v = inw(HD64461_GPBDR);
45 		v &= ~HD64461_GPBDR_LCDOFF;
46 		outw(v, HD64461_GPBDR);
47 		sh_dac_output(255-(u8)intensity, DAC_LCD_BRIGHTNESS);
48 	} else if (intensity == 0 && current_intensity != 0) {
49 		sh_dac_output(255-(u8)intensity, DAC_LCD_BRIGHTNESS);
50 		sh_dac_disable(DAC_LCD_BRIGHTNESS);
51 		v = inw(HD64461_GPBDR);
52 		v |= HD64461_GPBDR_LCDOFF;
53 		outw(v, HD64461_GPBDR);
54 	} else if (intensity) {
55 		sh_dac_output(255-(u8)intensity, DAC_LCD_BRIGHTNESS);
56 	}
57 	spin_unlock_irqrestore(&bl_lock, flags);
58 
59 	current_intensity = intensity;
60 }
61 
62 
63 #ifdef CONFIG_PM_SLEEP
64 static int hp680bl_suspend(struct device *dev)
65 {
66 	struct backlight_device *bd = dev_get_drvdata(dev);
67 
68 	hp680bl_suspended = 1;
69 	hp680bl_send_intensity(bd);
70 	return 0;
71 }
72 
73 static int hp680bl_resume(struct device *dev)
74 {
75 	struct backlight_device *bd = dev_get_drvdata(dev);
76 
77 	hp680bl_suspended = 0;
78 	hp680bl_send_intensity(bd);
79 	return 0;
80 }
81 #endif
82 
83 static SIMPLE_DEV_PM_OPS(hp680bl_pm_ops, hp680bl_suspend, hp680bl_resume);
84 
85 static int hp680bl_set_intensity(struct backlight_device *bd)
86 {
87 	hp680bl_send_intensity(bd);
88 	return 0;
89 }
90 
91 static int hp680bl_get_intensity(struct backlight_device *bd)
92 {
93 	return current_intensity;
94 }
95 
96 static const struct backlight_ops hp680bl_ops = {
97 	.get_brightness = hp680bl_get_intensity,
98 	.update_status  = hp680bl_set_intensity,
99 };
100 
101 static int hp680bl_probe(struct platform_device *pdev)
102 {
103 	struct backlight_properties props;
104 	struct backlight_device *bd;
105 
106 	memset(&props, 0, sizeof(struct backlight_properties));
107 	props.type = BACKLIGHT_RAW;
108 	props.max_brightness = HP680_MAX_INTENSITY;
109 	bd = devm_backlight_device_register(&pdev->dev, "hp680-bl", &pdev->dev,
110 					NULL, &hp680bl_ops, &props);
111 	if (IS_ERR(bd))
112 		return PTR_ERR(bd);
113 
114 	platform_set_drvdata(pdev, bd);
115 
116 	bd->props.brightness = HP680_DEFAULT_INTENSITY;
117 	hp680bl_send_intensity(bd);
118 
119 	return 0;
120 }
121 
122 static int hp680bl_remove(struct platform_device *pdev)
123 {
124 	struct backlight_device *bd = platform_get_drvdata(pdev);
125 
126 	bd->props.brightness = 0;
127 	bd->props.power = 0;
128 	hp680bl_send_intensity(bd);
129 
130 	return 0;
131 }
132 
133 static struct platform_driver hp680bl_driver = {
134 	.probe		= hp680bl_probe,
135 	.remove		= hp680bl_remove,
136 	.driver		= {
137 		.name	= "hp680-bl",
138 		.pm	= &hp680bl_pm_ops,
139 	},
140 };
141 
142 static struct platform_device *hp680bl_device;
143 
144 static int __init hp680bl_init(void)
145 {
146 	int ret;
147 
148 	ret = platform_driver_register(&hp680bl_driver);
149 	if (ret)
150 		return ret;
151 	hp680bl_device = platform_device_register_simple("hp680-bl", -1,
152 							NULL, 0);
153 	if (IS_ERR(hp680bl_device)) {
154 		platform_driver_unregister(&hp680bl_driver);
155 		return PTR_ERR(hp680bl_device);
156 	}
157 	return 0;
158 }
159 
160 static void __exit hp680bl_exit(void)
161 {
162 	platform_device_unregister(hp680bl_device);
163 	platform_driver_unregister(&hp680bl_driver);
164 }
165 
166 module_init(hp680bl_init);
167 module_exit(hp680bl_exit);
168 
169 MODULE_AUTHOR("Andriy Skulysh <askulysh@gmail.com>");
170 MODULE_DESCRIPTION("HP Jornada 680 Backlight Driver");
171 MODULE_LICENSE("GPL");
172