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
28struct max77503_dev {
29	struct device *dev;
30	struct device_node *of_node;
31	struct regulator_desc desc;
32	struct regulator_dev *rdev;
33	struct regmap *regmap;
34};
35
36static const struct regmap_config max77503_regmap_config = {
37	.reg_bits = 8,
38	.val_bits = 8,
39	.max_register = 0x2,
40};
41
42static const struct regulator_ops max77503_buck_ops = {
43	.list_voltage = regulator_list_voltage_linear_range,
44	.map_voltage = regulator_map_voltage_ascend,
45	.is_enabled = regulator_is_enabled_regmap,
46	.enable = regulator_enable_regmap,
47	.disable = regulator_disable_regmap,
48	.get_voltage_sel = regulator_get_voltage_sel_regmap,
49	.set_voltage_sel = regulator_set_voltage_sel_regmap,
50	.get_current_limit = regulator_get_current_limit_regmap,
51	.set_current_limit = regulator_set_current_limit_regmap,
52	.set_active_discharge = regulator_set_active_discharge_regmap,
53	.set_soft_start = regulator_set_soft_start_regmap,
54};
55
56static const struct linear_range max77503_buck_ranges[] = {
57	REGULATOR_LINEAR_RANGE(800000, 0x00, 0x54, 50000)
58};
59
60static const unsigned int max77503_current_limit_table[] = {
61	500000, 2000000
62};
63
64static const struct regulator_desc max77503_regulators_desc = {
65	.name = "max77503",
66	.enable_reg = MAX77503_REG_CFG,
67	.enable_mask = MAX77503_BIT_EN,
68	.ops = &max77503_buck_ops,
69	.type = REGULATOR_VOLTAGE,
70	.linear_ranges = max77503_buck_ranges,
71	.n_linear_ranges = ARRAY_SIZE(max77503_buck_ranges),
72	.vsel_reg = MAX77503_REG_VOUT,
73	.vsel_mask = MAX77503_BITS_MX_VOUT,
74	.soft_start_reg = MAX77503_REG_CFG,
75	.soft_start_mask = MAX77503_BITS_SOFT_START,
76	.active_discharge_reg = MAX77503_REG_CFG,
77	.active_discharge_mask = MAX77503_BIT_ADEN,
78	.active_discharge_off = MAX77503_AD_DISABLED,
79	.active_discharge_on = MAX77503_AD_ENABLED,
80	.csel_reg = MAX77503_REG_CFG,
81	.csel_mask = MAX77503_BIT_CURR_LIM,
82	.curr_table = max77503_current_limit_table,
83	.n_current_limits = ARRAY_SIZE(max77503_current_limit_table),
84	.owner = THIS_MODULE,
85};
86
87static int max77503_regulator_probe(struct i2c_client *client)
88{
89	struct device *dev = &client->dev;
90	struct regulator_config config = {};
91	struct regulator_dev *rdev;
92
93	config.dev = dev;
94	config.of_node = dev->of_node;
95	config.regmap = devm_regmap_init_i2c(client, &max77503_regmap_config);
96	if (IS_ERR(config.regmap)) {
97		dev_err(dev, "Failed to init regmap");
98		return PTR_ERR(config.regmap);
99	}
100
101	rdev = devm_regulator_register(dev, &max77503_regulators_desc, &config);
102	if (IS_ERR(rdev)) {
103		dev_err(dev, "Failed to register regulator MAX77503");
104		return PTR_ERR(rdev);
105	}
106
107	return 0;
108}
109
110static const struct of_device_id of_max77503_match_tbl[] = {
111	{ .compatible = "adi,max77503", },
112	{ }
113};
114
115MODULE_DEVICE_TABLE(of, of_max77503_match_tbl);
116
117static const struct i2c_device_id max77503_regulator_id[] = {
118	{"max77503"},
119	{ }
120};
121
122MODULE_DEVICE_TABLE(i2c, max77503_regulator_id);
123
124static struct i2c_driver max77503_regulator_driver = {
125	.driver = {
126		.name = "max77503",
127		.of_match_table = of_max77503_match_tbl
128	},
129	.probe = max77503_regulator_probe,
130	.id_table = max77503_regulator_id,
131};
132
133module_i2c_driver(max77503_regulator_driver);
134
135MODULE_AUTHOR("Gokhan Celik <Gokhan.Celik@analog.com>");
136MODULE_DESCRIPTION("MAX77503 regulator driver");
137MODULE_LICENSE("GPL");
138