|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only OR MIT |
| 2 | +/* |
| 3 | + * Apple SoC PMGR device power state driver |
| 4 | + * |
| 5 | + * Copyright The Asahi Linux Contributors |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/bitops.h> |
| 9 | +#include <linux/bitfield.h> |
| 10 | +#include <linux/err.h> |
| 11 | +#include <linux/io.h> |
| 12 | +#include <linux/of.h> |
| 13 | +#include <linux/platform_device.h> |
| 14 | +#include <linux/module.h> |
| 15 | + |
| 16 | +#define APPLE_CLKGEN_PSTATE 0 |
| 17 | +#define APPLE_CLKGEN_PSTATE_DESIRED GENMASK(3, 0) |
| 18 | + |
| 19 | +#define SYS_DEV_PSTATE_SUSPEND 1 |
| 20 | + |
| 21 | +enum sys_device { |
| 22 | + DEV_FABRIC, |
| 23 | + DEV_DCS, |
| 24 | + DEV_MAX, |
| 25 | +}; |
| 26 | + |
| 27 | +struct apple_pmgr_sys_device { |
| 28 | + void __iomem *base; |
| 29 | + u32 active_state; |
| 30 | + u32 suspend_state; |
| 31 | +}; |
| 32 | + |
| 33 | +struct apple_pmgr_misc { |
| 34 | + struct device *dev; |
| 35 | + struct apple_pmgr_sys_device devices[DEV_MAX]; |
| 36 | +}; |
| 37 | + |
| 38 | +static void apple_pmgr_sys_dev_set_pstate(struct apple_pmgr_misc *misc, |
| 39 | + enum sys_device dev, bool active) |
| 40 | +{ |
| 41 | + u32 pstate; |
| 42 | + u32 val; |
| 43 | + |
| 44 | + if (!misc->devices[dev].base) |
| 45 | + return; |
| 46 | + |
| 47 | + if (active) |
| 48 | + pstate = misc->devices[dev].active_state; |
| 49 | + else |
| 50 | + pstate = misc->devices[dev].suspend_state; |
| 51 | + |
| 52 | + printk("set %d ps to pstate %d\n", dev, pstate); |
| 53 | + |
| 54 | + val = readl_relaxed(misc->devices[dev].base + APPLE_CLKGEN_PSTATE); |
| 55 | + val &= ~APPLE_CLKGEN_PSTATE_DESIRED; |
| 56 | + val |= FIELD_PREP(APPLE_CLKGEN_PSTATE_DESIRED, pstate); |
| 57 | + writel_relaxed(val, misc->devices[dev].base); |
| 58 | +} |
| 59 | + |
| 60 | +static int __maybe_unused apple_pmgr_misc_suspend_noirq(struct device *dev) |
| 61 | +{ |
| 62 | + struct apple_pmgr_misc *misc = dev_get_drvdata(dev); |
| 63 | + int i; |
| 64 | + |
| 65 | + for (i = 0; i < DEV_MAX; i++) |
| 66 | + apple_pmgr_sys_dev_set_pstate(misc, i, false); |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +static int __maybe_unused apple_pmgr_misc_resume_noirq(struct device *dev) |
| 72 | +{ |
| 73 | + struct apple_pmgr_misc *misc = dev_get_drvdata(dev); |
| 74 | + int i; |
| 75 | + |
| 76 | + for (i = 0; i < DEV_MAX; i++) |
| 77 | + apple_pmgr_sys_dev_set_pstate(misc, i, true); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +static bool apple_pmgr_init_device(struct apple_pmgr_misc *misc, |
| 83 | + enum sys_device dev, const char *device_name) |
| 84 | +{ |
| 85 | + void __iomem *base; |
| 86 | + char name[32]; |
| 87 | + u32 val; |
| 88 | + |
| 89 | + snprintf(name, sizeof(name), "%s-ps", device_name); |
| 90 | + |
| 91 | + base = devm_platform_ioremap_resource_byname( |
| 92 | + to_platform_device(misc->dev), name); |
| 93 | + if (!base) |
| 94 | + return false; |
| 95 | + |
| 96 | + val = readl_relaxed(base + APPLE_CLKGEN_PSTATE); |
| 97 | + |
| 98 | + misc->devices[dev].base = base; |
| 99 | + misc->devices[dev].active_state = |
| 100 | + FIELD_GET(APPLE_CLKGEN_PSTATE_DESIRED, val); |
| 101 | + misc->devices[dev].suspend_state = SYS_DEV_PSTATE_SUSPEND; |
| 102 | + |
| 103 | + snprintf(name, sizeof(name), "apple,%s-min-ps", device_name); |
| 104 | + of_property_read_u32(misc->dev->of_node, name, |
| 105 | + &misc->devices[dev].suspend_state); |
| 106 | + |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +static int apple_pmgr_misc_probe(struct platform_device *pdev) |
| 111 | +{ |
| 112 | + struct device *dev = &pdev->dev; |
| 113 | + struct apple_pmgr_misc *misc; |
| 114 | + int ret = -ENODEV; |
| 115 | + |
| 116 | + misc = devm_kzalloc(dev, sizeof(*misc), GFP_KERNEL); |
| 117 | + if (!misc) |
| 118 | + return -ENOMEM; |
| 119 | + |
| 120 | + misc->dev = dev; |
| 121 | + |
| 122 | + if (apple_pmgr_init_device(misc, DEV_FABRIC, "fabric")) |
| 123 | + ret = 0; |
| 124 | + |
| 125 | + if (apple_pmgr_init_device(misc, DEV_DCS, "dcs")) |
| 126 | + ret = 0; |
| 127 | + |
| 128 | + platform_set_drvdata(pdev, misc); |
| 129 | + |
| 130 | + return ret; |
| 131 | +} |
| 132 | + |
| 133 | +static const struct of_device_id apple_pmgr_misc_of_match[] = { |
| 134 | + { .compatible = "apple,t6000-pmgr-misc" }, |
| 135 | + {} |
| 136 | +}; |
| 137 | + |
| 138 | +MODULE_DEVICE_TABLE(of, apple_pmgr_misc_of_match); |
| 139 | + |
| 140 | +static const struct dev_pm_ops apple_pmgr_misc_pm_ops = { |
| 141 | + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(apple_pmgr_misc_suspend_noirq, |
| 142 | + apple_pmgr_misc_resume_noirq) |
| 143 | +}; |
| 144 | + |
| 145 | +static struct platform_driver apple_pmgr_misc_driver = { |
| 146 | + .probe = apple_pmgr_misc_probe, |
| 147 | + .driver = { |
| 148 | + .name = "apple-pmgr-misc", |
| 149 | + .of_match_table = apple_pmgr_misc_of_match, |
| 150 | + .pm = pm_ptr(&apple_pmgr_misc_pm_ops), |
| 151 | + }, |
| 152 | +}; |
| 153 | + |
| 154 | +MODULE_AUTHOR("Hector Martin <marcan@marcan.st>"); |
| 155 | +MODULE_DESCRIPTION("PMGR misc driver for Apple SoCs"); |
| 156 | +MODULE_LICENSE("GPL v2"); |
| 157 | + |
| 158 | +module_platform_driver(apple_pmgr_misc_driver); |
0 commit comments