blob: 8a36342e60d2aaf23ad434152e208b7ee5d1b8fd [file] [log] [blame]
Thara Gopinath2f34ce82010-05-29 22:02:21 +05301/*
2 * OMAP3/OMAP4 Voltage Management Routines
3 *
4 * Author: Thara Gopinath <thara@ti.com>
5 *
6 * Copyright (C) 2007 Texas Instruments, Inc.
7 * Rajendra Nayak <rnayak@ti.com>
8 * Lesly A M <x0080970@ti.com>
9 *
Paul Walmsleyc0718df2011-03-10 22:17:45 -070010 * Copyright (C) 2008, 2011 Nokia Corporation
Thara Gopinath2f34ce82010-05-29 22:02:21 +053011 * Kalle Jokiniemi
Paul Walmsleyc0718df2011-03-10 22:17:45 -070012 * Paul Walmsley
Thara Gopinath2f34ce82010-05-29 22:02:21 +053013 *
14 * Copyright (C) 2010 Texas Instruments, Inc.
15 * Thara Gopinath <thara@ti.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21
22#include <linux/delay.h>
23#include <linux/io.h>
Thara Gopinath2f34ce82010-05-29 22:02:21 +053024#include <linux/err.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040025#include <linux/export.h>
Thara Gopinath2f34ce82010-05-29 22:02:21 +053026#include <linux/debugfs.h>
27#include <linux/slab.h>
Kevin Hilman0e2f3d92011-04-04 17:22:28 -070028#include <linux/clk.h>
Thara Gopinath2f34ce82010-05-29 22:02:21 +053029
Tony Lindgren4e653312011-11-10 22:45:17 +010030#include "common.h"
Thara Gopinath2f34ce82010-05-29 22:02:21 +053031
32#include "prm-regbits-34xx.h"
Thara Gopinathbd381072010-12-10 23:15:23 +053033#include "prm-regbits-44xx.h"
34#include "prm44xx.h"
35#include "prcm44xx.h"
36#include "prminst44xx.h"
Thara Gopinath2f34ce82010-05-29 22:02:21 +053037#include "control.h"
38
Paul Walmsleye1d6f472011-02-25 15:54:33 -070039#include "voltage.h"
Kevin Hilmane69c22b2011-03-16 16:13:15 -070040#include "powerdomain.h"
Paul Walmsleye1d6f472011-02-25 15:54:33 -070041
Paul Walmsleyc0718df2011-03-10 22:17:45 -070042#include "vc.h"
43#include "vp.h"
44
Kevin Hilman81a60482011-03-16 14:25:45 -070045static LIST_HEAD(voltdm_list);
46
Thara Gopinath2f34ce82010-05-29 22:02:21 +053047/* Public functions */
48/**
Kevin Hilmand5c12822011-07-15 16:05:12 -070049 * voltdm_get_voltage() - Gets the current non-auto-compensated voltage
50 * @voltdm: pointer to the voltdm for which current voltage info is needed
Thara Gopinath2f34ce82010-05-29 22:02:21 +053051 *
Kevin Hilmand5c12822011-07-15 16:05:12 -070052 * API to get the current non-auto-compensated voltage for a voltage domain.
53 * Returns 0 in case of error else returns the current voltage.
Thara Gopinath2f34ce82010-05-29 22:02:21 +053054 */
Kevin Hilmand5c12822011-07-15 16:05:12 -070055unsigned long voltdm_get_voltage(struct voltagedomain *voltdm)
Thara Gopinath2f34ce82010-05-29 22:02:21 +053056{
Thara Gopinath2f34ce82010-05-29 22:02:21 +053057 if (!voltdm || IS_ERR(voltdm)) {
58 pr_warning("%s: VDD specified does not exist!\n", __func__);
59 return 0;
60 }
61
Kevin Hilman7590f602011-04-05 16:55:22 -070062 return voltdm->nominal_volt;
Thara Gopinath2f34ce82010-05-29 22:02:21 +053063}
64
65/**
Kevin Hilman5e5651b2011-04-05 16:27:21 -070066 * voltdm_scale() - API to scale voltage of a particular voltage domain.
67 * @voltdm: pointer to the voltage domain which is to be scaled.
68 * @target_volt: The target voltage of the voltage domain
Thara Gopinath2f34ce82010-05-29 22:02:21 +053069 *
70 * This API should be called by the kernel to do the voltage scaling
Kevin Hilman5e5651b2011-04-05 16:27:21 -070071 * for a particular voltage domain during DVFS.
Thara Gopinath2f34ce82010-05-29 22:02:21 +053072 */
Kevin Hilman5e5651b2011-04-05 16:27:21 -070073int voltdm_scale(struct voltagedomain *voltdm,
74 unsigned long target_volt)
Thara Gopinath2f34ce82010-05-29 22:02:21 +053075{
Kevin Hilmand5c12822011-07-15 16:05:12 -070076 int ret;
77
Thara Gopinath2f34ce82010-05-29 22:02:21 +053078 if (!voltdm || IS_ERR(voltdm)) {
79 pr_warning("%s: VDD specified does not exist!\n", __func__);
80 return -EINVAL;
81 }
82
Kevin Hilman0f015652011-07-14 11:12:32 -070083 if (!voltdm->scale) {
Thara Gopinath2f34ce82010-05-29 22:02:21 +053084 pr_err("%s: No voltage scale API registered for vdd_%s\n",
85 __func__, voltdm->name);
86 return -ENODATA;
87 }
88
Kevin Hilman6a62b782011-07-18 16:24:17 -070089 ret = voltdm->scale(voltdm, target_volt);
90 if (!ret)
91 voltdm->nominal_volt = target_volt;
92
93 return ret;
Thara Gopinath2f34ce82010-05-29 22:02:21 +053094}
95
96/**
Kevin Hilman5e5651b2011-04-05 16:27:21 -070097 * voltdm_reset() - Resets the voltage of a particular voltage domain
98 * to that of the current OPP.
99 * @voltdm: pointer to the voltage domain whose voltage is to be reset.
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530100 *
101 * This API finds out the correct voltage the voltage domain is supposed
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300102 * to be at and resets the voltage to that level. Should be used especially
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530103 * while disabling any voltage compensation modules.
104 */
Kevin Hilman5e5651b2011-04-05 16:27:21 -0700105void voltdm_reset(struct voltagedomain *voltdm)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530106{
Kevin Hilman5e5651b2011-04-05 16:27:21 -0700107 unsigned long target_volt;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530108
109 if (!voltdm || IS_ERR(voltdm)) {
110 pr_warning("%s: VDD specified does not exist!\n", __func__);
111 return;
112 }
113
Kevin Hilmand5c12822011-07-15 16:05:12 -0700114 target_volt = voltdm_get_voltage(voltdm);
Kevin Hilman5e5651b2011-04-05 16:27:21 -0700115 if (!target_volt) {
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530116 pr_err("%s: unable to find current voltage for vdd_%s\n",
117 __func__, voltdm->name);
118 return;
119 }
120
Kevin Hilman5e5651b2011-04-05 16:27:21 -0700121 voltdm_scale(voltdm, target_volt);
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530122}
123
124/**
125 * omap_voltage_get_volttable() - API to get the voltage table associated with a
126 * particular voltage domain.
127 * @voltdm: pointer to the VDD for which the voltage table is required
128 * @volt_data: the voltage table for the particular vdd which is to be
129 * populated by this API
130 *
131 * This API populates the voltage table associated with a VDD into the
132 * passed parameter pointer. Returns the count of distinct voltages
133 * supported by this vdd.
134 *
135 */
136void omap_voltage_get_volttable(struct voltagedomain *voltdm,
Kevin Hilmane3277882011-07-14 11:29:06 -0700137 struct omap_volt_data **volt_data)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530138{
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530139 if (!voltdm || IS_ERR(voltdm)) {
140 pr_warning("%s: VDD specified does not exist!\n", __func__);
141 return;
142 }
143
Kevin Hilmane3277882011-07-14 11:29:06 -0700144 *volt_data = voltdm->volt_data;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530145}
146
147/**
148 * omap_voltage_get_voltdata() - API to get the voltage table entry for a
149 * particular voltage
150 * @voltdm: pointer to the VDD whose voltage table has to be searched
151 * @volt: the voltage to be searched in the voltage table
152 *
153 * This API searches through the voltage table for the required voltage
154 * domain and tries to find a matching entry for the passed voltage volt.
155 * If a matching entry is found volt_data is populated with that entry.
156 * This API searches only through the non-compensated voltages int the
157 * voltage table.
158 * Returns pointer to the voltage table entry corresponding to volt on
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300159 * success. Returns -ENODATA if no voltage table exisits for the passed voltage
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530160 * domain or if there is no matching entry.
161 */
162struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
Kevin Hilmane3277882011-07-14 11:29:06 -0700163 unsigned long volt)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530164{
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530165 int i;
166
167 if (!voltdm || IS_ERR(voltdm)) {
168 pr_warning("%s: VDD specified does not exist!\n", __func__);
169 return ERR_PTR(-EINVAL);
170 }
171
Kevin Hilmane3277882011-07-14 11:29:06 -0700172 if (!voltdm->volt_data) {
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530173 pr_warning("%s: voltage table does not exist for vdd_%s\n",
174 __func__, voltdm->name);
175 return ERR_PTR(-ENODATA);
176 }
177
Kevin Hilmane3277882011-07-14 11:29:06 -0700178 for (i = 0; voltdm->volt_data[i].volt_nominal != 0; i++) {
179 if (voltdm->volt_data[i].volt_nominal == volt)
180 return &voltdm->volt_data[i];
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530181 }
182
183 pr_notice("%s: Unable to match the current voltage with the voltage"
184 "table for vdd_%s\n", __func__, voltdm->name);
185
186 return ERR_PTR(-ENODATA);
187}
188
189/**
190 * omap_voltage_register_pmic() - API to register PMIC specific data
191 * @voltdm: pointer to the VDD for which the PMIC specific data is
192 * to be registered
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700193 * @pmic: the structure containing pmic info
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530194 *
195 * This API is to be called by the SOC/PMIC file to specify the
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700196 * pmic specific info as present in omap_voltdm_pmic structure.
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530197 */
198int omap_voltage_register_pmic(struct voltagedomain *voltdm,
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700199 struct omap_voltdm_pmic *pmic)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530200{
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530201 if (!voltdm || IS_ERR(voltdm)) {
202 pr_warning("%s: VDD specified does not exist!\n", __func__);
203 return -EINVAL;
204 }
205
Kevin Hilmance8ebe02011-03-30 11:01:10 -0700206 voltdm->pmic = pmic;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530207
208 return 0;
209}
210
211/**
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530212 * omap_change_voltscale_method() - API to change the voltage scaling method.
213 * @voltdm: pointer to the VDD whose voltage scaling method
214 * has to be changed.
215 * @voltscale_method: the method to be used for voltage scaling.
216 *
217 * This API can be used by the board files to change the method of voltage
218 * scaling between vpforceupdate and vcbypass. The parameter values are
219 * defined in voltage.h
220 */
221void omap_change_voltscale_method(struct voltagedomain *voltdm,
Kevin Hilman0f015652011-07-14 11:12:32 -0700222 int voltscale_method)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530223{
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530224 if (!voltdm || IS_ERR(voltdm)) {
225 pr_warning("%s: VDD specified does not exist!\n", __func__);
226 return;
227 }
228
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530229 switch (voltscale_method) {
230 case VOLTSCALE_VPFORCEUPDATE:
Kevin Hilman0f015652011-07-14 11:12:32 -0700231 voltdm->scale = omap_vp_forceupdate_scale;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530232 return;
233 case VOLTSCALE_VCBYPASS:
Kevin Hilman0f015652011-07-14 11:12:32 -0700234 voltdm->scale = omap_vc_bypass_scale;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530235 return;
236 default:
237 pr_warning("%s: Trying to change the method of voltage scaling"
238 "to an unsupported one!\n", __func__);
239 }
240}
241
242/**
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530243 * omap_voltage_late_init() - Init the various voltage parameters
244 *
245 * This API is to be called in the later stages of the
246 * system boot to init the voltage controller and
247 * voltage processors.
248 */
249int __init omap_voltage_late_init(void)
250{
Kevin Hilman81a60482011-03-16 14:25:45 -0700251 struct voltagedomain *voltdm;
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530252
Kevin Hilman81a60482011-03-16 14:25:45 -0700253 if (list_empty(&voltdm_list)) {
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530254 pr_err("%s: Voltage driver support not added\n",
255 __func__);
256 return -EINVAL;
257 }
258
Kevin Hilman81a60482011-03-16 14:25:45 -0700259 list_for_each_entry(voltdm, &voltdm_list, node) {
Kevin Hilman0e2f3d92011-04-04 17:22:28 -0700260 struct clk *sys_ck;
261
Kevin Hilman37efca72011-03-23 17:00:21 -0700262 if (!voltdm->scalable)
263 continue;
264
Kevin Hilman0e2f3d92011-04-04 17:22:28 -0700265 sys_ck = clk_get(NULL, voltdm->sys_clk.name);
266 if (IS_ERR(sys_ck)) {
267 pr_warning("%s: Could not get sys clk.\n", __func__);
268 return -EINVAL;
269 }
270 voltdm->sys_clk.rate = clk_get_rate(sys_ck);
271 WARN_ON(!voltdm->sys_clk.rate);
272 clk_put(sys_ck);
273
Kevin Hilman4d475062011-07-18 15:48:22 -0700274 if (voltdm->vc) {
Kevin Hilman0f015652011-07-14 11:12:32 -0700275 voltdm->scale = omap_vc_bypass_scale;
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700276 omap_vc_init_channel(voltdm);
Kevin Hilman4d475062011-07-18 15:48:22 -0700277 }
Kevin Hilmand84adcf2011-03-22 16:14:57 -0700278
Kevin Hilmane3277882011-07-14 11:29:06 -0700279 if (voltdm->vp) {
280 voltdm->scale = omap_vp_forceupdate_scale;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700281 omap_vp_init(voltdm);
Kevin Hilman81a60482011-03-16 14:25:45 -0700282 }
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530283 }
284
285 return 0;
286}
287
Kevin Hilman81a60482011-03-16 14:25:45 -0700288static struct voltagedomain *_voltdm_lookup(const char *name)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530289{
Kevin Hilman81a60482011-03-16 14:25:45 -0700290 struct voltagedomain *voltdm, *temp_voltdm;
291
292 voltdm = NULL;
293
294 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
295 if (!strcmp(name, temp_voltdm->name)) {
296 voltdm = temp_voltdm;
297 break;
298 }
299 }
300
301 return voltdm;
302}
303
Kevin Hilmane69c22b2011-03-16 16:13:15 -0700304/**
305 * voltdm_add_pwrdm - add a powerdomain to a voltagedomain
306 * @voltdm: struct voltagedomain * to add the powerdomain to
307 * @pwrdm: struct powerdomain * to associate with a voltagedomain
308 *
309 * Associate the powerdomain @pwrdm with a voltagedomain @voltdm. This
310 * enables the use of voltdm_for_each_pwrdm(). Returns -EINVAL if
311 * presented with invalid pointers; -ENOMEM if memory could not be allocated;
312 * or 0 upon success.
313 */
314int voltdm_add_pwrdm(struct voltagedomain *voltdm, struct powerdomain *pwrdm)
315{
316 if (!voltdm || !pwrdm)
317 return -EINVAL;
318
319 pr_debug("voltagedomain: associating powerdomain %s with voltagedomain "
320 "%s\n", pwrdm->name, voltdm->name);
321
322 list_add(&pwrdm->voltdm_node, &voltdm->pwrdm_list);
323
324 return 0;
325}
326
327/**
328 * voltdm_for_each_pwrdm - call function for each pwrdm in a voltdm
329 * @voltdm: struct voltagedomain * to iterate over
330 * @fn: callback function *
331 *
332 * Call the supplied function @fn for each powerdomain in the
333 * voltagedomain @voltdm. Returns -EINVAL if presented with invalid
334 * pointers; or passes along the last return value of the callback
335 * function, which should be 0 for success or anything else to
336 * indicate failure.
337 */
338int voltdm_for_each_pwrdm(struct voltagedomain *voltdm,
339 int (*fn)(struct voltagedomain *voltdm,
340 struct powerdomain *pwrdm))
341{
342 struct powerdomain *pwrdm;
343 int ret = 0;
344
345 if (!fn)
346 return -EINVAL;
347
348 list_for_each_entry(pwrdm, &voltdm->pwrdm_list, voltdm_node)
349 ret = (*fn)(voltdm, pwrdm);
350
351 return ret;
352}
353
354/**
355 * voltdm_for_each - call function on each registered voltagedomain
356 * @fn: callback function *
357 *
358 * Call the supplied function @fn for each registered voltagedomain.
359 * The callback function @fn can return anything but 0 to bail out
360 * early from the iterator. Returns the last return value of the
361 * callback function, which should be 0 for success or anything else
362 * to indicate failure; or -EINVAL if the function pointer is null.
363 */
364int voltdm_for_each(int (*fn)(struct voltagedomain *voltdm, void *user),
365 void *user)
366{
367 struct voltagedomain *temp_voltdm;
368 int ret = 0;
369
370 if (!fn)
371 return -EINVAL;
372
373 list_for_each_entry(temp_voltdm, &voltdm_list, node) {
374 ret = (*fn)(temp_voltdm, user);
375 if (ret)
376 break;
377 }
378
379 return ret;
380}
381
Kevin Hilman81a60482011-03-16 14:25:45 -0700382static int _voltdm_register(struct voltagedomain *voltdm)
383{
384 if (!voltdm || !voltdm->name)
385 return -EINVAL;
386
Kevin Hilmane69c22b2011-03-16 16:13:15 -0700387 INIT_LIST_HEAD(&voltdm->pwrdm_list);
Kevin Hilman81a60482011-03-16 14:25:45 -0700388 list_add(&voltdm->node, &voltdm_list);
389
390 pr_debug("voltagedomain: registered %s\n", voltdm->name);
391
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530392 return 0;
393}
Kevin Hilman81a60482011-03-16 14:25:45 -0700394
395/**
396 * voltdm_lookup - look up a voltagedomain by name, return a pointer
397 * @name: name of voltagedomain
398 *
399 * Find a registered voltagedomain by its name @name. Returns a pointer
400 * to the struct voltagedomain if found, or NULL otherwise.
401 */
402struct voltagedomain *voltdm_lookup(const char *name)
403{
404 struct voltagedomain *voltdm ;
405
406 if (!name)
407 return NULL;
408
409 voltdm = _voltdm_lookup(name);
410
411 return voltdm;
412}
413
414/**
415 * voltdm_init - set up the voltagedomain layer
416 * @voltdm_list: array of struct voltagedomain pointers to register
417 *
418 * Loop through the array of voltagedomains @voltdm_list, registering all
419 * that are available on the current CPU. If voltdm_list is supplied
420 * and not null, all of the referenced voltagedomains will be
421 * registered. No return value.
422 */
423void voltdm_init(struct voltagedomain **voltdms)
424{
425 struct voltagedomain **v;
426
427 if (voltdms) {
428 for (v = voltdms; *v; v++)
429 _voltdm_register(*v);
430 }
431}