1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2022 Analog Devices, Inc. 4 * ADI Regulator driver for the MAX77540 and MAX77541 5 */ 6 7 #include <linux/mfd/max77541.h> 8 #include <linux/platform_device.h> 9 #include <linux/regmap.h> 10 #include <linux/regulator/driver.h> 11 12 static const struct regulator_ops max77541_buck_ops = { 13 .enable = regulator_enable_regmap, 14 .disable = regulator_disable_regmap, 15 .is_enabled = regulator_is_enabled_regmap, 16 .list_voltage = regulator_list_voltage_pickable_linear_range, 17 .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap, 18 .set_voltage_sel = regulator_set_voltage_sel_pickable_regmap, 19 }; 20 21 static const struct linear_range max77540_buck_ranges[] = { 22 /* Ranges when VOLT_SEL bits are 0x00 */ 23 REGULATOR_LINEAR_RANGE(500000, 0x00, 0x8B, 5000), 24 REGULATOR_LINEAR_RANGE(1200000, 0x8C, 0xFF, 0), 25 /* Ranges when VOLT_SEL bits are 0x40 */ 26 REGULATOR_LINEAR_RANGE(1200000, 0x00, 0x8B, 10000), 27 REGULATOR_LINEAR_RANGE(2400000, 0x8C, 0xFF, 0), 28 /* Ranges when VOLT_SEL bits are 0x80 */ 29 REGULATOR_LINEAR_RANGE(2000000, 0x00, 0x9F, 20000), 30 REGULATOR_LINEAR_RANGE(5200000, 0xA0, 0xFF, 0), 31 }; 32 33 static const struct linear_range max77541_buck_ranges[] = { 34 /* Ranges when VOLT_SEL bits are 0x00 */ 35 REGULATOR_LINEAR_RANGE(300000, 0x00, 0xB3, 5000), 36 REGULATOR_LINEAR_RANGE(1200000, 0xB4, 0xFF, 0), 37 /* Ranges when VOLT_SEL bits are 0x40 */ 38 REGULATOR_LINEAR_RANGE(1200000, 0x00, 0x8B, 10000), 39 REGULATOR_LINEAR_RANGE(2400000, 0x8C, 0xFF, 0), 40 /* Ranges when VOLT_SEL bits are 0x80 */ 41 REGULATOR_LINEAR_RANGE(2000000, 0x00, 0x9F, 20000), 42 REGULATOR_LINEAR_RANGE(5200000, 0xA0, 0xFF, 0), 43 }; 44 45 static const unsigned int max77541_buck_volt_range_sel[] = { 46 0x0, 0x0, 0x1, 0x1, 0x2, 0x2, 47 }; 48 49 enum max77541_regulators { 50 MAX77541_BUCK1 = 1, 51 MAX77541_BUCK2, 52 }; 53 54 #define MAX77540_BUCK(_id, _ops) \ 55 { .id = MAX77541_BUCK ## _id, \ 56 .name = "buck"#_id, \ 57 .of_match = "buck"#_id, \ 58 .regulators_node = "regulators", \ 59 .enable_reg = MAX77541_REG_EN_CTRL, \ 60 .enable_mask = MAX77541_BIT_M ## _id ## _EN, \ 61 .ops = &(_ops), \ 62 .type = REGULATOR_VOLTAGE, \ 63 .linear_ranges = max77540_buck_ranges, \ 64 .n_linear_ranges = ARRAY_SIZE(max77540_buck_ranges), \ 65 .vsel_reg = MAX77541_REG_M ## _id ## _VOUT, \ 66 .vsel_mask = MAX77541_BITS_MX_VOUT, \ 67 .vsel_range_reg = MAX77541_REG_M ## _id ## _CFG1, \ 68 .vsel_range_mask = MAX77541_BITS_MX_CFG1_RNG, \ 69 .linear_range_selectors_bitfield = max77541_buck_volt_range_sel, \ 70 .owner = THIS_MODULE, \ 71 } 72 73 #define MAX77541_BUCK(_id, _ops) \ 74 { .id = MAX77541_BUCK ## _id, \ 75 .name = "buck"#_id, \ 76 .of_match = "buck"#_id, \ 77 .regulators_node = "regulators", \ 78 .enable_reg = MAX77541_REG_EN_CTRL, \ 79 .enable_mask = MAX77541_BIT_M ## _id ## _EN, \ 80 .ops = &(_ops), \ 81 .type = REGULATOR_VOLTAGE, \ 82 .linear_ranges = max77541_buck_ranges, \ 83 .n_linear_ranges = ARRAY_SIZE(max77541_buck_ranges), \ 84 .vsel_reg = MAX77541_REG_M ## _id ## _VOUT, \ 85 .vsel_mask = MAX77541_BITS_MX_VOUT, \ 86 .vsel_range_reg = MAX77541_REG_M ## _id ## _CFG1, \ 87 .vsel_range_mask = MAX77541_BITS_MX_CFG1_RNG, \ 88 .linear_range_selectors_bitfield = max77541_buck_volt_range_sel, \ 89 .owner = THIS_MODULE, \ 90 } 91 92 static const struct regulator_desc max77540_regulators_desc[] = { 93 MAX77540_BUCK(1, max77541_buck_ops), 94 MAX77540_BUCK(2, max77541_buck_ops), 95 }; 96 97 static const struct regulator_desc max77541_regulators_desc[] = { 98 MAX77541_BUCK(1, max77541_buck_ops), 99 MAX77541_BUCK(2, max77541_buck_ops), 100 }; 101 102 static int max77541_regulator_probe(struct platform_device *pdev) 103 { 104 struct regulator_config config = {}; 105 const struct regulator_desc *desc; 106 struct device *dev = &pdev->dev; 107 struct regulator_dev *rdev; 108 struct max77541 *max77541 = dev_get_drvdata(dev->parent); 109 unsigned int i; 110 111 config.dev = dev->parent; 112 113 switch (max77541->id) { 114 case MAX77540: 115 desc = max77540_regulators_desc; 116 break; 117 case MAX77541: 118 desc = max77541_regulators_desc; 119 break; 120 default: 121 return -EINVAL; 122 } 123 124 for (i = 0; i < MAX77541_MAX_REGULATORS; i++) { 125 rdev = devm_regulator_register(dev, &desc[i], &config); 126 if (IS_ERR(rdev)) 127 return dev_err_probe(dev, PTR_ERR(rdev), 128 "Failed to register regulator\n"); 129 } 130 131 return 0; 132 } 133 134 static const struct platform_device_id max77541_regulator_platform_id[] = { 135 { .name = "max77540-regulator" }, 136 { .name = "max77541-regulator" }, 137 { } 138 }; 139 MODULE_DEVICE_TABLE(platform, max77541_regulator_platform_id); 140 141 static struct platform_driver max77541_regulator_driver = { 142 .driver = { 143 .name = "max77541-regulator", 144 }, 145 .probe = max77541_regulator_probe, 146 .id_table = max77541_regulator_platform_id, 147 }; 148 module_platform_driver(max77541_regulator_driver); 149 150 MODULE_AUTHOR("Okan Sahin <Okan.Sahin@analog.com>"); 151 MODULE_DESCRIPTION("MAX77540/MAX77541 regulator driver"); 152 MODULE_LICENSE("GPL"); 153