blob: c873ee0082cf2d83e79d678efe2c662561c53e81 [file] [log] [blame]
Jin Parkf7eb6c52011-07-04 17:43:42 +09001/*
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>
Randy Dunlap3a7d0212011-07-30 06:13:09 +020025#include <linux/module.h>
Jin Parkf7eb6c52011-07-04 17:43:42 +090026#include <linux/slab.h>
Jin Parkf7eb6c52011-07-04 17:43:42 +090027#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 {
Axel Linb21bcd12012-03-02 09:28:44 +080033 struct aat2870_data *aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090034 struct regulator_desc desc;
35
Jin Parkf7eb6c52011-07-04 17:43:42 +090036 u8 enable_addr;
37 u8 enable_shift;
38 u8 enable_mask;
39
40 u8 voltage_addr;
41 u8 voltage_shift;
42 u8 voltage_mask;
43};
44
Jin Parkf7eb6c52011-07-04 17:43:42 +090045static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev,
46 unsigned selector)
47{
48 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
Axel Linb21bcd12012-03-02 09:28:44 +080049 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090050
51 return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask,
Axel Lina5228d22011-11-28 14:07:48 +080052 selector << ri->voltage_shift);
Jin Parkf7eb6c52011-07-04 17:43:42 +090053}
54
55static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev)
56{
57 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
Axel Linb21bcd12012-03-02 09:28:44 +080058 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090059 u8 val;
60 int ret;
61
62 ret = aat2870->read(aat2870, ri->voltage_addr, &val);
63 if (ret)
64 return ret;
65
66 return (val & ri->voltage_mask) >> ri->voltage_shift;
67}
68
69static int aat2870_ldo_enable(struct regulator_dev *rdev)
70{
71 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
Axel Linb21bcd12012-03-02 09:28:44 +080072 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090073
74 return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask,
75 ri->enable_mask);
76}
77
78static int aat2870_ldo_disable(struct regulator_dev *rdev)
79{
80 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
Axel Linb21bcd12012-03-02 09:28:44 +080081 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090082
83 return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, 0);
84}
85
86static int aat2870_ldo_is_enabled(struct regulator_dev *rdev)
87{
88 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
Axel Linb21bcd12012-03-02 09:28:44 +080089 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090090 u8 val;
91 int ret;
92
93 ret = aat2870->read(aat2870, ri->enable_addr, &val);
94 if (ret)
95 return ret;
96
97 return val & ri->enable_mask ? 1 : 0;
98}
99
100static struct regulator_ops aat2870_ldo_ops = {
Axel Lin4506c6d2012-07-06 19:14:32 +0800101 .list_voltage = regulator_list_voltage_table,
Axel Lin08d6da22014-03-24 20:45:38 +0800102 .map_voltage = regulator_map_voltage_ascend,
Jin Parkf7eb6c52011-07-04 17:43:42 +0900103 .set_voltage_sel = aat2870_ldo_set_voltage_sel,
104 .get_voltage_sel = aat2870_ldo_get_voltage_sel,
105 .enable = aat2870_ldo_enable,
106 .disable = aat2870_ldo_disable,
107 .is_enabled = aat2870_ldo_is_enabled,
108};
109
Axel Lin4506c6d2012-07-06 19:14:32 +0800110static const unsigned int aat2870_ldo_voltages[] = {
Jin Parkf7eb6c52011-07-04 17:43:42 +0900111 1200000, 1300000, 1500000, 1600000,
112 1800000, 2000000, 2200000, 2500000,
113 2600000, 2700000, 2800000, 2900000,
114 3000000, 3100000, 3200000, 3300000,
115};
116
117#define AAT2870_LDO(ids) \
118 { \
119 .desc = { \
120 .name = #ids, \
121 .id = AAT2870_ID_##ids, \
122 .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \
Axel Lin4506c6d2012-07-06 19:14:32 +0800123 .volt_table = aat2870_ldo_voltages, \
Jin Parkf7eb6c52011-07-04 17:43:42 +0900124 .ops = &aat2870_ldo_ops, \
125 .type = REGULATOR_VOLTAGE, \
126 .owner = THIS_MODULE, \
127 }, \
Jin Parkf7eb6c52011-07-04 17:43:42 +0900128 }
129
130static struct aat2870_regulator aat2870_regulators[] = {
131 AAT2870_LDO(LDOA),
132 AAT2870_LDO(LDOB),
133 AAT2870_LDO(LDOC),
134 AAT2870_LDO(LDOD),
135};
136
137static struct aat2870_regulator *aat2870_get_regulator(int id)
138{
139 struct aat2870_regulator *ri = NULL;
140 int i;
141
142 for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) {
143 ri = &aat2870_regulators[i];
144 if (ri->desc.id == id)
145 break;
146 }
147
Axel Lind4d63732011-11-28 14:06:31 +0800148 if (i == ARRAY_SIZE(aat2870_regulators))
Jin Parkf7eb6c52011-07-04 17:43:42 +0900149 return NULL;
150
151 ri->enable_addr = AAT2870_LDO_EN;
152 ri->enable_shift = id - AAT2870_ID_LDOA;
153 ri->enable_mask = 0x1 << ri->enable_shift;
154
155 ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
156 AAT2870_LDO_CD : AAT2870_LDO_AB;
157 ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4;
158 ri->voltage_mask = 0xF << ri->voltage_shift;
159
160 return ri;
161}
162
163static int aat2870_regulator_probe(struct platform_device *pdev)
164{
165 struct aat2870_regulator *ri;
Mark Browne4c52882012-09-10 12:07:23 +0800166 struct regulator_config config = { };
Jin Parkf7eb6c52011-07-04 17:43:42 +0900167 struct regulator_dev *rdev;
168
169 ri = aat2870_get_regulator(pdev->id);
170 if (!ri) {
171 dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
172 return -EINVAL;
173 }
Axel Linb21bcd12012-03-02 09:28:44 +0800174 ri->aat2870 = dev_get_drvdata(pdev->dev.parent);
Jin Parkf7eb6c52011-07-04 17:43:42 +0900175
Mark Brownc1727082012-04-04 00:50:22 +0100176 config.dev = &pdev->dev;
177 config.driver_data = ri;
Jingoo Handff91d02013-07-30 17:20:47 +0900178 config.init_data = dev_get_platdata(&pdev->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100179
Axel Linb1a613d2013-09-03 14:19:00 +0800180 rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
Jin Parkf7eb6c52011-07-04 17:43:42 +0900181 if (IS_ERR(rdev)) {
182 dev_err(&pdev->dev, "Failed to register regulator %s\n",
183 ri->desc.name);
184 return PTR_ERR(rdev);
185 }
186 platform_set_drvdata(pdev, rdev);
187
188 return 0;
189}
190
Jin Parkf7eb6c52011-07-04 17:43:42 +0900191static struct platform_driver aat2870_regulator_driver = {
192 .driver = {
193 .name = "aat2870-regulator",
194 .owner = THIS_MODULE,
195 },
196 .probe = aat2870_regulator_probe,
Jin Parkf7eb6c52011-07-04 17:43:42 +0900197};
198
199static int __init aat2870_regulator_init(void)
200{
201 return platform_driver_register(&aat2870_regulator_driver);
202}
203subsys_initcall(aat2870_regulator_init);
204
205static void __exit aat2870_regulator_exit(void)
206{
207 platform_driver_unregister(&aat2870_regulator_driver);
208}
209module_exit(aat2870_regulator_exit);
210
211MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator");
212MODULE_LICENSE("GPL");
213MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");
Mark Brownad9c5ff2012-03-14 17:44:56 +0000214MODULE_ALIAS("platform:aat2870-regulator");