Writing to Embedded Controller registers in Ubuntu 14.04
It turns out that the 15th register is responsible for enabling write, so I write 0D to that location first, as Notebook FanControl does, then start controlling the fan speed as above. Below is the full code if anyone is interested (It works on HP Envy m6 series laptops. Also you should first install lm-sensors)
sudo modprobe -r ec_sys
sudo modprobe ec_sys write_support=1
echo -n -e "\015" | sudo dd of="/sys/kernel/debug/ec/ec0/io" bs=1 seek=15 count=1 conv=notrunc 2> /dev/null
echo "Fan control enabled"
while [ 1 ]; do $(if [ "0$(bc -q <<< $(echo "`sensors -A -u | grep "temp1_input" --max-count=2 | tail -n1 | cut -d 'n' -f 2 | tr -cd '0123456789\.'`-0 > 54");)" -eq 1 ]; then echo -n -e "\051"; else echo -n -e "\031"; fi | sudo dd of="/sys/kernel/debug/ec/ec0/io" bs=1 seek=20 count=1 conv=notrunc 2> /dev/null); sleep 1; done