lin
2025-02-25 a02983e50ab34c3e7366b27cdeca427a327faebd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SPDX-License-Identifier: GPL-2.0
/*
 * cpu vf test module
 * Copyright (C) 2019 frank@allwinnertech.com
 */
 
#include <linux/device.h>
#include <linux/clk.h>
#include <linux/cpu.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regulator/consumer.h>
 
struct vf_dev {
   struct regulator *cpu_reg;
   struct clk *cpu_clk;
} vf_dev;
 
static ssize_t volt_show(struct class *class, struct class_attribute *attr,
            char *buf)
{
   return sprintf(buf, "%d\n", regulator_get_voltage(vf_dev.cpu_reg));
}
 
static ssize_t volt_store(struct class *class, struct class_attribute *attr,
             const char *buf, size_t count)
{
   int volt;
   int ret;
 
   ret = kstrtoint(buf, 10, &volt);
   if (ret)
       return -EINVAL;
 
   regulator_set_voltage(vf_dev.cpu_reg, volt, volt);
 
   return count;
}
 
static ssize_t freq_show(struct class *class, struct class_attribute *attr,
            char *buf)
{
   return sprintf(buf, "%lu\n", clk_get_rate(vf_dev.cpu_clk));
}
 
static ssize_t freq_store(struct class *class, struct class_attribute *attr,
             const char *buf, size_t count)
{
   int freq;
   int ret;
 
   ret = kstrtoint(buf, 10, &freq);
   if (ret)
       return -EINVAL;
 
   clk_set_rate(vf_dev.cpu_clk, freq);
 
   return count;
}
 
static struct class_attribute vf_class_attrs[] = {
   __ATTR(cpu_volt, S_IWUSR | S_IRUGO, volt_show, volt_store),
   __ATTR(cpu_freq, S_IWUSR | S_IRUGO, freq_show, freq_store),
   __ATTR_NULL,
};
 
static struct class vf_class = {
   .name        = "vf_table",
   .class_attrs    = vf_class_attrs,
};
 
/*
 * An earlier version of opp-v1 bindings used to name the regulator
 * "cpu0-supply", we still need to handle that for backwards compatibility.
 */
static const char *find_supply_name(struct device *dev)
{
   struct device_node *np;
   struct property *pp;
   int cpu = dev->id;
   const char *name = NULL;
 
   np = of_node_get(dev->of_node);
 
   /* This must be valid for sure */
   if (WARN_ON(!np))
       return NULL;
 
   /* Try "cpu0" for older DTs */
   if (!cpu) {
       pp = of_find_property(np, "cpu0-supply", NULL);
       if (pp) {
           name = "cpu0";
           goto node_put;
       }
   }
 
   pp = of_find_property(np, "cpu-supply", NULL);
   if (pp) {
       name = "cpu";
       goto node_put;
   }
 
   dev_dbg(dev, "no regulator for cpu%d\n", cpu);
node_put:
   of_node_put(np);
   return name;
}
 
static int resources_available(void)
{
   struct device *cpu_dev;
   int ret = 0;
   const char *name;
 
   cpu_dev = get_cpu_device(0);
   if (!cpu_dev) {
       pr_err("failed to get cpu0 device\n");
       return -ENODEV;
   }
 
   vf_dev.cpu_clk = clk_get(cpu_dev, NULL);
   ret = PTR_ERR_OR_ZERO(vf_dev.cpu_clk);
   if (ret) {
       /*
        * If cpu's clk node is present, but clock is not yet
        * registered, we should try defering probe.
        */
       if (ret == -EPROBE_DEFER)
           dev_dbg(cpu_dev, "clock not ready, retry\n");
       else
           dev_err(cpu_dev, "failed to get clock: %d\n", ret);
 
       return ret;
   }
 
   name = find_supply_name(cpu_dev);
   /* Platform doesn't require regulator */
   if (!name)
       return 0;
 
   vf_dev.cpu_reg = regulator_get_optional(cpu_dev, name);
   ret = PTR_ERR_OR_ZERO(vf_dev.cpu_reg);
   if (ret) {
       /*
        * If cpu's regulator supply node is present, but regulator is
        * not yet registered, we should try defering probe.
        */
       if (ret == -EPROBE_DEFER)
           dev_dbg(cpu_dev, "cpu0 regulator not ready, retry\n");
       else
           dev_dbg(cpu_dev, "no regulator for cpu0: %d\n", ret);
 
       return ret;
   }
 
   return 0;
}
 
static int __init vf_init(void)
{
   int ret;
 
   ret = resources_available();
   if (ret)
       return ret;
 
   return class_register(&vf_class);
}
 
module_init(vf_init);
MODULE_LICENSE("GPL");