summaryrefslogtreecommitdiffstats
path: root/src/soc/mediatek/mt8195/mt6360.c
blob: 729478e521d99ef8c3f7f21041f2a472508fdcbf (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* SPDX-License-Identifier: GPL-2.0-only */

#include <console/console.h>
#include <device/i2c_simple.h>
#include <soc/mt6360.h>
#include <stdbool.h>

static struct mt6360_i2c_data i2c_data[] = {
	[MT6360_INDEX_LDO] = {
		.addr = MT6360_LDO_I2C_ADDR,
	},
	[MT6360_INDEX_PMIC] = {
		.addr = MT6360_PMIC_I2C_ADDR,
	},
};

static const uint32_t mt6360_ldo1_vsel_table[0x10] = {
	[0x4] = 1800000,
	[0x5] = 2000000,
	[0x6] = 2100000,
	[0x7] = 2500000,
	[0x8] = 2700000,
	[0x9] = 2800000,
	[0xA] = 2900000,
	[0xB] = 3000000,
	[0xC] = 3100000,
	[0xD] = 3300000,
};

static const uint32_t mt6360_ldo3_vsel_table[0x10] = {
	[0x4] = 1800000,
	[0xA] = 2900000,
	[0xB] = 3000000,
	[0xD] = 3300000,
};

static const uint32_t mt6360_ldo5_vsel_table[0x10] = {
	[0x2] = 2900000,
	[0x3] = 3000000,
	[0x5] = 3300000,
};

static const struct mt6360_data regulator_data[MT6360_REGULATOR_COUNT] = {
	[MT6360_LDO3]  = MT6360_DATA(0x05, 0x40, 0x09, 0xff, mt6360_ldo3_vsel_table),
	[MT6360_LDO5]  = MT6360_DATA(0x0b, 0x40, 0x0f, 0xff, mt6360_ldo5_vsel_table),
	[MT6360_LDO6]  = MT6360_DATA(0x37, 0x40, 0x3b, 0xff, mt6360_ldo3_vsel_table),
	[MT6360_LDO7]  = MT6360_DATA(0x31, 0x40, 0x35, 0xff, mt6360_ldo5_vsel_table),
	[MT6360_BUCK1] = MT6360_DATA(0x17, 0x40, 0x10, 0xff, mt6360_ldo1_vsel_table),
	[MT6360_BUCK2] = MT6360_DATA(0x27, 0x40, 0x20, 0xff, mt6360_ldo1_vsel_table),
	[MT6360_LDO1]  = MT6360_DATA(0x17, 0x40, 0x1b, 0xff, mt6360_ldo1_vsel_table),
	[MT6360_LDO2]  = MT6360_DATA(0x11, 0x40, 0x15, 0xff, mt6360_ldo1_vsel_table),
};

#define CRC8_TABLE_SIZE 256
static u8 crc8_table[MT6360_INDEX_COUNT][CRC8_TABLE_SIZE];

static u8 crc8(const u8 table[CRC8_TABLE_SIZE], u8 *pdata, size_t nbytes)
{
	u8 crc = 0;

	while (nbytes-- > 0)
		crc = table[(crc ^ *pdata++) & 0xff];

	return crc;
}

static void crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial)
{
	int i, j;
	const u8 msbit = 0x80;
	u8 t = msbit;

	table[0] = 0;

	for (i = 1; i < CRC8_TABLE_SIZE; i *= 2) {
		t = (t << 1) ^ (t & msbit ? polynomial : 0);
		for (j = 0; j < i; j++)
			table[i + j] = table[j] ^ t;
	}
}

static int mt6360_i2c_write_byte(u8 index, u8 reg, u8 data)
{
	u8 chunk[5] = { 0 };
	struct mt6360_i2c_data *i2c = &i2c_data[index];

	if ((reg & 0xc0) != 0) {
		printk(BIOS_ERR, "%s: not support reg [%#x]\n", __func__, reg);
		return -1;
	}

	/*
	 * chunk[0], dev address
	 * chunk[1], reg address
	 * chunk[2], data to write
	 * chunk[3], crc of chunk[0 ~ 2]
	 * chunk[4], blank
	 */
	chunk[0] = (i2c->addr & 0x7f) << 1;
	chunk[1] = (reg & 0x3f);
	chunk[2] = data;
	chunk[3] = crc8(crc8_table[index], chunk, 3);

	return i2c_write_raw(i2c->bus, i2c->addr, &chunk[1], 4);
}

static int mt6360_i2c_read_byte(u8 index, u8 reg, u8 *data)
{
	u8 chunk[4] = { 0 };
	u8 buf[2];
	int ret;
	u8 crc;
	struct mt6360_i2c_data *i2c = &i2c_data[index];

	ret = i2c_read_bytes(i2c->bus, i2c->addr, reg & 0x3f, buf, 2);

	if (ret)
		return ret;
	/*
	 * chunk[0], dev address
	 * chunk[1], reg address
	 * chunk[2], received data
	 * chunk[3], received crc of chunk[0 ~ 2]
	 */
	chunk[0] = ((i2c->addr & 0x7f) << 1) + 1;
	chunk[1] = (reg & 0x3f);
	chunk[2] = buf[0];
	chunk[3] = buf[1];
	crc = crc8(crc8_table[index], chunk, 3);

	if (chunk[3] != crc) {
		printk(BIOS_ERR, "%s: incorrect CRC: expected %#x, got %#x",
		       __func__, crc, chunk[3]);
		return -1;
	}

	*data = chunk[2];

	return 0;
}

