Index: firmware/target/hosted/ypr0/ascodec-ypr0.h
===================================================================
--- firmware/target/hosted/ypr0/ascodec-ypr0.h	(revision 0)
+++ firmware/target/hosted/ypr0/ascodec-ypr0.h	(revision 0)
@@ -0,0 +1,98 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: ascodec-target.h 26116 2010-05-17 20:53:25Z funman $
+ *
+ * Module wrapper for AS3543 audio codec, using /dev/afe (afe.ko) of Samsung YP-R0
+ *
+ * Copyright (c) 2011 Lorenzo Miori
+ *
+ * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#ifndef _ASCODEC_TARGET_H
+#define _ASCODEC_TARGET_H
+
+/* ioctl parameter structs */
+
+struct codec_write_reg {
+            unsigned char reg;
+            unsigned char value;
+};
+
+struct codec_write_subreg {
+            unsigned char reg;
+            unsigned char subreg;
+            unsigned char value;
+};
+
+struct codec_read_subreg {
+            unsigned char reg;
+            unsigned char subreg;
+};
+
+struct codec_read_reg {
+            unsigned char reg;
+};
+
+int ascodec_init(void);
+void ascodec_close(void);
+int ascodec_write(unsigned int reg, unsigned int value);
+int ascodec_read(unsigned int reg);
+void ascodec_write_pmu(unsigned int index, unsigned int subreg, unsigned int value);
+int ascodec_read_pmu(unsigned int index, unsigned int subreg);
+
+/*
+static inline bool ascodec_chg_status(void)
+{
+    return ascodec_read(AS3514_IRQ_ENRD0) & CHG_STATUS;
+}
+
+static inline bool ascodec_endofch(void)
+{
+    return ascodec_read(AS3514_IRQ_ENRD0) & CHG_ENDOFCH;
+}
+
+static inline void ascodec_monitor_endofch(void)
+{
+    ascodec_write(AS3514_IRQ_ENRD0, IRQ_ENDOFCH);
+}
+
+static inline void ascodec_wait_adc_finished(void)
+{
+    *
+     * FIXME: not implemented
+     *
+     * If irqs are not available on the target platform,
+     * this should be most likely implemented by polling
+     * AS3514_IRQ_ENRD2 in the same way powermgmt-ascodec.c
+     * is polling IRQ_ENDOFCH.
+     *
+}
+
+static inline void ascodec_write_charger(int value)
+{
+    ascodec_write(AS3514_CHARGER, value);
+}
+
+static inline int ascodec_read_charger(void)
+{
+    return ascodec_read(AS3514_CHARGER);
+}
+
+extern void ascodec_suppressor_on(bool on);
+
+*/
+
+#endif /* !_ASCODEC_TARGET_H */
Index: firmware/target/hosted/ypr0/app/button-target.h
===================================================================
--- firmware/target/hosted/ypr0/app/button-target.h	(revision 30742)
+++ firmware/target/hosted/ypr0/app/button-target.h	(working copy)
@@ -24,40 +24,40 @@
 
 #include <stdbool.h>
 #include "config.h"
-/*
 
 #define HAS_BUTTON_HOLD
 
 bool button_hold(void);
-*/
 void button_init_device(void);
-#ifdef HAVE_BUTTON_DATA
-int button_read_device(int *data);
-#else
 int button_read_device(void);
-#endif
+/*TODO: to be implemented. There should be an irq somewhere */
+//void headphone_detect_event(void);
+//void headphone_init(void);
 
 /* Main unit's buttons */
+#define BUTTON_NONE         0x00000000
 #define BUTTON_UP           0x00000001
 #define BUTTON_DOWN         0x00000002
 #define BUTTON_LEFT         0x00000004
 #define BUTTON_RIGHT        0x00000008
-#define BUTTON_SELECT       0x00000010
+#define BUTTON_USER         0x00000010
+#define BUTTON_CENTER       0x00010000
 #define BUTTON_MENU         0x00000020
 #define BUTTON_BACK         0x00000040
 #define BUTTON_SCROLL_FWD   0x00000100
 #define BUTTON_SCROLL_BACK  0x00000200
+#define BUTTON_POWER        0x00000400
 
-/* Touch Screen Area Buttons */
-#define BUTTON_TOPLEFT      0x00001000
-#define BUTTON_TOPMIDDLE    0x00002000
-#define BUTTON_TOPRIGHT     0x00004000
-#define BUTTON_MIDLEFT      0x00008000
-#define BUTTON_CENTER       0x00010000
-#define BUTTON_MIDRIGHT     0x00020000
-#define BUTTON_BOTTOMLEFT   0x00040000
-#define BUTTON_BOTTOMMIDDLE 0x00080000
-#define BUTTON_BOTTOMRIGHT  0x00100000
+/* R0 physical key codes */
+#define R0BTN_F4            1  //PowerKey
+#define R0BTN_UP            2
+#define R0BTN_DOWN          3
+#define R0BTN_RIGHT         4
+#define R0BTN_LEFT          5
+#define R0BTN_CENTRAL       6
+#define R0BTN_F3            7  //ContextKey
+#define R0BTN_F2            8  //BackKey
+#define R0BTN_F1            11 //UserKey
 
 #define BUTTON_MAIN 0x1FFF
 
