^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) // System Control and Management Interface (SCMI) based regulator driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright (C) 2020 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Implements a regulator driver on top of the SCMI Voltage Protocol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) // The ARM SCMI Protocol aims in general to hide as much as possible all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) // underlying operational details while providing an abstracted interface for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) // its users to operate upon: as a consequence the resulting operational
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) // capabilities and configurability of this regulator device are much more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) // limited than the ones usually available on a standard physical regulator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) // The supported SCMI regulator ops are restricted to the bare minimum:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) // - 'status_ops': enable/disable/is_enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) // - 'voltage_ops': get_voltage_sel/set_voltage_sel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) // list_voltage/map_voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) // Each SCMI regulator instance is associated, through the means of a proper DT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) // entry description, to a specific SCMI Voltage Domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/linear_range.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/regulator/machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/regulator/of_regulator.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/scmi_protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static const struct scmi_voltage_proto_ops *voltage_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct scmi_regulator {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u32 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct scmi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct scmi_protocol_handle *ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct regulator_dev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct device_node *of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct regulator_desc desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct regulator_config conf;
^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) struct scmi_regulator_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int num_doms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct scmi_regulator **sregv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int scmi_reg_enable(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct scmi_regulator *sreg = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return voltage_ops->config_set(sreg->ph, sreg->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) SCMI_VOLTAGE_ARCH_STATE_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int scmi_reg_disable(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct scmi_regulator *sreg = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return voltage_ops->config_set(sreg->ph, sreg->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) SCMI_VOLTAGE_ARCH_STATE_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int scmi_reg_is_enabled(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u32 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct scmi_regulator *sreg = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret = voltage_ops->config_get(sreg->ph, sreg->id, &config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dev_err(&sreg->sdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) "Error %d reading regulator %s status.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ret, sreg->desc.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return config & SCMI_VOLTAGE_ARCH_STATE_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int scmi_reg_get_voltage_sel(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) s32 volt_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct scmi_regulator *sreg = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ret = voltage_ops->level_get(sreg->ph, sreg->id, &volt_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return sreg->desc.ops->map_voltage(rdev, volt_uV, volt_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static int scmi_reg_set_voltage_sel(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned int selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) s32 volt_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct scmi_regulator *sreg = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) volt_uV = sreg->desc.ops->list_voltage(rdev, selector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (volt_uV <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return voltage_ops->level_set(sreg->ph, sreg->id, 0x0, volt_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static const struct regulator_ops scmi_reg_fixed_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .enable = scmi_reg_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .disable = scmi_reg_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .is_enabled = scmi_reg_is_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static const struct regulator_ops scmi_reg_linear_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .enable = scmi_reg_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .disable = scmi_reg_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .is_enabled = scmi_reg_is_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .get_voltage_sel = scmi_reg_get_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .set_voltage_sel = scmi_reg_set_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .list_voltage = regulator_list_voltage_linear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .map_voltage = regulator_map_voltage_linear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static const struct regulator_ops scmi_reg_discrete_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .enable = scmi_reg_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .disable = scmi_reg_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .is_enabled = scmi_reg_is_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .get_voltage_sel = scmi_reg_get_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .set_voltage_sel = scmi_reg_set_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .list_voltage = regulator_list_voltage_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .map_voltage = regulator_map_voltage_iterate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) scmi_config_linear_regulator_mappings(struct scmi_regulator *sreg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) const struct scmi_voltage_info *vinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) s32 delta_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Note that SCMI voltage domains describable by linear ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * (segments) {low, high, step} are guaranteed to come in one single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * triplet by the SCMI Voltage Domain protocol support itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) delta_uV = (vinfo->levels_uv[SCMI_VOLTAGE_SEGMENT_HIGH] -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) vinfo->levels_uv[SCMI_VOLTAGE_SEGMENT_LOW]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Rule out buggy negative-intervals answers from fw */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (delta_uV < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) dev_err(&sreg->sdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) "Invalid volt-range %d-%duV for domain %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) vinfo->levels_uv[SCMI_VOLTAGE_SEGMENT_LOW],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) vinfo->levels_uv[SCMI_VOLTAGE_SEGMENT_HIGH],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) sreg->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!delta_uV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* Just one fixed voltage exposed by SCMI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) sreg->desc.fixed_uV =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) vinfo->levels_uv[SCMI_VOLTAGE_SEGMENT_LOW];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) sreg->desc.n_voltages = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) sreg->desc.ops = &scmi_reg_fixed_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* One simple linear mapping. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) sreg->desc.min_uV =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) vinfo->levels_uv[SCMI_VOLTAGE_SEGMENT_LOW];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) sreg->desc.uV_step =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) vinfo->levels_uv[SCMI_VOLTAGE_SEGMENT_STEP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) sreg->desc.linear_min_sel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) sreg->desc.n_voltages = (delta_uV / sreg->desc.uV_step) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) sreg->desc.ops = &scmi_reg_linear_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) scmi_config_discrete_regulator_mappings(struct scmi_regulator *sreg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) const struct scmi_voltage_info *vinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* Discrete non linear levels are mapped to volt_table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) sreg->desc.n_voltages = vinfo->num_levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (sreg->desc.n_voltages > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) sreg->desc.volt_table = (const unsigned int *)vinfo->levels_uv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) sreg->desc.ops = &scmi_reg_discrete_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) sreg->desc.fixed_uV = vinfo->levels_uv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) sreg->desc.ops = &scmi_reg_fixed_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int scmi_regulator_common_init(struct scmi_regulator *sreg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct device *dev = &sreg->sdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) const struct scmi_voltage_info *vinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) vinfo = voltage_ops->info_get(sreg->ph, sreg->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!vinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) dev_warn(dev, "Failure to get voltage domain %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) sreg->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * Regulator framework does not fully support negative voltages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * so we discard any voltage domain reported as supporting negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * voltages: as a consequence each levels_uv entry is guaranteed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * be non-negative from here on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (vinfo->negative_volts_allowed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dev_warn(dev, "Negative voltages NOT supported...skip %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) sreg->of_node->full_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) sreg->desc.name = devm_kasprintf(dev, GFP_KERNEL, "%s", vinfo->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!sreg->desc.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) sreg->desc.id = sreg->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) sreg->desc.type = REGULATOR_VOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) sreg->desc.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) sreg->desc.of_match_full_name = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) sreg->desc.of_match = sreg->of_node->full_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) sreg->desc.regulators_node = "regulators";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (vinfo->segmented)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ret = scmi_config_linear_regulator_mappings(sreg, vinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ret = scmi_config_discrete_regulator_mappings(sreg, vinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * Using the scmi device here to have DT searched from Voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * protocol node down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) sreg->conf.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* Store for later retrieval via rdev_get_drvdata() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) sreg->conf.driver_data = sreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int process_scmi_regulator_of_node(struct scmi_device *sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct scmi_protocol_handle *ph,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct scmi_regulator_info *rinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) u32 dom, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) ret = of_property_read_u32(np, "reg", &dom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (dom >= rinfo->num_doms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (rinfo->sregv[dom]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) dev_err(&sdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) "SCMI Voltage Domain %d already in use. Skipping: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) dom, np->full_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) rinfo->sregv[dom] = devm_kzalloc(&sdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) sizeof(struct scmi_regulator),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (!rinfo->sregv[dom])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) rinfo->sregv[dom]->id = dom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) rinfo->sregv[dom]->sdev = sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) rinfo->sregv[dom]->ph = ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* get hold of good nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) of_node_get(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) rinfo->sregv[dom]->of_node = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dev_dbg(&sdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) "Found SCMI Regulator entry -- OF node [%d] -> %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) dom, np->full_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static int scmi_regulator_probe(struct scmi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int d, ret, num_doms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct device_node *np, *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) const struct scmi_handle *handle = sdev->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct scmi_regulator_info *rinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct scmi_protocol_handle *ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) voltage_ops = handle->devm_get_protocol(sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) SCMI_PROTOCOL_VOLTAGE, &ph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (IS_ERR(voltage_ops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return PTR_ERR(voltage_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) num_doms = voltage_ops->num_domains_get(ph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (num_doms <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!num_doms) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) dev_err(&sdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) "number of voltage domains invalid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) num_doms = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) dev_err(&sdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) "failed to get voltage domains - err:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) num_doms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return num_doms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) rinfo = devm_kzalloc(&sdev->dev, sizeof(*rinfo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (!rinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* Allocate pointers array for all possible domains */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) rinfo->sregv = devm_kcalloc(&sdev->dev, num_doms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) sizeof(void *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (!rinfo->sregv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) rinfo->num_doms = num_doms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * Start collecting into rinfo->sregv possibly good SCMI Regulators as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * described by a well-formed DT entry and associated with an existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * plausible SCMI Voltage Domain number, all belonging to this SCMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * platform instance node (handle->dev->of_node).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) np = of_find_node_by_name(handle->dev->of_node, "regulators");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) for_each_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ret = process_scmi_regulator_of_node(sdev, ph, child, rinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /* abort on any mem issue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (ret == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * Register a regulator for each valid regulator-DT-entry that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * can successfully reach via SCMI and has a valid associated voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) for (d = 0; d < num_doms; d++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct scmi_regulator *sreg = rinfo->sregv[d];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* Skip empty slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (!sreg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ret = scmi_regulator_common_init(sreg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* Skip invalid voltage domains */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) sreg->rdev = devm_regulator_register(&sdev->dev, &sreg->desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) &sreg->conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (IS_ERR(sreg->rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) sreg->rdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) dev_info(&sdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) "Regulator %s registered for domain [%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) sreg->desc.name, sreg->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) dev_set_drvdata(&sdev->dev, rinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static void scmi_regulator_remove(struct scmi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) struct scmi_regulator_info *rinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) rinfo = dev_get_drvdata(&sdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (!rinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) for (d = 0; d < rinfo->num_doms; d++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (!rinfo->sregv[d])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) of_node_put(rinfo->sregv[d]->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static const struct scmi_device_id scmi_regulator_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) { SCMI_PROTOCOL_VOLTAGE, "regulator" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) MODULE_DEVICE_TABLE(scmi, scmi_regulator_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static struct scmi_driver scmi_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .name = "scmi-regulator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .probe = scmi_regulator_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) .remove = scmi_regulator_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) .id_table = scmi_regulator_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) module_scmi_driver(scmi_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) MODULE_AUTHOR("Cristian Marussi <cristian.marussi@arm.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) MODULE_DESCRIPTION("ARM SCMI regulator driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) MODULE_LICENSE("GPL v2");