1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments CDCE913/925/937/949 clock synthesizer driver
5 * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Tero Kristo <t-kristo@ti.com>
8 * Based on Linux kernel clk-cdce925.c.
14 #include <clk-uclass.h>
17 #define MAX_NUMBER_OF_PLLS 4
18 #define MAX_NUMER_OF_OUTPUTS 9
20 #define CDCE9XX_REG_GLOBAL1 0x01
21 #define CDCE9XX_REG_Y1SPIPDIVH 0x02
22 #define CDCE9XX_REG_PDIV1L 0x03
23 #define CDCE9XX_REG_XCSEL 0x05
25 #define CDCE9XX_PDIV1_H_MASK 0x3
27 #define CDCE9XX_REG_PDIV(clk) (0x16 + (((clk) - 1) & 1) + \
28 ((clk) - 1) / 2 * 0x10)
30 #define CDCE9XX_PDIV_MASK 0x7f
32 #define CDCE9XX_BYTE_TRANSFER BIT(7)
34 struct cdce9xx_chip_info {
39 struct cdce9xx_clk_data {
41 struct cdce9xx_chip_info *chip;
45 static const struct cdce9xx_chip_info cdce913_chip_info = {
46 .num_plls = 1, .num_outputs = 3,
49 static const struct cdce9xx_chip_info cdce925_chip_info = {
50 .num_plls = 2, .num_outputs = 5,
53 static const struct cdce9xx_chip_info cdce937_chip_info = {
54 .num_plls = 3, .num_outputs = 7,
57 static const struct cdce9xx_chip_info cdce949_chip_info = {
58 .num_plls = 4, .num_outputs = 9,
61 static int cdce9xx_reg_read(struct udevice *dev, u8 addr, u8 *buf)
63 struct cdce9xx_clk_data *data = dev_get_priv(dev);
66 ret = dm_i2c_read(data->i2c, addr | CDCE9XX_BYTE_TRANSFER, buf, 1);
68 dev_err(dev, "%s: failed for addr:%x, ret:%d\n", __func__,
74 static int cdce9xx_reg_write(struct udevice *dev, u8 addr, u8 val)
76 struct cdce9xx_clk_data *data = dev_get_priv(dev);
79 ret = dm_i2c_write(data->i2c, addr | CDCE9XX_BYTE_TRANSFER, &val, 1);
81 dev_err(dev, "%s: failed for addr:%x, ret:%d\n", __func__,
87 static int cdce9xx_clk_of_xlate(struct clk *clk,
88 struct ofnode_phandle_args *args)
90 struct cdce9xx_clk_data *data = dev_get_priv(clk->dev);
92 if (args->args_count != 1)
95 if (args->args[0] > data->chip->num_outputs)
98 clk->id = args->args[0];
103 static int cdce9xx_clk_probe(struct udevice *dev)
105 struct cdce9xx_clk_data *data = dev_get_priv(dev);
106 struct cdce9xx_chip_info *chip = (void *)dev_get_driver_data(dev);
111 val = (u32)dev_read_addr_ptr(dev);
113 ret = i2c_get_chip(dev->parent, val, 1, &data->i2c);
115 dev_err(dev, "I2C probe failed.\n");
121 ret = clk_get_by_index(dev, 0, &clk);
122 data->xtal_rate = clk_get_rate(&clk);
124 val = dev_read_u32_default(dev, "xtal-load-pf", -1);
126 cdce9xx_reg_write(dev, CDCE9XX_REG_XCSEL, val << 3);
131 static u16 cdce9xx_clk_get_pdiv(struct clk *clk)
138 ret = cdce9xx_reg_read(clk->dev, CDCE9XX_REG_Y1SPIPDIVH, &val);
142 pdiv = (val & CDCE9XX_PDIV1_H_MASK) << 8;
144 ret = cdce9xx_reg_read(clk->dev, CDCE9XX_REG_PDIV1L, &val);
150 ret = cdce9xx_reg_read(clk->dev, CDCE9XX_REG_PDIV(clk->id),
155 pdiv = val & CDCE9XX_PDIV_MASK;
161 static u32 cdce9xx_clk_get_parent_rate(struct clk *clk)
163 struct cdce9xx_clk_data *data = dev_get_priv(clk->dev);
165 return data->xtal_rate;
168 static ulong cdce9xx_clk_get_rate(struct clk *clk)
173 parent_rate = cdce9xx_clk_get_parent_rate(clk);
175 pdiv = cdce9xx_clk_get_pdiv(clk);
177 return parent_rate / pdiv;
180 static ulong cdce9xx_clk_set_rate(struct clk *clk, ulong rate)
188 parent_rate = cdce9xx_clk_get_parent_rate(clk);
190 pdiv = parent_rate / rate;
192 diff = rate - parent_rate / pdiv;
194 if (rate - parent_rate / (pdiv + 1) < diff)
198 ret = cdce9xx_reg_read(clk->dev, CDCE9XX_REG_Y1SPIPDIVH, &val);
202 val &= ~CDCE9XX_PDIV1_H_MASK;
206 ret = cdce9xx_reg_write(clk->dev, CDCE9XX_REG_Y1SPIPDIVH, val);
210 ret = cdce9xx_reg_write(clk->dev, CDCE9XX_REG_PDIV1L,
215 ret = cdce9xx_reg_read(clk->dev, CDCE9XX_REG_PDIV(clk->id),
220 val &= ~CDCE9XX_PDIV_MASK;
224 ret = cdce9xx_reg_write(clk->dev, CDCE9XX_REG_PDIV(clk->id),
233 static const struct udevice_id cdce9xx_clk_of_match[] = {
234 { .compatible = "ti,cdce913", .data = (u32)&cdce913_chip_info },
235 { .compatible = "ti,cdce925", .data = (u32)&cdce925_chip_info },
236 { .compatible = "ti,cdce937", .data = (u32)&cdce937_chip_info },
237 { .compatible = "ti,cdce949", .data = (u32)&cdce949_chip_info },
241 static const struct clk_ops cdce9xx_clk_ops = {
242 .of_xlate = cdce9xx_clk_of_xlate,
243 .get_rate = cdce9xx_clk_get_rate,
244 .set_rate = cdce9xx_clk_set_rate,
247 U_BOOT_DRIVER(cdce9xx_clk) = {
248 .name = "cdce9xx-clk",
250 .of_match = cdce9xx_clk_of_match,
251 .probe = cdce9xx_clk_probe,
252 .priv_auto_alloc_size = sizeof(struct cdce9xx_clk_data),
253 .ops = &cdce9xx_clk_ops,