@@ -67,5 +67,8 @@
 /* Software power-off */
 #define POWEROFF_BUTTON BUTTON_POWER
 #define POWEROFF_COUNT 10
+/*FIXME-TODO!!!*/
+#define HOLD_BUTTON BUTTON_POWER
+#define HOLD_COUNT 5
                 
 #endif /* _BUTTON_TARGET_H_ */
Index: firmware/target/hosted/ypr0/app/button-application.c
===================================================================
--- firmware/target/hosted/ypr0/app/button-application.c	(revision 30742)
+++ firmware/target/hosted/ypr0/app/button-application.c	(working copy)
@@ -7,7 +7,7 @@
  *                     \/            \/     \/    \/            \/
  * $Id$
  *
- * Copyright (C) 2010 Thomas Martitz
+ * Copyright (C) 2011 Lorenzo Miori
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -25,74 +25,41 @@
 #include "button.h"
 #include "buttonmap.h"
 
-int key_to_button(int keyboard_key)
+int key_to_button(int keyboard_button)
 {
     int new_btn = BUTTON_NONE;
-    switch (keyboard_key)
+    switch (keyboard_button)
     {
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO4)
-        case SDLK_ESCAPE:
-#endif
-        case SDLK_KP7:
-            new_btn = BUTTON_TOPLEFT;
+        case R0BTN_LEFT:
+            new_btn = BUTTON_LEFT;
             break;
-        case SDLK_KP8:
-        case SDLK_UP:
-            new_btn = BUTTON_TOPMIDDLE;
+        case R0BTN_RIGHT:
+            new_btn = BUTTON_RIGHT;
             break;
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO4)
-        case SDLK_F7:
-#endif
-        case SDLK_KP9:
-            new_btn = BUTTON_TOPRIGHT;
+        case R0BTN_UP:
+            new_btn = BUTTON_UP;
             break;
-        case SDLK_KP4:
-        case SDLK_LEFT:
-            new_btn = BUTTON_MIDLEFT;
+        case R0BTN_DOWN:
+            new_btn = BUTTON_DOWN;
             break;
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO|PLATFORM_PANDORA)
-        case SDLK_RETURN:
-        case SDLK_KP_ENTER:
-#endif
-        case SDLK_KP5:
+        case R0BTN_CENTRAL:
             new_btn = BUTTON_CENTER;
             break;
-        case SDLK_KP6:
-        case SDLK_RIGHT:
-            new_btn = BUTTON_MIDRIGHT;
+        case R0BTN_F1:
+            new_btn = BUTTON_USER;
             break;
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO4)
-        case SDLK_F6:
-#endif
-        case SDLK_KP1:
-            new_btn = BUTTON_BOTTOMLEFT;
+        case R0BTN_F2:
+            new_btn = BUTTON_BACK;
             break;
-        case SDLK_KP2:
-        case SDLK_DOWN:
-            new_btn = BUTTON_BOTTOMMIDDLE;
+        case R0BTN_F3:
+            new_btn = BUTTON_MENU;
             break;
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO4)
-        case SDLK_F8:
-#endif
-        case SDLK_KP3:
-            new_btn = BUTTON_BOTTOMRIGHT;
+        case R0BTN_F4:
+            new_btn = BUTTON_POWER;
             break;
-#ifdef HAVE_SCROLLWHEEL
-        case SDL_BUTTON_WHEELUP:
-            new_btn = BUTTON_SCROLL_BACK;
-            break;
-        case SDL_BUTTON_WHEELDOWN:
-            new_btn = BUTTON_SCROLL_FWD;
-            break;
-#endif
-        case SDL_BUTTON_RIGHT:
-            new_btn = BUTTON_MIDLEFT;
-            break;
-        case SDL_BUTTON_MIDDLE:
-            new_btn = BUTTON_MIDRIGHT;
-            break;
         default:
             break;
     }
+
     return new_btn;
-}
+}
\ No newline at end of file
Index: firmware/target/hosted/ypr0/pcm-sdl.c
===================================================================
--- firmware/target/hosted/ypr0/pcm-sdl.c	(revision 30742)
+++ firmware/target/hosted/ypr0/pcm-sdl.c	(working copy)
@@ -417,6 +417,7 @@
 void pcm_set_mixer_volume(int volume)
 {
     sim_volume = volume;
+    /* volume | 0xc0*/
 }
 
 #endif /* CONFIG_CODEC == SWCODEC */
