1 /*
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  *
10  * Copyright (C) 2008, 2011 Nokia Corporation
11  * Kalle Jokiniemi
12  * Paul Walmsley
13  *
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>
24 #include <linux/err.h>
25 #include <linux/export.h>
26 #include <linux/debugfs.h>
27 #include <linux/slab.h>
28 #include <linux/clk.h>
29 
30 #include "common.h"
31 
32 #include "prm-regbits-34xx.h"
33 #include "prm-regbits-44xx.h"
34 #include "prm44xx.h"
35 #include "prcm44xx.h"
36 #include "prminst44xx.h"
37 #include "control.h"
38 
39 #include "voltage.h"
40 #include "powerdomain.h"
41 
42 #include "vc.h"
43 #include "vp.h"
44 
45 static LIST_HEAD(voltdm_list);
46 
47 /* Public functions */
48 /**
49  * voltdm_get_voltage() - Gets the current non-auto-compensated voltage
50  * @voltdm:	pointer to the voltdm for which current voltage info is needed
51  *
52  * 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.
54  */
voltdm_get_voltage(struct voltagedomain * voltdm)55 unsigned long voltdm_get_voltage(struct voltagedomain *voltdm)
56 {
57 	if (!voltdm || IS_ERR(voltdm)) {
58 		pr_warning("%s: VDD specified does not exist!\n", __func__);
59 		return 0;
60 	}
61 
62 	return voltdm->nominal_volt;
63 }
64 
65 /**
66  * 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
69  *
70  * This API should be called by the kernel to do the voltage scaling
71  * for a particular voltage domain during DVFS.
72  */
voltdm_scale(struct voltagedomain * voltdm,unsigned long target_volt)73 int voltdm_scale(struct voltagedomain *voltdm,
74 		 unsigned long target_volt)
75 {
76 	int ret;
77 
78 	if (!voltdm || IS_ERR(voltdm)) {
79 		pr_warning("%s: VDD specified does not exist!\n", __func__);
80 		return -EINVAL;
81 	}
82 
83 	if (!voltdm->scale) {
84 		pr_err("%s: No voltage scale API registered for vdd_%s\n",
85 			__func__, voltdm->name);
86 		return -ENODATA;
87 	}
88 
89 	ret = voltdm->scale(voltdm, target_volt);
90 	if (!ret)
91 		voltdm->nominal_volt = target_volt;
92 
93 	return ret;
94 }
95 
96 /**
97  * 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.
100  *
101  * This API finds out the correct voltage the voltage domain is supposed
102  * to be at and resets the voltage to that level. Should be used especially
103  * while disabling any voltage compensation modules.
104  */
voltdm_reset(struct voltagedomain * voltdm)105 void voltdm_reset(struct voltagedomain *voltdm)
106 {
107 	unsigned long target_volt;
108 
109 	if (!voltdm || IS_ERR(voltdm)) {
110 		pr_warning("%s: VDD specified does not exist!\n", __func__);
111 		return;
112 	}
113 
114 	target_volt = voltdm_get_voltage(voltdm);
115 	if (!target_volt) {
116 		pr_err("%s: unable to find current voltage for vdd_%s\n",
117 			__func__, voltdm->name);
118 		return;
119 	}
120 
121 	voltdm_scale(voltdm, target_volt);
122 }
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  */
omap_voltage_get_volttable(struct voltagedomain * voltdm,struct omap_volt_data ** volt_data)136 void omap_voltage_get_volttable(struct voltagedomain *voltdm,
137 				struct omap_volt_data **volt_data)
138 {
139 	if (!voltdm || IS_ERR(voltdm)) {
140 		pr_warning("%s: VDD specified does not exist!\n", __func__);
141 		return;
142 	}
143 
144 	*volt_data = voltdm->volt_data;
145 }
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
159  * success. Returns -ENODATA if no voltage table exisits for the passed voltage
160  * domain or if there is no matching entry.
161  */
omap_voltage_get_voltdata(struct voltagedomain * voltdm,unsigned long volt)162 struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
163 						 unsigned long volt)
164 {
165 	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 
172 	if (!voltdm->volt_data) {
173 		pr_warning("%s: voltage table does not exist for vdd_%s\n",
174 			__func__, voltdm->name);
175 		return ERR_PTR(-ENODATA);
176 	}
177 
178 	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];
181 	}
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
193  * @pmic:	the structure containing pmic info
194  *
195  * This API is to be called by the SOC/PMIC file to specify the
196  * pmic specific info as present in omap_voltdm_pmic structure.
197  */
omap_voltage_register_pmic(struct voltagedomain * voltdm,struct omap_voltdm_pmic * pmic)198 int omap_voltage_register_pmic(struct voltagedomain *voltdm,
199 			       struct omap_voltdm_pmic *pmic)
200 {
201 	if (!voltdm || IS_ERR(voltdm)) {
202 		pr_warning("%s: VDD specified does not exist!\n", __func__);
203 		return -EINVAL;
204 	}
205 
206 	voltdm->pmic = pmic;
207 
208 	return 0;
209 }
210 
211 /**
212  * 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  */
omap_change_voltscale_method(struct voltagedomain * voltdm,int voltscale_method)221 void omap_change_voltscale_method(struct voltagedomain *voltdm,
222 				  int voltscale_method)
223 {
224 	if (!voltdm || IS_ERR(voltdm)) {
225 		pr_warning("%s: VDD specified does not exist!\n", __func__);
226 		return;
227 	}
228 
229 	switch (voltscale_method) {
230 	case VOLTSCALE_VPFORCEUPDATE:
231 		voltdm->scale = omap_vp_forceupdate_scale;
232 		return;
233 	case VOLTSCALE_VCBYPASS:
234 		voltdm->scale = omap_vc_bypass_scale;
235 		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 /**
243  * 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  */
omap_voltage_late_init(void)249 int __init omap_voltage_late_init(void)
250 {
251 	struct voltagedomain *voltdm;
252 
253 	if (list_empty(&voltdm_list)) {
254 		pr_err("%s: Voltage driver support not added\n",
255 			__func__);
256 		return -EINVAL;
257 	}
258 
259 	list_for_each_entry(voltdm, &voltdm_list, node) {
260 		struct clk *sys_ck;
261 
262 		if (!voltdm->scalable)
263 			continue;
264 
265 		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 
274 		if (voltdm->vc) {
275 			voltdm->scale = omap_vc_bypass_scale;
276 			omap_vc_init_channel(voltdm);
277 		}
278 
279 		if (voltdm->vp) {
280 			voltdm->scale = omap_vp_forceupdate_scale;
281 			omap_vp_init(voltdm);
282 		}
283 	}
284 
285 	return 0;
286 }
287 
_voltdm_lookup(const char * name)288 static struct voltagedomain *_voltdm_lookup(const char *name)
289 {
290 	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 
304 /**
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  */
voltdm_add_pwrdm(struct voltagedomain * voltdm,struct powerdomain * pwrdm)314 int 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  */
voltdm_for_each_pwrdm(struct voltagedomain * voltdm,int (* fn)(struct voltagedomain * voltdm,struct powerdomain * pwrdm))338 int 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  */
voltdm_for_each(int (* fn)(struct voltagedomain * voltdm,void * user),void * user)364 int 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 
_voltdm_register(struct voltagedomain * voltdm)382 static int _voltdm_register(struct voltagedomain *voltdm)
383 {
384 	if (!voltdm || !voltdm->name)
385 		return -EINVAL;
386 
387 	INIT_LIST_HEAD(&voltdm->pwrdm_list);
388 	list_add(&voltdm->node, &voltdm_list);
389 
390 	pr_debug("voltagedomain: registered %s\n", voltdm->name);
391 
392 	return 0;
393 }
394 
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  */
voltdm_lookup(const char * name)402 struct 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  */
voltdm_init(struct voltagedomain ** voltdms)423 void 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 }
432