blob: 285acc705a556b8f88999e9683ef8ba79d41cbfd [file] [log] [blame]
Christoph Fritze6dea512020-07-02 23:08:45 +02001// SPDX-License-Identifier: GPL-2.0+
2#include <linux/module.h>
3#include <linux/i2c.h>
4#include <linux/regmap.h>
5#include <linux/regulator/driver.h>
6
7enum fan53880_regulator_ids {
8 FAN53880_LDO1,
9 FAN53880_LDO2,
10 FAN53880_LDO3,
11 FAN53880_LDO4,
12 FAN53880_BUCK,
13 FAN53880_BOOST,
14};
15
16enum fan53880_registers {
17 FAN53880_PRODUCT_ID = 0x00,
18 FAN53880_SILICON_REV,
19 FAN53880_BUCKVOUT,
20 FAN53880_BOOSTVOUT,
21 FAN53880_LDO1VOUT,
22 FAN53880_LDO2VOUT,
23 FAN53880_LDO3VOUT,
24 FAN53880_LDO4VOUT,
25 FAN53880_IOUT,
26 FAN53880_ENABLE,
27 FAN53880_ENABLE_BOOST,
28};
29
30#define FAN53880_ID 0x01
31
32static const struct regulator_ops fan53880_ops = {
33 .list_voltage = regulator_list_voltage_linear_range,
34 .map_voltage = regulator_map_voltage_linear_range,
35 .set_voltage_sel = regulator_set_voltage_sel_regmap,
36 .get_voltage_sel = regulator_get_voltage_sel_regmap,
37 .enable = regulator_enable_regmap,
38 .disable = regulator_disable_regmap,
39 .is_enabled = regulator_is_enabled_regmap,
40};
41
42#define FAN53880_LDO(_num, _supply, _default) \
43 [FAN53880_LDO ## _num] = { \
44 .name = "LDO"#_num, \
45 .of_match = of_match_ptr("LDO"#_num), \
46 .regulators_node = of_match_ptr("regulators"), \
47 .type = REGULATOR_VOLTAGE, \
48 .linear_ranges = (struct linear_range[]) { \
49 REGULATOR_LINEAR_RANGE(_default, 0x0, 0x0, 0), \
50 REGULATOR_LINEAR_RANGE(800000, 0xf, 0x73, 25000), \
51 }, \
52 .n_linear_ranges = 2, \
53 .vsel_reg = FAN53880_LDO ## _num ## VOUT, \
54 .vsel_mask = 0x7f, \
55 .enable_reg = FAN53880_ENABLE, \
56 .enable_mask = BIT(_num - 1), \
57 .enable_time = 150, \
58 .supply_name = _supply, \
59 .ops = &fan53880_ops, \
60 }
61
62static const struct regulator_desc fan53880_regulators[] = {
63 FAN53880_LDO(1, "VIN12", 2800000),
64 FAN53880_LDO(2, "VIN12", 2800000),
65 FAN53880_LDO(3, "VIN3", 1800000),
66 FAN53880_LDO(4, "VIN4", 1800000),
67 [FAN53880_BUCK] = {
68 .name = "BUCK",
69 .of_match = of_match_ptr("BUCK"),
70 .regulators_node = of_match_ptr("regulators"),
71 .type = REGULATOR_VOLTAGE,
72 .linear_ranges = (struct linear_range[]) {
73 REGULATOR_LINEAR_RANGE(1100000, 0x0, 0x0, 0),
74 REGULATOR_LINEAR_RANGE(600000, 0x1f, 0xf7, 12500),
75 },
76 .n_linear_ranges = 2,
77 .vsel_reg = FAN53880_BUCKVOUT,
78 .vsel_mask = 0x7f,
79 .enable_reg = FAN53880_ENABLE,
80 .enable_mask = 0x10,
81 .enable_time = 480,
82 .supply_name = "PVIN",
83 .ops = &fan53880_ops,
84 },
85 [FAN53880_BOOST] = {
86 .name = "BOOST",
87 .of_match = of_match_ptr("BOOST"),
88 .regulators_node = of_match_ptr("regulators"),
89 .type = REGULATOR_VOLTAGE,
90 .linear_ranges = (struct linear_range[]) {
91 REGULATOR_LINEAR_RANGE(5000000, 0x0, 0x0, 0),
92 REGULATOR_LINEAR_RANGE(3000000, 0x4, 0x70, 25000),
93 },
94 .n_linear_ranges = 2,
95 .vsel_reg = FAN53880_BOOSTVOUT,
96 .vsel_mask = 0x7f,
97 .enable_reg = FAN53880_ENABLE_BOOST,
98 .enable_mask = 0xff,
99 .enable_time = 580,
100 .supply_name = "PVIN",
101 .ops = &fan53880_ops,
102 },
103};
104
105static const struct regmap_config fan53880_regmap = {
106 .reg_bits = 8,
107 .val_bits = 8,
108 .max_register = FAN53880_ENABLE_BOOST,
109};
110
111static int fan53880_i2c_probe(struct i2c_client *i2c,
112 const struct i2c_device_id *id)
113{
114 struct regulator_config config = { };
115 struct regulator_dev *rdev;
116 struct regmap *regmap;
117 int i, ret;
118 unsigned int data;
119
120 regmap = devm_regmap_init_i2c(i2c, &fan53880_regmap);
121 if (IS_ERR(regmap)) {
122 ret = PTR_ERR(regmap);
123 dev_err(&i2c->dev, "Failed to create regmap: %d\n", ret);
124 return ret;
125 }
126
127 ret = regmap_read(regmap, FAN53880_PRODUCT_ID, &data);
128 if (ret < 0) {
129 dev_err(&i2c->dev, "Failed to read PRODUCT_ID: %d\n", ret);
130 return ret;
131 }
132 if (data != FAN53880_ID) {
133 dev_err(&i2c->dev, "Unsupported device id: 0x%x.\n", data);
134 return -ENODEV;
135 }
136
137 config.dev = &i2c->dev;
138 config.init_data = NULL;
139
140 for (i = 0; i < ARRAY_SIZE(fan53880_regulators); i++) {
141 rdev = devm_regulator_register(&i2c->dev,
142 &fan53880_regulators[i],
143 &config);
144 if (IS_ERR(rdev)) {
145 ret = PTR_ERR(rdev);
146 dev_err(&i2c->dev, "Failed to register %s: %d\n",
147 fan53880_regulators[i].name, ret);
148 return ret;
149 }
150 }
151
152 return 0;
153}
154
155static const struct of_device_id fan53880_dt_ids[] = {
156 { .compatible = "onnn,fan53880", },
157 {}
158};
159MODULE_DEVICE_TABLE(of, fan53880_dt_ids);
160
161static const struct i2c_device_id fan53880_i2c_id[] = {
162 { "fan53880", },
163 {}
164};
165MODULE_DEVICE_TABLE(i2c, fan53880_i2c_id);
166
167static struct i2c_driver fan53880_regulator_driver = {
168 .driver = {
169 .name = "fan53880",
170 .of_match_table = of_match_ptr(fan53880_dt_ids),
171 },
172 .probe = fan53880_i2c_probe,
173 .id_table = fan53880_i2c_id,
174};
175module_i2c_driver(fan53880_regulator_driver);
176
177MODULE_DESCRIPTION("FAN53880 PMIC voltage regulator driver");
178MODULE_AUTHOR("Christoph Fritz <chf.fritz@googlemail.com>");
179MODULE_LICENSE("GPL");