Index: firmware/target/hosted/ypr0/button-sdl.h
===================================================================
--- firmware/target/hosted/ypr0/button-sdl.h	(revision 30742)
+++ firmware/target/hosted/ypr0/button-sdl.h	(working copy)
@@ -28,13 +28,13 @@
 
 extern int sdl_app_has_input_focus;
 
-bool button_hold(void);
 #undef button_init_device
 void button_init_device(void);
-#ifdef HAVE_BUTTON_DATA
-int button_read_device(int *data);
-#else
+#undef button_close_device
+void button_close_device(void);
 int button_read_device(void);
+#ifdef HAS_BUTTON_HOLD
+bool button_hold(void);
 #endif
 
 #endif /* __BUTTON_SDL_H__ */
Index: firmware/target/hosted/ypr0/system-sdl.c
===================================================================
--- firmware/target/hosted/ypr0/system-sdl.c	(revision 30742)
+++ firmware/target/hosted/ypr0/system-sdl.c	(working copy)
@@ -47,16 +47,18 @@
 
 #endif
 
+#include "ascodec-ypr0.h"
+
 SDL_Surface    *gui_surface;
 
-bool            background = true;          /* use backgrounds by default */
+bool            background = false;          /* use backgrounds by default */
 #ifdef HAVE_REMOTE_LCD
 bool            showremote = true;          /* include remote by default */
 #endif
 bool            mapping = false;
 bool            debug_buttons = false;
-
-bool            lcd_display_redraw = true;  /* Used for player simulator */
+/* Don't know that. If it's related to continuos lcd refresh, well, disable it! we aren't using multi windows ;) */
+bool            lcd_display_redraw = false;  /* Used for player simulator */
 char            having_new_lcd = true;      /* Used for player simulator */
 bool            sim_alarm_wakeup = false;
 const char     *sim_root_dir = NULL;
@@ -84,23 +86,10 @@
 {
     SDL_InitSubSystem(SDL_INIT_VIDEO);
 
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO)
-    SDL_sem *wait_for_maemo_startup;
-#endif
-    SDL_Surface *picture_surface = NULL;
     int width, height;
     int depth;
     Uint32 flags;
 
-    /* Try and load the background image. If it fails go without */
-    if (background) {
-        picture_surface = SDL_LoadBMP("UI256.bmp");
-        if (picture_surface == NULL) {
-            background = false;
-            DEBUGF("warn: %s\n", SDL_GetError());
-        }
-    }
-    
     /* Set things up */
     if (background)
     {
@@ -127,67 +116,22 @@
     if (depth < 8)
         depth = 16;
 
-    flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
-#if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
-    /* Fullscreen mode for maemo app */
-    flags |= SDL_FULLSCREEN;
-#endif
+    flags = SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN;
 
     if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, depth, flags)) == NULL) {
         panicf("%s", SDL_GetError());
     }
 
-#if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
-    /* SDL touch screen fix: Work around a SDL assumption that returns
-       relative mouse coordinates when you get to the screen edges
-       using the touchscreen and a disabled mouse cursor.
-     */
-    uint8_t hiddenCursorData = 0;
-    SDL_Cursor *hiddenCursor = SDL_CreateCursor(&hiddenCursorData, &hiddenCursorData, 8, 1, 0, 0);
+    SDL_ShowCursor(SDL_DISABLE);
 
-    SDL_ShowCursor(SDL_ENABLE);
-    SDL_SetCursor(hiddenCursor);
-#endif
-
-    SDL_WM_SetCaption(UI_TITLE, NULL);
-
-    if (background && picture_surface != NULL)
-        SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
-
     /* let system_init proceed */
     SDL_SemPost((SDL_sem *)param);
 
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO)
-    /* Start maemo thread: Listen to display on/off events and battery monitoring */
-    wait_for_maemo_startup = SDL_CreateSemaphore(0); /* 0-count so it blocks */
-    SDL_Thread *maemo_thread = SDL_CreateThread(maemo_thread_func, wait_for_maemo_startup);
-#endif
-
     /*
      * finally enter the button loop */
-    gui_message_loop();
+//    gui_message_loop();
 
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO)
-    /* Ensure maemo thread is up and running */
-    SDL_SemWait(wait_for_maemo_startup);
-    SDL_DestroySemaphore(wait_for_maemo_startup);
 
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO5)
-    pcm_shutdown_gstreamer();
-#endif
-
-    g_main_loop_quit (maemo_main_loop);
-    g_main_loop_unref(maemo_main_loop);
-    SDL_WaitThread(maemo_thread, NULL);
-#endif
-
-#if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
-    SDL_FreeCursor(hiddenCursor);
-#endif
-
-    if(picture_surface)
-        SDL_FreeSurface(picture_surface);
-
     /* Order here is relevent to prevent deadlocks and use of destroyed
        sync primitives by kernel threads */
 #ifdef HAVE_SDL_THREADS
