stm32mp_pmic.c 7.61 KB
Newer Older
Yann Gautier's avatar
Yann Gautier committed
1
/*
2
 * Copyright (c) 2017-2019, STMicroelectronics - All Rights Reserved
Yann Gautier's avatar
Yann Gautier committed
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <errno.h>
8

Yann Gautier's avatar
Yann Gautier committed
9
#include <libfdt.h>
10

Yann Gautier's avatar
Yann Gautier committed
11
#include <platform_def.h>
12
13
14

#include <common/debug.h>
#include <drivers/delay_timer.h>
15
#include <drivers/st/stm32_i2c.h>
16
17
#include <drivers/st/stm32mp_pmic.h>
#include <drivers/st/stpmic1.h>
18
19
20
#include <lib/mmio.h>
#include <lib/utils_def.h>

21
22
23
24
25
#define STPMIC1_LDO12356_OUTPUT_MASK	(uint8_t)(GENMASK(6, 2))
#define STPMIC1_LDO12356_OUTPUT_SHIFT	2
#define STPMIC1_LDO3_MODE		(uint8_t)(BIT(7))
#define STPMIC1_LDO3_DDR_SEL		31U
#define STPMIC1_LDO3_1800000		(9U << STPMIC1_LDO12356_OUTPUT_SHIFT)
Yann Gautier's avatar
Yann Gautier committed
26

27
28
#define STPMIC1_BUCK_OUTPUT_SHIFT	2
#define STPMIC1_BUCK3_1V8		(39U << STPMIC1_BUCK_OUTPUT_SHIFT)
Yann Gautier's avatar
Yann Gautier committed
29

30
#define STPMIC1_DEFAULT_START_UP_DELAY_MS	1
Yann Gautier's avatar
Yann Gautier committed
31
32
33
34
35
36

static struct i2c_handle_s i2c_handle;
static uint32_t pmic_i2c_addr;

static int dt_get_pmic_node(void *fdt)
{
37
	return fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
Yann Gautier's avatar
Yann Gautier committed
38
39
}

40
int dt_pmic_status(void)
Yann Gautier's avatar
Yann Gautier committed
41
42
43
44
45
{
	int node;
	void *fdt;

	if (fdt_get_address(&fdt) == 0) {
46
		return -ENOENT;
Yann Gautier's avatar
Yann Gautier committed
47
48
49
	}

	node = dt_get_pmic_node(fdt);
50
51
	if (node <= 0) {
		return -FDT_ERR_NOTFOUND;
Yann Gautier's avatar
Yann Gautier committed
52
53
	}

54
	return fdt_get_status(node);
Yann Gautier's avatar
Yann Gautier committed
55
56
}

57
58
59
60
61
62
/*
 * Get PMIC and its I2C bus configuration from the device tree.
 * Return 0 on success, negative on error, 1 if no PMIC node is found.
 */
static int dt_pmic_i2c_config(struct dt_node_info *i2c_info,
			      struct stm32_i2c_init_s *init)
Yann Gautier's avatar
Yann Gautier committed
63
64
65
66
67
68
69
70
71
72
73
{
	int pmic_node, i2c_node;
	void *fdt;
	const fdt32_t *cuint;

	if (fdt_get_address(&fdt) == 0) {
		return -ENOENT;
	}

	pmic_node = dt_get_pmic_node(fdt);
	if (pmic_node < 0) {
74
		return 1;
Yann Gautier's avatar
Yann Gautier committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	}

	cuint = fdt_getprop(fdt, pmic_node, "reg", NULL);
	if (cuint == NULL) {
		return -FDT_ERR_NOTFOUND;
	}

	pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1;
	if (pmic_i2c_addr > UINT16_MAX) {
		return -EINVAL;
	}

	i2c_node = fdt_parent_offset(fdt, pmic_node);
	if (i2c_node < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	dt_fill_device_info(i2c_info, i2c_node);
	if (i2c_info->base == 0U) {
		return -FDT_ERR_NOTFOUND;
	}

97
	return stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init);
Yann Gautier's avatar
Yann Gautier committed
98
99
}

