summaryrefslogtreecommitdiff
path: root/drivers/regulator/rtq6752-regulator.c
blob: 609d3fcf4923d2a8df3ac8268ed6d49d34dad75b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: GPL-2.0+

#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>

enum {
	RTQ6752_IDX_PAVDD = 0,
	RTQ6752_IDX_NAVDD = 1,
	RTQ6752_IDX_MAX
};

#define RTQ6752_REG_PAVDD	0x00
#define RTQ6752_REG_NAVDD	0x01
#define RTQ6752_REG_PAVDDONDLY	0x07
#define RTQ6752_REG_PAVDDSSTIME	0x08
#define RTQ6752_REG_NAVDDONDLY	0x0D
#define RTQ6752_REG_NAVDDSSTIME	0x0E
#define RTQ6752_REG_OPTION1	0x12
#define RTQ6752_REG_CHSWITCH	0x16
#define RTQ6752_REG_FAULT	0x1D

#define RTQ6752_VOUT_MASK	GENMASK(5, 0)
#define RTQ6752_NAVDDEN_MASK	BIT(3)
#define RTQ6752_PAVDDEN_MASK	BIT(0)
#define RTQ6752_PAVDDAD_MASK	BIT(4)
#define RTQ6752_NAVDDAD_MASK	BIT(3)
#define RTQ6752_PAVDDF_MASK	BIT(3)
#define RTQ6752_NAVDDF_MASK	BIT(0)
#define RTQ6752_ENABLE_MASK	(BIT(RTQ6752_IDX_MAX) - 1)

#define RTQ6752_VOUT_MINUV	5000000
#define RTQ6752_VOUT_STEPUV	50000
#define RTQ6752_VOUT_NUM	47
#define RTQ6752_I2CRDY_TIMEUS	1000
#define RTQ6752_MINSS_TIMEUS	5000

struct rtq6752_priv {
	struct regmap *regmap;
	struct gpio_desc *enable_gpio;
	struct mutex lock;
	unsigned char enable_flag;
};

static int rtq6752_set_vdd_enable(struct regulator_dev *rdev)
{
	struct rtq6752_priv *priv = rdev_get_drvdata(rdev);
	int rid = rdev_get_id(rdev), ret;

	mutex_lock(&priv->lock);
	if (priv->enable_gpio) {
		gpiod_set_value(priv->enable_gpio, 1);

		usleep_range(RTQ6752_I2CRDY_TIMEUS,
			     RTQ6752_I2CRDY_TIMEUS + 100);
	}

	if (!priv->enable_flag) {
		regcache_cache_only(priv->regmap, false);
		ret = regcache_sync(priv->regmap);
		if (ret) {
			mutex_unlock(&priv->lock);
			return ret;
		}
	}

	priv->enable_flag |= BIT(rid);
	mutex_unlock(&priv->lock);

	return regulator_enable_regmap(rdev);
}

static int rtq6752_set_vdd_disable(struct regulator_dev *rdev)
{
	struct rtq6752_priv *priv = rdev_get_drvdata(rdev);
	int rid = rdev_get_id(rdev), ret;

	ret = regulator_disable_regmap(rdev);
	if (ret)
		return ret;

	mutex_lock(&priv->lock);
	priv->enable_flag &= ~BIT(rid);

	if (!priv->enable_flag) {
		regcache_cache_only(priv->regmap, true);
		regcache_mark_dirty(priv->regmap);
	}

	if (priv->enable_gpio)
		gpiod_set_value(priv->enable_gpio, 0);

	mutex_unlock(&priv->lock);

	return 0;
}

static int rtq6752_get_error_flags(struct regulator_dev *rdev,
				   unsigned int *flags)
{
	unsigned int val, events = 0;
	const unsigned int fault_mask[] = {
		RTQ6752_PAVDDF_MASK, RTQ6752_NAVDDF_MASK };
	int rid = rdev_get_id(rdev), ret;

	ret = regmap_read(rdev->regmap, RTQ6752_REG_FAULT, &val);
	if (ret)
		return ret;

	if (val & fault_mask[rid])
		events = REGULATOR_ERROR_REGULATION_OUT;

	*flags = events;
	return 0;
}

static const struct regulator_ops rtq6752_regulator_ops = {
	.list_voltage = regulator_list_voltage_linear,
	.set_voltage_sel = regulator_set_voltage_sel_regmap,
	.get_voltage_sel = regulator_get_voltage_sel_regmap,
	.enable = rtq6752_set_vdd_enable,
	.disable = rtq6752_set_vdd_disable,
	.is_enabled = regulator_is_enabled_regmap,
	.set_active_discharge = regulator_set_active_discharge_regmap,
	.get_error_flags = rtq6752_get_error_flags,
};

