viafb: use proper pci config API
[linux/fpc-iii.git] / drivers / video / backlight / tosa_bl.c
blobf57bbf170049c8c7444210155a4d143a43ec0090
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>
22 #include <asm/mach/sharpsl_param.h>
24 #include <mach/tosa.h>
26 #define COMADJ_DEFAULT 97
28 #define DAC_CH1 0
29 #define DAC_CH2 1
31 struct tosa_bl_data {
32 struct i2c_client *i2c;
33 struct backlight_device *bl;
35 int comadj;
38 static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness)
40 struct spi_device *spi = data->i2c->dev.platform_data;
42 i2c_smbus_write_byte_data(data->i2c, DAC_CH1, data->comadj);
44 /* SetBacklightDuty */
45 i2c_smbus_write_byte_data(data->i2c, DAC_CH2, (u8)(brightness & 0xff));
47 /* SetBacklightVR */
48 gpio_set_value(TOSA_GPIO_BL_C20MA, brightness & 0x100);
50 tosa_bl_enable(spi, brightness);
53 static int tosa_bl_update_status(struct backlight_device *dev)
55 struct backlight_properties *props = &dev->props;
56 struct tosa_bl_data *data = dev_get_drvdata(&dev->dev);
57 int power = max(props->power, props->fb_blank);
58 int brightness = props->brightness;
60 if (power)
61 brightness = 0;
63 tosa_bl_set_backlight(data, brightness);
65 return 0;
68 static int tosa_bl_get_brightness(struct backlight_device *dev)
70 struct backlight_properties *props = &dev->props;
72 return props->brightness;
75 static const struct backlight_ops bl_ops = {
76 .get_brightness = tosa_bl_get_brightness,
77 .update_status = tosa_bl_update_status,
80 static int __devinit tosa_bl_probe(struct i2c_client *client,
81 const struct i2c_device_id *id)
83 struct backlight_properties props;
84 struct tosa_bl_data *data = kzalloc(sizeof(struct tosa_bl_data), GFP_KERNEL);
85 int ret = 0;
86 if (!data)
87 return -ENOMEM;
89 data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj;
91 ret = gpio_request(TOSA_GPIO_BL_C20MA, "backlight");
92 if (ret) {
93 dev_dbg(&data->bl->dev, "Unable to request gpio!\n");
94 goto err_gpio_bl;
96 ret = gpio_direction_output(TOSA_GPIO_BL_C20MA, 0);
97 if (ret)
98 goto err_gpio_dir;
100 i2c_set_clientdata(client, data);
101 data->i2c = client;
103 memset(&props, 0, sizeof(struct backlight_properties));
104 props.max_brightness = 512 - 1;
105 data->bl = backlight_device_register("tosa-bl", &client->dev, data,
106 &bl_ops, &props);
107 if (IS_ERR(data->bl)) {
108 ret = PTR_ERR(data->bl);
109 goto err_reg;
112 data->bl->props.brightness = 69;
113 data->bl->props.power = FB_BLANK_UNBLANK;
115 backlight_update_status(data->bl);
117 return 0;
119 err_reg:
120 data->bl = NULL;
121 i2c_set_clientdata(client, 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;
135 i2c_set_clientdata(client, NULL);
137 gpio_free(TOSA_GPIO_BL_C20MA);
139 kfree(data);
141 return 0;
144 #ifdef CONFIG_PM
145 static int tosa_bl_suspend(struct i2c_client *client, pm_message_t pm)
147 struct tosa_bl_data *data = i2c_get_clientdata(client);
149 tosa_bl_set_backlight(data, 0);
151 return 0;
154 static int tosa_bl_resume(struct i2c_client *client)
156 struct tosa_bl_data *data = i2c_get_clientdata(client);
158 backlight_update_status(data->bl);
159 return 0;
161 #else
162 #define tosa_bl_suspend NULL
163 #define tosa_bl_resume NULL
164 #endif
166 static const struct i2c_device_id tosa_bl_id[] = {
167 { "tosa-bl", 0 },
168 { },
172 static struct i2c_driver tosa_bl_driver = {
173 .driver = {
174 .name = "tosa-bl",
175 .owner = THIS_MODULE,
177 .probe = tosa_bl_probe,
178 .remove = __devexit_p(tosa_bl_remove),
179 .suspend = tosa_bl_suspend,
180 .resume = tosa_bl_resume,
181 .id_table = tosa_bl_id,
184 static int __init tosa_bl_init(void)
186 return i2c_add_driver(&tosa_bl_driver);
189 static void __exit tosa_bl_exit(void)
191 i2c_del_driver(&tosa_bl_driver);
194 module_init(tosa_bl_init);
195 module_exit(tosa_bl_exit);
197 MODULE_AUTHOR("Dmitry Baryshkov");
198 MODULE_LICENSE("GPL v2");
199 MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");