1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2022, Linaro Ltd.
7 #include <linux/bitfield.h>
8 #include <linux/gpio/consumer.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>
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
29 struct device_node *of_node;
30 struct regulator_desc desc;
31 struct regulator_dev *rdev;
32 struct regmap *regmap;
35 static const unsigned int max20411_slew_rates[] = { 13100, 6600, 3300, 1600 };
37 static int max20411_enable_time(struct regulator_dev *rdev)
39 int voltage, rate, ret;
43 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
47 val &= rdev->desc->vsel_mask;
48 voltage = regulator_list_voltage_linear(rdev, val);
51 ret = regmap_read(rdev->regmap, MAX20411_SLEW_OFFSET, &val);
55 val = FIELD_GET(MAX20411_SLEW_SR_MASK, val);
56 rate = max20411_slew_rates[val];
58 return DIV_ROUND_UP(voltage, rate);
61 static const struct regmap_config max20411_regmap_config = {
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,
74 static const struct regulator_desc max20411_desc = {
77 .type = REGULATOR_VOLTAGE,
82 * voltage = 0.24375V + selector * 6.25mV
83 * with valid selector between 41 to 165 (0.5V to 1.275V)
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,
90 .vsel_reg = MAX20411_VID_OFFSET,
91 .vsel_mask = MAX20411_VID_MASK,
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),
99 static int max20411_probe(struct i2c_client *client)
101 struct regulator_init_data *init_data;
102 struct device *dev = &client->dev;
103 struct regulator_config cfg = {};
104 struct max20411 *max20411;
106 max20411 = devm_kzalloc(dev, sizeof(*max20411), GFP_KERNEL);
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);
117 max20411->of_node = dev->of_node;
119 max20411->desc = max20411_desc;
120 init_data = of_get_regulator_init_data(max20411->dev, max20411->of_node, &max20411->desc);
124 cfg.dev = max20411->dev;
125 cfg.init_data = init_data;
126 cfg.of_node = max20411->of_node;
127 cfg.driver_data = max20411;
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");
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");
138 return PTR_ERR_OR_ZERO(max20411->rdev);
141 static const struct of_device_id of_max20411_match_tbl[] = {
142 { .compatible = "maxim,max20411", },
145 MODULE_DEVICE_TABLE(of, of_max20411_match_tbl);
147 static const struct i2c_device_id max20411_id[] = {
151 MODULE_DEVICE_TABLE(i2c, max20411_id);
153 static struct i2c_driver max20411_i2c_driver = {
156 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
157 .of_match_table = of_max20411_match_tbl,
159 .probe = max20411_probe,
160 .id_table = max20411_id,
162 module_i2c_driver(max20411_i2c_driver);
164 MODULE_LICENSE("GPL");