static int mt6360_read_interface(u8 index, u8 reg, u8 *data, u8 mask, u8 shift)
{
	int ret;
	u8 val = 0;

	ret = mt6360_i2c_read_byte(index, reg, &val);
	if (ret < 0) {
		printk(BIOS_ERR, "%s: fail, reg = %#x, ret = %d\n",
		       __func__, reg, ret);
		return ret;
	}
	val &= (mask << shift);
	*data = (val >> shift);
	return 0;
}

static int mt6360_config_interface(u8 index, u8 reg, u8 data, u8 mask, u8 shift)
{
	int ret;
	u8 val = 0;

	ret = mt6360_i2c_read_byte(index, reg, &val);
	if (ret < 0) {
		printk(BIOS_ERR, "%s: fail, reg = %#x, ret = %d\n",
		       __func__, reg, ret);
		return ret;
	}
	val &= ~(mask << shift);
	val |= (data << shift);

	return mt6360_i2c_write_byte(index, reg, val);
}

static bool is_valid_ldo(enum mt6360_regulator_id id)
{
	if (id != MT6360_LDO1 && id != MT6360_LDO2 &&
	    id != MT6360_LDO3 && id != MT6360_LDO5) {
		printk(BIOS_ERR, "%s: LDO %d is not supported\n", __func__, id);
		return false;
	}

	return true;
}

static bool is_valid_pmic(enum mt6360_regulator_id id)
{
	if (id != MT6360_LDO6 && id != MT6360_LDO7 &&
	    id != MT6360_BUCK1 && id != MT6360_BUCK2) {
		printk(BIOS_ERR, "%s: PMIC %d is not supported\n", __func__, id);
		return false;
	}

	return true;
}

static void mt6360_ldo_enable(enum mt6360_regulator_id id, uint8_t enable)
{
	u8 val;
	const struct mt6360_data *data;

	if (!is_valid_ldo(id))
		return;

	data = &regulator_data[id];

	if (mt6360_read_interface(MT6360_INDEX_LDO, data->enable_reg, &val, 0xff, 0) < 0)
		return;

	if (enable)
		val |= data->enable_mask;
	else
		val &= ~(data->enable_mask);

	mt6360_config_interface(MT6360_INDEX_LDO, data->enable_reg, val, 0xff, 0);
}

static uint8_t mt6360_ldo_is_enabled(enum mt6360_regulator_id id)
{
	u8 val;
	const struct mt6360_data *data;

	if (!is_valid_ldo(id))
		return 0;

	data = &regulator_data[id];

	if (mt6360_read_interface(MT6360_INDEX_LDO, data->enable_reg, &val, 0xff, 0) < 0)
		return 0;

	return (val & data->enable_mask) ? 1 : 0;
}

static void mt6360_ldo_set_voltage(enum mt6360_regulator_id id, u32 voltage_uv)
{
	u8 val = 0;
	u32 voltage_uv_temp = 0;
	int i;

	const struct mt6360_data *data;

	if (!is_valid_ldo(id))
		return;

	data = &regulator_data[id];

	for (i = 0; i < data->vsel_table_len; i++) {
		u32 uv = data->vsel_table[i];

		if (uv == 0)
			continue;
		if (uv > voltage_uv)
			break;

		val = i << 4;
		voltage_uv_temp = voltage_uv - uv;
	}

	if (val == 0) {
		printk(BIOS_ERR, "%s: LDO %d, set %d uV not supported\n",
		       __func__, id, voltage_uv);
		return;
	}

	voltage_uv_temp /= 10000;
	voltage_uv_temp = MIN(voltage_uv_temp, 0xa);
	val |= (u8)voltage_uv_temp;

	mt6360_config_interface(MT6360_INDEX_LDO, data->vsel_reg, val, 0xff, 0);
}

static u32 mt6360_ldo_get_voltage(enum mt6360_regulator_id id)
{
	u8 val;
	u32 voltage_uv;

	const struct mt6360_data *data;

	if (!is_valid_ldo(id))
		return 0;

	data = &regulator_data[id];

	if (mt6360_read_interface(MT6360_INDEX_LDO, data->vsel_reg, &val, 0xff, 0) < 0)
		return 0;

	voltage_uv = data->vsel_table[(val & 0xf0) >> 4];
	if (voltage_uv == 0) {
		printk(BIOS_ERR, "%s: LDO %d read fail, reg = %#x\n", __func__, id, val);
		return 0;
	}

	val = MIN(val & 0x0f, 0x0a);
	voltage_uv += val * 10000;

	return voltage_uv;
}

