blob: 4f80bf6e5223c2e6bb3b106855b94f4a77453937 [file] [log] [blame]
Mario Sixf0bcbe62018-04-27 14:53:15 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2017
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 *
6 * based on the gdsys osd driver, which is
7 *
8 * (C) Copyright 2010
9 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
10 */
11
12#include <common.h>
13#include <dm.h>
14#include <clk-uclass.h>
15#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Mario Sixf0bcbe62018-04-27 14:53:15 +020017
18const long long ICS8N3QV01_FREF = 114285000;
19const long long ICS8N3QV01_FREF_LL = 114285000LL;
20const long long ICS8N3QV01_F_DEFAULT_0 = 156250000LL;
21const long long ICS8N3QV01_F_DEFAULT_1 = 125000000LL;
22const long long ICS8N3QV01_F_DEFAULT_2 = 100000000LL;
23const long long ICS8N3QV01_F_DEFAULT_3 = 25175000LL;
24
25const uint MAX_FREQ_INDEX = 3;
26
27struct ics8n3qv01_priv {
28 ulong rate;
29};
30
31static int ics8n3qv01_get_fout_calc(struct udevice *dev, uint index,
32 uint *fout_calc)
33{
34 u64 n, mint, mfrac;
35 u8 reg_a, reg_b, reg_c, reg_d, reg_f;
36 int val[6];
37 int i;
38
39 if (index > MAX_FREQ_INDEX)
40 return -EINVAL;
41
42 for (i = 0; i <= 5; ++i) {
43 u8 tmp = dm_i2c_reg_read(dev, 4 * i + index);
44
45 if (tmp < 0) {
46 debug("%s: Error while reading i2c register %d.\n",
47 dev->name, 4 * i + index);
48 return tmp;
49 }
50
51 val[i] = tmp;
52 }
53
54 reg_a = val[0]; /* Register 0 + index */
55 reg_b = val[1]; /* Register 4 + index */
56 reg_c = val[2]; /* Register 8 + index */
57 reg_d = val[3]; /* Register 12 + index */
58 reg_f = val[5]; /* Register 20 + index */
59
60 mint = ((reg_a >> 1) & 0x1f) | /* MINTi[4-0]*/
61 (reg_f & 0x20); /* MINTi[5] */
62 mfrac = ((reg_a & 0x01) << 17) | /* MFRACi[17] */
63 (reg_b << 9) | /* MFRACi[16-9] */
64 (reg_c << 1) | /* MFRACi[8-1] */
65 (reg_d >> 7); /* MFRACi[0] */
66 n = reg_d & 0x7f; /* Ni[6-0] */
67
68 *fout_calc = (mint * ICS8N3QV01_FREF_LL
69 + mfrac * ICS8N3QV01_FREF_LL / 262144LL
70 + ICS8N3QV01_FREF_LL / 524288LL
71 + n / 2)
72 / n
73 * 1000000
74 / (1000000 - 100);
75
76 return 0;
77}
78
79static int ics8n3qv01_calc_parameters(uint fout, uint *_mint, uint *_mfrac,
80 uint *_n)
81{
82 uint n, foutiic, fvcoiic, mint;
83 u64 mfrac;
84
85 n = (2215000000U + fout / 2) / fout;
86 if (fout < 417000000U)
87 n = 2 * ((2215000000U / 2 + fout / 2) / fout);
88 else
89 n = (2215000000U + fout / 2) / fout;
90
91 if ((n & 1) && n > 5)
92 n -= 1;
93
94 foutiic = fout - (fout / 10000);
95 fvcoiic = foutiic * n;
96
97 mint = fvcoiic / 114285000;
98 if (mint < 17 || mint > 63)
99 return -EINVAL;
100
101 mfrac = ((u64)fvcoiic % 114285000LL) * 262144LL
102 / 114285000LL;
103
104 *_mint = mint;
105 *_mfrac = mfrac;
106 *_n = n;
107
108 return 0;
109}
110
111static ulong ics8n3qv01_set_rate(struct clk *clk, ulong fout)
112{
113 struct ics8n3qv01_priv *priv = dev_get_priv(clk->dev);
114 uint n, mint, mfrac;
115 uint fout_calc = 0;
116 u64 fout_prog;
117 long long off_ppm;
118 int res, i;
119 u8 reg[6];
120 int tmp;
121 int addr[] = {0, 4, 8, 12, 18, 20};
122
123 priv->rate = fout;
124
125 res = ics8n3qv01_get_fout_calc(clk->dev, 1, &fout_calc);
126
127 if (res) {
128 debug("%s: Error during output frequency calculation.\n",
129 clk->dev->name);
130 return res;
131 }
132
133 off_ppm = (fout_calc - ICS8N3QV01_F_DEFAULT_1) * 1000000
134 / ICS8N3QV01_F_DEFAULT_1;
135 printf("%s: PLL is off by %lld ppm\n", clk->dev->name, off_ppm);
136 fout_prog = (u64)fout * (u64)fout_calc
137 / ICS8N3QV01_F_DEFAULT_1;
138 res = ics8n3qv01_calc_parameters(fout_prog, &mint, &mfrac, &n);
139
140 if (res) {
141 debug("%s: Cannot determine mint parameter.\n",
142 clk->dev->name);
143 return res;
144 }
145
146 /* Register 0 */
147 tmp = dm_i2c_reg_read(clk->dev, 0) & 0xc0;
148 if (tmp < 0)
149 return tmp;
150 reg[0] = tmp | (mint & 0x1f) << 1;
151 reg[0] |= (mfrac >> 17) & 0x01;
152
153 /* Register 4 */
154 reg[1] = mfrac >> 9;
155
156 /* Register 8 */
157 reg[2] = mfrac >> 1;
158
159 /* Register 12 */
160 reg[3] = mfrac << 7;
161 reg[3] |= n & 0x7f;
162
163 /* Register 18 */
164 tmp = dm_i2c_reg_read(clk->dev, 18) & 0x03;
165 if (tmp < 0)
166 return tmp;
167 reg[4] = tmp | 0x20;
168
169 /* Register 20 */
170 tmp = dm_i2c_reg_read(clk->dev, 20) & 0x1f;
171 if (tmp < 0)
172 return tmp;
173 reg[5] = tmp | (mint & (1 << 5));
174
175 for (i = 0; i <= 5; ++i) {
176 res = dm_i2c_reg_write(clk->dev, addr[i], reg[i]);
177 if (res < 0)
178 return res;
179 }
180
181 return 0;
182}
183
184static int ics8n3qv01_request(struct clk *clock)
185{
186 return 0;
187}
188
189static ulong ics8n3qv01_get_rate(struct clk *clk)
190{
191 struct ics8n3qv01_priv *priv = dev_get_priv(clk->dev);
192
193 return priv->rate;
194}
195
196static int ics8n3qv01_enable(struct clk *clk)
197{
198 return 0;
199}
200
201static int ics8n3qv01_disable(struct clk *clk)
202{
203 return 0;
204}
205
206static const struct clk_ops ics8n3qv01_ops = {
207 .request = ics8n3qv01_request,
208 .get_rate = ics8n3qv01_get_rate,
209 .set_rate = ics8n3qv01_set_rate,
210 .enable = ics8n3qv01_enable,
211 .disable = ics8n3qv01_disable,
212};
213
214static const struct udevice_id ics8n3qv01_ids[] = {
215 { .compatible = "idt,ics8n3qv01" },
216 { /* sentinel */ }
217};
218
219int ics8n3qv01_probe(struct udevice *dev)
220{
221 return 0;
222}
223
224U_BOOT_DRIVER(ics8n3qv01) = {
225 .name = "ics8n3qv01",
226 .id = UCLASS_CLK,
227 .ops = &ics8n3qv01_ops,
228 .of_match = ics8n3qv01_ids,
229 .probe = ics8n3qv01_probe,
230 .priv_auto_alloc_size = sizeof(struct ics8n3qv01_priv),
231};