ubifs: Correctly use tnc_next() in search_dh_cookie()
[linux-stable.git] / drivers / clk / at91 / pmc.c
blob5c2b26de303e887c8c853e4e4aff206eca10a471
1 /*
2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 */
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
14 #include <linux/of.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/syscore_ops.h>
20 #include <asm/proc-fns.h>
22 #include "pmc.h"
24 #define PMC_MAX_IDS 128
26 int of_at91_get_clk_range(struct device_node *np, const char *propname,
27 struct clk_range *range)
29 u32 min, max;
30 int ret;
32 ret = of_property_read_u32_index(np, propname, 0, &min);
33 if (ret)
34 return ret;
36 ret = of_property_read_u32_index(np, propname, 1, &max);
37 if (ret)
38 return ret;
40 if (range) {
41 range->min = min;
42 range->max = max;
45 return 0;
47 EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
49 #ifdef CONFIG_PM
50 static struct regmap *pmcreg;
52 static u8 registered_ids[PMC_MAX_IDS];
54 static struct
56 u32 scsr;
57 u32 pcsr0;
58 u32 uckr;
59 u32 mor;
60 u32 mcfr;
61 u32 pllar;
62 u32 mckr;
63 u32 usb;
64 u32 imr;
65 u32 pcsr1;
66 u32 pcr[PMC_MAX_IDS];
67 u32 audio_pll0;
68 u32 audio_pll1;
69 } pmc_cache;
71 void pmc_register_id(u8 id)
73 int i;
75 for (i = 0; i < PMC_MAX_IDS; i++) {
76 if (registered_ids[i] == 0) {
77 registered_ids[i] = id;
78 break;
80 if (registered_ids[i] == id)
81 break;
85 static int pmc_suspend(void)
87 int i;
89 regmap_read(pmcreg, AT91_PMC_IMR, &pmc_cache.scsr);
90 regmap_read(pmcreg, AT91_PMC_PCSR, &pmc_cache.pcsr0);
91 regmap_read(pmcreg, AT91_CKGR_UCKR, &pmc_cache.uckr);
92 regmap_read(pmcreg, AT91_CKGR_MOR, &pmc_cache.mor);
93 regmap_read(pmcreg, AT91_CKGR_MCFR, &pmc_cache.mcfr);
94 regmap_read(pmcreg, AT91_CKGR_PLLAR, &pmc_cache.pllar);
95 regmap_read(pmcreg, AT91_PMC_MCKR, &pmc_cache.mckr);
96 regmap_read(pmcreg, AT91_PMC_USB, &pmc_cache.usb);
97 regmap_read(pmcreg, AT91_PMC_IMR, &pmc_cache.imr);
98 regmap_read(pmcreg, AT91_PMC_PCSR1, &pmc_cache.pcsr1);
100 for (i = 0; registered_ids[i]; i++) {
101 regmap_write(pmcreg, AT91_PMC_PCR,
102 (registered_ids[i] & AT91_PMC_PCR_PID_MASK));
103 regmap_read(pmcreg, AT91_PMC_PCR,
104 &pmc_cache.pcr[registered_ids[i]]);
107 return 0;
110 static bool pmc_ready(unsigned int mask)
112 unsigned int status;
114 regmap_read(pmcreg, AT91_PMC_SR, &status);
116 return ((status & mask) == mask) ? 1 : 0;
119 static void pmc_resume(void)
121 int i;
122 u32 tmp;
123 u32 mask = AT91_PMC_MCKRDY | AT91_PMC_LOCKA;
125 regmap_read(pmcreg, AT91_PMC_MCKR, &tmp);
126 if (pmc_cache.mckr != tmp)
127 pr_warn("MCKR was not configured properly by the firmware\n");
128 regmap_read(pmcreg, AT91_CKGR_PLLAR, &tmp);
129 if (pmc_cache.pllar != tmp)
130 pr_warn("PLLAR was not configured properly by the firmware\n");
132 regmap_write(pmcreg, AT91_PMC_IMR, pmc_cache.scsr);
133 regmap_write(pmcreg, AT91_PMC_PCER, pmc_cache.pcsr0);
134 regmap_write(pmcreg, AT91_CKGR_UCKR, pmc_cache.uckr);
135 regmap_write(pmcreg, AT91_CKGR_MOR, pmc_cache.mor);
136 regmap_write(pmcreg, AT91_CKGR_MCFR, pmc_cache.mcfr);
137 regmap_write(pmcreg, AT91_PMC_USB, pmc_cache.usb);
138 regmap_write(pmcreg, AT91_PMC_IMR, pmc_cache.imr);
139 regmap_write(pmcreg, AT91_PMC_PCER1, pmc_cache.pcsr1);
141 for (i = 0; registered_ids[i]; i++) {
142 regmap_write(pmcreg, AT91_PMC_PCR,
143 pmc_cache.pcr[registered_ids[i]] |
144 AT91_PMC_PCR_CMD);
147 if (pmc_cache.uckr & AT91_PMC_UPLLEN)
148 mask |= AT91_PMC_LOCKU;
150 while (!pmc_ready(mask))
151 cpu_relax();
154 static struct syscore_ops pmc_syscore_ops = {
155 .suspend = pmc_suspend,
156 .resume = pmc_resume,
159 static const struct of_device_id sama5d2_pmc_dt_ids[] = {
160 { .compatible = "atmel,sama5d2-pmc" },
161 { /* sentinel */ }
164 static int __init pmc_register_ops(void)
166 struct device_node *np;
168 np = of_find_matching_node(NULL, sama5d2_pmc_dt_ids);
170 pmcreg = syscon_node_to_regmap(np);
171 if (IS_ERR(pmcreg))
172 return PTR_ERR(pmcreg);
174 register_syscore_ops(&pmc_syscore_ops);
176 return 0;
178 /* This has to happen before arch_initcall because of the tcb_clksrc driver */
179 postcore_initcall(pmc_register_ops);
180 #endif