Skip to content

Commit dc16594

Browse files
committed
Add of_regulator_get_optional() and Fix MTK Power
Merge series from Chen-Yu Tsai <[email protected]>: At ELCE, Sebastian told me about his recent work on adding regulator supply support to the Rockchip power domain driver [2], how the MediaTek driver has been using the existing devm_regulator_get() API and reassigning different device nodes to the device doing the lookup, and how the new of_regulator_get_optional() is the proper fit for this. Patch 1 adds a new of_regulator_get_optional() function to look up regulator supplies using device tree nodes. Patch 2 adds a devres version of the aforementioned function at Sebastian's request for the two power domain drivers.
2 parents 18be43a + 36ec3f4 commit dc16594

File tree

5 files changed

+135
-14
lines changed

5 files changed

+135
-14
lines changed

drivers/regulator/core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,8 +1959,8 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
19591959
regulator_supply_alias(&dev, &supply);
19601960

19611961
/* first do a dt based lookup */
1962-
if (dev && dev->of_node) {
1963-
r = of_regulator_dev_lookup(dev, supply);
1962+
if (dev_of_node(dev)) {
1963+
r = of_regulator_dev_lookup(dev, dev_of_node(dev), supply);
19641964
if (!IS_ERR(r))
19651965
return r;
19661966
if (PTR_ERR(r) == -EPROBE_DEFER)

drivers/regulator/devres.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,3 +749,42 @@ void *devm_regulator_irq_helper(struct device *dev,
749749
return ptr;
750750
}
751751
EXPORT_SYMBOL_GPL(devm_regulator_irq_helper);
752+
753+
#if IS_ENABLED(CONFIG_OF)
754+
static struct regulator *_devm_of_regulator_get(struct device *dev, struct device_node *node,
755+
const char *id, int get_type)
756+
{
757+
struct regulator **ptr, *regulator;
758+
759+
ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
760+
if (!ptr)
761+
return ERR_PTR(-ENOMEM);
762+
763+
regulator = _of_regulator_get(dev, node, id, get_type);
764+
if (!IS_ERR(regulator)) {
765+
*ptr = regulator;
766+
devres_add(dev, ptr);
767+
} else {
768+
devres_free(ptr);
769+
}
770+
771+
return regulator;
772+
}
773+
774+
/**
775+
* devm_of_regulator_get_optional - Resource managed of_regulator_get_optional()
776+
* @dev: device used for dev_printk() messages and resource lifetime management
777+
* @node: device node for regulator "consumer"
778+
* @id: supply name or regulator ID.
779+
*
780+
* Managed regulator_get_optional(). Regulators returned from this
781+
* function are automatically regulator_put() on driver detach. See
782+
* of_regulator_get_optional() for more information.
783+
*/
784+
struct regulator *devm_of_regulator_get_optional(struct device *dev, struct device_node *node,
785+
const char *id)
786+
{
787+
return _devm_of_regulator_get(dev, node, id, OPTIONAL_GET);
788+
}
789+
EXPORT_SYMBOL_GPL(devm_of_regulator_get_optional);
790+
#endif

drivers/regulator/internal.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,25 @@ static inline struct regulator_dev *dev_to_rdev(struct device *dev)
6565
return container_of(dev, struct regulator_dev, dev);
6666
}
6767

68+
enum regulator_get_type {
69+
NORMAL_GET,
70+
EXCLUSIVE_GET,
71+
OPTIONAL_GET,
72+
MAX_GET_TYPE
73+
};
74+
6875
#ifdef CONFIG_OF
6976
struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
77+
struct device_node *np,
7078
const char *supply);
7179
struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
7280
const struct regulator_desc *desc,
7381
struct regulator_config *config,
7482
struct device_node **node);
7583

84+
struct regulator *_of_regulator_get(struct device *dev, struct device_node *node,
85+
const char *id, enum regulator_get_type get_type);
86+
7687
struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
7788
int index);
7889

@@ -82,6 +93,7 @@ bool of_check_coupling_data(struct regulator_dev *rdev);
8293

8394
#else
8495
static inline struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
96+
struct device_node *np,
8597
const char *supply)
8698
{
8799
return ERR_PTR(-ENODEV);
@@ -114,12 +126,6 @@ static inline bool of_check_coupling_data(struct regulator_dev *rdev)
114126
}
115127

116128
#endif
117-
enum regulator_get_type {
118-
NORMAL_GET,
119-
EXCLUSIVE_GET,
120-
OPTIONAL_GET,
121-
MAX_GET_TYPE
122-
};
123129

124130
int _regulator_get_common_check(struct device *dev, const char *id,
125131
enum regulator_get_type get_type);

