1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2017
4  * Texas Instruments Incorporated, <www.ti.com>
5  *
6  * Keerthy <j-keerthy@ti.com>
7  */
8 
9 #include <common.h>
10 #include <fdtdec.h>
11 #include <errno.h>
12 #include <dm.h>
13 #include <i2c.h>
14 #include <power/pmic.h>
15 #include <power/regulator.h>
16 #include <power/lp87565.h>
17 
18 static const char lp87565_buck_ctrl1[LP87565_BUCK_NUM] = {0x2, 0x4, 0x6, 0x8, 0x2, 0x6};
19 static const char lp87565_buck_vout[LP87565_BUCK_NUM] = {0xA, 0xC, 0xE, 0x10, 0xA, 0xE };
20 
21 static int lp87565_buck_enable(struct udevice *dev, int op, bool *enable)
22 {
23 	int ret;
24 	unsigned int adr;
25 	struct dm_regulator_uclass_platdata *uc_pdata;
26 
27 	uc_pdata = dev_get_uclass_platdata(dev);
28 	adr = uc_pdata->ctrl_reg;
29 
30 	ret = pmic_reg_read(dev->parent, adr);
31 	if (ret < 0)
32 		return ret;
33 
34 	if (op == PMIC_OP_GET) {
35 		ret &= LP87565_BUCK_MODE_MASK;
36 
37 		if (ret)
38 			*enable = true;
39 		else
40 			*enable = false;
41 
42 		return 0;
43 	} else if (op == PMIC_OP_SET) {
44 		if (*enable)
45 			ret |= LP87565_BUCK_MODE_MASK;
46 		else
47 			ret &= ~LP87565_BUCK_MODE_MASK;
48 		ret = pmic_reg_write(dev->parent, adr, ret);
49 		if (ret)
50 			return ret;
51 	}
52 
53 	return 0;
54 }
55 
56 static int lp87565_buck_volt2val(int uV)
57 {
58 	if (uV > LP87565_BUCK_VOLT_MAX)
59 		return -EINVAL;
60 	else if (uV > 1400000)
61 		return (uV - 1420000) / 20000 + 0x9E;
62 	else if (uV > 730000)
63 		return (uV - 735000) / 5000 + 0x18;
64 	else if (uV >= 500000)
65 		return (uV - 500000) / 10000;
66 	else
67 		return -EINVAL;
68 }
69 
70 static int lp87565_buck_val2volt(int val)
71 {
72 	if (val > LP87565_BUCK_VOLT_MAX_HEX)
73 		return -EINVAL;
74 	else if (val > 0x9D)
75 		return 1400000 + (val - 0x9D) * 20000;
76 	else if (val > 0x17)
77 		return 730000 + (val - 0x17) * 5000;
78 	else if (val >= 0x0)
79 		return 500000 + val * 10000;
80 	else
81 		return -EINVAL;
82 }
83 
84 static int lp87565_buck_val(struct udevice *dev, int op, int *uV)
85 {
86 	unsigned int hex, adr;
87 	int ret;
88 	struct dm_regulator_uclass_platdata *uc_pdata;
89 
90 	uc_pdata = dev_get_uclass_platdata(dev);
91 
92 	if (op == PMIC_OP_GET)
93 		*uV = 0;
94 
95 	adr = uc_pdata->volt_reg;
96 
97 	ret = pmic_reg_read(dev->parent, adr);
98 	if (ret < 0)
99 		return ret;
100 
101 	if (op == PMIC_OP_GET) {
102 		ret &= LP87565_BUCK_VOLT_MASK;
103 		ret = lp87565_buck_val2volt(ret);
104 		if (ret < 0)
105 			return ret;
106 		*uV = ret;
107 
108 		return 0;
109 	}
110 
111 	hex = lp87565_buck_volt2val(*uV);
112 	if (hex < 0)
113 		return hex;
114 
115 	ret &= 0x0;
116 	ret = hex;
117 
118 	ret = pmic_reg_write(dev->parent, adr, ret);
119 
120 	return ret;
121 }
122 
123 static int lp87565_buck_probe(struct udevice *dev)
124 {
125 	struct dm_regulator_uclass_platdata *uc_pdata;
126 	int idx;
127 
128 	uc_pdata = dev_get_uclass_platdata(dev);
129 	uc_pdata->type = REGULATOR_TYPE_BUCK;
130 
131 	idx = dev->driver_data;
132 	if (idx == 0 || idx == 1 || idx == 2 || idx == 3) {
133 		debug("Single phase regulator\n");
134 	} else if (idx == 23) {
135 		idx = 5;
136 	} else if (idx == 10) {
137 		idx = 4;
138 	} else {
139 		printf("Wrong ID for regulator\n");
140 		return -EINVAL;
141 	}
142 
143 	uc_pdata->ctrl_reg = lp87565_buck_ctrl1[idx];
144 	uc_pdata->volt_reg = lp87565_buck_vout[idx];
145 
146 	return 0;
147 }
148 
149 static int buck_get_value(struct udevice *dev)
150 {
151 	int uV;
152 	int ret;
153 
154 	ret = lp87565_buck_val(dev, PMIC_OP_GET, &uV);
155 	if (ret)
156 		return ret;
157 
158 	return uV;
159 }
160 
161 static int buck_set_value(struct udevice *dev, int uV)
162 {
163 	return lp87565_buck_val(dev, PMIC_OP_SET, &uV);
164 }
165 
166 static int buck_get_enable(struct udevice *dev)
167 {
168 	bool enable = false;
169 	int ret;
170 
171 
172 	ret = lp87565_buck_enable(dev, PMIC_OP_GET, &enable);
173 	if (ret)
174 		return ret;
175 
176 	return enable;
177 }
178 
179 static int buck_set_enable(struct udevice *dev, bool enable)
180 {
181 	return lp87565_buck_enable(dev, PMIC_OP_SET, &enable);
182 }
183 
184 static const struct dm_regulator_ops lp87565_buck_ops = {
185 	.get_value  = buck_get_value,
186 	.set_value  = buck_set_value,
187 	.get_enable = buck_get_enable,
188 	.set_enable = buck_set_enable,
189 };
190 
191 U_BOOT_DRIVER(lp87565_buck) = {
192 	.name = LP87565_BUCK_DRIVER,
193 	.id = UCLASS_REGULATOR,
194 	.ops = &lp87565_buck_ops,
195 	.probe = lp87565_buck_probe,
196 };
197