1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // sy8106a-regulator.c - Regulator device driver for SY8106A
4 //
5 // Copyright (C) 2016 Ondřej Jirman <megous@megous.com>
6 // Copyright (c) 2017-2018 Icenowy Zheng <icenowy@aosc.io>
7
8 #include <linux/err.h>
9 #include <linux/i2c.h>
10 #include <linux/module.h>
11 #include <linux/regmap.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/of_regulator.h>
14
15 #define SY8106A_REG_VOUT1_SEL 0x01
16 #define SY8106A_REG_VOUT_COM 0x02
17 #define SY8106A_REG_VOUT1_SEL_MASK 0x7f
18 #define SY8106A_DISABLE_REG BIT(0)
19 /*
20 * The I2C controlled voltage will only work when this bit is set; otherwise
21 * it will behave like a fixed regulator.
22 */
23 #define SY8106A_GO_BIT BIT(7)
24
25 static const struct regmap_config sy8106a_regmap_config = {
26 .reg_bits = 8,
27 .val_bits = 8,
28 };
29
30 static const struct regulator_ops sy8106a_ops = {
31 .set_voltage_sel = regulator_set_voltage_sel_regmap,
32 .set_voltage_time_sel = regulator_set_voltage_time_sel,
33 .get_voltage_sel = regulator_get_voltage_sel_regmap,
34 .list_voltage = regulator_list_voltage_linear,
35 /* Enabling/disabling the regulator is not yet implemented */
36 };
37
38 /* Default limits measured in millivolts */
39 #define SY8106A_MIN_MV 680
40 #define SY8106A_MAX_MV 1950
41 #define SY8106A_STEP_MV 10
42
43 static const struct regulator_desc sy8106a_reg = {
44 .name = "SY8106A",
45 .id = 0,
46 .ops = &sy8106a_ops,
47 .type = REGULATOR_VOLTAGE,
48 .n_voltages = ((SY8106A_MAX_MV - SY8106A_MIN_MV) / SY8106A_STEP_MV) + 1,
49 .min_uV = (SY8106A_MIN_MV * 1000),
50 .uV_step = (SY8106A_STEP_MV * 1000),
51 .vsel_reg = SY8106A_REG_VOUT1_SEL,
52 .vsel_mask = SY8106A_REG_VOUT1_SEL_MASK,
53 /*
54 * This ramp_delay is a conservative default value which works on
55 * H3/H5 boards VDD-CPUX situations.
56 */
57 .ramp_delay = 200,
58 .owner = THIS_MODULE,
59 };
60
61 /*
62 * I2C driver interface functions
63 */
sy8106a_i2c_probe(struct i2c_client * i2c)64 static int sy8106a_i2c_probe(struct i2c_client *i2c)
65 {
66 struct device *dev = &i2c->dev;
67 struct regulator_dev *rdev;
68 struct regulator_config config = { };
69 struct regmap *regmap;
70 unsigned int reg, vsel;
71 u32 fixed_voltage;
72 int error;
73
74 error = of_property_read_u32(dev->of_node, "silergy,fixed-microvolt",
75 &fixed_voltage);
76 if (error)
77 return error;
78
79 if (fixed_voltage < SY8106A_MIN_MV * 1000 ||
80 fixed_voltage > SY8106A_MAX_MV * 1000)
81 return -EINVAL;
82
83 regmap = devm_regmap_init_i2c(i2c, &sy8106a_regmap_config);
84 if (IS_ERR(regmap)) {
85 error = PTR_ERR(regmap);
86 dev_err(dev, "Failed to allocate register map: %d\n", error);
87 return error;
88 }
89
90 config.dev = &i2c->dev;
91 config.regmap = regmap;
92
93 config.of_node = dev->of_node;
94 config.init_data = of_get_regulator_init_data(dev, dev->of_node,
95 &sy8106a_reg);
96
97 if (!config.init_data)
98 return -ENOMEM;
99
100 /* Ensure GO_BIT is enabled when probing */
101 error = regmap_read(regmap, SY8106A_REG_VOUT1_SEL, ®);
102 if (error)
103 return error;
104
105 if (!(reg & SY8106A_GO_BIT)) {
106 vsel = (fixed_voltage / 1000 - SY8106A_MIN_MV) /
107 SY8106A_STEP_MV;
108
109 error = regmap_write(regmap, SY8106A_REG_VOUT1_SEL,
110 vsel | SY8106A_GO_BIT);
111 if (error)
112 return error;
113 }
114
115 /* Probe regulator */
116 rdev = devm_regulator_register(&i2c->dev, &sy8106a_reg, &config);
117 if (IS_ERR(rdev)) {
118 error = PTR_ERR(rdev);
119 dev_err(&i2c->dev, "Failed to register SY8106A regulator: %d\n", error);
120 return error;
121 }
122
123 return 0;
124 }
125
126 static const struct of_device_id sy8106a_i2c_of_match[] = {
127 { .compatible = "silergy,sy8106a" },
128 { },
129 };
130 MODULE_DEVICE_TABLE(of, sy8106a_i2c_of_match);
131
132 static const struct i2c_device_id sy8106a_i2c_id[] = {
133 { "sy8106a", 0 },
134 { },
135 };
136 MODULE_DEVICE_TABLE(i2c, sy8106a_i2c_id);
137
138 static struct i2c_driver sy8106a_regulator_driver = {
139 .driver = {
140 .name = "sy8106a",
141 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
142 .of_match_table = sy8106a_i2c_of_match,
143 },
144 .probe = sy8106a_i2c_probe,
145 .id_table = sy8106a_i2c_id,
146 };
147
148 module_i2c_driver(sy8106a_regulator_driver);
149
150 MODULE_AUTHOR("Ondřej Jirman <megous@megous.com>");
151 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
152 MODULE_DESCRIPTION("Regulator device driver for Silergy SY8106A");
153 MODULE_LICENSE("GPL");
154