1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2019 MediaTek Inc.
5 #include <linux/mfd/mt6358/registers.h>
6 #include <linux/mfd/mt6397/core.h>
7 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/driver.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/mt6358-regulator.h>
14 #include <linux/regulator/of_regulator.h>
16 #define MT6358_BUCK_MODE_AUTO 0
17 #define MT6358_BUCK_MODE_FORCE_PWM 1
20 * MT6358 regulators' information
22 * @desc: standard fields of regulator description.
23 * @qi: Mask for query enable signal status of regulators
25 struct mt6358_regulator_info {
26 struct regulator_desc desc;
29 const u32 *index_table;
37 #define MT6358_BUCK(match, vreg, min, max, step, \
38 volt_ranges, vosel_mask, _da_vsel_reg, _da_vsel_mask, \
39 _modeset_reg, _modeset_shift) \
40 [MT6358_ID_##vreg] = { \
43 .of_match = of_match_ptr(match), \
44 .ops = &mt6358_volt_range_ops, \
45 .type = REGULATOR_VOLTAGE, \
46 .id = MT6358_ID_##vreg, \
47 .owner = THIS_MODULE, \
48 .n_voltages = ((max) - (min)) / (step) + 1, \
49 .linear_ranges = volt_ranges, \
50 .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
51 .vsel_reg = MT6358_BUCK_##vreg##_ELR0, \
52 .vsel_mask = vosel_mask, \
53 .enable_reg = MT6358_BUCK_##vreg##_CON0, \
54 .enable_mask = BIT(0), \
55 .of_map_mode = mt6358_map_mode, \
57 .status_reg = MT6358_BUCK_##vreg##_DBG1, \
59 .da_vsel_reg = _da_vsel_reg, \
60 .da_vsel_mask = _da_vsel_mask, \
61 .modeset_reg = _modeset_reg, \
62 .modeset_mask = BIT(_modeset_shift), \
65 #define MT6358_LDO(match, vreg, ldo_volt_table, \
66 ldo_index_table, enreg, enbit, vosel, \
68 [MT6358_ID_##vreg] = { \
71 .of_match = of_match_ptr(match), \
72 .ops = &mt6358_volt_table_ops, \
73 .type = REGULATOR_VOLTAGE, \
74 .id = MT6358_ID_##vreg, \
75 .owner = THIS_MODULE, \
76 .n_voltages = ARRAY_SIZE(ldo_volt_table), \
77 .volt_table = ldo_volt_table, \
79 .vsel_mask = vosel_mask, \
80 .enable_reg = enreg, \
81 .enable_mask = BIT(enbit), \
83 .status_reg = MT6358_LDO_##vreg##_CON1, \
85 .index_table = ldo_index_table, \
86 .n_table = ARRAY_SIZE(ldo_index_table), \
89 #define MT6358_LDO1(match, vreg, min, max, step, \
90 volt_ranges, _da_vsel_reg, _da_vsel_mask, \
92 [MT6358_ID_##vreg] = { \
95 .of_match = of_match_ptr(match), \
96 .ops = &mt6358_volt_range_ops, \
97 .type = REGULATOR_VOLTAGE, \
98 .id = MT6358_ID_##vreg, \
99 .owner = THIS_MODULE, \
100 .n_voltages = ((max) - (min)) / (step) + 1, \
101 .linear_ranges = volt_ranges, \
102 .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
104 .vsel_mask = vosel_mask, \
105 .enable_reg = MT6358_LDO_##vreg##_CON0, \
106 .enable_mask = BIT(0), \
108 .da_vsel_reg = _da_vsel_reg, \
109 .da_vsel_mask = _da_vsel_mask, \
110 .status_reg = MT6358_LDO_##vreg##_DBG1, \
114 #define MT6358_REG_FIXED(match, vreg, \
115 enreg, enbit, volt) \
116 [MT6358_ID_##vreg] = { \
119 .of_match = of_match_ptr(match), \
120 .ops = &mt6358_volt_fixed_ops, \
121 .type = REGULATOR_VOLTAGE, \
122 .id = MT6358_ID_##vreg, \
123 .owner = THIS_MODULE, \
125 .enable_reg = enreg, \
126 .enable_mask = BIT(enbit), \
129 .status_reg = MT6358_LDO_##vreg##_CON1, \
133 static const struct linear_range buck_volt_range1[] = {
134 REGULATOR_LINEAR_RANGE(500000, 0, 0x7f, 6250),
137 static const struct linear_range buck_volt_range2[] = {
138 REGULATOR_LINEAR_RANGE(500000, 0, 0x7f, 12500),
141 static const struct linear_range buck_volt_range3[] = {
142 REGULATOR_LINEAR_RANGE(500000, 0, 0x3f, 50000),
145 static const struct linear_range buck_volt_range4[] = {
146 REGULATOR_LINEAR_RANGE(1000000, 0, 0x7f, 12500),
149 static const unsigned int vdram2_voltages[] = {
153 static const unsigned int vsim_voltages[] = {
154 1700000, 1800000, 2700000, 3000000, 3100000,
157 static const unsigned int vibr_voltages[] = {
158 1200000, 1300000, 1500000, 1800000,
159 2000000, 2800000, 3000000, 3300000,
162 static const unsigned int vusb_voltages[] = {
166 static const unsigned int vcamd_voltages[] = {
167 900000, 1000000, 1100000, 1200000,
168 1300000, 1500000, 1800000,
171 static const unsigned int vefuse_voltages[] = {
172 1700000, 1800000, 1900000,
175 static const unsigned int vmch_vemc_voltages[] = {
176 2900000, 3000000, 3300000,
179 static const unsigned int vcama_voltages[] = {
180 1800000, 2500000, 2700000,
181 2800000, 2900000, 3000000,
184 static const unsigned int vcn33_bt_wifi_voltages[] = {
185 3300000, 3400000, 3500000,
188 static const unsigned int vmc_voltages[] = {
189 1800000, 2900000, 3000000, 3300000,
192 static const unsigned int vldo28_voltages[] = {
196 static const u32 vdram2_idx[] = {
200 static const u32 vsim_idx[] = {
204 static const u32 vibr_idx[] = {
205 0, 1, 2, 4, 5, 9, 11, 13,
208 static const u32 vusb_idx[] = {
212 static const u32 vcamd_idx[] = {
213 3, 4, 5, 6, 7, 9, 12,
216 static const u32 vefuse_idx[] = {
220 static const u32 vmch_vemc_idx[] = {
224 static const u32 vcama_idx[] = {
228 static const u32 vcn33_bt_wifi_idx[] = {
232 static const u32 vmc_idx[] = {
236 static const u32 vldo28_idx[] = {
240 static unsigned int mt6358_map_mode(unsigned int mode)
242 return mode == MT6358_BUCK_MODE_AUTO ?
243 REGULATOR_MODE_NORMAL : REGULATOR_MODE_FAST;
246 static int mt6358_set_voltage_sel(struct regulator_dev *rdev,
247 unsigned int selector)
251 struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
253 pvol = info->index_table;
255 idx = pvol[selector];
256 idx <<= ffs(info->desc.vsel_mask) - 1;
257 ret = regmap_update_bits(rdev->regmap, info->desc.vsel_reg,
258 info->desc.vsel_mask, idx);
263 static int mt6358_get_voltage_sel(struct regulator_dev *rdev)
267 struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
270 ret = regmap_read(rdev->regmap, info->desc.vsel_reg, &selector);
273 "Failed to get mt6358 %s vsel reg: %d\n",
274 info->desc.name, ret);
278 selector = (selector & info->desc.vsel_mask) >>
279 (ffs(info->desc.vsel_mask) - 1);
280 pvol = info->index_table;
281 for (idx = 0; idx < info->desc.n_voltages; idx++) {
282 if (pvol[idx] == selector)
289 static int mt6358_get_buck_voltage_sel(struct regulator_dev *rdev)
292 struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
294 ret = regmap_read(rdev->regmap, info->da_vsel_reg, ®val);
297 "Failed to get mt6358 Buck %s vsel reg: %d\n",
298 info->desc.name, ret);
302 ret = (regval & info->da_vsel_mask) >> (ffs(info->da_vsel_mask) - 1);
307 static int mt6358_get_status(struct regulator_dev *rdev)
311 struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
313 ret = regmap_read(rdev->regmap, info->status_reg, ®val);
315 dev_info(&rdev->dev, "Failed to get enable reg: %d\n", ret);
319 return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
322 static int mt6358_regulator_set_mode(struct regulator_dev *rdev,
325 struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
329 case REGULATOR_MODE_FAST:
330 val = MT6358_BUCK_MODE_FORCE_PWM;
332 case REGULATOR_MODE_NORMAL:
333 val = MT6358_BUCK_MODE_AUTO;
339 dev_dbg(&rdev->dev, "mt6358 buck set_mode %#x, %#x, %#x\n",
340 info->modeset_reg, info->modeset_mask, val);
342 val <<= ffs(info->modeset_mask) - 1;
344 return regmap_update_bits(rdev->regmap, info->modeset_reg,
345 info->modeset_mask, val);
348 static unsigned int mt6358_regulator_get_mode(struct regulator_dev *rdev)
350 struct mt6358_regulator_info *info = rdev_get_drvdata(rdev);
353 ret = regmap_read(rdev->regmap, info->modeset_reg, ®val);
356 "Failed to get mt6358 buck mode: %d\n", ret);
360 switch ((regval & info->modeset_mask) >> (ffs(info->modeset_mask) - 1)) {
361 case MT6358_BUCK_MODE_AUTO:
362 return REGULATOR_MODE_NORMAL;
363 case MT6358_BUCK_MODE_FORCE_PWM:
364 return REGULATOR_MODE_FAST;
370 static const struct regulator_ops mt6358_volt_range_ops = {
371 .list_voltage = regulator_list_voltage_linear_range,
372 .map_voltage = regulator_map_voltage_linear_range,
373 .set_voltage_sel = regulator_set_voltage_sel_regmap,
374 .get_voltage_sel = mt6358_get_buck_voltage_sel,
375 .set_voltage_time_sel = regulator_set_voltage_time_sel,
376 .enable = regulator_enable_regmap,
377 .disable = regulator_disable_regmap,
378 .is_enabled = regulator_is_enabled_regmap,
379 .get_status = mt6358_get_status,
380 .set_mode = mt6358_regulator_set_mode,
381 .get_mode = mt6358_regulator_get_mode,
384 static const struct regulator_ops mt6358_volt_table_ops = {
385 .list_voltage = regulator_list_voltage_table,
386 .map_voltage = regulator_map_voltage_iterate,
387 .set_voltage_sel = mt6358_set_voltage_sel,
388 .get_voltage_sel = mt6358_get_voltage_sel,
389 .set_voltage_time_sel = regulator_set_voltage_time_sel,
390 .enable = regulator_enable_regmap,
391 .disable = regulator_disable_regmap,
392 .is_enabled = regulator_is_enabled_regmap,
393 .get_status = mt6358_get_status,
396 static const struct regulator_ops mt6358_volt_fixed_ops = {
397 .list_voltage = regulator_list_voltage_linear,
398 .enable = regulator_enable_regmap,
399 .disable = regulator_disable_regmap,
400 .is_enabled = regulator_is_enabled_regmap,
401 .get_status = mt6358_get_status,
404 /* The array is indexed by id(MT6358_ID_XXX) */
405 static struct mt6358_regulator_info mt6358_regulators[] = {
406 MT6358_BUCK("buck_vdram1", VDRAM1, 500000, 2087500, 12500,
407 buck_volt_range2, 0x7f, MT6358_BUCK_VDRAM1_DBG0, 0x7f,
408 MT6358_VDRAM1_ANA_CON0, 8),
409 MT6358_BUCK("buck_vcore", VCORE, 500000, 1293750, 6250,
410 buck_volt_range1, 0x7f, MT6358_BUCK_VCORE_DBG0, 0x7f,
411 MT6358_VCORE_VGPU_ANA_CON0, 1),
412 MT6358_BUCK("buck_vpa", VPA, 500000, 3650000, 50000,
413 buck_volt_range3, 0x3f, MT6358_BUCK_VPA_DBG0, 0x3f,
414 MT6358_VPA_ANA_CON0, 3),
415 MT6358_BUCK("buck_vproc11", VPROC11, 500000, 1293750, 6250,
416 buck_volt_range1, 0x7f, MT6358_BUCK_VPROC11_DBG0, 0x7f,
417 MT6358_VPROC_ANA_CON0, 1),
418 MT6358_BUCK("buck_vproc12", VPROC12, 500000, 1293750, 6250,
419 buck_volt_range1, 0x7f, MT6358_BUCK_VPROC12_DBG0, 0x7f,
420 MT6358_VPROC_ANA_CON0, 2),
421 MT6358_BUCK("buck_vgpu", VGPU, 500000, 1293750, 6250,
422 buck_volt_range1, 0x7f, MT6358_BUCK_VGPU_ELR0, 0x7f,
423 MT6358_VCORE_VGPU_ANA_CON0, 2),
424 MT6358_BUCK("buck_vs2", VS2, 500000, 2087500, 12500,
425 buck_volt_range2, 0x7f, MT6358_BUCK_VS2_DBG0, 0x7f,
426 MT6358_VS2_ANA_CON0, 8),
427 MT6358_BUCK("buck_vmodem", VMODEM, 500000, 1293750, 6250,
428 buck_volt_range1, 0x7f, MT6358_BUCK_VMODEM_DBG0, 0x7f,
429 MT6358_VMODEM_ANA_CON0, 8),
430 MT6358_BUCK("buck_vs1", VS1, 1000000, 2587500, 12500,
431 buck_volt_range4, 0x7f, MT6358_BUCK_VS1_DBG0, 0x7f,
432 MT6358_VS1_ANA_CON0, 8),
433 MT6358_REG_FIXED("ldo_vrf12", VRF12,
434 MT6358_LDO_VRF12_CON0, 0, 1200000),
435 MT6358_REG_FIXED("ldo_vio18", VIO18,
436 MT6358_LDO_VIO18_CON0, 0, 1800000),
437 MT6358_REG_FIXED("ldo_vcamio", VCAMIO,
438 MT6358_LDO_VCAMIO_CON0, 0, 1800000),
439 MT6358_REG_FIXED("ldo_vcn18", VCN18, MT6358_LDO_VCN18_CON0, 0, 1800000),
440 MT6358_REG_FIXED("ldo_vfe28", VFE28, MT6358_LDO_VFE28_CON0, 0, 2800000),
441 MT6358_REG_FIXED("ldo_vcn28", VCN28, MT6358_LDO_VCN28_CON0, 0, 2800000),
442 MT6358_REG_FIXED("ldo_vxo22", VXO22, MT6358_LDO_VXO22_CON0, 0, 2200000),
443 MT6358_REG_FIXED("ldo_vaux18", VAUX18,
444 MT6358_LDO_VAUX18_CON0, 0, 1800000),
445 MT6358_REG_FIXED("ldo_vbif28", VBIF28,
446 MT6358_LDO_VBIF28_CON0, 0, 2800000),
447 MT6358_REG_FIXED("ldo_vio28", VIO28, MT6358_LDO_VIO28_CON0, 0, 2800000),
448 MT6358_REG_FIXED("ldo_va12", VA12, MT6358_LDO_VA12_CON0, 0, 1200000),
449 MT6358_REG_FIXED("ldo_vrf18", VRF18, MT6358_LDO_VRF18_CON0, 0, 1800000),
450 MT6358_REG_FIXED("ldo_vaud28", VAUD28,
451 MT6358_LDO_VAUD28_CON0, 0, 2800000),
452 MT6358_LDO("ldo_vdram2", VDRAM2, vdram2_voltages, vdram2_idx,
453 MT6358_LDO_VDRAM2_CON0, 0, MT6358_LDO_VDRAM2_ELR0, 0xf),
454 MT6358_LDO("ldo_vsim1", VSIM1, vsim_voltages, vsim_idx,
455 MT6358_LDO_VSIM1_CON0, 0, MT6358_VSIM1_ANA_CON0, 0xf00),
456 MT6358_LDO("ldo_vibr", VIBR, vibr_voltages, vibr_idx,
457 MT6358_LDO_VIBR_CON0, 0, MT6358_VIBR_ANA_CON0, 0xf00),
458 MT6358_LDO("ldo_vusb", VUSB, vusb_voltages, vusb_idx,
459 MT6358_LDO_VUSB_CON0_0, 0, MT6358_VUSB_ANA_CON0, 0x700),
460 MT6358_LDO("ldo_vcamd", VCAMD, vcamd_voltages, vcamd_idx,
461 MT6358_LDO_VCAMD_CON0, 0, MT6358_VCAMD_ANA_CON0, 0xf00),
462 MT6358_LDO("ldo_vefuse", VEFUSE, vefuse_voltages, vefuse_idx,
463 MT6358_LDO_VEFUSE_CON0, 0, MT6358_VEFUSE_ANA_CON0, 0xf00),
464 MT6358_LDO("ldo_vmch", VMCH, vmch_vemc_voltages, vmch_vemc_idx,
465 MT6358_LDO_VMCH_CON0, 0, MT6358_VMCH_ANA_CON0, 0x700),
466 MT6358_LDO("ldo_vcama1", VCAMA1, vcama_voltages, vcama_idx,
467 MT6358_LDO_VCAMA1_CON0, 0, MT6358_VCAMA1_ANA_CON0, 0xf00),
468 MT6358_LDO("ldo_vemc", VEMC, vmch_vemc_voltages, vmch_vemc_idx,
469 MT6358_LDO_VEMC_CON0, 0, MT6358_VEMC_ANA_CON0, 0x700),
470 MT6358_LDO("ldo_vcn33_bt", VCN33_BT, vcn33_bt_wifi_voltages,
471 vcn33_bt_wifi_idx, MT6358_LDO_VCN33_CON0_0,
472 0, MT6358_VCN33_ANA_CON0, 0x300),
473 MT6358_LDO("ldo_vcn33_wifi", VCN33_WIFI, vcn33_bt_wifi_voltages,
474 vcn33_bt_wifi_idx, MT6358_LDO_VCN33_CON0_1,
475 0, MT6358_VCN33_ANA_CON0, 0x300),
476 MT6358_LDO("ldo_vcama2", VCAMA2, vcama_voltages, vcama_idx,
477 MT6358_LDO_VCAMA2_CON0, 0, MT6358_VCAMA2_ANA_CON0, 0xf00),
478 MT6358_LDO("ldo_vmc", VMC, vmc_voltages, vmc_idx,
479 MT6358_LDO_VMC_CON0, 0, MT6358_VMC_ANA_CON0, 0xf00),
480 MT6358_LDO("ldo_vldo28", VLDO28, vldo28_voltages, vldo28_idx,
481 MT6358_LDO_VLDO28_CON0_0, 0,
482 MT6358_VLDO28_ANA_CON0, 0x300),
483 MT6358_LDO("ldo_vsim2", VSIM2, vsim_voltages, vsim_idx,
484 MT6358_LDO_VSIM2_CON0, 0, MT6358_VSIM2_ANA_CON0, 0xf00),
485 MT6358_LDO1("ldo_vsram_proc11", VSRAM_PROC11, 500000, 1293750, 6250,
486 buck_volt_range1, MT6358_LDO_VSRAM_PROC11_DBG0, 0x7f00,
487 MT6358_LDO_VSRAM_CON0, 0x7f),
488 MT6358_LDO1("ldo_vsram_others", VSRAM_OTHERS, 500000, 1293750, 6250,
489 buck_volt_range1, MT6358_LDO_VSRAM_OTHERS_DBG0, 0x7f00,
490 MT6358_LDO_VSRAM_CON2, 0x7f),
491 MT6358_LDO1("ldo_vsram_gpu", VSRAM_GPU, 500000, 1293750, 6250,
492 buck_volt_range1, MT6358_LDO_VSRAM_GPU_DBG0, 0x7f00,
493 MT6358_LDO_VSRAM_CON3, 0x7f),
494 MT6358_LDO1("ldo_vsram_proc12", VSRAM_PROC12, 500000, 1293750, 6250,
495 buck_volt_range1, MT6358_LDO_VSRAM_PROC12_DBG0, 0x7f00,
496 MT6358_LDO_VSRAM_CON1, 0x7f),
499 static int mt6358_regulator_probe(struct platform_device *pdev)
501 struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
502 struct regulator_config config = {};
503 struct regulator_dev *rdev;
506 for (i = 0; i < MT6358_MAX_REGULATOR; i++) {
507 config.dev = &pdev->dev;
508 config.driver_data = &mt6358_regulators[i];
509 config.regmap = mt6397->regmap;
511 rdev = devm_regulator_register(&pdev->dev,
512 &mt6358_regulators[i].desc,
515 dev_err(&pdev->dev, "failed to register %s\n",
516 mt6358_regulators[i].desc.name);
517 return PTR_ERR(rdev);
524 static const struct platform_device_id mt6358_platform_ids[] = {
525 {"mt6358-regulator", 0},
528 MODULE_DEVICE_TABLE(platform, mt6358_platform_ids);
530 static struct platform_driver mt6358_regulator_driver = {
532 .name = "mt6358-regulator",
534 .probe = mt6358_regulator_probe,
535 .id_table = mt6358_platform_ids,
538 module_platform_driver(mt6358_regulator_driver);
540 MODULE_AUTHOR("Hsin-Hsiung Wang <hsin-hsiung.wang@mediatek.com>");
541 MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6358 PMIC");
542 MODULE_LICENSE("GPL");