blob: c21cedbdf4512f9d23d841c3ab6505cebb12cd68 [file] [log] [blame]
Balaji T K11469e02014-02-19 20:26:40 +05301/*
2 * pbias-regulator.c
3 *
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
5 * Author: Balaji T K <balajitk@ti.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 as
9 * published by the Free Software Foundation version 2.
10 *
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/err.h>
18#include <linux/io.h>
19#include <linux/module.h>
20#include <linux/mfd/syscon.h>
21#include <linux/platform_device.h>
22#include <linux/regulator/driver.h>
23#include <linux/regulator/machine.h>
24#include <linux/regulator/of_regulator.h>
25#include <linux/regmap.h>
26#include <linux/slab.h>
27#include <linux/of.h>
28#include <linux/of_device.h>
29
30struct pbias_reg_info {
31 u32 enable;
32 u32 enable_mask;
33 u32 vmode;
34 unsigned int enable_time;
35 char *name;
36};
37
38struct pbias_regulator_data {
39 struct regulator_desc desc;
40 void __iomem *pbias_addr;
Balaji T K11469e02014-02-19 20:26:40 +053041 struct regulator_dev *dev;
42 struct regmap *syscon;
43 const struct pbias_reg_info *info;
44 int voltage;
45};
46
Kishon Vijay Abraham Ib9c93642015-09-03 12:20:37 +053047struct pbias_of_data {
48 unsigned int offset;
49};
50
Axel Lin60e8c1e2014-03-08 11:56:47 +080051static const unsigned int pbias_volt_table[] = {
52 1800000,
53 3000000
54};
Balaji T K11469e02014-02-19 20:26:40 +053055
Balaji T K11469e02014-02-19 20:26:40 +053056static struct regulator_ops pbias_regulator_voltage_ops = {
Axel Lin60e8c1e2014-03-08 11:56:47 +080057 .list_voltage = regulator_list_voltage_table,
58 .get_voltage_sel = regulator_get_voltage_sel_regmap,
59 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Lin75dbf0a2014-04-15 12:02:08 +080060 .enable = regulator_enable_regmap,
Axel Lin60e8c1e2014-03-08 11:56:47 +080061 .disable = regulator_disable_regmap,
Axel Lin75dbf0a2014-04-15 12:02:08 +080062 .is_enabled = regulator_is_enabled_regmap,
Balaji T K11469e02014-02-19 20:26:40 +053063};
64
65static const struct pbias_reg_info pbias_mmc_omap2430 = {
66 .enable = BIT(1),
67 .enable_mask = BIT(1),
68 .vmode = BIT(0),
69 .enable_time = 100,
70 .name = "pbias_mmc_omap2430"
71};
72
73static const struct pbias_reg_info pbias_sim_omap3 = {
74 .enable = BIT(9),
75 .enable_mask = BIT(9),
76 .vmode = BIT(8),
77 .enable_time = 100,
78 .name = "pbias_sim_omap3"
79};
80
81static const struct pbias_reg_info pbias_mmc_omap4 = {
82 .enable = BIT(26) | BIT(22),
83 .enable_mask = BIT(26) | BIT(25) | BIT(22),
84 .vmode = BIT(21),
85 .enable_time = 100,
86 .name = "pbias_mmc_omap4"
87};
88
89static const struct pbias_reg_info pbias_mmc_omap5 = {
90 .enable = BIT(27) | BIT(26),
91 .enable_mask = BIT(27) | BIT(25) | BIT(26),
92 .vmode = BIT(21),
93 .enable_time = 100,
94 .name = "pbias_mmc_omap5"
95};
96
97static struct of_regulator_match pbias_matches[] = {
98 { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430},
99 { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3},
100 { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4},
101 { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5},
102};
103#define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches)
104
Kishon Vijay Abraham Ib9c93642015-09-03 12:20:37 +0530105/* Offset from SCM general area (and syscon) base */
106
107static const struct pbias_of_data pbias_of_data_omap2 = {
108 .offset = 0x230,
109};
110
111static const struct pbias_of_data pbias_of_data_omap3 = {
112 .offset = 0x2b0,
113};
114
115static const struct pbias_of_data pbias_of_data_omap4 = {
116 .offset = 0x60,
117};
118
119static const struct pbias_of_data pbias_of_data_omap5 = {
120 .offset = 0x60,
121};
122
123static const struct pbias_of_data pbias_of_data_dra7 = {
124 .offset = 0xe00,
125};
126
Balaji T K11469e02014-02-19 20:26:40 +0530127static const struct of_device_id pbias_of_match[] = {
128 { .compatible = "ti,pbias-omap", },
Kishon Vijay Abraham Ib9c93642015-09-03 12:20:37 +0530129 { .compatible = "ti,pbias-omap2", .data = &pbias_of_data_omap2, },
130 { .compatible = "ti,pbias-omap3", .data = &pbias_of_data_omap3, },
131 { .compatible = "ti,pbias-omap4", .data = &pbias_of_data_omap4, },
132 { .compatible = "ti,pbias-omap5", .data = &pbias_of_data_omap5, },
133 { .compatible = "ti,pbias-dra7", .data = &pbias_of_data_dra7, },
Balaji T K11469e02014-02-19 20:26:40 +0530134 {},
135};
136MODULE_DEVICE_TABLE(of, pbias_of_match);
137
138static int pbias_regulator_probe(struct platform_device *pdev)
139{
140 struct device_node *np = pdev->dev.of_node;
141 struct pbias_regulator_data *drvdata;
142 struct resource *res;
143 struct regulator_config cfg = { };
144 struct regmap *syscon;
145 const struct pbias_reg_info *info;
146 int ret = 0;
147 int count, idx, data_idx = 0;
Kishon Vijay Abraham Ib9c93642015-09-03 12:20:37 +0530148 const struct of_device_id *match;
149 const struct pbias_of_data *data;
150 unsigned int offset;
Balaji T K11469e02014-02-19 20:26:40 +0530151
152 count = of_regulator_match(&pdev->dev, np, pbias_matches,
153 PBIAS_NUM_REGS);
154 if (count < 0)
155 return count;
156
157 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct pbias_regulator_data)
158 * count, GFP_KERNEL);
Jingoo Han0ee42bb2014-06-02 15:30:44 +0900159 if (!drvdata)
Balaji T K11469e02014-02-19 20:26:40 +0530160 return -ENOMEM;
Balaji T K11469e02014-02-19 20:26:40 +0530161
162 syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
163 if (IS_ERR(syscon))
164 return PTR_ERR(syscon);
165
Kishon Vijay Abraham Ib9c93642015-09-03 12:20:37 +0530166 match = of_match_device(of_match_ptr(pbias_of_match), &pdev->dev);
167 if (match && match->data) {
168 data = match->data;
169 offset = data->offset;
170 } else {
171 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
172 if (!res)
173 return -EINVAL;
174
175 offset = res->start;
176 dev_WARN(&pdev->dev,
177 "using legacy dt data for pbias offset\n");
178 }
179
Axel Lin60e8c1e2014-03-08 11:56:47 +0800180 cfg.regmap = syscon;
Balaji T K11469e02014-02-19 20:26:40 +0530181 cfg.dev = &pdev->dev;
182
183 for (idx = 0; idx < PBIAS_NUM_REGS && data_idx < count; idx++) {
184 if (!pbias_matches[idx].init_data ||
185 !pbias_matches[idx].of_node)
186 continue;
187
188 info = pbias_matches[idx].driver_data;
189 if (!info)
190 return -ENODEV;
191
Balaji T K11469e02014-02-19 20:26:40 +0530192 drvdata[data_idx].syscon = syscon;
193 drvdata[data_idx].info = info;
194 drvdata[data_idx].desc.name = info->name;
195 drvdata[data_idx].desc.owner = THIS_MODULE;
196 drvdata[data_idx].desc.type = REGULATOR_VOLTAGE;
197 drvdata[data_idx].desc.ops = &pbias_regulator_voltage_ops;
Axel Lin60e8c1e2014-03-08 11:56:47 +0800198 drvdata[data_idx].desc.volt_table = pbias_volt_table;
Balaji T K11469e02014-02-19 20:26:40 +0530199 drvdata[data_idx].desc.n_voltages = 2;
200 drvdata[data_idx].desc.enable_time = info->enable_time;
Kishon Vijay Abraham Ib9c93642015-09-03 12:20:37 +0530201 drvdata[data_idx].desc.vsel_reg = offset;
Axel Lin60e8c1e2014-03-08 11:56:47 +0800202 drvdata[data_idx].desc.vsel_mask = info->vmode;
Kishon Vijay Abraham Ib9c93642015-09-03 12:20:37 +0530203 drvdata[data_idx].desc.enable_reg = offset;
Axel Lin60e8c1e2014-03-08 11:56:47 +0800204 drvdata[data_idx].desc.enable_mask = info->enable_mask;
Axel Lin75dbf0a2014-04-15 12:02:08 +0800205 drvdata[data_idx].desc.enable_val = info->enable;
Balaji T K11469e02014-02-19 20:26:40 +0530206
207 cfg.init_data = pbias_matches[idx].init_data;
208 cfg.driver_data = &drvdata[data_idx];
209 cfg.of_node = pbias_matches[idx].of_node;
210
211 drvdata[data_idx].dev = devm_regulator_register(&pdev->dev,
212 &drvdata[data_idx].desc, &cfg);
213 if (IS_ERR(drvdata[data_idx].dev)) {
214 ret = PTR_ERR(drvdata[data_idx].dev);
215 dev_err(&pdev->dev,
216 "Failed to register regulator: %d\n", ret);
217 goto err_regulator;
218 }
219 data_idx++;
220 }
221
222 platform_set_drvdata(pdev, drvdata);
223
224err_regulator:
225 return ret;
226}
227
228static struct platform_driver pbias_regulator_driver = {
229 .probe = pbias_regulator_probe,
230 .driver = {
231 .name = "pbias-regulator",
Balaji T K11469e02014-02-19 20:26:40 +0530232 .of_match_table = of_match_ptr(pbias_of_match),
233 },
234};
235
236module_platform_driver(pbias_regulator_driver);
237
238MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
239MODULE_DESCRIPTION("pbias voltage regulator");
240MODULE_LICENSE("GPL");
241MODULE_ALIAS("platform:pbias-regulator");