1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Rockchip Electronics Co., Ltd
5 * Based on kernel drivers/regulator/pwm-regulator.c
6 * Copyright (C) 2014 - STMicroelectronics Inc.
7 * Author: Lee Jones <lee.jones@linaro.org>
15 #include <dm/device_compat.h>
16 #include <power/regulator.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 struct pwm_regulator_info {
21 /* pwm id corresponding to the PWM driver */
23 /* the period of one PWM cycle */
26 * the polarity of one PWM
28 * 1: inverted polarity
32 /* initialize voltage of regulator */
34 /* the maximum voltage of regulator */
36 /* the minimum voltage of regulator */
38 /* the current voltage of regulator */
42 static int pwm_regulator_enable(struct udevice *dev, bool enable)
44 struct pwm_regulator_info *priv = dev_get_priv(dev);
46 return pwm_set_enable(priv->pwm, priv->pwm_id, enable);
49 static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
51 struct pwm_regulator_info *priv = dev_get_priv(dev);
52 int min_uV = priv->min_voltage;
53 int max_uV = priv->max_voltage;
54 int diff = max_uV - min_uV;
56 return ((req_uV * 100) - (min_uV * 100)) / diff;
59 static int pwm_regulator_get_voltage(struct udevice *dev)
61 struct pwm_regulator_info *priv = dev_get_priv(dev);
66 static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
68 struct pwm_regulator_info *priv = dev_get_priv(dev);
72 duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
74 ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
76 dev_err(dev, "Failed to init PWM\n");
80 ret = pwm_set_config(priv->pwm, priv->pwm_id,
81 priv->period_ns, (priv->period_ns / 100) * duty_cycle);
83 dev_err(dev, "Failed to configure PWM\n");
87 priv->volt_uV = uvolt;
92 static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
94 struct pwm_regulator_info *priv = dev_get_priv(dev);
95 struct ofnode_phandle_args args;
98 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
100 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
104 priv->period_ns = args.args[1];
105 priv->polarity = args.args[2];
107 priv->init_voltage = dev_read_u32_default(dev, "regulator-init-microvolt", -1);
108 if (priv->init_voltage < 0) {
109 printf("Cannot find regulator pwm init_voltage\n");
113 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
115 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
122 static int pwm_regulator_probe(struct udevice *dev)
124 struct pwm_regulator_info *priv = dev_get_priv(dev);
125 struct dm_regulator_uclass_platdata *uc_pdata;
127 uc_pdata = dev_get_uclass_platdata(dev);
129 uc_pdata->type = REGULATOR_TYPE_BUCK;
130 uc_pdata->mode_count = 0;
131 priv->max_voltage = uc_pdata->max_uV;
132 priv->min_voltage = uc_pdata->min_uV;
134 if (priv->init_voltage)
135 pwm_regulator_set_voltage(dev, priv->init_voltage);
140 static const struct dm_regulator_ops pwm_regulator_ops = {
141 .get_value = pwm_regulator_get_voltage,
142 .set_value = pwm_regulator_set_voltage,
143 .set_enable = pwm_regulator_enable,
146 static const struct udevice_id pwm_regulator_ids[] = {
147 { .compatible = "pwm-regulator" },
151 U_BOOT_DRIVER(pwm_regulator) = {
152 .name = "pwm_regulator",
153 .id = UCLASS_REGULATOR,
154 .ops = &pwm_regulator_ops,
155 .probe = pwm_regulator_probe,
156 .of_match = pwm_regulator_ids,
157 .ofdata_to_platdata = pwm_regulator_ofdata_to_platdata,
158 .priv_auto_alloc_size = sizeof(struct pwm_regulator_info),