| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Simple Power-Managed Bus Driver |
| 4 |
* |
| 5 |
* Copyright (C) 2014-2015 Glider bvba |
| 6 |
* |
| 7 |
* This file is subject to the terms and conditions of the GNU General Public |
| 8 |
* License. See the file "COPYING" in the main directory of this archive |
| 9 |
* for more details. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/clk.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/of.h> |
| 15 |
#include <linux/of_device.h> |
| 16 |
#include <linux/of_platform.h> |
| 17 |
#include <linux/platform_device.h> |
| 18 |
#include <linux/pm_runtime.h> |
| 19 |
|
| 20 |
struct simple_pm_bus { |
| 21 |
struct clk_bulk_data *clks; |
| 22 |
int num_clks; |
| 23 |
}; |
| 24 |
|
| 25 |
static int simple_pm_bus_probe(struct platform_device *pdev) |
| 26 |
{ |
| 27 |
const struct device *dev = &pdev->dev; |
| 28 |
const struct of_dev_auxdata *lookup = dev_get_platdata(dev); |
| 29 |
struct device_node *np = dev->of_node; |
| 30 |
const struct of_device_id *match; |
| 31 |
struct simple_pm_bus *bus; |
| 32 |
|
| 33 |
/* |
| 34 |
* Allow user to use driver_override to bind this driver to a |
| 35 |
* transparent bus device which has a different compatible string |
| 36 |
* that's not listed in simple_pm_bus_of_match. We don't want to do any |
| 37 |
* of the simple-pm-bus tasks for these devices, so return early. |
| 38 |
*/ |
| 39 |
if (device_has_driver_override(&pdev->dev)) |
| 40 |
return 0; |
| 41 |
|
| 42 |
match = of_match_device(dev->driver->of_match_table, dev); |
| 43 |
/* |
| 44 |
* These are transparent bus devices (not simple-pm-bus matches) that |
| 45 |
* have their child nodes populated automatically. So, don't need to |
| 46 |
* do anything more. We only match with the device if this driver is |
| 47 |
* the most specific match because we don't want to incorrectly bind to |
| 48 |
* a device that has a more specific driver. |
| 49 |
*/ |
| 50 |
if (match && match->data) { |
| 51 |
if (of_property_match_string(np, "compatible", match->compatible) == 0) |
| 52 |
return 0; |
| 53 |
else |
| 54 |
return -ENODEV; |
| 55 |
} |
| 56 |
|
| 57 |
bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL); |
| 58 |
if (!bus) |
| 59 |
return -ENOMEM; |
| 60 |
|
| 61 |
bus->num_clks = devm_clk_bulk_get_all(&pdev->dev, &bus->clks); |
| 62 |
if (bus->num_clks < 0) |
| 63 |
return dev_err_probe(&pdev->dev, bus->num_clks, "failed to get clocks\n"); |
| 64 |
|
| 65 |
dev_set_drvdata(&pdev->dev, bus); |
| 66 |
|
| 67 |
dev_dbg(&pdev->dev, "%s\n", __func__); |
| 68 |
|
| 69 |
pm_runtime_enable(&pdev->dev); |
| 70 |
|
| 71 |
if (np) |
| 72 |
of_platform_populate(np, NULL, lookup, &pdev->dev); |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
static void simple_pm_bus_remove(struct platform_device *pdev) |
| 78 |
{ |
| 79 |
const void *data = of_device_get_match_data(&pdev->dev); |
| 80 |
|
| 81 |
if (device_has_driver_override(&pdev->dev) || data) |
| 82 |
return; |
| 83 |
|
| 84 |
dev_dbg(&pdev->dev, "%s\n", __func__); |
| 85 |
|
| 86 |
pm_runtime_disable(&pdev->dev); |
| 87 |
} |
| 88 |
|
| 89 |
static int simple_pm_bus_runtime_suspend(struct device *dev) |
| 90 |
{ |
| 91 |
struct simple_pm_bus *bus = dev_get_drvdata(dev); |
| 92 |
|
| 93 |
clk_bulk_disable_unprepare(bus->num_clks, bus->clks); |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
static int simple_pm_bus_runtime_resume(struct device *dev) |
| 99 |
{ |
| 100 |
struct simple_pm_bus *bus = dev_get_drvdata(dev); |
| 101 |
int ret; |
| 102 |
|
| 103 |
ret = clk_bulk_prepare_enable(bus->num_clks, bus->clks); |
| 104 |
if (ret) { |
| 105 |
dev_err(dev, "failed to enable clocks: %d\n", ret); |
| 106 |
return ret; |
| 107 |
} |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
static int simple_pm_bus_suspend(struct device *dev) |
| 113 |
{ |
| 114 |
struct simple_pm_bus *bus = dev_get_drvdata(dev); |
| 115 |
|
| 116 |
if (!bus) |
| 117 |
return 0; |
| 118 |
|
| 119 |
return pm_runtime_force_suspend(dev); |
| 120 |
} |
| 121 |
|
| 122 |
static int simple_pm_bus_resume(struct device *dev) |
| 123 |
{ |
| 124 |
struct simple_pm_bus *bus = dev_get_drvdata(dev); |
| 125 |
|
| 126 |
if (!bus) |
| 127 |
return 0; |
| 128 |
|
| 129 |
return pm_runtime_force_resume(dev); |
| 130 |
} |
| 131 |
|
| 132 |
static const struct dev_pm_ops simple_pm_bus_pm_ops = { |
| 133 |
RUNTIME_PM_OPS(simple_pm_bus_runtime_suspend, simple_pm_bus_runtime_resume, NULL) |
| 134 |
NOIRQ_SYSTEM_SLEEP_PM_OPS(simple_pm_bus_suspend, simple_pm_bus_resume) |
| 135 |
}; |
| 136 |
|
| 137 |
#define ONLY_BUS ((void *) 1) /* Match if the device is only a bus. */ |
| 138 |
|
| 139 |
static const struct of_device_id simple_pm_bus_of_match[] = { |
| 140 |
{ .compatible = "simple-pm-bus", }, |
| 141 |
{ .compatible = "simple-bus", .data = ONLY_BUS }, |
| 142 |
{ .compatible = "simple-mfd", .data = ONLY_BUS }, |
| 143 |
{ .compatible = "isa", .data = ONLY_BUS }, |
| 144 |
{ .compatible = "arm,amba-bus", .data = ONLY_BUS }, |
| 145 |
{ .compatible = "fsl,ls1021a-scfg", }, |
| 146 |
{ .compatible = "fsl,ls1043a-scfg", }, |
| 147 |
{ .compatible = "fsl,ls1046a-scfg", }, |
| 148 |
{ .compatible = "fsl,ls1088a-isc", }, |
| 149 |
{ .compatible = "fsl,ls2080a-isc", }, |
| 150 |
{ .compatible = "fsl,lx2160a-isc", }, |
| 151 |
{ /* sentinel */ } |
| 152 |
}; |
| 153 |
MODULE_DEVICE_TABLE(of, simple_pm_bus_of_match); |
| 154 |
|
| 155 |
static struct platform_driver simple_pm_bus_driver = { |
| 156 |
.probe = simple_pm_bus_probe, |
| 157 |
.remove = simple_pm_bus_remove, |
| 158 |
.driver = { |
| 159 |
.name = "simple-pm-bus", |
| 160 |
.of_match_table = simple_pm_bus_of_match, |
| 161 |
.pm = pm_ptr(&simple_pm_bus_pm_ops), |
| 162 |
}, |
| 163 |
}; |
| 164 |
|
| 165 |
module_platform_driver(simple_pm_bus_driver); |
| 166 |
|
| 167 |
MODULE_DESCRIPTION("Simple Power-Managed Bus Driver"); |
| 168 |
MODULE_AUTHOR("Geert Uytterhoeven <[email protected]>"); |