consumer.h 17.3 KB
Newer Older
1 2 3 4 5
/*
 * consumer.h -- SoC Regulator consumer support.
 *
 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
 *
6
 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Regulator Consumer Interface.
 *
 * A Power Management Regulator framework for SoC based devices.
 * Features:-
 *   o Voltage and current level control.
 *   o Operating mode control.
 *   o Regulator status.
 *   o sysfs entries for showing client devices and status
 *
 * EXPERIMENTAL FEATURES:
 *   Dynamic Regulator operating Mode Switching (DRMS) - allows regulators
 *   to use most efficient operating mode depending upon voltage and load and
 *   is transparent to client drivers.
 *
 *   e.g. Devices x,y,z share regulator r. Device x and y draw 20mA each during
 *   IO and 1mA at idle. Device z draws 100mA when under load and 5mA when
 *   idling. Regulator r has > 90% efficiency in NORMAL mode at loads > 100mA
 *   but this drops rapidly to 60% when below 100mA. Regulator r has > 90%
 *   efficiency in IDLE mode at loads < 10mA. Thus regulator r will operate
 *   in normal mode for loads > 10mA and in IDLE mode for load <= 10mA.
 *
 */

#ifndef __LINUX_REGULATOR_CONSUMER_H_
#define __LINUX_REGULATOR_CONSUMER_H_

38 39
#include <linux/err.h>

40 41
struct device;
struct notifier_block;
42
struct regmap;
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/*
 * Regulator operating modes.
 *
 * Regulators can run in a variety of different operating modes depending on
 * output load. This allows further system power savings by selecting the
 * best (and most efficient) regulator mode for a desired load.
 *
 * Most drivers will only care about NORMAL. The modes below are generic and
 * will probably not match the naming convention of your regulator data sheet
 * but should match the use cases in the datasheet.
 *
 * In order of power efficiency (least efficient at top).
 *
 *  Mode       Description
 *  FAST       Regulator can handle fast changes in it's load.
 *             e.g. useful in CPU voltage & frequency scaling where
 *             load can quickly increase with CPU frequency increases.
 *
 *  NORMAL     Normal regulator power supply mode. Most drivers will
 *             use this mode.
 *
 *  IDLE       Regulator runs in a more efficient mode for light
 *             loads. Can be used for devices that have a low power
 *             requirement during periods of inactivity. This mode
 *             may be more noisy than NORMAL and may not be able
 *             to handle fast load switching.
 *
 *  STANDBY    Regulator runs in the most efficient mode for very
 *             light loads. Can be used by devices when they are
 *             in a sleep/standby state. This mode is likely to be
 *             the most noisy and may not be able to handle fast load
 *             switching.
 *
 * NOTE: Most regulators will only support a subset of these modes. Some
 * will only just support NORMAL.
 *
 * These modes can be OR'ed together to make up a mask of valid register modes.
 */

#define REGULATOR_MODE_FAST			0x1
#define REGULATOR_MODE_NORMAL			0x2
#define REGULATOR_MODE_IDLE			0x4
#define REGULATOR_MODE_STANDBY			0x8

/*
 * Regulator notifier events.
 *
 * UNDER_VOLTAGE  Regulator output is under voltage.
 * OVER_CURRENT   Regulator output current is too high.
 * REGULATION_OUT Regulator output is out of regulation.
 * FAIL           Regulator output has failed.
 * OVER_TEMP      Regulator over temp.
96
 * FORCE_DISABLE  Regulator forcibly shut down by software.
97
 * VOLTAGE_CHANGE Regulator voltage changed.
98
 *                Data passed is old voltage cast to (void *).
99
 * DISABLE        Regulator was disabled.
100 101 102 103
 * PRE_VOLTAGE_CHANGE   Regulator is about to have voltage changed.
 *                      Data passed is "struct pre_voltage_change_data"
 * ABORT_VOLTAGE_CHANGE Regulator voltage change failed for some reason.
 *                      Data passed is old voltage cast to (void *).
104 105
 * PRE_DISABLE    Regulator is about to be disabled
 * ABORT_DISABLE  Regulator disable failed for some reason
106 107 108 109 110 111 112 113 114 115
 *
 * NOTE: These events can be OR'ed together when passed into handler.
 */

#define REGULATOR_EVENT_UNDER_VOLTAGE		0x01
#define REGULATOR_EVENT_OVER_CURRENT		0x02
#define REGULATOR_EVENT_REGULATION_OUT		0x04
#define REGULATOR_EVENT_FAIL			0x08
#define REGULATOR_EVENT_OVER_TEMP		0x10
#define REGULATOR_EVENT_FORCE_DISABLE		0x20
116
#define REGULATOR_EVENT_VOLTAGE_CHANGE		0x40
117
#define REGULATOR_EVENT_DISABLE			0x80
118 119
#define REGULATOR_EVENT_PRE_VOLTAGE_CHANGE	0x100
#define REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE	0x200
120 121
#define REGULATOR_EVENT_PRE_DISABLE		0x400
#define REGULATOR_EVENT_ABORT_DISABLE		0x800
122 123 124 125 126 127 128 129 130 131 132 133 134