static const struct regulator_desc rtq6752_regulator_descs[] = {
	{
		.name = "rtq6752-pavdd",
		.of_match = of_match_ptr("pavdd"),
		.regulators_node = of_match_ptr("regulators"),
		.id = RTQ6752_IDX_PAVDD,
		.n_voltages = RTQ6752_VOUT_NUM,
		.ops = &rtq6752_regulator_ops,
		.owner = THIS_MODULE,
		.min_uV = RTQ6752_VOUT_MINUV,
		.uV_step = RTQ6752_VOUT_STEPUV,
		.enable_time = RTQ6752_MINSS_TIMEUS,
		.vsel_reg = RTQ6752_REG_PAVDD,
		.vsel_mask = RTQ6752_VOUT_MASK,
		.enable_reg = RTQ6752_REG_CHSWITCH,
		.enable_mask = RTQ6752_PAVDDEN_MASK,
		.active_discharge_reg = RTQ6752_REG_OPTION1,
		.active_discharge_mask = RTQ6752_PAVDDAD_MASK,
		.active_discharge_off = RTQ6752_PAVDDAD_MASK,
	},
	{
		.name = "rtq6752-navdd",
		.of_match = of_match_ptr("navdd"),
		.regulators_node = of_match_ptr("regulators"),
		.id = RTQ6752_IDX_NAVDD,
		.n_voltages = RTQ6752_VOUT_NUM,
		.ops = &rtq6752_regulator_ops,
		.owner = THIS_MODULE,
		.min_uV = RTQ6752_VOUT_MINUV,
		.uV_step = RTQ6752_VOUT_STEPUV,
		.enable_time = RTQ6752_MINSS_TIMEUS,
		.vsel_reg = RTQ6752_REG_NAVDD,
		.vsel_mask = RTQ6752_VOUT_MASK,
		.enable_reg = RTQ6752_REG_CHSWITCH,
		.enable_mask = RTQ6752_NAVDDEN_MASK,
		.active_discharge_reg = RTQ6752_REG_OPTION1,
		.active_discharge_mask = RTQ6752_NAVDDAD_MASK,
		.active_discharge_off = RTQ6752_NAVDDAD_MASK,
	}
};

static int rtq6752_init_device_properties(struct rtq6752_priv *priv)
{
	u8 raw_vals[] = { 0, 0 };
	int ret;

	/* Configure PAVDD on and softstart delay time to the minimum */
	ret = regmap_raw_write(priv->regmap, RTQ6752_REG_PAVDDONDLY, raw_vals,
			       ARRAY_SIZE(raw_vals));
	if (ret)
		return ret;

	/* Configure NAVDD on and softstart delay time to the minimum */
	return regmap_raw_write(priv->regmap, RTQ6752_REG_NAVDDONDLY, raw_vals,
				ARRAY_SIZE(raw_vals));
}

static bool rtq6752_is_volatile_reg(struct device *dev, unsigned int reg)
{
	if (reg == RTQ6752_REG_FAULT)
		return true;
	return false;
}

static const struct reg_default rtq6752_reg_defaults[] = {
	{ RTQ6752_REG_PAVDD, 0x14 },
	{ RTQ6752_REG_NAVDD, 0x14 },
	{ RTQ6752_REG_PAVDDONDLY, 0x01 },
	{ RTQ6752_REG_PAVDDSSTIME, 0x01 },
	{ RTQ6752_REG_NAVDDONDLY, 0x01 },
	{ RTQ6752_REG_NAVDDSSTIME, 0x01 },
	{ RTQ6752_REG_OPTION1, 0x07 },
	{ RTQ6752_REG_CHSWITCH, 0x29 },
};

static const struct regmap_config rtq6752_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.cache_type = REGCACHE_RBTREE,
	.max_register = RTQ6752_REG_FAULT,
	.reg_defaults = rtq6752_reg_defaults,
	.num_reg_defaults = ARRAY_SIZE(rtq6752_reg_defaults),
	.volatile_reg = rtq6752_is_volatile_reg,
};

static int rtq6752_probe(struct i2c_client *i2c)
{
	struct rtq6752_priv *priv;
	struct regulator_config reg_cfg = {};
	struct regulator_dev *rdev;
	int i, ret;

	priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	mutex_init(&priv->lock);

	priv->enable_gpio = devm_gpiod_get_optional(&i2c->dev, "enable",
						    GPIOD_OUT_HIGH);
	if (IS_ERR(priv->enable_gpio)) {
		dev_err(&i2c->dev, "Failed to get 'enable' gpio\n");
		return PTR_ERR(priv->enable_gpio);
	}

	usleep_range(RTQ6752_I2CRDY_TIMEUS, RTQ6752_I2CRDY_TIMEUS + 100);
	/* Default EN pin to high, PAVDD and NAVDD will be on */
	priv->enable_flag = RTQ6752_ENABLE_MASK;

	priv->regmap = devm_regmap_init_i2c(i2c, &rtq6752_regmap_config);
	if (IS_ERR(priv->regmap)) {
		dev_err(&i2c->dev, "Failed to init regmap\n");
		return PTR_ERR(priv->regmap);
	}

	ret = rtq6752_init_device_properties(priv);
	if (ret) {
		dev_err(&i2c->dev, "Failed to init device properties\n");
		return ret;
	}

	reg_cfg.dev = &i2c->dev;
	reg_cfg.regmap = priv->regmap;
	reg_cfg.driver_data = priv;

	for (i = 0; i < ARRAY_SIZE(rtq6752_regulator_descs); i++) {
		rdev = devm_regulator_register(&i2c->dev,
					       rtq6752_regulator_descs + i,
					       &reg_cfg);
		if (IS_ERR(rdev)) {
			dev_err(&i2c->dev, "Failed to init %d regulator\n", i);
			return PTR_ERR(rdev);
		}
	}

	return 0;
}

static const struct of_device_id __maybe_unused rtq6752_device_table[] = {
	{ .compatible = "richtek,rtq6752", },
	{}
};
MODULE_DEVICE_TABLE(of, rtq6752_device_table);

static struct i2c_driver rtq6752_driver = {
	.driver = {
		.name = "rtq6752",
		.of_match_table = rtq6752_device_table,
	},
	.probe_new = rtq6752_probe,
};
module_i2c_driver(rtq6752_driver);

MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
MODULE_DESCRIPTION("Richtek RTQ6752 Regulator Driver");
MODULE_LICENSE("GPL v2");