summaryrefslogtreecommitdiff
path: root/drivers/regulator/sc2731-regulator.c
blob: 5447e1a47d154f6656952831b4e7463eb94555c2 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2017 Spreadtrum Communications Inc.
 */

#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/of_regulator.h>

/*
 * SC2731 regulator lock register
 */
#define SC2731_PWR_WR_PROT		0xf0c
#define SC2731_WR_UNLOCK_VALUE		0x6e7f

/*
 * SC2731 enable register
 */
#define SC2731_POWER_PD_SW		0xc28
#define SC2731_LDO_CAMA0_PD		0xcfc
#define SC2731_LDO_CAMA1_PD		0xd04
#define SC2731_LDO_CAMMOT_PD		0xd0c
#define SC2731_LDO_VLDO_PD		0xd6c
#define SC2731_LDO_EMMCCORE_PD		0xd2c
#define SC2731_LDO_SDCORE_PD		0xd74
#define SC2731_LDO_SDIO_PD		0xd70
#define SC2731_LDO_WIFIPA_PD		0xd4c
#define SC2731_LDO_USB33_PD		0xd5c
#define SC2731_LDO_CAMD0_PD		0xd7c
#define SC2731_LDO_CAMD1_PD		0xd84
#define SC2731_LDO_CON_PD		0xd8c
#define SC2731_LDO_CAMIO_PD		0xd94
#define SC2731_LDO_SRAM_PD		0xd78

/*
 * SC2731 enable mask
 */
#define SC2731_DCDC_CPU0_PD_MASK	BIT(4)
#define SC2731_DCDC_CPU1_PD_MASK	BIT(3)
#define SC2731_DCDC_RF_PD_MASK		BIT(11)
#define SC2731_LDO_CAMA0_PD_MASK	BIT(0)
#define SC2731_LDO_CAMA1_PD_MASK	BIT(0)
#define SC2731_LDO_CAMMOT_PD_MASK	BIT(0)
#define SC2731_LDO_VLDO_PD_MASK		BIT(0)
#define SC2731_LDO_EMMCCORE_PD_MASK	BIT(0)
#define SC2731_LDO_SDCORE_PD_MASK	BIT(0)
#define SC2731_LDO_SDIO_PD_MASK		BIT(0)
#define SC2731_LDO_WIFIPA_PD_MASK	BIT(0)
#define SC2731_LDO_USB33_PD_MASK	BIT(0)
#define SC2731_LDO_CAMD0_PD_MASK	BIT(0)
#define SC2731_LDO_CAMD1_PD_MASK	BIT(0)
#define SC2731_LDO_CON_PD_MASK		BIT(0)
#define SC2731_LDO_CAMIO_PD_MASK	BIT(0)
#define SC2731_LDO_SRAM_PD_MASK		BIT(0)

/*
 * SC2731 vsel register
 */
#define SC2731_DCDC_CPU0_VOL		0xc54
#define SC2731_DCDC_CPU1_VOL		0xc64
#define SC2731_DCDC_RF_VOL		0xcb8
#define SC2731_LDO_CAMA0_VOL		0xd00
#define SC2731_LDO_CAMA1_VOL		0xd08
#define SC2731_LDO_CAMMOT_VOL		0xd10
#define SC2731_LDO_VLDO_VOL		0xd28
#define SC2731_LDO_EMMCCORE_VOL		0xd30
#define SC2731_LDO_SDCORE_VOL		0xd38
#define SC2731_LDO_SDIO_VOL		0xd40
#define SC2731_LDO_WIFIPA_VOL		0xd50
#define SC2731_LDO_USB33_VOL		0xd60
#define SC2731_LDO_CAMD0_VOL		0xd80
#define SC2731_LDO_CAMD1_VOL		0xd88
#define SC2731_LDO_CON_VOL		0xd90
#define SC2731_LDO_CAMIO_VOL		0xd98
#define SC2731_LDO_SRAM_VOL		0xdB0

/*
 * SC2731 vsel register mask
 */
#define SC2731_DCDC_CPU0_VOL_MASK	GENMASK(8, 0)
#define SC2731_DCDC_CPU1_VOL_MASK	GENMASK(8, 0)
#define SC2731_DCDC_RF_VOL_MASK		GENMASK(8, 0)
#define SC2731_LDO_CAMA0_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_CAMA1_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_CAMMOT_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_VLDO_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_EMMCCORE_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_SDCORE_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_SDIO_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_WIFIPA_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_USB33_VOL_MASK	GENMASK(7, 0)
#define SC2731_LDO_CAMD0_VOL_MASK	GENMASK(6, 0)
#define SC2731_LDO_CAMD1_VOL_MASK	GENMASK(6, 0)
#define SC2731_LDO_CON_VOL_MASK		GENMASK(6, 0)
#define SC2731_LDO_CAMIO_VOL_MASK	GENMASK(6, 0)
#define SC2731_LDO_SRAM_VOL_MASK	GENMASK(6, 0)

