summaryrefslogtreecommitdiff
path: root/drivers/regulator/sgm3804-regulator.c
blob: c3406cfb73d031f319dd1a8212e273a3d49297c1 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// SPDX-License-Identifier: GPL-2.0-only
//
// SGMicro SGM3804 regulator Driver
//
// Copyright (C) 2025 Kancy Joe <kancy2333@outlook.com>
// Copyright (C) 2026 Linaro Limited
// Author: Neil Armstrong <neil.armstrong@linaro.org>

#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/of_regulator.h>
#include <linux/gpio/consumer.h>

#define SGM3804_POS_RAIL_VOLTAGE_REG	0x0
#define SGM3804_NEG_RAIL_VOLTAGE_REG	0x1
#define SGM3804_RAIL_DISCHARGE_REG	0x3

#define RAIL_VOLTAGE_MASK	GENMASK(5, 0)

#define POS_RAIL_DISCHARGE_EN	BIT(1)
#define NEG_RAIL_DISCHARGE_EN	BIT(0)

#define RAIL_VOLTAGE_INVALID		RAIL_VOLTAGE_MASK
#define RAIL_DISCHARGE_REG_DEFAULT	(POS_RAIL_DISCHARGE_EN | NEG_RAIL_DISCHARGE_EN)

#define SGM3804_VOLTAGES_MAX_SELECTOR	0x2f

enum {
	SGM3804_POS_RAIL = 0,
	SGM3804_NEG_RAIL,
	SGM3804_RAIL_COUNT,
};

/*
 * The registers are only writable when the gpio is enabled, so
 * we need to use the cache for read operations and set the regmap
 * as cache_only when both GPIOs are down.
 */
struct sgm3804_data {
	struct regmap *regmap;
	/* Protects the regcache state update */
	struct mutex lock;
	struct gpio_desc *gpios[SGM3804_RAIL_COUNT];
};

static const struct linear_range sgm3804_voltages[] = {
	REGULATOR_LINEAR_RANGE(2400000, 0x20, 0x2f, 100000),
	REGULATOR_LINEAR_RANGE(4000000, 0x00, 0x17, 100000),
};

/*
 * The cache is populated with those hardware default values
 * so the regmap_update_bits operation will use the cached
 * value to build a new register value and write it when GPIOs
 * are enabled.
 */
static const struct reg_default sgm3804_reg_defaults[] = {
	{ SGM3804_POS_RAIL_VOLTAGE_REG, RAIL_VOLTAGE_INVALID },
	{ SGM3804_NEG_RAIL_VOLTAGE_REG, RAIL_VOLTAGE_INVALID },
	{ SGM3804_RAIL_DISCHARGE_REG, RAIL_DISCHARGE_REG_DEFAULT },
};

/* Registers are only writable */
static bool sgm3804_writeable_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case SGM3804_POS_RAIL_VOLTAGE_REG:
	case SGM3804_NEG_RAIL_VOLTAGE_REG:
	case SGM3804_RAIL_DISCHARGE_REG:
		return true;
	default:
		return false;
	}
}

/*
 * Since all registers are only writeable, regmap will only read from the cache data.
 */
static bool sgm3804_readable_reg(struct device *dev, unsigned int reg)
{
	return false;
}

static const struct regmap_config sgm3804_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.max_register = 0x03,
	.writeable_reg = sgm3804_writeable_reg,
	.readable_reg = sgm3804_readable_reg,
	.cache_type = REGCACHE_MAPLE,
	.reg_defaults = sgm3804_reg_defaults,
	.num_reg_defaults = ARRAY_SIZE(sgm3804_reg_defaults),
};

static int sgm3804_sync_regcache_state(struct sgm3804_data *ctx)
{
	guard(mutex)(&ctx->lock);

	/* If both GPIOs are down, IC is powered down and I2C writes will fail */
	if (!gpiod_get_value_cansleep(ctx->gpios[SGM3804_POS_RAIL]) &&
	    !gpiod_get_value_cansleep(ctx->gpios[SGM3804_NEG_RAIL])) {
		regcache_cache_only(ctx->regmap, true);
		regcache_mark_dirty(ctx->regmap);
	} else {
		int ret;

		/* At least a GPIO is up, we can write registers */
		regcache_cache_only(ctx->regmap, false);
		ret = regcache_sync(ctx->regmap);
		if (ret) {
			regcache_cache_only(ctx->regmap, true);
			return ret;
		}
	}

	return 0;
}

static int sgm3804_get_voltage_sel(struct regulator_dev *rdev)
{
	int ret;

	ret = regulator_get_voltage_sel_regmap(rdev);
	if (ret < 0)
		return ret;

	/* Force setting a voltage on probe */
	if (ret == RAIL_VOLTAGE_INVALID)
		return -ENOTRECOVERABLE;

	return ret;
}

static int sgm3804_enable(struct regulator_dev *rdev)
{
	struct sgm3804_data *ctx = rdev->reg_data;
	int ret;

	ret = gpiod_set_value_cansleep(ctx->gpios[rdev_get_id(rdev)], 1);
	if (ret)
		return ret;

	ret = sgm3804_sync_regcache_state(ctx);
	if (ret)
		goto err;

	return 0;

err:
	gpiod_set_value_cansleep(ctx->gpios[rdev_get_id(rdev)], 0);
	return ret;
}

static int sgm3804_disable(struct regulator_dev *rdev)
{
	struct sgm3804_data *ctx = rdev->reg_data;
	int ret;

	ret = gpiod_set_value_cansleep(ctx->gpios[rdev_get_id(rdev)], 0);
	if (ret)
		return ret;

	return sgm3804_sync_regcache_state(ctx);
}

