^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) ST-Ericsson SA 2010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * UX500 common part of Power domain regulators
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "dbx500-prcmu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * power state reference count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int power_state_active_cnt; /* will initialize to zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static DEFINE_SPINLOCK(power_state_active_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void power_state_active_enable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) spin_lock_irqsave(&power_state_active_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) power_state_active_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) spin_unlock_irqrestore(&power_state_active_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int power_state_active_disable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) spin_lock_irqsave(&power_state_active_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (power_state_active_cnt <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pr_err("power state: unbalanced enable/disable calls\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) power_state_active_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) spin_unlock_irqrestore(&power_state_active_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #ifdef CONFIG_REGULATOR_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int power_state_active_get(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) spin_lock_irqsave(&power_state_active_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) cnt = power_state_active_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) spin_unlock_irqrestore(&power_state_active_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static struct ux500_regulator_debug {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct dbx500_regulator_info *regulator_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int num_regulators;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u8 *state_before_suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u8 *state_after_suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) } rdebug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int ux500_regulator_power_state_cnt_show(struct seq_file *s, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* print power state count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) seq_printf(s, "ux500-regulator power state count: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) power_state_active_get());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) DEFINE_SHOW_ATTRIBUTE(ux500_regulator_power_state_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int ux500_regulator_status_show(struct seq_file *s, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* print dump header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) seq_puts(s, "ux500-regulator status:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) seq_printf(s, "%31s : %8s : %8s\n", "current", "before", "after");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) for (i = 0; i < rdebug.num_regulators; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct dbx500_regulator_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Access per-regulator data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) info = &rdebug.regulator_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* print status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) seq_printf(s, "%20s : %8s : %8s : %8s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) info->desc.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) info->is_enabled ? "enabled" : "disabled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rdebug.state_before_suspend[i] ? "enabled" : "disabled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) rdebug.state_after_suspend[i] ? "enabled" : "disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) DEFINE_SHOW_ATTRIBUTE(ux500_regulator_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ux500_regulator_debug_init(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct dbx500_regulator_info *regulator_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int num_regulators)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* create directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* create "status" file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) debugfs_create_file("status", S_IRUGO, rdebug.dir, &pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) &ux500_regulator_status_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* create "power-state-count" file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) debugfs_create_file("power-state-count", S_IRUGO, rdebug.dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) &pdev->dev, &ux500_regulator_power_state_cnt_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) rdebug.regulator_array = regulator_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) rdebug.num_regulators = num_regulators;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (!rdebug.state_before_suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) goto exit_destroy_power_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (!rdebug.state_after_suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto exit_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) exit_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) kfree(rdebug.state_before_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) exit_destroy_power_state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) debugfs_remove_recursive(rdebug.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int ux500_regulator_debug_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) debugfs_remove_recursive(rdebug.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) kfree(rdebug.state_after_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) kfree(rdebug.state_before_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #endif