Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / backlight / tosa_bl.c
blob2a04b382ec4834a03270db15701136f5f01694cf
1 /*
2 * LCD / Backlight control code for Sharp SL-6000x (tosa)
4 * Copyright (c) 2005 Dirk Opfer
5 * Copyright (c) 2007,2008 Dmitry Baryshkov
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/spi/spi.h>
17 #include <linux/i2c.h>
18 #include <linux/gpio.h>
19 #include <linux/fb.h>
20 #include <linux/backlight.h>
21 #include <linux/slab.h>
23 #include <asm/mach/sharpsl_param.h>
25 #include <mach/tosa.h>
27 #define COMADJ_DEFAULT 97
29 #define DAC_CH1 0
30 #define DAC_CH2 1
32 struct tosa_bl_data {
33 struct i2c_client *i2c;
34 struct backlight_device *bl;
36 int comadj;
39 static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness)
41 struct spi_device *spi = data->i2c->dev.platform_data;
43 i2c_smbus_write_byte_data(data->i2c, DAC_CH1, data->comadj);
45 /* SetBacklightDuty */
46 i2c_smbus_write_byte_data(data->i2c, DAC_CH2, (u8)(brightness & 0xff));
48 /* SetBacklightVR */
49 gpio_set_value(TOSA_GPIO_BL_C20MA, brightness & 0x100);
51 tosa_bl_enable(spi, brightness);
54 static int tosa_bl_update_status(struct backlight_device *dev)
56 struct backlight_properties *props = &dev->props;
57 struct tosa_bl_data *data = dev_get_drvdata(&dev->dev);
58 int power = max(props->power, props->fb_blank);
59 int brightness = props->brightness;
61 if (power)
62 brightness = 0;
64 tosa_bl_set_backlight(data, brightness);
66 return 0;
69 static int tosa_bl_get_brightness(struct backlight_device *dev)
71 struct backlight_properties *props = &dev->props;
73 return props->brightness;
76 static const struct backlight_ops bl_ops = {
77 .get_brightness = tosa_bl_get_brightness,
78 .update_status = tosa_bl_update_status,
81 static int __devinit tosa_bl_probe(struct i2c_client *client,
82 const struct i2c_device_id *id)
84 struct backlight_properties props;
85 struct tosa_bl_data *data = kzalloc(sizeof(struct tosa_bl_data), GFP_KERNEL);
86 int ret = 0;
87 if (!data)
88 return -ENOMEM;
90 data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj;
92 ret = gpio_request(TOSA_GPIO_BL_C20MA, "backlight");
93 if (ret) {
94 dev_dbg(&data->bl->dev, "Unable to request gpio!\n");
95 goto err_gpio_bl;
97 ret = gpio_direction_output(TOSA_GPIO_BL_C20MA, 0);
98 if (ret)
99 goto err_gpio_dir;
101 i2c_set_clientdata(client, data);
102 data->i2c = client;
104 memset(&props, 0, sizeof(struct backlight_properties));
105 props.max_brightness = 512 - 1;
106 data->bl = backlight_device_register("tosa-bl", &client->dev, data,
107 &bl_ops, &props);
108 if (IS_ERR(data->bl)) {
109 ret = PTR_ERR(data->bl);
110 goto err_reg;
113 data->bl->props.brightness = 69;
114 data->bl->props.power = FB_BLANK_UNBLANK;
116 backlight_update_status(data->bl);
118 return 0;
120 err_reg:
121 data->bl = NULL;
122 err_gpio_dir:
123 gpio_free(TOSA_GPIO_BL_C20MA);
124 err_gpio_bl:
125 kfree(data);
126 return ret;
129 static int __devexit tosa_bl_remove(struct i2c_client *client)
131 struct tosa_bl_data *data = i2c_get_clientdata(client);
133 backlight_device_unregister(data->bl);
134 data->bl = NULL;
136 gpio_free(TOSA_GPIO_BL_C20MA);
138 kfree(data);
140 return 0;
143 #ifdef CONFIG_PM
144 static int tosa_bl_suspend(struct i2c_client *client, pm_message_t pm)
146 struct tosa_bl_data *data = i2c_get_clientdata(client);
148 tosa_bl_set_backlight(data, 0);
150 return 0;
153 static int tosa_bl_resume(struct i2c_client *client)
155 struct tosa_bl_data *data = i2c_get_clientdata(client);
157 backlight_update_status(data->bl);
158 return 0;
160 #else
161 #define tosa_bl_suspend NULL
162 #define tosa_bl_resume NULL
163 #endif
165 static const struct i2c_device_id tosa_bl_id[] = {
166 { "tosa-bl", 0 },
167 { },
171 static struct i2c_driver tosa_bl_driver = {
172 .driver = {
173 .name = "tosa-bl",
174 .owner = THIS_MODULE,
176 .probe = tosa_bl_probe,
177 .remove = __devexit_p(tosa_bl_remove),
178 .suspend = tosa_bl_suspend,
179 .resume = tosa_bl_resume,
180 .id_table = tosa_bl_id,
183 static int __init tosa_bl_init(void)
185 return i2c_add_driver(&tosa_bl_driver);
188 static void __exit tosa_bl_exit(void)
190 i2c_del_driver(&tosa_bl_driver);
193 module_init(tosa_bl_init);
194 module_exit(tosa_bl_exit);
196 MODULE_AUTHOR("Dmitry Baryshkov");
197 MODULE_LICENSE("GPL v2");
198 MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");