1
0
mirror of git://projects.qi-hardware.com/iris.git synced 2024-11-17 01:40:36 +02:00
iris/boot-programs/gpio.ccp

357 lines
11 KiB
Plaintext
Raw Normal View History

2009-07-20 03:09:12 +03:00
#pypp 0
// Iris: micro-kernel for a capability-based operating system.
// boot-programs/gpio.ccp: GPIO driver, controlling all devices without special hardware.
// Copyright 2009 Bas Wijnen <wijnen@debian.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 3 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, see <http://www.gnu.org/licenses/>.
#include "devices.hh"
#define ARCH
#include "arch.hh"
2009-07-25 01:54:12 +03:00
// Interval between polls for keyboard (when keys are pressed) and battery/power (always) events
2009-07-25 23:00:32 +03:00
#define ALARM_INTERVAL (HZ / 1)
2009-07-25 01:54:12 +03:00
2009-07-20 03:09:12 +03:00
// GPIO pins for the devices (port.pin)
// keyboard
// Cols = 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15, 3.29
// Rows = 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7
// For some reason, it only works if the rows are input and the columns are output.
// interrupts: yes, with all columns set to output 0, the first key press can be detected as an interrupt.
// touchpad buttons
// Left: 0.16
// Right: 0.13
// interrupts: yes, for any change.
// Lock leds
// Num lock: 2.22
// Caps lock: 0.27
// Scroll lock: 0.9
// interrupts: no, output only.
// Power output (setting to 0 switches off the machine): 2.2
// interrupts: no, output only.
// Power button: 3.1 (note that this is also a keyboard column.)
// Battery presence and charge detection: 3.29 (note that this is also a keyboard column.)
// interrupts: no; it would be possible, but setting these to input makes it impossible to detect certain key presses as interrupts.
2009-07-21 13:17:52 +03:00
// interrupt summary
// Port 0: pin 0, 1, 2, 3, 4, 5, 6, 7: keyboard; 13, 16: touchpad
// Port 1: None.
// Port 2: None.
// Port 3: None.
enum event_type:
KEYBOARD_EVENT
TOUCHPAD_EVENT
POWERBUTTON_EVENT
BATTERY_EVENT
NUM_EVENTS
enum cap_type:
CAP_KEYBOARD = 32
CAP_TOUCHPAD
CAP_POWEROFF
CAP_POWERBUTTON
CAP_BATTERY
CAP_LOCKLEDS
CAP_PWM
static Capability cbs[NUM_EVENTS]
static void event (event_type type, unsigned data):
if !cbs[type]:
return
invoke_01 (cbs[type], data)
static void set_cb (event_type type, Capability cb):
if cbs[type]:
drop (cbs[type])
cbs[type] = cb
enum battery_type:
BATTERY_ABSENT
BATTERY_CHARGING
BATTERY_CHARGED
2009-07-20 03:09:12 +03:00
class Keyboard:
2009-07-25 23:00:32 +03:00
unsigned keys[GPIO_KBD_NUM_COLS]
void parse (unsigned col, unsigned data):
for unsigned row = 0; row < GPIO_KBD_NUM_ROWS; ++row:
if (data ^ keys[col]) & (1 << row):
unsigned code = (col << 3) | row
if data & (1 << row):
code |= 0x10000
event (KEYBOARD_EVENT, code)
keys[col] = data
2009-07-20 03:09:12 +03:00
public:
2009-07-25 23:00:32 +03:00
void scan ():
kdebug ("keyboard scan\n")
// Disable interrupts during scan.
GPIO_GPIER (GPIO_KBD_ROW_PORT) &= ~GPIO_KBD_ROW_MASK
// All columns are input.
GPIO_GPDIR (GPIO_KBD_COL_PORT) &= ~GPIO_KBD_COL_MASK
int const cols[GPIO_KBD_NUM_COLS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 29 }
unsigned dir = GPIO_GPDIR (GPIO_KBD_COL_PORT) & ~GPIO_KBD_COL_MASK
unsigned dat = GPIO_GPDR (GPIO_KBD_COL_PORT) & ~GPIO_KBD_COL_MASK
unsigned data
for unsigned col = 0; col < GPIO_KBD_NUM_COLS; ++col:
// clear pin
GPIO_GPDR (GPIO_KBD_COL_PORT) = dat
// output
GPIO_GPDIR (GPIO_KBD_COL_PORT) = dir | (1 << cols[col])
// Generate events of previous column. Do that now, so there is a short delay for the data to stabilize.
if col != 0:
parse (col - 1, data)
else:
// Add a short delay for stabilization.
parse (0, keys[0])
data = GPIO_GPDR (GPIO_KBD_ROW_PORT) & GPIO_KBD_ROW_MASK
// set pin
GPIO_GPDR (GPIO_KBD_COL_PORT) = dat | (1 << cols[col])
// input
GPIO_GPDIR (GPIO_KBD_COL_PORT) = dir
parse (GPIO_KBD_NUM_COLS - 1, data)
// set all to 0.
GPIO_GPDR (GPIO_KBD_COL_PORT) = dat
// set all to output.
GPIO_GPDIR (GPIO_KBD_COL_PORT) = dir | GPIO_KBD_COL_MASK
// clear pending interrupts.
for unsigned i = 0; i < 8; ++i:
GPIO_GPFR (GPIO_KBD_ROW_PORT) |= 1 << i
// Reenable interrupts.
GPIO_GPIER (GPIO_KBD_ROW_PORT) |= GPIO_KBD_ROW_MASK
2009-07-20 03:09:12 +03:00
Keyboard ():
2009-07-25 23:00:32 +03:00
// Set all columns to output without pull-ups when set as input.
GPIO_GPPUR (GPIO_KBD_COL_PORT) &= ~GPIO_KBD_COL_MASK
GPIO_GPDIR (GPIO_KBD_COL_PORT) |= GPIO_KBD_COL_MASK
2009-07-20 03:09:12 +03:00
// Set all rows to input and enable the pull-ups.
2009-07-25 23:00:32 +03:00
GPIO_GPPUR (GPIO_KBD_ROW_PORT) |= GPIO_KBD_ROW_MASK
GPIO_GPDIR (GPIO_KBD_ROW_PORT) &= ~GPIO_KBD_ROW_MASK
// Detect interrupts on falling edge.
for unsigned i = 0; i < GPIO_KBD_NUM_ROWS; ++i:
gpio_irq_fall (GPIO_KBD_ROW_PORT, i)
// Initialize matrix.
for unsigned i = 0; i < GPIO_KBD_NUM_COLS; ++i:
2009-07-20 03:09:12 +03:00
keys[i] = 0xff
2009-07-25 23:00:32 +03:00
// Perform initial scan to get real values into matrix and set up the rest.
2009-07-25 01:54:12 +03:00
scan ()
2009-07-20 03:09:12 +03:00
class Touchpad:
unsigned old_state
2009-07-21 13:17:52 +03:00
public:
void check_events ():
2009-07-25 23:00:32 +03:00
unsigned state = GPIO_GPDR (GPIO_TP_LEFT_PORT)
if state & (1 << GPIO_TP_LEFT):
gpio_irq_fall (GPIO_TP_LEFT_PORT, GPIO_TP_LEFT)
if (state ^ old_state) & (1 << GPIO_TP_LEFT):
2009-07-21 13:17:52 +03:00
event (TOUCHPAD_EVENT, 0)
2009-07-25 23:00:32 +03:00
else:
gpio_irq_rise (GPIO_TP_LEFT_PORT, GPIO_TP_LEFT)
if (state ^ old_state) & (1 << GPIO_TP_LEFT):
2009-07-21 13:17:52 +03:00
event (TOUCHPAD_EVENT, 0x10000)
2009-07-25 23:00:32 +03:00
if state & (1 << GPIO_TP_RIGHT):
gpio_irq_fall (GPIO_TP_RIGHT_PORT, GPIO_TP_RIGHT)
if (state ^ old_state) & (1 << GPIO_TP_RIGHT):
2009-07-21 13:17:52 +03:00
event (TOUCHPAD_EVENT, 1)
2009-07-25 23:00:32 +03:00
else:
gpio_irq_rise (GPIO_TP_RIGHT_PORT, GPIO_TP_RIGHT)
if (state ^ old_state) & (1 << GPIO_TP_RIGHT):
2009-07-21 13:17:52 +03:00
event (TOUCHPAD_EVENT, 0x10001)
2009-07-20 03:09:12 +03:00
old_state = state
Touchpad ():
// Set pins to input with pull-ups.
2009-07-25 01:54:12 +03:00
gpio_as_input (GPIO_TP_LEFT_PORT, GPIO_TP_LEFT)
gpio_as_input (GPIO_TP_RIGHT_PORT, GPIO_TP_RIGHT)
GPIO_GPPUR (0) |= (1 << GPIO_TP_LEFT) | (1 << GPIO_TP_RIGHT)
2009-07-20 03:09:12 +03:00
old_state = 0
2009-07-25 23:00:32 +03:00
// See if they are already pressed. Also set up interrupts.
2009-07-21 13:17:52 +03:00
check_events ()
2009-07-20 03:09:12 +03:00
// Now enable the interrupts.
class Lockleds:
// Note that num lock is in port 2. The others are in port 0.
public:
Lockleds ():
2009-07-25 01:54:12 +03:00
gpio_as_output (GPIO_NUM_PORT, GPIO_NUM)
gpio_as_output (GPIO_CAPS_PORT, GPIO_CAPS)
gpio_as_output (GPIO_SCROLL_PORT, GPIO_SCROLL)
GPIO_GPDR (GPIO_NUM_PORT) |= 1 << GPIO_NUM
GPIO_GPDR (GPIO_CAPS_PORT) |= 1 << GPIO_CAPS
GPIO_GPDR (GPIO_SCROLL_PORT) |= 1 << GPIO_SCROLL
2009-07-21 13:17:52 +03:00
void set (unsigned state):
if state & 4:
2009-07-25 01:54:12 +03:00
GPIO_GPDR (GPIO_NUM_PORT) &= ~(1 << GPIO_NUM)
2009-07-20 03:09:12 +03:00
else:
2009-07-25 01:54:12 +03:00
GPIO_GPDR (GPIO_NUM_PORT) |= 1 << GPIO_NUM
2009-07-21 13:17:52 +03:00
if state & 2:
2009-07-25 01:54:12 +03:00
GPIO_GPDR (GPIO_CAPS_PORT) &= ~(1 << GPIO_CAPS)
2009-07-20 03:09:12 +03:00
else:
2009-07-25 01:54:12 +03:00
GPIO_GPDR (GPIO_CAPS_PORT) |= 1 << GPIO_CAPS
2009-07-21 13:17:52 +03:00
if state & 1:
2009-07-25 01:54:12 +03:00
GPIO_GPDR (GPIO_SCROLL_PORT) &= ~(1 << GPIO_SCROLL)
2009-07-20 03:09:12 +03:00
else:
2009-07-25 01:54:12 +03:00
GPIO_GPDR (GPIO_SCROLL_PORT) |= 1 << GPIO_SCROLL
2009-07-20 03:09:12 +03:00
class Power:
// Power out is in port 2, the rest in port 3.
unsigned old_state
bool was_present
2009-07-21 13:17:52 +03:00
public:
2009-07-20 03:09:12 +03:00
void poll ():
2009-07-25 23:00:32 +03:00
#if 0
2009-07-20 03:09:12 +03:00
// Switch off keyboard interrupts, because this may interfere with them.
GPIO_GPIER (0) &= ~0xff
GPIO_GPDIR (3) &= ~(PWR_IN | BATTERY)
GPIO_GPPUR (3) &= ~(PWR_IN | BATTERY)
2009-07-25 01:54:12 +03:00
//udelay (100)
2009-07-20 03:09:12 +03:00
unsigned state = GPIO_GPDR (3)
if (state ^ old_state) & PWR_IN:
2009-07-21 13:17:52 +03:00
event (POWERBUTTON_EVENT, state & PWR_IN ? 0 : 0x10000)
2009-07-20 03:09:12 +03:00
if (state ^ old_state) & BATTERY:
if !(state & BATTERY):
GPIO_GPPUR (3) |= BATTERY
2009-07-25 01:54:12 +03:00
//udelay (100)
2009-07-20 03:09:12 +03:00
if GPIO_GPDR (3) & BATTERY:
if !was_present:
2009-07-21 13:17:52 +03:00
event (BATTERY_EVENT, BATTERY_CHARGED)
2009-07-20 03:09:12 +03:00
was_present = true
else:
if was_present:
2009-07-21 13:17:52 +03:00
event (BATTERY_EVENT, BATTERY_ABSENT)
2009-07-20 03:09:12 +03:00
was_present = false
2009-07-21 13:17:52 +03:00
else:
event (BATTERY_EVENT, BATTERY_CHARGING)
2009-07-20 03:09:12 +03:00
old_state = state
GPIO_GPPUR (3) &= ~BATTERY
GPIO_GPDIR (3) &= ~(PWR_IN | BATTERY)
2009-07-25 01:54:12 +03:00
//udelay (100)
2009-07-20 03:09:12 +03:00
GPIO_GPIER (3) |= 0xff
2009-07-25 23:00:32 +03:00
#endif
2009-07-20 03:09:12 +03:00
Power ():
was_present = true
2009-07-25 23:00:32 +03:00
//old_state = BATTERY
2009-07-20 03:09:12 +03:00
poll ()
void poweroff ():
2009-07-25 23:00:32 +03:00
// TODO: doesn't work.
i2c_open ()
i2c_write_page (I2C_DEV_MCU, I2C_MCU_SHUTDOWN, "\1", 1)
i2c_close ()
2009-07-20 03:09:12 +03:00
while true:
// Do nothing; wait until the device stops running.
2009-07-25 01:54:12 +03:00
void reboot ():
wdt_set_count (0xffffffff - 32)
wdt_start ()
while true:
// Do nothing; wait until the device reboots.
2009-07-20 03:09:12 +03:00
2009-07-21 13:17:52 +03:00
// Not really a gpio device, but it's so small, and uses gpio, so I include it here to avoid ipc.
class Pwm:
public:
Pwm ():
2009-07-25 23:00:32 +03:00
GPIO_GPDIR (GPIO_PWM_ENABLE_PORT) |= 1 << GPIO_PWM_ENABLE
2009-07-21 13:17:52 +03:00
PWM_PER (0) = 300
2009-07-25 23:00:32 +03:00
void set_backlight (unsigned level):
if level > 300:
level = 300
PWM_DUT (0) = level
if level:
PWM_CTR (0) = 0x80
GPIO_GPDR (GPIO_PWM_ENABLE_PORT) |= 1 << GPIO_PWM_ENABLE
2009-07-21 13:17:52 +03:00
else:
2009-07-25 23:00:32 +03:00
PWM_CTR (0) = 0x00
GPIO_GPDR (GPIO_PWM_ENABLE_PORT) &= ~(1 << GPIO_PWM_ENABLE)
2009-07-21 13:17:52 +03:00
// TODO: make it really work as a pwm instead of a switch; check if pwm1 is connected to anything.
2009-07-20 03:09:12 +03:00
int main ():
map_gpio ()
2009-07-21 13:17:52 +03:00
map_pwm0 ()
2009-07-25 01:54:12 +03:00
map_wdt ()
2009-07-25 23:00:32 +03:00
map_i2c ()
2009-07-20 03:09:12 +03:00
Keyboard kbd
Touchpad tp
2009-07-25 01:54:12 +03:00
Lockleds leds
2009-07-20 03:09:12 +03:00
Power power
2009-07-21 13:17:52 +03:00
Pwm pwm
2009-07-25 23:00:32 +03:00
// Enable interrupts. All are in port 0.
GPIO_GPIER (GPIO_KBD_ROW_PORT) = (1 << GPIO_TP_LEFT) | (1 << GPIO_TP_RIGHT) | GPIO_KBD_ROW_MASK
2009-07-21 13:17:52 +03:00
register_interrupt (IRQ_GPIO0)
Capability cap_kbd = receiver_create_capability (__my_receiver, CAP_KEYBOARD)
Capability cap_tp = receiver_create_capability (__my_receiver, CAP_TOUCHPAD)
Capability cap_poweroff = receiver_create_capability (__my_receiver, CAP_POWEROFF)
Capability cap_powerbutton = receiver_create_capability (__my_receiver, CAP_POWERBUTTON)
Capability cap_battery = receiver_create_capability (__my_receiver, CAP_BATTERY)
Capability cap_lockleds = receiver_create_capability (__my_receiver, CAP_LOCKLEDS)
Capability cap_pwm = receiver_create_capability (__my_receiver, CAP_PWM)
invoke_41 (__my_parent, cap_copy (cap_kbd), cap_copy (cap_tp), cap_copy (cap_poweroff), cap_copy (cap_powerbutton), INIT_SET_GPIO_0)
invoke_31 (__my_parent, cap_copy (cap_battery), cap_copy (cap_lockleds), cap_copy (cap_pwm), INIT_SET_GPIO_1)
2009-07-20 03:09:12 +03:00
2009-07-25 01:54:12 +03:00
receiver_set_alarm (__my_receiver, ALARM_INTERVAL)
2009-07-20 03:09:12 +03:00
while true:
2009-07-25 23:00:32 +03:00
schedule ()
2009-07-20 03:09:12 +03:00
Message msg
wait (&msg)
switch msg.protected_data:
2009-07-21 13:17:52 +03:00
case ~0:
// Alarm.
2009-07-25 23:00:32 +03:00
kdebug ("alarm\n")
// Periodically scan several devices.
kbd.scan ()
power.poll ()
2009-07-25 01:54:12 +03:00
receiver_set_alarm (__my_receiver, ALARM_INTERVAL)
2009-07-21 13:17:52 +03:00
break
2009-07-20 03:09:12 +03:00
case IRQ_GPIO0:
2009-07-25 23:00:32 +03:00
kdebug ("gpio interrupt\n")
//unsigned irq = GPIO_GPFR (0)
// Ack all.
GPIO_GPFR (0) = (1 << GPIO_TP_LEFT) | (1 << GPIO_TP_RIGHT) | GPIO_KBD_ROW_MASK
// Always scan keyboard and touchpad on any interrupt.
kbd.scan ()
tp.check_events ()
// Reregister the interrupt.
2009-07-25 01:54:12 +03:00
register_interrupt (IRQ_GPIO0)
break
2009-07-21 13:17:52 +03:00
case CAP_KEYBOARD:
set_cb (KEYBOARD_EVENT, msg.cap[0])
break
case CAP_TOUCHPAD:
set_cb (TOUCHPAD_EVENT, msg.cap[0])
break
case CAP_POWEROFF:
2009-07-25 01:54:12 +03:00
if msg.data[0]:
power.poweroff ()
else:
power.reboot ()
2009-07-21 13:17:52 +03:00
break
case CAP_POWERBUTTON:
set_cb (POWERBUTTON_EVENT, msg.cap[0])
break
case CAP_BATTERY:
set_cb (BATTERY_EVENT, msg.cap[0])
break
case CAP_LOCKLEDS:
2009-07-25 01:54:12 +03:00
leds.set (msg.data[0])
2009-07-21 13:17:52 +03:00
break
case CAP_PWM:
pwm.set_backlight (msg.data[0])
break