1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2022 MediaTek Inc.
4 // Copyright (c) 2022 BayLibre, SAS.
5 // Author: Chen Zhong <chen.zhong@mediatek.com>
6 // Author: Fabien Parent <fparent@baylibre.com>
7 // Author: Alexandre Mergnat <amergnat@baylibre.com>
9 // Based on mt6397-regulator.c
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/mfd/mt6397/core.h>
17 #include <linux/mfd/mt6357/registers.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/mt6357-regulator.h>
21 #include <linux/regulator/of_regulator.h>
24 * MT6357 regulators' information
26 * @desc: standard fields of regulator description.
27 * @da_vsel_reg: Monitor register for query buck's voltage.
28 * @da_vsel_mask: Mask for query buck's voltage.
30 struct mt6357_regulator_info {
31 struct regulator_desc desc;
36 #define MT6357_BUCK(match, vreg, min, max, step, \
37 volt_ranges, vosel_reg, vosel_mask, _da_vsel_mask) \
38 [MT6357_ID_##vreg] = { \
41 .of_match = of_match_ptr(match), \
42 .regulators_node = "regulators", \
43 .ops = &mt6357_volt_range_ops, \
44 .type = REGULATOR_VOLTAGE, \
45 .id = MT6357_ID_##vreg, \
46 .owner = THIS_MODULE, \
47 .n_voltages = ((max) - (min)) / (step) + 1, \
48 .linear_ranges = volt_ranges, \
49 .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
50 .vsel_reg = vosel_reg, \
51 .vsel_mask = vosel_mask, \
52 .enable_reg = MT6357_BUCK_##vreg##_CON0, \
53 .enable_mask = BIT(0), \
55 .da_vsel_reg = MT6357_BUCK_##vreg##_DBG0, \
56 .da_vsel_mask = vosel_mask, \
59 #define MT6357_LDO(match, vreg, ldo_volt_table, \
60 enreg, vosel, vosel_mask) \
61 [MT6357_ID_##vreg] = { \
64 .of_match = of_match_ptr(match), \
65 .regulators_node = "regulators", \
66 .ops = &mt6357_volt_table_ops, \
67 .type = REGULATOR_VOLTAGE, \
68 .id = MT6357_ID_##vreg, \
69 .owner = THIS_MODULE, \
70 .n_voltages = ARRAY_SIZE(ldo_volt_table), \
71 .volt_table = ldo_volt_table, \
73 .vsel_mask = vosel_mask, \
74 .enable_reg = enreg, \
75 .enable_mask = BIT(0), \
79 #define MT6357_LDO1(match, vreg, min, max, step, volt_ranges, \
80 enreg, vosel, vosel_mask) \
81 [MT6357_ID_##vreg] = { \
84 .of_match = of_match_ptr(match), \
85 .regulators_node = "regulators", \
86 .ops = &mt6357_volt_range_ops, \
87 .type = REGULATOR_VOLTAGE, \
88 .id = MT6357_ID_##vreg, \
89 .owner = THIS_MODULE, \
90 .n_voltages = ((max) - (min)) / (step) + 1, \
91 .linear_ranges = volt_ranges, \
92 .n_linear_ranges = ARRAY_SIZE(volt_ranges), \
94 .vsel_mask = vosel_mask, \
95 .enable_reg = enreg, \
96 .enable_mask = BIT(0), \
98 .da_vsel_reg = MT6357_LDO_##vreg##_DBG0, \
99 .da_vsel_mask = 0x7f00, \
102 #define MT6357_REG_FIXED(match, vreg, volt) \
103 [MT6357_ID_##vreg] = { \
106 .of_match = of_match_ptr(match), \
107 .regulators_node = "regulators", \
108 .ops = &mt6357_volt_fixed_ops, \
109 .type = REGULATOR_VOLTAGE, \
110 .id = MT6357_ID_##vreg, \
111 .owner = THIS_MODULE, \
113 .enable_reg = MT6357_LDO_##vreg##_CON0, \
114 .enable_mask = BIT(0), \
120 * mt6357_get_buck_voltage_sel - get_voltage_sel for regmap users
122 * @rdev: regulator to operate on
124 * Regulators that use regmap for their register I/O can set the
125 * da_vsel_reg and da_vsel_mask fields in the info structure and
126 * then use this as their get_voltage_vsel operation.
128 static int mt6357_get_buck_voltage_sel(struct regulator_dev *rdev)
131 struct mt6357_regulator_info *info = rdev_get_drvdata(rdev);
133 ret = regmap_read(rdev->regmap, info->da_vsel_reg, ®val);
136 "Failed to get mt6357 Buck %s vsel reg: %d\n",
137 info->desc.name, ret);
141 regval &= info->da_vsel_mask;
142 regval >>= ffs(info->da_vsel_mask) - 1;
147 static const struct regulator_ops mt6357_volt_range_ops = {
148 .list_voltage = regulator_list_voltage_linear_range,
149 .map_voltage = regulator_map_voltage_linear_range,
150 .set_voltage_sel = regulator_set_voltage_sel_regmap,
151 .get_voltage_sel = mt6357_get_buck_voltage_sel,
152 .set_voltage_time_sel = regulator_set_voltage_time_sel,
153 .enable = regulator_enable_regmap,
154 .disable = regulator_disable_regmap,
155 .is_enabled = regulator_is_enabled_regmap,
158 static const struct regulator_ops mt6357_volt_table_ops = {
159 .list_voltage = regulator_list_voltage_table,
160 .map_voltage = regulator_map_voltage_iterate,
161 .set_voltage_sel = regulator_set_voltage_sel_regmap,
162 .get_voltage_sel = regulator_get_voltage_sel_regmap,
163 .set_voltage_time_sel = regulator_set_voltage_time_sel,
164 .enable = regulator_enable_regmap,
165 .disable = regulator_disable_regmap,
166 .is_enabled = regulator_is_enabled_regmap,
169 static const struct regulator_ops mt6357_volt_fixed_ops = {
170 .list_voltage = regulator_list_voltage_linear,
171 .enable = regulator_enable_regmap,
172 .disable = regulator_disable_regmap,
173 .is_enabled = regulator_is_enabled_regmap,
176 static const int vxo22_voltages[] = {
182 static const int vefuse_voltages[] = {
199 static const int vcn33_voltages[] = {
206 static const int vcama_voltages[] = {
220 static const int vcamd_voltages[] = {
236 static const int vldo28_voltages[] = {
243 static const int vdram_voltages[] = {
249 static const int vsim_voltages[] = {
265 static const int vibr_voltages[] = {
282 static const int vmc_voltages[] = {
299 static const int vmch_voltages[] = {
308 static const int vemc_voltages[] = {
317 static const int vusb_voltages[] = {
325 static const struct linear_range buck_volt_range1[] = {
326 REGULATOR_LINEAR_RANGE(518750, 0, 0x7f, 6250),
329 static const struct linear_range buck_volt_range2[] = {
330 REGULATOR_LINEAR_RANGE(500000, 0, 0x7f, 6250),
333 static const struct linear_range buck_volt_range3[] = {
334 REGULATOR_LINEAR_RANGE(500000, 0, 0x3f, 50000),
337 static const struct linear_range buck_volt_range4[] = {
338 REGULATOR_LINEAR_RANGE(1200000, 0, 0x7f, 12500),
341 /* The array is indexed by id(MT6357_ID_XXX) */
342 static struct mt6357_regulator_info mt6357_regulators[] = {
344 MT6357_BUCK("buck-vcore", VCORE, 518750, 1312500, 6250,
345 buck_volt_range1, MT6357_BUCK_VCORE_ELR0, 0x7f, 0x7f),
346 MT6357_BUCK("buck-vproc", VPROC, 518750, 1312500, 6250,
347 buck_volt_range1, MT6357_BUCK_VPROC_ELR0, 0x7f, 0x7f),
348 MT6357_BUCK("buck-vmodem", VMODEM, 500000, 1293750, 6250,
349 buck_volt_range2, MT6357_BUCK_VMODEM_ELR0, 0x7f, 0x7f),
350 MT6357_BUCK("buck-vpa", VPA, 500000, 3650000, 50000,
351 buck_volt_range3, MT6357_BUCK_VPA_CON1, 0x3f, 0x3f),
352 MT6357_BUCK("buck-vs1", VS1, 1200000, 2787500, 12500,
353 buck_volt_range4, MT6357_BUCK_VS1_ELR0, 0x7f, 0x7f),
356 MT6357_LDO("ldo-vcama", VCAMA, vcama_voltages,
357 MT6357_LDO_VCAMA_CON0, MT6357_VCAMA_ANA_CON0, 0xf00),
358 MT6357_LDO("ldo-vcamd", VCAMD, vcamd_voltages,
359 MT6357_LDO_VCAMD_CON0, MT6357_VCAMD_ANA_CON0, 0xf00),
360 MT6357_LDO("ldo-vcn33-bt", VCN33_BT, vcn33_voltages,
361 MT6357_LDO_VCN33_CON0_0, MT6357_VCN33_ANA_CON0, 0x300),
362 MT6357_LDO("ldo-vcn33-wifi", VCN33_WIFI, vcn33_voltages,
363 MT6357_LDO_VCN33_CON0_1, MT6357_VCN33_ANA_CON0, 0x300),
364 MT6357_LDO("ldo-vdram", VDRAM, vdram_voltages,
365 MT6357_LDO_VDRAM_CON0, MT6357_VDRAM_ELR_2, 0x300),
366 MT6357_LDO("ldo-vefuse", VEFUSE, vefuse_voltages,
367 MT6357_LDO_VEFUSE_CON0, MT6357_VEFUSE_ANA_CON0, 0xf00),
368 MT6357_LDO("ldo-vemc", VEMC, vemc_voltages,
369 MT6357_LDO_VEMC_CON0, MT6357_VEMC_ANA_CON0, 0x700),
370 MT6357_LDO("ldo-vibr", VIBR, vibr_voltages,
371 MT6357_LDO_VIBR_CON0, MT6357_VIBR_ANA_CON0, 0xf00),
372 MT6357_LDO("ldo-vldo28", VLDO28, vldo28_voltages,
373 MT6357_LDO_VLDO28_CON0_0, MT6357_VLDO28_ANA_CON0, 0x300),
374 MT6357_LDO("ldo-vmc", VMC, vmc_voltages,
375 MT6357_LDO_VMC_CON0, MT6357_VMC_ANA_CON0, 0xf00),
376 MT6357_LDO("ldo-vmch", VMCH, vmch_voltages,
377 MT6357_LDO_VMCH_CON0, MT6357_VMCH_ANA_CON0, 0x700),
378 MT6357_LDO("ldo-vsim1", VSIM1, vsim_voltages,
379 MT6357_LDO_VSIM1_CON0, MT6357_VSIM1_ANA_CON0, 0xf00),
380 MT6357_LDO("ldo-vsim2", VSIM2, vsim_voltages,
381 MT6357_LDO_VSIM2_CON0, MT6357_VSIM2_ANA_CON0, 0xf00),
382 MT6357_LDO("ldo-vusb33", VUSB33, vusb_voltages,
383 MT6357_LDO_VUSB33_CON0_0, MT6357_VUSB33_ANA_CON0, 0x700),
384 MT6357_LDO("ldo-vxo22", VXO22, vxo22_voltages,
385 MT6357_LDO_VXO22_CON0, MT6357_VXO22_ANA_CON0, 0x300),
387 MT6357_LDO1("ldo-vsram-proc", VSRAM_PROC, 518750, 1312500, 6250,
388 buck_volt_range1, MT6357_LDO_VSRAM_PROC_CON0,
389 MT6357_LDO_VSRAM_CON0, 0x7f00),
390 MT6357_LDO1("ldo-vsram-others", VSRAM_OTHERS, 518750, 1312500, 6250,
391 buck_volt_range1, MT6357_LDO_VSRAM_OTHERS_CON0,
392 MT6357_LDO_VSRAM_CON1, 0x7f00),
394 MT6357_REG_FIXED("ldo-vaud28", VAUD28, 2800000),
395 MT6357_REG_FIXED("ldo-vaux18", VAUX18, 1800000),
396 MT6357_REG_FIXED("ldo-vcamio18", VCAMIO, 1800000),
397 MT6357_REG_FIXED("ldo-vcn18", VCN18, 1800000),
398 MT6357_REG_FIXED("ldo-vcn28", VCN28, 2800000),
399 MT6357_REG_FIXED("ldo-vfe28", VFE28, 2800000),
400 MT6357_REG_FIXED("ldo-vio18", VIO18, 1800000),
401 MT6357_REG_FIXED("ldo-vio28", VIO28, 2800000),
402 MT6357_REG_FIXED("ldo-vrf12", VRF12, 1200000),
403 MT6357_REG_FIXED("ldo-vrf18", VRF18, 1800000),
406 static int mt6357_regulator_probe(struct platform_device *pdev)
408 struct mt6397_chip *mt6357 = dev_get_drvdata(pdev->dev.parent);
409 struct regulator_config config = {};
410 struct regulator_dev *rdev;
413 pdev->dev.of_node = pdev->dev.parent->of_node;
415 for (i = 0; i < MT6357_MAX_REGULATOR; i++) {
416 config.dev = &pdev->dev;
417 config.driver_data = &mt6357_regulators[i];
418 config.regmap = mt6357->regmap;
420 rdev = devm_regulator_register(&pdev->dev,
421 &mt6357_regulators[i].desc,
424 dev_err(&pdev->dev, "failed to register %s\n",
425 mt6357_regulators[i].desc.name);
426 return PTR_ERR(rdev);
433 static const struct platform_device_id mt6357_platform_ids[] = {
434 { "mt6357-regulator" },
437 MODULE_DEVICE_TABLE(platform, mt6357_platform_ids);
439 static struct platform_driver mt6357_regulator_driver = {
441 .name = "mt6357-regulator",
442 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
444 .probe = mt6357_regulator_probe,
445 .id_table = mt6357_platform_ids,
448 module_platform_driver(mt6357_regulator_driver);
450 MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
451 MODULE_AUTHOR("Fabien Parent <fabien.parent@linaro.org>");
452 MODULE_AUTHOR("Alexandre Mergnat <amergnat@baylibre.com>");
453 MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6357 PMIC");
454 MODULE_LICENSE("GPL");