blob: d6ed5bf9235e52e85b59f8de3fc328953e085f5d [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Jin Parkf7eb6c52011-07-04 17:43:42 +09002/*
3 * linux/drivers/regulator/aat2870-regulator.c
4 *
5 * Copyright (c) 2011, NVIDIA Corporation.
6 * Author: Jin Park <jinyoungp@nvidia.com>
Jin Parkf7eb6c52011-07-04 17:43:42 +09007 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/err.h>
Randy Dunlap3a7d0212011-07-30 06:13:09 +020012#include <linux/module.h>
Jin Parkf7eb6c52011-07-04 17:43:42 +090013#include <linux/slab.h>
Jin Parkf7eb6c52011-07-04 17:43:42 +090014#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 {
Axel Linb21bcd12012-03-02 09:28:44 +080020 struct aat2870_data *aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090021 struct regulator_desc desc;
22
Jin Parkf7eb6c52011-07-04 17:43:42 +090023 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
Jin Parkf7eb6c52011-07-04 17:43:42 +090032static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev,
33 unsigned selector)
34{
35 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
Axel Linb21bcd12012-03-02 09:28:44 +080036 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090037
38 return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask,
Axel Lina5228d22011-11-28 14:07:48 +080039 selector << ri->voltage_shift);
Jin Parkf7eb6c52011-07-04 17:43:42 +090040}
41
42static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev)
43{
44 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
Axel Linb21bcd12012-03-02 09:28:44 +080045 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090046 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);
Axel Linb21bcd12012-03-02 09:28:44 +080059 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090060
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);
Axel Linb21bcd12012-03-02 09:28:44 +080068 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090069
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);
Axel Linb21bcd12012-03-02 09:28:44 +080076 struct aat2870_data *aat2870 = ri->aat2870;
Jin Parkf7eb6c52011-07-04 17:43:42 +090077 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
Bhumika Goyal2abf29a2017-01-28 19:00:52 +053087static const struct regulator_ops aat2870_ldo_ops = {
Axel Lin4506c6d2012-07-06 19:14:32 +080088 .list_voltage = regulator_list_voltage_table,
Axel Lin08d6da22014-03-24 20:45:38 +080089 .map_voltage = regulator_map_voltage_ascend,
Jin Parkf7eb6c52011-07-04 17:43:42 +090090 .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
Axel Lin4506c6d2012-07-06 19:14:32 +080097static const unsigned int aat2870_ldo_voltages[] = {
Jin Parkf7eb6c52011-07-04 17:43:42 +090098 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), \
Axel Lin4506c6d2012-07-06 19:14:32 +0800110 .volt_table = aat2870_ldo_voltages, \
Jin Parkf7eb6c52011-07-04 17:43:42 +0900111 .ops = &aat2870_ldo_ops, \
112 .type = REGULATOR_VOLTAGE, \
113 .owner = THIS_MODULE, \
114 }, \
Jin Parkf7eb6c52011-07-04 17:43:42 +0900115 }
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
Axel Lind4d63732011-11-28 14:06:31 +0800135 if (i == ARRAY_SIZE(aat2870_regulators))
Jin Parkf7eb6c52011-07-04 17:43:42 +0900136 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;
Mark Browne4c52882012-09-10 12:07:23 +0800153 struct regulator_config config = { };
Jin Parkf7eb6c52011-07-04 17:43:42 +0900154 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 }
Axel Linb21bcd12012-03-02 09:28:44 +0800161 ri->aat2870 = dev_get_drvdata(pdev->dev.parent);
Jin Parkf7eb6c52011-07-04 17:43:42 +0900162
Mark Brownc172708d2012-04-04 00:50:22 +0100163 config.dev = &pdev->dev;
164 config.driver_data = ri;
Jingoo Handff91d02013-07-30 17:20:47 +0900165 config.init_data = dev_get_platdata(&pdev->dev);
Mark Brownc172708d2012-04-04 00:50:22 +0100166
Axel Linb1a613d2013-09-03 14:19:00 +0800167 rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
Jin Parkf7eb6c52011-07-04 17:43:42 +0900168 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
Jin Parkf7eb6c52011-07-04 17:43:42 +0900178static struct platform_driver aat2870_regulator_driver = {
179 .driver = {
180 .name = "aat2870-regulator",
Jin Parkf7eb6c52011-07-04 17:43:42 +0900181 },
182 .probe = aat2870_regulator_probe,
Jin Parkf7eb6c52011-07-04 17:43:42 +0900183};
184
185static int __init aat2870_regulator_init(void)
186{
187 return platform_driver_register(&aat2870_regulator_driver);
188}
189subsys_initcall(aat2870_regulator_init);
190
191static void __exit aat2870_regulator_exit(void)
192{
193 platform_driver_unregister(&aat2870_regulator_driver);
194}
195module_exit(aat2870_regulator_exit);
196
197MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator");
198MODULE_LICENSE("GPL");
199MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");
Mark Brownad9c5ff2012-03-14 17:44:56 +0000200MODULE_ALIAS("platform:aat2870-regulator");