[xburst] jz4740_fb: Add support for parallel displays, improve power consumption
[openwrt.git] / target / linux / xburst / files-2.6.32 / drivers / video / backlight / gpm940b0.c
1 /*
2 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4720/JZ4740 SoC LCD framebuffer driver
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
13 *
14 */
15
16 #include <linux/module.h>
17 #include <linux/spi/spi.h>
18 #include <linux/lcd.h>
19 #include <linux/backlight.h>
20 #include <linux/delay.h>
21
22 struct gpm940b0 {
23 struct spi_device *spi;
24 struct lcd_device *lcd;
25 struct backlight_device *bl;
26 unsigned enabled:1;
27 };
28
29 static int gpm940b0_write_reg(struct spi_device *spi, uint8_t reg,
30 uint8_t data)
31 {
32 uint8_t buf[2];
33 buf[0] = ((reg & 0x40) << 1) | (reg & 0x3f);
34 buf[1] = data;
35
36 return spi_write(spi, buf, sizeof(buf));
37 }
38
39 static void gpm940b0_power_disable(struct gpm940b0 *gpm940b0)
40 {
41 int ret = gpm940b0_write_reg(gpm940b0->spi, 0x5, 0xc6) ;
42 if (ret < 0)
43 printk("Failed to disable power: %d\n", ret);
44 }
45
46 static void gpm940b0_power_enable(struct gpm940b0 *gpm940b0)
47 {
48 gpm940b0_write_reg(gpm940b0->spi, 0x5, 0xc7);
49 }
50
51
52 static int gpm940b0_set_power(struct lcd_device *lcd, int power)
53 {
54 struct gpm940b0 *gpm940b0 = lcd_get_data(lcd);
55
56 switch (power) {
57 case FB_BLANK_UNBLANK:
58 mdelay(20);
59 gpm940b0->enabled = 1;
60 gpm940b0_power_enable(gpm940b0);
61 break;
62 default:
63 gpm940b0->enabled = 0;
64 gpm940b0_power_disable(gpm940b0);
65 mdelay(20);
66 break;
67 }
68 return 0;
69 }
70
71 static int gpm940b0_set_contrast(struct lcd_device *lcd, int contrast)
72 {
73 struct gpm940b0 *gpm940b0 = lcd_get_data(lcd);
74 gpm940b0_write_reg(gpm940b0->spi, 0x0d, contrast);
75 return 0;
76 }
77
78 static int gpm940b0_set_mode(struct lcd_device *lcd, struct fb_videomode *mode)
79 {
80 if (mode->xres != 320 && mode->yres != 240)
81 return -EINVAL;
82
83 return 0;
84 }
85
86 /*
87 int gpm940b0_bl_update_status(struct backlight_device *bl)
88 {
89 struct gpm940b0 *gpm940b0 = bl_get_data(bl);
90
91 gpm940b0->reg5 &= ~0x38;
92 gpm940b0->reg5 |= ((bl->props.brightness << 3) & 0x38);
93
94 gpm940b0_write_reg(gpm940b0->spi, 0x5, gpm940b0->reg5);
95
96 return 0;
97 }*/
98
99 static ssize_t reg_write(struct device *dev, struct device_attribute *attr,
100 const char *buf, size_t count)
101 {
102 char *buf2;
103 uint32_t reg = simple_strtoul(buf, &buf2, 10);
104 uint32_t val = simple_strtoul(buf2 + 1, NULL, 10);
105 struct gpm940b0 *gpm940b0 = dev_get_drvdata(dev);
106
107 if (reg < 0 || val < 0)
108 return -EINVAL;
109
110 gpm940b0_write_reg(gpm940b0->spi, reg, val);
111 return count;
112 }
113
114 static DEVICE_ATTR(reg, 0644, NULL, reg_write);
115
116 static struct lcd_ops gpm940b0_lcd_ops = {
117 .set_power = gpm940b0_set_power,
118 .set_contrast = gpm940b0_set_contrast,
119 .set_mode = gpm940b0_set_mode,
120 };
121
122 #if 0
123 static struct backlight_ops gpm940b0_bl_ops = {
124 /* .get_brightness = gpm940b0_bl_get_brightness,*/
125 .update_status = gpm940b0_bl_update_status,
126 };
127 #endif
128
129 static int __devinit gpm940b0_probe(struct spi_device *spi)
130 {
131 int ret;
132 struct gpm940b0 *gpm940b0;
133
134 gpm940b0 = kmalloc(sizeof(*gpm940b0), GFP_KERNEL);
135
136 spi->bits_per_word = 8;
137
138 ret = spi_setup(spi);
139 if (ret) {
140 dev_err(&spi->dev, "Failed to setup spi\n");
141 goto err_free_gpm940b0;
142 }
143
144 gpm940b0->spi = spi;
145
146 gpm940b0->lcd = lcd_device_register("gpm940b0-lcd", &spi->dev, gpm940b0,
147 &gpm940b0_lcd_ops);
148
149 if (IS_ERR(gpm940b0->lcd)) {
150 ret = PTR_ERR(gpm940b0->lcd);
151 dev_err(&spi->dev, "Failed to register lcd device: %d\n", ret);
152 goto err_free_gpm940b0;
153 }
154
155 gpm940b0->lcd->props.max_contrast = 255;
156
157 #if 0
158 gpm940b0->bl = backlight_device_register("gpm940b0-bl", &spi->dev, gpm940b0,
159 &gpm940b0_bl_ops);
160
161 if (IS_ERR(gpm940b0->bl)) {
162 ret = PTR_ERR(gpm940b0->bl);
163 dev_err(&spi->dev, "Failed to register backlight device: %d\n", ret);
164 gpm940b0->bl = NULL;
165 } else {
166 gpm940b0->bl->props.max_brightness = 8;
167 gpm940b0->bl->props.brightness = 0;
168 gpm940b0->bl->props.power = FB_BLANK_UNBLANK;
169 }
170 #endif
171
172 ret = device_create_file(&spi->dev, &dev_attr_reg);
173 if (ret)
174 goto err_unregister_lcd;
175
176 gpm940b0->enabled = 1;
177 dev_set_drvdata(&spi->dev, gpm940b0);
178
179 gpm940b0_write_reg(spi, 0x13, 0x01);
180 gpm940b0_write_reg(spi, 0x5, 0xc7);
181 return 0;
182 err_unregister_lcd:
183 lcd_device_unregister(gpm940b0->lcd);
184 err_free_gpm940b0:
185 kfree(gpm940b0);
186 return ret;
187 }
188
189 static int __devexit gpm940b0_remove(struct spi_device *spi)
190 {
191 struct gpm940b0 *gpm940b0 = spi_get_drvdata(spi);
192 #if 0
193 if (gpm940b0->bl)
194 backlight_device_unregister(gpm940b0->bl);
195 #endif
196
197 lcd_device_unregister(gpm940b0->lcd);
198
199 spi_set_drvdata(spi, NULL);
200 kfree(gpm940b0);
201 return 0;
202 }
203
204 #ifdef CONFIG_PM
205
206 static int gpm940b0_suspend(struct spi_device *spi, pm_message_t state)
207 {
208 struct gpm940b0 *gpm940b0 = spi_get_drvdata(spi);
209 if (gpm940b0->enabled) {
210 gpm940b0_power_disable(gpm940b0);
211 mdelay(10);
212 }
213 return 0;
214 }
215
216 static int gpm940b0_resume(struct spi_device *spi)
217 {
218 struct gpm940b0 *gpm940b0 = spi_get_drvdata(spi);
219 if (gpm940b0->enabled)
220 gpm940b0_power_enable(gpm940b0);
221 return 0;
222 }
223
224 #else
225 #define gpm940b0_suspend NULL
226 #define gpm940b0_resume NULL
227 #endif
228
229 static struct spi_driver gpm940b0_driver = {
230 .driver = {
231 .name = "gpm940b0",
232 .owner = THIS_MODULE,
233 },
234 .probe = gpm940b0_probe,
235 .remove = __devexit_p(gpm940b0_remove),
236 .suspend = gpm940b0_suspend,
237 .resume = gpm940b0_resume,
238 };
239
240 static int __init gpm940b0_init(void)
241 {
242 return spi_register_driver(&gpm940b0_driver);
243 }
244 module_init(gpm940b0_init);
245
246 static void __exit gpm940b0_exit(void)
247 {
248 return spi_unregister_driver(&gpm940b0_driver);
249 }
250 module_exit(gpm940b0_exit)
251
252 MODULE_AUTHOR("Lars-Peter Clausen");
253 MODULE_LICENSE("GPL v2");
254 MODULE_DESCRIPTION("LCD and backlight controll for Giantplus GPM940B0");
255 MODULE_ALIAS("spi:gpm940b0");
This page took 0.056025 seconds and 5 git commands to generate.