blob: 881c37e61f7547120b0a07e07b4dce3ce459181d [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Russell King778b28b2014-06-29 17:55:54 +010027#include <linux/gpio/consumer.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053028#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010029#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053030#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010031#include <linux/regulator/consumer.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040034#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010035
Mark Brown02fa3ec2010-11-10 14:38:30 +000036#define CREATE_TRACE_POINTS
37#include <trace/events/regulator.h>
38
Mark Brown34abbd62010-02-12 10:18:08 +000039#include "dummy.h"
Mark Brown0cdfcc02013-09-11 13:15:40 +010040#include "internal.h"
Mark Brown34abbd62010-02-12 10:18:08 +000041
Mark Brown7d51a0d2011-06-09 16:06:37 +010042#define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080044#define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50#define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52
Liam Girdwood414c70c2008-04-30 15:59:04 +010053static DEFINE_MUTEX(regulator_list_mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +010054static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000055static LIST_HEAD(regulator_ena_gpio_list);
Charles Keepaxa06ccd92013-10-15 20:14:20 +010056static LIST_HEAD(regulator_supply_alias_list);
Mark Brown21cf8912010-12-21 23:30:07 +000057static bool has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010058
Mark Brown1130e5b2010-12-21 23:49:31 +000059static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000060
Tomeu Vizoso85f3b432015-09-21 16:02:47 +020061static struct class regulator_class;
62
Mark Brown8dc53902008-12-31 12:52:40 +000063/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010064 * struct regulator_map
65 *
66 * Used to provide symbolic supply names to devices.
67 */
68struct regulator_map {
69 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010070 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010071 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010072 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010073};
74
Liam Girdwood414c70c2008-04-30 15:59:04 +010075/*
Kim, Milof19b00d2013-02-18 06:50:39 +000076 * struct regulator_enable_gpio
77 *
78 * Management for shared enable GPIO pin
79 */
80struct regulator_enable_gpio {
81 struct list_head list;
Russell King778b28b2014-06-29 17:55:54 +010082 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +000083 u32 enable_count; /* a number of enabled shared GPIO */
84 u32 request_count; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert:1;
86};
87
Charles Keepaxa06ccd92013-10-15 20:14:20 +010088/*
89 * struct regulator_supply_alias
90 *
91 * Used to map lookups for a supply onto an alternative device.
92 */
93struct regulator_supply_alias {
94 struct list_head list;
95 struct device *src_dev;
96 const char *src_supply;
97 struct device *alias_dev;
98 const char *alias_supply;
99};
100
Liam Girdwood414c70c2008-04-30 15:59:04 +0100101static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100102static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100103static int _regulator_get_voltage(struct regulator_dev *rdev);
104static int _regulator_get_current_limit(struct regulator_dev *rdev);
105static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
Heiko Stübner71795692014-08-28 12:36:04 -0700106static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +0100107 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000108static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100110static struct regulator *create_regulator(struct regulator_dev *rdev,
111 struct device *dev,
112 const char *supply_name);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +0200113static void _regulator_put(struct regulator *regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100114
Mark Brown609ca5f2015-08-10 19:43:47 +0100115static struct regulator_dev *dev_to_rdev(struct device *dev)
116{
117 return container_of(dev, struct regulator_dev, dev);
118}
Liam Girdwood414c70c2008-04-30 15:59:04 +0100119
Mark Brown1083c392009-10-22 16:31:32 +0100120static const char *rdev_get_name(struct regulator_dev *rdev)
121{
122 if (rdev->constraints && rdev->constraints->name)
123 return rdev->constraints->name;
124 else if (rdev->desc->name)
125 return rdev->desc->name;
126 else
127 return "";
128}
129
Mark Brown87b28412013-11-27 16:13:10 +0000130static bool have_full_constraints(void)
131{
Mark Brown75bc9642013-11-27 16:22:53 +0000132 return has_full_constraints || of_have_populated_dt();
Mark Brown87b28412013-11-27 16:13:10 +0000133}
134
Thierry Reding70a7fb82015-12-02 16:54:50 +0100135static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
136{
137 if (rdev && rdev->supply)
138 return rdev->supply->rdev;
139
140 return NULL;
141}
142
Rajendra Nayak69511a42011-11-18 16:47:20 +0530143/**
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200144 * regulator_lock_supply - lock a regulator and its supplies
145 * @rdev: regulator source
146 */
147static void regulator_lock_supply(struct regulator_dev *rdev)
148{
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100149 int i;
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200150
Thierry Reding70a7fb82015-12-02 16:54:50 +0100151 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
Arnd Bergmannfa731ac2015-11-20 15:24:39 +0100152 mutex_lock_nested(&rdev->mutex, i);
Sascha Hauer9f01cd42015-09-30 16:05:42 +0200153}
154
155/**
156 * regulator_unlock_supply - unlock a regulator and its supplies
157 * @rdev: regulator source
158 */
159static void regulator_unlock_supply(struct regulator_dev *rdev)
160{
161 struct regulator *supply;
162
163 while (1) {
164 mutex_unlock(&rdev->mutex);
165 supply = rdev->supply;
166
167 if (!rdev->supply)
168 return;
169
170 rdev = supply->rdev;
171 }
172}
173
174/**
Rajendra Nayak69511a42011-11-18 16:47:20 +0530175 * of_get_regulator - get a regulator device node based on supply name
176 * @dev: Device pointer for the consumer (of regulator) device
177 * @supply: regulator supply name
178 *
179 * Extract the regulator device node corresponding to the supply name.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100180 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530181 * returns NULL.
182 */
183static struct device_node *of_get_regulator(struct device *dev, const char *supply)
184{
185 struct device_node *regnode = NULL;
186 char prop_name[32]; /* 32 is max size of property name */
187
188 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
189
190 snprintf(prop_name, 32, "%s-supply", supply);
191 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
192
193 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530194 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530195 prop_name, dev->of_node->full_name);
196 return NULL;
197 }
198 return regnode;
199}
200
Mark Brown6492bc12012-04-19 13:19:07 +0100201static int _regulator_can_change_status(struct regulator_dev *rdev)
202{
203 if (!rdev->constraints)
204 return 0;
205
206 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
207 return 1;
208 else
209 return 0;
210}
211
Liam Girdwood414c70c2008-04-30 15:59:04 +0100212/* Platform voltage constraint check */
213static int regulator_check_voltage(struct regulator_dev *rdev,
214 int *min_uV, int *max_uV)
215{
216 BUG_ON(*min_uV > *max_uV);
217
218 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800219 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100220 return -ENODEV;
221 }
222 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700223 rdev_err(rdev, "voltage operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100224 return -EPERM;
225 }
226
227 if (*max_uV > rdev->constraints->max_uV)
228 *max_uV = rdev->constraints->max_uV;
229 if (*min_uV < rdev->constraints->min_uV)
230 *min_uV = rdev->constraints->min_uV;
231
Mark Brown89f425e2011-07-12 11:20:37 +0900232 if (*min_uV > *max_uV) {
233 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100234 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900236 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100237
238 return 0;
239}
240
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100241/* Make sure we select a voltage that suits the needs of all
242 * regulator consumers
243 */
244static int regulator_check_consumers(struct regulator_dev *rdev,
245 int *min_uV, int *max_uV)
246{
247 struct regulator *regulator;
248
249 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700250 /*
251 * Assume consumers that didn't say anything are OK
252 * with anything in the constraint range.
253 */
254 if (!regulator->min_uV && !regulator->max_uV)
255 continue;
256
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100257 if (*max_uV > regulator->max_uV)
258 *max_uV = regulator->max_uV;
259 if (*min_uV < regulator->min_uV)
260 *min_uV = regulator->min_uV;
261 }
262
Mark Browndd8004a2012-11-28 17:09:27 +0000263 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800264 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
265 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100266 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000267 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100268
269 return 0;
270}
271
Liam Girdwood414c70c2008-04-30 15:59:04 +0100272/* current constraint check */
273static int regulator_check_current_limit(struct regulator_dev *rdev,
274 int *min_uA, int *max_uA)
275{
276 BUG_ON(*min_uA > *max_uA);
277
278 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800279 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 return -ENODEV;
281 }
282 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700283 rdev_err(rdev, "current operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 return -EPERM;
285 }
286
287 if (*max_uA > rdev->constraints->max_uA)
288 *max_uA = rdev->constraints->max_uA;
289 if (*min_uA < rdev->constraints->min_uA)
290 *min_uA = rdev->constraints->min_uA;
291
Mark Brown89f425e2011-07-12 11:20:37 +0900292 if (*min_uA > *max_uA) {
293 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100294 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100295 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900296 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100297
298 return 0;
299}
300
301/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900302static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100303{
Mark Brown2c608232011-03-30 06:29:12 +0900304 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800305 case REGULATOR_MODE_FAST:
306 case REGULATOR_MODE_NORMAL:
307 case REGULATOR_MODE_IDLE:
308 case REGULATOR_MODE_STANDBY:
309 break;
310 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900311 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800312 return -EINVAL;
313 }
314
Liam Girdwood414c70c2008-04-30 15:59:04 +0100315 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800316 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100317 return -ENODEV;
318 }
319 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700320 rdev_err(rdev, "mode operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321 return -EPERM;
322 }
Mark Brown2c608232011-03-30 06:29:12 +0900323
324 /* The modes are bitmasks, the most power hungry modes having
325 * the lowest values. If the requested mode isn't supported
326 * try higher modes. */
327 while (*mode) {
328 if (rdev->constraints->valid_modes_mask & *mode)
329 return 0;
330 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100331 }
Mark Brown2c608232011-03-30 06:29:12 +0900332
333 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100334}
335
336/* dynamic regulator mode switching constraint check */
337static int regulator_check_drms(struct regulator_dev *rdev)
338{
339 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800340 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100341 return -ENODEV;
342 }
343 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Stephen Boyd7ebcf262015-09-16 12:10:26 -0700344 rdev_dbg(rdev, "drms operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345 return -EPERM;
346 }
347 return 0;
348}
349
Liam Girdwood414c70c2008-04-30 15:59:04 +0100350static ssize_t regulator_uV_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100353 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100354 ssize_t ret;
355
356 mutex_lock(&rdev->mutex);
357 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
358 mutex_unlock(&rdev->mutex);
359
360 return ret;
361}
David Brownell7ad68e22008-11-11 17:39:02 -0800362static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100363
364static ssize_t regulator_uA_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
366{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100367 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100368
369 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
370}
David Brownell7ad68e22008-11-11 17:39:02 -0800371static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100372
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700373static ssize_t name_show(struct device *dev, struct device_attribute *attr,
374 char *buf)
Mark Brownbc558a62008-10-10 15:33:20 +0100375{
376 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100377
Mark Brown1083c392009-10-22 16:31:32 +0100378 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100379}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700380static DEVICE_ATTR_RO(name);
Mark Brownbc558a62008-10-10 15:33:20 +0100381
David Brownell4fca9542008-11-11 17:38:53 -0800382static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100383{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100384 switch (mode) {
385 case REGULATOR_MODE_FAST:
386 return sprintf(buf, "fast\n");
387 case REGULATOR_MODE_NORMAL:
388 return sprintf(buf, "normal\n");
389 case REGULATOR_MODE_IDLE:
390 return sprintf(buf, "idle\n");
391 case REGULATOR_MODE_STANDBY:
392 return sprintf(buf, "standby\n");
393 }
394 return sprintf(buf, "unknown\n");
395}
396
David Brownell4fca9542008-11-11 17:38:53 -0800397static ssize_t regulator_opmode_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100399{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100400 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100401
David Brownell4fca9542008-11-11 17:38:53 -0800402 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
403}
David Brownell7ad68e22008-11-11 17:39:02 -0800404static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800405
406static ssize_t regulator_print_state(char *buf, int state)
407{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100408 if (state > 0)
409 return sprintf(buf, "enabled\n");
410 else if (state == 0)
411 return sprintf(buf, "disabled\n");
412 else
413 return sprintf(buf, "unknown\n");
414}
415
David Brownell4fca9542008-11-11 17:38:53 -0800416static ssize_t regulator_state_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
418{
419 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100420 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800421
Mark Brown93325462009-08-03 18:49:56 +0100422 mutex_lock(&rdev->mutex);
423 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
424 mutex_unlock(&rdev->mutex);
425
426 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800427}
David Brownell7ad68e22008-11-11 17:39:02 -0800428static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800429
David Brownell853116a2009-01-14 23:03:17 -0800430static ssize_t regulator_status_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
432{
433 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 int status;
435 char *label;
436
437 status = rdev->desc->ops->get_status(rdev);
438 if (status < 0)
439 return status;
440
441 switch (status) {
442 case REGULATOR_STATUS_OFF:
443 label = "off";
444 break;
445 case REGULATOR_STATUS_ON:
446 label = "on";
447 break;
448 case REGULATOR_STATUS_ERROR:
449 label = "error";
450 break;
451 case REGULATOR_STATUS_FAST:
452 label = "fast";
453 break;
454 case REGULATOR_STATUS_NORMAL:
455 label = "normal";
456 break;
457 case REGULATOR_STATUS_IDLE:
458 label = "idle";
459 break;
460 case REGULATOR_STATUS_STANDBY:
461 label = "standby";
462 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700463 case REGULATOR_STATUS_BYPASS:
464 label = "bypass";
465 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100466 case REGULATOR_STATUS_UNDEFINED:
467 label = "undefined";
468 break;
David Brownell853116a2009-01-14 23:03:17 -0800469 default:
470 return -ERANGE;
471 }
472
473 return sprintf(buf, "%s\n", label);
474}
475static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
476
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477static ssize_t regulator_min_uA_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
479{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100480 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100481
482 if (!rdev->constraints)
483 return sprintf(buf, "constraint not defined\n");
484
485 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
486}
David Brownell7ad68e22008-11-11 17:39:02 -0800487static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488
489static ssize_t regulator_max_uA_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100492 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493
494 if (!rdev->constraints)
495 return sprintf(buf, "constraint not defined\n");
496
497 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
498}
David Brownell7ad68e22008-11-11 17:39:02 -0800499static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500
501static ssize_t regulator_min_uV_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
503{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100504 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505
506 if (!rdev->constraints)
507 return sprintf(buf, "constraint not defined\n");
508
509 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
510}
David Brownell7ad68e22008-11-11 17:39:02 -0800511static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512
513static ssize_t regulator_max_uV_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
515{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100516 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100517
518 if (!rdev->constraints)
519 return sprintf(buf, "constraint not defined\n");
520
521 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
522}
David Brownell7ad68e22008-11-11 17:39:02 -0800523static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524
525static ssize_t regulator_total_uA_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100528 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529 struct regulator *regulator;
530 int uA = 0;
531
532 mutex_lock(&rdev->mutex);
533 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100534 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535 mutex_unlock(&rdev->mutex);
536 return sprintf(buf, "%d\n", uA);
537}
David Brownell7ad68e22008-11-11 17:39:02 -0800538static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700540static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
541 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100543 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544 return sprintf(buf, "%d\n", rdev->use_count);
545}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700546static DEVICE_ATTR_RO(num_users);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700548static ssize_t type_show(struct device *dev, struct device_attribute *attr,
549 char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100551 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
553 switch (rdev->desc->type) {
554 case REGULATOR_VOLTAGE:
555 return sprintf(buf, "voltage\n");
556 case REGULATOR_CURRENT:
557 return sprintf(buf, "current\n");
558 }
559 return sprintf(buf, "unknown\n");
560}
Greg Kroah-Hartman587cea22013-07-24 15:05:21 -0700561static DEVICE_ATTR_RO(type);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
563static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100566 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100567
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
569}
David Brownell7ad68e22008-11-11 17:39:02 -0800570static DEVICE_ATTR(suspend_mem_microvolts, 0444,
571 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572
573static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100576 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100577
Liam Girdwood414c70c2008-04-30 15:59:04 +0100578 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
579}
David Brownell7ad68e22008-11-11 17:39:02 -0800580static DEVICE_ATTR(suspend_disk_microvolts, 0444,
581 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
583static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100586 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
589}
David Brownell7ad68e22008-11-11 17:39:02 -0800590static DEVICE_ATTR(suspend_standby_microvolts, 0444,
591 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100592
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
595{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100596 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100597
David Brownell4fca9542008-11-11 17:38:53 -0800598 return regulator_print_opmode(buf,
599 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100600}
David Brownell7ad68e22008-11-11 17:39:02 -0800601static DEVICE_ATTR(suspend_mem_mode, 0444,
602 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100603
604static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
605 struct device_attribute *attr, char *buf)
606{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100607 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100608
David Brownell4fca9542008-11-11 17:38:53 -0800609 return regulator_print_opmode(buf,
610 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100611}
David Brownell7ad68e22008-11-11 17:39:02 -0800612static DEVICE_ATTR(suspend_disk_mode, 0444,
613 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100614
615static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
617{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100618 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100619
David Brownell4fca9542008-11-11 17:38:53 -0800620 return regulator_print_opmode(buf,
621 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100622}
David Brownell7ad68e22008-11-11 17:39:02 -0800623static DEVICE_ATTR(suspend_standby_mode, 0444,
624 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100625
626static ssize_t regulator_suspend_mem_state_show(struct device *dev,
627 struct device_attribute *attr, char *buf)
628{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100629 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100630
David Brownell4fca9542008-11-11 17:38:53 -0800631 return regulator_print_state(buf,
632 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100633}
David Brownell7ad68e22008-11-11 17:39:02 -0800634static DEVICE_ATTR(suspend_mem_state, 0444,
635 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100636
637static ssize_t regulator_suspend_disk_state_show(struct device *dev,
638 struct device_attribute *attr, char *buf)
639{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100640 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100641
David Brownell4fca9542008-11-11 17:38:53 -0800642 return regulator_print_state(buf,
643 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100644}
David Brownell7ad68e22008-11-11 17:39:02 -0800645static DEVICE_ATTR(suspend_disk_state, 0444,
646 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100647
648static ssize_t regulator_suspend_standby_state_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
650{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100651 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100652
David Brownell4fca9542008-11-11 17:38:53 -0800653 return regulator_print_state(buf,
654 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100655}
David Brownell7ad68e22008-11-11 17:39:02 -0800656static DEVICE_ATTR(suspend_standby_state, 0444,
657 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100658
Mark Brownf59c8f92012-08-31 10:36:37 -0700659static ssize_t regulator_bypass_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
661{
662 struct regulator_dev *rdev = dev_get_drvdata(dev);
663 const char *report;
664 bool bypass;
665 int ret;
666
667 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
668
669 if (ret != 0)
670 report = "unknown";
671 else if (bypass)
672 report = "enabled";
673 else
674 report = "disabled";
675
676 return sprintf(buf, "%s\n", report);
677}
678static DEVICE_ATTR(bypass, 0444,
679 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800680
Liam Girdwood414c70c2008-04-30 15:59:04 +0100681/* Calculate the new optimum regulator operating mode based on the new total
682 * consumer load. All locks held by caller */
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800683static int drms_uA_update(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100684{
685 struct regulator *sibling;
686 int current_uA = 0, output_uV, input_uV, err;
687 unsigned int mode;
688
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900689 lockdep_assert_held_once(&rdev->mutex);
690
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800691 /*
692 * first check to see if we can set modes at all, otherwise just
693 * tell the consumer everything is OK.
694 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100695 err = regulator_check_drms(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800696 if (err < 0)
697 return 0;
698
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800699 if (!rdev->desc->ops->get_optimum_mode &&
700 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800701 return 0;
702
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800703 if (!rdev->desc->ops->set_mode &&
704 !rdev->desc->ops->set_load)
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800705 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100706
707 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000708 output_uV = _regulator_get_voltage(rdev);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800709 if (output_uV <= 0) {
710 rdev_err(rdev, "invalid output voltage found\n");
711 return -EINVAL;
712 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100713
714 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000715 input_uV = 0;
716 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800717 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000718 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100719 input_uV = rdev->constraints->input_uV;
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800720 if (input_uV <= 0) {
721 rdev_err(rdev, "invalid input voltage found\n");
722 return -EINVAL;
723 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100724
725 /* calc total requested load */
726 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100727 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728
Stephen Boyd22a10bc2015-06-11 17:37:03 -0700729 current_uA += rdev->constraints->system_load;
730
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800731 if (rdev->desc->ops->set_load) {
732 /* set the optimum mode for our new total regulator load */
733 err = rdev->desc->ops->set_load(rdev, current_uA);
734 if (err < 0)
735 rdev_err(rdev, "failed to set load %d\n", current_uA);
736 } else {
737 /* now get the optimum mode for our new total regulator load */
738 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
739 output_uV, current_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100740
Bjorn Andersson8f4490e2015-02-11 19:39:12 -0800741 /* check the new mode is allowed */
742 err = regulator_mode_constrain(rdev, &mode);
743 if (err < 0) {
744 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
745 current_uA, input_uV, output_uV);
746 return err;
747 }
748
749 err = rdev->desc->ops->set_mode(rdev, mode);
750 if (err < 0)
751 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800752 }
753
Bjorn Andersson8460ef32015-01-27 18:46:31 -0800754 return err;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755}
756
757static int suspend_set_state(struct regulator_dev *rdev,
758 struct regulator_state *rstate)
759{
760 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100761
762 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800763 * only warn if the driver implements set_suspend_voltage or
764 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100765 */
766 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800767 if (rdev->desc->ops->set_suspend_voltage ||
768 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800769 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100770 return 0;
771 }
772
773 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800774 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100775 return -EINVAL;
776 }
777
Axel Lin8ac0e952012-04-14 09:14:34 +0800778 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100779 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800780 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100781 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800782 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
783 ret = 0;
784
Liam Girdwood414c70c2008-04-30 15:59:04 +0100785 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800786 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100787 return ret;
788 }
789
790 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
791 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
792 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800793 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100794 return ret;
795 }
796 }
797
798 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
799 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
800 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800801 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100802 return ret;
803 }
804 }
805 return ret;
806}
807
808/* locks held by caller */
809static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
810{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +0900811 lockdep_assert_held_once(&rdev->mutex);
812
Liam Girdwood414c70c2008-04-30 15:59:04 +0100813 if (!rdev->constraints)
814 return -EINVAL;
815
816 switch (state) {
817 case PM_SUSPEND_STANDBY:
818 return suspend_set_state(rdev,
819 &rdev->constraints->state_standby);
820 case PM_SUSPEND_MEM:
821 return suspend_set_state(rdev,
822 &rdev->constraints->state_mem);
823 case PM_SUSPEND_MAX:
824 return suspend_set_state(rdev,
825 &rdev->constraints->state_disk);
826 default:
827 return -EINVAL;
828 }
829}
830
831static void print_constraints(struct regulator_dev *rdev)
832{
833 struct regulation_constraints *constraints = rdev->constraints;
Stefan Wahrena7068e32015-06-09 20:09:42 +0000834 char buf[160] = "";
Stefan Wahren5751a992015-06-10 06:13:15 +0000835 size_t len = sizeof(buf) - 1;
Mark Brown8f031b42009-10-22 16:31:31 +0100836 int count = 0;
837 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100838
Mark Brown8f031b42009-10-22 16:31:31 +0100839 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100840 if (constraints->min_uV == constraints->max_uV)
Stefan Wahren5751a992015-06-10 06:13:15 +0000841 count += scnprintf(buf + count, len - count, "%d mV ",
842 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100843 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000844 count += scnprintf(buf + count, len - count,
845 "%d <--> %d mV ",
846 constraints->min_uV / 1000,
847 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100848 }
Mark Brown8f031b42009-10-22 16:31:31 +0100849
850 if (!constraints->min_uV ||
851 constraints->min_uV != constraints->max_uV) {
852 ret = _regulator_get_voltage(rdev);
853 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000854 count += scnprintf(buf + count, len - count,
855 "at %d mV ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100856 }
857
Mark Brownbf5892a2011-05-08 22:13:37 +0100858 if (constraints->uV_offset)
Stefan Wahren5751a992015-06-10 06:13:15 +0000859 count += scnprintf(buf + count, len - count, "%dmV offset ",
860 constraints->uV_offset / 1000);
Mark Brownbf5892a2011-05-08 22:13:37 +0100861
Mark Brown8f031b42009-10-22 16:31:31 +0100862 if (constraints->min_uA && constraints->max_uA) {
863 if (constraints->min_uA == constraints->max_uA)
Stefan Wahren5751a992015-06-10 06:13:15 +0000864 count += scnprintf(buf + count, len - count, "%d mA ",
865 constraints->min_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100866 else
Stefan Wahren5751a992015-06-10 06:13:15 +0000867 count += scnprintf(buf + count, len - count,
868 "%d <--> %d mA ",
869 constraints->min_uA / 1000,
870 constraints->max_uA / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100871 }
872
873 if (!constraints->min_uA ||
874 constraints->min_uA != constraints->max_uA) {
875 ret = _regulator_get_current_limit(rdev);
876 if (ret > 0)
Stefan Wahren5751a992015-06-10 06:13:15 +0000877 count += scnprintf(buf + count, len - count,
878 "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100879 }
880
Liam Girdwood414c70c2008-04-30 15:59:04 +0100881 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
Stefan Wahren5751a992015-06-10 06:13:15 +0000882 count += scnprintf(buf + count, len - count, "fast ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100883 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
Stefan Wahren5751a992015-06-10 06:13:15 +0000884 count += scnprintf(buf + count, len - count, "normal ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100885 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
Stefan Wahren5751a992015-06-10 06:13:15 +0000886 count += scnprintf(buf + count, len - count, "idle ");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100887 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
Stefan Wahren5751a992015-06-10 06:13:15 +0000888 count += scnprintf(buf + count, len - count, "standby");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100889
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200890 if (!count)
Stefan Wahren5751a992015-06-10 06:13:15 +0000891 scnprintf(buf, len, "no parameters");
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200892
Mark Brown194dbae2014-10-31 19:11:59 +0000893 rdev_dbg(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000894
895 if ((constraints->min_uV != constraints->max_uV) &&
896 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
897 rdev_warn(rdev,
898 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100899}
900
Mark Browne79055d2009-10-19 15:53:50 +0100901static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100902 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100903{
Guodong Xu272e2312014-08-13 19:33:38 +0800904 const struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100905 int ret;
906
907 /* do we need to apply the constraint voltage */
908 if (rdev->constraints->apply_uV &&
Mark Brownfa93fd42016-03-21 18:12:52 +0000909 rdev->constraints->min_uV && rdev->constraints->max_uV) {
910 int target_min, target_max;
Alban Bedel064d5cd2014-05-20 12:12:16 +0200911 int current_uV = _regulator_get_voltage(rdev);
912 if (current_uV < 0) {
Nishanth Menon69d58832014-06-04 14:53:25 -0500913 rdev_err(rdev,
914 "failed to get the current voltage(%d)\n",
915 current_uV);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200916 return current_uV;
917 }
Mark Brownfa93fd42016-03-21 18:12:52 +0000918
919 /*
920 * If we're below the minimum voltage move up to the
921 * minimum voltage, if we're above the maximum voltage
922 * then move down to the maximum.
923 */
924 target_min = current_uV;
925 target_max = current_uV;
926
927 if (current_uV < rdev->constraints->min_uV) {
928 target_min = rdev->constraints->min_uV;
929 target_max = rdev->constraints->min_uV;
930 }
931
932 if (current_uV > rdev->constraints->max_uV) {
933 target_min = rdev->constraints->max_uV;
934 target_max = rdev->constraints->max_uV;
935 }
936
937 if (target_min != current_uV || target_max != current_uV) {
Alban Bedel064d5cd2014-05-20 12:12:16 +0200938 ret = _regulator_do_set_voltage(
Mark Brownfa93fd42016-03-21 18:12:52 +0000939 rdev, target_min, target_max);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200940 if (ret < 0) {
941 rdev_err(rdev,
Mark Brownfa93fd42016-03-21 18:12:52 +0000942 "failed to apply %d-%duV constraint(%d)\n",
943 target_min, target_max, ret);
Alban Bedel064d5cd2014-05-20 12:12:16 +0200944 return ret;
945 }
Mark Brown75790252010-12-12 14:25:50 +0000946 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100947 }
Mark Browne79055d2009-10-19 15:53:50 +0100948
949 /* constrain machine-level voltage specs to fit
950 * the actual range supported by this regulator.
951 */
952 if (ops->list_voltage && rdev->desc->n_voltages) {
953 int count = rdev->desc->n_voltages;
954 int i;
955 int min_uV = INT_MAX;
956 int max_uV = INT_MIN;
957 int cmin = constraints->min_uV;
958 int cmax = constraints->max_uV;
959
960 /* it's safe to autoconfigure fixed-voltage supplies
961 and the constraints are used by list_voltage. */
962 if (count == 1 && !cmin) {
963 cmin = 1;
964 cmax = INT_MAX;
965 constraints->min_uV = cmin;
966 constraints->max_uV = cmax;
967 }
968
969 /* voltage constraints are optional */
970 if ((cmin == 0) && (cmax == 0))
971 return 0;
972
973 /* else require explicit machine-level constraints */
974 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800975 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100976 return -EINVAL;
977 }
978
979 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
980 for (i = 0; i < count; i++) {
981 int value;
982
983 value = ops->list_voltage(rdev, i);
984 if (value <= 0)
985 continue;
986
987 /* maybe adjust [min_uV..max_uV] */
988 if (value >= cmin && value < min_uV)
989 min_uV = value;
990 if (value <= cmax && value > max_uV)
991 max_uV = value;
992 }
993
994 /* final: [min_uV..max_uV] valid iff constraints valid */
995 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000996 rdev_err(rdev,
997 "unsupportable voltage constraints %u-%uuV\n",
998 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100999 return -EINVAL;
1000 }
1001
1002 /* use regulator's subset of machine constraints */
1003 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001004 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1005 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +01001006 constraints->min_uV = min_uV;
1007 }
1008 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001009 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1010 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +01001011 constraints->max_uV = max_uV;
1012 }
1013 }
1014
1015 return 0;
1016}
1017
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301018static int machine_constraints_current(struct regulator_dev *rdev,
1019 struct regulation_constraints *constraints)
1020{
Guodong Xu272e2312014-08-13 19:33:38 +08001021 const struct regulator_ops *ops = rdev->desc->ops;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301022 int ret;
1023
1024 if (!constraints->min_uA && !constraints->max_uA)
1025 return 0;
1026
1027 if (constraints->min_uA > constraints->max_uA) {
1028 rdev_err(rdev, "Invalid current constraints\n");
1029 return -EINVAL;
1030 }
1031
1032 if (!ops->set_current_limit || !ops->get_current_limit) {
1033 rdev_warn(rdev, "Operation of current configuration missing\n");
1034 return 0;
1035 }
1036
1037 /* Set regulator current in constraints range */
1038 ret = ops->set_current_limit(rdev, constraints->min_uA,
1039 constraints->max_uA);
1040 if (ret < 0) {
1041 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1042 return ret;
1043 }
1044
1045 return 0;
1046}
1047
Markus Pargmann30c21972014-02-20 17:36:03 +01001048static int _regulator_do_enable(struct regulator_dev *rdev);
1049
Liam Girdwooda5766f12008-10-10 13:22:20 +01001050/**
1051 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +00001052 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +00001053 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001054 *
1055 * Allows platform initialisation code to define and constrain
1056 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1057 * Constraints *must* be set by platform code in order for some
1058 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1059 * set_mode.
1060 */
1061static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +00001062 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001063{
1064 int ret = 0;
Guodong Xu272e2312014-08-13 19:33:38 +08001065 const struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +01001066
Mark Brown9a8f5e02011-11-29 18:11:19 +00001067 if (constraints)
1068 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1069 GFP_KERNEL);
1070 else
1071 rdev->constraints = kzalloc(sizeof(*constraints),
1072 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +00001073 if (!rdev->constraints)
1074 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +01001075
Mark Brownf8c12fe2010-11-29 15:55:17 +00001076 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +01001077 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001078 return ret;
David Brownell4367cfd2009-02-26 11:48:36 -08001079
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301080 ret = machine_constraints_current(rdev, rdev->constraints);
1081 if (ret != 0)
Charles Keepax6333ef42016-01-26 16:38:59 +00001082 return ret;
Laxman Dewanganf8c17002013-09-20 13:13:02 +05301083
Stephen Boyd36e4f832015-06-11 17:37:06 -07001084 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1085 ret = ops->set_input_current_limit(rdev,
1086 rdev->constraints->ilim_uA);
1087 if (ret < 0) {
1088 rdev_err(rdev, "failed to set input limit\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001089 return ret;
Stephen Boyd36e4f832015-06-11 17:37:06 -07001090 }
1091 }
1092
Liam Girdwooda5766f12008-10-10 13:22:20 +01001093 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +00001094 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +00001095 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +01001096 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001097 rdev_err(rdev, "failed to set suspend state\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001098 return ret;
Mark Browne06f5b42008-09-09 16:21:19 +01001099 }
1100 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001101
Mark Brown9a8f5e02011-11-29 18:11:19 +00001102 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +00001103 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001104 rdev_err(rdev, "no set_mode operation\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001105 return -EINVAL;
Mark Browna3084662009-02-26 19:24:19 +00001106 }
1107
Mark Brownf8c12fe2010-11-29 15:55:17 +00001108 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +00001109 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001110 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Charles Keepax6333ef42016-01-26 16:38:59 +00001111 return ret;
Mark Browna3084662009-02-26 19:24:19 +00001112 }
1113 }
1114
Mark Browncacf90f2009-03-02 16:32:46 +00001115 /* If the constraints say the regulator should be on at this point
1116 * and we have control then make sure it is enabled.
1117 */
Markus Pargmann30c21972014-02-20 17:36:03 +01001118 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1119 ret = _regulator_do_enable(rdev);
1120 if (ret < 0 && ret != -EINVAL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001121 rdev_err(rdev, "failed to enable\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001122 return ret;
Mark Browne5fda262008-09-09 16:21:20 +01001123 }
1124 }
1125
Yadwinder Singh Brar1653ccf2013-06-29 18:21:15 +05301126 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1127 && ops->set_ramp_delay) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301128 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1129 if (ret < 0) {
1130 rdev_err(rdev, "failed to set ramp_delay\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001131 return ret;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05301132 }
1133 }
1134
Stephen Boyd23c779b2015-06-11 17:37:04 -07001135 if (rdev->constraints->pull_down && ops->set_pull_down) {
1136 ret = ops->set_pull_down(rdev);
1137 if (ret < 0) {
1138 rdev_err(rdev, "failed to set pull down\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001139 return ret;
Stephen Boyd23c779b2015-06-11 17:37:04 -07001140 }
1141 }
1142
Stephen Boyd57f66b72015-06-11 17:37:05 -07001143 if (rdev->constraints->soft_start && ops->set_soft_start) {
1144 ret = ops->set_soft_start(rdev);
1145 if (ret < 0) {
1146 rdev_err(rdev, "failed to set soft start\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001147 return ret;
Stephen Boyd57f66b72015-06-11 17:37:05 -07001148 }
1149 }
1150
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001151 if (rdev->constraints->over_current_protection
1152 && ops->set_over_current_protection) {
1153 ret = ops->set_over_current_protection(rdev);
1154 if (ret < 0) {
1155 rdev_err(rdev, "failed to set over current protection\n");
Charles Keepax6333ef42016-01-26 16:38:59 +00001156 return ret;
Stephen Boyd3a003ba2015-07-17 14:41:54 -07001157 }
1158 }
1159
Laxman Dewangan670666b2016-03-02 16:24:46 +05301160 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1161 bool ad_state = (rdev->constraints->active_discharge ==
1162 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1163
1164 ret = ops->set_active_discharge(rdev, ad_state);
1165 if (ret < 0) {
1166 rdev_err(rdev, "failed to set active discharge\n");
1167 return ret;
1168 }
1169 }
1170
Laxman Dewangan909f7ee2016-03-02 16:24:46 +05301171 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1172 bool ad_state = (rdev->constraints->active_discharge ==
1173 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1174
1175 ret = ops->set_active_discharge(rdev, ad_state);
1176 if (ret < 0) {
1177 rdev_err(rdev, "failed to set active discharge\n");
1178 return ret;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001179 }
1180 }
1181
1182 print_constraints(rdev);
Mark Brown69279fb2008-12-31 12:52:41 +00001183 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001184}
1185
1186/**
1187 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001188 * @rdev: regulator name
1189 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001190 *
1191 * Called by platform initialisation code to set the supply regulator for this
1192 * regulator. This ensures that a regulators supply will also be enabled by the
1193 * core if it's child is enabled.
1194 */
1195static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001196 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001197{
1198 int err;
1199
Mark Brown3801b862011-06-09 16:22:22 +01001200 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1201
Javier Martinez Canillase2c09ae2015-07-15 16:10:28 +02001202 if (!try_module_get(supply_rdev->owner))
1203 return -ENODEV;
1204
Mark Brown3801b862011-06-09 16:22:22 +01001205 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001206 if (rdev->supply == NULL) {
1207 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001208 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001209 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301210 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001211
1212 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001213}
1214
1215/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001216 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001217 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001218 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001219 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001220 *
1221 * Allows platform initialisation code to map physical regulator
1222 * sources to symbolic names for supplies for use by devices. Devices
1223 * should use these symbolic names to request regulators, avoiding the
1224 * need to provide board-specific regulator names as platform data.
1225 */
1226static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001227 const char *consumer_dev_name,
1228 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001229{
1230 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001231 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001232
1233 if (supply == NULL)
1234 return -EINVAL;
1235
Mark Brown9ed20992009-07-21 16:00:26 +01001236 if (consumer_dev_name != NULL)
1237 has_dev = 1;
1238 else
1239 has_dev = 0;
1240
David Brownell6001e132008-12-31 12:54:19 +00001241 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001242 if (node->dev_name && consumer_dev_name) {
1243 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1244 continue;
1245 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001246 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001247 }
1248
David Brownell6001e132008-12-31 12:54:19 +00001249 if (strcmp(node->supply, supply) != 0)
1250 continue;
1251
Mark Brown737f3602012-02-02 00:10:51 +00001252 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1253 consumer_dev_name,
1254 dev_name(&node->regulator->dev),
1255 node->regulator->desc->name,
1256 supply,
1257 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001258 return -EBUSY;
1259 }
1260
Mark Brown9ed20992009-07-21 16:00:26 +01001261 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001262 if (node == NULL)
1263 return -ENOMEM;
1264
1265 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001266 node->supply = supply;
1267
Mark Brown9ed20992009-07-21 16:00:26 +01001268 if (has_dev) {
1269 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1270 if (node->dev_name == NULL) {
1271 kfree(node);
1272 return -ENOMEM;
1273 }
Mark Brown40f92442009-06-17 17:56:39 +01001274 }
1275
Liam Girdwooda5766f12008-10-10 13:22:20 +01001276 list_add(&node->list, &regulator_map_list);
1277 return 0;
1278}
1279
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001280static void unset_regulator_supplies(struct regulator_dev *rdev)
1281{
1282 struct regulator_map *node, *n;
1283
1284 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1285 if (rdev == node->regulator) {
1286 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001287 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001288 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001289 }
1290 }
1291}
1292
Mark Brownf5726ae2011-06-09 16:22:20 +01001293#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001294
1295static struct regulator *create_regulator(struct regulator_dev *rdev,
1296 struct device *dev,
1297 const char *supply_name)
1298{
1299 struct regulator *regulator;
1300 char buf[REG_STR_SIZE];
1301 int err, size;
1302
1303 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1304 if (regulator == NULL)
1305 return NULL;
1306
1307 mutex_lock(&rdev->mutex);
1308 regulator->rdev = rdev;
1309 list_add(&regulator->list, &rdev->consumer_list);
1310
1311 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001312 regulator->dev = dev;
1313
Mark Brown222cc7b2012-06-22 11:39:16 +01001314 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001315 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1316 dev->kobj.name, supply_name);
1317 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001318 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001319
1320 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1321 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001322 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001323
Stephen Boydff268b52015-06-01 18:47:54 -07001324 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
Liam Girdwood414c70c2008-04-30 15:59:04 +01001325 buf);
1326 if (err) {
Stephen Boydff268b52015-06-01 18:47:54 -07001327 rdev_dbg(rdev, "could not add device link %s err %d\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001328 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001329 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001330 }
Mark Brown5de70512011-06-19 13:33:16 +01001331 } else {
1332 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1333 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001334 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001335 }
Mark Brown5de70512011-06-19 13:33:16 +01001336
Mark Brown5de70512011-06-19 13:33:16 +01001337 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1338 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001339 if (!regulator->debugfs) {
Stephen Boydad3a9422015-06-01 18:47:55 -07001340 rdev_dbg(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001341 } else {
1342 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1343 &regulator->uA_load);
1344 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1345 &regulator->min_uV);
1346 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1347 &regulator->max_uV);
1348 }
Mark Brown5de70512011-06-19 13:33:16 +01001349
Mark Brown6492bc12012-04-19 13:19:07 +01001350 /*
1351 * Check now if the regulator is an always on regulator - if
1352 * it is then we don't need to do nearly so much work for
1353 * enable/disable calls.
1354 */
1355 if (!_regulator_can_change_status(rdev) &&
1356 _regulator_is_enabled(rdev))
1357 regulator->always_on = true;
1358
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359 mutex_unlock(&rdev->mutex);
1360 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361overflow_err:
1362 list_del(&regulator->list);
1363 kfree(regulator);
1364 mutex_unlock(&rdev->mutex);
1365 return NULL;
1366}
1367
Mark Brown31aae2b2009-12-21 12:21:52 +00001368static int _regulator_get_enable_time(struct regulator_dev *rdev)
1369{
Laxman Dewangan00c877c2013-09-18 18:18:02 +05301370 if (rdev->constraints && rdev->constraints->enable_time)
1371 return rdev->constraints->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001372 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001373 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001374 return rdev->desc->ops->enable_time(rdev);
1375}
1376
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001377static struct regulator_supply_alias *regulator_find_supply_alias(
1378 struct device *dev, const char *supply)
1379{
1380 struct regulator_supply_alias *map;
1381
1382 list_for_each_entry(map, &regulator_supply_alias_list, list)
1383 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1384 return map;
1385
1386 return NULL;
1387}
1388
1389static void regulator_supply_alias(struct device **dev, const char **supply)
1390{
1391 struct regulator_supply_alias *map;
1392
1393 map = regulator_find_supply_alias(*dev, *supply);
1394 if (map) {
1395 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1396 *supply, map->alias_supply,
1397 dev_name(map->alias_dev));
1398 *dev = map->alias_dev;
1399 *supply = map->alias_supply;
1400 }
1401}
1402
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001403static int of_node_match(struct device *dev, const void *data)
1404{
1405 return dev->of_node == data;
1406}
1407
1408static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1409{
1410 struct device *dev;
1411
1412 dev = class_find_device(&regulator_class, NULL, np, of_node_match);
1413
1414 return dev ? dev_to_rdev(dev) : NULL;
1415}
1416
1417static int regulator_match(struct device *dev, const void *data)
1418{
1419 struct regulator_dev *r = dev_to_rdev(dev);
1420
1421 return strcmp(rdev_get_name(r), data) == 0;
1422}
1423
1424static struct regulator_dev *regulator_lookup_by_name(const char *name)
1425{
1426 struct device *dev;
1427
1428 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1429
1430 return dev ? dev_to_rdev(dev) : NULL;
1431}
1432
1433/**
1434 * regulator_dev_lookup - lookup a regulator device.
1435 * @dev: device for regulator "consumer".
1436 * @supply: Supply name or regulator ID.
1437 * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1438 * lookup could succeed in the future.
1439 *
1440 * If successful, returns a struct regulator_dev that corresponds to the name
1441 * @supply and with the embedded struct device refcount incremented by one,
1442 * or NULL on failure. The refcount must be dropped by calling put_device().
1443 */
Rajendra Nayak69511a42011-11-18 16:47:20 +05301444static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001445 const char *supply,
1446 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301447{
1448 struct regulator_dev *r;
1449 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001450 struct regulator_map *map;
1451 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301452
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001453 regulator_supply_alias(&dev, &supply);
1454
Rajendra Nayak69511a42011-11-18 16:47:20 +05301455 /* first do a dt based lookup */
1456 if (dev && dev->of_node) {
1457 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001458 if (node) {
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001459 r = of_find_regulator_by_node(node);
1460 if (r)
1461 return r;
Mark Brown317b5682014-01-27 17:34:07 +00001462 *ret = -EPROBE_DEFER;
1463 return NULL;
Mark Brown6d191a52012-03-29 14:19:02 +01001464 } else {
1465 /*
1466 * If we couldn't even get the node then it's
1467 * not just that the device didn't register
1468 * yet, there's no node and we'll never
1469 * succeed.
1470 */
1471 *ret = -ENODEV;
1472 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301473 }
1474
1475 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001476 if (dev)
1477 devname = dev_name(dev);
1478
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001479 r = regulator_lookup_by_name(supply);
1480 if (r)
1481 return r;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301482
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001483 mutex_lock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001484 list_for_each_entry(map, &regulator_map_list, list) {
1485 /* If the mapping has a device set up it must match */
1486 if (map->dev_name &&
1487 (!devname || strcmp(map->dev_name, devname)))
1488 continue;
1489
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001490 if (strcmp(map->supply, supply) == 0 &&
1491 get_device(&map->regulator->dev)) {
1492 mutex_unlock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001493 return map->regulator;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001494 }
Mark Brown576ca4362012-03-30 12:24:37 +01001495 }
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001496 mutex_unlock(&regulator_list_mutex);
Mark Brown576ca4362012-03-30 12:24:37 +01001497
Rajendra Nayak69511a42011-11-18 16:47:20 +05301498 return NULL;
1499}
1500
Bjorn Andersson6261b062015-03-24 18:56:05 -07001501static int regulator_resolve_supply(struct regulator_dev *rdev)
1502{
1503 struct regulator_dev *r;
1504 struct device *dev = rdev->dev.parent;
1505 int ret;
1506
1507 /* No supply to resovle? */
1508 if (!rdev->supply_name)
1509 return 0;
1510
1511 /* Supply already resolved? */
1512 if (rdev->supply)
1513 return 0;
1514
1515 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001516 if (!r) {
Charles Keepax23c3f312015-09-17 14:50:20 +01001517 if (ret == -ENODEV) {
1518 /*
1519 * No supply was specified for this regulator and
1520 * there will never be one.
1521 */
1522 return 0;
1523 }
1524
Mark Brown06423122015-10-01 10:59:48 +01001525 /* Did the lookup explicitly defer for us? */
1526 if (ret == -EPROBE_DEFER)
1527 return ret;
1528
Mark Brown9f7e25e2015-07-14 11:17:26 +01001529 if (have_full_constraints()) {
1530 r = dummy_regulator_rdev;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001531 get_device(&r->dev);
Mark Brown9f7e25e2015-07-14 11:17:26 +01001532 } else {
1533 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1534 rdev->supply_name, rdev->desc->name);
1535 return -EPROBE_DEFER;
1536 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001537 }
1538
1539 /* Recursively resolve the supply of the supply */
1540 ret = regulator_resolve_supply(r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001541 if (ret < 0) {
1542 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001543 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001544 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001545
1546 ret = set_supply(rdev, r);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001547 if (ret < 0) {
1548 put_device(&r->dev);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001549 return ret;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001550 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001551
1552 /* Cascade always-on state to supply */
Sudip Mukherjee9f8df6a2015-09-02 16:14:06 +05301553 if (_regulator_is_enabled(rdev) && rdev->supply) {
Bjorn Andersson6261b062015-03-24 18:56:05 -07001554 ret = regulator_enable(rdev->supply);
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001555 if (ret < 0) {
Sudip Mukherjee9f8df6a2015-09-02 16:14:06 +05301556 _regulator_put(rdev->supply);
Bjorn Andersson6261b062015-03-24 18:56:05 -07001557 return ret;
Javier Martinez Canillas36a1f1b2015-07-15 16:10:29 +02001558 }
Bjorn Andersson6261b062015-03-24 18:56:05 -07001559 }
1560
1561 return 0;
1562}
1563
Mark Brown5ffbd132009-07-21 16:00:23 +01001564/* Internal regulator request function */
1565static struct regulator *_regulator_get(struct device *dev, const char *id,
Mark Brown4ddfebd2013-09-13 19:50:37 +01001566 bool exclusive, bool allow_dummy)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001567{
1568 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001569 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001570 const char *devname = NULL;
Mark Brown317b5682014-01-27 17:34:07 +00001571 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001572
1573 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001574 pr_err("get() with no identifier\n");
Mark Brown043c9982013-09-20 12:32:18 +01001575 return ERR_PTR(-EINVAL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001576 }
1577
Mark Brown40f92442009-06-17 17:56:39 +01001578 if (dev)
1579 devname = dev_name(dev);
1580
Mark Brown317b5682014-01-27 17:34:07 +00001581 if (have_full_constraints())
1582 ret = -ENODEV;
1583 else
1584 ret = -EPROBE_DEFER;
1585
Mark Brown6d191a52012-03-29 14:19:02 +01001586 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301587 if (rdev)
1588 goto found;
1589
Mark Brownef60abb2013-09-23 16:12:52 +01001590 regulator = ERR_PTR(ret);
1591
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001592 /*
1593 * If we have return value from dev_lookup fail, we do not expect to
1594 * succeed, so, quit with appropriate error value
1595 */
Jingoo Han0d25d092014-01-08 10:02:16 +09001596 if (ret && ret != -ENODEV)
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001597 return regulator;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001598
Mark Brown34abbd62010-02-12 10:18:08 +00001599 if (!devname)
1600 devname = "deviceless";
1601
Mark Brown4ddfebd2013-09-13 19:50:37 +01001602 /*
1603 * Assume that a regulator is physically present and enabled
1604 * even if it isn't hooked up and just provide a dummy.
Mark Brown34abbd62010-02-12 10:18:08 +00001605 */
Mark Brown87b28412013-11-27 16:13:10 +00001606 if (have_full_constraints() && allow_dummy) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001607 pr_warn("%s supply %s not found, using dummy regulator\n",
1608 devname, id);
Mark Brown4ddfebd2013-09-13 19:50:37 +01001609
Mark Brown34abbd62010-02-12 10:18:08 +00001610 rdev = dummy_regulator_rdev;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001611 get_device(&rdev->dev);
Mark Brown34abbd62010-02-12 10:18:08 +00001612 goto found;
Hans de Goede07817192013-12-17 16:24:57 +01001613 /* Don't log an error when called from regulator_get_optional() */
1614 } else if (!have_full_constraints() || exclusive) {
Shuah Khanacc3d5c2014-02-20 12:58:15 -07001615 dev_warn(dev, "dummy supplies not allowed\n");
Mark Brown34abbd62010-02-12 10:18:08 +00001616 }
Mark Brown34abbd62010-02-12 10:18:08 +00001617
Liam Girdwood414c70c2008-04-30 15:59:04 +01001618 return regulator;
1619
1620found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001621 if (rdev->exclusive) {
1622 regulator = ERR_PTR(-EPERM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001623 put_device(&rdev->dev);
1624 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001625 }
1626
1627 if (exclusive && rdev->open_count) {
1628 regulator = ERR_PTR(-EBUSY);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001629 put_device(&rdev->dev);
1630 return regulator;
Mark Brown5ffbd132009-07-21 16:00:23 +01001631 }
1632
Bjorn Andersson6261b062015-03-24 18:56:05 -07001633 ret = regulator_resolve_supply(rdev);
1634 if (ret < 0) {
1635 regulator = ERR_PTR(ret);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001636 put_device(&rdev->dev);
1637 return regulator;
Bjorn Andersson6261b062015-03-24 18:56:05 -07001638 }
1639
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001640 if (!try_module_get(rdev->owner)) {
1641 put_device(&rdev->dev);
1642 return regulator;
1643 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01001644
Liam Girdwood414c70c2008-04-30 15:59:04 +01001645 regulator = create_regulator(rdev, dev, id);
1646 if (regulator == NULL) {
1647 regulator = ERR_PTR(-ENOMEM);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001648 put_device(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001649 module_put(rdev->owner);
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001650 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001651 }
1652
Mark Brown5ffbd132009-07-21 16:00:23 +01001653 rdev->open_count++;
1654 if (exclusive) {
1655 rdev->exclusive = 1;
1656
1657 ret = _regulator_is_enabled(rdev);
1658 if (ret > 0)
1659 rdev->use_count = 1;
1660 else
1661 rdev->use_count = 0;
1662 }
1663
Liam Girdwood414c70c2008-04-30 15:59:04 +01001664 return regulator;
1665}
Mark Brown5ffbd132009-07-21 16:00:23 +01001666
1667/**
1668 * regulator_get - lookup and obtain a reference to a regulator.
1669 * @dev: device for regulator "consumer"
1670 * @id: Supply name or regulator ID.
1671 *
1672 * Returns a struct regulator corresponding to the regulator producer,
1673 * or IS_ERR() condition containing errno.
1674 *
1675 * Use of supply names configured via regulator_set_device_supply() is
1676 * strongly encouraged. It is recommended that the supply name used
1677 * should match the name used for the supply and/or the relevant
1678 * device pins in the datasheet.
1679 */
1680struct regulator *regulator_get(struct device *dev, const char *id)
1681{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001682 return _regulator_get(dev, id, false, true);
Mark Brown5ffbd132009-07-21 16:00:23 +01001683}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001684EXPORT_SYMBOL_GPL(regulator_get);
1685
Stephen Boyd070b9072012-01-16 19:39:58 -08001686/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001687 * regulator_get_exclusive - obtain exclusive access to a regulator.
1688 * @dev: device for regulator "consumer"
1689 * @id: Supply name or regulator ID.
1690 *
1691 * Returns a struct regulator corresponding to the regulator producer,
1692 * or IS_ERR() condition containing errno. Other consumers will be
Stephen Boyd69c3f722014-05-28 12:41:28 -07001693 * unable to obtain this regulator while this reference is held and the
1694 * use count for the regulator will be initialised to reflect the current
1695 * state of the regulator.
Mark Brown5ffbd132009-07-21 16:00:23 +01001696 *
1697 * This is intended for use by consumers which cannot tolerate shared
1698 * use of the regulator such as those which need to force the
1699 * regulator off for correct operation of the hardware they are
1700 * controlling.
1701 *
1702 * Use of supply names configured via regulator_set_device_supply() is
1703 * strongly encouraged. It is recommended that the supply name used
1704 * should match the name used for the supply and/or the relevant
1705 * device pins in the datasheet.
1706 */
1707struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1708{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001709 return _regulator_get(dev, id, true, false);
Mark Brown5ffbd132009-07-21 16:00:23 +01001710}
1711EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1712
Mark Brownde1dd9f2013-07-29 21:42:42 +01001713/**
1714 * regulator_get_optional - obtain optional access to a regulator.
1715 * @dev: device for regulator "consumer"
1716 * @id: Supply name or regulator ID.
1717 *
1718 * Returns a struct regulator corresponding to the regulator producer,
Stephen Boyd69c3f722014-05-28 12:41:28 -07001719 * or IS_ERR() condition containing errno.
Mark Brownde1dd9f2013-07-29 21:42:42 +01001720 *
1721 * This is intended for use by consumers for devices which can have
1722 * some supplies unconnected in normal use, such as some MMC devices.
1723 * It can allow the regulator core to provide stub supplies for other
1724 * supplies requested using normal regulator_get() calls without
1725 * disrupting the operation of drivers that can handle absent
1726 * supplies.
1727 *
1728 * Use of supply names configured via regulator_set_device_supply() is
1729 * strongly encouraged. It is recommended that the supply name used
1730 * should match the name used for the supply and/or the relevant
1731 * device pins in the datasheet.
1732 */
1733struct regulator *regulator_get_optional(struct device *dev, const char *id)
1734{
Mark Brown4ddfebd2013-09-13 19:50:37 +01001735 return _regulator_get(dev, id, false, false);
Mark Brownde1dd9f2013-07-29 21:42:42 +01001736}
1737EXPORT_SYMBOL_GPL(regulator_get_optional);
1738
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301739/* regulator_list_mutex lock held by regulator_put() */
Charles Keepax23ff2f02012-11-14 09:39:31 +00001740static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001741{
1742 struct regulator_dev *rdev;
1743
Viresh Kumar93576842015-08-17 12:30:58 +05301744 if (IS_ERR_OR_NULL(regulator))
Liam Girdwood414c70c2008-04-30 15:59:04 +01001745 return;
1746
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09001747 lockdep_assert_held_once(&regulator_list_mutex);
1748
Liam Girdwood414c70c2008-04-30 15:59:04 +01001749 rdev = regulator->rdev;
1750
Mark Brown5de70512011-06-19 13:33:16 +01001751 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001752
Liam Girdwood414c70c2008-04-30 15:59:04 +01001753 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001754 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001755 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301756 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001757 list_del(&regulator->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001758
Mark Brown5ffbd132009-07-21 16:00:23 +01001759 rdev->open_count--;
1760 rdev->exclusive = 0;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02001761 put_device(&rdev->dev);
Ashay Jaiswal83b03022015-01-08 18:54:25 +05301762 mutex_unlock(&rdev->mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001763
Mark Brown17685142015-08-07 21:19:26 +01001764 kfree(regulator->supply_name);
1765 kfree(regulator);
1766
Liam Girdwood414c70c2008-04-30 15:59:04 +01001767 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001768}
1769
1770/**
1771 * regulator_put - "free" the regulator source
1772 * @regulator: regulator source
1773 *
1774 * Note: drivers must ensure that all regulator_enable calls made on this
1775 * regulator source are balanced by regulator_disable calls prior to calling
1776 * this function.
1777 */
1778void regulator_put(struct regulator *regulator)
1779{
1780 mutex_lock(&regulator_list_mutex);
1781 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001782 mutex_unlock(&regulator_list_mutex);
1783}
1784EXPORT_SYMBOL_GPL(regulator_put);
1785
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001786/**
1787 * regulator_register_supply_alias - Provide device alias for supply lookup
1788 *
1789 * @dev: device that will be given as the regulator "consumer"
1790 * @id: Supply name or regulator ID
1791 * @alias_dev: device that should be used to lookup the supply
1792 * @alias_id: Supply name or regulator ID that should be used to lookup the
1793 * supply
1794 *
1795 * All lookups for id on dev will instead be conducted for alias_id on
1796 * alias_dev.
1797 */
1798int regulator_register_supply_alias(struct device *dev, const char *id,
1799 struct device *alias_dev,
1800 const char *alias_id)
1801{
1802 struct regulator_supply_alias *map;
1803
1804 map = regulator_find_supply_alias(dev, id);
1805 if (map)
1806 return -EEXIST;
1807
1808 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1809 if (!map)
1810 return -ENOMEM;
1811
1812 map->src_dev = dev;
1813 map->src_supply = id;
1814 map->alias_dev = alias_dev;
1815 map->alias_supply = alias_id;
1816
1817 list_add(&map->list, &regulator_supply_alias_list);
1818
1819 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1820 id, dev_name(dev), alias_id, dev_name(alias_dev));
1821
1822 return 0;
1823}
1824EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1825
1826/**
1827 * regulator_unregister_supply_alias - Remove device alias
1828 *
1829 * @dev: device that will be given as the regulator "consumer"
1830 * @id: Supply name or regulator ID
1831 *
1832 * Remove a lookup alias if one exists for id on dev.
1833 */
1834void regulator_unregister_supply_alias(struct device *dev, const char *id)
1835{
1836 struct regulator_supply_alias *map;
1837
1838 map = regulator_find_supply_alias(dev, id);
1839 if (map) {
1840 list_del(&map->list);
1841 kfree(map);
1842 }
1843}
1844EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1845
1846/**
1847 * regulator_bulk_register_supply_alias - register multiple aliases
1848 *
1849 * @dev: device that will be given as the regulator "consumer"
1850 * @id: List of supply names or regulator IDs
1851 * @alias_dev: device that should be used to lookup the supply
1852 * @alias_id: List of supply names or regulator IDs that should be used to
1853 * lookup the supply
1854 * @num_id: Number of aliases to register
1855 *
1856 * @return 0 on success, an errno on failure.
1857 *
1858 * This helper function allows drivers to register several supply
1859 * aliases in one operation. If any of the aliases cannot be
1860 * registered any aliases that were registered will be removed
1861 * before returning to the caller.
1862 */
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001863int regulator_bulk_register_supply_alias(struct device *dev,
1864 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001865 struct device *alias_dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001866 const char *const *alias_id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001867 int num_id)
1868{
1869 int i;
1870 int ret;
1871
1872 for (i = 0; i < num_id; ++i) {
1873 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1874 alias_id[i]);
1875 if (ret < 0)
1876 goto err;
1877 }
1878
1879 return 0;
1880
1881err:
1882 dev_err(dev,
1883 "Failed to create supply alias %s,%s -> %s,%s\n",
1884 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1885
1886 while (--i >= 0)
1887 regulator_unregister_supply_alias(dev, id[i]);
1888
1889 return ret;
1890}
1891EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1892
1893/**
1894 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1895 *
1896 * @dev: device that will be given as the regulator "consumer"
1897 * @id: List of supply names or regulator IDs
1898 * @num_id: Number of aliases to unregister
1899 *
1900 * This helper function allows drivers to unregister several supply
1901 * aliases in one operation.
1902 */
1903void regulator_bulk_unregister_supply_alias(struct device *dev,
Lee Jones9f8c0fe2014-05-23 16:44:10 +01001904 const char *const *id,
Charles Keepaxa06ccd92013-10-15 20:14:20 +01001905 int num_id)
1906{
1907 int i;
1908
1909 for (i = 0; i < num_id; ++i)
1910 regulator_unregister_supply_alias(dev, id[i]);
1911}
1912EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1913
1914
Kim, Milof19b00d2013-02-18 06:50:39 +00001915/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1916static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1917 const struct regulator_config *config)
1918{
1919 struct regulator_enable_gpio *pin;
Russell King778b28b2014-06-29 17:55:54 +01001920 struct gpio_desc *gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001921 int ret;
1922
Russell King778b28b2014-06-29 17:55:54 +01001923 gpiod = gpio_to_desc(config->ena_gpio);
1924
Kim, Milof19b00d2013-02-18 06:50:39 +00001925 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001926 if (pin->gpiod == gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001927 rdev_dbg(rdev, "GPIO %d is already used\n",
1928 config->ena_gpio);
1929 goto update_ena_gpio_to_rdev;
1930 }
1931 }
1932
1933 ret = gpio_request_one(config->ena_gpio,
1934 GPIOF_DIR_OUT | config->ena_gpio_flags,
1935 rdev_get_name(rdev));
1936 if (ret)
1937 return ret;
1938
1939 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1940 if (pin == NULL) {
1941 gpio_free(config->ena_gpio);
1942 return -ENOMEM;
1943 }
1944
Russell King778b28b2014-06-29 17:55:54 +01001945 pin->gpiod = gpiod;
Kim, Milof19b00d2013-02-18 06:50:39 +00001946 pin->ena_gpio_invert = config->ena_gpio_invert;
1947 list_add(&pin->list, &regulator_ena_gpio_list);
1948
1949update_ena_gpio_to_rdev:
1950 pin->request_count++;
1951 rdev->ena_pin = pin;
1952 return 0;
1953}
1954
1955static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1956{
1957 struct regulator_enable_gpio *pin, *n;
1958
1959 if (!rdev->ena_pin)
1960 return;
1961
1962 /* Free the GPIO only in case of no use */
1963 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
Russell King778b28b2014-06-29 17:55:54 +01001964 if (pin->gpiod == rdev->ena_pin->gpiod) {
Kim, Milof19b00d2013-02-18 06:50:39 +00001965 if (pin->request_count <= 1) {
1966 pin->request_count = 0;
Russell King778b28b2014-06-29 17:55:54 +01001967 gpiod_put(pin->gpiod);
Kim, Milof19b00d2013-02-18 06:50:39 +00001968 list_del(&pin->list);
1969 kfree(pin);
Seung-Woo Kim60a23622014-12-04 19:17:17 +09001970 rdev->ena_pin = NULL;
1971 return;
Kim, Milof19b00d2013-02-18 06:50:39 +00001972 } else {
1973 pin->request_count--;
1974 }
1975 }
1976 }
1977}
1978
Kim, Milo967cfb12013-02-18 06:50:48 +00001979/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001980 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1981 * @rdev: regulator_dev structure
1982 * @enable: enable GPIO at initial use?
1983 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001984 * GPIO is enabled in case of initial use. (enable_count is 0)
1985 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1986 */
1987static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1988{
1989 struct regulator_enable_gpio *pin = rdev->ena_pin;
1990
1991 if (!pin)
1992 return -EINVAL;
1993
1994 if (enable) {
1995 /* Enable GPIO at initial use */
1996 if (pin->enable_count == 0)
Russell King778b28b2014-06-29 17:55:54 +01001997 gpiod_set_value_cansleep(pin->gpiod,
1998 !pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00001999
2000 pin->enable_count++;
2001 } else {
2002 if (pin->enable_count > 1) {
2003 pin->enable_count--;
2004 return 0;
2005 }
2006
2007 /* Disable GPIO if not used */
2008 if (pin->enable_count <= 1) {
Russell King778b28b2014-06-29 17:55:54 +01002009 gpiod_set_value_cansleep(pin->gpiod,
2010 pin->ena_gpio_invert);
Kim, Milo967cfb12013-02-18 06:50:48 +00002011 pin->enable_count = 0;
2012 }
2013 }
2014
2015 return 0;
2016}
2017
Guodong Xu79fd1142014-08-13 19:33:39 +08002018/**
2019 * _regulator_enable_delay - a delay helper function
2020 * @delay: time to delay in microseconds
2021 *
2022 * Delay for the requested amount of time as per the guidelines in:
2023 *
2024 * Documentation/timers/timers-howto.txt
2025 *
2026 * The assumption here is that regulators will never be enabled in
2027 * atomic context and therefore sleeping functions can be used.
2028 */
2029static void _regulator_enable_delay(unsigned int delay)
2030{
2031 unsigned int ms = delay / 1000;
2032 unsigned int us = delay % 1000;
2033
2034 if (ms > 0) {
2035 /*
2036 * For small enough values, handle super-millisecond
2037 * delays in the usleep_range() call below.
2038 */
2039 if (ms < 20)
2040 us += ms * 1000;
2041 else
2042 msleep(ms);
2043 }
2044
2045 /*
2046 * Give the scheduler some room to coalesce with any other
2047 * wakeup sources. For delays shorter than 10 us, don't even
2048 * bother setting up high-resolution timers and just busy-
2049 * loop.
2050 */
2051 if (us >= 10)
2052 usleep_range(us, us + 100);
2053 else
2054 udelay(us);
2055}
2056
Mark Brown5c5659d2012-06-27 13:21:26 +01002057static int _regulator_do_enable(struct regulator_dev *rdev)
2058{
2059 int ret, delay;
2060
2061 /* Query before enabling in case configuration dependent. */
2062 ret = _regulator_get_enable_time(rdev);
2063 if (ret >= 0) {
2064 delay = ret;
2065 } else {
2066 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2067 delay = 0;
2068 }
2069
2070 trace_regulator_enable(rdev_get_name(rdev));
2071
Guodong Xu871f5652014-08-13 19:33:40 +08002072 if (rdev->desc->off_on_delay) {
2073 /* if needed, keep a distance of off_on_delay from last time
2074 * this regulator was disabled.
2075 */
2076 unsigned long start_jiffy = jiffies;
2077 unsigned long intended, max_delay, remaining;
2078
2079 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2080 intended = rdev->last_off_jiffy + max_delay;
2081
2082 if (time_before(start_jiffy, intended)) {
2083 /* calc remaining jiffies to deal with one-time
2084 * timer wrapping.
2085 * in case of multiple timer wrapping, either it can be
2086 * detected by out-of-range remaining, or it cannot be
2087 * detected and we gets a panelty of
2088 * _regulator_enable_delay().
2089 */
2090 remaining = intended - start_jiffy;
2091 if (remaining <= max_delay)
2092 _regulator_enable_delay(
2093 jiffies_to_usecs(remaining));
2094 }
2095 }
2096
Kim, Milo967cfb12013-02-18 06:50:48 +00002097 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002098 if (!rdev->ena_gpio_state) {
2099 ret = regulator_ena_gpio_ctrl(rdev, true);
2100 if (ret < 0)
2101 return ret;
2102 rdev->ena_gpio_state = 1;
2103 }
Mark Brown65f73502012-06-27 14:14:38 +01002104 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01002105 ret = rdev->desc->ops->enable(rdev);
2106 if (ret < 0)
2107 return ret;
2108 } else {
2109 return -EINVAL;
2110 }
2111
2112 /* Allow the regulator to ramp; it would be useful to extend
2113 * this for bulk operations so that the regulators can ramp
2114 * together. */
2115 trace_regulator_enable_delay(rdev_get_name(rdev));
2116
Guodong Xu79fd1142014-08-13 19:33:39 +08002117 _regulator_enable_delay(delay);
Mark Brown5c5659d2012-06-27 13:21:26 +01002118
2119 trace_regulator_enable_complete(rdev_get_name(rdev));
2120
2121 return 0;
2122}
2123
Liam Girdwood414c70c2008-04-30 15:59:04 +01002124/* locks held by regulator_enable() */
2125static int _regulator_enable(struct regulator_dev *rdev)
2126{
Mark Brown5c5659d2012-06-27 13:21:26 +01002127 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002128
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002129 lockdep_assert_held_once(&rdev->mutex);
2130
Liam Girdwood414c70c2008-04-30 15:59:04 +01002131 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01002132 if (rdev->constraints &&
2133 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
2134 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002135
Mark Brown9a2372f2009-08-03 18:49:57 +01002136 if (rdev->use_count == 0) {
2137 /* The regulator may on if it's not switchable or left on */
2138 ret = _regulator_is_enabled(rdev);
2139 if (ret == -EINVAL || ret == 0) {
2140 if (!_regulator_can_change_status(rdev))
2141 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002142
Mark Brown5c5659d2012-06-27 13:21:26 +01002143 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00002144 if (ret < 0)
2145 return ret;
2146
Linus Walleija7433cf2009-08-26 12:54:04 +02002147 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002148 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002149 return ret;
2150 }
Linus Walleija7433cf2009-08-26 12:54:04 +02002151 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002152 }
2153
Mark Brown9a2372f2009-08-03 18:49:57 +01002154 rdev->use_count++;
2155
2156 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002157}
2158
2159/**
2160 * regulator_enable - enable regulator output
2161 * @regulator: regulator source
2162 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002163 * Request that the regulator be enabled with the regulator output at
2164 * the predefined voltage or current value. Calls to regulator_enable()
2165 * must be balanced with calls to regulator_disable().
2166 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002167 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00002168 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002169 */
2170int regulator_enable(struct regulator *regulator)
2171{
David Brownell412aec62008-11-16 11:44:46 -08002172 struct regulator_dev *rdev = regulator->rdev;
2173 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002174
Mark Brown6492bc12012-04-19 13:19:07 +01002175 if (regulator->always_on)
2176 return 0;
2177
Mark Brown3801b862011-06-09 16:22:22 +01002178 if (rdev->supply) {
2179 ret = regulator_enable(rdev->supply);
2180 if (ret != 0)
2181 return ret;
2182 }
2183
David Brownell412aec62008-11-16 11:44:46 -08002184 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08002185 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002186 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002187
Heiko Stübnerd1685e42011-10-14 18:00:29 +02002188 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002189 regulator_disable(rdev->supply);
2190
Liam Girdwood414c70c2008-04-30 15:59:04 +01002191 return ret;
2192}
2193EXPORT_SYMBOL_GPL(regulator_enable);
2194
Mark Brown5c5659d2012-06-27 13:21:26 +01002195static int _regulator_do_disable(struct regulator_dev *rdev)
2196{
2197 int ret;
2198
2199 trace_regulator_disable(rdev_get_name(rdev));
2200
Kim, Milo967cfb12013-02-18 06:50:48 +00002201 if (rdev->ena_pin) {
Doug Anderson29d62ec2015-03-03 15:20:47 -08002202 if (rdev->ena_gpio_state) {
2203 ret = regulator_ena_gpio_ctrl(rdev, false);
2204 if (ret < 0)
2205 return ret;
2206 rdev->ena_gpio_state = 0;
2207 }
Mark Brown5c5659d2012-06-27 13:21:26 +01002208
2209 } else if (rdev->desc->ops->disable) {
2210 ret = rdev->desc->ops->disable(rdev);
2211 if (ret != 0)
2212 return ret;
2213 }
2214
Guodong Xu871f5652014-08-13 19:33:40 +08002215 /* cares about last_off_jiffy only if off_on_delay is required by
2216 * device.
2217 */
2218 if (rdev->desc->off_on_delay)
2219 rdev->last_off_jiffy = jiffies;
2220
Mark Brown5c5659d2012-06-27 13:21:26 +01002221 trace_regulator_disable_complete(rdev_get_name(rdev));
2222
Mark Brown5c5659d2012-06-27 13:21:26 +01002223 return 0;
2224}
2225
Liam Girdwood414c70c2008-04-30 15:59:04 +01002226/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002227static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002228{
2229 int ret = 0;
2230
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002231 lockdep_assert_held_once(&rdev->mutex);
2232
David Brownellcd94b502009-03-11 16:43:34 -08002233 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08002234 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08002235 return -EIO;
2236
Liam Girdwood414c70c2008-04-30 15:59:04 +01002237 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01002238 if (rdev->use_count == 1 &&
2239 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002240
2241 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01002242 if (_regulator_can_change_status(rdev)) {
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002243 ret = _notifier_call_chain(rdev,
2244 REGULATOR_EVENT_PRE_DISABLE,
2245 NULL);
2246 if (ret & NOTIFY_STOP_MASK)
2247 return -EINVAL;
2248
Mark Brown5c5659d2012-06-27 13:21:26 +01002249 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002250 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002251 rdev_err(rdev, "failed to disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002252 _notifier_call_chain(rdev,
2253 REGULATOR_EVENT_ABORT_DISABLE,
2254 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002255 return ret;
2256 }
Markus Pargmann66fda752014-02-20 17:36:04 +01002257 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2258 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002259 }
2260
Liam Girdwood414c70c2008-04-30 15:59:04 +01002261 rdev->use_count = 0;
2262 } else if (rdev->use_count > 1) {
2263
2264 if (rdev->constraints &&
2265 (rdev->constraints->valid_ops_mask &
2266 REGULATOR_CHANGE_DRMS))
2267 drms_uA_update(rdev);
2268
2269 rdev->use_count--;
2270 }
Mark Brown3801b862011-06-09 16:22:22 +01002271
Liam Girdwood414c70c2008-04-30 15:59:04 +01002272 return ret;
2273}
2274
2275/**
2276 * regulator_disable - disable regulator output
2277 * @regulator: regulator source
2278 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00002279 * Disable the regulator output voltage or current. Calls to
2280 * regulator_enable() must be balanced with calls to
2281 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00002282 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01002283 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00002284 * devices have it enabled, the regulator device supports disabling and
2285 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002286 */
2287int regulator_disable(struct regulator *regulator)
2288{
David Brownell412aec62008-11-16 11:44:46 -08002289 struct regulator_dev *rdev = regulator->rdev;
2290 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002291
Mark Brown6492bc12012-04-19 13:19:07 +01002292 if (regulator->always_on)
2293 return 0;
2294
David Brownell412aec62008-11-16 11:44:46 -08002295 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01002296 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08002297 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002298
Mark Brown3801b862011-06-09 16:22:22 +01002299 if (ret == 0 && rdev->supply)
2300 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002301
Liam Girdwood414c70c2008-04-30 15:59:04 +01002302 return ret;
2303}
2304EXPORT_SYMBOL_GPL(regulator_disable);
2305
2306/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01002307static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002308{
2309 int ret = 0;
2310
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09002311 lockdep_assert_held_once(&rdev->mutex);
2312
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002313 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2314 REGULATOR_EVENT_PRE_DISABLE, NULL);
2315 if (ret & NOTIFY_STOP_MASK)
2316 return -EINVAL;
2317
Markus Pargmann66fda752014-02-20 17:36:04 +01002318 ret = _regulator_do_disable(rdev);
2319 if (ret < 0) {
2320 rdev_err(rdev, "failed to force disable\n");
Richard Fitzgeralda1c8a552014-11-24 14:10:52 +00002321 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2322 REGULATOR_EVENT_ABORT_DISABLE, NULL);
Markus Pargmann66fda752014-02-20 17:36:04 +01002323 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002324 }
2325
Markus Pargmann66fda752014-02-20 17:36:04 +01002326 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2327 REGULATOR_EVENT_DISABLE, NULL);
2328
2329 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002330}
2331
2332/**
2333 * regulator_force_disable - force disable regulator output
2334 * @regulator: regulator source
2335 *
2336 * Forcibly disable the regulator output voltage or current.
2337 * NOTE: this *will* disable the regulator output even if other consumer
2338 * devices have it enabled. This should be used for situations when device
2339 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2340 */
2341int regulator_force_disable(struct regulator *regulator)
2342{
Mark Brown82d15832011-05-09 11:41:02 +02002343 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002344 int ret;
2345
Mark Brown82d15832011-05-09 11:41:02 +02002346 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002347 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01002348 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02002349 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002350
Mark Brown3801b862011-06-09 16:22:22 +01002351 if (rdev->supply)
2352 while (rdev->open_count--)
2353 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05002354
Liam Girdwood414c70c2008-04-30 15:59:04 +01002355 return ret;
2356}
2357EXPORT_SYMBOL_GPL(regulator_force_disable);
2358
Mark Brownda07ecd2011-09-11 09:53:50 +01002359static void regulator_disable_work(struct work_struct *work)
2360{
2361 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2362 disable_work.work);
2363 int count, i, ret;
2364
2365 mutex_lock(&rdev->mutex);
2366
2367 BUG_ON(!rdev->deferred_disables);
2368
2369 count = rdev->deferred_disables;
2370 rdev->deferred_disables = 0;
2371
2372 for (i = 0; i < count; i++) {
2373 ret = _regulator_disable(rdev);
2374 if (ret != 0)
2375 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2376 }
2377
2378 mutex_unlock(&rdev->mutex);
2379
2380 if (rdev->supply) {
2381 for (i = 0; i < count; i++) {
2382 ret = regulator_disable(rdev->supply);
2383 if (ret != 0) {
2384 rdev_err(rdev,
2385 "Supply disable failed: %d\n", ret);
2386 }
2387 }
2388 }
2389}
2390
2391/**
2392 * regulator_disable_deferred - disable regulator output with delay
2393 * @regulator: regulator source
2394 * @ms: miliseconds until the regulator is disabled
2395 *
2396 * Execute regulator_disable() on the regulator after a delay. This
2397 * is intended for use with devices that require some time to quiesce.
2398 *
2399 * NOTE: this will only disable the regulator output if no other consumer
2400 * devices have it enabled, the regulator device supports disabling and
2401 * machine constraints permit this operation.
2402 */
2403int regulator_disable_deferred(struct regulator *regulator, int ms)
2404{
2405 struct regulator_dev *rdev = regulator->rdev;
2406
Mark Brown6492bc12012-04-19 13:19:07 +01002407 if (regulator->always_on)
2408 return 0;
2409
Mark Brown2b5a24a2012-09-07 11:00:53 +08002410 if (!ms)
2411 return regulator_disable(regulator);
2412
Mark Brownda07ecd2011-09-11 09:53:50 +01002413 mutex_lock(&rdev->mutex);
2414 rdev->deferred_disables++;
2415 mutex_unlock(&rdev->mutex);
2416
Dan Carpenter70dc6da2016-01-07 13:03:08 +03002417 queue_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2418 msecs_to_jiffies(ms));
2419 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01002420}
2421EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2422
Liam Girdwood414c70c2008-04-30 15:59:04 +01002423static int _regulator_is_enabled(struct regulator_dev *rdev)
2424{
Mark Brown65f73502012-06-27 14:14:38 +01002425 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002426 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002427 return rdev->ena_gpio_state;
2428
Mark Brown9a7f6a42010-02-11 17:22:45 +00002429 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002430 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002431 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002432
Mark Brown93325462009-08-03 18:49:56 +01002433 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002434}
2435
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002436static int _regulator_list_voltage(struct regulator *regulator,
2437 unsigned selector, int lock)
2438{
2439 struct regulator_dev *rdev = regulator->rdev;
2440 const struct regulator_ops *ops = rdev->desc->ops;
2441 int ret;
2442
2443 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2444 return rdev->desc->fixed_uV;
2445
2446 if (ops->list_voltage) {
2447 if (selector >= rdev->desc->n_voltages)
2448 return -EINVAL;
2449 if (lock)
2450 mutex_lock(&rdev->mutex);
2451 ret = ops->list_voltage(rdev, selector);
2452 if (lock)
2453 mutex_unlock(&rdev->mutex);
2454 } else if (rdev->supply) {
2455 ret = _regulator_list_voltage(rdev->supply, selector, lock);
2456 } else {
2457 return -EINVAL;
2458 }
2459
2460 if (ret > 0) {
2461 if (ret < rdev->constraints->min_uV)
2462 ret = 0;
2463 else if (ret > rdev->constraints->max_uV)
2464 ret = 0;
2465 }
2466
2467 return ret;
2468}
2469
Liam Girdwood414c70c2008-04-30 15:59:04 +01002470/**
2471 * regulator_is_enabled - is the regulator output enabled
2472 * @regulator: regulator source
2473 *
David Brownell412aec62008-11-16 11:44:46 -08002474 * Returns positive if the regulator driver backing the source/client
2475 * has requested that the device be enabled, zero if it hasn't, else a
2476 * negative errno code.
2477 *
2478 * Note that the device backing this regulator handle can have multiple
2479 * users, so it might be enabled even if regulator_enable() was never
2480 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002481 */
2482int regulator_is_enabled(struct regulator *regulator)
2483{
Mark Brown93325462009-08-03 18:49:56 +01002484 int ret;
2485
Mark Brown6492bc12012-04-19 13:19:07 +01002486 if (regulator->always_on)
2487 return 1;
2488
Mark Brown93325462009-08-03 18:49:56 +01002489 mutex_lock(&regulator->rdev->mutex);
2490 ret = _regulator_is_enabled(regulator->rdev);
2491 mutex_unlock(&regulator->rdev->mutex);
2492
2493 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002494}
2495EXPORT_SYMBOL_GPL(regulator_is_enabled);
2496
2497/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002498 * regulator_can_change_voltage - check if regulator can change voltage
2499 * @regulator: regulator source
2500 *
2501 * Returns positive if the regulator driver backing the source/client
Masanari Iidae2278672014-02-18 22:54:36 +09002502 * can change its voltage, false otherwise. Useful for detecting fixed
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002503 * or dummy regulators and disabling voltage change logic in the client
2504 * driver.
2505 */
2506int regulator_can_change_voltage(struct regulator *regulator)
2507{
2508 struct regulator_dev *rdev = regulator->rdev;
2509
2510 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002511 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2512 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2513 return 1;
2514
2515 if (rdev->desc->continuous_voltage_range &&
2516 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2517 rdev->constraints->min_uV != rdev->constraints->max_uV)
2518 return 1;
2519 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002520
2521 return 0;
2522}
2523EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2524
2525/**
David Brownell4367cfd2009-02-26 11:48:36 -08002526 * regulator_count_voltages - count regulator_list_voltage() selectors
2527 * @regulator: regulator source
2528 *
2529 * Returns number of selectors, or negative errno. Selectors are
2530 * numbered starting at zero, and typically correspond to bitfields
2531 * in hardware registers.
2532 */
2533int regulator_count_voltages(struct regulator *regulator)
2534{
2535 struct regulator_dev *rdev = regulator->rdev;
2536
Javier Martinez Canillas26988ef2014-07-29 18:28:56 +02002537 if (rdev->desc->n_voltages)
2538 return rdev->desc->n_voltages;
2539
2540 if (!rdev->supply)
2541 return -EINVAL;
2542
2543 return regulator_count_voltages(rdev->supply);
David Brownell4367cfd2009-02-26 11:48:36 -08002544}
2545EXPORT_SYMBOL_GPL(regulator_count_voltages);
2546
2547/**
2548 * regulator_list_voltage - enumerate supported voltages
2549 * @regulator: regulator source
2550 * @selector: identify voltage to list
2551 * Context: can sleep
2552 *
2553 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002554 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002555 * negative errno.
2556 */
2557int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2558{
Sascha Hauer3a40cfc2015-09-30 16:05:43 +02002559 return _regulator_list_voltage(regulator, selector, 1);
David Brownell4367cfd2009-02-26 11:48:36 -08002560}
2561EXPORT_SYMBOL_GPL(regulator_list_voltage);
2562
2563/**
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002564 * regulator_get_regmap - get the regulator's register map
2565 * @regulator: regulator source
2566 *
2567 * Returns the register map for the given regulator, or an ERR_PTR value
2568 * if the regulator doesn't use regmap.
2569 */
2570struct regmap *regulator_get_regmap(struct regulator *regulator)
2571{
2572 struct regmap *map = regulator->rdev->regmap;
2573
2574 return map ? map : ERR_PTR(-EOPNOTSUPP);
2575}
2576
2577/**
2578 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2579 * @regulator: regulator source
2580 * @vsel_reg: voltage selector register, output parameter
2581 * @vsel_mask: mask for voltage selector bitfield, output parameter
2582 *
2583 * Returns the hardware register offset and bitmask used for setting the
2584 * regulator voltage. This might be useful when configuring voltage-scaling
2585 * hardware or firmware that can make I2C requests behind the kernel's back,
2586 * for example.
2587 *
2588 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2589 * and 0 is returned, otherwise a negative errno is returned.
2590 */
2591int regulator_get_hardware_vsel_register(struct regulator *regulator,
2592 unsigned *vsel_reg,
2593 unsigned *vsel_mask)
2594{
Guodong Xu39f54602014-08-19 18:07:41 +08002595 struct regulator_dev *rdev = regulator->rdev;
2596 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002597
2598 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2599 return -EOPNOTSUPP;
2600
2601 *vsel_reg = rdev->desc->vsel_reg;
2602 *vsel_mask = rdev->desc->vsel_mask;
2603
2604 return 0;
2605}
2606EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2607
2608/**
2609 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2610 * @regulator: regulator source
2611 * @selector: identify voltage to list
2612 *
2613 * Converts the selector to a hardware-specific voltage selector that can be
2614 * directly written to the regulator registers. The address of the voltage
2615 * register can be determined by calling @regulator_get_hardware_vsel_register.
2616 *
2617 * On error a negative errno is returned.
2618 */
2619int regulator_list_hardware_vsel(struct regulator *regulator,
2620 unsigned selector)
2621{
Guodong Xu39f54602014-08-19 18:07:41 +08002622 struct regulator_dev *rdev = regulator->rdev;
2623 const struct regulator_ops *ops = rdev->desc->ops;
Tuomas Tynkkynen04eca282014-07-21 18:38:48 +03002624
2625 if (selector >= rdev->desc->n_voltages)
2626 return -EINVAL;
2627 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2628 return -EOPNOTSUPP;
2629
2630 return selector;
2631}
2632EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2633
2634/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002635 * regulator_get_linear_step - return the voltage step size between VSEL values
2636 * @regulator: regulator source
2637 *
2638 * Returns the voltage step size between VSEL values for linear
2639 * regulators, or return 0 if the regulator isn't a linear regulator.
2640 */
2641unsigned int regulator_get_linear_step(struct regulator *regulator)
2642{
2643 struct regulator_dev *rdev = regulator->rdev;
2644
2645 return rdev->desc->uV_step;
2646}
2647EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2648
2649/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002650 * regulator_is_supported_voltage - check if a voltage range can be supported
2651 *
2652 * @regulator: Regulator to check.
2653 * @min_uV: Minimum required voltage in uV.
2654 * @max_uV: Maximum required voltage in uV.
2655 *
2656 * Returns a boolean or a negative error code.
2657 */
2658int regulator_is_supported_voltage(struct regulator *regulator,
2659 int min_uV, int max_uV)
2660{
Mark Brownc5f39392012-07-02 15:00:18 +01002661 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002662 int i, voltages, ret;
2663
Mark Brownc5f39392012-07-02 15:00:18 +01002664 /* If we can't change voltage check the current voltage */
2665 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2666 ret = regulator_get_voltage(regulator);
2667 if (ret >= 0)
Jingoo Han0d25d092014-01-08 10:02:16 +09002668 return min_uV <= ret && ret <= max_uV;
Mark Brownc5f39392012-07-02 15:00:18 +01002669 else
2670 return ret;
2671 }
2672
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002673 /* Any voltage within constrains range is fine? */
2674 if (rdev->desc->continuous_voltage_range)
2675 return min_uV >= rdev->constraints->min_uV &&
2676 max_uV <= rdev->constraints->max_uV;
2677
Mark Browna7a1ad92009-07-21 16:00:24 +01002678 ret = regulator_count_voltages(regulator);
2679 if (ret < 0)
2680 return ret;
2681 voltages = ret;
2682
2683 for (i = 0; i < voltages; i++) {
2684 ret = regulator_list_voltage(regulator, i);
2685
2686 if (ret >= min_uV && ret <= max_uV)
2687 return 1;
2688 }
2689
2690 return 0;
2691}
Mark Browna398eaa2011-12-28 12:48:45 +00002692EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002693
Sascha Hauera204f412015-10-20 14:37:27 +02002694static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2695 int max_uV)
2696{
2697 const struct regulator_desc *desc = rdev->desc;
2698
2699 if (desc->ops->map_voltage)
2700 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2701
2702 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2703 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2704
2705 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2706 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2707
2708 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2709}
2710
Heiko Stübner71795692014-08-28 12:36:04 -07002711static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2712 int min_uV, int max_uV,
2713 unsigned *selector)
2714{
2715 struct pre_voltage_change_data data;
2716 int ret;
2717
2718 data.old_uV = _regulator_get_voltage(rdev);
2719 data.min_uV = min_uV;
2720 data.max_uV = max_uV;
2721 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2722 &data);
2723 if (ret & NOTIFY_STOP_MASK)
2724 return -EINVAL;
2725
2726 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2727 if (ret >= 0)
2728 return ret;
2729
2730 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2731 (void *)data.old_uV);
2732
2733 return ret;
2734}
2735
2736static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2737 int uV, unsigned selector)
2738{
2739 struct pre_voltage_change_data data;
2740 int ret;
2741
2742 data.old_uV = _regulator_get_voltage(rdev);
2743 data.min_uV = uV;
2744 data.max_uV = uV;
2745 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2746 &data);
2747 if (ret & NOTIFY_STOP_MASK)
2748 return -EINVAL;
2749
2750 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2751 if (ret >= 0)
2752 return ret;
2753
2754 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2755 (void *)data.old_uV);
2756
2757 return ret;
2758}
2759
Mark Brown75790252010-12-12 14:25:50 +00002760static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2761 int min_uV, int max_uV)
2762{
2763 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002764 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002765 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002766 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002767 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002768
2769 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2770
Mark Brownbf5892a2011-05-08 22:13:37 +01002771 min_uV += rdev->constraints->uV_offset;
2772 max_uV += rdev->constraints->uV_offset;
2773
Axel Lineba41a52012-04-04 10:32:10 +08002774 /*
2775 * If we can't obtain the old selector there is not enough
2776 * info to call set_voltage_time_sel().
2777 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002778 if (_regulator_is_enabled(rdev) &&
2779 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002780 rdev->desc->ops->get_voltage_sel) {
2781 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2782 if (old_selector < 0)
2783 return old_selector;
2784 }
2785
Mark Brown75790252010-12-12 14:25:50 +00002786 if (rdev->desc->ops->set_voltage) {
Heiko Stübner71795692014-08-28 12:36:04 -07002787 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2788 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002789
2790 if (ret >= 0) {
2791 if (rdev->desc->ops->list_voltage)
2792 best_val = rdev->desc->ops->list_voltage(rdev,
2793 selector);
2794 else
2795 best_val = _regulator_get_voltage(rdev);
2796 }
2797
Mark Browne8eef822010-12-12 14:36:17 +00002798 } else if (rdev->desc->ops->set_voltage_sel) {
Sascha Hauera204f412015-10-20 14:37:27 +02002799 ret = regulator_map_voltage(rdev, min_uV, max_uV);
Mark Browne843fc42012-05-09 21:16:06 +01002800 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002801 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2802 if (min_uV <= best_val && max_uV >= best_val) {
2803 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002804 if (old_selector == selector)
2805 ret = 0;
2806 else
Heiko Stübner71795692014-08-28 12:36:04 -07002807 ret = _regulator_call_set_voltage_sel(
2808 rdev, best_val, selector);
Mark Browne113d792012-06-26 10:57:51 +01002809 } else {
2810 ret = -EINVAL;
2811 }
Mark Browne8eef822010-12-12 14:36:17 +00002812 }
Mark Brown75790252010-12-12 14:25:50 +00002813 } else {
2814 ret = -EINVAL;
2815 }
2816
Axel Lineba41a52012-04-04 10:32:10 +08002817 /* Call set_voltage_time_sel if successfully obtained old_selector */
Yadwinder Singh Brar5b175952013-06-29 18:21:19 +05302818 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2819 && old_selector != selector) {
Axel Lineba41a52012-04-04 10:32:10 +08002820
2821 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2822 old_selector, selector);
2823 if (delay < 0) {
2824 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2825 delay);
2826 delay = 0;
2827 }
Axel Lineba41a52012-04-04 10:32:10 +08002828
Philip Rakity8b96de32012-06-14 15:07:56 -07002829 /* Insert any necessary delays */
2830 if (delay >= 1000) {
2831 mdelay(delay / 1000);
2832 udelay(delay % 1000);
2833 } else if (delay) {
2834 udelay(delay);
2835 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002836 }
2837
Axel Lin2f6c7972012-08-06 23:44:19 +08002838 if (ret == 0 && best_val >= 0) {
2839 unsigned long data = best_val;
2840
Mark Brownded06a52010-12-16 13:59:10 +00002841 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002842 (void *)data);
2843 }
Mark Brownded06a52010-12-16 13:59:10 +00002844
Axel Lineba41a52012-04-04 10:32:10 +08002845 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002846
2847 return ret;
2848}
2849
Sascha Hauera9f226b2015-10-13 12:45:25 +02002850static int regulator_set_voltage_unlocked(struct regulator *regulator,
2851 int min_uV, int max_uV)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002852{
2853 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002854 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002855 int old_min_uV, old_max_uV;
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002856 int current_uV;
Sascha Hauerfc421122015-10-20 14:37:28 +02002857 int best_supply_uV = 0;
2858 int supply_change_uV = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002859
Mark Brown95a3c232010-12-16 15:49:37 +00002860 /* If we're setting the same range as last time the change
2861 * should be a noop (some cpufreq implementations use the same
2862 * voltage for multiple frequencies, for example).
2863 */
2864 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2865 goto out;
2866
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002867 /* If we're trying to set a range that overlaps the current voltage,
Viresh Kumard3fb9802015-08-13 18:09:49 +05302868 * return successfully even though the regulator does not support
Bjorn Anderssonc00dc352014-02-05 12:30:26 -08002869 * changing the voltage.
2870 */
2871 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2872 current_uV = _regulator_get_voltage(rdev);
2873 if (min_uV <= current_uV && current_uV <= max_uV) {
2874 regulator->min_uV = min_uV;
2875 regulator->max_uV = max_uV;
2876 goto out;
2877 }
2878 }
2879
Liam Girdwood414c70c2008-04-30 15:59:04 +01002880 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002881 if (!rdev->desc->ops->set_voltage &&
2882 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002883 ret = -EINVAL;
2884 goto out;
2885 }
2886
2887 /* constraints check */
2888 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2889 if (ret < 0)
2890 goto out;
Jingoo Han0d25d092014-01-08 10:02:16 +09002891
Paolo Pisati92d7a552012-12-13 10:13:00 +01002892 /* restore original values in case of error */
2893 old_min_uV = regulator->min_uV;
2894 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002895 regulator->min_uV = min_uV;
2896 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002897
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002898 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2899 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002900 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002901
Sascha Hauerfc421122015-10-20 14:37:28 +02002902 if (rdev->supply && (rdev->desc->min_dropout_uV ||
2903 !rdev->desc->ops->get_voltage)) {
2904 int current_supply_uV;
2905 int selector;
2906
2907 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2908 if (selector < 0) {
2909 ret = selector;
2910 goto out2;
2911 }
2912
2913 best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
2914 if (best_supply_uV < 0) {
2915 ret = best_supply_uV;
2916 goto out2;
2917 }
2918
2919 best_supply_uV += rdev->desc->min_dropout_uV;
2920
2921 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
2922 if (current_supply_uV < 0) {
2923 ret = current_supply_uV;
2924 goto out2;
2925 }
2926
2927 supply_change_uV = best_supply_uV - current_supply_uV;
2928 }
2929
2930 if (supply_change_uV > 0) {
2931 ret = regulator_set_voltage_unlocked(rdev->supply,
2932 best_supply_uV, INT_MAX);
2933 if (ret) {
2934 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
2935 ret);
2936 goto out2;
2937 }
2938 }
2939
Mark Brown75790252010-12-12 14:25:50 +00002940 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002941 if (ret < 0)
2942 goto out2;
Jingoo Han0d25d092014-01-08 10:02:16 +09002943
Sascha Hauerfc421122015-10-20 14:37:28 +02002944 if (supply_change_uV < 0) {
2945 ret = regulator_set_voltage_unlocked(rdev->supply,
2946 best_supply_uV, INT_MAX);
2947 if (ret)
2948 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
2949 ret);
2950 /* No need to fail here */
2951 ret = 0;
2952 }
2953
Liam Girdwood414c70c2008-04-30 15:59:04 +01002954out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002955 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002956out2:
2957 regulator->min_uV = old_min_uV;
2958 regulator->max_uV = old_max_uV;
Sascha Hauera9f226b2015-10-13 12:45:25 +02002959
2960 return ret;
2961}
2962
2963/**
2964 * regulator_set_voltage - set regulator output voltage
2965 * @regulator: regulator source
2966 * @min_uV: Minimum required voltage in uV
2967 * @max_uV: Maximum acceptable voltage in uV
2968 *
2969 * Sets a voltage regulator to the desired output voltage. This can be set
2970 * during any regulator state. IOW, regulator can be disabled or enabled.
2971 *
2972 * If the regulator is enabled then the voltage will change to the new value
2973 * immediately otherwise if the regulator is disabled the regulator will
2974 * output at the new voltage when enabled.
2975 *
2976 * NOTE: If the regulator is shared between several devices then the lowest
2977 * request voltage that meets the system constraints will be used.
2978 * Regulator system constraints must be set for this regulator before
2979 * calling this function otherwise this call will fail.
2980 */
2981int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2982{
2983 int ret = 0;
2984
Sascha Hauerfc421122015-10-20 14:37:28 +02002985 regulator_lock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02002986
2987 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV);
2988
Sascha Hauerfc421122015-10-20 14:37:28 +02002989 regulator_unlock_supply(regulator->rdev);
Sascha Hauera9f226b2015-10-13 12:45:25 +02002990
Paolo Pisati92d7a552012-12-13 10:13:00 +01002991 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002992}
2993EXPORT_SYMBOL_GPL(regulator_set_voltage);
2994
Mark Brown606a2562010-12-16 15:49:36 +00002995/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002996 * regulator_set_voltage_time - get raise/fall time
2997 * @regulator: regulator source
2998 * @old_uV: starting voltage in microvolts
2999 * @new_uV: target voltage in microvolts
3000 *
3001 * Provided with the starting and ending voltage, this function attempts to
3002 * calculate the time in microseconds required to rise or fall to this new
3003 * voltage.
3004 */
3005int regulator_set_voltage_time(struct regulator *regulator,
3006 int old_uV, int new_uV)
3007{
Guodong Xu272e2312014-08-13 19:33:38 +08003008 struct regulator_dev *rdev = regulator->rdev;
3009 const struct regulator_ops *ops = rdev->desc->ops;
Linus Walleij88cd222b2011-03-17 13:24:52 +01003010 int old_sel = -1;
3011 int new_sel = -1;
3012 int voltage;
3013 int i;
3014
3015 /* Currently requires operations to do this */
3016 if (!ops->list_voltage || !ops->set_voltage_time_sel
3017 || !rdev->desc->n_voltages)
3018 return -EINVAL;
3019
3020 for (i = 0; i < rdev->desc->n_voltages; i++) {
3021 /* We only look for exact voltage matches here */
3022 voltage = regulator_list_voltage(regulator, i);
3023 if (voltage < 0)
3024 return -EINVAL;
3025 if (voltage == 0)
3026 continue;
3027 if (voltage == old_uV)
3028 old_sel = i;
3029 if (voltage == new_uV)
3030 new_sel = i;
3031 }
3032
3033 if (old_sel < 0 || new_sel < 0)
3034 return -EINVAL;
3035
3036 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3037}
3038EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3039
3040/**
Randy Dunlap296c6562012-08-18 17:44:14 -07003041 * regulator_set_voltage_time_sel - get raise/fall time
3042 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303043 * @old_selector: selector for starting voltage
3044 * @new_selector: selector for target voltage
3045 *
3046 * Provided with the starting and target voltage selectors, this function
3047 * returns time in microseconds required to rise or fall to this new voltage
3048 *
Axel Linf11d08c2012-06-19 09:38:45 +08003049 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08003050 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303051 */
3052int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3053 unsigned int old_selector,
3054 unsigned int new_selector)
3055{
Axel Lin398715a2012-06-18 10:11:28 +08003056 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08003057 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08003058
3059 if (rdev->constraints->ramp_delay)
3060 ramp_delay = rdev->constraints->ramp_delay;
3061 else if (rdev->desc->ramp_delay)
3062 ramp_delay = rdev->desc->ramp_delay;
3063
3064 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05303065 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08003066 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05303067 }
Axel Lin398715a2012-06-18 10:11:28 +08003068
Axel Linf11d08c2012-06-19 09:38:45 +08003069 /* sanity check */
3070 if (!rdev->desc->ops->list_voltage)
3071 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08003072
Axel Linf11d08c2012-06-19 09:38:45 +08003073 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3074 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3075
3076 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303077}
Mark Brownb19dbf72012-06-23 11:34:20 +01003078EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05303079
3080/**
Mark Brown606a2562010-12-16 15:49:36 +00003081 * regulator_sync_voltage - re-apply last regulator output voltage
3082 * @regulator: regulator source
3083 *
3084 * Re-apply the last configured voltage. This is intended to be used
3085 * where some external control source the consumer is cooperating with
3086 * has caused the configured voltage to change.
3087 */
3088int regulator_sync_voltage(struct regulator *regulator)
3089{
3090 struct regulator_dev *rdev = regulator->rdev;
3091 int ret, min_uV, max_uV;
3092
3093 mutex_lock(&rdev->mutex);
3094
3095 if (!rdev->desc->ops->set_voltage &&
3096 !rdev->desc->ops->set_voltage_sel) {
3097 ret = -EINVAL;
3098 goto out;
3099 }
3100
3101 /* This is only going to work if we've had a voltage configured. */
3102 if (!regulator->min_uV && !regulator->max_uV) {
3103 ret = -EINVAL;
3104 goto out;
3105 }
3106
3107 min_uV = regulator->min_uV;
3108 max_uV = regulator->max_uV;
3109
3110 /* This should be a paranoia check... */
3111 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3112 if (ret < 0)
3113 goto out;
3114
3115 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
3116 if (ret < 0)
3117 goto out;
3118
3119 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3120
3121out:
3122 mutex_unlock(&rdev->mutex);
3123 return ret;
3124}
3125EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3126
Liam Girdwood414c70c2008-04-30 15:59:04 +01003127static int _regulator_get_voltage(struct regulator_dev *rdev)
3128{
Mark Brownbf5892a2011-05-08 22:13:37 +01003129 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00003130
3131 if (rdev->desc->ops->get_voltage_sel) {
3132 sel = rdev->desc->ops->get_voltage_sel(rdev);
3133 if (sel < 0)
3134 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01003135 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08003136 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01003137 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01003138 } else if (rdev->desc->ops->list_voltage) {
3139 ret = rdev->desc->ops->list_voltage(rdev, 0);
Laxman Dewangan5a523602013-09-10 15:45:05 +05303140 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3141 ret = rdev->desc->fixed_uV;
Javier Martinez Canillase3039962014-07-29 18:28:55 +02003142 } else if (rdev->supply) {
Mark Brownd9b96d32015-11-03 05:58:14 +00003143 ret = _regulator_get_voltage(rdev->supply->rdev);
Axel Lincb220d12011-05-23 20:08:10 +08003144 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003145 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08003146 }
Mark Brownbf5892a2011-05-08 22:13:37 +01003147
Axel Lincb220d12011-05-23 20:08:10 +08003148 if (ret < 0)
3149 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01003150 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003151}
3152
3153/**
3154 * regulator_get_voltage - get regulator output voltage
3155 * @regulator: regulator source
3156 *
3157 * This returns the current regulator voltage in uV.
3158 *
3159 * NOTE: If the regulator is disabled it will return the voltage value. This
3160 * function should not be used to determine regulator state.
3161 */
3162int regulator_get_voltage(struct regulator *regulator)
3163{
3164 int ret;
3165
Mark Brownd9b96d32015-11-03 05:58:14 +00003166 regulator_lock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003167
3168 ret = _regulator_get_voltage(regulator->rdev);
3169
Mark Brownd9b96d32015-11-03 05:58:14 +00003170 regulator_unlock_supply(regulator->rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003171
3172 return ret;
3173}
3174EXPORT_SYMBOL_GPL(regulator_get_voltage);
3175
3176/**
3177 * regulator_set_current_limit - set regulator output current limit
3178 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01003179 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01003180 * @max_uA: Maximum supported current in uA
3181 *
3182 * Sets current sink to the desired output current. This can be set during
3183 * any regulator state. IOW, regulator can be disabled or enabled.
3184 *
3185 * If the regulator is enabled then the current will change to the new value
3186 * immediately otherwise if the regulator is disabled the regulator will
3187 * output at the new current when enabled.
3188 *
3189 * NOTE: Regulator system constraints must be set for this regulator before
3190 * calling this function otherwise this call will fail.
3191 */
3192int regulator_set_current_limit(struct regulator *regulator,
3193 int min_uA, int max_uA)
3194{
3195 struct regulator_dev *rdev = regulator->rdev;
3196 int ret;
3197
3198 mutex_lock(&rdev->mutex);
3199
3200 /* sanity check */
3201 if (!rdev->desc->ops->set_current_limit) {
3202 ret = -EINVAL;
3203 goto out;
3204 }
3205
3206 /* constraints check */
3207 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3208 if (ret < 0)
3209 goto out;
3210
3211 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3212out:
3213 mutex_unlock(&rdev->mutex);
3214 return ret;
3215}
3216EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3217
3218static int _regulator_get_current_limit(struct regulator_dev *rdev)
3219{
3220 int ret;
3221
3222 mutex_lock(&rdev->mutex);
3223
3224 /* sanity check */
3225 if (!rdev->desc->ops->get_current_limit) {
3226 ret = -EINVAL;
3227 goto out;
3228 }
3229
3230 ret = rdev->desc->ops->get_current_limit(rdev);
3231out:
3232 mutex_unlock(&rdev->mutex);
3233 return ret;
3234}
3235
3236/**
3237 * regulator_get_current_limit - get regulator output current
3238 * @regulator: regulator source
3239 *
3240 * This returns the current supplied by the specified current sink in uA.
3241 *
3242 * NOTE: If the regulator is disabled it will return the current value. This
3243 * function should not be used to determine regulator state.
3244 */
3245int regulator_get_current_limit(struct regulator *regulator)
3246{
3247 return _regulator_get_current_limit(regulator->rdev);
3248}
3249EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3250
3251/**
3252 * regulator_set_mode - set regulator operating mode
3253 * @regulator: regulator source
3254 * @mode: operating mode - one of the REGULATOR_MODE constants
3255 *
3256 * Set regulator operating mode to increase regulator efficiency or improve
3257 * regulation performance.
3258 *
3259 * NOTE: Regulator system constraints must be set for this regulator before
3260 * calling this function otherwise this call will fail.
3261 */
3262int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3263{
3264 struct regulator_dev *rdev = regulator->rdev;
3265 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303266 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003267
3268 mutex_lock(&rdev->mutex);
3269
3270 /* sanity check */
3271 if (!rdev->desc->ops->set_mode) {
3272 ret = -EINVAL;
3273 goto out;
3274 }
3275
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05303276 /* return if the same mode is requested */
3277 if (rdev->desc->ops->get_mode) {
3278 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3279 if (regulator_curr_mode == mode) {
3280 ret = 0;
3281 goto out;
3282 }
3283 }
3284
Liam Girdwood414c70c2008-04-30 15:59:04 +01003285 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08003286 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003287 if (ret < 0)
3288 goto out;
3289
3290 ret = rdev->desc->ops->set_mode(rdev, mode);
3291out:
3292 mutex_unlock(&rdev->mutex);
3293 return ret;
3294}
3295EXPORT_SYMBOL_GPL(regulator_set_mode);
3296
3297static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3298{
3299 int ret;
3300
3301 mutex_lock(&rdev->mutex);
3302
3303 /* sanity check */
3304 if (!rdev->desc->ops->get_mode) {
3305 ret = -EINVAL;
3306 goto out;
3307 }
3308
3309 ret = rdev->desc->ops->get_mode(rdev);
3310out:
3311 mutex_unlock(&rdev->mutex);
3312 return ret;
3313}
3314
3315/**
3316 * regulator_get_mode - get regulator operating mode
3317 * @regulator: regulator source
3318 *
3319 * Get the current regulator operating mode.
3320 */
3321unsigned int regulator_get_mode(struct regulator *regulator)
3322{
3323 return _regulator_get_mode(regulator->rdev);
3324}
3325EXPORT_SYMBOL_GPL(regulator_get_mode);
3326
3327/**
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003328 * regulator_set_load - set regulator load
Liam Girdwood414c70c2008-04-30 15:59:04 +01003329 * @regulator: regulator source
3330 * @uA_load: load current
3331 *
3332 * Notifies the regulator core of a new device load. This is then used by
3333 * DRMS (if enabled by constraints) to set the most efficient regulator
3334 * operating mode for the new regulator loading.
3335 *
3336 * Consumer devices notify their supply regulator of the maximum power
3337 * they will require (can be taken from device datasheet in the power
3338 * consumption tables) when they change operational status and hence power
3339 * state. Examples of operational state changes that can affect power
3340 * consumption are :-
3341 *
3342 * o Device is opened / closed.
3343 * o Device I/O is about to begin or has just finished.
3344 * o Device is idling in between work.
3345 *
3346 * This information is also exported via sysfs to userspace.
3347 *
3348 * DRMS will sum the total requested load on the regulator and change
3349 * to the most efficient operating mode if platform constraints allow.
3350 *
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003351 * On error a negative errno is returned.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003352 */
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003353int regulator_set_load(struct regulator *regulator, int uA_load)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003354{
3355 struct regulator_dev *rdev = regulator->rdev;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003356 int ret;
Stephen Boydd92d95b62012-07-02 19:21:06 -07003357
Liam Girdwood414c70c2008-04-30 15:59:04 +01003358 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003359 regulator->uA_load = uA_load;
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003360 ret = drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003361 mutex_unlock(&rdev->mutex);
Bjorn Andersson8460ef32015-01-27 18:46:31 -08003362
Liam Girdwood414c70c2008-04-30 15:59:04 +01003363 return ret;
3364}
Bjorn Anderssone39ce482015-02-11 19:35:27 -08003365EXPORT_SYMBOL_GPL(regulator_set_load);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003366
3367/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003368 * regulator_allow_bypass - allow the regulator to go into bypass mode
3369 *
3370 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003371 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003372 *
3373 * Allow the regulator to go into bypass mode if all other consumers
3374 * for the regulator also enable bypass mode and the machine
3375 * constraints allow this. Bypass mode means that the regulator is
3376 * simply passing the input directly to the output with no regulation.
3377 */
3378int regulator_allow_bypass(struct regulator *regulator, bool enable)
3379{
3380 struct regulator_dev *rdev = regulator->rdev;
3381 int ret = 0;
3382
3383 if (!rdev->desc->ops->set_bypass)
3384 return 0;
3385
3386 if (rdev->constraints &&
3387 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3388 return 0;
3389
3390 mutex_lock(&rdev->mutex);
3391
3392 if (enable && !regulator->bypass) {
3393 rdev->bypass_count++;
3394
3395 if (rdev->bypass_count == rdev->open_count) {
3396 ret = rdev->desc->ops->set_bypass(rdev, enable);
3397 if (ret != 0)
3398 rdev->bypass_count--;
3399 }
3400
3401 } else if (!enable && regulator->bypass) {
3402 rdev->bypass_count--;
3403
3404 if (rdev->bypass_count != rdev->open_count) {
3405 ret = rdev->desc->ops->set_bypass(rdev, enable);
3406 if (ret != 0)
3407 rdev->bypass_count++;
3408 }
3409 }
3410
3411 if (ret == 0)
3412 regulator->bypass = enable;
3413
3414 mutex_unlock(&rdev->mutex);
3415
3416 return ret;
3417}
3418EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3419
3420/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003421 * regulator_register_notifier - register regulator event notifier
3422 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003423 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003424 *
3425 * Register notifier block to receive regulator events.
3426 */
3427int regulator_register_notifier(struct regulator *regulator,
3428 struct notifier_block *nb)
3429{
3430 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3431 nb);
3432}
3433EXPORT_SYMBOL_GPL(regulator_register_notifier);
3434
3435/**
3436 * regulator_unregister_notifier - unregister regulator event notifier
3437 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003438 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003439 *
3440 * Unregister regulator event notifier block.
3441 */
3442int regulator_unregister_notifier(struct regulator *regulator,
3443 struct notifier_block *nb)
3444{
3445 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3446 nb);
3447}
3448EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3449
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003450/* notify regulator consumers and downstream regulator consumers.
3451 * Note mutex must be held by caller.
3452 */
Heiko Stübner71795692014-08-28 12:36:04 -07003453static int _notifier_call_chain(struct regulator_dev *rdev,
Liam Girdwood414c70c2008-04-30 15:59:04 +01003454 unsigned long event, void *data)
3455{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003456 /* call rdev chain first */
Heiko Stübner71795692014-08-28 12:36:04 -07003457 return blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003458}
3459
3460/**
3461 * regulator_bulk_get - get multiple regulator consumers
3462 *
3463 * @dev: Device to supply
3464 * @num_consumers: Number of consumers to register
3465 * @consumers: Configuration of consumers; clients are stored here.
3466 *
3467 * @return 0 on success, an errno on failure.
3468 *
3469 * This helper function allows drivers to get several regulator
3470 * consumers in one operation. If any of the regulators cannot be
3471 * acquired then any regulators that were allocated will be freed
3472 * before returning to the caller.
3473 */
3474int regulator_bulk_get(struct device *dev, int num_consumers,
3475 struct regulator_bulk_data *consumers)
3476{
3477 int i;
3478 int ret;
3479
3480 for (i = 0; i < num_consumers; i++)
3481 consumers[i].consumer = NULL;
3482
3483 for (i = 0; i < num_consumers; i++) {
Bjorn Andersson3ff3f512015-11-09 22:20:37 -08003484 consumers[i].consumer = _regulator_get(dev,
3485 consumers[i].supply,
3486 false,
3487 !consumers[i].optional);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003488 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003489 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003490 dev_err(dev, "Failed to get supply '%s': %d\n",
3491 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003492 consumers[i].consumer = NULL;
3493 goto err;
3494 }
3495 }
3496
3497 return 0;
3498
3499err:
Axel Linb29c7692012-02-20 10:32:16 +08003500 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003501 regulator_put(consumers[i].consumer);
3502
3503 return ret;
3504}
3505EXPORT_SYMBOL_GPL(regulator_bulk_get);
3506
Mark Brownf21e0e82011-05-24 08:12:40 +08003507static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3508{
3509 struct regulator_bulk_data *bulk = data;
3510
3511 bulk->ret = regulator_enable(bulk->consumer);
3512}
3513
Liam Girdwood414c70c2008-04-30 15:59:04 +01003514/**
3515 * regulator_bulk_enable - enable multiple regulator consumers
3516 *
3517 * @num_consumers: Number of consumers
3518 * @consumers: Consumer data; clients are stored here.
3519 * @return 0 on success, an errno on failure
3520 *
3521 * This convenience API allows consumers to enable multiple regulator
3522 * clients in a single API call. If any consumers cannot be enabled
3523 * then any others that were enabled will be disabled again prior to
3524 * return.
3525 */
3526int regulator_bulk_enable(int num_consumers,
3527 struct regulator_bulk_data *consumers)
3528{
Dan Williams2955b472012-07-09 19:33:25 -07003529 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003530 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003531 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003532
Mark Brown6492bc12012-04-19 13:19:07 +01003533 for (i = 0; i < num_consumers; i++) {
3534 if (consumers[i].consumer->always_on)
3535 consumers[i].ret = 0;
3536 else
3537 async_schedule_domain(regulator_bulk_enable_async,
3538 &consumers[i], &async_domain);
3539 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003540
3541 async_synchronize_full_domain(&async_domain);
3542
3543 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003544 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003545 if (consumers[i].ret != 0) {
3546 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003547 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003548 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003549 }
3550
3551 return 0;
3552
3553err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003554 for (i = 0; i < num_consumers; i++) {
3555 if (consumers[i].ret < 0)
3556 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3557 consumers[i].ret);
3558 else
3559 regulator_disable(consumers[i].consumer);
3560 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003561
3562 return ret;
3563}
3564EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3565
3566/**
3567 * regulator_bulk_disable - disable multiple regulator consumers
3568 *
3569 * @num_consumers: Number of consumers
3570 * @consumers: Consumer data; clients are stored here.
3571 * @return 0 on success, an errno on failure
3572 *
3573 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003574 * clients in a single API call. If any consumers cannot be disabled
3575 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003576 * return.
3577 */
3578int regulator_bulk_disable(int num_consumers,
3579 struct regulator_bulk_data *consumers)
3580{
3581 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003582 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003583
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003584 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003585 ret = regulator_disable(consumers[i].consumer);
3586 if (ret != 0)
3587 goto err;
3588 }
3589
3590 return 0;
3591
3592err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003593 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003594 for (++i; i < num_consumers; ++i) {
3595 r = regulator_enable(consumers[i].consumer);
3596 if (r != 0)
3597 pr_err("Failed to reename %s: %d\n",
3598 consumers[i].supply, r);
3599 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003600
3601 return ret;
3602}
3603EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3604
3605/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003606 * regulator_bulk_force_disable - force disable multiple regulator consumers
3607 *
3608 * @num_consumers: Number of consumers
3609 * @consumers: Consumer data; clients are stored here.
3610 * @return 0 on success, an errno on failure
3611 *
3612 * This convenience API allows consumers to forcibly disable multiple regulator
3613 * clients in a single API call.
3614 * NOTE: This should be used for situations when device damage will
3615 * likely occur if the regulators are not disabled (e.g. over temp).
3616 * Although regulator_force_disable function call for some consumers can
3617 * return error numbers, the function is called for all consumers.
3618 */
3619int regulator_bulk_force_disable(int num_consumers,
3620 struct regulator_bulk_data *consumers)
3621{
3622 int i;
3623 int ret;
3624
3625 for (i = 0; i < num_consumers; i++)
3626 consumers[i].ret =
3627 regulator_force_disable(consumers[i].consumer);
3628
3629 for (i = 0; i < num_consumers; i++) {
3630 if (consumers[i].ret != 0) {
3631 ret = consumers[i].ret;
3632 goto out;
3633 }
3634 }
3635
3636 return 0;
3637out:
3638 return ret;
3639}
3640EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3641
3642/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003643 * regulator_bulk_free - free multiple regulator consumers
3644 *
3645 * @num_consumers: Number of consumers
3646 * @consumers: Consumer data; clients are stored here.
3647 *
3648 * This convenience API allows consumers to free multiple regulator
3649 * clients in a single API call.
3650 */
3651void regulator_bulk_free(int num_consumers,
3652 struct regulator_bulk_data *consumers)
3653{
3654 int i;
3655
3656 for (i = 0; i < num_consumers; i++) {
3657 regulator_put(consumers[i].consumer);
3658 consumers[i].consumer = NULL;
3659 }
3660}
3661EXPORT_SYMBOL_GPL(regulator_bulk_free);
3662
3663/**
3664 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003665 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003666 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003667 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003668 *
3669 * Called by regulator drivers to notify clients a regulator event has
3670 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003671 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003672 */
3673int regulator_notifier_call_chain(struct regulator_dev *rdev,
3674 unsigned long event, void *data)
3675{
Krzysztof Kozlowski70cfef22015-06-29 09:04:43 +09003676 lockdep_assert_held_once(&rdev->mutex);
3677
Liam Girdwood414c70c2008-04-30 15:59:04 +01003678 _notifier_call_chain(rdev, event, data);
3679 return NOTIFY_DONE;
3680
3681}
3682EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3683
Mark Brownbe721972009-08-04 20:09:52 +02003684/**
3685 * regulator_mode_to_status - convert a regulator mode into a status
3686 *
3687 * @mode: Mode to convert
3688 *
3689 * Convert a regulator mode into a status.
3690 */
3691int regulator_mode_to_status(unsigned int mode)
3692{
3693 switch (mode) {
3694 case REGULATOR_MODE_FAST:
3695 return REGULATOR_STATUS_FAST;
3696 case REGULATOR_MODE_NORMAL:
3697 return REGULATOR_STATUS_NORMAL;
3698 case REGULATOR_MODE_IDLE:
3699 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003700 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003701 return REGULATOR_STATUS_STANDBY;
3702 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003703 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003704 }
3705}
3706EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3707
Takashi Iwai39f802d2015-01-30 20:29:31 +01003708static struct attribute *regulator_dev_attrs[] = {
3709 &dev_attr_name.attr,
3710 &dev_attr_num_users.attr,
3711 &dev_attr_type.attr,
3712 &dev_attr_microvolts.attr,
3713 &dev_attr_microamps.attr,
3714 &dev_attr_opmode.attr,
3715 &dev_attr_state.attr,
3716 &dev_attr_status.attr,
3717 &dev_attr_bypass.attr,
3718 &dev_attr_requested_microamps.attr,
3719 &dev_attr_min_microvolts.attr,
3720 &dev_attr_max_microvolts.attr,
3721 &dev_attr_min_microamps.attr,
3722 &dev_attr_max_microamps.attr,
3723 &dev_attr_suspend_standby_state.attr,
3724 &dev_attr_suspend_mem_state.attr,
3725 &dev_attr_suspend_disk_state.attr,
3726 &dev_attr_suspend_standby_microvolts.attr,
3727 &dev_attr_suspend_mem_microvolts.attr,
3728 &dev_attr_suspend_disk_microvolts.attr,
3729 &dev_attr_suspend_standby_mode.attr,
3730 &dev_attr_suspend_mem_mode.attr,
3731 &dev_attr_suspend_disk_mode.attr,
3732 NULL
3733};
3734
David Brownell7ad68e22008-11-11 17:39:02 -08003735/*
3736 * To avoid cluttering sysfs (and memory) with useless state, only
3737 * create attributes that can be meaningfully displayed.
3738 */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003739static umode_t regulator_attr_is_visible(struct kobject *kobj,
3740 struct attribute *attr, int idx)
David Brownell7ad68e22008-11-11 17:39:02 -08003741{
Takashi Iwai39f802d2015-01-30 20:29:31 +01003742 struct device *dev = kobj_to_dev(kobj);
Geliang Tang83080a12016-01-05 22:07:55 +08003743 struct regulator_dev *rdev = dev_to_rdev(dev);
Guodong Xu272e2312014-08-13 19:33:38 +08003744 const struct regulator_ops *ops = rdev->desc->ops;
Takashi Iwai39f802d2015-01-30 20:29:31 +01003745 umode_t mode = attr->mode;
3746
3747 /* these three are always present */
3748 if (attr == &dev_attr_name.attr ||
3749 attr == &dev_attr_num_users.attr ||
3750 attr == &dev_attr_type.attr)
3751 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003752
3753 /* some attributes need specific methods to be displayed */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003754 if (attr == &dev_attr_microvolts.attr) {
3755 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3756 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3757 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3758 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3759 return mode;
3760 return 0;
Mark Brownf59c8f92012-08-31 10:36:37 -07003761 }
David Brownell7ad68e22008-11-11 17:39:02 -08003762
Takashi Iwai39f802d2015-01-30 20:29:31 +01003763 if (attr == &dev_attr_microamps.attr)
3764 return ops->get_current_limit ? mode : 0;
3765
3766 if (attr == &dev_attr_opmode.attr)
3767 return ops->get_mode ? mode : 0;
3768
3769 if (attr == &dev_attr_state.attr)
3770 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3771
3772 if (attr == &dev_attr_status.attr)
3773 return ops->get_status ? mode : 0;
3774
3775 if (attr == &dev_attr_bypass.attr)
3776 return ops->get_bypass ? mode : 0;
3777
David Brownell7ad68e22008-11-11 17:39:02 -08003778 /* some attributes are type-specific */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003779 if (attr == &dev_attr_requested_microamps.attr)
3780 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003781
David Brownell7ad68e22008-11-11 17:39:02 -08003782 /* constraints need specific supporting methods */
Takashi Iwai39f802d2015-01-30 20:29:31 +01003783 if (attr == &dev_attr_min_microvolts.attr ||
3784 attr == &dev_attr_max_microvolts.attr)
3785 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003786
Takashi Iwai39f802d2015-01-30 20:29:31 +01003787 if (attr == &dev_attr_min_microamps.attr ||
3788 attr == &dev_attr_max_microamps.attr)
3789 return ops->set_current_limit ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003790
Takashi Iwai39f802d2015-01-30 20:29:31 +01003791 if (attr == &dev_attr_suspend_standby_state.attr ||
3792 attr == &dev_attr_suspend_mem_state.attr ||
3793 attr == &dev_attr_suspend_disk_state.attr)
3794 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003795
Takashi Iwai39f802d2015-01-30 20:29:31 +01003796 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3797 attr == &dev_attr_suspend_mem_microvolts.attr ||
3798 attr == &dev_attr_suspend_disk_microvolts.attr)
3799 return ops->set_suspend_voltage ? mode : 0;
David Brownell7ad68e22008-11-11 17:39:02 -08003800
Takashi Iwai39f802d2015-01-30 20:29:31 +01003801 if (attr == &dev_attr_suspend_standby_mode.attr ||
3802 attr == &dev_attr_suspend_mem_mode.attr ||
3803 attr == &dev_attr_suspend_disk_mode.attr)
3804 return ops->set_suspend_mode ? mode : 0;
3805
3806 return mode;
David Brownell7ad68e22008-11-11 17:39:02 -08003807}
3808
Takashi Iwai39f802d2015-01-30 20:29:31 +01003809static const struct attribute_group regulator_dev_group = {
3810 .attrs = regulator_dev_attrs,
3811 .is_visible = regulator_attr_is_visible,
3812};
3813
3814static const struct attribute_group *regulator_dev_groups[] = {
3815 &regulator_dev_group,
3816 NULL
3817};
3818
3819static void regulator_dev_release(struct device *dev)
3820{
3821 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown29f5f482015-08-07 20:01:32 +01003822
3823 kfree(rdev->constraints);
3824 of_node_put(rdev->dev.of_node);
Takashi Iwai39f802d2015-01-30 20:29:31 +01003825 kfree(rdev);
3826}
3827
3828static struct class regulator_class = {
3829 .name = "regulator",
3830 .dev_release = regulator_dev_release,
3831 .dev_groups = regulator_dev_groups,
3832};
3833
Mark Brown1130e5b2010-12-21 23:49:31 +00003834static void rdev_init_debugfs(struct regulator_dev *rdev)
3835{
Guenter Roecka9eaa812014-10-17 08:17:03 -07003836 struct device *parent = rdev->dev.parent;
3837 const char *rname = rdev_get_name(rdev);
3838 char name[NAME_MAX];
3839
3840 /* Avoid duplicate debugfs directory names */
3841 if (parent && rname == rdev->desc->name) {
3842 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3843 rname);
3844 rname = name;
3845 }
3846
3847 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003848 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003849 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003850 return;
3851 }
3852
3853 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3854 &rdev->use_count);
3855 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3856 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003857 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3858 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003859}
3860
Liam Girdwood414c70c2008-04-30 15:59:04 +01003861/**
3862 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003863 * @regulator_desc: regulator to register
Krzysztof Kozlowskif47531b2015-01-12 09:01:39 +01003864 * @cfg: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003865 *
3866 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003867 * Returns a valid pointer to struct regulator_dev on success
3868 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003869 */
Mark Brown65f26842012-04-03 20:46:53 +01003870struct regulator_dev *
3871regulator_register(const struct regulator_desc *regulator_desc,
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003872 const struct regulator_config *cfg)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003873{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003874 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003875 const struct regulator_init_data *init_data;
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003876 struct regulator_config *config = NULL;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303877 static atomic_t regulator_no = ATOMIC_INIT(-1);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003878 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003879 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003880 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003881
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003882 if (regulator_desc == NULL || cfg == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003883 return ERR_PTR(-EINVAL);
3884
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003885 dev = cfg->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003886 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003887
Liam Girdwood414c70c2008-04-30 15:59:04 +01003888 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3889 return ERR_PTR(-EINVAL);
3890
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003891 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3892 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003893 return ERR_PTR(-EINVAL);
3894
Mark Brown476c2d82010-12-10 17:28:07 +00003895 /* Only one of each should be implemented */
3896 WARN_ON(regulator_desc->ops->get_voltage &&
3897 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003898 WARN_ON(regulator_desc->ops->set_voltage &&
3899 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003900
3901 /* If we're using selectors we must implement list_voltage. */
3902 if (regulator_desc->ops->get_voltage_sel &&
3903 !regulator_desc->ops->list_voltage) {
3904 return ERR_PTR(-EINVAL);
3905 }
Mark Browne8eef822010-12-12 14:36:17 +00003906 if (regulator_desc->ops->set_voltage_sel &&
3907 !regulator_desc->ops->list_voltage) {
3908 return ERR_PTR(-EINVAL);
3909 }
Mark Brown476c2d82010-12-10 17:28:07 +00003910
Liam Girdwood414c70c2008-04-30 15:59:04 +01003911 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3912 if (rdev == NULL)
3913 return ERR_PTR(-ENOMEM);
3914
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01003915 /*
3916 * Duplicate the config so the driver could override it after
3917 * parsing init data.
3918 */
3919 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3920 if (config == NULL) {
3921 kfree(rdev);
3922 return ERR_PTR(-ENOMEM);
3923 }
3924
Krzysztof Kozlowskibfa21a02015-01-05 12:48:42 +01003925 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
Mark Browna0c7b162014-09-09 23:13:57 +01003926 &rdev->dev.of_node);
3927 if (!init_data) {
3928 init_data = config->init_data;
3929 rdev->dev.of_node = of_node_get(config->of_node);
3930 }
3931
Liam Girdwood414c70c2008-04-30 15:59:04 +01003932 mutex_lock(&regulator_list_mutex);
3933
3934 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003935 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003936 rdev->owner = regulator_desc->owner;
3937 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003938 if (config->regmap)
3939 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303940 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003941 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303942 else if (dev->parent)
3943 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003944 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003945 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003946 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003947 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003948
Liam Girdwooda5766f12008-10-10 13:22:20 +01003949 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003950 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003951 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003952 if (ret < 0)
3953 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003954 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003955
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01003956 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3957 gpio_is_valid(config->ena_gpio)) {
3958 ret = regulator_ena_gpio_request(rdev, config);
3959 if (ret != 0) {
3960 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3961 config->ena_gpio, ret);
Krzysztof Adamski32165232016-02-24 11:52:50 +01003962 goto clean;
Krzysztof Adamskidaad1342016-02-22 09:24:00 +01003963 }
3964 }
3965
Liam Girdwooda5766f12008-10-10 13:22:20 +01003966 /* register with sysfs */
3967 rdev->dev.class = &regulator_class;
3968 rdev->dev.parent = dev;
Aniroop Mathur72dca062014-12-28 22:08:38 +05303969 dev_set_name(&rdev->dev, "regulator.%lu",
Aniroop Mathur39138812014-12-29 22:36:48 +05303970 (unsigned long) atomic_inc_return(&regulator_no));
Liam Girdwooda5766f12008-10-10 13:22:20 +01003971 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003972 if (ret != 0) {
3973 put_device(&rdev->dev);
Krzysztof Adamski32165232016-02-24 11:52:50 +01003974 goto wash;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003975 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003976
3977 dev_set_drvdata(&rdev->dev, rdev);
3978
Mike Rapoport74f544c2008-11-25 14:53:53 +02003979 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003980 if (init_data)
3981 constraints = &init_data->constraints;
3982
3983 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003984 if (ret < 0)
3985 goto scrub;
3986
Mark Brown9a8f5e02011-11-29 18:11:19 +00003987 if (init_data && init_data->supply_regulator)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003988 rdev->supply_name = init_data->supply_regulator;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303989 else if (regulator_desc->supply_name)
Bjorn Andersson6261b062015-03-24 18:56:05 -07003990 rdev->supply_name = regulator_desc->supply_name;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303991
Liam Girdwooda5766f12008-10-10 13:22:20 +01003992 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003993 if (init_data) {
3994 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3995 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003996 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003997 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003998 if (ret < 0) {
3999 dev_err(dev, "Failed to set supply %s\n",
4000 init_data->consumer_supplies[i].supply);
4001 goto unset_supplies;
4002 }
Mark Brown23c2f042011-02-24 17:39:09 +00004003 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01004004 }
4005
Mark Brown1130e5b2010-12-21 23:49:31 +00004006 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01004007out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01004008 mutex_unlock(&regulator_list_mutex);
Krzysztof Kozlowski1b3de222015-01-05 12:48:41 +01004009 kfree(config);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004010 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08004011
Jani Nikulad4033b52010-04-29 10:55:11 +03004012unset_supplies:
4013 unset_regulator_supplies(rdev);
4014
David Brownell4fca9542008-11-11 17:38:53 -08004015scrub:
Kim, Milof19b00d2013-02-18 06:50:39 +00004016 regulator_ena_gpio_free(rdev);
David Brownell4fca9542008-11-11 17:38:53 -08004017 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06004018 /* device core frees rdev */
4019 rdev = ERR_PTR(ret);
4020 goto out;
4021
Krzysztof Adamski32165232016-02-24 11:52:50 +01004022wash:
4023 regulator_ena_gpio_free(rdev);
David Brownell4fca9542008-11-11 17:38:53 -08004024clean:
4025 kfree(rdev);
4026 rdev = ERR_PTR(ret);
4027 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004028}
4029EXPORT_SYMBOL_GPL(regulator_register);
4030
4031/**
4032 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00004033 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01004034 *
4035 * Called by regulator drivers to unregister a regulator.
4036 */
4037void regulator_unregister(struct regulator_dev *rdev)
4038{
4039 if (rdev == NULL)
4040 return;
4041
Mark Brown891636e2013-07-08 09:14:45 +01004042 if (rdev->supply) {
4043 while (rdev->use_count--)
4044 regulator_disable(rdev->supply);
Mark Browne032b372012-03-28 21:17:55 +01004045 regulator_put(rdev->supply);
Mark Brown891636e2013-07-08 09:14:45 +01004046 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01004047 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00004048 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07004049 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01004050 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02004051 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004052 list_del(&rdev->list);
Mark Brown7cd71c32015-08-07 13:00:35 +01004053 mutex_unlock(&regulator_list_mutex);
Kim, Milof19b00d2013-02-18 06:50:39 +00004054 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01004055 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004056}
4057EXPORT_SYMBOL_GPL(regulator_unregister);
4058
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004059static int _regulator_suspend_prepare(struct device *dev, void *data)
4060{
4061 struct regulator_dev *rdev = dev_to_rdev(dev);
4062 const suspend_state_t *state = data;
4063 int ret;
4064
4065 mutex_lock(&rdev->mutex);
4066 ret = suspend_prepare(rdev, *state);
4067 mutex_unlock(&rdev->mutex);
4068
4069 return ret;
4070}
4071
Liam Girdwood414c70c2008-04-30 15:59:04 +01004072/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00004073 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01004074 * @state: system suspend state
4075 *
4076 * Configure each regulator with it's suspend operating parameters for state.
4077 * This will usually be called by machine suspend code prior to supending.
4078 */
4079int regulator_suspend_prepare(suspend_state_t state)
4080{
Liam Girdwood414c70c2008-04-30 15:59:04 +01004081 /* ON is handled by regulator active state */
4082 if (state == PM_SUSPEND_ON)
4083 return -EINVAL;
4084
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004085 return class_for_each_device(&regulator_class, NULL, &state,
4086 _regulator_suspend_prepare);
Liam Girdwood414c70c2008-04-30 15:59:04 +01004087}
4088EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
4089
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004090static int _regulator_suspend_finish(struct device *dev, void *data)
4091{
4092 struct regulator_dev *rdev = dev_to_rdev(dev);
4093 int ret;
4094
4095 mutex_lock(&rdev->mutex);
4096 if (rdev->use_count > 0 || rdev->constraints->always_on) {
4097 if (!_regulator_is_enabled(rdev)) {
4098 ret = _regulator_do_enable(rdev);
4099 if (ret)
4100 dev_err(dev,
4101 "Failed to resume regulator %d\n",
4102 ret);
4103 }
4104 } else {
4105 if (!have_full_constraints())
4106 goto unlock;
4107 if (!_regulator_is_enabled(rdev))
4108 goto unlock;
4109
4110 ret = _regulator_do_disable(rdev);
4111 if (ret)
4112 dev_err(dev, "Failed to suspend regulator %d\n", ret);
4113 }
4114unlock:
4115 mutex_unlock(&rdev->mutex);
4116
4117 /* Keep processing regulators in spite of any errors */
4118 return 0;
4119}
4120
Liam Girdwood414c70c2008-04-30 15:59:04 +01004121/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004122 * regulator_suspend_finish - resume regulators from system wide suspend
4123 *
4124 * Turn on regulators that might be turned off by regulator_suspend_prepare
4125 * and that should be turned on according to the regulators properties.
4126 */
4127int regulator_suspend_finish(void)
4128{
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004129 return class_for_each_device(&regulator_class, NULL, NULL,
4130 _regulator_suspend_finish);
MyungJoo Ham7a32b582011-03-11 10:13:59 +09004131}
4132EXPORT_SYMBOL_GPL(regulator_suspend_finish);
4133
4134/**
Mark Brownca725562009-03-16 19:36:34 +00004135 * regulator_has_full_constraints - the system has fully specified constraints
4136 *
4137 * Calling this function will cause the regulator API to disable all
4138 * regulators which have a zero use count and don't have an always_on
4139 * constraint in a late_initcall.
4140 *
4141 * The intention is that this will become the default behaviour in a
4142 * future kernel release so users are encouraged to use this facility
4143 * now.
4144 */
4145void regulator_has_full_constraints(void)
4146{
4147 has_full_constraints = 1;
4148}
4149EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4150
4151/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01004152 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00004153 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004154 *
4155 * Get rdev regulator driver private data. This call can be used in the
4156 * regulator driver context.
4157 */
4158void *rdev_get_drvdata(struct regulator_dev *rdev)
4159{
4160 return rdev->reg_data;
4161}
4162EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4163
4164/**
4165 * regulator_get_drvdata - get regulator driver data
4166 * @regulator: regulator
4167 *
4168 * Get regulator driver private data. This call can be used in the consumer
4169 * driver context when non API regulator specific functions need to be called.
4170 */
4171void *regulator_get_drvdata(struct regulator *regulator)
4172{
4173 return regulator->rdev->reg_data;
4174}
4175EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4176
4177/**
4178 * regulator_set_drvdata - set regulator driver data
4179 * @regulator: regulator
4180 * @data: data
4181 */
4182void regulator_set_drvdata(struct regulator *regulator, void *data)
4183{
4184 regulator->rdev->reg_data = data;
4185}
4186EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4187
4188/**
4189 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00004190 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01004191 */
4192int rdev_get_id(struct regulator_dev *rdev)
4193{
4194 return rdev->desc->id;
4195}
4196EXPORT_SYMBOL_GPL(rdev_get_id);
4197
Liam Girdwooda5766f12008-10-10 13:22:20 +01004198struct device *rdev_get_dev(struct regulator_dev *rdev)
4199{
4200 return &rdev->dev;
4201}
4202EXPORT_SYMBOL_GPL(rdev_get_dev);
4203
4204void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4205{
4206 return reg_init_data->driver_data;
4207}
4208EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4209
Mark Brownba55a972011-08-23 17:39:10 +01004210#ifdef CONFIG_DEBUG_FS
4211static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4212 size_t count, loff_t *ppos)
4213{
4214 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4215 ssize_t len, ret = 0;
4216 struct regulator_map *map;
4217
4218 if (!buf)
4219 return -ENOMEM;
4220
4221 list_for_each_entry(map, &regulator_map_list, list) {
4222 len = snprintf(buf + ret, PAGE_SIZE - ret,
4223 "%s -> %s.%s\n",
4224 rdev_get_name(map->regulator), map->dev_name,
4225 map->supply);
4226 if (len >= 0)
4227 ret += len;
4228 if (ret > PAGE_SIZE) {
4229 ret = PAGE_SIZE;
4230 break;
4231 }
4232 }
4233
4234 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4235
4236 kfree(buf);
4237
4238 return ret;
4239}
Stephen Boyd24751432012-02-20 22:50:42 -08004240#endif
Mark Brownba55a972011-08-23 17:39:10 +01004241
4242static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004243#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004244 .read = supply_map_read_file,
4245 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004246#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004247};
Mark Brownba55a972011-08-23 17:39:10 +01004248
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004249#ifdef CONFIG_DEBUG_FS
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004250struct summary_data {
4251 struct seq_file *s;
4252 struct regulator_dev *parent;
4253 int level;
4254};
4255
4256static void regulator_summary_show_subtree(struct seq_file *s,
4257 struct regulator_dev *rdev,
4258 int level);
4259
4260static int regulator_summary_show_children(struct device *dev, void *data)
4261{
4262 struct regulator_dev *rdev = dev_to_rdev(dev);
4263 struct summary_data *summary_data = data;
4264
4265 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4266 regulator_summary_show_subtree(summary_data->s, rdev,
4267 summary_data->level + 1);
4268
4269 return 0;
4270}
4271
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004272static void regulator_summary_show_subtree(struct seq_file *s,
4273 struct regulator_dev *rdev,
4274 int level)
4275{
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004276 struct regulation_constraints *c;
4277 struct regulator *consumer;
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004278 struct summary_data summary_data;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004279
4280 if (!rdev)
4281 return;
4282
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004283 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4284 level * 3 + 1, "",
4285 30 - level * 3, rdev_get_name(rdev),
4286 rdev->use_count, rdev->open_count, rdev->bypass_count);
4287
Heiko Stübner23296092015-04-10 13:48:41 +02004288 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4289 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004290
4291 c = rdev->constraints;
4292 if (c) {
4293 switch (rdev->desc->type) {
4294 case REGULATOR_VOLTAGE:
4295 seq_printf(s, "%5dmV %5dmV ",
4296 c->min_uV / 1000, c->max_uV / 1000);
4297 break;
4298 case REGULATOR_CURRENT:
4299 seq_printf(s, "%5dmA %5dmA ",
4300 c->min_uA / 1000, c->max_uA / 1000);
4301 break;
4302 }
4303 }
4304
4305 seq_puts(s, "\n");
4306
4307 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4308 if (consumer->dev->class == &regulator_class)
4309 continue;
4310
4311 seq_printf(s, "%*s%-*s ",
4312 (level + 1) * 3 + 1, "",
4313 30 - (level + 1) * 3, dev_name(consumer->dev));
4314
4315 switch (rdev->desc->type) {
4316 case REGULATOR_VOLTAGE:
Heiko Stübner23296092015-04-10 13:48:41 +02004317 seq_printf(s, "%37dmV %5dmV",
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004318 consumer->min_uV / 1000,
4319 consumer->max_uV / 1000);
4320 break;
4321 case REGULATOR_CURRENT:
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004322 break;
4323 }
4324
4325 seq_puts(s, "\n");
4326 }
4327
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004328 summary_data.s = s;
4329 summary_data.level = level;
4330 summary_data.parent = rdev;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004331
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004332 class_for_each_device(&regulator_class, NULL, &summary_data,
4333 regulator_summary_show_children);
4334}
4335
4336static int regulator_summary_show_roots(struct device *dev, void *data)
4337{
4338 struct regulator_dev *rdev = dev_to_rdev(dev);
4339 struct seq_file *s = data;
4340
4341 if (!rdev->supply)
4342 regulator_summary_show_subtree(s, rdev, 0);
4343
4344 return 0;
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004345}
4346
4347static int regulator_summary_show(struct seq_file *s, void *data)
4348{
Heiko Stübner23296092015-04-10 13:48:41 +02004349 seq_puts(s, " regulator use open bypass voltage current min max\n");
4350 seq_puts(s, "-------------------------------------------------------------------------------\n");
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004351
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004352 class_for_each_device(&regulator_class, NULL, s,
4353 regulator_summary_show_roots);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004354
4355 return 0;
4356}
4357
4358static int regulator_summary_open(struct inode *inode, struct file *file)
4359{
4360 return single_open(file, regulator_summary_show, inode->i_private);
4361}
4362#endif
4363
4364static const struct file_operations regulator_summary_fops = {
4365#ifdef CONFIG_DEBUG_FS
4366 .open = regulator_summary_open,
4367 .read = seq_read,
4368 .llseek = seq_lseek,
4369 .release = single_release,
4370#endif
4371};
4372
Liam Girdwood414c70c2008-04-30 15:59:04 +01004373static int __init regulator_init(void)
4374{
Mark Brown34abbd62010-02-12 10:18:08 +00004375 int ret;
4376
Mark Brown34abbd62010-02-12 10:18:08 +00004377 ret = class_register(&regulator_class);
4378
Mark Brown1130e5b2010-12-21 23:49:31 +00004379 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004380 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004381 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004382
Mark Brownf4d562c2012-02-20 21:01:04 +00004383 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4384 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004385
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004386 debugfs_create_file("regulator_summary", 0444, debugfs_root,
Tomeu Vizoso85f3b432015-09-21 16:02:47 +02004387 NULL, &regulator_summary_fops);
Heiko Stübner7c225ec2015-04-07 16:16:39 +02004388
Mark Brown34abbd62010-02-12 10:18:08 +00004389 regulator_dummy_init();
4390
4391 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004392}
4393
4394/* init early to allow our consumers to complete system booting */
4395core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004396
Mark Brown609ca5f2015-08-10 19:43:47 +01004397static int __init regulator_late_cleanup(struct device *dev, void *data)
Mark Brownca725562009-03-16 19:36:34 +00004398{
Mark Brown609ca5f2015-08-10 19:43:47 +01004399 struct regulator_dev *rdev = dev_to_rdev(dev);
4400 const struct regulator_ops *ops = rdev->desc->ops;
4401 struct regulation_constraints *c = rdev->constraints;
Mark Brownca725562009-03-16 19:36:34 +00004402 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004403
Mark Brown609ca5f2015-08-10 19:43:47 +01004404 if (c && c->always_on)
4405 return 0;
4406
4407 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4408 return 0;
4409
4410 mutex_lock(&rdev->mutex);
4411
4412 if (rdev->use_count)
4413 goto unlock;
4414
4415 /* If we can't read the status assume it's on. */
4416 if (ops->is_enabled)
4417 enabled = ops->is_enabled(rdev);
4418 else
4419 enabled = 1;
4420
4421 if (!enabled)
4422 goto unlock;
4423
4424 if (have_full_constraints()) {
4425 /* We log since this may kill the system if it goes
4426 * wrong. */
4427 rdev_info(rdev, "disabling\n");
4428 ret = _regulator_do_disable(rdev);
4429 if (ret != 0)
4430 rdev_err(rdev, "couldn't disable: %d\n", ret);
4431 } else {
4432 /* The intention is that in future we will
4433 * assume that full constraints are provided
4434 * so warn even if we aren't going to do
4435 * anything here.
4436 */
4437 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4438 }
4439
4440unlock:
4441 mutex_unlock(&rdev->mutex);
4442
4443 return 0;
4444}
4445
4446static int __init regulator_init_complete(void)
4447{
Mark Brown86f5fcf2012-07-06 18:19:13 +01004448 /*
4449 * Since DT doesn't provide an idiomatic mechanism for
4450 * enabling full constraints and since it's much more natural
4451 * with DT to provide them just assume that a DT enabled
4452 * system has full constraints.
4453 */
4454 if (of_have_populated_dt())
4455 has_full_constraints = true;
4456
Mark Brownca725562009-03-16 19:36:34 +00004457 /* If we have a full configuration then disable any regulators
Mark Browne9535832014-06-01 19:15:16 +01004458 * we have permission to change the status for and which are
4459 * not in use or always_on. This is effectively the default
4460 * for DT and ACPI as they have full constraints.
Mark Brownca725562009-03-16 19:36:34 +00004461 */
Mark Brown609ca5f2015-08-10 19:43:47 +01004462 class_for_each_device(&regulator_class, NULL, NULL,
4463 regulator_late_cleanup);
Mark Brownca725562009-03-16 19:36:34 +00004464
4465 return 0;
4466}
Saravana Kannanfd482a32014-04-23 18:10:50 -05004467late_initcall_sync(regulator_init_complete);