blob: cae07267a884f5233582382fc2d0499e1cca357b [file] [log] [blame]
Marek Vasut3e1aec4e2017-01-12 02:03:24 +01001/*
2 * Driver for IDT Versaclock 5
3 *
4 * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17/*
18 * Possible optimizations:
19 * - Use spread spectrum
20 * - Use integer divider in FOD if applicable
21 */
22
23#include <linux/clk.h>
24#include <linux/clk-provider.h>
25#include <linux/delay.h>
26#include <linux/i2c.h>
27#include <linux/interrupt.h>
28#include <linux/mod_devicetable.h>
29#include <linux/module.h>
30#include <linux/of.h>
31#include <linux/of_platform.h>
32#include <linux/rational.h>
33#include <linux/regmap.h>
34#include <linux/slab.h>
35
36/* VersaClock5 registers */
37#define VC5_OTP_CONTROL 0x00
38
39/* Factory-reserved register block */
40#define VC5_RSVD_DEVICE_ID 0x01
41#define VC5_RSVD_ADC_GAIN_7_0 0x02
42#define VC5_RSVD_ADC_GAIN_15_8 0x03
43#define VC5_RSVD_ADC_OFFSET_7_0 0x04
44#define VC5_RSVD_ADC_OFFSET_15_8 0x05
45#define VC5_RSVD_TEMPY 0x06
46#define VC5_RSVD_OFFSET_TBIN 0x07
47#define VC5_RSVD_GAIN 0x08
48#define VC5_RSVD_TEST_NP 0x09
49#define VC5_RSVD_UNUSED 0x0a
50#define VC5_RSVD_BANDGAP_TRIM_UP 0x0b
51#define VC5_RSVD_BANDGAP_TRIM_DN 0x0c
52#define VC5_RSVD_CLK_R_12_CLK_AMP_4 0x0d
53#define VC5_RSVD_CLK_R_34_CLK_AMP_4 0x0e
54#define VC5_RSVD_CLK_AMP_123 0x0f
55
56/* Configuration register block */
57#define VC5_PRIM_SRC_SHDN 0x10
58#define VC5_PRIM_SRC_SHDN_EN_XTAL BIT(7)
59#define VC5_PRIM_SRC_SHDN_EN_CLKIN BIT(6)
60#define VC5_PRIM_SRC_SHDN_SP BIT(1)
61#define VC5_PRIM_SRC_SHDN_EN_GBL_SHDN BIT(0)
62
63#define VC5_VCO_BAND 0x11
64#define VC5_XTAL_X1_LOAD_CAP 0x12
65#define VC5_XTAL_X2_LOAD_CAP 0x13
66#define VC5_REF_DIVIDER 0x15
67#define VC5_REF_DIVIDER_SEL_PREDIV2 BIT(7)
68#define VC5_REF_DIVIDER_REF_DIV(n) ((n) & 0x3f)
69
70#define VC5_VCO_CTRL_AND_PREDIV 0x16
71#define VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV BIT(7)
72
73#define VC5_FEEDBACK_INT_DIV 0x17
74#define VC5_FEEDBACK_INT_DIV_BITS 0x18
75#define VC5_FEEDBACK_FRAC_DIV(n) (0x19 + (n))
76#define VC5_RC_CONTROL0 0x1e
77#define VC5_RC_CONTROL1 0x1f
78/* Register 0x20 is factory reserved */
79
80/* Output divider control for divider 1,2,3,4 */
81#define VC5_OUT_DIV_CONTROL(idx) (0x21 + ((idx) * 0x10))
82#define VC5_OUT_DIV_CONTROL_RESET BIT(7)
83#define VC5_OUT_DIV_CONTROL_SELB_NORM BIT(3)
84#define VC5_OUT_DIV_CONTROL_SEL_EXT BIT(2)
85#define VC5_OUT_DIV_CONTROL_INT_MODE BIT(1)
86#define VC5_OUT_DIV_CONTROL_EN_FOD BIT(0)
87
88#define VC5_OUT_DIV_FRAC(idx, n) (0x22 + ((idx) * 0x10) + (n))
89#define VC5_OUT_DIV_FRAC4_OD_SCEE BIT(1)
90
91#define VC5_OUT_DIV_STEP_SPREAD(idx, n) (0x26 + ((idx) * 0x10) + (n))
92#define VC5_OUT_DIV_SPREAD_MOD(idx, n) (0x29 + ((idx) * 0x10) + (n))
93#define VC5_OUT_DIV_SKEW_INT(idx, n) (0x2b + ((idx) * 0x10) + (n))
94#define VC5_OUT_DIV_INT(idx, n) (0x2d + ((idx) * 0x10) + (n))
95#define VC5_OUT_DIV_SKEW_FRAC(idx) (0x2f + ((idx) * 0x10))
96/* Registers 0x30, 0x40, 0x50 are factory reserved */
97
98/* Clock control register for clock 1,2 */
99#define VC5_CLK_OUTPUT_CFG(idx, n) (0x60 + ((idx) * 0x2) + (n))
100#define VC5_CLK_OUTPUT_CFG1_EN_CLKBUF BIT(0)
101
102#define VC5_CLK_OE_SHDN 0x68
103#define VC5_CLK_OS_SHDN 0x69
104
105#define VC5_GLOBAL_REGISTER 0x76
106#define VC5_GLOBAL_REGISTER_GLOBAL_RESET BIT(5)
107
108/* PLL/VCO runs between 2.5 GHz and 3.0 GHz */
109#define VC5_PLL_VCO_MIN 2500000000UL
110#define VC5_PLL_VCO_MAX 3000000000UL
111
112/* VC5 Input mux settings */
113#define VC5_MUX_IN_XIN BIT(0)
114#define VC5_MUX_IN_CLKIN BIT(1)
115
Alexey Firago9adddb02017-04-07 12:12:22 +0300116/* Maximum number of clk_out supported by this driver */
Alexey Firago1193e142017-04-07 12:12:24 +0300117#define VC5_MAX_CLK_OUT_NUM 5
Alexey Firago9adddb02017-04-07 12:12:22 +0300118
119/* Maximum number of FODs supported by this driver */
Alexey Firago1193e142017-04-07 12:12:24 +0300120#define VC5_MAX_FOD_NUM 4
Alexey Firago9adddb02017-04-07 12:12:22 +0300121
122/* flags to describe chip features */
123/* chip has built-in oscilator */
124#define VC5_HAS_INTERNAL_XTAL BIT(0)
125
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100126/* Supported IDT VC5 models. */
127enum vc5_model {
128 IDT_VC5_5P49V5923,
129 IDT_VC5_5P49V5933,
Alexey Firago1193e142017-04-07 12:12:24 +0300130 IDT_VC5_5P49V5935,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100131};
132
Alexey Firago9adddb02017-04-07 12:12:22 +0300133/* Structure to describe features of a particular VC5 model */
134struct vc5_chip_info {
135 const enum vc5_model model;
136 const unsigned int clk_fod_cnt;
137 const unsigned int clk_out_cnt;
138 const u32 flags;
139};
140
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100141struct vc5_driver_data;
142
143struct vc5_hw_data {
144 struct clk_hw hw;
145 struct vc5_driver_data *vc5;
146 u32 div_int;
147 u32 div_frc;
148 unsigned int num;
149};
150
151struct vc5_driver_data {
152 struct i2c_client *client;
153 struct regmap *regmap;
Alexey Firago9adddb02017-04-07 12:12:22 +0300154 const struct vc5_chip_info *chip_info;
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100155
156 struct clk *pin_xin;
157 struct clk *pin_clkin;
158 unsigned char clk_mux_ins;
159 struct clk_hw clk_mux;
Marek Vasut55997db2017-07-09 15:28:11 +0200160 struct clk_hw clk_pfd;
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100161 struct vc5_hw_data clk_pll;
Alexey Firago9adddb02017-04-07 12:12:22 +0300162 struct vc5_hw_data clk_fod[VC5_MAX_FOD_NUM];
163 struct vc5_hw_data clk_out[VC5_MAX_CLK_OUT_NUM];
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100164};
165
166static const char * const vc5_mux_names[] = {
167 "mux"
168};
169
Marek Vasut55997db2017-07-09 15:28:11 +0200170static const char * const vc5_pfd_names[] = {
171 "pfd"
172};
173
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100174static const char * const vc5_pll_names[] = {
175 "pll"
176};
177
178static const char * const vc5_fod_names[] = {
179 "fod0", "fod1", "fod2", "fod3",
180};
181
182static const char * const vc5_clk_out_names[] = {
183 "out0_sel_i2cb", "out1", "out2", "out3", "out4",
184};
185
186/*
187 * VersaClock5 i2c regmap
188 */
189static bool vc5_regmap_is_writeable(struct device *dev, unsigned int reg)
190{
191 /* Factory reserved regs, make them read-only */
192 if (reg <= 0xf)
193 return false;
194
195 /* Factory reserved regs, make them read-only */
196 if (reg == 0x14 || reg == 0x1c || reg == 0x1d)
197 return false;
198
199 return true;
200}
201
202static const struct regmap_config vc5_regmap_config = {
203 .reg_bits = 8,
204 .val_bits = 8,
205 .cache_type = REGCACHE_RBTREE,
206 .max_register = 0x76,
207 .writeable_reg = vc5_regmap_is_writeable,
208};
209
210/*
211 * VersaClock5 input multiplexer between XTAL and CLKIN divider
212 */
213static unsigned char vc5_mux_get_parent(struct clk_hw *hw)
214{
215 struct vc5_driver_data *vc5 =
216 container_of(hw, struct vc5_driver_data, clk_mux);
217 const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
218 unsigned int src;
219
220 regmap_read(vc5->regmap, VC5_PRIM_SRC_SHDN, &src);
221 src &= mask;
222
223 if (src == VC5_PRIM_SRC_SHDN_EN_XTAL)
224 return 0;
225
226 if (src == VC5_PRIM_SRC_SHDN_EN_CLKIN)
227 return 1;
228
229 dev_warn(&vc5->client->dev,
230 "Invalid clock input configuration (%02x)\n", src);
231 return 0;
232}
233
234static int vc5_mux_set_parent(struct clk_hw *hw, u8 index)
235{
236 struct vc5_driver_data *vc5 =
237 container_of(hw, struct vc5_driver_data, clk_mux);
238 const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
239 u8 src;
240
241 if ((index > 1) || !vc5->clk_mux_ins)
242 return -EINVAL;
243
244 if (vc5->clk_mux_ins == (VC5_MUX_IN_CLKIN | VC5_MUX_IN_XIN)) {
245 if (index == 0)
246 src = VC5_PRIM_SRC_SHDN_EN_XTAL;
247 if (index == 1)
248 src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
249 } else {
250 if (index != 0)
251 return -EINVAL;
252
253 if (vc5->clk_mux_ins == VC5_MUX_IN_XIN)
254 src = VC5_PRIM_SRC_SHDN_EN_XTAL;
255 if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
256 src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
257 }
258
259 return regmap_update_bits(vc5->regmap, VC5_PRIM_SRC_SHDN, mask, src);
260}
261
Marek Vasut55997db2017-07-09 15:28:11 +0200262static const struct clk_ops vc5_mux_ops = {
263 .set_parent = vc5_mux_set_parent,
264 .get_parent = vc5_mux_get_parent,
265};
266
267static unsigned long vc5_pfd_recalc_rate(struct clk_hw *hw,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100268 unsigned long parent_rate)
269{
270 struct vc5_driver_data *vc5 =
Marek Vasut55997db2017-07-09 15:28:11 +0200271 container_of(hw, struct vc5_driver_data, clk_pfd);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100272 unsigned int prediv, div;
273
274 regmap_read(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV, &prediv);
275
276 /* The bypass_prediv is set, PLL fed from Ref_in directly. */
277 if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
278 return parent_rate;
279
280 regmap_read(vc5->regmap, VC5_REF_DIVIDER, &div);
281
282 /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
283 if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
284 return parent_rate / 2;
285 else
286 return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
287}
288
Marek Vasut55997db2017-07-09 15:28:11 +0200289static long vc5_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100290 unsigned long *parent_rate)
291{
292 unsigned long idiv;
293
294 /* PLL cannot operate with input clock above 50 MHz. */
295 if (rate > 50000000)
296 return -EINVAL;
297
298 /* CLKIN within range of PLL input, feed directly to PLL. */
299 if (*parent_rate <= 50000000)
300 return *parent_rate;
301
302 idiv = DIV_ROUND_UP(*parent_rate, rate);
303 if (idiv > 127)
304 return -EINVAL;
305
306 return *parent_rate / idiv;
307}
308
Marek Vasut55997db2017-07-09 15:28:11 +0200309static int vc5_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100310 unsigned long parent_rate)
311{
312 struct vc5_driver_data *vc5 =
Marek Vasut55997db2017-07-09 15:28:11 +0200313 container_of(hw, struct vc5_driver_data, clk_pfd);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100314 unsigned long idiv;
315 u8 div;
316
317 /* CLKIN within range of PLL input, feed directly to PLL. */
318 if (parent_rate <= 50000000) {
319 regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
320 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV,
321 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
322 regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, 0x00);
323 return 0;
324 }
325
326 idiv = DIV_ROUND_UP(parent_rate, rate);
327
328 /* We have dedicated div-2 predivider. */
329 if (idiv == 2)
330 div = VC5_REF_DIVIDER_SEL_PREDIV2;
331 else
332 div = VC5_REF_DIVIDER_REF_DIV(idiv);
333
334 regmap_update_bits(vc5->regmap, VC5_REF_DIVIDER, 0xff, div);
335 regmap_update_bits(vc5->regmap, VC5_VCO_CTRL_AND_PREDIV,
336 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV, 0);
337
338 return 0;
339}
340
Marek Vasut55997db2017-07-09 15:28:11 +0200341static const struct clk_ops vc5_pfd_ops = {
342 .recalc_rate = vc5_pfd_recalc_rate,
343 .round_rate = vc5_pfd_round_rate,
344 .set_rate = vc5_pfd_set_rate,
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100345};
346
347/*
348 * VersaClock5 PLL/VCO
349 */
350static unsigned long vc5_pll_recalc_rate(struct clk_hw *hw,
351 unsigned long parent_rate)
352{
353 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
354 struct vc5_driver_data *vc5 = hwdata->vc5;
355 u32 div_int, div_frc;
356 u8 fb[5];
357
358 regmap_bulk_read(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
359
360 div_int = (fb[0] << 4) | (fb[1] >> 4);
361 div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
362
363 /* The PLL divider has 12 integer bits and 24 fractional bits */
364 return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
365}
366
367static long vc5_pll_round_rate(struct clk_hw *hw, unsigned long rate,
368 unsigned long *parent_rate)
369{
370 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
371 u32 div_int;
372 u64 div_frc;
373
374 if (rate < VC5_PLL_VCO_MIN)
375 rate = VC5_PLL_VCO_MIN;
376 if (rate > VC5_PLL_VCO_MAX)
377 rate = VC5_PLL_VCO_MAX;
378
379 /* Determine integer part, which is 12 bit wide */
380 div_int = rate / *parent_rate;
381 if (div_int > 0xfff)
382 rate = *parent_rate * 0xfff;
383
384 /* Determine best fractional part, which is 24 bit wide */
385 div_frc = rate % *parent_rate;
386 div_frc *= BIT(24) - 1;
387 do_div(div_frc, *parent_rate);
388
389 hwdata->div_int = div_int;
390 hwdata->div_frc = (u32)div_frc;
391
392 return (*parent_rate * div_int) + ((*parent_rate * div_frc) >> 24);
393}
394
395static int vc5_pll_set_rate(struct clk_hw *hw, unsigned long rate,
396 unsigned long parent_rate)
397{
398 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
399 struct vc5_driver_data *vc5 = hwdata->vc5;
400 u8 fb[5];
401
402 fb[0] = hwdata->div_int >> 4;
403 fb[1] = hwdata->div_int << 4;
404 fb[2] = hwdata->div_frc >> 16;
405 fb[3] = hwdata->div_frc >> 8;
406 fb[4] = hwdata->div_frc;
407
408 return regmap_bulk_write(vc5->regmap, VC5_FEEDBACK_INT_DIV, fb, 5);
409}
410
411static const struct clk_ops vc5_pll_ops = {
412 .recalc_rate = vc5_pll_recalc_rate,
413 .round_rate = vc5_pll_round_rate,
414 .set_rate = vc5_pll_set_rate,
415};
416
417static unsigned long vc5_fod_recalc_rate(struct clk_hw *hw,
418 unsigned long parent_rate)
419{
420 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
421 struct vc5_driver_data *vc5 = hwdata->vc5;
422 /* VCO frequency is divided by two before entering FOD */
423 u32 f_in = parent_rate / 2;
424 u32 div_int, div_frc;
425 u8 od_int[2];
426 u8 od_frc[4];
427
428 regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_INT(hwdata->num, 0),
429 od_int, 2);
430 regmap_bulk_read(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
431 od_frc, 4);
432
433 div_int = (od_int[0] << 4) | (od_int[1] >> 4);
434 div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
435 (od_frc[2] << 6) | (od_frc[3] >> 2);
436
Marek Vasut3bded562017-07-09 15:28:07 +0200437 /* Avoid division by zero if the output is not configured. */
438 if (div_int == 0 && div_frc == 0)
439 return 0;
440
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100441 /* The PLL divider has 12 integer bits and 30 fractional bits */
442 return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
443}
444
445static long vc5_fod_round_rate(struct clk_hw *hw, unsigned long rate,
446 unsigned long *parent_rate)
447{
448 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
449 /* VCO frequency is divided by two before entering FOD */
450 u32 f_in = *parent_rate / 2;
451 u32 div_int;
452 u64 div_frc;
453
454 /* Determine integer part, which is 12 bit wide */
455 div_int = f_in / rate;
456 /*
457 * WARNING: The clock chip does not output signal if the integer part
458 * of the divider is 0xfff and fractional part is non-zero.
459 * Clamp the divider at 0xffe to keep the code simple.
460 */
461 if (div_int > 0xffe) {
462 div_int = 0xffe;
463 rate = f_in / div_int;
464 }
465
466 /* Determine best fractional part, which is 30 bit wide */
467 div_frc = f_in % rate;
468 div_frc <<= 24;
469 do_div(div_frc, rate);
470
471 hwdata->div_int = div_int;
472 hwdata->div_frc = (u32)div_frc;
473
474 return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
475}
476
477static int vc5_fod_set_rate(struct clk_hw *hw, unsigned long rate,
478 unsigned long parent_rate)
479{
480 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
481 struct vc5_driver_data *vc5 = hwdata->vc5;
482 u8 data[14] = {
483 hwdata->div_frc >> 22, hwdata->div_frc >> 14,
484 hwdata->div_frc >> 6, hwdata->div_frc << 2,
485 0, 0, 0, 0, 0,
486 0, 0,
487 hwdata->div_int >> 4, hwdata->div_int << 4,
488 0
489 };
490
491 regmap_bulk_write(vc5->regmap, VC5_OUT_DIV_FRAC(hwdata->num, 0),
492 data, 14);
493
494 /*
495 * Toggle magic bit in undocumented register for unknown reason.
496 * This is what the IDT timing commander tool does and the chip
497 * datasheet somewhat implies this is needed, but the register
498 * and the bit is not documented.
499 */
500 regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
501 VC5_GLOBAL_REGISTER_GLOBAL_RESET, 0);
502 regmap_update_bits(vc5->regmap, VC5_GLOBAL_REGISTER,
503 VC5_GLOBAL_REGISTER_GLOBAL_RESET,
504 VC5_GLOBAL_REGISTER_GLOBAL_RESET);
505 return 0;
506}
507
508static const struct clk_ops vc5_fod_ops = {
509 .recalc_rate = vc5_fod_recalc_rate,
510 .round_rate = vc5_fod_round_rate,
511 .set_rate = vc5_fod_set_rate,
512};
513
514static int vc5_clk_out_prepare(struct clk_hw *hw)
515{
516 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
517 struct vc5_driver_data *vc5 = hwdata->vc5;
Marek Vasut718f4692017-07-09 15:28:10 +0200518 const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
519 VC5_OUT_DIV_CONTROL_SEL_EXT |
520 VC5_OUT_DIV_CONTROL_EN_FOD;
521 unsigned int src;
522 int ret;
523
524 /*
525 * If the input mux is disabled, enable it first and
526 * select source from matching FOD.
527 */
528 regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
529 if ((src & mask) == 0) {
530 src = VC5_OUT_DIV_CONTROL_RESET | VC5_OUT_DIV_CONTROL_EN_FOD;
531 ret = regmap_update_bits(vc5->regmap,
532 VC5_OUT_DIV_CONTROL(hwdata->num),
533 mask | VC5_OUT_DIV_CONTROL_RESET, src);
534 if (ret)
535 return ret;
536 }
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100537
538 /* Enable the clock buffer */
539 regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
540 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF,
541 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
542 return 0;
543}
544
545static void vc5_clk_out_unprepare(struct clk_hw *hw)
546{
547 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
548 struct vc5_driver_data *vc5 = hwdata->vc5;
549
Marek Vasuta4decf52017-07-09 15:28:08 +0200550 /* Disable the clock buffer */
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100551 regmap_update_bits(vc5->regmap, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
552 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF, 0);
553}
554
555static unsigned char vc5_clk_out_get_parent(struct clk_hw *hw)
556{
557 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
558 struct vc5_driver_data *vc5 = hwdata->vc5;
559 const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
560 VC5_OUT_DIV_CONTROL_SEL_EXT |
561 VC5_OUT_DIV_CONTROL_EN_FOD;
562 const u8 fodclkmask = VC5_OUT_DIV_CONTROL_SELB_NORM |
563 VC5_OUT_DIV_CONTROL_EN_FOD;
564 const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
565 VC5_OUT_DIV_CONTROL_SEL_EXT;
566 unsigned int src;
567
568 regmap_read(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num), &src);
569 src &= mask;
570
Marek Vasut325b7b92017-07-09 15:28:09 +0200571 if (src == 0) /* Input mux set to DISABLED */
572 return 0;
573
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100574 if ((src & fodclkmask) == VC5_OUT_DIV_CONTROL_EN_FOD)
575 return 0;
576
577 if (src == extclk)
578 return 1;
579
580 dev_warn(&vc5->client->dev,
581 "Invalid clock output configuration (%02x)\n", src);
582 return 0;
583}
584
585static int vc5_clk_out_set_parent(struct clk_hw *hw, u8 index)
586{
587 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
588 struct vc5_driver_data *vc5 = hwdata->vc5;
589 const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
590 VC5_OUT_DIV_CONTROL_SELB_NORM |
591 VC5_OUT_DIV_CONTROL_SEL_EXT |
592 VC5_OUT_DIV_CONTROL_EN_FOD;
593 const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
594 VC5_OUT_DIV_CONTROL_SEL_EXT;
595 u8 src = VC5_OUT_DIV_CONTROL_RESET;
596
597 if (index == 0)
598 src |= VC5_OUT_DIV_CONTROL_EN_FOD;
599 else
600 src |= extclk;
601
602 return regmap_update_bits(vc5->regmap, VC5_OUT_DIV_CONTROL(hwdata->num),
603 mask, src);
604}
605
606static const struct clk_ops vc5_clk_out_ops = {
607 .prepare = vc5_clk_out_prepare,
608 .unprepare = vc5_clk_out_unprepare,
609 .set_parent = vc5_clk_out_set_parent,
610 .get_parent = vc5_clk_out_get_parent,
611};
612
613static struct clk_hw *vc5_of_clk_get(struct of_phandle_args *clkspec,
614 void *data)
615{
616 struct vc5_driver_data *vc5 = data;
617 unsigned int idx = clkspec->args[0];
618
Alexey Firago9adddb02017-04-07 12:12:22 +0300619 if (idx >= vc5->chip_info->clk_out_cnt)
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100620 return ERR_PTR(-EINVAL);
621
622 return &vc5->clk_out[idx].hw;
623}
624
625static int vc5_map_index_to_output(const enum vc5_model model,
626 const unsigned int n)
627{
628 switch (model) {
629 case IDT_VC5_5P49V5933:
630 return (n == 0) ? 0 : 3;
631 case IDT_VC5_5P49V5923:
Alexey Firago1193e142017-04-07 12:12:24 +0300632 case IDT_VC5_5P49V5935:
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100633 default:
634 return n;
635 }
636}
637
638static const struct of_device_id clk_vc5_of_match[];
639
640static int vc5_probe(struct i2c_client *client,
641 const struct i2c_device_id *id)
642{
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100643 struct vc5_driver_data *vc5;
644 struct clk_init_data init;
645 const char *parent_names[2];
Alexey Firago9adddb02017-04-07 12:12:22 +0300646 unsigned int n, idx = 0;
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100647 int ret;
648
649 vc5 = devm_kzalloc(&client->dev, sizeof(*vc5), GFP_KERNEL);
650 if (vc5 == NULL)
651 return -ENOMEM;
652
653 i2c_set_clientdata(client, vc5);
654 vc5->client = client;
Alexey Firago9adddb02017-04-07 12:12:22 +0300655 vc5->chip_info = of_device_get_match_data(&client->dev);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100656
657 vc5->pin_xin = devm_clk_get(&client->dev, "xin");
658 if (PTR_ERR(vc5->pin_xin) == -EPROBE_DEFER)
659 return -EPROBE_DEFER;
660
661 vc5->pin_clkin = devm_clk_get(&client->dev, "clkin");
662 if (PTR_ERR(vc5->pin_clkin) == -EPROBE_DEFER)
663 return -EPROBE_DEFER;
664
665 vc5->regmap = devm_regmap_init_i2c(client, &vc5_regmap_config);
666 if (IS_ERR(vc5->regmap)) {
667 dev_err(&client->dev, "failed to allocate register map\n");
668 return PTR_ERR(vc5->regmap);
669 }
670
671 /* Register clock input mux */
672 memset(&init, 0, sizeof(init));
673
674 if (!IS_ERR(vc5->pin_xin)) {
675 vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
676 parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
Alexey Firago9adddb02017-04-07 12:12:22 +0300677 } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100678 vc5->pin_xin = clk_register_fixed_rate(&client->dev,
679 "internal-xtal", NULL,
680 0, 25000000);
681 if (IS_ERR(vc5->pin_xin))
682 return PTR_ERR(vc5->pin_xin);
683 vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
684 parent_names[init.num_parents++] = __clk_get_name(vc5->pin_xin);
685 }
686
687 if (!IS_ERR(vc5->pin_clkin)) {
688 vc5->clk_mux_ins |= VC5_MUX_IN_CLKIN;
689 parent_names[init.num_parents++] =
690 __clk_get_name(vc5->pin_clkin);
691 }
692
693 if (!init.num_parents) {
694 dev_err(&client->dev, "no input clock specified!\n");
695 return -EINVAL;
696 }
697
698 init.name = vc5_mux_names[0];
699 init.ops = &vc5_mux_ops;
700 init.flags = 0;
701 init.parent_names = parent_names;
702 vc5->clk_mux.init = &init;
703 ret = devm_clk_hw_register(&client->dev, &vc5->clk_mux);
704 if (ret) {
705 dev_err(&client->dev, "unable to register %s\n", init.name);
706 goto err_clk;
707 }
708
Marek Vasut55997db2017-07-09 15:28:11 +0200709 /* Register PFD */
710 memset(&init, 0, sizeof(init));
711 init.name = vc5_pfd_names[0];
712 init.ops = &vc5_pfd_ops;
713 init.flags = CLK_SET_RATE_PARENT;
714 init.parent_names = vc5_mux_names;
715 init.num_parents = 1;
716 vc5->clk_pfd.init = &init;
717 ret = devm_clk_hw_register(&client->dev, &vc5->clk_pfd);
718 if (ret) {
719 dev_err(&client->dev, "unable to register %s\n", init.name);
720 goto err_clk;
721 }
722
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100723 /* Register PLL */
724 memset(&init, 0, sizeof(init));
725 init.name = vc5_pll_names[0];
726 init.ops = &vc5_pll_ops;
727 init.flags = CLK_SET_RATE_PARENT;
Marek Vasut55997db2017-07-09 15:28:11 +0200728 init.parent_names = vc5_pfd_names;
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100729 init.num_parents = 1;
730 vc5->clk_pll.num = 0;
731 vc5->clk_pll.vc5 = vc5;
732 vc5->clk_pll.hw.init = &init;
733 ret = devm_clk_hw_register(&client->dev, &vc5->clk_pll.hw);
734 if (ret) {
735 dev_err(&client->dev, "unable to register %s\n", init.name);
736 goto err_clk;
737 }
738
739 /* Register FODs */
Alexey Firago9adddb02017-04-07 12:12:22 +0300740 for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
741 idx = vc5_map_index_to_output(vc5->chip_info->model, n);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100742 memset(&init, 0, sizeof(init));
743 init.name = vc5_fod_names[idx];
744 init.ops = &vc5_fod_ops;
745 init.flags = CLK_SET_RATE_PARENT;
746 init.parent_names = vc5_pll_names;
747 init.num_parents = 1;
748 vc5->clk_fod[n].num = idx;
749 vc5->clk_fod[n].vc5 = vc5;
750 vc5->clk_fod[n].hw.init = &init;
751 ret = devm_clk_hw_register(&client->dev, &vc5->clk_fod[n].hw);
752 if (ret) {
753 dev_err(&client->dev, "unable to register %s\n",
754 init.name);
755 goto err_clk;
756 }
757 }
758
759 /* Register MUX-connected OUT0_I2C_SELB output */
760 memset(&init, 0, sizeof(init));
761 init.name = vc5_clk_out_names[0];
762 init.ops = &vc5_clk_out_ops;
763 init.flags = CLK_SET_RATE_PARENT;
764 init.parent_names = vc5_mux_names;
765 init.num_parents = 1;
766 vc5->clk_out[0].num = idx;
767 vc5->clk_out[0].vc5 = vc5;
768 vc5->clk_out[0].hw.init = &init;
769 ret = devm_clk_hw_register(&client->dev, &vc5->clk_out[0].hw);
770 if (ret) {
771 dev_err(&client->dev, "unable to register %s\n",
772 init.name);
773 goto err_clk;
774 }
775
776 /* Register FOD-connected OUTx outputs */
Alexey Firago9adddb02017-04-07 12:12:22 +0300777 for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
778 idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100779 parent_names[0] = vc5_fod_names[idx];
780 if (n == 1)
781 parent_names[1] = vc5_mux_names[0];
782 else
783 parent_names[1] = vc5_clk_out_names[n - 1];
784
785 memset(&init, 0, sizeof(init));
786 init.name = vc5_clk_out_names[idx + 1];
787 init.ops = &vc5_clk_out_ops;
788 init.flags = CLK_SET_RATE_PARENT;
789 init.parent_names = parent_names;
790 init.num_parents = 2;
791 vc5->clk_out[n].num = idx;
792 vc5->clk_out[n].vc5 = vc5;
793 vc5->clk_out[n].hw.init = &init;
794 ret = devm_clk_hw_register(&client->dev,
795 &vc5->clk_out[n].hw);
796 if (ret) {
797 dev_err(&client->dev, "unable to register %s\n",
798 init.name);
799 goto err_clk;
800 }
801 }
802
803 ret = of_clk_add_hw_provider(client->dev.of_node, vc5_of_clk_get, vc5);
804 if (ret) {
805 dev_err(&client->dev, "unable to add clk provider\n");
806 goto err_clk;
807 }
808
809 return 0;
810
811err_clk:
Alexey Firago9adddb02017-04-07 12:12:22 +0300812 if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100813 clk_unregister_fixed_rate(vc5->pin_xin);
814 return ret;
815}
816
817static int vc5_remove(struct i2c_client *client)
818{
819 struct vc5_driver_data *vc5 = i2c_get_clientdata(client);
820
821 of_clk_del_provider(client->dev.of_node);
822
Alexey Firago9adddb02017-04-07 12:12:22 +0300823 if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100824 clk_unregister_fixed_rate(vc5->pin_xin);
825
826 return 0;
827}
828
Alexey Firago9adddb02017-04-07 12:12:22 +0300829static const struct vc5_chip_info idt_5p49v5923_info = {
830 .model = IDT_VC5_5P49V5923,
831 .clk_fod_cnt = 2,
832 .clk_out_cnt = 3,
833 .flags = 0,
834};
835
836static const struct vc5_chip_info idt_5p49v5933_info = {
837 .model = IDT_VC5_5P49V5933,
838 .clk_fod_cnt = 2,
839 .clk_out_cnt = 3,
840 .flags = VC5_HAS_INTERNAL_XTAL,
841};
842
Alexey Firago1193e142017-04-07 12:12:24 +0300843static const struct vc5_chip_info idt_5p49v5935_info = {
844 .model = IDT_VC5_5P49V5935,
845 .clk_fod_cnt = 4,
846 .clk_out_cnt = 5,
847 .flags = VC5_HAS_INTERNAL_XTAL,
848};
849
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100850static const struct i2c_device_id vc5_id[] = {
851 { "5p49v5923", .driver_data = IDT_VC5_5P49V5923 },
852 { "5p49v5933", .driver_data = IDT_VC5_5P49V5933 },
Alexey Firago1193e142017-04-07 12:12:24 +0300853 { "5p49v5935", .driver_data = IDT_VC5_5P49V5935 },
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100854 { }
855};
856MODULE_DEVICE_TABLE(i2c, vc5_id);
857
858static const struct of_device_id clk_vc5_of_match[] = {
Alexey Firago9adddb02017-04-07 12:12:22 +0300859 { .compatible = "idt,5p49v5923", .data = &idt_5p49v5923_info },
860 { .compatible = "idt,5p49v5933", .data = &idt_5p49v5933_info },
Alexey Firago1193e142017-04-07 12:12:24 +0300861 { .compatible = "idt,5p49v5935", .data = &idt_5p49v5935_info },
Marek Vasut3e1aec4e2017-01-12 02:03:24 +0100862 { },
863};
864MODULE_DEVICE_TABLE(of, clk_vc5_of_match);
865
866static struct i2c_driver vc5_driver = {
867 .driver = {
868 .name = "vc5",
869 .of_match_table = clk_vc5_of_match,
870 },
871 .probe = vc5_probe,
872 .remove = vc5_remove,
873 .id_table = vc5_id,
874};
875module_i2c_driver(vc5_driver);
876
877MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
878MODULE_DESCRIPTION("IDT VersaClock 5 driver");
879MODULE_LICENSE("GPL");