1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2021, The Linux Foundation. All rights reserved. 4 * Copyright (c) 2022, Linaro Ltd. 5 */ 6 7 #include <linux/bitfield.h> 8 #include <linux/gpio/consumer.h> 9 #include <linux/i2c.h> 10 #include <linux/module.h> 11 #include <linux/of_platform.h> 12 #include <linux/regmap.h> 13 #include <linux/regulator/driver.h> 14 #include <linux/regulator/machine.h> 15 #include <linux/regulator/of_regulator.h> 16 17 #define MAX20411_UV_STEP 6250 18 #define MAX20411_BASE_UV 243750 19 #define MAX20411_MIN_SEL 41 /* 0.5V */ 20 #define MAX20411_MAX_SEL 165 /* 1.275V */ 21 #define MAX20411_VID_OFFSET 0x7 22 #define MAX20411_VID_MASK 0xff 23 #define MAX20411_SLEW_OFFSET 0x6 24 #define MAX20411_SLEW_DVS_MASK 0xc 25 #define MAX20411_SLEW_SR_MASK 0x3 26 27 struct max20411 { 28 struct device *dev; 29 struct device_node *of_node; 30 struct regulator_desc desc; 31 struct regulator_dev *rdev; 32 struct regmap *regmap; 33 }; 34 35 static const unsigned int max20411_slew_rates[] = { 13100, 6600, 3300, 1600 }; 36 37 static int max20411_enable_time(struct regulator_dev *rdev) 38 { 39 int voltage, rate, ret; 40 unsigned int val; 41 42 /* get voltage */ 43 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val); 44 if (ret) 45 return ret; 46 47 val &= rdev->desc->vsel_mask; 48 voltage = regulator_list_voltage_linear(rdev, val); 49 50 /* get rate */ 51 ret = regmap_read(rdev->regmap, MAX20411_SLEW_OFFSET, &val); 52 if (ret) 53 return ret; 54 55 val = FIELD_GET(MAX20411_SLEW_SR_MASK, val); 56 rate = max20411_slew_rates[val]; 57 58 return DIV_ROUND_UP(voltage, rate); 59 } 60 61 static const struct regmap_config max20411_regmap_config = { 62 .reg_bits = 8, 63 .val_bits = 8, 64 .max_register = 0xe, 65 }; 66 67 static const struct regulator_ops max20411_ops = { 68 .get_voltage_sel = regulator_get_voltage_sel_regmap, 69 .set_voltage_sel = regulator_set_voltage_sel_regmap, 70 .list_voltage = regulator_list_voltage_linear, 71 .enable_time = max20411_enable_time, 72 }; 73 74 static const struct regulator_desc max20411_desc = { 75 .ops = &max20411_ops, 76 .owner = THIS_MODULE, 77 .type = REGULATOR_VOLTAGE, 78 .supply_name = "vin", 79 .name = "max20411", 80 81 /* 82 * voltage = 0.24375V + selector * 6.25mV 83 * with valid selector between 41 to 165 (0.5V to 1.275V) 84 */ 85 .min_uV = MAX20411_BASE_UV, 86 .uV_step = MAX20411_UV_STEP, 87 .linear_min_sel = MAX20411_MIN_SEL, 88 .n_voltages = MAX20411_MAX_SEL + 1, 89 90 .vsel_reg = MAX20411_VID_OFFSET, 91 .vsel_mask = MAX20411_VID_MASK, 92 93 .ramp_reg = MAX20411_SLEW_OFFSET, 94 .ramp_mask = MAX20411_SLEW_DVS_MASK, 95 .ramp_delay_table = max20411_slew_rates, 96 .n_ramp_values = ARRAY_SIZE(max20411_slew_rates), 97 }; 98 99 static int max20411_probe(struct i2c_client *client) 100 { 101 struct regulator_init_data *init_data; 102 struct device *dev = &client->dev; 103 struct regulator_config cfg = {}; 104 struct max20411 *max20411; 105 106 max20411 = devm_kzalloc(dev, sizeof(*max20411), GFP_KERNEL); 107 if (!max20411) 108 return -ENOMEM; 109 110 max20411->regmap = devm_regmap_init_i2c(client, &max20411_regmap_config); 111 if (IS_ERR(max20411->regmap)) { 112 dev_err(dev, "Failed to allocate regmap!\n"); 113 return PTR_ERR(max20411->regmap); 114 } 115 116 max20411->dev = dev; 117 max20411->of_node = dev->of_node; 118 119 max20411->desc = max20411_desc; 120 init_data = of_get_regulator_init_data(max20411->dev, max20411->of_node, &max20411->desc); 121 if (!init_data) 122 return -ENODATA; 123 124 cfg.dev = max20411->dev; 125 cfg.init_data = init_data; 126 cfg.of_node = max20411->of_node; 127 cfg.driver_data = max20411; 128 129 cfg.ena_gpiod = gpiod_get(max20411->dev, "enable", GPIOD_ASIS); 130 if (IS_ERR(cfg.ena_gpiod)) 131 return dev_err_probe(dev, PTR_ERR(cfg.ena_gpiod), 132 "unable to acquire enable gpio\n"); 133 134 max20411->rdev = devm_regulator_register(max20411->dev, &max20411->desc, &cfg); 135 if (IS_ERR(max20411->rdev)) 136 dev_err(max20411->dev, "Failed to register regulator\n"); 137 138 return PTR_ERR_OR_ZERO(max20411->rdev); 139 } 140 141 static const struct of_device_id of_max20411_match_tbl[] = { 142 { .compatible = "maxim,max20411", }, 143 { }, 144 }; 145 MODULE_DEVICE_TABLE(of, of_max20411_match_tbl); 146 147 static const struct i2c_device_id max20411_id[] = { 148 { "max20411" }, 149 { } 150 }; 151 MODULE_DEVICE_TABLE(i2c, max20411_id); 152 153 static struct i2c_driver max20411_i2c_driver = { 154 .driver = { 155 .name = "max20411", 156 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 157 .of_match_table = of_max20411_match_tbl, 158 }, 159 .probe = max20411_probe, 160 .id_table = max20411_id, 161 }; 162 module_i2c_driver(max20411_i2c_driver); 163 164 MODULE_DESCRIPTION("Maxim MAX20411 High-Efficiency Single Step-Down Converter driver"); 165 MODULE_LICENSE("GPL"); 166