Blame view

drivers/cpufreq/imx-cpufreq-dt.c 5.33 KB
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
1
2
3
4
  // SPDX-License-Identifier: GPL-2.0
  /*
   * Copyright 2019 NXP
   */
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
5
  #include <linux/clk.h>
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
6
  #include <linux/cpu.h>
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
7
  #include <linux/cpufreq.h>
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
8
9
10
11
12
13
14
15
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/nvmem-consumer.h>
  #include <linux/of.h>
  #include <linux/platform_device.h>
  #include <linux/pm_opp.h>
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
16
  #include <linux/regulator/consumer.h>
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
17
  #include <linux/slab.h>
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
18
  #include "cpufreq-dt.h"
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
19
20
  #define OCOTP_CFG3_SPEED_GRADE_SHIFT	8
  #define OCOTP_CFG3_SPEED_GRADE_MASK	(0x3 << 8)
75c000c4b   Anson Huang   cpufreq: imx-cpuf...
21
  #define IMX8MN_OCOTP_CFG3_SPEED_GRADE_MASK	(0xf << 8)
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
22
23
  #define OCOTP_CFG3_MKT_SEGMENT_SHIFT    6
  #define OCOTP_CFG3_MKT_SEGMENT_MASK     (0x3 << 6)
c98330446   Anson Huang   cpufreq: imx-cpuf...
24
25
  #define IMX8MP_OCOTP_CFG3_MKT_SEGMENT_SHIFT    5
  #define IMX8MP_OCOTP_CFG3_MKT_SEGMENT_MASK     (0x3 << 5)
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
26

