1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2021-2022 Linaro Ltd
5 * Author: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>, based on
6 * previous work of Thara Gopinath and msm-4.9 downstream sources.
10 #include <linux/interconnect.h>
11 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_opp.h>
18 #include <linux/regmap.h>
19 #include <linux/sizes.h>
22 * The BWMON samples data throughput within 'sample_ms' time. With three
23 * configurable thresholds (Low, Medium and High) gives four windows (called
24 * zones) of current bandwidth:
26 * Zone 0: byte count < THRES_LO
27 * Zone 1: THRES_LO < byte count < THRES_MED
28 * Zone 2: THRES_MED < byte count < THRES_HIGH
29 * Zone 3: THRES_HIGH < byte count
31 * Zones 0 and 2 are not used by this driver.
34 /* Internal sampling clock frequency */
35 #define HW_TIMER_HZ 19200000
37 #define BWMON_V4_GLOBAL_IRQ_CLEAR 0x108
38 #define BWMON_V4_GLOBAL_IRQ_ENABLE 0x10c
40 * All values here and further are matching regmap fields, so without absolute
43 #define BWMON_V4_GLOBAL_IRQ_ENABLE_ENABLE BIT(0)
46 * Starting with SDM845, the BWMON4 register space has changed a bit:
47 * the global registers were jammed into the beginning of the monitor region.
48 * To keep the proper offsets, one would have to map <GLOBAL_BASE 0x200> and
49 * <GLOBAL_BASE+0x100 0x300>, which is straight up wrong.
50 * To facilitate for that, while allowing the older, arguably more proper
51 * implementations to work, offset the global registers by -0x100 to avoid
52 * having to map half of the global registers twice.
54 #define BWMON_V4_845_OFFSET 0x100
55 #define BWMON_V4_GLOBAL_IRQ_CLEAR_845 (BWMON_V4_GLOBAL_IRQ_CLEAR - BWMON_V4_845_OFFSET)
56 #define BWMON_V4_GLOBAL_IRQ_ENABLE_845 (BWMON_V4_GLOBAL_IRQ_ENABLE - BWMON_V4_845_OFFSET)
58 #define BWMON_V4_IRQ_STATUS 0x100
59 #define BWMON_V4_IRQ_CLEAR 0x108
61 #define BWMON_V4_IRQ_ENABLE 0x10c
62 #define BWMON_IRQ_ENABLE_MASK (BIT(1) | BIT(3))
63 #define BWMON_V5_IRQ_STATUS 0x000
64 #define BWMON_V5_IRQ_CLEAR 0x008
65 #define BWMON_V5_IRQ_ENABLE 0x00c
67 #define BWMON_V4_ENABLE 0x2a0
68 #define BWMON_V5_ENABLE 0x010
69 #define BWMON_ENABLE_ENABLE BIT(0)
71 #define BWMON_V4_CLEAR 0x2a4
72 #define BWMON_V5_CLEAR 0x014
73 #define BWMON_CLEAR_CLEAR BIT(0)
74 #define BWMON_CLEAR_CLEAR_ALL BIT(1)
76 #define BWMON_V4_SAMPLE_WINDOW 0x2a8
77 #define BWMON_V5_SAMPLE_WINDOW 0x020
79 #define BWMON_V4_THRESHOLD_HIGH 0x2ac
80 #define BWMON_V4_THRESHOLD_MED 0x2b0
81 #define BWMON_V4_THRESHOLD_LOW 0x2b4
82 #define BWMON_V5_THRESHOLD_HIGH 0x024
83 #define BWMON_V5_THRESHOLD_MED 0x028
84 #define BWMON_V5_THRESHOLD_LOW 0x02c
86 #define BWMON_V4_ZONE_ACTIONS 0x2b8
87 #define BWMON_V5_ZONE_ACTIONS 0x030
89 * Actions to perform on some zone 'z' when current zone hits the threshold:
90 * Increment counter of zone 'z'
92 #define BWMON_ZONE_ACTIONS_INCREMENT(z) (0x2 << ((z) * 2))
93 /* Clear counter of zone 'z' */
94 #define BWMON_ZONE_ACTIONS_CLEAR(z) (0x1 << ((z) * 2))
96 /* Zone 0 threshold hit: Clear zone count */
97 #define BWMON_ZONE_ACTIONS_ZONE0 (BWMON_ZONE_ACTIONS_CLEAR(0))
99 /* Zone 1 threshold hit: Increment zone count & clear lower zones */
100 #define BWMON_ZONE_ACTIONS_ZONE1 (BWMON_ZONE_ACTIONS_INCREMENT(1) | \
101 BWMON_ZONE_ACTIONS_CLEAR(0))
103 /* Zone 2 threshold hit: Increment zone count & clear lower zones */
104 #define BWMON_ZONE_ACTIONS_ZONE2 (BWMON_ZONE_ACTIONS_INCREMENT(2) | \
105 BWMON_ZONE_ACTIONS_CLEAR(1) | \
106 BWMON_ZONE_ACTIONS_CLEAR(0))
108 /* Zone 3 threshold hit: Increment zone count & clear lower zones */
109 #define BWMON_ZONE_ACTIONS_ZONE3 (BWMON_ZONE_ACTIONS_INCREMENT(3) | \
110 BWMON_ZONE_ACTIONS_CLEAR(2) | \
111 BWMON_ZONE_ACTIONS_CLEAR(1) | \
112 BWMON_ZONE_ACTIONS_CLEAR(0))
115 * There is no clear documentation/explanation of BWMON_V4_THRESHOLD_COUNT
116 * register. Based on observations, this is number of times one threshold has to
117 * be reached, to trigger interrupt in given zone.
119 * 0xff are maximum values meant to ignore the zones 0 and 2.
121 #define BWMON_V4_THRESHOLD_COUNT 0x2bc
122 #define BWMON_V5_THRESHOLD_COUNT 0x034
123 #define BWMON_THRESHOLD_COUNT_ZONE0_DEFAULT 0xff
124 #define BWMON_THRESHOLD_COUNT_ZONE2_DEFAULT 0xff
126 #define BWMON_V4_ZONE_MAX(zone) (0x2e0 + 4 * (zone))
127 #define BWMON_V5_ZONE_MAX(zone) (0x044 + 4 * (zone))
129 /* Quirks for specific BWMON types */
130 #define BWMON_HAS_GLOBAL_IRQ BIT(0)
131 #define BWMON_NEEDS_FORCE_CLEAR BIT(1)
134 /* Global region fields, keep them at the top */
139 /* Monitor region fields */
140 F_IRQ_STATUS
= F_NUM_GLOBAL_FIELDS
,
149 F_ZONE_ACTIONS_ZONE0
,
150 F_ZONE_ACTIONS_ZONE1
,
151 F_ZONE_ACTIONS_ZONE2
,
152 F_ZONE_ACTIONS_ZONE3
,
153 F_THRESHOLD_COUNT_ZONE0
,
154 F_THRESHOLD_COUNT_ZONE1
,
155 F_THRESHOLD_COUNT_ZONE2
,
156 F_THRESHOLD_COUNT_ZONE3
,
165 struct icc_bwmon_data
{
166 unsigned int sample_ms
;
167 unsigned int count_unit_kb
; /* kbytes */
168 unsigned int default_highbw_kbps
;
169 unsigned int default_medbw_kbps
;
170 unsigned int default_lowbw_kbps
;
171 u8 zone1_thres_count
;
172 u8 zone3_thres_count
;
175 const struct regmap_config
*regmap_cfg
;
176 const struct reg_field
*regmap_fields
;
178 const struct regmap_config
*global_regmap_cfg
;
179 const struct reg_field
*global_regmap_fields
;
184 const struct icc_bwmon_data
*data
;
187 struct regmap_field
*regs
[F_NUM_FIELDS
];
188 struct regmap_field
*global_regs
[F_NUM_GLOBAL_FIELDS
];
190 unsigned int max_bw_kbps
;
191 unsigned int min_bw_kbps
;
192 unsigned int target_kbps
;
193 unsigned int current_kbps
;
197 static const struct reg_field msm8998_bwmon_reg_fields
[] = {
198 [F_GLOBAL_IRQ_CLEAR
] = {},
199 [F_GLOBAL_IRQ_ENABLE
] = {},
200 [F_IRQ_STATUS
] = REG_FIELD(BWMON_V4_IRQ_STATUS
, 4, 7),
201 [F_IRQ_CLEAR
] = REG_FIELD(BWMON_V4_IRQ_CLEAR
, 4, 7),
202 [F_IRQ_ENABLE
] = REG_FIELD(BWMON_V4_IRQ_ENABLE
, 4, 7),
203 /* F_ENABLE covers entire register to disable other features */
204 [F_ENABLE
] = REG_FIELD(BWMON_V4_ENABLE
, 0, 31),
205 [F_CLEAR
] = REG_FIELD(BWMON_V4_CLEAR
, 0, 1),
206 [F_SAMPLE_WINDOW
] = REG_FIELD(BWMON_V4_SAMPLE_WINDOW
, 0, 23),
207 [F_THRESHOLD_HIGH
] = REG_FIELD(BWMON_V4_THRESHOLD_HIGH
, 0, 11),
208 [F_THRESHOLD_MED
] = REG_FIELD(BWMON_V4_THRESHOLD_MED
, 0, 11),
209 [F_THRESHOLD_LOW
] = REG_FIELD(BWMON_V4_THRESHOLD_LOW
, 0, 11),
210 [F_ZONE_ACTIONS_ZONE0
] = REG_FIELD(BWMON_V4_ZONE_ACTIONS
, 0, 7),
211 [F_ZONE_ACTIONS_ZONE1
] = REG_FIELD(BWMON_V4_ZONE_ACTIONS
, 8, 15),
212 [F_ZONE_ACTIONS_ZONE2
] = REG_FIELD(BWMON_V4_ZONE_ACTIONS
, 16, 23),
213 [F_ZONE_ACTIONS_ZONE3
] = REG_FIELD(BWMON_V4_ZONE_ACTIONS
, 24, 31),
214 [F_THRESHOLD_COUNT_ZONE0
] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT
, 0, 7),
215 [F_THRESHOLD_COUNT_ZONE1
] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT
, 8, 15),
216 [F_THRESHOLD_COUNT_ZONE2
] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT
, 16, 23),
217 [F_THRESHOLD_COUNT_ZONE3
] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT
, 24, 31),
218 [F_ZONE0_MAX
] = REG_FIELD(BWMON_V4_ZONE_MAX(0), 0, 11),
219 [F_ZONE1_MAX
] = REG_FIELD(BWMON_V4_ZONE_MAX(1), 0, 11),
220 [F_ZONE2_MAX
] = REG_FIELD(BWMON_V4_ZONE_MAX(2), 0, 11),
221 [F_ZONE3_MAX
] = REG_FIELD(BWMON_V4_ZONE_MAX(3), 0, 11),
224 static const struct regmap_range msm8998_bwmon_reg_noread_ranges
[] = {
225 regmap_reg_range(BWMON_V4_IRQ_CLEAR
, BWMON_V4_IRQ_CLEAR
),
226 regmap_reg_range(BWMON_V4_CLEAR
, BWMON_V4_CLEAR
),
229 static const struct regmap_access_table msm8998_bwmon_reg_read_table
= {
230 .no_ranges
= msm8998_bwmon_reg_noread_ranges
,
231 .n_no_ranges
= ARRAY_SIZE(msm8998_bwmon_reg_noread_ranges
),
234 static const struct regmap_range msm8998_bwmon_reg_volatile_ranges
[] = {
235 regmap_reg_range(BWMON_V4_IRQ_STATUS
, BWMON_V4_IRQ_STATUS
),
236 regmap_reg_range(BWMON_V4_ZONE_MAX(0), BWMON_V4_ZONE_MAX(3)),
239 static const struct regmap_access_table msm8998_bwmon_reg_volatile_table
= {
240 .yes_ranges
= msm8998_bwmon_reg_volatile_ranges
,
241 .n_yes_ranges
= ARRAY_SIZE(msm8998_bwmon_reg_volatile_ranges
),
244 static const struct reg_field msm8998_bwmon_global_reg_fields
[] = {
245 [F_GLOBAL_IRQ_CLEAR
] = REG_FIELD(BWMON_V4_GLOBAL_IRQ_CLEAR
, 0, 0),
246 [F_GLOBAL_IRQ_ENABLE
] = REG_FIELD(BWMON_V4_GLOBAL_IRQ_ENABLE
, 0, 0),
249 static const struct regmap_range msm8998_bwmon_global_reg_noread_ranges
[] = {
250 regmap_reg_range(BWMON_V4_GLOBAL_IRQ_CLEAR
, BWMON_V4_GLOBAL_IRQ_CLEAR
),
253 static const struct regmap_access_table msm8998_bwmon_global_reg_read_table
= {
254 .no_ranges
= msm8998_bwmon_global_reg_noread_ranges
,
255 .n_no_ranges
= ARRAY_SIZE(msm8998_bwmon_global_reg_noread_ranges
),
259 * Fill the cache for non-readable registers only as rest does not really
260 * matter and can be read from the device.
262 static const struct reg_default msm8998_bwmon_reg_defaults
[] = {
263 { BWMON_V4_IRQ_CLEAR
, 0x0 },
264 { BWMON_V4_CLEAR
, 0x0 },
267 static const struct reg_default msm8998_bwmon_global_reg_defaults
[] = {
268 { BWMON_V4_GLOBAL_IRQ_CLEAR
, 0x0 },
271 static const struct regmap_config msm8998_bwmon_regmap_cfg
= {
276 * No concurrent access expected - driver has one interrupt handler,
277 * regmap is not shared, no driver or user-space API.
279 .disable_locking
= true,
280 .rd_table
= &msm8998_bwmon_reg_read_table
,
281 .volatile_table
= &msm8998_bwmon_reg_volatile_table
,
282 .reg_defaults
= msm8998_bwmon_reg_defaults
,
283 .num_reg_defaults
= ARRAY_SIZE(msm8998_bwmon_reg_defaults
),
285 * Cache is necessary for using regmap fields with non-readable
288 .cache_type
= REGCACHE_RBTREE
,
291 static const struct regmap_config msm8998_bwmon_global_regmap_cfg
= {
296 * No concurrent access expected - driver has one interrupt handler,
297 * regmap is not shared, no driver or user-space API.
299 .disable_locking
= true,
300 .rd_table
= &msm8998_bwmon_global_reg_read_table
,
301 .reg_defaults
= msm8998_bwmon_global_reg_defaults
,
302 .num_reg_defaults
= ARRAY_SIZE(msm8998_bwmon_global_reg_defaults
),
304 * Cache is necessary for using regmap fields with non-readable
307 .cache_type
= REGCACHE_RBTREE
,
310 static const struct reg_field sdm845_cpu_bwmon_reg_fields
[] = {
311 [F_GLOBAL_IRQ_CLEAR
] = REG_FIELD(BWMON_V4_GLOBAL_IRQ_CLEAR_845
, 0, 0),
312 [F_GLOBAL_IRQ_ENABLE
] = REG_FIELD(BWMON_V4_GLOBAL_IRQ_ENABLE_845
, 0, 0),
313 [F_IRQ_STATUS
] = REG_FIELD(BWMON_V4_IRQ_STATUS
, 4, 7),
314 [F_IRQ_CLEAR
] = REG_FIELD(BWMON_V4_IRQ_CLEAR
, 4, 7),
315 [F_IRQ_ENABLE
] = REG_FIELD(BWMON_V4_IRQ_ENABLE
, 4, 7),
316 /* F_ENABLE covers entire register to disable other features */
317 [F_ENABLE
] = REG_FIELD(BWMON_V4_ENABLE
, 0, 31),
318 [F_CLEAR
] = REG_FIELD(BWMON_V4_CLEAR
, 0, 1),
319 [F_SAMPLE_WINDOW
] = REG_FIELD(BWMON_V4_SAMPLE_WINDOW
, 0, 23),
320 [F_THRESHOLD_HIGH
] = REG_FIELD(BWMON_V4_THRESHOLD_HIGH
, 0, 11),
321 [F_THRESHOLD_MED
] = REG_FIELD(BWMON_V4_THRESHOLD_MED
, 0, 11),
322 [F_THRESHOLD_LOW
] = REG_FIELD(BWMON_V4_THRESHOLD_LOW
, 0, 11),
323 [F_ZONE_ACTIONS_ZONE0
] = REG_FIELD(BWMON_V4_ZONE_ACTIONS
, 0, 7),
324 [F_ZONE_ACTIONS_ZONE1
] = REG_FIELD(BWMON_V4_ZONE_ACTIONS
, 8, 15),
325 [F_ZONE_ACTIONS_ZONE2
] = REG_FIELD(BWMON_V4_ZONE_ACTIONS
, 16, 23),
326 [F_ZONE_ACTIONS_ZONE3
] = REG_FIELD(BWMON_V4_ZONE_ACTIONS
, 24, 31),
327 [F_THRESHOLD_COUNT_ZONE0
] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT
, 0, 7),
328 [F_THRESHOLD_COUNT_ZONE1
] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT
, 8, 15),
329 [F_THRESHOLD_COUNT_ZONE2
] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT
, 16, 23),
330 [F_THRESHOLD_COUNT_ZONE3
] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT
, 24, 31),
331 [F_ZONE0_MAX
] = REG_FIELD(BWMON_V4_ZONE_MAX(0), 0, 11),
332 [F_ZONE1_MAX
] = REG_FIELD(BWMON_V4_ZONE_MAX(1), 0, 11),
333 [F_ZONE2_MAX
] = REG_FIELD(BWMON_V4_ZONE_MAX(2), 0, 11),
334 [F_ZONE3_MAX
] = REG_FIELD(BWMON_V4_ZONE_MAX(3), 0, 11),
337 static const struct regmap_range sdm845_cpu_bwmon_reg_noread_ranges
[] = {
338 regmap_reg_range(BWMON_V4_GLOBAL_IRQ_CLEAR_845
, BWMON_V4_GLOBAL_IRQ_CLEAR_845
),
339 regmap_reg_range(BWMON_V4_IRQ_CLEAR
, BWMON_V4_IRQ_CLEAR
),
340 regmap_reg_range(BWMON_V4_CLEAR
, BWMON_V4_CLEAR
),
343 static const struct regmap_access_table sdm845_cpu_bwmon_reg_read_table
= {
344 .no_ranges
= sdm845_cpu_bwmon_reg_noread_ranges
,
345 .n_no_ranges
= ARRAY_SIZE(sdm845_cpu_bwmon_reg_noread_ranges
),
349 * Fill the cache for non-readable registers only as rest does not really
350 * matter and can be read from the device.
352 static const struct reg_default sdm845_cpu_bwmon_reg_defaults
[] = {
353 { BWMON_V4_GLOBAL_IRQ_CLEAR_845
, 0x0 },
354 { BWMON_V4_IRQ_CLEAR
, 0x0 },
355 { BWMON_V4_CLEAR
, 0x0 },
358 static const struct regmap_config sdm845_cpu_bwmon_regmap_cfg
= {
363 * No concurrent access expected - driver has one interrupt handler,
364 * regmap is not shared, no driver or user-space API.
366 .disable_locking
= true,
367 .rd_table
= &sdm845_cpu_bwmon_reg_read_table
,
368 .volatile_table
= &msm8998_bwmon_reg_volatile_table
,
369 .reg_defaults
= sdm845_cpu_bwmon_reg_defaults
,
370 .num_reg_defaults
= ARRAY_SIZE(sdm845_cpu_bwmon_reg_defaults
),
372 * Cache is necessary for using regmap fields with non-readable
375 .cache_type
= REGCACHE_RBTREE
,
379 static const struct reg_field sdm845_llcc_bwmon_reg_fields
[] = {
380 [F_GLOBAL_IRQ_CLEAR
] = {},
381 [F_GLOBAL_IRQ_ENABLE
] = {},
382 [F_IRQ_STATUS
] = REG_FIELD(BWMON_V5_IRQ_STATUS
, 0, 3),
383 [F_IRQ_CLEAR
] = REG_FIELD(BWMON_V5_IRQ_CLEAR
, 0, 3),
384 [F_IRQ_ENABLE
] = REG_FIELD(BWMON_V5_IRQ_ENABLE
, 0, 3),
385 /* F_ENABLE covers entire register to disable other features */
386 [F_ENABLE
] = REG_FIELD(BWMON_V5_ENABLE
, 0, 31),
387 [F_CLEAR
] = REG_FIELD(BWMON_V5_CLEAR
, 0, 1),
388 [F_SAMPLE_WINDOW
] = REG_FIELD(BWMON_V5_SAMPLE_WINDOW
, 0, 19),
389 [F_THRESHOLD_HIGH
] = REG_FIELD(BWMON_V5_THRESHOLD_HIGH
, 0, 11),
390 [F_THRESHOLD_MED
] = REG_FIELD(BWMON_V5_THRESHOLD_MED
, 0, 11),
391 [F_THRESHOLD_LOW
] = REG_FIELD(BWMON_V5_THRESHOLD_LOW
, 0, 11),
392 [F_ZONE_ACTIONS_ZONE0
] = REG_FIELD(BWMON_V5_ZONE_ACTIONS
, 0, 7),
393 [F_ZONE_ACTIONS_ZONE1
] = REG_FIELD(BWMON_V5_ZONE_ACTIONS
, 8, 15),
394 [F_ZONE_ACTIONS_ZONE2
] = REG_FIELD(BWMON_V5_ZONE_ACTIONS
, 16, 23),
395 [F_ZONE_ACTIONS_ZONE3
] = REG_FIELD(BWMON_V5_ZONE_ACTIONS
, 24, 31),
396 [F_THRESHOLD_COUNT_ZONE0
] = REG_FIELD(BWMON_V5_THRESHOLD_COUNT
, 0, 7),
397 [F_THRESHOLD_COUNT_ZONE1
] = REG_FIELD(BWMON_V5_THRESHOLD_COUNT
, 8, 15),
398 [F_THRESHOLD_COUNT_ZONE2
] = REG_FIELD(BWMON_V5_THRESHOLD_COUNT
, 16, 23),
399 [F_THRESHOLD_COUNT_ZONE3
] = REG_FIELD(BWMON_V5_THRESHOLD_COUNT
, 24, 31),
400 [F_ZONE0_MAX
] = REG_FIELD(BWMON_V5_ZONE_MAX(0), 0, 11),
401 [F_ZONE1_MAX
] = REG_FIELD(BWMON_V5_ZONE_MAX(1), 0, 11),
402 [F_ZONE2_MAX
] = REG_FIELD(BWMON_V5_ZONE_MAX(2), 0, 11),
403 [F_ZONE3_MAX
] = REG_FIELD(BWMON_V5_ZONE_MAX(3), 0, 11),
406 static const struct regmap_range sdm845_llcc_bwmon_reg_noread_ranges
[] = {
407 regmap_reg_range(BWMON_V5_IRQ_CLEAR
, BWMON_V5_IRQ_CLEAR
),
408 regmap_reg_range(BWMON_V5_CLEAR
, BWMON_V5_CLEAR
),
411 static const struct regmap_access_table sdm845_llcc_bwmon_reg_read_table
= {
412 .no_ranges
= sdm845_llcc_bwmon_reg_noread_ranges
,
413 .n_no_ranges
= ARRAY_SIZE(sdm845_llcc_bwmon_reg_noread_ranges
),
416 static const struct regmap_range sdm845_llcc_bwmon_reg_volatile_ranges
[] = {
417 regmap_reg_range(BWMON_V5_IRQ_STATUS
, BWMON_V5_IRQ_STATUS
),
418 regmap_reg_range(BWMON_V5_ZONE_MAX(0), BWMON_V5_ZONE_MAX(3)),
421 static const struct regmap_access_table sdm845_llcc_bwmon_reg_volatile_table
= {
422 .yes_ranges
= sdm845_llcc_bwmon_reg_volatile_ranges
,
423 .n_yes_ranges
= ARRAY_SIZE(sdm845_llcc_bwmon_reg_volatile_ranges
),
427 * Fill the cache for non-readable registers only as rest does not really
428 * matter and can be read from the device.
430 static const struct reg_default sdm845_llcc_bwmon_reg_defaults
[] = {
431 { BWMON_V5_IRQ_CLEAR
, 0x0 },
432 { BWMON_V5_CLEAR
, 0x0 },
435 static const struct regmap_config sdm845_llcc_bwmon_regmap_cfg
= {
440 * No concurrent access expected - driver has one interrupt handler,
441 * regmap is not shared, no driver or user-space API.
443 .disable_locking
= true,
444 .rd_table
= &sdm845_llcc_bwmon_reg_read_table
,
445 .volatile_table
= &sdm845_llcc_bwmon_reg_volatile_table
,
446 .reg_defaults
= sdm845_llcc_bwmon_reg_defaults
,
447 .num_reg_defaults
= ARRAY_SIZE(sdm845_llcc_bwmon_reg_defaults
),
449 * Cache is necessary for using regmap fields with non-readable
452 .cache_type
= REGCACHE_RBTREE
,
455 static void bwmon_clear_counters(struct icc_bwmon
*bwmon
, bool clear_all
)
457 unsigned int val
= BWMON_CLEAR_CLEAR
;
460 val
|= BWMON_CLEAR_CLEAR_ALL
;
462 * Clear counters. The order and barriers are
463 * important. Quoting downstream Qualcomm msm-4.9 tree:
465 * The counter clear and IRQ clear bits are not in the same 4KB
466 * region. So, we need to make sure the counter clear is completed
467 * before we try to clear the IRQ or do any other counter operations.
469 regmap_field_force_write(bwmon
->regs
[F_CLEAR
], val
);
470 if (bwmon
->data
->quirks
& BWMON_NEEDS_FORCE_CLEAR
)
471 regmap_field_force_write(bwmon
->regs
[F_CLEAR
], 0);
474 static void bwmon_clear_irq(struct icc_bwmon
*bwmon
)
476 struct regmap_field
*global_irq_clr
;
478 if (bwmon
->data
->global_regmap_fields
)
479 global_irq_clr
= bwmon
->global_regs
[F_GLOBAL_IRQ_CLEAR
];
481 global_irq_clr
= bwmon
->regs
[F_GLOBAL_IRQ_CLEAR
];
484 * Clear zone and global interrupts. The order and barriers are
485 * important. Quoting downstream Qualcomm msm-4.9 tree:
487 * Synchronize the local interrupt clear in mon_irq_clear()
488 * with the global interrupt clear here. Otherwise, the CPU
489 * may reorder the two writes and clear the global interrupt
490 * before the local interrupt, causing the global interrupt
491 * to be retriggered by the local interrupt still being high.
493 * Similarly, because the global registers are in a different
494 * region than the local registers, we need to ensure any register
495 * writes to enable the monitor after this call are ordered with the
496 * clearing here so that local writes don't happen before the
497 * interrupt is cleared.
499 regmap_field_force_write(bwmon
->regs
[F_IRQ_CLEAR
], BWMON_IRQ_ENABLE_MASK
);
500 if (bwmon
->data
->quirks
& BWMON_NEEDS_FORCE_CLEAR
)
501 regmap_field_force_write(bwmon
->regs
[F_IRQ_CLEAR
], 0);
502 if (bwmon
->data
->quirks
& BWMON_HAS_GLOBAL_IRQ
)
503 regmap_field_force_write(global_irq_clr
,
504 BWMON_V4_GLOBAL_IRQ_ENABLE_ENABLE
);
507 static void bwmon_disable(struct icc_bwmon
*bwmon
)
509 struct regmap_field
*global_irq_en
;
511 if (bwmon
->data
->global_regmap_fields
)
512 global_irq_en
= bwmon
->global_regs
[F_GLOBAL_IRQ_ENABLE
];
514 global_irq_en
= bwmon
->regs
[F_GLOBAL_IRQ_ENABLE
];
516 /* Disable interrupts. Strict ordering, see bwmon_clear_irq(). */
517 if (bwmon
->data
->quirks
& BWMON_HAS_GLOBAL_IRQ
)
518 regmap_field_write(global_irq_en
, 0x0);
519 regmap_field_write(bwmon
->regs
[F_IRQ_ENABLE
], 0x0);
522 * Disable bwmon. Must happen before bwmon_clear_irq() to avoid spurious
525 regmap_field_write(bwmon
->regs
[F_ENABLE
], 0x0);
528 static void bwmon_enable(struct icc_bwmon
*bwmon
, unsigned int irq_enable
)
530 struct regmap_field
*global_irq_en
;
532 if (bwmon
->data
->global_regmap_fields
)
533 global_irq_en
= bwmon
->global_regs
[F_GLOBAL_IRQ_ENABLE
];
535 global_irq_en
= bwmon
->regs
[F_GLOBAL_IRQ_ENABLE
];
537 /* Enable interrupts */
538 if (bwmon
->data
->quirks
& BWMON_HAS_GLOBAL_IRQ
)
539 regmap_field_write(global_irq_en
,
540 BWMON_V4_GLOBAL_IRQ_ENABLE_ENABLE
);
542 regmap_field_write(bwmon
->regs
[F_IRQ_ENABLE
], irq_enable
);
545 regmap_field_write(bwmon
->regs
[F_ENABLE
], BWMON_ENABLE_ENABLE
);
548 static unsigned int bwmon_kbps_to_count(struct icc_bwmon
*bwmon
,
551 return kbps
/ bwmon
->data
->count_unit_kb
;
554 static void bwmon_set_threshold(struct icc_bwmon
*bwmon
,
555 struct regmap_field
*reg
, unsigned int kbps
)
559 thres
= mult_frac(bwmon_kbps_to_count(bwmon
, kbps
),
560 bwmon
->data
->sample_ms
, MSEC_PER_SEC
);
561 regmap_field_write(reg
, thres
);
564 static void bwmon_start(struct icc_bwmon
*bwmon
)
566 const struct icc_bwmon_data
*data
= bwmon
->data
;
569 bwmon_clear_counters(bwmon
, true);
571 window
= mult_frac(bwmon
->data
->sample_ms
, HW_TIMER_HZ
, MSEC_PER_SEC
);
572 /* Maximum sampling window: 0xffffff for v4 and 0xfffff for v5 */
573 regmap_field_write(bwmon
->regs
[F_SAMPLE_WINDOW
], window
);
575 bwmon_set_threshold(bwmon
, bwmon
->regs
[F_THRESHOLD_HIGH
],
576 data
->default_highbw_kbps
);
577 bwmon_set_threshold(bwmon
, bwmon
->regs
[F_THRESHOLD_MED
],
578 data
->default_medbw_kbps
);
579 bwmon_set_threshold(bwmon
, bwmon
->regs
[F_THRESHOLD_LOW
],
580 data
->default_lowbw_kbps
);
582 regmap_field_write(bwmon
->regs
[F_THRESHOLD_COUNT_ZONE0
],
583 BWMON_THRESHOLD_COUNT_ZONE0_DEFAULT
);
584 regmap_field_write(bwmon
->regs
[F_THRESHOLD_COUNT_ZONE1
],
585 data
->zone1_thres_count
);
586 regmap_field_write(bwmon
->regs
[F_THRESHOLD_COUNT_ZONE2
],
587 BWMON_THRESHOLD_COUNT_ZONE2_DEFAULT
);
588 regmap_field_write(bwmon
->regs
[F_THRESHOLD_COUNT_ZONE3
],
589 data
->zone3_thres_count
);
591 regmap_field_write(bwmon
->regs
[F_ZONE_ACTIONS_ZONE0
],
592 BWMON_ZONE_ACTIONS_ZONE0
);
593 regmap_field_write(bwmon
->regs
[F_ZONE_ACTIONS_ZONE1
],
594 BWMON_ZONE_ACTIONS_ZONE1
);
595 regmap_field_write(bwmon
->regs
[F_ZONE_ACTIONS_ZONE2
],
596 BWMON_ZONE_ACTIONS_ZONE2
);
597 regmap_field_write(bwmon
->regs
[F_ZONE_ACTIONS_ZONE3
],
598 BWMON_ZONE_ACTIONS_ZONE3
);
600 bwmon_clear_irq(bwmon
);
601 bwmon_enable(bwmon
, BWMON_IRQ_ENABLE_MASK
);
604 static irqreturn_t
bwmon_intr(int irq
, void *dev_id
)
606 struct icc_bwmon
*bwmon
= dev_id
;
607 unsigned int status
, max
;
610 if (regmap_field_read(bwmon
->regs
[F_IRQ_STATUS
], &status
))
613 status
&= BWMON_IRQ_ENABLE_MASK
;
616 * Only zone 1 and zone 3 interrupts are enabled but zone 2
617 * threshold could be hit and trigger interrupt even if not
619 * Such spurious interrupt might come with valuable max count or
620 * not, so solution would be to always check all
621 * BWMON_ZONE_MAX() registers to find the highest value.
622 * Such case is currently ignored.
627 bwmon_disable(bwmon
);
629 zone
= get_bitmask_order(status
) - 1;
631 * Zone max bytes count register returns count units within sampling
632 * window. Downstream kernel for BWMONv4 (called BWMON type 2 in
633 * downstream) always increments the max bytes count by one.
635 if (regmap_field_read(bwmon
->regs
[F_ZONE0_MAX
+ zone
], &max
))
639 max
*= bwmon
->data
->count_unit_kb
;
640 bwmon
->target_kbps
= mult_frac(max
, MSEC_PER_SEC
, bwmon
->data
->sample_ms
);
642 return IRQ_WAKE_THREAD
;
645 static irqreturn_t
bwmon_intr_thread(int irq
, void *dev_id
)
647 struct icc_bwmon
*bwmon
= dev_id
;
648 unsigned int irq_enable
= 0;
649 struct dev_pm_opp
*opp
, *target_opp
;
650 unsigned int bw_kbps
, up_kbps
, down_kbps
;
652 bw_kbps
= bwmon
->target_kbps
;
654 target_opp
= dev_pm_opp_find_bw_ceil(bwmon
->dev
, &bw_kbps
, 0);
655 if (IS_ERR(target_opp
) && PTR_ERR(target_opp
) == -ERANGE
)
656 target_opp
= dev_pm_opp_find_bw_floor(bwmon
->dev
, &bw_kbps
, 0);
658 bwmon
->target_kbps
= bw_kbps
;
661 opp
= dev_pm_opp_find_bw_floor(bwmon
->dev
, &bw_kbps
, 0);
662 if (IS_ERR(opp
) && PTR_ERR(opp
) == -ERANGE
)
663 down_kbps
= bwmon
->target_kbps
;
667 up_kbps
= bwmon
->target_kbps
+ 1;
669 if (bwmon
->target_kbps
>= bwmon
->max_bw_kbps
)
671 else if (bwmon
->target_kbps
<= bwmon
->min_bw_kbps
)
674 irq_enable
= BWMON_IRQ_ENABLE_MASK
;
676 bwmon_set_threshold(bwmon
, bwmon
->regs
[F_THRESHOLD_HIGH
],
678 bwmon_set_threshold(bwmon
, bwmon
->regs
[F_THRESHOLD_MED
],
680 bwmon_clear_counters(bwmon
, false);
681 bwmon_clear_irq(bwmon
);
682 bwmon_enable(bwmon
, irq_enable
);
684 if (bwmon
->target_kbps
== bwmon
->current_kbps
)
687 dev_pm_opp_set_opp(bwmon
->dev
, target_opp
);
688 bwmon
->current_kbps
= bwmon
->target_kbps
;
691 dev_pm_opp_put(target_opp
);
698 static int bwmon_init_regmap(struct platform_device
*pdev
,
699 struct icc_bwmon
*bwmon
)
701 struct device
*dev
= &pdev
->dev
;
706 /* Map the monitor base */
707 base
= devm_platform_ioremap_resource(pdev
, 0);
709 return dev_err_probe(dev
, PTR_ERR(base
),
710 "failed to map bwmon registers\n");
712 map
= devm_regmap_init_mmio(dev
, base
, bwmon
->data
->regmap_cfg
);
714 return dev_err_probe(dev
, PTR_ERR(map
),
715 "failed to initialize regmap\n");
717 BUILD_BUG_ON(ARRAY_SIZE(msm8998_bwmon_global_reg_fields
) != F_NUM_GLOBAL_FIELDS
);
718 BUILD_BUG_ON(ARRAY_SIZE(msm8998_bwmon_reg_fields
) != F_NUM_FIELDS
);
719 BUILD_BUG_ON(ARRAY_SIZE(sdm845_cpu_bwmon_reg_fields
) != F_NUM_FIELDS
);
720 BUILD_BUG_ON(ARRAY_SIZE(sdm845_llcc_bwmon_reg_fields
) != F_NUM_FIELDS
);
722 ret
= devm_regmap_field_bulk_alloc(dev
, map
, bwmon
->regs
,
723 bwmon
->data
->regmap_fields
,
728 if (bwmon
->data
->global_regmap_cfg
) {
729 /* Map the global base, if separate */
730 base
= devm_platform_ioremap_resource(pdev
, 1);
732 return dev_err_probe(dev
, PTR_ERR(base
),
733 "failed to map bwmon global registers\n");
735 map
= devm_regmap_init_mmio(dev
, base
, bwmon
->data
->global_regmap_cfg
);
737 return dev_err_probe(dev
, PTR_ERR(map
),
738 "failed to initialize global regmap\n");
740 ret
= devm_regmap_field_bulk_alloc(dev
, map
, bwmon
->global_regs
,
741 bwmon
->data
->global_regmap_fields
,
742 F_NUM_GLOBAL_FIELDS
);
748 static int bwmon_probe(struct platform_device
*pdev
)
750 struct device
*dev
= &pdev
->dev
;
751 struct dev_pm_opp
*opp
;
752 struct icc_bwmon
*bwmon
;
755 bwmon
= devm_kzalloc(dev
, sizeof(*bwmon
), GFP_KERNEL
);
759 bwmon
->data
= of_device_get_match_data(dev
);
761 ret
= bwmon_init_regmap(pdev
, bwmon
);
765 bwmon
->irq
= platform_get_irq(pdev
, 0);
769 ret
= devm_pm_opp_of_add_table(dev
);
771 return dev_err_probe(dev
, ret
, "failed to add OPP table\n");
773 bwmon
->max_bw_kbps
= UINT_MAX
;
774 opp
= dev_pm_opp_find_bw_floor(dev
, &bwmon
->max_bw_kbps
, 0);
776 return dev_err_probe(dev
, PTR_ERR(opp
), "failed to find max peak bandwidth\n");
778 bwmon
->min_bw_kbps
= 0;
779 opp
= dev_pm_opp_find_bw_ceil(dev
, &bwmon
->min_bw_kbps
, 0);
781 return dev_err_probe(dev
, PTR_ERR(opp
), "failed to find min peak bandwidth\n");
785 bwmon_disable(bwmon
);
786 ret
= devm_request_threaded_irq(dev
, bwmon
->irq
, bwmon_intr
,
788 IRQF_ONESHOT
, dev_name(dev
), bwmon
);
790 return dev_err_probe(dev
, ret
, "failed to request IRQ\n");
792 platform_set_drvdata(pdev
, bwmon
);
798 static int bwmon_remove(struct platform_device
*pdev
)
800 struct icc_bwmon
*bwmon
= platform_get_drvdata(pdev
);
802 bwmon_disable(bwmon
);
807 static const struct icc_bwmon_data msm8998_bwmon_data
= {
809 .count_unit_kb
= 1024,
810 .default_highbw_kbps
= 4800 * 1024, /* 4.8 GBps */
811 .default_medbw_kbps
= 512 * 1024, /* 512 MBps */
812 .default_lowbw_kbps
= 0,
813 .zone1_thres_count
= 16,
814 .zone3_thres_count
= 1,
815 .quirks
= BWMON_HAS_GLOBAL_IRQ
,
816 .regmap_fields
= msm8998_bwmon_reg_fields
,
817 .regmap_cfg
= &msm8998_bwmon_regmap_cfg
,
818 .global_regmap_fields
= msm8998_bwmon_global_reg_fields
,
819 .global_regmap_cfg
= &msm8998_bwmon_global_regmap_cfg
,
822 static const struct icc_bwmon_data sdm845_cpu_bwmon_data
= {
825 .default_highbw_kbps
= 4800 * 1024, /* 4.8 GBps */
826 .default_medbw_kbps
= 512 * 1024, /* 512 MBps */
827 .default_lowbw_kbps
= 0,
828 .zone1_thres_count
= 16,
829 .zone3_thres_count
= 1,
830 .quirks
= BWMON_HAS_GLOBAL_IRQ
,
831 .regmap_fields
= sdm845_cpu_bwmon_reg_fields
,
832 .regmap_cfg
= &sdm845_cpu_bwmon_regmap_cfg
,
835 static const struct icc_bwmon_data sdm845_llcc_bwmon_data
= {
837 .count_unit_kb
= 1024,
838 .default_highbw_kbps
= 800 * 1024, /* 800 MBps */
839 .default_medbw_kbps
= 256 * 1024, /* 256 MBps */
840 .default_lowbw_kbps
= 0,
841 .zone1_thres_count
= 16,
842 .zone3_thres_count
= 1,
843 .regmap_fields
= sdm845_llcc_bwmon_reg_fields
,
844 .regmap_cfg
= &sdm845_llcc_bwmon_regmap_cfg
,
847 static const struct icc_bwmon_data sc7280_llcc_bwmon_data
= {
850 .default_highbw_kbps
= 800 * 1024, /* 800 MBps */
851 .default_medbw_kbps
= 256 * 1024, /* 256 MBps */
852 .default_lowbw_kbps
= 0,
853 .zone1_thres_count
= 16,
854 .zone3_thres_count
= 1,
855 .quirks
= BWMON_NEEDS_FORCE_CLEAR
,
856 .regmap_fields
= sdm845_llcc_bwmon_reg_fields
,
857 .regmap_cfg
= &sdm845_llcc_bwmon_regmap_cfg
,
860 static const struct of_device_id bwmon_of_match
[] = {
861 /* BWMONv4, separate monitor and global register spaces */
862 { .compatible
= "qcom,msm8998-bwmon", .data
= &msm8998_bwmon_data
},
863 /* BWMONv4, unified register space */
864 { .compatible
= "qcom,sdm845-bwmon", .data
= &sdm845_cpu_bwmon_data
},
866 { .compatible
= "qcom,sdm845-llcc-bwmon", .data
= &sdm845_llcc_bwmon_data
},
867 { .compatible
= "qcom,sc7280-llcc-bwmon", .data
= &sc7280_llcc_bwmon_data
},
869 /* Compatibles kept for legacy reasons */
870 { .compatible
= "qcom,sc7280-cpu-bwmon", .data
= &sdm845_cpu_bwmon_data
},
871 { .compatible
= "qcom,sc8280xp-cpu-bwmon", .data
= &sdm845_cpu_bwmon_data
},
872 { .compatible
= "qcom,sm8550-cpu-bwmon", .data
= &sdm845_cpu_bwmon_data
},
875 MODULE_DEVICE_TABLE(of
, bwmon_of_match
);
877 static struct platform_driver bwmon_driver
= {
878 .probe
= bwmon_probe
,
879 .remove
= bwmon_remove
,
881 .name
= "qcom-bwmon",
882 .of_match_table
= bwmon_of_match
,
885 module_platform_driver(bwmon_driver
);
887 MODULE_AUTHOR("Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>");
888 MODULE_DESCRIPTION("QCOM BWMON driver");
889 MODULE_LICENSE("GPL");