Wednesday, July 25, 2012

Android | Linux "Sysfs" interface in kernel [Xperia X10]

Before reading this, make sure you have read this post and this one.
Sysfs is a virtual file system provided by the linux kernel. It is exports information about devices and drivers, and is also used for configuration. We used this in previous post to set scaling_governor and scaling_max_freq. All those files are part of Sysfs interface. This post will outline on creating a new file in the same old cpufreq folder to display all possible frequencies that the cpu can run on. This was done on Xperia X10, and I have good reasons to believe that The interface files are created from the source in /drivers folder of the /kernel. This particular one we are searching for must be in /cpufreq. Inside the folder we find the source files. Open up cpufreq.c. 

Go to that part of the code that says sysfs interface. And find suitable place to create our file. As you can see I found mine.

Now that you have added a function to display the frequencies. Its time to create the file in the interface that would display them

They can be created by adding a line to the descriptors that create a file. Just add


We need to add a line to the attributes structure also. Add


to the lines.

Now the driver file is ready. But if you have observed, we have made use of an external function by name


This is not defined in acpuclock file. So we need to head over to the file


and add this function to output the value of frequencies as a string buffer.

The function plainly alters the string in *buf to hold the values of the frequencies. This completes the work. Now compile the kernel and load it to the phone, and discover your own new interface that shows all the possible frequency values.

Tuesday, July 24, 2012

Android kernel overclock [QSD8250] [Xperia X10]

Playing Temple Run is great on my phone [Xperia X10]. But I was irritated by its constant lagging gameplay on my phone. One of awesome things about linux is that it allows for dynamic frequency scaling (???). That means changing the frequency at which the board is running on the go. I just remembered that, So connected the phone to the computer and established adb link. Then I went to the cpufreq folder. It was easy to find, I found it in sys/devices/system/cpu/cpu0. In this folder we find a lot of files that we can write to and control the freq of the board. A ls command would reveal all of them

The files are self explanatory. For example doing

$cat cpuinfo_max_freq

We get to know that the maximum freq of the cpu is 998400. The freq of the board is constantly changed to minimize the power consumption of the device. This change is done by a program known as governor. Many governors are found in a linux system. Typically five are found in any linux system.
  1. powersave
  2. performance
  3. userspace
  4. ondemand
  5. conservative 

But in my case (X10) there are only two

$cat scaling_available_governors
ondemand performance

Performance runs the cpu in fastest mode always and ondemand allows the freq to be controlled by superuser or root.

I did change my governor to performance. 

#echo performance > scaling_governor
echo performance > scaling_governor

But I was still not satisfied with the results. Xperia X10 is officially a 1GHz phone, as announced on Gsmarena

But a little help of google shows that it can run upto 1.2GHz. A lot of developers have created overclocked kernels. I decided to create my own (uff... So much for Temple Run). Rest of the post shows how that was achieved. 

Try this at your own risk. I am not responsible for whatever the hell happens to your phone.

Basically we need a Linux environment to compile kernel. Oh I will not take the liberty of explaining the way in which we can compile the kernel. There is already a tutorial online on how to do so by an awesome guy (lol). And as usual we need the phone to be rooted and bootloader unlocked. And the source code for the Xperia X10 kernel can be found here. The link in the tutorial leads you to the latest kernel source, which surprisingly hasn't been configured for xperia x10. 

Ohk get into the kernel folder of the source. The clock is controlled using the code in a file by name of acpuclock-{model}.c. In our case the file would be acpuclock-8x50.c. You will find the file in ../../kernel/arch/arm/mach-msm folder. Open the file in a editor. A chipset can only run at certain frequencies. The possible frequencies are therefore tabulated and required freq selected from it in our case we must find the table somewhere inside this file. We find that there is a structure that holds all the possible freq values for the board

struct clkctl_acpu_speed acpu_freq_tbl_998[]

You can ignore the other struct as the max freq in that is less than X10's. The struct is just defined above the table   

