1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright Axis Communications AB
6 #include <linux/module.h>
8 #include <linux/regmap.h>
9 #include <linux/regulator/of_regulator.h>
10 #include <linux/regulator/machine.h>
11 #include <linux/regulator/driver.h>
13 #include <dt-bindings/regulator/ti,tps62864.h>
15 #define TPS6286X_VOUT1 0x01
16 #define TPS6286X_VOUT1_VO1_SET GENMASK(7, 0)
18 #define TPS6286X_CONTROL 0x03
19 #define TPS6286X_CONTROL_FPWM BIT(4)
20 #define TPS6286X_CONTROL_SWEN BIT(5)
22 #define TPS6286X_MIN_MV 400
23 #define TPS6286X_MAX_MV 1675
24 #define TPS6286X_STEP_MV 5
26 static const struct regmap_config tps6286x_regmap_config
= {
31 static int tps6286x_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
36 case REGULATOR_MODE_NORMAL
:
39 case REGULATOR_MODE_FAST
:
40 val
= TPS6286X_CONTROL_FPWM
;
46 return regmap_update_bits(rdev
->regmap
, TPS6286X_CONTROL
,
47 TPS6286X_CONTROL_FPWM
, val
);
50 static unsigned int tps6286x_get_mode(struct regulator_dev
*rdev
)
55 ret
= regmap_read(rdev
->regmap
, TPS6286X_CONTROL
, &val
);
59 return (val
& TPS6286X_CONTROL_FPWM
) ? REGULATOR_MODE_FAST
: REGULATOR_MODE_NORMAL
;
62 static const struct regulator_ops tps6286x_regulator_ops
= {
63 .enable
= regulator_enable_regmap
,
64 .disable
= regulator_disable_regmap
,
65 .set_mode
= tps6286x_set_mode
,
66 .get_mode
= tps6286x_get_mode
,
67 .is_enabled
= regulator_is_enabled_regmap
,
68 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
69 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
70 .list_voltage
= regulator_list_voltage_linear
,
73 static unsigned int tps6286x_of_map_mode(unsigned int mode
)
76 case TPS62864_MODE_NORMAL
:
77 return REGULATOR_MODE_NORMAL
;
78 case TPS62864_MODE_FPWM
:
79 return REGULATOR_MODE_FAST
;
81 return REGULATOR_MODE_INVALID
;
85 static const struct regulator_desc tps6286x_reg
= {
89 .ops
= &tps6286x_regulator_ops
,
90 .of_map_mode
= tps6286x_of_map_mode
,
91 .regulators_node
= "regulators",
92 .type
= REGULATOR_VOLTAGE
,
93 .n_voltages
= ((TPS6286X_MAX_MV
- TPS6286X_MIN_MV
) / TPS6286X_STEP_MV
) + 1,
94 .min_uV
= TPS6286X_MIN_MV
* 1000,
95 .uV_step
= TPS6286X_STEP_MV
* 1000,
96 .vsel_reg
= TPS6286X_VOUT1
,
97 .vsel_mask
= TPS6286X_VOUT1_VO1_SET
,
98 .enable_reg
= TPS6286X_CONTROL
,
99 .enable_mask
= TPS6286X_CONTROL_SWEN
,
101 /* tDelay + tRamp, rounded up */
105 static const struct of_device_id tps6286x_dt_ids
[] = {
106 { .compatible
= "ti,tps62864", },
107 { .compatible
= "ti,tps62866", },
108 { .compatible
= "ti,tps62868", },
109 { .compatible
= "ti,tps62869", },
112 MODULE_DEVICE_TABLE(of
, tps6286x_dt_ids
);
114 static int tps6286x_i2c_probe(struct i2c_client
*i2c
)
116 struct device
*dev
= &i2c
->dev
;
117 struct regulator_config config
= {};
118 struct regulator_dev
*rdev
;
119 struct regmap
*regmap
;
121 regmap
= devm_regmap_init_i2c(i2c
, &tps6286x_regmap_config
);
123 return PTR_ERR(regmap
);
125 config
.dev
= &i2c
->dev
;
126 config
.of_node
= dev
->of_node
;
127 config
.regmap
= regmap
;
129 rdev
= devm_regulator_register(&i2c
->dev
, &tps6286x_reg
, &config
);
131 dev_err(&i2c
->dev
, "Failed to register tps6286x regulator\n");
132 return PTR_ERR(rdev
);
138 static const struct i2c_device_id tps6286x_i2c_id
[] = {
145 MODULE_DEVICE_TABLE(i2c
, tps6286x_i2c_id
);
147 static struct i2c_driver tps6286x_regulator_driver
= {
150 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
151 .of_match_table
= tps6286x_dt_ids
,
153 .probe
= tps6286x_i2c_probe
,
154 .id_table
= tps6286x_i2c_id
,
157 module_i2c_driver(tps6286x_regulator_driver
);
159 MODULE_DESCRIPTION("TI TPS6286x Power Regulator driver");
160 MODULE_LICENSE("GPL v2");