/*
 * gpio.c: KB3310 gpio functions for Asus Eeepc.
 * Copyright 2008 Mike Baker <mbm@openwrt.org>
 * 
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/io.h>

#define IOBASE_DEFAULT			(0x381)

unsigned short iobase = IOBASE_DEFAULT;

enum { // GPIO OFFSETS
	OutputFunctionSelect	= 0xFC00,
	OutputEnable		= 0xFC10,
	DataOutput		= 0xFC20,
	InputStatus		= 0xFC30,
	PullUp			= 0xFC40,
	OpenDrain		= 0xFC50,
	InputEnable		= 0xFC60,
	Misc			= 0xFC70,

	// GPIO INTERRUPTS
	EventEnable		= 0xFF30,
	PendingFlag		= 0xFF40,
	PolaritySelection	= 0xFF50,
	EdgeLevel		= 0xFF60,

};

void setecindex(unsigned short index)
{
	unsigned char hi = index>>8;
	unsigned char lo = index;

	outb(hi, iobase);
	outb(lo, iobase+1);
}

unsigned char readecdata(unsigned short index)
{
	unsigned char data;
	setecindex(index);
	data = inb(iobase+2);
	return data;
}

void writeecdata(unsigned short index, unsigned char data)
{
	setecindex(index);
	outb(data, iobase+2);
}

void setecdata(unsigned short index, unsigned char mask, unsigned char data)
{
	unsigned char val;
	val = readecdata(index);
	val &= mask;
	val |= data;
	writeecdata(index, val);
}

unsigned char gpiooffset(unsigned char gpio)
{
	return (gpio/8);
}

unsigned char gpiomask(unsigned char gpio)
{
	return (1<<(gpio%8));
}

void setgpio(unsigned char gpio, unsigned char val)
{
	unsigned short index = DataOutput + gpiooffset(gpio);
	unsigned char mask = gpiomask(gpio);
	val = (!!val) * mask; 
	setecdata(index, ~mask, val);
}

char *gpioname(unsigned char gpio)
{
	struct {
		unsigned char gpio;
		char *name;
	} *gpioname_p, gpioname_t[] = {
		{   0, "A20" },
		{   1, "KBRST#" },
		{  10, "Lid sense" },
		{  20, "Fan sense" },
		{  21, "Fan sense (?)" },
		{  61, "LCD backlight" },
		{  64, "AC sense" },
		{  66, "Battery sense" },
		{  77, "LCD backlight (?)" },
		{  78, "PS2 Mouse" },
		{  79, "PS2 Mouse" },
		{  82, "Charge Led" },
		{ 103, "Charge" },
		{ 0, 0 },
	};

	for (gpioname_p = gpioname_t; gpioname_p->name != 0; gpioname_p++) {
		if (gpioname_p->gpio == gpio) {
			return gpioname_p->name;
		}
	}

	return "unknown";
}

void gpiodump()
{
	unsigned char gpio;

	struct {
		unsigned short index;
		char *string;
	} *field_p, field_t[] = {
		{ OutputFunctionSelect, "OFS" },
		{ OutputEnable, "OE" },
		{ DataOutput, "DO" },
		{ InputStatus, "IS" },
		{ PullUp, "PU" },
		{ OpenDrain, "OD" },
		{ InputEnable, "IE" },
#if 0
		{ Misc, "MISC" },
		{ EventEnable, "EN" },
		{ PendingFlag, "PF" },
		{ PolaritySelection, "PS" },
		{ EdgeLevel, "EL" },
#endif
		{ 0, ""},
	};

	for (gpio = 0; gpio < 128; gpio++) {
		unsigned char offset = gpiooffset(gpio);
		unsigned char mask = gpiomask(gpio);

		printf("%03d(%02x %02x:%d) ", gpio, gpio, offset, gpio%8);

		for (field_p = field_t; field_p->index != 0; field_p++) {
			unsigned char val;
		       	val = readecdata(field_p->index + offset) & mask;
			printf("%s(%d) ", field_p->string, !!val);
		}

		printf("[%s]", gpioname(gpio));

		printf("\n");
	}
}

int main(int argc, char **argv)
{
	unsigned char val;

	if (iopl(3) < 0){
		perror("iop(3)");
		exit(1);
	}

	gpiodump();

	// 82: charge led
	//     0: on
	//     1: off
	//setgpio(82, 0);

	return 0;
}