100
int dt_pmic_configure_boot_on_regulators(void)
Yann Gautier's avatar
Yann Gautier committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{
	int pmic_node, regulators_node, regulator_node;
	void *fdt;

	if (fdt_get_address(&fdt) == 0) {
		return -ENOENT;
	}

	pmic_node = dt_get_pmic_node(fdt);
	if (pmic_node < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	regulators_node = fdt_subnode_offset(fdt, pmic_node, "regulators");

	fdt_for_each_subnode(regulator_node, fdt, regulators_node) {
		const fdt32_t *cuint;
118
		const char *node_name = fdt_get_name(fdt, regulator_node, NULL);
Yann Gautier's avatar
Yann Gautier committed
119
		uint16_t voltage;
120
121
122
123
124
125
126
127
		int status;

#if defined(IMAGE_BL2)
		if ((fdt_getprop(fdt, regulator_node, "regulator-boot-on",
				 NULL) == NULL) &&
		    (fdt_getprop(fdt, regulator_node, "regulator-always-on",
				 NULL) == NULL)) {
#else
Yann Gautier's avatar
Yann Gautier committed
128
129
		if (fdt_getprop(fdt, regulator_node, "regulator-boot-on",
				NULL) == NULL) {
130
#endif
Yann Gautier's avatar
Yann Gautier committed
131
132
133
			continue;
		}

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
		if (fdt_getprop(fdt, regulator_node, "regulator-pull-down",
				NULL) != NULL) {

			status = stpmic1_regulator_pull_down_set(node_name);
			if (status != 0) {
				return status;
			}
		}

		if (fdt_getprop(fdt, regulator_node, "st,mask-reset",
				NULL) != NULL) {

			status = stpmic1_regulator_mask_reset_set(node_name);
			if (status != 0) {
				return status;
			}
		}

Yann Gautier's avatar
Yann Gautier committed
152
153
154
155
156
157
158
159
160
		cuint = fdt_getprop(fdt, regulator_node,
				    "regulator-min-microvolt", NULL);
		if (cuint == NULL) {
			continue;
		}

		/* DT uses microvolts, whereas driver awaits millivolts */
		voltage = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U);

161
162
163
164
		status = stpmic1_regulator_voltage_set(node_name, voltage);
		if (status != 0) {
			return status;
		}
Yann Gautier's avatar
Yann Gautier committed
165

166
		if (stpmic1_is_regulator_enabled(node_name) == 0U) {
167
			status = stpmic1_regulator_enable(node_name);
Yann Gautier's avatar
Yann Gautier committed
168
169
170
171
172
173
174
175
176
			if (status != 0) {
				return status;
			}
		}
	}

	return 0;
}

177
bool initialize_pmic_i2c(void)
Yann Gautier's avatar
Yann Gautier committed
178
179
180
{
	int ret;
	struct dt_node_info i2c_info;
181
182
	struct i2c_handle_s *i2c = &i2c_handle;
	struct stm32_i2c_init_s i2c_init;
Yann Gautier's avatar
Yann Gautier committed
183

184
185
186
	ret = dt_pmic_i2c_config(&i2c_info, &i2c_init);
	if (ret < 0) {
		ERROR("I2C configuration failed %d\n", ret);
Yann Gautier's avatar
Yann Gautier committed
187
188
189
		panic();
	}

190
191
	if (ret != 0) {
		return false;
Yann Gautier's avatar
Yann Gautier committed
192
193
194
	}

	/* Initialize PMIC I2C */
195
196
197
198
199
200
201
202
203
204
205
206
207
208
	i2c->i2c_base_addr		= i2c_info.base;
	i2c->dt_status			= i2c_info.status;
	i2c->clock			= i2c_info.clock;
	i2c_init.own_address1		= pmic_i2c_addr;
	i2c_init.addressing_mode	= I2C_ADDRESSINGMODE_7BIT;
	i2c_init.dual_address_mode	= I2C_DUALADDRESS_DISABLE;
	i2c_init.own_address2		= 0;
	i2c_init.own_address2_masks	= I2C_OAR2_OA2NOMASK;
	i2c_init.general_call_mode	= I2C_GENERALCALL_DISABLE;
	i2c_init.no_stretch_mode	= I2C_NOSTRETCH_DISABLE;
	i2c_init.analog_filter		= 1;
	i2c_init.digital_filter_coef	= 0;

	ret = stm32_i2c_init(i2c, &i2c_init);
Yann Gautier's avatar
Yann Gautier committed
209
210
	if (ret != 0) {
		ERROR("Cannot initialize I2C %x (%d)\n",
211
		      i2c->i2c_base_addr, ret);
Yann Gautier's avatar
Yann Gautier committed
212
213
214
		panic();
	}

215
216
217
	if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, 1,
				       I2C_TIMEOUT_BUSY_MS)) {
		ERROR("I2C device not ready\n");
Yann Gautier's avatar
Yann Gautier committed
218
219
220
		panic();
	}

221
	stpmic1_bind_i2c(i2c, (uint16_t)pmic_i2c_addr);
Yann Gautier's avatar
Yann Gautier committed
222

223
	return true;
Yann Gautier's avatar
Yann Gautier committed
224
225
226
227
}