/**
 * struct pre_voltage_change_data - Data sent with PRE_VOLTAGE_CHANGE event
 *
 * @old_uV: Current voltage before change.
 * @min_uV: Min voltage we'll change to.
 * @max_uV: Max voltage we'll change to.
 */
struct pre_voltage_change_data {
	unsigned long old_uV;
	unsigned long min_uV;
	unsigned long max_uV;
};
135 136 137 138 139 140

struct regulator;

/**
 * struct regulator_bulk_data - Data used for bulk regulator operations.
 *
141 142
 * @supply:   The name of the supply.  Initialised by the user before
 *            using the bulk regulator APIs.
143 144
 * @optional: The supply should be considered optional. Initialised by the user
 *            before using the bulk regulator APIs.
145 146
 * @consumer: The regulator consumer for the supply.  This will be managed
 *            by the bulk API.
147 148 149 150 151 152 153
 *
 * The regulator APIs provide a series of regulator_bulk_() API calls as
 * a convenience to consumers which require multiple supplies.  This
 * structure is used to manage data for these calls.
 */
struct regulator_bulk_data {
	const char *supply;
154
	bool optional;
155
	struct regulator *consumer;
156

157
	/* private: Internal use */
158
	int ret;
159 160 161 162 163 164 165
};

#if defined(CONFIG_REGULATOR)

/* regulator get and put */
struct regulator *__must_check regulator_get(struct device *dev,
					     const char *id);
166 167
struct regulator *__must_check devm_regulator_get(struct device *dev,
					     const char *id);
168 169
struct regulator *__must_check regulator_get_exclusive(struct device *dev,
						       const char *id);
170 171
struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
							const char *id);
172 173 174 175
struct regulator *__must_check regulator_get_optional(struct device *dev,
						      const char *id);
struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
							   const char *id);
176
void regulator_put(struct regulator *regulator);
177
void devm_regulator_put(struct regulator *regulator);
178

179 180 181 182 183
int regulator_register_supply_alias(struct device *dev, const char *id,
				    struct device *alias_dev,
				    const char *alias_id);
void regulator_unregister_supply_alias(struct device *dev, const char *id);

184 185
int regulator_bulk_register_supply_alias(struct device *dev,
					 const char *const *id,
186
					 struct device *alias_dev,
187 188
					 const char *const *alias_id,
					 int num_id);
189
void regulator_bulk_unregister_supply_alias(struct device *dev,
190
					    const char * const *id, int num_id);
191 192 193 194 195 196 197 198

int devm_regulator_register_supply_alias(struct device *dev, const char *id,
					 struct device *alias_dev,
					 const char *alias_id);
void devm_regulator_unregister_supply_alias(struct device *dev,
					    const char *id);

int devm_regulator_bulk_register_supply_alias(struct device *dev,
199
					      const char *const *id,
200
					      struct device *alias_dev,
201
					      const char *const *alias_id,
202 203
					      int num_id);
void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
204
						 const char *const *id,
205 206
						 int num_id);

207
/* regulator output control and status */
208
int __must_check regulator_enable(struct regulator *regulator);
209 210 211
int regulator_disable(struct regulator *regulator);
int regulator_force_disable(struct regulator *regulator);
int regulator_is_enabled(struct regulator *regulator);
212
int regulator_disable_deferred(struct regulator *regulator, int ms);
213

214 215 216 217 218 219
int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
				    struct regulator_bulk_data *consumers);
int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
					 struct regulator_bulk_data *consumers);
int __must_check regulator_bulk_enable(int num_consumers,
				       struct regulator_bulk_data *consumers);
220 221
int regulator_bulk_disable(int num_consumers,
			   struct regulator_bulk_data *consumers);
222 223
int regulator_bulk_force_disable(int num_consumers,
			   struct regulator_bulk_data *consumers);
224 225 226
void regulator_bulk_free(int num_consumers,
			 struct regulator_bulk_data *consumers);

227
int __deprecated regulator_can_change_voltage(struct regulator *regulator);
228 229
int regulator_count_voltages(struct regulator *regulator);
int regulator_list_voltage(struct regulator *regulator, unsigned selector);
230 231
int regulator_is_supported_voltage(struct regulator *regulator,
				   int min_uV, int max_uV);
232
unsigned int regulator_get_linear_step(struct regulator *regulator);
233
int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV);
234 235
int regulator_set_voltage_time(struct regulator *regulator,
			       int old_uV, int new_uV);
