Skip to content

Commit 5441b69

Browse files
wensbroonie
authored andcommitted
regulator: Add of_regulator_get_optional() for pure DT regulator lookup
The to-be-introduced I2C component prober needs to enable regulator supplies (and toggle GPIO pins) for the various components it intends to probe. To support this, a new "pure DT lookup" method for getting regulator supplies is needed, since the device normally requesting the supply won't get created until after the component is probed to be available. Add a new of_regulator_get_optional() function for this. This mirrors the existing regulator_get_optional() function, but is OF-specific. The underlying code that supports the existing regulator_get*() functions has been reworked in previous patches to support this specific case. Also convert an existing usage of "dev && dev->of_node" to "dev_of_node(dev)". Link: https://lore.kernel.org/all/[email protected]/ [1] Signed-off-by: Chen-Yu Tsai <[email protected]> Link: https://patch.msgid.link/[email protected] Reviewed-by: Andy Shevchenko <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent 9852d85 commit 5441b69

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
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/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static inline struct regulator_dev *dev_to_rdev(struct device *dev)
6767

6868
#ifdef CONFIG_OF
6969
struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
70+
struct device_node *np,
7071
const char *supply);
7172
struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
7273
const struct regulator_desc *desc,
@@ -82,6 +83,7 @@ bool of_check_coupling_data(struct regulator_dev *rdev);
8283

8384
#else
8485
static inline struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
86+
struct device_node *np,
8587
const char *supply)
8688
{
8789
return ERR_PTR(-ENODEV);

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+
static 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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ 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+
#else
176+
static inline struct regulator *__must_check of_regulator_get_optional(struct device *dev,
177+
struct device_node *node,
178+
const char *id)
179+
{
180+
return ERR_PTR(-ENODEV);
181+
}
182+
#endif
183+
171184
int regulator_register_supply_alias(struct device *dev, const char *id,
172185
struct device *alias_dev,
173186
const char *alias_id);
@@ -350,6 +363,13 @@ devm_regulator_get_optional(struct device *dev, const char *id)
350363
return ERR_PTR(-ENODEV);
351364
}
352365

366+
static inline struct regulator *__must_check of_regulator_get_optional(struct device *dev,
367+
struct device_node *node,
368+
const char *id)
369+
{
370+
return ERR_PTR(-ENODEV);
371+
}
372+
353373
static inline void regulator_put(struct regulator *regulator)
354374
{
355375
}

0 commit comments

Comments
 (0)