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