1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "lights"
18 
19 #include <cutils/log.h>
20 
21 #include <malloc.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 
31 #include <hardware/lights.h>
32 #include <hardware/hardware.h>
33 
34 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
35 char const* const MODE_RGB_FILE = "/sys/class/leds/indicator/ModeRGB";
36 char const* const BACKLIGHT_FILE = "/sys/class/backlight/tegra-dsi-backlight.0/brightness";
write_int(char const * path,int value)37 static int write_int(char const *path, int value)
38 {
39 	int fd;
40 	static int already_warned = -1;
41 	fd = open(path, O_RDWR);
42 	if (fd >= 0) {
43 		char buffer[20];
44 		int bytes = snprintf(buffer, 20, "%d\n", value);
45 		int amt = write(fd, buffer, bytes);
46 		close(fd);
47 		return amt == -1 ? -errno : 0;
48 	} else {
49 		if (already_warned == -1) {
50 			ALOGE("write_int failed to open %s\n", path);
51 			already_warned = 1;
52 		}
53 		return -errno;
54 	}
55 }
56 
rgb_to_brightness(struct light_state_t const * state)57 static int rgb_to_brightness(struct light_state_t const *state)
58 {
59 	int color = state->color & 0x00ffffff;
60 	return ((77 * ((color >> 16) & 0x00ff))
61 			+ (150 * ((color >> 8) & 0x00ff)) +
62 			(29 * (color & 0x00ff))) >> 8;
63 }
64 
set_light_battery(struct light_device_t * dev,struct light_state_t const * state)65 static int set_light_battery(struct light_device_t* dev,
66 		struct light_state_t const* state)
67 {
68 	int err;
69 	pthread_mutex_lock(&g_lock);
70 	ALOGV("set_light_battery, flashMode:%x, color:%x", state->flashMode, state->color);
71 	if(state->color)
72 		err = write_int(MODE_RGB_FILE, 0x1ffffff);
73 	else
74 		err = write_int(MODE_RGB_FILE, 0x00);
75 	pthread_mutex_unlock(&g_lock);
76 	return err;
77 }
78 
set_light_notifications(struct light_device_t * dev,struct light_state_t const * state)79 static int set_light_notifications(struct light_device_t* dev,
80 		struct light_state_t const* state)
81 {
82 	int err;
83 	pthread_mutex_lock(&g_lock);
84 	ALOGV("set_light_notifications, flashMode:%x, color:%x", state->flashMode, state->color);
85 	if(state->flashMode)
86 		err = write_int(MODE_RGB_FILE, 0x6ffffff);
87 	else
88 		err = write_int(MODE_RGB_FILE, 0x00);
89 	pthread_mutex_unlock(&g_lock);
90 	return err;
91 }
92 
set_light_backlight(struct light_device_t * dev,struct light_state_t const * state)93 static int set_light_backlight(struct light_device_t *dev,
94 		struct light_state_t const *state)
95 {
96 	int err;
97 	int brightness = rgb_to_brightness(state);
98 
99 	pthread_mutex_lock(&g_lock);
100 	err = write_int(BACKLIGHT_FILE, brightness);
101 	pthread_mutex_unlock(&g_lock);
102 
103 	return err;
104 }
105 
106 /** Close the lights device */
close_lights(struct light_device_t * dev)107 static int close_lights(struct light_device_t *dev)
108 {
109 	if (dev)
110 		free(dev);
111 	return 0;
112 }
113 
114 /** Open a new instance of a lights device using name */
open_lights(const struct hw_module_t * module,char const * name,struct hw_device_t ** device)115 static int open_lights(const struct hw_module_t *module, char const *name,
116 		struct hw_device_t **device)
117 {
118 	struct light_device_t *dev = malloc(sizeof(struct light_device_t));
119 	int (*set_light) (struct light_device_t *dev,
120 			struct light_state_t const *state);
121 	pthread_t lighting_poll_thread;
122 	ALOGV("open lights");
123 	if (dev == NULL) {
124 		ALOGE("failed to allocate memory");
125 		return -1;
126 	}
127 	memset(dev, 0, sizeof(*dev));
128 
129 	if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
130 		set_light = set_light_backlight;
131 	else if (0 == strcmp(LIGHT_ID_BATTERY, name))
132 		set_light = set_light_battery;
133 	else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
134 		set_light = set_light_notifications;
135 	else
136 		return -EINVAL;
137 
138 	pthread_mutex_init(&g_lock, NULL);
139 
140 	dev->common.tag = HARDWARE_DEVICE_TAG;
141 	dev->common.version = 0;
142 	dev->common.module = (struct hw_module_t *)module;
143 	dev->common.close = (int (*)(struct hw_device_t *))close_lights;
144 	dev->set_light = set_light;
145 
146 	*device = (struct hw_device_t *)dev;
147 
148 	return 0;
149 }
150 
151 static struct hw_module_methods_t lights_methods =
152 {
153 	.open =  open_lights,
154 };
155 
156 /*
157  * The backlight Module
158  */
159 struct hw_module_t HAL_MODULE_INFO_SYM =
160 {
161 	.tag = HARDWARE_MODULE_TAG,
162 	.version_major = 1,
163 	.version_minor = 0,
164 	.id = LIGHTS_HARDWARE_MODULE_ID,
165 	.name = "Flounder lights module",
166 	.author = "NVIDIA",
167 	.methods = &lights_methods,
168 };
169