7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
27
  #define IMX7ULP_MAX_RUN_FREQ	528000
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
28
29
30
  /* cpufreq-dt device registered by imx-cpufreq-dt */
  static struct platform_device *cpufreq_dt_pdev;
  static struct opp_table *cpufreq_opp_table;
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
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
  static struct device *cpu_dev;
  
  enum IMX7ULP_CPUFREQ_CLKS {
  	ARM,
  	CORE,
  	SCS_SEL,
  	HSRUN_CORE,
  	HSRUN_SCS_SEL,
  	FIRC,
  };
  
  static struct clk_bulk_data imx7ulp_clks[] = {
  	{ .id = "arm" },
  	{ .id = "core" },
  	{ .id = "scs_sel" },
  	{ .id = "hsrun_core" },
  	{ .id = "hsrun_scs_sel" },
  	{ .id = "firc" },
  };
  
  static unsigned int imx7ulp_get_intermediate(struct cpufreq_policy *policy,
  					     unsigned int index)
  {
  	return clk_get_rate(imx7ulp_clks[FIRC].clk);
  }
  
  static int imx7ulp_target_intermediate(struct cpufreq_policy *policy,
  					unsigned int index)
  {
  	unsigned int newfreq = policy->freq_table[index].frequency;
  
  	clk_set_parent(imx7ulp_clks[SCS_SEL].clk, imx7ulp_clks[FIRC].clk);
  	clk_set_parent(imx7ulp_clks[HSRUN_SCS_SEL].clk, imx7ulp_clks[FIRC].clk);
  
  	if (newfreq > IMX7ULP_MAX_RUN_FREQ)
  		clk_set_parent(imx7ulp_clks[ARM].clk,
  			       imx7ulp_clks[HSRUN_CORE].clk);
  	else
  		clk_set_parent(imx7ulp_clks[ARM].clk, imx7ulp_clks[CORE].clk);
  
  	return 0;
  }
  
  static struct cpufreq_dt_platform_data imx7ulp_data = {
  	.target_intermediate = imx7ulp_target_intermediate,
  	.get_intermediate = imx7ulp_get_intermediate,
  };
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
78
79
80
  
  static int imx_cpufreq_dt_probe(struct platform_device *pdev)
  {
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
81
  	struct platform_device *dt_pdev;
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
82
83
84
  	u32 cell_value, supported_hw[2];
  	int speed_grade, mkt_segment;
  	int ret;
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
85
  	cpu_dev = get_cpu_device(0);
a30f8a91f   Anson Huang   cpufreq: imx-cpuf...
86
87
  	if (!of_find_property(cpu_dev->of_node, "cpu-supply", NULL))
  		return -ENODEV;
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  	if (of_machine_is_compatible("fsl,imx7ulp")) {
  		ret = clk_bulk_get(cpu_dev, ARRAY_SIZE(imx7ulp_clks),
  				   imx7ulp_clks);
  		if (ret)
  			return ret;
  
  		dt_pdev = platform_device_register_data(NULL, "cpufreq-dt",
  							-1, &imx7ulp_data,
  							sizeof(imx7ulp_data));
  		if (IS_ERR(dt_pdev)) {
  			clk_bulk_put(ARRAY_SIZE(imx7ulp_clks), imx7ulp_clks);
  			ret = PTR_ERR(dt_pdev);
  			dev_err(&pdev->dev, "Failed to register cpufreq-dt: %d
  ", ret);
  			return ret;
  		}
  
  		cpufreq_dt_pdev = dt_pdev;
  
  		return 0;
  	}
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
109
110
111
  	ret = nvmem_cell_read_u32(cpu_dev, "speed_grade", &cell_value);
  	if (ret)
  		return ret;
83fe39ad0   Anson Huang   cpufreq: imx-cpuf...
112
113
  	if (of_machine_is_compatible("fsl,imx8mn") ||
  	    of_machine_is_compatible("fsl,imx8mp"))
75c000c4b   Anson Huang   cpufreq: imx-cpuf...
114
115
116
117
118
  		speed_grade = (cell_value & IMX8MN_OCOTP_CFG3_SPEED_GRADE_MASK)
  			      >> OCOTP_CFG3_SPEED_GRADE_SHIFT;
  	else
  		speed_grade = (cell_value & OCOTP_CFG3_SPEED_GRADE_MASK)
  			      >> OCOTP_CFG3_SPEED_GRADE_SHIFT;
c98330446   Anson Huang   cpufreq: imx-cpuf...
119
120
121
122
123
124
125
  
  	if (of_machine_is_compatible("fsl,imx8mp"))
  		mkt_segment = (cell_value & IMX8MP_OCOTP_CFG3_MKT_SEGMENT_MASK)
  			       >> IMX8MP_OCOTP_CFG3_MKT_SEGMENT_SHIFT;
  	else
  		mkt_segment = (cell_value & OCOTP_CFG3_MKT_SEGMENT_MASK)
  			       >> OCOTP_CFG3_MKT_SEGMENT_SHIFT;
c2147585c   Leonard Crestez   cpufreq: imx-cpuf...
126
127
  
  	/*
af44d180e   Anson Huang   cpufreq: imx-cpuf...
128
129
130
  	 * Early samples without fuses written report "0 0" which may NOT
  	 * match any OPP defined in DT. So clamp to minimum OPP defined in
  	 * DT to avoid warning for "no OPPs".
c2147585c   Leonard Crestez   cpufreq: imx-cpuf...
131
  	 *
5b8010ba7   Anson Huang   cpufreq: imx-cpuf...
132
  	 * Applies to i.MX8M series SoCs.
c2147585c   Leonard Crestez   cpufreq: imx-cpuf...
133
  	 */
af44d180e   Anson Huang   cpufreq: imx-cpuf...
134
135
136
137
  	if (mkt_segment == 0 && speed_grade == 0) {
  		if (of_machine_is_compatible("fsl,imx8mm") ||
  		    of_machine_is_compatible("fsl,imx8mq"))
  			speed_grade = 1;
83fe39ad0   Anson Huang   cpufreq: imx-cpuf...
138
139
  		if (of_machine_is_compatible("fsl,imx8mn") ||
  		    of_machine_is_compatible("fsl,imx8mp"))
af44d180e   Anson Huang   cpufreq: imx-cpuf...
140
141
  			speed_grade = 0xb;
  	}
c2147585c   Leonard Crestez   cpufreq: imx-cpuf...
142

4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
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
  	supported_hw[0] = BIT(speed_grade);
  	supported_hw[1] = BIT(mkt_segment);
  	dev_info(&pdev->dev, "cpu speed grade %d mkt segment %d supported-hw %#x %#x
  ",
  			speed_grade, mkt_segment, supported_hw[0], supported_hw[1]);
  
  	cpufreq_opp_table = dev_pm_opp_set_supported_hw(cpu_dev, supported_hw, 2);
  	if (IS_ERR(cpufreq_opp_table)) {
  		ret = PTR_ERR(cpufreq_opp_table);
  		dev_err(&pdev->dev, "Failed to set supported opp: %d
  ", ret);
  		return ret;
  	}
  
  	cpufreq_dt_pdev = platform_device_register_data(
  			&pdev->dev, "cpufreq-dt", -1, NULL, 0);
  	if (IS_ERR(cpufreq_dt_pdev)) {
  		dev_pm_opp_put_supported_hw(cpufreq_opp_table);
  		ret = PTR_ERR(cpufreq_dt_pdev);
  		dev_err(&pdev->dev, "Failed to register cpufreq-dt: %d
  ", ret);
  		return ret;
  	}
  
  	return 0;
  }
  
  static int imx_cpufreq_dt_remove(struct platform_device *pdev)
  {
  	platform_device_unregister(cpufreq_dt_pdev);
7c2553f0d   Peng Fan   cpufreq: imx-cpuf...
173
174
175
176
  	if (!of_machine_is_compatible("fsl,imx7ulp"))
  		dev_pm_opp_put_supported_hw(cpufreq_opp_table);
  	else
  		clk_bulk_put(ARRAY_SIZE(imx7ulp_clks), imx7ulp_clks);
4d28ba1d6   Leonard Crestez   cpufreq: Add imx-...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  
  	return 0;
  }
  
  static struct platform_driver imx_cpufreq_dt_driver = {
  	.probe = imx_cpufreq_dt_probe,
  	.remove = imx_cpufreq_dt_remove,
  	.driver = {
  		.name = "imx-cpufreq-dt",
  	},
  };
  module_platform_driver(imx_cpufreq_dt_driver);
  
  MODULE_ALIAS("platform:imx-cpufreq-dt");
  MODULE_DESCRIPTION("Freescale i.MX cpufreq speed grading driver");
  MODULE_LICENSE("GPL v2");