drivers/regulator/of_regulator.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -588,23 +588,25 @@ static struct device_node *of_get_child_regulator(struct device_node *parent,
588588

589589
/**
590590
* of_get_regulator - get a regulator device node based on supply name
591-
* @dev: Device pointer for the consumer (of regulator) device
591+
* @dev: Device pointer for dev_printk() messages
592+
* @node: Device node pointer for supply property lookup
592593
* @supply: regulator supply name
593594
*
594595
* Extract the regulator device node corresponding to the supply name.
595596
*
596597
* Return: Pointer to the &struct device_node corresponding to the regulator
597598
* if found, or %NULL if not found.
598599
*/
599-
static struct device_node *of_get_regulator(struct device *dev, const char *supply)
600+
static struct device_node *of_get_regulator(struct device *dev, struct device_node *node,
601+
const char *supply)
600602
{
601603
struct device_node *regnode = NULL;
602604
char prop_name[64]; /* 64 is max size of property name */
603605

604-
dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
606+
dev_dbg(dev, "Looking up %s-supply from device node %pOF\n", supply, node);
605607

606608
snprintf(prop_name, 64, "%s-supply", supply);
607-
regnode = of_parse_phandle(dev->of_node, prop_name, 0);
609+
regnode = of_parse_phandle(node, prop_name, 0);
608610
if (regnode)
609611
return regnode;
610612

@@ -628,6 +630,7 @@ static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
628630
/**
629631
* of_regulator_dev_lookup - lookup a regulator device with device tree only
630632
* @dev: Device pointer for regulator supply lookup.
633+
* @np: Device node pointer for regulator supply lookup.
631634
* @supply: Supply name or regulator ID.
632635
*
633636
* Return: Pointer to the &struct regulator_dev on success, or ERR_PTR()
@@ -642,13 +645,13 @@ static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
642645
* * -%ENODEV if lookup fails permanently.
643646
* * -%EPROBE_DEFER if lookup could succeed in the future.
644647
*/
645-
struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
648+
struct regulator_dev *of_regulator_dev_lookup(struct device *dev, struct device_node *np,
646649
const char *supply)
647650
{
648651
struct regulator_dev *r;
649652
struct device_node *node;
650653

651-
node = of_get_regulator(dev, supply);
654+
node = of_get_regulator(dev, np, supply);
652655
if (node) {
653656
r = of_find_regulator_by_node(node);
654657
of_node_put(node);
@@ -665,6 +668,42 @@ struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
665668
return ERR_PTR(-ENODEV);
666669
}
667670

671+
struct regulator *_of_regulator_get(struct device *dev, struct device_node *node,
672+
const char *id, enum regulator_get_type get_type)
673+
{
674+
struct regulator_dev *r;
675+
int ret;
676+
677+
ret = _regulator_get_common_check(dev, id, get_type);
678+
if (ret)
679+
return ERR_PTR(ret);
680+
681+
r = of_regulator_dev_lookup(dev, node, id);
682+
return _regulator_get_common(r, dev, id, get_type);
683+
}
684+
685+
/**
686+
* of_regulator_get_optional - get optional regulator via device tree lookup
687+
* @dev: device used for dev_printk() messages
688+
* @node: device node for regulator "consumer"
689+
* @id: Supply name
690+
*
691+
* Return: pointer to struct regulator corresponding to the regulator producer,
692+
* or PTR_ERR() encoded error number.
693+
*
694+
* This is intended for use by consumers that want to get a regulator
695+
* supply directly from a device node, and can and want to deal with
696+
* absence of such supplies. This will _not_ consider supply aliases.
697+
* See regulator_dev_lookup().
698+
*/
699+
struct regulator *of_regulator_get_optional(struct device *dev,
700+
struct device_node *node,
701+
const char *id)
702+
{
703+
return _of_regulator_get(dev, node, id, OPTIONAL_GET);
704+
}
705+
EXPORT_SYMBOL_GPL(of_regulator_get_optional);
706+
668707
/*
669708
* Returns number of regulators coupled with rdev.
670709
*/

include/linux/regulator/consumer.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ int devm_regulator_get_enable_read_voltage(struct device *dev, const char *id);
168168
void regulator_put(struct regulator *regulator);
169169
void devm_regulator_put(struct regulator *regulator);
170170

171+
#if IS_ENABLED(CONFIG_OF)
172+
struct regulator *__must_check of_regulator_get_optional(struct device *dev,
173+
struct device_node *node,
174+
const char *id);
175+
struct regulator *__must_check devm_of_regulator_get_optional(struct device *dev,
176+
struct device_node *node,
177+
const char *id);
178+
#else
179+
static inline struct regulator *__must_check of_regulator_get_optional(struct device *dev,
180+
struct device_node *node,
181+
const char *id)
182+
{
183+
return ERR_PTR(-ENODEV);
184+
}
185+
186+
static inline struct regulator *__must_check devm_of_regulator_get_optional(struct device *dev,
187+
struct device_node *node,
188+
const char *id)
189+
{
190+
return ERR_PTR(-ENODEV);
191+
}
192+
#endif
193+
171194
int regulator_register_supply_alias(struct device *dev, const char *id,
172195
struct device *alias_dev,
173196
const char *alias_id);
@@ -350,6 +373,20 @@ devm_regulator_get_optional(struct device *dev, const char *id)
350373
return ERR_PTR(-ENODEV);
351374
}
352375

376+
static inline struct regulator *__must_check of_regulator_get_optional(struct device *dev,
377+
struct device_node *node,
378+
const char *id)
379+
{
380+
return ERR_PTR(-ENODEV);
381+
}
382+
383+
static inline struct regulator *__must_check devm_of_regulator_get_optional(struct device *dev,
384+
struct device_node *node,
385+
const char *id)
386+
{
387+
return ERR_PTR(-ENODEV);
388+
}
389+
353390
static inline void regulator_put(struct regulator *regulator)
354391
{
355392
}

0 commit comments

Comments
 (0)