blob: 6ff872342648cc2cacbf23565826715b0d3bbff7 [file] [log] [blame]
Mark Brown69dc16c2010-01-25 19:41:57 +00001/*
2 * wm8994-regulator.c -- Regulator driver for the WM8994
3 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/bitops.h>
18#include <linux/err.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brown69dc16c2010-01-25 19:41:57 +000023
24#include <linux/mfd/wm8994/core.h>
25#include <linux/mfd/wm8994/registers.h>
26#include <linux/mfd/wm8994/pdata.h>
27
28struct wm8994_ldo {
Mark Brown69dc16c2010-01-25 19:41:57 +000029 struct regulator_dev *regulator;
30 struct wm8994 *wm8994;
31};
32
33#define WM8994_LDO1_MAX_SELECTOR 0x7
34#define WM8994_LDO2_MAX_SELECTOR 0x3
35
Mark Brown69dc16c2010-01-25 19:41:57 +000036static struct regulator_ops wm8994_ldo1_ops = {
Axel Linf2d103a2012-05-23 22:40:58 +080037 .list_voltage = regulator_list_voltage_linear,
38 .map_voltage = regulator_map_voltage_linear,
Mark Brown633b6fc2012-04-15 11:25:36 +010039 .get_voltage_sel = regulator_get_voltage_sel_regmap,
40 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brown69dc16c2010-01-25 19:41:57 +000041};
42
43static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
44 unsigned int selector)
45{
Mark Brown5a7743e2010-12-13 15:07:40 +000046 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
47
Mark Brown69dc16c2010-01-25 19:41:57 +000048 if (selector > WM8994_LDO2_MAX_SELECTOR)
49 return -EINVAL;
50
Mark Brown5a7743e2010-12-13 15:07:40 +000051 switch (ldo->wm8994->type) {
52 case WM8994:
53 return (selector * 100000) + 900000;
54 case WM8958:
55 return (selector * 100000) + 1000000;
Mark Browna1ff89e2011-07-24 12:40:48 +010056 case WM1811:
57 switch (selector) {
58 case 0:
59 return -EINVAL;
60 default:
61 return (selector * 100000) + 950000;
62 }
63 break;
Mark Brown5a7743e2010-12-13 15:07:40 +000064 default:
65 return -EINVAL;
66 }
Mark Brown69dc16c2010-01-25 19:41:57 +000067}
68
Mark Brown69dc16c2010-01-25 19:41:57 +000069static struct regulator_ops wm8994_ldo2_ops = {
Mark Brown69dc16c2010-01-25 19:41:57 +000070 .list_voltage = wm8994_ldo2_list_voltage,
Mark Brown633b6fc2012-04-15 11:25:36 +010071 .get_voltage_sel = regulator_get_voltage_sel_regmap,
72 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brown69dc16c2010-01-25 19:41:57 +000073};
74
Mark Brown25e4d602012-04-03 20:47:27 +010075static const struct regulator_desc wm8994_ldo_desc[] = {
Mark Brown69dc16c2010-01-25 19:41:57 +000076 {
77 .name = "LDO1",
78 .id = 1,
79 .type = REGULATOR_VOLTAGE,
80 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
Mark Brown633b6fc2012-04-15 11:25:36 +010081 .vsel_reg = WM8994_LDO_1,
82 .vsel_mask = WM8994_LDO1_VSEL_MASK,
Mark Brown69dc16c2010-01-25 19:41:57 +000083 .ops = &wm8994_ldo1_ops,
Axel Linf2d103a2012-05-23 22:40:58 +080084 .min_uV = 2400000,
85 .uV_step = 100000,
Mark Brown00581b32012-06-27 14:29:27 +010086 .enable_time = 3000,
Mark Brown69dc16c2010-01-25 19:41:57 +000087 .owner = THIS_MODULE,
88 },
89 {
90 .name = "LDO2",
91 .id = 2,
92 .type = REGULATOR_VOLTAGE,
93 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
Mark Brown633b6fc2012-04-15 11:25:36 +010094 .vsel_reg = WM8994_LDO_2,
95 .vsel_mask = WM8994_LDO2_VSEL_MASK,
Mark Brown69dc16c2010-01-25 19:41:57 +000096 .ops = &wm8994_ldo2_ops,
Mark Brown00581b32012-06-27 14:29:27 +010097 .enable_time = 3000,
Mark Brown69dc16c2010-01-25 19:41:57 +000098 .owner = THIS_MODULE,
99 },
100};
101
Bill Pembertona5023572012-11-19 13:22:22 -0500102static int wm8994_ldo_probe(struct platform_device *pdev)
Mark Brown69dc16c2010-01-25 19:41:57 +0000103{
104 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
105 struct wm8994_pdata *pdata = wm8994->dev->platform_data;
106 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
Mark Brownc1727082012-04-04 00:50:22 +0100107 struct regulator_config config = { };
Mark Brown69dc16c2010-01-25 19:41:57 +0000108 struct wm8994_ldo *ldo;
109 int ret;
110
111 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
112
Mark Brownb9e03482012-02-15 23:12:17 -0800113 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
Mark Brown69dc16c2010-01-25 19:41:57 +0000114 if (ldo == NULL) {
115 dev_err(&pdev->dev, "Unable to allocate private data\n");
116 return -ENOMEM;
117 }
118
119 ldo->wm8994 = wm8994;
120
Mark Brown5d0526e2012-05-08 17:45:31 +0100121 config.dev = wm8994->dev;
Mark Brownc1727082012-04-04 00:50:22 +0100122 config.driver_data = ldo;
Mark Brown633b6fc2012-04-15 11:25:36 +0100123 config.regmap = wm8994->regmap;
Mark Brown495fd8c2012-06-27 14:31:17 +0100124 if (pdata) {
Mark Brownb68b45d2012-05-14 13:24:13 +0100125 config.init_data = pdata->ldo[id].init_data;
Mark Brown495fd8c2012-06-27 14:31:17 +0100126 config.ena_gpio = pdata->ldo[id].enable;
127 }
Mark Brownc1727082012-04-04 00:50:22 +0100128
129 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config);
Mark Brown69dc16c2010-01-25 19:41:57 +0000130 if (IS_ERR(ldo->regulator)) {
131 ret = PTR_ERR(ldo->regulator);
132 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
133 id + 1, ret);
Mark Brown495fd8c2012-06-27 14:31:17 +0100134 goto err;
Mark Brown69dc16c2010-01-25 19:41:57 +0000135 }
136
137 platform_set_drvdata(pdev, ldo);
138
139 return 0;
140
Mark Brown69dc16c2010-01-25 19:41:57 +0000141err:
Mark Brown69dc16c2010-01-25 19:41:57 +0000142 return ret;
143}
144
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500145static int wm8994_ldo_remove(struct platform_device *pdev)
Mark Brown69dc16c2010-01-25 19:41:57 +0000146{
147 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
148
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800149 platform_set_drvdata(pdev, NULL);
150
Mark Brown69dc16c2010-01-25 19:41:57 +0000151 regulator_unregister(ldo->regulator);
Mark Brown69dc16c2010-01-25 19:41:57 +0000152
153 return 0;
154}
155
156static struct platform_driver wm8994_ldo_driver = {
157 .probe = wm8994_ldo_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500158 .remove = wm8994_ldo_remove,
Mark Brown69dc16c2010-01-25 19:41:57 +0000159 .driver = {
160 .name = "wm8994-ldo",
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800161 .owner = THIS_MODULE,
Mark Brown69dc16c2010-01-25 19:41:57 +0000162 },
163};
164
165static int __init wm8994_ldo_init(void)
166{
167 int ret;
168
169 ret = platform_driver_register(&wm8994_ldo_driver);
170 if (ret != 0)
171 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
172
173 return ret;
174}
175subsys_initcall(wm8994_ldo_init);
176
177static void __exit wm8994_ldo_exit(void)
178{
179 platform_driver_unregister(&wm8994_ldo_driver);
180}
181module_exit(wm8994_ldo_exit);
182
183/* Module information */
184MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
185MODULE_DESCRIPTION("WM8994 LDO driver");
186MODULE_LICENSE("GPL");
187MODULE_ALIAS("platform:wm8994-ldo");