mvebu: remove redundant patch for Linksys LED trigger
[openwrt/staging/hauke.git] / target / linux / mvebu / patches-5.4 / 030-linkstation-poweroff.patch
1 --- a/drivers/power/reset/Kconfig
2 +++ b/drivers/power/reset/Kconfig
3 @@ -99,6 +99,17 @@ config POWER_RESET_HISI
4 help
5 Reboot support for Hisilicon boards.
6
7 +config POWER_RESET_LINKSTATION
8 + tristate "Buffalo LinkStation power-off driver"
9 + depends on ARCH_MVEBU || COMPILE_TEST
10 + depends on OF_MDIO && PHYLIB
11 + help
12 + This driver supports turning off some Buffalo LinkStations by
13 + setting an output pin at the ethernet PHY to the correct state.
14 + It also makes the device compatible with the WoL function.
15 +
16 + Say Y here if you have a Buffalo LinkStation LS421D/E.
17 +
18 config POWER_RESET_MSM
19 bool "Qualcomm MSM power-off driver"
20 depends on ARCH_QCOM
21 --- a/drivers/power/reset/Makefile
22 +++ b/drivers/power/reset/Makefile
23 @@ -10,6 +10,7 @@ obj-$(CONFIG_POWER_RESET_GEMINI_POWEROFF
24 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
25 obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
26 obj-$(CONFIG_POWER_RESET_HISI) += hisi-reboot.o
27 +obj-${CONFIG_POWER_RESET_LINKSTATION} += linkstation-poweroff.o
28 obj-$(CONFIG_POWER_RESET_MSM) += msm-poweroff.o
29 obj-$(CONFIG_POWER_RESET_QCOM_PON) += qcom-pon.o
30 obj-$(CONFIG_POWER_RESET_OCELOT_RESET) += ocelot-reset.o
31 --- /dev/null
32 +++ b/drivers/power/reset/linkstation-poweroff.c
33 @@ -0,0 +1,144 @@
34 +// SPDX-License-Identifier: GPL-2.0
35 +/*
36 + * LinkStation power off restart driver
37 + * Copyright (C) 2020 Daniel González Cabanelas <dgcbueu@gmail.com>
38 + */
39 +
40 +#include <linux/module.h>
41 +#include <linux/notifier.h>
42 +#include <linux/of.h>
43 +#include <linux/of_mdio.h>
44 +#include <linux/of_platform.h>
45 +#include <linux/reboot.h>
46 +#include <linux/phy.h>
47 +
48 +/* Defines from the eth phy Marvell driver */
49 +#define MII_MARVELL_COPPER_PAGE 0
50 +#define MII_MARVELL_LED_PAGE 3
51 +#define MII_MARVELL_WOL_PAGE 17
52 +#define MII_MARVELL_PHY_PAGE 22
53 +
54 +#define MII_PHY_LED_CTRL 16
55 +#define MII_88E1318S_PHY_LED_TCR 18
56 +#define MII_88E1318S_PHY_WOL_CTRL 16
57 +#define MII_M1011_IEVENT 19
58 +
59 +#define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE BIT(7)
60 +#define MII_88E1318S_PHY_LED_TCR_FORCE_INT BIT(15)
61 +#define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS BIT(12)
62 +#define LED2_FORCE_ON (0x8 << 8)
63 +#define LEDMASK GENMASK(11,8)
64 +
65 +static struct phy_device *phydev;
66 +
67 +static void mvphy_reg_intn(u16 data)
68 +{
69 + int rc = 0, saved_page;
70 +
71 + saved_page = phy_select_page(phydev, MII_MARVELL_LED_PAGE);
72 + if (saved_page < 0)
73 + goto err;
74 +
75 + /* Force manual LED2 control to let INTn work */
76 + __phy_modify(phydev, MII_PHY_LED_CTRL, LEDMASK, LED2_FORCE_ON);
77 +
78 + /* Set the LED[2]/INTn pin to the required state */
79 + __phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
80 + MII_88E1318S_PHY_LED_TCR_FORCE_INT,
81 + MII_88E1318S_PHY_LED_TCR_INTn_ENABLE | data);
82 +
83 + if (!data) {
84 + /* Clear interrupts to ensure INTn won't be holded in high state */
85 + __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_COPPER_PAGE);
86 + __phy_read(phydev, MII_M1011_IEVENT);
87 +
88 + /* If WOL was enabled and a magic packet was received before powering
89 + * off, we won't be able to wake up by sending another magic packet.
90 + * Clear WOL status.
91 + */
92 + __phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_MARVELL_WOL_PAGE);
93 + __phy_set_bits(phydev, MII_88E1318S_PHY_WOL_CTRL,
94 + MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
95 + }
96 +err:
97 + rc = phy_restore_page(phydev, saved_page, rc);
98 + if (rc < 0)
99 + dev_err(&phydev->mdio.dev, "Write register failed, %d\n", rc);
100 +
101 + if (!data) {
102 + /* Slow down the PHY to save energy */
103 + rc= phy_speed_down(phydev, false);
104 + if (rc < 0)
105 + dev_err(&phydev->mdio.dev, "PHY speed down failed, %d\n", rc);
106 + }
107 +}
108 +
109 +static int linkstation_reboot_notifier(struct notifier_block *nb,
110 + unsigned long action, void *unused)
111 +{
112 + if (action == SYS_RESTART)
113 + mvphy_reg_intn(MII_88E1318S_PHY_LED_TCR_FORCE_INT);
114 +
115 + return NOTIFY_DONE;
116 +}
117 +
118 +static struct notifier_block linkstation_reboot_nb = {
119 + .notifier_call = linkstation_reboot_notifier,
120 +};
121 +
122 +static void linkstation_poweroff(void)
123 +{
124 + unregister_reboot_notifier(&linkstation_reboot_nb);
125 + mvphy_reg_intn(0);
126 +
127 + kernel_restart("Power off");
128 +}
129 +
130 +static const struct of_device_id ls_poweroff_of_match[] = {
131 + { .compatible = "buffalo,ls421d" },
132 + { .compatible = "buffalo,ls421de" },
133 + { },
134 +};
135 +
136 +static int __init linkstation_poweroff_init(void)
137 +{
138 + struct mii_bus *bus;
139 + struct device_node *dn;
140 +
141 + dn = of_find_matching_node(NULL, ls_poweroff_of_match);
142 + if (!dn)
143 + return -ENODEV;
144 + of_node_put(dn);
145 +
146 + dn = of_find_node_by_name(NULL, "mdio");
147 + if (!dn)
148 + return -ENODEV;
149 +
150 + bus = of_mdio_find_bus(dn);
151 + of_node_put(dn);
152 + if (!bus)
153 + return -EPROBE_DEFER;
154 +
155 + phydev = phy_find_first(bus);
156 + if (!phydev)
157 + return -EPROBE_DEFER;
158 +
159 + register_reboot_notifier(&linkstation_reboot_nb);
160 + pm_power_off = linkstation_poweroff;
161 +
162 + pr_info("LinkStation power off driver registered\n");
163 + return 0;
164 +}
165 +
166 +static void __exit linkstation_poweroff_exit(void)
167 +{
168 + pm_power_off = NULL;
169 + unregister_reboot_notifier(&linkstation_reboot_nb);
170 +}
171 +
172 +module_init(linkstation_poweroff_init);
173 +module_exit(linkstation_poweroff_exit);
174 +
175 +MODULE_AUTHOR("Daniel González Cabanelas <dgcbueu@gmail.com>");
176 +MODULE_DESCRIPTION("LinkStation power off driver");
177 +MODULE_LICENSE("GPL v2");