blob: 624115e82ff92a6c982db5e65aa6e73c95732d5a [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
Thierry Redinge8f6a682016-04-08 15:09:56 +020064static int build_opp_table(struct device *dev, const struct cvb_table *table,
65 int speedo_value, unsigned long max_freq)
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +030066{
Thierry Redinge8f6a682016-04-08 15:09:56 +020067 const struct rail_alignment *align = &table->alignment;
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +030068 int i, ret, dfll_mv, min_mv, max_mv;
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +030069
Thierry Redinge8f6a682016-04-08 15:09:56 +020070 min_mv = round_voltage(table->min_millivolts, align, UP);
71 max_mv = round_voltage(table->max_millivolts, align, DOWN);
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +030072
73 for (i = 0; i < MAX_DVFS_FREQS; i++) {
Thierry Redinge8f6a682016-04-08 15:09:56 +020074 const struct cvb_table_freq_entry *entry = &table->entries[i];
75
76 if (!entry->freq || (entry->freq > max_freq))
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +030077 break;
78
Thierry Redinge8f6a682016-04-08 15:09:56 +020079 dfll_mv = get_cvb_voltage(speedo_value, table->speedo_scale,
80 &entry->coefficients);
81 dfll_mv = round_cvb_voltage(dfll_mv, table->voltage_scale,
82 align);
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +030083 dfll_mv = clamp(dfll_mv, min_mv, max_mv);
84
Thierry Redinge8f6a682016-04-08 15:09:56 +020085 ret = dev_pm_opp_add(dev, entry->freq, dfll_mv * 1000);
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +030086 if (ret)
87 return ret;
88 }
89
90 return 0;
91}
92
93/**
Thierry Redinge8f6a682016-04-08 15:09:56 +020094 * tegra_cvb_add_opp_table - build OPP table from Tegra CVB tables
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +030095 * @cvb_tables: array of CVB tables
96 * @sz: size of the previously mentioned array
97 * @process_id: process id of the HW module
98 * @speedo_id: speedo id of the HW module
99 * @speedo_value: speedo value of the HW module
100 * @max_rate: highest safe clock rate
101 * @opp_dev: the struct device * for which the OPP table is built
102 *
103 * On Tegra, a CVB table encodes the relationship between operating voltage
104 * and safe maximal frequency for a given module (e.g. GPU or CPU). This
105 * function calculates the optimal voltage-frequency operating points
106 * for the given arguments and exports them via the OPP library for the
107 * given @opp_dev. Returns a pointer to the struct cvb_table that matched
108 * or an ERR_PTR on failure.
109 */
Thierry Redinge8f6a682016-04-08 15:09:56 +0200110const struct cvb_table *
111tegra_cvb_add_opp_table(struct device *dev, const struct cvb_table *tables,
112 size_t count, int process_id, int speedo_id,
113 int speedo_value, unsigned long max_freq)
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +0300114{
Thierry Redinge8f6a682016-04-08 15:09:56 +0200115 size_t i;
116 int ret;
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +0300117
Thierry Redinge8f6a682016-04-08 15:09:56 +0200118 for (i = 0; i < count; i++) {
119 const struct cvb_table *table = &tables[i];
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +0300120
Thierry Redinge8f6a682016-04-08 15:09:56 +0200121 if (table->speedo_id != -1 && table->speedo_id != speedo_id)
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +0300122 continue;
123
Thierry Redinge8f6a682016-04-08 15:09:56 +0200124 if (table->process_id != -1 && table->process_id != process_id)
125 continue;
126
127 ret = build_opp_table(dev, table, speedo_value, max_freq);
128 return ret ? ERR_PTR(ret) : table;
Tuomas Tynkkynenfa63aa32015-05-13 17:58:38 +0300129 }
130
131 return ERR_PTR(-EINVAL);
132}
Thierry Redingf7c42d92016-04-08 15:16:28 +0200133
134void tegra_cvb_remove_opp_table(struct device *dev,
135 const struct cvb_table *table,
136 unsigned long max_freq)
137{
138 unsigned int i;
139
140 for (i = 0; i < MAX_DVFS_FREQS; i++) {
141 const struct cvb_table_freq_entry *entry = &table->entries[i];
142
143 if (!entry->freq || (entry->freq > max_freq))
144 break;
145
146 dev_pm_opp_remove(dev, entry->freq);
147 }
148}