@@ -220,6 +164,10 @@
 #else
     sim_do_exit();
 #endif
+    
+    /* Something that we need to do before exit on our platform YPR0 */
+    
+    ascodec_close();
 }
 
 void sim_do_exit()
@@ -238,12 +186,6 @@
     /* fake stack, OS manages size (and growth) */
     stackbegin = stackend = (uintptr_t*)&s;
 
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO)
-    /* Make glib thread safe */
-    g_thread_init(NULL);
-    g_type_init();
-#endif
-
     if (SDL_Init(SDL_INIT_TIMER))
         panicf("%s", SDL_GetError());
 
@@ -256,6 +198,10 @@
     SDL_SemWait(s);
     /* cleanup */
     SDL_DestroySemaphore(s);
+    
+    /* Here begins our platform specific initilization for various things */
+    
+    ascodec_init();
 }
 
 
@@ -273,6 +219,46 @@
     system_reboot();
 }
 
+#ifdef HAVE_ADJUSTABLE_CPU_FREQ
+/* This is the Linux Kernel CPU governor... */
+int set_cpu_freq(int speed)
+    {
+        FILE * cpu_dev;
+        cpu_dev = fopen ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed","w");
+        if (cpu_dev == NULL)
+            {
+                return 0;
+            }
+        else
+            {
+                char temp[10];
+                sprintf(temp, "%i", speed);
+                fputs (temp, cpu_dev);
+                fclose (cpu_dev);
+                return 1;
+            }
+    }
+
+void set_cpu_frequency(long frequency)
+{
+    switch (frequency)
+    {
+        case CPUFREQ_MAX:
+            set_cpu_freq(532000);
+            cpu_frequency = CPUFREQ_MAX;
+            break;
+        case CPUFREQ_NORMAL:
+            set_cpu_freq(400000);
+            cpu_frequency = CPUFREQ_NORMAL;
+            break;
+        default:
+            set_cpu_freq(200000);
+            cpu_frequency = CPUFREQ_DEFAULT;
+            break;
+    }
+}
+#endif
+
 void sys_handle_argv(int argc, char *argv[])
 {
     if (argc >= 1) 
@@ -346,7 +332,7 @@
             }
             else 
             {
-                printf("rockboxui\n");
+                printf("rockbox\n");
                 printf("Arguments:\n");
 #ifdef DEBUG
                 printf("  --debugaudio \t Write raw PCM data to audiodebug.raw\n");
Index: firmware/target/hosted/ypr0/backlight-target.c
===================================================================
--- firmware/target/hosted/ypr0/backlight-target.c	(revision 0)
+++ firmware/target/hosted/ypr0/backlight-target.c	(revision 0)
@@ -0,0 +1,67 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: backlight-gigabeat-s.c 25800 2010-05-04 10:07:53Z jethead71 $
+ *
+ * Copyright (C) 2011 by Lorenzo Miori
+ *
+ * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include "config.h"
+#include "system.h"
+#include "backlight.h"
+#include "backlight-target.h"
+#include "lcd.h"
+#include "as3514.h"
+#include "ascodec-ypr0.h"
+
+static bool backlight_on_status = true; /* Is on or off? */
+
+bool _backlight_init(void)
+{
+    return true;
+}
+
+void _backlight_on(void)
+{
+
+    if (!backlight_on_status)
+    {
+        /* Original app sets this to 0xb1 when backlight is on... */
+        ascodec_write_pmu(AS3543_BACKLIGHT, 0x1, 0xb1);
+    }
+
+    backlight_on_status = true;
+
+}
+
+void _backlight_off(void)
+{
+    if (backlight_on_status) {
+        ascodec_write_pmu(AS3543_BACKLIGHT, 0x1, 0x00);
+    }
+
+    backlight_on_status = false;
+}
+
+
+
+void _backlight_set_brightness(int brightness)
+{
+    if (brightness > MAX_BRIGHTNESS_SETTING)
+        brightness = MAX_BRIGHTNESS_SETTING;
+    if (brightness < MIN_BRIGHTNESS_SETTING)
+        brightness = MIN_BRIGHTNESS_SETTING;
+    ascodec_write_pmu(AS3543_BACKLIGHT, 0x3, brightness << 3 & 0xf8);
+}
\ No newline at end of file
Index: firmware/target/hosted/ypr0/backlight-target.h
===================================================================
--- firmware/target/hosted/ypr0/backlight-target.h	(revision 0)
+++ firmware/target/hosted/ypr0/backlight-target.h	(revision 0)
@@ -0,0 +1,31 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: backlight-target.h 19322 2008-12-04 04:16:53Z jethead71 $
+ *
+ * Copyright (C) 2006 by Linus Nielsen Feltzing
+ *
+ * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#ifndef BACKLIGHT_TARGET_H
+#define BACKLIGHT_TARGET_H
+
+bool _backlight_init(void);
+void _backlight_on(void);
+void _backlight_off(void);
+void _backlight_set_brightness(int brightness);
+
+void _backlight_lcd_sleep(void);
+
+#endif /* BACKLIGHT_TARGET_H */
Index: firmware/target/hosted/ypr0/lcd-sdl.c
===================================================================
--- firmware/target/hosted/ypr0/lcd-sdl.c	(revision 30742)
+++ firmware/target/hosted/ypr0/lcd-sdl.c	(working copy)
@@ -152,4 +152,4 @@
      * directly; for now assume the more or less standard 96 */
     return 96;
 #endif
-}
+}
\ No newline at end of file
Index: firmware/target/hosted/ypr0/lcd-sdl.h
===================================================================
--- firmware/target/hosted/ypr0/lcd-sdl.h	(revision 30742)
+++ firmware/target/hosted/ypr0/lcd-sdl.h	(working copy)
@@ -39,5 +39,9 @@
 void sdl_set_gradient(SDL_Surface *surface, SDL_Color *start, SDL_Color *end,
                       int first, int steps);
 
