stm32mp_pmic.c 7.28 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
9
#include <stdbool.h>

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

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

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

Yann Gautier's avatar
Yann Gautier committed
22
23
24
25
26
27
28
/* I2C Timing hard-coded value, for I2C clock source is HSI at 64MHz */
#define I2C_TIMING			0x10D07DB5

#define I2C_TIMEOUT			0xFFFFF

#define MASK_RESET_BUCK3		BIT(2)

29
30
31
32
33
#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
34

35
36
#define STPMIC1_BUCK_OUTPUT_SHIFT	2
#define STPMIC1_BUCK3_1V8		(39U << STPMIC1_BUCK_OUTPUT_SHIFT)
Yann Gautier's avatar
Yann Gautier committed
37

38
#define STPMIC1_DEFAULT_START_UP_DELAY_MS	1
Yann Gautier's avatar
Yann Gautier committed
39
40
41
42
43
44

static struct i2c_handle_s i2c_handle;
static uint32_t pmic_i2c_addr;

static int dt_get_pmic_node(void *fdt)
{
45
	return fdt_node_offset_by_compatible(fdt, -1, "st,stpmic1");
Yann Gautier's avatar
Yann Gautier committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
}

bool dt_check_pmic(void)
{
	int node;
	void *fdt;

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

	node = dt_get_pmic_node(fdt);
	if (node < 0) {
		VERBOSE("%s: No PMIC node found in DT\n", __func__);
		return false;
	}

63
	return fdt_get_status(node);
Yann Gautier's avatar
Yann Gautier committed
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
}

static int dt_pmic_i2c_config(struct dt_node_info *i2c_info)
{
	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) {
		return -FDT_ERR_NOTFOUND;
	}

	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;
	}

	return dt_set_pinctrl_config(i2c_node);
}

int dt_pmic_enable_boot_on_regulators(void)
{
	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;
		const char *node_name;
		uint16_t voltage;

		if (fdt_getprop(fdt, regulator_node, "regulator-boot-on",
				NULL) == NULL) {
			continue;
		}

		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);
		node_name = fdt_get_name(fdt, regulator_node, NULL);

140
		if (stpmic1_is_regulator_enabled(node_name) == 0U) {
Yann Gautier's avatar
Yann Gautier committed
141
142
			int status;

143
144
			status = stpmic1_regulator_voltage_set(node_name,
							       voltage);
Yann Gautier's avatar
Yann Gautier committed
145
146
147
148
			if (status != 0) {
				return status;
			}

149
			status = stpmic1_regulator_enable(node_name);
Yann Gautier's avatar
Yann Gautier committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
			if (status != 0) {
				return status;
			}
		}
	}

	return 0;
}

void initialize_pmic_i2c(void)
{
	int ret;
	struct dt_node_info i2c_info;

	if (dt_pmic_i2c_config(&i2c_info) != 0) {
		ERROR("I2C configuration failed\n");
		panic();
	}

169
	if (stm32mp_clk_enable((uint32_t)i2c_info.clock) < 0) {
Yann Gautier's avatar
Yann Gautier committed
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
		ERROR("I2C clock enable failed\n");
		panic();
	}

	/* Initialize PMIC I2C */
	i2c_handle.i2c_base_addr		= i2c_info.base;
	i2c_handle.i2c_init.timing		= I2C_TIMING;
	i2c_handle.i2c_init.own_address1	= pmic_i2c_addr;
	i2c_handle.i2c_init.addressing_mode	= I2C_ADDRESSINGMODE_7BIT;
	i2c_handle.i2c_init.dual_address_mode	= I2C_DUALADDRESS_DISABLE;
	i2c_handle.i2c_init.own_address2	= 0;
	i2c_handle.i2c_init.own_address2_masks	= I2C_OAR2_OA2NOMASK;
	i2c_handle.i2c_init.general_call_mode	= I2C_GENERALCALL_DISABLE;
	i2c_handle.i2c_init.no_stretch_mode	= I2C_NOSTRETCH_DISABLE;

	ret = stm32_i2c_init(&i2c_handle);
	if (ret != 0) {
		ERROR("Cannot initialize I2C %x (%d)\n",
		      i2c_handle.i2c_base_addr, ret);
		panic();
	}

	ret = stm32_i2c_config_analog_filter(&i2c_handle,
					     I2C_ANALOGFILTER_ENABLE);
	if (ret != 0) {
		ERROR("Cannot initialize I2C analog filter (%d)\n", ret);
		panic();
	}

	ret = stm32_i2c_is_device_ready(&i2c_handle, (uint16_t)pmic_i2c_addr, 1,
					I2C_TIMEOUT);
	if (ret != 0) {
		ERROR("I2C device not ready (%d)\n", ret);
		panic();
	}

206
	stpmic1_bind_i2c(&i2c_handle, (uint16_t)pmic_i2c_addr);
Yann Gautier's avatar
Yann Gautier committed
207
208
209
210
211
212
213
214
215
}

