1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Renesas 9-series PCIe clock generator driver
5 * The following series can be supported:
6 * - 9FGV/9DBV/9DMV/9FGL/9DML/9QXL/9SQ
11 * Copyright (C) 2022 Marek Vasut <marex@denx.de>
14 #include <linux/clk-provider.h>
15 #include <linux/i2c.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
19 #include <linux/regmap.h>
21 #define RS9_REG_OE 0x0
22 #define RS9_REG_SS 0x1
23 #define RS9_REG_SS_AMP_0V6 0x0
24 #define RS9_REG_SS_AMP_0V7 0x1
25 #define RS9_REG_SS_AMP_0V8 0x2
26 #define RS9_REG_SS_AMP_0V9 0x3
27 #define RS9_REG_SS_AMP_MASK 0x3
28 #define RS9_REG_SS_SSC_100 0
29 #define RS9_REG_SS_SSC_M025 (1 << 3)
30 #define RS9_REG_SS_SSC_M050 (3 << 3)
31 #define RS9_REG_SS_SSC_MASK (3 << 3)
32 #define RS9_REG_SS_SSC_LOCK BIT(5)
33 #define RS9_REG_SR 0x2
34 #define RS9_REG_REF 0x3
35 #define RS9_REG_REF_OE BIT(4)
36 #define RS9_REG_REF_OD BIT(5)
37 #define RS9_REG_REF_SR_SLOWEST 0
38 #define RS9_REG_REF_SR_SLOW (1 << 6)
39 #define RS9_REG_REF_SR_FAST (2 << 6)
40 #define RS9_REG_REF_SR_FASTER (3 << 6)
41 #define RS9_REG_VID 0x5
42 #define RS9_REG_DID 0x6
43 #define RS9_REG_BCP 0x7
45 #define RS9_REG_VID_IDT 0x01
47 #define RS9_REG_DID_TYPE_FGV (0x0 << RS9_REG_DID_TYPE_SHIFT)
48 #define RS9_REG_DID_TYPE_DBV (0x1 << RS9_REG_DID_TYPE_SHIFT)
49 #define RS9_REG_DID_TYPE_DMV (0x2 << RS9_REG_DID_TYPE_SHIFT)
50 #define RS9_REG_DID_TYPE_SHIFT 0x6
52 /* Supported Renesas 9-series models. */
58 /* Structure to describe features of a particular 9-series model */
59 struct rs9_chip_info {
60 const enum rs9_model model;
61 unsigned int num_clks;
65 struct rs9_driver_data {
66 struct i2c_client *client;
67 struct regmap *regmap;
68 const struct rs9_chip_info *chip_info;
69 struct clk_hw *clk_dif[4];
76 * Renesas 9-series i2c regmap
78 static const struct regmap_range rs9_readable_ranges[] = {
79 regmap_reg_range(RS9_REG_OE, RS9_REG_REF),
80 regmap_reg_range(RS9_REG_VID, RS9_REG_BCP),
83 static const struct regmap_access_table rs9_readable_table = {
84 .yes_ranges = rs9_readable_ranges,
85 .n_yes_ranges = ARRAY_SIZE(rs9_readable_ranges),
88 static const struct regmap_range rs9_writeable_ranges[] = {
89 regmap_reg_range(RS9_REG_OE, RS9_REG_REF),
90 regmap_reg_range(RS9_REG_BCP, RS9_REG_BCP),
93 static const struct regmap_access_table rs9_writeable_table = {
94 .yes_ranges = rs9_writeable_ranges,
95 .n_yes_ranges = ARRAY_SIZE(rs9_writeable_ranges),
98 static int rs9_regmap_i2c_write(void *context,
99 unsigned int reg, unsigned int val)
101 struct i2c_client *i2c = context;
102 const u8 data[3] = { reg, 1, val };
103 const int count = ARRAY_SIZE(data);
106 ret = i2c_master_send(i2c, data, count);
115 static int rs9_regmap_i2c_read(void *context,
116 unsigned int reg, unsigned int *val)
118 struct i2c_client *i2c = context;
119 struct i2c_msg xfer[2];
124 xfer[0].addr = i2c->addr;
127 xfer[0].buf = (void *)&txdata;
129 xfer[1].addr = i2c->addr;
130 xfer[1].flags = I2C_M_RD;
132 xfer[1].buf = (void *)rxdata;
134 ret = i2c_transfer(i2c->adapter, xfer, 2);
141 * Byte 0 is transfer length, which is always 1 due
142 * to BCP register programming to 1 in rs9_probe(),
143 * ignore it and use data from Byte 1.
149 static const struct regmap_config rs9_regmap_config = {
152 .cache_type = REGCACHE_FLAT,
153 .max_register = RS9_REG_BCP,
154 .num_reg_defaults_raw = 0x8,
155 .rd_table = &rs9_readable_table,
156 .wr_table = &rs9_writeable_table,
157 .reg_write = rs9_regmap_i2c_write,
158 .reg_read = rs9_regmap_i2c_read,
161 static u8 rs9_calc_dif(const struct rs9_driver_data *rs9, int idx)
163 enum rs9_model model = rs9->chip_info->model;
165 if (model == RENESAS_9FGV0241)
167 else if (model == RENESAS_9FGV0441)
173 static int rs9_get_output_config(struct rs9_driver_data *rs9, int idx)
175 struct i2c_client *client = rs9->client;
176 u8 dif = rs9_calc_dif(rs9, idx);
177 unsigned char name[5] = "DIF0";
178 struct device_node *np;
183 rs9->clk_dif_sr |= dif;
185 snprintf(name, 5, "DIF%d", idx);
186 np = of_get_child_by_name(client->dev.of_node, name);
190 /* Output clock slew rate */
191 ret = of_property_read_u32(np, "renesas,slew-rate", &sr);
194 if (sr == 2000000) { /* 2V/ns */
195 rs9->clk_dif_sr &= ~dif;
196 } else if (sr == 3000000) { /* 3V/ns (default) */
197 rs9->clk_dif_sr |= dif;
199 ret = dev_err_probe(&client->dev, -EINVAL,
200 "Invalid renesas,slew-rate value\n");
206 static int rs9_get_common_config(struct rs9_driver_data *rs9)
208 struct i2c_client *client = rs9->client;
209 struct device_node *np = client->dev.of_node;
210 unsigned int amp, ssc;
214 rs9->pll_amplitude = RS9_REG_SS_AMP_0V7;
215 rs9->pll_ssc = RS9_REG_SS_SSC_100;
217 /* Output clock amplitude */
218 ret = of_property_read_u32(np, "renesas,out-amplitude-microvolt",
221 if (amp == 600000) /* 0.6V */
222 rs9->pll_amplitude = RS9_REG_SS_AMP_0V6;
223 else if (amp == 700000) /* 0.7V (default) */
224 rs9->pll_amplitude = RS9_REG_SS_AMP_0V7;
225 else if (amp == 800000) /* 0.8V */
226 rs9->pll_amplitude = RS9_REG_SS_AMP_0V8;
227 else if (amp == 900000) /* 0.9V */
228 rs9->pll_amplitude = RS9_REG_SS_AMP_0V9;
230 return dev_err_probe(&client->dev, -EINVAL,
231 "Invalid renesas,out-amplitude-microvolt value\n");
234 /* Output clock spread spectrum */
235 ret = of_property_read_u32(np, "renesas,out-spread-spectrum", &ssc);
237 if (ssc == 100000) /* 100% ... no spread (default) */
238 rs9->pll_ssc = RS9_REG_SS_SSC_100;
239 else if (ssc == 99750) /* -0.25% ... down spread */
240 rs9->pll_ssc = RS9_REG_SS_SSC_M025;
241 else if (ssc == 99500) /* -0.50% ... down spread */
242 rs9->pll_ssc = RS9_REG_SS_SSC_M050;
244 return dev_err_probe(&client->dev, -EINVAL,
245 "Invalid renesas,out-spread-spectrum value\n");
251 static void rs9_update_config(struct rs9_driver_data *rs9)
255 /* If amplitude is non-default, update it. */
256 if (rs9->pll_amplitude != RS9_REG_SS_AMP_0V7) {
257 regmap_update_bits(rs9->regmap, RS9_REG_SS, RS9_REG_SS_AMP_MASK,
261 /* If SSC is non-default, update it. */
262 if (rs9->pll_ssc != RS9_REG_SS_SSC_100) {
263 regmap_update_bits(rs9->regmap, RS9_REG_SS, RS9_REG_SS_SSC_MASK,
267 for (i = 0; i < rs9->chip_info->num_clks; i++) {
268 u8 dif = rs9_calc_dif(rs9, i);
270 if (rs9->clk_dif_sr & dif)
273 regmap_update_bits(rs9->regmap, RS9_REG_SR, dif,
274 rs9->clk_dif_sr & dif);
278 static struct clk_hw *
279 rs9_of_clk_get(struct of_phandle_args *clkspec, void *data)
281 struct rs9_driver_data *rs9 = data;
282 unsigned int idx = clkspec->args[0];
284 return rs9->clk_dif[idx];
287 static int rs9_probe(struct i2c_client *client)
289 unsigned char name[5] = "DIF0";
290 struct rs9_driver_data *rs9;
291 unsigned int vid, did;
295 rs9 = devm_kzalloc(&client->dev, sizeof(*rs9), GFP_KERNEL);
299 i2c_set_clientdata(client, rs9);
300 rs9->client = client;
301 rs9->chip_info = device_get_match_data(&client->dev);
305 /* Fetch common configuration from DT (if specified) */
306 ret = rs9_get_common_config(rs9);
310 /* Fetch DIFx output configuration from DT (if specified) */
311 for (i = 0; i < rs9->chip_info->num_clks; i++) {
312 ret = rs9_get_output_config(rs9, i);
317 rs9->regmap = devm_regmap_init(&client->dev, NULL,
318 client, &rs9_regmap_config);
319 if (IS_ERR(rs9->regmap))
320 return dev_err_probe(&client->dev, PTR_ERR(rs9->regmap),
321 "Failed to allocate register map\n");
323 /* Always read back 1 Byte via I2C */
324 ret = regmap_write(rs9->regmap, RS9_REG_BCP, 1);
328 ret = regmap_read(rs9->regmap, RS9_REG_VID, &vid);
332 ret = regmap_read(rs9->regmap, RS9_REG_DID, &did);
336 if (vid != RS9_REG_VID_IDT || did != rs9->chip_info->did)
337 return dev_err_probe(&client->dev, -ENODEV,
338 "Incorrect VID/DID: %#02x, %#02x. Expected %#02x, %#02x\n",
339 vid, did, RS9_REG_VID_IDT,
340 rs9->chip_info->did);
343 for (i = 0; i < rs9->chip_info->num_clks; i++) {
344 snprintf(name, 5, "DIF%d", i);
345 hw = devm_clk_hw_register_fixed_factor_index(&client->dev, name,
350 rs9->clk_dif[i] = hw;
353 ret = devm_of_clk_add_hw_provider(&client->dev, rs9_of_clk_get, rs9);
355 rs9_update_config(rs9);
360 static int __maybe_unused rs9_suspend(struct device *dev)
362 struct rs9_driver_data *rs9 = dev_get_drvdata(dev);
364 regcache_cache_only(rs9->regmap, true);
365 regcache_mark_dirty(rs9->regmap);
370 static int __maybe_unused rs9_resume(struct device *dev)
372 struct rs9_driver_data *rs9 = dev_get_drvdata(dev);
375 regcache_cache_only(rs9->regmap, false);
376 ret = regcache_sync(rs9->regmap);
378 dev_err(dev, "Failed to restore register map: %d\n", ret);
382 static const struct rs9_chip_info renesas_9fgv0241_info = {
383 .model = RENESAS_9FGV0241,
385 .did = RS9_REG_DID_TYPE_FGV | 0x02,
388 static const struct rs9_chip_info renesas_9fgv0441_info = {
389 .model = RENESAS_9FGV0441,
391 .did = RS9_REG_DID_TYPE_FGV | 0x04,
394 static const struct i2c_device_id rs9_id[] = {
395 { "9fgv0241", .driver_data = (kernel_ulong_t)&renesas_9fgv0241_info },
396 { "9fgv0441", .driver_data = (kernel_ulong_t)&renesas_9fgv0441_info },
399 MODULE_DEVICE_TABLE(i2c, rs9_id);
401 static const struct of_device_id clk_rs9_of_match[] = {
402 { .compatible = "renesas,9fgv0241", .data = &renesas_9fgv0241_info },
403 { .compatible = "renesas,9fgv0441", .data = &renesas_9fgv0441_info },
406 MODULE_DEVICE_TABLE(of, clk_rs9_of_match);
408 static SIMPLE_DEV_PM_OPS(rs9_pm_ops, rs9_suspend, rs9_resume);
410 static struct i2c_driver rs9_driver = {
412 .name = "clk-renesas-pcie-9series",
414 .of_match_table = clk_rs9_of_match,
419 module_i2c_driver(rs9_driver);
421 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
422 MODULE_DESCRIPTION("Renesas 9-series PCIe clock generator driver");
423 MODULE_LICENSE("GPL");