+#ifdef HAVE_LCD_FLIP
+void lcd_set_flip(bool yesno);
+#endif
+
 #endif /* #ifndef __LCDSDL_H__ */
 
Index: firmware/target/hosted/ypr0/ascodec-ypr0.c
===================================================================
--- firmware/target/hosted/ypr0/ascodec-ypr0.c	(revision 0)
+++ firmware/target/hosted/ypr0/ascodec-ypr0.c	(revision 0)
@@ -0,0 +1,101 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: ascodec-target.h 26116 2010-05-17 20:53:25Z funman $
+ *
+ * Module wrapper for AS3543 audio codec, using /dev/afe (afe.ko) of Samsung YP-R0
+ *
+ * Copyright (c) 2011 Lorenzo Miori
+ *
+ * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include "fcntl.h"
+#include "unistd.h"
+#include "stdio.h"
+#include "string.h"
+#include "sys/ioctl.h"
+#include "stdlib.h"
+
+#include "ascodec-ypr0.h"
+
+int afe_dev = -1;
+
+static int IOCTL_REG_WRITE = 0x40034101;
+/* Write to a PMU register */
+static int IOCTL_SUBREG_WRITE = 0x40034103;
+static int IOCTL_REG_READ = 0x80034102;
+/* Read from a PMU register */
+static int IOCTL_SUBREG_READ = 0x80034103;
+
+int ascodec_init(void) {
+
+    afe_dev = open("/dev/afe", O_RDWR);
+    return afe_dev;
+}
+
+void ascodec_close(void) {
+
+    if (afe_dev >= 0) {
+        close(afe_dev);
+    }
+}
+
+int ascodec_write(unsigned int reg, unsigned int value)
+{
+    int retval = -1;
+    struct codec_write_reg y;
+    struct codec_write_reg *p;
+    p = &y;
+    p->reg = reg;
+    p->value = value;
+    retval = ioctl(afe_dev, IOCTL_REG_WRITE, p);
+    return retval;
+}
+
+int ascodec_read(unsigned int reg)
+{
+    int retval = -1;
+    struct codec_write_reg y;
+    struct codec_write_reg *p;
+    p = &y;
+    p->reg = reg;
+    retval = ioctl(afe_dev, IOCTL_REG_READ, p);
+    return retval;
+}
+
+void ascodec_write_pmu(unsigned int index, unsigned int subreg,
+                                     unsigned int value)
+{
+    int retval = -1;
+    struct codec_write_subreg y;
+    struct codec_write_subreg *p;
+    p = &y;
+    p->reg = index;
+    p->subreg = subreg;
+    p->value = value;
+    retval = ioctl(afe_dev, IOCTL_SUBREG_WRITE, p);
+}
+
+int ascodec_read_pmu(unsigned int index, unsigned int subreg)
+{
+    int retval = -1;
+    struct codec_read_subreg y;
+    struct codec_read_subreg *p;
+    p = &y;
+    p->reg = index;
+    p->subreg = subreg;
+    retval = ioctl(afe_dev, IOCTL_SUBREG_READ, p);
+    return retval;
+}
\ No newline at end of file
Index: firmware/target/hosted/ypr0/button-sdl.c
===================================================================
--- firmware/target/hosted/ypr0/button-sdl.c	(revision 30742)
+++ firmware/target/hosted/ypr0/button-sdl.c	(working copy)
@@ -34,10 +34,15 @@
 #include "buttonmap.h"
 #include "debug.h"
 
-#ifdef HAVE_TOUCHSCREEN
-#include "touchscreen.h"
-static int mouse_coords = 0;
-#endif
+//#include <stdio.h>
+//#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+static int r0Btn_fd = 0;
+static int r0_read_key(void);
+//static int btn = 0;    /* Hopefully keeps track of currently pressed keys... */
+
 /* how long until repeat kicks in */
 #define REPEAT_START      6
 