static void mt6360_pmic_enable(enum mt6360_regulator_id id, uint8_t enable)
{
	u8 val;
	const struct mt6360_data *data;

	if (!is_valid_pmic(id))
		return;

	data = &regulator_data[id];

	if (mt6360_read_interface(MT6360_INDEX_PMIC, data->enable_reg, &val, 0xff, 0) < 0)
		return;

	if (enable)
		val |= data->enable_mask;
	else
		val &= ~(data->enable_mask);

	mt6360_config_interface(MT6360_INDEX_PMIC, data->enable_reg, val, 0xff, 0);
}

static uint8_t mt6360_pmic_is_enabled(enum mt6360_regulator_id id)
{
	u8 val;
	const struct mt6360_data *data;

	if (!is_valid_pmic(id))
		return 0;

	data = &regulator_data[id];

	if (mt6360_read_interface(MT6360_INDEX_PMIC, data->enable_reg, &val, 0xff, 0) < 0)
		return 0;

	return (val & data->enable_mask) ? 1 : 0;
}

static void mt6360_pmic_set_voltage(enum mt6360_regulator_id id, u32 voltage_uv)
{
	u8 val = 0;

	const struct mt6360_data *data;

	if (!is_valid_pmic(id))
		return;

	data = &regulator_data[id];

	if (id == MT6360_BUCK1 || id == MT6360_BUCK2) {
		val = (voltage_uv - 300000) / 5000;
	} else if (id == MT6360_LDO6 || id == MT6360_LDO7) {
		val = (((voltage_uv - 500000) / 100000) << 4);
		val += (((voltage_uv - 500000) % 100000) / 10000);
	}

	mt6360_config_interface(MT6360_INDEX_PMIC, data->vsel_reg, val, 0xff, 0);
}

static u32 mt6360_pmic_get_voltage(enum mt6360_regulator_id id)
{
	u8 val;
	u32 voltage_uv = 0;

	const struct mt6360_data *data;

	if (!is_valid_pmic(id))
		return 0;

	data = &regulator_data[id];

	if (mt6360_read_interface(MT6360_INDEX_PMIC, data->vsel_reg, &val, 0xff, 0) < 0)
		return 0;

	if (id == MT6360_BUCK1 || id == MT6360_BUCK2) {
		voltage_uv = 300000 + val * 5000;
	} else if (id == MT6360_LDO6 || id == MT6360_LDO7) {
		voltage_uv = 500000 + 100000 * (val >> 4);
		voltage_uv += MIN(val & 0xf, 0xa) * 10000;
	}

	return voltage_uv;
}

void mt6360_init(uint8_t bus)
{
	u8 delay01, delay02, delay03, delay04;

	crc8_populate_msb(crc8_table[MT6360_INDEX_LDO], 0x7);
	crc8_populate_msb(crc8_table[MT6360_INDEX_PMIC], 0x7);

	i2c_data[MT6360_INDEX_LDO].bus = bus;
	i2c_data[MT6360_INDEX_PMIC].bus = bus;

	mt6360_config_interface(MT6360_INDEX_PMIC, 0x07, 0x04, 0xff, 0);
	mt6360_config_interface(MT6360_INDEX_PMIC, 0x08, 0x00, 0xff, 0);
	mt6360_config_interface(MT6360_INDEX_PMIC, 0x09, 0x02, 0xff, 0);
	mt6360_config_interface(MT6360_INDEX_PMIC, 0x0a, 0x00, 0xff, 0);

	mt6360_read_interface(MT6360_INDEX_PMIC, 0x07, &delay01, 0xff, 0);
	mt6360_read_interface(MT6360_INDEX_PMIC, 0x08, &delay02, 0xff, 0);
	mt6360_read_interface(MT6360_INDEX_PMIC, 0x09, &delay03, 0xff, 0);
	mt6360_read_interface(MT6360_INDEX_PMIC, 0x0a, &delay04, 0xff, 0);
	printk(BIOS_DEBUG,
	       "%s: power off sequence delay: %#x, %#x, %#x, %#x\n",
	       __func__, delay01, delay02, delay03, delay04);
}

void mt6360_enable(enum mt6360_regulator_id id, uint8_t enable)
{
	if (is_valid_ldo(id))
		mt6360_ldo_enable(id, enable);
	else if (is_valid_pmic(id))
		mt6360_pmic_enable(id, enable);
}

uint8_t mt6360_is_enabled(enum mt6360_regulator_id id)
{
	if (is_valid_ldo(id))
		return mt6360_ldo_is_enabled(id);
	else if (is_valid_pmic(id))
		return mt6360_pmic_is_enabled(id);
	else
		return 0;
}

void mt6360_set_voltage(enum mt6360_regulator_id id, u32 voltage_uv)
{
	if (is_valid_ldo(id))
		mt6360_ldo_set_voltage(id, voltage_uv);
	else if (is_valid_pmic(id))
		mt6360_pmic_set_voltage(id, voltage_uv);
}

u32 mt6360_get_voltage(enum mt6360_regulator_id id)
{
	if (is_valid_ldo(id))
		return mt6360_ldo_get_voltage(id);
	else if (is_valid_pmic(id))
		return mt6360_pmic_get_voltage(id);
	else
		return 0;
}