enum sc2731_regulator_id {
	SC2731_BUCK_CPU0,
	SC2731_BUCK_CPU1,
	SC2731_BUCK_RF,
	SC2731_LDO_CAMA0,
	SC2731_LDO_CAMA1,
	SC2731_LDO_CAMMOT,
	SC2731_LDO_VLDO,
	SC2731_LDO_EMMCCORE,
	SC2731_LDO_SDCORE,
	SC2731_LDO_SDIO,
	SC2731_LDO_WIFIPA,
	SC2731_LDO_USB33,
	SC2731_LDO_CAMD0,
	SC2731_LDO_CAMD1,
	SC2731_LDO_CON,
	SC2731_LDO_CAMIO,
	SC2731_LDO_SRAM,
};

static const struct regulator_ops sc2731_regu_linear_ops = {
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
	.is_enabled = regulator_is_enabled_regmap,
	.list_voltage = regulator_list_voltage_linear,
	.get_voltage_sel = regulator_get_voltage_sel_regmap,
	.set_voltage_sel = regulator_set_voltage_sel_regmap,
};

#define SC2731_REGU_LINEAR(_id, en_reg, en_mask, vreg, vmask,	\
			  vstep, vmin, vmax) {			\
	.name			= #_id,				\
	.of_match		= of_match_ptr(#_id),		\
	.ops			= &sc2731_regu_linear_ops,	\
	.type			= REGULATOR_VOLTAGE,		\
	.id			= SC2731_##_id,			\
	.owner			= THIS_MODULE,			\
	.min_uV			= vmin,				\
	.n_voltages		= ((vmax) - (vmin)) / (vstep) + 1,	\
	.uV_step		= vstep,			\
	.enable_is_inverted	= true,				\
	.enable_val		= 0,				\
	.enable_reg		= en_reg,			\
	.enable_mask		= en_mask,			\
	.vsel_reg		= vreg,				\
	.vsel_mask		= vmask,			\
}

static const struct regulator_desc regulators[] = {
	SC2731_REGU_LINEAR(BUCK_CPU0, SC2731_POWER_PD_SW,
			   SC2731_DCDC_CPU0_PD_MASK, SC2731_DCDC_CPU0_VOL,
			   SC2731_DCDC_CPU0_VOL_MASK, 3125, 400000, 1996875),
	SC2731_REGU_LINEAR(BUCK_CPU1, SC2731_POWER_PD_SW,
			   SC2731_DCDC_CPU1_PD_MASK, SC2731_DCDC_CPU1_VOL,
			   SC2731_DCDC_CPU1_VOL_MASK, 3125, 400000, 1996875),
	SC2731_REGU_LINEAR(BUCK_RF, SC2731_POWER_PD_SW, SC2731_DCDC_RF_PD_MASK,
			   SC2731_DCDC_RF_VOL, SC2731_DCDC_RF_VOL_MASK,
			   3125, 600000, 2196875),
	SC2731_REGU_LINEAR(LDO_CAMA0, SC2731_LDO_CAMA0_PD,
			   SC2731_LDO_CAMA0_PD_MASK, SC2731_LDO_CAMA0_VOL,
			   SC2731_LDO_CAMA0_VOL_MASK, 10000, 1200000, 3750000),
	SC2731_REGU_LINEAR(LDO_CAMA1, SC2731_LDO_CAMA1_PD,
			   SC2731_LDO_CAMA1_PD_MASK, SC2731_LDO_CAMA1_VOL,
			   SC2731_LDO_CAMA1_VOL_MASK, 10000, 1200000, 3750000),
	SC2731_REGU_LINEAR(LDO_CAMMOT, SC2731_LDO_CAMMOT_PD,
			   SC2731_LDO_CAMMOT_PD_MASK, SC2731_LDO_CAMMOT_VOL,
			   SC2731_LDO_CAMMOT_VOL_MASK, 10000, 1200000, 3750000),
	SC2731_REGU_LINEAR(LDO_VLDO, SC2731_LDO_VLDO_PD,
			   SC2731_LDO_VLDO_PD_MASK, SC2731_LDO_VLDO_VOL,
			   SC2731_LDO_VLDO_VOL_MASK, 10000, 1200000, 3750000),
	SC2731_REGU_LINEAR(LDO_EMMCCORE, SC2731_LDO_EMMCCORE_PD,
			   SC2731_LDO_EMMCCORE_PD_MASK, SC2731_LDO_EMMCCORE_VOL,
			   SC2731_LDO_EMMCCORE_VOL_MASK, 10000, 1200000,
			   3750000),
	SC2731_REGU_LINEAR(LDO_SDCORE, SC2731_LDO_SDCORE_PD,
			   SC2731_LDO_SDCORE_PD_MASK, SC2731_LDO_SDCORE_VOL,
			   SC2731_LDO_SDCORE_VOL_MASK, 10000, 1200000, 3750000),
	SC2731_REGU_LINEAR(LDO_SDIO, SC2731_LDO_SDIO_PD,
			   SC2731_LDO_SDIO_PD_MASK, SC2731_LDO_SDIO_VOL,
			   SC2731_LDO_SDIO_VOL_MASK, 10000, 1200000, 3750000),
	SC2731_REGU_LINEAR(LDO_WIFIPA, SC2731_LDO_WIFIPA_PD,
			   SC2731_LDO_WIFIPA_PD_MASK, SC2731_LDO_WIFIPA_VOL,
			   SC2731_LDO_WIFIPA_VOL_MASK, 10000, 1200000, 3750000),
	SC2731_REGU_LINEAR(LDO_USB33, SC2731_LDO_USB33_PD,
			   SC2731_LDO_USB33_PD_MASK, SC2731_LDO_USB33_VOL,
			   SC2731_LDO_USB33_VOL_MASK, 10000, 1200000, 3750000),
	SC2731_REGU_LINEAR(LDO_CAMD0, SC2731_LDO_CAMD0_PD,
			   SC2731_LDO_CAMD0_PD_MASK, SC2731_LDO_CAMD0_VOL,
			   SC2731_LDO_CAMD0_VOL_MASK, 6250, 1000000, 1793750),
	SC2731_REGU_LINEAR(LDO_CAMD1, SC2731_LDO_CAMD1_PD,
			   SC2731_LDO_CAMD1_PD_MASK, SC2731_LDO_CAMD1_VOL,
			   SC2731_LDO_CAMD1_VOL_MASK, 6250, 1000000, 1793750),
	SC2731_REGU_LINEAR(LDO_CON, SC2731_LDO_CON_PD,
			   SC2731_LDO_CON_PD_MASK, SC2731_LDO_CON_VOL,
			   SC2731_LDO_CON_VOL_MASK, 6250, 1000000, 1793750),
	SC2731_REGU_LINEAR(LDO_CAMIO, SC2731_LDO_CAMIO_PD,
			   SC2731_LDO_CAMIO_PD_MASK, SC2731_LDO_CAMIO_VOL,
			   SC2731_LDO_CAMIO_VOL_MASK, 6250, 1000000, 1793750),
	SC2731_REGU_LINEAR(LDO_SRAM, SC2731_LDO_SRAM_PD,
			   SC2731_LDO_SRAM_PD_MASK, SC2731_LDO_SRAM_VOL,
			   SC2731_LDO_SRAM_VOL_MASK, 6250, 1000000, 1793750),
};

static int sc2731_regulator_unlock(struct regmap *regmap)
{
	return regmap_write(regmap, SC2731_PWR_WR_PROT,
			    SC2731_WR_UNLOCK_VALUE);
}

static int sc2731_regulator_probe(struct platform_device *pdev)
{
	int i, ret;
	struct regmap *regmap;
	struct regulator_config config = { };
	struct regulator_dev *rdev;

	regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!regmap) {
		dev_err(&pdev->dev, "failed to get regmap.\n");
		return -ENODEV;
	}

	ret = sc2731_regulator_unlock(regmap);
	if (ret) {
		dev_err(&pdev->dev, "failed to release regulator lock\n");
		return ret;
	}

	config.dev = &pdev->dev;
	config.regmap = regmap;

	for (i = 0; i < ARRAY_SIZE(regulators); i++) {
		rdev = devm_regulator_register(&pdev->dev, &regulators[i],
					       &config);
		if (IS_ERR(rdev)) {
			dev_err(&pdev->dev, "failed to register regulator %s\n",
				regulators[i].name);
			return PTR_ERR(rdev);
		}
	}

	return 0;
}

static struct platform_driver sc2731_regulator_driver = {
	.driver = {
		.name = "sc27xx-regulator",
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
	.probe = sc2731_regulator_probe,
};

module_platform_driver(sc2731_regulator_driver);

MODULE_AUTHOR("Chen Junhui <erick.chen@spreadtrum.com>");
MODULE_DESCRIPTION("Spreadtrum SC2731 regulator driver");
MODULE_LICENSE("GPL v2");