xref: /OK3568_Linux_fs/u-boot/drivers/power/regulator/pwm_regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2016 Rockchip Electronics Co., Ltd
3  *
4  * Based on kernel drivers/regulator/pwm-regulator.c
5  * Copyright (C) 2014 - STMicroelectronics Inc.
6  * Author: Lee Jones <lee.jones@linaro.org>
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 
11 #include <common.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <pwm.h>
15 #include <power/regulator.h>
16 #include <linux/libfdt.h>
17 #include <fdt_support.h>
18 #include <fdtdec.h>
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
22 struct pwm_regulator_info {
23 	/* pwm id corresponding to the PWM driver */
24 	int pwm_id;
25 	/* the period of one PWM cycle */
26 	int period_ns;
27 	/*
28 	 * the polarity of one PWM
29 	 * 0: normal polarity
30 	 * 1: inverted polarity
31 	 */
32 	bool polarity;
33 	struct udevice *pwm;
34 	/* initialize voltage of regulator */
35 	int init_voltage;
36 	/* the maximum voltage of regulator */
37 	int max_voltage;
38 	/* the minimum voltage of regulator */
39 	int min_voltage;
40 	/* the current voltage of regulator */
41 	int volt_uV;
42 };
43 
pwm_regulator_enable(struct udevice * dev,bool enable)44 static int pwm_regulator_enable(struct udevice *dev, bool enable)
45 {
46 	struct pwm_regulator_info *priv = dev_get_priv(dev);
47 
48 	return pwm_set_enable(priv->pwm, priv->pwm_id, enable);
49 }
50 
pwm_voltage_to_duty_cycle_percentage(struct udevice * dev,int req_uV)51 static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
52 {
53 	struct pwm_regulator_info *priv = dev_get_priv(dev);
54 	int min_uV = priv->min_voltage;
55 	int max_uV = priv->max_voltage;
56 	int diff = max_uV - min_uV;
57 
58 	return ((req_uV * 100) - (min_uV * 100)) / diff;
59 }
60 
pwm_regulator_get_voltage(struct udevice * dev)61 static int pwm_regulator_get_voltage(struct udevice *dev)
62 {
63 	struct pwm_regulator_info *priv = dev_get_priv(dev);
64 
65 	return priv->volt_uV;
66 }
67 
pwm_regulator_set_voltage(struct udevice * dev,int uvolt)68 static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
69 {
70 	struct pwm_regulator_info *priv = dev_get_priv(dev);
71 	int duty_cycle;
72 	int ret = 0;
73 
74 	duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
75 
76 	ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
77 	if (ret) {
78 		dev_err(dev, "Failed to init PWM\n");
79 		return ret;
80 	}
81 
82 	ret = pwm_set_config(priv->pwm, priv->pwm_id,
83 			priv->period_ns, (priv->period_ns / 100) * duty_cycle);
84 	if (ret) {
85 		dev_err(dev, "Failed to configure PWM\n");
86 		return ret;
87 	}
88 
89 	priv->volt_uV = uvolt;
90 
91 	return ret;
92 }
93 
pwm_regulator_ofdata_to_platdata(struct udevice * dev)94 static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
95 {
96 	struct pwm_regulator_info *priv = dev_get_priv(dev);
97 	struct ofnode_phandle_args args;
98 	int ret;
99 
100 	ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
101 	if (ret) {
102 		debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
103 		return ret;
104 	}
105 
106 	priv->period_ns = args.args[1];
107 	priv->polarity = args.args[2];
108 
109 	priv->init_voltage = dev_read_u32_default(dev, "regulator-init-microvolt", -1);
110 	if (priv->init_voltage < 0) {
111 		debug("Cannot find 'regulator-init-microvolt'\n");
112 
113 		/*
114 		 * 1. Compatible legacy pwm-regulator driver on rkdevelop;
115 		 * 2. Give pwm-regulator default init voltage 1.1v;
116 		 */
117 		priv->init_voltage = dev_read_u32_default(dev, "rockchip,pwm_voltage",
118 							  1100000);
119 	}
120 
121 	ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
122 	if (ret) {
123 		debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
124 		return ret;
125 	}
126 
127 	return 0;
128 }
129 
pwm_regulator_probe(struct udevice * dev)130 static int pwm_regulator_probe(struct udevice *dev)
131 {
132 	struct pwm_regulator_info *priv = dev_get_priv(dev);
133 	struct dm_regulator_uclass_platdata *uc_pdata;
134 
135 	uc_pdata = dev_get_uclass_platdata(dev);
136 
137 	uc_pdata->type = REGULATOR_TYPE_BUCK;
138 	uc_pdata->mode_count = 0;
139 	priv->max_voltage = uc_pdata->max_uV;
140 	priv->min_voltage = uc_pdata->min_uV;
141 
142 	if (priv->init_voltage > 0) {
143 		debug("pwm-regulator(%s): init %d uV\n",
144 		       dev->name, priv->init_voltage);
145 		pwm_regulator_set_voltage(dev, priv->init_voltage);
146 	}
147 
148 	return 0;
149 }
150 
151 static const struct dm_regulator_ops pwm_regulator_ops = {
152 	.get_value  = pwm_regulator_get_voltage,
153 	.set_value  = pwm_regulator_set_voltage,
154 	.set_enable = pwm_regulator_enable,
155 };
156 
157 static const struct udevice_id pwm_regulator_ids[] = {
158 	{ .compatible = "pwm-regulator" },
159 	{ }
160 };
161 
162 U_BOOT_DRIVER(pwm_regulator) = {
163 	.name = "pwm_regulator",
164 	.id = UCLASS_REGULATOR,
165 	.ops = &pwm_regulator_ops,
166 	.probe = pwm_regulator_probe,
167 	.of_match = pwm_regulator_ids,
168 	.ofdata_to_platdata	= pwm_regulator_ofdata_to_platdata,
169 	.priv_auto_alloc_size	= sizeof(struct pwm_regulator_info),
170 };
171