Loading...
1/*
2 * linux/drivers/regulator/aat2870-regulator.c
3 *
4 * Copyright (c) 2011, NVIDIA Corporation.
5 * Author: Jin Park <jinyoungp@nvidia.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28#include <linux/regulator/driver.h>
29#include <linux/regulator/machine.h>
30#include <linux/mfd/aat2870.h>
31
32struct aat2870_regulator {
33 struct platform_device *pdev;
34 struct regulator_desc desc;
35
36 const int *voltages; /* uV */
37
38 int min_uV;
39 int max_uV;
40
41 u8 enable_addr;
42 u8 enable_shift;
43 u8 enable_mask;
44
45 u8 voltage_addr;
46 u8 voltage_shift;
47 u8 voltage_mask;
48};
49
50static int aat2870_ldo_list_voltage(struct regulator_dev *rdev,
51 unsigned selector)
52{
53 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
54
55 return ri->voltages[selector];
56}
57
58static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev,
59 unsigned selector)
60{
61 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
62 struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
63
64 return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask,
65 (selector << ri->voltage_shift) & ri->voltage_mask);
66}
67
68static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev)
69{
70 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
71 struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
72 u8 val;
73 int ret;
74
75 ret = aat2870->read(aat2870, ri->voltage_addr, &val);
76 if (ret)
77 return ret;
78
79 return (val & ri->voltage_mask) >> ri->voltage_shift;
80}
81
82static int aat2870_ldo_enable(struct regulator_dev *rdev)
83{
84 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
85 struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
86
87 return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask,
88 ri->enable_mask);
89}
90
91static int aat2870_ldo_disable(struct regulator_dev *rdev)
92{
93 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
94 struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
95
96 return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, 0);
97}
98
99static int aat2870_ldo_is_enabled(struct regulator_dev *rdev)
100{
101 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
102 struct aat2870_data *aat2870 = dev_get_drvdata(ri->pdev->dev.parent);
103 u8 val;
104 int ret;
105
106 ret = aat2870->read(aat2870, ri->enable_addr, &val);
107 if (ret)
108 return ret;
109
110 return val & ri->enable_mask ? 1 : 0;
111}
112
113static struct regulator_ops aat2870_ldo_ops = {
114 .list_voltage = aat2870_ldo_list_voltage,
115 .set_voltage_sel = aat2870_ldo_set_voltage_sel,
116 .get_voltage_sel = aat2870_ldo_get_voltage_sel,
117 .enable = aat2870_ldo_enable,
118 .disable = aat2870_ldo_disable,
119 .is_enabled = aat2870_ldo_is_enabled,
120};
121
122static const int aat2870_ldo_voltages[] = {
123 1200000, 1300000, 1500000, 1600000,
124 1800000, 2000000, 2200000, 2500000,
125 2600000, 2700000, 2800000, 2900000,
126 3000000, 3100000, 3200000, 3300000,
127};
128
129#define AAT2870_LDO(ids) \
130 { \
131 .desc = { \
132 .name = #ids, \
133 .id = AAT2870_ID_##ids, \
134 .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \
135 .ops = &aat2870_ldo_ops, \
136 .type = REGULATOR_VOLTAGE, \
137 .owner = THIS_MODULE, \
138 }, \
139 .voltages = aat2870_ldo_voltages, \
140 .min_uV = 1200000, \
141 .max_uV = 3300000, \
142 }
143
144static struct aat2870_regulator aat2870_regulators[] = {
145 AAT2870_LDO(LDOA),
146 AAT2870_LDO(LDOB),
147 AAT2870_LDO(LDOC),
148 AAT2870_LDO(LDOD),
149};
150
151static struct aat2870_regulator *aat2870_get_regulator(int id)
152{
153 struct aat2870_regulator *ri = NULL;
154 int i;
155
156 for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) {
157 ri = &aat2870_regulators[i];
158 if (ri->desc.id == id)
159 break;
160 }
161
162 if (!ri)
163 return NULL;
164
165 ri->enable_addr = AAT2870_LDO_EN;
166 ri->enable_shift = id - AAT2870_ID_LDOA;
167 ri->enable_mask = 0x1 << ri->enable_shift;
168
169 ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
170 AAT2870_LDO_CD : AAT2870_LDO_AB;
171 ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4;
172 ri->voltage_mask = 0xF << ri->voltage_shift;
173
174 return ri;
175}
176
177static int aat2870_regulator_probe(struct platform_device *pdev)
178{
179 struct aat2870_regulator *ri;
180 struct regulator_dev *rdev;
181
182 ri = aat2870_get_regulator(pdev->id);
183 if (!ri) {
184 dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
185 return -EINVAL;
186 }
187 ri->pdev = pdev;
188
189 rdev = regulator_register(&ri->desc, &pdev->dev,
190 pdev->dev.platform_data, ri);
191 if (IS_ERR(rdev)) {
192 dev_err(&pdev->dev, "Failed to register regulator %s\n",
193 ri->desc.name);
194 return PTR_ERR(rdev);
195 }
196 platform_set_drvdata(pdev, rdev);
197
198 return 0;
199}
200
201static int __devexit aat2870_regulator_remove(struct platform_device *pdev)
202{
203 struct regulator_dev *rdev = platform_get_drvdata(pdev);
204
205 regulator_unregister(rdev);
206 return 0;
207}
208
209static struct platform_driver aat2870_regulator_driver = {
210 .driver = {
211 .name = "aat2870-regulator",
212 .owner = THIS_MODULE,
213 },
214 .probe = aat2870_regulator_probe,
215 .remove = __devexit_p(aat2870_regulator_remove),
216};
217
218static int __init aat2870_regulator_init(void)
219{
220 return platform_driver_register(&aat2870_regulator_driver);
221}
222subsys_initcall(aat2870_regulator_init);
223
224static void __exit aat2870_regulator_exit(void)
225{
226 platform_driver_unregister(&aat2870_regulator_driver);
227}
228module_exit(aat2870_regulator_exit);
229
230MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator");
231MODULE_LICENSE("GPL");
232MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/drivers/regulator/aat2870-regulator.c
4 *
5 * Copyright (c) 2011, NVIDIA Corporation.
6 * Author: Jin Park <jinyoungp@nvidia.com>
7 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/err.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/platform_device.h>
15#include <linux/regulator/driver.h>
16#include <linux/regulator/machine.h>
17#include <linux/mfd/aat2870.h>
18
19struct aat2870_regulator {
20 struct aat2870_data *aat2870;
21 struct regulator_desc desc;
22
23 u8 enable_addr;
24 u8 enable_shift;
25 u8 enable_mask;
26
27 u8 voltage_addr;
28 u8 voltage_shift;
29 u8 voltage_mask;
30};
31
32static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev,
33 unsigned selector)
34{
35 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
36 struct aat2870_data *aat2870 = ri->aat2870;
37
38 return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask,
39 selector << ri->voltage_shift);
40}
41
42static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev)
43{
44 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
45 struct aat2870_data *aat2870 = ri->aat2870;
46 u8 val;
47 int ret;
48
49 ret = aat2870->read(aat2870, ri->voltage_addr, &val);
50 if (ret)
51 return ret;
52
53 return (val & ri->voltage_mask) >> ri->voltage_shift;
54}
55
56static int aat2870_ldo_enable(struct regulator_dev *rdev)
57{
58 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
59 struct aat2870_data *aat2870 = ri->aat2870;
60
61 return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask,
62 ri->enable_mask);
63}
64
65static int aat2870_ldo_disable(struct regulator_dev *rdev)
66{
67 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
68 struct aat2870_data *aat2870 = ri->aat2870;
69
70 return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, 0);
71}
72
73static int aat2870_ldo_is_enabled(struct regulator_dev *rdev)
74{
75 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
76 struct aat2870_data *aat2870 = ri->aat2870;
77 u8 val;
78 int ret;
79
80 ret = aat2870->read(aat2870, ri->enable_addr, &val);
81 if (ret)
82 return ret;
83
84 return val & ri->enable_mask ? 1 : 0;
85}
86
87static const struct regulator_ops aat2870_ldo_ops = {
88 .list_voltage = regulator_list_voltage_table,
89 .map_voltage = regulator_map_voltage_ascend,
90 .set_voltage_sel = aat2870_ldo_set_voltage_sel,
91 .get_voltage_sel = aat2870_ldo_get_voltage_sel,
92 .enable = aat2870_ldo_enable,
93 .disable = aat2870_ldo_disable,
94 .is_enabled = aat2870_ldo_is_enabled,
95};
96
97static const unsigned int aat2870_ldo_voltages[] = {
98 1200000, 1300000, 1500000, 1600000,
99 1800000, 2000000, 2200000, 2500000,
100 2600000, 2700000, 2800000, 2900000,
101 3000000, 3100000, 3200000, 3300000,
102};
103
104#define AAT2870_LDO(ids) \
105 { \
106 .desc = { \
107 .name = #ids, \
108 .id = AAT2870_ID_##ids, \
109 .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \
110 .volt_table = aat2870_ldo_voltages, \
111 .ops = &aat2870_ldo_ops, \
112 .type = REGULATOR_VOLTAGE, \
113 .owner = THIS_MODULE, \
114 }, \
115 }
116
117static struct aat2870_regulator aat2870_regulators[] = {
118 AAT2870_LDO(LDOA),
119 AAT2870_LDO(LDOB),
120 AAT2870_LDO(LDOC),
121 AAT2870_LDO(LDOD),
122};
123
124static struct aat2870_regulator *aat2870_get_regulator(int id)
125{
126 struct aat2870_regulator *ri = NULL;
127 int i;
128
129 for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) {
130 ri = &aat2870_regulators[i];
131 if (ri->desc.id == id)
132 break;
133 }
134
135 if (i == ARRAY_SIZE(aat2870_regulators))
136 return NULL;
137
138 ri->enable_addr = AAT2870_LDO_EN;
139 ri->enable_shift = id - AAT2870_ID_LDOA;
140 ri->enable_mask = 0x1 << ri->enable_shift;
141
142 ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
143 AAT2870_LDO_CD : AAT2870_LDO_AB;
144 ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4;
145 ri->voltage_mask = 0xF << ri->voltage_shift;
146
147 return ri;
148}
149
150static int aat2870_regulator_probe(struct platform_device *pdev)
151{
152 struct aat2870_regulator *ri;
153 struct regulator_config config = { };
154 struct regulator_dev *rdev;
155
156 ri = aat2870_get_regulator(pdev->id);
157 if (!ri) {
158 dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
159 return -EINVAL;
160 }
161 ri->aat2870 = dev_get_drvdata(pdev->dev.parent);
162
163 config.dev = &pdev->dev;
164 config.driver_data = ri;
165 config.init_data = dev_get_platdata(&pdev->dev);
166
167 rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
168 if (IS_ERR(rdev)) {
169 dev_err(&pdev->dev, "Failed to register regulator %s\n",
170 ri->desc.name);
171 return PTR_ERR(rdev);
172 }
173 platform_set_drvdata(pdev, rdev);
174
175 return 0;
176}
177
178static struct platform_driver aat2870_regulator_driver = {
179 .driver = {
180 .name = "aat2870-regulator",
181 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
182 },
183 .probe = aat2870_regulator_probe,
184};
185
186static int __init aat2870_regulator_init(void)
187{
188 return platform_driver_register(&aat2870_regulator_driver);
189}
190subsys_initcall(aat2870_regulator_init);
191
192static void __exit aat2870_regulator_exit(void)
193{
194 platform_driver_unregister(&aat2870_regulator_driver);
195}
196module_exit(aat2870_regulator_exit);
197
198MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator");
199MODULE_LICENSE("GPL");
200MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");
201MODULE_ALIAS("platform:aat2870-regulator");