blob: 2b241abced43467378ceb3ea68e9633f78644684 [file] [log] [blame]
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +04001/*
2 * LCD / Backlight control code for Sharp SL-6000x (tosa)
3 *
4 * Copyright (c) 2005 Dirk Opfer
5 * Copyright (c) 2007,2008 Dmitry Baryshkov
6 *
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.
10 *
11 */
12
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040022
23#include <asm/mach/sharpsl_param.h>
24
25#include <mach/tosa.h>
26
27#define COMADJ_DEFAULT 97
28
29#define DAC_CH1 0
30#define DAC_CH2 1
31
32struct tosa_bl_data {
33 struct i2c_client *i2c;
34 struct backlight_device *bl;
35
36 int comadj;
37};
38
39static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness)
40{
41 struct spi_device *spi = data->i2c->dev.platform_data;
42
43 i2c_smbus_write_byte_data(data->i2c, DAC_CH1, data->comadj);
44
45 /* SetBacklightDuty */
46 i2c_smbus_write_byte_data(data->i2c, DAC_CH2, (u8)(brightness & 0xff));
47
48 /* SetBacklightVR */
49 gpio_set_value(TOSA_GPIO_BL_C20MA, brightness & 0x100);
50
51 tosa_bl_enable(spi, brightness);
52}
53
54static int tosa_bl_update_status(struct backlight_device *dev)
55{
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;
60
61 if (power)
62 brightness = 0;
63
64 tosa_bl_set_backlight(data, brightness);
65
66 return 0;
67}
68
69static int tosa_bl_get_brightness(struct backlight_device *dev)
70{
71 struct backlight_properties *props = &dev->props;
72
73 return props->brightness;
74}
75
Emese Revfy9905a432009-12-14 00:58:57 +010076static const struct backlight_ops bl_ops = {
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040077 .get_brightness = tosa_bl_get_brightness,
78 .update_status = tosa_bl_update_status,
79};
80
81static int __devinit tosa_bl_probe(struct i2c_client *client,
82 const struct i2c_device_id *id)
83{
Matthew Garretta19a6ee2010-02-17 16:39:44 -050084 struct backlight_properties props;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040085 struct tosa_bl_data *data = kzalloc(sizeof(struct tosa_bl_data), GFP_KERNEL);
86 int ret = 0;
87 if (!data)
88 return -ENOMEM;
89
90 data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj;
91
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;
96 }
97 ret = gpio_direction_output(TOSA_GPIO_BL_C20MA, 0);
98 if (ret)
99 goto err_gpio_dir;
100
101 i2c_set_clientdata(client, data);
102 data->i2c = client;
103
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500104 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700105 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500106 props.max_brightness = 512 - 1;
107 data->bl = backlight_device_register("tosa-bl", &client->dev, data,
108 &bl_ops, &props);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400109 if (IS_ERR(data->bl)) {
110 ret = PTR_ERR(data->bl);
111 goto err_reg;
112 }
113
114 data->bl->props.brightness = 69;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400115 data->bl->props.power = FB_BLANK_UNBLANK;
116
117 backlight_update_status(data->bl);
118
119 return 0;
120
121err_reg:
122 data->bl = NULL;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400123err_gpio_dir:
124 gpio_free(TOSA_GPIO_BL_C20MA);
125err_gpio_bl:
126 kfree(data);
127 return ret;
128}
129
130static int __devexit tosa_bl_remove(struct i2c_client *client)
131{
132 struct tosa_bl_data *data = i2c_get_clientdata(client);
133
134 backlight_device_unregister(data->bl);
135 data->bl = NULL;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400136
137 gpio_free(TOSA_GPIO_BL_C20MA);
138
139 kfree(data);
140
141 return 0;
142}
143
144#ifdef CONFIG_PM
145static int tosa_bl_suspend(struct i2c_client *client, pm_message_t pm)
146{
147 struct tosa_bl_data *data = i2c_get_clientdata(client);
148
149 tosa_bl_set_backlight(data, 0);
150
151 return 0;
152}
153
154static int tosa_bl_resume(struct i2c_client *client)
155{
156 struct tosa_bl_data *data = i2c_get_clientdata(client);
157
158 backlight_update_status(data->bl);
159 return 0;
160}
161#else
162#define tosa_bl_suspend NULL
163#define tosa_bl_resume NULL
164#endif
165
166static const struct i2c_device_id tosa_bl_id[] = {
167 { "tosa-bl", 0 },
168 { },
169};
170
171
172static struct i2c_driver tosa_bl_driver = {
173 .driver = {
174 .name = "tosa-bl",
175 .owner = THIS_MODULE,
176 },
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,
182};
183
Axel Lin81ce6862012-03-23 15:01:59 -0700184module_i2c_driver(tosa_bl_driver);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400185
186MODULE_AUTHOR("Dmitry Baryshkov");
187MODULE_LICENSE("GPL v2");
188MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");
189