1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  *
6  * Placeholder regulator driver for as3722.
7  */
8 
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <power/as3722.h>
13 #include <power/pmic.h>
14 #include <power/regulator.h>
15 
16 #define AS3722_LDO_CONTROL0_MAX_INDEX 7
17 
18 static int stepdown_get_value(struct udevice *dev)
19 {
20 	return -ENOSYS;
21 }
22 
23 static int stepdown_set_value(struct udevice *dev, int uvolt)
24 {
25 	return -ENOSYS;
26 }
27 
28 static int stepdown_set_enable(struct udevice *dev, bool enable)
29 {
30 	struct udevice *pmic = dev_get_parent(dev);
31 	int sd = dev->driver_data;
32 	int ret;
33 
34 	ret = pmic_clrsetbits(pmic, AS3722_SD_CONTROL, 0, 1 << sd);
35 	if (ret < 0) {
36 		debug("%s: failed to write SD control register: %d", __func__,
37 		      ret);
38 		return ret;
39 	}
40 
41 	return 0;
42 }
43 
44 static int stepdown_get_enable(struct udevice *dev)
45 {
46 	struct udevice *pmic = dev_get_parent(dev);
47 	int sd = dev->driver_data;
48 	int ret;
49 
50 	ret = pmic_reg_read(pmic, AS3722_SD_CONTROL);
51 	if (ret < 0) {
52 		debug("%s: failed to read SD control register: %d", __func__,
53 		      ret);
54 		return ret;
55 	}
56 
57 	return ret & (1 << sd) ? true : false;
58 }
59 
60 static int ldo_get_value(struct udevice *dev)
61 {
62 	return -ENOSYS;
63 }
64 
65 static int ldo_set_value(struct udevice *dev, int uvolt)
66 {
67 	return -ENOSYS;
68 }
69 
70 static int ldo_set_enable(struct udevice *dev, bool enable)
71 {
72 	struct udevice *pmic = dev_get_parent(dev);
73 	u8 ctrl_reg = AS3722_LDO_CONTROL0;
74 	int ldo = dev->driver_data;
75 	int ret;
76 
77 	if (ldo > AS3722_LDO_CONTROL0_MAX_INDEX) {
78 		ctrl_reg = AS3722_LDO_CONTROL1;
79 		ldo -= 8;
80 	}
81 
82 	ret = pmic_clrsetbits(pmic, ctrl_reg, !enable << ldo, enable << ldo);
83 	if (ret < 0) {
84 		debug("%s: failed to write LDO control register: %d", __func__,
85 		      ret);
86 		return ret;
87 	}
88 
89 	return 0;
90 }
91 
92 static int ldo_get_enable(struct udevice *dev)
93 {
94 	struct udevice *pmic = dev_get_parent(dev);
95 	u8 ctrl_reg = AS3722_LDO_CONTROL0;
96 	int ldo = dev->driver_data;
97 	int ret;
98 
99 	if (ldo > AS3722_LDO_CONTROL0_MAX_INDEX) {
100 		ctrl_reg = AS3722_LDO_CONTROL1;
101 		ldo -= 8;
102 	}
103 
104 	ret = pmic_reg_read(pmic, ctrl_reg);
105 	if (ret < 0) {
106 		debug("%s: failed to read SD control register: %d", __func__,
107 		      ret);
108 		return ret;
109 	}
110 
111 	return ret & (1 << ldo) ? true : false;
112 }
113 
114 static int as3722_stepdown_probe(struct udevice *dev)
115 {
116 	struct dm_regulator_uclass_platdata *uc_pdata;
117 
118 	uc_pdata = dev_get_uclass_platdata(dev);
119 
120 	uc_pdata->type = REGULATOR_TYPE_BUCK;
121 
122 	return 0;
123 }
124 
125 static int as3722_ldo_probe(struct udevice *dev)
126 {
127 	struct dm_regulator_uclass_platdata *uc_pdata;
128 
129 	uc_pdata = dev_get_uclass_platdata(dev);
130 
131 	uc_pdata->type = REGULATOR_TYPE_LDO;
132 
133 	return 0;
134 }
135 
136 static const struct dm_regulator_ops as3722_stepdown_ops = {
137 	.get_value  = stepdown_get_value,
138 	.set_value  = stepdown_set_value,
139 	.get_enable = stepdown_get_enable,
140 	.set_enable = stepdown_set_enable,
141 };
142 
143 static const struct dm_regulator_ops as3722_ldo_ops = {
144 	.get_value  = ldo_get_value,
145 	.set_value  = ldo_set_value,
146 	.get_enable = ldo_get_enable,
147 	.set_enable = ldo_set_enable,
148 };
149 
150 U_BOOT_DRIVER(as3722_stepdown) = {
151 	.name = "as3722_stepdown",
152 	.id = UCLASS_REGULATOR,
153 	.ops = &as3722_stepdown_ops,
154 	.probe = as3722_stepdown_probe,
155 };
156 
157 U_BOOT_DRIVER(as3722_ldo) = {
158 	.name = "as3722_ldo",
159 	.id = UCLASS_REGULATOR,
160 	.ops = &as3722_ldo_ops,
161 	.probe = as3722_ldo_probe,
162 };
163