4c93d2525e113e16a14802b7269a2247cf8ecc56
[platform/adaptation/RPI3/device-manager-plugin-RPI3.git] / hw / display / display.c
1 /*
2  * device-node
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <linux/limits.h>
25
26 #include <hw/display.h>
27 #include "../shared.h"
28
29 #ifndef BACKLIGHT_PATH
30 #define BACKLIGHT_PATH  "/sys/class/backlight/rpi_backlight"
31 #endif
32
33 static int display_get_max_brightness(int *val)
34 {
35         static int max = -1;
36         int r;
37
38         if (!val)
39                 return -EINVAL;
40
41         if (max < 0) {
42                 r = sys_get_int(BACKLIGHT_PATH"/max_brightness", &max);
43                 if (r < 0)
44                         return r;
45         }
46
47         *val = max;
48         return 0;
49 }
50
51 static int display_get_brightness(int *brightness)
52 {
53         int r, v;
54
55         if (!brightness) {
56                 _E("wrong parameter");
57                 return -EINVAL;
58         }
59
60         r = sys_get_int(BACKLIGHT_PATH"/brightness", &v);
61         if (r < 0) {
62                 _E("fail to get brightness (errno:%d)", r);
63                 return r;
64         }
65
66         *brightness = v;
67         return 0;
68 }
69
70 static int display_set_brightness(int brightness)
71 {
72         int r, max;
73
74         r = display_get_max_brightness(&max);
75         if (r < 0) {
76                 _E("fail to get max brightness (errno:%d)", r);
77                 return r;
78         }
79
80         if (brightness < 0 || brightness > max) {
81                 _E("wrong parameter");
82                 return -EINVAL;
83         }
84
85         r = sys_set_int(BACKLIGHT_PATH"/brightness", brightness);
86         if (r < 0) {
87                 _E("fail to set brightness (errno:%d)", r);
88                 return r;
89         }
90
91         return 0;
92 }
93
94 static int display_open(struct hw_info *info,
95                 const char *id, struct hw_common **common)
96 {
97         struct display_device *display_dev;
98
99         if (!info || !common)
100                 return -EINVAL;
101
102         display_dev = calloc(1, sizeof(struct display_device));
103         if (!display_dev)
104                 return -ENOMEM;
105
106         display_dev->common.info = info;
107         display_dev->get_max_brightness = display_get_max_brightness;
108         display_dev->get_brightness = display_get_brightness;
109         display_dev->set_brightness = display_set_brightness;
110
111         *common = (struct hw_common *)display_dev;
112         return 0;
113 }
114
115 static int display_close(struct hw_common *common)
116 {
117         if (!common)
118                 return -EINVAL;
119
120         free(common);
121         return 0;
122 }
123
124 HARDWARE_MODULE_STRUCTURE = {
125         .magic = HARDWARE_INFO_TAG,
126         .hal_version = HARDWARE_INFO_VERSION,
127         .device_version = DISPLAY_HARDWARE_DEVICE_VERSION,
128         .id = DISPLAY_HARDWARE_DEVICE_ID,
129         .name = "Display",
130         .open = display_open,
131         .close = display_close,
132 };