@@ -53,24 +58,11 @@
 #define USB_KEY SDLK_u
 #endif
 
-#if defined(IRIVER_H100_SERIES) || defined (IRIVER_H300_SERIES)
-int _remote_type=REMOTETYPE_H100_LCD;
+//struct event_queue button_queue;
 
-int remote_type(void)
-{
-    return _remote_type;
-}
-#endif
-
-struct event_queue button_queue;
-
-static int btn = 0;    /* Hopefully keeps track of currently pressed keys... */
-
 int sdl_app_has_input_focus = 1;
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO)
-static int n900_updown_key_pressed = 0;
-#endif
 
+/*TODO: Implement the button hold of R0, see what do this stuff means */
 #ifdef HAS_BUTTON_HOLD
 bool hold_button_state = false;
 bool button_hold(void) {
@@ -78,142 +70,12 @@
 }
 #endif
 
-#ifdef HAS_REMOTE_BUTTON_HOLD
-bool remote_hold_button_state = false;
-bool remote_button_hold(void) {
-    return remote_hold_button_state;
-}
-#endif
-static void button_event(int key, bool pressed);
+//static void button_event(int key, bool pressed);
 extern bool debug_wps;
 extern bool mapping;
 
-#ifdef HAVE_TOUCHSCREEN
-static void touchscreen_event(int x, int y)
-{
-    if(background) {
-        x -= UI_LCD_POSX;
-        y -= UI_LCD_POSY;
-    }
-
-    if(x >= 0 && y >= 0 && x < SIM_LCD_WIDTH && y < SIM_LCD_HEIGHT) {
-        mouse_coords = (x << 16) | y;
-        button_event(BUTTON_TOUCHSCREEN, true);
-        if (debug_wps)
-            printf("Mouse at 1: (%d, %d)\n", x, y);
-    }
-}
-#endif
-
-static void mouse_event(SDL_MouseButtonEvent *event, bool button_up)
-{
-#define SQUARE(x) ((x)*(x))
-    static int x,y;
-#ifdef SIMULATOR
-    static int xybutton = 0;
-#endif
-
-    if(button_up) {
-        switch ( event->button )
-        {
-#ifdef HAVE_SCROLLWHEEL
-        case SDL_BUTTON_WHEELUP:
-        case SDL_BUTTON_WHEELDOWN:
-#endif
-        case SDL_BUTTON_MIDDLE:
-        case SDL_BUTTON_RIGHT:
-            button_event( event->button, false );
-            break;
-        /* The scrollwheel button up events are ignored as they are queued immediately */
-        case SDL_BUTTON_LEFT:
-            if ( mapping && background ) {
-                printf("    { SDLK_,     %d, %d, %d, \"\" },\n", x, y,
-                (int)sqrt( SQUARE(x-(int)event->x) + SQUARE(y-(int)event->y))
-                );
-            }
-#ifdef SIMULATOR
-            if ( background && xybutton ) {
-                    button_event( xybutton, false );
-                    xybutton = 0;
-                }
-#endif
-#ifdef HAVE_TOUCHSCREEN
-                else
-                    button_event(BUTTON_TOUCHSCREEN, false);
-#endif
-            break;
-        }
-    } else {    /* button down */
-        switch ( event->button )
-        {
-#ifdef HAVE_SCROLLWHEEL
-        case SDL_BUTTON_WHEELUP:
-        case SDL_BUTTON_WHEELDOWN:
-#endif
-        case SDL_BUTTON_MIDDLE:
-        case SDL_BUTTON_RIGHT:
-            button_event( event->button, true );
-            break;
-        case SDL_BUTTON_LEFT:
-            if ( mapping && background ) {
-                x = event->x;
-                y = event->y;
-            }
-#ifdef SIMULATOR
-            if ( background ) {
-                xybutton = xy2button( event->x, event->y );
-                if( xybutton ) {
-                    button_event( xybutton, true );
-                    break;
-                }
-            }
-#endif
-#ifdef HAVE_TOUCHSCREEN
-            touchscreen_event(event->x, event->y);
-#endif
-            break;
-        }
-
-        if (debug_wps && event->button == SDL_BUTTON_LEFT)
-        {
-            int m_x, m_y;
-
-            if ( background )
-            {
-                m_x = event->x - 1;
-                m_y = event->y - 1;
-#ifdef HAVE_REMOTE
-                if ( event->y >= UI_REMOTE_POSY ) /* Remote Screen */
-                {
-                    m_x -= UI_REMOTE_POSX;
-                    m_y -= UI_REMOTE_POSY;
-                }
-                else
-#endif
-                {
-                    m_x -= UI_LCD_POSX;
-                    m_y -= UI_LCD_POSY;
-                }
-            }
-            else
-            {
-                m_x = event->x;
-                m_y = event->y;
-#ifdef HAVE_REMOTE
-                if ( m_y >= LCD_HEIGHT ) /* Remote Screen */
-                    m_y -= LCD_HEIGHT;
-#endif
-            }
-
-            printf("Mouse at 2: (%d, %d)\n", m_x, m_y);
-        }
-    }
-#undef SQUARE
-}
-
 static bool event_handler(SDL_Event *event)
 {
-    SDLKey ev_key;
 
     switch(event->type)
     {
@@ -226,53 +88,6 @@
                 sdl_app_has_input_focus = 0;
         }
         break;
-    case SDL_KEYDOWN:
-    case SDL_KEYUP:
-        ev_key = event->key.keysym.sym;
-#if (CONFIG_PLATFORM & PLATFORM_MAEMO5)
-        /* N900 with shared up/down cursor mapping. Seen on the German,
-           Finnish, Italian, French and Russian version. Probably more. */
-        if (event->key.keysym.mod & KMOD_MODE || n900_updown_key_pressed)
-        {
-            /* Prevent stuck up/down keys: If you release the ALT key before the cursor key,
-               rockbox will see a KEYUP event for left/right instead of up/down and
-               the previously pressed up/down key would stay active. */
-            if (ev_key == SDLK_LEFT || ev_key == SDLK_RIGHT)
-            {
-                if (event->type == SDL_KEYDOWN)
-                    n900_updown_key_pressed = 1;
-                else
-                    n900_updown_key_pressed = 0;
-            }
-
-            if (ev_key == SDLK_LEFT)
-                ev_key = SDLK_UP;
-            else if (ev_key == SDLK_RIGHT)
-                ev_key = SDLK_DOWN;
-        }
-#endif
-        button_event(ev_key, event->type == SDL_KEYDOWN);
-        break;
-#ifdef HAVE_TOUCHSCREEN
-    case SDL_MOUSEMOTION:
-        if (event->motion.state & SDL_BUTTON(1))
-        {
-            int x = event->motion.x / display_zoom;
-            int y = event->motion.y / display_zoom;
-            touchscreen_event(x, y);
-        }
-        break;
-#endif
-
-    case SDL_MOUSEBUTTONUP:
-    case SDL_MOUSEBUTTONDOWN:
-    {
-        SDL_MouseButtonEvent *mev = &event->button;
-        mev->x /= display_zoom;
-        mev->y /= display_zoom;
-        mouse_event(mev, event->type == SDL_MOUSEBUTTONUP);
-        break;
-    }
     case SDL_QUIT:
         /* Will post SDL_USEREVENT in shutdown_hw() if successful. */
         sys_poweroff();
@@ -304,160 +119,78 @@
     } while(!quit);
 }
 