236
int regulator_get_voltage(struct regulator *regulator);
237
int regulator_sync_voltage(struct regulator *regulator);
238 239 240 241 242 243
int regulator_set_current_limit(struct regulator *regulator,
			       int min_uA, int max_uA);
int regulator_get_current_limit(struct regulator *regulator);

int regulator_set_mode(struct regulator *regulator, unsigned int mode);
unsigned int regulator_get_mode(struct regulator *regulator);
244
int regulator_set_load(struct regulator *regulator, int load_uA);
245

246 247
int regulator_allow_bypass(struct regulator *regulator, bool allow);

248 249 250 251 252 253 254
struct regmap *regulator_get_regmap(struct regulator *regulator);
int regulator_get_hardware_vsel_register(struct regulator *regulator,
					 unsigned *vsel_reg,
					 unsigned *vsel_mask);
int regulator_list_hardware_vsel(struct regulator *regulator,
				 unsigned selector);

255 256 257
/* regulator notifier block */
int regulator_register_notifier(struct regulator *regulator,
			      struct notifier_block *nb);
258 259
int devm_regulator_register_notifier(struct regulator *regulator,
				     struct notifier_block *nb);
260 261
int regulator_unregister_notifier(struct regulator *regulator,
				struct notifier_block *nb);
262 263
void devm_regulator_unregister_notifier(struct regulator *regulator,
					struct notifier_block *nb);
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

/* driver data - core doesn't touch */
void *regulator_get_drvdata(struct regulator *regulator);
void regulator_set_drvdata(struct regulator *regulator, void *data);

#else

/*
 * Make sure client drivers will still build on systems with no software
 * controllable voltage or current regulators.
 */
static inline struct regulator *__must_check regulator_get(struct device *dev,
	const char *id)
{
	/* Nothing except the stubbed out regulator API should be
	 * looking at the value except to check if it is an error
280 281 282 283 284
	 * value. Drivers are free to handle NULL specifically by
	 * skipping all regulator API calls, but they don't have to.
	 * Drivers which don't, should make sure they properly handle
	 * corner cases of the API, such as regulator_get_voltage()
	 * returning 0.
285
	 */
286
	return NULL;
287
}
288 289 290 291 292 293 294

static inline struct regulator *__must_check
devm_regulator_get(struct device *dev, const char *id)
{
	return NULL;
}

295 296 297
static inline struct regulator *__must_check
regulator_get_exclusive(struct device *dev, const char *id)
{
298
	return ERR_PTR(-ENODEV);
299 300
}

301 302 303
static inline struct regulator *__must_check
regulator_get_optional(struct device *dev, const char *id)
{
304
	return ERR_PTR(-ENODEV);
305 306
}

307

308
static inline struct regulator *__must_check
309
devm_regulator_get_optional(struct device *dev, const char *id)
310
{
311
	return ERR_PTR(-ENODEV);
312 313
}

314 315 316 317
static inline void regulator_put(struct regulator *regulator)
{
}

318 319 320 321
static inline void devm_regulator_put(struct regulator *regulator)
{
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335
static inline int regulator_register_supply_alias(struct device *dev,
						  const char *id,
						  struct device *alias_dev,
						  const char *alias_id)
{
	return 0;
}

static inline void regulator_unregister_supply_alias(struct device *dev,
						    const char *id)
{
}

static inline int regulator_bulk_register_supply_alias(struct device *dev,
336 337 338 339
						const char *const *id,
						struct device *alias_dev,
						const char * const *alias_id,
						int num_id)
340 341 342 343 344
{
	return 0;
}

static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
345 346
						const char * const *id,
						int num_id)
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
{
}

static inline int devm_regulator_register_supply_alias(struct device *dev,
						       const char *id,
						       struct device *alias_dev,
						       const char *alias_id)
{
	return 0;
}

static inline void devm_regulator_unregister_supply_alias(struct device *dev,
							  const char *id)
{
}

363 364 365 366 367
static inline int devm_regulator_bulk_register_supply_alias(struct device *dev,
						const char *const *id,
						struct device *alias_dev,
						const char *const *alias_id,
						int num_id)
368 369 370 371 372
{
	return 0;
}

static inline void devm_regulator_bulk_unregister_supply_alias(
373
	struct device *dev, const char *const *id, int num_id)
374 375 376
{
}

377 378 379 380 381 382 383 384 385 386
static inline int regulator_enable(struct regulator *regulator)
{
	return 0;
}

static inline int regulator_disable(struct regulator *regulator)
{
	return 0;
}

387 388 389 390 391
static inline int regulator_force_disable(struct regulator *regulator)
{
	return 0;
}

392 393 394 395 396 397
static inline int regulator_disable_deferred(struct regulator *regulator,
					     int ms)
{
	return 0;
}

398 399 400 401 402 403 404 405 406 407 408 409
static inline int regulator_is_enabled(struct regulator *regulator)
{
	return 1;
}

