blob: 6fa0b9d5559ac712c033eb0508fd21ca9a4dc08e [file] [log] [blame]
Andriy Skulysh06c6f902006-02-01 03:06:53 -08001/*
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>
Richard Purdie5f27a272006-03-31 02:31:50 -080016#include <linux/platform_device.h>
Andriy Skulysh06c6f902006-02-01 03:06:53 -080017#include <linux/spinlock.h>
18#include <linux/fb.h>
19#include <linux/backlight.h>
20
Paul Mundt193f3c22008-07-30 02:16:12 +090021#include <cpu/dac.h>
Kristoffer Ericson1aea7e02006-10-31 11:47:27 +090022#include <asm/hp6xx.h>
Andriy Skulysh848dd262006-09-27 16:09:59 +090023#include <asm/hd64461.h>
Andriy Skulysh06c6f902006-02-01 03:06:53 -080024
25#define HP680_MAX_INTENSITY 255
26#define HP680_DEFAULT_INTENSITY 10
27
Richard Purdie5f27a272006-03-31 02:31:50 -080028static int hp680bl_suspended;
Andriy Skulysh06c6f902006-02-01 03:06:53 -080029static int current_intensity = 0;
Ingo Molnar34af9462006-06-27 02:53:55 -070030static DEFINE_SPINLOCK(bl_lock);
Andriy Skulysh06c6f902006-02-01 03:06:53 -080031
Richard Purdie5f27a272006-03-31 02:31:50 -080032static void hp680bl_send_intensity(struct backlight_device *bd)
Andriy Skulysh06c6f902006-02-01 03:06:53 -080033{
34 unsigned long flags;
Richard Purdie5f27a272006-03-31 02:31:50 -080035 u16 v;
Richard Purdie599a52d2007-02-10 23:07:48 +000036 int intensity = bd->props.brightness;
Andriy Skulysh06c6f902006-02-01 03:06:53 -080037
Richard Purdie599a52d2007-02-10 23:07:48 +000038 if (bd->props.power != FB_BLANK_UNBLANK)
Richard Purdie5f27a272006-03-31 02:31:50 -080039 intensity = 0;
Richard Purdie599a52d2007-02-10 23:07:48 +000040 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
Richard Purdie5f27a272006-03-31 02:31:50 -080041 intensity = 0;
42 if (hp680bl_suspended)
Andriy Skulysh06c6f902006-02-01 03:06:53 -080043 intensity = 0;
44
45 spin_lock_irqsave(&bl_lock, flags);
Richard Purdie5f27a272006-03-31 02:31:50 -080046 if (intensity && current_intensity == 0) {
47 sh_dac_enable(DAC_LCD_BRIGHTNESS);
48 v = inw(HD64461_GPBDR);
49 v &= ~HD64461_GPBDR_LCDOFF;
50 outw(v, HD64461_GPBDR);
51 sh_dac_output(255-(u8)intensity, DAC_LCD_BRIGHTNESS);
52 } else if (intensity == 0 && current_intensity != 0) {
53 sh_dac_output(255-(u8)intensity, DAC_LCD_BRIGHTNESS);
54 sh_dac_disable(DAC_LCD_BRIGHTNESS);
55 v = inw(HD64461_GPBDR);
56 v |= HD64461_GPBDR_LCDOFF;
57 outw(v, HD64461_GPBDR);
58 } else if (intensity) {
59 sh_dac_output(255-(u8)intensity, DAC_LCD_BRIGHTNESS);
Andriy Skulysh06c6f902006-02-01 03:06:53 -080060 }
Richard Purdie5f27a272006-03-31 02:31:50 -080061 spin_unlock_irqrestore(&bl_lock, flags);
62
63 current_intensity = intensity;
Andriy Skulysh06c6f902006-02-01 03:06:53 -080064}
65
Richard Purdie5f27a272006-03-31 02:31:50 -080066
Andriy Skulysh06c6f902006-02-01 03:06:53 -080067#ifdef CONFIG_PM
Richard Purdiea8db3c12007-02-08 00:33:24 +000068static int hp680bl_suspend(struct platform_device *pdev, pm_message_t state)
Andriy Skulysh06c6f902006-02-01 03:06:53 -080069{
Richard Purdiea8db3c12007-02-08 00:33:24 +000070 struct backlight_device *bd = platform_get_drvdata(pdev);
71
Richard Purdie5f27a272006-03-31 02:31:50 -080072 hp680bl_suspended = 1;
Richard Purdiea8db3c12007-02-08 00:33:24 +000073 hp680bl_send_intensity(bd);
Andriy Skulysh06c6f902006-02-01 03:06:53 -080074 return 0;
75}
76
Richard Purdiea8db3c12007-02-08 00:33:24 +000077static int hp680bl_resume(struct platform_device *pdev)
Andriy Skulysh06c6f902006-02-01 03:06:53 -080078{
Richard Purdiea8db3c12007-02-08 00:33:24 +000079 struct backlight_device *bd = platform_get_drvdata(pdev);
80
Richard Purdie5f27a272006-03-31 02:31:50 -080081 hp680bl_suspended = 0;
Richard Purdiea8db3c12007-02-08 00:33:24 +000082 hp680bl_send_intensity(bd);
Andriy Skulysh06c6f902006-02-01 03:06:53 -080083 return 0;
84}
85#else
86#define hp680bl_suspend NULL
87#define hp680bl_resume NULL
88#endif
89
Richard Purdie5f27a272006-03-31 02:31:50 -080090static int hp680bl_set_intensity(struct backlight_device *bd)
Andriy Skulysh06c6f902006-02-01 03:06:53 -080091{
Richard Purdie5f27a272006-03-31 02:31:50 -080092 hp680bl_send_intensity(bd);
Andriy Skulysh06c6f902006-02-01 03:06:53 -080093 return 0;
94}
95
96static int hp680bl_get_intensity(struct backlight_device *bd)
97{
98 return current_intensity;
99}
100
Richard Purdie599a52d2007-02-10 23:07:48 +0000101static struct backlight_ops hp680bl_ops = {
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800102 .get_brightness = hp680bl_get_intensity,
Richard Purdie5f27a272006-03-31 02:31:50 -0800103 .update_status = hp680bl_set_intensity,
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800104};
105
Richard Purdiea8db3c12007-02-08 00:33:24 +0000106static int __init hp680bl_probe(struct platform_device *pdev)
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800107{
Richard Purdiea8db3c12007-02-08 00:33:24 +0000108 struct backlight_device *bd;
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800109
Richard Purdiea8db3c12007-02-08 00:33:24 +0000110 bd = backlight_device_register ("hp680-bl", &pdev->dev, NULL,
Richard Purdie599a52d2007-02-10 23:07:48 +0000111 &hp680bl_ops);
Richard Purdiea8db3c12007-02-08 00:33:24 +0000112 if (IS_ERR(bd))
113 return PTR_ERR(bd);
114
115 platform_set_drvdata(pdev, bd);
116
Richard Purdie599a52d2007-02-10 23:07:48 +0000117 bd->props.max_brightness = HP680_MAX_INTENSITY;
118 bd->props.brightness = HP680_DEFAULT_INTENSITY;
Richard Purdiea8db3c12007-02-08 00:33:24 +0000119 hp680bl_send_intensity(bd);
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800120
121 return 0;
122}
123
Richard Purdiea8db3c12007-02-08 00:33:24 +0000124static int hp680bl_remove(struct platform_device *pdev)
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800125{
Richard Purdiea8db3c12007-02-08 00:33:24 +0000126 struct backlight_device *bd = platform_get_drvdata(pdev);
127
Kristoffer Ericsoneb650d62007-09-11 12:44:38 +0900128 bd->props.brightness = 0;
129 bd->props.power = 0;
Richard Purdiea8db3c12007-02-08 00:33:24 +0000130 hp680bl_send_intensity(bd);
Henrique de Moraes Holschuh4437cd12006-12-08 02:40:49 -0800131
Richard Purdiea8db3c12007-02-08 00:33:24 +0000132 backlight_device_unregister(bd);
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800133
134 return 0;
135}
136
Richard Purdie5f27a272006-03-31 02:31:50 -0800137static struct platform_driver hp680bl_driver = {
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800138 .probe = hp680bl_probe,
139 .remove = hp680bl_remove,
140 .suspend = hp680bl_suspend,
141 .resume = hp680bl_resume,
Richard Purdie5f27a272006-03-31 02:31:50 -0800142 .driver = {
143 .name = "hp680-bl",
144 },
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800145};
146
Richard Purdie5f27a272006-03-31 02:31:50 -0800147static struct platform_device *hp680bl_device;
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800148
149static int __init hp680bl_init(void)
150{
151 int ret;
152
Richard Purdie5f27a272006-03-31 02:31:50 -0800153 ret = platform_driver_register(&hp680bl_driver);
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800154 if (!ret) {
Richard Purdie5f27a272006-03-31 02:31:50 -0800155 hp680bl_device = platform_device_alloc("hp680-bl", -1);
156 if (!hp680bl_device)
157 return -ENOMEM;
158
159 ret = platform_device_add(hp680bl_device);
160
161 if (ret) {
162 platform_device_put(hp680bl_device);
163 platform_driver_unregister(&hp680bl_driver);
164 }
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800165 }
166 return ret;
167}
168
169static void __exit hp680bl_exit(void)
170{
Richard Purdie5f27a272006-03-31 02:31:50 -0800171 platform_device_unregister(hp680bl_device);
172 platform_driver_unregister(&hp680bl_driver);
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800173}
174
175module_init(hp680bl_init);
176module_exit(hp680bl_exit);
177
Andriy Skulysh848dd262006-09-27 16:09:59 +0900178MODULE_AUTHOR("Andriy Skulysh <askulysh@gmail.com>");
Andriy Skulysh06c6f902006-02-01 03:06:53 -0800179MODULE_DESCRIPTION("HP Jornada 680 Backlight Driver");
180MODULE_LICENSE("GPL");