summaryrefslogtreecommitdiffstats
path: root/lights/gta04_lights.c
blob: 845a17dfdba19cc187745c85a4770d3f236d7df1 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (C) 2012-2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * 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 3 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/types.h>

#define LOG_TAG "gta04_lights"
#include <cutils/log.h>

#include <hardware/lights.h>

/*
 * Globals
 */

const char backlight_brightness[] =
	"/sys/class/backlight/pwm-backlight/brightness";
const char backlight_max_brightness[] =
	"/sys/class/backlight/pwm-backlight/max_brightness";

const char battery_red_brightness[] =
	"/sys/class/leds/gta04:red:power/brightness";
const char battery_red_max_brightness[] =
	"/sys/class/leds/gta04:red:power/max_brightness";
const char battery_green_brightness[] =
	"/sys/class/leds/gta04:green:power/brightness";
const char battery_green_max_brightness[] =
	"/sys/class/leds/gta04:green:power/max_brightness";

const char notifications_red_brightness[] =
	"/sys/class/leds/gta04:red:aux/brightness";
const char notifications_red_max_brightness[] =
	"/sys/class/leds/gta04:red:aux/max_brightness";
const char notifications_green_brightness[] =
	"/sys/class/leds/gta04:green:aux/brightness";
const char notifications_green_max_brightness[] =
	"/sys/class/leds/gta04:green:aux/max_brightness";

pthread_mutex_t lights_mutex = PTHREAD_MUTEX_INITIALIZER;

/*
 * Sysfs
 */

int sysfs_value_read(const char *path)
{
    char buffer[100];
    int value;
    int fd = -1;
    int rc;

    if (path == NULL)
        return -1;

    fd = open(path, O_RDONLY);
    if (fd < 0)
        goto error;

    rc = read(fd, &buffer, sizeof(buffer));
    if (rc <= 0)
        goto error;

    value = atoi(buffer);
    goto complete;

error:
    value = -1;

complete:
    if (fd >= 0)
        close(fd);

    return value;
}

int sysfs_value_write(const char *path, int value)
{
    char buffer[100];
    int fd = -1;
    int rc;

    if (path == NULL)
        return -1;

    fd = open(path, O_WRONLY);
    if (fd < 0)
        goto error;

    snprintf((char *) &buffer, sizeof(buffer), "%d\n", value);

    rc = write(fd, buffer, strlen(buffer));
    if (rc < (int) strlen(buffer))
        goto error;

    rc = 0;
    goto complete;

error:
    rc = -1;

complete:
    if (fd >= 0)
        close(fd);

    return rc;
}

/*
 * GTA04 Lights
 */

int gta04_lights_set_light_notifications(struct light_device_t *device,
	const struct light_state_t *state)
{
	int red, green;
	int max;
	int rc = 0;

	if (state == NULL)
		return -EINVAL;

	// GTA04 only has red and green: blue is merged to green
	red = (state->color & 0x00ff0000) >> 16;
	green = (state->color & 0x0000ff00) >> 8 | (state->color & 0x000000ff);

	pthread_mutex_lock(&lights_mutex);

	// Red max
	max = sysfs_value_read(notifications_red_max_brightness);
	if (max > 0)
		red = (red * max) / 0xff;

	// Green max
	max = sysfs_value_read(notifications_green_max_brightness);
	if (max > 0)
		green = (green * max) / 0xff;

	rc |= sysfs_value_write(notifications_red_brightness, red);
	rc |= sysfs_value_write(notifications_green_brightness, green);

	pthread_mutex_unlock(&lights_mutex);

	return rc;
}

int gta04_lights_set_light_battery(struct light_device_t *device,
	const struct light_state_t *state)
{
	int red, green;
	int max;
	int rc = 0;

	if (state == NULL)
		return -EINVAL;

	// GTA04 only has red and green: blue is merged to green
	red = (state->color & 0x00ff0000) >> 16;
	green = (state->color & 0x0000ff00) >> 8 | (state->color & 0x000000ff);

	pthread_mutex_lock(&lights_mutex);

	// Red max
	max = sysfs_value_read(battery_red_max_brightness);
	if (max > 0)
		red = (red * max) / 0xff;

	// Green max
	max = sysfs_value_read(battery_green_max_brightness);
	if (max > 0)
		green = (green * max) / 0xff;

	rc |= sysfs_value_write(battery_red_brightness, red);
	rc |= sysfs_value_write(battery_green_brightness, green);

	pthread_mutex_unlock(&lights_mutex);

	return rc;
}

int gta04_lights_set_light_backlight(struct light_device_t *device,
	const struct light_state_t *state)
{
	int brightness;
	int color;
	int max;
	int rc;

	if (state == NULL)
		return -EINVAL;

	color = state->color & 0x00ffffff;
	brightness = ((77 * ((color >> 16) & 0xff)) + (150 * ((color >> 8) & 0xff)) + (29 * (color & 0xff))) >> 8;

	pthread_mutex_lock(&lights_mutex);

	// Brightness max
	max = sysfs_value_read(backlight_max_brightness);
	if (max > 0)
		brightness = (brightness * max) / 0xff;

	rc = sysfs_value_write(backlight_brightness, brightness);

	pthread_mutex_unlock(&lights_mutex);

	return rc;
}

/*
 * Interface
 */

int gta04_lights_close(struct light_device_t *device)
{
	ALOGD("%s(%p)", __func__, device);

	if (device == NULL)
		return -EINVAL;

	pthread_mutex_destroy(&lights_mutex);

	free(device);

	return 0;
}

int gta04_lights_open(const struct hw_module_t *module, char const *light_id,
	struct hw_device_t **device)
{
	struct light_device_t *light_device;
	int (*set_light)(struct light_device_t *light_device,
		const struct light_state_t *state);

	ALOGD("%s(%p, %s, %p)", __func__, module, light_id, device);

	if (module == NULL || light_id == NULL || device == NULL)
		return -EINVAL;

	if (strcmp(LIGHT_ID_BACKLIGHT, light_id) == 0)
		set_light = gta04_lights_set_light_backlight;
	else if (strcmp(LIGHT_ID_BATTERY, light_id) == 0)
		set_light = gta04_lights_set_light_battery;
	else if (strcmp(LIGHT_ID_NOTIFICATIONS, light_id) == 0)
		set_light = gta04_lights_set_light_notifications;
	else
		return -EINVAL;

	pthread_mutex_init(&lights_mutex, NULL);

	light_device = (struct light_device_t *) calloc(1, sizeof(struct light_device_t));
	light_device->common.tag = HARDWARE_DEVICE_TAG;
	light_device->common.version = 0;
	light_device->common.module = (struct hw_module_t *) module;
	light_device->common.close = (int (*)(struct hw_device_t *)) gta04_lights_close;
	light_device->set_light = set_light;

	*device = (struct hw_device_t *) light_device;

	return 0;
}

struct hw_module_methods_t gta04_lights_module_methods = {
	.open =  gta04_lights_open,
};

struct hw_module_t HAL_MODULE_INFO_SYM = {
	.tag = HARDWARE_MODULE_TAG,
	.version_major = 1,
	.version_minor = 0,
	.id = LIGHTS_HARDWARE_MODULE_ID,
	.name = "GTA04 Lights",
	.author = "Paul Kocialkowski",
	.methods = &gta04_lights_module_methods,
};