void initialize_pmic(void)
{
228
	unsigned long pmic_version;
Yann Gautier's avatar
Yann Gautier committed
229

230
231
232
233
	if (!initialize_pmic_i2c()) {
		VERBOSE("No PMIC\n");
		return;
	}
Yann Gautier's avatar
Yann Gautier committed
234

235
236
	if (stpmic1_get_version(&pmic_version) != 0) {
		ERROR("Failed to access PMIC\n");
Yann Gautier's avatar
Yann Gautier committed
237
238
239
		panic();
	}

240
241
	INFO("PMIC version = 0x%02lx\n", pmic_version);
	stpmic1_dump_regulators();
Yann Gautier's avatar
Yann Gautier committed
242

243
244
#if defined(IMAGE_BL2)
	if (dt_pmic_configure_boot_on_regulators() != 0) {
Yann Gautier's avatar
Yann Gautier committed
245
		panic();
246
247
	};
#endif
Yann Gautier's avatar
Yann Gautier committed
248
249
250
251
252
253
254
255
256
257
258
}

int pmic_ddr_power_init(enum ddr_type ddr_type)
{
	bool buck3_at_1v8 = false;
	uint8_t read_val;
	int status;

	switch (ddr_type) {
	case STM32MP_DDR3:
		/* Set LDO3 to sync mode */
259
		status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautier's avatar
Yann Gautier committed
260
261
262
263
		if (status != 0) {
			return status;
		}

264
265
266
267
		read_val &= ~STPMIC1_LDO3_MODE;
		read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
		read_val |= STPMIC1_LDO3_DDR_SEL <<
			    STPMIC1_LDO12356_OUTPUT_SHIFT;
Yann Gautier's avatar
Yann Gautier committed
268

269
		status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautier's avatar
Yann Gautier committed
270
271
272
273
		if (status != 0) {
			return status;
		}

274
		status = stpmic1_regulator_voltage_set("buck2", 1350);
Yann Gautier's avatar
Yann Gautier committed
275
276
277
278
		if (status != 0) {
			return status;
		}

279
		status = stpmic1_regulator_enable("buck2");
Yann Gautier's avatar
Yann Gautier committed
280
281
282
283
		if (status != 0) {
			return status;
		}

284
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
285

286
		status = stpmic1_regulator_enable("vref_ddr");
Yann Gautier's avatar
Yann Gautier committed
287
288
289
290
		if (status != 0) {
			return status;
		}

291
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
292

293
		status = stpmic1_regulator_enable("ldo3");
Yann Gautier's avatar
Yann Gautier committed
294
295
296
297
		if (status != 0) {
			return status;
		}

298
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
299
300
301
302
303
304
305
306
		break;

	case STM32MP_LPDDR2:
		/*
		 * Set LDO3 to 1.8V
		 * Set LDO3 to bypass mode if BUCK3 = 1.8V
		 * Set LDO3 to normal mode if BUCK3 != 1.8V
		 */
307
		status = stpmic1_register_read(BUCK3_CONTROL_REG, &read_val);
Yann Gautier's avatar
Yann Gautier committed
308
309
310
311
		if (status != 0) {
			return status;
		}

312
		if ((read_val & STPMIC1_BUCK3_1V8) == STPMIC1_BUCK3_1V8) {
Yann Gautier's avatar
Yann Gautier committed
313
314
315
			buck3_at_1v8 = true;
		}

316
		status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautier's avatar
Yann Gautier committed
317
318
319
320
		if (status != 0) {
			return status;
		}

321
322
323
		read_val &= ~STPMIC1_LDO3_MODE;
		read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
		read_val |= STPMIC1_LDO3_1800000;
Yann Gautier's avatar
Yann Gautier committed
324
		if (buck3_at_1v8) {
325
			read_val |= STPMIC1_LDO3_MODE;
Yann Gautier's avatar
Yann Gautier committed
326
327
		}

328
		status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautier's avatar
Yann Gautier committed
329
330
331
332
		if (status != 0) {
			return status;
		}

333
		status = stpmic1_regulator_voltage_set("buck2", 1200);
Yann Gautier's avatar
Yann Gautier committed
334
335
336
337
		if (status != 0) {
			return status;
		}

338
		status = stpmic1_regulator_enable("ldo3");
Yann Gautier's avatar
Yann Gautier committed
339
340
341
342
		if (status != 0) {
			return status;
		}

343
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
344

345
		status = stpmic1_regulator_enable("buck2");
Yann Gautier's avatar
Yann Gautier committed
346
347
348
349
		if (status != 0) {
			return status;
		}

350
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
351

352
		status = stpmic1_regulator_enable("vref_ddr");
Yann Gautier's avatar
Yann Gautier committed
353
354
355
356
		if (status != 0) {
			return status;
		}

357
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
358
359
360
361
362
363
364
365
		break;

	default:
		break;
	};

	return 0;
}