xref: /linux/drivers/regulator/max77503-regulator.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2023 Analog Devices, Inc.
4  * ADI regulator driver for MAX77503.
5  */
6 
7 #include <linux/i2c.h>
8 #include <linux/module.h>
9 #include <linux/regmap.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/regulator/machine.h>
12 #include <linux/regulator/of_regulator.h>
13 #include <linux/util_macros.h>
14 
15 #define MAX77503_REG_CFG			0x00
16 #define MAX77503_REG_VOUT			0x01
17 
18 #define MAX77503_BIT_EN				BIT(0)
19 #define MAX77503_BIT_CURR_LIM		BIT(3)
20 #define MAX77503_BIT_ADEN			BIT(6)
21 
22 #define MAX77503_BITS_SOFT_START	GENMASK(5, 4)
23 #define MAX77503_BITS_MX_VOUT		GENMASK(7, 0)
24 
25 #define MAX77503_AD_ENABLED			0x1
26 #define MAX77503_AD_DISABLED		0x0
27 
28 static const struct regmap_config max77503_regmap_config = {
29 	.reg_bits = 8,
30 	.val_bits = 8,
31 	.max_register = 0x2,
32 };
33 
34 static const struct regulator_ops max77503_buck_ops = {
35 	.list_voltage = regulator_list_voltage_linear_range,
36 	.map_voltage = regulator_map_voltage_ascend,
37 	.is_enabled = regulator_is_enabled_regmap,
38 	.enable = regulator_enable_regmap,
39 	.disable = regulator_disable_regmap,
40 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
41 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
42 	.get_current_limit = regulator_get_current_limit_regmap,
43 	.set_current_limit = regulator_set_current_limit_regmap,
44 	.set_active_discharge = regulator_set_active_discharge_regmap,
45 	.set_soft_start = regulator_set_soft_start_regmap,
46 };
47 
48 static const struct linear_range max77503_buck_ranges[] = {
49 	REGULATOR_LINEAR_RANGE(800000, 0x00, 0x54, 50000)
50 };
51 
52 static const unsigned int max77503_current_limit_table[] = {
53 	500000, 2000000
54 };
55 
56 static const struct regulator_desc max77503_regulators_desc = {
57 	.name = "max77503",
58 	.enable_reg = MAX77503_REG_CFG,
59 	.enable_mask = MAX77503_BIT_EN,
60 	.ops = &max77503_buck_ops,
61 	.type = REGULATOR_VOLTAGE,
62 	.linear_ranges = max77503_buck_ranges,
63 	.n_linear_ranges = ARRAY_SIZE(max77503_buck_ranges),
64 	.vsel_reg = MAX77503_REG_VOUT,
65 	.vsel_mask = MAX77503_BITS_MX_VOUT,
66 	.soft_start_reg = MAX77503_REG_CFG,
67 	.soft_start_mask = MAX77503_BITS_SOFT_START,
68 	.active_discharge_reg = MAX77503_REG_CFG,
69 	.active_discharge_mask = MAX77503_BIT_ADEN,
70 	.active_discharge_off = MAX77503_AD_DISABLED,
71 	.active_discharge_on = MAX77503_AD_ENABLED,
72 	.csel_reg = MAX77503_REG_CFG,
73 	.csel_mask = MAX77503_BIT_CURR_LIM,
74 	.curr_table = max77503_current_limit_table,
75 	.n_current_limits = ARRAY_SIZE(max77503_current_limit_table),
76 	.owner = THIS_MODULE,
77 };
78 
79 static int max77503_regulator_probe(struct i2c_client *client)
80 {
81 	struct device *dev = &client->dev;
82 	struct regulator_config config = {};
83 	struct regulator_dev *rdev;
84 
85 	config.dev = dev;
86 	config.of_node = dev->of_node;
87 	config.regmap = devm_regmap_init_i2c(client, &max77503_regmap_config);
88 	if (IS_ERR(config.regmap)) {
89 		dev_err(dev, "Failed to init regmap");
90 		return PTR_ERR(config.regmap);
91 	}
92 
93 	rdev = devm_regulator_register(dev, &max77503_regulators_desc, &config);
94 	if (IS_ERR(rdev)) {
95 		dev_err(dev, "Failed to register regulator MAX77503");
96 		return PTR_ERR(rdev);
97 	}
98 
99 	return 0;
100 }
101 
102 static const struct of_device_id of_max77503_match_tbl[] = {
103 	{ .compatible = "adi,max77503", },
104 	{ }
105 };
106 
107 MODULE_DEVICE_TABLE(of, of_max77503_match_tbl);
108 
109 static const struct i2c_device_id max77503_regulator_id[] = {
110 	{"max77503"},
111 	{ }
112 };
113 
114 MODULE_DEVICE_TABLE(i2c, max77503_regulator_id);
115 
116 static struct i2c_driver max77503_regulator_driver = {
117 	.driver = {
118 		.name = "max77503",
119 		.of_match_table = of_max77503_match_tbl
120 	},
121 	.probe = max77503_regulator_probe,
122 	.id_table = max77503_regulator_id,
123 };
124 
125 module_i2c_driver(max77503_regulator_driver);
126 
127 MODULE_AUTHOR("Gokhan Celik <Gokhan.Celik@analog.com>");
128 MODULE_DESCRIPTION("MAX77503 regulator driver");
129 MODULE_LICENSE("GPL");
130