We find that the first value is a integer depicting weather the freq is to be used for scaling or not.

 Now let us add our own frequencies to this table. Let us add just two frequencies after 998400KHz entry. Add

{ 1, 1000000, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x1B, 1300},
{ 1, 1228800, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x1C, 1300},

to the table. This must create us two scalable frequencies 1GHz and 1.2288GHz.

Now that we have created two extra frequencies, but the kernel is still not ready to be compiled. We need to edit the structure that creates this table on runtime

static struct cpufreq_frequency_table freq_table[20];

This creates an array of twenty structures of those frequency values, which was just sufficient for the initial table. But now that we have extra two values let us increase this to say 23

This takes care of the array. But there is another function by name

static void __init acpu_freq_tbl_fixup(void)

This thing fixes back the cpu frequency to normal values if it finds that the frequency has exceeded the fixed value. It does this by using a case statement

switch (tcsr_spare2 & 0xF0) {
case 0x70:
acpu_freq_tbl = acpu_freq_tbl_768;
max_acpu_khz = 768000;
case 0x30:
case 0x00:
max_acpu_khz = 998400;
case 0x10:
max_acpu_khz = 1267200;
pr_warning("Invalid efuse data (%x) on Max ACPU freq!\n",
goto skip_efuse_fixup;

Now change the value of case 0x00: max_acpu_khz to 1228800.

switch (tcsr_spare2 & 0xF0) {
case 0x70:
acpu_freq_tbl = acpu_freq_tbl_768;
max_acpu_khz = 768000;
case 0x30:
case 0x00:
max_acpu_khz = 1228800;
case 0x10:
max_acpu_khz = 1267200;
pr_warning("Invalid efuse data (%x) on Max ACPU freq!\n",
goto skip_efuse_fixup;

Ok that takes care of everything. Now just compile the kernel, and load it to your phone. And If you get this error during the compile time

Go to the Makefile in /kernel folder. And edit the KBUILD_CFLAGS in it, Just remove -Werror\ line.

That flag makes the compiler check for warnings and terminates the compile process in case of small warnings. On removal of the flag these are ignored. Any other problems you encounter just leave a comment at the end. I will try to rectify. Now to check out the build. After you load it to your phone connect it in USB debugging mode and open the shell. Now check for the maximum frequency. The best way to run the cpu in maximum freq is to run performance governor. So I changed the governor to performance and then updated the scaling_max_freq.

And voila the Temple Run was running awesome on the phone. And don't put it in that state for too long, it drains battery fast. And enjoy the new awesomeness of Xperia X10. We can create custom governors like smartass minmax .,etc. And run the phone more efficiently. We can also port the  2.6.32 kernel of newest xperia phones to Xperia X10. But that is for another day. Enjoy with this for now. Or there are a lot of custom kernels available online download them. Try creating more entries to the table and run the cpu on different frequencies and feel the awesomeness of your X10. Incase you don't own a X10 the case must be similar with whatever phone you own. But make sure it is overclockable, cause some phones already run at the highest freq supported by the board. And any tweak to the kernel will not work. That will not spoil the phone though, but try it at your own risk.

Thursday, July 5, 2012

Voltage Regulators

The basis of today's electronic devices is a voltage regulator as all of the intelligent electronic devices run on DC voltage and the marketing is done on AC power. A basic understanding of the DC voltage regulator is therefore considered essential. There are mainly two types of power supply designs linear and switched power supplies. This document mainly discusses the linear type.
The most simplest approach to a hobbyist voltage regulator is to use 78XX series voltage regulator IC's to build one. Lets get into the task of building a simple 5V regulator using 7805. It is always intelligent to look into the datasheet of the IC before using one. Assuming we use LM7805 from Fairchild Semiconductor the IC would typically look like this. The part where 7805 is written would be the black material enclosing the original circuitry and the upper one which has a small 4 mm hole is made of metal to dissapate heat and is usally also the output pin anong with the output pin below the original IC.

Lets get into business of building a voltage regulator. If we skim through the document ( ) we find that in the last pages of the document under Typical Applications

This is how the IC must be used be typically used according to the manufacturers. Now let us get into building one. We shall build a custom one that looks like

Materials required

  • 12V-0V-12V, 1A Step Down transformer
  • IC7805
  • Capacitor 470uf, 60v electrolytic
  • 4 diodes, IN4007 would do
  • Bread board to build the circuit
  • RMC connectors to take the output
  • Connecting plug to connect to AC mains (CEE 7/17) and some insulated  multi strand wire (why?) to connect transformers to the plug
Connect the two input wires of the transformer to the  multistrand wire and the other end of the multistrand wire to the plug. At this stage one may wish to put their multimeter to AC voltage mode and read the output from the secondary end by connecting the plug to the AC mains, It is not suggested that you do this concerning the safety due to existence of the AC main from the wire. Next build the rectifier bridge (Give attention to the diode directions). I strongly suggest that you use a bread board to build the circuit on (Building it on a pin board or an etched copper board is more of a permanent installation, Trust me you don't want to end up with this circuit for a regular electronic hobby kit). Connect the output from the transformer to the bridge Now i strongly suggest you to check the output voltage using a multimeter in DC voltage mode. Depending on various factors viz. Input voltage of the AC voltage, the practical drop out of the diodes you may get the voltage >20V and <8V. Anything out of this range, you may want to check the circuit you have built. Next connect the electrolytic capacitor. If you check the DC voltage now it must be almost same as before (why?). Next connect the IC to the circuitry and measure the output voltage. It must show 5.07V - 5.04V. Congratulations for having built your first Voltage Regulator.

Tweak 1 (Adjustable voltage regulator )

Though the above is an awesome regulator it is not the one you would want. What you would really want is the one that can be adjusted. And also the above one is a 5V power supply. Sometimes ( when working with Op-Amp's like uA741) we need   +12V | 0V | -12V power supply. So an ideal power supply must be a controlled one. How to get a regulated power supply now. The answer lies in another magical IC by the name LM317. This IC provides trickery to just do the same. The datasheet of this IC says that this trickery is achived by two resistors connected as shown
The output is voltage is given by the above formula. You can neglect IadjR2  part as Iadj  is a very small quantity usually around 50 uA. So the output of the IC would be V0 = 1.25V(1+R2/R1). In order to simplify the understanding of the IC we must remember the fact that the IC always produces a voltage of 1.25V between the pins adj and output

The first problem of a adjusting the voltage is thus almost solved now (almost ??) what must be the values of the resistors R1 and R2 ?,  From the data sheet we find that typical value of Iadj must be 50 uA. So considering that the current flowing in R1 and R2 must be greater than this so as to affect the assumption made about neglecting IadjR2 part of the formula. Let the effective current through the resistor be atleast 20 times greater than  Iadj. That gives us a current of 1mA so the resistor R1 would be 1.25V/1 mA, that is 1.25K resistor. We shall assume a standard value of 1K resistor. And so as to adjust the voltage fully until the highest value we can take a 10K pot for the R2.  This under all favourable conditions must give us an max output voltage 1.25(1+10/1) = 13.75V. This isn't possible in the present circuitry as the input voltage from the regulator may not be that high but it is fairly possible to reach voltages of 12V - 10V DC for max voltage and the min voltage would be 1.25V DC (Oops..).
Now to tackle the second problem, the dual mode ( both positive and negative Voltages ). This is fairly simple. Simply build two of these adjustable regulators (that means another bridge from the remaining 12V wire and 0V wire and then the regulator circuit ) and connect one of their ground to anothers output voltage. Now the output voltage of the circuit taking the point at ground and output voltage short as the refrence voltage would be +ve at the first output and - ve voltage can be taken from the ground of the second regulator.

Now this is the most basic thing that you would want to keep in your electronics hobby kit. But wait before getting this hard wired there are a couple more tweaks.

Tweak 2 ( Current Booster )

The datasheet document claims to output a max of 1A of current which in real sense would be more than sufficient for most of the regular applications but we missed a point. Lets say we are using a transformer to step down the AC voltage from the mains (220V-230V, 50-60Hz) to 12V-0V-12V,50-60Hz AC and then rectify it using a conventional rectifier the output voltage must then be around 9V DC (say) the output of a 7805 is then 5V therefore the voltage drop on the regulator IC is around 4V. Then the power dissapated on the rectifier must be 4Vx1A = 4W. Seeing the document we see that the the thermal resistence from junction to the air is 65 0C/W therefore the temperature diffrence between junction and the outside must be 65 x 4 = 260 0C. The datasheet lists that the maximum current is 2.2A when the junction temperature is maintained at 25 0C. Considering we do the same at 1A ( must be easier at 1A ). So let us say the Junction temperature is at 25 0C. Then the temperature of the outside body of the IC must be 25 0C - 260 0C = -235 0C. That means the outside of our IC must be maintained at -235 0C or at  ~38K something only achievable using Liquid Helium (Well Fairchild where did you mention that?!!!). In short the output current can only be a maximum of 100 mA from these conventional IC's without use of a heat sink. Anything greater would heat up the IC rapidly and burn it out. That puts a serious limitation on the current handling capacity of the IC. Though in most of the cases this must be more than sufficient (Typical useage for a hobbyist would be a max of 50 mA) you will understand in the next section why this restriction is a backlog for a lot of awesomeness.
Now getting to the problem. How to boost the current in the output pin?. The answer lies in datasheet itself by using an extra transistor this problem can be overcome

Neglect the capacitors around they aren't required when you have a filter of 470uF after the rectifier R1 must be very small. The document suggests 3ohms, I suggest you find the smallest value you can find. The document also says the transistor to be BD536, a power transistor that can handle very high output currents. Using this will allow you to handle currents up to ~1A. Datasheet claims up to 8A, But we know how realistic the claim would be

I don't really suggest you to use this transistor itself,  try others, read their datasheets and experiment. 2N2955 would allow you to get currents upto 1A~2A (According to the datasheet the maximum would be 7A) though that much current you will never use as a hobbyist, It would be a cool thing to have. For small applications BC548 would do, but I would  not suggest it. It would be an understatement to use one. There are lots of other transistors available. The resistor is used to provide the required voltage between collector and the base. Read the document and decide upon the resistor to be used based on the power that is being used. Its usually enough to use smaller resistors of around 22 ohms. The high current Power transistor 2N2955 would typically look like

Tweak 3 (USB Charger for Mobile phones)

If you have been through the USB documentation (why would anyone do that??). You will find that the USB host must provide a constant voltage of 5V. Most of the mobiles exploit this into charging their battery. This reduces the number of physical ports they must provide on the phone. The same port can be used as a USB slave and also for charging purposes. Now out 5V regulator can be used as a mobile phone charger ( Current Boost is must, Trust me it doesn't work otherwise. The current drawn from the mobile phone is great that the voltage soon drops below acceptable values from the IC ). If you have designed a 5V regulator or adjustable regulator adjusted to 5V with current boost then connect a USB female pin. Depending on the type of the female pin that you may find in the market the pin out would look like this.

Type A is the most common one used in the market and also mobile USB cables are of male type A connector on one end, So I would suggest you get type A female pin for all good purposes. If you are using a adjustable voltage regulator make sure you set it to 5V so as to avoid any damage to the mobile phone.

Tweak 4 (Overload Protection )

Now that we have all the designs for a stable voltage regulator, lets look at improving some of the operation parameters. Shortcircuit overload protection is the easiest way to avoid high power drain due to short circuit that can damage the circuitry and lead you into lot of problems. Refering back to the datasheet we get the easiest way to implement one would be 
The transistor suggested is TIP42. You may use BD536 also with no much change in the design. Feel free, experiment. VBEQ2 is the voltage drop between base and collector of Q2. ISC  is the short circuit current. This will be the maximum current that can be drawn from the regulator. By this we limit the current that can be drawn from the IC, thus protecting it against damage. 

Further Reading