static int sgm3804_is_enabled(struct regulator_dev *rdev)
{
	struct sgm3804_data *ctx = rdev->reg_data;

	return gpiod_get_value_cansleep(ctx->gpios[rdev_get_id(rdev)]);
}

static const struct regulator_ops sgm3804_ops = {
	.list_voltage = regulator_list_voltage_linear_range,
	.map_voltage = regulator_map_voltage_linear_range,
	.set_voltage_sel = regulator_set_voltage_sel_regmap,
	.get_voltage_sel = sgm3804_get_voltage_sel,
	.set_active_discharge = regulator_set_active_discharge_regmap,
	.enable = sgm3804_enable,
	.disable = sgm3804_disable,
	.is_enabled = sgm3804_is_enabled,
};

static const struct regulator_desc sgm3804_regulator_desc[] = {
	/* Positive Output */
	{
		.name = "pos",
		.of_match = "pos",
		.supply_name = "vin",
		.id = SGM3804_POS_RAIL,
		.ops = &sgm3804_ops,
		.type = REGULATOR_VOLTAGE,
		.linear_ranges = sgm3804_voltages,
		.n_linear_ranges = ARRAY_SIZE(sgm3804_voltages),
		.n_voltages = SGM3804_VOLTAGES_MAX_SELECTOR + 1,
		.vsel_reg = SGM3804_POS_RAIL_VOLTAGE_REG,
		.vsel_mask = RAIL_VOLTAGE_MASK,
		.active_discharge_on = POS_RAIL_DISCHARGE_EN,
		.active_discharge_mask = POS_RAIL_DISCHARGE_EN,
		.active_discharge_reg = SGM3804_RAIL_DISCHARGE_REG,
		.enable_time = 40000,
		.owner = THIS_MODULE,
	},
	/* Negative Output */
	{
		.name = "neg",
		.of_match = "neg",
		.supply_name = "vin",
		.id = SGM3804_NEG_RAIL,
		.ops = &sgm3804_ops,
		.type = REGULATOR_VOLTAGE,
		.linear_ranges = sgm3804_voltages,
		.n_linear_ranges = ARRAY_SIZE(sgm3804_voltages),
		.n_voltages = SGM3804_VOLTAGES_MAX_SELECTOR + 1,
		.vsel_reg = SGM3804_NEG_RAIL_VOLTAGE_REG,
		.vsel_mask = RAIL_VOLTAGE_MASK,
		.active_discharge_on = NEG_RAIL_DISCHARGE_EN,
		.active_discharge_mask = NEG_RAIL_DISCHARGE_EN,
		.active_discharge_reg = SGM3804_RAIL_DISCHARGE_REG,
		.enable_time = 40000,
		.owner = THIS_MODULE,
	},
};

static int sgm3804_probe(struct i2c_client *i2c)
{
	struct device *dev = &i2c->dev;
	struct sgm3804_data *ctx;
	int ret, i;

	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

	mutex_init(&ctx->lock);

	ctx->regmap = devm_regmap_init_i2c(i2c, &sgm3804_regmap_config);
	if (IS_ERR(ctx->regmap))
		return dev_err_probe(dev, PTR_ERR(ctx->regmap),
				     "failed to init regmap\n");

	/* Get enable GPIOs */
	for (i = 0; i < ARRAY_SIZE(sgm3804_regulator_desc); i++) {
		const struct regulator_desc *reg = &sgm3804_regulator_desc[i];
		struct fwnode_handle *child;

		child = device_get_named_child_node(dev, reg->of_match);
		if (!child) {
			dev_err(dev, "missing child '%s'\n", reg->of_match);
			return -EINVAL;
		}

		ctx->gpios[i] = devm_fwnode_gpiod_get(dev, child, "enable",
						      GPIOD_ASIS, reg->name);
		fwnode_handle_put(child);
		if (IS_ERR(ctx->gpios[i]))
			return dev_err_probe(dev, PTR_ERR(ctx->gpios[i]),
					     "failed to get '%s' enable GPIO\n",
					     reg->name);
	}

	ret = sgm3804_sync_regcache_state(ctx);
	if (ret)
		return ret;

	for (i = 0; i < ARRAY_SIZE(sgm3804_regulator_desc); i++) {
		struct regulator_config config = { };
		struct regulator_dev *rdev;

		config.dev = dev;
		config.regmap = ctx->regmap;
		config.of_node = dev_of_node(dev);
		config.driver_data = ctx;
		rdev = devm_regulator_register(dev, &sgm3804_regulator_desc[i],
					       &config);
		if (IS_ERR(rdev))
			return dev_err_probe(dev, PTR_ERR(rdev),
					     "failed to register regulator %d\n", i);
	}

	return 0;
}

static const struct i2c_device_id sgm3804_id[] = {
	{ "sgm3804" },
	{ }
};
MODULE_DEVICE_TABLE(i2c, sgm3804_id);

static const struct of_device_id sgm3804_of_match[] = {
	{ .compatible = "sgmicro,sgm3804" },
	{ }
};
MODULE_DEVICE_TABLE(of, sgm3804_of_match);

static struct i2c_driver sgm3804_regulator_driver = {
	.driver = {
		.name = "sgm3804",
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
		.of_match_table = sgm3804_of_match,
	},
	.probe = sgm3804_probe,
	.id_table = sgm3804_id,
};

module_i2c_driver(sgm3804_regulator_driver);

MODULE_DESCRIPTION("SGMicro SGM3804 regulator Driver");
MODULE_AUTHOR("Kancy Joe <kancy2333@outlook.com>");
MODULE_AUTHOR("Neil Armstrong <neil.armstrong@linaro.org>");
MODULE_LICENSE("GPL");