static inline int regulator_bulk_get(struct device *dev,
				     int num_consumers,
				     struct regulator_bulk_data *consumers)
{
	return 0;
}

410 411 412 413 414 415
static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
					  struct regulator_bulk_data *consumers)
{
	return 0;
}

416 417 418 419 420 421 422 423 424 425 426 427
static inline int regulator_bulk_enable(int num_consumers,
					struct regulator_bulk_data *consumers)
{
	return 0;
}

static inline int regulator_bulk_disable(int num_consumers,
					 struct regulator_bulk_data *consumers)
{
	return 0;
}

428 429 430 431 432 433
static inline int regulator_bulk_force_disable(int num_consumers,
					struct regulator_bulk_data *consumers)
{
	return 0;
}

434 435 436 437 438
static inline void regulator_bulk_free(int num_consumers,
				       struct regulator_bulk_data *consumers)
{
}

439
static inline int __deprecated regulator_can_change_voltage(struct regulator *regulator)
440 441 442
{
	return 0;
}
443 444 445 446 447 448 449

static inline int regulator_set_voltage(struct regulator *regulator,
					int min_uV, int max_uV)
{
	return 0;
}

450 451 452 453 454 455
static inline int regulator_set_voltage_time(struct regulator *regulator,
					     int old_uV, int new_uV)
{
	return 0;
}

456 457
static inline int regulator_get_voltage(struct regulator *regulator)
{
458
	return -EINVAL;
459 460
}

461 462 463 464 465 466
static inline int regulator_is_supported_voltage(struct regulator *regulator,
				   int min_uV, int max_uV)
{
	return 0;
}

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
static inline int regulator_set_current_limit(struct regulator *regulator,
					     int min_uA, int max_uA)
{
	return 0;
}

static inline int regulator_get_current_limit(struct regulator *regulator)
{
	return 0;
}

static inline int regulator_set_mode(struct regulator *regulator,
	unsigned int mode)
{
	return 0;
}

static inline unsigned int regulator_get_mode(struct regulator *regulator)
{
	return REGULATOR_MODE_NORMAL;
}

489
static inline int regulator_set_load(struct regulator *regulator, int load_uA)
490 491 492 493
{
	return REGULATOR_MODE_NORMAL;
}

494 495 496 497 498 499
static inline int regulator_allow_bypass(struct regulator *regulator,
					 bool allow)
{
	return 0;
}

500
static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
501 502 503 504
{
	return ERR_PTR(-EOPNOTSUPP);
}

505 506 507
static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
						       unsigned *vsel_reg,
						       unsigned *vsel_mask)
508 509 510 511
{
	return -EOPNOTSUPP;
}

512 513
static inline int regulator_list_hardware_vsel(struct regulator *regulator,
					       unsigned selector)
514 515 516 517
{
	return -EOPNOTSUPP;
}

518 519 520 521 522 523
static inline int regulator_register_notifier(struct regulator *regulator,
			      struct notifier_block *nb)
{
	return 0;
}

524 525 526 527 528 529
static inline int devm_regulator_register_notifier(struct regulator *regulator,
						   struct notifier_block *nb)
{
	return 0;
}

530 531 532 533 534 535
static inline int regulator_unregister_notifier(struct regulator *regulator,
				struct notifier_block *nb)
{
	return 0;
}

536 537 538 539 540 541
static inline int devm_regulator_unregister_notifier(struct regulator *regulator,
						     struct notifier_block *nb)
{
	return 0;
}

542 543 544 545 546 547 548 549 550 551
static inline void *regulator_get_drvdata(struct regulator *regulator)
{
	return NULL;
}

static inline void regulator_set_drvdata(struct regulator *regulator,
	void *data)
{
}

552 553 554 555
static inline int regulator_count_voltages(struct regulator *regulator)
{
	return 0;
}
556 557 558 559 560 561

static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector)
{
	return -EINVAL;
}

562 563
#endif

564 565 566 567 568 569 570 571 572 573
static inline int regulator_set_voltage_triplet(struct regulator *regulator,
						int min_uV, int target_uV,
						int max_uV)
{
	if (regulator_set_voltage(regulator, target_uV, max_uV) == 0)
		return 0;

	return regulator_set_voltage(regulator, min_uV, max_uV);
}

574 575 576
static inline int regulator_set_voltage_tol(struct regulator *regulator,
					    int new_uV, int tol_uV)
{
577 578 579 580 581
	if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
		return 0;
	else
		return regulator_set_voltage(regulator,
					     new_uV - tol_uV, new_uV + tol_uV);
582 583
}

584 585 586 587 588 589 590 591
static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
						     int target_uV, int tol_uV)
{
	return regulator_is_supported_voltage(regulator,
					      target_uV - tol_uV,
					      target_uV + tol_uV);
}

592
#endif