blob: 38928cdcb6e6584e4b30adf0653e84f2963da0bd [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>
Mark Browne3d27772013-04-05 13:11:31 +010021#include <linux/regulator/machine.h>
Linus Walleij1d2f4682018-11-15 09:01:18 +010022#include <linux/gpio/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mark Brown69dc16c2010-01-25 19:41:57 +000024
25#include <linux/mfd/wm8994/core.h>
26#include <linux/mfd/wm8994/registers.h>
27#include <linux/mfd/wm8994/pdata.h>
28
29struct wm8994_ldo {
Mark Brown69dc16c2010-01-25 19:41:57 +000030 struct regulator_dev *regulator;
31 struct wm8994 *wm8994;
Mark Browne3d27772013-04-05 13:11:31 +010032 struct regulator_consumer_supply supply;
33 struct regulator_init_data init_data;
Mark Brown69dc16c2010-01-25 19:41:57 +000034};
35
36#define WM8994_LDO1_MAX_SELECTOR 0x7
37#define WM8994_LDO2_MAX_SELECTOR 0x3
38
Julia Lawallb0d6dd32015-12-19 16:31:24 +010039static const struct regulator_ops wm8994_ldo1_ops = {
Axel Linf2d103a2012-05-23 22:40:58 +080040 .list_voltage = regulator_list_voltage_linear,
41 .map_voltage = regulator_map_voltage_linear,
Mark Brown633b6fc2012-04-15 11:25:36 +010042 .get_voltage_sel = regulator_get_voltage_sel_regmap,
43 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brown69dc16c2010-01-25 19:41:57 +000044};
45
46static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
47 unsigned int selector)
48{
Mark Brown5a7743e2010-12-13 15:07:40 +000049 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
50
Mark Brown69dc16c2010-01-25 19:41:57 +000051 if (selector > WM8994_LDO2_MAX_SELECTOR)
52 return -EINVAL;
53
Mark Brown5a7743e2010-12-13 15:07:40 +000054 switch (ldo->wm8994->type) {
55 case WM8994:
56 return (selector * 100000) + 900000;
57 case WM8958:
58 return (selector * 100000) + 1000000;
Mark Browna1ff89e2011-07-24 12:40:48 +010059 case WM1811:
60 switch (selector) {
61 case 0:
62 return -EINVAL;
63 default:
64 return (selector * 100000) + 950000;
65 }
66 break;
Mark Brown5a7743e2010-12-13 15:07:40 +000067 default:
68 return -EINVAL;
69 }
Mark Brown69dc16c2010-01-25 19:41:57 +000070}
71
Julia Lawallb0d6dd32015-12-19 16:31:24 +010072static const struct regulator_ops wm8994_ldo2_ops = {
Mark Brown69dc16c2010-01-25 19:41:57 +000073 .list_voltage = wm8994_ldo2_list_voltage,
Mark Brown633b6fc2012-04-15 11:25:36 +010074 .get_voltage_sel = regulator_get_voltage_sel_regmap,
75 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brown69dc16c2010-01-25 19:41:57 +000076};
77
Mark Brown25e4d602012-04-03 20:47:27 +010078static const struct regulator_desc wm8994_ldo_desc[] = {
Mark Brown69dc16c2010-01-25 19:41:57 +000079 {
80 .name = "LDO1",
81 .id = 1,
82 .type = REGULATOR_VOLTAGE,
83 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
Mark Brown633b6fc2012-04-15 11:25:36 +010084 .vsel_reg = WM8994_LDO_1,
85 .vsel_mask = WM8994_LDO1_VSEL_MASK,
Mark Brown69dc16c2010-01-25 19:41:57 +000086 .ops = &wm8994_ldo1_ops,
Axel Linf2d103a2012-05-23 22:40:58 +080087 .min_uV = 2400000,
88 .uV_step = 100000,
Mark Brown00581b32012-06-27 14:29:27 +010089 .enable_time = 3000,
Mark Brown69dc16c2010-01-25 19:41:57 +000090 .owner = THIS_MODULE,
91 },
92 {
93 .name = "LDO2",
94 .id = 2,
95 .type = REGULATOR_VOLTAGE,
96 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
Mark Brown633b6fc2012-04-15 11:25:36 +010097 .vsel_reg = WM8994_LDO_2,
98 .vsel_mask = WM8994_LDO2_VSEL_MASK,
Mark Brown69dc16c2010-01-25 19:41:57 +000099 .ops = &wm8994_ldo2_ops,
Mark Brown00581b32012-06-27 14:29:27 +0100100 .enable_time = 3000,
Mark Brown69dc16c2010-01-25 19:41:57 +0000101 .owner = THIS_MODULE,
102 },
103};
104
Mark Browne3d27772013-04-05 13:11:31 +0100105static const struct regulator_consumer_supply wm8994_ldo_consumer[] = {
106 { .supply = "AVDD1" },
107 { .supply = "DCVDD" },
108};
109
110static const struct regulator_init_data wm8994_ldo_default[] = {
111 {
112 .constraints = {
113 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
114 },
115 .num_consumer_supplies = 1,
116 },
117 {
118 .constraints = {
119 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
120 },
121 .num_consumer_supplies = 1,
122 },
123};
124
Bill Pembertona5023572012-11-19 13:22:22 -0500125static int wm8994_ldo_probe(struct platform_device *pdev)
Mark Brown69dc16c2010-01-25 19:41:57 +0000126{
127 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900128 struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev);
Mark Brown69dc16c2010-01-25 19:41:57 +0000129 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
Mark Brownc1727082012-04-04 00:50:22 +0100130 struct regulator_config config = { };
Mark Brown69dc16c2010-01-25 19:41:57 +0000131 struct wm8994_ldo *ldo;
Linus Walleij1d2f4682018-11-15 09:01:18 +0100132 struct gpio_desc *gpiod;
Mark Brown69dc16c2010-01-25 19:41:57 +0000133 int ret;
134
135 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
136
Mark Brownb9e03482012-02-15 23:12:17 -0800137 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
Sachin Kamat4deaf852014-02-20 14:23:23 +0530138 if (!ldo)
Mark Brown69dc16c2010-01-25 19:41:57 +0000139 return -ENOMEM;
Mark Brown69dc16c2010-01-25 19:41:57 +0000140
141 ldo->wm8994 = wm8994;
Mark Browne3d27772013-04-05 13:11:31 +0100142 ldo->supply = wm8994_ldo_consumer[id];
143 ldo->supply.dev_name = dev_name(wm8994->dev);
Mark Brown69dc16c2010-01-25 19:41:57 +0000144
Mark Brown5d0526e2012-05-08 17:45:31 +0100145 config.dev = wm8994->dev;
Mark Brownc1727082012-04-04 00:50:22 +0100146 config.driver_data = ldo;
Mark Brown633b6fc2012-04-15 11:25:36 +0100147 config.regmap = wm8994->regmap;
Mark Browne3d27772013-04-05 13:11:31 +0100148 config.init_data = &ldo->init_data;
Linus Walleij1d2f4682018-11-15 09:01:18 +0100149
Charles Keepax466affa2018-11-21 10:13:16 +0000150 /*
151 * Look up LDO enable GPIO from the parent device node, we don't
152 * use devm because the regulator core will free the GPIO
153 */
154 gpiod = gpiod_get_optional(pdev->dev.parent,
155 id ? "wlf,ldo2ena" : "wlf,ldo1ena",
156 GPIOD_OUT_LOW |
157 GPIOD_FLAGS_BIT_NONEXCLUSIVE);
Linus Walleij1d2f4682018-11-15 09:01:18 +0100158 if (IS_ERR(gpiod))
159 return PTR_ERR(gpiod);
160 config.ena_gpiod = gpiod;
Mark Browne3d27772013-04-05 13:11:31 +0100161
162 /* Use default constraints if none set up */
Sylwester Nawrocki3ea93892013-04-11 18:36:57 +0200163 if (!pdata || !pdata->ldo[id].init_data || wm8994->dev->of_node) {
Mark Browne3d27772013-04-05 13:11:31 +0100164 dev_dbg(wm8994->dev, "Using default init data, supply %s %s\n",
165 ldo->supply.dev_name, ldo->supply.supply);
166
167 ldo->init_data = wm8994_ldo_default[id];
168 ldo->init_data.consumer_supplies = &ldo->supply;
Linus Walleij1d2f4682018-11-15 09:01:18 +0100169 if (!gpiod)
Mark Browne3d27772013-04-05 13:11:31 +0100170 ldo->init_data.constraints.valid_ops_mask = 0;
171 } else {
172 ldo->init_data = *pdata->ldo[id].init_data;
Mark Brown495fd8c2012-06-27 14:31:17 +0100173 }
Mark Brownc1727082012-04-04 00:50:22 +0100174
Linus Walleijdaa531d2018-12-11 10:53:18 +0100175 /*
176 * At this point the GPIO descriptor is handled over to the
177 * regulator core and we need not worry about it on the
178 * error path.
179 */
Mark Brown9e2bfbb2013-08-31 12:01:12 +0100180 ldo->regulator = devm_regulator_register(&pdev->dev,
181 &wm8994_ldo_desc[id],
182 &config);
Mark Brown69dc16c2010-01-25 19:41:57 +0000183 if (IS_ERR(ldo->regulator)) {
184 ret = PTR_ERR(ldo->regulator);
185 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
186 id + 1, ret);
Linus Walleijdaa531d2018-12-11 10:53:18 +0100187 return ret;
Mark Brown69dc16c2010-01-25 19:41:57 +0000188 }
189
190 platform_set_drvdata(pdev, ldo);
191
192 return 0;
Mark Brown69dc16c2010-01-25 19:41:57 +0000193}
194
Mark Brown69dc16c2010-01-25 19:41:57 +0000195static struct platform_driver wm8994_ldo_driver = {
196 .probe = wm8994_ldo_probe,
Mark Brown69dc16c2010-01-25 19:41:57 +0000197 .driver = {
198 .name = "wm8994-ldo",
Mark Brown69dc16c2010-01-25 19:41:57 +0000199 },
200};
201
Mark Brown05650212013-04-04 12:08:08 +0100202module_platform_driver(wm8994_ldo_driver);
Mark Brown69dc16c2010-01-25 19:41:57 +0000203
204/* Module information */
205MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
206MODULE_DESCRIPTION("WM8994 LDO driver");
207MODULE_LICENSE("GPL");
208MODULE_ALIAS("platform:wm8994-ldo");