Linus Nielsen Feltzing | 4feb0fc | 2006-02-23 15:24:45 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Linus Nielsen Feltzing |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Linus Nielsen Feltzing | 4feb0fc | 2006-02-23 15:24:45 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
Linus Nielsen Feltzing | 4feb0fc | 2006-02-23 15:24:45 +0000 | [diff] [blame] | 22 | #include "system.h" |
Michael Sevakis | 784dd78 | 2006-10-25 23:24:07 +0000 | [diff] [blame] | 23 | #include "kernel.h" |
| 24 | #include "pcf50606.h" |
Michael Sevakis | c4a0d45 | 2006-10-28 23:10:45 +0000 | [diff] [blame] | 25 | #include "button-target.h" |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 26 | #include "powermgmt.h" |
Linus Nielsen Feltzing | 4feb0fc | 2006-02-23 15:24:45 +0000 | [diff] [blame] | 27 | |
Linus Nielsen Feltzing | 4feb0fc | 2006-02-23 15:24:45 +0000 | [diff] [blame] | 28 | /* These voltages were determined by measuring the output of the PCF50606 |
| 29 | on a running X5, and verified by disassembling the original firmware */ |
| 30 | static void set_voltages(void) |
| 31 | { |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 32 | static const unsigned char buf[4] = |
| 33 | { |
| 34 | 0xf4, /* IOREGC = 2.9V, ON in all states */ |
| 35 | 0xf0, /* D1REGC = 2.5V, ON in all states */ |
Jens Arnold | 39e1b59 | 2007-03-15 19:08:24 +0000 | [diff] [blame] | 36 | #ifdef IAUDIO_M5 |
| 37 | 0xf8, /* D2REGC = 3.3V, ON in all states */ |
| 38 | #else |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 39 | 0xf6, /* D2REGC = 3.1V, ON in all states */ |
Jens Arnold | 39e1b59 | 2007-03-15 19:08:24 +0000 | [diff] [blame] | 40 | #endif |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 41 | 0xf4, /* D3REGC = 2.9V, ON in all states */ |
| 42 | }; |
Linus Nielsen Feltzing | 4feb0fc | 2006-02-23 15:24:45 +0000 | [diff] [blame] | 43 | |
| 44 | pcf50606_write_multiple(0x23, buf, 4); |
| 45 | } |
| 46 | |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 47 | static void init_pmu_interrupts(void) |
Linus Nielsen Feltzing | 4feb0fc | 2006-02-23 15:24:45 +0000 | [diff] [blame] | 48 | { |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 49 | /* inital data is interrupt masks */ |
| 50 | unsigned char data[3] = |
| 51 | { |
| 52 | ~0x04, /* unmask ONKEY1S */ |
| 53 | ~0x00, |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 54 | ~0x06, /* unmask ACDREM, ACDINS */ |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 55 | }; |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 56 | |
Michael Sevakis | c3b1475 | 2006-09-10 04:32:35 +0000 | [diff] [blame] | 57 | /* make sure GPI0 interrupt is off before unmasking anything */ |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 58 | and_l(~0xf, &INTPRI5); /* INT32 - Priority 0 (Off) */ |
Michael Sevakis | c3b1475 | 2006-09-10 04:32:35 +0000 | [diff] [blame] | 59 | |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 60 | /* unmask the PMU interrupts we want to service */ |
| 61 | pcf50606_write_multiple(0x05, data, 3); |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 62 | /* clear INT1-3 as these are left set after standby */ |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 63 | pcf50606_read_multiple(0x02, data, 3); |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 64 | |
Michael Sevakis | c3b1475 | 2006-09-10 04:32:35 +0000 | [diff] [blame] | 65 | /* Set to read pcf50606 INT but keep GPI0 off until init completes */ |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 66 | and_l(~0x00000001, &GPIO_ENABLE); |
| 67 | or_l(0x00000001, &GPIO_FUNCTION); |
| 68 | or_l(0x00000100, &GPIO_INT_EN); /* GPI0 H-L */ |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 69 | } |
Michael Sevakis | c3b1475 | 2006-09-10 04:32:35 +0000 | [diff] [blame] | 70 | |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 71 | static inline void enable_pmu_interrupts(void) |
| 72 | { |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 73 | /* clear pending GPI0 interrupts first or it may miss the first |
| 74 | H-L transition */ |
| 75 | or_l(0x00000100, &GPIO_INT_CLEAR); |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 76 | or_l(0x3, &INTPRI5); /* INT32 - Priority 3 */ |
| 77 | } |
| 78 | |
| 79 | void pcf50606_init(void) |
| 80 | { |
Michael Sevakis | 784dd78 | 2006-10-25 23:24:07 +0000 | [diff] [blame] | 81 | pcf50606_i2c_init(); |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 82 | |
| 83 | /* initialize pmu interrupts but don't service them yet */ |
| 84 | init_pmu_interrupts(); |
Michael Sevakis | c3b1475 | 2006-09-10 04:32:35 +0000 | [diff] [blame] | 85 | set_voltages(); |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 86 | |
Rani Hod | 3c810c4 | 2006-07-23 21:41:58 +0000 | [diff] [blame] | 87 | pcf50606_write(0x39, 0x00); /* GPOOD0 = green led OFF */ |
| 88 | pcf50606_write(0x3a, 0x00); /* GPOOD1 = red led OFF */ |
| 89 | |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 90 | /* Accessory detect */ |
Jens Arnold | 6adbbc0 | 2006-11-26 18:24:23 +0000 | [diff] [blame] | 91 | pcf50606_write(0x33, 0x8c); /* ACDAPE=1, THRSHLD=2.20V */ |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 92 | |
Michael Sevakis | c3b1475 | 2006-09-10 04:32:35 +0000 | [diff] [blame] | 93 | /* allow GPI0 interrupts from PMU now */ |
Michael Sevakis | b26e396 | 2006-10-25 06:10:22 +0000 | [diff] [blame] | 94 | enable_pmu_interrupts(); |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void pcf50606_reset_timeout(void) |
| 98 | { |
Michael Sevakis | af395f4 | 2008-03-26 01:50:41 +0000 | [diff] [blame] | 99 | int level = disable_irq_save(); |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 100 | pcf50606_write(0x08, pcf50606_read(0x08) | 0x02); /* OOCC1 - TOTRST=1 */ |
Michael Sevakis | af395f4 | 2008-03-26 01:50:41 +0000 | [diff] [blame] | 101 | restore_irq(level); |
Linus Nielsen Feltzing | 4feb0fc | 2006-02-23 15:24:45 +0000 | [diff] [blame] | 102 | } |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 103 | |
| 104 | /* Handles interrupts generated by the pcf50606 */ |
| 105 | void GPI0(void) __attribute__ ((interrupt_handler, section(".text"))); |
| 106 | void GPI0(void) |
| 107 | { |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 108 | unsigned char data[3]; /* 0 = INT1, 1 = INT2, 2 = INT3 */ |
| 109 | |
| 110 | /* Clear pending GPI0 interrupts */ |
| 111 | or_l(0x00000100, &GPIO_INT_CLEAR); |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 112 | |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 113 | /* clear pending interrupts from pcf50606 */ |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 114 | pcf50606_read_multiple(0x02, data, 3); |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 115 | |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 116 | if (data[0] & 0x04) |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 117 | { |
Michael Sevakis | 3bee89e | 2006-09-12 14:20:29 +0000 | [diff] [blame] | 118 | /* ONKEY1S */ |
| 119 | if (GPIO_READ & 0x02000000) |
| 120 | sys_poweroff(); /* main ONKEY */ |
| 121 | else |
| 122 | pcf50606_reset_timeout(); /* remote ONKEY */ |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 125 | if (data[2] & 0x06) |
| 126 | { |
| 127 | /* ACDINS/ACDREM */ |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 128 | /* Check if main buttons should be actually be scanned or not |
| 129 | - bias towards "yes" out of paranoia. */ |
Michael Sevakis | c4a0d45 | 2006-10-28 23:10:45 +0000 | [diff] [blame] | 130 | button_enable_scan((data[2] & 0x02) != 0 || |
| 131 | (pcf50606_read(0x33) & 0x01) != 0); |
Michael Sevakis | 1d4a9c6 | 2006-10-27 05:31:28 +0000 | [diff] [blame] | 132 | } |
Michael Sevakis | 32ba11f | 2006-09-09 07:49:25 +0000 | [diff] [blame] | 133 | } |