summaryrefslogtreecommitdiffstats
path: root/drivers/video/simple-panel.c
blob: be39ff0e947456bfe51b33b8bea8f2877fc15b1e (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
/*
 * simple panel support for barebox
 *
 * (C) Copyright 2014 Sascha Hauer, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program 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.
 *
 * This program 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.
 *
 */
#include <common.h>
#include <malloc.h>
#include <init.h>
#include <linux/err.h>
#include <of.h>
#include <fb.h>
#include <gpio.h>
#include <of_gpio.h>
#include <video/backlight.h>
#include <video/vpl.h>
#include <i2c/i2c.h>

struct simple_panel {
	struct device_d *dev;
	struct vpl vpl;
	int enable_gpio;
	int enable_active_high;
	int enabled;
	struct device_node *backlight_node;
	struct backlight_device *backlight;
	struct device_node *ddc_node;
	int enable_delay;
};

static int simple_panel_enable(struct simple_panel *panel)
{
	int ret;

	dev_dbg(panel->dev, "enabling\n");

	if (panel->backlight_node && !panel->backlight) {
		panel->backlight = of_backlight_find(panel->backlight_node);
		if (!panel->backlight) {
			dev_err(panel->dev, "Cannot find backlight\n");
			return -ENODEV;
		}
	}

	if (gpio_is_valid(panel->enable_gpio))
		gpio_direction_output(panel->enable_gpio,
			panel->enable_active_high);

	if (panel->enable_delay)
		mdelay(panel->enable_delay);

	if (panel->backlight) {
		ret = backlight_set_brightness_default(panel->backlight);
		if (ret)
			return ret;
	}

	return 0;
}

static int simple_panel_disable(struct simple_panel *panel)
{
	dev_dbg(panel->dev, "disabling\n");

	if (panel->backlight)
		backlight_set_brightness(panel->backlight, 0);

	if (gpio_is_valid(panel->enable_gpio))
		gpio_direction_output(panel->enable_gpio,
			!panel->enable_active_high);

	return 0;
}

static int simple_panel_get_modes(struct simple_panel *panel, struct display_timings *timings)
{
	struct display_timings *modes;
	int ret;

	if (panel->ddc_node && IS_ENABLED(CONFIG_DRIVER_VIDEO_EDID) &&
	    IS_ENABLED(CONFIG_I2C)) {
		struct i2c_adapter *i2c;

                i2c = of_find_i2c_adapter_by_node(panel->ddc_node);
		if (!i2c) {
			dev_err(panel->dev, "cannot find edid i2c node\n");
			return -ENODEV;
		}
		timings->edid = edid_read_i2c(i2c);
		if (!timings->edid) {
			dev_err(panel->dev, "cannot read edid data\n");
			return -EINVAL;
		}

		ret = edid_to_display_timings(timings, timings->edid);
		if (ret) {
			dev_err(panel->dev, "cannot convert edid data to timings\n");
			return ret;
		}
	}

	modes = of_get_display_timings(panel->dev->device_node);
	if (modes) {
		timings->modes = modes->modes;
		timings->num_modes = modes->num_modes;
		return 0;
	}

	dev_err(panel->dev, "No modes found\n");

	return -ENOENT;
}

static int simple_panel_ioctl(struct vpl *vpl, unsigned int port,
			unsigned int cmd, void *ptr)
{
	struct simple_panel *panel = container_of(vpl,
			struct simple_panel, vpl);

	switch (cmd) {
	case VPL_ENABLE:
		return simple_panel_enable(panel);
	case VPL_DISABLE:
		return simple_panel_disable(panel);
	case VPL_GET_VIDEOMODES:
		return simple_panel_get_modes(panel, ptr);
	default:
		return -ENOSYS;
	}
}

static int simple_panel_probe(struct device_d *dev)
{
	struct simple_panel *panel;
	struct device_node *node = dev->device_node;
	enum of_gpio_flags flags;
	int ret;

	panel = xzalloc(sizeof(*panel));

	panel->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0, &flags);
	panel->vpl.node = node;
	panel->vpl.ioctl = simple_panel_ioctl;
	panel->dev = dev;

	if (gpio_is_valid(panel->enable_gpio)) {
		if (!(flags & OF_GPIO_ACTIVE_LOW))
			panel->enable_active_high = 1;
	}

	panel->ddc_node = of_parse_phandle(node, "ddc-i2c-bus", 0);

	of_property_read_u32(node, "enable-delay", &panel->enable_delay);

	panel->backlight_node = of_parse_phandle(node, "backlight", 0);

	ret = vpl_register(&panel->vpl);
	if (ret)
		return ret;

	return 0;
}

static struct of_device_id simple_panel_of_ids[] = {
	{ .compatible = "simple-panel", },
	{ }
};

static struct driver_d simple_panel_driver = {
	.name  = "simple-panel",
	.probe = simple_panel_probe,
	.of_compatible = DRV_OF_COMPAT(simple_panel_of_ids),
};
device_platform_driver(simple_panel_driver);