1 // SPDX-License-Identifier: GPL-2.0-only
3 * Device driver for RT5739 regulator
5 * Copyright (C) 2023 Richtek Technology Corp.
7 * Author: ChiYuan Huang <cy_huang@richtek.com>
10 #include <linux/bits.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/kernel.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/of_regulator.h>
20 #define RT5739_AUTO_MODE 0
21 #define RT5739_FPWM_MODE 1
23 #define RT5739_REG_NSEL0 0x00
24 #define RT5739_REG_NSEL1 0x01
25 #define RT5739_REG_CNTL1 0x02
26 #define RT5739_REG_ID1 0x03
27 #define RT5739_REG_CNTL2 0x06
28 #define RT5739_REG_CNTL4 0x08
30 #define RT5739_VSEL_MASK GENMASK(7, 0)
31 #define RT5739_MODEVSEL1_MASK BIT(1)
32 #define RT5739_MODEVSEL0_MASK BIT(0)
33 #define RT5739_VID_MASK GENMASK(7, 5)
34 #define RT5739_DID_MASK GENMASK(3, 0)
35 #define RT5739_ACTD_MASK BIT(7)
36 #define RT5739_ENVSEL1_MASK BIT(1)
37 #define RT5739_ENVSEL0_MASK BIT(0)
39 #define RT5733_CHIPDIE_ID 0x1
40 #define RT5733_VOLT_MINUV 270000
41 #define RT5733_VOLT_MAXUV 1401250
42 #define RT5733_VOLT_STPUV 6250
43 #define RT5733_N_VOLTS 182
45 #define RT5739_VOLT_MINUV 300000
46 #define RT5739_VOLT_MAXUV 1300000
47 #define RT5739_VOLT_STPUV 5000
48 #define RT5739_N_VOLTS 201
49 #define RT5739_I2CRDY_TIMEUS 1000
51 static int rt5739_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
53 const struct regulator_desc
*desc
= rdev
->desc
;
54 struct regmap
*regmap
= rdev_get_regmap(rdev
);
55 unsigned int mask
, val
;
57 if (desc
->vsel_reg
== RT5739_REG_NSEL0
)
58 mask
= RT5739_MODEVSEL0_MASK
;
60 mask
= RT5739_MODEVSEL1_MASK
;
63 case REGULATOR_MODE_FAST
:
66 case REGULATOR_MODE_NORMAL
:
73 return regmap_update_bits(regmap
, RT5739_REG_CNTL1
, mask
, val
);
76 static unsigned int rt5739_get_mode(struct regulator_dev
*rdev
)
78 const struct regulator_desc
*desc
= rdev
->desc
;
79 struct regmap
*regmap
= rdev_get_regmap(rdev
);
80 unsigned int mask
, val
;
83 if (desc
->vsel_reg
== RT5739_REG_NSEL0
)
84 mask
= RT5739_MODEVSEL0_MASK
;
86 mask
= RT5739_MODEVSEL1_MASK
;
88 ret
= regmap_read(regmap
, RT5739_REG_CNTL1
, &val
);
90 return REGULATOR_MODE_INVALID
;
93 return REGULATOR_MODE_FAST
;
95 return REGULATOR_MODE_NORMAL
;
98 static int rt5739_set_suspend_voltage(struct regulator_dev
*rdev
, int uV
)
100 const struct regulator_desc
*desc
= rdev
->desc
;
101 struct regmap
*regmap
= rdev_get_regmap(rdev
);
102 unsigned int reg
, vsel
;
105 max_uV
= desc
->min_uV
+ desc
->uV_step
* (desc
->n_voltages
- 1);
107 if (uV
< desc
->min_uV
|| uV
> max_uV
)
110 if (desc
->vsel_reg
== RT5739_REG_NSEL0
)
111 reg
= RT5739_REG_NSEL1
;
113 reg
= RT5739_REG_NSEL0
;
115 vsel
= (uV
- desc
->min_uV
) / desc
->uV_step
;
116 return regmap_write(regmap
, reg
, vsel
);
119 static int rt5739_set_suspend_enable(struct regulator_dev
*rdev
)
121 const struct regulator_desc
*desc
= rdev
->desc
;
122 struct regmap
*regmap
= rdev_get_regmap(rdev
);
125 if (desc
->vsel_reg
== RT5739_REG_NSEL0
)
126 mask
= RT5739_ENVSEL1_MASK
;
128 mask
= RT5739_ENVSEL0_MASK
;
130 return regmap_update_bits(regmap
, desc
->enable_reg
, mask
, mask
);
133 static int rt5739_set_suspend_disable(struct regulator_dev
*rdev
)
135 const struct regulator_desc
*desc
= rdev
->desc
;
136 struct regmap
*regmap
= rdev_get_regmap(rdev
);
139 if (desc
->vsel_reg
== RT5739_REG_NSEL0
)
140 mask
= RT5739_ENVSEL1_MASK
;
142 mask
= RT5739_ENVSEL0_MASK
;
144 return regmap_update_bits(regmap
, desc
->enable_reg
, mask
, 0);
147 static int rt5739_set_suspend_mode(struct regulator_dev
*rdev
,
150 const struct regulator_desc
*desc
= rdev
->desc
;
151 struct regmap
*regmap
= rdev_get_regmap(rdev
);
152 unsigned int mask
, val
;
154 if (desc
->vsel_reg
== RT5739_REG_NSEL0
)
155 mask
= RT5739_MODEVSEL1_MASK
;
157 mask
= RT5739_MODEVSEL0_MASK
;
160 case REGULATOR_MODE_FAST
:
163 case REGULATOR_MODE_NORMAL
:
170 return regmap_update_bits(regmap
, RT5739_REG_CNTL1
, mask
, val
);
173 static const struct regulator_ops rt5739_regulator_ops
= {
174 .list_voltage
= regulator_list_voltage_linear
,
175 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
176 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
177 .enable
= regulator_enable_regmap
,
178 .disable
= regulator_disable_regmap
,
179 .is_enabled
= regulator_is_enabled_regmap
,
180 .set_active_discharge
= regulator_set_active_discharge_regmap
,
181 .set_mode
= rt5739_set_mode
,
182 .get_mode
= rt5739_get_mode
,
183 .set_suspend_voltage
= rt5739_set_suspend_voltage
,
184 .set_suspend_enable
= rt5739_set_suspend_enable
,
185 .set_suspend_disable
= rt5739_set_suspend_disable
,
186 .set_suspend_mode
= rt5739_set_suspend_mode
,
189 static unsigned int rt5739_of_map_mode(unsigned int mode
)
192 case RT5739_AUTO_MODE
:
193 return REGULATOR_MODE_NORMAL
;
194 case RT5739_FPWM_MODE
:
195 return REGULATOR_MODE_FAST
;
197 return REGULATOR_MODE_INVALID
;
201 static void rt5739_init_regulator_desc(struct regulator_desc
*desc
,
202 bool vsel_active_high
, u8 did
)
205 desc
->name
= "rt5739-regulator";
206 desc
->owner
= THIS_MODULE
;
207 desc
->ops
= &rt5739_regulator_ops
;
208 desc
->vsel_mask
= RT5739_VSEL_MASK
;
209 desc
->enable_reg
= RT5739_REG_CNTL2
;
210 desc
->active_discharge_reg
= RT5739_REG_CNTL1
;
211 desc
->active_discharge_mask
= RT5739_ACTD_MASK
;
212 desc
->active_discharge_on
= RT5739_ACTD_MASK
;
213 desc
->of_map_mode
= rt5739_of_map_mode
;
215 /* Assigned by vsel level */
216 if (vsel_active_high
) {
217 desc
->vsel_reg
= RT5739_REG_NSEL1
;
218 desc
->enable_mask
= RT5739_ENVSEL1_MASK
;
220 desc
->vsel_reg
= RT5739_REG_NSEL0
;
221 desc
->enable_mask
= RT5739_ENVSEL0_MASK
;
224 /* Assigned by CHIPDIE ID */
226 case RT5733_CHIPDIE_ID
:
227 desc
->n_voltages
= RT5733_N_VOLTS
;
228 desc
->min_uV
= RT5733_VOLT_MINUV
;
229 desc
->uV_step
= RT5733_VOLT_STPUV
;
232 desc
->n_voltages
= RT5739_N_VOLTS
;
233 desc
->min_uV
= RT5739_VOLT_MINUV
;
234 desc
->uV_step
= RT5739_VOLT_STPUV
;
239 static const struct regmap_config rt5739_regmap_config
= {
243 .max_register
= RT5739_REG_CNTL4
,
246 static int rt5739_probe(struct i2c_client
*i2c
)
248 struct device
*dev
= &i2c
->dev
;
249 struct regulator_desc
*desc
;
250 struct regmap
*regmap
;
251 struct gpio_desc
*enable_gpio
;
252 struct regulator_config cfg
= {};
253 struct regulator_dev
*rdev
;
258 desc
= devm_kzalloc(dev
, sizeof(*desc
), GFP_KERNEL
);
262 enable_gpio
= devm_gpiod_get_optional(dev
, "enable", GPIOD_OUT_HIGH
);
263 if (IS_ERR(enable_gpio
))
264 return dev_err_probe(dev
, PTR_ERR(enable_gpio
), "Failed to get 'enable' gpio\n");
265 else if (enable_gpio
)
266 usleep_range(RT5739_I2CRDY_TIMEUS
, RT5739_I2CRDY_TIMEUS
+ 1000);
268 regmap
= devm_regmap_init_i2c(i2c
, &rt5739_regmap_config
);
270 return dev_err_probe(dev
, PTR_ERR(regmap
), "Failed to init regmap\n");
272 ret
= regmap_read(regmap
, RT5739_REG_ID1
, &vid
);
274 return dev_err_probe(dev
, ret
, "Failed to read VID\n");
276 /* RT5739: (VID & MASK) must be 0 */
277 if (vid
& RT5739_VID_MASK
)
278 return dev_err_probe(dev
, -ENODEV
, "Incorrect VID (0x%02x)\n", vid
);
280 vsel_acth
= device_property_read_bool(dev
, "richtek,vsel-active-high");
282 rt5739_init_regulator_desc(desc
, vsel_acth
, vid
& RT5739_DID_MASK
);
285 cfg
.of_node
= dev_of_node(dev
);
286 cfg
.init_data
= of_get_regulator_init_data(dev
, dev_of_node(dev
), desc
);
287 rdev
= devm_regulator_register(dev
, desc
, &cfg
);
289 return dev_err_probe(dev
, PTR_ERR(rdev
), "Failed to register regulator\n");
294 static const struct of_device_id rt5739_device_table
[] = {
295 { .compatible
= "richtek,rt5733" },
296 { .compatible
= "richtek,rt5739" },
299 MODULE_DEVICE_TABLE(of
, rt5739_device_table
);
301 static struct i2c_driver rt5739_driver
= {
304 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
305 .of_match_table
= rt5739_device_table
,
307 .probe
= rt5739_probe
,
309 module_i2c_driver(rt5739_driver
);
311 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
312 MODULE_DESCRIPTION("Richtek RT5739 regulator driver");
313 MODULE_LICENSE("GPL");