CAM bug

I recently had a discussion with someone regarding the benefits of screen-off profiles/daemons that put a limit on the CPU frequency while the screen is switched off for the sake of reducing the power consumption and thus prolonging the battery runtime. My argument was that putting a hard limit on the CPU frequency would rather have a detrimental effect of the battery runtime since this would hinder the CPU from racing-to-idle. I also argued that due to this race-to-idle effect, in general restricting the CPU frequency is not beneficial to the battery runtime and in contrast to intuition overclocking should reduce the average battery drain (as long as no disproportionate increase in voltage is necessary).Since this is just theory and Morfic noted that his experience seems to indicate otherwise and that limiting the CPU frequency does actually decrease the battery drain, I put this theory to the test and logged the battery charge over 10h of FLAC playback in Airplane mode with the screen switched off.As one can see, limiting the frequency to a maximum of 880MHz does indeed reduce the power consumption.Thus it seems that race-to-idle does not properly work on this device. But why? The answer is that, while the technical manual showed that the hardware supports six different power states: NORMAL, IDLE, DEEP-IDLE, STOP, DEEP-STOP and SLEEP, a quick look into the source code revealed that (besides SLEEP) only the most rudimentary power saving state IDLE is actually implemented in the kernel. And for this IDLE state no parts of the hardware are powered down; the only thing that essentially happens is that the CPU clock is set to zero.For the real power savings to kick in, one would have to power down parts of the hardware (at least the CPU) and that is exactly what the DEEP-IDLE state is supposed to do. Unfortunately this was not implemented. So, following the technical manual I tried to implement this DEEP-IDLE state on my own... and failed miserably. Well, not that miserably... at least I had an idea where the problem was. However I had no idea how to fix it since I did not know enough about the hardware details, had no experience working that close to the CPU and also the manual was far from instructive.So I googled to maybe find someone smarter than me who had figured out a solution and found out that "some guys" already tried to implement this DEEP-IDLE state for the Galaxy S, however that did not work for some reason. I located the source code in some older version of sixstringsg's Glitch kernel, made some modification, put it into my reference kernel and everything is running fine for more than two days now.I have repeated the above test runs on a kernel including the deep-idle state.The battery drain is reduced to 17-18% compared to the 41%/46% (880MHz/1320MHz) obtained without deep-idle support. Thus the battery consumption is reduced by more than 55%. Also we see that with the kernel including deep-idle both 880MHz and 1320MHz yield almost the same average battery drain, showing that race-to-idle actually does work as long as the power saving features are properly supported. So, once these modifications are implemented in your favorite kernel, there should be no need for limiting the frequency anymore and you should be able to blast away without wasting a thought about the battery drain. Also, there is no reason anymore not to overclock your device, except of course the thermal limit of the device auto-incinerating in your hand.Changes to the source code: http://www.pastie.org/2679956 BUGFIX:1. While the screen is on, only the normal IDLE state is used.2. Removed DEEP IDLE state with TOP=ON.3. For the DEEP IDLE state with TOP=OFF, all external interruptwakeup sources except XEINT[30] (key input) are disabled andall internal wakeup sources are disabled.Bugfix: http://www.pastie.org/2686141 BUGFIX #2:1. The reserved values of S5P_WAKEUP_MASK are not touched.2. Only XEINT[22] = GPH2[6] = GPIO_nPOWER = GPIO_N_POWER andand XEINT[29] = GPH3[5] = GPIO_OK_KEY are enabled as externalwakeup sources.3. Only RTC TICK and I2S are enabled as wakeup sources.Bugfix: http://www.pastie.org/2692199 BUGFIX #3:For DEEP IDLE the same wakeup interrupt sources are used as for thesleep state plus at least XEINT[22] = GPH2[6] = GPIO_nPOWER =GPIO_N_POWER, XEINT[29] = GPH3[5] = GPIO_OK_KEY, RTC TICK and I2S.Bugfix: http://www.pastie.org/2697909 BUGFIX #4:Implemented a sysfs interface (/sys/class/misc/deepidle) for enabling/disabling the use of the DEEP IDLE state.Bugfix: http://www.pastie.org/2709212 BUGFIX #5:Fixed the BT problem and made sure the DEEP IDLE state is not used when the device is going to/waking from suspend (SLEEP).Bugfix: http://www.pastie.org/2723702 BUGFIX #6:1. Mimicking the procedure for the SLEEP state, when entering/returningfrom the DEEP IDLE state, a notification is send to kernel modulesusing 'pm_notifier_call_chain'. This will cause CPUfreq to staticallyset the frequency to SLEEP_FREQ and prevent further frequency changesuntil the system returns from DEEP IDLE.2. The APLL lock is not modified.Bugfix: http://www.pastie.org/2744527 BUGFIX #7:Fixed GPS problems.Bugfix: http://www.pastie.org/2775884 BUGFIX #8:The DEEP IDLE with TOP=ON is used while GPS or BT are running.Bugfix: http://www.pastie.org/2778855 BUGFIX #9:Notifications using 'pm_notifier_call_chain' when entering/leaving DEEP IDLE have been removed.Bugfix: http://www.pastie.org/2796786 Test kernel: http://www.multiupload.com/K8ZXS5HTLS For each of the different idle states, the number of idle calls and the total amount of time spend in each state are saved. These stats can be shown using 'idle_stats' in the sysfs interface and resetted with 'reset_stats'.Bugfix: http://www.pastie.org/2779930 BUGFIX #10:1. Changed stats variables to 'unsigned long long' to defer overflow and keep the idle times in us for increased accuracy.2. In case of an overflow of one of the stats variables, all stats are resetted.3. Put in a mutex to prevent simultanous access to the stats variables.Bugfix: http://www.pastie.org/2788087 BUGFIX #11:Notifications using 'pm_notifier_call_chain' when entering/leaving DEEP IDLE have been removed.Bugfix: http://www.pastie.org/2796786 BUGFIX #12:Removed RTC TICK as wakeup source and added RTC ALARM.Bugfix: http://www.pastie.org/2805119 BUGFIX #13:Added ST as wakeup source.Bugfix: http://www.pastie.org/2815223 BUGFIX #14:Added KEY as wakeup source.Bugfix: http://www.pastie.org/2815884 As I said above, this code is almost entirely written by "some other guys". I just made some improvements and extensions to it. Unfortunately I have no idea who these great people are, so I cannot give any credit (yet). However I asked sixstringsg and he told me he would ask around.There is a bug in the Google Talk appwhich causes the CAM submodule to be activated on boot. As a consequence the DEEP IDLE state will not be used. Opening the camera app once and then closing it, shuts down the CAM module so after that the DEEP IDLE state will be used properly. The Talk versiondoes not have this issue. Unfortunately both version are shown as Version 1.3 in the application info, however the app size differs (740KB with video-chat, 500KB without video-chat) as well as the permissions (with video-chat additionally has the permission 'Hardware controls').without video-chat:with video-chat:A big thank you to FloHimself for tracking down the problem!