1 /*
2  * Clock driver for the ARM Integrator/IM-PD1 board
3  * Copyright (C) 2012-2013 Linus Walleij
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 #include <linux/clk-provider.h>
10 #include <linux/clk.h>
11 #include <linux/clkdev.h>
12 #include <linux/err.h>
13 #include <linux/io.h>
14 #include <linux/platform_data/clk-integrator.h>
15 
16 #include "clk-icst.h"
17 
18 #define IMPD1_OSC1	0x00
19 #define IMPD1_OSC2	0x04
20 #define IMPD1_LOCK	0x08
21 
22 struct impd1_clk {
23 	char *vco1name;
24 	struct clk *vco1clk;
25 	char *vco2name;
26 	struct clk *vco2clk;
27 	struct clk *mmciclk;
28 	char *uartname;
29 	struct clk *uartclk;
30 	char *spiname;
31 	struct clk *spiclk;
32 	char *scname;
33 	struct clk *scclk;
34 	struct clk_lookup *clks[6];
35 };
36 
37 /* One entry for each connected IM-PD1 LM */
38 static struct impd1_clk impd1_clks[4];
39 
40 /*
41  * There are two VCO's on the IM-PD1
42  */
43 
44 static const struct icst_params impd1_vco1_params = {
45 	.ref		= 24000000,	/* 24 MHz */
46 	.vco_max	= ICST525_VCO_MAX_3V,
47 	.vco_min	= ICST525_VCO_MIN,
48 	.vd_min		= 12,
49 	.vd_max		= 519,
50 	.rd_min		= 3,
51 	.rd_max		= 120,
52 	.s2div		= icst525_s2div,
53 	.idx2s		= icst525_idx2s,
54 };
55 
56 static const struct clk_icst_desc impd1_icst1_desc = {
57 	.params = &impd1_vco1_params,
58 	.vco_offset = IMPD1_OSC1,
59 	.lock_offset = IMPD1_LOCK,
60 };
61 
62 static const struct icst_params impd1_vco2_params = {
63 	.ref		= 24000000,	/* 24 MHz */
64 	.vco_max	= ICST525_VCO_MAX_3V,
65 	.vco_min	= ICST525_VCO_MIN,
66 	.vd_min		= 12,
67 	.vd_max		= 519,
68 	.rd_min		= 3,
69 	.rd_max		= 120,
70 	.s2div		= icst525_s2div,
71 	.idx2s		= icst525_idx2s,
72 };
73 
74 static const struct clk_icst_desc impd1_icst2_desc = {
75 	.params = &impd1_vco2_params,
76 	.vco_offset = IMPD1_OSC2,
77 	.lock_offset = IMPD1_LOCK,
78 };
79 
80 /**
81  * integrator_impd1_clk_init() - set up the integrator clock tree
82  * @base: base address of the logic module (LM)
83  * @id: the ID of this LM
84  */
85 void integrator_impd1_clk_init(void __iomem *base, unsigned int id)
86 {
87 	struct impd1_clk *imc;
88 	struct clk *clk;
89 	int i;
90 
91 	if (id > 3) {
92 		pr_crit("no more than 4 LMs can be attached\n");
93 		return;
94 	}
95 	imc = &impd1_clks[id];
96 
97 	imc->vco1name = kasprintf(GFP_KERNEL, "lm%x-vco1", id);
98 	clk = icst_clk_register(NULL, &impd1_icst1_desc, imc->vco1name, NULL,
99 				base);
100 	imc->vco1clk = clk;
101 	imc->clks[0] = clkdev_alloc(clk, NULL, "lm%x:01000", id);
102 
103 	/* VCO2 is also called "CLK2" */
104 	imc->vco2name = kasprintf(GFP_KERNEL, "lm%x-vco2", id);
105 	clk = icst_clk_register(NULL, &impd1_icst2_desc, imc->vco2name, NULL,
106 				base);
107 	imc->vco2clk = clk;
108 
109 	/* MMCI uses CLK2 right off */
110 	imc->clks[1] = clkdev_alloc(clk, NULL, "lm%x:00700", id);
111 
112 	/* UART reference clock divides CLK2 by a fixed factor 4 */
113 	imc->uartname = kasprintf(GFP_KERNEL, "lm%x-uartclk", id);
114 	clk = clk_register_fixed_factor(NULL, imc->uartname, imc->vco2name,
115 				   CLK_IGNORE_UNUSED, 1, 4);
116 	imc->uartclk = clk;
117 	imc->clks[2] = clkdev_alloc(clk, NULL, "lm%x:00100", id);
118 	imc->clks[3] = clkdev_alloc(clk, NULL, "lm%x:00200", id);
119 
120 	/* SPI PL022 clock divides CLK2 by a fixed factor 64 */
121 	imc->spiname = kasprintf(GFP_KERNEL, "lm%x-spiclk", id);
122 	clk = clk_register_fixed_factor(NULL, imc->spiname, imc->vco2name,
123 				   CLK_IGNORE_UNUSED, 1, 64);
124 	imc->clks[4] = clkdev_alloc(clk, NULL, "lm%x:00300", id);
125 
126 	/* Smart Card clock divides CLK2 by a fixed factor 4 */
127 	imc->scname = kasprintf(GFP_KERNEL, "lm%x-scclk", id);
128 	clk = clk_register_fixed_factor(NULL, imc->scname, imc->vco2name,
129 				   CLK_IGNORE_UNUSED, 1, 4);
130 	imc->scclk = clk;
131 	imc->clks[5] = clkdev_alloc(clk, NULL, "lm%x:00600", id);
132 
133 	for (i = 0; i < ARRAY_SIZE(imc->clks); i++)
134 		clkdev_add(imc->clks[i]);
135 }
136 
137 void integrator_impd1_clk_exit(unsigned int id)
138 {
139 	int i;
140 	struct impd1_clk *imc;
141 
142 	if (id > 3)
143 		return;
144 	imc = &impd1_clks[id];
145 
146 	for (i = 0; i < ARRAY_SIZE(imc->clks); i++)
147 		clkdev_drop(imc->clks[i]);
148 	clk_unregister(imc->spiclk);
149 	clk_unregister(imc->uartclk);
150 	clk_unregister(imc->vco2clk);
151 	clk_unregister(imc->vco1clk);
152 	kfree(imc->scname);
153 	kfree(imc->spiname);
154 	kfree(imc->uartname);
155 	kfree(imc->vco2name);
156 	kfree(imc->vco1name);
157 }
158