^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pm_clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/clkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of_clk.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/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pm_domain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #ifdef CONFIG_PM_CLK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) enum pce_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) PCE_STATUS_NONE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) PCE_STATUS_ACQUIRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) PCE_STATUS_ENABLED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) PCE_STATUS_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct pm_clock_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) char *con_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) enum pce_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * pm_clk_enable - Enable a clock, reporting any errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @dev: The device for the given clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @ce: PM clock entry corresponding to the clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static inline void __pm_clk_enable(struct device *dev, struct pm_clock_entry *ce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (ce->status < PCE_STATUS_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ret = clk_enable(ce->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ce->status = PCE_STATUS_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) dev_err(dev, "%s: failed to enable clk %p, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) __func__, ce->clk, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * pm_clk_acquire - Acquire a device clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @dev: Device whose clock is to be acquired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @ce: PM clock entry corresponding to the clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!ce->clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ce->clk = clk_get(dev, ce->con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (IS_ERR(ce->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ce->status = PCE_STATUS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (clk_prepare(ce->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ce->status = PCE_STATUS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dev_err(dev, "clk_prepare() failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ce->status = PCE_STATUS_ACQUIRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dev_dbg(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) "Clock %pC con_id %s managed by runtime PM.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ce->clk, ce->con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int __pm_clk_add(struct device *dev, const char *con_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct clk *clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct pm_subsys_data *psd = dev_to_psd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct pm_clock_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!psd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ce = kzalloc(sizeof(*ce), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!ce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (con_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) ce->con_id = kstrdup(con_id, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!ce->con_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) kfree(ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (IS_ERR(clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) kfree(ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ce->clk = clk;
^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) pm_clk_acquire(dev, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) spin_lock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) list_add_tail(&ce->node, &psd->clock_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) spin_unlock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * pm_clk_add - Start using a device clock for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @dev: Device whose clock is going to be used for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @con_id: Connection ID of the clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Add the clock represented by @con_id to the list of clocks used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * the power management of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int pm_clk_add(struct device *dev, const char *con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return __pm_clk_add(dev, con_id, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) EXPORT_SYMBOL_GPL(pm_clk_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * pm_clk_add_clk - Start using a device clock for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * @dev: Device whose clock is going to be used for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * @clk: Clock pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * Add the clock to the list of clocks used for the power management of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * The power-management code will take control of the clock reference, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * callers should not call clk_put() on @clk after this function sucessfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int pm_clk_add_clk(struct device *dev, struct clk *clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return __pm_clk_add(dev, NULL, clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) EXPORT_SYMBOL_GPL(pm_clk_add_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^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) * of_pm_clk_add_clk - Start using a device clock for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * @dev: Device whose clock is going to be used for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * @name: Name of clock that is going to be used for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Add the clock described in the 'clocks' device-tree node that matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * with the 'name' provided, to the list of clocks used for the power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * management of @dev. On success, returns 0. Returns a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * code if the clock is not found or cannot be added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int of_pm_clk_add_clk(struct device *dev, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!dev || !dev->of_node || !name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) clk = of_clk_get_by_name(dev->of_node, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (IS_ERR(clk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return PTR_ERR(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ret = pm_clk_add_clk(dev, clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) clk_put(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) EXPORT_SYMBOL_GPL(of_pm_clk_add_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * of_pm_clk_add_clks - Start using device clock(s) for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @dev: Device whose clock(s) is going to be used for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * Add a series of clocks described in the 'clocks' device-tree node for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * a device to the list of clocks used for the power management of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * On success, returns the number of clocks added. Returns a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * error code if there are no clocks in the device node for the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * or if adding a clock fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int of_pm_clk_add_clks(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct clk **clks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!dev || !dev->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) count = of_clk_get_parent_count(dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (count <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) clks = kcalloc(count, sizeof(*clks), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (!clks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) clks[i] = of_clk_get(dev->of_node, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (IS_ERR(clks[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ret = PTR_ERR(clks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ret = pm_clk_add_clk(dev, clks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) clk_put(clks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) kfree(clks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) while (i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pm_clk_remove_clk(dev, clks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) kfree(clks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) EXPORT_SYMBOL_GPL(of_pm_clk_add_clks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * __pm_clk_remove - Destroy PM clock entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * @ce: PM clock entry to destroy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static void __pm_clk_remove(struct pm_clock_entry *ce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!ce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (ce->status < PCE_STATUS_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (ce->status == PCE_STATUS_ENABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) clk_disable(ce->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (ce->status >= PCE_STATUS_ACQUIRED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) clk_unprepare(ce->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) clk_put(ce->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) kfree(ce->con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) kfree(ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * pm_clk_remove - Stop using a device clock for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * @dev: Device whose clock should not be used for PM any more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * @con_id: Connection ID of the clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * Remove the clock represented by @con_id from the list of clocks used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * the power management of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) void pm_clk_remove(struct device *dev, const char *con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct pm_subsys_data *psd = dev_to_psd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct pm_clock_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!psd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) spin_lock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) list_for_each_entry(ce, &psd->clock_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (!con_id && !ce->con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) goto remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) else if (!con_id || !ce->con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) else if (!strcmp(con_id, ce->con_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) goto remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) spin_unlock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) list_del(&ce->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) spin_unlock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) __pm_clk_remove(ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) EXPORT_SYMBOL_GPL(pm_clk_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * pm_clk_remove_clk - Stop using a device clock for power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * @dev: Device whose clock should not be used for PM any more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * @clk: Clock pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * Remove the clock pointed to by @clk from the list of clocks used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * the power management of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) void pm_clk_remove_clk(struct device *dev, struct clk *clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct pm_subsys_data *psd = dev_to_psd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct pm_clock_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (!psd || !clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) spin_lock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) list_for_each_entry(ce, &psd->clock_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (clk == ce->clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) goto remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) spin_unlock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) list_del(&ce->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) spin_unlock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) __pm_clk_remove(ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) EXPORT_SYMBOL_GPL(pm_clk_remove_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * pm_clk_init - Initialize a device's list of power management clocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * @dev: Device to initialize the list of PM clocks for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * Initialize the lock and clock_list members of the device's pm_subsys_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) void pm_clk_init(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct pm_subsys_data *psd = dev_to_psd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (psd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) INIT_LIST_HEAD(&psd->clock_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) EXPORT_SYMBOL_GPL(pm_clk_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * pm_clk_create - Create and initialize a device's list of PM clocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * @dev: Device to create and initialize the list of PM clocks for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * members and make the @dev's power.subsys_data field point to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) int pm_clk_create(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return dev_pm_get_subsys_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) EXPORT_SYMBOL_GPL(pm_clk_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * pm_clk_destroy - Destroy a device's list of power management clocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * @dev: Device to destroy the list of PM clocks for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * Clear the @dev's power.subsys_data field, remove the list of clock entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * from the struct pm_subsys_data object pointed to by it before and free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * that object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) void pm_clk_destroy(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct pm_subsys_data *psd = dev_to_psd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct pm_clock_entry *ce, *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!psd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) INIT_LIST_HEAD(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) spin_lock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) list_move(&ce->node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) spin_unlock_irq(&psd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) dev_pm_put_subsys_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) list_for_each_entry_safe_reverse(ce, c, &list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) list_del(&ce->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) __pm_clk_remove(ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) EXPORT_SYMBOL_GPL(pm_clk_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * pm_clk_suspend - Disable clocks in a device's PM clock list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * @dev: Device to disable the clocks for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int pm_clk_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct pm_subsys_data *psd = dev_to_psd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct pm_clock_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) dev_dbg(dev, "%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (!psd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) spin_lock_irqsave(&psd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) list_for_each_entry_reverse(ce, &psd->clock_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (ce->status < PCE_STATUS_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (ce->status == PCE_STATUS_ENABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) clk_disable(ce->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ce->status = PCE_STATUS_ACQUIRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) spin_unlock_irqrestore(&psd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) EXPORT_SYMBOL_GPL(pm_clk_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * pm_clk_resume - Enable clocks in a device's PM clock list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @dev: Device to enable the clocks for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) int pm_clk_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct pm_subsys_data *psd = dev_to_psd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct pm_clock_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) dev_dbg(dev, "%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (!psd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) spin_lock_irqsave(&psd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) list_for_each_entry(ce, &psd->clock_list, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) __pm_clk_enable(dev, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) spin_unlock_irqrestore(&psd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) EXPORT_SYMBOL_GPL(pm_clk_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * pm_clk_notify - Notify routine for device addition and removal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * @nb: Notifier block object this function is a member of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * @action: Operation being carried out by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * @data: Device the routine is being run for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * For this function to work, @nb must be a member of an object of type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * struct pm_clk_notifier_block containing all of the requisite data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * Specifically, the pm_domain member of that object is copied to the device's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * pm_domain field and its con_ids member is used to populate the device's list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * of PM clocks, depending on @action.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * If the device's pm_domain field is already populated with a value different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * from the one stored in the struct pm_clk_notifier_block object, the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * does nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static int pm_clk_notify(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) struct pm_clk_notifier_block *clknb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) struct device *dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) char **con_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) dev_dbg(dev, "%s() %ld\n", __func__, action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) clknb = container_of(nb, struct pm_clk_notifier_block, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) case BUS_NOTIFY_ADD_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (dev->pm_domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) error = pm_clk_create(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) dev_pm_domain_set(dev, clknb->pm_domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (clknb->con_ids[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) for (con_id = clknb->con_ids; *con_id; con_id++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) pm_clk_add(dev, *con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) pm_clk_add(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) case BUS_NOTIFY_DEL_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (dev->pm_domain != clknb->pm_domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) dev_pm_domain_set(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) pm_clk_destroy(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) int pm_clk_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) dev_dbg(dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ret = pm_generic_runtime_suspend(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) dev_err(dev, "failed to suspend device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) ret = pm_clk_suspend(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) dev_err(dev, "failed to suspend clock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) pm_generic_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) EXPORT_SYMBOL_GPL(pm_clk_runtime_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) int pm_clk_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) dev_dbg(dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) ret = pm_clk_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) dev_err(dev, "failed to resume clock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return pm_generic_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) EXPORT_SYMBOL_GPL(pm_clk_runtime_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) #else /* !CONFIG_PM_CLK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * enable_clock - Enable a device clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * @dev: Device whose clock is to be enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * @con_id: Connection ID of the clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static void enable_clock(struct device *dev, const char *con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) clk = clk_get(dev, con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (!IS_ERR(clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) clk_prepare_enable(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) clk_put(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) dev_info(dev, "Runtime PM disabled, clock forced on.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * disable_clock - Disable a device clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * @dev: Device whose clock is to be disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * @con_id: Connection ID of the clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static void disable_clock(struct device *dev, const char *con_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) clk = clk_get(dev, con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (!IS_ERR(clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) clk_disable_unprepare(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) clk_put(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) dev_info(dev, "Runtime PM disabled, clock forced off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * pm_clk_notify - Notify routine for device addition and removal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * @nb: Notifier block object this function is a member of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * @action: Operation being carried out by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * @data: Device the routine is being run for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * For this function to work, @nb must be a member of an object of type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * struct pm_clk_notifier_block containing all of the requisite data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * Specifically, the con_ids member of that object is used to enable or disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * the device's clocks, depending on @action.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static int pm_clk_notify(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct pm_clk_notifier_block *clknb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) struct device *dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) char **con_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) dev_dbg(dev, "%s() %ld\n", __func__, action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) clknb = container_of(nb, struct pm_clk_notifier_block, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) case BUS_NOTIFY_BIND_DRIVER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (clknb->con_ids[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) for (con_id = clknb->con_ids; *con_id; con_id++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) enable_clock(dev, *con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) enable_clock(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) case BUS_NOTIFY_DRIVER_NOT_BOUND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) case BUS_NOTIFY_UNBOUND_DRIVER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (clknb->con_ids[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) for (con_id = clknb->con_ids; *con_id; con_id++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) disable_clock(dev, *con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) disable_clock(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) #endif /* !CONFIG_PM_CLK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * pm_clk_add_notifier - Add bus type notifier for power management clocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * @bus: Bus type to add the notifier to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * @clknb: Notifier to be added to the given bus type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * The nb member of @clknb is not expected to be initialized and its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * notifier_call member will be replaced with pm_clk_notify(). However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * the remaining members of @clknb should be populated prior to calling this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) void pm_clk_add_notifier(struct bus_type *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) struct pm_clk_notifier_block *clknb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (!bus || !clknb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) clknb->nb.notifier_call = pm_clk_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) bus_register_notifier(bus, &clknb->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) EXPORT_SYMBOL_GPL(pm_clk_add_notifier);