#define dev_fmt(fmt) "pwrctrl: " fmt
#include <linux/device.h>
#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/of_graph.h>
#include <linux/of_platform.h>
#include <linux/pci.h>
#include <linux/pci-pwrctrl.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include "../pci.h"
static int pci_pwrctrl_notify(struct notifier_block *nb, unsigned long action,
void *data)
{
struct pci_pwrctrl *pwrctrl = container_of(nb, struct pci_pwrctrl, nb);
struct device *dev = data;
if (dev_fwnode(dev) != dev_fwnode(pwrctrl->dev))
return NOTIFY_DONE;
switch (action) {
case BUS_NOTIFY_ADD_DEVICE:
dev->of_node_reused = true;
break;
}
return NOTIFY_DONE;
}
void pci_pwrctrl_init(struct pci_pwrctrl *pwrctrl, struct device *dev)
{
pwrctrl->dev = dev;
dev_set_drvdata(dev, pwrctrl);
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_init);
int pci_pwrctrl_device_set_ready(struct pci_pwrctrl *pwrctrl)
{
int ret;
if (!pwrctrl->dev)
return -ENODEV;
pwrctrl->nb.notifier_call = pci_pwrctrl_notify;
ret = bus_register_notifier(&pci_bus_type, &pwrctrl->nb);
if (ret)
return ret;
return 0;
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_device_set_ready);
void pci_pwrctrl_device_unset_ready(struct pci_pwrctrl *pwrctrl)
{
bus_unregister_notifier(&pci_bus_type, &pwrctrl->nb);
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_device_unset_ready);
static void devm_pci_pwrctrl_device_unset_ready(void *data)
{
struct pci_pwrctrl *pwrctrl = data;
pci_pwrctrl_device_unset_ready(pwrctrl);
}
int devm_pci_pwrctrl_device_set_ready(struct device *dev,
struct pci_pwrctrl *pwrctrl)
{
int ret;
ret = pci_pwrctrl_device_set_ready(pwrctrl);
if (ret)
return ret;
return devm_add_action_or_reset(dev,
devm_pci_pwrctrl_device_unset_ready,
pwrctrl);
}
EXPORT_SYMBOL_GPL(devm_pci_pwrctrl_device_set_ready);
static int __pci_pwrctrl_power_off_device(struct device *dev)
{
struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
if (!pwrctrl)
return 0;
return pwrctrl->power_off(pwrctrl);
}
static void pci_pwrctrl_power_off_device(struct device_node *np)
{
struct platform_device *pdev;
int ret;
for_each_available_child_of_node_scoped(np, child)
pci_pwrctrl_power_off_device(child);
pdev = of_find_device_by_node(np);
if (!pdev)
return;
if (device_is_bound(&pdev->dev)) {
ret = __pci_pwrctrl_power_off_device(&pdev->dev);
if (ret)
dev_err(&pdev->dev, "Failed to power off device: %d", ret);
}
platform_device_put(pdev);
}
void pci_pwrctrl_power_off_devices(struct device *parent)
{
struct device_node *np = parent->of_node;
for_each_available_child_of_node_scoped(np, child)
pci_pwrctrl_power_off_device(child);
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_power_off_devices);
static int __pci_pwrctrl_power_on_device(struct device *dev)
{
struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
if (!pwrctrl)
return 0;
return pwrctrl->power_on(pwrctrl);
}
static int pci_pwrctrl_power_on_device(struct device_node *np)
{
struct platform_device *pdev;
int ret;
for_each_available_child_of_node_scoped(np, child) {
ret = pci_pwrctrl_power_on_device(child);
if (ret)
return ret;
}
pdev = of_find_device_by_node(np);
if (!pdev)
return 0;
if (device_is_bound(&pdev->dev)) {
ret = __pci_pwrctrl_power_on_device(&pdev->dev);
} else {
dev_dbg(&pdev->dev, "driver is not bound\n");
ret = -EPROBE_DEFER;
}
platform_device_put(pdev);
return ret;
}
int pci_pwrctrl_power_on_devices(struct device *parent)
{
struct device_node *np = parent->of_node;
struct device_node *child = NULL;
int ret;
for_each_available_child_of_node(np, child) {
ret = pci_pwrctrl_power_on_device(child);
if (ret)
goto err_power_off;
}
return 0;
err_power_off:
for_each_available_child_of_node_scoped(np, tmp) {
if (tmp == child)
break;
pci_pwrctrl_power_off_device(tmp);
}
of_node_put(child);
return ret;
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_power_on_devices);
static bool pci_pwrctrl_is_required(struct device_node *np)
{
struct device_node *endpoint;
const char *compat;
int ret;
ret = of_property_read_string(np, "compatible", &compat);
if (ret < 0)
return false;
if (!strstarts(compat, "pci"))
return false;
if (of_pci_supply_present(np))
return true;
if (of_graph_is_present(np)) {
for_each_endpoint_of_node(np, endpoint) {
struct device_node *remote __free(device_node) =
of_graph_get_remote_port_parent(endpoint);
if (remote) {
if (of_pci_supply_present(remote)) {
of_node_put(endpoint);
return true;
}
}
}
}
return false;
}
static int pci_pwrctrl_create_device(struct device_node *np,
struct device *parent)
{
struct platform_device *pdev;
int ret;
for_each_available_child_of_node_scoped(np, child) {
ret = pci_pwrctrl_create_device(child, parent);
if (ret)
return ret;
}
pdev = of_find_device_by_node(np);
if (pdev) {
platform_device_put(pdev);
return 0;
}
if (!pci_pwrctrl_is_required(np)) {
dev_dbg(parent, "Skipping OF node: %s\n", np->name);
return 0;
}
pdev = of_platform_device_create(np, NULL, parent);
if (!pdev) {
dev_err(parent, "Failed to create pwrctrl device for node: %s\n", np->name);
return -EINVAL;
}
return 0;
}
int pci_pwrctrl_create_devices(struct device *parent)
{
int ret;
for_each_available_child_of_node_scoped(parent->of_node, child) {
ret = pci_pwrctrl_create_device(child, parent);
if (ret) {
pci_pwrctrl_destroy_devices(parent);
return ret;
}
}
return 0;
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_create_devices);
static void pci_pwrctrl_destroy_device(struct device_node *np)
{
struct platform_device *pdev;
for_each_available_child_of_node_scoped(np, child)
pci_pwrctrl_destroy_device(child);
pdev = of_find_device_by_node(np);
if (!pdev)
return;
of_device_unregister(pdev);
platform_device_put(pdev);
of_node_clear_flag(np, OF_POPULATED);
}
void pci_pwrctrl_destroy_devices(struct device *parent)
{
struct device_node *np = parent->of_node;
for_each_available_child_of_node_scoped(np, child)
pci_pwrctrl_destroy_device(child);
}
EXPORT_SYMBOL_GPL(pci_pwrctrl_destroy_devices);
MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
MODULE_DESCRIPTION("PCI Device Power Control core driver");
MODULE_LICENSE("GPL");