dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / power / reset / qnap-poweroff.c
blob2789a61cec68aef32e689e6d2540050a4bc35f53
1 /*
2 * QNAP Turbo NAS Board power off. Can also be used on Synology devices.
4 * Copyright (C) 2012 Andrew Lunn <andrew@lunn.ch>
6 * Based on the code from:
8 * Copyright (C) 2009 Martin Michlmayr <tbm@cyrius.com>
9 * Copyright (C) 2008 Byron Bradley <byron.bbradley@gmail.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/serial_reg.h>
21 #include <linux/kallsyms.h>
22 #include <linux/of.h>
23 #include <linux/io.h>
24 #include <linux/clk.h>
26 #define UART1_REG(x) (base + ((UART_##x) << 2))
28 struct power_off_cfg {
29 u32 baud;
30 char cmd;
33 static const struct power_off_cfg qnap_power_off_cfg = {
34 .baud = 19200,
35 .cmd = 'A',
38 static const struct power_off_cfg synology_power_off_cfg = {
39 .baud = 9600,
40 .cmd = '1',
43 static const struct of_device_id qnap_power_off_of_match_table[] = {
44 { .compatible = "qnap,power-off",
45 .data = &qnap_power_off_cfg,
47 { .compatible = "synology,power-off",
48 .data = &synology_power_off_cfg,
52 MODULE_DEVICE_TABLE(of, qnap_power_off_of_match_table);
54 static void __iomem *base;
55 static unsigned long tclk;
56 static const struct power_off_cfg *cfg;
58 static void qnap_power_off(void)
60 const unsigned divisor = ((tclk + (8 * cfg->baud)) / (16 * cfg->baud));
62 pr_err("%s: triggering power-off...\n", __func__);
64 /* hijack UART1 and reset into sane state */
65 writel(0x83, UART1_REG(LCR));
66 writel(divisor & 0xff, UART1_REG(DLL));
67 writel((divisor >> 8) & 0xff, UART1_REG(DLM));
68 writel(0x03, UART1_REG(LCR));
69 writel(0x00, UART1_REG(IER));
70 writel(0x00, UART1_REG(FCR));
71 writel(0x00, UART1_REG(MCR));
73 /* send the power-off command to PIC */
74 writel(cfg->cmd, UART1_REG(TX));
77 static int qnap_power_off_probe(struct platform_device *pdev)
79 struct device_node *np = pdev->dev.of_node;
80 struct resource *res;
81 struct clk *clk;
82 char symname[KSYM_NAME_LEN];
84 const struct of_device_id *match =
85 of_match_node(qnap_power_off_of_match_table, np);
86 cfg = match->data;
88 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
89 if (!res) {
90 dev_err(&pdev->dev, "Missing resource");
91 return -EINVAL;
94 base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
95 if (!base) {
96 dev_err(&pdev->dev, "Unable to map resource");
97 return -EINVAL;
100 /* We need to know tclk in order to calculate the UART divisor */
101 clk = devm_clk_get(&pdev->dev, NULL);
102 if (IS_ERR(clk)) {
103 dev_err(&pdev->dev, "Clk missing");
104 return PTR_ERR(clk);
107 tclk = clk_get_rate(clk);
109 /* Check that nothing else has already setup a handler */
110 if (pm_power_off) {
111 lookup_symbol_name((ulong)pm_power_off, symname);
112 dev_err(&pdev->dev,
113 "pm_power_off already claimed %p %s",
114 pm_power_off, symname);
115 return -EBUSY;
117 pm_power_off = qnap_power_off;
119 return 0;
122 static int qnap_power_off_remove(struct platform_device *pdev)
124 pm_power_off = NULL;
125 return 0;
128 static struct platform_driver qnap_power_off_driver = {
129 .probe = qnap_power_off_probe,
130 .remove = qnap_power_off_remove,
131 .driver = {
132 .name = "qnap_power_off",
133 .of_match_table = of_match_ptr(qnap_power_off_of_match_table),
136 module_platform_driver(qnap_power_off_driver);
138 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
139 MODULE_DESCRIPTION("QNAP Power off driver");
140 MODULE_LICENSE("GPL v2");