xref: /OK3568_Linux_fs/kernel/drivers/regulator/dio5632-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2020 Rockchip Electronics Co. Ltd.
4  *
5  * Author: Guochun Huang <hero.huang@rock-chips.com>
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 
17 #define DIO5632_REG_VPOS		0x00
18 #define DIO5632_REG_VNEG		0x01
19 #define DIO5632_REG_APPS_DISP_DISN	0x03
20 #define DIO5632_REG_CONTROL		0x0FF
21 
22 #define DIO5632_VOUT_MASK		0x1F
23 #define DIO5632_VOUT_N_VOLTAGE		0x15
24 #define DIO5632_VOUT_VMIN		4000000
25 #define DIO5632_VOUT_VMAX		6000000
26 #define DIO5632_VOUT_STEP		100000
27 
28 #define DIO5632_REG_DIS_VPOS		BIT(1)
29 #define DIO5632_REG_DIS_VNEG		BIT(0)
30 
31 #define DIO5632_REGULATOR_ID_VPOS	0
32 #define DIO5632_REGULATOR_ID_VNEG	1
33 #define DIO5632_MAX_REGULATORS		2
34 
35 #define DIO5632_ACT_DIS_TIME_SLACK		1000
36 
37 struct DIO5632_reg_pdata {
38 	struct gpio_desc *en_gpiod;
39 	int ena_gpio_state;
40 };
41 
42 struct DIO5632_regulator {
43 	struct device *dev;
44 	struct DIO5632_reg_pdata reg_pdata[DIO5632_MAX_REGULATORS];
45 };
46 
DIO5632_regulator_enable(struct regulator_dev * rdev)47 static int DIO5632_regulator_enable(struct regulator_dev *rdev)
48 {
49 	struct DIO5632_regulator *dio = rdev_get_drvdata(rdev);
50 	int id = rdev_get_id(rdev);
51 	struct DIO5632_reg_pdata *rpdata = &dio->reg_pdata[id];
52 	int ret;
53 
54 	if (!IS_ERR(rpdata->en_gpiod)) {
55 		gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
56 		rpdata->ena_gpio_state = 1;
57 	}
58 
59 	/* Hardware automatically enable discharge bit in enable */
60 	if (rdev->constraints->active_discharge ==
61 			REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
62 		ret = regulator_set_active_discharge_regmap(rdev, false);
63 		if (ret < 0) {
64 			dev_err(dio->dev, "Failed to disable active discharge: %d\n",
65 				ret);
66 			return ret;
67 		}
68 	}
69 
70 	return 0;
71 }
72 
DIO5632_regulator_disable(struct regulator_dev * rdev)73 static int DIO5632_regulator_disable(struct regulator_dev *rdev)
74 {
75 	struct DIO5632_regulator *dio = rdev_get_drvdata(rdev);
76 	int id = rdev_get_id(rdev);
77 	struct DIO5632_reg_pdata *rpdata = &dio->reg_pdata[id];
78 
79 	if (!IS_ERR(rpdata->en_gpiod)) {
80 		gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
81 		rpdata->ena_gpio_state = 0;
82 	}
83 
84 	return 0;
85 }
86 
DIO5632_regulator_is_enabled(struct regulator_dev * rdev)87 static int DIO5632_regulator_is_enabled(struct regulator_dev *rdev)
88 {
89 	struct DIO5632_regulator *dio = rdev_get_drvdata(rdev);
90 	int id = rdev_get_id(rdev);
91 	struct DIO5632_reg_pdata *rpdata = &dio->reg_pdata[id];
92 
93 	if (!IS_ERR(rpdata->en_gpiod))
94 		return rpdata->ena_gpio_state;
95 
96 	return 1;
97 }
98 
99 static const struct regulator_ops DIO5632_regulator_ops = {
100 	.enable = DIO5632_regulator_enable,
101 	.disable = DIO5632_regulator_disable,
102 	.is_enabled = DIO5632_regulator_is_enabled,
103 	.list_voltage = regulator_list_voltage_linear,
104 	.map_voltage = regulator_map_voltage_linear,
105 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
106 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
107 	.set_active_discharge = regulator_set_active_discharge_regmap,
108 };
109 
DIO5632_of_parse_cb(struct device_node * np,const struct regulator_desc * desc,struct regulator_config * config)110 static int DIO5632_of_parse_cb(struct device_node *np,
111 				const struct regulator_desc *desc,
112 				struct regulator_config *config)
113 {
114 	struct DIO5632_regulator *dio = config->driver_data;
115 	struct DIO5632_reg_pdata *rpdata = &dio->reg_pdata[desc->id];
116 	int ret;
117 
118 	rpdata->en_gpiod = devm_fwnode_get_index_gpiod_from_child(dio->dev,
119 					"enable", 0, &np->fwnode,
120 					GPIOD_OUT_HIGH, "enable");
121 	if (IS_ERR_OR_NULL(rpdata->en_gpiod)) {
122 		ret = PTR_ERR(rpdata->en_gpiod);
123 
124 		/* Ignore the error other than probe defer */
125 		if (ret == -EPROBE_DEFER)
126 			return ret;
127 		return 0;
128 	}
129 
130 	return 0;
131 }
132 
133 #define DIO5632_REGULATOR_DESC(_id, _name)		\
134 	[DIO5632_REGULATOR_ID_##_id] = {		\
135 		.name = "DIO5632-"#_name,		\
136 		.supply_name = "vin",			\
137 		.id = DIO5632_REGULATOR_ID_##_id,	\
138 		.of_match = of_match_ptr(#_name),	\
139 		.of_parse_cb	= DIO5632_of_parse_cb,	\
140 		.ops = &DIO5632_regulator_ops,		\
141 		.n_voltages = DIO5632_VOUT_N_VOLTAGE,	\
142 		.min_uV = DIO5632_VOUT_VMIN,		\
143 		.uV_step = DIO5632_VOUT_STEP,		\
144 		.enable_time = 500,			\
145 		.vsel_mask = DIO5632_VOUT_MASK,	\
146 		.vsel_reg = DIO5632_REG_##_id,		\
147 		.active_discharge_off = 0,			\
148 		.active_discharge_on = DIO5632_REG_DIS_##_id, \
149 		.active_discharge_mask = DIO5632_REG_DIS_##_id, \
150 		.active_discharge_reg = DIO5632_REG_APPS_DISP_DISN, \
151 		.type = REGULATOR_VOLTAGE,		\
152 		.owner = THIS_MODULE,			\
153 	}
154 
155 static const struct regulator_desc dio_regs_desc[DIO5632_MAX_REGULATORS] = {
156 	DIO5632_REGULATOR_DESC(VPOS, outp),
157 	DIO5632_REGULATOR_DESC(VNEG, outn),
158 };
159 
160 static const struct regmap_range DIO5632_no_reg_ranges[] = {
161 	regmap_reg_range(DIO5632_REG_APPS_DISP_DISN + 1,
162 			 DIO5632_REG_CONTROL - 1),
163 };
164 
165 static const struct regmap_access_table DIO5632_no_reg_table = {
166 	.no_ranges = DIO5632_no_reg_ranges,
167 	.n_no_ranges = ARRAY_SIZE(DIO5632_no_reg_ranges),
168 };
169 
170 static const struct regmap_config DIO5632_regmap_config = {
171 	.reg_bits	= 8,
172 	.val_bits	= 8,
173 	.max_register	= DIO5632_REG_CONTROL,
174 	.cache_type	= REGCACHE_NONE,
175 	.rd_table	= &DIO5632_no_reg_table,
176 	.wr_table	= &DIO5632_no_reg_table,
177 };
178 
DIO5632_probe(struct i2c_client * client,const struct i2c_device_id * client_id)179 static int DIO5632_probe(struct i2c_client *client,
180 			  const struct i2c_device_id *client_id)
181 {
182 	struct device *dev = &client->dev;
183 	struct DIO5632_regulator *dio;
184 	struct regulator_dev *rdev;
185 	struct regmap *rmap;
186 	struct regulator_config config = { };
187 	int id;
188 	int ret;
189 
190 	dio = devm_kzalloc(dev, sizeof(*dio), GFP_KERNEL);
191 	if (!dio)
192 		return -ENOMEM;
193 
194 	rmap = devm_regmap_init_i2c(client, &DIO5632_regmap_config);
195 	if (IS_ERR(rmap)) {
196 		ret = PTR_ERR(rmap);
197 		dev_err(dev, "regmap init failed: %d\n", ret);
198 		return ret;
199 	}
200 
201 	i2c_set_clientdata(client, dio);
202 	dio->dev = dev;
203 
204 	for (id = 0; id < DIO5632_MAX_REGULATORS; ++id) {
205 		config.regmap = rmap;
206 		config.dev = dev;
207 		config.driver_data = dio;
208 
209 		rdev = devm_regulator_register(dev, &dio_regs_desc[id],
210 					       &config);
211 		if (IS_ERR(rdev)) {
212 			ret = PTR_ERR(rdev);
213 			dev_err(dev, "regulator %s register failed: %d\n",
214 				dio_regs_desc[id].name, ret);
215 			return ret;
216 		}
217 	}
218 	return 0;
219 }
220 
221 static const struct i2c_device_id DIO5632_id[] = {
222 	{.name = "DIO5632",},
223 	{},
224 };
225 MODULE_DEVICE_TABLE(i2c, DIO5632_id);
226 
227 static struct i2c_driver DIO5632_i2c_driver = {
228 	.driver = {
229 		.name = "DIO5632",
230 	},
231 	.probe = DIO5632_probe,
232 	.id_table = DIO5632_id,
233 };
234 
235 module_i2c_driver(DIO5632_i2c_driver);
236 
237 MODULE_DESCRIPTION("DIO5632 regulator driver");
238 MODULE_AUTHOR("Guochun Huang <hero.huang@rockchips.com>");
239 MODULE_LICENSE("GPL v2");
240