aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/drivers/video/backlight/ubicom32lcdpower.c
blob: 6aeed43bc85b209a2d61471ba62f599ccc649d3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * drivers/video/backlight/ubicom32lcdpowerpower.c
 *	LCD power driver for the Ubicom32 platform
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/lcd.h>
#include <linux/fb.h>
#include <linux/gpio.h>

#include <asm/ubicom32lcdpower.h>
#include <asm/ip5000.h>

#define DRIVER_NAME			"ubicom32lcdpower"

struct ubicom32lcdpower_data {
	/*
	 * Pointer to the platform data structure.  Keep this around since we need values
	 * from it to set the backlight intensity.
	 */
	const struct ubicom32lcdpower_platform_data	*pdata;

	/*
	 * LCD device, we have to save this for use when we remove ourselves.
	 */
	struct lcd_device			*lcddev;
};

/*
 * ubicom32lcdpower_set_power
 */
static int ubicom32lcdpower_set_power(struct lcd_device *ld, int power)
{
	struct ubicom32lcdpower_data *ud = (struct ubicom32lcdpower_data *)lcd_get_data(ld);
	if (power == FB_BLANK_UNBLANK) {
		gpio_direction_output(ud->pdata->vgh_gpio, ud->pdata->vgh_polarity);
		return 0;
	}

	gpio_direction_output(ud->pdata->vgh_gpio, !ud->pdata->vgh_polarity);
	return 0;
}

/*
 * ubicom32lcdpower_get_power
 */
static int ubicom32lcdpower_get_power(struct lcd_device *ld)
{
	struct ubicom32lcdpower_data *ud = (struct ubicom32lcdpower_data *)lcd_get_data(ld);
	int vgh = gpio_get_value(ud->pdata->vgh_gpio);
	if ((vgh && ud->pdata->vgh_polarity) || (!vgh && !ud->pdata->vgh_polarity)) {
		return 1;
	}

	return 0;
}

static struct lcd_ops ubicom32lcdpower_ops = {
	.get_power = ubicom32lcdpower_get_power,
	.set_power = ubicom32lcdpower_set_power,
};

/*
 * ubicom32lcdpower_probe
 */
static int ubicom32lcdpower_probe(struct platform_device *pdev)
{
	const struct ubicom32lcdpower_platform_data *pdata = pdev->dev.platform_data;
	struct ubicom32lcdpower_data *ud;
	struct lcd_device *lcddev;
	int retval;

	/*
	 * Check to see if we have any platform data, if we don't have a LCD to control
	 */
	if (!pdata) {
		return -ENODEV;
	}

	/*
	 * Allocate our private data
	 */
	ud = kzalloc(sizeof(struct ubicom32lcdpower_data), GFP_KERNEL);
	if (!ud) {
		return -ENOMEM;
	}

	ud->pdata = pdata;

	/*
	 * Request our GPIOs
	 */
	retval = gpio_request(pdata->vgh_gpio, "vgh");
	if (retval) {
		dev_err(&pdev->dev, "Failed to allocate vgh GPIO\n");
		goto fail_gpio;
	}

	/*
	 * Register our lcd device
	 */
	lcddev = lcd_device_register(DRIVER_NAME, &pdev->dev, ud, &ubicom32lcdpower_ops);
	if (IS_ERR(lcddev)) {
		retval = PTR_ERR(lcddev);
		goto fail;
	}

	ud->lcddev = lcddev;
	platform_set_drvdata(pdev, ud);

	ubicom32lcdpower_set_power(lcddev, FB_BLANK_UNBLANK);

	printk(KERN_INFO DRIVER_NAME ": LCD driver started\n");

	return 0;

fail:
	gpio_free(pdata->vgh_gpio);

fail_gpio:
	platform_set_drvdata(pdev, NULL);
	kfree(ud);
	return retval;
}

/*
 * ubicom32lcdpower_remove
 */
static int __exit ubicom32lcdpower_remove(struct platform_device *pdev)
{
	struct ubicom32lcdpower_data *ud = platform_get_drvdata(pdev);

	lcd_device_unregister(ud->lcddev);
	platform_set_drvdata(pdev, NULL);
	kfree(ud);

	return 0;
}

static struct platform_driver ubicom32lcdpower_driver = {
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	},

	.remove = __exit_p(ubicom32lcdpower_remove),
};

/*
 * ubicom32lcdpower_init
 */
static int __init ubicom32lcdpower_init(void)
{
	return platform_driver_probe(&ubicom32lcdpower_driver, ubicom32lcdpower_probe);
}
module_init(ubicom32lcdpower_init);

/*
 * ubicom32lcdpower_exit
 */
static void __exit ubicom32lcdpower_exit(void)
{
	platform_driver_unregister(&ubicom32lcdpower_driver);
}
module_exit(ubicom32lcdpower_exit);

MODULE_AUTHOR("Patrick Tjin <@ubicom.com>");
MODULE_DESCRIPTION("Ubicom32 lcd power driver");
MODULE_LICENSE("GPL");