diff options
Diffstat (limited to 'drivers/devfreq')
| -rw-r--r-- | drivers/devfreq/Kconfig | 139 | ||||
| -rw-r--r-- | drivers/devfreq/Makefile | 10 | ||||
| -rw-r--r-- | drivers/devfreq/devfreq.c | 1078 | ||||
| -rw-r--r-- | drivers/devfreq/exynos4_bus.c | 1153 | ||||
| -rw-r--r-- | drivers/devfreq/governor.h | 41 | ||||
| -rw-r--r-- | drivers/devfreq/governor_ondemand.c | 729 | ||||
| -rw-r--r-- | drivers/devfreq/governor_performance.c | 67 | ||||
| -rw-r--r-- | drivers/devfreq/governor_powersave.c | 64 | ||||
| -rw-r--r-- | drivers/devfreq/governor_simpleondemand.c | 147 | ||||
| -rw-r--r-- | drivers/devfreq/governor_userspace.c | 162 | ||||
| -rw-r--r-- | drivers/devfreq/scx35_dmc.c | 852 |
11 files changed, 4442 insertions, 0 deletions
diff --git a/drivers/devfreq/Kconfig b/drivers/devfreq/Kconfig new file mode 100644 index 00000000..92af8386 --- /dev/null +++ b/drivers/devfreq/Kconfig @@ -0,0 +1,139 @@ +menuconfig PM_DEVFREQ + bool "Generic Dynamic Voltage and Frequency Scaling (DVFS) support" + help + A device may have a list of frequencies and voltages available. + devfreq, a generic DVFS framework can be registered for a device + in order to let the governor provided to devfreq choose an + operating frequency based on the device driver's policy. + + Each device may have its own governor and policy. Devfreq can + reevaluate the device state periodically and/or based on the + notification to "nb", a notifier block, of devfreq. + + Like some CPUs with CPUfreq, a device may have multiple clocks. + However, because the clock frequencies of a single device are + determined by the single device's state, an instance of devfreq + is attached to a single device and returns a "representative" + clock frequency of the device, which is also attached + to a device by 1-to-1. The device registering devfreq takes the + responsibility to "interpret" the representative frequency and + to set its every clock accordingly with the "target" callback + given to devfreq. + + When OPP is used with the devfreq device, it is recommended to + register devfreq's nb to the OPP's notifier head. If OPP is + used with the devfreq device, you may use OPP helper + functions defined in devfreq.h. + +if PM_DEVFREQ + +comment "DEVFREQ Governors" + +config DEVFREQ_GOV_SIMPLE_ONDEMAND + tristate "Simple Ondemand" + help + Chooses frequency based on the recent load on the device. Works + similar as ONDEMAND governor of CPUFREQ does. A device with + Simple-Ondemand should be able to provide busy/total counter + values that imply the usage rate. A device may provide tuned + values to the governor with data field at devfreq_add_device(). + +config DEVFREQ_GOV_PERFORMANCE + tristate "Performance" + help + Sets the frequency at the maximum available frequency. + This governor always returns UINT_MAX as frequency so that + the DEVFREQ framework returns the highest frequency available + at any time. + +config DEVFREQ_GOV_POWERSAVE + tristate "Powersave" + help + Sets the frequency at the minimum available frequency. + This governor always returns 0 as frequency so that + the DEVFREQ framework returns the lowest frequency available + at any time. + +config DEVFREQ_GOV_USERSPACE + tristate "Userspace" + help + Sets the frequency at the user specified one. + This governor returns the user configured frequency if there + has been an input to /sys/devices/.../power/devfreq_set_freq. + Otherwise, the governor does not change the frequnecy + given at the initialization. + +config DEVFREQ_GOV_ONDEMAND + tristate "Ondemand" + help + Chooses frequency based on the recent load on the device. Works + similar as ONDEMAND governor of CPUFREQ does. A device with + Simple-Ondemand should be able to provide busy/total counter + values that imply the usage rate. + Requests from user space and devices drivers can also tune frequency +comment "DEVFREQ Drivers" + +config ARM_EXYNOS4_BUS_DEVFREQ + bool "ARM Exynos4210/4212/4412 Memory Bus DEVFREQ Driver" + depends on CPU_EXYNOS4210 || CPU_EXYNOS4212 || CPU_EXYNOS4412 + select ARCH_HAS_OPP + select DEVFREQ_GOV_SIMPLE_ONDEMAND + help + This adds the DEVFREQ driver for Exynos4210 memory bus (vdd_int) + and Exynos4212/4412 memory interface and bus (vdd_mif + vdd_int). + It reads PPMU counters of memory controllers and adjusts + the operating frequencies and voltages with OPP support. + To operate with optimal voltages, ASV support is required + (CONFIG_EXYNOS_ASV). + +config SPRD_SCX35_DMC_FREQ + bool "Spreadtrum scx35 Memory Bus DMCFREQ Driver" + depends on ARCH_SCX35 + select ARCH_HAS_OPP + select DEVFREQ_GOV_ONDEMAND + help + This adds the DMCFREQ driver for Spreadtrum scx35 memory bus (vdd_core) + It reads PPMU counters of memory controllers and adjusts + the operating frequencies with OPP support. + +config SCX35_DMC_FREQ_AP + bool "Spreadtrum scx35 DDR DFS in AP side" + depends on ARCH_SCX35 + help + This adds the DMCFREQ drive, DDR DFS run in AP side + +config SCX35_DMC_FREQ_CP0 + bool "Spreadtrum scx35 DDR DFS in cp0 side" + depends on ARCH_SCX35 + help + This adds the DMCFREQ drive, DDR DFS run in cp0. + please make sure the cp0 is free. + +config SCX35_DMC_FREQ_CP1 + bool "Spreadtrum scx35 DDR DFS in cp1 side" + depends on ARCH_SCX35 + help + This adds the DMCFREQ drive, DDR DFS run in cp1. + please make sure the cp1 is free. + +config SCX35_DMC_FREQ_CP2 + bool "Spreadtrum scx35 DDR DFS in cp2 side" + depends on ARCH_SCX35 + help + This adds the DMCFREQ drive, DDR DFS run in cp2. + please make sure the cp2 is free. + +config SCX35_DMC_FREQ_DDR3 + bool "Spreadtrum scx35 DDR DFS for ddr3 side" + depends on ARCH_SCX35 + help + This adds the DMCFREQ drive, DDR DFS run for ddr3. + please make sure the cp1 is free. + +config SPRD_SCX35_DMC_FREQ_DEBUG + bool "Spreadtrum scx35 Memory Bus DMCFREQ Driver debug" + depends on SPRD_SCX35_DMC_FREQ + help + DMCFREQ driver for Spreadtrum scx35 memory bus debug. + +endif # PM_DEVFREQ diff --git a/drivers/devfreq/Makefile b/drivers/devfreq/Makefile new file mode 100644 index 00000000..9f512669 --- /dev/null +++ b/drivers/devfreq/Makefile @@ -0,0 +1,10 @@ +obj-$(CONFIG_PM_DEVFREQ) += devfreq.o +obj-$(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND) += governor_simpleondemand.o +obj-$(CONFIG_DEVFREQ_GOV_PERFORMANCE) += governor_performance.o +obj-$(CONFIG_DEVFREQ_GOV_POWERSAVE) += governor_powersave.o +obj-$(CONFIG_DEVFREQ_GOV_USERSPACE) += governor_userspace.o +obj-$(CONFIG_DEVFREQ_GOV_ONDEMAND) += governor_ondemand.o + +# DEVFREQ Drivers +obj-$(CONFIG_ARM_EXYNOS4_BUS_DEVFREQ) += exynos4_bus.o +obj-$(CONFIG_SPRD_SCX35_DMC_FREQ) += scx35_dmc.o diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c new file mode 100644 index 00000000..1ee69d93 --- /dev/null +++ b/drivers/devfreq/devfreq.c @@ -0,0 +1,1078 @@ +/* + * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework + * for Non-CPU Devices. + * + * Copyright (C) 2011 Samsung Electronics + * MyungJoo Ham <myungjoo.ham@samsung.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include <linux/kernel.h> +#include <linux/sched.h> +#include <linux/errno.h> +#include <linux/err.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/stat.h> +#include <linux/opp.h> +#include <linux/devfreq.h> +#include <linux/workqueue.h> +#include <linux/platform_device.h> +#include <linux/list.h> +#include <linux/printk.h> +#include <linux/hrtimer.h> +#include "governor.h" + +static struct class *devfreq_class; + +/* + * devfreq core provides delayed work based load monitoring helper + * functions. Governors can use these or can implement their own + * monitoring mechanism. + */ +static struct workqueue_struct *devfreq_wq; + +/* The list of all device-devfreq governors */ +static LIST_HEAD(devfreq_governor_list); +/* The list of all device-devfreq */ +static LIST_HEAD(devfreq_list); +static DEFINE_MUTEX(devfreq_list_lock); + +/** + * find_device_devfreq() - find devfreq struct using device pointer + * @dev: device pointer used to lookup device devfreq. + * + * Search the list of device devfreqs and return the matched device's + * devfreq info. devfreq_list_lock should be held by the caller. + */ +static struct devfreq *find_device_devfreq(struct device *dev) +{ + struct devfreq *tmp_devfreq; + + if (unlikely(IS_ERR_OR_NULL(dev))) { + pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); + return ERR_PTR(-EINVAL); + } + WARN(!mutex_is_locked(&devfreq_list_lock), + "devfreq_list_lock must be locked."); + + list_for_each_entry(tmp_devfreq, &devfreq_list, node) { + if (tmp_devfreq->dev.parent == dev) + return tmp_devfreq; + } + + return ERR_PTR(-ENODEV); +} + +/** + * devfreq_get_freq_level() - Lookup freq_table for the frequency + * @devfreq: the devfreq instance + * @freq: the target frequency + */ +static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq) +{ + int lev; + + for (lev = 0; lev < devfreq->profile->max_state; lev++) + if (freq == devfreq->profile->freq_table[lev]) + return lev; + + return -EINVAL; +} + +/** + * devfreq_update_status() - Update statistics of devfreq behavior + * @devfreq: the devfreq instance + * @freq: the update target frequency + */ +static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq) +{ + int lev, prev_lev; + unsigned long cur_time; + + lev = devfreq_get_freq_level(devfreq, freq); + if (lev < 0) + return lev; + + cur_time = jiffies; + devfreq->time_in_state[lev] += + cur_time - devfreq->last_stat_updated; + if (freq != devfreq->previous_freq) { + prev_lev = devfreq_get_freq_level(devfreq, + devfreq->previous_freq); + devfreq->trans_table[(prev_lev * + devfreq->profile->max_state) + lev]++; + devfreq->total_trans++; + } + devfreq->last_stat_updated = cur_time; + + return 0; +} + +/** + * find_devfreq_governor() - find devfreq governor from name + * @name: name of the governor + * + * Search the list of devfreq governors and return the matched + * governor's pointer. devfreq_list_lock should be held by the caller. + */ +static struct devfreq_governor *find_devfreq_governor(const char *name) +{ + struct devfreq_governor *tmp_governor; + + if (unlikely(IS_ERR_OR_NULL(name))) { + pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); + return ERR_PTR(-EINVAL); + } + WARN(!mutex_is_locked(&devfreq_list_lock), + "devfreq_list_lock must be locked."); + + list_for_each_entry(tmp_governor, &devfreq_governor_list, node) { + if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN)) + return tmp_governor; + } + + return ERR_PTR(-ENODEV); +} + +/* Load monitoring helper functions for governors use */ + +/** + * update_devfreq() - Reevaluate the device and configure frequency. + * @devfreq: the devfreq instance. + * + * Note: Lock devfreq->lock before calling update_devfreq + * This function is exported for governors. + */ +int update_devfreq(struct devfreq *devfreq) +{ + unsigned long freq; + int err = 0; + u32 flags = 0; + + if (!mutex_is_locked(&devfreq->lock)) { + WARN(true, "devfreq->lock must be locked by the caller.\n"); + return -EINVAL; + } + + if (!devfreq->governor) + return -EINVAL; + + /* Reevaluate the proper frequency */ + err = devfreq->governor->get_target_freq(devfreq, &freq); + if (err) + return err; + + /* + * Adjust the freuqency with user freq and QoS. + * + * List from the highest proiority + * max_freq (probably called by thermal when it's too hot) + * min_freq + */ + + if (devfreq->min_freq && freq < devfreq->min_freq) { + freq = devfreq->min_freq; + flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */ + } + if (devfreq->max_freq && freq > devfreq->max_freq) { + freq = devfreq->max_freq; + flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */ + } + + err = devfreq->profile->target(devfreq->dev.parent, &freq, flags); + if (err) + return err; + + if (devfreq->profile->freq_table) + if (devfreq_update_status(devfreq, freq)) + dev_err(&devfreq->dev, + "Couldn't update frequency transition information.\n"); + + devfreq->previous_freq = freq; + return err; +} +EXPORT_SYMBOL(update_devfreq); + +/** + * devfreq_monitor() - Periodically poll devfreq objects. + * @work: the work struct used to run devfreq_monitor periodically. + * + */ +static void devfreq_monitor(struct work_struct *work) +{ + int err; + struct devfreq *devfreq = container_of(work, + struct devfreq, work.work); + + mutex_lock(&devfreq->lock); + err = update_devfreq(devfreq); + if (err) + dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err); + + queue_delayed_work_on(0, devfreq_wq, &devfreq->work, + msecs_to_jiffies(devfreq->profile->polling_ms)); + mutex_unlock(&devfreq->lock); +} + +/** + * devfreq_monitor_start() - Start load monitoring of devfreq instance + * @devfreq: the devfreq instance. + * + * Helper function for starting devfreq device load monitoing. By + * default delayed work based monitoring is supported. Function + * to be called from governor in response to DEVFREQ_GOV_START + * event when device is added to devfreq framework. + */ +void devfreq_monitor_start(struct devfreq *devfreq) +{ + INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor); + if (devfreq->profile->polling_ms) + queue_delayed_work_on(0, devfreq_wq, &devfreq->work, + msecs_to_jiffies(devfreq->profile->polling_ms)); +} +EXPORT_SYMBOL(devfreq_monitor_start); + +/** + * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance + * @devfreq: the devfreq instance. + * + * Helper function to stop devfreq device load monitoing. Function + * to be called from governor in response to DEVFREQ_GOV_STOP + * event when device is removed from devfreq framework. + */ +void devfreq_monitor_stop(struct devfreq *devfreq) +{ + cancel_delayed_work_sync(&devfreq->work); +} +EXPORT_SYMBOL(devfreq_monitor_stop); + +/** + * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance + * @devfreq: the devfreq instance. + * + * Helper function to suspend devfreq device load monitoing. Function + * to be called from governor in response to DEVFREQ_GOV_SUSPEND + * event or when polling interval is set to zero. + * + * Note: Though this function is same as devfreq_monitor_stop(), + * intentionally kept separate to provide hooks for collecting + * transition statistics. + */ +void devfreq_monitor_suspend(struct devfreq *devfreq) +{ + mutex_lock(&devfreq->lock); + if (devfreq->stop_polling) { + mutex_unlock(&devfreq->lock); + return; + } + + devfreq->stop_polling = true; + mutex_unlock(&devfreq->lock); + cancel_delayed_work_sync(&devfreq->work); +} +EXPORT_SYMBOL(devfreq_monitor_suspend); + +/** + * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance + * @devfreq: the devfreq instance. + * + * Helper function to resume devfreq device load monitoing. Function + * to be called from governor in response to DEVFREQ_GOV_RESUME + * event or when polling interval is set to non-zero. + */ +void devfreq_monitor_resume(struct devfreq *devfreq) +{ + mutex_lock(&devfreq->lock); + if (!devfreq->stop_polling) + goto out; + + if (!delayed_work_pending(&devfreq->work) && + devfreq->profile->polling_ms) + queue_delayed_work_on(0, devfreq_wq, &devfreq->work, + msecs_to_jiffies(devfreq->profile->polling_ms)); + devfreq->stop_polling = false; + +out: + mutex_unlock(&devfreq->lock); +} +EXPORT_SYMBOL(devfreq_monitor_resume); + +/** + * devfreq_interval_update() - Update device devfreq monitoring interval + * @devfreq: the devfreq instance. + * @delay: new polling interval to be set. + * + * Helper function to set new load monitoring polling interval. Function + * to be called from governor in response to DEVFREQ_GOV_INTERVAL event. + */ +void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay) +{ + unsigned int cur_delay = devfreq->profile->polling_ms; + unsigned int new_delay = *delay; + + mutex_lock(&devfreq->lock); + devfreq->profile->polling_ms = new_delay; + + if (devfreq->stop_polling) + goto out; + + /* if new delay is zero, stop polling */ + if (!new_delay) { + mutex_unlock(&devfreq->lock); + cancel_delayed_work_sync(&devfreq->work); + return; + } + + /* if current delay is zero, start polling with new delay */ + if (!cur_delay) { + queue_delayed_work_on(0, devfreq_wq, &devfreq->work, + msecs_to_jiffies(devfreq->profile->polling_ms)); + goto out; + } + + /* if current delay is greater than new delay, restart polling */ + if (cur_delay > new_delay) { + mutex_unlock(&devfreq->lock); + cancel_delayed_work_sync(&devfreq->work); + mutex_lock(&devfreq->lock); + if (!devfreq->stop_polling) + queue_delayed_work_on(0, devfreq_wq, &devfreq->work, + msecs_to_jiffies(devfreq->profile->polling_ms)); + } +out: + mutex_unlock(&devfreq->lock); +} +EXPORT_SYMBOL(devfreq_interval_update); + +/** + * devfreq_notifier_call() - Notify that the device frequency requirements + * has been changed out of devfreq framework. + * @nb: the notifier_block (supposed to be devfreq->nb) + * @type: not used + * @devp: not used + * + * Called by a notifier that uses devfreq->nb. + */ +static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type, + void *devp) +{ + struct devfreq *devfreq = container_of(nb, struct devfreq, nb); + int ret; + + mutex_lock(&devfreq->lock); + ret = update_devfreq(devfreq); + mutex_unlock(&devfreq->lock); + + return ret; +} + +/** + * _remove_devfreq() - Remove devfreq from the list and release its resources. + * @devfreq: the devfreq struct + * @skip: skip calling device_unregister(). + */ +static void _remove_devfreq(struct devfreq *devfreq, bool skip) +{ + mutex_lock(&devfreq_list_lock); + if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) { + mutex_unlock(&devfreq_list_lock); + dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n"); + return; + } + list_del(&devfreq->node); + mutex_unlock(&devfreq_list_lock); + + if (devfreq->governor) + devfreq->governor->event_handler(devfreq, + DEVFREQ_GOV_STOP, NULL); + + if (devfreq->profile->exit) + devfreq->profile->exit(devfreq->dev.parent); + + if (!skip && get_device(&devfreq->dev)) { + device_unregister(&devfreq->dev); + put_device(&devfreq->dev); + } + + mutex_destroy(&devfreq->lock); + kfree(devfreq); +} + +/** + * devfreq_dev_release() - Callback for struct device to release the device. + * @dev: the devfreq device + * + * This calls _remove_devfreq() if _remove_devfreq() is not called. + * Note that devfreq_dev_release() could be called by _remove_devfreq() as + * well as by others unregistering the device. + */ +static void devfreq_dev_release(struct device *dev) +{ + struct devfreq *devfreq = to_devfreq(dev); + + _remove_devfreq(devfreq, true); +} + +/** + * devfreq_add_device() - Add devfreq feature to the device + * @dev: the device to add devfreq feature. + * @profile: device-specific profile to run devfreq. + * @governor_name: name of the policy to choose frequency. + * @data: private data for the governor. The devfreq framework does not + * touch this value. + */ +struct devfreq *devfreq_add_device(struct device *dev, + struct devfreq_dev_profile *profile, + const char *governor_name, + void *data) +{ + struct devfreq *devfreq; + struct devfreq_governor *governor; + int err = 0; + + if (!dev || !profile || !governor_name) { + dev_err(dev, "%s: Invalid parameters.\n", __func__); + return ERR_PTR(-EINVAL); + } + + mutex_lock(&devfreq_list_lock); + devfreq = find_device_devfreq(dev); + mutex_unlock(&devfreq_list_lock); + if (!IS_ERR(devfreq)) { + dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__); + err = -EINVAL; + goto err_out; + } + + devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); + if (!devfreq) { + dev_err(dev, "%s: Unable to create devfreq for the device\n", + __func__); + err = -ENOMEM; + goto err_out; + } + + mutex_init(&devfreq->lock); + mutex_lock(&devfreq->lock); + devfreq->dev.parent = dev; + devfreq->dev.class = devfreq_class; + devfreq->dev.release = devfreq_dev_release; + devfreq->profile = profile; + strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN); + devfreq->previous_freq = profile->initial_freq; + devfreq->data = data; + devfreq->nb.notifier_call = devfreq_notifier_call; + + devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) * + devfreq->profile->max_state * + devfreq->profile->max_state, + GFP_KERNEL); + devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) * + devfreq->profile->max_state, + GFP_KERNEL); + devfreq->last_stat_updated = jiffies; + + dev_set_name(&devfreq->dev, dev_name(dev)); + err = device_register(&devfreq->dev); + if (err) { + put_device(&devfreq->dev); + mutex_unlock(&devfreq->lock); + goto err_dev; + } + + mutex_unlock(&devfreq->lock); + + mutex_lock(&devfreq_list_lock); + list_add(&devfreq->node, &devfreq_list); + + governor = find_devfreq_governor(devfreq->governor_name); + if (!IS_ERR(governor)) + devfreq->governor = governor; + if (devfreq->governor) + err = devfreq->governor->event_handler(devfreq, + DEVFREQ_GOV_START, NULL); + mutex_unlock(&devfreq_list_lock); + if (err) { + dev_err(dev, "%s: Unable to start governor for the device\n", + __func__); + goto err_init; + } + + return devfreq; + +err_init: + list_del(&devfreq->node); + device_unregister(&devfreq->dev); +err_dev: + kfree(devfreq); +err_out: + return ERR_PTR(err); +} +EXPORT_SYMBOL(devfreq_add_device); + +/** + * devfreq_remove_device() - Remove devfreq feature from a device. + * @devfreq: the devfreq instance to be removed + */ +int devfreq_remove_device(struct devfreq *devfreq) +{ + if (!devfreq) + return -EINVAL; + + _remove_devfreq(devfreq, false); + + return 0; +} +EXPORT_SYMBOL(devfreq_remove_device); + +/** + * devfreq_suspend_device() - Suspend devfreq of a device. + * @devfreq: the devfreq instance to be suspended + */ +int devfreq_suspend_device(struct devfreq *devfreq) +{ + if (!devfreq) + return -EINVAL; + + if (!devfreq->governor) + return 0; + + return devfreq->governor->event_handler(devfreq, + DEVFREQ_GOV_SUSPEND, NULL); +} +EXPORT_SYMBOL(devfreq_suspend_device); + +/** + * devfreq_resume_device() - Resume devfreq of a device. + * @devfreq: the devfreq instance to be resumed + */ +int devfreq_resume_device(struct devfreq *devfreq) +{ + if (!devfreq) + return -EINVAL; + + if (!devfreq->governor) + return 0; + + return devfreq->governor->event_handler(devfreq, + DEVFREQ_GOV_RESUME, NULL); +} +EXPORT_SYMBOL(devfreq_resume_device); + +/** + * devfreq_add_governor() - Add devfreq governor + * @governor: the devfreq governor to be added + */ +int devfreq_add_governor(struct devfreq_governor *governor) +{ + struct devfreq_governor *g; + struct devfreq *devfreq; + int err = 0; + + if (!governor) { + pr_err("%s: Invalid parameters.\n", __func__); + return -EINVAL; + } + + mutex_lock(&devfreq_list_lock); + g = find_devfreq_governor(governor->name); + if (!IS_ERR(g)) { + pr_err("%s: governor %s already registered\n", __func__, + g->name); + err = -EINVAL; + goto err_out; + } + + list_add(&governor->node, &devfreq_governor_list); + + list_for_each_entry(devfreq, &devfreq_list, node) { + int ret = 0; + struct device *dev = devfreq->dev.parent; + + if (!strncmp(devfreq->governor_name, governor->name, + DEVFREQ_NAME_LEN)) { + /* The following should never occur */ + if (devfreq->governor) { + dev_warn(dev, + "%s: Governor %s already present\n", + __func__, devfreq->governor->name); + ret = devfreq->governor->event_handler(devfreq, + DEVFREQ_GOV_STOP, NULL); + if (ret) { + dev_warn(dev, + "%s: Governor %s stop = %d\n", + __func__, + devfreq->governor->name, ret); + } + /* Fall through */ + } + devfreq->governor = governor; + ret = devfreq->governor->event_handler(devfreq, + DEVFREQ_GOV_START, NULL); + if (ret) { + dev_warn(dev, "%s: Governor %s start=%d\n", + __func__, devfreq->governor->name, + ret); + } + } + } + +err_out: + mutex_unlock(&devfreq_list_lock); + + return err; +} +EXPORT_SYMBOL(devfreq_add_governor); + +/** + * devfreq_remove_device() - Remove devfreq feature from a device. + * @governor: the devfreq governor to be removed + */ +int devfreq_remove_governor(struct devfreq_governor *governor) +{ + struct devfreq_governor *g; + struct devfreq *devfreq; + int err = 0; + + if (!governor) { + pr_err("%s: Invalid parameters.\n", __func__); + return -EINVAL; + } + + mutex_lock(&devfreq_list_lock); + g = find_devfreq_governor(governor->name); + if (IS_ERR(g)) { + pr_err("%s: governor %s not registered\n", __func__, + governor->name); + err = PTR_ERR(g); + goto err_out; + } + list_for_each_entry(devfreq, &devfreq_list, node) { + int ret; + struct device *dev = devfreq->dev.parent; + + if (!strncmp(devfreq->governor_name, governor->name, + DEVFREQ_NAME_LEN)) { + /* we should have a devfreq governor! */ + if (!devfreq->governor) { + dev_warn(dev, "%s: Governor %s NOT present\n", + __func__, governor->name); + continue; + /* Fall through */ + } + ret = devfreq->governor->event_handler(devfreq, + DEVFREQ_GOV_STOP, NULL); + if (ret) { + dev_warn(dev, "%s: Governor %s stop=%d\n", + __func__, devfreq->governor->name, + ret); + } + devfreq->governor = NULL; + } + } + + list_del(&governor->node); +err_out: + mutex_unlock(&devfreq_list_lock); + + return err; +} +EXPORT_SYMBOL(devfreq_remove_governor); + +static ssize_t show_governor(struct device *dev, + struct device_attribute *attr, char *buf) +{ + if (!to_devfreq(dev)->governor) + return -EINVAL; + + return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name); +} + +static ssize_t store_governor(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *df = to_devfreq(dev); + int ret; + char str_governor[DEVFREQ_NAME_LEN + 1]; + struct devfreq_governor *governor; + + ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor); + if (ret != 1) + return -EINVAL; + + mutex_lock(&devfreq_list_lock); + governor = find_devfreq_governor(str_governor); + if (IS_ERR(governor)) { + ret = PTR_ERR(governor); + goto out; + } + if (df->governor == governor) + goto out; + + if (df->governor) { + ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL); + if (ret) { + dev_warn(dev, "%s: Governor %s not stopped(%d)\n", + __func__, df->governor->name, ret); + goto out; + } + } + df->governor = governor; + strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN); + ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); + if (ret) + dev_warn(dev, "%s: Governor %s not started(%d)\n", + __func__, df->governor->name, ret); +out: + mutex_unlock(&devfreq_list_lock); + + if (!ret) + ret = count; + return ret; +} +static ssize_t show_available_governors(struct device *d, + struct device_attribute *attr, + char *buf) +{ + struct devfreq_governor *tmp_governor; + ssize_t count = 0; + + mutex_lock(&devfreq_list_lock); + list_for_each_entry(tmp_governor, &devfreq_governor_list, node) + count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), + "%s ", tmp_governor->name); + mutex_unlock(&devfreq_list_lock); + + /* Truncate the trailing space */ + if (count) + count--; + + count += sprintf(&buf[count], "\n"); + + return count; +} + +static ssize_t show_freq(struct device *dev, + struct device_attribute *attr, char *buf) +{ + unsigned long freq; + struct devfreq *devfreq = to_devfreq(dev); + + if (devfreq->profile->get_cur_freq && + !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq)) + return sprintf(buf, "%lu\n", freq); + + return sprintf(buf, "%lu\n", devfreq->previous_freq); +} + +static ssize_t show_target_freq(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq); +} + +static ssize_t show_polling_interval(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms); +} + +static ssize_t store_polling_interval(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *df = to_devfreq(dev); + unsigned int value; + int ret; + + if (!df->governor) + return -EINVAL; + + ret = sscanf(buf, "%u", &value); + if (ret != 1) + return -EINVAL; + + df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value); + ret = count; + + return ret; +} + +static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *df = to_devfreq(dev); + unsigned long value; + int ret; + unsigned long max; + + ret = sscanf(buf, "%lu", &value); + if (ret != 1) + return -EINVAL; + + mutex_lock(&df->lock); + max = df->max_freq; + if (value && max && value > max) { + ret = -EINVAL; + goto unlock; + } + + df->min_freq = value; + update_devfreq(df); + ret = count; +unlock: + mutex_unlock(&df->lock); + return ret; +} + +static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr, + char *buf) +{ + return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq); +} + +static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *df = to_devfreq(dev); + unsigned long value; + int ret; + unsigned long min; + + ret = sscanf(buf, "%lu", &value); + if (ret != 1) + return -EINVAL; + + mutex_lock(&df->lock); + min = df->min_freq; + if (value && min && value < min) { + ret = -EINVAL; + goto unlock; + } + + df->max_freq = value; + update_devfreq(df); + ret = count; +unlock: + mutex_unlock(&df->lock); + return ret; +} + +static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr, + char *buf) +{ + return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq); +} + +static ssize_t show_available_freqs(struct device *d, + struct device_attribute *attr, + char *buf) +{ + struct devfreq *df = to_devfreq(d); + struct device *dev = df->dev.parent; + struct opp *opp; + ssize_t count = 0; + unsigned long freq = 0; + + rcu_read_lock(); + do { + opp = opp_find_freq_ceil(dev, &freq); + if (IS_ERR(opp)) + break; + + count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), + "%lu ", freq); + freq++; + } while (1); + rcu_read_unlock(); + + /* Truncate the trailing space */ + if (count) + count--; + + count += sprintf(&buf[count], "\n"); + + return count; +} + +static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct devfreq *devfreq = to_devfreq(dev); + ssize_t len; + int i, j, err; + unsigned int max_state = devfreq->profile->max_state; + + err = devfreq_update_status(devfreq, devfreq->previous_freq); + if (err) + return 0; + + len = sprintf(buf, " From : To\n"); + len += sprintf(buf + len, " :"); + for (i = 0; i < max_state; i++) + len += sprintf(buf + len, "%8u", + devfreq->profile->freq_table[i]); + + len += sprintf(buf + len, " time(ms)\n"); + + for (i = 0; i < max_state; i++) { + if (devfreq->profile->freq_table[i] + == devfreq->previous_freq) { + len += sprintf(buf + len, "*"); + } else { + len += sprintf(buf + len, " "); + } + len += sprintf(buf + len, "%8u:", + devfreq->profile->freq_table[i]); + for (j = 0; j < max_state; j++) + len += sprintf(buf + len, "%8u", + devfreq->trans_table[(i * max_state) + j]); + len += sprintf(buf + len, "%10u\n", + jiffies_to_msecs(devfreq->time_in_state[i])); + } + + len += sprintf(buf + len, "Total transition : %u\n", + devfreq->total_trans); + return len; +} + +static struct device_attribute devfreq_attrs[] = { + __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor), + __ATTR(available_governors, S_IRUGO, show_available_governors, NULL), + __ATTR(cur_freq, S_IRUGO, show_freq, NULL), + __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL), + __ATTR(target_freq, S_IRUGO, show_target_freq, NULL), + __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval, + store_polling_interval), + __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq), + __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq), + __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL), + { }, +}; + +static int __init devfreq_init(void) +{ + devfreq_class = class_create(THIS_MODULE, "devfreq"); + if (IS_ERR(devfreq_class)) { + pr_err("%s: couldn't create class\n", __FILE__); + return PTR_ERR(devfreq_class); + } + + devfreq_wq = alloc_workqueue(("devfreq_wq"), WQ_FREEZABLE | WQ_MEM_RECLAIM, 1); + if (IS_ERR(devfreq_wq)) { + class_destroy(devfreq_class); + pr_err("%s: couldn't create workqueue\n", __FILE__); + return PTR_ERR(devfreq_wq); + } + devfreq_class->dev_attrs = devfreq_attrs; + + return 0; +} +subsys_initcall(devfreq_init); + +static void __exit devfreq_exit(void) +{ + class_destroy(devfreq_class); + destroy_workqueue(devfreq_wq); +} +module_exit(devfreq_exit); + +/* + * The followings are helper functions for devfreq user device drivers with + * OPP framework. + */ + +/** + * devfreq_recommended_opp() - Helper function to get proper OPP for the + * freq value given to target callback. + * @dev: The devfreq user device. (parent of devfreq) + * @freq: The frequency given to target function + * @flags: Flags handed from devfreq framework. + * + * Locking: This function must be called under rcu_read_lock(). opp is a rcu + * protected pointer. The reason for the same is that the opp pointer which is + * returned will remain valid for use with opp_get_{voltage, freq} only while + * under the locked area. The pointer returned must be used prior to unlocking + * with rcu_read_unlock() to maintain the integrity of the pointer. + */ +struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq, + u32 flags) +{ + struct opp *opp; + + if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) { + /* The freq is an upper bound. opp should be lower */ + opp = opp_find_freq_floor(dev, freq); + + /* If not available, use the closest opp */ + if (opp == ERR_PTR(-ERANGE)) + opp = opp_find_freq_ceil(dev, freq); + } else { + /* The freq is an lower bound. opp should be higher */ + opp = opp_find_freq_ceil(dev, freq); + + /* If not available, use the closest opp */ + if (opp == ERR_PTR(-ERANGE)) + opp = opp_find_freq_floor(dev, freq); + } + + return opp; +} + +/** + * devfreq_register_opp_notifier() - Helper function to get devfreq notified + * for any changes in the OPP availability + * changes + * @dev: The devfreq user device. (parent of devfreq) + * @devfreq: The devfreq object. + */ +int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) +{ + struct srcu_notifier_head *nh; + int ret = 0; + + rcu_read_lock(); + nh = opp_get_notifier(dev); + if (IS_ERR(nh)) + ret = PTR_ERR(nh); + rcu_read_unlock(); + if (!ret) + ret = srcu_notifier_chain_register(nh, &devfreq->nb); + + return ret; +} + +/** + * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq + * notified for any changes in the OPP + * availability changes anymore. + * @dev: The devfreq user device. (parent of devfreq) + * @devfreq: The devfreq object. + * + * At exit() callback of devfreq_dev_profile, this must be included if + * devfreq_recommended_opp is used. + */ +int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) +{ + struct srcu_notifier_head *nh; + int ret = 0; + + rcu_read_lock(); + nh = opp_get_notifier(dev); + if (IS_ERR(nh)) + ret = PTR_ERR(nh); + rcu_read_unlock(); + if (!ret) + ret = srcu_notifier_chain_unregister(nh, &devfreq->nb); + + return ret; +} + +MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); +MODULE_DESCRIPTION("devfreq class support"); +MODULE_LICENSE("GPL"); diff --git a/drivers/devfreq/exynos4_bus.c b/drivers/devfreq/exynos4_bus.c new file mode 100644 index 00000000..3f37f3b3 --- /dev/null +++ b/drivers/devfreq/exynos4_bus.c @@ -0,0 +1,1153 @@ +/* drivers/devfreq/exynos4210_memorybus.c + * + * Copyright (c) 2011 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + * MyungJoo Ham <myungjoo.ham@samsung.com> + * + * EXYNOS4 - Memory/Bus clock frequency scaling support in DEVFREQ framework + * This version supports EXYNOS4210 only. This changes bus frequencies + * and vddint voltages. Exynos4412/4212 should be able to be supported + * with minor modifications. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +#include <linux/io.h> +#include <linux/slab.h> +#include <linux/mutex.h> +#include <linux/suspend.h> +#include <linux/opp.h> +#include <linux/devfreq.h> +#include <linux/platform_device.h> +#include <linux/regulator/consumer.h> +#include <linux/module.h> + +/* Exynos4 ASV has been in the mailing list, but not upstreamed, yet. */ +#ifdef CONFIG_EXYNOS_ASV +extern unsigned int exynos_result_of_asv; +#endif + +#include <mach/regs-clock.h> + +#include <plat/map-s5p.h> + +#define MAX_SAFEVOLT 1200000 /* 1.2V */ + +enum exynos4_busf_type { + TYPE_BUSF_EXYNOS4210, + TYPE_BUSF_EXYNOS4x12, +}; + +/* Assume that the bus is saturated if the utilization is 40% */ +#define BUS_SATURATION_RATIO 40 + +enum ppmu_counter { + PPMU_PMNCNT0 = 0, + PPMU_PMCCNT1, + PPMU_PMNCNT2, + PPMU_PMNCNT3, + PPMU_PMNCNT_MAX, +}; +struct exynos4_ppmu { + void __iomem *hw_base; + unsigned int ccnt; + unsigned int event; + unsigned int count[PPMU_PMNCNT_MAX]; + bool ccnt_overflow; + bool count_overflow[PPMU_PMNCNT_MAX]; +}; + +enum busclk_level_idx { + LV_0 = 0, + LV_1, + LV_2, + LV_3, + LV_4, + _LV_END +}; +#define EX4210_LV_MAX LV_2 +#define EX4x12_LV_MAX LV_4 +#define EX4210_LV_NUM (LV_2 + 1) +#define EX4x12_LV_NUM (LV_4 + 1) + +/** + * struct busfreq_opp_info - opp information for bus + * @rate: Frequency in hertz + * @volt: Voltage in microvolts corresponding to this OPP + */ +struct busfreq_opp_info { + unsigned long rate; + unsigned long volt; +}; + +struct busfreq_data { + enum exynos4_busf_type type; + struct device *dev; + struct devfreq *devfreq; + bool disabled; + struct regulator *vdd_int; + struct regulator *vdd_mif; /* Exynos4412/4212 only */ + struct busfreq_opp_info curr_oppinfo; + struct exynos4_ppmu dmc[2]; + + struct notifier_block pm_notifier; + struct mutex lock; + + /* Dividers calculated at boot/probe-time */ + unsigned int dmc_divtable[_LV_END]; /* DMC0 */ + unsigned int top_divtable[_LV_END]; +}; + +struct bus_opp_table { + unsigned int idx; + unsigned long clk; + unsigned long volt; +}; + +/* 4210 controls clock of mif and voltage of int */ +static struct bus_opp_table exynos4210_busclk_table[] = { + {LV_0, 400000, 1150000}, + {LV_1, 267000, 1050000}, + {LV_2, 133000, 1025000}, + {0, 0, 0}, +}; + +/* + * MIF is the main control knob clock for exynox4x12 MIF/INT + * clock and voltage of both mif/int are controlled. + */ +static struct bus_opp_table exynos4x12_mifclk_table[] = { + {LV_0, 400000, 1100000}, + {LV_1, 267000, 1000000}, + {LV_2, 160000, 950000}, + {LV_3, 133000, 950000}, + {LV_4, 100000, 950000}, + {0, 0, 0}, +}; + +/* + * INT is not the control knob of 4x12. LV_x is not meant to represent + * the current performance. (MIF does) + */ +static struct bus_opp_table exynos4x12_intclk_table[] = { + {LV_0, 200000, 1000000}, + {LV_1, 160000, 950000}, + {LV_2, 133000, 925000}, + {LV_3, 100000, 900000}, + {0, 0, 0}, +}; + +/* TODO: asv volt definitions are "__initdata"? */ +/* Some chips have different operating voltages */ +static unsigned int exynos4210_asv_volt[][EX4210_LV_NUM] = { + {1150000, 1050000, 1050000}, + {1125000, 1025000, 1025000}, + {1100000, 1000000, 1000000}, + {1075000, 975000, 975000}, + {1050000, 950000, 950000}, +}; + +static unsigned int exynos4x12_mif_step_50[][EX4x12_LV_NUM] = { + /* 400 267 160 133 100 */ + {1050000, 950000, 900000, 900000, 900000}, /* ASV0 */ + {1050000, 950000, 900000, 900000, 900000}, /* ASV1 */ + {1050000, 950000, 900000, 900000, 900000}, /* ASV2 */ + {1050000, 900000, 900000, 900000, 900000}, /* ASV3 */ + {1050000, 900000, 900000, 900000, 850000}, /* ASV4 */ + {1050000, 900000, 900000, 850000, 850000}, /* ASV5 */ + {1050000, 900000, 850000, 850000, 850000}, /* ASV6 */ + {1050000, 900000, 850000, 850000, 850000}, /* ASV7 */ + {1050000, 900000, 850000, 850000, 850000}, /* ASV8 */ +}; + +static unsigned int exynos4x12_int_volt[][EX4x12_LV_NUM] = { + /* 200 160 133 100 */ + {1000000, 950000, 925000, 900000}, /* ASV0 */ + {975000, 925000, 925000, 900000}, /* ASV1 */ + {950000, 925000, 900000, 875000}, /* ASV2 */ + {950000, 900000, 900000, 875000}, /* ASV3 */ + {925000, 875000, 875000, 875000}, /* ASV4 */ + {900000, 850000, 850000, 850000}, /* ASV5 */ + {900000, 850000, 850000, 850000}, /* ASV6 */ + {900000, 850000, 850000, 850000}, /* ASV7 */ + {900000, 850000, 850000, 850000}, /* ASV8 */ +}; + +/*** Clock Divider Data for Exynos4210 ***/ +static unsigned int exynos4210_clkdiv_dmc0[][8] = { + /* + * Clock divider value for following + * { DIVACP, DIVACP_PCLK, DIVDPHY, DIVDMC, DIVDMCD + * DIVDMCP, DIVCOPY2, DIVCORE_TIMERS } + */ + + /* DMC L0: 400MHz */ + { 3, 1, 1, 1, 1, 1, 3, 1 }, + /* DMC L1: 266.7MHz */ + { 4, 1, 1, 2, 1, 1, 3, 1 }, + /* DMC L2: 133MHz */ + { 5, 1, 1, 5, 1, 1, 3, 1 }, +}; +static unsigned int exynos4210_clkdiv_top[][5] = { + /* + * Clock divider value for following + * { DIVACLK200, DIVACLK100, DIVACLK160, DIVACLK133, DIVONENAND } + */ + /* ACLK200 L0: 200MHz */ + { 3, 7, 4, 5, 1 }, + /* ACLK200 L1: 160MHz */ + { 4, 7, 5, 6, 1 }, + /* ACLK200 L2: 133MHz */ + { 5, 7, 7, 7, 1 }, +}; +static unsigned int exynos4210_clkdiv_lr_bus[][2] = { + /* + * Clock divider value for following + * { DIVGDL/R, DIVGPL/R } + */ + /* ACLK_GDL/R L1: 200MHz */ + { 3, 1 }, + /* ACLK_GDL/R L2: 160MHz */ + { 4, 1 }, + /* ACLK_GDL/R L3: 133MHz */ + { 5, 1 }, +}; + +/*** Clock Divider Data for Exynos4212/4412 ***/ +static unsigned int exynos4x12_clkdiv_dmc0[][6] = { + /* + * Clock divider value for following + * { DIVACP, DIVACP_PCLK, DIVDPHY, DIVDMC, DIVDMCD + * DIVDMCP} + */ + + /* DMC L0: 400MHz */ + {3, 1, 1, 1, 1, 1}, + /* DMC L1: 266.7MHz */ + {4, 1, 1, 2, 1, 1}, + /* DMC L2: 160MHz */ + {5, 1, 1, 4, 1, 1}, + /* DMC L3: 133MHz */ + {5, 1, 1, 5, 1, 1}, + /* DMC L4: 100MHz */ + {7, 1, 1, 7, 1, 1}, +}; +static unsigned int exynos4x12_clkdiv_dmc1[][6] = { + /* + * Clock divider value for following + * { G2DACP, DIVC2C, DIVC2C_ACLK } + */ + + /* DMC L0: 400MHz */ + {3, 1, 1}, + /* DMC L1: 266.7MHz */ + {4, 2, 1}, + /* DMC L2: 160MHz */ + {5, 4, 1}, + /* DMC L3: 133MHz */ + {5, 5, 1}, + /* DMC L4: 100MHz */ + {7, 7, 1}, +}; +static unsigned int exynos4x12_clkdiv_top[][5] = { + /* + * Clock divider value for following + * { DIVACLK266_GPS, DIVACLK100, DIVACLK160, + DIVACLK133, DIVONENAND } + */ + + /* ACLK_GDL/R L0: 200MHz */ + {2, 7, 4, 5, 1}, + /* ACLK_GDL/R L1: 200MHz */ + {2, 7, 4, 5, 1}, + /* ACLK_GDL/R L2: 160MHz */ + {4, 7, 5, 7, 1}, + /* ACLK_GDL/R L3: 133MHz */ + {4, 7, 5, 7, 1}, + /* ACLK_GDL/R L4: 100MHz */ + {7, 7, 7, 7, 1}, +}; +static unsigned int exynos4x12_clkdiv_lr_bus[][2] = { + /* + * Clock divider value for following + * { DIVGDL/R, DIVGPL/R } + */ + + /* ACLK_GDL/R L0: 200MHz */ + {3, 1}, + /* ACLK_GDL/R L1: 200MHz */ + {3, 1}, + /* ACLK_GDL/R L2: 160MHz */ + {4, 1}, + /* ACLK_GDL/R L3: 133MHz */ + {5, 1}, + /* ACLK_GDL/R L4: 100MHz */ + {7, 1}, +}; +static unsigned int exynos4x12_clkdiv_sclkip[][3] = { + /* + * Clock divider value for following + * { DIVMFC, DIVJPEG, DIVFIMC0~3} + */ + + /* SCLK_MFC: 200MHz */ + {3, 3, 4}, + /* SCLK_MFC: 200MHz */ + {3, 3, 4}, + /* SCLK_MFC: 160MHz */ + {4, 4, 5}, + /* SCLK_MFC: 133MHz */ + {5, 5, 5}, + /* SCLK_MFC: 100MHz */ + {7, 7, 7}, +}; + + +static int exynos4210_set_busclk(struct busfreq_data *data, + struct busfreq_opp_info *oppi) +{ + unsigned int index; + unsigned int tmp; + + for (index = LV_0; index < EX4210_LV_NUM; index++) + if (oppi->rate == exynos4210_busclk_table[index].clk) + break; + + if (index == EX4210_LV_NUM) + return -EINVAL; + + /* Change Divider - DMC0 */ + tmp = data->dmc_divtable[index]; + + __raw_writel(tmp, EXYNOS4_CLKDIV_DMC0); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_DMC0); + } while (tmp & 0x11111111); + + /* Change Divider - TOP */ + tmp = data->top_divtable[index]; + + __raw_writel(tmp, EXYNOS4_CLKDIV_TOP); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_TOP); + } while (tmp & 0x11111); + + /* Change Divider - LEFTBUS */ + tmp = __raw_readl(EXYNOS4_CLKDIV_LEFTBUS); + + tmp &= ~(EXYNOS4_CLKDIV_BUS_GDLR_MASK | EXYNOS4_CLKDIV_BUS_GPLR_MASK); + + tmp |= ((exynos4210_clkdiv_lr_bus[index][0] << + EXYNOS4_CLKDIV_BUS_GDLR_SHIFT) | + (exynos4210_clkdiv_lr_bus[index][1] << + EXYNOS4_CLKDIV_BUS_GPLR_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_LEFTBUS); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_LEFTBUS); + } while (tmp & 0x11); + + /* Change Divider - RIGHTBUS */ + tmp = __raw_readl(EXYNOS4_CLKDIV_RIGHTBUS); + + tmp &= ~(EXYNOS4_CLKDIV_BUS_GDLR_MASK | EXYNOS4_CLKDIV_BUS_GPLR_MASK); + + tmp |= ((exynos4210_clkdiv_lr_bus[index][0] << + EXYNOS4_CLKDIV_BUS_GDLR_SHIFT) | + (exynos4210_clkdiv_lr_bus[index][1] << + EXYNOS4_CLKDIV_BUS_GPLR_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_RIGHTBUS); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_RIGHTBUS); + } while (tmp & 0x11); + + return 0; +} + +static int exynos4x12_set_busclk(struct busfreq_data *data, + struct busfreq_opp_info *oppi) +{ + unsigned int index; + unsigned int tmp; + + for (index = LV_0; index < EX4x12_LV_NUM; index++) + if (oppi->rate == exynos4x12_mifclk_table[index].clk) + break; + + if (index == EX4x12_LV_NUM) + return -EINVAL; + + /* Change Divider - DMC0 */ + tmp = data->dmc_divtable[index]; + + __raw_writel(tmp, EXYNOS4_CLKDIV_DMC0); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_DMC0); + } while (tmp & 0x11111111); + + /* Change Divider - DMC1 */ + tmp = __raw_readl(EXYNOS4_CLKDIV_DMC1); + + tmp &= ~(EXYNOS4_CLKDIV_DMC1_G2D_ACP_MASK | + EXYNOS4_CLKDIV_DMC1_C2C_MASK | + EXYNOS4_CLKDIV_DMC1_C2CACLK_MASK); + + tmp |= ((exynos4x12_clkdiv_dmc1[index][0] << + EXYNOS4_CLKDIV_DMC1_G2D_ACP_SHIFT) | + (exynos4x12_clkdiv_dmc1[index][1] << + EXYNOS4_CLKDIV_DMC1_C2C_SHIFT) | + (exynos4x12_clkdiv_dmc1[index][2] << + EXYNOS4_CLKDIV_DMC1_C2CACLK_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_DMC1); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_DMC1); + } while (tmp & 0x111111); + + /* Change Divider - TOP */ + tmp = __raw_readl(EXYNOS4_CLKDIV_TOP); + + tmp &= ~(EXYNOS4_CLKDIV_TOP_ACLK266_GPS_MASK | + EXYNOS4_CLKDIV_TOP_ACLK100_MASK | + EXYNOS4_CLKDIV_TOP_ACLK160_MASK | + EXYNOS4_CLKDIV_TOP_ACLK133_MASK | + EXYNOS4_CLKDIV_TOP_ONENAND_MASK); + + tmp |= ((exynos4x12_clkdiv_top[index][0] << + EXYNOS4_CLKDIV_TOP_ACLK266_GPS_SHIFT) | + (exynos4x12_clkdiv_top[index][1] << + EXYNOS4_CLKDIV_TOP_ACLK100_SHIFT) | + (exynos4x12_clkdiv_top[index][2] << + EXYNOS4_CLKDIV_TOP_ACLK160_SHIFT) | + (exynos4x12_clkdiv_top[index][3] << + EXYNOS4_CLKDIV_TOP_ACLK133_SHIFT) | + (exynos4x12_clkdiv_top[index][4] << + EXYNOS4_CLKDIV_TOP_ONENAND_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_TOP); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_TOP); + } while (tmp & 0x11111); + + /* Change Divider - LEFTBUS */ + tmp = __raw_readl(EXYNOS4_CLKDIV_LEFTBUS); + + tmp &= ~(EXYNOS4_CLKDIV_BUS_GDLR_MASK | EXYNOS4_CLKDIV_BUS_GPLR_MASK); + + tmp |= ((exynos4x12_clkdiv_lr_bus[index][0] << + EXYNOS4_CLKDIV_BUS_GDLR_SHIFT) | + (exynos4x12_clkdiv_lr_bus[index][1] << + EXYNOS4_CLKDIV_BUS_GPLR_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_LEFTBUS); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_LEFTBUS); + } while (tmp & 0x11); + + /* Change Divider - RIGHTBUS */ + tmp = __raw_readl(EXYNOS4_CLKDIV_RIGHTBUS); + + tmp &= ~(EXYNOS4_CLKDIV_BUS_GDLR_MASK | EXYNOS4_CLKDIV_BUS_GPLR_MASK); + + tmp |= ((exynos4x12_clkdiv_lr_bus[index][0] << + EXYNOS4_CLKDIV_BUS_GDLR_SHIFT) | + (exynos4x12_clkdiv_lr_bus[index][1] << + EXYNOS4_CLKDIV_BUS_GPLR_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_RIGHTBUS); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_RIGHTBUS); + } while (tmp & 0x11); + + /* Change Divider - MFC */ + tmp = __raw_readl(EXYNOS4_CLKDIV_MFC); + + tmp &= ~(EXYNOS4_CLKDIV_MFC_MASK); + + tmp |= ((exynos4x12_clkdiv_sclkip[index][0] << + EXYNOS4_CLKDIV_MFC_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_MFC); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_MFC); + } while (tmp & 0x1); + + /* Change Divider - JPEG */ + tmp = __raw_readl(EXYNOS4_CLKDIV_CAM1); + + tmp &= ~(EXYNOS4_CLKDIV_CAM1_JPEG_MASK); + + tmp |= ((exynos4x12_clkdiv_sclkip[index][1] << + EXYNOS4_CLKDIV_CAM1_JPEG_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_CAM1); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_CAM1); + } while (tmp & 0x1); + + /* Change Divider - FIMC0~3 */ + tmp = __raw_readl(EXYNOS4_CLKDIV_CAM); + + tmp &= ~(EXYNOS4_CLKDIV_CAM_FIMC0_MASK | EXYNOS4_CLKDIV_CAM_FIMC1_MASK | + EXYNOS4_CLKDIV_CAM_FIMC2_MASK | EXYNOS4_CLKDIV_CAM_FIMC3_MASK); + + tmp |= ((exynos4x12_clkdiv_sclkip[index][2] << + EXYNOS4_CLKDIV_CAM_FIMC0_SHIFT) | + (exynos4x12_clkdiv_sclkip[index][2] << + EXYNOS4_CLKDIV_CAM_FIMC1_SHIFT) | + (exynos4x12_clkdiv_sclkip[index][2] << + EXYNOS4_CLKDIV_CAM_FIMC2_SHIFT) | + (exynos4x12_clkdiv_sclkip[index][2] << + EXYNOS4_CLKDIV_CAM_FIMC3_SHIFT)); + + __raw_writel(tmp, EXYNOS4_CLKDIV_CAM); + + do { + tmp = __raw_readl(EXYNOS4_CLKDIV_STAT_CAM1); + } while (tmp & 0x1111); + + return 0; +} + + +static void busfreq_mon_reset(struct busfreq_data *data) +{ + unsigned int i; + + for (i = 0; i < 2; i++) { + void __iomem *ppmu_base = data->dmc[i].hw_base; + + /* Reset PPMU */ + __raw_writel(0x8000000f, ppmu_base + 0xf010); + __raw_writel(0x8000000f, ppmu_base + 0xf050); + __raw_writel(0x6, ppmu_base + 0xf000); + __raw_writel(0x0, ppmu_base + 0xf100); + + /* Set PPMU Event */ + data->dmc[i].event = 0x6; + __raw_writel(((data->dmc[i].event << 12) | 0x1), + ppmu_base + 0xfc); + + /* Start PPMU */ + __raw_writel(0x1, ppmu_base + 0xf000); + } +} + +static void exynos4_read_ppmu(struct busfreq_data *data) +{ + int i, j; + + for (i = 0; i < 2; i++) { + void __iomem *ppmu_base = data->dmc[i].hw_base; + u32 overflow; + + /* Stop PPMU */ + __raw_writel(0x0, ppmu_base + 0xf000); + + /* Update local data from PPMU */ + overflow = __raw_readl(ppmu_base + 0xf050); + + data->dmc[i].ccnt = __raw_readl(ppmu_base + 0xf100); + data->dmc[i].ccnt_overflow = overflow & (1 << 31); + + for (j = 0; j < PPMU_PMNCNT_MAX; j++) { + data->dmc[i].count[j] = __raw_readl( + ppmu_base + (0xf110 + (0x10 * j))); + data->dmc[i].count_overflow[j] = overflow & (1 << j); + } + } + + busfreq_mon_reset(data); +} + +static int exynos4x12_get_intspec(unsigned long mifclk) +{ + int i = 0; + + while (exynos4x12_intclk_table[i].clk) { + if (exynos4x12_intclk_table[i].clk <= mifclk) + return i; + i++; + } + + return -EINVAL; +} + +static int exynos4_bus_setvolt(struct busfreq_data *data, + struct busfreq_opp_info *oppi, + struct busfreq_opp_info *oldoppi) +{ + int err = 0, tmp; + unsigned long volt = oppi->volt; + + switch (data->type) { + case TYPE_BUSF_EXYNOS4210: + /* OPP represents DMC clock + INT voltage */ + err = regulator_set_voltage(data->vdd_int, volt, + MAX_SAFEVOLT); + break; + case TYPE_BUSF_EXYNOS4x12: + /* OPP represents MIF clock + MIF voltage */ + err = regulator_set_voltage(data->vdd_mif, volt, + MAX_SAFEVOLT); + if (err) + break; + + tmp = exynos4x12_get_intspec(oppi->rate); + if (tmp < 0) { + err = tmp; + regulator_set_voltage(data->vdd_mif, + oldoppi->volt, + MAX_SAFEVOLT); + break; + } + err = regulator_set_voltage(data->vdd_int, + exynos4x12_intclk_table[tmp].volt, + MAX_SAFEVOLT); + /* Try to recover */ + if (err) + regulator_set_voltage(data->vdd_mif, + oldoppi->volt, + MAX_SAFEVOLT); + break; + default: + err = -EINVAL; + } + + return err; +} + +static int exynos4_bus_target(struct device *dev, unsigned long *_freq, + u32 flags) +{ + int err = 0; + struct platform_device *pdev = container_of(dev, struct platform_device, + dev); + struct busfreq_data *data = platform_get_drvdata(pdev); + struct opp *opp; + unsigned long freq; + unsigned long old_freq = data->curr_oppinfo.rate; + struct busfreq_opp_info new_oppinfo; + + rcu_read_lock(); + opp = devfreq_recommended_opp(dev, _freq, flags); + if (IS_ERR(opp)) { + rcu_read_unlock(); + return PTR_ERR(opp); + } + new_oppinfo.rate = opp_get_freq(opp); + new_oppinfo.volt = opp_get_voltage(opp); + rcu_read_unlock(); + freq = new_oppinfo.rate; + + if (old_freq == freq) + return 0; + + dev_dbg(dev, "targeting %lukHz %luuV\n", freq, new_oppinfo.volt); + + mutex_lock(&data->lock); + + if (data->disabled) + goto out; + + if (old_freq < freq) + err = exynos4_bus_setvolt(data, &new_oppinfo, + &data->curr_oppinfo); + if (err) + goto out; + + if (old_freq != freq) { + switch (data->type) { + case TYPE_BUSF_EXYNOS4210: + err = exynos4210_set_busclk(data, &new_oppinfo); + break; + case TYPE_BUSF_EXYNOS4x12: + err = exynos4x12_set_busclk(data, &new_oppinfo); + break; + default: + err = -EINVAL; + } + } + if (err) + goto out; + + if (old_freq > freq) + err = exynos4_bus_setvolt(data, &new_oppinfo, + &data->curr_oppinfo); + if (err) + goto out; + + data->curr_oppinfo = new_oppinfo; +out: + mutex_unlock(&data->lock); + return err; +} + +static int exynos4_get_busier_dmc(struct busfreq_data *data) +{ + u64 p0 = data->dmc[0].count[0]; + u64 p1 = data->dmc[1].count[0]; + + p0 *= data->dmc[1].ccnt; + p1 *= data->dmc[0].ccnt; + + if (data->dmc[1].ccnt == 0) + return 0; + + if (p0 > p1) + return 0; + return 1; +} + +static int exynos4_bus_get_dev_status(struct device *dev, + struct devfreq_dev_status *stat) +{ + struct busfreq_data *data = dev_get_drvdata(dev); + int busier_dmc; + int cycles_x2 = 2; /* 2 x cycles */ + void __iomem *addr; + u32 timing; + u32 memctrl; + + exynos4_read_ppmu(data); + busier_dmc = exynos4_get_busier_dmc(data); + stat->current_frequency = data->curr_oppinfo.rate; + + if (busier_dmc) + addr = S5P_VA_DMC1; + else + addr = S5P_VA_DMC0; + + memctrl = __raw_readl(addr + 0x04); /* one of DDR2/3/LPDDR2 */ + timing = __raw_readl(addr + 0x38); /* CL or WL/RL values */ + + switch ((memctrl >> 8) & 0xf) { + case 0x4: /* DDR2 */ + cycles_x2 = ((timing >> 16) & 0xf) * 2; + break; + case 0x5: /* LPDDR2 */ + case 0x6: /* DDR3 */ + cycles_x2 = ((timing >> 8) & 0xf) + ((timing >> 0) & 0xf); + break; + default: + pr_err("%s: Unknown Memory Type(%d).\n", __func__, + (memctrl >> 8) & 0xf); + return -EINVAL; + } + + /* Number of cycles spent on memory access */ + stat->busy_time = data->dmc[busier_dmc].count[0] / 2 * (cycles_x2 + 2); + stat->busy_time *= 100 / BUS_SATURATION_RATIO; + stat->total_time = data->dmc[busier_dmc].ccnt; + + /* If the counters have overflown, retry */ + if (data->dmc[busier_dmc].ccnt_overflow || + data->dmc[busier_dmc].count_overflow[0]) + return -EAGAIN; + + return 0; +} + +static void exynos4_bus_exit(struct device *dev) +{ + struct busfreq_data *data = dev_get_drvdata(dev); + + devfreq_unregister_opp_notifier(dev, data->devfreq); +} + +static struct devfreq_dev_profile exynos4_devfreq_profile = { + .initial_freq = 400000, + .polling_ms = 50, + .target = exynos4_bus_target, + .get_dev_status = exynos4_bus_get_dev_status, + .exit = exynos4_bus_exit, +}; + +static int exynos4210_init_tables(struct busfreq_data *data) +{ + u32 tmp; + int mgrp; + int i, err = 0; + + tmp = __raw_readl(EXYNOS4_CLKDIV_DMC0); + for (i = LV_0; i < EX4210_LV_NUM; i++) { + tmp &= ~(EXYNOS4_CLKDIV_DMC0_ACP_MASK | + EXYNOS4_CLKDIV_DMC0_ACPPCLK_MASK | + EXYNOS4_CLKDIV_DMC0_DPHY_MASK | + EXYNOS4_CLKDIV_DMC0_DMC_MASK | + EXYNOS4_CLKDIV_DMC0_DMCD_MASK | + EXYNOS4_CLKDIV_DMC0_DMCP_MASK | + EXYNOS4_CLKDIV_DMC0_COPY2_MASK | + EXYNOS4_CLKDIV_DMC0_CORETI_MASK); + + tmp |= ((exynos4210_clkdiv_dmc0[i][0] << + EXYNOS4_CLKDIV_DMC0_ACP_SHIFT) | + (exynos4210_clkdiv_dmc0[i][1] << + EXYNOS4_CLKDIV_DMC0_ACPPCLK_SHIFT) | + (exynos4210_clkdiv_dmc0[i][2] << + EXYNOS4_CLKDIV_DMC0_DPHY_SHIFT) | + (exynos4210_clkdiv_dmc0[i][3] << + EXYNOS4_CLKDIV_DMC0_DMC_SHIFT) | + (exynos4210_clkdiv_dmc0[i][4] << + EXYNOS4_CLKDIV_DMC0_DMCD_SHIFT) | + (exynos4210_clkdiv_dmc0[i][5] << + EXYNOS4_CLKDIV_DMC0_DMCP_SHIFT) | + (exynos4210_clkdiv_dmc0[i][6] << + EXYNOS4_CLKDIV_DMC0_COPY2_SHIFT) | + (exynos4210_clkdiv_dmc0[i][7] << + EXYNOS4_CLKDIV_DMC0_CORETI_SHIFT)); + + data->dmc_divtable[i] = tmp; + } + + tmp = __raw_readl(EXYNOS4_CLKDIV_TOP); + for (i = LV_0; i < EX4210_LV_NUM; i++) { + tmp &= ~(EXYNOS4_CLKDIV_TOP_ACLK200_MASK | + EXYNOS4_CLKDIV_TOP_ACLK100_MASK | + EXYNOS4_CLKDIV_TOP_ACLK160_MASK | + EXYNOS4_CLKDIV_TOP_ACLK133_MASK | + EXYNOS4_CLKDIV_TOP_ONENAND_MASK); + + tmp |= ((exynos4210_clkdiv_top[i][0] << + EXYNOS4_CLKDIV_TOP_ACLK200_SHIFT) | + (exynos4210_clkdiv_top[i][1] << + EXYNOS4_CLKDIV_TOP_ACLK100_SHIFT) | + (exynos4210_clkdiv_top[i][2] << + EXYNOS4_CLKDIV_TOP_ACLK160_SHIFT) | + (exynos4210_clkdiv_top[i][3] << + EXYNOS4_CLKDIV_TOP_ACLK133_SHIFT) | + (exynos4210_clkdiv_top[i][4] << + EXYNOS4_CLKDIV_TOP_ONENAND_SHIFT)); + + data->top_divtable[i] = tmp; + } + +#ifdef CONFIG_EXYNOS_ASV + tmp = exynos4_result_of_asv; +#else + tmp = 0; /* Max voltages for the reliability of the unknown */ +#endif + + pr_debug("ASV Group of Exynos4 is %d\n", tmp); + /* Use merged grouping for voltage */ + switch (tmp) { + case 0: + mgrp = 0; + break; + case 1: + case 2: + mgrp = 1; + break; + case 3: + case 4: + mgrp = 2; + break; + case 5: + case 6: + mgrp = 3; + break; + case 7: + mgrp = 4; + break; + default: + pr_warn("Unknown ASV Group. Use max voltage.\n"); + mgrp = 0; + } + + for (i = LV_0; i < EX4210_LV_NUM; i++) + exynos4210_busclk_table[i].volt = exynos4210_asv_volt[mgrp][i]; + + for (i = LV_0; i < EX4210_LV_NUM; i++) { + err = opp_add(data->dev, exynos4210_busclk_table[i].clk, + exynos4210_busclk_table[i].volt); + if (err) { + dev_err(data->dev, "Cannot add opp entries.\n"); + return err; + } + } + + + return 0; +} + +static int exynos4x12_init_tables(struct busfreq_data *data) +{ + unsigned int i; + unsigned int tmp; + int ret; + + /* Enable pause function for DREX2 DVFS */ + tmp = __raw_readl(EXYNOS4_DMC_PAUSE_CTRL); + tmp |= EXYNOS4_DMC_PAUSE_ENABLE; + __raw_writel(tmp, EXYNOS4_DMC_PAUSE_CTRL); + + tmp = __raw_readl(EXYNOS4_CLKDIV_DMC0); + + for (i = 0; i < EX4x12_LV_NUM; i++) { + tmp &= ~(EXYNOS4_CLKDIV_DMC0_ACP_MASK | + EXYNOS4_CLKDIV_DMC0_ACPPCLK_MASK | + EXYNOS4_CLKDIV_DMC0_DPHY_MASK | + EXYNOS4_CLKDIV_DMC0_DMC_MASK | + EXYNOS4_CLKDIV_DMC0_DMCD_MASK | + EXYNOS4_CLKDIV_DMC0_DMCP_MASK); + + tmp |= ((exynos4x12_clkdiv_dmc0[i][0] << + EXYNOS4_CLKDIV_DMC0_ACP_SHIFT) | + (exynos4x12_clkdiv_dmc0[i][1] << + EXYNOS4_CLKDIV_DMC0_ACPPCLK_SHIFT) | + (exynos4x12_clkdiv_dmc0[i][2] << + EXYNOS4_CLKDIV_DMC0_DPHY_SHIFT) | + (exynos4x12_clkdiv_dmc0[i][3] << + EXYNOS4_CLKDIV_DMC0_DMC_SHIFT) | + (exynos4x12_clkdiv_dmc0[i][4] << + EXYNOS4_CLKDIV_DMC0_DMCD_SHIFT) | + (exynos4x12_clkdiv_dmc0[i][5] << + EXYNOS4_CLKDIV_DMC0_DMCP_SHIFT)); + + data->dmc_divtable[i] = tmp; + } + +#ifdef CONFIG_EXYNOS_ASV + tmp = exynos4_result_of_asv; +#else + tmp = 0; /* Max voltages for the reliability of the unknown */ +#endif + + if (tmp > 8) + tmp = 0; + pr_debug("ASV Group of Exynos4x12 is %d\n", tmp); + + for (i = 0; i < EX4x12_LV_NUM; i++) { + exynos4x12_mifclk_table[i].volt = + exynos4x12_mif_step_50[tmp][i]; + exynos4x12_intclk_table[i].volt = + exynos4x12_int_volt[tmp][i]; + } + + for (i = 0; i < EX4x12_LV_NUM; i++) { + ret = opp_add(data->dev, exynos4x12_mifclk_table[i].clk, + exynos4x12_mifclk_table[i].volt); + if (ret) { + dev_err(data->dev, "Fail to add opp entries.\n"); + return ret; + } + } + + return 0; +} + +static int exynos4_busfreq_pm_notifier_event(struct notifier_block *this, + unsigned long event, void *ptr) +{ + struct busfreq_data *data = container_of(this, struct busfreq_data, + pm_notifier); + struct opp *opp; + struct busfreq_opp_info new_oppinfo; + unsigned long maxfreq = ULONG_MAX; + int err = 0; + + switch (event) { + case PM_SUSPEND_PREPARE: + /* Set Fastest and Deactivate DVFS */ + mutex_lock(&data->lock); + + data->disabled = true; + + rcu_read_lock(); + opp = opp_find_freq_floor(data->dev, &maxfreq); + if (IS_ERR(opp)) { + rcu_read_unlock(); + dev_err(data->dev, "%s: unable to find a min freq\n", + __func__); + return PTR_ERR(opp); + } + new_oppinfo.rate = opp_get_freq(opp); + new_oppinfo.volt = opp_get_voltage(opp); + rcu_read_unlock(); + + err = exynos4_bus_setvolt(data, &new_oppinfo, + &data->curr_oppinfo); + if (err) + goto unlock; + + switch (data->type) { + case TYPE_BUSF_EXYNOS4210: + err = exynos4210_set_busclk(data, &new_oppinfo); + break; + case TYPE_BUSF_EXYNOS4x12: + err = exynos4x12_set_busclk(data, &new_oppinfo); + break; + default: + err = -EINVAL; + } + if (err) + goto unlock; + + data->curr_oppinfo = new_oppinfo; +unlock: + mutex_unlock(&data->lock); + if (err) + return err; + return NOTIFY_OK; + case PM_POST_RESTORE: + case PM_POST_SUSPEND: + /* Reactivate */ + mutex_lock(&data->lock); + data->disabled = false; + mutex_unlock(&data->lock); + return NOTIFY_OK; + } + + return NOTIFY_DONE; +} + +static int exynos4_busfreq_probe(struct platform_device *pdev) +{ + struct busfreq_data *data; + struct opp *opp; + struct device *dev = &pdev->dev; + int err = 0; + + data = devm_kzalloc(&pdev->dev, sizeof(struct busfreq_data), GFP_KERNEL); + if (data == NULL) { + dev_err(dev, "Cannot allocate memory.\n"); + return -ENOMEM; + } + + data->type = pdev->id_entry->driver_data; + data->dmc[0].hw_base = S5P_VA_DMC0; + data->dmc[1].hw_base = S5P_VA_DMC1; + data->pm_notifier.notifier_call = exynos4_busfreq_pm_notifier_event; + data->dev = dev; + mutex_init(&data->lock); + + switch (data->type) { + case TYPE_BUSF_EXYNOS4210: + err = exynos4210_init_tables(data); + break; + case TYPE_BUSF_EXYNOS4x12: + err = exynos4x12_init_tables(data); + break; + default: + dev_err(dev, "Cannot determine the device id %d\n", data->type); + err = -EINVAL; + } + if (err) + return err; + + data->vdd_int = devm_regulator_get(dev, "vdd_int"); + if (IS_ERR(data->vdd_int)) { + dev_err(dev, "Cannot get the regulator \"vdd_int\"\n"); + return PTR_ERR(data->vdd_int); + } + if (data->type == TYPE_BUSF_EXYNOS4x12) { + data->vdd_mif = devm_regulator_get(dev, "vdd_mif"); + if (IS_ERR(data->vdd_mif)) { + dev_err(dev, "Cannot get the regulator \"vdd_mif\"\n"); + return PTR_ERR(data->vdd_mif); + } + } + + rcu_read_lock(); + opp = opp_find_freq_floor(dev, &exynos4_devfreq_profile.initial_freq); + if (IS_ERR(opp)) { + rcu_read_unlock(); + dev_err(dev, "Invalid initial frequency %lu kHz.\n", + exynos4_devfreq_profile.initial_freq); + return PTR_ERR(opp); + } + data->curr_oppinfo.rate = opp_get_freq(opp); + data->curr_oppinfo.volt = opp_get_voltage(opp); + rcu_read_unlock(); + + platform_set_drvdata(pdev, data); + + busfreq_mon_reset(data); + + data->devfreq = devfreq_add_device(dev, &exynos4_devfreq_profile, + "simple_ondemand", NULL); + if (IS_ERR(data->devfreq)) + return PTR_ERR(data->devfreq); + + devfreq_register_opp_notifier(dev, data->devfreq); + + err = register_pm_notifier(&data->pm_notifier); + if (err) { + dev_err(dev, "Failed to setup pm notifier\n"); + devfreq_remove_device(data->devfreq); + return err; + } + + return 0; +} + +static int exynos4_busfreq_remove(struct platform_device *pdev) +{ + struct busfreq_data *data = platform_get_drvdata(pdev); + + unregister_pm_notifier(&data->pm_notifier); + devfreq_remove_device(data->devfreq); + + return 0; +} + +static int exynos4_busfreq_resume(struct device *dev) +{ + struct busfreq_data *data = dev_get_drvdata(dev); + + busfreq_mon_reset(data); + return 0; +} + +static const struct dev_pm_ops exynos4_busfreq_pm = { + .resume = exynos4_busfreq_resume, +}; + +static const struct platform_device_id exynos4_busfreq_id[] = { + { "exynos4210-busfreq", TYPE_BUSF_EXYNOS4210 }, + { "exynos4412-busfreq", TYPE_BUSF_EXYNOS4x12 }, + { "exynos4212-busfreq", TYPE_BUSF_EXYNOS4x12 }, + { }, +}; + +static struct platform_driver exynos4_busfreq_driver = { + .probe = exynos4_busfreq_probe, + .remove = exynos4_busfreq_remove, + .id_table = exynos4_busfreq_id, + .driver = { + .name = "exynos4-busfreq", + .owner = THIS_MODULE, + .pm = &exynos4_busfreq_pm, + }, +}; + +static int __init exynos4_busfreq_init(void) +{ + return platform_driver_register(&exynos4_busfreq_driver); +} +late_initcall(exynos4_busfreq_init); + +static void __exit exynos4_busfreq_exit(void) +{ + platform_driver_unregister(&exynos4_busfreq_driver); +} +module_exit(exynos4_busfreq_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("EXYNOS4 busfreq driver with devfreq framework"); +MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); diff --git a/drivers/devfreq/governor.h b/drivers/devfreq/governor.h new file mode 100644 index 00000000..fad7d632 --- /dev/null +++ b/drivers/devfreq/governor.h @@ -0,0 +1,41 @@ +/* + * governor.h - internal header for devfreq governors. + * + * Copyright (C) 2011 Samsung Electronics + * MyungJoo Ham <myungjoo.ham@samsung.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This header is for devfreq governors in drivers/devfreq/ + */ + +#ifndef _GOVERNOR_H +#define _GOVERNOR_H + +#include <linux/devfreq.h> + +#define to_devfreq(DEV) container_of((DEV), struct devfreq, dev) + +/* Devfreq events */ +#define DEVFREQ_GOV_START 0x1 +#define DEVFREQ_GOV_STOP 0x2 +#define DEVFREQ_GOV_INTERVAL 0x3 +#define DEVFREQ_GOV_SUSPEND 0x4 +#define DEVFREQ_GOV_RESUME 0x5 + +/* Caution: devfreq->lock must be locked before calling update_devfreq */ +extern int update_devfreq(struct devfreq *devfreq); + +extern void devfreq_monitor_start(struct devfreq *devfreq); +extern void devfreq_monitor_stop(struct devfreq *devfreq); +extern void devfreq_monitor_suspend(struct devfreq *devfreq); +extern void devfreq_monitor_resume(struct devfreq *devfreq); +extern void devfreq_interval_update(struct devfreq *devfreq, + unsigned int *delay); + +extern int devfreq_add_governor(struct devfreq_governor *governor); +extern int devfreq_remove_governor(struct devfreq_governor *governor); + +#endif /* _GOVERNOR_H */ diff --git a/drivers/devfreq/governor_ondemand.c b/drivers/devfreq/governor_ondemand.c new file mode 100644 index 00000000..ac358bae --- /dev/null +++ b/drivers/devfreq/governor_ondemand.c @@ -0,0 +1,729 @@ +/* + * Copyright (C) 2013 Spreadtrum Communications Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * 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. + */ + +#include <linux/errno.h> +#include <linux/sched.h> +#include <linux/slab.h> +#include <linux/devfreq.h> +#include <linux/math64.h> +#include <linux/module.h> +#include <linux/spinlock.h> +#include <linux/timer.h> +#include <linux/earlysuspend.h> +#include "governor.h" + +/* Default constants for DevFreq-Ondemand (DFO) */ +#define DFO_UPTHRESHOLD (80) +#define DFO_DOWNDIFFERENCTIAL (30) + +/* +* TODO: add kernel space requests +*/ +#ifdef CONFIG_SPRD_SCX35_DMC_FREQ +extern void devfreq_min_freq_cnt_reset(unsigned int, unsigned int); +extern int devfreq_request_ignore(void); +#endif + +static void dfs_req_timer_timeout(unsigned long arg); +#define REQ_TIMEOUT_DEF (HZ/20); +static DEFINE_SPINLOCK(dfs_req_lock); +static unsigned int dfs_req_timeout; +static DEFINE_TIMER(dfs_req_timer, dfs_req_timer_timeout, 0, 0); +struct dfs_request_state{ + int req_sum; /* in KHz */ + int req_timeout; /* in KHz */ + int req_quirk; /* in KHz */ + u32 ddr_freq_after_req; /* in KHz */ +}; +static struct dfs_request_state user_requests; +static struct devfreq *g_devfreq; /* for requests from kernel */ +static int gov_eb = 1; +struct userspace_data { + int req_bw; + unsigned long set_freq; + unsigned long set_count; + unsigned long upthreshold; + unsigned long downdifferential; + unsigned long (*convert_bw_to_freq)(u32 req_bw); + bool enable; /*sysfs only*/ + bool devfreq_enable; +}; + +/************ kernel interface *****************/ +/* +* get dfs enable flag +* enabled == 0 --> dfs disable +* enabled == 1 --> dfs enable +*/ +bool dfs_get_enable(void) +{ + struct userspace_data *user_data; + bool enabled; + + if(g_devfreq && g_devfreq->data){ + user_data = (struct userspace_data *)(g_devfreq->data); + enabled = user_data->enable; + pr_debug("%s, enabled = %d, user_data->enable = %d\n", __func__, enabled, user_data->enable); + } + + return enabled; +} +EXPORT_SYMBOL(dfs_get_enable); +/* +* set ddr frequnecy +* @freq: KHz +* if ddr frequency is set through this function, DVS is disabled +*/ +int dfs_set_freq(int freq) +{ + struct userspace_data *user_data; + int err; + + if(freq < 0){ + err = -1; + pr_debug("*** %s,freq < 0\n",__func__); + goto done; + } + + user_data = (struct userspace_data *)(g_devfreq->data); + mutex_lock(&g_devfreq->lock); + if(user_data){ + if(freq > 0){ + user_data->set_count++; + user_data->devfreq_enable = false; + if(freq > user_data->set_freq) + user_data->set_freq = freq; + }else{ + if(user_data->set_count > 0){ + user_data->set_count--; + if(user_data->set_count == 0){ + user_data->set_freq = 0; + user_data->devfreq_enable = true; + } + } + } + pr_debug("*** %s, set freq:%d KHz, set_count:%lu ***\n", __func__, freq, user_data->set_count ); + } + else + { + pr_debug("*** %s,user_data == 0\n",__func__); + } + err = update_devfreq(g_devfreq); + mutex_unlock(&g_devfreq->lock); +done: + return err; +} + +/* +* add a new ddr bandwidth request. +* @req_bw: KB +* + addition(add>=0) or - subtraction(add<0) +*/ +void dfs_request_bw(int req_bw) +{ + u32 req_freq = 0; + int add = 1; + struct userspace_data *user_data; + + if (req_bw < 0) { + req_bw = -req_bw; + add = -1; + } + + if(g_devfreq && g_devfreq->data){ + user_data = (struct userspace_data *)(g_devfreq->data); + if(user_data->convert_bw_to_freq){ + req_freq = (user_data->convert_bw_to_freq)(req_bw); + } + } + pr_debug("*** %s, pid:%u, %creq_bw:%u, req_freq:%u ***\n", + __func__, current->pid, add>=0?'+':'-', req_bw, req_freq ); + if(req_freq){ + mutex_lock(&g_devfreq->lock); + if(add >= 0) + user_requests.req_sum += req_freq; + else + user_requests.req_sum -= req_freq; + if(user_requests.req_sum < 0) + user_requests.req_sum = 0; + update_devfreq(g_devfreq); + mutex_unlock(&g_devfreq->lock); + } +} + +/* +* set request timer timeout +* @timeout, ms +*/ +void dfs_req_set_timeout(unsigned int timeout) +{ + spin_lock(&dfs_req_lock); + dfs_req_timeout = msecs_to_jiffies(timeout); + spin_unlock(&dfs_req_lock); +} +EXPORT_SYMBOL(dfs_req_set_timeout); + +/* +* get request timer timeout +* @return, ms +*/ +unsigned int dfs_req_get_timeout(void) +{ + unsigned int timeout; + + spin_lock(&dfs_req_lock); + timeout = dfs_req_timeout; + spin_unlock(&dfs_req_lock); + + timeout = jiffies_to_msecs(timeout); + return timeout; +} +EXPORT_SYMBOL(dfs_req_get_timeout); + +static void dfs_req_timer_timeout(unsigned long arg) +{ + spin_lock(&dfs_req_lock); + user_requests.req_timeout = 0; + spin_unlock(&dfs_req_lock); + return; +} + +/* +* add a new ddr bandwidth request. when time is up, request is cleared automatically +* @req_bw, KB +*/ +void dfs_request_bw_timeout(unsigned int req_bw) +{ + struct userspace_data *user_data; + unsigned int req_freq; + + req_freq = 0; + if(req_bw == 0) + return; + + spin_lock(&dfs_req_lock); + if( user_requests.req_timeout ){ + spin_unlock(&dfs_req_lock); + pr_debug("*** %s, ignore, req_timeout:%d ***\n", __func__, user_requests.req_timeout); + return; + } + spin_unlock(&dfs_req_lock); + + if(g_devfreq && g_devfreq->data){ + user_data = (struct userspace_data *)(g_devfreq->data); + if(user_data->convert_bw_to_freq){ + req_freq = (user_data->convert_bw_to_freq)(req_bw); + printk("*** %s, req_freq:%d ***\n", __func__, req_freq ); + } + } + spin_lock(&dfs_req_lock); + user_requests.req_timeout = req_freq; + spin_unlock(&dfs_req_lock); + + if(req_freq) + mod_timer(&dfs_req_timer, jiffies+dfs_req_timeout); + else + del_timer_sync(&dfs_req_timer); + + if(req_freq){ + mutex_lock(&g_devfreq->lock); + update_devfreq(g_devfreq); + mutex_unlock(&g_devfreq->lock); + } +} +EXPORT_SYMBOL(dfs_request_bw_timeout); + +/* +* raise ddr frequency up temporarily +* @req_bw, KB +*/ +#ifdef CONFIG_SPRD_SCX35_DMC_FREQ +void dfs_freq_raise_quirk(unsigned int req_bw) +{ + if(req_bw == 0) + return; + + spin_lock(&dfs_req_lock); + if(user_requests.req_quirk || devfreq_request_ignore() ){ + spin_unlock(&dfs_req_lock); + return; + } + user_requests.req_quirk = req_bw; + spin_unlock(&dfs_req_lock); + + mutex_lock(&g_devfreq->lock); + devfreq_min_freq_cnt_reset(-1, 1); + update_devfreq(g_devfreq); + devfreq_min_freq_cnt_reset(-1, 0); + user_requests.req_quirk = 0; + mutex_unlock(&g_devfreq->lock); +} +EXPORT_SYMBOL(dfs_freq_raise_quirk); +#endif + +/************ early suspend *****************/ + +static void devfreq_early_suspend(struct early_suspend *h) +{ +#ifdef CONFIG_ARCH_SCX15 + dfs_set_freq(192000); +#else +#ifdef CONFIG_ARCH_SCX35 + dfs_set_freq(200000); +#endif +#endif + gov_eb = 0; +} + +static void devfreq_late_resume(struct early_suspend *h) +{ + dfs_set_freq(0); +} + +static struct early_suspend devfreq_early_suspend_desc = { + .level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 100, + .suspend = devfreq_early_suspend, + .resume = devfreq_late_resume, +}; + +static void devfreq_enable_late_resume(struct early_suspend *h) +{ + gov_eb = 1; +} +static struct early_suspend devfreq_enable_desc = { + .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN, + .resume = devfreq_enable_late_resume, +}; +/************ userspace interface *****************/ + +static ssize_t store_upthreshold(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + unsigned long wanted; + + + mutex_lock(&devfreq->lock); + data = devfreq->data; + sscanf(buf, "%lu", &wanted); + if(data) + data->upthreshold = wanted; + mutex_unlock(&devfreq->lock); + return count; +} + +static ssize_t show_upthreshold(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + int err = 0; + + mutex_lock(&devfreq->lock); + data = devfreq->data; + if(data){ + err = sprintf(buf, "%lu\n", data->upthreshold); + }else + err = sprintf(buf, "%d\n", DFO_UPTHRESHOLD); + mutex_unlock(&devfreq->lock); + return err; +} + +static ssize_t store_downdifferential(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + unsigned long wanted; + + + mutex_lock(&devfreq->lock); + data = devfreq->data; + sscanf(buf, "%lu", &wanted); + if(data) + data->downdifferential = wanted; + mutex_unlock(&devfreq->lock); + return count; +} + +static ssize_t show_downdifferential(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + int err = 0; + + mutex_lock(&devfreq->lock); + data = devfreq->data; + if(data){ + err = sprintf(buf, "%lu\n", data->downdifferential); + }else{ + err = sprintf(buf, "%d\n", DFO_DOWNDIFFERENCTIAL); + } + mutex_unlock(&devfreq->lock); + return err; +} + + +static ssize_t store_request(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + int wanted; + int req_freq; + int err = 0; + + req_freq = 0; + mutex_lock(&devfreq->lock); + data = devfreq->data; + sscanf(buf, "%d", &wanted); + if(data){ + data->req_bw += wanted; + pr_debug("*** %s, request:%d, total request:%d ***\n", + __func__, wanted, data->req_bw); + if(data->req_bw < 0) + data->req_bw = 0; + if(data->convert_bw_to_freq) + req_freq = data->convert_bw_to_freq(wanted); + } + user_requests.req_sum += req_freq; + if(user_requests.req_sum < 0) + user_requests.req_sum = 0; + err = update_devfreq(devfreq); + if (err == 0) + err = count; + mutex_unlock(&devfreq->lock); + return err; + +} + +static ssize_t show_request(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + int err = 0; + + mutex_lock(&devfreq->lock); + data = devfreq->data; + if(data) + err = sprintf(buf, "%d KB\n", data->req_bw); + mutex_unlock(&devfreq->lock); + return err; +} + +static ssize_t store_enable(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + unsigned long wanted; + + mutex_lock(&devfreq->lock); + data = devfreq->data; + sscanf(buf, "%lu", &wanted); + if(data){ + data->enable = wanted; + } + mutex_unlock(&devfreq->lock); + return count; +} + +static ssize_t show_enable(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + int err = 0; + + mutex_lock(&devfreq->lock); + data = devfreq->data; + if(data) + err = sprintf(buf, "%d \n", data->enable); + mutex_unlock(&devfreq->lock); + return err; +} + +static ssize_t store_freq(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + unsigned long wanted; + int err = 0; + + sscanf(buf, "%lu", &wanted); + err = dfs_set_freq(wanted); + if (err == 0) + err = count; + return err; +} + +static ssize_t show_freq(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + int err = 0; + + mutex_lock(&devfreq->lock); + data = devfreq->data; + if(data) + err = sprintf(buf, "%lu KHz, set count:%lu\n", data->set_freq, data->set_count); + mutex_unlock(&devfreq->lock); + return err; +} + +static DEVICE_ATTR(set_freq, 0644, show_freq, store_freq); +static DEVICE_ATTR(set_enable, 0644, show_enable, store_enable); +static DEVICE_ATTR(set_request, 0644, show_request, store_request); +static DEVICE_ATTR(set_upthreshold, 0644, show_upthreshold, store_upthreshold); +static DEVICE_ATTR(set_downdifferential, 0644, show_downdifferential, store_downdifferential); +static struct attribute *dev_entries[] = { + &dev_attr_set_freq.attr, + &dev_attr_set_enable.attr, + &dev_attr_set_request.attr, + &dev_attr_set_upthreshold.attr, + &dev_attr_set_downdifferential.attr, + NULL, +}; +static struct attribute_group dev_attr_group = { + .name = "ondemand", + .attrs = dev_entries, +}; + +static int devfreq_ondemand_start(struct devfreq *devfreq) +{ + int err = 0; + struct userspace_data *data = kzalloc(sizeof(struct userspace_data), + GFP_KERNEL); + + if (!data) { + err = -ENOMEM; + goto out; + } + data->req_bw = 0; + data->set_freq = 0; + data->upthreshold = DFO_UPTHRESHOLD; + data->downdifferential = DFO_DOWNDIFFERENCTIAL; +#if defined(CONFIG_ARCH_SCX30G) + data->enable = false; +#else + data->enable = true; +#endif + data->devfreq_enable = true; + if(devfreq->data){ + data->convert_bw_to_freq = devfreq->data; + pr_info("*** %s, data->convert_bw_to_freq:%pf ***\n", __func__, data->convert_bw_to_freq); + } + devfreq->data = data; + g_devfreq = devfreq; + err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group); + register_early_suspend(&devfreq_early_suspend_desc); + /* + * disable DFS before DISPC late resume + */ + register_early_suspend(&devfreq_enable_desc); + + spin_lock(&dfs_req_lock); + dfs_req_timeout = REQ_TIMEOUT_DEF; + spin_unlock(&dfs_req_lock); +out: + return err; +} + +static int devfreq_ondemand_stop(struct devfreq *devfreq) +{ + int err = 0; + if(devfreq->data){ + kfree(devfreq->data); + } + + return err; + +} +/************ userspace interface *****************/ + +static int devfreq_ondemand_func(struct devfreq *df, + unsigned long *freq) +{ + struct devfreq_dev_status stat; + int err = df->profile->get_dev_status(df->dev.parent, &stat); + unsigned long long a, b; + unsigned int dfso_upthreshold = DFO_UPTHRESHOLD; + unsigned int dfso_downdifferential = DFO_DOWNDIFFERENCTIAL; + struct userspace_data *data = df->data; + unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX; + unsigned long req_freq; + + if (err) + return err; + + if(user_requests.req_quirk && + stat.current_frequency==df->min_freq){ + /* + * QUIRK: only set frequency larger than minimum + */ + *freq = df->min_freq + 1; + pr_debug("*** %s, req_quirk, freq:%lu, return ***\n", __func__, *freq); + return 0; + } + /* + * TODO: add request frequency + */ + req_freq = user_requests.req_sum + user_requests.req_timeout; + + if (data) { + if (data->enable==false || !(data->devfreq_enable) || + data->set_freq || !gov_eb){ + if(user_requests.ddr_freq_after_req == 0) + user_requests.ddr_freq_after_req = max; + *freq = (data->set_freq?data->set_freq:user_requests.ddr_freq_after_req); + pr_debug("*** %s, data->enable:%d, data->set_freq:%u, gov_eb:%d ***\n", + __func__, data->enable, data->set_freq, gov_eb ); + return 0; + } + if (data->upthreshold) + dfso_upthreshold = data->upthreshold; + if (data->downdifferential) + dfso_downdifferential = data->downdifferential; + }else{ + printk("*** %s, data is NULL ***\n", __func__ ); + } + + if (dfso_upthreshold > 100 || + dfso_upthreshold < dfso_downdifferential){ + printk("*** %s, dfso_upthreshold:%u, dfso_downdifferential:%u ***\n", + __func__, dfso_upthreshold, dfso_downdifferential ); + return -EINVAL; + } + + /* Assume MAX if it is going to be divided by zero */ + if (stat.total_time == 0) { + *freq = max; + user_requests.ddr_freq_after_req = *freq; + pr_debug("*** %s, stat.total_time == 0, freq:%lu ***\n", __func__, *freq); + return 0; + } + + /* Prevent overflow */ + if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) { + stat.busy_time >>= 7; + stat.total_time >>= 7; + } + + /* Set MAX if it's busy enough */ + if (stat.busy_time * 100 > + stat.total_time * dfso_upthreshold) { + *freq = max; + user_requests.ddr_freq_after_req = *freq; + pr_debug("*** %s, set max freq:%lu ***\n", __func__, *freq); + return 0; + } + + /* Set MAX if we do not know the initial frequency */ + if (stat.current_frequency == 0) { + *freq = max; + user_requests.ddr_freq_after_req = *freq; + pr_debug("*** %s, stat.current_frequency == 0, freq:%lu ***\n", __func__, *freq); + return 0; + } + + /* Keep the current frequency */ + if (stat.busy_time * 100 > + stat.total_time * (dfso_upthreshold - dfso_downdifferential)) { + *freq = stat.current_frequency + req_freq; + user_requests.ddr_freq_after_req = *freq; + pr_debug("*** %s, Keep the current frequency %lu, req_freq:%lu ***\n", + __func__, stat.current_frequency, req_freq); + return 0; + } + + /* Set the desired frequency based on the load */ + a = stat.busy_time; + a *= stat.current_frequency; + b = div_u64(a, stat.total_time); + b *= 100; + b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2)); + *freq = (unsigned long) b + req_freq; + user_requests.ddr_freq_after_req = *freq; + pr_debug("*** %s, calculate freq:%lu, req_freq:%lu ***\n", + __func__, (unsigned long)b, req_freq); + + if (df->min_freq && *freq < df->min_freq) + *freq = df->min_freq; + if (df->max_freq && *freq > df->max_freq) + *freq = df->max_freq; + + return 0; +} + +static int devfreq_ondemand_handler(struct devfreq *devfreq, + unsigned int event, void *data) +{ + switch (event) { + case DEVFREQ_GOV_START: + devfreq_ondemand_start(devfreq); + devfreq_monitor_start(devfreq); + break; + + case DEVFREQ_GOV_STOP: + devfreq_monitor_stop(devfreq); + //devfreq_ondemand_stop(devfreq); + break; + + case DEVFREQ_GOV_INTERVAL: + devfreq_interval_update(devfreq, (unsigned int *)data); + break; + + case DEVFREQ_GOV_SUSPEND: + devfreq_monitor_suspend(devfreq); + break; + + case DEVFREQ_GOV_RESUME: + devfreq_monitor_resume(devfreq); + break; + + default: + break; + } + + return 0; +} + +const struct devfreq_governor devfreq_ondemand = { + .name = "ondemand", + .get_target_freq = devfreq_ondemand_func, + .event_handler = devfreq_ondemand_handler, +}; + +static int __init devfreq_ondemand_init(void) +{ + return devfreq_add_governor(&devfreq_ondemand); +} +subsys_initcall(devfreq_ondemand_init); + +static void __exit devfreq_ondemand_exit(void) +{ + int ret; + + ret = devfreq_remove_governor(&devfreq_ondemand); + if (ret) + pr_err("%s: failed remove governor %d\n", __func__, ret); + + return; +} +module_exit(devfreq_ondemand_exit); +MODULE_LICENSE("GPL"); diff --git a/drivers/devfreq/governor_performance.c b/drivers/devfreq/governor_performance.c new file mode 100644 index 00000000..c72f942f --- /dev/null +++ b/drivers/devfreq/governor_performance.c @@ -0,0 +1,67 @@ +/* + * linux/drivers/devfreq/governor_performance.c + * + * Copyright (C) 2011 Samsung Electronics + * MyungJoo Ham <myungjoo.ham@samsung.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include <linux/devfreq.h> +#include <linux/module.h> +#include "governor.h" + +static int devfreq_performance_func(struct devfreq *df, + unsigned long *freq) +{ + /* + * target callback should be able to get floor value as + * said in devfreq.h + */ + if (!df->max_freq) + *freq = UINT_MAX; + else + *freq = df->max_freq; + return 0; +} + +static int devfreq_performance_handler(struct devfreq *devfreq, + unsigned int event, void *data) +{ + int ret = 0; + + if (event == DEVFREQ_GOV_START) { + mutex_lock(&devfreq->lock); + ret = update_devfreq(devfreq); + mutex_unlock(&devfreq->lock); + } + + return ret; +} + +static struct devfreq_governor devfreq_performance = { + .name = "performance", + .get_target_freq = devfreq_performance_func, + .event_handler = devfreq_performance_handler, +}; + +static int __init devfreq_performance_init(void) +{ + return devfreq_add_governor(&devfreq_performance); +} +subsys_initcall(devfreq_performance_init); + +static void __exit devfreq_performance_exit(void) +{ + int ret; + + ret = devfreq_remove_governor(&devfreq_performance); + if (ret) + pr_err("%s: failed remove governor %d\n", __func__, ret); + + return; +} +module_exit(devfreq_performance_exit); +MODULE_LICENSE("GPL"); diff --git a/drivers/devfreq/governor_powersave.c b/drivers/devfreq/governor_powersave.c new file mode 100644 index 00000000..0c6bed56 --- /dev/null +++ b/drivers/devfreq/governor_powersave.c @@ -0,0 +1,64 @@ +/* + * linux/drivers/devfreq/governor_powersave.c + * + * Copyright (C) 2011 Samsung Electronics + * MyungJoo Ham <myungjoo.ham@samsung.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include <linux/devfreq.h> +#include <linux/module.h> +#include "governor.h" + +static int devfreq_powersave_func(struct devfreq *df, + unsigned long *freq) +{ + /* + * target callback should be able to get ceiling value as + * said in devfreq.h + */ + *freq = df->min_freq; + return 0; +} + +static int devfreq_powersave_handler(struct devfreq *devfreq, + unsigned int event, void *data) +{ + int ret = 0; + + if (event == DEVFREQ_GOV_START) { + mutex_lock(&devfreq->lock); + ret = update_devfreq(devfreq); + mutex_unlock(&devfreq->lock); + } + + return ret; +} + +static struct devfreq_governor devfreq_powersave = { + .name = "powersave", + .get_target_freq = devfreq_powersave_func, + .event_handler = devfreq_powersave_handler, +}; + +static int __init devfreq_powersave_init(void) +{ + return devfreq_add_governor(&devfreq_powersave); +} +subsys_initcall(devfreq_powersave_init); + +static void __exit devfreq_powersave_exit(void) +{ + int ret; + + ret = devfreq_remove_governor(&devfreq_powersave); + if (ret) + pr_err("%s: failed remove governor %d\n", __func__, ret); + + return; +} +module_exit(devfreq_powersave_exit); +MODULE_LICENSE("GPL"); diff --git a/drivers/devfreq/governor_simpleondemand.c b/drivers/devfreq/governor_simpleondemand.c new file mode 100644 index 00000000..0720ba84 --- /dev/null +++ b/drivers/devfreq/governor_simpleondemand.c @@ -0,0 +1,147 @@ +/* + * linux/drivers/devfreq/governor_simpleondemand.c + * + * Copyright (C) 2011 Samsung Electronics + * MyungJoo Ham <myungjoo.ham@samsung.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include <linux/errno.h> +#include <linux/module.h> +#include <linux/devfreq.h> +#include <linux/math64.h> +#include "governor.h" + +/* Default constants for DevFreq-Simple-Ondemand (DFSO) */ +#define DFSO_UPTHRESHOLD (90) +#define DFSO_DOWNDIFFERENCTIAL (5) +static int devfreq_simple_ondemand_func(struct devfreq *df, + unsigned long *freq) +{ + struct devfreq_dev_status stat; + int err = df->profile->get_dev_status(df->dev.parent, &stat); + unsigned long long a, b; + unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD; + unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL; + struct devfreq_simple_ondemand_data *data = df->data; + unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX; + + if (err) + return err; + + if (data) { + if (data->upthreshold) + dfso_upthreshold = data->upthreshold; + if (data->downdifferential) + dfso_downdifferential = data->downdifferential; + } + if (dfso_upthreshold > 100 || + dfso_upthreshold < dfso_downdifferential) + return -EINVAL; + + /* Assume MAX if it is going to be divided by zero */ + if (stat.total_time == 0) { + *freq = max; + return 0; + } + + /* Prevent overflow */ + if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) { + stat.busy_time >>= 7; + stat.total_time >>= 7; + } + + /* Set MAX if it's busy enough */ + if (stat.busy_time * 100 > + stat.total_time * dfso_upthreshold) { + *freq = max; + return 0; + } + + /* Set MAX if we do not know the initial frequency */ + if (stat.current_frequency == 0) { + *freq = max; + return 0; + } + + /* Keep the current frequency */ + if (stat.busy_time * 100 > + stat.total_time * (dfso_upthreshold - dfso_downdifferential)) { + *freq = stat.current_frequency; + return 0; + } + + /* Set the desired frequency based on the load */ + a = stat.busy_time; + a *= stat.current_frequency; + b = div_u64(a, stat.total_time); + b *= 100; + b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2)); + *freq = (unsigned long) b; + + if (df->min_freq && *freq < df->min_freq) + *freq = df->min_freq; + if (df->max_freq && *freq > df->max_freq) + *freq = df->max_freq; + + return 0; +} + +static int devfreq_simple_ondemand_handler(struct devfreq *devfreq, + unsigned int event, void *data) +{ + switch (event) { + case DEVFREQ_GOV_START: + devfreq_monitor_start(devfreq); + break; + + case DEVFREQ_GOV_STOP: + devfreq_monitor_stop(devfreq); + break; + + case DEVFREQ_GOV_INTERVAL: + devfreq_interval_update(devfreq, (unsigned int *)data); + break; + + case DEVFREQ_GOV_SUSPEND: + devfreq_monitor_suspend(devfreq); + break; + + case DEVFREQ_GOV_RESUME: + devfreq_monitor_resume(devfreq); + break; + + default: + break; + } + + return 0; +} + +static struct devfreq_governor devfreq_simple_ondemand = { + .name = "simple_ondemand", + .get_target_freq = devfreq_simple_ondemand_func, + .event_handler = devfreq_simple_ondemand_handler, +}; + +static int __init devfreq_simple_ondemand_init(void) +{ + return devfreq_add_governor(&devfreq_simple_ondemand); +} +subsys_initcall(devfreq_simple_ondemand_init); + +static void __exit devfreq_simple_ondemand_exit(void) +{ + int ret; + + ret = devfreq_remove_governor(&devfreq_simple_ondemand); + if (ret) + pr_err("%s: failed remove governor %d\n", __func__, ret); + + return; +} +module_exit(devfreq_simple_ondemand_exit); +MODULE_LICENSE("GPL"); diff --git a/drivers/devfreq/governor_userspace.c b/drivers/devfreq/governor_userspace.c new file mode 100644 index 00000000..35de6e83 --- /dev/null +++ b/drivers/devfreq/governor_userspace.c @@ -0,0 +1,162 @@ +/* + * linux/drivers/devfreq/governor_simpleondemand.c + * + * Copyright (C) 2011 Samsung Electronics + * MyungJoo Ham <myungjoo.ham@samsung.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include <linux/slab.h> +#include <linux/device.h> +#include <linux/devfreq.h> +#include <linux/pm.h> +#include <linux/mutex.h> +#include <linux/module.h> +#include "governor.h" + +struct userspace_data { + unsigned long user_frequency; + bool valid; +}; + +static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq) +{ + struct userspace_data *data = df->data; + + if (data->valid) { + unsigned long adjusted_freq = data->user_frequency; + + if (df->max_freq && adjusted_freq > df->max_freq) + adjusted_freq = df->max_freq; + + if (df->min_freq && adjusted_freq < df->min_freq) + adjusted_freq = df->min_freq; + + *freq = adjusted_freq; + } else { + *freq = df->previous_freq; /* No user freq specified yet */ + } + return 0; +} + +static ssize_t store_freq(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + unsigned long wanted; + int err = 0; + + + mutex_lock(&devfreq->lock); + data = devfreq->data; + + sscanf(buf, "%lu", &wanted); + data->user_frequency = wanted; + data->valid = true; + err = update_devfreq(devfreq); + if (err == 0) + err = count; + mutex_unlock(&devfreq->lock); + return err; +} + +static ssize_t show_freq(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct devfreq *devfreq = to_devfreq(dev); + struct userspace_data *data; + int err = 0; + + mutex_lock(&devfreq->lock); + data = devfreq->data; + + if (data->valid) + err = sprintf(buf, "%lu\n", data->user_frequency); + else + err = sprintf(buf, "undefined\n"); + mutex_unlock(&devfreq->lock); + return err; +} + +static DEVICE_ATTR(set_freq, 0644, show_freq, store_freq); +static struct attribute *dev_entries[] = { + &dev_attr_set_freq.attr, + NULL, +}; +static struct attribute_group dev_attr_group = { + .name = "userspace", + .attrs = dev_entries, +}; + +static int userspace_init(struct devfreq *devfreq) +{ + int err = 0; + struct userspace_data *data = kzalloc(sizeof(struct userspace_data), + GFP_KERNEL); + + if (!data) { + err = -ENOMEM; + goto out; + } + data->valid = false; + devfreq->data = data; + + err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group); +out: + return err; +} + +static void userspace_exit(struct devfreq *devfreq) +{ + sysfs_remove_group(&devfreq->dev.kobj, &dev_attr_group); + kfree(devfreq->data); + devfreq->data = NULL; +} + +static int devfreq_userspace_handler(struct devfreq *devfreq, + unsigned int event, void *data) +{ + int ret = 0; + + switch (event) { + case DEVFREQ_GOV_START: + ret = userspace_init(devfreq); + break; + case DEVFREQ_GOV_STOP: + userspace_exit(devfreq); + break; + default: + break; + } + + return ret; +} + +static struct devfreq_governor devfreq_userspace = { + .name = "userspace", + .get_target_freq = devfreq_userspace_func, + .event_handler = devfreq_userspace_handler, +}; + +static int __init devfreq_userspace_init(void) +{ + return devfreq_add_governor(&devfreq_userspace); +} +subsys_initcall(devfreq_userspace_init); + +static void __exit devfreq_userspace_exit(void) +{ + int ret; + + ret = devfreq_remove_governor(&devfreq_userspace); + if (ret) + pr_err("%s: failed remove governor %d\n", __func__, ret); + + return; +} +module_exit(devfreq_userspace_exit); +MODULE_LICENSE("GPL"); diff --git a/drivers/devfreq/scx35_dmc.c b/drivers/devfreq/scx35_dmc.c new file mode 100644 index 00000000..539cef66 --- /dev/null +++ b/drivers/devfreq/scx35_dmc.c @@ -0,0 +1,852 @@ +/* + * Copyright (C) 2013 Spreadtrum Communications Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * 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. + */ + +#include <linux/io.h> +#include <linux/jiffies.h> +#include <linux/slab.h> +#include <linux/math64.h> +#include <linux/spinlock.h> +#include <linux/suspend.h> +#include <linux/opp.h> +#include <linux/devfreq.h> +#include <linux/platform_device.h> +#include <linux/regulator/consumer.h> +#include <linux/module.h> +#include <linux/irq.h> +#include <linux/interrupt.h> +#include <linux/bitops.h> +#include <mach/irqs.h> +#include <soc/sprd/hardware.h> +#include <linux/cpu.h> + +#ifdef CONFIG_BUS_MONITOR +#include <soc/sprd/busmonitor.h> +#endif +#define EMC_FREQ_NORMAL_SWITCH_SENE 0x02 +#define EMC_FREQ_DEEP_SLEEP_SENE 0x03 +#define EMC_FREQ_RESUME_SENE 0x04 + +#define CP2AP_INT_CTRL (SPRD_IPI_BASE + 0x04) +#define CP0_AP_MCU_IRQ1_CLR BIT(2) +#define CP1_AP_MCU_IRQ1_CLR BIT(6) +#define CPT_SHARE_MEM (CPT_RING_ADDR + 0x880) +#define CPW_SHARE_MEM (CPW_RING_ADDR + 0x880) + +/*#define DFS_AUTO_TEST*/ + +#ifdef DFS_AUTO_TEST +static u32 get_sys_cnt(void) +{ + return __raw_readl(SPRD_GPTIMER_BASE + 0x44); +} +#endif + +extern u32 emc_clk_set(u32 new_clk, u32 sene); +extern u32 emc_clk_get(void); +#ifdef CONFIG_SMP +extern int scxx30_all_nonboot_cpus_died(void); +#endif + +#define MIN_FREQ_CNT (1) +static DEFINE_SPINLOCK(min_freq_cnt_lock); + +enum scxx30_dmc_type { + TYPE_DMC_SCXX30 , +}; + +enum dmcclk_level_idx { + LV_0 = 0, + LV_1, + LV_2, + LV_3, + LV_4, + _LV_END +}; + +struct dmc_opp_table { + unsigned int idx; + unsigned long clk; /* KHz */ + unsigned long volt; /* uv */ + unsigned long bandwidth; /* MB/s, max: clk*2*32/8 */ +}; + +static struct dmc_opp_table scxx30_dmcclk_table[] = { +#ifdef CONFIG_ARCH_SCX15 + {LV_0, 332000, 1200000, 2656}, + {LV_1, 192000, 1200000, 1600}, +#else +#ifdef CONFIG_ARCH_SCX35 + {LV_0, 464000, 1200000, 3712}, + {LV_1, 384000, 1200000, 2656}, + {LV_2, 200000, 1200000, 1600}, +#endif +#endif +#ifdef CONFIG_ARCH_SCX30G + {LV_0, 400000, 1200000, 3200}, + {LV_1, 384000, 1200000, 2656}, + {LV_2, 200000, 1200000, 1600}, +#endif + {0, 0, 0}, +}; + +struct dmcfreq_data { + enum scxx30_dmc_type type; + struct device *dev; + struct devfreq *devfreq; + bool disabled; + struct opp *curr_opp; +#ifdef CONFIG_SIPC_TD + void __iomem *cpt_share_mem_base; +#endif +#ifdef CONFIG_SIPC_WCDMA + void __iomem *cpw_share_mem_base; +#endif + struct notifier_block pm_notifier; + unsigned long last_jiffies; + unsigned long quirk_jiffies; + spinlock_t lock; +}; + +#ifdef CONFIG_ARCH_SCX15 +#define SCXX30_LV_NUM (LV_2) +#define SCXX30_MAX_FREQ (332000) +#define SCXX30_MIN_FREQ (192000) +#else +#ifdef CONFIG_ARCH_SCX35 +#define SCXX30_LV_NUM (LV_3) +#define SCXX30_MAX_FREQ (464000) +#define SCXX30_MIN_FREQ (200000) +#endif +#endif +#ifdef CONFIG_ARCH_SCX30G +#define SCXX30_LV_NUM (LV_3) +#define SCXX30_MAX_FREQ (400000) +#define SCXX30_MIN_FREQ (200000) +#endif + +#define SCXX30_INITIAL_FREQ SCXX30_MAX_FREQ +#define SCXX30_POLLING_MS (100) +#define BOOT_TIME (40*HZ) +static u32 boot_done; +static unsigned int min_freq_cnt = 0; +static u32 request_quirk = 0; +#if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_SCX35_DMC_FREQ_AP) +static struct dmcfreq_data *g_dmcfreq_data; +#endif +static void inline scxx30_set_max(struct dmcfreq_data *data); +static void inline scxx30_set_min_deep_sleep(struct dmcfreq_data *data); + +extern bool dfs_get_enable(void); + +int devfreq_request_ignore(void) +{ + if((emc_clk_get()*1000) > SCXX30_MIN_FREQ ) + return 1; + else + return 0; +} + +void devfreq_min_freq_cnt_reset(unsigned int cnt, unsigned int quirk) +{ + + if(cnt >= MIN_FREQ_CNT){ + cnt = MIN_FREQ_CNT; + } + spin_lock(&min_freq_cnt_lock); + min_freq_cnt = cnt; + request_quirk = quirk; + spin_unlock(&min_freq_cnt_lock); +} +#if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_SCX35_DMC_FREQ_AP) +static int devfreq_cpu_callback(struct notifier_block *nfb, + unsigned long action, void *hcpu) +{ + long cpu = (long)hcpu; + int err = 0; + + if(!dfs_get_enable()) + return notifier_from_errno(err); + + switch (action) { + case CPU_UP_PREPARE: + if(num_online_cpus() == 1 && scxx30_all_nonboot_cpus_died() ){ + if(g_dmcfreq_data){ + printk("*** %s, CPU_UP_PREPARE set ddr freq max ***\n", __func__ ); + scxx30_set_max(g_dmcfreq_data); + } + } + break; + /* set 200M ddr clk when C0 is only active in MP Core case like TShark */ + case CPU_DEAD: + case CPU_DEAD_FROZEN: + if(num_online_cpus() == 1 && scxx30_all_nonboot_cpus_died() ){ + if(g_dmcfreq_data){ + scxx30_set_min_deep_sleep(g_dmcfreq_data); + printk("*** %s, CPU_DEAD&FROZEN set ddr freq min for deep sleep ***\n", __func__ ); + } + } + break; + } + + return notifier_from_errno(err); +} + +static struct notifier_block devfreq_cpu_notifier = { + &devfreq_cpu_callback, NULL, 0 +}; +#endif + +/************ devfreq notifier *****************/ +static LIST_HEAD(devfreq_dbs_handlers); +static DEFINE_MUTEX(devfreq_dbs_lock); + +/* register a callback function called before DDR frequency change +* @handler: callback function +*/ +int devfreq_notifier_register(struct devfreq_dbs *handler) +{ + + struct list_head *pos; + struct devfreq_dbs *e; + + mutex_lock(&devfreq_dbs_lock); + list_for_each(pos, &devfreq_dbs_handlers) { + e = list_entry(pos, struct devfreq_dbs, link); + if(e == handler){ + printk("***** %s, %pf already exsited ****\n", + __func__, e->devfreq_notifier); + return -1; + } + } + list_for_each(pos, &devfreq_dbs_handlers) { + struct devfreq_dbs *e; + e = list_entry(pos, struct devfreq_dbs, link); + if (e->level > handler->level) + break; + } + list_add_tail(&handler->link, pos); + mutex_unlock(&devfreq_dbs_lock); + + return 0; +} +EXPORT_SYMBOL(devfreq_notifier_register); + +/* unregister a callback function called before DDR frequency change +* @handler: callback function +*/ +int devfreq_notifier_unregister(struct devfreq_dbs *handler) +{ + mutex_lock(&devfreq_dbs_lock); + list_del(&handler->link); + mutex_unlock(&devfreq_dbs_lock); + + return 0; +} +EXPORT_SYMBOL(devfreq_notifier_unregister); + +static unsigned int devfreq_change_notification(unsigned int state) +{ + struct devfreq_dbs *pos; + int forbidden; + + mutex_lock(&devfreq_dbs_lock); + list_for_each_entry(pos, &devfreq_dbs_handlers, link) { + if (pos->devfreq_notifier != NULL) { + pr_debug("%s: state:%u, calling %pf\n", __func__, state, pos->devfreq_notifier); + forbidden = pos->devfreq_notifier(pos, state); + if(forbidden){ + mutex_unlock(&devfreq_dbs_lock); + return forbidden; + } + pr_debug("%s: calling %pf done \n", + __func__, pos->devfreq_notifier ); + } + } + mutex_unlock(&devfreq_dbs_lock); + return 0; +} +/************ devfreq notifier *****************/ + +static unsigned long scxx30_max_freq(struct dmcfreq_data *data) +{ + switch (data->type) { + case TYPE_DMC_SCXX30: + return SCXX30_MAX_FREQ; + default: + pr_err("Cannot determine the device id %d\n", data->type); + return (-EINVAL); + } +} + +static unsigned long scxx30_min_freq(struct dmcfreq_data *data) +{ + switch (data->type) { + case TYPE_DMC_SCXX30: + return SCXX30_MIN_FREQ; + default: + pr_err("Cannot determine the device id %d\n", data->type); + return (-EINVAL); + } +} + +/* +* convert bandwidth request to DDR freq +* @bw, request bandwidth, KB +* @return, KHz +*/ +static int scxx30_convert_bw_to_freq(int bw) +{ + int freq; + + freq = 0; + /*freq = dmc_convert_bw_to_freq(bw);*/ + /* + * freq(KHz)*2(DDR)*32(BUS width)/8 = bw(KB)*4(efficiency ratio 25%) + */ + freq = bw/2; + + return freq; +} + +static int scxx30_dmc_target(struct device *dev, unsigned long *_freq, + u32 flags) +{ + int err = 0; + int cnt = 0; + struct platform_device *pdev = container_of(dev, struct platform_device, dev); + struct dmcfreq_data *data = platform_get_drvdata(pdev); + struct opp *opp = devfreq_recommended_opp(dev, _freq, flags); + unsigned long freq = opp_get_freq(opp); + unsigned long old_freq = emc_clk_get()*1000 ; + unsigned char cp_req; + unsigned long spinlock_flags; + unsigned long range = msecs_to_jiffies(SCXX30_POLLING_MS)/5; +#ifdef DFS_AUTO_TEST + u32 start_t1, end_t1; + static u32 max_u_time = 0; + u32 current_u_time; +#endif + + if(time_before(jiffies, boot_done)){ + return 0; + } + + if(!dfs_get_enable()) + return 0; + + if (IS_ERR(opp)) + return PTR_ERR(opp); + + pr_debug("*** %s, old_freq:%luKHz, freq:%luKHz ***\n", __func__, old_freq, freq); + +#ifdef CONFIG_SCX35_DMC_FREQ_AP + if (num_online_cpus() != 1){ + printk("*** %s, num_online_cpus:%d ***\n", __func__, num_online_cpus() ); + return 0; + } +#endif + + if (old_freq == freq) + return 0; + + /* + * if sampling timer is just later than last quirk request in 20ms, + * keep current frequency + */ + if(freq==scxx30_min_freq(data) && + time_in_range(jiffies, data->quirk_jiffies, data->quirk_jiffies+range) ){ + return 0; + } + spin_lock(&min_freq_cnt_lock); + cnt = min_freq_cnt; + spin_unlock(&min_freq_cnt_lock); + + if (scxx30_min_freq(data)==freq && cnt<MIN_FREQ_CNT){ + /* + *DDR frequency drops down more conservatively + */ + spin_lock(&min_freq_cnt_lock); + min_freq_cnt++; + spin_unlock(&min_freq_cnt_lock); + freq += 1; + opp = devfreq_recommended_opp(dev, &freq, flags); + freq = opp_get_freq(opp); + } else { + spin_lock(&min_freq_cnt_lock); + min_freq_cnt = 0; + spin_unlock(&min_freq_cnt_lock); + } + + pr_debug("*** %s, min_freq_cnt:%d***\n", __func__, min_freq_cnt); + +#ifdef CONFIG_SIPC_TD + if(data->cpt_share_mem_base){ + cp_req = readb(data->cpt_share_mem_base); + if(cp_req){ + printk("*** %s, cpt:cp_req:%u ***\n", __func__, cp_req); + return 0; + } + } +#endif +#ifdef CONFIG_SIPC_WCDMA + if(data->cpw_share_mem_base){ + cp_req = readb(data->cpw_share_mem_base); + if(cp_req){ + printk("*** %s, cpw:cp_req:%u ***\n", __func__, cp_req); + return 0; + } + } +#endif + dev_dbg(dev, "targetting %lukHz %luuV\n", freq, opp_get_voltage(opp)); + freq = freq/1000; /* conver KHz to MHz */ + + /* Keep the current frequency if forbidden */ + if( devfreq_change_notification(DEVFREQ_PRE_CHANGE) ){ + return 0; + } + +#ifdef CONFIG_SCX35_DMC_FREQ_AP + spin_lock_irqsave(&data->lock, spinlock_flags); +#ifdef DFS_AUTO_TEST + start_t1 = get_sys_cnt(); +#endif +#ifdef CONFIG_SMP + if (!scxx30_all_nonboot_cpus_died() || data->disabled){ +#else + if (data->disabled){ +#endif +#else + spin_lock(&data->lock); + if (data->disabled){ +#endif + printk("*** %s, data->disabled, goto out ***\n", __func__ ); + goto out; + } + /* + *TODO:time spent on emc_clk_set() should be optimized + */ + err = emc_clk_set(freq, EMC_FREQ_NORMAL_SWITCH_SENE); //tdpll 192 + data->curr_opp = opp; + +out: +#ifdef CONFIG_SCX35_DMC_FREQ_AP +#ifdef DFS_AUTO_TEST + end_t1 = get_sys_cnt(); + + current_u_time = (start_t1 - end_t1)/128; + if(max_u_time < current_u_time) { + max_u_time = current_u_time; + } + printk("**************dfs %s use current = %08u max %08u\n", __func__, current_u_time, max_u_time); +#endif + spin_unlock_irqrestore(&data->lock, spinlock_flags); +#else + spin_unlock(&data->lock); +#endif + devfreq_change_notification(DEVFREQ_POST_CHANGE); + pr_debug("*** %s, old_freq:%luKHz, set emc done, err:%d, current freq:%uKHz ***\n", + __func__, old_freq, err, emc_clk_get()*1000 ); + return err; +} + +static int scxx30_dmc_get_dev_status(struct device *dev, + struct devfreq_dev_status *stat) +{ +#ifdef CONFIG_BUS_MONITOR + struct dmcfreq_data *data = dev_get_drvdata(dev); + u32 total_bw; + u64 trans_bw; + u32 interval; + dmc_mon_cnt_stop(); + trans_bw = (u64)dmc_mon_cnt_bw(); /* total access: B */ + dmc_mon_cnt_clr(); + dmc_mon_cnt_start(); + interval = jiffies - data->last_jiffies; + data->last_jiffies = jiffies; + + if(request_quirk) + data->quirk_jiffies = jiffies; + + stat->current_frequency = emc_clk_get() * 1000; /* KHz */ + /* stat->current_frequency = opp_get_freq(data->curr_opp); */ + total_bw = (stat->current_frequency)*8; /* freq*2*32/8 */ + pr_debug("*** %s, trans_bw:%lluB, curr freq:%lu, total_bw:%uKB ***\n", + __func__, trans_bw, stat->current_frequency, total_bw); + + /* + * TODO: efficiency ratio could be more accurate?? + */ + if(interval){ + stat->busy_time = (u32)div_u64(trans_bw*HZ, interval); /* BW: B/s */ + stat->total_time = total_bw*350 ; /* BW: KB*1000*(efficiency ratio 35%) B/s */ + }else{ + stat->busy_time = (u32)div_u64(trans_bw*HZ, 1); /* BW: B/s */ + stat->total_time = total_bw*350 ; /* BW: KB*1000*(efficiency ratio 35%) B/s */ + } + pr_debug("*** %s, interval:%u, busy_time:%lu, totoal_time:%lu ***\n", + __func__, interval, stat->busy_time, stat->total_time ); +#else + stat->busy_time = 0 ; + stat->total_time = 0 ; +#endif + return 0; +} + + +static void scxx30_dmc_exit(struct device *dev) +{ + struct dmcfreq_data *data = dev_get_drvdata(dev); + + devfreq_unregister_opp_notifier(dev, data->devfreq); + + return; +} + +static struct devfreq_dev_profile scxx30_dmcfreq_profile = { + .initial_freq = SCXX30_INITIAL_FREQ, + .polling_ms = SCXX30_POLLING_MS, + .target = scxx30_dmc_target, + .get_dev_status = scxx30_dmc_get_dev_status, + .exit = scxx30_dmc_exit, +}; + +static int scxx30_init_tables(struct dmcfreq_data *data) +{ + int i, err; + + switch (data->type) { + case TYPE_DMC_SCXX30: + for (i = LV_0; i < SCXX30_LV_NUM; i++) { + err = opp_add(data->dev, scxx30_dmcclk_table[i].clk, + scxx30_dmcclk_table[i].volt); + if (err) { + dev_err(data->dev, "Cannot add opp entries.\n"); + return err; + } + } + break; + default: + dev_err(data->dev, "Cannot determine the device id %d\n", data->type); + err = -EINVAL; + } + + return err; +} + +static int scxx30_dmcfreq_pm_notifier(struct notifier_block *this, + unsigned long event, void *ptr) +{ + struct dmcfreq_data *data = container_of(this, struct dmcfreq_data, + pm_notifier); + + unsigned long flags; + printk("*** %s, event:0x%x, set freq:%d ***\n", __func__, event, SCXX30_MIN_FREQ/1000); + + switch (event) { + case PM_SUSPEND_PREPARE: + + /* + * DMC must be set 200MHz before deep sleep in ES chips + */ +#ifdef CONFIG_SCX35_DMC_FREQ_AP + spin_lock_irqsave(&data->lock, flags); +#ifdef CONFIG_SMP + if (!scxx30_all_nonboot_cpus_died()) + { + spin_unlock_irqrestore(&data->lock, flags); + return NOTIFY_DONE; + } +#endif + data->disabled = true; + if(dfs_get_enable()) + { + emc_clk_set(SCXX30_MIN_FREQ/1000, EMC_FREQ_NORMAL_SWITCH_SENE); /*nomarl switch to tdpll SCXX30_MIN_FREQ/1000*/ + emc_clk_set(SCXX30_MIN_FREQ/1000, EMC_FREQ_DEEP_SLEEP_SENE); /*deep sleep to dpll SCXX30_MIN_FREQ/1000*/ + } + spin_unlock_irqrestore(&data->lock, flags); +#else + spin_lock(&data->lock); + data->disabled = true; + emc_clk_set(SCXX30_MIN_FREQ/1000, EMC_FREQ_NORMAL_SWITCH_SENE); /*nomarl switch to tdpll SCXX30_MIN_FREQ/1000*/ + spin_unlock(&data->lock); +#endif + return NOTIFY_OK; + case PM_POST_RESTORE: + case PM_POST_SUSPEND: + /* Reactivate */ +#ifdef CONFIG_SCX35_DMC_FREQ_AP + spin_lock_irqsave(&data->lock, flags); +#ifdef CONFIG_SMP + if (!scxx30_all_nonboot_cpus_died()) + { + spin_unlock_irqrestore(&data->lock, flags); + return NOTIFY_DONE; + } +#endif + if(dfs_get_enable()) + { + emc_clk_set(SCXX30_MIN_FREQ/1000, EMC_FREQ_RESUME_SENE); /*resume dpll -- tdpll SCXX30_MIN_FREQ*/ + } + data->disabled = false; + spin_unlock_irqrestore(&data->lock, flags); +#else + spin_lock(&data->lock); + /* shark does not care EMC_FREQ_XX_SENE, dolphin only*/ + emc_clk_set(SCXX30_MIN_FREQ/1000, EMC_FREQ_RESUME_SENE); /*resume dpll -- tdpll SCXX30_MIN_FREQ*/ + data->disabled = false; + spin_unlock(&data->lock); +#endif + return NOTIFY_OK; + } + + return NOTIFY_DONE; +} + +static void inline scxx30_set_max(struct dmcfreq_data *data) +{ + unsigned long max; + unsigned long flags; + + spin_lock_irqsave(&data->lock, flags); + max = scxx30_max_freq(data); + emc_clk_set(SCXX30_MAX_FREQ/1000, EMC_FREQ_NORMAL_SWITCH_SENE); + spin_unlock_irqrestore(&data->lock, flags); + +} + +static void inline scxx30_set_min_deep_sleep(struct dmcfreq_data *data) +{ + unsigned long min; + unsigned long flags; + + spin_lock_irqsave(&data->lock, flags); + min = scxx30_min_freq(data); + emc_clk_set(SCXX30_MIN_FREQ/1000, EMC_FREQ_NORMAL_SWITCH_SENE); /*nomarl switch to tdpll SCXX30_MIN_FREQ/1000*/ + emc_clk_set(SCXX30_MIN_FREQ/1000, EMC_FREQ_DEEP_SLEEP_SENE); /*deep sleep to dpll SCXX30_MIN_FREQ/1000*/ + spin_unlock_irqrestore(&data->lock, flags); + +} + +static irqreturn_t scxx30_cp0_irq_handler(int irq, void *data) +{ + struct dmcfreq_data *usr = (struct dmcfreq_data *)data; + scxx30_set_max(usr); + __raw_writel(CP0_AP_MCU_IRQ1_CLR, CP2AP_INT_CTRL); + + return IRQ_HANDLED; +} + +static irqreturn_t scxx30_cp1_irq_handler(int irq, void *data) +{ + + struct dmcfreq_data *usr = (struct dmcfreq_data *)data; + scxx30_set_max(usr); + __raw_writel(CP1_AP_MCU_IRQ1_CLR, CP2AP_INT_CTRL); + + return IRQ_HANDLED; +} + +static int scxx30_dmcfreq_probe(struct platform_device *pdev) +{ + struct dmcfreq_data *data; + struct opp *opp; + struct device *dev = &pdev->dev; + int err = 0; + + data = kzalloc(sizeof(struct dmcfreq_data), GFP_KERNEL); + if (data == NULL) { + dev_err(dev, "Cannot allocate memory.\n"); + return -ENOMEM; + } + + data->type = pdev->id_entry->driver_data; + data->pm_notifier.notifier_call = scxx30_dmcfreq_pm_notifier; + data->dev = dev; + spin_lock_init(&data->lock); + + switch (data->type) { + case TYPE_DMC_SCXX30: + err = scxx30_init_tables(data); + break; + default: + dev_err(dev, "Cannot determine the device id %d\n", data->type); + err = -EINVAL; + } + if (err) + goto err_opp_add; + + opp = opp_find_freq_floor(dev, &scxx30_dmcfreq_profile.initial_freq); + if (IS_ERR(opp)) { + dev_err(dev, "Invalid initial frequency %lu kHz.\n", + scxx30_dmcfreq_profile.initial_freq); + err = PTR_ERR(opp); + goto err_opp_add; + } + data->curr_opp = opp; + data->last_jiffies = jiffies; + platform_set_drvdata(pdev, data); + data->devfreq = devfreq_add_device(dev, &scxx30_dmcfreq_profile, + "ondemand", scxx30_convert_bw_to_freq); + if (IS_ERR(data->devfreq)) { + err = PTR_ERR(data->devfreq); + dev_err(dev, "Failed to add device\n"); + goto err_opp_add; + } + + devfreq_register_opp_notifier(dev, data->devfreq); + + data->devfreq->min_freq = scxx30_min_freq(data); + data->devfreq->max_freq = scxx30_max_freq(data); + + err = register_pm_notifier(&data->pm_notifier); + if (err) { + dev_err(dev, "Failed to setup pm notifier\n"); + goto err_devfreq_add; + } +#ifdef CONFIG_BUS_MONITOR + dmc_mon_cnt_clr( ); + dmc_mon_cnt_start( ); +#endif + boot_done = jiffies + BOOT_TIME; + + /* register isr */ + err = request_irq(IRQ_CP0_MCU1_INT, scxx30_cp0_irq_handler, IRQF_DISABLED, "dfs_cp0_int1", data); + if (err) { + printk(KERN_ERR ": failed to cp0 int1 request irq!\n"); + err = -EINVAL; + goto err_devfreq_add; + } + err = request_irq(IRQ_CP1_MCU1_INT, scxx30_cp1_irq_handler, IRQF_DISABLED, "dfs_cp1_int1", data); + if (err) { + printk(KERN_ERR ": failed to cp1 int1 request irq!\n"); + err = -EINVAL; + goto err_cp1_irq; + } +#ifdef CONFIG_SIPC_TD + data->cpt_share_mem_base = ioremap(CPT_SHARE_MEM, 128); + if (!data->cpt_share_mem_base){ + printk("*** %s, remap CPT_SHARE_MEM error ***\n", __func__); + err = -ENOMEM; + goto err_irq; + } +#endif +#ifdef CONFIG_SIPC_WCDMA + data->cpw_share_mem_base = ioremap(CPW_SHARE_MEM, 128); + if (!data->cpw_share_mem_base){ + printk("*** %s, remap CPW_SHARE_MEM error ***\n", __func__); + err = -ENOMEM; + goto err_map; + } +#endif + +#if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_SCX35_DMC_FREQ_AP) + register_cpu_notifier(&devfreq_cpu_notifier); + g_dmcfreq_data = data; +#endif + pr_info(" %s done, current freq:%lu \n", __func__, opp_get_freq(data->curr_opp)); + return 0; + +err_map: +#ifdef CONFIG_SIPC_TD + iounmap(data->cpt_share_mem_base); +#endif +err_irq: + free_irq(IRQ_CP1_MCU1_INT, data); +err_cp1_irq: + free_irq(IRQ_CP0_MCU1_INT, data); +err_devfreq_add: + devfreq_remove_device(data->devfreq); +err_opp_add: + kfree(data); + return err; +} + +static int scxx30_dmcfreq_remove(struct platform_device *pdev) +{ + struct dmcfreq_data *data = platform_get_drvdata(pdev); + + free_irq(IRQ_CP0_MCU1_INT, data); + free_irq(IRQ_CP1_MCU1_INT, data); +#ifdef CONFIG_SIPC_TD + iounmap(data->cpt_share_mem_base); +#endif +#ifdef CONFIG_SIPC_WCDMA + iounmap(data->cpw_share_mem_base); +#endif + unregister_pm_notifier(&data->pm_notifier); + devfreq_remove_device(data->devfreq); + kfree(data); + return 0; +} + +static int scxx30_dmcfreq_resume(struct device *dev) +{ + struct dmcfreq_data *data = dev_get_drvdata(dev); + spin_lock(&data->lock); + data->disabled = false; + spin_unlock(&data->lock); +#ifdef CONFIG_BUS_MONITOR + dmc_mon_resume(); + dmc_mon_cnt_clr( ); + dmc_mon_cnt_start( ); +#endif + return 0; +} + +static const struct dev_pm_ops scxx30_dmcfreq_pm = { + .resume = scxx30_dmcfreq_resume, +}; + +static const struct platform_device_id scxx30_dmcfreq_id[] = { + { "scxx30-dmcfreq", TYPE_DMC_SCXX30 }, + { }, +}; + +static struct platform_device scxx30_dmcfreq = { + .name = "scxx30-dmcfreq", +}; + +static struct platform_driver scxx30_dmcfreq_driver = { + .probe = scxx30_dmcfreq_probe, + .remove = scxx30_dmcfreq_remove, + .id_table = scxx30_dmcfreq_id, + .driver = { + .name = "scxx30_dmcfreq", + .owner = THIS_MODULE, + .pm = &scxx30_dmcfreq_pm, + }, +}; + +static int __init scxx30_dmcfreq_init(void) +{ + int err; + err = platform_device_register(&scxx30_dmcfreq); + if(err){ + pr_err(" register scxx30_dmcfreq failed, err:%d\n", err); + } + err = platform_driver_register(&scxx30_dmcfreq_driver); + if(err){ + pr_err(" register scxx30_dmcfreq_driver failed, err:%d\n", err); + } + return err; +} +late_initcall(scxx30_dmcfreq_init); + +static void __exit scxx30_dmcfreq_exit(void) +{ + platform_driver_unregister(&scxx30_dmcfreq_driver); +} +module_exit(scxx30_dmcfreq_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("SCxx30 dmcfreq driver with devfreq framework"); |