-static void button_event(int key, bool pressed)
+int button_read_device(void)
 {
-    int new_btn = 0;
-    static bool usb_connected = false;
-    if (usb_connected && key != USB_KEY)
-        return;
-    switch (key)
-    {
-    case USB_KEY:
-        if (!pressed)
-        {
-            usb_connected = !usb_connected;
-            if (usb_connected)
-                queue_post(&button_queue, SYS_USB_CONNECTED, 0);
-            else
-                queue_post(&button_queue, SYS_USB_DISCONNECTED, 0);
-        }
-        return;
+#ifdef HAS_BUTTON_HOLD
+    int hold_button = button_hold();
 
-#if (CONFIG_PLATFORM & PLATFORM_PANDORA)
-    case SDLK_LCTRL:
-        /* Will post SDL_USEREVENT in shutdown_hw() if successful. */
-        sys_poweroff();
-        break;
+#ifdef HAVE_BACKLIGHT
+    /* light handling */
+    static int hold_button_old = false;
+    if (hold_button != hold_button_old)
+    {
+        hold_button_old = hold_button;
+        backlight_hold_changed(hold_button);
+    }
 #endif
-#ifdef HAS_BUTTON_HOLD
-    case SDLK_h:
-        if(pressed)
-        {
-            hold_button_state = !hold_button_state;
-            DEBUGF("Hold button is %s\n", hold_button_state?"ON":"OFF");
-        }
-        return;
-#endif
-        
-#ifdef HAS_REMOTE_BUTTON_HOLD
-    case SDLK_j:
-        if(pressed)
-        {
-            remote_hold_button_state = !remote_hold_button_state;
-            DEBUGF("Remote hold button is %s\n",
-                   remote_hold_button_state?"ON":"OFF");
-        }
-        return;
-#endif
 
-#if defined(IRIVER_H100_SERIES) || defined (IRIVER_H300_SERIES)
-    case SDLK_t:
-        if(pressed)
-            switch(_remote_type)
-            {
-                case REMOTETYPE_UNPLUGGED: 
-                    _remote_type=REMOTETYPE_H100_LCD;
-                    DEBUGF("Changed remote type to H100\n");
-                    break;
-                case REMOTETYPE_H100_LCD:
-                    _remote_type=REMOTETYPE_H300_LCD;
-                    DEBUGF("Changed remote type to H300\n");
-                    break;
-                case REMOTETYPE_H300_LCD:
-                    _remote_type=REMOTETYPE_H300_NONLCD;
-                    DEBUGF("Changed remote type to H300 NON-LCD\n");
-                    break;
-                case REMOTETYPE_H300_NONLCD:
-                    _remote_type=REMOTETYPE_UNPLUGGED;
-                    DEBUGF("Changed remote type to none\n");
-                    break;
-            }
-        break;
+    if (hold_button)
+        return BUTTON_NONE;
 #endif
-    case SDLK_KP0:
-    case SDLK_F5:
-        if(pressed)
-        {
-            sim_trigger_screendump();
-            return;
-        }
-        break;
-#ifdef HAVE_TOUCHSCREEN
-    case SDLK_F4:
-        if(pressed)
-        {
-            touchscreen_set_mode(touchscreen_get_mode() == TOUCHSCREEN_POINT ? TOUCHSCREEN_BUTTON : TOUCHSCREEN_POINT);
-            printf("Touchscreen mode: %s\n", touchscreen_get_mode() == TOUCHSCREEN_POINT ? "TOUCHSCREEN_POINT" : "TOUCHSCREEN_BUTTON");
-        }
-#endif
-    default:
-#ifdef HAVE_TOUCHSCREEN
-        new_btn = key_to_touch(key, mouse_coords);
-        if (!new_btn)
-#endif
-            new_btn = key_to_button(key);
-        break;
-    }
-    /* Call to make up for scrollwheel target implementation.  This is
-     * not handled in the main button.c driver, but on the target
-     * implementation (look at button-e200.c for example if you are trying to 
-     * figure out why using button_get_data needed a hack before).
-     */
-#if defined(BUTTON_SCROLL_FWD) && defined(BUTTON_SCROLL_BACK)
-    if((new_btn == BUTTON_SCROLL_FWD || new_btn == BUTTON_SCROLL_BACK) && 
-        pressed)
+
+    int key = 0;
+    int new_btn = 0;
+    key = r0_read_key();
+
+    new_btn = key_to_button(key);
+    if (new_btn != 0)
     {
-        /* Clear these buttons from the data - adding them to the queue is
-         *  handled in the scrollwheel drivers for the targets.  They do not
-         *  store the scroll forward/back buttons in their button data for
-         *  the button_read call.
-         */
 #ifdef HAVE_BACKLIGHT
         backlight_on();
 #endif
-#ifdef HAVE_BUTTON_LIGHT
-        buttonlight_on();
-#endif
-        queue_post(&button_queue, new_btn, 1<<24);
-        new_btn &= ~(BUTTON_SCROLL_FWD | BUTTON_SCROLL_BACK);
     }
-#endif
-
+/*        
     if (pressed)
         btn |= new_btn;
     else
         btn &= ~new_btn;
+*/
+    return new_btn;
 }
