blob: 0204e0861134d203f4c2cc0665eaf113d22e445c [file] [log] [blame]
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +03001/*
2 * Utility functions for parsing Tegra CVB voltage tables
3 *
4 * Copyright (C) 2012-2014 NVIDIA Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16#include <linux/err.h>
17#include <linux/kernel.h>
18#include <linux/pm_opp.h>
19
20#include "cvb.h"
21
22/* cvb_mv = ((c2 * speedo / s_scale + c1) * speedo / s_scale + c0) */
23static inline int get_cvb_voltage(int speedo, int s_scale,
24 const struct cvb_coefficients *cvb)
25{
26 int mv;
27
28 /* apply only speedo scale: output mv = cvb_mv * v_scale */
29 mv = DIV_ROUND_CLOSEST(cvb->c2 * speedo, s_scale);
30 mv = DIV_ROUND_CLOSEST((mv + cvb->c1) * speedo, s_scale) + cvb->c0;
31 return mv;
32}
33
34static int round_cvb_voltage(int mv, int v_scale,
35 const struct rail_alignment *align)
36{
37 /* combined: apply voltage scale and round to cvb alignment step */
38 int uv;
39 int step = (align->step_uv ? : 1000) * v_scale;
40 int offset = align->offset_uv * v_scale;
41
42 uv = max(mv * 1000, offset) - offset;
43 uv = DIV_ROUND_UP(uv, step) * align->step_uv + align->offset_uv;
44 return uv / 1000;
45}
46
47enum {
48 DOWN,
49 UP
50};
51
52static int round_voltage(int mv, const struct rail_alignment *align, int up)
53{
54 if (align->step_uv) {
55 int uv;
56
57 uv = max(mv * 1000, align->offset_uv) - align->offset_uv;
58 uv = (uv + (up ? align->step_uv - 1 : 0)) / align->step_uv;
59 return (uv * align->step_uv + align->offset_uv) / 1000;
60 }
61 return mv;
62}
63
64static int build_opp_table(const struct cvb_table *d,
65 int speedo_value,
66 unsigned long max_freq,
67 struct device *opp_dev)
68{
69 int i, ret, dfll_mv, min_mv, max_mv;
70 const struct cvb_table_freq_entry *table = NULL;
71 const struct rail_alignment *align = &d->alignment;
72
73 min_mv = round_voltage(d->min_millivolts, align, UP);
74 max_mv = round_voltage(d->max_millivolts, align, DOWN);
75
76 for (i = 0; i < MAX_DVFS_FREQS; i++) {
77 table = &d->cvb_table[i];
78 if (!table->freq || (table->freq > max_freq))
79 break;
80
81 /*
82 * FIXME after clk_round_rate/clk_determine_rate prototypes
83 * have been updated
84 */
85 if (table->freq & (1<<31))
86 continue;
87
88 dfll_mv = get_cvb_voltage(
89 speedo_value, d->speedo_scale, &table->coefficients);
90 dfll_mv = round_cvb_voltage(dfll_mv, d->voltage_scale, align);
91 dfll_mv = clamp(dfll_mv, min_mv, max_mv);
92
93 ret = dev_pm_opp_add(opp_dev, table->freq, dfll_mv * 1000);
94 if (ret)
95 return ret;
96 }
97
98 return 0;
99}
100
101/**
102 * tegra_cvb_build_opp_table - build OPP table from Tegra CVB tables
103 * @cvb_tables: array of CVB tables
104 * @sz: size of the previously mentioned array
105 * @process_id: process id of the HW module
106 * @speedo_id: speedo id of the HW module
107 * @speedo_value: speedo value of the HW module
108 * @max_rate: highest safe clock rate
109 * @opp_dev: the struct device * for which the OPP table is built
110 *
111 * On Tegra, a CVB table encodes the relationship between operating voltage
112 * and safe maximal frequency for a given module (e.g. GPU or CPU). This
113 * function calculates the optimal voltage-frequency operating points
114 * for the given arguments and exports them via the OPP library for the
115 * given @opp_dev. Returns a pointer to the struct cvb_table that matched
116 * or an ERR_PTR on failure.
117 */
118const struct cvb_table *tegra_cvb_build_opp_table(
119 const struct cvb_table *cvb_tables,
120 size_t sz, int process_id,
121 int speedo_id, int speedo_value,
122 unsigned long max_rate,
123 struct device *opp_dev)
124{
125 int i, ret;
126
127 for (i = 0; i < sz; i++) {
128 const struct cvb_table *d = &cvb_tables[i];
129
130 if (d->speedo_id != -1 && d->speedo_id != speedo_id)
131 continue;
132 if (d->process_id != -1 && d->process_id != process_id)
133 continue;
134
135 ret = build_opp_table(d, speedo_value, max_rate, opp_dev);
136 return ret ? ERR_PTR(ret) : d;
137 }
138
139 return ERR_PTR(-EINVAL);
140}