2 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4720/JZ4740 SoC LCD framebuffer driver
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.
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.
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>
23 struct spi_device *spi;
24 struct lcd_device *lcd;
25 struct backlight_device *bl;
29 static int gpm940b0_write_reg(struct spi_device *spi, uint8_t reg,
33 buf[0] = ((reg & 0x40) << 1) | (reg & 0x3f);
36 return spi_write(spi, buf, sizeof(buf));
39 static void gpm940b0_power_disable(struct gpm940b0 *gpm940b0)
41 int ret = gpm940b0_write_reg(gpm940b0->spi, 0x5, 0xc6) ;
43 printk("Failed to disable power: %d\n", ret);
46 static void gpm940b0_power_enable(struct gpm940b0 *gpm940b0)
48 gpm940b0_write_reg(gpm940b0->spi, 0x5, 0xc7);
52 static int gpm940b0_set_power(struct lcd_device *lcd, int power)
54 struct gpm940b0 *gpm940b0 = lcd_get_data(lcd);
57 case FB_BLANK_UNBLANK:
59 gpm940b0->enabled = 1;
60 gpm940b0_power_enable(gpm940b0);
63 gpm940b0->enabled = 0;
64 gpm940b0_power_disable(gpm940b0);
71 static int gpm940b0_set_contrast(struct lcd_device *lcd, int contrast)
73 struct gpm940b0 *gpm940b0 = lcd_get_data(lcd);
74 gpm940b0_write_reg(gpm940b0->spi, 0x0d, contrast);
78 static int gpm940b0_set_mode(struct lcd_device *lcd, struct fb_videomode *mode)
80 if (mode->xres != 320 && mode->yres != 240)
87 int gpm940b0_bl_update_status(struct backlight_device *bl)
89 struct gpm940b0 *gpm940b0 = bl_get_data(bl);
91 gpm940b0->reg5 &= ~0x38;
92 gpm940b0->reg5 |= ((bl->props.brightness << 3) & 0x38);
94 gpm940b0_write_reg(gpm940b0->spi, 0x5, gpm940b0->reg5);
99 static ssize_t reg_write(struct device *dev, struct device_attribute *attr,
100 const char *buf, size_t count)
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);
107 if (reg < 0 || val < 0)
110 gpm940b0_write_reg(gpm940b0->spi, reg, val);
114 static DEVICE_ATTR(reg, 0644, NULL, reg_write);
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,
123 static struct backlight_ops gpm940b0_bl_ops = {
124 /* .get_brightness = gpm940b0_bl_get_brightness,*/
125 .update_status = gpm940b0_bl_update_status,
129 static int __devinit gpm940b0_probe(struct spi_device *spi)
132 struct gpm940b0 *gpm940b0;
134 gpm940b0 = kmalloc(sizeof(*gpm940b0), GFP_KERNEL);
136 spi->bits_per_word = 8;
138 ret = spi_setup(spi);
140 dev_err(&spi->dev, "Failed to setup spi\n");
141 goto err_free_gpm940b0;
146 gpm940b0->lcd = lcd_device_register("gpm940b0-lcd", &spi->dev, gpm940b0,
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;
155 gpm940b0->lcd->props.max_contrast = 255;
158 gpm940b0->bl = backlight_device_register("gpm940b0-bl", &spi->dev, gpm940b0,
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);
166 gpm940b0->bl->props.max_brightness = 8;
167 gpm940b0->bl->props.brightness = 0;
168 gpm940b0->bl->props.power = FB_BLANK_UNBLANK;
172 ret = device_create_file(&spi->dev, &dev_attr_reg);
174 goto err_unregister_lcd;
176 gpm940b0->enabled = 1;
177 dev_set_drvdata(&spi->dev, gpm940b0);
179 gpm940b0_write_reg(spi, 0x13, 0x01);
180 gpm940b0_write_reg(spi, 0x5, 0xc7);
183 lcd_device_unregister(gpm940b0->lcd);
189 static int __devexit gpm940b0_remove(struct spi_device *spi)
191 struct gpm940b0 *gpm940b0 = spi_get_drvdata(spi);
194 backlight_device_unregister(gpm940b0->bl);
197 lcd_device_unregister(gpm940b0->lcd);
199 spi_set_drvdata(spi, NULL);
206 static int gpm940b0_suspend(struct spi_device *spi, pm_message_t state)
208 struct gpm940b0 *gpm940b0 = spi_get_drvdata(spi);
209 if (gpm940b0->enabled) {
210 gpm940b0_power_disable(gpm940b0);
216 static int gpm940b0_resume(struct spi_device *spi)
218 struct gpm940b0 *gpm940b0 = spi_get_drvdata(spi);
219 if (gpm940b0->enabled)
220 gpm940b0_power_enable(gpm940b0);
225 #define gpm940b0_suspend NULL
226 #define gpm940b0_resume NULL
229 static struct spi_driver gpm940b0_driver = {
232 .owner = THIS_MODULE,
234 .probe = gpm940b0_probe,
235 .remove = __devexit_p(gpm940b0_remove),
236 .suspend = gpm940b0_suspend,
237 .resume = gpm940b0_resume,
240 static int __init gpm940b0_init(void)
242 return spi_register_driver(&gpm940b0_driver);
244 module_init(gpm940b0_init);
246 static void __exit gpm940b0_exit(void)
248 return spi_unregister_driver(&gpm940b0_driver);
250 module_exit(gpm940b0_exit)
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");