Mark Kettenis | 2fc9405 | 2022-01-10 20:58:44 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org> |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <asm/io.h> |
| 8 | #include <dm.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/bitfield.h> |
| 11 | #include <power-domain-uclass.h> |
| 12 | #include <regmap.h> |
| 13 | #include <syscon.h> |
| 14 | |
| 15 | #define APPLE_PMGR_PS_TARGET GENMASK(3, 0) |
| 16 | #define APPLE_PMGR_PS_ACTUAL GENMASK(7, 4) |
| 17 | |
| 18 | #define APPLE_PMGR_PS_ACTIVE 0xf |
| 19 | #define APPLE_PMGR_PS_PWRGATE 0x0 |
| 20 | |
| 21 | #define APPLE_PMGR_PS_SET_TIMEOUT_US 100 |
| 22 | |
| 23 | struct apple_pmgr_priv { |
| 24 | struct regmap *regmap; |
| 25 | u32 offset; /* offset within regmap for this domain */ |
| 26 | }; |
| 27 | |
| 28 | static int apple_pmgr_request(struct power_domain *power_domain) |
| 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | static int apple_pmgr_rfree(struct power_domain *power_domain) |
| 34 | { |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static int apple_pmgr_ps_set(struct power_domain *power_domain, u32 pstate) |
| 39 | { |
| 40 | struct apple_pmgr_priv *priv = dev_get_priv(power_domain->dev); |
| 41 | uint reg; |
| 42 | |
| 43 | regmap_update_bits(priv->regmap, priv->offset, APPLE_PMGR_PS_TARGET, |
| 44 | FIELD_PREP(APPLE_PMGR_PS_TARGET, pstate)); |
| 45 | |
| 46 | return regmap_read_poll_timeout( |
| 47 | priv->regmap, priv->offset, reg, |
| 48 | (FIELD_GET(APPLE_PMGR_PS_ACTUAL, reg) == pstate), 1, |
| 49 | APPLE_PMGR_PS_SET_TIMEOUT_US); |
| 50 | } |
| 51 | |
| 52 | static int apple_pmgr_on(struct power_domain *power_domain) |
| 53 | { |
| 54 | return apple_pmgr_ps_set(power_domain, APPLE_PMGR_PS_ACTIVE); |
| 55 | } |
| 56 | |
| 57 | static int apple_pmgr_off(struct power_domain *power_domain) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int apple_pmgr_of_xlate(struct power_domain *power_domain, |
| 63 | struct ofnode_phandle_args *args) |
| 64 | { |
| 65 | if (args->args_count != 0) { |
| 66 | debug("Invalid args_count: %d\n", args->args_count); |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static const struct udevice_id apple_pmgr_ids[] = { |
| 74 | { .compatible = "apple,pmgr-pwrstate" }, |
| 75 | { /* sentinel */ } |
| 76 | }; |
| 77 | |
| 78 | static int apple_pmgr_probe(struct udevice *dev) |
| 79 | { |
| 80 | struct apple_pmgr_priv *priv = dev_get_priv(dev); |
| 81 | int ret; |
| 82 | |
| 83 | ret = dev_power_domain_on(dev); |
| 84 | if (ret) |
| 85 | return ret; |
| 86 | |
| 87 | priv->regmap = syscon_get_regmap(dev->parent); |
| 88 | if (IS_ERR(priv->regmap)) |
| 89 | return PTR_ERR(priv->regmap); |
| 90 | |
| 91 | ret = dev_read_u32(dev, "reg", &priv->offset); |
| 92 | if (ret < 0) |
| 93 | return ret; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | struct power_domain_ops apple_pmgr_ops = { |
| 99 | .request = apple_pmgr_request, |
| 100 | .rfree = apple_pmgr_rfree, |
| 101 | .on = apple_pmgr_on, |
| 102 | .off = apple_pmgr_off, |
| 103 | .of_xlate = apple_pmgr_of_xlate, |
| 104 | }; |
| 105 | |
| 106 | U_BOOT_DRIVER(apple_pmgr) = { |
| 107 | .name = "apple_pmgr", |
| 108 | .id = UCLASS_POWER_DOMAIN, |
| 109 | .of_match = apple_pmgr_ids, |
| 110 | .ops = &apple_pmgr_ops, |
| 111 | .probe = apple_pmgr_probe, |
| 112 | .priv_auto = sizeof(struct apple_pmgr_priv), |
| 113 | }; |