042a4edbf7245f087ecb7552d5427f4ef846de70
[coreboot.git] / src / ec / lenovo / pmh7 / pmh7.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 Sven Schnelle <svens@stackframe.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <arch/io.h>
21 #include <console/console.h>
22 #include <device/device.h>
23 #include <device/pnp.h>
24 #include <stdlib.h>
25 #include "pmh7.h"
26 #include "chip.h"
27
28 void pmh7_backlight_enable(int onoff)
29 {
30         if (onoff)
31                 pmh7_register_set_bit(0x50, 5);
32         else
33                 pmh7_register_clear_bit(0x50, 5);
34 }
35
36 void pmh7_dock_event_enable(int onoff)
37 {
38         if (onoff)
39                 pmh7_register_set_bit(0x60, 3);
40         else
41                 pmh7_register_clear_bit(0x60, 3);
42
43 }
44 void pmh7_register_set_bit(int reg, int bit)
45 {
46         char val;
47
48         outb(reg, EC_LENOVO_PMH7_ADDR);
49         val = inb(EC_LENOVO_PMH7_DATA);
50         outb(reg, EC_LENOVO_PMH7_ADDR);
51         outb(val | (1 << bit), EC_LENOVO_PMH7_DATA);
52 }
53
54 void pmh7_register_clear_bit(int reg, int bit)
55 {
56         char val;
57
58         outb(reg, EC_LENOVO_PMH7_ADDR);
59         val = inb(EC_LENOVO_PMH7_DATA);
60         outb(reg, EC_LENOVO_PMH7_ADDR);
61         outb(val &= ~(1 << bit), EC_LENOVO_PMH7_DATA);
62 }
63
64 char pmh7_register_read(int reg)
65 {
66         outb(reg, EC_LENOVO_PMH7_ADDR);
67         return inb(EC_LENOVO_PMH7_DATA);
68 }
69
70 void pmh7_register_write(int reg, int val)
71 {
72         outb(reg, EC_LENOVO_PMH7_ADDR);
73         outb(val, EC_LENOVO_PMH7_DATA);
74 }
75
76 static void enable_dev(device_t dev)
77 {
78         struct ec_lenovo_pmh7_config *conf = dev->chip_info;
79         struct resource *resource;
80
81         resource = new_resource(dev, EC_LENOVO_PMH7_INDEX);
82         resource->flags = IORESOURCE_IO | IORESOURCE_FIXED;
83         resource->base = EC_LENOVO_PMH7_BASE;
84         resource->size = 16;
85         resource->align = 5;
86         resource->gran = 5;
87
88         pmh7_backlight_enable(conf->backlight_enable);
89         pmh7_dock_event_enable(conf->dock_event_enable);
90 }
91
92 struct chip_operations ec_lenovo_pmh7_ops = {
93         CHIP_NAME("Lenovo Power Management Hardware Hub 7")
94         .enable_dev = enable_dev,
95 };