-#if defined(HAVE_BUTTON_DATA) && defined(HAVE_TOUCHSCREEN)
-int button_read_device(int* data)
+
+int r0_read_key()
 {
-    *data = mouse_coords;
-#else
-int button_read_device(void)
-{
-#endif
-#ifdef HAS_BUTTON_HOLD
-    int hold_button = button_hold();
+    unsigned char keys;
+    int key = 0;
 
-#ifdef HAVE_BACKLIGHT
-    /* light handling */
-    static int hold_button_old = false;
-    if (hold_button != hold_button_old)
+    if (r0Btn_fd < 0)
+        return 0;
+    /* r0Btn device gives us 4 bytes "chucks". We read 1 byte a time, then RB will avoid for us 0 bytes */
+    if (read(r0Btn_fd, &keys, 1))
     {
-        hold_button_old = hold_button;
-        backlight_hold_changed(hold_button);
+        key = (int)keys;
+    } else {
+        key = 0;
     }
-#endif
 
-    if (hold_button)
-        return BUTTON_NONE;
-    else
-#endif
-
-    return btn;
+    return key;
 }
 
 void button_init_device(void)
 {
-    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
+    r0Btn_fd = open("/dev/r0Btn", O_RDONLY);
+    if (r0Btn_fd < 0) {
+        printf("/dev/r0Btn open error!");
+   }
 }
+
+#ifdef BUTTON_DRIVER_CLOSE
+/*FIXME: I'm not sure it's called at shutdown...give a check! */
+void button_close_device(void)
+{
+    if (r0Btn_fd < 0) {
+        close(r0Btn_fd);
+        printf("/dev/r0Btn closed!");
+    }
+}
+#endif /* BUTTON_DRIVER_CLOSE */
\ No newline at end of file
