aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Brown <broonie@linaro.org>2013-09-01 08:50:09 -0400
committerMark Brown <broonie@linaro.org>2013-09-01 08:50:09 -0400
commit724d054490883fed2d7e5fc716dffa7675476740 (patch)
tree5f090f26956bdac7ed91cc05acecffdd876675f5
parent446b4665e323a755d5c5390da324134645a809fb (diff)
parentd295f7670127eb241d81e96e003b380c77c2b254 (diff)
Merge remote-tracking branch 'regulator/topic/helpers' into regulator-next
-rw-r--r--drivers/regulator/Makefile2
-rw-r--r--drivers/regulator/core.c427
-rw-r--r--drivers/regulator/da903x.c45
-rw-r--r--drivers/regulator/helpers.c447
4 files changed, 458 insertions, 463 deletions
diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
index 40cbb1b35c1d..90c31e74ca28 100644
--- a/drivers/regulator/Makefile
+++ b/drivers/regulator/Makefile
@@ -3,7 +3,7 @@
3# 3#
4 4
5 5
6obj-$(CONFIG_REGULATOR) += core.o dummy.o fixed-helper.o 6obj-$(CONFIG_REGULATOR) += core.o dummy.o fixed-helper.o helpers.o
7obj-$(CONFIG_OF) += of_regulator.o 7obj-$(CONFIG_OF) += of_regulator.o
8obj-$(CONFIG_REGULATOR_FIXED_VOLTAGE) += fixed.o 8obj-$(CONFIG_REGULATOR_FIXED_VOLTAGE) += fixed.o
9obj-$(CONFIG_REGULATOR_VIRTUAL_CONSUMER) += virtual.o 9obj-$(CONFIG_REGULATOR_VIRTUAL_CONSUMER) += virtual.o
diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index e8dd361cba28..a560da3a633e 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1904,77 +1904,6 @@ int regulator_disable_deferred(struct regulator *regulator, int ms)
1904} 1904}
1905EXPORT_SYMBOL_GPL(regulator_disable_deferred); 1905EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1906 1906
1907/**
1908 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1909 *
1910 * @rdev: regulator to operate on
1911 *
1912 * Regulators that use regmap for their register I/O can set the
1913 * enable_reg and enable_mask fields in their descriptor and then use
1914 * this as their is_enabled operation, saving some code.
1915 */
1916int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1917{
1918 unsigned int val;
1919 int ret;
1920
1921 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1922 if (ret != 0)
1923 return ret;
1924
1925 if (rdev->desc->enable_is_inverted)
1926 return (val & rdev->desc->enable_mask) == 0;
1927 else
1928 return (val & rdev->desc->enable_mask) != 0;
1929}
1930EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1931
1932/**
1933 * regulator_enable_regmap - standard enable() for regmap users
1934 *
1935 * @rdev: regulator to operate on
1936 *
1937 * Regulators that use regmap for their register I/O can set the
1938 * enable_reg and enable_mask fields in their descriptor and then use
1939 * this as their enable() operation, saving some code.
1940 */
1941int regulator_enable_regmap(struct regulator_dev *rdev)
1942{
1943 unsigned int val;
1944
1945 if (rdev->desc->enable_is_inverted)
1946 val = 0;
1947 else
1948 val = rdev->desc->enable_mask;
1949
1950 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1951 rdev->desc->enable_mask, val);
1952}
1953EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1954
1955/**
1956 * regulator_disable_regmap - standard disable() for regmap users
1957 *
1958 * @rdev: regulator to operate on
1959 *
1960 * Regulators that use regmap for their register I/O can set the
1961 * enable_reg and enable_mask fields in their descriptor and then use
1962 * this as their disable() operation, saving some code.
1963 */
1964int regulator_disable_regmap(struct regulator_dev *rdev)
1965{
1966 unsigned int val;
1967
1968 if (rdev->desc->enable_is_inverted)
1969 val = rdev->desc->enable_mask;
1970 else
1971 val = 0;
1972
1973 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1974 rdev->desc->enable_mask, val);
1975}
1976EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1977
1978static int _regulator_is_enabled(struct regulator_dev *rdev) 1907static int _regulator_is_enabled(struct regulator_dev *rdev)
1979{ 1908{
1980 /* A GPIO control always takes precedence */ 1909 /* A GPIO control always takes precedence */
@@ -2060,92 +1989,6 @@ int regulator_count_voltages(struct regulator *regulator)
2060EXPORT_SYMBOL_GPL(regulator_count_voltages); 1989EXPORT_SYMBOL_GPL(regulator_count_voltages);
2061 1990
2062/** 1991/**
2063 * regulator_list_voltage_linear - List voltages with simple calculation
2064 *
2065 * @rdev: Regulator device
2066 * @selector: Selector to convert into a voltage
2067 *
2068 * Regulators with a simple linear mapping between voltages and
2069 * selectors can set min_uV and uV_step in the regulator descriptor
2070 * and then use this function as their list_voltage() operation,
2071 */
2072int regulator_list_voltage_linear(struct regulator_dev *rdev,
2073 unsigned int selector)
2074{
2075 if (selector >= rdev->desc->n_voltages)
2076 return -EINVAL;
2077 if (selector < rdev->desc->linear_min_sel)
2078 return 0;
2079
2080 selector -= rdev->desc->linear_min_sel;
2081
2082 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2083}
2084EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2085
2086/**
2087 * regulator_list_voltage_linear_range - List voltages for linear ranges
2088 *
2089 * @rdev: Regulator device
2090 * @selector: Selector to convert into a voltage
2091 *
2092 * Regulators with a series of simple linear mappings between voltages
2093 * and selectors can set linear_ranges in the regulator descriptor and
2094 * then use this function as their list_voltage() operation,
2095 */
2096int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
2097 unsigned int selector)
2098{
2099 const struct regulator_linear_range *range;
2100 int i;
2101
2102 if (!rdev->desc->n_linear_ranges) {
2103 BUG_ON(!rdev->desc->n_linear_ranges);
2104 return -EINVAL;
2105 }
2106
2107 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
2108 range = &rdev->desc->linear_ranges[i];
2109
2110 if (!(selector >= range->min_sel &&
2111 selector <= range->max_sel))
2112 continue;
2113
2114 selector -= range->min_sel;
2115
2116 return range->min_uV + (range->uV_step * selector);
2117 }
2118
2119 return -EINVAL;
2120}
2121EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
2122
2123/**
2124 * regulator_list_voltage_table - List voltages with table based mapping
2125 *
2126 * @rdev: Regulator device
2127 * @selector: Selector to convert into a voltage
2128 *
2129 * Regulators with table based mapping between voltages and
2130 * selectors can set volt_table in the regulator descriptor
2131 * and then use this function as their list_voltage() operation.
2132 */
2133int regulator_list_voltage_table(struct regulator_dev *rdev,
2134 unsigned int selector)
2135{
2136 if (!rdev->desc->volt_table) {
2137 BUG_ON(!rdev->desc->volt_table);
2138 return -EINVAL;
2139 }
2140
2141 if (selector >= rdev->desc->n_voltages)
2142 return -EINVAL;
2143
2144 return rdev->desc->volt_table[selector];
2145}
2146EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2147
2148/**
2149 * regulator_list_voltage - enumerate supported voltages 1992 * regulator_list_voltage - enumerate supported voltages
2150 * @regulator: regulator source 1993 * @regulator: regulator source
2151 * @selector: identify voltage to list 1994 * @selector: identify voltage to list
@@ -2239,235 +2082,6 @@ int regulator_is_supported_voltage(struct regulator *regulator,
2239} 2082}
2240EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); 2083EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2241 2084
2242/**
2243 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2244 *
2245 * @rdev: regulator to operate on
2246 *
2247 * Regulators that use regmap for their register I/O can set the
2248 * vsel_reg and vsel_mask fields in their descriptor and then use this
2249 * as their get_voltage_vsel operation, saving some code.
2250 */
2251int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2252{
2253 unsigned int val;
2254 int ret;
2255
2256 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2257 if (ret != 0)
2258 return ret;
2259
2260 val &= rdev->desc->vsel_mask;
2261 val >>= ffs(rdev->desc->vsel_mask) - 1;
2262
2263 return val;
2264}
2265EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2266
2267/**
2268 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2269 *
2270 * @rdev: regulator to operate on
2271 * @sel: Selector to set
2272 *
2273 * Regulators that use regmap for their register I/O can set the
2274 * vsel_reg and vsel_mask fields in their descriptor and then use this
2275 * as their set_voltage_vsel operation, saving some code.
2276 */
2277int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2278{
2279 int ret;
2280
2281 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2282
2283 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2284 rdev->desc->vsel_mask, sel);
2285 if (ret)
2286 return ret;
2287
2288 if (rdev->desc->apply_bit)
2289 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2290 rdev->desc->apply_bit,
2291 rdev->desc->apply_bit);
2292 return ret;
2293}
2294EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2295
2296/**
2297 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2298 *
2299 * @rdev: Regulator to operate on
2300 * @min_uV: Lower bound for voltage
2301 * @max_uV: Upper bound for voltage
2302 *
2303 * Drivers implementing set_voltage_sel() and list_voltage() can use
2304 * this as their map_voltage() operation. It will find a suitable
2305 * voltage by calling list_voltage() until it gets something in bounds
2306 * for the requested voltages.
2307 */
2308int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2309 int min_uV, int max_uV)
2310{
2311 int best_val = INT_MAX;
2312 int selector = 0;
2313 int i, ret;
2314
2315 /* Find the smallest voltage that falls within the specified
2316 * range.
2317 */
2318 for (i = 0; i < rdev->desc->n_voltages; i++) {
2319 ret = rdev->desc->ops->list_voltage(rdev, i);
2320 if (ret < 0)
2321 continue;
2322
2323 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2324 best_val = ret;
2325 selector = i;
2326 }
2327 }
2328
2329 if (best_val != INT_MAX)
2330 return selector;
2331 else
2332 return -EINVAL;
2333}
2334EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2335
2336/**
2337 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2338 *
2339 * @rdev: Regulator to operate on
2340 * @min_uV: Lower bound for voltage
2341 * @max_uV: Upper bound for voltage
2342 *
2343 * Drivers that have ascendant voltage list can use this as their
2344 * map_voltage() operation.
2345 */
2346int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2347 int min_uV, int max_uV)
2348{
2349 int i, ret;
2350
2351 for (i = 0; i < rdev->desc->n_voltages; i++) {
2352 ret = rdev->desc->ops->list_voltage(rdev, i);
2353 if (ret < 0)
2354 continue;
2355
2356 if (ret > max_uV)
2357 break;
2358
2359 if (ret >= min_uV && ret <= max_uV)
2360 return i;
2361 }
2362
2363 return -EINVAL;
2364}
2365EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2366
2367/**
2368 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2369 *
2370 * @rdev: Regulator to operate on
2371 * @min_uV: Lower bound for voltage
2372 * @max_uV: Upper bound for voltage
2373 *
2374 * Drivers providing min_uV and uV_step in their regulator_desc can
2375 * use this as their map_voltage() operation.
2376 */
2377int regulator_map_voltage_linear(struct regulator_dev *rdev,
2378 int min_uV, int max_uV)
2379{
2380 int ret, voltage;
2381
2382 /* Allow uV_step to be 0 for fixed voltage */
2383 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2384 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2385 return 0;
2386 else
2387 return -EINVAL;
2388 }
2389
2390 if (!rdev->desc->uV_step) {
2391 BUG_ON(!rdev->desc->uV_step);
2392 return -EINVAL;
2393 }
2394
2395 if (min_uV < rdev->desc->min_uV)
2396 min_uV = rdev->desc->min_uV;
2397
2398 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2399 if (ret < 0)
2400 return ret;
2401
2402 ret += rdev->desc->linear_min_sel;
2403
2404 /* Map back into a voltage to verify we're still in bounds */
2405 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2406 if (voltage < min_uV || voltage > max_uV)
2407 return -EINVAL;
2408
2409 return ret;
2410}
2411EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2412
2413/**
2414 * regulator_map_voltage_linear - map_voltage() for multiple linear ranges
2415 *
2416 * @rdev: Regulator to operate on
2417 * @min_uV: Lower bound for voltage
2418 * @max_uV: Upper bound for voltage
2419 *
2420 * Drivers providing linear_ranges in their descriptor can use this as
2421 * their map_voltage() callback.
2422 */
2423int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
2424 int min_uV, int max_uV)
2425{
2426 const struct regulator_linear_range *range;
2427 int ret = -EINVAL;
2428 int voltage, i;
2429
2430 if (!rdev->desc->n_linear_ranges) {
2431 BUG_ON(!rdev->desc->n_linear_ranges);
2432 return -EINVAL;
2433 }
2434
2435 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
2436 range = &rdev->desc->linear_ranges[i];
2437
2438 if (!(min_uV <= range->max_uV && max_uV >= range->min_uV))
2439 continue;
2440
2441 if (min_uV <= range->min_uV)
2442 min_uV = range->min_uV;
2443
2444 /* range->uV_step == 0 means fixed voltage range */
2445 if (range->uV_step == 0) {
2446 ret = 0;
2447 } else {
2448 ret = DIV_ROUND_UP(min_uV - range->min_uV,
2449 range->uV_step);
2450 if (ret < 0)
2451 return ret;
2452 }
2453
2454 ret += range->min_sel;
2455
2456 break;
2457 }
2458
2459 if (i == rdev->desc->n_linear_ranges)
2460 return -EINVAL;
2461
2462 /* Map back into a voltage to verify we're still in bounds */
2463 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2464 if (voltage < min_uV || voltage > max_uV)
2465 return -EINVAL;
2466
2467 return ret;
2468}
2469EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
2470
2471static int _regulator_do_set_voltage(struct regulator_dev *rdev, 2085static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2472 int min_uV, int max_uV) 2086 int min_uV, int max_uV)
2473{ 2087{
@@ -3071,47 +2685,6 @@ out:
3071EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); 2685EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
3072 2686
3073/** 2687/**
3074 * regulator_set_bypass_regmap - Default set_bypass() using regmap
3075 *
3076 * @rdev: device to operate on.
3077 * @enable: state to set.
3078 */
3079int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
3080{
3081 unsigned int val;
3082
3083 if (enable)
3084 val = rdev->desc->bypass_mask;
3085 else
3086 val = 0;
3087
3088 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
3089 rdev->desc->bypass_mask, val);
3090}
3091EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
3092
3093/**
3094 * regulator_get_bypass_regmap - Default get_bypass() using regmap
3095 *
3096 * @rdev: device to operate on.
3097 * @enable: current state.
3098 */
3099int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
3100{
3101 unsigned int val;
3102 int ret;
3103
3104 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
3105 if (ret != 0)
3106 return ret;
3107
3108 *enable = val & rdev->desc->bypass_mask;
3109
3110 return 0;
3111}
3112EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
3113
3114/**
3115 * regulator_allow_bypass - allow the regulator to go into bypass mode 2688 * regulator_allow_bypass - allow the regulator to go into bypass mode
3116 * 2689 *
3117 * @regulator: Regulator to configure 2690 * @regulator: Regulator to configure
diff --git a/drivers/regulator/da903x.c b/drivers/regulator/da903x.c
index 2afa5730f324..1378a242a3af 100644
--- a/drivers/regulator/da903x.c
+++ b/drivers/regulator/da903x.c
@@ -252,39 +252,12 @@ static int da9034_set_dvc_voltage_sel(struct regulator_dev *rdev,
252 return ret; 252 return ret;
253} 253}
254 254
255static int da9034_map_ldo12_voltage(struct regulator_dev *rdev, 255static const struct regulator_linear_range da9034_ldo12_ranges[] = {
256 int min_uV, int max_uV) 256 { .min_uV = 1700000, .max_uV = 2050000, .min_sel = 0, .max_sel = 7,
257{ 257 .uV_step = 50000 },
258 struct da903x_regulator_info *info = rdev_get_drvdata(rdev); 258 { .min_uV = 2700000, .max_uV = 3050000, .min_sel = 8, .max_sel = 15,
259 int sel; 259 .uV_step = 50000 },
260 260};
261 if (check_range(info, min_uV, max_uV)) {
262 pr_err("invalid voltage range (%d, %d) uV\n", min_uV, max_uV);
263 return -EINVAL;
264 }
265
266 sel = DIV_ROUND_UP(min_uV - info->desc.min_uV, info->desc.uV_step);
267 sel = (sel >= 20) ? sel - 12 : ((sel > 7) ? 8 : sel);
268
269 return sel;
270}
271
272static int da9034_list_ldo12_voltage(struct regulator_dev *rdev,
273 unsigned selector)
274{
275 struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
276 int volt;
277
278 if (selector >= 8)
279 volt = 2700000 + rdev->desc->uV_step * (selector - 8);
280 else
281 volt = rdev->desc->min_uV + rdev->desc->uV_step * selector;
282
283 if (volt > info->max_uV)
284 return -EINVAL;
285
286 return volt;
287}
288 261
289static struct regulator_ops da903x_regulator_ldo_ops = { 262static struct regulator_ops da903x_regulator_ldo_ops = {
290 .set_voltage_sel = da903x_set_voltage_sel, 263 .set_voltage_sel = da903x_set_voltage_sel,
@@ -332,8 +305,8 @@ static struct regulator_ops da9034_regulator_dvc_ops = {
332static struct regulator_ops da9034_regulator_ldo12_ops = { 305static struct regulator_ops da9034_regulator_ldo12_ops = {
333 .set_voltage_sel = da903x_set_voltage_sel, 306 .set_voltage_sel = da903x_set_voltage_sel,
334 .get_voltage_sel = da903x_get_voltage_sel, 307 .get_voltage_sel = da903x_get_voltage_sel,
335 .list_voltage = da9034_list_ldo12_voltage, 308 .list_voltage = regulator_list_voltage_linear_range,
336 .map_voltage = da9034_map_ldo12_voltage, 309 .map_voltage = regulator_map_voltage_linear_range,
337 .enable = da903x_enable, 310 .enable = da903x_enable,
338 .disable = da903x_disable, 311 .disable = da903x_disable,
339 .is_enabled = da903x_is_enabled, 312 .is_enabled = da903x_is_enabled,
@@ -476,6 +449,8 @@ static int da903x_regulator_probe(struct platform_device *pdev)
476 if (ri->desc.id == DA9034_ID_LDO12) { 449 if (ri->desc.id == DA9034_ID_LDO12) {
477 ri->desc.ops = &da9034_regulator_ldo12_ops; 450 ri->desc.ops = &da9034_regulator_ldo12_ops;
478 ri->desc.n_voltages = 16; 451 ri->desc.n_voltages = 16;
452 ri->desc.linear_ranges = da9034_ldo12_ranges;
453 ri->desc.n_linear_ranges = ARRAY_SIZE(da9034_ldo12_ranges);
479 } 454 }
480 455
481 if (ri->desc.id == DA9030_ID_LDO14) 456 if (ri->desc.id == DA9030_ID_LDO14)
diff --git a/drivers/regulator/helpers.c b/drivers/regulator/helpers.c
new file mode 100644
index 000000000000..6e30df14714b
--- /dev/null
+++ b/drivers/regulator/helpers.c
@@ -0,0 +1,447 @@
1/*
2 * helpers.c -- Voltage/Current Regulator framework helper functions.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/err.h>
16#include <linux/delay.h>
17#include <linux/regmap.h>
18#include <linux/regulator/consumer.h>
19#include <linux/regulator/driver.h>
20#include <linux/module.h>
21
22/**
23 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
24 *
25 * @rdev: regulator to operate on
26 *
27 * Regulators that use regmap for their register I/O can set the
28 * enable_reg and enable_mask fields in their descriptor and then use
29 * this as their is_enabled operation, saving some code.
30 */
31int regulator_is_enabled_regmap(struct regulator_dev *rdev)
32{
33 unsigned int val;
34 int ret;
35
36 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
37 if (ret != 0)
38 return ret;
39
40 if (rdev->desc->enable_is_inverted)
41 return (val & rdev->desc->enable_mask) == 0;
42 else
43 return (val & rdev->desc->enable_mask) != 0;
44}
45EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
46
47/**
48 * regulator_enable_regmap - standard enable() for regmap users
49 *
50 * @rdev: regulator to operate on
51 *
52 * Regulators that use regmap for their register I/O can set the
53 * enable_reg and enable_mask fields in their descriptor and then use
54 * this as their enable() operation, saving some code.
55 */
56int regulator_enable_regmap(struct regulator_dev *rdev)
57{
58 unsigned int val;
59
60 if (rdev->desc->enable_is_inverted)
61 val = 0;
62 else
63 val = rdev->desc->enable_mask;
64
65 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
66 rdev->desc->enable_mask, val);
67}
68EXPORT_SYMBOL_GPL(regulator_enable_regmap);
69
70/**
71 * regulator_disable_regmap - standard disable() for regmap users
72 *
73 * @rdev: regulator to operate on
74 *
75 * Regulators that use regmap for their register I/O can set the
76 * enable_reg and enable_mask fields in their descriptor and then use
77 * this as their disable() operation, saving some code.
78 */
79int regulator_disable_regmap(struct regulator_dev *rdev)
80{
81 unsigned int val;
82
83 if (rdev->desc->enable_is_inverted)
84 val = rdev->desc->enable_mask;
85 else
86 val = 0;
87
88 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
89 rdev->desc->enable_mask, val);
90}
91EXPORT_SYMBOL_GPL(regulator_disable_regmap);
92
93/**
94 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
95 *
96 * @rdev: regulator to operate on
97 *
98 * Regulators that use regmap for their register I/O can set the
99 * vsel_reg and vsel_mask fields in their descriptor and then use this
100 * as their get_voltage_vsel operation, saving some code.
101 */
102int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
103{
104 unsigned int val;
105 int ret;
106
107 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
108 if (ret != 0)
109 return ret;
110
111 val &= rdev->desc->vsel_mask;
112 val >>= ffs(rdev->desc->vsel_mask) - 1;
113
114 return val;
115}
116EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
117
118/**
119 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
120 *
121 * @rdev: regulator to operate on
122 * @sel: Selector to set
123 *
124 * Regulators that use regmap for their register I/O can set the
125 * vsel_reg and vsel_mask fields in their descriptor and then use this
126 * as their set_voltage_vsel operation, saving some code.
127 */
128int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
129{
130 int ret;
131
132 sel <<= ffs(rdev->desc->vsel_mask) - 1;
133
134 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
135 rdev->desc->vsel_mask, sel);
136 if (ret)
137 return ret;
138
139 if (rdev->desc->apply_bit)
140 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
141 rdev->desc->apply_bit,
142 rdev->desc->apply_bit);
143 return ret;
144}
145EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
146
147/**
148 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
149 *
150 * @rdev: Regulator to operate on
151 * @min_uV: Lower bound for voltage
152 * @max_uV: Upper bound for voltage
153 *
154 * Drivers implementing set_voltage_sel() and list_voltage() can use
155 * this as their map_voltage() operation. It will find a suitable
156 * voltage by calling list_voltage() until it gets something in bounds
157 * for the requested voltages.
158 */
159int regulator_map_voltage_iterate(struct regulator_dev *rdev,
160 int min_uV, int max_uV)
161{
162 int best_val = INT_MAX;
163 int selector = 0;
164 int i, ret;
165
166 /* Find the smallest voltage that falls within the specified
167 * range.
168 */
169 for (i = 0; i < rdev->desc->n_voltages; i++) {
170 ret = rdev->desc->ops->list_voltage(rdev, i);
171 if (ret < 0)
172 continue;
173
174 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
175 best_val = ret;
176 selector = i;
177 }
178 }
179
180 if (best_val != INT_MAX)
181 return selector;
182 else
183 return -EINVAL;
184}
185EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
186
187/**
188 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
189 *
190 * @rdev: Regulator to operate on
191 * @min_uV: Lower bound for voltage
192 * @max_uV: Upper bound for voltage
193 *
194 * Drivers that have ascendant voltage list can use this as their
195 * map_voltage() operation.
196 */
197int regulator_map_voltage_ascend(struct regulator_dev *rdev,
198 int min_uV, int max_uV)
199{
200 int i, ret;
201
202 for (i = 0; i < rdev->desc->n_voltages; i++) {
203 ret = rdev->desc->ops->list_voltage(rdev, i);
204 if (ret < 0)
205 continue;
206
207 if (ret > max_uV)
208 break;
209
210 if (ret >= min_uV && ret <= max_uV)
211 return i;
212 }
213
214 return -EINVAL;
215}
216EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
217
218/**
219 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
220 *
221 * @rdev: Regulator to operate on
222 * @min_uV: Lower bound for voltage
223 * @max_uV: Upper bound for voltage
224 *
225 * Drivers providing min_uV and uV_step in their regulator_desc can
226 * use this as their map_voltage() operation.
227 */
228int regulator_map_voltage_linear(struct regulator_dev *rdev,
229 int min_uV, int max_uV)
230{
231 int ret, voltage;
232
233 /* Allow uV_step to be 0 for fixed voltage */
234 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
235 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
236 return 0;
237 else
238 return -EINVAL;
239 }
240
241 if (!rdev->desc->uV_step) {
242 BUG_ON(!rdev->desc->uV_step);
243 return -EINVAL;
244 }
245
246 if (min_uV < rdev->desc->min_uV)
247 min_uV = rdev->desc->min_uV;
248
249 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
250 if (ret < 0)
251 return ret;
252
253 ret += rdev->desc->linear_min_sel;
254
255 /* Map back into a voltage to verify we're still in bounds */
256 voltage = rdev->desc->ops->list_voltage(rdev, ret);
257 if (voltage < min_uV || voltage > max_uV)
258 return -EINVAL;
259
260 return ret;
261}
262EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
263
264/**
265 * regulator_map_voltage_linear - map_voltage() for multiple linear ranges
266 *
267 * @rdev: Regulator to operate on
268 * @min_uV: Lower bound for voltage
269 * @max_uV: Upper bound for voltage
270 *
271 * Drivers providing linear_ranges in their descriptor can use this as
272 * their map_voltage() callback.
273 */
274int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
275 int min_uV, int max_uV)
276{
277 const struct regulator_linear_range *range;
278 int ret = -EINVAL;
279 int voltage, i;
280
281 if (!rdev->desc->n_linear_ranges) {
282 BUG_ON(!rdev->desc->n_linear_ranges);
283 return -EINVAL;
284 }
285
286 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
287 range = &rdev->desc->linear_ranges[i];
288
289 if (!(min_uV <= range->max_uV && max_uV >= range->min_uV))
290 continue;
291
292 if (min_uV <= range->min_uV)
293 min_uV = range->min_uV;
294
295 /* range->uV_step == 0 means fixed voltage range */
296 if (range->uV_step == 0) {
297 ret = 0;
298 } else {
299 ret = DIV_ROUND_UP(min_uV - range->min_uV,
300 range->uV_step);
301 if (ret < 0)
302 return ret;
303 }
304
305 ret += range->min_sel;
306
307 break;
308 }
309
310 if (i == rdev->desc->n_linear_ranges)
311 return -EINVAL;
312
313 /* Map back into a voltage to verify we're still in bounds */
314 voltage = rdev->desc->ops->list_voltage(rdev, ret);
315 if (voltage < min_uV || voltage > max_uV)
316 return -EINVAL;
317
318 return ret;
319}
320EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
321
322/**
323 * regulator_list_voltage_linear - List voltages with simple calculation
324 *
325 * @rdev: Regulator device
326 * @selector: Selector to convert into a voltage
327 *
328 * Regulators with a simple linear mapping between voltages and
329 * selectors can set min_uV and uV_step in the regulator descriptor
330 * and then use this function as their list_voltage() operation,
331 */
332int regulator_list_voltage_linear(struct regulator_dev *rdev,
333 unsigned int selector)
334{
335 if (selector >= rdev->desc->n_voltages)
336 return -EINVAL;
337 if (selector < rdev->desc->linear_min_sel)
338 return 0;
339
340 selector -= rdev->desc->linear_min_sel;
341
342 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
343}
344EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
345
346/**
347 * regulator_list_voltage_linear_range - List voltages for linear ranges
348 *
349 * @rdev: Regulator device
350 * @selector: Selector to convert into a voltage
351 *
352 * Regulators with a series of simple linear mappings between voltages
353 * and selectors can set linear_ranges in the regulator descriptor and
354 * then use this function as their list_voltage() operation,
355 */
356int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
357 unsigned int selector)
358{
359 const struct regulator_linear_range *range;
360 int i;
361
362 if (!rdev->desc->n_linear_ranges) {
363 BUG_ON(!rdev->desc->n_linear_ranges);
364 return -EINVAL;
365 }
366
367 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
368 range = &rdev->desc->linear_ranges[i];
369
370 if (!(selector >= range->min_sel &&
371 selector <= range->max_sel))
372 continue;
373
374 selector -= range->min_sel;
375
376 return range->min_uV + (range->uV_step * selector);
377 }
378
379 return -EINVAL;
380}
381EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
382
383/**
384 * regulator_list_voltage_table - List voltages with table based mapping
385 *
386 * @rdev: Regulator device
387 * @selector: Selector to convert into a voltage
388 *
389 * Regulators with table based mapping between voltages and
390 * selectors can set volt_table in the regulator descriptor
391 * and then use this function as their list_voltage() operation.
392 */
393int regulator_list_voltage_table(struct regulator_dev *rdev,
394 unsigned int selector)
395{
396 if (!rdev->desc->volt_table) {
397 BUG_ON(!rdev->desc->volt_table);
398 return -EINVAL;
399 }
400
401 if (selector >= rdev->desc->n_voltages)
402 return -EINVAL;
403
404 return rdev->desc->volt_table[selector];
405}
406EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
407
408/**
409 * regulator_set_bypass_regmap - Default set_bypass() using regmap
410 *
411 * @rdev: device to operate on.
412 * @enable: state to set.
413 */
414int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
415{
416 unsigned int val;
417
418 if (enable)
419 val = rdev->desc->bypass_mask;
420 else
421 val = 0;
422
423 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
424 rdev->desc->bypass_mask, val);
425}
426EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
427
428/**
429 * regulator_get_bypass_regmap - Default get_bypass() using regmap
430 *
431 * @rdev: device to operate on.
432 * @enable: current state.
433 */
434int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
435{
436 unsigned int val;
437 int ret;
438
439 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
440 if (ret != 0)
441 return ret;
442
443 *enable = val & rdev->desc->bypass_mask;
444
445 return 0;
446}
447EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);