xref: /openbmc/linux/drivers/regulator/aw37503-regulator.c (revision 2796a01cdf2c639e605088c53a1ac36923ade93c)
1*2796a01cSAlec Li // SPDX-License-Identifier: GPL-2.0
2*2796a01cSAlec Li //
3*2796a01cSAlec Li // AWINIC AW37503 Regulator Driver
4*2796a01cSAlec Li //
5*2796a01cSAlec Li // Copyright (C) 2023 awinic. All Rights Reserved
6*2796a01cSAlec Li //
7*2796a01cSAlec Li // Author: <like@awinic.com>
8*2796a01cSAlec Li 
9*2796a01cSAlec Li #include <linux/err.h>
10*2796a01cSAlec Li #include <linux/gpio/consumer.h>
11*2796a01cSAlec Li #include <linux/i2c.h>
12*2796a01cSAlec Li #include <linux/module.h>
13*2796a01cSAlec Li #include <linux/regmap.h>
14*2796a01cSAlec Li #include <linux/regulator/driver.h>
15*2796a01cSAlec Li #include <linux/regulator/machine.h>
16*2796a01cSAlec Li 
17*2796a01cSAlec Li #define AW37503_REG_VPOS		0x00
18*2796a01cSAlec Li #define AW37503_REG_VNEG		0x01
19*2796a01cSAlec Li #define AW37503_REG_APPS		0x03
20*2796a01cSAlec Li #define AW37503_REG_CONTROL		0x04
21*2796a01cSAlec Li #define AW37503_REG_WPRTEN		0x21
22*2796a01cSAlec Li 
23*2796a01cSAlec Li #define AW37503_VOUT_MASK		0x1F
24*2796a01cSAlec Li #define AW37503_VOUT_N_VOLTAGE		0x15
25*2796a01cSAlec Li #define AW37503_VOUT_VMIN		4000000
26*2796a01cSAlec Li #define AW37503_VOUT_VMAX		6000000
27*2796a01cSAlec Li #define AW37503_VOUT_STEP		100000
28*2796a01cSAlec Li 
29*2796a01cSAlec Li #define AW37503_REG_APPS_DIS_VPOS	BIT(1)
30*2796a01cSAlec Li #define AW37503_REG_APPS_DIS_VNEG	BIT(0)
31*2796a01cSAlec Li 
32*2796a01cSAlec Li #define AW37503_REGULATOR_ID_VPOS	0
33*2796a01cSAlec Li #define AW37503_REGULATOR_ID_VNEG	1
34*2796a01cSAlec Li #define AW37503_MAX_REGULATORS		2
35*2796a01cSAlec Li 
36*2796a01cSAlec Li struct aw37503_reg_pdata {
37*2796a01cSAlec Li 	struct gpio_desc *en_gpiod;
38*2796a01cSAlec Li 	int ena_gpio_state;
39*2796a01cSAlec Li };
40*2796a01cSAlec Li 
41*2796a01cSAlec Li struct aw37503_regulator {
42*2796a01cSAlec Li 	struct device *dev;
43*2796a01cSAlec Li 	struct aw37503_reg_pdata reg_pdata[AW37503_MAX_REGULATORS];
44*2796a01cSAlec Li };
45*2796a01cSAlec Li 
46*2796a01cSAlec Li static int aw37503_regulator_enable(struct regulator_dev *rdev)
47*2796a01cSAlec Li {
48*2796a01cSAlec Li 	struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
49*2796a01cSAlec Li 	int id = rdev_get_id(rdev);
50*2796a01cSAlec Li 	struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
51*2796a01cSAlec Li 	int ret;
52*2796a01cSAlec Li 
53*2796a01cSAlec Li 	if (!IS_ERR(rpdata->en_gpiod)) {
54*2796a01cSAlec Li 		gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
55*2796a01cSAlec Li 		rpdata->ena_gpio_state = 1;
56*2796a01cSAlec Li 	}
57*2796a01cSAlec Li 
58*2796a01cSAlec Li 	/* Hardware automatically enable discharge bit in enable */
59*2796a01cSAlec Li 	if (rdev->constraints->active_discharge ==
60*2796a01cSAlec Li 			REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
61*2796a01cSAlec Li 		ret = regulator_set_active_discharge_regmap(rdev, false);
62*2796a01cSAlec Li 		if (ret < 0) {
63*2796a01cSAlec Li 			dev_err(chip->dev, "Failed to disable active discharge: %d\n",
64*2796a01cSAlec Li 				ret);
65*2796a01cSAlec Li 			return ret;
66*2796a01cSAlec Li 		}
67*2796a01cSAlec Li 	}
68*2796a01cSAlec Li 
69*2796a01cSAlec Li 	return 0;
70*2796a01cSAlec Li }
71*2796a01cSAlec Li 
72*2796a01cSAlec Li static int aw37503_regulator_disable(struct regulator_dev *rdev)
73*2796a01cSAlec Li {
74*2796a01cSAlec Li 	struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
75*2796a01cSAlec Li 	int id = rdev_get_id(rdev);
76*2796a01cSAlec Li 	struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
77*2796a01cSAlec Li 
78*2796a01cSAlec Li 	if (!IS_ERR(rpdata->en_gpiod)) {
79*2796a01cSAlec Li 		gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
80*2796a01cSAlec Li 		rpdata->ena_gpio_state = 0;
81*2796a01cSAlec Li 	}
82*2796a01cSAlec Li 
83*2796a01cSAlec Li 	return 0;
84*2796a01cSAlec Li }
85*2796a01cSAlec Li 
86*2796a01cSAlec Li static int aw37503_regulator_is_enabled(struct regulator_dev *rdev)
87*2796a01cSAlec Li {
88*2796a01cSAlec Li 	struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
89*2796a01cSAlec Li 	int id = rdev_get_id(rdev);
90*2796a01cSAlec Li 	struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
91*2796a01cSAlec Li 
92*2796a01cSAlec Li 	if (!IS_ERR(rpdata->en_gpiod))
93*2796a01cSAlec Li 		return rpdata->ena_gpio_state;
94*2796a01cSAlec Li 
95*2796a01cSAlec Li 	return 1;
96*2796a01cSAlec Li }
97*2796a01cSAlec Li 
98*2796a01cSAlec Li static const struct regulator_ops aw37503_regulator_ops = {
99*2796a01cSAlec Li 	.enable = aw37503_regulator_enable,
100*2796a01cSAlec Li 	.disable = aw37503_regulator_disable,
101*2796a01cSAlec Li 	.is_enabled = aw37503_regulator_is_enabled,
102*2796a01cSAlec Li 	.list_voltage = regulator_list_voltage_linear,
103*2796a01cSAlec Li 	.map_voltage = regulator_map_voltage_linear,
104*2796a01cSAlec Li 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
105*2796a01cSAlec Li 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
106*2796a01cSAlec Li 	.set_active_discharge = regulator_set_active_discharge_regmap,
107*2796a01cSAlec Li };
108*2796a01cSAlec Li 
109*2796a01cSAlec Li static int aw37503_of_parse_cb(struct device_node *np,
110*2796a01cSAlec Li 				const struct regulator_desc *desc,
111*2796a01cSAlec Li 				struct regulator_config *config)
112*2796a01cSAlec Li {
113*2796a01cSAlec Li 	struct aw37503_regulator *chip = config->driver_data;
114*2796a01cSAlec Li 	struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[desc->id];
115*2796a01cSAlec Li 	int ret;
116*2796a01cSAlec Li 
117*2796a01cSAlec Li 	rpdata->en_gpiod = devm_fwnode_gpiod_get(chip->dev, of_fwnode_handle(np),
118*2796a01cSAlec Li 						 "enable", GPIOD_OUT_LOW,
119*2796a01cSAlec Li 						 "enable");
120*2796a01cSAlec Li 
121*2796a01cSAlec Li 	if (IS_ERR(rpdata->en_gpiod)) {
122*2796a01cSAlec Li 		ret = PTR_ERR(rpdata->en_gpiod);
123*2796a01cSAlec Li 
124*2796a01cSAlec Li 		/* Ignore the error other than probe defer */
125*2796a01cSAlec Li 		if (ret == -EPROBE_DEFER)
126*2796a01cSAlec Li 			return ret;
127*2796a01cSAlec Li 		return 0;
128*2796a01cSAlec Li 	}
129*2796a01cSAlec Li 
130*2796a01cSAlec Li 	return 0;
131*2796a01cSAlec Li }
132*2796a01cSAlec Li 
133*2796a01cSAlec Li #define AW37503_REGULATOR_DESC(_id, _name)		\
134*2796a01cSAlec Li 	[AW37503_REGULATOR_ID_##_id] = {		\
135*2796a01cSAlec Li 		.name = "aw37503-"#_name,		\
136*2796a01cSAlec Li 		.supply_name = "vin",			\
137*2796a01cSAlec Li 		.id = AW37503_REGULATOR_ID_##_id,	\
138*2796a01cSAlec Li 		.of_match = of_match_ptr(#_name),	\
139*2796a01cSAlec Li 		.of_parse_cb = aw37503_of_parse_cb,	\
140*2796a01cSAlec Li 		.ops = &aw37503_regulator_ops,		\
141*2796a01cSAlec Li 		.n_voltages = AW37503_VOUT_N_VOLTAGE,	\
142*2796a01cSAlec Li 		.min_uV = AW37503_VOUT_VMIN,		\
143*2796a01cSAlec Li 		.uV_step = AW37503_VOUT_STEP,		\
144*2796a01cSAlec Li 		.enable_time = 500,			\
145*2796a01cSAlec Li 		.vsel_mask = AW37503_VOUT_MASK,	\
146*2796a01cSAlec Li 		.vsel_reg = AW37503_REG_##_id,		\
147*2796a01cSAlec Li 		.active_discharge_off = 0,			\
148*2796a01cSAlec Li 		.active_discharge_on = AW37503_REG_APPS_DIS_##_id, \
149*2796a01cSAlec Li 		.active_discharge_mask = AW37503_REG_APPS_DIS_##_id, \
150*2796a01cSAlec Li 		.active_discharge_reg = AW37503_REG_APPS, \
151*2796a01cSAlec Li 		.type = REGULATOR_VOLTAGE,		\
152*2796a01cSAlec Li 		.owner = THIS_MODULE,			\
153*2796a01cSAlec Li 	}
154*2796a01cSAlec Li 
155*2796a01cSAlec Li static const struct regulator_desc aw_regs_desc[AW37503_MAX_REGULATORS] = {
156*2796a01cSAlec Li 	AW37503_REGULATOR_DESC(VPOS, outp),
157*2796a01cSAlec Li 	AW37503_REGULATOR_DESC(VNEG, outn),
158*2796a01cSAlec Li };
159*2796a01cSAlec Li 
160*2796a01cSAlec Li static const struct regmap_range aw37503_no_reg_ranges[] = {
161*2796a01cSAlec Li 	regmap_reg_range(AW37503_REG_CONTROL + 1,
162*2796a01cSAlec Li 			 AW37503_REG_WPRTEN - 1),
163*2796a01cSAlec Li };
164*2796a01cSAlec Li 
165*2796a01cSAlec Li static const struct regmap_access_table aw37503_no_reg_table = {
166*2796a01cSAlec Li 	.no_ranges = aw37503_no_reg_ranges,
167*2796a01cSAlec Li 	.n_no_ranges = ARRAY_SIZE(aw37503_no_reg_ranges),
168*2796a01cSAlec Li };
169*2796a01cSAlec Li 
170*2796a01cSAlec Li static const struct regmap_config aw37503_regmap_config = {
171*2796a01cSAlec Li 	.reg_bits	= 8,
172*2796a01cSAlec Li 	.val_bits	= 8,
173*2796a01cSAlec Li 	.max_register	= AW37503_REG_WPRTEN,
174*2796a01cSAlec Li 	.rd_table	= &aw37503_no_reg_table,
175*2796a01cSAlec Li 	.wr_table	= &aw37503_no_reg_table,
176*2796a01cSAlec Li };
177*2796a01cSAlec Li 
178*2796a01cSAlec Li static int aw37503_probe(struct i2c_client *client)
179*2796a01cSAlec Li {
180*2796a01cSAlec Li 	struct device *dev = &client->dev;
181*2796a01cSAlec Li 	struct aw37503_regulator *chip;
182*2796a01cSAlec Li 	struct regulator_dev *rdev;
183*2796a01cSAlec Li 	struct regmap *regmap;
184*2796a01cSAlec Li 	struct regulator_config config = { };
185*2796a01cSAlec Li 	int id;
186*2796a01cSAlec Li 
187*2796a01cSAlec Li 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
188*2796a01cSAlec Li 	if (!chip)
189*2796a01cSAlec Li 		return -ENOMEM;
190*2796a01cSAlec Li 
191*2796a01cSAlec Li 	regmap = devm_regmap_init_i2c(client, &aw37503_regmap_config);
192*2796a01cSAlec Li 	if (IS_ERR(regmap))
193*2796a01cSAlec Li 		return dev_err_probe(dev, PTR_ERR(regmap),
194*2796a01cSAlec Li 				     "Failed to init regmap\n");
195*2796a01cSAlec Li 
196*2796a01cSAlec Li 	i2c_set_clientdata(client, chip);
197*2796a01cSAlec Li 	chip->dev = dev;
198*2796a01cSAlec Li 
199*2796a01cSAlec Li 	config.regmap = regmap;
200*2796a01cSAlec Li 	config.dev = dev;
201*2796a01cSAlec Li 	config.driver_data = chip;
202*2796a01cSAlec Li 
203*2796a01cSAlec Li 	for (id = 0; id < AW37503_MAX_REGULATORS; ++id) {
204*2796a01cSAlec Li 		rdev = devm_regulator_register(dev, &aw_regs_desc[id],
205*2796a01cSAlec Li 					       &config);
206*2796a01cSAlec Li 		if (IS_ERR(rdev))
207*2796a01cSAlec Li 			return dev_err_probe(dev, PTR_ERR(rdev),
208*2796a01cSAlec Li 					     "Failed to register regulator %s\n",
209*2796a01cSAlec Li 					     aw_regs_desc[id].name);
210*2796a01cSAlec Li 	}
211*2796a01cSAlec Li 	return 0;
212*2796a01cSAlec Li }
213*2796a01cSAlec Li 
214*2796a01cSAlec Li static const struct i2c_device_id aw37503_id[] = {
215*2796a01cSAlec Li 	{.name = "aw37503",},
216*2796a01cSAlec Li 	{},
217*2796a01cSAlec Li };
218*2796a01cSAlec Li MODULE_DEVICE_TABLE(i2c, aw37503_id);
219*2796a01cSAlec Li 
220*2796a01cSAlec Li static const struct of_device_id aw37503_of_match[] = {
221*2796a01cSAlec Li 	{.compatible = "awinic,aw37503",},
222*2796a01cSAlec Li 	{ /* Sentinel */ },
223*2796a01cSAlec Li };
224*2796a01cSAlec Li 
225*2796a01cSAlec Li MODULE_DEVICE_TABLE(of, aw37503_of_match);
226*2796a01cSAlec Li 
227*2796a01cSAlec Li static struct i2c_driver aw37503_i2c_driver = {
228*2796a01cSAlec Li 	.driver = {
229*2796a01cSAlec Li 		.name = "aw37503",
230*2796a01cSAlec Li 		.of_match_table = aw37503_of_match,
231*2796a01cSAlec Li 	},
232*2796a01cSAlec Li 	.probe_new = aw37503_probe,
233*2796a01cSAlec Li 	.id_table = aw37503_id,
234*2796a01cSAlec Li };
235*2796a01cSAlec Li 
236*2796a01cSAlec Li module_i2c_driver(aw37503_i2c_driver);
237*2796a01cSAlec Li 
238*2796a01cSAlec Li MODULE_DESCRIPTION("aw37503 regulator driver");
239*2796a01cSAlec Li MODULE_AUTHOR("Alec Li <like@awinic.com>");
240*2796a01cSAlec Li MODULE_LICENSE("GPL");
241