Hi Andrew/Zhang/Len,
This current patchset is based on 3.6-rc1 and Zhang Rui's core thermal
enhancement patches(http://permalink.gmane.org/gmane.linux.acpi.devel/54564).
Version V5 is present in linux-next tree. Since Zhang Rui's implementation are
already present in linux-next so requesting Andrew to add this V6 series and drop
earlier V5 version.
Changes since v5:
This patchset basically simplifies the cpufreq cooling API's to take just
cpumask as an input parameter and also removes the state management logic as they
are now taken care in the core thermal layer.
All these patches over Zhang Rui's work can be found in the git link
git://git.linaro.org/people/amitdanielk/linux.git exynos_v6_thermal_tree
Thanks,
Amit Daniel
Amit Daniel Kachhap (6):
thermal: add generic cpufreq cooling implementation
hwmon: exynos4: move thermal sensor driver to driver/thermal
directory
thermal: exynos5: add exynos5250 thermal sensor driver support
thermal: exynos: register the tmu sensor with the kernel thermal
layer
ARM: exynos: add thermal sensor driver platform data support
thermal: exynos: Use devm_* functions
Documentation/hwmon/exynos4_tmu | 81 ---
Documentation/thermal/cpu-cooling-api.txt | 52 ++
Documentation/thermal/exynos_thermal | 52 ++
drivers/hwmon/Kconfig | 10 -
drivers/hwmon/Makefile | 1 -
drivers/hwmon/exynos4_tmu.c | 518 --------------
drivers/thermal/Kconfig | 18 +
drivers/thermal/Makefile | 2 +
drivers/thermal/cpu_cooling.c | 512 +++++++++++++
drivers/thermal/exynos_thermal.c | 994 ++++++++++++++++++++++++++
include/linux/cpu_cooling.h | 79 ++
include/linux/platform_data/exynos4_tmu.h | 83 ---
include/linux/platform_data/exynos_thermal.h | 116 +++
13 files changed, 1825 insertions(+), 693 deletions(-)
delete mode 100644 Documentation/hwmon/exynos4_tmu
create mode 100644 Documentation/thermal/cpu-cooling-api.txt
create mode 100644 Documentation/thermal/exynos_thermal
delete mode 100644 drivers/hwmon/exynos4_tmu.c
create mode 100644 drivers/thermal/cpu_cooling.c
create mode 100644 drivers/thermal/exynos_thermal.c
create mode 100644 include/linux/cpu_cooling.h
delete mode 100644 include/linux/platform_data/exynos4_tmu.h
create mode 100644 include/linux/platform_data/exynos_thermal.h
This patchset introduces a new generic cooling device based on cpufreq
that can be used on non-ACPI platforms. As a proof of concept, we have
drivers for the following platforms using this mechanism now:
* Samsung Exynos (Exynos4 and Exynos5) in the current patchset.
* Freescale i.MX (git://git.linaro.org/people/amitdanielk/linux.git imx6q_thermal)
There is a small change in cpufreq cooling registration APIs, so a minor
change is needed for Freescale platforms.
Brief Description:
1) The generic cooling devices code is placed inside driver/thermal/*
as placing inside acpi folder will need un-necessary enabling of acpi
code. This code is architecture independent.
2) This patchset adds generic cpu cooling low level implementation
through frequency clipping. In future, other cpu related cooling
devices may be added here. An ACPI version of this already exists
(drivers/acpi/processor_thermal.c) .But this will be useful for
platforms like ARM using the generic thermal interface along with the
generic cpu cooling devices. The cooling device registration API's
return cooling device pointers which can be easily binded with the
thermal zone trip points. The important APIs exposed are,
a) struct thermal_cooling_device *cpufreq_cooling_register(
struct cpumask *clip_cpus)
b) void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
3) Samsung exynos platform thermal implementation is done using the
generic cpu cooling APIs and the new trip type. The temperature sensor
driver present in the hwmon folder(registered as hwmon driver) is moved
to thermal folder and registered as a thermal driver.
A simple data/control flow diagrams is shown below,
Core Linux thermal <-----> Exynos thermal interface <----- Temperature Sensor
| |
\|/ |
Cpufreq cooling device <---------------
TODO:
*Will send the DT enablement patches later after the driver is merged.
This patch:
Add support for generic cpu thermal cooling low level implementations
using frequency scaling up/down based on the registration parameters.
Different cpu related cooling devices can be registered by the user and
the binding of these cooling devices to the corresponding trip points can
be easily done as the registration APIs return the cooling device pointer.
The user of these APIs are responsible for passing clipping frequency .
The drivers can also register to recieve notification about any cooling
action called.
[[email protected]: fix comment layout]
Signed-off-by: Amit Daniel Kachhap <[email protected]>
Cc: Guenter Roeck <[email protected]>
Cc: SangWook Ju <[email protected]>
Cc: Durgadoss <[email protected]>
Cc: Len Brown <[email protected]>
Cc: Jean Delvare <[email protected]>
Cc: Kyungmin Park <[email protected]>
Cc: Kukjin Kim <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Amit Daniel Kachhap <[email protected]>
---
Documentation/thermal/cpu-cooling-api.txt | 52 +++
drivers/thermal/Kconfig | 11 +
drivers/thermal/Makefile | 1 +
drivers/thermal/cpu_cooling.c | 512 +++++++++++++++++++++++++++++
include/linux/cpu_cooling.h | 79 +++++
5 files changed, 655 insertions(+), 0 deletions(-)
create mode 100644 Documentation/thermal/cpu-cooling-api.txt
create mode 100644 drivers/thermal/cpu_cooling.c
create mode 100644 include/linux/cpu_cooling.h
diff --git a/Documentation/thermal/cpu-cooling-api.txt b/Documentation/thermal/cpu-cooling-api.txt
new file mode 100644
index 0000000..a1f2a6b
--- /dev/null
+++ b/Documentation/thermal/cpu-cooling-api.txt
@@ -0,0 +1,52 @@
+CPU cooling APIs How To
+===================================
+
+Written by Amit Daniel Kachhap <[email protected]>
+
+Updated: 12 May 2012
+
+Copyright (c) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
+
+0. Introduction
+
+The generic cpu cooling(freq clipping) provides registration/unregistration APIs
+to the caller. The binding of the cooling devices to the trip point is left for
+the user. The registration APIs returns the cooling device pointer.
+
+1. cpu cooling APIs
+
+1.1 cpufreq registration/unregistration APIs
+1.1.1 struct thermal_cooling_device *cpufreq_cooling_register(
+ struct cpumask *clip_cpus)
+
+ This interface function registers the cpufreq cooling device with the name
+ "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
+ cooling devices.
+
+ clip_cpus: cpumask of cpus where the frequency constraints will happen.
+
+1.1.2 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
+
+ This interface function unregisters the "thermal-cpufreq-%x" cooling device.
+
+ cdev: Cooling device pointer which has to be unregistered.
+
+
+1.2 CPU cooling action notifier register/unregister interface
+1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
+ unsigned int list)
+
+ This interface registers a driver with cpu cooling layer. The driver will
+ be notified when any cpu cooling action is called.
+
+ nb: notifier function to register
+ list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
+
+1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
+ unsigned int list)
+
+ This interface registers a driver with cpu cooling layer. The driver will
+ be notified when any cpu cooling action is called.
+
+ nb: notifier function to register
+ list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 7dd8c34..996003b 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -19,6 +19,17 @@ config THERMAL_HWMON
depends on HWMON=y || HWMON=THERMAL
default y
+config CPU_THERMAL
+ bool "generic cpu cooling support"
+ depends on THERMAL && CPU_FREQ
+ help
+ This implements the generic cpu cooling mechanism through frequency
+ reduction, cpu hotplug and any other ways of reducing temperature. An
+ ACPI version of this already exists(drivers/acpi/processor_thermal.c).
+ This will be useful for platforms using the generic thermal interface
+ and not the ACPI interface.
+ If you want this support, you should say Y here.
+
config SPEAR_THERMAL
bool "SPEAr thermal sensor driver"
depends on THERMAL
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index fd9369a..aae59ad 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -3,5 +3,6 @@
#
obj-$(CONFIG_THERMAL) += thermal_sys.o
+obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
new file mode 100644
index 0000000..c42e557
--- /dev/null
+++ b/drivers/thermal/cpu_cooling.c
@@ -0,0 +1,512 @@
+/*
+ * linux/drivers/thermal/cpu_cooling.c
+ *
+ * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
+ * Copyright (C) 2012 Amit Daniel <[email protected]>
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * 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; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/thermal.h>
+#include <linux/platform_device.h>
+#include <linux/cpufreq.h>
+#include <linux/err.h>
+#include <linux/slab.h>
+#include <linux/cpu.h>
+#include <linux/cpu_cooling.h>
+
+/**
+ * struct cpufreq_cooling_device
+ * @id: unique integer value corresponding to each cpufreq_cooling_device
+ * registered.
+ * @cool_dev: thermal_cooling_device pointer to keep track of the the
+ * egistered cooling device.
+ * @cpufreq_state: integer value representing the current state of cpufreq
+ * cooling devices.
+ * @cpufreq_val: integer value representing the absolute value of the clipped
+ * frequency.
+ * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
+ * @node: list_head to link all cpufreq_cooling_device together.
+ *
+ * This structure is required for keeping information of each
+ * cpufreq_cooling_device registered as a list whose head is represented by
+ * cooling_cpufreq_list. In order to prevent corruption of this list a
+ * mutex lock cooling_cpufreq_lock is used.
+ */
+struct cpufreq_cooling_device {
+ int id;
+ struct thermal_cooling_device *cool_dev;
+ unsigned int cpufreq_state;
+ unsigned int cpufreq_val;
+ struct cpumask allowed_cpus;
+ struct list_head node;
+};
+static LIST_HEAD(cooling_cpufreq_list);
+static DEFINE_IDR(cpufreq_idr);
+
+static struct mutex cooling_cpufreq_lock;
+
+/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
+#define NOTIFY_INVALID NULL
+struct cpufreq_cooling_device *notify_device;
+
+/* Head of the blocking notifier chain to inform about frequency clamping */
+static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
+
+/**
+ * get_idr - function to get a unique id.
+ * @idr: struct idr * handle used to create a id.
+ * @id: int * value generated by this function.
+ */
+static int get_idr(struct idr *idr, int *id)
+{
+ int err;
+again:
+ if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
+ return -ENOMEM;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ err = idr_get_new(idr, NULL, id);
+ mutex_unlock(&cooling_cpufreq_lock);
+
+ if (unlikely(err == -EAGAIN))
+ goto again;
+ else if (unlikely(err))
+ return err;
+
+ *id = *id & MAX_ID_MASK;
+ return 0;
+}
+
+/**
+ * release_idr - function to free the unique id.
+ * @idr: struct idr * handle used for creating the id.
+ * @id: int value representing the unique id.
+ */
+static void release_idr(struct idr *idr, int id)
+{
+ mutex_lock(&cooling_cpufreq_lock);
+ idr_remove(idr, id);
+ mutex_unlock(&cooling_cpufreq_lock);
+}
+
+/**
+ * cputherm_register_notifier - Register a notifier with cpu cooling interface.
+ * @nb: struct notifier_block * with callback info.
+ * @list: integer value for which notification is needed. possible values are
+ * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
+ *
+ * This exported function registers a driver with cpu cooling layer. The driver
+ * will be notified when any cpu cooling action is called.
+ */
+int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
+{
+ int ret = 0;
+
+ switch (list) {
+ case CPUFREQ_COOLING_START:
+ case CPUFREQ_COOLING_STOP:
+ ret = blocking_notifier_chain_register(
+ &cputherm_state_notifier_list, nb);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ return ret;
+}
+EXPORT_SYMBOL(cputherm_register_notifier);
+
+/**
+ * cputherm_unregister_notifier - Un-register a notifier.
+ * @nb: struct notifier_block * with callback info.
+ * @list: integer value for which notification is needed. values possible are
+ * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
+ *
+ * This exported function un-registers a driver with cpu cooling layer.
+ */
+int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
+{
+ int ret = 0;
+
+ switch (list) {
+ case CPUFREQ_COOLING_START:
+ case CPUFREQ_COOLING_STOP:
+ ret = blocking_notifier_chain_unregister(
+ &cputherm_state_notifier_list, nb);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ return ret;
+}
+EXPORT_SYMBOL(cputherm_unregister_notifier);
+
+/* Below code defines functions to be used for cpufreq as cooling device */
+
+/**
+ * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
+ * @cpu: cpu for which check is needed.
+ */
+static int is_cpufreq_valid(int cpu)
+{
+ struct cpufreq_policy policy;
+ return !cpufreq_get_policy(&policy, cpu);
+}
+
+/**
+ * get_cpu_frequency - get the absolute value of frequency from level.
+ * @cpu: cpu for which frequency is fetched.
+ * @level: level of frequency of the CPU
+ * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
+ */
+static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
+{
+ int ret = 0, i = 0;
+ unsigned long level_index;
+ bool descend = false;
+ struct cpufreq_frequency_table *table =
+ cpufreq_frequency_get_table(cpu);
+ if (!table)
+ return ret;
+
+ while (table[i].frequency != CPUFREQ_TABLE_END) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+
+ /*check if table in ascending or descending order*/
+ if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
+ (table[i + 1].frequency < table[i].frequency)
+ && !descend) {
+ descend = true;
+ }
+
+ /*return if level matched and table in descending order*/
+ if (descend && i == level)
+ return table[i].frequency;
+ i++;
+ }
+ i--;
+
+ if (level > i || descend)
+ return ret;
+ level_index = i - level;
+
+ /*Scan the table in reverse order and match the level*/
+ while (i >= 0) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+ /*return if level matched*/
+ if (i == level_index)
+ return table[i].frequency;
+ i--;
+ }
+ return ret;
+}
+
+/**
+ * cpufreq_apply_cooling - function to apply frequency clipping.
+ * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
+ * clipping data.
+ * @cooling_state: value of the cooling state.
+ */
+static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
+ unsigned long cooling_state)
+{
+ unsigned int event, cpuid, clip_freq;
+ struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
+ unsigned int cpu = cpumask_any(maskPtr);
+
+
+ /* Check if the old cooling action is same as new cooling action */
+ if (cpufreq_device->cpufreq_state == cooling_state)
+ return 0;
+
+ clip_freq = get_cpu_frequency(cpu, cooling_state);
+ if (!clip_freq)
+ return -EINVAL;
+
+ cpufreq_device->cpufreq_state = cooling_state;
+ cpufreq_device->cpufreq_val = clip_freq;
+ notify_device = cpufreq_device;
+
+ if (cooling_state != 0)
+ event = CPUFREQ_COOLING_START;
+ else
+ event = CPUFREQ_COOLING_STOP;
+
+ blocking_notifier_call_chain(&cputherm_state_notifier_list,
+ event, &clip_freq);
+
+ for_each_cpu(cpuid, maskPtr) {
+ if (is_cpufreq_valid(cpuid))
+ cpufreq_update_policy(cpuid);
+ }
+
+ notify_device = NOTIFY_INVALID;
+
+ return 0;
+}
+
+/**
+ * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
+ * @nb: struct notifier_block * with callback info.
+ * @event: value showing cpufreq event for which this function invoked.
+ * @data: callback-specific data
+ */
+static int cpufreq_thermal_notifier(struct notifier_block *nb,
+ unsigned long event, void *data)
+{
+ struct cpufreq_policy *policy = data;
+ unsigned long max_freq = 0;
+
+ if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
+ return 0;
+
+ if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
+ max_freq = notify_device->cpufreq_val;
+
+ /* Never exceed user_policy.max*/
+ if (max_freq > policy->user_policy.max)
+ max_freq = policy->user_policy.max;
+
+ if (policy->max != max_freq)
+ cpufreq_verify_within_limits(policy, 0, max_freq);
+
+ return 0;
+}
+
+/*
+ * cpufreq cooling device callback functions are defined below
+ */
+
+/**
+ * cpufreq_get_max_state - callback function to get the max cooling state.
+ * @cdev: thermal cooling device pointer.
+ * @state: fill this variable with the max cooling state.
+ */
+static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
+ unsigned long *state)
+{
+ int ret = -EINVAL, i = 0;
+ struct cpufreq_cooling_device *cpufreq_device;
+ struct cpumask *maskPtr;
+ unsigned int cpu;
+ struct cpufreq_frequency_table *table;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
+ if (cpufreq_device && cpufreq_device->cool_dev == cdev)
+ break;
+ }
+ if (cpufreq_device == NULL)
+ goto return_get_max_state;
+
+ maskPtr = &cpufreq_device->allowed_cpus;
+ cpu = cpumask_any(maskPtr);
+ table = cpufreq_frequency_get_table(cpu);
+ if (!table) {
+ *state = 0;
+ ret = 0;
+ goto return_get_max_state;
+ }
+
+ while (table[i].frequency != CPUFREQ_TABLE_END) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+ i++;
+ }
+ if (i > 0) {
+ *state = --i;
+ ret = 0;
+ }
+
+return_get_max_state:
+ mutex_unlock(&cooling_cpufreq_lock);
+ return ret;
+}
+
+/**
+ * cpufreq_get_cur_state - callback function to get the current cooling state.
+ * @cdev: thermal cooling device pointer.
+ * @state: fill this variable with the current cooling state.
+ */
+static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
+ unsigned long *state)
+{
+ int ret = -EINVAL;
+ struct cpufreq_cooling_device *cpufreq_device;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
+ if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
+ *state = cpufreq_device->cpufreq_state;
+ ret = 0;
+ break;
+ }
+ }
+ mutex_unlock(&cooling_cpufreq_lock);
+
+ return ret;
+}
+
+/**
+ * cpufreq_set_cur_state - callback function to set the current cooling state.
+ * @cdev: thermal cooling device pointer.
+ * @state: set this variable to the current cooling state.
+ */
+static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
+ unsigned long state)
+{
+ int ret = -EINVAL;
+ struct cpufreq_cooling_device *cpufreq_device;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
+ if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
+ ret = 0;
+ break;
+ }
+ }
+ if (!ret)
+ ret = cpufreq_apply_cooling(cpufreq_device, state);
+
+ mutex_unlock(&cooling_cpufreq_lock);
+
+ return ret;
+}
+
+/* Bind cpufreq callbacks to thermal cooling device ops */
+static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
+ .get_max_state = cpufreq_get_max_state,
+ .get_cur_state = cpufreq_get_cur_state,
+ .set_cur_state = cpufreq_set_cur_state,
+};
+
+/* Notifier for cpufreq policy change */
+static struct notifier_block thermal_cpufreq_notifier_block = {
+ .notifier_call = cpufreq_thermal_notifier,
+};
+
+/**
+ * cpufreq_cooling_register - function to create cpufreq cooling device.
+ * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
+ */
+struct thermal_cooling_device *cpufreq_cooling_register(
+ struct cpumask *clip_cpus)
+{
+ struct thermal_cooling_device *cool_dev;
+ struct cpufreq_cooling_device *cpufreq_dev = NULL;
+ unsigned int cpufreq_dev_count = 0, min = 0, max = 0;
+ char dev_name[THERMAL_NAME_LENGTH];
+ int ret = 0, id = 0, i;
+ struct cpufreq_policy policy;
+
+ list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node)
+ cpufreq_dev_count++;
+
+ /*Verify that all the clip cpus have same freq_min, freq_max limit*/
+ for_each_cpu(i, clip_cpus) {
+ /*continue if cpufreq policy not found and not return error*/
+ if (!cpufreq_get_policy(&policy, i))
+ continue;
+ if (min == 0 && max == 0) {
+ min = policy.cpuinfo.min_freq;
+ max = policy.cpuinfo.max_freq;
+ } else {
+ if (min != policy.cpuinfo.min_freq ||
+ max != policy.cpuinfo.max_freq)
+ return ERR_PTR(-EINVAL);
+}
+ }
+ cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
+ GFP_KERNEL);
+ if (!cpufreq_dev)
+ return ERR_PTR(-ENOMEM);
+
+ cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
+
+ if (cpufreq_dev_count == 0)
+ mutex_init(&cooling_cpufreq_lock);
+
+ ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
+ if (ret) {
+ kfree(cpufreq_dev);
+ return ERR_PTR(-EINVAL);
+ }
+
+ sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
+
+ cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
+ &cpufreq_cooling_ops);
+ if (!cool_dev) {
+ release_idr(&cpufreq_idr, cpufreq_dev->id);
+ kfree(cpufreq_dev);
+ return ERR_PTR(-EINVAL);
+ }
+ cpufreq_dev->id = id;
+ cpufreq_dev->cool_dev = cool_dev;
+ cpufreq_dev->cpufreq_state = 0;
+ mutex_lock(&cooling_cpufreq_lock);
+ list_add_tail(&cpufreq_dev->node, &cooling_cpufreq_list);
+
+ /* Register the notifier for first cpufreq cooling device */
+ if (cpufreq_dev_count == 0)
+ cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
+ CPUFREQ_POLICY_NOTIFIER);
+
+ mutex_unlock(&cooling_cpufreq_lock);
+ return cool_dev;
+}
+EXPORT_SYMBOL(cpufreq_cooling_register);
+
+/**
+ * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
+ * @cdev: thermal cooling device pointer.
+ */
+void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
+{
+ struct cpufreq_cooling_device *cpufreq_dev = NULL;
+ unsigned int cpufreq_dev_count = 0;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node) {
+ if (cpufreq_dev && cpufreq_dev->cool_dev == cdev)
+ break;
+ cpufreq_dev_count++;
+ }
+
+ if (!cpufreq_dev || cpufreq_dev->cool_dev != cdev) {
+ mutex_unlock(&cooling_cpufreq_lock);
+ return;
+ }
+
+ list_del(&cpufreq_dev->node);
+
+ /* Unregister the notifier for the last cpufreq cooling device */
+ if (cpufreq_dev_count == 1) {
+ cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
+ CPUFREQ_POLICY_NOTIFIER);
+ }
+ mutex_unlock(&cooling_cpufreq_lock);
+ thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
+ release_idr(&cpufreq_idr, cpufreq_dev->id);
+ if (cpufreq_dev_count == 1)
+ mutex_destroy(&cooling_cpufreq_lock);
+ kfree(cpufreq_dev);
+}
+EXPORT_SYMBOL(cpufreq_cooling_unregister);
diff --git a/include/linux/cpu_cooling.h b/include/linux/cpu_cooling.h
new file mode 100644
index 0000000..555b917
--- /dev/null
+++ b/include/linux/cpu_cooling.h
@@ -0,0 +1,79 @@
+/*
+ * linux/include/linux/cpu_cooling.h
+ *
+ * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
+ * Copyright (C) 2012 Amit Daniel <[email protected]>
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * 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; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+
+#ifndef __CPU_COOLING_H__
+#define __CPU_COOLING_H__
+
+#include <linux/thermal.h>
+
+#define CPUFREQ_COOLING_START 0
+#define CPUFREQ_COOLING_STOP 1
+
+/**
+ * cputherm_register_notifier - Register a notifier with cpu cooling interface.
+ * @nb: struct notifier_block * with callback info.
+ * @list: integer value for which notification is needed. possible values are
+ * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
+ *
+ * This exported function registers a driver with cpu cooling layer. The driver
+ * will be notified when any cpu cooling action is called.
+ */
+int cputherm_register_notifier(struct notifier_block *nb, unsigned int list);
+
+/**
+ * cputherm_unregister_notifier - Un-register a notifier.
+ * @nb: struct notifier_block * with callback info.
+ * @list: integer value for which notification is needed. values possible are
+ * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
+ *
+ * This exported function un-registers a driver with cpu cooling layer.
+ */
+int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list);
+
+#ifdef CONFIG_CPU_THERMAL
+/**
+ * cpufreq_cooling_register - function to create cpufreq cooling device.
+ * @clip_cpus: cpumask of cpus where the frequency constraints will happen
+ */
+struct thermal_cooling_device *cpufreq_cooling_register(
+ struct cpumask *clip_cpus);
+
+/**
+ * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
+ * @cdev: thermal cooling device pointer.
+ */
+void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev);
+#else /* !CONFIG_CPU_THERMAL */
+static inline struct thermal_cooling_device *cpufreq_cooling_register(
+ struct cpumask *clip_cpus)
+{
+ return NULL;
+}
+static inline void cpufreq_cooling_unregister(
+ struct thermal_cooling_device *cdev)
+{
+ return;
+}
+#endif /* CONFIG_CPU_THERMAL */
+
+#endif /* __CPU_COOLING_H__ */
--
1.7.1
This movement is needed because the hwmon entries and corresponding sysfs
interface is a duplicate of utilities already provided by
driver/thermal/thermal_sys.c. The goal is to place it in thermal folder
and add necessary functions to use the in-kernel thermal interfaces.
Signed-off-by: Amit Daniel Kachhap <[email protected]>
Acked-by: Guenter Roeck <[email protected]>
Cc: SangWook Ju <[email protected]>
Cc: Durgadoss <[email protected]>
Cc: Len Brown <[email protected]>
Cc: Jean Delvare <[email protected]>
Cc: Kyungmin Park <[email protected]>
Cc: Kukjin Kim <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Amit Daniel Kachhap <[email protected]>
---
Documentation/hwmon/exynos4_tmu | 81 ----
Documentation/thermal/exynos_thermal | 52 +++
drivers/hwmon/Kconfig | 10 -
drivers/hwmon/Makefile | 1 -
drivers/hwmon/exynos4_tmu.c | 518 --------------------------
drivers/thermal/Kconfig | 7 +
drivers/thermal/Makefile | 1 +
drivers/thermal/exynos_thermal.c | 413 ++++++++++++++++++++
include/linux/platform_data/exynos4_tmu.h | 83 ----
include/linux/platform_data/exynos_thermal.h | 83 ++++
10 files changed, 556 insertions(+), 693 deletions(-)
delete mode 100644 Documentation/hwmon/exynos4_tmu
create mode 100644 Documentation/thermal/exynos_thermal
delete mode 100644 drivers/hwmon/exynos4_tmu.c
create mode 100644 drivers/thermal/exynos_thermal.c
delete mode 100644 include/linux/platform_data/exynos4_tmu.h
create mode 100644 include/linux/platform_data/exynos_thermal.h
diff --git a/Documentation/hwmon/exynos4_tmu b/Documentation/hwmon/exynos4_tmu
deleted file mode 100644
index c3c6b41..0000000
--- a/Documentation/hwmon/exynos4_tmu
+++ /dev/null
@@ -1,81 +0,0 @@
-Kernel driver exynos4_tmu
-=================
-
-Supported chips:
-* ARM SAMSUNG EXYNOS4 series of SoC
- Prefix: 'exynos4-tmu'
- Datasheet: Not publicly available
-
-Authors: Donggeun Kim <[email protected]>
-
-Description
------------
-
-This driver allows to read temperature inside SAMSUNG EXYNOS4 series of SoC.
-
-The chip only exposes the measured 8-bit temperature code value
-through a register.
-Temperature can be taken from the temperature code.
-There are three equations converting from temperature to temperature code.
-
-The three equations are:
- 1. Two point trimming
- Tc = (T - 25) * (TI2 - TI1) / (85 - 25) + TI1
-
- 2. One point trimming
- Tc = T + TI1 - 25
-
- 3. No trimming
- Tc = T + 50
-
- Tc: Temperature code, T: Temperature,
- TI1: Trimming info for 25 degree Celsius (stored at TRIMINFO register)
- Temperature code measured at 25 degree Celsius which is unchanged
- TI2: Trimming info for 85 degree Celsius (stored at TRIMINFO register)
- Temperature code measured at 85 degree Celsius which is unchanged
-
-TMU(Thermal Management Unit) in EXYNOS4 generates interrupt
-when temperature exceeds pre-defined levels.
-The maximum number of configurable threshold is four.
-The threshold levels are defined as follows:
- Level_0: current temperature > trigger_level_0 + threshold
- Level_1: current temperature > trigger_level_1 + threshold
- Level_2: current temperature > trigger_level_2 + threshold
- Level_3: current temperature > trigger_level_3 + threshold
-
- The threshold and each trigger_level are set
- through the corresponding registers.
-
-When an interrupt occurs, this driver notify user space of
-one of four threshold levels for the interrupt
-through kobject_uevent_env and sysfs_notify functions.
-Although an interrupt condition for level_0 can be set,
-it is not notified to user space through sysfs_notify function.
-
-Sysfs Interface
----------------
-name name of the temperature sensor
- RO
-
-temp1_input temperature
- RO
-
-temp1_max temperature for level_1 interrupt
- RO
-
-temp1_crit temperature for level_2 interrupt
- RO
-
-temp1_emergency temperature for level_3 interrupt
- RO
-
-temp1_max_alarm alarm for level_1 interrupt
- RO
-
-temp1_crit_alarm
- alarm for level_2 interrupt
- RO
-
-temp1_emergency_alarm
- alarm for level_3 interrupt
- RO
diff --git a/Documentation/thermal/exynos_thermal b/Documentation/thermal/exynos_thermal
new file mode 100644
index 0000000..2b46f67
--- /dev/null
+++ b/Documentation/thermal/exynos_thermal
@@ -0,0 +1,52 @@
+Kernel driver exynos4_tmu
+=================
+
+Supported chips:
+* ARM SAMSUNG EXYNOS4 series of SoC
+ Prefix: 'exynos4-tmu'
+ Datasheet: Not publicly available
+
+Authors: Donggeun Kim <[email protected]>
+
+Description
+-----------
+
+This driver allows to read temperature inside SAMSUNG EXYNOS4 series of SoC.
+
+The chip only exposes the measured 8-bit temperature code value
+through a register.
+Temperature can be taken from the temperature code.
+There are three equations converting from temperature to temperature code.
+
+The three equations are:
+ 1. Two point trimming
+ Tc = (T - 25) * (TI2 - TI1) / (85 - 25) + TI1
+
+ 2. One point trimming
+ Tc = T + TI1 - 25
+
+ 3. No trimming
+ Tc = T + 50
+
+ Tc: Temperature code, T: Temperature,
+ TI1: Trimming info for 25 degree Celsius (stored at TRIMINFO register)
+ Temperature code measured at 25 degree Celsius which is unchanged
+ TI2: Trimming info for 85 degree Celsius (stored at TRIMINFO register)
+ Temperature code measured at 85 degree Celsius which is unchanged
+
+TMU(Thermal Management Unit) in EXYNOS4 generates interrupt
+when temperature exceeds pre-defined levels.
+The maximum number of configurable threshold is four.
+The threshold levels are defined as follows:
+ Level_0: current temperature > trigger_level_0 + threshold
+ Level_1: current temperature > trigger_level_1 + threshold
+ Level_2: current temperature > trigger_level_2 + threshold
+ Level_3: current temperature > trigger_level_3 + threshold
+
+ The threshold and each trigger_level are set
+ through the corresponding registers.
+
+When an interrupt occurs, this driver notify kernel thermal framework
+with the function exynos4_report_trigger.
+Although an interrupt condition for level_0 can be set,
+it can be used to synchronize the cooling action.
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index b0a2e4c..84e02b4 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -324,16 +324,6 @@ config SENSORS_DA9052_ADC
This driver can also be built as module. If so, the module
will be called da9052-hwmon.
-config SENSORS_EXYNOS4_TMU
- tristate "Temperature sensor on Samsung EXYNOS4"
- depends on ARCH_EXYNOS4
- help
- If you say yes here you get support for TMU (Thermal Management
- Unit) on SAMSUNG EXYNOS4 series of SoC.
-
- This driver can also be built as a module. If so, the module
- will be called exynos4-tmu.
-
config SENSORS_I5K_AMB
tristate "FB-DIMM AMB temperature sensor on Intel 5000 series chipsets"
depends on PCI && EXPERIMENTAL
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index 7aa9811..3eafe48 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -49,7 +49,6 @@ obj-$(CONFIG_SENSORS_DS1621) += ds1621.o
obj-$(CONFIG_SENSORS_EMC1403) += emc1403.o
obj-$(CONFIG_SENSORS_EMC2103) += emc2103.o
obj-$(CONFIG_SENSORS_EMC6W201) += emc6w201.o
-obj-$(CONFIG_SENSORS_EXYNOS4_TMU) += exynos4_tmu.o
obj-$(CONFIG_SENSORS_F71805F) += f71805f.o
obj-$(CONFIG_SENSORS_F71882FG) += f71882fg.o
obj-$(CONFIG_SENSORS_F75375S) += f75375s.o
diff --git a/drivers/hwmon/exynos4_tmu.c b/drivers/hwmon/exynos4_tmu.c
deleted file mode 100644
index e912059..0000000
--- a/drivers/hwmon/exynos4_tmu.c
+++ /dev/null
@@ -1,518 +0,0 @@
-/*
- * exynos4_tmu.c - Samsung EXYNOS4 TMU (Thermal Management Unit)
- *
- * Copyright (C) 2011 Samsung Electronics
- * Donggeun Kim <[email protected]>
- *
- * 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 program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
-
-#include <linux/module.h>
-#include <linux/err.h>
-#include <linux/kernel.h>
-#include <linux/slab.h>
-#include <linux/platform_device.h>
-#include <linux/interrupt.h>
-#include <linux/clk.h>
-#include <linux/workqueue.h>
-#include <linux/sysfs.h>
-#include <linux/kobject.h>
-#include <linux/io.h>
-#include <linux/mutex.h>
-
-#include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
-
-#include <linux/platform_data/exynos4_tmu.h>
-
-#define EXYNOS4_TMU_REG_TRIMINFO 0x0
-#define EXYNOS4_TMU_REG_CONTROL 0x20
-#define EXYNOS4_TMU_REG_STATUS 0x28
-#define EXYNOS4_TMU_REG_CURRENT_TEMP 0x40
-#define EXYNOS4_TMU_REG_THRESHOLD_TEMP 0x44
-#define EXYNOS4_TMU_REG_TRIG_LEVEL0 0x50
-#define EXYNOS4_TMU_REG_TRIG_LEVEL1 0x54
-#define EXYNOS4_TMU_REG_TRIG_LEVEL2 0x58
-#define EXYNOS4_TMU_REG_TRIG_LEVEL3 0x5C
-#define EXYNOS4_TMU_REG_PAST_TEMP0 0x60
-#define EXYNOS4_TMU_REG_PAST_TEMP1 0x64
-#define EXYNOS4_TMU_REG_PAST_TEMP2 0x68
-#define EXYNOS4_TMU_REG_PAST_TEMP3 0x6C
-#define EXYNOS4_TMU_REG_INTEN 0x70
-#define EXYNOS4_TMU_REG_INTSTAT 0x74
-#define EXYNOS4_TMU_REG_INTCLEAR 0x78
-
-#define EXYNOS4_TMU_GAIN_SHIFT 8
-#define EXYNOS4_TMU_REF_VOLTAGE_SHIFT 24
-
-#define EXYNOS4_TMU_TRIM_TEMP_MASK 0xff
-#define EXYNOS4_TMU_CORE_ON 3
-#define EXYNOS4_TMU_CORE_OFF 2
-#define EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET 50
-#define EXYNOS4_TMU_TRIG_LEVEL0_MASK 0x1
-#define EXYNOS4_TMU_TRIG_LEVEL1_MASK 0x10
-#define EXYNOS4_TMU_TRIG_LEVEL2_MASK 0x100
-#define EXYNOS4_TMU_TRIG_LEVEL3_MASK 0x1000
-#define EXYNOS4_TMU_INTCLEAR_VAL 0x1111
-
-struct exynos4_tmu_data {
- struct exynos4_tmu_platform_data *pdata;
- struct device *hwmon_dev;
- struct resource *mem;
- void __iomem *base;
- int irq;
- struct work_struct irq_work;
- struct mutex lock;
- struct clk *clk;
- u8 temp_error1, temp_error2;
-};
-
-/*
- * TMU treats temperature as a mapped temperature code.
- * The temperature is converted differently depending on the calibration type.
- */
-static int temp_to_code(struct exynos4_tmu_data *data, u8 temp)
-{
- struct exynos4_tmu_platform_data *pdata = data->pdata;
- int temp_code;
-
- /* temp should range between 25 and 125 */
- if (temp < 25 || temp > 125) {
- temp_code = -EINVAL;
- goto out;
- }
-
- switch (pdata->cal_type) {
- case TYPE_TWO_POINT_TRIMMING:
- temp_code = (temp - 25) *
- (data->temp_error2 - data->temp_error1) /
- (85 - 25) + data->temp_error1;
- break;
- case TYPE_ONE_POINT_TRIMMING:
- temp_code = temp + data->temp_error1 - 25;
- break;
- default:
- temp_code = temp + EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET;
- break;
- }
-out:
- return temp_code;
-}
-
-/*
- * Calculate a temperature value from a temperature code.
- * The unit of the temperature is degree Celsius.
- */
-static int code_to_temp(struct exynos4_tmu_data *data, u8 temp_code)
-{
- struct exynos4_tmu_platform_data *pdata = data->pdata;
- int temp;
-
- /* temp_code should range between 75 and 175 */
- if (temp_code < 75 || temp_code > 175) {
- temp = -ENODATA;
- goto out;
- }
-
- switch (pdata->cal_type) {
- case TYPE_TWO_POINT_TRIMMING:
- temp = (temp_code - data->temp_error1) * (85 - 25) /
- (data->temp_error2 - data->temp_error1) + 25;
- break;
- case TYPE_ONE_POINT_TRIMMING:
- temp = temp_code - data->temp_error1 + 25;
- break;
- default:
- temp = temp_code - EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET;
- break;
- }
-out:
- return temp;
-}
-
-static int exynos4_tmu_initialize(struct platform_device *pdev)
-{
- struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
- struct exynos4_tmu_platform_data *pdata = data->pdata;
- unsigned int status, trim_info;
- int ret = 0, threshold_code;
-
- mutex_lock(&data->lock);
- clk_enable(data->clk);
-
- status = readb(data->base + EXYNOS4_TMU_REG_STATUS);
- if (!status) {
- ret = -EBUSY;
- goto out;
- }
-
- /* Save trimming info in order to perform calibration */
- trim_info = readl(data->base + EXYNOS4_TMU_REG_TRIMINFO);
- data->temp_error1 = trim_info & EXYNOS4_TMU_TRIM_TEMP_MASK;
- data->temp_error2 = ((trim_info >> 8) & EXYNOS4_TMU_TRIM_TEMP_MASK);
-
- /* Write temperature code for threshold */
- threshold_code = temp_to_code(data, pdata->threshold);
- if (threshold_code < 0) {
- ret = threshold_code;
- goto out;
- }
- writeb(threshold_code,
- data->base + EXYNOS4_TMU_REG_THRESHOLD_TEMP);
-
- writeb(pdata->trigger_levels[0],
- data->base + EXYNOS4_TMU_REG_TRIG_LEVEL0);
- writeb(pdata->trigger_levels[1],
- data->base + EXYNOS4_TMU_REG_TRIG_LEVEL1);
- writeb(pdata->trigger_levels[2],
- data->base + EXYNOS4_TMU_REG_TRIG_LEVEL2);
- writeb(pdata->trigger_levels[3],
- data->base + EXYNOS4_TMU_REG_TRIG_LEVEL3);
-
- writel(EXYNOS4_TMU_INTCLEAR_VAL,
- data->base + EXYNOS4_TMU_REG_INTCLEAR);
-out:
- clk_disable(data->clk);
- mutex_unlock(&data->lock);
-
- return ret;
-}
-
-static void exynos4_tmu_control(struct platform_device *pdev, bool on)
-{
- struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
- struct exynos4_tmu_platform_data *pdata = data->pdata;
- unsigned int con, interrupt_en;
-
- mutex_lock(&data->lock);
- clk_enable(data->clk);
-
- con = pdata->reference_voltage << EXYNOS4_TMU_REF_VOLTAGE_SHIFT |
- pdata->gain << EXYNOS4_TMU_GAIN_SHIFT;
- if (on) {
- con |= EXYNOS4_TMU_CORE_ON;
- interrupt_en = pdata->trigger_level3_en << 12 |
- pdata->trigger_level2_en << 8 |
- pdata->trigger_level1_en << 4 |
- pdata->trigger_level0_en;
- } else {
- con |= EXYNOS4_TMU_CORE_OFF;
- interrupt_en = 0; /* Disable all interrupts */
- }
- writel(interrupt_en, data->base + EXYNOS4_TMU_REG_INTEN);
- writel(con, data->base + EXYNOS4_TMU_REG_CONTROL);
-
- clk_disable(data->clk);
- mutex_unlock(&data->lock);
-}
-
-static int exynos4_tmu_read(struct exynos4_tmu_data *data)
-{
- u8 temp_code;
- int temp;
-
- mutex_lock(&data->lock);
- clk_enable(data->clk);
-
- temp_code = readb(data->base + EXYNOS4_TMU_REG_CURRENT_TEMP);
- temp = code_to_temp(data, temp_code);
-
- clk_disable(data->clk);
- mutex_unlock(&data->lock);
-
- return temp;
-}
-
-static void exynos4_tmu_work(struct work_struct *work)
-{
- struct exynos4_tmu_data *data = container_of(work,
- struct exynos4_tmu_data, irq_work);
-
- mutex_lock(&data->lock);
- clk_enable(data->clk);
-
- writel(EXYNOS4_TMU_INTCLEAR_VAL, data->base + EXYNOS4_TMU_REG_INTCLEAR);
-
- kobject_uevent(&data->hwmon_dev->kobj, KOBJ_CHANGE);
-
- enable_irq(data->irq);
-
- clk_disable(data->clk);
- mutex_unlock(&data->lock);
-}
-
-static irqreturn_t exynos4_tmu_irq(int irq, void *id)
-{
- struct exynos4_tmu_data *data = id;
-
- disable_irq_nosync(irq);
- schedule_work(&data->irq_work);
-
- return IRQ_HANDLED;
-}
-
-static ssize_t exynos4_tmu_show_name(struct device *dev,
- struct device_attribute *attr, char *buf)
-{
- return sprintf(buf, "exynos4-tmu\n");
-}
-
-static ssize_t exynos4_tmu_show_temp(struct device *dev,
- struct device_attribute *attr, char *buf)
-{
- struct exynos4_tmu_data *data = dev_get_drvdata(dev);
- int ret;
-
- ret = exynos4_tmu_read(data);
- if (ret < 0)
- return ret;
-
- /* convert from degree Celsius to millidegree Celsius */
- return sprintf(buf, "%d\n", ret * 1000);
-}
-
-static ssize_t exynos4_tmu_show_alarm(struct device *dev,
- struct device_attribute *devattr, char *buf)
-{
- struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
- struct exynos4_tmu_data *data = dev_get_drvdata(dev);
- struct exynos4_tmu_platform_data *pdata = data->pdata;
- int temp;
- unsigned int trigger_level;
-
- temp = exynos4_tmu_read(data);
- if (temp < 0)
- return temp;
-
- trigger_level = pdata->threshold + pdata->trigger_levels[attr->index];
-
- return sprintf(buf, "%d\n", !!(temp > trigger_level));
-}
-
-static ssize_t exynos4_tmu_show_level(struct device *dev,
- struct device_attribute *devattr, char *buf)
-{
- struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
- struct exynos4_tmu_data *data = dev_get_drvdata(dev);
- struct exynos4_tmu_platform_data *pdata = data->pdata;
- unsigned int temp = pdata->threshold +
- pdata->trigger_levels[attr->index];
-
- return sprintf(buf, "%u\n", temp * 1000);
-}
-
-static DEVICE_ATTR(name, S_IRUGO, exynos4_tmu_show_name, NULL);
-static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, exynos4_tmu_show_temp, NULL, 0);
-
-static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
- exynos4_tmu_show_alarm, NULL, 1);
-static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
- exynos4_tmu_show_alarm, NULL, 2);
-static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO,
- exynos4_tmu_show_alarm, NULL, 3);
-
-static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, exynos4_tmu_show_level, NULL, 1);
-static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, exynos4_tmu_show_level, NULL, 2);
-static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO,
- exynos4_tmu_show_level, NULL, 3);
-
-static struct attribute *exynos4_tmu_attributes[] = {
- &dev_attr_name.attr,
- &sensor_dev_attr_temp1_input.dev_attr.attr,
- &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
- &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
- &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
- &sensor_dev_attr_temp1_max.dev_attr.attr,
- &sensor_dev_attr_temp1_crit.dev_attr.attr,
- &sensor_dev_attr_temp1_emergency.dev_attr.attr,
- NULL,
-};
-
-static const struct attribute_group exynos4_tmu_attr_group = {
- .attrs = exynos4_tmu_attributes,
-};
-
-static int __devinit exynos4_tmu_probe(struct platform_device *pdev)
-{
- struct exynos4_tmu_data *data;
- struct exynos4_tmu_platform_data *pdata = pdev->dev.platform_data;
- int ret;
-
- if (!pdata) {
- dev_err(&pdev->dev, "No platform init data supplied.\n");
- return -ENODEV;
- }
-
- data = kzalloc(sizeof(struct exynos4_tmu_data), GFP_KERNEL);
- if (!data) {
- dev_err(&pdev->dev, "Failed to allocate driver structure\n");
- return -ENOMEM;
- }
-
- data->irq = platform_get_irq(pdev, 0);
- if (data->irq < 0) {
- ret = data->irq;
- dev_err(&pdev->dev, "Failed to get platform irq\n");
- goto err_free;
- }
-
- INIT_WORK(&data->irq_work, exynos4_tmu_work);
-
- data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (!data->mem) {
- ret = -ENOENT;
- dev_err(&pdev->dev, "Failed to get platform resource\n");
- goto err_free;
- }
-
- data->mem = request_mem_region(data->mem->start,
- resource_size(data->mem), pdev->name);
- if (!data->mem) {
- ret = -ENODEV;
- dev_err(&pdev->dev, "Failed to request memory region\n");
- goto err_free;
- }
-
- data->base = ioremap(data->mem->start, resource_size(data->mem));
- if (!data->base) {
- ret = -ENODEV;
- dev_err(&pdev->dev, "Failed to ioremap memory\n");
- goto err_mem_region;
- }
-
- ret = request_irq(data->irq, exynos4_tmu_irq,
- IRQF_TRIGGER_RISING,
- "exynos4-tmu", data);
- if (ret) {
- dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
- goto err_io_remap;
- }
-
- data->clk = clk_get(NULL, "tmu_apbif");
- if (IS_ERR(data->clk)) {
- ret = PTR_ERR(data->clk);
- dev_err(&pdev->dev, "Failed to get clock\n");
- goto err_irq;
- }
-
- data->pdata = pdata;
- platform_set_drvdata(pdev, data);
- mutex_init(&data->lock);
-
- ret = exynos4_tmu_initialize(pdev);
- if (ret) {
- dev_err(&pdev->dev, "Failed to initialize TMU\n");
- goto err_clk;
- }
-
- ret = sysfs_create_group(&pdev->dev.kobj, &exynos4_tmu_attr_group);
- if (ret) {
- dev_err(&pdev->dev, "Failed to create sysfs group\n");
- goto err_clk;
- }
-
- data->hwmon_dev = hwmon_device_register(&pdev->dev);
- if (IS_ERR(data->hwmon_dev)) {
- ret = PTR_ERR(data->hwmon_dev);
- dev_err(&pdev->dev, "Failed to register hwmon device\n");
- goto err_create_group;
- }
-
- exynos4_tmu_control(pdev, true);
-
- return 0;
-
-err_create_group:
- sysfs_remove_group(&pdev->dev.kobj, &exynos4_tmu_attr_group);
-err_clk:
- platform_set_drvdata(pdev, NULL);
- clk_put(data->clk);
-err_irq:
- free_irq(data->irq, data);
-err_io_remap:
- iounmap(data->base);
-err_mem_region:
- release_mem_region(data->mem->start, resource_size(data->mem));
-err_free:
- kfree(data);
-
- return ret;
-}
-
-static int __devexit exynos4_tmu_remove(struct platform_device *pdev)
-{
- struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
-
- exynos4_tmu_control(pdev, false);
-
- hwmon_device_unregister(data->hwmon_dev);
- sysfs_remove_group(&pdev->dev.kobj, &exynos4_tmu_attr_group);
-
- clk_put(data->clk);
-
- free_irq(data->irq, data);
-
- iounmap(data->base);
- release_mem_region(data->mem->start, resource_size(data->mem));
-
- platform_set_drvdata(pdev, NULL);
-
- kfree(data);
-
- return 0;
-}
-
-#ifdef CONFIG_PM_SLEEP
-static int exynos4_tmu_suspend(struct device *dev)
-{
- exynos4_tmu_control(to_platform_device(dev), false);
-
- return 0;
-}
-
-static int exynos4_tmu_resume(struct device *dev)
-{
- struct platform_device *pdev = to_platform_device(dev);
-
- exynos4_tmu_initialize(pdev);
- exynos4_tmu_control(pdev, true);
-
- return 0;
-}
-
-static SIMPLE_DEV_PM_OPS(exynos4_tmu_pm,
- exynos4_tmu_suspend, exynos4_tmu_resume);
-#define EXYNOS4_TMU_PM &exynos4_tmu_pm
-#else
-#define EXYNOS4_TMU_PM NULL
-#endif
-
-static struct platform_driver exynos4_tmu_driver = {
- .driver = {
- .name = "exynos4-tmu",
- .owner = THIS_MODULE,
- .pm = EXYNOS4_TMU_PM,
- },
- .probe = exynos4_tmu_probe,
- .remove = __devexit_p(exynos4_tmu_remove),
-};
-
-module_platform_driver(exynos4_tmu_driver);
-
-MODULE_DESCRIPTION("EXYNOS4 TMU Driver");
-MODULE_AUTHOR("Donggeun Kim <[email protected]>");
-MODULE_LICENSE("GPL");
-MODULE_ALIAS("platform:exynos4-tmu");
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 996003b..8f2b6ea 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -46,3 +46,10 @@ config RCAR_THERMAL
help
Enable this to plug the R-Car thermal sensor driver into the Linux
thermal framework
+
+config EXYNOS_THERMAL
+ tristate "Temperature sensor on Samsung EXYNOS"
+ depends on ARCH_EXYNOS4 && THERMAL
+ help
+ If you say yes here you get support for TMU (Thermal Managment
+ Unit) on SAMSUNG EXYNOS series of SoC.
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index aae59ad..885550d 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -6,3 +6,4 @@ obj-$(CONFIG_THERMAL) += thermal_sys.o
obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
+obj-$(CONFIG_EXYNOS_THERMAL) += exynos_thermal.o
diff --git a/drivers/thermal/exynos_thermal.c b/drivers/thermal/exynos_thermal.c
new file mode 100644
index 0000000..556d15b
--- /dev/null
+++ b/drivers/thermal/exynos_thermal.c
@@ -0,0 +1,413 @@
+/*
+ * exynos_thermal.c - Samsung EXYNOS TMU (Thermal Management Unit)
+ *
+ * Copyright (C) 2011 Samsung Electronics
+ * Donggeun Kim <[email protected]>
+ * Amit Daniel Kachhap <[email protected]>
+ *
+ * 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 program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <linux/interrupt.h>
+#include <linux/clk.h>
+#include <linux/workqueue.h>
+#include <linux/sysfs.h>
+#include <linux/kobject.h>
+#include <linux/io.h>
+#include <linux/mutex.h>
+
+#include <linux/platform_data/exynos_thermal.h>
+
+#define EXYNOS4_TMU_REG_TRIMINFO 0x0
+#define EXYNOS4_TMU_REG_CONTROL 0x20
+#define EXYNOS4_TMU_REG_STATUS 0x28
+#define EXYNOS4_TMU_REG_CURRENT_TEMP 0x40
+#define EXYNOS4_TMU_REG_THRESHOLD_TEMP 0x44
+#define EXYNOS4_TMU_REG_TRIG_LEVEL0 0x50
+#define EXYNOS4_TMU_REG_TRIG_LEVEL1 0x54
+#define EXYNOS4_TMU_REG_TRIG_LEVEL2 0x58
+#define EXYNOS4_TMU_REG_TRIG_LEVEL3 0x5C
+#define EXYNOS4_TMU_REG_PAST_TEMP0 0x60
+#define EXYNOS4_TMU_REG_PAST_TEMP1 0x64
+#define EXYNOS4_TMU_REG_PAST_TEMP2 0x68
+#define EXYNOS4_TMU_REG_PAST_TEMP3 0x6C
+#define EXYNOS4_TMU_REG_INTEN 0x70
+#define EXYNOS4_TMU_REG_INTSTAT 0x74
+#define EXYNOS4_TMU_REG_INTCLEAR 0x78
+
+#define EXYNOS4_TMU_GAIN_SHIFT 8
+#define EXYNOS4_TMU_REF_VOLTAGE_SHIFT 24
+
+#define EXYNOS4_TMU_TRIM_TEMP_MASK 0xff
+#define EXYNOS4_TMU_CORE_ON 3
+#define EXYNOS4_TMU_CORE_OFF 2
+#define EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET 50
+#define EXYNOS4_TMU_TRIG_LEVEL0_MASK 0x1
+#define EXYNOS4_TMU_TRIG_LEVEL1_MASK 0x10
+#define EXYNOS4_TMU_TRIG_LEVEL2_MASK 0x100
+#define EXYNOS4_TMU_TRIG_LEVEL3_MASK 0x1000
+#define EXYNOS4_TMU_INTCLEAR_VAL 0x1111
+
+struct exynos4_tmu_data {
+ struct exynos4_tmu_platform_data *pdata;
+ struct resource *mem;
+ void __iomem *base;
+ int irq;
+ struct work_struct irq_work;
+ struct mutex lock;
+ struct clk *clk;
+ u8 temp_error1, temp_error2;
+};
+
+/*
+ * TMU treats temperature as a mapped temperature code.
+ * The temperature is converted differently depending on the calibration type.
+ */
+static int temp_to_code(struct exynos4_tmu_data *data, u8 temp)
+{
+ struct exynos4_tmu_platform_data *pdata = data->pdata;
+ int temp_code;
+
+ /* temp should range between 25 and 125 */
+ if (temp < 25 || temp > 125) {
+ temp_code = -EINVAL;
+ goto out;
+ }
+
+ switch (pdata->cal_type) {
+ case TYPE_TWO_POINT_TRIMMING:
+ temp_code = (temp - 25) *
+ (data->temp_error2 - data->temp_error1) /
+ (85 - 25) + data->temp_error1;
+ break;
+ case TYPE_ONE_POINT_TRIMMING:
+ temp_code = temp + data->temp_error1 - 25;
+ break;
+ default:
+ temp_code = temp + EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET;
+ break;
+ }
+out:
+ return temp_code;
+}
+
+/*
+ * Calculate a temperature value from a temperature code.
+ * The unit of the temperature is degree Celsius.
+ */
+static int code_to_temp(struct exynos4_tmu_data *data, u8 temp_code)
+{
+ struct exynos4_tmu_platform_data *pdata = data->pdata;
+ int temp;
+
+ /* temp_code should range between 75 and 175 */
+ if (temp_code < 75 || temp_code > 175) {
+ temp = -ENODATA;
+ goto out;
+ }
+
+ switch (pdata->cal_type) {
+ case TYPE_TWO_POINT_TRIMMING:
+ temp = (temp_code - data->temp_error1) * (85 - 25) /
+ (data->temp_error2 - data->temp_error1) + 25;
+ break;
+ case TYPE_ONE_POINT_TRIMMING:
+ temp = temp_code - data->temp_error1 + 25;
+ break;
+ default:
+ temp = temp_code - EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET;
+ break;
+ }
+out:
+ return temp;
+}
+
+static int exynos4_tmu_initialize(struct platform_device *pdev)
+{
+ struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
+ struct exynos4_tmu_platform_data *pdata = data->pdata;
+ unsigned int status, trim_info;
+ int ret = 0, threshold_code;
+
+ mutex_lock(&data->lock);
+ clk_enable(data->clk);
+
+ status = readb(data->base + EXYNOS4_TMU_REG_STATUS);
+ if (!status) {
+ ret = -EBUSY;
+ goto out;
+ }
+
+ /* Save trimming info in order to perform calibration */
+ trim_info = readl(data->base + EXYNOS4_TMU_REG_TRIMINFO);
+ data->temp_error1 = trim_info & EXYNOS4_TMU_TRIM_TEMP_MASK;
+ data->temp_error2 = ((trim_info >> 8) & EXYNOS4_TMU_TRIM_TEMP_MASK);
+
+ /* Write temperature code for threshold */
+ threshold_code = temp_to_code(data, pdata->threshold);
+ if (threshold_code < 0) {
+ ret = threshold_code;
+ goto out;
+ }
+ writeb(threshold_code,
+ data->base + EXYNOS4_TMU_REG_THRESHOLD_TEMP);
+
+ writeb(pdata->trigger_levels[0],
+ data->base + EXYNOS4_TMU_REG_TRIG_LEVEL0);
+ writeb(pdata->trigger_levels[1],
+ data->base + EXYNOS4_TMU_REG_TRIG_LEVEL1);
+ writeb(pdata->trigger_levels[2],
+ data->base + EXYNOS4_TMU_REG_TRIG_LEVEL2);
+ writeb(pdata->trigger_levels[3],
+ data->base + EXYNOS4_TMU_REG_TRIG_LEVEL3);
+
+ writel(EXYNOS4_TMU_INTCLEAR_VAL,
+ data->base + EXYNOS4_TMU_REG_INTCLEAR);
+out:
+ clk_disable(data->clk);
+ mutex_unlock(&data->lock);
+
+ return ret;
+}
+
+static void exynos4_tmu_control(struct platform_device *pdev, bool on)
+{
+ struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
+ struct exynos4_tmu_platform_data *pdata = data->pdata;
+ unsigned int con, interrupt_en;
+
+ mutex_lock(&data->lock);
+ clk_enable(data->clk);
+
+ con = pdata->reference_voltage << EXYNOS4_TMU_REF_VOLTAGE_SHIFT |
+ pdata->gain << EXYNOS4_TMU_GAIN_SHIFT;
+ if (on) {
+ con |= EXYNOS4_TMU_CORE_ON;
+ interrupt_en = pdata->trigger_level3_en << 12 |
+ pdata->trigger_level2_en << 8 |
+ pdata->trigger_level1_en << 4 |
+ pdata->trigger_level0_en;
+ } else {
+ con |= EXYNOS4_TMU_CORE_OFF;
+ interrupt_en = 0; /* Disable all interrupts */
+ }
+ writel(interrupt_en, data->base + EXYNOS4_TMU_REG_INTEN);
+ writel(con, data->base + EXYNOS4_TMU_REG_CONTROL);
+
+ clk_disable(data->clk);
+ mutex_unlock(&data->lock);
+}
+
+static int exynos4_tmu_read(struct exynos4_tmu_data *data)
+{
+ u8 temp_code;
+ int temp;
+
+ mutex_lock(&data->lock);
+ clk_enable(data->clk);
+
+ temp_code = readb(data->base + EXYNOS4_TMU_REG_CURRENT_TEMP);
+ temp = code_to_temp(data, temp_code);
+
+ clk_disable(data->clk);
+ mutex_unlock(&data->lock);
+
+ return temp;
+}
+
+static void exynos4_tmu_work(struct work_struct *work)
+{
+ struct exynos4_tmu_data *data = container_of(work,
+ struct exynos4_tmu_data, irq_work);
+
+ mutex_lock(&data->lock);
+ clk_enable(data->clk);
+
+ writel(EXYNOS4_TMU_INTCLEAR_VAL, data->base + EXYNOS4_TMU_REG_INTCLEAR);
+
+ enable_irq(data->irq);
+
+ clk_disable(data->clk);
+ mutex_unlock(&data->lock);
+}
+
+static irqreturn_t exynos4_tmu_irq(int irq, void *id)
+{
+ struct exynos4_tmu_data *data = id;
+
+ disable_irq_nosync(irq);
+ schedule_work(&data->irq_work);
+
+ return IRQ_HANDLED;
+}
+
+static int __devinit exynos4_tmu_probe(struct platform_device *pdev)
+{
+ struct exynos4_tmu_data *data;
+ struct exynos4_tmu_platform_data *pdata = pdev->dev.platform_data;
+ int ret;
+
+ if (!pdata) {
+ dev_err(&pdev->dev, "No platform init data supplied.\n");
+ return -ENODEV;
+ }
+
+ data = kzalloc(sizeof(struct exynos4_tmu_data), GFP_KERNEL);
+ if (!data) {
+ dev_err(&pdev->dev, "Failed to allocate driver structure\n");
+ return -ENOMEM;
+ }
+
+ data->irq = platform_get_irq(pdev, 0);
+ if (data->irq < 0) {
+ ret = data->irq;
+ dev_err(&pdev->dev, "Failed to get platform irq\n");
+ goto err_free;
+ }
+
+ INIT_WORK(&data->irq_work, exynos4_tmu_work);
+
+ data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!data->mem) {
+ ret = -ENOENT;
+ dev_err(&pdev->dev, "Failed to get platform resource\n");
+ goto err_free;
+ }
+
+ data->mem = request_mem_region(data->mem->start,
+ resource_size(data->mem), pdev->name);
+ if (!data->mem) {
+ ret = -ENODEV;
+ dev_err(&pdev->dev, "Failed to request memory region\n");
+ goto err_free;
+ }
+
+ data->base = ioremap(data->mem->start, resource_size(data->mem));
+ if (!data->base) {
+ ret = -ENODEV;
+ dev_err(&pdev->dev, "Failed to ioremap memory\n");
+ goto err_mem_region;
+ }
+
+ ret = request_irq(data->irq, exynos4_tmu_irq,
+ IRQF_TRIGGER_RISING,
+ "exynos4-tmu", data);
+ if (ret) {
+ dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
+ goto err_io_remap;
+ }
+
+ data->clk = clk_get(NULL, "tmu_apbif");
+ if (IS_ERR(data->clk)) {
+ ret = PTR_ERR(data->clk);
+ dev_err(&pdev->dev, "Failed to get clock\n");
+ goto err_irq;
+ }
+
+ data->pdata = pdata;
+ platform_set_drvdata(pdev, data);
+ mutex_init(&data->lock);
+
+ ret = exynos4_tmu_initialize(pdev);
+ if (ret) {
+ dev_err(&pdev->dev, "Failed to initialize TMU\n");
+ goto err_clk;
+ }
+
+ exynos4_tmu_control(pdev, true);
+
+ return 0;
+err_clk:
+ platform_set_drvdata(pdev, NULL);
+ clk_put(data->clk);
+err_irq:
+ free_irq(data->irq, data);
+err_io_remap:
+ iounmap(data->base);
+err_mem_region:
+ release_mem_region(data->mem->start, resource_size(data->mem));
+err_free:
+ kfree(data);
+
+ return ret;
+}
+
+static int __devexit exynos4_tmu_remove(struct platform_device *pdev)
+{
+ struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
+
+ exynos4_tmu_control(pdev, false);
+
+ clk_put(data->clk);
+
+ free_irq(data->irq, data);
+
+ iounmap(data->base);
+ release_mem_region(data->mem->start, resource_size(data->mem));
+
+ platform_set_drvdata(pdev, NULL);
+
+ kfree(data);
+
+ return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int exynos4_tmu_suspend(struct device *dev)
+{
+ exynos4_tmu_control(to_platform_device(dev), false);
+
+ return 0;
+}
+
+static int exynos4_tmu_resume(struct device *dev)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+
+ exynos4_tmu_initialize(pdev);
+ exynos4_tmu_control(pdev, true);
+
+ return 0;
+}
+
+static SIMPLE_DEV_PM_OPS(exynos4_tmu_pm,
+ exynos4_tmu_suspend, exynos4_tmu_resume);
+#define EXYNOS4_TMU_PM (&exynos4_tmu_pm)
+#else
+#define EXYNOS4_TMU_PM NULL
+#endif
+
+static struct platform_driver exynos4_tmu_driver = {
+ .driver = {
+ .name = "exynos4-tmu",
+ .owner = THIS_MODULE,
+ .pm = EXYNOS4_TMU_PM,
+ },
+ .probe = exynos4_tmu_probe,
+ .remove = __devexit_p(exynos4_tmu_remove),
+};
+
+module_platform_driver(exynos4_tmu_driver);
+
+MODULE_DESCRIPTION("EXYNOS4 TMU Driver");
+MODULE_AUTHOR("Donggeun Kim <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:exynos4-tmu");
diff --git a/include/linux/platform_data/exynos4_tmu.h b/include/linux/platform_data/exynos4_tmu.h
deleted file mode 100644
index 39e038c..0000000
--- a/include/linux/platform_data/exynos4_tmu.h
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * exynos4_tmu.h - Samsung EXYNOS4 TMU (Thermal Management Unit)
- *
- * Copyright (C) 2011 Samsung Electronics
- * Donggeun Kim <[email protected]>
- *
- * 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 program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef _LINUX_EXYNOS4_TMU_H
-#define _LINUX_EXYNOS4_TMU_H
-
-enum calibration_type {
- TYPE_ONE_POINT_TRIMMING,
- TYPE_TWO_POINT_TRIMMING,
- TYPE_NONE,
-};
-
-/**
- * struct exynos4_tmu_platform_data
- * @threshold: basic temperature for generating interrupt
- * 25 <= threshold <= 125 [unit: degree Celsius]
- * @trigger_levels: array for each interrupt levels
- * [unit: degree Celsius]
- * 0: temperature for trigger_level0 interrupt
- * condition for trigger_level0 interrupt:
- * current temperature > threshold + trigger_levels[0]
- * 1: temperature for trigger_level1 interrupt
- * condition for trigger_level1 interrupt:
- * current temperature > threshold + trigger_levels[1]
- * 2: temperature for trigger_level2 interrupt
- * condition for trigger_level2 interrupt:
- * current temperature > threshold + trigger_levels[2]
- * 3: temperature for trigger_level3 interrupt
- * condition for trigger_level3 interrupt:
- * current temperature > threshold + trigger_levels[3]
- * @trigger_level0_en:
- * 1 = enable trigger_level0 interrupt,
- * 0 = disable trigger_level0 interrupt
- * @trigger_level1_en:
- * 1 = enable trigger_level1 interrupt,
- * 0 = disable trigger_level1 interrupt
- * @trigger_level2_en:
- * 1 = enable trigger_level2 interrupt,
- * 0 = disable trigger_level2 interrupt
- * @trigger_level3_en:
- * 1 = enable trigger_level3 interrupt,
- * 0 = disable trigger_level3 interrupt
- * @gain: gain of amplifier in the positive-TC generator block
- * 0 <= gain <= 15
- * @reference_voltage: reference voltage of amplifier
- * in the positive-TC generator block
- * 0 <= reference_voltage <= 31
- * @cal_type: calibration type for temperature
- *
- * This structure is required for configuration of exynos4_tmu driver.
- */
-struct exynos4_tmu_platform_data {
- u8 threshold;
- u8 trigger_levels[4];
- bool trigger_level0_en;
- bool trigger_level1_en;
- bool trigger_level2_en;
- bool trigger_level3_en;
-
- u8 gain;
- u8 reference_voltage;
-
- enum calibration_type cal_type;
-};
-#endif /* _LINUX_EXYNOS4_TMU_H */
diff --git a/include/linux/platform_data/exynos_thermal.h b/include/linux/platform_data/exynos_thermal.h
new file mode 100644
index 0000000..d6c3f93
--- /dev/null
+++ b/include/linux/platform_data/exynos_thermal.h
@@ -0,0 +1,83 @@
+/*
+ * exynos_thermal.h - Samsung EXYNOS4 TMU (Thermal Management Unit)
+ *
+ * Copyright (C) 2011 Samsung Electronics
+ * Donggeun Kim <[email protected]>
+ *
+ * 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 program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef _LINUX_EXYNOS_THERMAL_H
+#define _LINUX_EXYNOS_THERMAL_H
+
+enum calibration_type {
+ TYPE_ONE_POINT_TRIMMING,
+ TYPE_TWO_POINT_TRIMMING,
+ TYPE_NONE,
+};
+
+/**
+ * struct exynos4_tmu_platform_data
+ * @threshold: basic temperature for generating interrupt
+ * 25 <= threshold <= 125 [unit: degree Celsius]
+ * @trigger_levels: array for each interrupt levels
+ * [unit: degree Celsius]
+ * 0: temperature for trigger_level0 interrupt
+ * condition for trigger_level0 interrupt:
+ * current temperature > threshold + trigger_levels[0]
+ * 1: temperature for trigger_level1 interrupt
+ * condition for trigger_level1 interrupt:
+ * current temperature > threshold + trigger_levels[1]
+ * 2: temperature for trigger_level2 interrupt
+ * condition for trigger_level2 interrupt:
+ * current temperature > threshold + trigger_levels[2]
+ * 3: temperature for trigger_level3 interrupt
+ * condition for trigger_level3 interrupt:
+ * current temperature > threshold + trigger_levels[3]
+ * @trigger_level0_en:
+ * 1 = enable trigger_level0 interrupt,
+ * 0 = disable trigger_level0 interrupt
+ * @trigger_level1_en:
+ * 1 = enable trigger_level1 interrupt,
+ * 0 = disable trigger_level1 interrupt
+ * @trigger_level2_en:
+ * 1 = enable trigger_level2 interrupt,
+ * 0 = disable trigger_level2 interrupt
+ * @trigger_level3_en:
+ * 1 = enable trigger_level3 interrupt,
+ * 0 = disable trigger_level3 interrupt
+ * @gain: gain of amplifier in the positive-TC generator block
+ * 0 <= gain <= 15
+ * @reference_voltage: reference voltage of amplifier
+ * in the positive-TC generator block
+ * 0 <= reference_voltage <= 31
+ * @cal_type: calibration type for temperature
+ *
+ * This structure is required for configuration of exynos4_tmu driver.
+ */
+struct exynos4_tmu_platform_data {
+ u8 threshold;
+ u8 trigger_levels[4];
+ bool trigger_level0_en;
+ bool trigger_level1_en;
+ bool trigger_level2_en;
+ bool trigger_level3_en;
+
+ u8 gain;
+ u8 reference_voltage;
+
+ enum calibration_type cal_type;
+};
+#endif /* _LINUX_EXYNOS_THERMAL_H */
--
1.7.1
devm_* functions are used to replace kzalloc, request_mem_region, ioremap
and request_irq functions in probe call. With the usage of devm_* functions
explicit freeing and unmapping is not required.
Signed-off-by: Sachin Kamat <[email protected]>
Signed-off-by: Sachin Kamat <[email protected]>
Signed-off-by: Amit Daniel Kachhap <[email protected]>
---
drivers/thermal/exynos_thermal.c | 45 +++++++------------------------------
1 files changed, 9 insertions(+), 36 deletions(-)
diff --git a/drivers/thermal/exynos_thermal.c b/drivers/thermal/exynos_thermal.c
index 03a99e4..e84acde 100644
--- a/drivers/thermal/exynos_thermal.c
+++ b/drivers/thermal/exynos_thermal.c
@@ -842,7 +842,8 @@ static int __devinit exynos_tmu_probe(struct platform_device *pdev)
dev_err(&pdev->dev, "No platform init data supplied.\n");
return -ENODEV;
}
- data = kzalloc(sizeof(struct exynos_tmu_data), GFP_KERNEL);
+ data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
+ GFP_KERNEL);
if (!data) {
dev_err(&pdev->dev, "Failed to allocate driver structure\n");
return -ENOMEM;
@@ -850,47 +851,35 @@ static int __devinit exynos_tmu_probe(struct platform_device *pdev)
data->irq = platform_get_irq(pdev, 0);
if (data->irq < 0) {
- ret = data->irq;
dev_err(&pdev->dev, "Failed to get platform irq\n");
- goto err_free;
+ return data->irq;
}
INIT_WORK(&data->irq_work, exynos_tmu_work);
data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!data->mem) {
- ret = -ENOENT;
dev_err(&pdev->dev, "Failed to get platform resource\n");
- goto err_free;
+ return -ENOENT;
}
- data->mem = request_mem_region(data->mem->start,
- resource_size(data->mem), pdev->name);
- if (!data->mem) {
- ret = -ENODEV;
- dev_err(&pdev->dev, "Failed to request memory region\n");
- goto err_free;
- }
-
- data->base = ioremap(data->mem->start, resource_size(data->mem));
+ data->base = devm_request_and_ioremap(&pdev->dev, data->mem);
if (!data->base) {
- ret = -ENODEV;
dev_err(&pdev->dev, "Failed to ioremap memory\n");
- goto err_mem_region;
+ return -ENODEV;
}
- ret = request_irq(data->irq, exynos_tmu_irq,
+ ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
IRQF_TRIGGER_RISING, "exynos-tmu", data);
if (ret) {
dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
- goto err_io_remap;
+ return ret;
}
data->clk = clk_get(NULL, "tmu_apbif");
if (IS_ERR(data->clk)) {
- ret = PTR_ERR(data->clk);
dev_err(&pdev->dev, "Failed to get clock\n");
- goto err_irq;
+ return PTR_ERR(data->clk);
}
if (pdata->type == SOC_ARCH_EXYNOS ||
@@ -942,15 +931,6 @@ static int __devinit exynos_tmu_probe(struct platform_device *pdev)
err_clk:
platform_set_drvdata(pdev, NULL);
clk_put(data->clk);
-err_irq:
- free_irq(data->irq, data);
-err_io_remap:
- iounmap(data->base);
-err_mem_region:
- release_mem_region(data->mem->start, resource_size(data->mem));
-err_free:
- kfree(data);
-
return ret;
}
@@ -964,15 +944,8 @@ static int __devexit exynos_tmu_remove(struct platform_device *pdev)
clk_put(data->clk);
- free_irq(data->irq, data);
-
- iounmap(data->base);
- release_mem_region(data->mem->start, resource_size(data->mem));
-
platform_set_drvdata(pdev, NULL);
- kfree(data);
-
return 0;
}
--
1.7.1
This code added creates a link between temperature sensors, linux thermal
framework and cooling devices for samsung exynos platform. This layer
monitors the temperature from the sensor and informs the generic thermal
layer to take the necessary cooling action.
[[email protected]: fix comment layout]
Signed-off-by: Amit Daniel Kachhap <[email protected]>
Acked-by: Guenter Roeck <[email protected]>
Cc: SangWook Ju <[email protected]>
Cc: Durgadoss <[email protected]>
Cc: Len Brown <[email protected]>
Cc: Jean Delvare <[email protected]>
Cc: Kyungmin Park <[email protected]>
Cc: Kukjin Kim <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Amit Daniel Kachhap <[email protected]>
---
drivers/thermal/exynos_thermal.c | 406 +++++++++++++++++++++++++-
include/linux/platform_data/exynos_thermal.h | 22 ++
2 files changed, 426 insertions(+), 2 deletions(-)
diff --git a/drivers/thermal/exynos_thermal.c b/drivers/thermal/exynos_thermal.c
index c9a33dd..e79cdc9 100644
--- a/drivers/thermal/exynos_thermal.c
+++ b/drivers/thermal/exynos_thermal.c
@@ -34,6 +34,9 @@
#include <linux/io.h>
#include <linux/mutex.h>
#include <linux/platform_data/exynos_thermal.h>
+#include <linux/thermal.h>
+#include <linux/cpufreq.h>
+#include <linux/cpu_cooling.h>
#include <linux/of.h>
#include <plat/cpu.h>
@@ -94,6 +97,7 @@
#define ACTIVE_INTERVAL 500
#define IDLE_INTERVAL 10000
+#define MCELSIUS 1000
/* CPU Zone information */
#define PANIC_ZONE 4
@@ -104,6 +108,8 @@
#define GET_ZONE(trip) (trip + 2)
#define GET_TRIP(zone) (zone - 2)
+#define EXYNOS_ZONE_COUNT 3
+
struct exynos_tmu_data {
struct exynos_tmu_platform_data *pdata;
struct resource *mem;
@@ -116,6 +122,371 @@ struct exynos_tmu_data {
u8 temp_error1, temp_error2;
};
+struct thermal_trip_point_conf {
+ int trip_val[MAX_TRIP_COUNT];
+ int trip_count;
+};
+
+struct thermal_cooling_conf {
+ struct freq_clip_table freq_data[MAX_TRIP_COUNT];
+ int freq_clip_count;
+};
+
+struct thermal_sensor_conf {
+ char name[SENSOR_NAME_LEN];
+ int (*read_temperature)(void *data);
+ struct thermal_trip_point_conf trip_data;
+ struct thermal_cooling_conf cooling_data;
+ void *private_data;
+};
+
+struct exynos_thermal_zone {
+ enum thermal_device_mode mode;
+ struct thermal_zone_device *therm_dev;
+ struct thermal_cooling_device *cool_dev[MAX_COOLING_DEVICE];
+ unsigned int cool_dev_size;
+ struct platform_device *exynos4_dev;
+ struct thermal_sensor_conf *sensor_conf;
+ bool bind;
+};
+
+static struct exynos_thermal_zone *th_zone;
+static void exynos_unregister_thermal(void);
+static int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf);
+
+/* Get mode callback functions for thermal zone */
+static int exynos_get_mode(struct thermal_zone_device *thermal,
+ enum thermal_device_mode *mode)
+{
+ if (th_zone)
+ *mode = th_zone->mode;
+ return 0;
+}
+
+/* Set mode callback functions for thermal zone */
+static int exynos_set_mode(struct thermal_zone_device *thermal,
+ enum thermal_device_mode mode)
+{
+ if (!th_zone->therm_dev) {
+ pr_notice("thermal zone not registered\n");
+ return 0;
+ }
+
+ mutex_lock(&th_zone->therm_dev->lock);
+
+ if (mode == THERMAL_DEVICE_ENABLED)
+ th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
+ else
+ th_zone->therm_dev->polling_delay = 0;
+
+ mutex_unlock(&th_zone->therm_dev->lock);
+
+ th_zone->mode = mode;
+ thermal_zone_device_update(th_zone->therm_dev);
+ pr_info("thermal polling set for duration=%d msec\n",
+ th_zone->therm_dev->polling_delay);
+ return 0;
+}
+
+
+/* Get trip type callback functions for thermal zone */
+static int exynos_get_trip_type(struct thermal_zone_device *thermal, int trip,
+ enum thermal_trip_type *type)
+{
+ switch (GET_ZONE(trip)) {
+ case MONITOR_ZONE:
+ case WARN_ZONE:
+ *type = THERMAL_TRIP_ACTIVE;
+ break;
+ case PANIC_ZONE:
+ *type = THERMAL_TRIP_CRITICAL;
+ break;
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
+/* Get trip temperature callback functions for thermal zone */
+static int exynos_get_trip_temp(struct thermal_zone_device *thermal, int trip,
+ unsigned long *temp)
+{
+ if (trip < GET_TRIP(MONITOR_ZONE) || trip > GET_TRIP(PANIC_ZONE))
+ return -EINVAL;
+
+ *temp = th_zone->sensor_conf->trip_data.trip_val[trip];
+ /* convert the temperature into millicelsius */
+ *temp = *temp * MCELSIUS;
+
+ return 0;
+}
+
+/* Get critical temperature callback functions for thermal zone */
+static int exynos_get_crit_temp(struct thermal_zone_device *thermal,
+ unsigned long *temp)
+{
+ int ret;
+ /* Panic zone */
+ ret = exynos_get_trip_temp(thermal, GET_TRIP(PANIC_ZONE), temp);
+ return ret;
+}
+
+static int exynos_get_frequency_level(unsigned int cpu, unsigned int freq)
+{
+ int i = 0, ret = -EINVAL;
+ struct cpufreq_frequency_table *table = NULL;
+#ifdef CONFIG_CPU_FREQ
+ table = cpufreq_frequency_get_table(cpu);
+#endif
+ if (!table)
+ return ret;
+
+ while (table[i].frequency != CPUFREQ_TABLE_END) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+ if (table[i].frequency == freq)
+ return i;
+ i++;
+ }
+ return ret;
+}
+
+/* Bind callback functions for thermal zone */
+static int exynos_bind(struct thermal_zone_device *thermal,
+ struct thermal_cooling_device *cdev)
+{
+ int ret = 0, i, tab_size, level;
+ struct freq_clip_table *tab_ptr, *clip_data;
+ struct thermal_sensor_conf *data = th_zone->sensor_conf;
+
+ tab_ptr = (struct freq_clip_table *)data->cooling_data.freq_data;
+ tab_size = data->cooling_data.freq_clip_count;
+
+ if (tab_ptr == NULL || tab_size == 0)
+ return -EINVAL;
+
+ /* find the cooling device registered*/
+ for (i = 0; i < th_zone->cool_dev_size; i++)
+ if (cdev == th_zone->cool_dev[i])
+ break;
+
+ /* No matching cooling device */
+ if (i == th_zone->cool_dev_size)
+ return 0;
+
+ /* Bind the thermal zone to the cpufreq cooling device */
+ for (i = 0; i < tab_size; i++) {
+ clip_data = (struct freq_clip_table *)&(tab_ptr[i]);
+ level = exynos_get_frequency_level(0, clip_data->freq_clip_max);
+ if (level < 0)
+ return 0;
+ switch (GET_ZONE(i)) {
+ case MONITOR_ZONE:
+ case WARN_ZONE:
+ if (thermal_zone_bind_cooling_device(thermal, i, cdev,
+ level, level)) {
+ pr_err("error binding cdev inst %d\n", i);
+ ret = -EINVAL;
+ }
+ th_zone->bind = true;
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ }
+
+ return ret;
+}
+
+/* Unbind callback functions for thermal zone */
+static int exynos_unbind(struct thermal_zone_device *thermal,
+ struct thermal_cooling_device *cdev)
+{
+ int ret = 0, i, tab_size;
+ struct thermal_sensor_conf *data = th_zone->sensor_conf;
+
+ if (th_zone->bind == false)
+ return 0;
+
+ tab_size = data->cooling_data.freq_clip_count;
+
+ if (tab_size == 0)
+ return -EINVAL;
+
+ /* find the cooling device registered*/
+ for (i = 0; i < th_zone->cool_dev_size; i++)
+ if (cdev == th_zone->cool_dev[i])
+ break;
+
+ /* No matching cooling device */
+ if (i == th_zone->cool_dev_size)
+ return 0;
+
+ /* Bind the thermal zone to the cpufreq cooling device */
+ for (i = 0; i < tab_size; i++) {
+ switch (GET_ZONE(i)) {
+ case MONITOR_ZONE:
+ case WARN_ZONE:
+ if (thermal_zone_unbind_cooling_device(thermal, i,
+ cdev)) {
+ pr_err("error unbinding cdev inst=%d\n", i);
+ ret = -EINVAL;
+ }
+ th_zone->bind = false;
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ }
+ return ret;
+}
+
+/* Get temperature callback functions for thermal zone */
+static int exynos_get_temp(struct thermal_zone_device *thermal,
+ unsigned long *temp)
+{
+ void *data;
+
+ if (!th_zone->sensor_conf) {
+ pr_info("Temperature sensor not initialised\n");
+ return -EINVAL;
+ }
+ data = th_zone->sensor_conf->private_data;
+ *temp = th_zone->sensor_conf->read_temperature(data);
+ /* convert the temperature into millicelsius */
+ *temp = *temp * MCELSIUS;
+ return 0;
+}
+
+/* Get the temperature trend */
+static int exynos_get_trend(struct thermal_zone_device *thermal,
+ int trip, enum thermal_trend *trend)
+{
+ if (thermal->temperature >= trip)
+ *trend = THERMAL_TREND_RAISING;
+ else
+ *trend = THERMAL_TREND_DROPPING;
+
+ return 0;
+}
+/* Operation callback functions for thermal zone */
+static struct thermal_zone_device_ops const exynos_dev_ops = {
+ .bind = exynos_bind,
+ .unbind = exynos_unbind,
+ .get_temp = exynos_get_temp,
+ .get_trend = exynos_get_trend,
+ .get_mode = exynos_get_mode,
+ .set_mode = exynos_set_mode,
+ .get_trip_type = exynos_get_trip_type,
+ .get_trip_temp = exynos_get_trip_temp,
+ .get_crit_temp = exynos_get_crit_temp,
+};
+
+/*
+ * This function may be called from interrupt based temperature sensor
+ * when threshold is changed.
+ */
+static void exynos_report_trigger(void)
+{
+ unsigned int i;
+ char data[10];
+ char *envp[] = { data, NULL };
+
+ if (!th_zone || !th_zone->therm_dev)
+ return;
+ if (th_zone->bind == false) {
+ for (i = 0; i < th_zone->cool_dev_size; i++) {
+ if (!th_zone->cool_dev[i])
+ continue;
+ exynos_bind(th_zone->therm_dev,
+ th_zone->cool_dev[i]);
+ }
+ }
+
+ thermal_zone_device_update(th_zone->therm_dev);
+
+ mutex_lock(&th_zone->therm_dev->lock);
+ /* Find the level for which trip happened */
+ for (i = 0; i < th_zone->sensor_conf->trip_data.trip_count; i++) {
+ if (th_zone->therm_dev->last_temperature <
+ th_zone->sensor_conf->trip_data.trip_val[i] * MCELSIUS)
+ break;
+ }
+
+ if (th_zone->mode == THERMAL_DEVICE_ENABLED) {
+ if (i > 0)
+ th_zone->therm_dev->polling_delay = ACTIVE_INTERVAL;
+ else
+ th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
+ }
+
+ snprintf(data, sizeof(data), "%u", i);
+ kobject_uevent_env(&th_zone->therm_dev->device.kobj, KOBJ_CHANGE, envp);
+ mutex_unlock(&th_zone->therm_dev->lock);
+}
+
+/* Register with the in-kernel thermal management */
+static int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf)
+{
+ int ret;
+ struct cpumask mask_val;
+
+ if (!sensor_conf || !sensor_conf->read_temperature) {
+ pr_err("Temperature sensor not initialised\n");
+ return -EINVAL;
+ }
+
+ th_zone = kzalloc(sizeof(struct exynos_thermal_zone), GFP_KERNEL);
+ if (!th_zone)
+ return -ENOMEM;
+
+ th_zone->sensor_conf = sensor_conf;
+ cpumask_set_cpu(0, &mask_val);
+ th_zone->cool_dev[0] = cpufreq_cooling_register(&mask_val);
+ if (IS_ERR(th_zone->cool_dev[0])) {
+ pr_err("Failed to register cpufreq cooling device\n");
+ ret = -EINVAL;
+ goto err_unregister;
+ }
+ th_zone->cool_dev_size++;
+
+ th_zone->therm_dev = thermal_zone_device_register(sensor_conf->name,
+ EXYNOS_ZONE_COUNT, 0, NULL, &exynos_dev_ops, 0,
+ IDLE_INTERVAL);
+
+ if (IS_ERR(th_zone->therm_dev)) {
+ pr_err("Failed to register thermal zone device\n");
+ ret = -EINVAL;
+ goto err_unregister;
+ }
+ th_zone->mode = THERMAL_DEVICE_ENABLED;
+
+ pr_info("Exynos: Kernel Thermal management registered\n");
+
+ return 0;
+
+err_unregister:
+ exynos_unregister_thermal();
+ return ret;
+}
+
+/* Un-Register with the in-kernel thermal management */
+static void exynos_unregister_thermal(void)
+{
+ int i;
+
+ if (th_zone && th_zone->therm_dev)
+ thermal_zone_device_unregister(th_zone->therm_dev);
+
+ for (i = 0; i < th_zone->cool_dev_size; i++) {
+ if (th_zone && th_zone->cool_dev[i])
+ cpufreq_cooling_unregister(th_zone->cool_dev[i]);
+ }
+
+ kfree(th_zone);
+ pr_info("Exynos: Kernel Thermal management unregistered\n");
+}
+
/*
* TMU treats temperature as a mapped temperature code.
* The temperature is converted differently depending on the calibration type.
@@ -336,6 +707,7 @@ static void exynos_tmu_work(struct work_struct *work)
clk_disable(data->clk);
mutex_unlock(&data->lock);
+ exynos_report_trigger();
enable_irq(data->irq);
}
@@ -348,12 +720,16 @@ static irqreturn_t exynos_tmu_irq(int irq, void *id)
return IRQ_HANDLED;
}
-
+static struct thermal_sensor_conf exynos_sensor_conf = {
+ .name = "exynos-therm",
+ .read_temperature = (int (*)(void *))exynos_tmu_read,
+}
+;
static int __devinit exynos_tmu_probe(struct platform_device *pdev)
{
struct exynos_tmu_data *data;
struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
- int ret;
+ int ret, i;
if (!pdata) {
dev_err(&pdev->dev, "No platform init data supplied.\n");
@@ -431,6 +807,30 @@ static int __devinit exynos_tmu_probe(struct platform_device *pdev)
exynos_tmu_control(pdev, true);
+ /* Register the sensor with thermal management interface */
+ (&exynos_sensor_conf)->private_data = data;
+ exynos_sensor_conf.trip_data.trip_count = pdata->trigger_level0_en +
+ pdata->trigger_level1_en + pdata->trigger_level2_en +
+ pdata->trigger_level3_en;
+
+ for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++)
+ exynos_sensor_conf.trip_data.trip_val[i] =
+ pdata->threshold + pdata->trigger_levels[i];
+
+ exynos_sensor_conf.cooling_data.freq_clip_count =
+ pdata->freq_tab_count;
+ for (i = 0; i < pdata->freq_tab_count; i++) {
+ exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max =
+ pdata->freq_tab[i].freq_clip_max;
+ exynos_sensor_conf.cooling_data.freq_data[i].temp_level =
+ pdata->freq_tab[i].temp_level;
+ }
+
+ ret = exynos_register_thermal(&exynos_sensor_conf);
+ if (ret) {
+ dev_err(&pdev->dev, "Failed to register thermal interface\n");
+ goto err_clk;
+ }
return 0;
err_clk:
platform_set_drvdata(pdev, NULL);
@@ -453,6 +853,8 @@ static int __devexit exynos_tmu_remove(struct platform_device *pdev)
exynos_tmu_control(pdev, false);
+ exynos_unregister_thermal();
+
clk_put(data->clk);
free_irq(data->irq, data);
diff --git a/include/linux/platform_data/exynos_thermal.h b/include/linux/platform_data/exynos_thermal.h
index a9e960a..a7bdb2f 100644
--- a/include/linux/platform_data/exynos_thermal.h
+++ b/include/linux/platform_data/exynos_thermal.h
@@ -21,6 +21,7 @@
#ifndef _LINUX_EXYNOS_THERMAL_H
#define _LINUX_EXYNOS_THERMAL_H
+#include <linux/cpu_cooling.h>
enum calibration_type {
TYPE_ONE_POINT_TRIMMING,
@@ -33,6 +34,22 @@ enum soc_type {
SOC_ARCH_EXYNOS,
};
/**
+ * struct freq_clip_table
+ * @freq_clip_max: maximum frequency allowed for this cooling state.
+ * @temp_level: Temperature level at which the temperature clipping will
+ * happen.
+ * @mask_val: cpumask of the allowed cpu's where the clipping will take place.
+ *
+ * This structure is required to be filled and passed to the
+ * cpufreq_cooling_unregister function.
+ */
+struct freq_clip_table {
+ unsigned int freq_clip_max;
+ unsigned int temp_level;
+ const struct cpumask *mask_val;
+};
+
+/**
* struct exynos_tmu_platform_data
* @threshold: basic temperature for generating interrupt
* 25 <= threshold <= 125 [unit: degree Celsius]
@@ -72,6 +89,9 @@ enum soc_type {
* @type: determines the type of SOC
* @efuse_value: platform defined fuse value
* @cal_type: calibration type for temperature
+ * @freq_clip_table: Table representing frequency reduction percentage.
+ * @freq_tab_count: Count of the above table as frequency reduction may
+ * applicable to only some of the trigger levels.
*
* This structure is required for configuration of exynos_tmu driver.
*/
@@ -90,5 +110,7 @@ struct exynos_tmu_platform_data {
enum calibration_type cal_type;
enum soc_type type;
+ struct freq_clip_table freq_tab[4];
+ unsigned int freq_tab_count;
};
#endif /* _LINUX_EXYNOS_THERMAL_H */
--
1.7.1
Add necessary default platform data support needed for TMU driver. The
supplied dt/non-dt values are tested for origen exynos4210 and smdk exynos5250
platforms and only compile tested for exynos4412.
Signed-off-by: Amit Daniel Kachhap <[email protected]>
Acked-by: Guenter Roeck <[email protected]>
Cc: SangWook Ju <[email protected]>
Cc: Durgadoss <[email protected]>
Cc: Len Brown <[email protected]>
Cc: Jean Delvare <[email protected]>
Cc: jonghwa lee <[email protected]>
Cc: Kyungmin Park <[email protected]>
Cc: Kukjin Kim <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Amit Daniel Kachhap <[email protected]>
---
drivers/thermal/exynos_thermal.c | 111 +++++++++++++++++++++++++++++++++++++-
1 files changed, 110 insertions(+), 1 deletions(-)
diff --git a/drivers/thermal/exynos_thermal.c b/drivers/thermal/exynos_thermal.c
index e79cdc9..03a99e4 100644
--- a/drivers/thermal/exynos_thermal.c
+++ b/drivers/thermal/exynos_thermal.c
@@ -723,14 +723,121 @@ static irqreturn_t exynos_tmu_irq(int irq, void *id)
static struct thermal_sensor_conf exynos_sensor_conf = {
.name = "exynos-therm",
.read_temperature = (int (*)(void *))exynos_tmu_read,
+};
+
+#if defined(CONFIG_CPU_EXYNOS4210)
+static struct exynos_tmu_platform_data const exynos4210_default_tmu_data = {
+ .threshold = 80,
+ .trigger_levels[0] = 5,
+ .trigger_levels[1] = 20,
+ .trigger_levels[2] = 30,
+ .trigger_level0_en = 1,
+ .trigger_level1_en = 1,
+ .trigger_level2_en = 1,
+ .trigger_level3_en = 0,
+ .gain = 15,
+ .reference_voltage = 7,
+ .cal_type = TYPE_ONE_POINT_TRIMMING,
+ .freq_tab[0] = {
+ .freq_clip_max = 800 * 1000,
+ .temp_level = 85,
+ },
+ .freq_tab[1] = {
+ .freq_clip_max = 200 * 1000,
+ .temp_level = 100,
+ },
+ .freq_tab_count = 2,
+ .type = SOC_ARCH_EXYNOS4210,
+};
+#define EXYNOS4210_TMU_DRV_DATA (&exynos4210_default_tmu_data)
+#else
+#define EXYNOS4210_TMU_DRV_DATA (NULL)
+#endif
+
+#if defined(CONFIG_SOC_EXYNOS5250) || defined(CONFIG_SOC_EXYNOS4412)
+static struct exynos_tmu_platform_data const exynos_default_tmu_data = {
+ .trigger_levels[0] = 85,
+ .trigger_levels[1] = 103,
+ .trigger_levels[2] = 110,
+ .trigger_level0_en = 1,
+ .trigger_level1_en = 1,
+ .trigger_level2_en = 1,
+ .trigger_level3_en = 0,
+ .gain = 8,
+ .reference_voltage = 16,
+ .noise_cancel_mode = 4,
+ .cal_type = TYPE_ONE_POINT_TRIMMING,
+ .efuse_value = 55,
+ .freq_tab[0] = {
+ .freq_clip_max = 800 * 1000,
+ .temp_level = 85,
+ },
+ .freq_tab[1] = {
+ .freq_clip_max = 200 * 1000,
+ .temp_level = 103,
+ },
+ .freq_tab_count = 2,
+ .type = SOC_ARCH_EXYNOS,
+};
+#define EXYNOS_TMU_DRV_DATA (&exynos_default_tmu_data)
+#else
+#define EXYNOS_TMU_DRV_DATA (NULL)
+#endif
+
+#ifdef CONFIG_OF
+static const struct of_device_id exynos_tmu_match[] = {
+ {
+ .compatible = "samsung,exynos4210-tmu",
+ .data = (void *)EXYNOS4210_TMU_DRV_DATA,
+ },
+ {
+ .compatible = "samsung,exynos5250-tmu",
+ .data = (void *)EXYNOS_TMU_DRV_DATA,
+ },
+ {},
+};
+MODULE_DEVICE_TABLE(of, exynos_tmu_match);
+#else
+#define exynos_tmu_match NULL
+#endif
+
+static struct platform_device_id exynos_tmu_driver_ids[] = {
+ {
+ .name = "exynos4210-tmu",
+ .driver_data = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
+ },
+ {
+ .name = "exynos5250-tmu",
+ .driver_data = (kernel_ulong_t)EXYNOS_TMU_DRV_DATA,
+ },
+ { },
+};
+MODULE_DEVICE_TABLE(platform, exynos4_tmu_driver_ids);
+
+static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
+ struct platform_device *pdev)
+{
+#ifdef CONFIG_OF
+ if (pdev->dev.of_node) {
+ const struct of_device_id *match;
+ match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
+ if (!match)
+ return NULL;
+ return (struct exynos_tmu_platform_data *) match->data;
+ }
+#endif
+ return (struct exynos_tmu_platform_data *)
+ platform_get_device_id(pdev)->driver_data;
}
-;
static int __devinit exynos_tmu_probe(struct platform_device *pdev)
{
struct exynos_tmu_data *data;
struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
int ret, i;
+ if (!pdata)
+ pdata = exynos_get_driver_data(pdev);
+
if (!pdata) {
dev_err(&pdev->dev, "No platform init data supplied.\n");
return -ENODEV;
@@ -899,9 +1006,11 @@ static struct platform_driver exynos_tmu_driver = {
.name = "exynos-tmu",
.owner = THIS_MODULE,
.pm = EXYNOS_TMU_PM,
+ .of_match_table = exynos_tmu_match,
},
.probe = exynos_tmu_probe,
.remove = __devexit_p(exynos_tmu_remove),
+ .id_table = exynos_tmu_driver_ids,
};
module_platform_driver(exynos_tmu_driver);
--
1.7.1
Insert exynos5 TMU sensor changes into the thermal driver. Some exynos4
changes are made generic for exynos series.
[[email protected]: fix comment layout]
Signed-off-by: SangWook Ju <[email protected]>
Signed-off-by: Amit Daniel Kachhap <[email protected]>
Acked-by: Guenter Roeck <[email protected]>
Cc: Durgadoss <[email protected]>
Cc: Len Brown <[email protected]>
Cc: Jean Delvare <[email protected]>
Cc: Kyungmin Park <[email protected]>
Cc: Kukjin Kim <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Amit Daniel Kachhap <[email protected]>
---
drivers/thermal/Kconfig | 2 +-
drivers/thermal/exynos_thermal.c | 351 ++++++++++++++++----------
include/linux/platform_data/exynos_thermal.h | 19 ++-
3 files changed, 240 insertions(+), 132 deletions(-)
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 8f2b6ea..edfd67d 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -49,7 +49,7 @@ config RCAR_THERMAL
config EXYNOS_THERMAL
tristate "Temperature sensor on Samsung EXYNOS"
- depends on ARCH_EXYNOS4 && THERMAL
+ depends on (ARCH_EXYNOS4 || ARCH_EXYNOS5) && THERMAL
help
If you say yes here you get support for TMU (Thermal Managment
Unit) on SAMSUNG EXYNOS series of SoC.
diff --git a/drivers/thermal/exynos_thermal.c b/drivers/thermal/exynos_thermal.c
index 556d15b..c9a33dd 100644
--- a/drivers/thermal/exynos_thermal.c
+++ b/drivers/thermal/exynos_thermal.c
@@ -33,44 +33,83 @@
#include <linux/kobject.h>
#include <linux/io.h>
#include <linux/mutex.h>
-
#include <linux/platform_data/exynos_thermal.h>
-
-#define EXYNOS4_TMU_REG_TRIMINFO 0x0
-#define EXYNOS4_TMU_REG_CONTROL 0x20
-#define EXYNOS4_TMU_REG_STATUS 0x28
-#define EXYNOS4_TMU_REG_CURRENT_TEMP 0x40
-#define EXYNOS4_TMU_REG_THRESHOLD_TEMP 0x44
-#define EXYNOS4_TMU_REG_TRIG_LEVEL0 0x50
-#define EXYNOS4_TMU_REG_TRIG_LEVEL1 0x54
-#define EXYNOS4_TMU_REG_TRIG_LEVEL2 0x58
-#define EXYNOS4_TMU_REG_TRIG_LEVEL3 0x5C
-#define EXYNOS4_TMU_REG_PAST_TEMP0 0x60
-#define EXYNOS4_TMU_REG_PAST_TEMP1 0x64
-#define EXYNOS4_TMU_REG_PAST_TEMP2 0x68
-#define EXYNOS4_TMU_REG_PAST_TEMP3 0x6C
-#define EXYNOS4_TMU_REG_INTEN 0x70
-#define EXYNOS4_TMU_REG_INTSTAT 0x74
-#define EXYNOS4_TMU_REG_INTCLEAR 0x78
-
-#define EXYNOS4_TMU_GAIN_SHIFT 8
-#define EXYNOS4_TMU_REF_VOLTAGE_SHIFT 24
-
-#define EXYNOS4_TMU_TRIM_TEMP_MASK 0xff
-#define EXYNOS4_TMU_CORE_ON 3
-#define EXYNOS4_TMU_CORE_OFF 2
-#define EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET 50
-#define EXYNOS4_TMU_TRIG_LEVEL0_MASK 0x1
-#define EXYNOS4_TMU_TRIG_LEVEL1_MASK 0x10
-#define EXYNOS4_TMU_TRIG_LEVEL2_MASK 0x100
-#define EXYNOS4_TMU_TRIG_LEVEL3_MASK 0x1000
-#define EXYNOS4_TMU_INTCLEAR_VAL 0x1111
-
-struct exynos4_tmu_data {
- struct exynos4_tmu_platform_data *pdata;
+#include <linux/of.h>
+
+#include <plat/cpu.h>
+
+/* Exynos generic registers */
+#define EXYNOS_TMU_REG_TRIMINFO 0x0
+#define EXYNOS_TMU_REG_CONTROL 0x20
+#define EXYNOS_TMU_REG_STATUS 0x28
+#define EXYNOS_TMU_REG_CURRENT_TEMP 0x40
+#define EXYNOS_TMU_REG_INTEN 0x70
+#define EXYNOS_TMU_REG_INTSTAT 0x74
+#define EXYNOS_TMU_REG_INTCLEAR 0x78
+
+#define EXYNOS_TMU_TRIM_TEMP_MASK 0xff
+#define EXYNOS_TMU_GAIN_SHIFT 8
+#define EXYNOS_TMU_REF_VOLTAGE_SHIFT 24
+#define EXYNOS_TMU_CORE_ON 3
+#define EXYNOS_TMU_CORE_OFF 2
+#define EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET 50
+
+/* Exynos4210 specific registers */
+#define EXYNOS4210_TMU_REG_THRESHOLD_TEMP 0x44
+#define EXYNOS4210_TMU_REG_TRIG_LEVEL0 0x50
+#define EXYNOS4210_TMU_REG_TRIG_LEVEL1 0x54
+#define EXYNOS4210_TMU_REG_TRIG_LEVEL2 0x58
+#define EXYNOS4210_TMU_REG_TRIG_LEVEL3 0x5C
+#define EXYNOS4210_TMU_REG_PAST_TEMP0 0x60
+#define EXYNOS4210_TMU_REG_PAST_TEMP1 0x64
+#define EXYNOS4210_TMU_REG_PAST_TEMP2 0x68
+#define EXYNOS4210_TMU_REG_PAST_TEMP3 0x6C
+
+#define EXYNOS4210_TMU_TRIG_LEVEL0_MASK 0x1
+#define EXYNOS4210_TMU_TRIG_LEVEL1_MASK 0x10
+#define EXYNOS4210_TMU_TRIG_LEVEL2_MASK 0x100
+#define EXYNOS4210_TMU_TRIG_LEVEL3_MASK 0x1000
+#define EXYNOS4210_TMU_INTCLEAR_VAL 0x1111
+
+/* Exynos5250 and Exynos4412 specific registers */
+#define EXYNOS_TMU_TRIMINFO_CON 0x14
+#define EXYNOS_THD_TEMP_RISE 0x50
+#define EXYNOS_THD_TEMP_FALL 0x54
+#define EXYNOS_EMUL_CON 0x80
+
+#define EXYNOS_TRIMINFO_RELOAD 0x1
+#define EXYNOS_TMU_CLEAR_RISE_INT 0x111
+#define EXYNOS_TMU_CLEAR_FALL_INT (0x111 << 16)
+#define EXYNOS_MUX_ADDR_VALUE 6
+#define EXYNOS_MUX_ADDR_SHIFT 20
+#define EXYNOS_TMU_TRIP_MODE_SHIFT 13
+
+#define EFUSE_MIN_VALUE 40
+#define EFUSE_MAX_VALUE 100
+
+/* In-kernel thermal framework related macros & definations */
+#define SENSOR_NAME_LEN 16
+#define MAX_TRIP_COUNT 8
+#define MAX_COOLING_DEVICE 4
+
+#define ACTIVE_INTERVAL 500
+#define IDLE_INTERVAL 10000
+
+/* CPU Zone information */
+#define PANIC_ZONE 4
+#define WARN_ZONE 3
+#define MONITOR_ZONE 2
+#define SAFE_ZONE 1
+
+#define GET_ZONE(trip) (trip + 2)
+#define GET_TRIP(zone) (zone - 2)
+
+struct exynos_tmu_data {
+ struct exynos_tmu_platform_data *pdata;
struct resource *mem;
void __iomem *base;
int irq;
+ enum soc_type soc;
struct work_struct irq_work;
struct mutex lock;
struct clk *clk;
@@ -81,16 +120,17 @@ struct exynos4_tmu_data {
* TMU treats temperature as a mapped temperature code.
* The temperature is converted differently depending on the calibration type.
*/
-static int temp_to_code(struct exynos4_tmu_data *data, u8 temp)
+static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
{
- struct exynos4_tmu_platform_data *pdata = data->pdata;
+ struct exynos_tmu_platform_data *pdata = data->pdata;
int temp_code;
- /* temp should range between 25 and 125 */
- if (temp < 25 || temp > 125) {
- temp_code = -EINVAL;
- goto out;
- }
+ if (data->soc == SOC_ARCH_EXYNOS4210)
+ /* temp should range between 25 and 125 */
+ if (temp < 25 || temp > 125) {
+ temp_code = -EINVAL;
+ goto out;
+ }
switch (pdata->cal_type) {
case TYPE_TWO_POINT_TRIMMING:
@@ -102,7 +142,7 @@ static int temp_to_code(struct exynos4_tmu_data *data, u8 temp)
temp_code = temp + data->temp_error1 - 25;
break;
default:
- temp_code = temp + EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET;
+ temp_code = temp + EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
break;
}
out:
@@ -113,16 +153,17 @@ out:
* Calculate a temperature value from a temperature code.
* The unit of the temperature is degree Celsius.
*/
-static int code_to_temp(struct exynos4_tmu_data *data, u8 temp_code)
+static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
{
- struct exynos4_tmu_platform_data *pdata = data->pdata;
+ struct exynos_tmu_platform_data *pdata = data->pdata;
int temp;
- /* temp_code should range between 75 and 175 */
- if (temp_code < 75 || temp_code > 175) {
- temp = -ENODATA;
- goto out;
- }
+ if (data->soc == SOC_ARCH_EXYNOS4210)
+ /* temp_code should range between 75 and 175 */
+ if (temp_code < 75 || temp_code > 175) {
+ temp = -ENODATA;
+ goto out;
+ }
switch (pdata->cal_type) {
case TYPE_TWO_POINT_TRIMMING:
@@ -133,54 +174,92 @@ static int code_to_temp(struct exynos4_tmu_data *data, u8 temp_code)
temp = temp_code - data->temp_error1 + 25;
break;
default:
- temp = temp_code - EXYNOS4_TMU_DEF_CODE_TO_TEMP_OFFSET;
+ temp = temp_code - EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
break;
}
out:
return temp;
}
-static int exynos4_tmu_initialize(struct platform_device *pdev)
+static int exynos_tmu_initialize(struct platform_device *pdev)
{
- struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
- struct exynos4_tmu_platform_data *pdata = data->pdata;
- unsigned int status, trim_info;
+ struct exynos_tmu_data *data = platform_get_drvdata(pdev);
+ struct exynos_tmu_platform_data *pdata = data->pdata;
+ unsigned int status, trim_info, rising_threshold;
int ret = 0, threshold_code;
mutex_lock(&data->lock);
clk_enable(data->clk);
- status = readb(data->base + EXYNOS4_TMU_REG_STATUS);
+ status = readb(data->base + EXYNOS_TMU_REG_STATUS);
if (!status) {
ret = -EBUSY;
goto out;
}
+ if (data->soc == SOC_ARCH_EXYNOS) {
+ __raw_writel(EXYNOS_TRIMINFO_RELOAD,
+ data->base + EXYNOS_TMU_TRIMINFO_CON);
+ }
/* Save trimming info in order to perform calibration */
- trim_info = readl(data->base + EXYNOS4_TMU_REG_TRIMINFO);
- data->temp_error1 = trim_info & EXYNOS4_TMU_TRIM_TEMP_MASK;
- data->temp_error2 = ((trim_info >> 8) & EXYNOS4_TMU_TRIM_TEMP_MASK);
-
- /* Write temperature code for threshold */
- threshold_code = temp_to_code(data, pdata->threshold);
- if (threshold_code < 0) {
- ret = threshold_code;
- goto out;
+ trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
+ data->temp_error1 = trim_info & EXYNOS_TMU_TRIM_TEMP_MASK;
+ data->temp_error2 = ((trim_info >> 8) & EXYNOS_TMU_TRIM_TEMP_MASK);
+
+ if ((EFUSE_MIN_VALUE > data->temp_error1) ||
+ (data->temp_error1 > EFUSE_MAX_VALUE) ||
+ (data->temp_error2 != 0))
+ data->temp_error1 = pdata->efuse_value;
+
+ if (data->soc == SOC_ARCH_EXYNOS4210) {
+ /* Write temperature code for threshold */
+ threshold_code = temp_to_code(data, pdata->threshold);
+ if (threshold_code < 0) {
+ ret = threshold_code;
+ goto out;
+ }
+ writeb(threshold_code,
+ data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
+
+ writeb(pdata->trigger_levels[0],
+ data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL0);
+ writeb(pdata->trigger_levels[1],
+ data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL1);
+ writeb(pdata->trigger_levels[2],
+ data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL2);
+ writeb(pdata->trigger_levels[3],
+ data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL3);
+
+ writel(EXYNOS4210_TMU_INTCLEAR_VAL,
+ data->base + EXYNOS_TMU_REG_INTCLEAR);
+ } else if (data->soc == SOC_ARCH_EXYNOS) {
+ /* Write temperature code for threshold */
+ threshold_code = temp_to_code(data, pdata->trigger_levels[0]);
+ if (threshold_code < 0) {
+ ret = threshold_code;
+ goto out;
+ }
+ rising_threshold = threshold_code;
+ threshold_code = temp_to_code(data, pdata->trigger_levels[1]);
+ if (threshold_code < 0) {
+ ret = threshold_code;
+ goto out;
+ }
+ rising_threshold |= (threshold_code << 8);
+ threshold_code = temp_to_code(data, pdata->trigger_levels[2]);
+ if (threshold_code < 0) {
+ ret = threshold_code;
+ goto out;
+ }
+ rising_threshold |= (threshold_code << 16);
+
+ writel(rising_threshold,
+ data->base + EXYNOS_THD_TEMP_RISE);
+ writel(0, data->base + EXYNOS_THD_TEMP_FALL);
+
+ writel(EXYNOS_TMU_CLEAR_RISE_INT|EXYNOS_TMU_CLEAR_FALL_INT,
+ data->base + EXYNOS_TMU_REG_INTCLEAR);
}
- writeb(threshold_code,
- data->base + EXYNOS4_TMU_REG_THRESHOLD_TEMP);
-
- writeb(pdata->trigger_levels[0],
- data->base + EXYNOS4_TMU_REG_TRIG_LEVEL0);
- writeb(pdata->trigger_levels[1],
- data->base + EXYNOS4_TMU_REG_TRIG_LEVEL1);
- writeb(pdata->trigger_levels[2],
- data->base + EXYNOS4_TMU_REG_TRIG_LEVEL2);
- writeb(pdata->trigger_levels[3],
- data->base + EXYNOS4_TMU_REG_TRIG_LEVEL3);
-
- writel(EXYNOS4_TMU_INTCLEAR_VAL,
- data->base + EXYNOS4_TMU_REG_INTCLEAR);
out:
clk_disable(data->clk);
mutex_unlock(&data->lock);
@@ -188,35 +267,41 @@ out:
return ret;
}
-static void exynos4_tmu_control(struct platform_device *pdev, bool on)
+static void exynos_tmu_control(struct platform_device *pdev, bool on)
{
- struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
- struct exynos4_tmu_platform_data *pdata = data->pdata;
+ struct exynos_tmu_data *data = platform_get_drvdata(pdev);
+ struct exynos_tmu_platform_data *pdata = data->pdata;
unsigned int con, interrupt_en;
mutex_lock(&data->lock);
clk_enable(data->clk);
- con = pdata->reference_voltage << EXYNOS4_TMU_REF_VOLTAGE_SHIFT |
- pdata->gain << EXYNOS4_TMU_GAIN_SHIFT;
+ con = pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT |
+ pdata->gain << EXYNOS_TMU_GAIN_SHIFT;
+
+ if (data->soc == SOC_ARCH_EXYNOS) {
+ con |= pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT;
+ con |= (EXYNOS_MUX_ADDR_VALUE << EXYNOS_MUX_ADDR_SHIFT);
+ }
+
if (on) {
- con |= EXYNOS4_TMU_CORE_ON;
+ con |= EXYNOS_TMU_CORE_ON;
interrupt_en = pdata->trigger_level3_en << 12 |
pdata->trigger_level2_en << 8 |
pdata->trigger_level1_en << 4 |
pdata->trigger_level0_en;
} else {
- con |= EXYNOS4_TMU_CORE_OFF;
+ con |= EXYNOS_TMU_CORE_OFF;
interrupt_en = 0; /* Disable all interrupts */
}
- writel(interrupt_en, data->base + EXYNOS4_TMU_REG_INTEN);
- writel(con, data->base + EXYNOS4_TMU_REG_CONTROL);
+ writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
+ writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
clk_disable(data->clk);
mutex_unlock(&data->lock);
}
-static int exynos4_tmu_read(struct exynos4_tmu_data *data)
+static int exynos_tmu_read(struct exynos_tmu_data *data)
{
u8 temp_code;
int temp;
@@ -224,7 +309,7 @@ static int exynos4_tmu_read(struct exynos4_tmu_data *data)
mutex_lock(&data->lock);
clk_enable(data->clk);
- temp_code = readb(data->base + EXYNOS4_TMU_REG_CURRENT_TEMP);
+ temp_code = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
temp = code_to_temp(data, temp_code);
clk_disable(data->clk);
@@ -233,25 +318,30 @@ static int exynos4_tmu_read(struct exynos4_tmu_data *data)
return temp;
}
-static void exynos4_tmu_work(struct work_struct *work)
+static void exynos_tmu_work(struct work_struct *work)
{
- struct exynos4_tmu_data *data = container_of(work,
- struct exynos4_tmu_data, irq_work);
+ struct exynos_tmu_data *data = container_of(work,
+ struct exynos_tmu_data, irq_work);
mutex_lock(&data->lock);
clk_enable(data->clk);
- writel(EXYNOS4_TMU_INTCLEAR_VAL, data->base + EXYNOS4_TMU_REG_INTCLEAR);
- enable_irq(data->irq);
+ if (data->soc == SOC_ARCH_EXYNOS)
+ writel(EXYNOS_TMU_CLEAR_RISE_INT,
+ data->base + EXYNOS_TMU_REG_INTCLEAR);
+ else
+ writel(EXYNOS4210_TMU_INTCLEAR_VAL,
+ data->base + EXYNOS_TMU_REG_INTCLEAR);
clk_disable(data->clk);
mutex_unlock(&data->lock);
+ enable_irq(data->irq);
}
-static irqreturn_t exynos4_tmu_irq(int irq, void *id)
+static irqreturn_t exynos_tmu_irq(int irq, void *id)
{
- struct exynos4_tmu_data *data = id;
+ struct exynos_tmu_data *data = id;
disable_irq_nosync(irq);
schedule_work(&data->irq_work);
@@ -259,18 +349,17 @@ static irqreturn_t exynos4_tmu_irq(int irq, void *id)
return IRQ_HANDLED;
}
-static int __devinit exynos4_tmu_probe(struct platform_device *pdev)
+static int __devinit exynos_tmu_probe(struct platform_device *pdev)
{
- struct exynos4_tmu_data *data;
- struct exynos4_tmu_platform_data *pdata = pdev->dev.platform_data;
+ struct exynos_tmu_data *data;
+ struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
int ret;
if (!pdata) {
dev_err(&pdev->dev, "No platform init data supplied.\n");
return -ENODEV;
}
-
- data = kzalloc(sizeof(struct exynos4_tmu_data), GFP_KERNEL);
+ data = kzalloc(sizeof(struct exynos_tmu_data), GFP_KERNEL);
if (!data) {
dev_err(&pdev->dev, "Failed to allocate driver structure\n");
return -ENOMEM;
@@ -283,7 +372,7 @@ static int __devinit exynos4_tmu_probe(struct platform_device *pdev)
goto err_free;
}
- INIT_WORK(&data->irq_work, exynos4_tmu_work);
+ INIT_WORK(&data->irq_work, exynos_tmu_work);
data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!data->mem) {
@@ -307,9 +396,8 @@ static int __devinit exynos4_tmu_probe(struct platform_device *pdev)
goto err_mem_region;
}
- ret = request_irq(data->irq, exynos4_tmu_irq,
- IRQF_TRIGGER_RISING,
- "exynos4-tmu", data);
+ ret = request_irq(data->irq, exynos_tmu_irq,
+ IRQF_TRIGGER_RISING, "exynos-tmu", data);
if (ret) {
dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
goto err_io_remap;
@@ -322,17 +410,26 @@ static int __devinit exynos4_tmu_probe(struct platform_device *pdev)
goto err_irq;
}
+ if (pdata->type == SOC_ARCH_EXYNOS ||
+ pdata->type == SOC_ARCH_EXYNOS4210)
+ data->soc = pdata->type;
+ else {
+ ret = -EINVAL;
+ dev_err(&pdev->dev, "Platform not supported\n");
+ goto err_clk;
+ }
+
data->pdata = pdata;
platform_set_drvdata(pdev, data);
mutex_init(&data->lock);
- ret = exynos4_tmu_initialize(pdev);
+ ret = exynos_tmu_initialize(pdev);
if (ret) {
dev_err(&pdev->dev, "Failed to initialize TMU\n");
goto err_clk;
}
- exynos4_tmu_control(pdev, true);
+ exynos_tmu_control(pdev, true);
return 0;
err_clk:
@@ -350,11 +447,11 @@ err_free:
return ret;
}
-static int __devexit exynos4_tmu_remove(struct platform_device *pdev)
+static int __devexit exynos_tmu_remove(struct platform_device *pdev)
{
- struct exynos4_tmu_data *data = platform_get_drvdata(pdev);
+ struct exynos_tmu_data *data = platform_get_drvdata(pdev);
- exynos4_tmu_control(pdev, false);
+ exynos_tmu_control(pdev, false);
clk_put(data->clk);
@@ -371,43 +468,43 @@ static int __devexit exynos4_tmu_remove(struct platform_device *pdev)
}
#ifdef CONFIG_PM_SLEEP
-static int exynos4_tmu_suspend(struct device *dev)
+static int exynos_tmu_suspend(struct device *dev)
{
- exynos4_tmu_control(to_platform_device(dev), false);
+ exynos_tmu_control(to_platform_device(dev), false);
return 0;
}
-static int exynos4_tmu_resume(struct device *dev)
+static int exynos_tmu_resume(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
- exynos4_tmu_initialize(pdev);
- exynos4_tmu_control(pdev, true);
+ exynos_tmu_initialize(pdev);
+ exynos_tmu_control(pdev, true);
return 0;
}
-static SIMPLE_DEV_PM_OPS(exynos4_tmu_pm,
- exynos4_tmu_suspend, exynos4_tmu_resume);
-#define EXYNOS4_TMU_PM (&exynos4_tmu_pm)
+static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
+ exynos_tmu_suspend, exynos_tmu_resume);
+#define EXYNOS_TMU_PM (&exynos_tmu_pm)
#else
-#define EXYNOS4_TMU_PM NULL
+#define EXYNOS_TMU_PM NULL
#endif
-static struct platform_driver exynos4_tmu_driver = {
+static struct platform_driver exynos_tmu_driver = {
.driver = {
- .name = "exynos4-tmu",
+ .name = "exynos-tmu",
.owner = THIS_MODULE,
- .pm = EXYNOS4_TMU_PM,
+ .pm = EXYNOS_TMU_PM,
},
- .probe = exynos4_tmu_probe,
- .remove = __devexit_p(exynos4_tmu_remove),
+ .probe = exynos_tmu_probe,
+ .remove = __devexit_p(exynos_tmu_remove),
};
-module_platform_driver(exynos4_tmu_driver);
+module_platform_driver(exynos_tmu_driver);
-MODULE_DESCRIPTION("EXYNOS4 TMU Driver");
+MODULE_DESCRIPTION("EXYNOS TMU Driver");
MODULE_AUTHOR("Donggeun Kim <[email protected]>");
MODULE_LICENSE("GPL");
-MODULE_ALIAS("platform:exynos4-tmu");
+MODULE_ALIAS("platform:exynos-tmu");
diff --git a/include/linux/platform_data/exynos_thermal.h b/include/linux/platform_data/exynos_thermal.h
index d6c3f93..a9e960a 100644
--- a/include/linux/platform_data/exynos_thermal.h
+++ b/include/linux/platform_data/exynos_thermal.h
@@ -1,5 +1,5 @@
/*
- * exynos_thermal.h - Samsung EXYNOS4 TMU (Thermal Management Unit)
+ * exynos_thermal.h - Samsung EXYNOS TMU (Thermal Management Unit)
*
* Copyright (C) 2011 Samsung Electronics
* Donggeun Kim <[email protected]>
@@ -28,8 +28,12 @@ enum calibration_type {
TYPE_NONE,
};
+enum soc_type {
+ SOC_ARCH_EXYNOS4210 = 1,
+ SOC_ARCH_EXYNOS,
+};
/**
- * struct exynos4_tmu_platform_data
+ * struct exynos_tmu_platform_data
* @threshold: basic temperature for generating interrupt
* 25 <= threshold <= 125 [unit: degree Celsius]
* @trigger_levels: array for each interrupt levels
@@ -63,11 +67,15 @@ enum calibration_type {
* @reference_voltage: reference voltage of amplifier
* in the positive-TC generator block
* 0 <= reference_voltage <= 31
+ * @noise_cancel_mode: noise cancellation mode
+ * 000, 100, 101, 110 and 111 can be different modes
+ * @type: determines the type of SOC
+ * @efuse_value: platform defined fuse value
* @cal_type: calibration type for temperature
*
- * This structure is required for configuration of exynos4_tmu driver.
+ * This structure is required for configuration of exynos_tmu driver.
*/
-struct exynos4_tmu_platform_data {
+struct exynos_tmu_platform_data {
u8 threshold;
u8 trigger_levels[4];
bool trigger_level0_en;
@@ -77,7 +85,10 @@ struct exynos4_tmu_platform_data {
u8 gain;
u8 reference_voltage;
+ u8 noise_cancel_mode;
+ u32 efuse_value;
enum calibration_type cal_type;
+ enum soc_type type;
};
#endif /* _LINUX_EXYNOS_THERMAL_H */
--
1.7.1
Amit,
Thanks again for keeping this up.
On Thu, Aug 16, 2012 at 2:41 PM, Amit Daniel Kachhap
<[email protected]> wrote:
> This patchset introduces a new generic cooling device based on cpufreq
> that can be used on non-ACPI platforms. As a proof of concept, we have
> drivers for the following platforms using this mechanism now:
>
> * Samsung Exynos (Exynos4 and Exynos5) in the current patchset.
> * Freescale i.MX (git://git.linaro.org/people/amitdanielk/linux.git imx6q_thermal)
FYI, the OMAP code is under drivers/staging/omap-thermal/. The file
omap-thermal-common.c is the one which is using your generic cooling
device. But it needs to be updated accordingly to the API change you
mention.
>
> There is a small change in cpufreq cooling registration APIs, so a minor
> change is needed for Freescale platforms.
>
> Brief Description:
>
> 1) The generic cooling devices code is placed inside driver/thermal/*
> as placing inside acpi folder will need un-necessary enabling of acpi
> code. This code is architecture independent.
>
> 2) This patchset adds generic cpu cooling low level implementation
> through frequency clipping. In future, other cpu related cooling
> devices may be added here. An ACPI version of this already exists
> (drivers/acpi/processor_thermal.c) .But this will be useful for
> platforms like ARM using the generic thermal interface along with the
> generic cpu cooling devices. The cooling device registration API's
> return cooling device pointers which can be easily binded with the
> thermal zone trip points. The important APIs exposed are,
>
> a) struct thermal_cooling_device *cpufreq_cooling_register(
> struct cpumask *clip_cpus)
> b) void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
>
> 3) Samsung exynos platform thermal implementation is done using the
> generic cpu cooling APIs and the new trip type. The temperature sensor
> driver present in the hwmon folder(registered as hwmon driver) is moved
> to thermal folder and registered as a thermal driver.
>
> A simple data/control flow diagrams is shown below,
>
> Core Linux thermal <-----> Exynos thermal interface <----- Temperature Sensor
> | |
> \|/ |
> Cpufreq cooling device <---------------
>
> TODO:
> *Will send the DT enablement patches later after the driver is merged.
>
> This patch:
>
> Add support for generic cpu thermal cooling low level implementations
> using frequency scaling up/down based on the registration parameters.
> Different cpu related cooling devices can be registered by the user and
> the binding of these cooling devices to the corresponding trip points can
> be easily done as the registration APIs return the cooling device pointer.
> The user of these APIs are responsible for passing clipping frequency .
> The drivers can also register to recieve notification about any cooling
> action called.
>
> [[email protected]: fix comment layout]
> Signed-off-by: Amit Daniel Kachhap <[email protected]>
> Cc: Guenter Roeck <[email protected]>
> Cc: SangWook Ju <[email protected]>
> Cc: Durgadoss <[email protected]>
> Cc: Len Brown <[email protected]>
> Cc: Jean Delvare <[email protected]>
> Cc: Kyungmin Park <[email protected]>
> Cc: Kukjin Kim <[email protected]>
> Signed-off-by: Andrew Morton <[email protected]>
> Signed-off-by: Amit Daniel Kachhap <[email protected]>
Reviewed-by: Eduardo Valentin <[email protected]>
> ---
> Documentation/thermal/cpu-cooling-api.txt | 52 +++
> drivers/thermal/Kconfig | 11 +
> drivers/thermal/Makefile | 1 +
> drivers/thermal/cpu_cooling.c | 512 +++++++++++++++++++++++++++++
> include/linux/cpu_cooling.h | 79 +++++
> 5 files changed, 655 insertions(+), 0 deletions(-)
> create mode 100644 Documentation/thermal/cpu-cooling-api.txt
> create mode 100644 drivers/thermal/cpu_cooling.c
> create mode 100644 include/linux/cpu_cooling.h
>
> diff --git a/Documentation/thermal/cpu-cooling-api.txt b/Documentation/thermal/cpu-cooling-api.txt
> new file mode 100644
> index 0000000..a1f2a6b
> --- /dev/null
> +++ b/Documentation/thermal/cpu-cooling-api.txt
> @@ -0,0 +1,52 @@
> +CPU cooling APIs How To
> +===================================
> +
> +Written by Amit Daniel Kachhap <[email protected]>
> +
> +Updated: 12 May 2012
> +
> +Copyright (c) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> +
> +0. Introduction
> +
> +The generic cpu cooling(freq clipping) provides registration/unregistration APIs
> +to the caller. The binding of the cooling devices to the trip point is left for
> +the user. The registration APIs returns the cooling device pointer.
> +
> +1. cpu cooling APIs
> +
> +1.1 cpufreq registration/unregistration APIs
> +1.1.1 struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus)
> +
> + This interface function registers the cpufreq cooling device with the name
> + "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
> + cooling devices.
> +
> + clip_cpus: cpumask of cpus where the frequency constraints will happen.
> +
> +1.1.2 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
> +
> + This interface function unregisters the "thermal-cpufreq-%x" cooling device.
> +
> + cdev: Cooling device pointer which has to be unregistered.
> +
> +
> +1.2 CPU cooling action notifier register/unregister interface
> +1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
> + unsigned int list)
> +
> + This interface registers a driver with cpu cooling layer. The driver will
> + be notified when any cpu cooling action is called.
> +
> + nb: notifier function to register
> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> +
> +1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
> + unsigned int list)
> +
> + This interface registers a driver with cpu cooling layer. The driver will
> + be notified when any cpu cooling action is called.
> +
> + nb: notifier function to register
> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index 7dd8c34..996003b 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -19,6 +19,17 @@ config THERMAL_HWMON
> depends on HWMON=y || HWMON=THERMAL
> default y
>
> +config CPU_THERMAL
> + bool "generic cpu cooling support"
> + depends on THERMAL && CPU_FREQ
> + help
> + This implements the generic cpu cooling mechanism through frequency
> + reduction, cpu hotplug and any other ways of reducing temperature. An
> + ACPI version of this already exists(drivers/acpi/processor_thermal.c).
> + This will be useful for platforms using the generic thermal interface
> + and not the ACPI interface.
> + If you want this support, you should say Y here.
> +
> config SPEAR_THERMAL
> bool "SPEAr thermal sensor driver"
> depends on THERMAL
> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> index fd9369a..aae59ad 100644
> --- a/drivers/thermal/Makefile
> +++ b/drivers/thermal/Makefile
> @@ -3,5 +3,6 @@
> #
>
> obj-$(CONFIG_THERMAL) += thermal_sys.o
> +obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
> obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
> obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
> new file mode 100644
> index 0000000..c42e557
> --- /dev/null
> +++ b/drivers/thermal/cpu_cooling.c
> @@ -0,0 +1,512 @@
> +/*
> + * linux/drivers/thermal/cpu_cooling.c
> + *
> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> + * Copyright (C) 2012 Amit Daniel <[email protected]>
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + * 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; version 2 of the License.
> + *
> + * This program is distributed in the hope that it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, write to the Free Software Foundation, Inc.,
> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + */
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/thermal.h>
> +#include <linux/platform_device.h>
> +#include <linux/cpufreq.h>
> +#include <linux/err.h>
> +#include <linux/slab.h>
> +#include <linux/cpu.h>
> +#include <linux/cpu_cooling.h>
> +
> +/**
> + * struct cpufreq_cooling_device
> + * @id: unique integer value corresponding to each cpufreq_cooling_device
> + * registered.
> + * @cool_dev: thermal_cooling_device pointer to keep track of the the
> + * egistered cooling device.
> + * @cpufreq_state: integer value representing the current state of cpufreq
> + * cooling devices.
> + * @cpufreq_val: integer value representing the absolute value of the clipped
> + * frequency.
> + * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
> + * @node: list_head to link all cpufreq_cooling_device together.
> + *
> + * This structure is required for keeping information of each
> + * cpufreq_cooling_device registered as a list whose head is represented by
> + * cooling_cpufreq_list. In order to prevent corruption of this list a
> + * mutex lock cooling_cpufreq_lock is used.
> + */
> +struct cpufreq_cooling_device {
> + int id;
> + struct thermal_cooling_device *cool_dev;
> + unsigned int cpufreq_state;
> + unsigned int cpufreq_val;
> + struct cpumask allowed_cpus;
> + struct list_head node;
> +};
> +static LIST_HEAD(cooling_cpufreq_list);
> +static DEFINE_IDR(cpufreq_idr);
> +
> +static struct mutex cooling_cpufreq_lock;
> +
> +/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
> +#define NOTIFY_INVALID NULL
> +struct cpufreq_cooling_device *notify_device;
> +
> +/* Head of the blocking notifier chain to inform about frequency clamping */
> +static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
> +
> +/**
> + * get_idr - function to get a unique id.
> + * @idr: struct idr * handle used to create a id.
> + * @id: int * value generated by this function.
> + */
> +static int get_idr(struct idr *idr, int *id)
> +{
> + int err;
> +again:
> + if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
> + return -ENOMEM;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + err = idr_get_new(idr, NULL, id);
> + mutex_unlock(&cooling_cpufreq_lock);
> +
> + if (unlikely(err == -EAGAIN))
> + goto again;
> + else if (unlikely(err))
> + return err;
> +
> + *id = *id & MAX_ID_MASK;
> + return 0;
> +}
> +
> +/**
> + * release_idr - function to free the unique id.
> + * @idr: struct idr * handle used for creating the id.
> + * @id: int value representing the unique id.
> + */
> +static void release_idr(struct idr *idr, int id)
> +{
> + mutex_lock(&cooling_cpufreq_lock);
> + idr_remove(idr, id);
> + mutex_unlock(&cooling_cpufreq_lock);
> +}
> +
> +/**
> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. possible values are
> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> + *
> + * This exported function registers a driver with cpu cooling layer. The driver
> + * will be notified when any cpu cooling action is called.
> + */
> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
> +{
> + int ret = 0;
> +
> + switch (list) {
> + case CPUFREQ_COOLING_START:
> + case CPUFREQ_COOLING_STOP:
> + ret = blocking_notifier_chain_register(
> + &cputherm_state_notifier_list, nb);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> + return ret;
> +}
> +EXPORT_SYMBOL(cputherm_register_notifier);
> +
> +/**
> + * cputherm_unregister_notifier - Un-register a notifier.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. values possible are
> + * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
> + *
> + * This exported function un-registers a driver with cpu cooling layer.
> + */
> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
> +{
> + int ret = 0;
> +
> + switch (list) {
> + case CPUFREQ_COOLING_START:
> + case CPUFREQ_COOLING_STOP:
> + ret = blocking_notifier_chain_unregister(
> + &cputherm_state_notifier_list, nb);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> + return ret;
> +}
> +EXPORT_SYMBOL(cputherm_unregister_notifier);
> +
> +/* Below code defines functions to be used for cpufreq as cooling device */
> +
> +/**
> + * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
> + * @cpu: cpu for which check is needed.
> + */
> +static int is_cpufreq_valid(int cpu)
> +{
> + struct cpufreq_policy policy;
> + return !cpufreq_get_policy(&policy, cpu);
> +}
> +
> +/**
> + * get_cpu_frequency - get the absolute value of frequency from level.
> + * @cpu: cpu for which frequency is fetched.
> + * @level: level of frequency of the CPU
> + * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
> + */
> +static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
I like this. With previous implemention I had to write a similar
function, but with output of the required freq clip table. Now that
you have the freq walk here it simplifies the zone setup code.
> +{
> + int ret = 0, i = 0;
> + unsigned long level_index;
> + bool descend = false;
> + struct cpufreq_frequency_table *table =
> + cpufreq_frequency_get_table(cpu);
> + if (!table)
> + return ret;
> +
> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> +
> + /*check if table in ascending or descending order*/
> + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
> + (table[i + 1].frequency < table[i].frequency)
> + && !descend) {
> + descend = true;
> + }
> +
> + /*return if level matched and table in descending order*/
> + if (descend && i == level)
> + return table[i].frequency;
> + i++;
> + }
> + i--;
> +
> + if (level > i || descend)
> + return ret;
> + level_index = i - level;
> +
> + /*Scan the table in reverse order and match the level*/
> + while (i >= 0) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> + /*return if level matched*/
> + if (i == level_index)
> + return table[i].frequency;
> + i--;
> + }
> + return ret;
> +}
> +
> +/**
> + * cpufreq_apply_cooling - function to apply frequency clipping.
> + * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
> + * clipping data.
> + * @cooling_state: value of the cooling state.
> + */
> +static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
> + unsigned long cooling_state)
> +{
> + unsigned int event, cpuid, clip_freq;
> + struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
> + unsigned int cpu = cpumask_any(maskPtr);
> +
> +
> + /* Check if the old cooling action is same as new cooling action */
> + if (cpufreq_device->cpufreq_state == cooling_state)
> + return 0;
> +
> + clip_freq = get_cpu_frequency(cpu, cooling_state);
> + if (!clip_freq)
> + return -EINVAL;
> +
> + cpufreq_device->cpufreq_state = cooling_state;
> + cpufreq_device->cpufreq_val = clip_freq;
> + notify_device = cpufreq_device;
> +
> + if (cooling_state != 0)
> + event = CPUFREQ_COOLING_START;
> + else
> + event = CPUFREQ_COOLING_STOP;
> +
> + blocking_notifier_call_chain(&cputherm_state_notifier_list,
> + event, &clip_freq);
> +
> + for_each_cpu(cpuid, maskPtr) {
> + if (is_cpufreq_valid(cpuid))
> + cpufreq_update_policy(cpuid);
> + }
> +
> + notify_device = NOTIFY_INVALID;
> +
> + return 0;
> +}
> +
> +/**
> + * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
> + * @nb: struct notifier_block * with callback info.
> + * @event: value showing cpufreq event for which this function invoked.
> + * @data: callback-specific data
> + */
> +static int cpufreq_thermal_notifier(struct notifier_block *nb,
> + unsigned long event, void *data)
> +{
> + struct cpufreq_policy *policy = data;
> + unsigned long max_freq = 0;
> +
> + if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
> + return 0;
> +
> + if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
> + max_freq = notify_device->cpufreq_val;
> +
> + /* Never exceed user_policy.max*/
> + if (max_freq > policy->user_policy.max)
> + max_freq = policy->user_policy.max;
> +
> + if (policy->max != max_freq)
> + cpufreq_verify_within_limits(policy, 0, max_freq);
> +
> + return 0;
> +}
> +
> +/*
> + * cpufreq cooling device callback functions are defined below
> + */
> +
> +/**
> + * cpufreq_get_max_state - callback function to get the max cooling state.
> + * @cdev: thermal cooling device pointer.
> + * @state: fill this variable with the max cooling state.
> + */
> +static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
> + unsigned long *state)
> +{
> + int ret = -EINVAL, i = 0;
> + struct cpufreq_cooling_device *cpufreq_device;
> + struct cpumask *maskPtr;
> + unsigned int cpu;
> + struct cpufreq_frequency_table *table;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
> + break;
> + }
> + if (cpufreq_device == NULL)
> + goto return_get_max_state;
> +
> + maskPtr = &cpufreq_device->allowed_cpus;
> + cpu = cpumask_any(maskPtr);
> + table = cpufreq_frequency_get_table(cpu);
> + if (!table) {
> + *state = 0;
> + ret = 0;
> + goto return_get_max_state;
> + }
> +
> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> + i++;
> + }
> + if (i > 0) {
> + *state = --i;
> + ret = 0;
> + }
> +
> +return_get_max_state:
> + mutex_unlock(&cooling_cpufreq_lock);
> + return ret;
> +}
> +
> +/**
> + * cpufreq_get_cur_state - callback function to get the current cooling state.
> + * @cdev: thermal cooling device pointer.
> + * @state: fill this variable with the current cooling state.
> + */
> +static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
> + unsigned long *state)
> +{
> + int ret = -EINVAL;
> + struct cpufreq_cooling_device *cpufreq_device;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> + if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
> + *state = cpufreq_device->cpufreq_state;
> + ret = 0;
> + break;
> + }
> + }
> + mutex_unlock(&cooling_cpufreq_lock);
> +
> + return ret;
> +}
> +
> +/**
> + * cpufreq_set_cur_state - callback function to set the current cooling state.
> + * @cdev: thermal cooling device pointer.
> + * @state: set this variable to the current cooling state.
> + */
> +static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
> + unsigned long state)
> +{
> + int ret = -EINVAL;
> + struct cpufreq_cooling_device *cpufreq_device;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> + if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
> + ret = 0;
> + break;
> + }
> + }
> + if (!ret)
> + ret = cpufreq_apply_cooling(cpufreq_device, state);
> +
> + mutex_unlock(&cooling_cpufreq_lock);
> +
> + return ret;
> +}
> +
> +/* Bind cpufreq callbacks to thermal cooling device ops */
> +static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
> + .get_max_state = cpufreq_get_max_state,
> + .get_cur_state = cpufreq_get_cur_state,
> + .set_cur_state = cpufreq_set_cur_state,
> +};
> +
> +/* Notifier for cpufreq policy change */
> +static struct notifier_block thermal_cpufreq_notifier_block = {
> + .notifier_call = cpufreq_thermal_notifier,
> +};
> +
> +/**
> + * cpufreq_cooling_register - function to create cpufreq cooling device.
> + * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
> + */
> +struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus)
> +{
> + struct thermal_cooling_device *cool_dev;
> + struct cpufreq_cooling_device *cpufreq_dev = NULL;
> + unsigned int cpufreq_dev_count = 0, min = 0, max = 0;
> + char dev_name[THERMAL_NAME_LENGTH];
> + int ret = 0, id = 0, i;
> + struct cpufreq_policy policy;
> +
> + list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node)
> + cpufreq_dev_count++;
> +
> + /*Verify that all the clip cpus have same freq_min, freq_max limit*/
> + for_each_cpu(i, clip_cpus) {
> + /*continue if cpufreq policy not found and not return error*/
> + if (!cpufreq_get_policy(&policy, i))
> + continue;
> + if (min == 0 && max == 0) {
> + min = policy.cpuinfo.min_freq;
> + max = policy.cpuinfo.max_freq;
> + } else {
> + if (min != policy.cpuinfo.min_freq ||
> + max != policy.cpuinfo.max_freq)
> + return ERR_PTR(-EINVAL);
> +}
> + }
> + cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
> + GFP_KERNEL);
> + if (!cpufreq_dev)
> + return ERR_PTR(-ENOMEM);
> +
> + cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
> +
> + if (cpufreq_dev_count == 0)
> + mutex_init(&cooling_cpufreq_lock);
> +
> + ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
> + if (ret) {
> + kfree(cpufreq_dev);
> + return ERR_PTR(-EINVAL);
> + }
> +
> + sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
> +
> + cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
> + &cpufreq_cooling_ops);
> + if (!cool_dev) {
> + release_idr(&cpufreq_idr, cpufreq_dev->id);
> + kfree(cpufreq_dev);
> + return ERR_PTR(-EINVAL);
> + }
> + cpufreq_dev->id = id;
> + cpufreq_dev->cool_dev = cool_dev;
> + cpufreq_dev->cpufreq_state = 0;
> + mutex_lock(&cooling_cpufreq_lock);
> + list_add_tail(&cpufreq_dev->node, &cooling_cpufreq_list);
> +
> + /* Register the notifier for first cpufreq cooling device */
> + if (cpufreq_dev_count == 0)
> + cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
> + CPUFREQ_POLICY_NOTIFIER);
> +
> + mutex_unlock(&cooling_cpufreq_lock);
> + return cool_dev;
> +}
> +EXPORT_SYMBOL(cpufreq_cooling_register);
> +
> +/**
> + * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
> + * @cdev: thermal cooling device pointer.
> + */
> +void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
> +{
> + struct cpufreq_cooling_device *cpufreq_dev = NULL;
> + unsigned int cpufreq_dev_count = 0;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node) {
> + if (cpufreq_dev && cpufreq_dev->cool_dev == cdev)
> + break;
> + cpufreq_dev_count++;
> + }
> +
> + if (!cpufreq_dev || cpufreq_dev->cool_dev != cdev) {
> + mutex_unlock(&cooling_cpufreq_lock);
> + return;
> + }
> +
> + list_del(&cpufreq_dev->node);
> +
> + /* Unregister the notifier for the last cpufreq cooling device */
> + if (cpufreq_dev_count == 1) {
> + cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
> + CPUFREQ_POLICY_NOTIFIER);
> + }
> + mutex_unlock(&cooling_cpufreq_lock);
> + thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
> + release_idr(&cpufreq_idr, cpufreq_dev->id);
> + if (cpufreq_dev_count == 1)
> + mutex_destroy(&cooling_cpufreq_lock);
> + kfree(cpufreq_dev);
> +}
> +EXPORT_SYMBOL(cpufreq_cooling_unregister);
> diff --git a/include/linux/cpu_cooling.h b/include/linux/cpu_cooling.h
> new file mode 100644
> index 0000000..555b917
> --- /dev/null
> +++ b/include/linux/cpu_cooling.h
> @@ -0,0 +1,79 @@
> +/*
> + * linux/include/linux/cpu_cooling.h
> + *
> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> + * Copyright (C) 2012 Amit Daniel <[email protected]>
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + * 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; version 2 of the License.
> + *
> + * This program is distributed in the hope that it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, write to the Free Software Foundation, Inc.,
> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + */
> +
> +#ifndef __CPU_COOLING_H__
> +#define __CPU_COOLING_H__
> +
> +#include <linux/thermal.h>
> +
> +#define CPUFREQ_COOLING_START 0
> +#define CPUFREQ_COOLING_STOP 1
> +
> +/**
> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. possible values are
> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> + *
> + * This exported function registers a driver with cpu cooling layer. The driver
> + * will be notified when any cpu cooling action is called.
> + */
> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list);
> +
> +/**
> + * cputherm_unregister_notifier - Un-register a notifier.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. values possible are
> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> + *
> + * This exported function un-registers a driver with cpu cooling layer.
> + */
> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list);
> +
> +#ifdef CONFIG_CPU_THERMAL
> +/**
> + * cpufreq_cooling_register - function to create cpufreq cooling device.
> + * @clip_cpus: cpumask of cpus where the frequency constraints will happen
> + */
> +struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus);
> +
> +/**
> + * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
> + * @cdev: thermal cooling device pointer.
> + */
> +void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev);
> +#else /* !CONFIG_CPU_THERMAL */
> +static inline struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus)
> +{
> + return NULL;
> +}
> +static inline void cpufreq_cooling_unregister(
> + struct thermal_cooling_device *cdev)
> +{
> + return;
> +}
> +#endif /* CONFIG_CPU_THERMAL */
> +
> +#endif /* __CPU_COOLING_H__ */
> --
> 1.7.1
>
--
Eduardo Valentin
On 16 August 2012 20:22, Valentin, Eduardo <[email protected]> wrote:
>
> Amit,
>
> Thanks again for keeping this up.
>
> On Thu, Aug 16, 2012 at 2:41 PM, Amit Daniel Kachhap
> <[email protected]> wrote:
> > This patchset introduces a new generic cooling device based on cpufreq
> > that can be used on non-ACPI platforms. As a proof of concept, we have
> > drivers for the following platforms using this mechanism now:
> >
> > * Samsung Exynos (Exynos4 and Exynos5) in the current patchset.
> > * Freescale i.MX (git://git.linaro.org/people/amitdanielk/linux.git
> > imx6q_thermal)
>
> FYI, the OMAP code is under drivers/staging/omap-thermal/. The file
> omap-thermal-common.c is the one which is using your generic cooling
> device. But it needs to be updated accordingly to the API change you
> mention.
>
> >
> > There is a small change in cpufreq cooling registration APIs, so a minor
> > change is needed for Freescale platforms.
> >
> > Brief Description:
> >
> > 1) The generic cooling devices code is placed inside driver/thermal/*
> > as placing inside acpi folder will need un-necessary enabling of acpi
> > code. This code is architecture independent.
> >
> > 2) This patchset adds generic cpu cooling low level implementation
> > through frequency clipping. In future, other cpu related cooling
> > devices may be added here. An ACPI version of this already exists
> > (drivers/acpi/processor_thermal.c) .But this will be useful for
> > platforms like ARM using the generic thermal interface along with the
> > generic cpu cooling devices. The cooling device registration API's
> > return cooling device pointers which can be easily binded with the
> > thermal zone trip points. The important APIs exposed are,
> >
> > a) struct thermal_cooling_device *cpufreq_cooling_register(
> > struct cpumask *clip_cpus)
> > b) void cpufreq_cooling_unregister(struct thermal_cooling_device
> > *cdev)
> >
> > 3) Samsung exynos platform thermal implementation is done using the
> > generic cpu cooling APIs and the new trip type. The temperature
> > sensor
> > driver present in the hwmon folder(registered as hwmon driver) is
> > moved
> > to thermal folder and registered as a thermal driver.
> >
> > A simple data/control flow diagrams is shown below,
> >
> > Core Linux thermal <-----> Exynos thermal interface <----- Temperature
> > Sensor
> > | |
> > \|/ |
> > Cpufreq cooling device <---------------
> >
> > TODO:
> > *Will send the DT enablement patches later after the driver is merged.
> >
> > This patch:
> >
> > Add support for generic cpu thermal cooling low level implementations
> > using frequency scaling up/down based on the registration parameters.
> > Different cpu related cooling devices can be registered by the user and
> > the binding of these cooling devices to the corresponding trip points
> > can
> > be easily done as the registration APIs return the cooling device
> > pointer.
> > The user of these APIs are responsible for passing clipping frequency .
> > The drivers can also register to recieve notification about any cooling
> > action called.
> >
> > [[email protected]: fix comment layout]
> > Signed-off-by: Amit Daniel Kachhap <[email protected]>
> > Cc: Guenter Roeck <[email protected]>
> > Cc: SangWook Ju <[email protected]>
> > Cc: Durgadoss <[email protected]>
> > Cc: Len Brown <[email protected]>
> > Cc: Jean Delvare <[email protected]>
> > Cc: Kyungmin Park <[email protected]>
> > Cc: Kukjin Kim <[email protected]>
> > Signed-off-by: Andrew Morton <[email protected]>
> > Signed-off-by: Amit Daniel Kachhap <[email protected]>
>
>
> Reviewed-by: Eduardo Valentin <[email protected]>
Thanks Eduardo .
>
>
> > ---
> > Documentation/thermal/cpu-cooling-api.txt | 52 +++
> > drivers/thermal/Kconfig | 11 +
> > drivers/thermal/Makefile | 1 +
> > drivers/thermal/cpu_cooling.c | 512
> > +++++++++++++++++++++++++++++
> > include/linux/cpu_cooling.h | 79 +++++
> > 5 files changed, 655 insertions(+), 0 deletions(-)
> > create mode 100644 Documentation/thermal/cpu-cooling-api.txt
> > create mode 100644 drivers/thermal/cpu_cooling.c
> > create mode 100644 include/linux/cpu_cooling.h
> >
> > diff --git a/Documentation/thermal/cpu-cooling-api.txt
> > b/Documentation/thermal/cpu-cooling-api.txt
> > new file mode 100644
> > index 0000000..a1f2a6b
> > --- /dev/null
> > +++ b/Documentation/thermal/cpu-cooling-api.txt
> > @@ -0,0 +1,52 @@
> > +CPU cooling APIs How To
> > +===================================
> > +
> > +Written by Amit Daniel Kachhap <[email protected]>
> > +
> > +Updated: 12 May 2012
> > +
> > +Copyright (c) 2012 Samsung Electronics Co.,
> > Ltd(http://www.samsung.com)
> > +
> > +0. Introduction
> > +
> > +The generic cpu cooling(freq clipping) provides
> > registration/unregistration APIs
> > +to the caller. The binding of the cooling devices to the trip point is
> > left for
> > +the user. The registration APIs returns the cooling device pointer.
> > +
> > +1. cpu cooling APIs
> > +
> > +1.1 cpufreq registration/unregistration APIs
> > +1.1.1 struct thermal_cooling_device *cpufreq_cooling_register(
> > + struct cpumask *clip_cpus)
> > +
> > + This interface function registers the cpufreq cooling device with
> > the name
> > + "thermal-cpufreq-%x". This api can support multiple instances of
> > cpufreq
> > + cooling devices.
> > +
> > + clip_cpus: cpumask of cpus where the frequency constraints will
> > happen.
> > +
> > +1.1.2 void cpufreq_cooling_unregister(struct thermal_cooling_device
> > *cdev)
> > +
> > + This interface function unregisters the "thermal-cpufreq-%x"
> > cooling device.
> > +
> > + cdev: Cooling device pointer which has to be unregistered.
> > +
> > +
> > +1.2 CPU cooling action notifier register/unregister interface
> > +1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
> > + unsigned int list)
> > +
> > + This interface registers a driver with cpu cooling layer. The
> > driver will
> > + be notified when any cpu cooling action is called.
> > +
> > + nb: notifier function to register
> > + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> > +
> > +1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
> > + unsigned int list)
> > +
> > + This interface registers a driver with cpu cooling layer. The
> > driver will
> > + be notified when any cpu cooling action is called.
> > +
> > + nb: notifier function to register
> > + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> > diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> > index 7dd8c34..996003b 100644
> > --- a/drivers/thermal/Kconfig
> > +++ b/drivers/thermal/Kconfig
> > @@ -19,6 +19,17 @@ config THERMAL_HWMON
> > depends on HWMON=y || HWMON=THERMAL
> > default y
> >
> > +config CPU_THERMAL
> > + bool "generic cpu cooling support"
> > + depends on THERMAL && CPU_FREQ
> > + help
> > + This implements the generic cpu cooling mechanism through
> > frequency
> > + reduction, cpu hotplug and any other ways of reducing
> > temperature. An
> > + ACPI version of this already
> > exists(drivers/acpi/processor_thermal.c).
> > + This will be useful for platforms using the generic thermal
> > interface
> > + and not the ACPI interface.
> > + If you want this support, you should say Y here.
> > +
> > config SPEAR_THERMAL
> > bool "SPEAr thermal sensor driver"
> > depends on THERMAL
> > diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> > index fd9369a..aae59ad 100644
> > --- a/drivers/thermal/Makefile
> > +++ b/drivers/thermal/Makefile
> > @@ -3,5 +3,6 @@
> > #
> >
> > obj-$(CONFIG_THERMAL) += thermal_sys.o
> > +obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
> > obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
> > obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
> > diff --git a/drivers/thermal/cpu_cooling.c
> > b/drivers/thermal/cpu_cooling.c
> > new file mode 100644
> > index 0000000..c42e557
> > --- /dev/null
> > +++ b/drivers/thermal/cpu_cooling.c
> > @@ -0,0 +1,512 @@
> > +/*
> > + * linux/drivers/thermal/cpu_cooling.c
> > + *
> > + * Copyright (C) 2012 Samsung Electronics Co.,
> > Ltd(http://www.samsung.com)
> > + * Copyright (C) 2012 Amit Daniel <[email protected]>
> > + *
> > + *
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > + * 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; version 2 of the License.
> > + *
> > + * This program is distributed in the hope that it will be useful, but
> > + * WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> > + * General Public License for more details.
> > + *
> > + * You should have received a copy of the GNU General Public License
> > along
> > + * with this program; if not, write to the Free Software Foundation,
> > Inc.,
> > + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> > + *
> > + *
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > + */
> > +#include <linux/kernel.h>
> > +#include <linux/module.h>
> > +#include <linux/thermal.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/cpufreq.h>
> > +#include <linux/err.h>
> > +#include <linux/slab.h>
> > +#include <linux/cpu.h>
> > +#include <linux/cpu_cooling.h>
> > +
> > +/**
> > + * struct cpufreq_cooling_device
> > + * @id: unique integer value corresponding to each
> > cpufreq_cooling_device
> > + * registered.
> > + * @cool_dev: thermal_cooling_device pointer to keep track of the the
> > + * egistered cooling device.
> > + * @cpufreq_state: integer value representing the current state of
> > cpufreq
> > + * cooling devices.
> > + * @cpufreq_val: integer value representing the absolute value of the
> > clipped
> > + * frequency.
> > + * @allowed_cpus: all the cpus involved for this
> > cpufreq_cooling_device.
> > + * @node: list_head to link all cpufreq_cooling_device together.
> > + *
> > + * This structure is required for keeping information of each
> > + * cpufreq_cooling_device registered as a list whose head is
> > represented by
> > + * cooling_cpufreq_list. In order to prevent corruption of this list a
> > + * mutex lock cooling_cpufreq_lock is used.
> > + */
> > +struct cpufreq_cooling_device {
> > + int id;
> > + struct thermal_cooling_device *cool_dev;
> > + unsigned int cpufreq_state;
> > + unsigned int cpufreq_val;
> > + struct cpumask allowed_cpus;
> > + struct list_head node;
> > +};
> > +static LIST_HEAD(cooling_cpufreq_list);
> > +static DEFINE_IDR(cpufreq_idr);
> > +
> > +static struct mutex cooling_cpufreq_lock;
> > +
> > +/* notify_table passes value to the CPUFREQ_ADJUST callback function.
> > */
> > +#define NOTIFY_INVALID NULL
> > +struct cpufreq_cooling_device *notify_device;
> > +
> > +/* Head of the blocking notifier chain to inform about frequency
> > clamping */
> > +static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
> > +
> > +/**
> > + * get_idr - function to get a unique id.
> > + * @idr: struct idr * handle used to create a id.
> > + * @id: int * value generated by this function.
> > + */
> > +static int get_idr(struct idr *idr, int *id)
> > +{
> > + int err;
> > +again:
> > + if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
> > + return -ENOMEM;
> > +
> > + mutex_lock(&cooling_cpufreq_lock);
> > + err = idr_get_new(idr, NULL, id);
> > + mutex_unlock(&cooling_cpufreq_lock);
> > +
> > + if (unlikely(err == -EAGAIN))
> > + goto again;
> > + else if (unlikely(err))
> > + return err;
> > +
> > + *id = *id & MAX_ID_MASK;
> > + return 0;
> > +}
> > +
> > +/**
> > + * release_idr - function to free the unique id.
> > + * @idr: struct idr * handle used for creating the id.
> > + * @id: int value representing the unique id.
> > + */
> > +static void release_idr(struct idr *idr, int id)
> > +{
> > + mutex_lock(&cooling_cpufreq_lock);
> > + idr_remove(idr, id);
> > + mutex_unlock(&cooling_cpufreq_lock);
> > +}
> > +
> > +/**
> > + * cputherm_register_notifier - Register a notifier with cpu cooling
> > interface.
> > + * @nb: struct notifier_block * with callback info.
> > + * @list: integer value for which notification is needed. possible
> > values are
> > + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> > + *
> > + * This exported function registers a driver with cpu cooling layer.
> > The driver
> > + * will be notified when any cpu cooling action is called.
> > + */
> > +int cputherm_register_notifier(struct notifier_block *nb, unsigned int
> > list)
> > +{
> > + int ret = 0;
> > +
> > + switch (list) {
> > + case CPUFREQ_COOLING_START:
> > + case CPUFREQ_COOLING_STOP:
> > + ret = blocking_notifier_chain_register(
> > + &cputherm_state_notifier_list, nb);
> > + break;
> > + default:
> > + ret = -EINVAL;
> > + }
> > + return ret;
> > +}
> > +EXPORT_SYMBOL(cputherm_register_notifier);
> > +
> > +/**
> > + * cputherm_unregister_notifier - Un-register a notifier.
> > + * @nb: struct notifier_block * with callback info.
> > + * @list: integer value for which notification is needed. values
> > possible are
> > + * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
> > + *
> > + * This exported function un-registers a driver with cpu cooling layer.
> > + */
> > +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned
> > int list)
> > +{
> > + int ret = 0;
> > +
> > + switch (list) {
> > + case CPUFREQ_COOLING_START:
> > + case CPUFREQ_COOLING_STOP:
> > + ret = blocking_notifier_chain_unregister(
> > + &cputherm_state_notifier_list, nb);
> > + break;
> > + default:
> > + ret = -EINVAL;
> > + }
> > + return ret;
> > +}
> > +EXPORT_SYMBOL(cputherm_unregister_notifier);
> > +
> > +/* Below code defines functions to be used for cpufreq as cooling
> > device */
> > +
> > +/**
> > + * is_cpufreq_valid - function to check if a cpu has frequency
> > transition policy.
> > + * @cpu: cpu for which check is needed.
> > + */
> > +static int is_cpufreq_valid(int cpu)
> > +{
> > + struct cpufreq_policy policy;
> > + return !cpufreq_get_policy(&policy, cpu);
> > +}
> > +
> > +/**
> > + * get_cpu_frequency - get the absolute value of frequency from level.
> > + * @cpu: cpu for which frequency is fetched.
> > + * @level: level of frequency of the CPU
> > + * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, ....
> > etc
> > + */
> > +static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long
> > level)
>
>
> I like this. With previous implemention I had to write a similar
> function, but with output of the required freq clip table. Now that
> you have the freq walk here it simplifies the zone setup code.
>
>
> > +{
> > + int ret = 0, i = 0;
> > + unsigned long level_index;
> > + bool descend = false;
> > + struct cpufreq_frequency_table *table =
> > +
> > cpufreq_frequency_get_table(cpu);
> > + if (!table)
> > + return ret;
> > +
> > + while (table[i].frequency != CPUFREQ_TABLE_END) {
> > + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> > + continue;
> > +
> > + /*check if table in ascending or descending order*/
> > + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
> > + (table[i + 1].frequency < table[i].frequency)
> > + && !descend) {
> > + descend = true;
> > + }
> > +
> > + /*return if level matched and table in descending
> > order*/
> > + if (descend && i == level)
> > + return table[i].frequency;
> > + i++;
> > + }
> > + i--;
> > +
> > + if (level > i || descend)
> > + return ret;
> > + level_index = i - level;
> > +
> > + /*Scan the table in reverse order and match the level*/
> > + while (i >= 0) {
> > + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> > + continue;
> > + /*return if level matched*/
> > + if (i == level_index)
> > + return table[i].frequency;
> > + i--;
> > + }
> > + return ret;
> > +}
> > +
> > +/**
> > + * cpufreq_apply_cooling - function to apply frequency clipping.
> > + * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
> > + * clipping data.
> > + * @cooling_state: value of the cooling state.
> > + */
> > +static int cpufreq_apply_cooling(struct cpufreq_cooling_device
> > *cpufreq_device,
> > + unsigned long cooling_state)
> > +{
> > + unsigned int event, cpuid, clip_freq;
> > + struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
> > + unsigned int cpu = cpumask_any(maskPtr);
> > +
> > +
> > + /* Check if the old cooling action is same as new cooling action
> > */
> > + if (cpufreq_device->cpufreq_state == cooling_state)
> > + return 0;
> > +
> > + clip_freq = get_cpu_frequency(cpu, cooling_state);
> > + if (!clip_freq)
> > + return -EINVAL;
> > +
> > + cpufreq_device->cpufreq_state = cooling_state;
> > + cpufreq_device->cpufreq_val = clip_freq;
> > + notify_device = cpufreq_device;
> > +
> > + if (cooling_state != 0)
> > + event = CPUFREQ_COOLING_START;
> > + else
> > + event = CPUFREQ_COOLING_STOP;
> > +
> > + blocking_notifier_call_chain(&cputherm_state_notifier_list,
> > + event, &clip_freq);
> > +
> > + for_each_cpu(cpuid, maskPtr) {
> > + if (is_cpufreq_valid(cpuid))
> > + cpufreq_update_policy(cpuid);
> > + }
> > +
> > + notify_device = NOTIFY_INVALID;
> > +
> > + return 0;
> > +}
> > +
> > +/**
> > + * cpufreq_thermal_notifier - notifier callback for cpufreq policy
> > change.
> > + * @nb: struct notifier_block * with callback info.
> > + * @event: value showing cpufreq event for which this function invoked.
> > + * @data: callback-specific data
> > + */
> > +static int cpufreq_thermal_notifier(struct notifier_block *nb,
> > + unsigned long event, void *data)
> > +{
> > + struct cpufreq_policy *policy = data;
> > + unsigned long max_freq = 0;
> > +
> > + if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
> > + return 0;
> > +
> > + if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
> > + max_freq = notify_device->cpufreq_val;
> > +
> > + /* Never exceed user_policy.max*/
> > + if (max_freq > policy->user_policy.max)
> > + max_freq = policy->user_policy.max;
> > +
> > + if (policy->max != max_freq)
> > + cpufreq_verify_within_limits(policy, 0, max_freq);
> > +
> > + return 0;
> > +}
> > +
> > +/*
> > + * cpufreq cooling device callback functions are defined below
> > + */
> > +
> > +/**
> > + * cpufreq_get_max_state - callback function to get the max cooling
> > state.
> > + * @cdev: thermal cooling device pointer.
> > + * @state: fill this variable with the max cooling state.
> > + */
> > +static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
> > + unsigned long *state)
> > +{
> > + int ret = -EINVAL, i = 0;
> > + struct cpufreq_cooling_device *cpufreq_device;
> > + struct cpumask *maskPtr;
> > + unsigned int cpu;
> > + struct cpufreq_frequency_table *table;
> > +
> > + mutex_lock(&cooling_cpufreq_lock);
> > + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node)
> > {
> > + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
> > + break;
> > + }
> > + if (cpufreq_device == NULL)
> > + goto return_get_max_state;
> > +
> > + maskPtr = &cpufreq_device->allowed_cpus;
> > + cpu = cpumask_any(maskPtr);
> > + table = cpufreq_frequency_get_table(cpu);
> > + if (!table) {
> > + *state = 0;
> > + ret = 0;
> > + goto return_get_max_state;
> > + }
> > +
> > + while (table[i].frequency != CPUFREQ_TABLE_END) {
> > + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> > + continue;
> > + i++;
> > + }
> > + if (i > 0) {
> > + *state = --i;
> > + ret = 0;
> > + }
> > +
> > +return_get_max_state:
> > + mutex_unlock(&cooling_cpufreq_lock);
> > + return ret;
> > +}
> > +
> > +/**
> > + * cpufreq_get_cur_state - callback function to get the current cooling
> > state.
> > + * @cdev: thermal cooling device pointer.
> > + * @state: fill this variable with the current cooling state.
> > + */
> > +static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
> > + unsigned long *state)
> > +{
> > + int ret = -EINVAL;
> > + struct cpufreq_cooling_device *cpufreq_device;
> > +
> > + mutex_lock(&cooling_cpufreq_lock);
> > + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node)
> > {
> > + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
> > {
> > + *state = cpufreq_device->cpufreq_state;
> > + ret = 0;
> > + break;
> > + }
> > + }
> > + mutex_unlock(&cooling_cpufreq_lock);
> > +
> > + return ret;
> > +}
> > +
> > +/**
> > + * cpufreq_set_cur_state - callback function to set the current cooling
> > state.
> > + * @cdev: thermal cooling device pointer.
> > + * @state: set this variable to the current cooling state.
> > + */
> > +static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
> > + unsigned long state)
> > +{
> > + int ret = -EINVAL;
> > + struct cpufreq_cooling_device *cpufreq_device;
> > +
> > + mutex_lock(&cooling_cpufreq_lock);
> > + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node)
> > {
> > + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
> > {
> > + ret = 0;
> > + break;
> > + }
> > + }
> > + if (!ret)
> > + ret = cpufreq_apply_cooling(cpufreq_device, state);
> > +
> > + mutex_unlock(&cooling_cpufreq_lock);
> > +
> > + return ret;
> > +}
> > +
> > +/* Bind cpufreq callbacks to thermal cooling device ops */
> > +static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
> > + .get_max_state = cpufreq_get_max_state,
> > + .get_cur_state = cpufreq_get_cur_state,
> > + .set_cur_state = cpufreq_set_cur_state,
> > +};
> > +
> > +/* Notifier for cpufreq policy change */
> > +static struct notifier_block thermal_cpufreq_notifier_block = {
> > + .notifier_call = cpufreq_thermal_notifier,
> > +};
> > +
> > +/**
> > + * cpufreq_cooling_register - function to create cpufreq cooling
> > device.
> > + * @clip_cpus: cpumask of cpus where the frequency constraints will
> > happen.
> > + */
> > +struct thermal_cooling_device *cpufreq_cooling_register(
> > + struct cpumask *clip_cpus)
> > +{
> > + struct thermal_cooling_device *cool_dev;
> > + struct cpufreq_cooling_device *cpufreq_dev = NULL;
> > + unsigned int cpufreq_dev_count = 0, min = 0, max = 0;
> > + char dev_name[THERMAL_NAME_LENGTH];
> > + int ret = 0, id = 0, i;
> > + struct cpufreq_policy policy;
> > +
> > + list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node)
> > + cpufreq_dev_count++;
> > +
> > + /*Verify that all the clip cpus have same freq_min, freq_max
> > limit*/
> > + for_each_cpu(i, clip_cpus) {
> > + /*continue if cpufreq policy not found and not return
> > error*/
> > + if (!cpufreq_get_policy(&policy, i))
> > + continue;
> > + if (min == 0 && max == 0) {
> > + min = policy.cpuinfo.min_freq;
> > + max = policy.cpuinfo.max_freq;
> > + } else {
> > + if (min != policy.cpuinfo.min_freq ||
> > + max != policy.cpuinfo.max_freq)
> > + return ERR_PTR(-EINVAL);
> > +}
> > + }
> > + cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
> > + GFP_KERNEL);
> > + if (!cpufreq_dev)
> > + return ERR_PTR(-ENOMEM);
> > +
> > + cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
> > +
> > + if (cpufreq_dev_count == 0)
> > + mutex_init(&cooling_cpufreq_lock);
> > +
> > + ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
> > + if (ret) {
> > + kfree(cpufreq_dev);
> > + return ERR_PTR(-EINVAL);
> > + }
> > +
> > + sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
> > +
> > + cool_dev = thermal_cooling_device_register(dev_name,
> > cpufreq_dev,
> > + &cpufreq_cooling_ops);
> > + if (!cool_dev) {
> > + release_idr(&cpufreq_idr, cpufreq_dev->id);
> > + kfree(cpufreq_dev);
> > + return ERR_PTR(-EINVAL);
> > + }
> > + cpufreq_dev->id = id;
> > + cpufreq_dev->cool_dev = cool_dev;
> > + cpufreq_dev->cpufreq_state = 0;
> > + mutex_lock(&cooling_cpufreq_lock);
> > + list_add_tail(&cpufreq_dev->node, &cooling_cpufreq_list);
> > +
> > + /* Register the notifier for first cpufreq cooling device */
> > + if (cpufreq_dev_count == 0)
> > +
> > cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
> > +
> > CPUFREQ_POLICY_NOTIFIER);
> > +
> > + mutex_unlock(&cooling_cpufreq_lock);
> > + return cool_dev;
> > +}
> > +EXPORT_SYMBOL(cpufreq_cooling_register);
> > +
> > +/**
> > + * cpufreq_cooling_unregister - function to remove cpufreq cooling
> > device.
> > + * @cdev: thermal cooling device pointer.
> > + */
> > +void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
> > +{
> > + struct cpufreq_cooling_device *cpufreq_dev = NULL;
> > + unsigned int cpufreq_dev_count = 0;
> > +
> > + mutex_lock(&cooling_cpufreq_lock);
> > + list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node) {
> > + if (cpufreq_dev && cpufreq_dev->cool_dev == cdev)
> > + break;
> > + cpufreq_dev_count++;
> > + }
> > +
> > + if (!cpufreq_dev || cpufreq_dev->cool_dev != cdev) {
> > + mutex_unlock(&cooling_cpufreq_lock);
> > + return;
> > + }
> > +
> > + list_del(&cpufreq_dev->node);
> > +
> > + /* Unregister the notifier for the last cpufreq cooling device
> > */
> > + if (cpufreq_dev_count == 1) {
> > +
> > cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
> > + CPUFREQ_POLICY_NOTIFIER);
> > + }
> > + mutex_unlock(&cooling_cpufreq_lock);
> > + thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
> > + release_idr(&cpufreq_idr, cpufreq_dev->id);
> > + if (cpufreq_dev_count == 1)
> > + mutex_destroy(&cooling_cpufreq_lock);
> > + kfree(cpufreq_dev);
> > +}
> > +EXPORT_SYMBOL(cpufreq_cooling_unregister);
> > diff --git a/include/linux/cpu_cooling.h b/include/linux/cpu_cooling.h
> > new file mode 100644
> > index 0000000..555b917
> > --- /dev/null
> > +++ b/include/linux/cpu_cooling.h
> > @@ -0,0 +1,79 @@
> > +/*
> > + * linux/include/linux/cpu_cooling.h
> > + *
> > + * Copyright (C) 2012 Samsung Electronics Co.,
> > Ltd(http://www.samsung.com)
> > + * Copyright (C) 2012 Amit Daniel <[email protected]>
> > + *
> > + *
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > + * 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; version 2 of the License.
> > + *
> > + * This program is distributed in the hope that it will be useful, but
> > + * WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> > + * General Public License for more details.
> > + *
> > + * You should have received a copy of the GNU General Public License
> > along
> > + * with this program; if not, write to the Free Software Foundation,
> > Inc.,
> > + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> > + *
> > + *
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > + */
> > +
> > +#ifndef __CPU_COOLING_H__
> > +#define __CPU_COOLING_H__
> > +
> > +#include <linux/thermal.h>
> > +
> > +#define CPUFREQ_COOLING_START 0
> > +#define CPUFREQ_COOLING_STOP 1
> > +
> > +/**
> > + * cputherm_register_notifier - Register a notifier with cpu cooling
> > interface.
> > + * @nb: struct notifier_block * with callback info.
> > + * @list: integer value for which notification is needed. possible
> > values are
> > + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> > + *
> > + * This exported function registers a driver with cpu cooling layer.
> > The driver
> > + * will be notified when any cpu cooling action is called.
> > + */
> > +int cputherm_register_notifier(struct notifier_block *nb, unsigned int
> > list);
> > +
> > +/**
> > + * cputherm_unregister_notifier - Un-register a notifier.
> > + * @nb: struct notifier_block * with callback info.
> > + * @list: integer value for which notification is needed. values
> > possible are
> > + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> > + *
> > + * This exported function un-registers a driver with cpu cooling layer.
> > + */
> > +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned
> > int list);
> > +
> > +#ifdef CONFIG_CPU_THERMAL
> > +/**
> > + * cpufreq_cooling_register - function to create cpufreq cooling
> > device.
> > + * @clip_cpus: cpumask of cpus where the frequency constraints will
> > happen
> > + */
> > +struct thermal_cooling_device *cpufreq_cooling_register(
> > + struct cpumask *clip_cpus);
> > +
> > +/**
> > + * cpufreq_cooling_unregister - function to remove cpufreq cooling
> > device.
> > + * @cdev: thermal cooling device pointer.
> > + */
> > +void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev);
> > +#else /* !CONFIG_CPU_THERMAL */
> > +static inline struct thermal_cooling_device *cpufreq_cooling_register(
> > + struct cpumask *clip_cpus)
> > +{
> > + return NULL;
> > +}
> > +static inline void cpufreq_cooling_unregister(
> > + struct thermal_cooling_device *cdev)
> > +{
> > + return;
> > +}
> > +#endif /* CONFIG_CPU_THERMAL */
> > +
> > +#endif /* __CPU_COOLING_H__ */
> > --
> > 1.7.1
> >
>
>
>
> --
>
> Eduardo Valentin
On 16 August 2012 17:11, Amit Daniel Kachhap <[email protected]> wrote:
> Add necessary default platform data support needed for TMU driver. The
> supplied dt/non-dt values are tested for origen exynos4210 and smdk exynos5250
> platforms and only compile tested for exynos4412.
>
> Signed-off-by: Amit Daniel Kachhap <[email protected]>
> Acked-by: Guenter Roeck <[email protected]>
> Cc: SangWook Ju <[email protected]>
> Cc: Durgadoss <[email protected]>
> Cc: Len Brown <[email protected]>
> Cc: Jean Delvare <[email protected]>
> Cc: jonghwa lee <[email protected]>
> Cc: Kyungmin Park <[email protected]>
> Cc: Kukjin Kim <[email protected]>
> Signed-off-by: Andrew Morton <[email protected]>
> Signed-off-by: Amit Daniel Kachhap <[email protected]>
> ---
> drivers/thermal/exynos_thermal.c | 111 +++++++++++++++++++++++++++++++++++++-
> 1 files changed, 110 insertions(+), 1 deletions(-)
Reviewed-by: Thomas Abraham <[email protected]>
Few minor comments inline below.
>
> diff --git a/drivers/thermal/exynos_thermal.c b/drivers/thermal/exynos_thermal.c
> index e79cdc9..03a99e4 100644
> --- a/drivers/thermal/exynos_thermal.c
> +++ b/drivers/thermal/exynos_thermal.c
> @@ -723,14 +723,121 @@ static irqreturn_t exynos_tmu_irq(int irq, void *id)
> static struct thermal_sensor_conf exynos_sensor_conf = {
> .name = "exynos-therm",
> .read_temperature = (int (*)(void *))exynos_tmu_read,
> +};
> +
> +#if defined(CONFIG_CPU_EXYNOS4210)
> +static struct exynos_tmu_platform_data const exynos4210_default_tmu_data = {
> + .threshold = 80,
> + .trigger_levels[0] = 5,
> + .trigger_levels[1] = 20,
> + .trigger_levels[2] = 30,
> + .trigger_level0_en = 1,
> + .trigger_level1_en = 1,
> + .trigger_level2_en = 1,
> + .trigger_level3_en = 0,
> + .gain = 15,
> + .reference_voltage = 7,
> + .cal_type = TYPE_ONE_POINT_TRIMMING,
> + .freq_tab[0] = {
> + .freq_clip_max = 800 * 1000,
> + .temp_level = 85,
> + },
> + .freq_tab[1] = {
> + .freq_clip_max = 200 * 1000,
> + .temp_level = 100,
> + },
> + .freq_tab_count = 2,
> + .type = SOC_ARCH_EXYNOS4210,
> +};
> +#define EXYNOS4210_TMU_DRV_DATA (&exynos4210_default_tmu_data)
> +#else
> +#define EXYNOS4210_TMU_DRV_DATA (NULL)
> +#endif
> +
> +#if defined(CONFIG_SOC_EXYNOS5250) || defined(CONFIG_SOC_EXYNOS4412)
> +static struct exynos_tmu_platform_data const exynos_default_tmu_data = {
> + .trigger_levels[0] = 85,
> + .trigger_levels[1] = 103,
> + .trigger_levels[2] = 110,
> + .trigger_level0_en = 1,
> + .trigger_level1_en = 1,
> + .trigger_level2_en = 1,
> + .trigger_level3_en = 0,
> + .gain = 8,
> + .reference_voltage = 16,
> + .noise_cancel_mode = 4,
> + .cal_type = TYPE_ONE_POINT_TRIMMING,
> + .efuse_value = 55,
> + .freq_tab[0] = {
> + .freq_clip_max = 800 * 1000,
> + .temp_level = 85,
> + },
> + .freq_tab[1] = {
> + .freq_clip_max = 200 * 1000,
> + .temp_level = 103,
> + },
> + .freq_tab_count = 2,
> + .type = SOC_ARCH_EXYNOS,
> +};
> +#define EXYNOS_TMU_DRV_DATA (&exynos_default_tmu_data)
> +#else
> +#define EXYNOS_TMU_DRV_DATA (NULL)
> +#endif
> +
> +#ifdef CONFIG_OF
> +static const struct of_device_id exynos_tmu_match[] = {
> + {
> + .compatible = "samsung,exynos4210-tmu",
> + .data = (void *)EXYNOS4210_TMU_DRV_DATA,
> + },
> + {
This can be }, {
> + .compatible = "samsung,exynos5250-tmu",
> + .data = (void *)EXYNOS_TMU_DRV_DATA,
> + },
> + {},
> +};
> +MODULE_DEVICE_TABLE(of, exynos_tmu_match);
> +#else
> +#define exynos_tmu_match NULL
> +#endif
> +
> +static struct platform_device_id exynos_tmu_driver_ids[] = {
> + {
> + .name = "exynos4210-tmu",
> + .driver_data = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
> + },
> + {
> + .name = "exynos5250-tmu",
> + .driver_data = (kernel_ulong_t)EXYNOS_TMU_DRV_DATA,
> + },
Since Exynos5250 platforms are dt based, the above entry could be left out.
> + { },
> +};
> +MODULE_DEVICE_TABLE(platform, exynos4_tmu_driver_ids);
> +
> +static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
> + struct platform_device *pdev)
> +{
> +#ifdef CONFIG_OF
> + if (pdev->dev.of_node) {
> + const struct of_device_id *match;
> + match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
> + if (!match)
> + return NULL;
> + return (struct exynos_tmu_platform_data *) match->data;
> + }
> +#endif
> + return (struct exynos_tmu_platform_data *)
> + platform_get_device_id(pdev)->driver_data;
> }
> -;
> static int __devinit exynos_tmu_probe(struct platform_device *pdev)
> {
> struct exynos_tmu_data *data;
> struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
> int ret, i;
>
> + if (!pdata)
> + pdata = exynos_get_driver_data(pdev);
> +
> if (!pdata) {
> dev_err(&pdev->dev, "No platform init data supplied.\n");
> return -ENODEV;
> @@ -899,9 +1006,11 @@ static struct platform_driver exynos_tmu_driver = {
> .name = "exynos-tmu",
> .owner = THIS_MODULE,
> .pm = EXYNOS_TMU_PM,
> + .of_match_table = exynos_tmu_match,
> },
> .probe = exynos_tmu_probe,
> .remove = __devexit_p(exynos_tmu_remove),
> + .id_table = exynos_tmu_driver_ids,
> };
>
> module_platform_driver(exynos_tmu_driver);
> --
> 1.7.1
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
On 四, 2012-08-16 at 17:11 +0530, Amit Daniel Kachhap wrote:
> This patchset introduces a new generic cooling device based on cpufreq
> that can be used on non-ACPI platforms. As a proof of concept, we have
> drivers for the following platforms using this mechanism now:
>
> * Samsung Exynos (Exynos4 and Exynos5) in the current patchset.
> * Freescale i.MX (git://git.linaro.org/people/amitdanielk/linux.git imx6q_thermal)
>
> There is a small change in cpufreq cooling registration APIs, so a minor
> change is needed for Freescale platforms.
>
> Brief Description:
>
> 1) The generic cooling devices code is placed inside driver/thermal/*
> as placing inside acpi folder will need un-necessary enabling of acpi
> code. This code is architecture independent.
>
> 2) This patchset adds generic cpu cooling low level implementation
> through frequency clipping. In future, other cpu related cooling
> devices may be added here. An ACPI version of this already exists
> (drivers/acpi/processor_thermal.c) .But this will be useful for
> platforms like ARM using the generic thermal interface along with the
> generic cpu cooling devices. The cooling device registration API's
> return cooling device pointers which can be easily binded with the
> thermal zone trip points. The important APIs exposed are,
>
> a) struct thermal_cooling_device *cpufreq_cooling_register(
> struct cpumask *clip_cpus)
> b) void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
>
> 3) Samsung exynos platform thermal implementation is done using the
> generic cpu cooling APIs and the new trip type. The temperature sensor
> driver present in the hwmon folder(registered as hwmon driver) is moved
> to thermal folder and registered as a thermal driver.
>
> A simple data/control flow diagrams is shown below,
>
> Core Linux thermal <-----> Exynos thermal interface <----- Temperature Sensor
> | |
> \|/ |
> Cpufreq cooling device <---------------
>
> TODO:
> *Will send the DT enablement patches later after the driver is merged.
>
> This patch:
>
> Add support for generic cpu thermal cooling low level implementations
> using frequency scaling up/down based on the registration parameters.
> Different cpu related cooling devices can be registered by the user and
> the binding of these cooling devices to the corresponding trip points can
> be easily done as the registration APIs return the cooling device pointer.
> The user of these APIs are responsible for passing clipping frequency .
> The drivers can also register to recieve notification about any cooling
> action called.
>
> [[email protected]: fix comment layout]
> Signed-off-by: Amit Daniel Kachhap <[email protected]>
> Cc: Guenter Roeck <[email protected]>
> Cc: SangWook Ju <[email protected]>
> Cc: Durgadoss <[email protected]>
> Cc: Len Brown <[email protected]>
> Cc: Jean Delvare <[email protected]>
> Cc: Kyungmin Park <[email protected]>
> Cc: Kukjin Kim <[email protected]>
> Signed-off-by: Andrew Morton <[email protected]>
> Signed-off-by: Amit Daniel Kachhap <[email protected]>
> ---
> Documentation/thermal/cpu-cooling-api.txt | 52 +++
> drivers/thermal/Kconfig | 11 +
> drivers/thermal/Makefile | 1 +
> drivers/thermal/cpu_cooling.c | 512 +++++++++++++++++++++++++++++
> include/linux/cpu_cooling.h | 79 +++++
> 5 files changed, 655 insertions(+), 0 deletions(-)
> create mode 100644 Documentation/thermal/cpu-cooling-api.txt
> create mode 100644 drivers/thermal/cpu_cooling.c
> create mode 100644 include/linux/cpu_cooling.h
>
> diff --git a/Documentation/thermal/cpu-cooling-api.txt b/Documentation/thermal/cpu-cooling-api.txt
> new file mode 100644
> index 0000000..a1f2a6b
> --- /dev/null
> +++ b/Documentation/thermal/cpu-cooling-api.txt
> @@ -0,0 +1,52 @@
> +CPU cooling APIs How To
> +===================================
> +
> +Written by Amit Daniel Kachhap <[email protected]>
> +
> +Updated: 12 May 2012
> +
> +Copyright (c) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> +
> +0. Introduction
> +
> +The generic cpu cooling(freq clipping) provides registration/unregistration APIs
> +to the caller. The binding of the cooling devices to the trip point is left for
> +the user. The registration APIs returns the cooling device pointer.
> +
> +1. cpu cooling APIs
> +
> +1.1 cpufreq registration/unregistration APIs
> +1.1.1 struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus)
> +
> + This interface function registers the cpufreq cooling device with the name
> + "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
> + cooling devices.
> +
> + clip_cpus: cpumask of cpus where the frequency constraints will happen.
> +
> +1.1.2 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
> +
> + This interface function unregisters the "thermal-cpufreq-%x" cooling device.
> +
> + cdev: Cooling device pointer which has to be unregistered.
> +
> +
> +1.2 CPU cooling action notifier register/unregister interface
> +1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
> + unsigned int list)
> +
> + This interface registers a driver with cpu cooling layer. The driver will
> + be notified when any cpu cooling action is called.
> +
> + nb: notifier function to register
> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> +
> +1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
> + unsigned int list)
> +
> + This interface registers a driver with cpu cooling layer. The driver will
> + be notified when any cpu cooling action is called.
> +
> + nb: notifier function to register
> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
what are these two APIs used for?
I did not see they are used in your patch set, do I miss something?
> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index 7dd8c34..996003b 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -19,6 +19,17 @@ config THERMAL_HWMON
> depends on HWMON=y || HWMON=THERMAL
> default y
>
> +config CPU_THERMAL
> + bool "generic cpu cooling support"
> + depends on THERMAL && CPU_FREQ
> + help
> + This implements the generic cpu cooling mechanism through frequency
> + reduction, cpu hotplug and any other ways of reducing temperature. An
> + ACPI version of this already exists(drivers/acpi/processor_thermal.c).
> + This will be useful for platforms using the generic thermal interface
> + and not the ACPI interface.
> + If you want this support, you should say Y here.
> +
> config SPEAR_THERMAL
> bool "SPEAr thermal sensor driver"
> depends on THERMAL
> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> index fd9369a..aae59ad 100644
> --- a/drivers/thermal/Makefile
> +++ b/drivers/thermal/Makefile
> @@ -3,5 +3,6 @@
> #
>
> obj-$(CONFIG_THERMAL) += thermal_sys.o
> +obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
> obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
> obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
> new file mode 100644
> index 0000000..c42e557
> --- /dev/null
> +++ b/drivers/thermal/cpu_cooling.c
> @@ -0,0 +1,512 @@
> +/*
> + * linux/drivers/thermal/cpu_cooling.c
> + *
> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> + * Copyright (C) 2012 Amit Daniel <[email protected]>
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + * 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; version 2 of the License.
> + *
> + * This program is distributed in the hope that it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, write to the Free Software Foundation, Inc.,
> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + */
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/thermal.h>
> +#include <linux/platform_device.h>
> +#include <linux/cpufreq.h>
> +#include <linux/err.h>
> +#include <linux/slab.h>
> +#include <linux/cpu.h>
> +#include <linux/cpu_cooling.h>
> +
> +/**
> + * struct cpufreq_cooling_device
> + * @id: unique integer value corresponding to each cpufreq_cooling_device
> + * registered.
> + * @cool_dev: thermal_cooling_device pointer to keep track of the the
> + * egistered cooling device.
> + * @cpufreq_state: integer value representing the current state of cpufreq
> + * cooling devices.
> + * @cpufreq_val: integer value representing the absolute value of the clipped
> + * frequency.
> + * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
> + * @node: list_head to link all cpufreq_cooling_device together.
> + *
> + * This structure is required for keeping information of each
> + * cpufreq_cooling_device registered as a list whose head is represented by
> + * cooling_cpufreq_list. In order to prevent corruption of this list a
> + * mutex lock cooling_cpufreq_lock is used.
> + */
> +struct cpufreq_cooling_device {
> + int id;
> + struct thermal_cooling_device *cool_dev;
> + unsigned int cpufreq_state;
> + unsigned int cpufreq_val;
> + struct cpumask allowed_cpus;
> + struct list_head node;
> +};
> +static LIST_HEAD(cooling_cpufreq_list);
> +static DEFINE_IDR(cpufreq_idr);
> +
> +static struct mutex cooling_cpufreq_lock;
> +
> +/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
> +#define NOTIFY_INVALID NULL
> +struct cpufreq_cooling_device *notify_device;
> +
> +/* Head of the blocking notifier chain to inform about frequency clamping */
> +static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
> +
> +/**
> + * get_idr - function to get a unique id.
> + * @idr: struct idr * handle used to create a id.
> + * @id: int * value generated by this function.
> + */
> +static int get_idr(struct idr *idr, int *id)
> +{
> + int err;
> +again:
> + if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
> + return -ENOMEM;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + err = idr_get_new(idr, NULL, id);
> + mutex_unlock(&cooling_cpufreq_lock);
> +
> + if (unlikely(err == -EAGAIN))
> + goto again;
> + else if (unlikely(err))
> + return err;
> +
> + *id = *id & MAX_ID_MASK;
> + return 0;
> +}
> +
> +/**
> + * release_idr - function to free the unique id.
> + * @idr: struct idr * handle used for creating the id.
> + * @id: int value representing the unique id.
> + */
> +static void release_idr(struct idr *idr, int id)
> +{
> + mutex_lock(&cooling_cpufreq_lock);
> + idr_remove(idr, id);
> + mutex_unlock(&cooling_cpufreq_lock);
> +}
> +
> +/**
> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. possible values are
> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> + *
> + * This exported function registers a driver with cpu cooling layer. The driver
> + * will be notified when any cpu cooling action is called.
> + */
> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
> +{
> + int ret = 0;
> +
> + switch (list) {
> + case CPUFREQ_COOLING_START:
> + case CPUFREQ_COOLING_STOP:
> + ret = blocking_notifier_chain_register(
> + &cputherm_state_notifier_list, nb);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> + return ret;
> +}
> +EXPORT_SYMBOL(cputherm_register_notifier);
> +
> +/**
> + * cputherm_unregister_notifier - Un-register a notifier.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. values possible are
> + * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
> + *
> + * This exported function un-registers a driver with cpu cooling layer.
> + */
> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
> +{
> + int ret = 0;
> +
> + switch (list) {
> + case CPUFREQ_COOLING_START:
> + case CPUFREQ_COOLING_STOP:
> + ret = blocking_notifier_chain_unregister(
> + &cputherm_state_notifier_list, nb);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> + return ret;
> +}
> +EXPORT_SYMBOL(cputherm_unregister_notifier);
> +
> +/* Below code defines functions to be used for cpufreq as cooling device */
> +
> +/**
> + * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
> + * @cpu: cpu for which check is needed.
> + */
> +static int is_cpufreq_valid(int cpu)
> +{
> + struct cpufreq_policy policy;
> + return !cpufreq_get_policy(&policy, cpu);
> +}
> +
> +/**
> + * get_cpu_frequency - get the absolute value of frequency from level.
> + * @cpu: cpu for which frequency is fetched.
> + * @level: level of frequency of the CPU
> + * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
> + */
> +static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
> +{
> + int ret = 0, i = 0;
> + unsigned long level_index;
> + bool descend = false;
> + struct cpufreq_frequency_table *table =
> + cpufreq_frequency_get_table(cpu);
> + if (!table)
> + return ret;
> +
> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> +
> + /*check if table in ascending or descending order*/
> + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
> + (table[i + 1].frequency < table[i].frequency)
> + && !descend) {
> + descend = true;
> + }
> +
> + /*return if level matched and table in descending order*/
> + if (descend && i == level)
> + return table[i].frequency;
> + i++;
> + }
> + i--;
> +
> + if (level > i || descend)
> + return ret;
> + level_index = i - level;
> +
> + /*Scan the table in reverse order and match the level*/
> + while (i >= 0) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> + /*return if level matched*/
> + if (i == level_index)
> + return table[i].frequency;
> + i--;
> + }
> + return ret;
> +}
> +
> +/**
> + * cpufreq_apply_cooling - function to apply frequency clipping.
> + * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
> + * clipping data.
> + * @cooling_state: value of the cooling state.
> + */
> +static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
> + unsigned long cooling_state)
> +{
> + unsigned int event, cpuid, clip_freq;
> + struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
> + unsigned int cpu = cpumask_any(maskPtr);
> +
> +
> + /* Check if the old cooling action is same as new cooling action */
> + if (cpufreq_device->cpufreq_state == cooling_state)
> + return 0;
> +
> + clip_freq = get_cpu_frequency(cpu, cooling_state);
> + if (!clip_freq)
> + return -EINVAL;
> +
> + cpufreq_device->cpufreq_state = cooling_state;
> + cpufreq_device->cpufreq_val = clip_freq;
> + notify_device = cpufreq_device;
> +
> + if (cooling_state != 0)
> + event = CPUFREQ_COOLING_START;
> + else
> + event = CPUFREQ_COOLING_STOP;
> +
> + blocking_notifier_call_chain(&cputherm_state_notifier_list,
> + event, &clip_freq);
> +
> + for_each_cpu(cpuid, maskPtr) {
> + if (is_cpufreq_valid(cpuid))
> + cpufreq_update_policy(cpuid);
> + }
> +
> + notify_device = NOTIFY_INVALID;
> +
> + return 0;
> +}
> +
> +/**
> + * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
> + * @nb: struct notifier_block * with callback info.
> + * @event: value showing cpufreq event for which this function invoked.
> + * @data: callback-specific data
> + */
> +static int cpufreq_thermal_notifier(struct notifier_block *nb,
> + unsigned long event, void *data)
> +{
> + struct cpufreq_policy *policy = data;
> + unsigned long max_freq = 0;
> +
> + if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
> + return 0;
> +
> + if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
> + max_freq = notify_device->cpufreq_val;
> +
> + /* Never exceed user_policy.max*/
> + if (max_freq > policy->user_policy.max)
> + max_freq = policy->user_policy.max;
> +
> + if (policy->max != max_freq)
> + cpufreq_verify_within_limits(policy, 0, max_freq);
> +
> + return 0;
> +}
> +
> +/*
> + * cpufreq cooling device callback functions are defined below
> + */
> +
> +/**
> + * cpufreq_get_max_state - callback function to get the max cooling state.
> + * @cdev: thermal cooling device pointer.
> + * @state: fill this variable with the max cooling state.
> + */
> +static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
> + unsigned long *state)
> +{
> + int ret = -EINVAL, i = 0;
> + struct cpufreq_cooling_device *cpufreq_device;
> + struct cpumask *maskPtr;
> + unsigned int cpu;
> + struct cpufreq_frequency_table *table;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
> + break;
> + }
> + if (cpufreq_device == NULL)
> + goto return_get_max_state;
> +
> + maskPtr = &cpufreq_device->allowed_cpus;
> + cpu = cpumask_any(maskPtr);
> + table = cpufreq_frequency_get_table(cpu);
> + if (!table) {
> + *state = 0;
> + ret = 0;
> + goto return_get_max_state;
> + }
> +
> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> + i++;
> + }
> + if (i > 0) {
> + *state = --i;
> + ret = 0;
> + }
> +
> +return_get_max_state:
> + mutex_unlock(&cooling_cpufreq_lock);
> + return ret;
> +}
> +
> +/**
> + * cpufreq_get_cur_state - callback function to get the current cooling state.
> + * @cdev: thermal cooling device pointer.
> + * @state: fill this variable with the current cooling state.
> + */
> +static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
> + unsigned long *state)
> +{
> + int ret = -EINVAL;
> + struct cpufreq_cooling_device *cpufreq_device;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> + if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
> + *state = cpufreq_device->cpufreq_state;
> + ret = 0;
> + break;
> + }
> + }
> + mutex_unlock(&cooling_cpufreq_lock);
> +
as cpufreq may be changed in other places, e.g. via sysfs I/F, we should
use the current cpu frequency to get the REAL cooling state, rather than
using a cached value.
thanks,
rui
On 17 August 2012 12:54, Zhang Rui <[email protected]> wrote:
> On 四, 2012-08-16 at 17:11 +0530, Amit Daniel Kachhap wrote:
>> This patchset introduces a new generic cooling device based on cpufreq
>> that can be used on non-ACPI platforms. As a proof of concept, we have
>> drivers for the following platforms using this mechanism now:
>>
>> * Samsung Exynos (Exynos4 and Exynos5) in the current patchset.
>> * Freescale i.MX (git://git.linaro.org/people/amitdanielk/linux.git imx6q_thermal)
>>
>> There is a small change in cpufreq cooling registration APIs, so a minor
>> change is needed for Freescale platforms.
>>
>> Brief Description:
>>
>> 1) The generic cooling devices code is placed inside driver/thermal/*
>> as placing inside acpi folder will need un-necessary enabling of acpi
>> code. This code is architecture independent.
>>
>> 2) This patchset adds generic cpu cooling low level implementation
>> through frequency clipping. In future, other cpu related cooling
>> devices may be added here. An ACPI version of this already exists
>> (drivers/acpi/processor_thermal.c) .But this will be useful for
>> platforms like ARM using the generic thermal interface along with the
>> generic cpu cooling devices. The cooling device registration API's
>> return cooling device pointers which can be easily binded with the
>> thermal zone trip points. The important APIs exposed are,
>>
>> a) struct thermal_cooling_device *cpufreq_cooling_register(
>> struct cpumask *clip_cpus)
>> b) void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
>>
>> 3) Samsung exynos platform thermal implementation is done using the
>> generic cpu cooling APIs and the new trip type. The temperature sensor
>> driver present in the hwmon folder(registered as hwmon driver) is moved
>> to thermal folder and registered as a thermal driver.
>>
>> A simple data/control flow diagrams is shown below,
>>
>> Core Linux thermal <-----> Exynos thermal interface <----- Temperature Sensor
>> | |
>> \|/ |
>> Cpufreq cooling device <---------------
>>
>> TODO:
>> *Will send the DT enablement patches later after the driver is merged.
>>
>> This patch:
>>
>> Add support for generic cpu thermal cooling low level implementations
>> using frequency scaling up/down based on the registration parameters.
>> Different cpu related cooling devices can be registered by the user and
>> the binding of these cooling devices to the corresponding trip points can
>> be easily done as the registration APIs return the cooling device pointer.
>> The user of these APIs are responsible for passing clipping frequency .
>> The drivers can also register to recieve notification about any cooling
>> action called.
>>
>> [[email protected]: fix comment layout]
>> Signed-off-by: Amit Daniel Kachhap <[email protected]>
>> Cc: Guenter Roeck <[email protected]>
>> Cc: SangWook Ju <[email protected]>
>> Cc: Durgadoss <[email protected]>
>> Cc: Len Brown <[email protected]>
>> Cc: Jean Delvare <[email protected]>
>> Cc: Kyungmin Park <[email protected]>
>> Cc: Kukjin Kim <[email protected]>
>> Signed-off-by: Andrew Morton <[email protected]>
>> Signed-off-by: Amit Daniel Kachhap <[email protected]>
>> ---
>> Documentation/thermal/cpu-cooling-api.txt | 52 +++
>> drivers/thermal/Kconfig | 11 +
>> drivers/thermal/Makefile | 1 +
>> drivers/thermal/cpu_cooling.c | 512 +++++++++++++++++++++++++++++
>> include/linux/cpu_cooling.h | 79 +++++
>> 5 files changed, 655 insertions(+), 0 deletions(-)
>> create mode 100644 Documentation/thermal/cpu-cooling-api.txt
>> create mode 100644 drivers/thermal/cpu_cooling.c
>> create mode 100644 include/linux/cpu_cooling.h
>>
>> diff --git a/Documentation/thermal/cpu-cooling-api.txt b/Documentation/thermal/cpu-cooling-api.txt
>> new file mode 100644
>> index 0000000..a1f2a6b
>> --- /dev/null
>> +++ b/Documentation/thermal/cpu-cooling-api.txt
>> @@ -0,0 +1,52 @@
>> +CPU cooling APIs How To
>> +===================================
>> +
>> +Written by Amit Daniel Kachhap <[email protected]>
>> +
>> +Updated: 12 May 2012
>> +
>> +Copyright (c) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
>> +
>> +0. Introduction
>> +
>> +The generic cpu cooling(freq clipping) provides registration/unregistration APIs
>> +to the caller. The binding of the cooling devices to the trip point is left for
>> +the user. The registration APIs returns the cooling device pointer.
>> +
>> +1. cpu cooling APIs
>> +
>> +1.1 cpufreq registration/unregistration APIs
>> +1.1.1 struct thermal_cooling_device *cpufreq_cooling_register(
>> + struct cpumask *clip_cpus)
>> +
>> + This interface function registers the cpufreq cooling device with the name
>> + "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
>> + cooling devices.
>> +
>> + clip_cpus: cpumask of cpus where the frequency constraints will happen.
>> +
>> +1.1.2 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
>> +
>> + This interface function unregisters the "thermal-cpufreq-%x" cooling device.
>> +
>> + cdev: Cooling device pointer which has to be unregistered.
>> +
>> +
>> +1.2 CPU cooling action notifier register/unregister interface
>> +1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
>> + unsigned int list)
>> +
>> + This interface registers a driver with cpu cooling layer. The driver will
>> + be notified when any cpu cooling action is called.
>> +
>> + nb: notifier function to register
>> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
>> +
>> +1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
>> + unsigned int list)
>> +
>> + This interface registers a driver with cpu cooling layer. The driver will
>> + be notified when any cpu cooling action is called.
>> +
>> + nb: notifier function to register
>> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
>
> what are these two APIs used for?
> I did not see they are used in your patch set, do I miss something?
No currently they are not used by my patches. I added them on request
from Eduardo and others
>
>> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
>> index 7dd8c34..996003b 100644
>> --- a/drivers/thermal/Kconfig
>> +++ b/drivers/thermal/Kconfig
>> @@ -19,6 +19,17 @@ config THERMAL_HWMON
>> depends on HWMON=y || HWMON=THERMAL
>> default y
>>
>> +config CPU_THERMAL
>> + bool "generic cpu cooling support"
>> + depends on THERMAL && CPU_FREQ
>> + help
>> + This implements the generic cpu cooling mechanism through frequency
>> + reduction, cpu hotplug and any other ways of reducing temperature. An
>> + ACPI version of this already exists(drivers/acpi/processor_thermal.c).
>> + This will be useful for platforms using the generic thermal interface
>> + and not the ACPI interface.
>> + If you want this support, you should say Y here.
>> +
>> config SPEAR_THERMAL
>> bool "SPEAr thermal sensor driver"
>> depends on THERMAL
>> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
>> index fd9369a..aae59ad 100644
>> --- a/drivers/thermal/Makefile
>> +++ b/drivers/thermal/Makefile
>> @@ -3,5 +3,6 @@
>> #
>>
>> obj-$(CONFIG_THERMAL) += thermal_sys.o
>> +obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
>> obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
>> obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
>> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
>> new file mode 100644
>> index 0000000..c42e557
>> --- /dev/null
>> +++ b/drivers/thermal/cpu_cooling.c
>> @@ -0,0 +1,512 @@
>> +/*
>> + * linux/drivers/thermal/cpu_cooling.c
>> + *
>> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
>> + * Copyright (C) 2012 Amit Daniel <[email protected]>
>> + *
>> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> + * 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; version 2 of the License.
>> + *
>> + * This program is distributed in the hope that it will be useful, but
>> + * WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
>> + * General Public License for more details.
>> + *
>> + * You should have received a copy of the GNU General Public License along
>> + * with this program; if not, write to the Free Software Foundation, Inc.,
>> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
>> + *
>> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> + */
>> +#include <linux/kernel.h>
>> +#include <linux/module.h>
>> +#include <linux/thermal.h>
>> +#include <linux/platform_device.h>
>> +#include <linux/cpufreq.h>
>> +#include <linux/err.h>
>> +#include <linux/slab.h>
>> +#include <linux/cpu.h>
>> +#include <linux/cpu_cooling.h>
>> +
>> +/**
>> + * struct cpufreq_cooling_device
>> + * @id: unique integer value corresponding to each cpufreq_cooling_device
>> + * registered.
>> + * @cool_dev: thermal_cooling_device pointer to keep track of the the
>> + * egistered cooling device.
>> + * @cpufreq_state: integer value representing the current state of cpufreq
>> + * cooling devices.
>> + * @cpufreq_val: integer value representing the absolute value of the clipped
>> + * frequency.
>> + * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
>> + * @node: list_head to link all cpufreq_cooling_device together.
>> + *
>> + * This structure is required for keeping information of each
>> + * cpufreq_cooling_device registered as a list whose head is represented by
>> + * cooling_cpufreq_list. In order to prevent corruption of this list a
>> + * mutex lock cooling_cpufreq_lock is used.
>> + */
>> +struct cpufreq_cooling_device {
>> + int id;
>> + struct thermal_cooling_device *cool_dev;
>> + unsigned int cpufreq_state;
>> + unsigned int cpufreq_val;
>> + struct cpumask allowed_cpus;
>> + struct list_head node;
>> +};
>> +static LIST_HEAD(cooling_cpufreq_list);
>> +static DEFINE_IDR(cpufreq_idr);
>> +
>> +static struct mutex cooling_cpufreq_lock;
>> +
>> +/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
>> +#define NOTIFY_INVALID NULL
>> +struct cpufreq_cooling_device *notify_device;
>> +
>> +/* Head of the blocking notifier chain to inform about frequency clamping */
>> +static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
>> +
>> +/**
>> + * get_idr - function to get a unique id.
>> + * @idr: struct idr * handle used to create a id.
>> + * @id: int * value generated by this function.
>> + */
>> +static int get_idr(struct idr *idr, int *id)
>> +{
>> + int err;
>> +again:
>> + if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
>> + return -ENOMEM;
>> +
>> + mutex_lock(&cooling_cpufreq_lock);
>> + err = idr_get_new(idr, NULL, id);
>> + mutex_unlock(&cooling_cpufreq_lock);
>> +
>> + if (unlikely(err == -EAGAIN))
>> + goto again;
>> + else if (unlikely(err))
>> + return err;
>> +
>> + *id = *id & MAX_ID_MASK;
>> + return 0;
>> +}
>> +
>> +/**
>> + * release_idr - function to free the unique id.
>> + * @idr: struct idr * handle used for creating the id.
>> + * @id: int value representing the unique id.
>> + */
>> +static void release_idr(struct idr *idr, int id)
>> +{
>> + mutex_lock(&cooling_cpufreq_lock);
>> + idr_remove(idr, id);
>> + mutex_unlock(&cooling_cpufreq_lock);
>> +}
>> +
>> +/**
>> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
>> + * @nb: struct notifier_block * with callback info.
>> + * @list: integer value for which notification is needed. possible values are
>> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
>> + *
>> + * This exported function registers a driver with cpu cooling layer. The driver
>> + * will be notified when any cpu cooling action is called.
>> + */
>> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
>> +{
>> + int ret = 0;
>> +
>> + switch (list) {
>> + case CPUFREQ_COOLING_START:
>> + case CPUFREQ_COOLING_STOP:
>> + ret = blocking_notifier_chain_register(
>> + &cputherm_state_notifier_list, nb);
>> + break;
>> + default:
>> + ret = -EINVAL;
>> + }
>> + return ret;
>> +}
>> +EXPORT_SYMBOL(cputherm_register_notifier);
>> +
>> +/**
>> + * cputherm_unregister_notifier - Un-register a notifier.
>> + * @nb: struct notifier_block * with callback info.
>> + * @list: integer value for which notification is needed. values possible are
>> + * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
>> + *
>> + * This exported function un-registers a driver with cpu cooling layer.
>> + */
>> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
>> +{
>> + int ret = 0;
>> +
>> + switch (list) {
>> + case CPUFREQ_COOLING_START:
>> + case CPUFREQ_COOLING_STOP:
>> + ret = blocking_notifier_chain_unregister(
>> + &cputherm_state_notifier_list, nb);
>> + break;
>> + default:
>> + ret = -EINVAL;
>> + }
>> + return ret;
>> +}
>> +EXPORT_SYMBOL(cputherm_unregister_notifier);
>> +
>> +/* Below code defines functions to be used for cpufreq as cooling device */
>> +
>> +/**
>> + * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
>> + * @cpu: cpu for which check is needed.
>> + */
>> +static int is_cpufreq_valid(int cpu)
>> +{
>> + struct cpufreq_policy policy;
>> + return !cpufreq_get_policy(&policy, cpu);
>> +}
>> +
>> +/**
>> + * get_cpu_frequency - get the absolute value of frequency from level.
>> + * @cpu: cpu for which frequency is fetched.
>> + * @level: level of frequency of the CPU
>> + * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
>> + */
>> +static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
>> +{
>> + int ret = 0, i = 0;
>> + unsigned long level_index;
>> + bool descend = false;
>> + struct cpufreq_frequency_table *table =
>> + cpufreq_frequency_get_table(cpu);
>> + if (!table)
>> + return ret;
>> +
>> + while (table[i].frequency != CPUFREQ_TABLE_END) {
>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>> + continue;
>> +
>> + /*check if table in ascending or descending order*/
>> + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
>> + (table[i + 1].frequency < table[i].frequency)
>> + && !descend) {
>> + descend = true;
>> + }
>> +
>> + /*return if level matched and table in descending order*/
>> + if (descend && i == level)
>> + return table[i].frequency;
>> + i++;
>> + }
>> + i--;
>> +
>> + if (level > i || descend)
>> + return ret;
>> + level_index = i - level;
>> +
>> + /*Scan the table in reverse order and match the level*/
>> + while (i >= 0) {
>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>> + continue;
>> + /*return if level matched*/
>> + if (i == level_index)
>> + return table[i].frequency;
>> + i--;
>> + }
>> + return ret;
>> +}
>> +
>> +/**
>> + * cpufreq_apply_cooling - function to apply frequency clipping.
>> + * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
>> + * clipping data.
>> + * @cooling_state: value of the cooling state.
>> + */
>> +static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
>> + unsigned long cooling_state)
>> +{
>> + unsigned int event, cpuid, clip_freq;
>> + struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
>> + unsigned int cpu = cpumask_any(maskPtr);
>> +
>> +
>> + /* Check if the old cooling action is same as new cooling action */
>> + if (cpufreq_device->cpufreq_state == cooling_state)
>> + return 0;
>> +
>> + clip_freq = get_cpu_frequency(cpu, cooling_state);
>> + if (!clip_freq)
>> + return -EINVAL;
>> +
>> + cpufreq_device->cpufreq_state = cooling_state;
>> + cpufreq_device->cpufreq_val = clip_freq;
>> + notify_device = cpufreq_device;
>> +
>> + if (cooling_state != 0)
>> + event = CPUFREQ_COOLING_START;
>> + else
>> + event = CPUFREQ_COOLING_STOP;
>> +
>> + blocking_notifier_call_chain(&cputherm_state_notifier_list,
>> + event, &clip_freq);
>> +
>> + for_each_cpu(cpuid, maskPtr) {
>> + if (is_cpufreq_valid(cpuid))
>> + cpufreq_update_policy(cpuid);
>> + }
>> +
>> + notify_device = NOTIFY_INVALID;
>> +
>> + return 0;
>> +}
>> +
>> +/**
>> + * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
>> + * @nb: struct notifier_block * with callback info.
>> + * @event: value showing cpufreq event for which this function invoked.
>> + * @data: callback-specific data
>> + */
>> +static int cpufreq_thermal_notifier(struct notifier_block *nb,
>> + unsigned long event, void *data)
>> +{
>> + struct cpufreq_policy *policy = data;
>> + unsigned long max_freq = 0;
>> +
>> + if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
>> + return 0;
>> +
>> + if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
>> + max_freq = notify_device->cpufreq_val;
>> +
>> + /* Never exceed user_policy.max*/
>> + if (max_freq > policy->user_policy.max)
>> + max_freq = policy->user_policy.max;
>> +
>> + if (policy->max != max_freq)
>> + cpufreq_verify_within_limits(policy, 0, max_freq);
>> +
>> + return 0;
>> +}
>> +
>> +/*
>> + * cpufreq cooling device callback functions are defined below
>> + */
>> +
>> +/**
>> + * cpufreq_get_max_state - callback function to get the max cooling state.
>> + * @cdev: thermal cooling device pointer.
>> + * @state: fill this variable with the max cooling state.
>> + */
>> +static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
>> + unsigned long *state)
>> +{
>> + int ret = -EINVAL, i = 0;
>> + struct cpufreq_cooling_device *cpufreq_device;
>> + struct cpumask *maskPtr;
>> + unsigned int cpu;
>> + struct cpufreq_frequency_table *table;
>> +
>> + mutex_lock(&cooling_cpufreq_lock);
>> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
>> + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
>> + break;
>> + }
>> + if (cpufreq_device == NULL)
>> + goto return_get_max_state;
>> +
>> + maskPtr = &cpufreq_device->allowed_cpus;
>> + cpu = cpumask_any(maskPtr);
>> + table = cpufreq_frequency_get_table(cpu);
>> + if (!table) {
>> + *state = 0;
>> + ret = 0;
>> + goto return_get_max_state;
>> + }
>> +
>> + while (table[i].frequency != CPUFREQ_TABLE_END) {
>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>> + continue;
>> + i++;
>> + }
>> + if (i > 0) {
>> + *state = --i;
>> + ret = 0;
>> + }
>> +
>> +return_get_max_state:
>> + mutex_unlock(&cooling_cpufreq_lock);
>> + return ret;
>> +}
>> +
>> +/**
>> + * cpufreq_get_cur_state - callback function to get the current cooling state.
>> + * @cdev: thermal cooling device pointer.
>> + * @state: fill this variable with the current cooling state.
>> + */
>> +static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
>> + unsigned long *state)
>> +{
>> + int ret = -EINVAL;
>> + struct cpufreq_cooling_device *cpufreq_device;
>> +
>> + mutex_lock(&cooling_cpufreq_lock);
>> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
>> + if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
>> + *state = cpufreq_device->cpufreq_state;
>> + ret = 0;
>> + break;
>> + }
>> + }
>> + mutex_unlock(&cooling_cpufreq_lock);
>> +
>
> as cpufreq may be changed in other places, e.g. via sysfs I/F, we should
> use the current cpu frequency to get the REAL cooling state, rather than
> using a cached value.
Yes agreed , I will repost with your suggestion.
Thanks,
Amit
>
> thanks,
> rui
>
>
Hello,
On Fri, Aug 17, 2012 at 10:58 AM, Amit Kachhap <[email protected]> wrote:
> On 17 August 2012 12:54, Zhang Rui <[email protected]> wrote:
>> On 四, 2012-08-16 at 17:11 +0530, Amit Daniel Kachhap wrote:
>>> This patchset introduces a new generic cooling device based on cpufreq
>>> that can be used on non-ACPI platforms. As a proof of concept, we have
>>> drivers for the following platforms using this mechanism now:
>>>
>>> * Samsung Exynos (Exynos4 and Exynos5) in the current patchset.
>>> * Freescale i.MX (git://git.linaro.org/people/amitdanielk/linux.git imx6q_thermal)
>>>
>>> There is a small change in cpufreq cooling registration APIs, so a minor
>>> change is needed for Freescale platforms.
>>>
>>> Brief Description:
>>>
>>> 1) The generic cooling devices code is placed inside driver/thermal/*
>>> as placing inside acpi folder will need un-necessary enabling of acpi
>>> code. This code is architecture independent.
>>>
>>> 2) This patchset adds generic cpu cooling low level implementation
>>> through frequency clipping. In future, other cpu related cooling
>>> devices may be added here. An ACPI version of this already exists
>>> (drivers/acpi/processor_thermal.c) .But this will be useful for
>>> platforms like ARM using the generic thermal interface along with the
>>> generic cpu cooling devices. The cooling device registration API's
>>> return cooling device pointers which can be easily binded with the
>>> thermal zone trip points. The important APIs exposed are,
>>>
>>> a) struct thermal_cooling_device *cpufreq_cooling_register(
>>> struct cpumask *clip_cpus)
>>> b) void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
>>>
>>> 3) Samsung exynos platform thermal implementation is done using the
>>> generic cpu cooling APIs and the new trip type. The temperature sensor
>>> driver present in the hwmon folder(registered as hwmon driver) is moved
>>> to thermal folder and registered as a thermal driver.
>>>
>>> A simple data/control flow diagrams is shown below,
>>>
>>> Core Linux thermal <-----> Exynos thermal interface <----- Temperature Sensor
>>> | |
>>> \|/ |
>>> Cpufreq cooling device <---------------
>>>
>>> TODO:
>>> *Will send the DT enablement patches later after the driver is merged.
>>>
>>> This patch:
>>>
>>> Add support for generic cpu thermal cooling low level implementations
>>> using frequency scaling up/down based on the registration parameters.
>>> Different cpu related cooling devices can be registered by the user and
>>> the binding of these cooling devices to the corresponding trip points can
>>> be easily done as the registration APIs return the cooling device pointer.
>>> The user of these APIs are responsible for passing clipping frequency .
>>> The drivers can also register to recieve notification about any cooling
>>> action called.
>>>
>>> [[email protected]: fix comment layout]
>>> Signed-off-by: Amit Daniel Kachhap <[email protected]>
>>> Cc: Guenter Roeck <[email protected]>
>>> Cc: SangWook Ju <[email protected]>
>>> Cc: Durgadoss <[email protected]>
>>> Cc: Len Brown <[email protected]>
>>> Cc: Jean Delvare <[email protected]>
>>> Cc: Kyungmin Park <[email protected]>
>>> Cc: Kukjin Kim <[email protected]>
>>> Signed-off-by: Andrew Morton <[email protected]>
>>> Signed-off-by: Amit Daniel Kachhap <[email protected]>
>>> ---
>>> Documentation/thermal/cpu-cooling-api.txt | 52 +++
>>> drivers/thermal/Kconfig | 11 +
>>> drivers/thermal/Makefile | 1 +
>>> drivers/thermal/cpu_cooling.c | 512 +++++++++++++++++++++++++++++
>>> include/linux/cpu_cooling.h | 79 +++++
>>> 5 files changed, 655 insertions(+), 0 deletions(-)
>>> create mode 100644 Documentation/thermal/cpu-cooling-api.txt
>>> create mode 100644 drivers/thermal/cpu_cooling.c
>>> create mode 100644 include/linux/cpu_cooling.h
>>>
>>> diff --git a/Documentation/thermal/cpu-cooling-api.txt b/Documentation/thermal/cpu-cooling-api.txt
>>> new file mode 100644
>>> index 0000000..a1f2a6b
>>> --- /dev/null
>>> +++ b/Documentation/thermal/cpu-cooling-api.txt
>>> @@ -0,0 +1,52 @@
>>> +CPU cooling APIs How To
>>> +===================================
>>> +
>>> +Written by Amit Daniel Kachhap <[email protected]>
>>> +
>>> +Updated: 12 May 2012
>>> +
>>> +Copyright (c) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
>>> +
>>> +0. Introduction
>>> +
>>> +The generic cpu cooling(freq clipping) provides registration/unregistration APIs
>>> +to the caller. The binding of the cooling devices to the trip point is left for
>>> +the user. The registration APIs returns the cooling device pointer.
>>> +
>>> +1. cpu cooling APIs
>>> +
>>> +1.1 cpufreq registration/unregistration APIs
>>> +1.1.1 struct thermal_cooling_device *cpufreq_cooling_register(
>>> + struct cpumask *clip_cpus)
>>> +
>>> + This interface function registers the cpufreq cooling device with the name
>>> + "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
>>> + cooling devices.
>>> +
>>> + clip_cpus: cpumask of cpus where the frequency constraints will happen.
>>> +
>>> +1.1.2 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
>>> +
>>> + This interface function unregisters the "thermal-cpufreq-%x" cooling device.
>>> +
>>> + cdev: Cooling device pointer which has to be unregistered.
>>> +
>>> +
>>> +1.2 CPU cooling action notifier register/unregister interface
>>> +1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
>>> + unsigned int list)
>>> +
>>> + This interface registers a driver with cpu cooling layer. The driver will
>>> + be notified when any cpu cooling action is called.
>>> +
>>> + nb: notifier function to register
>>> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
>>> +
>>> +1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
>>> + unsigned int list)
>>> +
>>> + This interface registers a driver with cpu cooling layer. The driver will
>>> + be notified when any cpu cooling action is called.
>>> +
>>> + nb: notifier function to register
>>> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
>>
>> what are these two APIs used for?
>> I did not see they are used in your patch set, do I miss something?
> No currently they are not used by my patches. I added them on request
> from Eduardo and others
Yeah, this was a suggestion as we didn't really know how the FW part
would evolve by that time.
The reasoning is to allow any interested user (in kernel) to be
notified when max frequency changes. Actually, the use case behind
this is to allow such users to perform some handshaking or stop their
activities or even change some paramenters, in case the max frequency
would change. Ideally it would be possible to nack the cooling
transition. But that is yet a wider discussion. So far we don't have
users for this.
>>
>>> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
>>> index 7dd8c34..996003b 100644
>>> --- a/drivers/thermal/Kconfig
>>> +++ b/drivers/thermal/Kconfig
>>> @@ -19,6 +19,17 @@ config THERMAL_HWMON
>>> depends on HWMON=y || HWMON=THERMAL
>>> default y
>>>
>>> +config CPU_THERMAL
>>> + bool "generic cpu cooling support"
>>> + depends on THERMAL && CPU_FREQ
>>> + help
>>> + This implements the generic cpu cooling mechanism through frequency
>>> + reduction, cpu hotplug and any other ways of reducing temperature. An
>>> + ACPI version of this already exists(drivers/acpi/processor_thermal.c).
>>> + This will be useful for platforms using the generic thermal interface
>>> + and not the ACPI interface.
>>> + If you want this support, you should say Y here.
>>> +
>>> config SPEAR_THERMAL
>>> bool "SPEAr thermal sensor driver"
>>> depends on THERMAL
>>> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
>>> index fd9369a..aae59ad 100644
>>> --- a/drivers/thermal/Makefile
>>> +++ b/drivers/thermal/Makefile
>>> @@ -3,5 +3,6 @@
>>> #
>>>
>>> obj-$(CONFIG_THERMAL) += thermal_sys.o
>>> +obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
>>> obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
>>> obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
>>> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
>>> new file mode 100644
>>> index 0000000..c42e557
>>> --- /dev/null
>>> +++ b/drivers/thermal/cpu_cooling.c
>>> @@ -0,0 +1,512 @@
>>> +/*
>>> + * linux/drivers/thermal/cpu_cooling.c
>>> + *
>>> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
>>> + * Copyright (C) 2012 Amit Daniel <[email protected]>
>>> + *
>>> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> + * 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; version 2 of the License.
>>> + *
>>> + * This program is distributed in the hope that it will be useful, but
>>> + * WITHOUT ANY WARRANTY; without even the implied warranty of
>>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
>>> + * General Public License for more details.
>>> + *
>>> + * You should have received a copy of the GNU General Public License along
>>> + * with this program; if not, write to the Free Software Foundation, Inc.,
>>> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
>>> + *
>>> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> + */
>>> +#include <linux/kernel.h>
>>> +#include <linux/module.h>
>>> +#include <linux/thermal.h>
>>> +#include <linux/platform_device.h>
>>> +#include <linux/cpufreq.h>
>>> +#include <linux/err.h>
>>> +#include <linux/slab.h>
>>> +#include <linux/cpu.h>
>>> +#include <linux/cpu_cooling.h>
>>> +
>>> +/**
>>> + * struct cpufreq_cooling_device
>>> + * @id: unique integer value corresponding to each cpufreq_cooling_device
>>> + * registered.
>>> + * @cool_dev: thermal_cooling_device pointer to keep track of the the
>>> + * egistered cooling device.
>>> + * @cpufreq_state: integer value representing the current state of cpufreq
>>> + * cooling devices.
>>> + * @cpufreq_val: integer value representing the absolute value of the clipped
>>> + * frequency.
>>> + * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
>>> + * @node: list_head to link all cpufreq_cooling_device together.
>>> + *
>>> + * This structure is required for keeping information of each
>>> + * cpufreq_cooling_device registered as a list whose head is represented by
>>> + * cooling_cpufreq_list. In order to prevent corruption of this list a
>>> + * mutex lock cooling_cpufreq_lock is used.
>>> + */
>>> +struct cpufreq_cooling_device {
>>> + int id;
>>> + struct thermal_cooling_device *cool_dev;
>>> + unsigned int cpufreq_state;
>>> + unsigned int cpufreq_val;
>>> + struct cpumask allowed_cpus;
>>> + struct list_head node;
>>> +};
>>> +static LIST_HEAD(cooling_cpufreq_list);
>>> +static DEFINE_IDR(cpufreq_idr);
>>> +
>>> +static struct mutex cooling_cpufreq_lock;
>>> +
>>> +/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
>>> +#define NOTIFY_INVALID NULL
>>> +struct cpufreq_cooling_device *notify_device;
>>> +
>>> +/* Head of the blocking notifier chain to inform about frequency clamping */
>>> +static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
>>> +
>>> +/**
>>> + * get_idr - function to get a unique id.
>>> + * @idr: struct idr * handle used to create a id.
>>> + * @id: int * value generated by this function.
>>> + */
>>> +static int get_idr(struct idr *idr, int *id)
>>> +{
>>> + int err;
>>> +again:
>>> + if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
>>> + return -ENOMEM;
>>> +
>>> + mutex_lock(&cooling_cpufreq_lock);
>>> + err = idr_get_new(idr, NULL, id);
>>> + mutex_unlock(&cooling_cpufreq_lock);
>>> +
>>> + if (unlikely(err == -EAGAIN))
>>> + goto again;
>>> + else if (unlikely(err))
>>> + return err;
>>> +
>>> + *id = *id & MAX_ID_MASK;
>>> + return 0;
>>> +}
>>> +
>>> +/**
>>> + * release_idr - function to free the unique id.
>>> + * @idr: struct idr * handle used for creating the id.
>>> + * @id: int value representing the unique id.
>>> + */
>>> +static void release_idr(struct idr *idr, int id)
>>> +{
>>> + mutex_lock(&cooling_cpufreq_lock);
>>> + idr_remove(idr, id);
>>> + mutex_unlock(&cooling_cpufreq_lock);
>>> +}
>>> +
>>> +/**
>>> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
>>> + * @nb: struct notifier_block * with callback info.
>>> + * @list: integer value for which notification is needed. possible values are
>>> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
>>> + *
>>> + * This exported function registers a driver with cpu cooling layer. The driver
>>> + * will be notified when any cpu cooling action is called.
>>> + */
>>> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
>>> +{
>>> + int ret = 0;
>>> +
>>> + switch (list) {
>>> + case CPUFREQ_COOLING_START:
>>> + case CPUFREQ_COOLING_STOP:
>>> + ret = blocking_notifier_chain_register(
>>> + &cputherm_state_notifier_list, nb);
>>> + break;
>>> + default:
>>> + ret = -EINVAL;
>>> + }
>>> + return ret;
>>> +}
>>> +EXPORT_SYMBOL(cputherm_register_notifier);
>>> +
>>> +/**
>>> + * cputherm_unregister_notifier - Un-register a notifier.
>>> + * @nb: struct notifier_block * with callback info.
>>> + * @list: integer value for which notification is needed. values possible are
>>> + * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
>>> + *
>>> + * This exported function un-registers a driver with cpu cooling layer.
>>> + */
>>> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
>>> +{
>>> + int ret = 0;
>>> +
>>> + switch (list) {
>>> + case CPUFREQ_COOLING_START:
>>> + case CPUFREQ_COOLING_STOP:
>>> + ret = blocking_notifier_chain_unregister(
>>> + &cputherm_state_notifier_list, nb);
>>> + break;
>>> + default:
>>> + ret = -EINVAL;
>>> + }
>>> + return ret;
>>> +}
>>> +EXPORT_SYMBOL(cputherm_unregister_notifier);
>>> +
>>> +/* Below code defines functions to be used for cpufreq as cooling device */
>>> +
>>> +/**
>>> + * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
>>> + * @cpu: cpu for which check is needed.
>>> + */
>>> +static int is_cpufreq_valid(int cpu)
>>> +{
>>> + struct cpufreq_policy policy;
>>> + return !cpufreq_get_policy(&policy, cpu);
>>> +}
>>> +
>>> +/**
>>> + * get_cpu_frequency - get the absolute value of frequency from level.
>>> + * @cpu: cpu for which frequency is fetched.
>>> + * @level: level of frequency of the CPU
>>> + * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
>>> + */
>>> +static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
>>> +{
>>> + int ret = 0, i = 0;
>>> + unsigned long level_index;
>>> + bool descend = false;
>>> + struct cpufreq_frequency_table *table =
>>> + cpufreq_frequency_get_table(cpu);
>>> + if (!table)
>>> + return ret;
>>> +
>>> + while (table[i].frequency != CPUFREQ_TABLE_END) {
>>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>>> + continue;
>>> +
>>> + /*check if table in ascending or descending order*/
>>> + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
>>> + (table[i + 1].frequency < table[i].frequency)
>>> + && !descend) {
>>> + descend = true;
>>> + }
>>> +
>>> + /*return if level matched and table in descending order*/
>>> + if (descend && i == level)
>>> + return table[i].frequency;
>>> + i++;
>>> + }
>>> + i--;
>>> +
>>> + if (level > i || descend)
>>> + return ret;
>>> + level_index = i - level;
>>> +
>>> + /*Scan the table in reverse order and match the level*/
>>> + while (i >= 0) {
>>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>>> + continue;
>>> + /*return if level matched*/
>>> + if (i == level_index)
>>> + return table[i].frequency;
>>> + i--;
>>> + }
>>> + return ret;
>>> +}
>>> +
>>> +/**
>>> + * cpufreq_apply_cooling - function to apply frequency clipping.
>>> + * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
>>> + * clipping data.
>>> + * @cooling_state: value of the cooling state.
>>> + */
>>> +static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
>>> + unsigned long cooling_state)
>>> +{
>>> + unsigned int event, cpuid, clip_freq;
>>> + struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
>>> + unsigned int cpu = cpumask_any(maskPtr);
>>> +
>>> +
>>> + /* Check if the old cooling action is same as new cooling action */
>>> + if (cpufreq_device->cpufreq_state == cooling_state)
>>> + return 0;
>>> +
>>> + clip_freq = get_cpu_frequency(cpu, cooling_state);
>>> + if (!clip_freq)
>>> + return -EINVAL;
>>> +
>>> + cpufreq_device->cpufreq_state = cooling_state;
>>> + cpufreq_device->cpufreq_val = clip_freq;
>>> + notify_device = cpufreq_device;
>>> +
>>> + if (cooling_state != 0)
>>> + event = CPUFREQ_COOLING_START;
>>> + else
>>> + event = CPUFREQ_COOLING_STOP;
>>> +
>>> + blocking_notifier_call_chain(&cputherm_state_notifier_list,
>>> + event, &clip_freq);
>>> +
>>> + for_each_cpu(cpuid, maskPtr) {
>>> + if (is_cpufreq_valid(cpuid))
>>> + cpufreq_update_policy(cpuid);
>>> + }
>>> +
>>> + notify_device = NOTIFY_INVALID;
>>> +
>>> + return 0;
>>> +}
>>> +
>>> +/**
>>> + * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
>>> + * @nb: struct notifier_block * with callback info.
>>> + * @event: value showing cpufreq event for which this function invoked.
>>> + * @data: callback-specific data
>>> + */
>>> +static int cpufreq_thermal_notifier(struct notifier_block *nb,
>>> + unsigned long event, void *data)
>>> +{
>>> + struct cpufreq_policy *policy = data;
>>> + unsigned long max_freq = 0;
>>> +
>>> + if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
>>> + return 0;
>>> +
>>> + if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
>>> + max_freq = notify_device->cpufreq_val;
>>> +
>>> + /* Never exceed user_policy.max*/
>>> + if (max_freq > policy->user_policy.max)
>>> + max_freq = policy->user_policy.max;
>>> +
>>> + if (policy->max != max_freq)
>>> + cpufreq_verify_within_limits(policy, 0, max_freq);
>>> +
>>> + return 0;
>>> +}
>>> +
>>> +/*
>>> + * cpufreq cooling device callback functions are defined below
>>> + */
>>> +
>>> +/**
>>> + * cpufreq_get_max_state - callback function to get the max cooling state.
>>> + * @cdev: thermal cooling device pointer.
>>> + * @state: fill this variable with the max cooling state.
>>> + */
>>> +static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
>>> + unsigned long *state)
>>> +{
>>> + int ret = -EINVAL, i = 0;
>>> + struct cpufreq_cooling_device *cpufreq_device;
>>> + struct cpumask *maskPtr;
>>> + unsigned int cpu;
>>> + struct cpufreq_frequency_table *table;
>>> +
>>> + mutex_lock(&cooling_cpufreq_lock);
>>> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
>>> + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
>>> + break;
>>> + }
>>> + if (cpufreq_device == NULL)
>>> + goto return_get_max_state;
>>> +
>>> + maskPtr = &cpufreq_device->allowed_cpus;
>>> + cpu = cpumask_any(maskPtr);
>>> + table = cpufreq_frequency_get_table(cpu);
>>> + if (!table) {
>>> + *state = 0;
>>> + ret = 0;
>>> + goto return_get_max_state;
>>> + }
>>> +
>>> + while (table[i].frequency != CPUFREQ_TABLE_END) {
>>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>>> + continue;
>>> + i++;
>>> + }
>>> + if (i > 0) {
>>> + *state = --i;
>>> + ret = 0;
>>> + }
>>> +
>>> +return_get_max_state:
>>> + mutex_unlock(&cooling_cpufreq_lock);
>>> + return ret;
>>> +}
>>> +
>>> +/**
>>> + * cpufreq_get_cur_state - callback function to get the current cooling state.
>>> + * @cdev: thermal cooling device pointer.
>>> + * @state: fill this variable with the current cooling state.
>>> + */
>>> +static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
>>> + unsigned long *state)
>>> +{
>>> + int ret = -EINVAL;
>>> + struct cpufreq_cooling_device *cpufreq_device;
>>> +
>>> + mutex_lock(&cooling_cpufreq_lock);
>>> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
>>> + if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
>>> + *state = cpufreq_device->cpufreq_state;
>>> + ret = 0;
>>> + break;
>>> + }
>>> + }
>>> + mutex_unlock(&cooling_cpufreq_lock);
>>> +
>>
>> as cpufreq may be changed in other places, e.g. via sysfs I/F, we should
>> use the current cpu frequency to get the REAL cooling state, rather than
>> using a cached value.
>
> Yes agreed , I will repost with your suggestion.
>
> Thanks,
> Amit
>>
>> thanks,
>> rui
>>
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-acpi" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Eduardo Valentin
This patchset introduces a new generic cooling device based on cpufreq
that can be used on non-ACPI platforms. As a proof of concept, we have
drivers for the following platforms using this mechanism now:
* Samsung Exynos (Exynos4 and Exynos5) in the current patchset.
* Freescale i.MX (git://git.linaro.org/people/amitdanielk/linux.git imx6q_thermal)
* Omap platforms in drivers/staging/omap-thermal/
There is a small change in cpufreq cooling registration APIs, so a minor
change is needed for Freescale and omap platforms.
Brief Description:
1) The generic cooling devices code is placed inside driver/thermal/*
as placing inside acpi folder will need un-necessary enabling of acpi
code. This code is architecture independent.
2) This patchset adds generic cpu cooling low level implementation
through frequency clipping. In future, other cpu related cooling
devices may be added here. An ACPI version of this already exists
(drivers/acpi/processor_thermal.c) .But this will be useful for
platforms like ARM using the generic thermal interface along with the
generic cpu cooling devices. The cooling device registration API's
return cooling device pointers which can be easily binded with the
thermal zone trip points. The important APIs exposed are,
a) struct thermal_cooling_device *cpufreq_cooling_register(
struct cpumask *clip_cpus)
b) void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
3) Samsung exynos platform thermal implementation is done using the
generic cpu cooling APIs and the new trip type. The temperature sensor
driver present in the hwmon folder(registered as hwmon driver) is moved
to thermal folder and registered as a thermal driver.
A simple data/control flow diagrams is shown below,
Core Linux thermal <-----> Exynos thermal interface <----- Temperature Sensor
| |
\|/ |
Cpufreq cooling device <---------------
About this patch:
Add support for generic cpu thermal cooling low level implementations
using frequency scaling up/down based on the registration parameters.
Different cpu related cooling devices can be registered by the user and
the binding of these cooling devices to the corresponding trip points can
be easily done as the registration APIs return the cooling device pointer.
The user of these APIs are responsible for passing clipping frequency .
The drivers can also register to recieve notification about any cooling
action called.
[[email protected]: fix comment layout]
Signed-off-by: Amit Daniel Kachhap <[email protected]>
Cc: Guenter Roeck <[email protected]>
Cc: SangWook Ju <[email protected]>
Cc: Durgadoss <[email protected]>
Cc: Len Brown <[email protected]>
Cc: Jean Delvare <[email protected]>
Cc: Kyungmin Park <[email protected]>
Cc: Kukjin Kim <[email protected]>
Reviewed-by: Eduardo Valentin <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Amit Daniel Kachhap <[email protected]>
---
Documentation/thermal/cpu-cooling-api.txt | 52 +++
drivers/thermal/Kconfig | 11 +
drivers/thermal/Makefile | 1 +
drivers/thermal/cpu_cooling.c | 586 +++++++++++++++++++++++++++++
include/linux/cpu_cooling.h | 79 ++++
5 files changed, 729 insertions(+), 0 deletions(-)
create mode 100644 Documentation/thermal/cpu-cooling-api.txt
create mode 100644 drivers/thermal/cpu_cooling.c
create mode 100644 include/linux/cpu_cooling.h
diff --git a/Documentation/thermal/cpu-cooling-api.txt b/Documentation/thermal/cpu-cooling-api.txt
new file mode 100644
index 0000000..a1f2a6b
--- /dev/null
+++ b/Documentation/thermal/cpu-cooling-api.txt
@@ -0,0 +1,52 @@
+CPU cooling APIs How To
+===================================
+
+Written by Amit Daniel Kachhap <[email protected]>
+
+Updated: 12 May 2012
+
+Copyright (c) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
+
+0. Introduction
+
+The generic cpu cooling(freq clipping) provides registration/unregistration APIs
+to the caller. The binding of the cooling devices to the trip point is left for
+the user. The registration APIs returns the cooling device pointer.
+
+1. cpu cooling APIs
+
+1.1 cpufreq registration/unregistration APIs
+1.1.1 struct thermal_cooling_device *cpufreq_cooling_register(
+ struct cpumask *clip_cpus)
+
+ This interface function registers the cpufreq cooling device with the name
+ "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
+ cooling devices.
+
+ clip_cpus: cpumask of cpus where the frequency constraints will happen.
+
+1.1.2 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
+
+ This interface function unregisters the "thermal-cpufreq-%x" cooling device.
+
+ cdev: Cooling device pointer which has to be unregistered.
+
+
+1.2 CPU cooling action notifier register/unregister interface
+1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
+ unsigned int list)
+
+ This interface registers a driver with cpu cooling layer. The driver will
+ be notified when any cpu cooling action is called.
+
+ nb: notifier function to register
+ list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
+
+1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
+ unsigned int list)
+
+ This interface registers a driver with cpu cooling layer. The driver will
+ be notified when any cpu cooling action is called.
+
+ nb: notifier function to register
+ list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 7dd8c34..996003b 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -19,6 +19,17 @@ config THERMAL_HWMON
depends on HWMON=y || HWMON=THERMAL
default y
+config CPU_THERMAL
+ bool "generic cpu cooling support"
+ depends on THERMAL && CPU_FREQ
+ help
+ This implements the generic cpu cooling mechanism through frequency
+ reduction, cpu hotplug and any other ways of reducing temperature. An
+ ACPI version of this already exists(drivers/acpi/processor_thermal.c).
+ This will be useful for platforms using the generic thermal interface
+ and not the ACPI interface.
+ If you want this support, you should say Y here.
+
config SPEAR_THERMAL
bool "SPEAr thermal sensor driver"
depends on THERMAL
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index fd9369a..aae59ad 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -3,5 +3,6 @@
#
obj-$(CONFIG_THERMAL) += thermal_sys.o
+obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
new file mode 100644
index 0000000..66cbd52
--- /dev/null
+++ b/drivers/thermal/cpu_cooling.c
@@ -0,0 +1,586 @@
+/*
+ * linux/drivers/thermal/cpu_cooling.c
+ *
+ * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
+ * Copyright (C) 2012 Amit Daniel <[email protected]>
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * 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; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/thermal.h>
+#include <linux/platform_device.h>
+#include <linux/cpufreq.h>
+#include <linux/err.h>
+#include <linux/slab.h>
+#include <linux/cpu.h>
+#include <linux/cpu_cooling.h>
+
+/**
+ * struct cpufreq_cooling_device
+ * @id: unique integer value corresponding to each cpufreq_cooling_device
+ * registered.
+ * @cool_dev: thermal_cooling_device pointer to keep track of the the
+ * egistered cooling device.
+ * @cpufreq_state: integer value representing the current state of cpufreq
+ * cooling devices.
+ * @cpufreq_val: integer value representing the absolute value of the clipped
+ * frequency.
+ * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
+ * @node: list_head to link all cpufreq_cooling_device together.
+ *
+ * This structure is required for keeping information of each
+ * cpufreq_cooling_device registered as a list whose head is represented by
+ * cooling_cpufreq_list. In order to prevent corruption of this list a
+ * mutex lock cooling_cpufreq_lock is used.
+ */
+struct cpufreq_cooling_device {
+ int id;
+ struct thermal_cooling_device *cool_dev;
+ unsigned int cpufreq_state;
+ unsigned int cpufreq_val;
+ struct cpumask allowed_cpus;
+ struct list_head node;
+};
+static LIST_HEAD(cooling_cpufreq_list);
+static DEFINE_IDR(cpufreq_idr);
+
+static struct mutex cooling_cpufreq_lock;
+
+/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
+#define NOTIFY_INVALID NULL
+struct cpufreq_cooling_device *notify_device;
+
+/* Head of the blocking notifier chain to inform about frequency clamping */
+static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
+
+/**
+ * get_idr - function to get a unique id.
+ * @idr: struct idr * handle used to create a id.
+ * @id: int * value generated by this function.
+ */
+static int get_idr(struct idr *idr, int *id)
+{
+ int err;
+again:
+ if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
+ return -ENOMEM;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ err = idr_get_new(idr, NULL, id);
+ mutex_unlock(&cooling_cpufreq_lock);
+
+ if (unlikely(err == -EAGAIN))
+ goto again;
+ else if (unlikely(err))
+ return err;
+
+ *id = *id & MAX_ID_MASK;
+ return 0;
+}
+
+/**
+ * release_idr - function to free the unique id.
+ * @idr: struct idr * handle used for creating the id.
+ * @id: int value representing the unique id.
+ */
+static void release_idr(struct idr *idr, int id)
+{
+ mutex_lock(&cooling_cpufreq_lock);
+ idr_remove(idr, id);
+ mutex_unlock(&cooling_cpufreq_lock);
+}
+
+/**
+ * cputherm_register_notifier - Register a notifier with cpu cooling interface.
+ * @nb: struct notifier_block * with callback info.
+ * @list: integer value for which notification is needed. possible values are
+ * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
+ *
+ * This exported function registers a driver with cpu cooling layer. The driver
+ * will be notified when any cpu cooling action is called.
+ */
+int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
+{
+ int ret = 0;
+
+ switch (list) {
+ case CPUFREQ_COOLING_START:
+ case CPUFREQ_COOLING_STOP:
+ ret = blocking_notifier_chain_register(
+ &cputherm_state_notifier_list, nb);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ return ret;
+}
+EXPORT_SYMBOL(cputherm_register_notifier);
+
+/**
+ * cputherm_unregister_notifier - Un-register a notifier.
+ * @nb: struct notifier_block * with callback info.
+ * @list: integer value for which notification is needed. values possible are
+ * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
+ *
+ * This exported function un-registers a driver with cpu cooling layer.
+ */
+int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
+{
+ int ret = 0;
+
+ switch (list) {
+ case CPUFREQ_COOLING_START:
+ case CPUFREQ_COOLING_STOP:
+ ret = blocking_notifier_chain_unregister(
+ &cputherm_state_notifier_list, nb);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ return ret;
+}
+EXPORT_SYMBOL(cputherm_unregister_notifier);
+
+/* Below code defines functions to be used for cpufreq as cooling device */
+
+/**
+ * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
+ * @cpu: cpu for which check is needed.
+ */
+static int is_cpufreq_valid(int cpu)
+{
+ struct cpufreq_policy policy;
+ return !cpufreq_get_policy(&policy, cpu);
+}
+
+/**
+ * get_cpu_frequency - get the absolute value of frequency from level.
+ * @cpu: cpu for which frequency is fetched.
+ * @level: level of frequency of the CPU
+ * e.g level=0 --> 1st MAX FREQ, LEVEL=1 ---> 2nd MAX FREQ, .... etc
+ * @return: frequency of the cpu or 0 for error.
+ */
+static unsigned int get_cpu_frequency(unsigned int cpu, unsigned int level)
+{
+ int ret = 0, i = 0;
+ unsigned int level_index;
+ bool descend = false;
+ struct cpufreq_frequency_table *table =
+ cpufreq_frequency_get_table(cpu);
+ if (!table)
+ return ret;
+
+ while (table[i].frequency != CPUFREQ_TABLE_END) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+
+ /*check if table in ascending or descending order*/
+ if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
+ (table[i + 1].frequency < table[i].frequency)
+ && !descend) {
+ descend = true;
+ }
+
+ /*return if level matched and table in descending order*/
+ if (descend && i == level)
+ return table[i].frequency;
+ i++;
+ }
+ i--;
+
+ if (level > i || descend)
+ return ret;
+ level_index = i - level;
+
+ /*Scan the table in reverse order and match the level*/
+ while (i >= 0) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+ /*return if level matched*/
+ if (i == level_index)
+ return table[i].frequency;
+ i--;
+ }
+ return ret;
+}
+
+/**
+ * get_frequency_level - get the level from the frequency.
+ * @cpu: cpu for which frequency is fetched.
+ * @freq: frequency for which level is requested.
+ *
+ * @return: level corresponding to the frequency.
+ * e.g level=0 --> 1st MAX FREQ, LEVEL=1 ---> 2nd MAX FREQ, .... etc
+ * or negative value for any error.
+ */
+static int get_frequency_level(unsigned int cpu, unsigned int freq)
+{
+ int ret = -EINVAL, i = 0;
+ unsigned int max_index;
+ bool descend = false;
+ struct cpufreq_frequency_table *table =
+ cpufreq_frequency_get_table(cpu);
+ if (!table)
+ return ret;
+
+ while (table[i].frequency != CPUFREQ_TABLE_END) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+
+ /*check if table in ascending or descending order*/
+ if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
+ (table[i + 1].frequency < table[i].frequency)
+ && !descend) {
+ descend = true;
+ }
+
+ /*return if level matched and table in descending order*/
+ if (descend && table[i].frequency == freq)
+ return i;
+ i++;
+ }
+ i--;
+
+ if (descend)
+ return ret;
+
+ max_index = i;
+
+ /*Scan the table in reverse order and match the freq*/
+ while (i >= 0) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+ /*return if level matched*/
+ if (table[i].frequency == freq)
+ return max_index - i;
+ i--;
+ }
+ return ret;
+}
+
+/**
+ * cpufreq_apply_cooling - function to apply frequency clipping.
+ * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
+ * clipping data.
+ * @cooling_state: value of the cooling state.
+ */
+static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
+ unsigned long cooling_state)
+{
+ unsigned int event, cpuid, clip_freq;
+ struct cpumask *mask = &cpufreq_device->allowed_cpus;
+ unsigned int cpu = cpumask_any(mask);
+
+
+ /* Check if the old cooling action is same as new cooling action */
+ if (cpufreq_device->cpufreq_state == cooling_state)
+ return 0;
+
+ clip_freq = get_cpu_frequency(cpu, cooling_state);
+ if (!clip_freq)
+ return -EINVAL;
+
+ cpufreq_device->cpufreq_state = cooling_state;
+ cpufreq_device->cpufreq_val = clip_freq;
+ notify_device = cpufreq_device;
+
+ if (cooling_state != 0)
+ event = CPUFREQ_COOLING_START;
+ else
+ event = CPUFREQ_COOLING_STOP;
+
+ blocking_notifier_call_chain(&cputherm_state_notifier_list,
+ event, &clip_freq);
+
+ for_each_cpu(cpuid, mask) {
+ if (is_cpufreq_valid(cpuid))
+ cpufreq_update_policy(cpuid);
+ }
+
+ notify_device = NOTIFY_INVALID;
+
+ return 0;
+}
+
+/**
+ * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
+ * @nb: struct notifier_block * with callback info.
+ * @event: value showing cpufreq event for which this function invoked.
+ * @data: callback-specific data
+ */
+static int cpufreq_thermal_notifier(struct notifier_block *nb,
+ unsigned long event, void *data)
+{
+ struct cpufreq_policy *policy = data;
+ unsigned long max_freq = 0;
+
+ if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
+ return 0;
+
+ if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
+ max_freq = notify_device->cpufreq_val;
+
+ /* Never exceed user_policy.max*/
+ if (max_freq > policy->user_policy.max)
+ max_freq = policy->user_policy.max;
+
+ if (policy->max > max_freq)
+ cpufreq_verify_within_limits(policy, 0, max_freq);
+
+ return 0;
+}
+
+/*
+ * cpufreq cooling device callback functions are defined below
+ */
+
+/**
+ * cpufreq_get_max_state - callback function to get the max cooling state.
+ * @cdev: thermal cooling device pointer.
+ * @state: fill this variable with the max cooling state.
+ */
+static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
+ unsigned long *state)
+{
+ int ret = -EINVAL, i = 0;
+ struct cpufreq_cooling_device *cpufreq_device;
+ struct cpumask *mask;
+ unsigned int cpu;
+ struct cpufreq_frequency_table *table;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
+ if (cpufreq_device && cpufreq_device->cool_dev == cdev)
+ break;
+ }
+ if (cpufreq_device == NULL)
+ goto return_get_max_state;
+
+ mask = &cpufreq_device->allowed_cpus;
+ cpu = cpumask_any(mask);
+ table = cpufreq_frequency_get_table(cpu);
+ if (!table) {
+ *state = 0;
+ ret = 0;
+ goto return_get_max_state;
+ }
+
+ while (table[i].frequency != CPUFREQ_TABLE_END) {
+ if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
+ continue;
+ i++;
+ }
+ if (i > 0) {
+ *state = --i;
+ ret = 0;
+ }
+
+return_get_max_state:
+ mutex_unlock(&cooling_cpufreq_lock);
+ return ret;
+}
+
+/**
+ * cpufreq_get_cur_state - callback function to get the current cooling state.
+ * @cdev: thermal cooling device pointer.
+ * @state: fill this variable with the current cooling state.
+ */
+static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
+ unsigned long *state)
+{
+ int ret = -EINVAL;
+ struct cpufreq_cooling_device *cpufreq_device;
+ struct cpumask *mask;
+ unsigned int cpu;
+ struct cpufreq_policy policy;
+
+ *state = 0;
+ mutex_lock(&cooling_cpufreq_lock);
+ list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
+ if (cpufreq_device && cpufreq_device->cool_dev == cdev)
+ break;
+ }
+ if (cpufreq_device == NULL)
+ goto err_get_cur_state;
+
+ mask = &cpufreq_device->allowed_cpus;
+ cpu = cpumask_any(mask);
+
+ ret = cpufreq_get_policy(&policy, cpu);
+ if (ret)
+ goto uninit_get_cur_state;
+
+ ret = get_frequency_level(cpu, policy.max);
+ if (ret < 0)
+ goto uninit_get_cur_state;
+
+ *state = ret;
+
+uninit_get_cur_state:
+ ret = 0;
+err_get_cur_state:
+ mutex_unlock(&cooling_cpufreq_lock);
+ return ret;
+}
+
+/**
+ * cpufreq_set_cur_state - callback function to set the current cooling state.
+ * @cdev: thermal cooling device pointer.
+ * @state: set this variable to the current cooling state.
+ */
+static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
+ unsigned long state)
+{
+ int ret = -EINVAL;
+ struct cpufreq_cooling_device *cpufreq_device;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
+ if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
+ ret = 0;
+ break;
+ }
+ }
+ if (!ret)
+ ret = cpufreq_apply_cooling(cpufreq_device, state);
+
+ mutex_unlock(&cooling_cpufreq_lock);
+
+ return ret;
+}
+
+/* Bind cpufreq callbacks to thermal cooling device ops */
+static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
+ .get_max_state = cpufreq_get_max_state,
+ .get_cur_state = cpufreq_get_cur_state,
+ .set_cur_state = cpufreq_set_cur_state,
+};
+
+/* Notifier for cpufreq policy change */
+static struct notifier_block thermal_cpufreq_notifier_block = {
+ .notifier_call = cpufreq_thermal_notifier,
+};
+
+/**
+ * cpufreq_cooling_register - function to create cpufreq cooling device.
+ * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
+ */
+struct thermal_cooling_device *cpufreq_cooling_register(
+ struct cpumask *clip_cpus)
+{
+ struct thermal_cooling_device *cool_dev;
+ struct cpufreq_cooling_device *cpufreq_dev = NULL;
+ unsigned int cpufreq_dev_count = 0, min = 0, max = 0;
+ char dev_name[THERMAL_NAME_LENGTH];
+ int ret = 0, id = 0, i;
+ struct cpufreq_policy policy;
+
+ list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node)
+ cpufreq_dev_count++;
+
+ /*Verify that all the clip cpus have same freq_min, freq_max limit*/
+ for_each_cpu(i, clip_cpus) {
+ /*continue if cpufreq policy not found and not return error*/
+ if (!cpufreq_get_policy(&policy, i))
+ continue;
+ if (min == 0 && max == 0) {
+ min = policy.cpuinfo.min_freq;
+ max = policy.cpuinfo.max_freq;
+ } else {
+ if (min != policy.cpuinfo.min_freq ||
+ max != policy.cpuinfo.max_freq)
+ return ERR_PTR(-EINVAL);
+ }
+ }
+ cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
+ GFP_KERNEL);
+ if (!cpufreq_dev)
+ return ERR_PTR(-ENOMEM);
+
+ cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
+
+ if (cpufreq_dev_count == 0)
+ mutex_init(&cooling_cpufreq_lock);
+
+ ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
+ if (ret) {
+ kfree(cpufreq_dev);
+ return ERR_PTR(-EINVAL);
+ }
+
+ sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
+
+ cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
+ &cpufreq_cooling_ops);
+ if (!cool_dev) {
+ release_idr(&cpufreq_idr, cpufreq_dev->id);
+ kfree(cpufreq_dev);
+ return ERR_PTR(-EINVAL);
+ }
+ cpufreq_dev->id = id;
+ cpufreq_dev->cool_dev = cool_dev;
+ cpufreq_dev->cpufreq_state = 0;
+ mutex_lock(&cooling_cpufreq_lock);
+ list_add_tail(&cpufreq_dev->node, &cooling_cpufreq_list);
+
+ /* Register the notifier for first cpufreq cooling device */
+ if (cpufreq_dev_count == 0)
+ cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
+ CPUFREQ_POLICY_NOTIFIER);
+
+ mutex_unlock(&cooling_cpufreq_lock);
+ return cool_dev;
+}
+EXPORT_SYMBOL(cpufreq_cooling_register);
+
+/**
+ * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
+ * @cdev: thermal cooling device pointer.
+ */
+void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
+{
+ struct cpufreq_cooling_device *cpufreq_dev = NULL;
+ unsigned int cpufreq_dev_count = 0;
+
+ mutex_lock(&cooling_cpufreq_lock);
+ list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node) {
+ if (cpufreq_dev && cpufreq_dev->cool_dev == cdev)
+ break;
+ cpufreq_dev_count++;
+ }
+
+ if (!cpufreq_dev || cpufreq_dev->cool_dev != cdev) {
+ mutex_unlock(&cooling_cpufreq_lock);
+ return;
+ }
+
+ list_del(&cpufreq_dev->node);
+
+ /* Unregister the notifier for the last cpufreq cooling device */
+ if (cpufreq_dev_count == 1) {
+ cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
+ CPUFREQ_POLICY_NOTIFIER);
+ }
+ mutex_unlock(&cooling_cpufreq_lock);
+ thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
+ release_idr(&cpufreq_idr, cpufreq_dev->id);
+ if (cpufreq_dev_count == 1)
+ mutex_destroy(&cooling_cpufreq_lock);
+ kfree(cpufreq_dev);
+}
+EXPORT_SYMBOL(cpufreq_cooling_unregister);
diff --git a/include/linux/cpu_cooling.h b/include/linux/cpu_cooling.h
new file mode 100644
index 0000000..555b917
--- /dev/null
+++ b/include/linux/cpu_cooling.h
@@ -0,0 +1,79 @@
+/*
+ * linux/include/linux/cpu_cooling.h
+ *
+ * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
+ * Copyright (C) 2012 Amit Daniel <[email protected]>
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * 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; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+
+#ifndef __CPU_COOLING_H__
+#define __CPU_COOLING_H__
+
+#include <linux/thermal.h>
+
+#define CPUFREQ_COOLING_START 0
+#define CPUFREQ_COOLING_STOP 1
+
+/**
+ * cputherm_register_notifier - Register a notifier with cpu cooling interface.
+ * @nb: struct notifier_block * with callback info.
+ * @list: integer value for which notification is needed. possible values are
+ * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
+ *
+ * This exported function registers a driver with cpu cooling layer. The driver
+ * will be notified when any cpu cooling action is called.
+ */
+int cputherm_register_notifier(struct notifier_block *nb, unsigned int list);
+
+/**
+ * cputherm_unregister_notifier - Un-register a notifier.
+ * @nb: struct notifier_block * with callback info.
+ * @list: integer value for which notification is needed. values possible are
+ * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
+ *
+ * This exported function un-registers a driver with cpu cooling layer.
+ */
+int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list);
+
+#ifdef CONFIG_CPU_THERMAL
+/**
+ * cpufreq_cooling_register - function to create cpufreq cooling device.
+ * @clip_cpus: cpumask of cpus where the frequency constraints will happen
+ */
+struct thermal_cooling_device *cpufreq_cooling_register(
+ struct cpumask *clip_cpus);
+
+/**
+ * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
+ * @cdev: thermal cooling device pointer.
+ */
+void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev);
+#else /* !CONFIG_CPU_THERMAL */
+static inline struct thermal_cooling_device *cpufreq_cooling_register(
+ struct cpumask *clip_cpus)
+{
+ return NULL;
+}
+static inline void cpufreq_cooling_unregister(
+ struct thermal_cooling_device *cdev)
+{
+ return;
+}
+#endif /* CONFIG_CPU_THERMAL */
+
+#endif /* __CPU_COOLING_H__ */
--
1.7.1
This is repost containing the changes suggested by Zhang Rui. Now the
cpufreq current state will be calculated each time instead of earlier
way of returning the cached value.
Thanks,
Amit Daniel
On 17 August 2012 18:59, Amit Daniel Kachhap <[email protected]> wrote:
> This patchset introduces a new generic cooling device based on cpufreq
> that can be used on non-ACPI platforms. As a proof of concept, we have
> drivers for the following platforms using this mechanism now:
>
> * Samsung Exynos (Exynos4 and Exynos5) in the current patchset.
> * Freescale i.MX (git://git.linaro.org/people/amitdanielk/linux.git imx6q_thermal)
> * Omap platforms in drivers/staging/omap-thermal/
>
> There is a small change in cpufreq cooling registration APIs, so a minor
> change is needed for Freescale and omap platforms.
>
> Brief Description:
>
> 1) The generic cooling devices code is placed inside driver/thermal/*
> as placing inside acpi folder will need un-necessary enabling of acpi
> code. This code is architecture independent.
>
> 2) This patchset adds generic cpu cooling low level implementation
> through frequency clipping. In future, other cpu related cooling
> devices may be added here. An ACPI version of this already exists
> (drivers/acpi/processor_thermal.c) .But this will be useful for
> platforms like ARM using the generic thermal interface along with the
> generic cpu cooling devices. The cooling device registration API's
> return cooling device pointers which can be easily binded with the
> thermal zone trip points. The important APIs exposed are,
>
> a) struct thermal_cooling_device *cpufreq_cooling_register(
> struct cpumask *clip_cpus)
> b) void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
>
> 3) Samsung exynos platform thermal implementation is done using the
> generic cpu cooling APIs and the new trip type. The temperature sensor
> driver present in the hwmon folder(registered as hwmon driver) is moved
> to thermal folder and registered as a thermal driver.
>
> A simple data/control flow diagrams is shown below,
>
> Core Linux thermal <-----> Exynos thermal interface <----- Temperature Sensor
> | |
> \|/ |
> Cpufreq cooling device <---------------
>
> About this patch:
>
> Add support for generic cpu thermal cooling low level implementations
> using frequency scaling up/down based on the registration parameters.
> Different cpu related cooling devices can be registered by the user and
> the binding of these cooling devices to the corresponding trip points can
> be easily done as the registration APIs return the cooling device pointer.
> The user of these APIs are responsible for passing clipping frequency .
> The drivers can also register to recieve notification about any cooling
> action called.
>
> [[email protected]: fix comment layout]
> Signed-off-by: Amit Daniel Kachhap <[email protected]>
> Cc: Guenter Roeck <[email protected]>
> Cc: SangWook Ju <[email protected]>
> Cc: Durgadoss <[email protected]>
> Cc: Len Brown <[email protected]>
> Cc: Jean Delvare <[email protected]>
> Cc: Kyungmin Park <[email protected]>
> Cc: Kukjin Kim <[email protected]>
> Reviewed-by: Eduardo Valentin <[email protected]>
> Signed-off-by: Andrew Morton <[email protected]>
> Signed-off-by: Amit Daniel Kachhap <[email protected]>
> ---
> Documentation/thermal/cpu-cooling-api.txt | 52 +++
> drivers/thermal/Kconfig | 11 +
> drivers/thermal/Makefile | 1 +
> drivers/thermal/cpu_cooling.c | 586 +++++++++++++++++++++++++++++
> include/linux/cpu_cooling.h | 79 ++++
> 5 files changed, 729 insertions(+), 0 deletions(-)
> create mode 100644 Documentation/thermal/cpu-cooling-api.txt
> create mode 100644 drivers/thermal/cpu_cooling.c
> create mode 100644 include/linux/cpu_cooling.h
>
> diff --git a/Documentation/thermal/cpu-cooling-api.txt b/Documentation/thermal/cpu-cooling-api.txt
> new file mode 100644
> index 0000000..a1f2a6b
> --- /dev/null
> +++ b/Documentation/thermal/cpu-cooling-api.txt
> @@ -0,0 +1,52 @@
> +CPU cooling APIs How To
> +===================================
> +
> +Written by Amit Daniel Kachhap <[email protected]>
> +
> +Updated: 12 May 2012
> +
> +Copyright (c) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> +
> +0. Introduction
> +
> +The generic cpu cooling(freq clipping) provides registration/unregistration APIs
> +to the caller. The binding of the cooling devices to the trip point is left for
> +the user. The registration APIs returns the cooling device pointer.
> +
> +1. cpu cooling APIs
> +
> +1.1 cpufreq registration/unregistration APIs
> +1.1.1 struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus)
> +
> + This interface function registers the cpufreq cooling device with the name
> + "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
> + cooling devices.
> +
> + clip_cpus: cpumask of cpus where the frequency constraints will happen.
> +
> +1.1.2 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
> +
> + This interface function unregisters the "thermal-cpufreq-%x" cooling device.
> +
> + cdev: Cooling device pointer which has to be unregistered.
> +
> +
> +1.2 CPU cooling action notifier register/unregister interface
> +1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
> + unsigned int list)
> +
> + This interface registers a driver with cpu cooling layer. The driver will
> + be notified when any cpu cooling action is called.
> +
> + nb: notifier function to register
> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> +
> +1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
> + unsigned int list)
> +
> + This interface registers a driver with cpu cooling layer. The driver will
> + be notified when any cpu cooling action is called.
> +
> + nb: notifier function to register
> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index 7dd8c34..996003b 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -19,6 +19,17 @@ config THERMAL_HWMON
> depends on HWMON=y || HWMON=THERMAL
> default y
>
> +config CPU_THERMAL
> + bool "generic cpu cooling support"
> + depends on THERMAL && CPU_FREQ
> + help
> + This implements the generic cpu cooling mechanism through frequency
> + reduction, cpu hotplug and any other ways of reducing temperature. An
> + ACPI version of this already exists(drivers/acpi/processor_thermal.c).
> + This will be useful for platforms using the generic thermal interface
> + and not the ACPI interface.
> + If you want this support, you should say Y here.
> +
> config SPEAR_THERMAL
> bool "SPEAr thermal sensor driver"
> depends on THERMAL
> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> index fd9369a..aae59ad 100644
> --- a/drivers/thermal/Makefile
> +++ b/drivers/thermal/Makefile
> @@ -3,5 +3,6 @@
> #
>
> obj-$(CONFIG_THERMAL) += thermal_sys.o
> +obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
> obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
> obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
> new file mode 100644
> index 0000000..66cbd52
> --- /dev/null
> +++ b/drivers/thermal/cpu_cooling.c
> @@ -0,0 +1,586 @@
> +/*
> + * linux/drivers/thermal/cpu_cooling.c
> + *
> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> + * Copyright (C) 2012 Amit Daniel <[email protected]>
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + * 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; version 2 of the License.
> + *
> + * This program is distributed in the hope that it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, write to the Free Software Foundation, Inc.,
> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + */
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/thermal.h>
> +#include <linux/platform_device.h>
> +#include <linux/cpufreq.h>
> +#include <linux/err.h>
> +#include <linux/slab.h>
> +#include <linux/cpu.h>
> +#include <linux/cpu_cooling.h>
> +
> +/**
> + * struct cpufreq_cooling_device
> + * @id: unique integer value corresponding to each cpufreq_cooling_device
> + * registered.
> + * @cool_dev: thermal_cooling_device pointer to keep track of the the
> + * egistered cooling device.
> + * @cpufreq_state: integer value representing the current state of cpufreq
> + * cooling devices.
> + * @cpufreq_val: integer value representing the absolute value of the clipped
> + * frequency.
> + * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
> + * @node: list_head to link all cpufreq_cooling_device together.
> + *
> + * This structure is required for keeping information of each
> + * cpufreq_cooling_device registered as a list whose head is represented by
> + * cooling_cpufreq_list. In order to prevent corruption of this list a
> + * mutex lock cooling_cpufreq_lock is used.
> + */
> +struct cpufreq_cooling_device {
> + int id;
> + struct thermal_cooling_device *cool_dev;
> + unsigned int cpufreq_state;
> + unsigned int cpufreq_val;
> + struct cpumask allowed_cpus;
> + struct list_head node;
> +};
> +static LIST_HEAD(cooling_cpufreq_list);
> +static DEFINE_IDR(cpufreq_idr);
> +
> +static struct mutex cooling_cpufreq_lock;
> +
> +/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
> +#define NOTIFY_INVALID NULL
> +struct cpufreq_cooling_device *notify_device;
> +
> +/* Head of the blocking notifier chain to inform about frequency clamping */
> +static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
> +
> +/**
> + * get_idr - function to get a unique id.
> + * @idr: struct idr * handle used to create a id.
> + * @id: int * value generated by this function.
> + */
> +static int get_idr(struct idr *idr, int *id)
> +{
> + int err;
> +again:
> + if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
> + return -ENOMEM;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + err = idr_get_new(idr, NULL, id);
> + mutex_unlock(&cooling_cpufreq_lock);
> +
> + if (unlikely(err == -EAGAIN))
> + goto again;
> + else if (unlikely(err))
> + return err;
> +
> + *id = *id & MAX_ID_MASK;
> + return 0;
> +}
> +
> +/**
> + * release_idr - function to free the unique id.
> + * @idr: struct idr * handle used for creating the id.
> + * @id: int value representing the unique id.
> + */
> +static void release_idr(struct idr *idr, int id)
> +{
> + mutex_lock(&cooling_cpufreq_lock);
> + idr_remove(idr, id);
> + mutex_unlock(&cooling_cpufreq_lock);
> +}
> +
> +/**
> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. possible values are
> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> + *
> + * This exported function registers a driver with cpu cooling layer. The driver
> + * will be notified when any cpu cooling action is called.
> + */
> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
> +{
> + int ret = 0;
> +
> + switch (list) {
> + case CPUFREQ_COOLING_START:
> + case CPUFREQ_COOLING_STOP:
> + ret = blocking_notifier_chain_register(
> + &cputherm_state_notifier_list, nb);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> + return ret;
> +}
> +EXPORT_SYMBOL(cputherm_register_notifier);
> +
> +/**
> + * cputherm_unregister_notifier - Un-register a notifier.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. values possible are
> + * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
> + *
> + * This exported function un-registers a driver with cpu cooling layer.
> + */
> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
> +{
> + int ret = 0;
> +
> + switch (list) {
> + case CPUFREQ_COOLING_START:
> + case CPUFREQ_COOLING_STOP:
> + ret = blocking_notifier_chain_unregister(
> + &cputherm_state_notifier_list, nb);
> + break;
> + default:
> + ret = -EINVAL;
> + }
> + return ret;
> +}
> +EXPORT_SYMBOL(cputherm_unregister_notifier);
> +
> +/* Below code defines functions to be used for cpufreq as cooling device */
> +
> +/**
> + * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
> + * @cpu: cpu for which check is needed.
> + */
> +static int is_cpufreq_valid(int cpu)
> +{
> + struct cpufreq_policy policy;
> + return !cpufreq_get_policy(&policy, cpu);
> +}
> +
> +/**
> + * get_cpu_frequency - get the absolute value of frequency from level.
> + * @cpu: cpu for which frequency is fetched.
> + * @level: level of frequency of the CPU
> + * e.g level=0 --> 1st MAX FREQ, LEVEL=1 ---> 2nd MAX FREQ, .... etc
> + * @return: frequency of the cpu or 0 for error.
> + */
> +static unsigned int get_cpu_frequency(unsigned int cpu, unsigned int level)
> +{
> + int ret = 0, i = 0;
> + unsigned int level_index;
> + bool descend = false;
> + struct cpufreq_frequency_table *table =
> + cpufreq_frequency_get_table(cpu);
> + if (!table)
> + return ret;
> +
> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> +
> + /*check if table in ascending or descending order*/
> + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
> + (table[i + 1].frequency < table[i].frequency)
> + && !descend) {
> + descend = true;
> + }
> +
> + /*return if level matched and table in descending order*/
> + if (descend && i == level)
> + return table[i].frequency;
> + i++;
> + }
> + i--;
> +
> + if (level > i || descend)
> + return ret;
> + level_index = i - level;
> +
> + /*Scan the table in reverse order and match the level*/
> + while (i >= 0) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> + /*return if level matched*/
> + if (i == level_index)
> + return table[i].frequency;
> + i--;
> + }
> + return ret;
> +}
> +
> +/**
> + * get_frequency_level - get the level from the frequency.
> + * @cpu: cpu for which frequency is fetched.
> + * @freq: frequency for which level is requested.
> + *
> + * @return: level corresponding to the frequency.
> + * e.g level=0 --> 1st MAX FREQ, LEVEL=1 ---> 2nd MAX FREQ, .... etc
> + * or negative value for any error.
> + */
> +static int get_frequency_level(unsigned int cpu, unsigned int freq)
> +{
> + int ret = -EINVAL, i = 0;
> + unsigned int max_index;
> + bool descend = false;
> + struct cpufreq_frequency_table *table =
> + cpufreq_frequency_get_table(cpu);
> + if (!table)
> + return ret;
> +
> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> +
> + /*check if table in ascending or descending order*/
> + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
> + (table[i + 1].frequency < table[i].frequency)
> + && !descend) {
> + descend = true;
> + }
> +
> + /*return if level matched and table in descending order*/
> + if (descend && table[i].frequency == freq)
> + return i;
> + i++;
> + }
> + i--;
> +
> + if (descend)
> + return ret;
> +
> + max_index = i;
> +
> + /*Scan the table in reverse order and match the freq*/
> + while (i >= 0) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> + /*return if level matched*/
> + if (table[i].frequency == freq)
> + return max_index - i;
> + i--;
> + }
> + return ret;
> +}
> +
> +/**
> + * cpufreq_apply_cooling - function to apply frequency clipping.
> + * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
> + * clipping data.
> + * @cooling_state: value of the cooling state.
> + */
> +static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
> + unsigned long cooling_state)
> +{
> + unsigned int event, cpuid, clip_freq;
> + struct cpumask *mask = &cpufreq_device->allowed_cpus;
> + unsigned int cpu = cpumask_any(mask);
> +
> +
> + /* Check if the old cooling action is same as new cooling action */
> + if (cpufreq_device->cpufreq_state == cooling_state)
> + return 0;
> +
> + clip_freq = get_cpu_frequency(cpu, cooling_state);
> + if (!clip_freq)
> + return -EINVAL;
> +
> + cpufreq_device->cpufreq_state = cooling_state;
> + cpufreq_device->cpufreq_val = clip_freq;
> + notify_device = cpufreq_device;
> +
> + if (cooling_state != 0)
> + event = CPUFREQ_COOLING_START;
> + else
> + event = CPUFREQ_COOLING_STOP;
> +
> + blocking_notifier_call_chain(&cputherm_state_notifier_list,
> + event, &clip_freq);
> +
> + for_each_cpu(cpuid, mask) {
> + if (is_cpufreq_valid(cpuid))
> + cpufreq_update_policy(cpuid);
> + }
> +
> + notify_device = NOTIFY_INVALID;
> +
> + return 0;
> +}
> +
> +/**
> + * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
> + * @nb: struct notifier_block * with callback info.
> + * @event: value showing cpufreq event for which this function invoked.
> + * @data: callback-specific data
> + */
> +static int cpufreq_thermal_notifier(struct notifier_block *nb,
> + unsigned long event, void *data)
> +{
> + struct cpufreq_policy *policy = data;
> + unsigned long max_freq = 0;
> +
> + if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
> + return 0;
> +
> + if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
> + max_freq = notify_device->cpufreq_val;
> +
> + /* Never exceed user_policy.max*/
> + if (max_freq > policy->user_policy.max)
> + max_freq = policy->user_policy.max;
> +
> + if (policy->max > max_freq)
> + cpufreq_verify_within_limits(policy, 0, max_freq);
> +
> + return 0;
> +}
> +
> +/*
> + * cpufreq cooling device callback functions are defined below
> + */
> +
> +/**
> + * cpufreq_get_max_state - callback function to get the max cooling state.
> + * @cdev: thermal cooling device pointer.
> + * @state: fill this variable with the max cooling state.
> + */
> +static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
> + unsigned long *state)
> +{
> + int ret = -EINVAL, i = 0;
> + struct cpufreq_cooling_device *cpufreq_device;
> + struct cpumask *mask;
> + unsigned int cpu;
> + struct cpufreq_frequency_table *table;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
> + break;
> + }
> + if (cpufreq_device == NULL)
> + goto return_get_max_state;
> +
> + mask = &cpufreq_device->allowed_cpus;
> + cpu = cpumask_any(mask);
> + table = cpufreq_frequency_get_table(cpu);
> + if (!table) {
> + *state = 0;
> + ret = 0;
> + goto return_get_max_state;
> + }
> +
> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> + continue;
> + i++;
> + }
> + if (i > 0) {
> + *state = --i;
> + ret = 0;
> + }
> +
> +return_get_max_state:
> + mutex_unlock(&cooling_cpufreq_lock);
> + return ret;
> +}
> +
> +/**
> + * cpufreq_get_cur_state - callback function to get the current cooling state.
> + * @cdev: thermal cooling device pointer.
> + * @state: fill this variable with the current cooling state.
> + */
> +static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
> + unsigned long *state)
> +{
> + int ret = -EINVAL;
> + struct cpufreq_cooling_device *cpufreq_device;
> + struct cpumask *mask;
> + unsigned int cpu;
> + struct cpufreq_policy policy;
> +
> + *state = 0;
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
> + break;
> + }
> + if (cpufreq_device == NULL)
> + goto err_get_cur_state;
> +
> + mask = &cpufreq_device->allowed_cpus;
> + cpu = cpumask_any(mask);
> +
> + ret = cpufreq_get_policy(&policy, cpu);
> + if (ret)
> + goto uninit_get_cur_state;
> +
> + ret = get_frequency_level(cpu, policy.max);
> + if (ret < 0)
> + goto uninit_get_cur_state;
> +
> + *state = ret;
> +
> +uninit_get_cur_state:
> + ret = 0;
> +err_get_cur_state:
> + mutex_unlock(&cooling_cpufreq_lock);
> + return ret;
> +}
> +
> +/**
> + * cpufreq_set_cur_state - callback function to set the current cooling state.
> + * @cdev: thermal cooling device pointer.
> + * @state: set this variable to the current cooling state.
> + */
> +static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
> + unsigned long state)
> +{
> + int ret = -EINVAL;
> + struct cpufreq_cooling_device *cpufreq_device;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> + if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
> + ret = 0;
> + break;
> + }
> + }
> + if (!ret)
> + ret = cpufreq_apply_cooling(cpufreq_device, state);
> +
> + mutex_unlock(&cooling_cpufreq_lock);
> +
> + return ret;
> +}
> +
> +/* Bind cpufreq callbacks to thermal cooling device ops */
> +static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
> + .get_max_state = cpufreq_get_max_state,
> + .get_cur_state = cpufreq_get_cur_state,
> + .set_cur_state = cpufreq_set_cur_state,
> +};
> +
> +/* Notifier for cpufreq policy change */
> +static struct notifier_block thermal_cpufreq_notifier_block = {
> + .notifier_call = cpufreq_thermal_notifier,
> +};
> +
> +/**
> + * cpufreq_cooling_register - function to create cpufreq cooling device.
> + * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
> + */
> +struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus)
> +{
> + struct thermal_cooling_device *cool_dev;
> + struct cpufreq_cooling_device *cpufreq_dev = NULL;
> + unsigned int cpufreq_dev_count = 0, min = 0, max = 0;
> + char dev_name[THERMAL_NAME_LENGTH];
> + int ret = 0, id = 0, i;
> + struct cpufreq_policy policy;
> +
> + list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node)
> + cpufreq_dev_count++;
> +
> + /*Verify that all the clip cpus have same freq_min, freq_max limit*/
> + for_each_cpu(i, clip_cpus) {
> + /*continue if cpufreq policy not found and not return error*/
> + if (!cpufreq_get_policy(&policy, i))
> + continue;
> + if (min == 0 && max == 0) {
> + min = policy.cpuinfo.min_freq;
> + max = policy.cpuinfo.max_freq;
> + } else {
> + if (min != policy.cpuinfo.min_freq ||
> + max != policy.cpuinfo.max_freq)
> + return ERR_PTR(-EINVAL);
> + }
> + }
> + cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
> + GFP_KERNEL);
> + if (!cpufreq_dev)
> + return ERR_PTR(-ENOMEM);
> +
> + cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
> +
> + if (cpufreq_dev_count == 0)
> + mutex_init(&cooling_cpufreq_lock);
> +
> + ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
> + if (ret) {
> + kfree(cpufreq_dev);
> + return ERR_PTR(-EINVAL);
> + }
> +
> + sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
> +
> + cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
> + &cpufreq_cooling_ops);
> + if (!cool_dev) {
> + release_idr(&cpufreq_idr, cpufreq_dev->id);
> + kfree(cpufreq_dev);
> + return ERR_PTR(-EINVAL);
> + }
> + cpufreq_dev->id = id;
> + cpufreq_dev->cool_dev = cool_dev;
> + cpufreq_dev->cpufreq_state = 0;
> + mutex_lock(&cooling_cpufreq_lock);
> + list_add_tail(&cpufreq_dev->node, &cooling_cpufreq_list);
> +
> + /* Register the notifier for first cpufreq cooling device */
> + if (cpufreq_dev_count == 0)
> + cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
> + CPUFREQ_POLICY_NOTIFIER);
> +
> + mutex_unlock(&cooling_cpufreq_lock);
> + return cool_dev;
> +}
> +EXPORT_SYMBOL(cpufreq_cooling_register);
> +
> +/**
> + * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
> + * @cdev: thermal cooling device pointer.
> + */
> +void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
> +{
> + struct cpufreq_cooling_device *cpufreq_dev = NULL;
> + unsigned int cpufreq_dev_count = 0;
> +
> + mutex_lock(&cooling_cpufreq_lock);
> + list_for_each_entry(cpufreq_dev, &cooling_cpufreq_list, node) {
> + if (cpufreq_dev && cpufreq_dev->cool_dev == cdev)
> + break;
> + cpufreq_dev_count++;
> + }
> +
> + if (!cpufreq_dev || cpufreq_dev->cool_dev != cdev) {
> + mutex_unlock(&cooling_cpufreq_lock);
> + return;
> + }
> +
> + list_del(&cpufreq_dev->node);
> +
> + /* Unregister the notifier for the last cpufreq cooling device */
> + if (cpufreq_dev_count == 1) {
> + cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
> + CPUFREQ_POLICY_NOTIFIER);
> + }
> + mutex_unlock(&cooling_cpufreq_lock);
> + thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
> + release_idr(&cpufreq_idr, cpufreq_dev->id);
> + if (cpufreq_dev_count == 1)
> + mutex_destroy(&cooling_cpufreq_lock);
> + kfree(cpufreq_dev);
> +}
> +EXPORT_SYMBOL(cpufreq_cooling_unregister);
> diff --git a/include/linux/cpu_cooling.h b/include/linux/cpu_cooling.h
> new file mode 100644
> index 0000000..555b917
> --- /dev/null
> +++ b/include/linux/cpu_cooling.h
> @@ -0,0 +1,79 @@
> +/*
> + * linux/include/linux/cpu_cooling.h
> + *
> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> + * Copyright (C) 2012 Amit Daniel <[email protected]>
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + * 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; version 2 of the License.
> + *
> + * This program is distributed in the hope that it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, write to the Free Software Foundation, Inc.,
> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> + *
> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + */
> +
> +#ifndef __CPU_COOLING_H__
> +#define __CPU_COOLING_H__
> +
> +#include <linux/thermal.h>
> +
> +#define CPUFREQ_COOLING_START 0
> +#define CPUFREQ_COOLING_STOP 1
> +
> +/**
> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. possible values are
> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> + *
> + * This exported function registers a driver with cpu cooling layer. The driver
> + * will be notified when any cpu cooling action is called.
> + */
> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list);
> +
> +/**
> + * cputherm_unregister_notifier - Un-register a notifier.
> + * @nb: struct notifier_block * with callback info.
> + * @list: integer value for which notification is needed. values possible are
> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> + *
> + * This exported function un-registers a driver with cpu cooling layer.
> + */
> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list);
> +
> +#ifdef CONFIG_CPU_THERMAL
> +/**
> + * cpufreq_cooling_register - function to create cpufreq cooling device.
> + * @clip_cpus: cpumask of cpus where the frequency constraints will happen
> + */
> +struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus);
> +
> +/**
> + * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
> + * @cdev: thermal cooling device pointer.
> + */
> +void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev);
> +#else /* !CONFIG_CPU_THERMAL */
> +static inline struct thermal_cooling_device *cpufreq_cooling_register(
> + struct cpumask *clip_cpus)
> +{
> + return NULL;
> +}
> +static inline void cpufreq_cooling_unregister(
> + struct thermal_cooling_device *cdev)
> +{
> + return;
> +}
> +#endif /* CONFIG_CPU_THERMAL */
> +
> +#endif /* __CPU_COOLING_H__ */
> --
> 1.7.1
>
On 五, 2012-08-17 at 11:56 +0300, Valentin, Eduardo wrote:
> Hello,
> >>> +
> >>> +
> >>> +1.2 CPU cooling action notifier register/unregister interface
> >>> +1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
> >>> + unsigned int list)
> >>> +
> >>> + This interface registers a driver with cpu cooling layer. The driver will
> >>> + be notified when any cpu cooling action is called.
> >>> +
> >>> + nb: notifier function to register
> >>> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> >>> +
> >>> +1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
> >>> + unsigned int list)
> >>> +
> >>> + This interface registers a driver with cpu cooling layer. The driver will
> >>> + be notified when any cpu cooling action is called.
> >>> +
> >>> + nb: notifier function to register
> >>> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
> >>
> >> what are these two APIs used for?
> >> I did not see they are used in your patch set, do I miss something?
> > No currently they are not used by my patches. I added them on request
> > from Eduardo and others
>
> Yeah, this was a suggestion as we didn't really know how the FW part
> would evolve by that time.
>
> The reasoning is to allow any interested user (in kernel) to be
> notified when max frequency changes.
in this case, the cooling device should be updated.
Say all the target of the thermal_instances of a cooling devices is 0,
which means they want the cpu to run at maximum frequency, when the max
frequency changes, we should set the processor to the new max frequency
as well.
Using notification is not a good idea as user can not handle this
without interacting with the thermal framework.
IMO, we should introduce a new API to handle this, rather than just
notifications to users.
> Actually, the use case behind
> this is to allow such users to perform some handshaking or stop their
> activities or even change some paramenters, in case the max frequency
> would change.
It is the cooling device driver that notice this change first, and it
should invoke something like thermal_cooling_device_update/rebind() to
tell the thermal framework this change.
> Ideally it would be possible to nack the cooling
> transition. But that is yet a wider discussion. So far we don't have
> users for this.
>
yep.
I thought about this before, but I'd prefer to do this when there is a
real user. Or else, we are kind of over-designing here.
how about removing this piece of code for now?
thanks,
rui
> >>
> >>> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> >>> index 7dd8c34..996003b 100644
> >>> --- a/drivers/thermal/Kconfig
> >>> +++ b/drivers/thermal/Kconfig
> >>> @@ -19,6 +19,17 @@ config THERMAL_HWMON
> >>> depends on HWMON=y || HWMON=THERMAL
> >>> default y
> >>>
> >>> +config CPU_THERMAL
> >>> + bool "generic cpu cooling support"
> >>> + depends on THERMAL && CPU_FREQ
> >>> + help
> >>> + This implements the generic cpu cooling mechanism through frequency
> >>> + reduction, cpu hotplug and any other ways of reducing temperature. An
> >>> + ACPI version of this already exists(drivers/acpi/processor_thermal.c).
> >>> + This will be useful for platforms using the generic thermal interface
> >>> + and not the ACPI interface.
> >>> + If you want this support, you should say Y here.
> >>> +
> >>> config SPEAR_THERMAL
> >>> bool "SPEAr thermal sensor driver"
> >>> depends on THERMAL
> >>> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> >>> index fd9369a..aae59ad 100644
> >>> --- a/drivers/thermal/Makefile
> >>> +++ b/drivers/thermal/Makefile
> >>> @@ -3,5 +3,6 @@
> >>> #
> >>>
> >>> obj-$(CONFIG_THERMAL) += thermal_sys.o
> >>> +obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
> >>> obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
> >>> obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
> >>> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
> >>> new file mode 100644
> >>> index 0000000..c42e557
> >>> --- /dev/null
> >>> +++ b/drivers/thermal/cpu_cooling.c
> >>> @@ -0,0 +1,512 @@
> >>> +/*
> >>> + * linux/drivers/thermal/cpu_cooling.c
> >>> + *
> >>> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
> >>> + * Copyright (C) 2012 Amit Daniel <[email protected]>
> >>> + *
> >>> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >>> + * 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; version 2 of the License.
> >>> + *
> >>> + * This program is distributed in the hope that it will be useful, but
> >>> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> >>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> >>> + * General Public License for more details.
> >>> + *
> >>> + * You should have received a copy of the GNU General Public License along
> >>> + * with this program; if not, write to the Free Software Foundation, Inc.,
> >>> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
> >>> + *
> >>> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >>> + */
> >>> +#include <linux/kernel.h>
> >>> +#include <linux/module.h>
> >>> +#include <linux/thermal.h>
> >>> +#include <linux/platform_device.h>
> >>> +#include <linux/cpufreq.h>
> >>> +#include <linux/err.h>
> >>> +#include <linux/slab.h>
> >>> +#include <linux/cpu.h>
> >>> +#include <linux/cpu_cooling.h>
> >>> +
> >>> +/**
> >>> + * struct cpufreq_cooling_device
> >>> + * @id: unique integer value corresponding to each cpufreq_cooling_device
> >>> + * registered.
> >>> + * @cool_dev: thermal_cooling_device pointer to keep track of the the
> >>> + * egistered cooling device.
> >>> + * @cpufreq_state: integer value representing the current state of cpufreq
> >>> + * cooling devices.
> >>> + * @cpufreq_val: integer value representing the absolute value of the clipped
> >>> + * frequency.
> >>> + * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
> >>> + * @node: list_head to link all cpufreq_cooling_device together.
> >>> + *
> >>> + * This structure is required for keeping information of each
> >>> + * cpufreq_cooling_device registered as a list whose head is represented by
> >>> + * cooling_cpufreq_list. In order to prevent corruption of this list a
> >>> + * mutex lock cooling_cpufreq_lock is used.
> >>> + */
> >>> +struct cpufreq_cooling_device {
> >>> + int id;
> >>> + struct thermal_cooling_device *cool_dev;
> >>> + unsigned int cpufreq_state;
> >>> + unsigned int cpufreq_val;
> >>> + struct cpumask allowed_cpus;
> >>> + struct list_head node;
> >>> +};
> >>> +static LIST_HEAD(cooling_cpufreq_list);
> >>> +static DEFINE_IDR(cpufreq_idr);
> >>> +
> >>> +static struct mutex cooling_cpufreq_lock;
> >>> +
> >>> +/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
> >>> +#define NOTIFY_INVALID NULL
> >>> +struct cpufreq_cooling_device *notify_device;
> >>> +
> >>> +/* Head of the blocking notifier chain to inform about frequency clamping */
> >>> +static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
> >>> +
> >>> +/**
> >>> + * get_idr - function to get a unique id.
> >>> + * @idr: struct idr * handle used to create a id.
> >>> + * @id: int * value generated by this function.
> >>> + */
> >>> +static int get_idr(struct idr *idr, int *id)
> >>> +{
> >>> + int err;
> >>> +again:
> >>> + if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
> >>> + return -ENOMEM;
> >>> +
> >>> + mutex_lock(&cooling_cpufreq_lock);
> >>> + err = idr_get_new(idr, NULL, id);
> >>> + mutex_unlock(&cooling_cpufreq_lock);
> >>> +
> >>> + if (unlikely(err == -EAGAIN))
> >>> + goto again;
> >>> + else if (unlikely(err))
> >>> + return err;
> >>> +
> >>> + *id = *id & MAX_ID_MASK;
> >>> + return 0;
> >>> +}
> >>> +
> >>> +/**
> >>> + * release_idr - function to free the unique id.
> >>> + * @idr: struct idr * handle used for creating the id.
> >>> + * @id: int value representing the unique id.
> >>> + */
> >>> +static void release_idr(struct idr *idr, int id)
> >>> +{
> >>> + mutex_lock(&cooling_cpufreq_lock);
> >>> + idr_remove(idr, id);
> >>> + mutex_unlock(&cooling_cpufreq_lock);
> >>> +}
> >>> +
> >>> +/**
> >>> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
> >>> + * @nb: struct notifier_block * with callback info.
> >>> + * @list: integer value for which notification is needed. possible values are
> >>> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
> >>> + *
> >>> + * This exported function registers a driver with cpu cooling layer. The driver
> >>> + * will be notified when any cpu cooling action is called.
> >>> + */
> >>> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
> >>> +{
> >>> + int ret = 0;
> >>> +
> >>> + switch (list) {
> >>> + case CPUFREQ_COOLING_START:
> >>> + case CPUFREQ_COOLING_STOP:
> >>> + ret = blocking_notifier_chain_register(
> >>> + &cputherm_state_notifier_list, nb);
> >>> + break;
> >>> + default:
> >>> + ret = -EINVAL;
> >>> + }
> >>> + return ret;
> >>> +}
> >>> +EXPORT_SYMBOL(cputherm_register_notifier);
> >>> +
> >>> +/**
> >>> + * cputherm_unregister_notifier - Un-register a notifier.
> >>> + * @nb: struct notifier_block * with callback info.
> >>> + * @list: integer value for which notification is needed. values possible are
> >>> + * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
> >>> + *
> >>> + * This exported function un-registers a driver with cpu cooling layer.
> >>> + */
> >>> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
> >>> +{
> >>> + int ret = 0;
> >>> +
> >>> + switch (list) {
> >>> + case CPUFREQ_COOLING_START:
> >>> + case CPUFREQ_COOLING_STOP:
> >>> + ret = blocking_notifier_chain_unregister(
> >>> + &cputherm_state_notifier_list, nb);
> >>> + break;
> >>> + default:
> >>> + ret = -EINVAL;
> >>> + }
> >>> + return ret;
> >>> +}
> >>> +EXPORT_SYMBOL(cputherm_unregister_notifier);
> >>> +
> >>> +/* Below code defines functions to be used for cpufreq as cooling device */
> >>> +
> >>> +/**
> >>> + * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
> >>> + * @cpu: cpu for which check is needed.
> >>> + */
> >>> +static int is_cpufreq_valid(int cpu)
> >>> +{
> >>> + struct cpufreq_policy policy;
> >>> + return !cpufreq_get_policy(&policy, cpu);
> >>> +}
> >>> +
> >>> +/**
> >>> + * get_cpu_frequency - get the absolute value of frequency from level.
> >>> + * @cpu: cpu for which frequency is fetched.
> >>> + * @level: level of frequency of the CPU
> >>> + * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
> >>> + */
> >>> +static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
> >>> +{
> >>> + int ret = 0, i = 0;
> >>> + unsigned long level_index;
> >>> + bool descend = false;
> >>> + struct cpufreq_frequency_table *table =
> >>> + cpufreq_frequency_get_table(cpu);
> >>> + if (!table)
> >>> + return ret;
> >>> +
> >>> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> >>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> >>> + continue;
> >>> +
> >>> + /*check if table in ascending or descending order*/
> >>> + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
> >>> + (table[i + 1].frequency < table[i].frequency)
> >>> + && !descend) {
> >>> + descend = true;
> >>> + }
> >>> +
> >>> + /*return if level matched and table in descending order*/
> >>> + if (descend && i == level)
> >>> + return table[i].frequency;
> >>> + i++;
> >>> + }
> >>> + i--;
> >>> +
> >>> + if (level > i || descend)
> >>> + return ret;
> >>> + level_index = i - level;
> >>> +
> >>> + /*Scan the table in reverse order and match the level*/
> >>> + while (i >= 0) {
> >>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> >>> + continue;
> >>> + /*return if level matched*/
> >>> + if (i == level_index)
> >>> + return table[i].frequency;
> >>> + i--;
> >>> + }
> >>> + return ret;
> >>> +}
> >>> +
> >>> +/**
> >>> + * cpufreq_apply_cooling - function to apply frequency clipping.
> >>> + * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
> >>> + * clipping data.
> >>> + * @cooling_state: value of the cooling state.
> >>> + */
> >>> +static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
> >>> + unsigned long cooling_state)
> >>> +{
> >>> + unsigned int event, cpuid, clip_freq;
> >>> + struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
> >>> + unsigned int cpu = cpumask_any(maskPtr);
> >>> +
> >>> +
> >>> + /* Check if the old cooling action is same as new cooling action */
> >>> + if (cpufreq_device->cpufreq_state == cooling_state)
> >>> + return 0;
> >>> +
> >>> + clip_freq = get_cpu_frequency(cpu, cooling_state);
> >>> + if (!clip_freq)
> >>> + return -EINVAL;
> >>> +
> >>> + cpufreq_device->cpufreq_state = cooling_state;
> >>> + cpufreq_device->cpufreq_val = clip_freq;
> >>> + notify_device = cpufreq_device;
> >>> +
> >>> + if (cooling_state != 0)
> >>> + event = CPUFREQ_COOLING_START;
> >>> + else
> >>> + event = CPUFREQ_COOLING_STOP;
> >>> +
> >>> + blocking_notifier_call_chain(&cputherm_state_notifier_list,
> >>> + event, &clip_freq);
> >>> +
> >>> + for_each_cpu(cpuid, maskPtr) {
> >>> + if (is_cpufreq_valid(cpuid))
> >>> + cpufreq_update_policy(cpuid);
> >>> + }
> >>> +
> >>> + notify_device = NOTIFY_INVALID;
> >>> +
> >>> + return 0;
> >>> +}
> >>> +
> >>> +/**
> >>> + * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
> >>> + * @nb: struct notifier_block * with callback info.
> >>> + * @event: value showing cpufreq event for which this function invoked.
> >>> + * @data: callback-specific data
> >>> + */
> >>> +static int cpufreq_thermal_notifier(struct notifier_block *nb,
> >>> + unsigned long event, void *data)
> >>> +{
> >>> + struct cpufreq_policy *policy = data;
> >>> + unsigned long max_freq = 0;
> >>> +
> >>> + if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
> >>> + return 0;
> >>> +
> >>> + if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
> >>> + max_freq = notify_device->cpufreq_val;
> >>> +
> >>> + /* Never exceed user_policy.max*/
> >>> + if (max_freq > policy->user_policy.max)
> >>> + max_freq = policy->user_policy.max;
> >>> +
> >>> + if (policy->max != max_freq)
> >>> + cpufreq_verify_within_limits(policy, 0, max_freq);
> >>> +
> >>> + return 0;
> >>> +}
> >>> +
> >>> +/*
> >>> + * cpufreq cooling device callback functions are defined below
> >>> + */
> >>> +
> >>> +/**
> >>> + * cpufreq_get_max_state - callback function to get the max cooling state.
> >>> + * @cdev: thermal cooling device pointer.
> >>> + * @state: fill this variable with the max cooling state.
> >>> + */
> >>> +static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
> >>> + unsigned long *state)
> >>> +{
> >>> + int ret = -EINVAL, i = 0;
> >>> + struct cpufreq_cooling_device *cpufreq_device;
> >>> + struct cpumask *maskPtr;
> >>> + unsigned int cpu;
> >>> + struct cpufreq_frequency_table *table;
> >>> +
> >>> + mutex_lock(&cooling_cpufreq_lock);
> >>> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> >>> + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
> >>> + break;
> >>> + }
> >>> + if (cpufreq_device == NULL)
> >>> + goto return_get_max_state;
> >>> +
> >>> + maskPtr = &cpufreq_device->allowed_cpus;
> >>> + cpu = cpumask_any(maskPtr);
> >>> + table = cpufreq_frequency_get_table(cpu);
> >>> + if (!table) {
> >>> + *state = 0;
> >>> + ret = 0;
> >>> + goto return_get_max_state;
> >>> + }
> >>> +
> >>> + while (table[i].frequency != CPUFREQ_TABLE_END) {
> >>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> >>> + continue;
> >>> + i++;
> >>> + }
> >>> + if (i > 0) {
> >>> + *state = --i;
> >>> + ret = 0;
> >>> + }
> >>> +
> >>> +return_get_max_state:
> >>> + mutex_unlock(&cooling_cpufreq_lock);
> >>> + return ret;
> >>> +}
> >>> +
> >>> +/**
> >>> + * cpufreq_get_cur_state - callback function to get the current cooling state.
> >>> + * @cdev: thermal cooling device pointer.
> >>> + * @state: fill this variable with the current cooling state.
> >>> + */
> >>> +static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
> >>> + unsigned long *state)
> >>> +{
> >>> + int ret = -EINVAL;
> >>> + struct cpufreq_cooling_device *cpufreq_device;
> >>> +
> >>> + mutex_lock(&cooling_cpufreq_lock);
> >>> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
> >>> + if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
> >>> + *state = cpufreq_device->cpufreq_state;
> >>> + ret = 0;
> >>> + break;
> >>> + }
> >>> + }
> >>> + mutex_unlock(&cooling_cpufreq_lock);
> >>> +
> >>
> >> as cpufreq may be changed in other places, e.g. via sysfs I/F, we should
> >> use the current cpu frequency to get the REAL cooling state, rather than
> >> using a cached value.
> >
> > Yes agreed , I will repost with your suggestion.
> >
> > Thanks,
> > Amit
> >>
> >> thanks,
> >> rui
> >>
> >>
> > --
> > To unsubscribe from this list: send the line "unsubscribe linux-acpi" in
> > the body of a message to [email protected]
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
Hello Rui,
On Mon, Aug 20, 2012 at 5:16 AM, Zhang Rui <[email protected]> wrote:
> On 五, 2012-08-17 at 11:56 +0300, Valentin, Eduardo wrote:
>> Hello,
>
>> >>> +
>> >>> +
>> >>> +1.2 CPU cooling action notifier register/unregister interface
>> >>> +1.2.1 int cputherm_register_notifier(struct notifier_block *nb,
>> >>> + unsigned int list)
>> >>> +
>> >>> + This interface registers a driver with cpu cooling layer. The driver will
>> >>> + be notified when any cpu cooling action is called.
>> >>> +
>> >>> + nb: notifier function to register
>> >>> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
>> >>> +
>> >>> +1.2.2 int cputherm_unregister_notifier(struct notifier_block *nb,
>> >>> + unsigned int list)
>> >>> +
>> >>> + This interface registers a driver with cpu cooling layer. The driver will
>> >>> + be notified when any cpu cooling action is called.
>> >>> +
>> >>> + nb: notifier function to register
>> >>> + list: CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP
>> >>
>> >> what are these two APIs used for?
>> >> I did not see they are used in your patch set, do I miss something?
>> > No currently they are not used by my patches. I added them on request
>> > from Eduardo and others
>>
>> Yeah, this was a suggestion as we didn't really know how the FW part
>> would evolve by that time.
>>
>> The reasoning is to allow any interested user (in kernel) to be
>> notified when max frequency changes.
>
> in this case, the cooling device should be updated.
> Say all the target of the thermal_instances of a cooling devices is 0,
> which means they want the cpu to run at maximum frequency, when the max
> frequency changes, we should set the processor to the new max frequency
> as well.
> Using notification is not a good idea as user can not handle this
> without interacting with the thermal framework.
>
> IMO, we should introduce a new API to handle this, rather than just
> notifications to users.
Agreed. The context is actually much wider than the CPU cooling. In
fact, cooling co-processors is actually where things gets a bit
complicated. The idea behind this type of handshaking is to allow the
affected subsystem to change their actual setup when max performance
is not allowed anymore.
>
>> Actually, the use case behind
>> this is to allow such users to perform some handshaking or stop their
>> activities or even change some paramenters, in case the max frequency
>> would change.
>
> It is the cooling device driver that notice this change first, and it
> should invoke something like thermal_cooling_device_update/rebind() to
> tell the thermal framework this change.
>
Yeah. Ideally, the framework needs to be aware of cooling device state
change. Today, as we have pretty much a broadcast/unidirectional type
of messaging, the framework/policy always assumes the cooling devices
will be in sync with what it is dictated by the policy.
>> Ideally it would be possible to nack the cooling
>> transition. But that is yet a wider discussion. So far we don't have
>> users for this.
>>
> yep.
> I thought about this before, but I'd prefer to do this when there is a
> real user. Or else, we are kind of over-designing here.
> how about removing this piece of code for now?
Agreed. I second you that this problem is a much wider issue and needs
improvement in the FW itself and how the cooling devices are designed.
>
> thanks,
> rui
>
>> >>
>> >>> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
>> >>> index 7dd8c34..996003b 100644
>> >>> --- a/drivers/thermal/Kconfig
>> >>> +++ b/drivers/thermal/Kconfig
>> >>> @@ -19,6 +19,17 @@ config THERMAL_HWMON
>> >>> depends on HWMON=y || HWMON=THERMAL
>> >>> default y
>> >>>
>> >>> +config CPU_THERMAL
>> >>> + bool "generic cpu cooling support"
>> >>> + depends on THERMAL && CPU_FREQ
>> >>> + help
>> >>> + This implements the generic cpu cooling mechanism through frequency
>> >>> + reduction, cpu hotplug and any other ways of reducing temperature. An
>> >>> + ACPI version of this already exists(drivers/acpi/processor_thermal.c).
>> >>> + This will be useful for platforms using the generic thermal interface
>> >>> + and not the ACPI interface.
>> >>> + If you want this support, you should say Y here.
>> >>> +
>> >>> config SPEAR_THERMAL
>> >>> bool "SPEAr thermal sensor driver"
>> >>> depends on THERMAL
>> >>> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
>> >>> index fd9369a..aae59ad 100644
>> >>> --- a/drivers/thermal/Makefile
>> >>> +++ b/drivers/thermal/Makefile
>> >>> @@ -3,5 +3,6 @@
>> >>> #
>> >>>
>> >>> obj-$(CONFIG_THERMAL) += thermal_sys.o
>> >>> +obj-$(CONFIG_CPU_THERMAL) += cpu_cooling.o
>> >>> obj-$(CONFIG_SPEAR_THERMAL) += spear_thermal.o
>> >>> obj-$(CONFIG_RCAR_THERMAL) += rcar_thermal.o
>> >>> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
>> >>> new file mode 100644
>> >>> index 0000000..c42e557
>> >>> --- /dev/null
>> >>> +++ b/drivers/thermal/cpu_cooling.c
>> >>> @@ -0,0 +1,512 @@
>> >>> +/*
>> >>> + * linux/drivers/thermal/cpu_cooling.c
>> >>> + *
>> >>> + * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
>> >>> + * Copyright (C) 2012 Amit Daniel <[email protected]>
>> >>> + *
>> >>> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> >>> + * 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; version 2 of the License.
>> >>> + *
>> >>> + * This program is distributed in the hope that it will be useful, but
>> >>> + * WITHOUT ANY WARRANTY; without even the implied warranty of
>> >>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
>> >>> + * General Public License for more details.
>> >>> + *
>> >>> + * You should have received a copy of the GNU General Public License along
>> >>> + * with this program; if not, write to the Free Software Foundation, Inc.,
>> >>> + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
>> >>> + *
>> >>> + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> >>> + */
>> >>> +#include <linux/kernel.h>
>> >>> +#include <linux/module.h>
>> >>> +#include <linux/thermal.h>
>> >>> +#include <linux/platform_device.h>
>> >>> +#include <linux/cpufreq.h>
>> >>> +#include <linux/err.h>
>> >>> +#include <linux/slab.h>
>> >>> +#include <linux/cpu.h>
>> >>> +#include <linux/cpu_cooling.h>
>> >>> +
>> >>> +/**
>> >>> + * struct cpufreq_cooling_device
>> >>> + * @id: unique integer value corresponding to each cpufreq_cooling_device
>> >>> + * registered.
>> >>> + * @cool_dev: thermal_cooling_device pointer to keep track of the the
>> >>> + * egistered cooling device.
>> >>> + * @cpufreq_state: integer value representing the current state of cpufreq
>> >>> + * cooling devices.
>> >>> + * @cpufreq_val: integer value representing the absolute value of the clipped
>> >>> + * frequency.
>> >>> + * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
>> >>> + * @node: list_head to link all cpufreq_cooling_device together.
>> >>> + *
>> >>> + * This structure is required for keeping information of each
>> >>> + * cpufreq_cooling_device registered as a list whose head is represented by
>> >>> + * cooling_cpufreq_list. In order to prevent corruption of this list a
>> >>> + * mutex lock cooling_cpufreq_lock is used.
>> >>> + */
>> >>> +struct cpufreq_cooling_device {
>> >>> + int id;
>> >>> + struct thermal_cooling_device *cool_dev;
>> >>> + unsigned int cpufreq_state;
>> >>> + unsigned int cpufreq_val;
>> >>> + struct cpumask allowed_cpus;
>> >>> + struct list_head node;
>> >>> +};
>> >>> +static LIST_HEAD(cooling_cpufreq_list);
>> >>> +static DEFINE_IDR(cpufreq_idr);
>> >>> +
>> >>> +static struct mutex cooling_cpufreq_lock;
>> >>> +
>> >>> +/* notify_table passes value to the CPUFREQ_ADJUST callback function. */
>> >>> +#define NOTIFY_INVALID NULL
>> >>> +struct cpufreq_cooling_device *notify_device;
>> >>> +
>> >>> +/* Head of the blocking notifier chain to inform about frequency clamping */
>> >>> +static BLOCKING_NOTIFIER_HEAD(cputherm_state_notifier_list);
>> >>> +
>> >>> +/**
>> >>> + * get_idr - function to get a unique id.
>> >>> + * @idr: struct idr * handle used to create a id.
>> >>> + * @id: int * value generated by this function.
>> >>> + */
>> >>> +static int get_idr(struct idr *idr, int *id)
>> >>> +{
>> >>> + int err;
>> >>> +again:
>> >>> + if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
>> >>> + return -ENOMEM;
>> >>> +
>> >>> + mutex_lock(&cooling_cpufreq_lock);
>> >>> + err = idr_get_new(idr, NULL, id);
>> >>> + mutex_unlock(&cooling_cpufreq_lock);
>> >>> +
>> >>> + if (unlikely(err == -EAGAIN))
>> >>> + goto again;
>> >>> + else if (unlikely(err))
>> >>> + return err;
>> >>> +
>> >>> + *id = *id & MAX_ID_MASK;
>> >>> + return 0;
>> >>> +}
>> >>> +
>> >>> +/**
>> >>> + * release_idr - function to free the unique id.
>> >>> + * @idr: struct idr * handle used for creating the id.
>> >>> + * @id: int value representing the unique id.
>> >>> + */
>> >>> +static void release_idr(struct idr *idr, int id)
>> >>> +{
>> >>> + mutex_lock(&cooling_cpufreq_lock);
>> >>> + idr_remove(idr, id);
>> >>> + mutex_unlock(&cooling_cpufreq_lock);
>> >>> +}
>> >>> +
>> >>> +/**
>> >>> + * cputherm_register_notifier - Register a notifier with cpu cooling interface.
>> >>> + * @nb: struct notifier_block * with callback info.
>> >>> + * @list: integer value for which notification is needed. possible values are
>> >>> + * CPUFREQ_COOLING_START and CPUFREQ_COOLING_STOP.
>> >>> + *
>> >>> + * This exported function registers a driver with cpu cooling layer. The driver
>> >>> + * will be notified when any cpu cooling action is called.
>> >>> + */
>> >>> +int cputherm_register_notifier(struct notifier_block *nb, unsigned int list)
>> >>> +{
>> >>> + int ret = 0;
>> >>> +
>> >>> + switch (list) {
>> >>> + case CPUFREQ_COOLING_START:
>> >>> + case CPUFREQ_COOLING_STOP:
>> >>> + ret = blocking_notifier_chain_register(
>> >>> + &cputherm_state_notifier_list, nb);
>> >>> + break;
>> >>> + default:
>> >>> + ret = -EINVAL;
>> >>> + }
>> >>> + return ret;
>> >>> +}
>> >>> +EXPORT_SYMBOL(cputherm_register_notifier);
>> >>> +
>> >>> +/**
>> >>> + * cputherm_unregister_notifier - Un-register a notifier.
>> >>> + * @nb: struct notifier_block * with callback info.
>> >>> + * @list: integer value for which notification is needed. values possible are
>> >>> + * CPUFREQ_COOLING_START or CPUFREQ_COOLING_STOP.
>> >>> + *
>> >>> + * This exported function un-registers a driver with cpu cooling layer.
>> >>> + */
>> >>> +int cputherm_unregister_notifier(struct notifier_block *nb, unsigned int list)
>> >>> +{
>> >>> + int ret = 0;
>> >>> +
>> >>> + switch (list) {
>> >>> + case CPUFREQ_COOLING_START:
>> >>> + case CPUFREQ_COOLING_STOP:
>> >>> + ret = blocking_notifier_chain_unregister(
>> >>> + &cputherm_state_notifier_list, nb);
>> >>> + break;
>> >>> + default:
>> >>> + ret = -EINVAL;
>> >>> + }
>> >>> + return ret;
>> >>> +}
>> >>> +EXPORT_SYMBOL(cputherm_unregister_notifier);
>> >>> +
>> >>> +/* Below code defines functions to be used for cpufreq as cooling device */
>> >>> +
>> >>> +/**
>> >>> + * is_cpufreq_valid - function to check if a cpu has frequency transition policy.
>> >>> + * @cpu: cpu for which check is needed.
>> >>> + */
>> >>> +static int is_cpufreq_valid(int cpu)
>> >>> +{
>> >>> + struct cpufreq_policy policy;
>> >>> + return !cpufreq_get_policy(&policy, cpu);
>> >>> +}
>> >>> +
>> >>> +/**
>> >>> + * get_cpu_frequency - get the absolute value of frequency from level.
>> >>> + * @cpu: cpu for which frequency is fetched.
>> >>> + * @level: level of frequency of the CPU
>> >>> + * e.g level=1 --> 1st MAX FREQ, LEVEL=2 ---> 2nd MAX FREQ, .... etc
>> >>> + */
>> >>> +static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
>> >>> +{
>> >>> + int ret = 0, i = 0;
>> >>> + unsigned long level_index;
>> >>> + bool descend = false;
>> >>> + struct cpufreq_frequency_table *table =
>> >>> + cpufreq_frequency_get_table(cpu);
>> >>> + if (!table)
>> >>> + return ret;
>> >>> +
>> >>> + while (table[i].frequency != CPUFREQ_TABLE_END) {
>> >>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>> >>> + continue;
>> >>> +
>> >>> + /*check if table in ascending or descending order*/
>> >>> + if ((table[i + 1].frequency != CPUFREQ_TABLE_END) &&
>> >>> + (table[i + 1].frequency < table[i].frequency)
>> >>> + && !descend) {
>> >>> + descend = true;
>> >>> + }
>> >>> +
>> >>> + /*return if level matched and table in descending order*/
>> >>> + if (descend && i == level)
>> >>> + return table[i].frequency;
>> >>> + i++;
>> >>> + }
>> >>> + i--;
>> >>> +
>> >>> + if (level > i || descend)
>> >>> + return ret;
>> >>> + level_index = i - level;
>> >>> +
>> >>> + /*Scan the table in reverse order and match the level*/
>> >>> + while (i >= 0) {
>> >>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>> >>> + continue;
>> >>> + /*return if level matched*/
>> >>> + if (i == level_index)
>> >>> + return table[i].frequency;
>> >>> + i--;
>> >>> + }
>> >>> + return ret;
>> >>> +}
>> >>> +
>> >>> +/**
>> >>> + * cpufreq_apply_cooling - function to apply frequency clipping.
>> >>> + * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
>> >>> + * clipping data.
>> >>> + * @cooling_state: value of the cooling state.
>> >>> + */
>> >>> +static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
>> >>> + unsigned long cooling_state)
>> >>> +{
>> >>> + unsigned int event, cpuid, clip_freq;
>> >>> + struct cpumask *maskPtr = &cpufreq_device->allowed_cpus;
>> >>> + unsigned int cpu = cpumask_any(maskPtr);
>> >>> +
>> >>> +
>> >>> + /* Check if the old cooling action is same as new cooling action */
>> >>> + if (cpufreq_device->cpufreq_state == cooling_state)
>> >>> + return 0;
>> >>> +
>> >>> + clip_freq = get_cpu_frequency(cpu, cooling_state);
>> >>> + if (!clip_freq)
>> >>> + return -EINVAL;
>> >>> +
>> >>> + cpufreq_device->cpufreq_state = cooling_state;
>> >>> + cpufreq_device->cpufreq_val = clip_freq;
>> >>> + notify_device = cpufreq_device;
>> >>> +
>> >>> + if (cooling_state != 0)
>> >>> + event = CPUFREQ_COOLING_START;
>> >>> + else
>> >>> + event = CPUFREQ_COOLING_STOP;
>> >>> +
>> >>> + blocking_notifier_call_chain(&cputherm_state_notifier_list,
>> >>> + event, &clip_freq);
>> >>> +
>> >>> + for_each_cpu(cpuid, maskPtr) {
>> >>> + if (is_cpufreq_valid(cpuid))
>> >>> + cpufreq_update_policy(cpuid);
>> >>> + }
>> >>> +
>> >>> + notify_device = NOTIFY_INVALID;
>> >>> +
>> >>> + return 0;
>> >>> +}
>> >>> +
>> >>> +/**
>> >>> + * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
>> >>> + * @nb: struct notifier_block * with callback info.
>> >>> + * @event: value showing cpufreq event for which this function invoked.
>> >>> + * @data: callback-specific data
>> >>> + */
>> >>> +static int cpufreq_thermal_notifier(struct notifier_block *nb,
>> >>> + unsigned long event, void *data)
>> >>> +{
>> >>> + struct cpufreq_policy *policy = data;
>> >>> + unsigned long max_freq = 0;
>> >>> +
>> >>> + if (event != CPUFREQ_ADJUST || notify_device == NOTIFY_INVALID)
>> >>> + return 0;
>> >>> +
>> >>> + if (cpumask_test_cpu(policy->cpu, ¬ify_device->allowed_cpus))
>> >>> + max_freq = notify_device->cpufreq_val;
>> >>> +
>> >>> + /* Never exceed user_policy.max*/
>> >>> + if (max_freq > policy->user_policy.max)
>> >>> + max_freq = policy->user_policy.max;
>> >>> +
>> >>> + if (policy->max != max_freq)
>> >>> + cpufreq_verify_within_limits(policy, 0, max_freq);
>> >>> +
>> >>> + return 0;
>> >>> +}
>> >>> +
>> >>> +/*
>> >>> + * cpufreq cooling device callback functions are defined below
>> >>> + */
>> >>> +
>> >>> +/**
>> >>> + * cpufreq_get_max_state - callback function to get the max cooling state.
>> >>> + * @cdev: thermal cooling device pointer.
>> >>> + * @state: fill this variable with the max cooling state.
>> >>> + */
>> >>> +static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
>> >>> + unsigned long *state)
>> >>> +{
>> >>> + int ret = -EINVAL, i = 0;
>> >>> + struct cpufreq_cooling_device *cpufreq_device;
>> >>> + struct cpumask *maskPtr;
>> >>> + unsigned int cpu;
>> >>> + struct cpufreq_frequency_table *table;
>> >>> +
>> >>> + mutex_lock(&cooling_cpufreq_lock);
>> >>> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
>> >>> + if (cpufreq_device && cpufreq_device->cool_dev == cdev)
>> >>> + break;
>> >>> + }
>> >>> + if (cpufreq_device == NULL)
>> >>> + goto return_get_max_state;
>> >>> +
>> >>> + maskPtr = &cpufreq_device->allowed_cpus;
>> >>> + cpu = cpumask_any(maskPtr);
>> >>> + table = cpufreq_frequency_get_table(cpu);
>> >>> + if (!table) {
>> >>> + *state = 0;
>> >>> + ret = 0;
>> >>> + goto return_get_max_state;
>> >>> + }
>> >>> +
>> >>> + while (table[i].frequency != CPUFREQ_TABLE_END) {
>> >>> + if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
>> >>> + continue;
>> >>> + i++;
>> >>> + }
>> >>> + if (i > 0) {
>> >>> + *state = --i;
>> >>> + ret = 0;
>> >>> + }
>> >>> +
>> >>> +return_get_max_state:
>> >>> + mutex_unlock(&cooling_cpufreq_lock);
>> >>> + return ret;
>> >>> +}
>> >>> +
>> >>> +/**
>> >>> + * cpufreq_get_cur_state - callback function to get the current cooling state.
>> >>> + * @cdev: thermal cooling device pointer.
>> >>> + * @state: fill this variable with the current cooling state.
>> >>> + */
>> >>> +static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
>> >>> + unsigned long *state)
>> >>> +{
>> >>> + int ret = -EINVAL;
>> >>> + struct cpufreq_cooling_device *cpufreq_device;
>> >>> +
>> >>> + mutex_lock(&cooling_cpufreq_lock);
>> >>> + list_for_each_entry(cpufreq_device, &cooling_cpufreq_list, node) {
>> >>> + if (cpufreq_device && cpufreq_device->cool_dev == cdev) {
>> >>> + *state = cpufreq_device->cpufreq_state;
>> >>> + ret = 0;
>> >>> + break;
>> >>> + }
>> >>> + }
>> >>> + mutex_unlock(&cooling_cpufreq_lock);
>> >>> +
>> >>
>> >> as cpufreq may be changed in other places, e.g. via sysfs I/F, we should
>> >> use the current cpu frequency to get the REAL cooling state, rather than
>> >> using a cached value.
>> >
>> > Yes agreed , I will repost with your suggestion.
>> >
>> > Thanks,
>> > Amit
>> >>
>> >> thanks,
>> >> rui
>> >>
>> >>
>> > --
>> > To unsubscribe from this list: send the line "unsubscribe linux-acpi" in
>> > the body of a message to [email protected]
>> > More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>>
>>
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-acpi" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Eduardo Valentin