1 // SPDX-License-Identifier: GPL-2.0+
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
6 * based on the gdsys osd driver, which is
9 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
14 #include <clk-uclass.h>
17 const long long ICS8N3QV01_FREF = 114285000;
18 const long long ICS8N3QV01_FREF_LL = 114285000LL;
19 const long long ICS8N3QV01_F_DEFAULT_0 = 156250000LL;
20 const long long ICS8N3QV01_F_DEFAULT_1 = 125000000LL;
21 const long long ICS8N3QV01_F_DEFAULT_2 = 100000000LL;
22 const long long ICS8N3QV01_F_DEFAULT_3 = 25175000LL;
24 const uint MAX_FREQ_INDEX = 3;
26 struct ics8n3qv01_priv {
30 static int ics8n3qv01_get_fout_calc(struct udevice *dev, uint index,
34 u8 reg_a, reg_b, reg_c, reg_d, reg_f;
38 if (index > MAX_FREQ_INDEX)
41 for (i = 0; i <= 5; ++i) {
42 u8 tmp = dm_i2c_reg_read(dev, 4 * i + index);
45 debug("%s: Error while reading i2c register %d.\n",
46 dev->name, 4 * i + index);
53 reg_a = val[0]; /* Register 0 + index */
54 reg_b = val[1]; /* Register 4 + index */
55 reg_c = val[2]; /* Register 8 + index */
56 reg_d = val[3]; /* Register 12 + index */
57 reg_f = val[5]; /* Register 20 + index */
59 mint = ((reg_a >> 1) & 0x1f) | /* MINTi[4-0]*/
60 (reg_f & 0x20); /* MINTi[5] */
61 mfrac = ((reg_a & 0x01) << 17) | /* MFRACi[17] */
62 (reg_b << 9) | /* MFRACi[16-9] */
63 (reg_c << 1) | /* MFRACi[8-1] */
64 (reg_d >> 7); /* MFRACi[0] */
65 n = reg_d & 0x7f; /* Ni[6-0] */
67 *fout_calc = (mint * ICS8N3QV01_FREF_LL
68 + mfrac * ICS8N3QV01_FREF_LL / 262144LL
69 + ICS8N3QV01_FREF_LL / 524288LL
78 static int ics8n3qv01_calc_parameters(uint fout, uint *_mint, uint *_mfrac,
81 uint n, foutiic, fvcoiic, mint;
84 n = (2215000000U + fout / 2) / fout;
85 if (fout < 417000000U)
86 n = 2 * ((2215000000U / 2 + fout / 2) / fout);
88 n = (2215000000U + fout / 2) / fout;
93 foutiic = fout - (fout / 10000);
94 fvcoiic = foutiic * n;
96 mint = fvcoiic / 114285000;
97 if (mint < 17 || mint > 63)
100 mfrac = ((u64)fvcoiic % 114285000LL) * 262144LL
110 static ulong ics8n3qv01_set_rate(struct clk *clk, ulong fout)
112 struct ics8n3qv01_priv *priv = dev_get_priv(clk->dev);
120 int addr[] = {0, 4, 8, 12, 18, 20};
124 res = ics8n3qv01_get_fout_calc(clk->dev, 1, &fout_calc);
127 debug("%s: Error during output frequency calculation.\n",
132 off_ppm = (fout_calc - ICS8N3QV01_F_DEFAULT_1) * 1000000
133 / ICS8N3QV01_F_DEFAULT_1;
134 printf("%s: PLL is off by %lld ppm\n", clk->dev->name, off_ppm);
135 fout_prog = (u64)fout * (u64)fout_calc
136 / ICS8N3QV01_F_DEFAULT_1;
137 res = ics8n3qv01_calc_parameters(fout_prog, &mint, &mfrac, &n);
140 debug("%s: Cannot determine mint parameter.\n",
146 tmp = dm_i2c_reg_read(clk->dev, 0) & 0xc0;
149 reg[0] = tmp | (mint & 0x1f) << 1;
150 reg[0] |= (mfrac >> 17) & 0x01;
163 tmp = dm_i2c_reg_read(clk->dev, 18) & 0x03;
169 tmp = dm_i2c_reg_read(clk->dev, 20) & 0x1f;
172 reg[5] = tmp | (mint & (1 << 5));
174 for (i = 0; i <= 5; ++i) {
175 res = dm_i2c_reg_write(clk->dev, addr[i], reg[i]);
183 static int ics8n3qv01_request(struct clk *clock)
188 static ulong ics8n3qv01_get_rate(struct clk *clk)
190 struct ics8n3qv01_priv *priv = dev_get_priv(clk->dev);
195 static int ics8n3qv01_enable(struct clk *clk)
200 static int ics8n3qv01_disable(struct clk *clk)
205 static const struct clk_ops ics8n3qv01_ops = {
206 .request = ics8n3qv01_request,
207 .get_rate = ics8n3qv01_get_rate,
208 .set_rate = ics8n3qv01_set_rate,
209 .enable = ics8n3qv01_enable,
210 .disable = ics8n3qv01_disable,
213 static const struct udevice_id ics8n3qv01_ids[] = {
214 { .compatible = "idt,ics8n3qv01" },
218 int ics8n3qv01_probe(struct udevice *dev)
223 U_BOOT_DRIVER(ics8n3qv01) = {
224 .name = "ics8n3qv01",
226 .ops = &ics8n3qv01_ops,
227 .of_match = ics8n3qv01_ids,
228 .probe = ics8n3qv01_probe,
229 .priv_auto_alloc_size = sizeof(struct ics8n3qv01_priv),