1/*
2 * Copyright (C) 2012 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#define LOGE ALOGE
19
20#include <cutils/log.h>
21
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
34static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
35
36static int write_int(char const *path, int value)
37{
38	int fd;
39	static int already_warned = -1;
40	fd = open(path, O_RDWR);
41	if (fd >= 0) {
42		char buffer[20];
43		int bytes = sprintf(buffer, "%d\n", value);
44		int amt = write(fd, buffer, bytes);
45		close(fd);
46		return amt == -1 ? -errno : 0;
47	} else {
48		if (already_warned == -1) {
49			LOGE("write_int failed to open %s\n", path);
50			already_warned = 1;
51		}
52		return -errno;
53	}
54}
55
56static int rgb_to_brightness(struct light_state_t const *state)
57{
58	int color = state->color & 0x00ffffff;
59	return ((77 * ((color >> 16) & 0x00ff))
60		+ (150 * ((color >> 8) & 0x00ff)) +
61		(29 * (color & 0x00ff))) >> 8;
62}
63
64static int set_light_backlight(struct light_device_t *dev,
65			       struct light_state_t const *state)
66{
67	int err = 0;
68	int brightness = rgb_to_brightness(state);
69	//+++ power save
70	int range_num = 1;
71	int old_brightness_range_indexs[] = {160,255};
72	int new_brightness_range_indexs[] = {160,182};
73	int old_brightness = brightness;
74	int i=0;
75	int old_brightness_in_range = -1;// 0:1~10 , 1:10~102 , 2:102~255
76	for(i=0;i<range_num;i++){
77		int t_range_min = old_brightness_range_indexs[i];
78		int t_range_max = old_brightness_range_indexs[i+1];
79		if( brightness>=t_range_min && brightness<=t_range_max){
80			old_brightness_in_range = i;
81			break;
82		}
83	}
84	if(old_brightness_in_range>=0){
85		int old_br_min = old_brightness_range_indexs[old_brightness_in_range];
86		int olb_br_max = old_brightness_range_indexs[old_brightness_in_range+1];
87		int new_br_min = new_brightness_range_indexs[old_brightness_in_range];
88		int new_br_max = new_brightness_range_indexs[old_brightness_in_range+1];
89		int new_brightness = new_br_min +
90				(new_br_max-new_br_min)*(old_brightness-old_br_min)/(olb_br_max-old_br_min);
91		brightness = new_brightness;
92	}
93	//---
94	pthread_mutex_lock(&g_lock);
95	err = write_int("/sys/class/backlight/pwm-backlight/brightness",
96			brightness);
97	pthread_mutex_unlock(&g_lock);
98
99	return err;
100}
101
102/** Close the lights device */
103static int close_lights(struct light_device_t *dev)
104{
105	if (dev)
106		free(dev);
107	return 0;
108}
109
110/** Open a new instance of a lights device using name */
111static int open_lights(const struct hw_module_t *module, char const *name,
112		       struct hw_device_t **device)
113{
114	pthread_t lighting_poll_thread;
115
116	int (*set_light) (struct light_device_t *dev,
117			  struct light_state_t const *state);
118
119	if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
120		set_light = set_light_backlight;
121	else
122		return -EINVAL;
123
124	pthread_mutex_init(&g_lock, NULL);
125
126	struct light_device_t *dev = malloc(sizeof(struct light_device_t));
127	memset(dev, 0, sizeof(*dev));
128
129	dev->common.tag = HARDWARE_DEVICE_TAG;
130	dev->common.version = 0;
131	dev->common.module = (struct hw_module_t *)module;
132	dev->common.close = (int (*)(struct hw_device_t *))close_lights;
133	dev->set_light = set_light;
134
135	*device = (struct hw_device_t *)dev;
136
137	return 0;
138}
139
140static struct hw_module_methods_t lights_methods =
141{
142	.open =  open_lights,
143};
144
145/*
146 * The backlight Module
147 */
148struct hw_module_t HAL_MODULE_INFO_SYM =
149{
150	.tag = HARDWARE_MODULE_TAG,
151	.version_major = 1,
152	.version_minor = 0,
153	.id = LIGHTS_HARDWARE_MODULE_ID,
154	.name = "NVIDIA Kai lights module",
155	.author = "NVIDIA",
156	.methods = &lights_methods,
157};
158