ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / arch / arm / mach-omap2 / dvfs.c
blob4b20f19fdb0701a5aa1f5b9cee1e7c907965bf65
1 /*
2 * OMAP3/OMAP4 DVFS Management Routines
4 * Author: Vishwanath BS <vishwanath.bs@ti.com>
6 * Copyright (C) 2011 Texas Instruments, Inc.
7 * Vishwanath BS <vishwanath.bs@ti.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/err.h>
15 #include <linux/spinlock.h>
16 #include <linux/plist.h>
17 #include <linux/slab.h>
18 #include <linux/opp.h>
19 #include <linux/clk.h>
20 #include <linux/debugfs.h>
21 #include <linux/seq_file.h>
22 #include <linux/pm_qos_params.h>
23 #include <plat/common.h>
24 #include <plat/omap_device.h>
25 #include <plat/omap_hwmod.h>
26 #include <plat/clock.h>
27 #include "dvfs.h"
28 #include "smartreflex.h"
29 #include "powerdomain.h"
30 #include "pm.h"
32 /**
33 * DOC: Introduction
34 * =================
35 * DVFS is a technique that uses the optimal operating frequency and voltage to
36 * allow a task to be performed in the required amount of time.
37 * OMAP processors have voltage domains whose voltage can be scaled to
38 * various levels depending on which the operating frequencies of certain
39 * devices belonging to the domain will also need to be scaled. This voltage
40 * frequency tuple is known as Operating Performance Point (OPP). A device
41 * can have multiple OPP's. Also a voltage domain could be shared between
42 * multiple devices. Also there could be dependencies between various
43 * voltage domains for maintaining system performance like VDD<X>
44 * should be at voltage v1 when VDD<Y> is at voltage v2.
46 * The design of this framework takes into account all the above mentioned
47 * points. To summarize the basic design of DVFS framework:-
49 * 1. Have device opp tables for each device whose operating frequency can be
50 * scaled. This is easy now due to the existance of hwmod layer which
51 * allow storing of device specific info. The device opp tables contain
52 * the opp pairs (frequency voltage tuples), the voltage domain pointer
53 * to which the device belongs to, the device specific set_rate and
54 * get_rate API's which will do the actual scaling of the device frequency
55 * and retrieve the current device frequency.
56 * 2. Introduce use counting on a per VDD basis. This is to take care multiple
57 * requests to scale a VDD. The VDD will be scaled to the maximum of the
58 * voltages requested.
59 * 3. Keep track of all scalable devices belonging to a particular voltage
60 * domain the voltage layer.
61 * 4. Keep track of frequency requests for each of the device. This will enable
62 * to scale individual devices to different frequency (even w/o scaling
63 * voltage aka frequency throttling)
64 * 5. Generic dvfs API that can be called by anybody to scale a device opp.
65 * This API takes the device pointer and frequency to which the device
66 * needs to be scaled to. This API then internally finds out the voltage
67 * domain to which the device belongs to and the voltage to which the voltage
68 * domain needs to be put to for the device to be scaled to the new frequency
69 * from the device opp table. Then this API will add requested frequency into
70 * the corresponding target device frequency list and add voltage request to
71 * the corresponding vdd. Subsequently it calls voltage scale function which
72 * will find out the highest requested voltage for the given vdd and scales
73 * the voltage to the required one and also adds corresponding frequency
74 * request for that voltage. It also runs through the list of all
75 * scalable devices belonging to this voltage domain and scale them to the
76 * appropriate frequencies using the set_rate pointer in the device opp
77 * tables.
78 * 6. Handle inter VDD dependecies. This will take care of scaling domain's voltage
79 * and frequency together.
82 * DOC: The Core DVFS data structure:
83 * ==================================
84 * Structure Name Example Tree
85 * ---------
86 * /|\ +-------------------+ +-------------------+
87 * | |User2 (dev2, freq2)+---\ |User4 (dev4, freq4)+---\
88 * | +-------------------+ | +-------------------+ |
89 * (struct omap_dev_user_list) | |
90 * | +-------------------+ | +-------------------+ |
91 * | |User1 (dev1, freq1)+---| |User3 (dev3, freq3)+---|
92 * \|/ +-------------------+ | +-------------------+ |
93 * --------- | |
94 * /|\ +------------+------+ +---------------+--+
95 * | | DEV1 (dev, | | DEV2 (dev) |
96 * (struct omap_vdd_dev_list)|omap_dev_user_list)| |omap_dev_user_list|
97 * | +------------+------+ +--+---------------+
98 * \|/ /|\ /-----+-------------+------> others..
99 * --------- Frequency |
100 * /|\ +--+------------------+
101 * | | VDD_n |
102 * | | (omap_vdd_dev_list, |
103 * (struct omap_vdd_dvfs_info)** | omap_vdd_user_list) |
104 * | +--+------------------+
105 * | | (ROOT NODE: omap_dvfs_info_list)
106 * \|/ |
107 * --------- Voltage \---+-------------+----------> others..
108 * /|\ \|/ +-------+----+ +-----+--------+
109 * | | vdd_user2 | | vdd_user3 |
110 * (struct omap_vdd_user_list) | (dev, volt)| | (dev, volt) |
111 * \|/ +------------+ +--------------+
112 * ---------
113 * Key: ** -> Root of the tree.
114 * NOTE: we use the priority to store the voltage/frequency
116 * For voltage dependency description, see: struct dependency:
117 * voltagedomain -> (description of the voltagedomain)
118 * omap_vdd_info -> (vdd information)
119 * omap_vdd_dep_info[]-> (stores array of depedency info)
120 * omap_vdd_dep_volt[] -> (stores array of maps)
121 * (main_volt -> dep_volt) (a singular map)
124 /* Macros to give idea about scaling directions */
125 #define DVFS_VOLT_SCALE_DOWN 0
126 #define DVFS_VOLT_SCALE_NONE 1
127 #define DVFS_VOLT_SCALE_UP 2
130 * struct omap_dev_user_list - Structure maitain userlist per devide
131 * @dev: The device requesting for a particular frequency
132 * @node: The list head entry
134 * Using this structure, user list (requesting dev * and frequency) for
135 * each device is maintained. This is how we can have different devices
136 * at different frequencies (to support frequency locking and throttling).
137 * Even if one of the devices in a given vdd has locked it's frequency,
138 * other's can still scale their frequency using this list.
139 * If no one has placed a frequency request for a device, then device is
140 * set to the frequency from it's opp table.
142 struct omap_dev_user_list {
143 struct device *dev;
144 struct plist_node node;
148 * struct omap_vdd_dev_list - Device list per vdd
149 * @dev: The device belonging to a particular vdd
150 * @node: The list head entry
151 * @freq_user_list: The list of users for vdd device
152 * @clk: frequency control clock for this dev
153 * @user_lock: The lock for plist manipulation
155 struct omap_vdd_dev_list {
156 struct device *dev;
157 struct list_head node;
158 struct plist_head freq_user_list;
159 struct clk *clk;
160 spinlock_t user_lock; /* spinlock for plist */
164 * struct omap_vdd_user_list - The per vdd user list
165 * @dev: The device asking for the vdd to be set at a particular
166 * voltage
167 * @node: The list head entry
169 struct omap_vdd_user_list {
170 struct device *dev;
171 struct plist_node node;
175 * struct omap_vdd_dvfs_info - The per vdd dvfs info
176 * @node: list node for vdd_dvfs_info list
177 * @user_lock: spinlock for plist operations
178 * @vdd_user_list: The vdd user list
179 * @voltdm: Voltage domains for which dvfs info stored
180 * @dev_list: Device list maintained per domain
182 * This is a fundamental structure used to store all the required
183 * DVFS related information for a vdd.
185 struct omap_vdd_dvfs_info {
186 struct list_head node;
188 spinlock_t user_lock; /* spin lock */
189 struct plist_head vdd_user_list;
190 struct voltagedomain *voltdm;
191 struct list_head dev_list;
194 static LIST_HEAD(omap_dvfs_info_list);
195 DEFINE_MUTEX(omap_dvfs_lock);
197 /* QoS expected */
198 static struct pm_qos_request_list omap_dvfs_pm_qos_handle;
200 /* Dvfs scale helper function */
201 static int _dvfs_scale(struct device *req_dev, struct device *target_dev,
202 struct omap_vdd_dvfs_info *tdvfs_info);
204 /* Few search functions to traverse and find pointers of interest */
207 * _dvfs_info_to_dev() - Locate the parent device associated to dvfs_info
208 * @dvfs_info: dvfs_info to search for
210 * Returns NULL on failure.
212 static struct device *_dvfs_info_to_dev(struct omap_vdd_dvfs_info *dvfs_info)
214 struct omap_vdd_dev_list *tmp_dev;
215 if (IS_ERR_OR_NULL(dvfs_info))
216 return NULL;
217 if (list_empty(&dvfs_info->dev_list))
218 return NULL;
219 tmp_dev = list_first_entry(&dvfs_info->dev_list,
220 struct omap_vdd_dev_list, node);
221 return tmp_dev->dev;
225 * _dev_to_dvfs_info() - Locate the dvfs_info for a device
226 * @dev: dev to search for
228 * Returns NULL on failure.
230 static struct omap_vdd_dvfs_info *_dev_to_dvfs_info(struct device *dev)
232 struct omap_vdd_dvfs_info *dvfs_info;
233 struct omap_vdd_dev_list *temp_dev;
235 if (IS_ERR_OR_NULL(dev))
236 return NULL;
238 list_for_each_entry(dvfs_info, &omap_dvfs_info_list, node) {
239 list_for_each_entry(temp_dev, &dvfs_info->dev_list, node) {
240 if (temp_dev->dev == dev)
241 return dvfs_info;
245 return NULL;
249 * _voltdm_to_dvfs_info() - Locate a dvfs_info given a voltdm pointer
250 * @voltdm: voltdm to search for
252 * Returns NULL on failure.
254 static
255 struct omap_vdd_dvfs_info *_voltdm_to_dvfs_info(struct voltagedomain *voltdm)
257 struct omap_vdd_dvfs_info *dvfs_info;
259 if (IS_ERR_OR_NULL(voltdm))
260 return NULL;
262 list_for_each_entry(dvfs_info, &omap_dvfs_info_list, node) {
263 if (dvfs_info->voltdm == voltdm)
264 return dvfs_info;
267 return NULL;
271 * _volt_to_opp() - Find OPP corresponding to a given voltage
272 * @dev: device pointer associated with the OPP list
273 * @volt: voltage to search for in uV
275 * Searches for exact match in the OPP list and returns handle to the matching
276 * OPP if found, else return the max available OPP.
277 * If there are multiple opps with same voltage, it will return
278 * the first available entry. Return pointer should be checked against IS_ERR.
280 * NOTE: since this uses OPP functions, use under rcu_lock. This function also
281 * assumes that the cpufreq table and OPP table are in sync - any modifications
282 * to either should be synchronized.
284 static struct opp *_volt_to_opp(struct device *dev, unsigned long volt)
286 struct opp *opp = ERR_PTR(-ENODEV);
287 unsigned long f = 0;
289 do {
290 opp = opp_find_freq_ceil(dev, &f);
291 if (IS_ERR(opp)) {
293 * if there is no OPP for corresponding volt
294 * then return max available instead
296 opp = opp_find_freq_floor(dev, &f);
297 break;
299 if (opp_get_voltage(opp) >= volt)
300 break;
301 f++;
302 } while (1);
304 return opp;
307 /* rest of the helper functions */
309 * _add_vdd_user() - Add a voltage request
310 * @dvfs_info: omap_vdd_dvfs_info pointer for the required vdd
311 * @dev: device making the request
312 * @volt: requested voltage in uV
314 * Adds the given device's voltage request into corresponding
315 * vdd's omap_vdd_dvfs_info user list (plist). This list is used
316 * to find the maximum voltage request for a given vdd.
318 * Returns 0 on success.
320 static int _add_vdd_user(struct omap_vdd_dvfs_info *dvfs_info,
321 struct device *dev, unsigned long volt)
323 struct omap_vdd_user_list *user = NULL, *temp_user;
325 if (!dvfs_info || IS_ERR(dvfs_info)) {
326 dev_warn(dev, "%s: VDD specified does not exist!\n", __func__);
327 return -EINVAL;
330 spin_lock(&dvfs_info->user_lock);
331 plist_for_each_entry(temp_user, &dvfs_info->vdd_user_list, node) {
332 if (temp_user->dev == dev) {
333 user = temp_user;
334 break;
338 if (!user) {
339 user = kzalloc(sizeof(struct omap_vdd_user_list), GFP_ATOMIC);
340 if (!user) {
341 dev_err(dev,
342 "%s: Unable to creat a new user for vdd_%s\n",
343 __func__, dvfs_info->voltdm->name);
344 spin_unlock(&dvfs_info->user_lock);
345 return -ENOMEM;
347 user->dev = dev;
348 } else {
349 plist_del(&user->node, &dvfs_info->vdd_user_list);
352 plist_node_init(&user->node, volt);
353 plist_add(&user->node, &dvfs_info->vdd_user_list);
355 spin_unlock(&dvfs_info->user_lock);
356 return 0;
360 * _remove_vdd_user() - Remove a voltage request
361 * @dvfs_info: omap_vdd_dvfs_info pointer for the required vdd
362 * @dev: device making the request
364 * Removes the given device's voltage request from corresponding
365 * vdd's omap_vdd_dvfs_info user list (plist).
367 * Returns 0 on success.
369 static int _remove_vdd_user(struct omap_vdd_dvfs_info *dvfs_info,
370 struct device *dev)
372 struct omap_vdd_user_list *user = NULL, *temp_user;
373 int ret = 0;
375 if (!dvfs_info || IS_ERR(dvfs_info)) {
376 dev_err(dev, "%s: VDD specified does not exist!\n", __func__);
377 return -EINVAL;
380 spin_lock(&dvfs_info->user_lock);
381 plist_for_each_entry(temp_user, &dvfs_info->vdd_user_list, node) {
382 if (temp_user->dev == dev) {
383 user = temp_user;
384 break;
388 if (user)
389 plist_del(&user->node, &dvfs_info->vdd_user_list);
390 else {
391 dev_err(dev, "%s: Unable to find the user for vdd_%s\n",
392 __func__, dvfs_info->voltdm->name);
393 ret = -ENOENT;
396 spin_unlock(&dvfs_info->user_lock);
397 kfree(user);
399 return ret;
403 * _add_freq_request() - Add a requested device frequency
404 * @dvfs_info: omap_vdd_dvfs_info pointer for the required vdd
405 * @req_dev: device making the request
406 * @target_dev: target device for which frequency request is being made
407 * @freq: target device frequency
409 * This adds a requested frequency into target device's frequency list.
411 * Returns 0 on success.
413 static int _add_freq_request(struct omap_vdd_dvfs_info *dvfs_info,
414 struct device *req_dev, struct device *target_dev, unsigned long freq)
416 struct omap_dev_user_list *dev_user = NULL, *tmp_user;
417 struct omap_vdd_dev_list *temp_dev;
419 if (!dvfs_info || IS_ERR(dvfs_info)) {
420 dev_warn(target_dev, "%s: VDD specified does not exist!\n",
421 __func__);
422 return -EINVAL;
425 list_for_each_entry(temp_dev, &dvfs_info->dev_list, node) {
426 if (temp_dev->dev == target_dev)
427 break;
430 if (temp_dev->dev != target_dev) {
431 dev_warn(target_dev, "%s: target_dev does not exist!\n",
432 __func__);
433 return -EINVAL;
436 spin_lock(&temp_dev->user_lock);
437 plist_for_each_entry(tmp_user, &temp_dev->freq_user_list, node) {
438 if (tmp_user->dev == req_dev) {
439 dev_user = tmp_user;
440 break;
444 if (!dev_user) {
445 dev_user = kzalloc(sizeof(struct omap_dev_user_list),
446 GFP_ATOMIC);
447 if (!dev_user) {
448 dev_err(target_dev,
449 "%s: Unable to creat a new user for vdd_%s\n",
450 __func__, dvfs_info->voltdm->name);
451 spin_unlock(&temp_dev->user_lock);
452 return -ENOMEM;
454 dev_user->dev = req_dev;
455 } else {
456 plist_del(&dev_user->node, &temp_dev->freq_user_list);
459 plist_node_init(&dev_user->node, freq);
460 plist_add(&dev_user->node, &temp_dev->freq_user_list);
461 spin_unlock(&temp_dev->user_lock);
462 return 0;
466 * _remove_freq_request() - Remove the requested device frequency
468 * @dvfs_info: omap_vdd_dvfs_info pointer for the required vdd
469 * @req_dev: device removing the request
470 * @target_dev: target device from which frequency request is being removed
472 * This removes a requested frequency from target device's frequency list.
474 * Returns 0 on success.
476 static int _remove_freq_request(struct omap_vdd_dvfs_info *dvfs_info,
477 struct device *req_dev, struct device *target_dev)
479 struct omap_dev_user_list *dev_user = NULL, *tmp_user;
480 int ret = 0;
481 struct omap_vdd_dev_list *temp_dev;
483 if (!dvfs_info || IS_ERR(dvfs_info)) {
484 dev_warn(target_dev, "%s: VDD specified does not exist!\n",
485 __func__);
486 return -EINVAL;
490 list_for_each_entry(temp_dev, &dvfs_info->dev_list, node) {
491 if (temp_dev->dev == target_dev)
492 break;
495 if (temp_dev->dev != target_dev) {
496 dev_warn(target_dev, "%s: target_dev does not exist!\n",
497 __func__);
498 return -EINVAL;
501 spin_lock(&temp_dev->user_lock);
502 plist_for_each_entry(tmp_user, &temp_dev->freq_user_list, node) {
503 if (tmp_user->dev == req_dev) {
504 dev_user = tmp_user;
505 break;
509 if (dev_user) {
510 plist_del(&dev_user->node, &temp_dev->freq_user_list);
511 } else {
512 dev_err(target_dev,
513 "%s: Unable to remove the user for vdd_%s\n",
514 __func__, dvfs_info->voltdm->name);
515 ret = -EINVAL;
518 spin_unlock(&temp_dev->user_lock);
519 kfree(dev_user);
521 return ret;
525 * _dep_scan_table() - Scan a dependency table and mark for scaling
526 * @dev: device requesting the dependency scan (req_dev)
527 * @dep_info: dependency information (contains the table)
528 * @main_volt: voltage dependency to search for
530 * This runs down the table provided to find the match for main_volt
531 * provided and sets up a scale request for the dependent domain
532 * for the dependent OPP.
534 * Returns 0 if all went well.
536 static int _dep_scan_table(struct device *dev,
537 struct omap_vdd_dep_info *dep_info, unsigned long main_volt)
539 struct omap_vdd_dep_volt *dep_table = dep_info->dep_table;
540 struct device *target_dev;
541 struct omap_vdd_dvfs_info *tdvfs_info;
542 struct opp *opp;
543 int i, ret;
544 unsigned long dep_volt = 0, new_dep_volt = 0, new_freq = 0;
546 if (!dep_table) {
547 dev_err(dev, "%s: deptable not present for vdd%s\n",
548 __func__, dep_info->name);
549 return -EINVAL;
552 /* Now scan through the the dep table for a match */
553 for (i = 0; i < dep_info->nr_dep_entries; i++) {
554 if (dep_table[i].main_vdd_volt == main_volt) {
555 dep_volt = dep_table[i].dep_vdd_volt;
556 break;
559 if (!dep_volt) {
560 dev_warn(dev, "%s: %ld volt map missing in vdd_%s\n",
561 __func__, main_volt, dep_info->name);
562 return -EINVAL;
565 /* populate voltdm if it is not present */
566 if (!dep_info->_dep_voltdm) {
567 dep_info->_dep_voltdm = voltdm_lookup(dep_info->name);
568 if (!dep_info->_dep_voltdm) {
569 dev_warn(dev, "%s: unable to get vdm%s\n",
570 __func__, dep_info->name);
571 return -ENODEV;
575 tdvfs_info = _voltdm_to_dvfs_info(dep_info->_dep_voltdm);
576 if (!tdvfs_info) {
577 dev_warn(dev, "%s: no dvfs_info\n",
578 __func__);
579 return -ENODEV;
581 target_dev = _dvfs_info_to_dev(tdvfs_info);
582 if (!target_dev) {
583 dev_warn(dev, "%s: no target_dev\n",
584 __func__);
585 return -ENODEV;
588 rcu_read_lock();
589 opp = _volt_to_opp(target_dev, dep_volt);
590 if (!IS_ERR(opp)) {
591 new_dep_volt = opp_get_voltage(opp);
592 new_freq = opp_get_freq(opp);
594 rcu_read_unlock();
596 if (!new_dep_volt || !new_freq) {
597 dev_err(target_dev, "%s: no valid OPP for voltage %lu\n",
598 __func__, dep_volt);
599 return -ENODATA;
602 /* TODO: In case of _add_vdd_user() failure
603 * _dep_scan_table() will end up with previous voltage request,
604 * but without a frequency request.
605 * System should be left in previous state in case of failure.
606 * Same issue is present in omap_device_scale() function.
608 ret = _add_freq_request(tdvfs_info, dev, target_dev, new_freq);
609 if (ret) {
610 dev_err(target_dev, "%s: freqadd(%s) failed %d"
611 "[f=%ld, v=%ld(%ld)]\n",
612 __func__, dev_name(dev), ret, new_freq,
613 new_dep_volt, dep_volt);
614 return ret;
617 ret = _add_vdd_user(tdvfs_info, dev, new_dep_volt);
618 if (ret) {
619 dev_err(target_dev, "%s: vddadd(%s) failed %d"
620 "[f=%ld, v=%ld(%ld)]\n",
621 __func__, dev_name(dev), ret, new_freq,
622 new_dep_volt, dep_volt);
623 _remove_freq_request(tdvfs_info, dev, target_dev);
624 return ret;
627 return ret;
631 * _dep_scan_domains() - Scan dependency domains for a device
632 * @dev: device requesting the scan
633 * @vdd: vdd_info corresponding to the device
634 * @main_volt: voltage to scan for
636 * Since each domain *may* have multiple dependent domains, we scan
637 * through each of the dependent domains and invoke _dep_scan_table to
638 * scan each table for dependent domain for dependency scaling.
640 * This assumes that the dependent domain information is NULL entry terminated.
641 * Returns 0 if all went well.
643 static int _dep_scan_domains(struct device *dev,
644 struct omap_vdd_info *vdd, unsigned long main_volt)
646 struct omap_vdd_dep_info *dep_info = vdd->dep_vdd_info;
647 int ret = 0, r;
649 if (!dep_info) {
650 dev_dbg(dev, "%s: No dependent VDD\n", __func__);
651 return 0;
654 /* First scan through the mydomain->dep_domain list */
655 while (dep_info->nr_dep_entries) {
656 r = _dep_scan_table(dev, dep_info, main_volt);
657 /* Store last failed value */
658 ret = (r) ? r : ret;
659 dep_info++;
662 return ret;
666 * _dep_scale_domains() - Cause a scale of all dependent domains
667 * @req_dev: device requesting the scale
668 * @req_vdd: vdd_info corresponding to the requesting device.
670 * This walks through every dependent domain and triggers a scale
671 * It is assumed that the corresponding scale handling for the
672 * domain translates this to freq and voltage scale operations as
673 * needed.
675 * Note: This is uses _dvfs_scale and one should be careful not to
676 * create a circular depedency (e.g. vdd_mpu->vdd_core->vdd->mpu)
677 * which can create deadlocks. No protection is provided to prevent
678 * this condition and a tree organization is assumed.
680 * Returns 0 if all went fine.
682 static int _dep_scale_domains(struct device *req_dev,
683 struct omap_vdd_info *req_vdd)
685 struct omap_vdd_dep_info *dep_info = req_vdd->dep_vdd_info;
686 int ret = 0, r;
688 if (!dep_info) {
689 dev_dbg(req_dev, "%s: No dependent VDD\n", __func__);
690 return 0;
693 /* First scan through the mydomain->dep_domain list */
694 while (dep_info->nr_dep_entries) {
695 struct voltagedomain *tvoltdm = dep_info->_dep_voltdm;
697 r = 0;
698 /* Scale it only if I have a voltdm mapped up for the dep */
699 if (tvoltdm) {
700 struct omap_vdd_dvfs_info *tdvfs_info;
701 struct device *target_dev;
702 tdvfs_info = _voltdm_to_dvfs_info(tvoltdm);
703 if (!tdvfs_info) {
704 dev_warn(req_dev, "%s: no dvfs_info\n",
705 __func__);
706 goto next;
708 target_dev = _dvfs_info_to_dev(tdvfs_info);
709 if (!target_dev) {
710 dev_warn(req_dev, "%s: no target_dev\n",
711 __func__);
712 goto next;
714 r = _dvfs_scale(req_dev, target_dev, tdvfs_info);
715 next:
716 if (r)
717 dev_err(req_dev, "%s: dvfs_scale to %s =%d\n",
718 __func__, dev_name(target_dev), r);
720 /* Store last failed value */
721 ret = (r) ? r : ret;
722 dep_info++;
725 return ret;
729 * _dvfs_scale() : Scale the devices associated with a voltage domain
730 * @req_dev: Device requesting the scale
731 * @target_dev: Device requesting to be scaled
732 * @tdvfs_info: omap_vdd_dvfs_info pointer for the target domain
734 * This runs through the list of devices associated with the
735 * voltage domain and scales the device rates to the one requested
736 * by the user or those corresponding to the new voltage of the
737 * voltage domain. Target voltage is the highest voltage in the vdd_user_list.
739 * Returns 0 on success else the error value.
741 static int _dvfs_scale(struct device *req_dev, struct device *target_dev,
742 struct omap_vdd_dvfs_info *tdvfs_info)
744 unsigned long curr_volt, new_volt;
745 int volt_scale_dir = DVFS_VOLT_SCALE_DOWN;
746 struct omap_vdd_dev_list *temp_dev;
747 struct plist_node *node;
748 int ret = 0;
749 struct voltagedomain *voltdm;
750 struct omap_vdd_info *vdd;
751 struct omap_volt_data *new_vdata;
752 struct omap_volt_data *curr_vdata;
753 struct list_head *dev_list;
755 voltdm = tdvfs_info->voltdm;
756 if (IS_ERR_OR_NULL(voltdm)) {
757 dev_err(target_dev, "%s: bad voltdm\n", __func__);
758 return -EINVAL;
760 vdd = voltdm->vdd;
762 /* Find the highest voltage being requested */
763 node = plist_last(&tdvfs_info->vdd_user_list);
764 new_volt = node->prio;
766 new_vdata = omap_voltage_get_voltdata(voltdm, new_volt);
767 if (IS_ERR_OR_NULL(new_vdata)) {
768 pr_err("%s:%s: Bad New voltage data for %ld\n",
769 __func__, voltdm->name, new_volt);
770 return PTR_ERR(new_vdata);
772 new_volt = omap_get_operation_voltage(new_vdata);
773 curr_vdata = omap_voltage_get_curr_vdata(voltdm);
774 if (IS_ERR_OR_NULL(curr_vdata)) {
775 pr_err("%s:%s: Bad Current voltage data\n",
776 __func__, voltdm->name);
777 return PTR_ERR(curr_vdata);
780 /* Disable smartreflex module across voltage and frequency scaling */
781 omap_sr_disable(voltdm);
783 /* Pick up the current voltage ONLY after ensuring no changes occur */
784 curr_volt = omap_vp_get_curr_volt(voltdm);
785 if (!curr_volt)
786 curr_volt = omap_get_operation_voltage(curr_vdata);
788 /* Make a decision to scale dependent domain based on nominal voltage */
789 if (omap_get_nominal_voltage(new_vdata) >
790 omap_get_nominal_voltage(curr_vdata)) {
791 ret = _dep_scale_domains(target_dev, vdd);
792 if (ret) {
793 dev_err(target_dev,
794 "%s: Error(%d)scale dependent with %ld volt\n",
795 __func__, ret, new_volt);
796 goto fail;
800 if (voltdm->abb && omap_get_nominal_voltage(new_vdata) >
801 omap_get_nominal_voltage(curr_vdata)) {
802 ret = omap_ldo_abb_pre_scale(voltdm, new_vdata);
803 if (ret) {
804 pr_err("%s: ABB prescale failed for vdd%s: %d\n",
805 __func__, voltdm->name, ret);
806 goto fail;
810 /* Now decide on switching OPP */
811 if (curr_volt == new_volt) {
812 volt_scale_dir = DVFS_VOLT_SCALE_NONE;
813 } else if (curr_volt < new_volt) {
814 ret = voltdm_scale(voltdm, new_vdata);
815 if (ret) {
816 dev_err(target_dev,
817 "%s: Unable to scale the %s to %ld volt\n",
818 __func__, voltdm->name, new_volt);
819 goto fail;
821 volt_scale_dir = DVFS_VOLT_SCALE_UP;
824 if (voltdm->abb && omap_get_nominal_voltage(new_vdata) >
825 omap_get_nominal_voltage(curr_vdata)) {
826 ret = omap_ldo_abb_post_scale(voltdm, new_vdata);
827 if (ret) {
828 pr_err("%s: ABB prescale failed for vdd%s: %d\n",
829 __func__, voltdm->name, ret);
830 goto fail;
835 * Move all devices in list to the required frequencies.
836 * Devices are put in list in strict order, such as, when
837 * scaling up to higher OPP, dependent frequencies will be scaled
838 * after the frequency on which they depend. In case of scaling
839 * down to lower OPP the order of scaling frequencies is reverse.
841 dev_list = (volt_scale_dir == DVFS_VOLT_SCALE_DOWN) ?
842 tdvfs_info->dev_list.prev : tdvfs_info->dev_list.next;
843 while (dev_list != &tdvfs_info->dev_list) {
844 struct device *dev;
845 struct opp *opp;
846 unsigned long freq = 0;
847 int r;
849 temp_dev = list_entry(dev_list, struct omap_vdd_dev_list, node);
850 dev = temp_dev->dev;
851 if (!plist_head_empty(&temp_dev->freq_user_list)) {
852 node = plist_last(&temp_dev->freq_user_list);
853 freq = node->prio;
854 } else {
856 * Is the dev of dep domain target_device?
857 * we'd probably have a voltage request without
858 * a frequency dependency, scale appropriate frequency
859 * if there are none pending
861 if (target_dev == dev) {
862 rcu_read_lock();
863 opp = _volt_to_opp(dev, new_volt);
864 if (!IS_ERR(opp))
865 freq = opp_get_freq(opp);
866 rcu_read_unlock();
868 if (!freq)
869 goto next;
872 if (freq == clk_get_rate(temp_dev->clk)) {
873 dev_dbg(dev, "%s: Already at the requested"
874 "rate %ld\n", __func__, freq);
875 goto next;
878 r = clk_set_rate(temp_dev->clk, freq);
879 if (r < 0) {
880 dev_err(dev, "%s: clk set rate frq=%ld failed(%d)\n",
881 __func__, freq, r);
882 ret = r;
884 next:
885 dev_list = (volt_scale_dir == DVFS_VOLT_SCALE_DOWN) ?
886 dev_list->prev : dev_list->next;
889 if (ret)
890 goto fail;
892 if (voltdm->abb && omap_get_nominal_voltage(new_vdata) <
893 omap_get_nominal_voltage(curr_vdata)) {
894 ret = omap_ldo_abb_pre_scale(voltdm, new_vdata);
895 if (ret) {
896 pr_err("%s: ABB prescale failed for vdd%s: %d\n",
897 __func__, voltdm->name, ret);
898 goto fail;
902 if (DVFS_VOLT_SCALE_DOWN == volt_scale_dir)
903 voltdm_scale(voltdm, new_vdata);
905 if (voltdm->abb && omap_get_nominal_voltage(new_vdata) <
906 omap_get_nominal_voltage(curr_vdata)) {
907 ret = omap_ldo_abb_post_scale(voltdm, new_vdata);
908 if (ret)
909 pr_err("%s: ABB postscale failed for vdd%s: %d\n",
910 __func__, voltdm->name, ret);
913 /* Make a decision to scale dependent domain based on nominal voltage */
914 if (omap_get_nominal_voltage(new_vdata) <
915 omap_get_nominal_voltage(curr_vdata)) {
916 _dep_scale_domains(target_dev, vdd);
919 /* Ensure that current voltage data pointer points to new volt */
920 if (curr_volt == new_volt && omap_get_nominal_voltage(new_vdata) !=
921 omap_get_nominal_voltage(curr_vdata)) {
922 voltdm->curr_volt = new_vdata;
923 omap_vp_update_errorgain(voltdm, new_vdata);
926 /* All clear.. go out gracefully */
927 goto out;
929 fail:
930 pr_warning("%s: domain%s: No clean recovery available! could be bad!\n",
931 __func__, voltdm->name);
932 out:
933 /* Re-enable Smartreflex module */
934 omap_sr_enable(voltdm, new_vdata);
936 return ret;
939 /* Public functions */
942 * omap_device_scale() - Set a new rate at which the device is to operate
943 * @req_dev: pointer to the device requesting the scaling.
944 * @target_dev: pointer to the device that is to be scaled
945 * @rate: the rnew rate for the device.
947 * This API gets the device opp table associated with this device and
948 * tries putting the device to the requested rate and the voltage domain
949 * associated with the device to the voltage corresponding to the
950 * requested rate. Since multiple devices can be assocciated with a
951 * voltage domain this API finds out the possible voltage the
952 * voltage domain can enter and then decides on the final device
953 * rate.
955 * Return 0 on success else the error value
957 int omap_device_scale(struct device *req_dev, struct device *target_dev,
958 unsigned long rate)
960 struct opp *opp;
961 unsigned long volt, freq = rate, new_freq = 0;
962 struct omap_vdd_dvfs_info *tdvfs_info;
963 struct platform_device *pdev;
964 struct omap_device *od;
965 struct device *dev;
966 int ret = 0;
968 pdev = container_of(target_dev, struct platform_device, dev);
969 if (IS_ERR_OR_NULL(pdev)) {
970 pr_err("%s: pdev is null!\n", __func__);
971 return -EINVAL;
974 od = container_of(pdev, struct omap_device, pdev);
975 if (IS_ERR_OR_NULL(od)) {
976 pr_err("%s: od is null!\n", __func__);
977 return -EINVAL;
980 if (!omap_pm_is_ready()) {
981 dev_dbg(target_dev, "%s: pm is not ready yet\n", __func__);
982 return -EBUSY;
985 /* Lock me to ensure cross domain scaling is secure */
986 mutex_lock(&omap_dvfs_lock);
987 /* I would like CPU to be active always at this point */
988 pm_qos_update_request(&omap_dvfs_pm_qos_handle, 0);
990 rcu_read_lock();
991 opp = opp_find_freq_ceil(target_dev, &freq);
992 /* If we dont find a max, try a floor at least */
993 if (IS_ERR(opp))
994 opp = opp_find_freq_floor(target_dev, &freq);
995 if (IS_ERR(opp)) {
996 rcu_read_unlock();
997 dev_err(target_dev, "%s: Unable to find OPP for freq%ld\n",
998 __func__, rate);
999 ret = -ENODEV;
1000 goto out;
1002 volt = opp_get_voltage(opp);
1003 rcu_read_unlock();
1005 tdvfs_info = _dev_to_dvfs_info(target_dev);
1006 if (IS_ERR_OR_NULL(tdvfs_info)) {
1007 dev_err(target_dev, "%s: (req=%s) no vdd![f=%ld, v=%ld]\n",
1008 __func__, dev_name(req_dev), freq, volt);
1009 ret = -ENODEV;
1010 goto out;
1013 ret = _add_freq_request(tdvfs_info, req_dev, target_dev, freq);
1014 if (ret) {
1015 dev_err(target_dev, "%s: freqadd(%s) failed %d[f=%ld, v=%ld]\n",
1016 __func__, dev_name(req_dev), ret, freq, volt);
1017 goto out;
1020 ret = _add_vdd_user(tdvfs_info, req_dev, volt);
1021 if (ret) {
1022 dev_err(target_dev, "%s: vddadd(%s) failed %d[f=%ld, v=%ld]\n",
1023 __func__, dev_name(req_dev), ret, freq, volt);
1024 _remove_freq_request(tdvfs_info, req_dev,
1025 target_dev);
1026 goto out;
1029 /* Check for any dep domains and add the user request */
1030 ret = _dep_scan_domains(target_dev, tdvfs_info->voltdm->vdd, volt);
1031 if (ret) {
1032 dev_err(target_dev,
1033 "%s: Error in scan domains for vdd_%s\n",
1034 __func__, tdvfs_info->voltdm->name);
1035 goto out;
1038 dev = _dvfs_info_to_dev(tdvfs_info);
1039 if (!dev) {
1040 dev_warn(dev, "%s: no target_dev\n",
1041 __func__);
1042 ret = -ENODEV;
1043 goto out;
1046 if (dev != target_dev) {
1047 rcu_read_lock();
1048 opp = _volt_to_opp(dev, volt);
1049 if (!IS_ERR(opp))
1050 new_freq = opp_get_freq(opp);
1051 rcu_read_unlock();
1052 if (new_freq) {
1053 ret = _add_freq_request(tdvfs_info, req_dev, dev,
1054 new_freq);
1055 if (ret) {
1056 dev_err(target_dev, "%s: freqadd(%s) failed %d"
1057 "[f=%ld, v=%ld]\n", __func__,
1058 dev_name(req_dev), ret, freq, volt);
1059 goto out;
1064 /* Do the actual scaling */
1065 ret = _dvfs_scale(req_dev, target_dev, tdvfs_info);
1066 if (ret) {
1067 dev_err(target_dev, "%s: scale by %s failed %d[f=%ld, v=%ld]\n",
1068 __func__, dev_name(req_dev), ret, freq, volt);
1069 _remove_freq_request(tdvfs_info, req_dev,
1070 target_dev);
1071 _remove_vdd_user(tdvfs_info, target_dev);
1072 /* Fall through */
1074 /* Fall through */
1075 out:
1076 /* Remove the latency requirement */
1077 pm_qos_update_request(&omap_dvfs_pm_qos_handle, PM_QOS_DEFAULT_VALUE);
1078 mutex_unlock(&omap_dvfs_lock);
1079 return ret;
1081 EXPORT_SYMBOL(omap_device_scale);
1083 #ifdef CONFIG_PM_DEBUG
1084 static int dvfs_dump_vdd(struct seq_file *sf, void *unused)
1086 int k;
1087 struct omap_vdd_dvfs_info *dvfs_info;
1088 struct omap_vdd_dev_list *tdev;
1089 struct omap_dev_user_list *duser;
1090 struct omap_vdd_user_list *vuser;
1091 struct omap_vdd_info *vdd;
1092 struct omap_vdd_dep_info *dep_info;
1093 struct voltagedomain *voltdm;
1094 struct omap_volt_data *volt_data;
1095 int anyreq;
1096 int anyreq2;
1098 dvfs_info = (struct omap_vdd_dvfs_info *)sf->private;
1099 if (IS_ERR_OR_NULL(dvfs_info)) {
1100 pr_err("%s: NO DVFS?\n", __func__);
1101 return -EINVAL;
1104 voltdm = dvfs_info->voltdm;
1105 if (IS_ERR_OR_NULL(voltdm)) {
1106 pr_err("%s: NO voltdm?\n", __func__);
1107 return -EINVAL;
1110 vdd = voltdm->vdd;
1111 if (IS_ERR_OR_NULL(vdd)) {
1112 pr_err("%s: NO vdd data?\n", __func__);
1113 return -EINVAL;
1116 seq_printf(sf, "vdd_%s\n", voltdm->name);
1117 mutex_lock(&omap_dvfs_lock);
1118 spin_lock(&dvfs_info->user_lock);
1120 seq_printf(sf, "|- voltage requests\n| |\n");
1121 anyreq = 0;
1122 plist_for_each_entry(vuser, &dvfs_info->vdd_user_list, node) {
1123 seq_printf(sf, "| |-%d: %s:%s\n",
1124 vuser->node.prio,
1125 dev_driver_string(vuser->dev), dev_name(vuser->dev));
1126 anyreq = 1;
1129 spin_unlock(&dvfs_info->user_lock);
1131 if (!anyreq)
1132 seq_printf(sf, "| `-none\n");
1133 else
1134 seq_printf(sf, "| X\n");
1135 seq_printf(sf, "|\n");
1137 seq_printf(sf, "|- frequency requests\n| |\n");
1138 anyreq2 = 0;
1139 list_for_each_entry(tdev, &dvfs_info->dev_list, node) {
1140 anyreq = 0;
1141 seq_printf(sf, "| |- %s:%s\n",
1142 dev_driver_string(tdev->dev), dev_name(tdev->dev));
1143 spin_lock(&tdev->user_lock);
1144 plist_for_each_entry(duser, &tdev->freq_user_list, node) {
1145 seq_printf(sf, "| | |-%d: %s:%s\n",
1146 duser->node.prio,
1147 dev_driver_string(duser->dev),
1148 dev_name(duser->dev));
1149 anyreq = 1;
1152 spin_unlock(&tdev->user_lock);
1154 if (!anyreq)
1155 seq_printf(sf, "| | `-none\n");
1156 else
1157 seq_printf(sf, "| | X\n");
1158 anyreq2 = 1;
1160 if (!anyreq2)
1161 seq_printf(sf, "| `-none\n");
1162 else
1163 seq_printf(sf, "| X\n");
1165 volt_data = vdd->volt_data;
1166 seq_printf(sf, "|- Supported voltages\n| |\n");
1167 anyreq = 0;
1168 while (volt_data && volt_data->volt_nominal) {
1169 seq_printf(sf, "| |-%d\n", volt_data->volt_nominal);
1170 anyreq = 1;
1171 volt_data++;
1173 if (!anyreq)
1174 seq_printf(sf, "| `-none\n");
1175 else
1176 seq_printf(sf, "| X\n");
1178 dep_info = vdd->dep_vdd_info;
1179 seq_printf(sf, "`- voltage dependencies\n |\n");
1180 anyreq = 0;
1181 while (dep_info && dep_info->nr_dep_entries) {
1182 struct omap_vdd_dep_volt *dep_table = dep_info->dep_table;
1184 seq_printf(sf, " |-on vdd_%s\n", dep_info->name);
1186 for (k = 0; k < dep_info->nr_dep_entries; k++) {
1187 seq_printf(sf, " | |- %d => %d\n",
1188 dep_table[k].main_vdd_volt,
1189 dep_table[k].dep_vdd_volt);
1192 anyreq = 1;
1193 dep_info++;
1196 if (!anyreq)
1197 seq_printf(sf, " `- none\n");
1198 else
1199 seq_printf(sf, " X X\n");
1201 mutex_unlock(&omap_dvfs_lock);
1202 return 0;
1205 static int dvfs_dbg_open(struct inode *inode, struct file *file)
1207 return single_open(file, dvfs_dump_vdd, inode->i_private);
1210 static struct file_operations debugdvfs_fops = {
1211 .open = dvfs_dbg_open,
1212 .read = seq_read,
1213 .llseek = seq_lseek,
1214 .release = single_release,
1217 static struct dentry __initdata *dvfsdebugfs_dir;
1219 static void __init dvfs_dbg_init(struct omap_vdd_dvfs_info *dvfs_info)
1221 struct dentry *ddir;
1223 /* create a base dir */
1224 if (!dvfsdebugfs_dir)
1225 dvfsdebugfs_dir = debugfs_create_dir("dvfs", NULL);
1226 if (IS_ERR_OR_NULL(dvfsdebugfs_dir)) {
1227 WARN_ONCE("%s: Unable to create base DVFS dir\n", __func__);
1228 return;
1231 if (IS_ERR_OR_NULL(dvfs_info->voltdm)) {
1232 pr_err("%s: no voltdm\n", __func__);
1233 return;
1236 ddir = debugfs_create_dir(dvfs_info->voltdm->name, dvfsdebugfs_dir);
1237 if (IS_ERR_OR_NULL(ddir)) {
1238 pr_warning("%s: unable to create subdir %s\n", __func__,
1239 dvfs_info->voltdm->name);
1240 return;
1243 debugfs_create_file("info", S_IRUGO, ddir,
1244 (void *)dvfs_info, &debugdvfs_fops);
1246 #else /* CONFIG_PM_DEBUG */
1247 static inline void dvfs_dbg_init(struct omap_vdd_dvfs_info *dvfs_info)
1249 return;
1251 #endif /* CONFIG_PM_DEBUG */
1254 * omap_dvfs_register_device - Add a parent device into dvfs managed list
1255 * @dev: Device to be added
1256 * @voltdm_name: Name of the voltage domain for the device
1257 * @clk_name: Name of the clock for the device
1259 * This function adds a given device into user_list of corresponding
1260 * vdd's omap_vdd_dvfs_info strucure. This list is traversed to scale
1261 * frequencies of all the devices on a given vdd.
1263 * Returns 0 on success.
1265 int __init omap_dvfs_register_device(struct device *dev, char *voltdm_name,
1266 char *clk_name)
1268 struct omap_vdd_dev_list *temp_dev;
1269 struct omap_vdd_dvfs_info *dvfs_info;
1270 struct clk *clk = NULL;
1271 struct voltagedomain *voltdm;
1272 int ret = 0;
1273 static __initdata bool qos_create;
1275 if (!voltdm_name) {
1276 dev_err(dev, "%s: Bad voltdm name!\n", __func__);
1277 return -EINVAL;
1279 if (!clk_name) {
1280 dev_err(dev, "%s: Bad clk name!\n", __func__);
1281 return -EINVAL;
1284 /* Lock me to secure structure changes */
1285 mutex_lock(&omap_dvfs_lock);
1287 voltdm = voltdm_lookup(voltdm_name);
1288 if (!voltdm) {
1289 dev_warn(dev, "%s: unable to find voltdm %s!\n",
1290 __func__, voltdm_name);
1291 ret = -EINVAL;
1292 goto out;
1294 dvfs_info = _voltdm_to_dvfs_info(voltdm);
1295 if (!dvfs_info) {
1296 dvfs_info = kzalloc(sizeof(struct omap_vdd_dvfs_info),
1297 GFP_KERNEL);
1298 if (!dvfs_info) {
1299 dev_warn(dev, "%s: unable to alloc memory!\n",
1300 __func__);
1301 ret = -ENOMEM;
1302 goto out;
1304 dvfs_info->voltdm = voltdm;
1306 /* Init the plist */
1307 spin_lock_init(&dvfs_info->user_lock);
1308 plist_head_init(&dvfs_info->vdd_user_list);
1309 /* Init the device list */
1310 INIT_LIST_HEAD(&dvfs_info->dev_list);
1312 list_add(&dvfs_info->node, &omap_dvfs_info_list);
1314 dvfs_dbg_init(dvfs_info);
1317 /* If device already added, we dont need to do more.. */
1318 list_for_each_entry(temp_dev, &dvfs_info->dev_list, node) {
1319 if (temp_dev->dev == dev)
1320 goto out;
1323 temp_dev = kzalloc(sizeof(struct omap_vdd_dev_list), GFP_KERNEL);
1324 if (!temp_dev) {
1325 dev_err(dev, "%s: Unable to creat a new device for vdd_%s\n",
1326 __func__, dvfs_info->voltdm->name);
1327 ret = -ENOMEM;
1328 goto out;
1331 clk = clk_get(dev, clk_name);
1332 if (IS_ERR_OR_NULL(clk)) {
1333 dev_warn(dev, "%s: Bad clk pointer!\n", __func__);
1334 kfree(temp_dev);
1335 ret = -EINVAL;
1336 goto out;
1339 /* Initialize priority ordered list */
1340 spin_lock_init(&temp_dev->user_lock);
1341 plist_head_init(&temp_dev->freq_user_list);
1343 temp_dev->dev = dev;
1344 temp_dev->clk = clk;
1345 list_add_tail(&temp_dev->node, &dvfs_info->dev_list);
1347 /* Simpler to have a single request for all domains */
1348 if (!qos_create) {
1349 pm_qos_add_request(&omap_dvfs_pm_qos_handle,
1350 PM_QOS_CPU_DMA_LATENCY,
1351 PM_QOS_DEFAULT_VALUE);
1352 qos_create = true;
1354 /* Fall through */
1355 out:
1356 mutex_unlock(&omap_dvfs_lock);
1357 return ret;