xref: /openbmc/linux/drivers/mfd/qcom_rpm.c (revision 92a2c6b2)
1 /*
2  * Copyright (c) 2014, Sony Mobile Communications AB.
3  * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4  * Author: Bjorn Andersson <bjorn.andersson@sonymobile.com>
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 and
8  * only version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/of_platform.h>
19 #include <linux/io.h>
20 #include <linux/interrupt.h>
21 #include <linux/mfd/qcom_rpm.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/regmap.h>
24 
25 #include <dt-bindings/mfd/qcom-rpm.h>
26 
27 struct qcom_rpm_resource {
28 	unsigned target_id;
29 	unsigned status_id;
30 	unsigned select_id;
31 	unsigned size;
32 };
33 
34 struct qcom_rpm_data {
35 	u32 version;
36 	const struct qcom_rpm_resource *resource_table;
37 	unsigned n_resources;
38 };
39 
40 struct qcom_rpm {
41 	struct device *dev;
42 	struct regmap *ipc_regmap;
43 	unsigned ipc_offset;
44 	unsigned ipc_bit;
45 
46 	struct completion ack;
47 	struct mutex lock;
48 
49 	void __iomem *status_regs;
50 	void __iomem *ctrl_regs;
51 	void __iomem *req_regs;
52 
53 	u32 ack_status;
54 
55 	const struct qcom_rpm_data *data;
56 };
57 
58 #define RPM_STATUS_REG(rpm, i)	((rpm)->status_regs + (i) * 4)
59 #define RPM_CTRL_REG(rpm, i)	((rpm)->ctrl_regs + (i) * 4)
60 #define RPM_REQ_REG(rpm, i)	((rpm)->req_regs + (i) * 4)
61 
62 #define RPM_REQUEST_TIMEOUT	(5 * HZ)
63 
64 #define RPM_REQUEST_CONTEXT	3
65 #define RPM_REQ_SELECT		11
66 #define RPM_ACK_CONTEXT		15
67 #define RPM_ACK_SELECTOR	23
68 #define RPM_SELECT_SIZE		7
69 
70 #define RPM_NOTIFICATION	BIT(30)
71 #define RPM_REJECTED		BIT(31)
72 
73 #define RPM_SIGNAL		BIT(2)
74 
75 static const struct qcom_rpm_resource apq8064_rpm_resource_table[] = {
76 	[QCOM_RPM_CXO_CLK] =			{ 25, 9, 5, 1 },
77 	[QCOM_RPM_PXO_CLK] =			{ 26, 10, 6, 1 },
78 	[QCOM_RPM_APPS_FABRIC_CLK] =		{ 27, 11, 8, 1 },
79 	[QCOM_RPM_SYS_FABRIC_CLK] =		{ 28, 12, 9, 1 },
80 	[QCOM_RPM_MM_FABRIC_CLK] =		{ 29, 13, 10, 1 },
81 	[QCOM_RPM_DAYTONA_FABRIC_CLK] =		{ 30, 14, 11, 1 },
82 	[QCOM_RPM_SFPB_CLK] =			{ 31, 15, 12, 1 },
83 	[QCOM_RPM_CFPB_CLK] =			{ 32, 16, 13, 1 },
84 	[QCOM_RPM_MMFPB_CLK] =			{ 33, 17, 14, 1 },
85 	[QCOM_RPM_EBI1_CLK] =			{ 34, 18, 16, 1 },
86 	[QCOM_RPM_APPS_FABRIC_HALT] =		{ 35, 19, 18, 1 },
87 	[QCOM_RPM_APPS_FABRIC_MODE] =		{ 37, 20, 19, 1 },
88 	[QCOM_RPM_APPS_FABRIC_IOCTL] =		{ 40, 21, 20, 1 },
89 	[QCOM_RPM_APPS_FABRIC_ARB] =		{ 41, 22, 21, 12 },
90 	[QCOM_RPM_SYS_FABRIC_HALT] =		{ 53, 23, 22, 1 },
91 	[QCOM_RPM_SYS_FABRIC_MODE] =		{ 55, 24, 23, 1 },
92 	[QCOM_RPM_SYS_FABRIC_IOCTL] =		{ 58, 25, 24, 1 },
93 	[QCOM_RPM_SYS_FABRIC_ARB] =		{ 59, 26, 25, 30 },
94 	[QCOM_RPM_MM_FABRIC_HALT] =		{ 89, 27, 26, 1 },
95 	[QCOM_RPM_MM_FABRIC_MODE] =		{ 91, 28, 27, 1 },
96 	[QCOM_RPM_MM_FABRIC_IOCTL] =		{ 94, 29, 28, 1 },
97 	[QCOM_RPM_MM_FABRIC_ARB] =		{ 95, 30, 29, 21 },
98 	[QCOM_RPM_PM8921_SMPS1] =		{ 116, 31, 30, 2 },
99 	[QCOM_RPM_PM8921_SMPS2] =		{ 118, 33, 31, 2 },
100 	[QCOM_RPM_PM8921_SMPS3] =		{ 120, 35, 32, 2 },
101 	[QCOM_RPM_PM8921_SMPS4] =		{ 122, 37, 33, 2 },
102 	[QCOM_RPM_PM8921_SMPS5] =		{ 124, 39, 34, 2 },
103 	[QCOM_RPM_PM8921_SMPS6] =		{ 126, 41, 35, 2 },
104 	[QCOM_RPM_PM8921_SMPS7] =		{ 128, 43, 36, 2 },
105 	[QCOM_RPM_PM8921_SMPS8] =		{ 130, 45, 37, 2 },
106 	[QCOM_RPM_PM8921_LDO1] =		{ 132, 47, 38, 2 },
107 	[QCOM_RPM_PM8921_LDO2] =		{ 134, 49, 39, 2 },
108 	[QCOM_RPM_PM8921_LDO3] =		{ 136, 51, 40, 2 },
109 	[QCOM_RPM_PM8921_LDO4] =		{ 138, 53, 41, 2 },
110 	[QCOM_RPM_PM8921_LDO5] =		{ 140, 55, 42, 2 },
111 	[QCOM_RPM_PM8921_LDO6] =		{ 142, 57, 43, 2 },
112 	[QCOM_RPM_PM8921_LDO7] =		{ 144, 59, 44, 2 },
113 	[QCOM_RPM_PM8921_LDO8] =		{ 146, 61, 45, 2 },
114 	[QCOM_RPM_PM8921_LDO9] =		{ 148, 63, 46, 2 },
115 	[QCOM_RPM_PM8921_LDO10] =		{ 150, 65, 47, 2 },
116 	[QCOM_RPM_PM8921_LDO11] =		{ 152, 67, 48, 2 },
117 	[QCOM_RPM_PM8921_LDO12] =		{ 154, 69, 49, 2 },
118 	[QCOM_RPM_PM8921_LDO13] =		{ 156, 71, 50, 2 },
119 	[QCOM_RPM_PM8921_LDO14] =		{ 158, 73, 51, 2 },
120 	[QCOM_RPM_PM8921_LDO15] =		{ 160, 75, 52, 2 },
121 	[QCOM_RPM_PM8921_LDO16] =		{ 162, 77, 53, 2 },
122 	[QCOM_RPM_PM8921_LDO17] =		{ 164, 79, 54, 2 },
123 	[QCOM_RPM_PM8921_LDO18] =		{ 166, 81, 55, 2 },
124 	[QCOM_RPM_PM8921_LDO19] =		{ 168, 83, 56, 2 },
125 	[QCOM_RPM_PM8921_LDO20] =		{ 170, 85, 57, 2 },
126 	[QCOM_RPM_PM8921_LDO21] =		{ 172, 87, 58, 2 },
127 	[QCOM_RPM_PM8921_LDO22] =		{ 174, 89, 59, 2 },
128 	[QCOM_RPM_PM8921_LDO23] =		{ 176, 91, 60, 2 },
129 	[QCOM_RPM_PM8921_LDO24] =		{ 178, 93, 61, 2 },
130 	[QCOM_RPM_PM8921_LDO25] =		{ 180, 95, 62, 2 },
131 	[QCOM_RPM_PM8921_LDO26] =		{ 182, 97, 63, 2 },
132 	[QCOM_RPM_PM8921_LDO27] =		{ 184, 99, 64, 2 },
133 	[QCOM_RPM_PM8921_LDO28] =		{ 186, 101, 65, 2 },
134 	[QCOM_RPM_PM8921_LDO29] =		{ 188, 103, 66, 2 },
135 	[QCOM_RPM_PM8921_CLK1] =		{ 190, 105, 67, 2 },
136 	[QCOM_RPM_PM8921_CLK2] =		{ 192, 107, 68, 2 },
137 	[QCOM_RPM_PM8921_LVS1] =		{ 194, 109, 69, 1 },
138 	[QCOM_RPM_PM8921_LVS2] =		{ 195, 110, 70, 1 },
139 	[QCOM_RPM_PM8921_LVS3] =		{ 196, 111, 71, 1 },
140 	[QCOM_RPM_PM8921_LVS4] =		{ 197, 112, 72, 1 },
141 	[QCOM_RPM_PM8921_LVS5] =		{ 198, 113, 73, 1 },
142 	[QCOM_RPM_PM8921_LVS6] =		{ 199, 114, 74, 1 },
143 	[QCOM_RPM_PM8921_LVS7] =		{ 200, 115, 75, 1 },
144 	[QCOM_RPM_PM8821_SMPS1] =		{ 201, 116, 76, 2 },
145 	[QCOM_RPM_PM8821_SMPS2] =		{ 203, 118, 77, 2 },
146 	[QCOM_RPM_PM8821_LDO1] =		{ 205, 120, 78, 2 },
147 	[QCOM_RPM_PM8921_NCP] =			{ 207, 122, 80, 2 },
148 	[QCOM_RPM_CXO_BUFFERS] =		{ 209, 124, 81, 1 },
149 	[QCOM_RPM_USB_OTG_SWITCH] =		{ 210, 125, 82, 1 },
150 	[QCOM_RPM_HDMI_SWITCH] =		{ 211, 126, 83, 1 },
151 	[QCOM_RPM_DDR_DMM] =			{ 212, 127, 84, 2 },
152 	[QCOM_RPM_VDDMIN_GPIO] =		{ 215, 131, 89, 1 },
153 };
154 
155 static const struct qcom_rpm_data apq8064_template = {
156 	.version = 3,
157 	.resource_table = apq8064_rpm_resource_table,
158 	.n_resources = ARRAY_SIZE(apq8064_rpm_resource_table),
159 };
160 
161 static const struct qcom_rpm_resource msm8660_rpm_resource_table[] = {
162 	[QCOM_RPM_CXO_CLK] =			{ 32, 12, 5, 1 },
163 	[QCOM_RPM_PXO_CLK] =			{ 33, 13, 6, 1 },
164 	[QCOM_RPM_PLL_4] =			{ 34, 14, 7, 1 },
165 	[QCOM_RPM_APPS_FABRIC_CLK] =		{ 35, 15, 8, 1 },
166 	[QCOM_RPM_SYS_FABRIC_CLK] =		{ 36, 16, 9, 1 },
167 	[QCOM_RPM_MM_FABRIC_CLK] =		{ 37, 17, 10, 1 },
168 	[QCOM_RPM_DAYTONA_FABRIC_CLK] =		{ 38, 18, 11, 1 },
169 	[QCOM_RPM_SFPB_CLK] =			{ 39, 19, 12, 1 },
170 	[QCOM_RPM_CFPB_CLK] =			{ 40, 20, 13, 1 },
171 	[QCOM_RPM_MMFPB_CLK] =			{ 41, 21, 14, 1 },
172 	[QCOM_RPM_SMI_CLK] =			{ 42, 22, 15, 1 },
173 	[QCOM_RPM_EBI1_CLK] =			{ 43, 23, 16, 1 },
174 	[QCOM_RPM_APPS_L2_CACHE_CTL] =		{ 44, 24, 17, 1 },
175 	[QCOM_RPM_APPS_FABRIC_HALT] =		{ 45, 25, 18, 2 },
176 	[QCOM_RPM_APPS_FABRIC_MODE] =		{ 47, 26, 19, 3 },
177 	[QCOM_RPM_APPS_FABRIC_ARB] =		{ 51, 28, 21, 6 },
178 	[QCOM_RPM_SYS_FABRIC_HALT] =		{ 63, 29, 22, 2 },
179 	[QCOM_RPM_SYS_FABRIC_MODE] =		{ 65, 30, 23, 3 },
180 	[QCOM_RPM_SYS_FABRIC_ARB] =		{ 69, 32, 25, 22 },
181 	[QCOM_RPM_MM_FABRIC_HALT] =		{ 105, 33, 26, 2 },
182 	[QCOM_RPM_MM_FABRIC_MODE] =		{ 107, 34, 27, 3 },
183 	[QCOM_RPM_MM_FABRIC_ARB] =		{ 111, 36, 29, 23 },
184 	[QCOM_RPM_PM8901_SMPS0] =		{ 134, 37, 30, 2 },
185 	[QCOM_RPM_PM8901_SMPS1] =		{ 136, 39, 31, 2 },
186 	[QCOM_RPM_PM8901_SMPS2] =		{ 138, 41, 32, 2 },
187 	[QCOM_RPM_PM8901_SMPS3] =		{ 140, 43, 33, 2 },
188 	[QCOM_RPM_PM8901_SMPS4] =		{ 142, 45, 34, 2 },
189 	[QCOM_RPM_PM8901_LDO0] =		{ 144, 47, 35, 2 },
190 	[QCOM_RPM_PM8901_LDO1] =		{ 146, 49, 36, 2 },
191 	[QCOM_RPM_PM8901_LDO2] =		{ 148, 51, 37, 2 },
192 	[QCOM_RPM_PM8901_LDO3] =		{ 150, 53, 38, 2 },
193 	[QCOM_RPM_PM8901_LDO4] =		{ 152, 55, 39, 2 },
194 	[QCOM_RPM_PM8901_LDO5] =		{ 154, 57, 40, 2 },
195 	[QCOM_RPM_PM8901_LDO6] =		{ 156, 59, 41, 2 },
196 	[QCOM_RPM_PM8901_LVS0] =		{ 158, 61, 42, 1 },
197 	[QCOM_RPM_PM8901_LVS1] =		{ 159, 62, 43, 1 },
198 	[QCOM_RPM_PM8901_LVS2] =		{ 160, 63, 44, 1 },
199 	[QCOM_RPM_PM8901_LVS3] =		{ 161, 64, 45, 1 },
200 	[QCOM_RPM_PM8901_MVS] =			{ 162, 65, 46, 1 },
201 	[QCOM_RPM_PM8058_SMPS0] =		{ 163, 66, 47, 2 },
202 	[QCOM_RPM_PM8058_SMPS1] =		{ 165, 68, 48, 2 },
203 	[QCOM_RPM_PM8058_SMPS2] =		{ 167, 70, 49, 2 },
204 	[QCOM_RPM_PM8058_SMPS3] =		{ 169, 72, 50, 2 },
205 	[QCOM_RPM_PM8058_SMPS4] =		{ 171, 74, 51, 2 },
206 	[QCOM_RPM_PM8058_LDO0] =		{ 173, 76, 52, 2 },
207 	[QCOM_RPM_PM8058_LDO1] =		{ 175, 78, 53, 2 },
208 	[QCOM_RPM_PM8058_LDO2] =		{ 177, 80, 54, 2 },
209 	[QCOM_RPM_PM8058_LDO3] =		{ 179, 82, 55, 2 },
210 	[QCOM_RPM_PM8058_LDO4] =		{ 181, 84, 56, 2 },
211 	[QCOM_RPM_PM8058_LDO5] =		{ 183, 86, 57, 2 },
212 	[QCOM_RPM_PM8058_LDO6] =		{ 185, 88, 58, 2 },
213 	[QCOM_RPM_PM8058_LDO7] =		{ 187, 90, 59, 2 },
214 	[QCOM_RPM_PM8058_LDO8] =		{ 189, 92, 60, 2 },
215 	[QCOM_RPM_PM8058_LDO9] =		{ 191, 94, 61, 2 },
216 	[QCOM_RPM_PM8058_LDO10] =		{ 193, 96, 62, 2 },
217 	[QCOM_RPM_PM8058_LDO11] =		{ 195, 98, 63, 2 },
218 	[QCOM_RPM_PM8058_LDO12] =		{ 197, 100, 64, 2 },
219 	[QCOM_RPM_PM8058_LDO13] =		{ 199, 102, 65, 2 },
220 	[QCOM_RPM_PM8058_LDO14] =		{ 201, 104, 66, 2 },
221 	[QCOM_RPM_PM8058_LDO15] =		{ 203, 106, 67, 2 },
222 	[QCOM_RPM_PM8058_LDO16] =		{ 205, 108, 68, 2 },
223 	[QCOM_RPM_PM8058_LDO17] =		{ 207, 110, 69, 2 },
224 	[QCOM_RPM_PM8058_LDO18] =		{ 209, 112, 70, 2 },
225 	[QCOM_RPM_PM8058_LDO19] =		{ 211, 114, 71, 2 },
226 	[QCOM_RPM_PM8058_LDO20] =		{ 213, 116, 72, 2 },
227 	[QCOM_RPM_PM8058_LDO21] =		{ 215, 118, 73, 2 },
228 	[QCOM_RPM_PM8058_LDO22] =		{ 217, 120, 74, 2 },
229 	[QCOM_RPM_PM8058_LDO23] =		{ 219, 122, 75, 2 },
230 	[QCOM_RPM_PM8058_LDO24] =		{ 221, 124, 76, 2 },
231 	[QCOM_RPM_PM8058_LDO25] =		{ 223, 126, 77, 2 },
232 	[QCOM_RPM_PM8058_LVS0] =		{ 225, 128, 78, 1 },
233 	[QCOM_RPM_PM8058_LVS1] =		{ 226, 129, 79, 1 },
234 	[QCOM_RPM_PM8058_NCP] =			{ 227, 130, 80, 2 },
235 	[QCOM_RPM_CXO_BUFFERS] =		{ 229, 132, 81, 1 },
236 };
237 
238 static const struct qcom_rpm_data msm8660_template = {
239 	.version = 2,
240 	.resource_table = msm8660_rpm_resource_table,
241 	.n_resources = ARRAY_SIZE(msm8660_rpm_resource_table),
242 };
243 
244 static const struct qcom_rpm_resource msm8960_rpm_resource_table[] = {
245 	[QCOM_RPM_CXO_CLK] =			{ 25, 9, 5, 1 },
246 	[QCOM_RPM_PXO_CLK] =			{ 26, 10, 6, 1 },
247 	[QCOM_RPM_APPS_FABRIC_CLK] =		{ 27, 11, 8, 1 },
248 	[QCOM_RPM_SYS_FABRIC_CLK] =		{ 28, 12, 9, 1 },
249 	[QCOM_RPM_MM_FABRIC_CLK] =		{ 29, 13, 10, 1 },
250 	[QCOM_RPM_DAYTONA_FABRIC_CLK] =		{ 30, 14, 11, 1 },
251 	[QCOM_RPM_SFPB_CLK] =			{ 31, 15, 12, 1 },
252 	[QCOM_RPM_CFPB_CLK] =			{ 32, 16, 13, 1 },
253 	[QCOM_RPM_MMFPB_CLK] =			{ 33, 17, 14, 1 },
254 	[QCOM_RPM_EBI1_CLK] =			{ 34, 18, 16, 1 },
255 	[QCOM_RPM_APPS_FABRIC_HALT] =		{ 35, 19, 18, 1 },
256 	[QCOM_RPM_APPS_FABRIC_MODE] =		{ 37, 20, 19, 1 },
257 	[QCOM_RPM_APPS_FABRIC_IOCTL] =		{ 40, 21, 20, 1 },
258 	[QCOM_RPM_APPS_FABRIC_ARB] =		{ 41, 22, 21, 12 },
259 	[QCOM_RPM_SYS_FABRIC_HALT] =		{ 53, 23, 22, 1 },
260 	[QCOM_RPM_SYS_FABRIC_MODE] =		{ 55, 24, 23, 1 },
261 	[QCOM_RPM_SYS_FABRIC_IOCTL] =		{ 58, 25, 24, 1 },
262 	[QCOM_RPM_SYS_FABRIC_ARB] =		{ 59, 26, 25, 29 },
263 	[QCOM_RPM_MM_FABRIC_HALT] =		{ 88, 27, 26, 1 },
264 	[QCOM_RPM_MM_FABRIC_MODE] =		{ 90, 28, 27, 1 },
265 	[QCOM_RPM_MM_FABRIC_IOCTL] =		{ 93, 29, 28, 1 },
266 	[QCOM_RPM_MM_FABRIC_ARB] =		{ 94, 30, 29, 23 },
267 	[QCOM_RPM_PM8921_SMPS1] =		{ 117, 31, 30, 2 },
268 	[QCOM_RPM_PM8921_SMPS2] =		{ 119, 33, 31, 2 },
269 	[QCOM_RPM_PM8921_SMPS3] =		{ 121, 35, 32, 2 },
270 	[QCOM_RPM_PM8921_SMPS4] =		{ 123, 37, 33, 2 },
271 	[QCOM_RPM_PM8921_SMPS5] =		{ 125, 39, 34, 2 },
272 	[QCOM_RPM_PM8921_SMPS6] =		{ 127, 41, 35, 2 },
273 	[QCOM_RPM_PM8921_SMPS7] =		{ 129, 43, 36, 2 },
274 	[QCOM_RPM_PM8921_SMPS8] =		{ 131, 45, 37, 2 },
275 	[QCOM_RPM_PM8921_LDO1] =		{ 133, 47, 38, 2 },
276 	[QCOM_RPM_PM8921_LDO2] =		{ 135, 49, 39, 2 },
277 	[QCOM_RPM_PM8921_LDO3] =		{ 137, 51, 40, 2 },
278 	[QCOM_RPM_PM8921_LDO4] =		{ 139, 53, 41, 2 },
279 	[QCOM_RPM_PM8921_LDO5] =		{ 141, 55, 42, 2 },
280 	[QCOM_RPM_PM8921_LDO6] =		{ 143, 57, 43, 2 },
281 	[QCOM_RPM_PM8921_LDO7] =		{ 145, 59, 44, 2 },
282 	[QCOM_RPM_PM8921_LDO8] =		{ 147, 61, 45, 2 },
283 	[QCOM_RPM_PM8921_LDO9] =		{ 149, 63, 46, 2 },
284 	[QCOM_RPM_PM8921_LDO10] =		{ 151, 65, 47, 2 },
285 	[QCOM_RPM_PM8921_LDO11] =		{ 153, 67, 48, 2 },
286 	[QCOM_RPM_PM8921_LDO12] =		{ 155, 69, 49, 2 },
287 	[QCOM_RPM_PM8921_LDO13] =		{ 157, 71, 50, 2 },
288 	[QCOM_RPM_PM8921_LDO14] =		{ 159, 73, 51, 2 },
289 	[QCOM_RPM_PM8921_LDO15] =		{ 161, 75, 52, 2 },
290 	[QCOM_RPM_PM8921_LDO16] =		{ 163, 77, 53, 2 },
291 	[QCOM_RPM_PM8921_LDO17] =		{ 165, 79, 54, 2 },
292 	[QCOM_RPM_PM8921_LDO18] =		{ 167, 81, 55, 2 },
293 	[QCOM_RPM_PM8921_LDO19] =		{ 169, 83, 56, 2 },
294 	[QCOM_RPM_PM8921_LDO20] =		{ 171, 85, 57, 2 },
295 	[QCOM_RPM_PM8921_LDO21] =		{ 173, 87, 58, 2 },
296 	[QCOM_RPM_PM8921_LDO22] =		{ 175, 89, 59, 2 },
297 	[QCOM_RPM_PM8921_LDO23] =		{ 177, 91, 60, 2 },
298 	[QCOM_RPM_PM8921_LDO24] =		{ 179, 93, 61, 2 },
299 	[QCOM_RPM_PM8921_LDO25] =		{ 181, 95, 62, 2 },
300 	[QCOM_RPM_PM8921_LDO26] =		{ 183, 97, 63, 2 },
301 	[QCOM_RPM_PM8921_LDO27] =		{ 185, 99, 64, 2 },
302 	[QCOM_RPM_PM8921_LDO28] =		{ 187, 101, 65, 2 },
303 	[QCOM_RPM_PM8921_LDO29] =		{ 189, 103, 66, 2 },
304 	[QCOM_RPM_PM8921_CLK1] =		{ 191, 105, 67, 2 },
305 	[QCOM_RPM_PM8921_CLK2] =		{ 193, 107, 68, 2 },
306 	[QCOM_RPM_PM8921_LVS1] =		{ 195, 109, 69, 1 },
307 	[QCOM_RPM_PM8921_LVS2] =		{ 196, 110, 70, 1 },
308 	[QCOM_RPM_PM8921_LVS3] =		{ 197, 111, 71, 1 },
309 	[QCOM_RPM_PM8921_LVS4] =		{ 198, 112, 72, 1 },
310 	[QCOM_RPM_PM8921_LVS5] =		{ 199, 113, 73, 1 },
311 	[QCOM_RPM_PM8921_LVS6] =		{ 200, 114, 74, 1 },
312 	[QCOM_RPM_PM8921_LVS7] =		{ 201, 115, 75, 1 },
313 	[QCOM_RPM_PM8921_NCP] =			{ 202, 116, 80, 2 },
314 	[QCOM_RPM_CXO_BUFFERS] =		{ 204, 118, 81, 1 },
315 	[QCOM_RPM_USB_OTG_SWITCH] =		{ 205, 119, 82, 1 },
316 	[QCOM_RPM_HDMI_SWITCH] =		{ 206, 120, 83, 1 },
317 	[QCOM_RPM_DDR_DMM] =			{ 207, 121, 84, 2 },
318 };
319 
320 static const struct qcom_rpm_data msm8960_template = {
321 	.version = 3,
322 	.resource_table = msm8960_rpm_resource_table,
323 	.n_resources = ARRAY_SIZE(msm8960_rpm_resource_table),
324 };
325 
326 static const struct of_device_id qcom_rpm_of_match[] = {
327 	{ .compatible = "qcom,rpm-apq8064", .data = &apq8064_template },
328 	{ .compatible = "qcom,rpm-msm8660", .data = &msm8660_template },
329 	{ .compatible = "qcom,rpm-msm8960", .data = &msm8960_template },
330 	{ }
331 };
332 MODULE_DEVICE_TABLE(of, qcom_rpm_of_match);
333 
334 int qcom_rpm_write(struct qcom_rpm *rpm,
335 		   int state,
336 		   int resource,
337 		   u32 *buf, size_t count)
338 {
339 	const struct qcom_rpm_resource *res;
340 	const struct qcom_rpm_data *data = rpm->data;
341 	u32 sel_mask[RPM_SELECT_SIZE] = { 0 };
342 	int left;
343 	int ret = 0;
344 	int i;
345 
346 	if (WARN_ON(resource < 0 || resource >= data->n_resources))
347 		return -EINVAL;
348 
349 	res = &data->resource_table[resource];
350 	if (WARN_ON(res->size != count))
351 		return -EINVAL;
352 
353 	mutex_lock(&rpm->lock);
354 
355 	for (i = 0; i < res->size; i++)
356 		writel_relaxed(buf[i], RPM_REQ_REG(rpm, res->target_id + i));
357 
358 	bitmap_set((unsigned long *)sel_mask, res->select_id, 1);
359 	for (i = 0; i < ARRAY_SIZE(sel_mask); i++) {
360 		writel_relaxed(sel_mask[i],
361 			       RPM_CTRL_REG(rpm, RPM_REQ_SELECT + i));
362 	}
363 
364 	writel_relaxed(BIT(state), RPM_CTRL_REG(rpm, RPM_REQUEST_CONTEXT));
365 
366 	reinit_completion(&rpm->ack);
367 	regmap_write(rpm->ipc_regmap, rpm->ipc_offset, BIT(rpm->ipc_bit));
368 
369 	left = wait_for_completion_timeout(&rpm->ack, RPM_REQUEST_TIMEOUT);
370 	if (!left)
371 		ret = -ETIMEDOUT;
372 	else if (rpm->ack_status & RPM_REJECTED)
373 		ret = -EIO;
374 
375 	mutex_unlock(&rpm->lock);
376 
377 	return ret;
378 }
379 EXPORT_SYMBOL(qcom_rpm_write);
380 
381 static irqreturn_t qcom_rpm_ack_interrupt(int irq, void *dev)
382 {
383 	struct qcom_rpm *rpm = dev;
384 	u32 ack;
385 	int i;
386 
387 	ack = readl_relaxed(RPM_CTRL_REG(rpm, RPM_ACK_CONTEXT));
388 	for (i = 0; i < RPM_SELECT_SIZE; i++)
389 		writel_relaxed(0, RPM_CTRL_REG(rpm, RPM_ACK_SELECTOR + i));
390 	writel(0, RPM_CTRL_REG(rpm, RPM_ACK_CONTEXT));
391 
392 	if (ack & RPM_NOTIFICATION) {
393 		dev_warn(rpm->dev, "ignoring notification!\n");
394 	} else {
395 		rpm->ack_status = ack;
396 		complete(&rpm->ack);
397 	}
398 
399 	return IRQ_HANDLED;
400 }
401 
402 static irqreturn_t qcom_rpm_err_interrupt(int irq, void *dev)
403 {
404 	struct qcom_rpm *rpm = dev;
405 
406 	regmap_write(rpm->ipc_regmap, rpm->ipc_offset, BIT(rpm->ipc_bit));
407 	dev_err(rpm->dev, "RPM triggered fatal error\n");
408 
409 	return IRQ_HANDLED;
410 }
411 
412 static irqreturn_t qcom_rpm_wakeup_interrupt(int irq, void *dev)
413 {
414 	return IRQ_HANDLED;
415 }
416 
417 static int qcom_rpm_probe(struct platform_device *pdev)
418 {
419 	const struct of_device_id *match;
420 	struct device_node *syscon_np;
421 	struct resource *res;
422 	struct qcom_rpm *rpm;
423 	u32 fw_version[3];
424 	int irq_wakeup;
425 	int irq_ack;
426 	int irq_err;
427 	int ret;
428 
429 	rpm = devm_kzalloc(&pdev->dev, sizeof(*rpm), GFP_KERNEL);
430 	if (!rpm)
431 		return -ENOMEM;
432 
433 	rpm->dev = &pdev->dev;
434 	mutex_init(&rpm->lock);
435 	init_completion(&rpm->ack);
436 
437 	irq_ack = platform_get_irq_byname(pdev, "ack");
438 	if (irq_ack < 0) {
439 		dev_err(&pdev->dev, "required ack interrupt missing\n");
440 		return irq_ack;
441 	}
442 
443 	irq_err = platform_get_irq_byname(pdev, "err");
444 	if (irq_err < 0) {
445 		dev_err(&pdev->dev, "required err interrupt missing\n");
446 		return irq_err;
447 	}
448 
449 	irq_wakeup = platform_get_irq_byname(pdev, "wakeup");
450 	if (irq_wakeup < 0) {
451 		dev_err(&pdev->dev, "required wakeup interrupt missing\n");
452 		return irq_wakeup;
453 	}
454 
455 	match = of_match_device(qcom_rpm_of_match, &pdev->dev);
456 	rpm->data = match->data;
457 
458 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
459 	rpm->status_regs = devm_ioremap_resource(&pdev->dev, res);
460 	if (IS_ERR(rpm->status_regs))
461 		return PTR_ERR(rpm->status_regs);
462 	rpm->ctrl_regs = rpm->status_regs + 0x400;
463 	rpm->req_regs = rpm->status_regs + 0x600;
464 
465 	syscon_np = of_parse_phandle(pdev->dev.of_node, "qcom,ipc", 0);
466 	if (!syscon_np) {
467 		dev_err(&pdev->dev, "no qcom,ipc node\n");
468 		return -ENODEV;
469 	}
470 
471 	rpm->ipc_regmap = syscon_node_to_regmap(syscon_np);
472 	if (IS_ERR(rpm->ipc_regmap))
473 		return PTR_ERR(rpm->ipc_regmap);
474 
475 	ret = of_property_read_u32_index(pdev->dev.of_node, "qcom,ipc", 1,
476 					 &rpm->ipc_offset);
477 	if (ret < 0) {
478 		dev_err(&pdev->dev, "no offset in qcom,ipc\n");
479 		return -EINVAL;
480 	}
481 
482 	ret = of_property_read_u32_index(pdev->dev.of_node, "qcom,ipc", 2,
483 					 &rpm->ipc_bit);
484 	if (ret < 0) {
485 		dev_err(&pdev->dev, "no bit in qcom,ipc\n");
486 		return -EINVAL;
487 	}
488 
489 	dev_set_drvdata(&pdev->dev, rpm);
490 
491 	fw_version[0] = readl(RPM_STATUS_REG(rpm, 0));
492 	fw_version[1] = readl(RPM_STATUS_REG(rpm, 1));
493 	fw_version[2] = readl(RPM_STATUS_REG(rpm, 2));
494 	if (fw_version[0] != rpm->data->version) {
495 		dev_err(&pdev->dev,
496 			"RPM version %u.%u.%u incompatible with driver version %u",
497 			fw_version[0],
498 			fw_version[1],
499 			fw_version[2],
500 			rpm->data->version);
501 		return -EFAULT;
502 	}
503 
504 	dev_info(&pdev->dev, "RPM firmware %u.%u.%u\n", fw_version[0],
505 							fw_version[1],
506 							fw_version[2]);
507 
508 	ret = devm_request_irq(&pdev->dev,
509 			       irq_ack,
510 			       qcom_rpm_ack_interrupt,
511 			       IRQF_TRIGGER_RISING | IRQF_NO_SUSPEND,
512 			       "qcom_rpm_ack",
513 			       rpm);
514 	if (ret) {
515 		dev_err(&pdev->dev, "failed to request ack interrupt\n");
516 		return ret;
517 	}
518 
519 	ret = irq_set_irq_wake(irq_ack, 1);
520 	if (ret)
521 		dev_warn(&pdev->dev, "failed to mark ack irq as wakeup\n");
522 
523 	ret = devm_request_irq(&pdev->dev,
524 			       irq_err,
525 			       qcom_rpm_err_interrupt,
526 			       IRQF_TRIGGER_RISING,
527 			       "qcom_rpm_err",
528 			       rpm);
529 	if (ret) {
530 		dev_err(&pdev->dev, "failed to request err interrupt\n");
531 		return ret;
532 	}
533 
534 	ret = devm_request_irq(&pdev->dev,
535 			       irq_wakeup,
536 			       qcom_rpm_wakeup_interrupt,
537 			       IRQF_TRIGGER_RISING,
538 			       "qcom_rpm_wakeup",
539 			       rpm);
540 	if (ret) {
541 		dev_err(&pdev->dev, "failed to request wakeup interrupt\n");
542 		return ret;
543 	}
544 
545 	ret = irq_set_irq_wake(irq_wakeup, 1);
546 	if (ret)
547 		dev_warn(&pdev->dev, "failed to mark wakeup irq as wakeup\n");
548 
549 	return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
550 }
551 
552 static int qcom_rpm_remove(struct platform_device *pdev)
553 {
554 	of_platform_depopulate(&pdev->dev);
555 	return 0;
556 }
557 
558 static struct platform_driver qcom_rpm_driver = {
559 	.probe = qcom_rpm_probe,
560 	.remove = qcom_rpm_remove,
561 	.driver  = {
562 		.name  = "qcom_rpm",
563 		.of_match_table = qcom_rpm_of_match,
564 	},
565 };
566 
567 static int __init qcom_rpm_init(void)
568 {
569 	return platform_driver_register(&qcom_rpm_driver);
570 }
571 arch_initcall(qcom_rpm_init);
572 
573 static void __exit qcom_rpm_exit(void)
574 {
575 	platform_driver_unregister(&qcom_rpm_driver);
576 }
577 module_exit(qcom_rpm_exit)
578 
579 MODULE_DESCRIPTION("Qualcomm Resource Power Manager driver");
580 MODULE_LICENSE("GPL v2");
581 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
582