blob: be20b5cbe26cb9e1dd244a284f898727ffa5c5ea [file] [log] [blame]
Pavel Machek987132b2005-09-06 15:16:45 -07001/*
2 * Backlight control code for Sharp Zaurus SL-5500
3 *
4 * Copyright 2005 John Lenz <lenz@cs.wisc.edu>
Pavel Macheka2531292010-07-18 14:27:13 +02005 * Maintainer: Pavel Machek <pavel@ucw.cz> (unless John wants to :-)
Pavel Machek987132b2005-09-06 15:16:45 -07006 * GPL v2
7 *
8 * This driver assumes single CPU. That's okay, because collie is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009 * slightly old hardware, and no one is going to retrofit second CPU to
Pavel Machek987132b2005-09-06 15:16:45 -070010 * old PDA.
11 */
12
13/* LCD power functions */
Pavel Machek987132b2005-09-06 15:16:45 -070014#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/device.h>
18#include <linux/interrupt.h>
Richard Purdiee4423782006-06-26 00:26:27 -070019#include <linux/fb.h>
20#include <linux/backlight.h>
Pavel Machek987132b2005-09-06 15:16:45 -070021
22#include <asm/hardware/locomo.h>
23#include <asm/irq.h>
Pavel Machek36a95bf2006-03-25 03:07:33 -080024#include <asm/mach/sharpsl_param.h>
25#include <asm/mach-types.h>
Pavel Machek987132b2005-09-06 15:16:45 -070026
Pavel Machek36a95bf2006-03-25 03:07:33 -080027#include "../../../arch/arm/mach-sa1100/generic.h"
Pavel Machek987132b2005-09-06 15:16:45 -070028
Richard Purdiee4423782006-06-26 00:26:27 -070029static struct backlight_device *locomolcd_bl_device;
Pavel Machek987132b2005-09-06 15:16:45 -070030static struct locomo_dev *locomolcd_dev;
Richard Purdiee4423782006-06-26 00:26:27 -070031static unsigned long locomolcd_flags;
32#define LOCOMOLCD_SUSPENDED 0x01
Pavel Machek987132b2005-09-06 15:16:45 -070033
34static void locomolcd_on(int comadj)
35{
Richard Purdie8d484272006-09-25 20:11:48 +010036 locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 0);
37 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 1);
Pavel Machek987132b2005-09-06 15:16:45 -070038 mdelay(2);
39
Richard Purdie8d484272006-09-25 20:11:48 +010040 locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 0);
41 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 1);
Pavel Machek987132b2005-09-06 15:16:45 -070042 mdelay(2);
43
44 locomo_m62332_senddata(locomolcd_dev, comadj, 0);
45 mdelay(5);
46
Richard Purdie8d484272006-09-25 20:11:48 +010047 locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 0);
48 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 1);
Pavel Machek987132b2005-09-06 15:16:45 -070049 mdelay(10);
50
51 /* TFTCRST | CPSOUT=0 | CPSEN */
52 locomo_writel(0x01, locomolcd_dev->mapbase + LOCOMO_TC);
53
54 /* Set CPSD */
55 locomo_writel(6, locomolcd_dev->mapbase + LOCOMO_CPSD);
56
57 /* TFTCRST | CPSOUT=0 | CPSEN */
58 locomo_writel((0x04 | 0x01), locomolcd_dev->mapbase + LOCOMO_TC);
59 mdelay(10);
60
Richard Purdie8d484272006-09-25 20:11:48 +010061 locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 0);
62 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 1);
Pavel Machek987132b2005-09-06 15:16:45 -070063}
64
65static void locomolcd_off(int comadj)
66{
67 /* TFTCRST=1 | CPSOUT=1 | CPSEN = 0 */
68 locomo_writel(0x06, locomolcd_dev->mapbase + LOCOMO_TC);
69 mdelay(1);
70
Richard Purdie8d484272006-09-25 20:11:48 +010071 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 0);
Pavel Machek987132b2005-09-06 15:16:45 -070072 mdelay(110);
73
Richard Purdie8d484272006-09-25 20:11:48 +010074 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 0);
Pavel Machek987132b2005-09-06 15:16:45 -070075 mdelay(700);
76
77 /* TFTCRST=0 | CPSOUT=0 | CPSEN = 0 */
78 locomo_writel(0, locomolcd_dev->mapbase + LOCOMO_TC);
Richard Purdie8d484272006-09-25 20:11:48 +010079 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 0);
80 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 0);
Pavel Machek987132b2005-09-06 15:16:45 -070081}
82
83void locomolcd_power(int on)
84{
Pavel Machek36a95bf2006-03-25 03:07:33 -080085 int comadj = sharpsl_param.comadj;
Pavel Machek987132b2005-09-06 15:16:45 -070086 unsigned long flags;
87
88 local_irq_save(flags);
89
90 if (!locomolcd_dev) {
91 local_irq_restore(flags);
92 return;
93 }
94
95 /* read comadj */
Richard Purdiee4423782006-06-26 00:26:27 -070096 if (comadj == -1 && machine_is_collie())
97 comadj = 128;
98 if (comadj == -1 && machine_is_poodle())
99 comadj = 118;
Pavel Machek987132b2005-09-06 15:16:45 -0700100
101 if (on)
102 locomolcd_on(comadj);
103 else
104 locomolcd_off(comadj);
105
106 local_irq_restore(flags);
107}
108EXPORT_SYMBOL(locomolcd_power);
109
Richard Purdiee4423782006-06-26 00:26:27 -0700110
111static int current_intensity;
112
113static int locomolcd_set_intensity(struct backlight_device *bd)
114{
Richard Purdie599a52d2007-02-10 23:07:48 +0000115 int intensity = bd->props.brightness;
Richard Purdiee4423782006-06-26 00:26:27 -0700116
Richard Purdie599a52d2007-02-10 23:07:48 +0000117 if (bd->props.power != FB_BLANK_UNBLANK)
Richard Purdiee4423782006-06-26 00:26:27 -0700118 intensity = 0;
Richard Purdie599a52d2007-02-10 23:07:48 +0000119 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
Richard Purdiee4423782006-06-26 00:26:27 -0700120 intensity = 0;
121 if (locomolcd_flags & LOCOMOLCD_SUSPENDED)
122 intensity = 0;
123
124 switch (intensity) {
125 /* AC and non-AC are handled differently, but produce same results in sharp code? */
126 case 0: locomo_frontlight_set(locomolcd_dev, 0, 0, 161); break;
127 case 1: locomo_frontlight_set(locomolcd_dev, 117, 0, 161); break;
128 case 2: locomo_frontlight_set(locomolcd_dev, 163, 0, 148); break;
129 case 3: locomo_frontlight_set(locomolcd_dev, 194, 0, 161); break;
130 case 4: locomo_frontlight_set(locomolcd_dev, 194, 1, 161); break;
131
132 default:
133 return -ENODEV;
134 }
135 current_intensity = intensity;
136 return 0;
137}
138
139static int locomolcd_get_intensity(struct backlight_device *bd)
140{
141 return current_intensity;
142}
143
Emese Revfy9905a432009-12-14 00:58:57 +0100144static const struct backlight_ops locomobl_data = {
Richard Purdiee4423782006-06-26 00:26:27 -0700145 .get_brightness = locomolcd_get_intensity,
146 .update_status = locomolcd_set_intensity,
Richard Purdiee4423782006-06-26 00:26:27 -0700147};
148
149#ifdef CONFIG_PM
150static int locomolcd_suspend(struct locomo_dev *dev, pm_message_t state)
151{
152 locomolcd_flags |= LOCOMOLCD_SUSPENDED;
153 locomolcd_set_intensity(locomolcd_bl_device);
154 return 0;
155}
156
157static int locomolcd_resume(struct locomo_dev *dev)
158{
159 locomolcd_flags &= ~LOCOMOLCD_SUSPENDED;
160 locomolcd_set_intensity(locomolcd_bl_device);
161 return 0;
162}
163#else
164#define locomolcd_suspend NULL
165#define locomolcd_resume NULL
166#endif
167
Richard Purdie8d484272006-09-25 20:11:48 +0100168static int locomolcd_probe(struct locomo_dev *ldev)
Pavel Machek987132b2005-09-06 15:16:45 -0700169{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500170 struct backlight_properties props;
Pavel Machek987132b2005-09-06 15:16:45 -0700171 unsigned long flags;
172
173 local_irq_save(flags);
Richard Purdie8d484272006-09-25 20:11:48 +0100174 locomolcd_dev = ldev;
Pavel Machek987132b2005-09-06 15:16:45 -0700175
Richard Purdie8d484272006-09-25 20:11:48 +0100176 locomo_gpio_set_dir(ldev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
Richard Purdiee4423782006-06-26 00:26:27 -0700177
Pavel Machek987132b2005-09-06 15:16:45 -0700178 /* the poodle_lcd_power function is called for the first time
179 * from fs_initcall, which is before locomo is activated.
180 * We need to recall poodle_lcd_power here*/
Richard Purdiee4423782006-06-26 00:26:27 -0700181 if (machine_is_poodle())
182 locomolcd_power(1);
183
Pavel Machek987132b2005-09-06 15:16:45 -0700184 local_irq_restore(flags);
Richard Purdiee4423782006-06-26 00:26:27 -0700185
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500186 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700187 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500188 props.max_brightness = 4;
189 locomolcd_bl_device = backlight_device_register("locomo-bl",
190 &ldev->dev, NULL,
191 &locomobl_data, &props);
Richard Purdiee4423782006-06-26 00:26:27 -0700192
193 if (IS_ERR (locomolcd_bl_device))
194 return PTR_ERR (locomolcd_bl_device);
195
196 /* Set up frontlight so that screen is readable */
Richard Purdie599a52d2007-02-10 23:07:48 +0000197 locomolcd_bl_device->props.brightness = 2;
Richard Purdiee4423782006-06-26 00:26:27 -0700198 locomolcd_set_intensity(locomolcd_bl_device);
199
Pavel Machek987132b2005-09-06 15:16:45 -0700200 return 0;
201}
202
Richard Purdiee4423782006-06-26 00:26:27 -0700203static int locomolcd_remove(struct locomo_dev *dev)
Pavel Machek987132b2005-09-06 15:16:45 -0700204{
205 unsigned long flags;
Richard Purdiee4423782006-06-26 00:26:27 -0700206
Richard Purdie2a391a12007-03-07 00:29:33 +0100207 locomolcd_bl_device->props.brightness = 0;
208 locomolcd_bl_device->props.power = 0;
Henrique de Moraes Holschuh4437cd12006-12-08 02:40:49 -0800209 locomolcd_set_intensity(locomolcd_bl_device);
210
Richard Purdiee4423782006-06-26 00:26:27 -0700211 backlight_device_unregister(locomolcd_bl_device);
Pavel Machek987132b2005-09-06 15:16:45 -0700212 local_irq_save(flags);
213 locomolcd_dev = NULL;
214 local_irq_restore(flags);
215 return 0;
216}
217
218static struct locomo_driver poodle_lcd_driver = {
219 .drv = {
220 .name = "locomo-backlight",
221 },
222 .devid = LOCOMO_DEVID_BACKLIGHT,
Richard Purdiee4423782006-06-26 00:26:27 -0700223 .probe = locomolcd_probe,
224 .remove = locomolcd_remove,
225 .suspend = locomolcd_suspend,
226 .resume = locomolcd_resume,
Pavel Machek987132b2005-09-06 15:16:45 -0700227};
228
Richard Purdiee4423782006-06-26 00:26:27 -0700229
230static int __init locomolcd_init(void)
Pavel Machek987132b2005-09-06 15:16:45 -0700231{
232 int ret = locomo_driver_register(&poodle_lcd_driver);
Richard Purdiee4423782006-06-26 00:26:27 -0700233 if (ret)
234 return ret;
Pavel Machek987132b2005-09-06 15:16:45 -0700235
236#ifdef CONFIG_SA1100_COLLIE
237 sa1100fb_lcd_power = locomolcd_power;
238#endif
239 return 0;
240}
Pavel Machek987132b2005-09-06 15:16:45 -0700241
Richard Purdiee4423782006-06-26 00:26:27 -0700242static void __exit locomolcd_exit(void)
243{
244 locomo_driver_unregister(&poodle_lcd_driver);
245}
246
247module_init(locomolcd_init);
248module_exit(locomolcd_exit);
249
Pavel Macheka2531292010-07-18 14:27:13 +0200250MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>, Pavel Machek <pavel@ucw.cz>");
Richard Purdiee4423782006-06-26 00:26:27 -0700251MODULE_DESCRIPTION("Collie LCD driver");
252MODULE_LICENSE("GPL");