blob: f8706227058fec66b2eecdf058d73ac3d8a407e9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kever Yang8bbe1cc2016-09-23 15:57:19 +08002/*
3 * Copyright (C) 2016 Rockchip Electronics Co., Ltd
4 *
5 * Based on kernel drivers/regulator/pwm-regulator.c
6 * Copyright (C) 2014 - STMicroelectronics Inc.
7 * Author: Lee Jones <lee.jones@linaro.org>
Kever Yang8bbe1cc2016-09-23 15:57:19 +08008 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Kever Yang8bbe1cc2016-09-23 15:57:19 +080014#include <pwm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Kever Yang8bbe1cc2016-09-23 15:57:19 +080016#include <power/regulator.h>
Kever Yang8bbe1cc2016-09-23 15:57:19 +080017
18DECLARE_GLOBAL_DATA_PTR;
19
20struct pwm_regulator_info {
21 /* pwm id corresponding to the PWM driver */
22 int pwm_id;
23 /* the period of one PWM cycle */
24 int period_ns;
Kever Yang4c087422017-04-24 10:27:49 +080025 /*
26 * the polarity of one PWM
27 * 0: normal polarity
28 * 1: inverted polarity
29 */
30 bool polarity;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080031 struct udevice *pwm;
32 /* initialize voltage of regulator */
Mark Kettenis416b6c52017-05-13 20:17:05 +020033 int init_voltage;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080034 /* the maximum voltage of regulator */
Mark Kettenis416b6c52017-05-13 20:17:05 +020035 int max_voltage;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080036 /* the minimum voltage of regulator */
Mark Kettenis416b6c52017-05-13 20:17:05 +020037 int min_voltage;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080038 /* the current voltage of regulator */
Mark Kettenis416b6c52017-05-13 20:17:05 +020039 int volt_uV;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080040};
41
42static int pwm_regulator_enable(struct udevice *dev, bool enable)
43{
44 struct pwm_regulator_info *priv = dev_get_priv(dev);
45
46 return pwm_set_enable(priv->pwm, priv->pwm_id, enable);
47}
48
49static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
50{
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;
55
Kever Yang4c087422017-04-24 10:27:49 +080056 return ((req_uV * 100) - (min_uV * 100)) / diff;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080057}
58
59static int pwm_regulator_get_voltage(struct udevice *dev)
60{
61 struct pwm_regulator_info *priv = dev_get_priv(dev);
62
63 return priv->volt_uV;
64}
65
66static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
67{
68 struct pwm_regulator_info *priv = dev_get_priv(dev);
69 int duty_cycle;
70 int ret = 0;
71
72 duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
73
Kever Yang4c087422017-04-24 10:27:49 +080074 ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
75 if (ret) {
76 dev_err(dev, "Failed to init PWM\n");
77 return ret;
78 }
79
Kever Yang8bbe1cc2016-09-23 15:57:19 +080080 ret = pwm_set_config(priv->pwm, priv->pwm_id,
Kever Yang6ea3e5d2017-07-19 19:54:21 +080081 priv->period_ns, (priv->period_ns / 100) * duty_cycle);
Kever Yang8bbe1cc2016-09-23 15:57:19 +080082 if (ret) {
83 dev_err(dev, "Failed to configure PWM\n");
84 return ret;
85 }
86
Kever Yang8bbe1cc2016-09-23 15:57:19 +080087 priv->volt_uV = uvolt;
Kever Yangcba88f12017-07-19 19:54:22 +080088
Kever Yang8bbe1cc2016-09-23 15:57:19 +080089 return ret;
90}
91
92static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
93{
94 struct pwm_regulator_info *priv = dev_get_priv(dev);
Andy Yan0addcea2018-03-27 19:39:38 +080095 struct ofnode_phandle_args args;
Kever Yang8bbe1cc2016-09-23 15:57:19 +080096 int ret;
97
Andy Yan0addcea2018-03-27 19:39:38 +080098 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
Kever Yang8bbe1cc2016-09-23 15:57:19 +080099 if (ret) {
100 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
101 return ret;
102 }
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800103
104 priv->period_ns = args.args[1];
Kever Yang4c087422017-04-24 10:27:49 +0800105 priv->polarity = args.args[2];
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800106
Andy Yan0addcea2018-03-27 19:39:38 +0800107 priv->init_voltage = dev_read_u32_default(dev, "regulator-init-microvolt", -1);
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800108 if (priv->init_voltage < 0) {
109 printf("Cannot find regulator pwm init_voltage\n");
110 return -EINVAL;
111 }
112
Andy Yan0addcea2018-03-27 19:39:38 +0800113 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800114 if (ret) {
115 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
116 return ret;
117 }
118
119 return 0;
120}
121
122static int pwm_regulator_probe(struct udevice *dev)
123{
124 struct pwm_regulator_info *priv = dev_get_priv(dev);
125 struct dm_regulator_uclass_platdata *uc_pdata;
126
127 uc_pdata = dev_get_uclass_platdata(dev);
128
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;
133
134 if (priv->init_voltage)
135 pwm_regulator_set_voltage(dev, priv->init_voltage);
136
Kever Yang8bbe1cc2016-09-23 15:57:19 +0800137 return 0;
138}
139
140static 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,
144};
145
146static const struct udevice_id pwm_regulator_ids[] = {
147 { .compatible = "pwm-regulator" },
148 { }
149};
150
151U_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),
159};