summaryrefslogtreecommitdiffstats
path: root/backend/backlight_omapfb.c
blob: 0b37d3cbc54cc3fb0a86b350ceeb630545a48e52 (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
/*
 *   Copyright (C) 2010 Michael Buesch <m@bues.ch>
 *
 *   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 "backlight_omapfb.h"
#include "fileaccess.h"
#include "log.h"
#include "util.h"

#include <string.h>


#define BASEPATH	"/devices/platform/omapfb/panel"


static int omapfb_write_brightness(struct backlight_omapfb *bo)
{
	int err, level;

	if (bo->locked)
		level = 0;
	else
		level = bo->current_level;

	err = file_write_int(bo->level_file, level, 10);
	if (err)
		return err;

	return 0;
}

static int backlight_omapfb_max_brightness(struct backlight *b)
{
	struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);

	return bo->max_level;
}

static int backlight_omapfb_current_brightness(struct backlight *b)
{
	struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);

	return bo->current_level;
}

static int backlight_omapfb_set_brightness(struct backlight *b, int value)
{
	struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);
	int err;

	value = min(b->max_brightness(b), value);
	value = max(b->min_brightness(b), value);
	if (value == bo->current_level)
		return 0;
	bo->current_level = value;
	err = omapfb_write_brightness(bo);
	backlight_notify_state_change(b);

	return err;
}

static int backlight_omapfb_screen_lock(struct backlight *b, int lock)
{
	struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);
	int err;

	lock = !!lock;
	if (lock == bo->locked)
		return 0;
	bo->locked = lock;
	err = omapfb_write_brightness(bo);
	if (err)
		return err;
	backlight_notify_state_change(b);

	return 0;
}

static int backlight_omapfb_screen_is_locked(struct backlight *b)
{
	struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);

	return bo->locked;
}

static int backlight_omapfb_read_file(struct backlight_omapfb *bo)
{
	int err, value;

	err = file_read_int(bo->level_file, &value, 0);
	if (err) {
		logerr("WARNING: Failed to read backlight level file\n");
		return err;
	}
	bo->current_level = value;

	return 0;
}

static void backlight_omapfb_destroy(struct backlight *b)
{
	struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);

	file_close(bo->level_file);
	free(bo);
}

static struct backlight * backlight_omapfb_probe(void)
{
	struct backlight_omapfb *bo;
	struct fileaccess *file, *level_file;
	int max_level;
	int err;

	file = sysfs_file_open(O_RDONLY, "%s/backlight_max", BASEPATH);
	if (!file)
		goto error;
	err = file_read_int(file, &max_level, 0);
	file_close(file);
	if (err)
		goto error;
	level_file = sysfs_file_open(O_RDWR, "%s/backlight_level", BASEPATH);
	if (!level_file)
		goto error;

	bo = zalloc(sizeof(*bo));
	if (!bo)
		goto err_close;

	bo->level_file = level_file;
	bo->max_level = max_level;

	backlight_init(&bo->backlight, "omapfb");
	bo->backlight.max_brightness = backlight_omapfb_max_brightness;
	bo->backlight.current_brightness = backlight_omapfb_current_brightness;
	bo->backlight.set_brightness = backlight_omapfb_set_brightness;
	bo->backlight.screen_lock = backlight_omapfb_screen_lock;
	bo->backlight.screen_is_locked = backlight_omapfb_screen_is_locked;
	bo->backlight.destroy = backlight_omapfb_destroy;

	err = backlight_omapfb_read_file(bo);
	if (err)
		goto err_free;
	backlight_notify_state_change(&bo->backlight);

	return &bo->backlight;

err_free:
	free(bo);
err_close:
	file_close(level_file);
error:
	return NULL;
}

BACKLIGHT_PROBE(omapfb, backlight_omapfb_probe);
bues.ch cgit interface