xref: /linux/drivers/video/backlight/locomolcd.c (revision 9905a43b2d563e6f89e4c63c4278ada03f2ebb14)
1987132bbSPavel Machek /*
2987132bbSPavel Machek  * Backlight control code for Sharp Zaurus SL-5500
3987132bbSPavel Machek  *
4987132bbSPavel Machek  * Copyright 2005 John Lenz <lenz@cs.wisc.edu>
5987132bbSPavel Machek  * Maintainer: Pavel Machek <pavel@suse.cz> (unless John wants to :-)
6987132bbSPavel Machek  * GPL v2
7987132bbSPavel Machek  *
8987132bbSPavel Machek  * This driver assumes single CPU. That's okay, because collie is
9987132bbSPavel Machek  * slightly old hardware, and noone is going to retrofit second CPU to
10987132bbSPavel Machek  * old PDA.
11987132bbSPavel Machek  */
12987132bbSPavel Machek 
13987132bbSPavel Machek /* LCD power functions */
14987132bbSPavel Machek #include <linux/module.h>
15987132bbSPavel Machek #include <linux/init.h>
16987132bbSPavel Machek #include <linux/delay.h>
17987132bbSPavel Machek #include <linux/device.h>
18987132bbSPavel Machek #include <linux/interrupt.h>
19e4423781SRichard Purdie #include <linux/fb.h>
20e4423781SRichard Purdie #include <linux/backlight.h>
21987132bbSPavel Machek 
22987132bbSPavel Machek #include <asm/hardware/locomo.h>
23987132bbSPavel Machek #include <asm/irq.h>
2436a95bf5SPavel Machek #include <asm/mach/sharpsl_param.h>
2536a95bf5SPavel Machek #include <asm/mach-types.h>
26987132bbSPavel Machek 
2736a95bf5SPavel Machek #include "../../../arch/arm/mach-sa1100/generic.h"
28987132bbSPavel Machek 
29e4423781SRichard Purdie static struct backlight_device *locomolcd_bl_device;
30987132bbSPavel Machek static struct locomo_dev *locomolcd_dev;
31e4423781SRichard Purdie static unsigned long locomolcd_flags;
32e4423781SRichard Purdie #define LOCOMOLCD_SUSPENDED     0x01
33987132bbSPavel Machek 
34987132bbSPavel Machek static void locomolcd_on(int comadj)
35987132bbSPavel Machek {
368d48427eSRichard Purdie 	locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 0);
378d48427eSRichard Purdie 	locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 1);
38987132bbSPavel Machek 	mdelay(2);
39987132bbSPavel Machek 
408d48427eSRichard Purdie 	locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 0);
418d48427eSRichard Purdie 	locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 1);
42987132bbSPavel Machek 	mdelay(2);
43987132bbSPavel Machek 
44987132bbSPavel Machek 	locomo_m62332_senddata(locomolcd_dev, comadj, 0);
45987132bbSPavel Machek 	mdelay(5);
46987132bbSPavel Machek 
478d48427eSRichard Purdie 	locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 0);
488d48427eSRichard Purdie 	locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 1);
49987132bbSPavel Machek 	mdelay(10);
50987132bbSPavel Machek 
51987132bbSPavel Machek 	/* TFTCRST | CPSOUT=0 | CPSEN */
52987132bbSPavel Machek 	locomo_writel(0x01, locomolcd_dev->mapbase + LOCOMO_TC);
53987132bbSPavel Machek 
54987132bbSPavel Machek 	/* Set CPSD */
55987132bbSPavel Machek 	locomo_writel(6, locomolcd_dev->mapbase + LOCOMO_CPSD);
56987132bbSPavel Machek 
57987132bbSPavel Machek 	/* TFTCRST | CPSOUT=0 | CPSEN */
58987132bbSPavel Machek 	locomo_writel((0x04 | 0x01), locomolcd_dev->mapbase + LOCOMO_TC);
59987132bbSPavel Machek 	mdelay(10);
60987132bbSPavel Machek 
618d48427eSRichard Purdie 	locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 0);
628d48427eSRichard Purdie 	locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 1);
63987132bbSPavel Machek }
64987132bbSPavel Machek 
65987132bbSPavel Machek static void locomolcd_off(int comadj)
66987132bbSPavel Machek {
67987132bbSPavel Machek 	/* TFTCRST=1 | CPSOUT=1 | CPSEN = 0 */
68987132bbSPavel Machek 	locomo_writel(0x06, locomolcd_dev->mapbase + LOCOMO_TC);
69987132bbSPavel Machek 	mdelay(1);
70987132bbSPavel Machek 
718d48427eSRichard Purdie 	locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 0);
72987132bbSPavel Machek 	mdelay(110);
73987132bbSPavel Machek 
748d48427eSRichard Purdie 	locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 0);
75987132bbSPavel Machek 	mdelay(700);
76987132bbSPavel Machek 
77987132bbSPavel Machek 	/* TFTCRST=0 | CPSOUT=0 | CPSEN = 0 */
78987132bbSPavel Machek 	locomo_writel(0, locomolcd_dev->mapbase + LOCOMO_TC);
798d48427eSRichard Purdie 	locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 0);
808d48427eSRichard Purdie 	locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 0);
81987132bbSPavel Machek }
82987132bbSPavel Machek 
83987132bbSPavel Machek void locomolcd_power(int on)
84987132bbSPavel Machek {
8536a95bf5SPavel Machek 	int comadj = sharpsl_param.comadj;
86987132bbSPavel Machek 	unsigned long flags;
87987132bbSPavel Machek 
88987132bbSPavel Machek 	local_irq_save(flags);
89987132bbSPavel Machek 
90987132bbSPavel Machek 	if (!locomolcd_dev) {
91987132bbSPavel Machek 		local_irq_restore(flags);
92987132bbSPavel Machek 		return;
93987132bbSPavel Machek 	}
94987132bbSPavel Machek 
95987132bbSPavel Machek 	/* read comadj */
96e4423781SRichard Purdie 	if (comadj == -1 && machine_is_collie())
97987132bbSPavel Machek 		comadj = 128;
98e4423781SRichard Purdie 	if (comadj == -1 && machine_is_poodle())
99e4423781SRichard Purdie 		comadj = 118;
100987132bbSPavel Machek 
101987132bbSPavel Machek 	if (on)
102987132bbSPavel Machek 		locomolcd_on(comadj);
103987132bbSPavel Machek 	else
104987132bbSPavel Machek 		locomolcd_off(comadj);
105987132bbSPavel Machek 
106987132bbSPavel Machek 	local_irq_restore(flags);
107987132bbSPavel Machek }
108987132bbSPavel Machek EXPORT_SYMBOL(locomolcd_power);
109987132bbSPavel Machek 
110e4423781SRichard Purdie 
111e4423781SRichard Purdie static int current_intensity;
112e4423781SRichard Purdie 
113e4423781SRichard Purdie static int locomolcd_set_intensity(struct backlight_device *bd)
114e4423781SRichard Purdie {
115599a52d1SRichard Purdie 	int intensity = bd->props.brightness;
116e4423781SRichard Purdie 
117599a52d1SRichard Purdie 	if (bd->props.power != FB_BLANK_UNBLANK)
118e4423781SRichard Purdie 		intensity = 0;
119599a52d1SRichard Purdie 	if (bd->props.fb_blank != FB_BLANK_UNBLANK)
120e4423781SRichard Purdie 		intensity = 0;
121e4423781SRichard Purdie 	if (locomolcd_flags & LOCOMOLCD_SUSPENDED)
122e4423781SRichard Purdie 		intensity = 0;
123e4423781SRichard Purdie 
124e4423781SRichard Purdie 	switch (intensity) {
125e4423781SRichard Purdie 	/* AC and non-AC are handled differently, but produce same results in sharp code? */
126e4423781SRichard Purdie 	case 0: locomo_frontlight_set(locomolcd_dev, 0, 0, 161); break;
127e4423781SRichard Purdie 	case 1: locomo_frontlight_set(locomolcd_dev, 117, 0, 161); break;
128e4423781SRichard Purdie 	case 2: locomo_frontlight_set(locomolcd_dev, 163, 0, 148); break;
129e4423781SRichard Purdie 	case 3: locomo_frontlight_set(locomolcd_dev, 194, 0, 161); break;
130e4423781SRichard Purdie 	case 4: locomo_frontlight_set(locomolcd_dev, 194, 1, 161); break;
131e4423781SRichard Purdie 
132e4423781SRichard Purdie 	default:
133e4423781SRichard Purdie 		return -ENODEV;
134e4423781SRichard Purdie 	}
135e4423781SRichard Purdie 	current_intensity = intensity;
136e4423781SRichard Purdie 	return 0;
137e4423781SRichard Purdie }
138e4423781SRichard Purdie 
139e4423781SRichard Purdie static int locomolcd_get_intensity(struct backlight_device *bd)
140e4423781SRichard Purdie {
141e4423781SRichard Purdie 	return current_intensity;
142e4423781SRichard Purdie }
143e4423781SRichard Purdie 
144*9905a43bSEmese Revfy static const struct backlight_ops locomobl_data = {
145e4423781SRichard Purdie 	.get_brightness = locomolcd_get_intensity,
146e4423781SRichard Purdie 	.update_status  = locomolcd_set_intensity,
147e4423781SRichard Purdie };
148e4423781SRichard Purdie 
149e4423781SRichard Purdie #ifdef CONFIG_PM
150e4423781SRichard Purdie static int locomolcd_suspend(struct locomo_dev *dev, pm_message_t state)
151e4423781SRichard Purdie {
152e4423781SRichard Purdie 	locomolcd_flags |= LOCOMOLCD_SUSPENDED;
153e4423781SRichard Purdie 	locomolcd_set_intensity(locomolcd_bl_device);
154e4423781SRichard Purdie 	return 0;
155e4423781SRichard Purdie }
156e4423781SRichard Purdie 
157e4423781SRichard Purdie static int locomolcd_resume(struct locomo_dev *dev)
158e4423781SRichard Purdie {
159e4423781SRichard Purdie 	locomolcd_flags &= ~LOCOMOLCD_SUSPENDED;
160e4423781SRichard Purdie 	locomolcd_set_intensity(locomolcd_bl_device);
161e4423781SRichard Purdie 	return 0;
162e4423781SRichard Purdie }
163e4423781SRichard Purdie #else
164e4423781SRichard Purdie #define locomolcd_suspend	NULL
165e4423781SRichard Purdie #define locomolcd_resume	NULL
166e4423781SRichard Purdie #endif
167e4423781SRichard Purdie 
1688d48427eSRichard Purdie static int locomolcd_probe(struct locomo_dev *ldev)
169987132bbSPavel Machek {
170987132bbSPavel Machek 	unsigned long flags;
171987132bbSPavel Machek 
172987132bbSPavel Machek 	local_irq_save(flags);
1738d48427eSRichard Purdie 	locomolcd_dev = ldev;
174987132bbSPavel Machek 
1758d48427eSRichard Purdie 	locomo_gpio_set_dir(ldev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
176e4423781SRichard Purdie 
177987132bbSPavel Machek 	/* the poodle_lcd_power function is called for the first time
178987132bbSPavel Machek 	 * from fs_initcall, which is before locomo is activated.
179987132bbSPavel Machek 	 * We need to recall poodle_lcd_power here*/
180e4423781SRichard Purdie 	if (machine_is_poodle())
181987132bbSPavel Machek 		locomolcd_power(1);
182e4423781SRichard Purdie 
183987132bbSPavel Machek 	local_irq_restore(flags);
184e4423781SRichard Purdie 
18527c5d745SRichard Purdie 	locomolcd_bl_device = backlight_device_register("locomo-bl", &ldev->dev, NULL, &locomobl_data);
186e4423781SRichard Purdie 
187e4423781SRichard Purdie 	if (IS_ERR (locomolcd_bl_device))
188e4423781SRichard Purdie 		return PTR_ERR (locomolcd_bl_device);
189e4423781SRichard Purdie 
190e4423781SRichard Purdie 	/* Set up frontlight so that screen is readable */
191599a52d1SRichard Purdie 	locomolcd_bl_device->props.max_brightness = 4,
192599a52d1SRichard Purdie 	locomolcd_bl_device->props.brightness = 2;
193e4423781SRichard Purdie 	locomolcd_set_intensity(locomolcd_bl_device);
194e4423781SRichard Purdie 
195987132bbSPavel Machek 	return 0;
196987132bbSPavel Machek }
197987132bbSPavel Machek 
198e4423781SRichard Purdie static int locomolcd_remove(struct locomo_dev *dev)
199987132bbSPavel Machek {
200987132bbSPavel Machek 	unsigned long flags;
201e4423781SRichard Purdie 
2022a391a18SRichard Purdie 	locomolcd_bl_device->props.brightness = 0;
2032a391a18SRichard Purdie 	locomolcd_bl_device->props.power = 0;
2044437cd1eSHenrique de Moraes Holschuh 	locomolcd_set_intensity(locomolcd_bl_device);
2054437cd1eSHenrique de Moraes Holschuh 
206e4423781SRichard Purdie 	backlight_device_unregister(locomolcd_bl_device);
207987132bbSPavel Machek 	local_irq_save(flags);
208987132bbSPavel Machek 	locomolcd_dev = NULL;
209987132bbSPavel Machek 	local_irq_restore(flags);
210987132bbSPavel Machek 	return 0;
211987132bbSPavel Machek }
212987132bbSPavel Machek 
213987132bbSPavel Machek static struct locomo_driver poodle_lcd_driver = {
214987132bbSPavel Machek 	.drv = {
215987132bbSPavel Machek 		.name = "locomo-backlight",
216987132bbSPavel Machek 	},
217987132bbSPavel Machek 	.devid	= LOCOMO_DEVID_BACKLIGHT,
218e4423781SRichard Purdie 	.probe	= locomolcd_probe,
219e4423781SRichard Purdie 	.remove	= locomolcd_remove,
220e4423781SRichard Purdie 	.suspend = locomolcd_suspend,
221e4423781SRichard Purdie 	.resume = locomolcd_resume,
222987132bbSPavel Machek };
223987132bbSPavel Machek 
224e4423781SRichard Purdie 
225e4423781SRichard Purdie static int __init locomolcd_init(void)
226987132bbSPavel Machek {
227987132bbSPavel Machek 	int ret = locomo_driver_register(&poodle_lcd_driver);
228e4423781SRichard Purdie 	if (ret)
229e4423781SRichard Purdie 		return ret;
230987132bbSPavel Machek 
231987132bbSPavel Machek #ifdef CONFIG_SA1100_COLLIE
232987132bbSPavel Machek 	sa1100fb_lcd_power = locomolcd_power;
233987132bbSPavel Machek #endif
234987132bbSPavel Machek 	return 0;
235987132bbSPavel Machek }
236987132bbSPavel Machek 
237e4423781SRichard Purdie static void __exit locomolcd_exit(void)
238e4423781SRichard Purdie {
239e4423781SRichard Purdie 	locomo_driver_unregister(&poodle_lcd_driver);
240e4423781SRichard Purdie }
241e4423781SRichard Purdie 
242e4423781SRichard Purdie module_init(locomolcd_init);
243e4423781SRichard Purdie module_exit(locomolcd_exit);
244e4423781SRichard Purdie 
245e4423781SRichard Purdie MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>, Pavel Machek <pavel@suse.cz>");
246e4423781SRichard Purdie MODULE_DESCRIPTION("Collie LCD driver");
247e4423781SRichard Purdie MODULE_LICENSE("GPL");
248