kms_fbc_crc: Use I915_TILING_X to create fbs
[platform/upstream/intel-gpu-tools.git] / tools / intel_lid.c
1 /*
2  * Copyright © 2009 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Zhenyu Wang <zhenyu.z.wang@intel.com>
25  *
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <pciaccess.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <dirent.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39
40 #include "intel_io.h"
41 #include "intel_reg.h"
42 #include "intel_bios.h"
43 #include "intel_chipset.h"
44
45 enum lid_status {
46         LID_UNKNOWN = -1,
47         LID_OPEN,
48         LID_CLOSE,
49 };
50
51 #define ACPI_BUTTON "/proc/acpi/button/"
52 #define ACPI_LID "/proc/acpi/button/lid/"
53
54 static int i830_lvds_acpi_lid_state(void)
55 {
56         int fd;
57         DIR *button_dir;
58         DIR *lid_dir;
59         struct dirent *lid_dent;
60         char *state_name;
61         char state[64];
62         enum lid_status ret = LID_UNKNOWN;
63
64         button_dir = opendir(ACPI_BUTTON);
65         /* If acpi button driver is not loaded, bypass ACPI check method */
66         if (button_dir == NULL)
67                 goto out;
68         closedir(button_dir);
69
70         lid_dir = opendir(ACPI_LID);
71
72         /* no acpi lid object found */
73         if (lid_dir == NULL)
74                 goto out;
75
76         while (1) {
77                 lid_dent = readdir(lid_dir);
78                 if (lid_dent == NULL) {
79                         /* no LID object */
80                         closedir(lid_dir);
81                         goto out;
82                 }
83                 if (strcmp(lid_dent->d_name, ".") &&
84                     strcmp(lid_dent->d_name, "..")) {
85                         break;
86                 }
87         }
88         state_name = malloc(strlen(ACPI_LID) + strlen(lid_dent->d_name) + 7);
89         memset(state_name, 0, strlen(ACPI_LID) + strlen(lid_dent->d_name) + 7);
90         strcat(state_name, ACPI_LID);
91         strcat(state_name, lid_dent->d_name);
92         strcat(state_name, "/state");
93
94         closedir(lid_dir);
95
96         if ((fd = open(state_name, O_RDONLY)) == -1) {
97                 free(state_name);
98                 goto out;
99         }
100         free(state_name);
101         if (read(fd, state, 64) == -1) {
102                 close(fd);
103                 goto out;
104         }
105         close(fd);
106         if (strstr(state, "open"))
107                 ret = LID_OPEN;
108         else if (strstr(state, "closed"))
109                 ret = LID_CLOSE;
110         else                    /* "unsupported" */
111                 ret = LID_UNKNOWN;
112
113 out:
114         return ret;
115 }
116
117 int main(int argc, char **argv)
118 {
119         int swf14, acpi_lid;
120
121         intel_mmio_use_pci_bar(intel_get_pci_device());
122
123         while (1) {
124                 swf14 = INREG(SWF14);
125
126                 printf("Intel LVDS Lid status:\n");
127                 printf("\tSWF14(0x%x) : %s\n", swf14,
128                        swf14 & SWF14_LID_SWITCH_EN ? "close" : "open");
129
130                 acpi_lid = i830_lvds_acpi_lid_state();
131                 switch (acpi_lid) {
132                 case LID_UNKNOWN:
133                         printf("\tACPI Lid state : unknown\n");
134                         break;
135                 case LID_OPEN:
136                         printf("\tACPI Lid state : open\n");
137                         break;
138                 case LID_CLOSE:
139                         printf("\tACPI Lid state : close\n");
140                         break;
141                 }
142                 sleep(2);
143         }
144         return 0;
145 }