]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/regulator/hi655x-regulator.c
regulator: vexpress: Switch to SPDX identifier
[mirror_ubuntu-jammy-kernel.git] / drivers / regulator / hi655x-regulator.c
CommitLineData
4618119b
CF
1/*
2 * Device driver for regulators in Hi655x IC
3 *
4 * Copyright (c) 2016 Hisilicon.
5 *
6 * Authors:
7 * Chen Feng <puck.chen@hisilicon.com>
8 * Fei Wang <w.f@huawei.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/bitops.h>
16#include <linux/device.h>
17#include <linux/err.h>
18#include <linux/module.h>
19#include <linux/io.h>
20#include <linux/of.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/machine.h>
25#include <linux/regulator/of_regulator.h>
26#include <linux/mfd/hi655x-pmic.h>
27
28struct hi655x_regulator {
29 unsigned int disable_reg;
30 unsigned int status_reg;
4618119b
CF
31 struct regulator_desc rdesc;
32};
33
34/* LDO7 & LDO10 */
35static const unsigned int ldo7_voltages[] = {
36 1800000, 1850000, 2850000, 2900000,
37 3000000, 3100000, 3200000, 3300000,
38};
39
40static const unsigned int ldo19_voltages[] = {
41 1800000, 1850000, 1900000, 1750000,
42 2800000, 2850000, 2900000, 3000000,
43};
44
45static const unsigned int ldo22_voltages[] = {
46 900000, 1000000, 1050000, 1100000,
47 1150000, 1175000, 1185000, 1200000,
48};
49
50enum hi655x_regulator_id {
51 HI655X_LDO0,
52 HI655X_LDO1,
53 HI655X_LDO2,
54 HI655X_LDO3,
55 HI655X_LDO4,
56 HI655X_LDO5,
57 HI655X_LDO6,
58 HI655X_LDO7,
59 HI655X_LDO8,
60 HI655X_LDO9,
61 HI655X_LDO10,
62 HI655X_LDO11,
63 HI655X_LDO12,
64 HI655X_LDO13,
65 HI655X_LDO14,
66 HI655X_LDO15,
67 HI655X_LDO16,
68 HI655X_LDO17,
69 HI655X_LDO18,
70 HI655X_LDO19,
71 HI655X_LDO20,
72 HI655X_LDO21,
73 HI655X_LDO22,
74};
75
76static int hi655x_is_enabled(struct regulator_dev *rdev)
77{
78 unsigned int value = 0;
4618119b
CF
79 struct hi655x_regulator *regulator = rdev_get_drvdata(rdev);
80
81 regmap_read(rdev->regmap, regulator->status_reg, &value);
29e5cb65 82 return (value & rdev->desc->enable_mask);
4618119b
CF
83}
84
85static int hi655x_disable(struct regulator_dev *rdev)
86{
4618119b
CF
87 struct hi655x_regulator *regulator = rdev_get_drvdata(rdev);
88
29e5cb65
AL
89 return regmap_write(rdev->regmap, regulator->disable_reg,
90 rdev->desc->enable_mask);
4618119b
CF
91}
92
dc83c94a 93static const struct regulator_ops hi655x_regulator_ops = {
4618119b
CF
94 .enable = regulator_enable_regmap,
95 .disable = hi655x_disable,
96 .is_enabled = hi655x_is_enabled,
97 .list_voltage = regulator_list_voltage_table,
98 .get_voltage_sel = regulator_get_voltage_sel_regmap,
99 .set_voltage_sel = regulator_set_voltage_sel_regmap,
100};
101
dc83c94a 102static const struct regulator_ops hi655x_ldo_linear_ops = {
4618119b
CF
103 .enable = regulator_enable_regmap,
104 .disable = hi655x_disable,
105 .is_enabled = hi655x_is_enabled,
106 .list_voltage = regulator_list_voltage_linear,
107 .get_voltage_sel = regulator_get_voltage_sel_regmap,
108 .set_voltage_sel = regulator_set_voltage_sel_regmap,
109};
110
111#define HI655X_LDO(_ID, vreg, vmask, ereg, dreg, \
112 sreg, cmask, vtable) { \
113 .rdesc = { \
114 .name = #_ID, \
115 .of_match = of_match_ptr(#_ID), \
116 .ops = &hi655x_regulator_ops, \
117 .regulators_node = of_match_ptr("regulators"), \
118 .type = REGULATOR_VOLTAGE, \
119 .id = HI655X_##_ID, \
120 .owner = THIS_MODULE, \
121 .n_voltages = ARRAY_SIZE(vtable), \
122 .volt_table = vtable, \
123 .vsel_reg = HI655X_BUS_ADDR(vreg), \
124 .vsel_mask = vmask, \
125 .enable_reg = HI655X_BUS_ADDR(ereg), \
126 .enable_mask = BIT(cmask), \
127 }, \
128 .disable_reg = HI655X_BUS_ADDR(dreg), \
129 .status_reg = HI655X_BUS_ADDR(sreg), \
4618119b
CF
130}
131
132#define HI655X_LDO_LINEAR(_ID, vreg, vmask, ereg, dreg, \
133 sreg, cmask, minv, nvolt, vstep) { \
134 .rdesc = { \
135 .name = #_ID, \
136 .of_match = of_match_ptr(#_ID), \
137 .ops = &hi655x_ldo_linear_ops, \
138 .regulators_node = of_match_ptr("regulators"), \
139 .type = REGULATOR_VOLTAGE, \
140 .id = HI655X_##_ID, \
141 .owner = THIS_MODULE, \
142 .min_uV = minv, \
143 .n_voltages = nvolt, \
144 .uV_step = vstep, \
145 .vsel_reg = HI655X_BUS_ADDR(vreg), \
146 .vsel_mask = vmask, \
147 .enable_reg = HI655X_BUS_ADDR(ereg), \
148 .enable_mask = BIT(cmask), \
149 }, \
150 .disable_reg = HI655X_BUS_ADDR(dreg), \
151 .status_reg = HI655X_BUS_ADDR(sreg), \
4618119b
CF
152}
153
784816d4 154static const struct hi655x_regulator regulators[] = {
4618119b
CF
155 HI655X_LDO_LINEAR(LDO2, 0x72, 0x07, 0x29, 0x2a, 0x2b, 0x01,
156 2500000, 8, 100000),
157 HI655X_LDO(LDO7, 0x78, 0x07, 0x29, 0x2a, 0x2b, 0x06, ldo7_voltages),
158 HI655X_LDO(LDO10, 0x78, 0x07, 0x29, 0x2a, 0x2b, 0x01, ldo7_voltages),
159 HI655X_LDO_LINEAR(LDO13, 0x7e, 0x07, 0x2c, 0x2d, 0x2e, 0x04,
160 1600000, 8, 50000),
161 HI655X_LDO_LINEAR(LDO14, 0x7f, 0x07, 0x2c, 0x2d, 0x2e, 0x05,
162 2500000, 8, 100000),
163 HI655X_LDO_LINEAR(LDO15, 0x80, 0x07, 0x2c, 0x2d, 0x2e, 0x06,
164 1600000, 8, 50000),
165 HI655X_LDO_LINEAR(LDO17, 0x82, 0x07, 0x2f, 0x30, 0x31, 0x00,
166 2500000, 8, 100000),
167 HI655X_LDO(LDO19, 0x84, 0x07, 0x2f, 0x30, 0x31, 0x02, ldo19_voltages),
168 HI655X_LDO_LINEAR(LDO21, 0x86, 0x07, 0x2f, 0x30, 0x31, 0x04,
169 1650000, 8, 50000),
170 HI655X_LDO(LDO22, 0x87, 0x07, 0x2f, 0x30, 0x31, 0x05, ldo22_voltages),
171};
172
173static int hi655x_regulator_probe(struct platform_device *pdev)
174{
175 unsigned int i;
176 struct hi655x_regulator *regulator;
177 struct hi655x_pmic *pmic;
178 struct regulator_config config = { };
179 struct regulator_dev *rdev;
180
181 pmic = dev_get_drvdata(pdev->dev.parent);
182 if (!pmic) {
183 dev_err(&pdev->dev, "no pmic in the regulator parent node\n");
184 return -ENODEV;
185 }
186
187 regulator = devm_kzalloc(&pdev->dev, sizeof(*regulator), GFP_KERNEL);
188 if (!regulator)
189 return -ENOMEM;
190
191 platform_set_drvdata(pdev, regulator);
192
193 config.dev = pdev->dev.parent;
194 config.regmap = pmic->regmap;
195 config.driver_data = regulator;
196 for (i = 0; i < ARRAY_SIZE(regulators); i++) {
197 rdev = devm_regulator_register(&pdev->dev,
198 &regulators[i].rdesc,
199 &config);
200 if (IS_ERR(rdev)) {
201 dev_err(&pdev->dev, "failed to register regulator %s\n",
202 regulator->rdesc.name);
203 return PTR_ERR(rdev);
204 }
205 }
206 return 0;
207}
208
2f3c578f
JL
209static const struct platform_device_id hi655x_regulator_table[] = {
210 { .name = "hi655x-regulator" },
211 {},
212};
213MODULE_DEVICE_TABLE(platform, hi655x_regulator_table);
214
4618119b 215static struct platform_driver hi655x_regulator_driver = {
2f3c578f 216 .id_table = hi655x_regulator_table,
4618119b
CF
217 .driver = {
218 .name = "hi655x-regulator",
219 },
220 .probe = hi655x_regulator_probe,
221};
222module_platform_driver(hi655x_regulator_driver);
223
224MODULE_AUTHOR("Chen Feng <puck.chen@hisilicon.com>");
225MODULE_DESCRIPTION("Hisilicon Hi655x regulator driver");
226MODULE_LICENSE("GPL v2");