void initialize_pmic(void)
{
	int status;
	uint8_t read_val;

	initialize_pmic_i2c();

216
	status = stpmic1_register_read(VERSION_STATUS_REG, &read_val);
Yann Gautier's avatar
Yann Gautier committed
217
218
219
220
221
222
223
	if (status != 0) {
		panic();
	}

	INFO("PMIC version = 0x%x\n", read_val);

	/* Keep VDD on during the reset cycle */
224
	status = stpmic1_register_update(MASK_RESET_BUCK_REG,
Yann Gautier's avatar
Yann Gautier committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
					MASK_RESET_BUCK3,
					MASK_RESET_BUCK3);
	if (status != 0) {
		panic();
	}
}

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 */
241
		status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautier's avatar
Yann Gautier committed
242
243
244
245
		if (status != 0) {
			return status;
		}

246
247
248
249
		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
250

251
		status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautier's avatar
Yann Gautier committed
252
253
254
255
		if (status != 0) {
			return status;
		}

256
		status = stpmic1_regulator_voltage_set("buck2", 1350);
Yann Gautier's avatar
Yann Gautier committed
257
258
259
260
		if (status != 0) {
			return status;
		}

261
		status = stpmic1_regulator_enable("buck2");
Yann Gautier's avatar
Yann Gautier committed
262
263
264
265
		if (status != 0) {
			return status;
		}

266
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
267

268
		status = stpmic1_regulator_enable("vref_ddr");
Yann Gautier's avatar
Yann Gautier committed
269
270
271
272
		if (status != 0) {
			return status;
		}

273
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
274

275
		status = stpmic1_regulator_enable("ldo3");
Yann Gautier's avatar
Yann Gautier committed
276
277
278
279
		if (status != 0) {
			return status;
		}

280
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
281
282
283
284
285
286
287
288
		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
		 */
289
		status = stpmic1_register_read(BUCK3_CONTROL_REG, &read_val);
Yann Gautier's avatar
Yann Gautier committed
290
291
292
293
		if (status != 0) {
			return status;
		}

294
		if ((read_val & STPMIC1_BUCK3_1V8) == STPMIC1_BUCK3_1V8) {
Yann Gautier's avatar
Yann Gautier committed
295
296
297
			buck3_at_1v8 = true;
		}

298
		status = stpmic1_register_read(LDO3_CONTROL_REG, &read_val);
Yann Gautier's avatar
Yann Gautier committed
299
300
301
302
		if (status != 0) {
			return status;
		}

303
304
305
		read_val &= ~STPMIC1_LDO3_MODE;
		read_val &= ~STPMIC1_LDO12356_OUTPUT_MASK;
		read_val |= STPMIC1_LDO3_1800000;
Yann Gautier's avatar
Yann Gautier committed
306
		if (buck3_at_1v8) {
307
			read_val |= STPMIC1_LDO3_MODE;
Yann Gautier's avatar
Yann Gautier committed
308
309
		}

310
		status = stpmic1_register_write(LDO3_CONTROL_REG, read_val);
Yann Gautier's avatar
Yann Gautier committed
311
312
313
314
		if (status != 0) {
			return status;
		}

315
		status = stpmic1_regulator_voltage_set("buck2", 1200);
Yann Gautier's avatar
Yann Gautier committed
316
317
318
319
		if (status != 0) {
			return status;
		}

320
		status = stpmic1_regulator_enable("ldo3");
Yann Gautier's avatar
Yann Gautier committed
321
322
323
324
		if (status != 0) {
			return status;
		}

325
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
326

327
		status = stpmic1_regulator_enable("buck2");
Yann Gautier's avatar
Yann Gautier committed
328
329
330
331
		if (status != 0) {
			return status;
		}

332
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
333

334
		status = stpmic1_regulator_enable("vref_ddr");
Yann Gautier's avatar
Yann Gautier committed
335
336
337
338
		if (status != 0) {
			return status;
		}

339
		mdelay(STPMIC1_DEFAULT_START_UP_DELAY_MS);
Yann Gautier's avatar
Yann Gautier committed
340
341
342
343
344
345
346
347
		break;

	default:
		break;
	};

	return 0;
}