msm_thermal: add thermal driver support
Ported from Dennis (@showp1984) git https://github.com/showp1984
from pyramid and mako repo.

Some changes were done for the Galaxy S2, rest of commits squashed here for
easy pick. Refer below to the list of changes.

msm: thermal: Add MSM thermal sensing and limiting

Add a thermal driver to poll and detect any thermal condition at boot
and limit max cpu frequency on a thermal condition. This driver is
intended to limit cpu thermal output until the userspace thermald
kicks in. The thermald will disable this driver and take over the
cpu thermal sensing and mitigation.

Change-Id: I2710bfdeb5bf01c864437e13da835621d1b9202c
Signed-off-by: Praveen Chidambaram <pchidamb@codeaurora.org>

thermal: Fix hotplug race condition leading to memory corruption.

msm_thermal module made a copy of the CPUFreq policy structure
pointer but continues to try and use it after the core has been
hotplugged (and the original structure is freed). The fix is to
use cpufreq_cpu_get/put instead of copying cpufreq pointers.

CRs-fixed: 342351
Change-Id: I312d8bccc09a5df5805f99fecdc9409d9d4ed2be
Signed-off-by: Eugene Seah <eseah@codeaurora.org>

msm_thermal: Remove error messages from log when core is offline

If msm_thermal is not cancelled and TSENS reports a high temperature,
it might try to limit the frequency, but since the core is offline,
the frequency limit may not happen. We need not report this error.

Change-Id: I579e2329c6fb038d53f2d207a6a832185e633785
Signed-off-by: Praveen Chidambaram <pchidamb@codeaurora.org>

driver/thermal: create kernel MSM thermal management for MSM8x60 SOC

thermal: Don't limit CPU frequency after msm_thermal is disabled

There may be an outstanding task to check for threshold crossing
when msm_thermal is disabled and CPU is restored to maximum
frequency. This task can get scheduled after the maximum frequency
is restored, and result in another mitigation that pins CPU
frequencies to 918mHz. This happens in the case that the final
temperature was still above the max temperature threshold.
This patch ensures any outstanding tasks are cancelled and flushed
before finally restoring the CPU frequencies.

CRs-fixed: 351614
Change-Id: Ifce00b379989b78856803ac09c7403207f5c998b
Signed-off-by: Eugene Seah <eseah@codeaurora.org>

msm_thermal: 3-phase thermal throttling

This introduces 3-phase thermal throttling to the existing
msm_thermal driver (it now works more like thermald).
This also changes the sampling rate to check every second
for increased temperatures.
Additionally this adds clear thresholds for the 2 new tripping
points.

Signed-off-by: showp1984 <showp1984@gmail.com>

msm_thermal: add sysfs interface

This adds a sysfs interface to msm_thermal.
I've also restructured the tuneables into a struct
for better overview.

Signed-off-by: showp1984 <showp1984@gmail.com>

msm_thermal: move sysfs to /sys/kernel/msm_thermal/conf/

Signed-off-by: showp1984 <showp1984@gmail.com>

msm_thermal: fix saving pre-throttled freq

Signed-off-by: showp1984 <showp1984@gmail.com>

add thermal support for celox

drivers/thermal: create own unbound (highpri) wq for msm_thermal

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

drivers/thermal: add emergency shutdown

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: emergency shutdown rework

additionally to initiating the shutdown the lowest possible
frequency should be set. (=max throttle atm)
update the cpufreq and do not cancel the workqueue
+ some small code improvements

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: use temperature1 sensor too
The sensor1 seems to be cpu related too, and it usually
shows higher temps (~2°C). Use the higher value of both
to decide if thermal throttling is neccessary.

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: do not wait, unthrottle now

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: remove not needed condition

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: fix clear thresholds

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: only update freq if new is not > than saved_max

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: rework throttling states

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: set cpus online to set frequency back to unthrottled

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: change temperatures slightly

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: fixing debug messages

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: fix mid scaling

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>

thermal: don't calculate max everytime, once is enough

Signed-off-by: Dennis Rassmann <showp1984@gmail.com>