| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * BCM2835 master mode driver |
|---|
| 3 | | - * |
|---|
| 4 | | - * This software is licensed under the terms of the GNU General Public |
|---|
| 5 | | - * License version 2, as published by the Free Software Foundation, and |
|---|
| 6 | | - * may be copied, distributed, and modified under those terms. |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 9 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | | - * GNU General Public License for more details. |
|---|
| 12 | 4 | */ |
|---|
| 13 | 5 | |
|---|
| 14 | 6 | #include <linux/clk.h> |
|---|
| 7 | +#include <linux/clkdev.h> |
|---|
| 8 | +#include <linux/clk-provider.h> |
|---|
| 15 | 9 | #include <linux/completion.h> |
|---|
| 16 | 10 | #include <linux/err.h> |
|---|
| 17 | 11 | #include <linux/i2c.h> |
|---|
| 18 | 12 | #include <linux/interrupt.h> |
|---|
| 19 | 13 | #include <linux/io.h> |
|---|
| 20 | 14 | #include <linux/module.h> |
|---|
| 15 | +#include <linux/of_device.h> |
|---|
| 21 | 16 | #include <linux/platform_device.h> |
|---|
| 22 | 17 | #include <linux/slab.h> |
|---|
| 23 | 18 | |
|---|
| .. | .. |
|---|
| 28 | 23 | #define BCM2835_I2C_FIFO 0x10 |
|---|
| 29 | 24 | #define BCM2835_I2C_DIV 0x14 |
|---|
| 30 | 25 | #define BCM2835_I2C_DEL 0x18 |
|---|
| 26 | +/* |
|---|
| 27 | + * 16-bit field for the number of SCL cycles to wait after rising SCL |
|---|
| 28 | + * before deciding the slave is not responding. 0 disables the |
|---|
| 29 | + * timeout detection. |
|---|
| 30 | + */ |
|---|
| 31 | 31 | #define BCM2835_I2C_CLKT 0x1c |
|---|
| 32 | 32 | |
|---|
| 33 | 33 | #define BCM2835_I2C_C_READ BIT(0) |
|---|
| .. | .. |
|---|
| 59 | 59 | struct bcm2835_i2c_dev { |
|---|
| 60 | 60 | struct device *dev; |
|---|
| 61 | 61 | void __iomem *regs; |
|---|
| 62 | | - struct clk *clk; |
|---|
| 63 | 62 | int irq; |
|---|
| 64 | | - u32 bus_clk_rate; |
|---|
| 65 | 63 | struct i2c_adapter adapter; |
|---|
| 66 | 64 | struct completion completion; |
|---|
| 67 | 65 | struct i2c_msg *curr_msg; |
|---|
| 66 | + struct clk *bus_clk; |
|---|
| 68 | 67 | int num_msgs; |
|---|
| 69 | 68 | u32 msg_err; |
|---|
| 70 | 69 | u8 *msg_buf; |
|---|
| .. | .. |
|---|
| 82 | 81 | return readl(i2c_dev->regs + reg); |
|---|
| 83 | 82 | } |
|---|
| 84 | 83 | |
|---|
| 85 | | -static int bcm2835_i2c_set_divider(struct bcm2835_i2c_dev *i2c_dev) |
|---|
| 86 | | -{ |
|---|
| 87 | | - u32 divider, redl, fedl; |
|---|
| 84 | +#define to_clk_bcm2835_i2c(_hw) container_of(_hw, struct clk_bcm2835_i2c, hw) |
|---|
| 85 | +struct clk_bcm2835_i2c { |
|---|
| 86 | + struct clk_hw hw; |
|---|
| 87 | + struct bcm2835_i2c_dev *i2c_dev; |
|---|
| 88 | +}; |
|---|
| 88 | 89 | |
|---|
| 89 | | - divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), |
|---|
| 90 | | - i2c_dev->bus_clk_rate); |
|---|
| 90 | +static int clk_bcm2835_i2c_calc_divider(unsigned long rate, |
|---|
| 91 | + unsigned long parent_rate) |
|---|
| 92 | +{ |
|---|
| 93 | + u32 divider = DIV_ROUND_UP(parent_rate, rate); |
|---|
| 94 | + |
|---|
| 91 | 95 | /* |
|---|
| 92 | 96 | * Per the datasheet, the register is always interpreted as an even |
|---|
| 93 | 97 | * number, by rounding down. In other words, the LSB is ignored. So, |
|---|
| .. | .. |
|---|
| 96 | 100 | if (divider & 1) |
|---|
| 97 | 101 | divider++; |
|---|
| 98 | 102 | if ((divider < BCM2835_I2C_CDIV_MIN) || |
|---|
| 99 | | - (divider > BCM2835_I2C_CDIV_MAX)) { |
|---|
| 100 | | - dev_err_ratelimited(i2c_dev->dev, "Invalid clock-frequency\n"); |
|---|
| 103 | + (divider > BCM2835_I2C_CDIV_MAX)) |
|---|
| 101 | 104 | return -EINVAL; |
|---|
| 102 | | - } |
|---|
| 103 | 105 | |
|---|
| 104 | | - bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider); |
|---|
| 106 | + return divider; |
|---|
| 107 | +} |
|---|
| 108 | + |
|---|
| 109 | +static int clk_bcm2835_i2c_set_rate(struct clk_hw *hw, unsigned long rate, |
|---|
| 110 | + unsigned long parent_rate) |
|---|
| 111 | +{ |
|---|
| 112 | + struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw); |
|---|
| 113 | + u32 redl, fedl; |
|---|
| 114 | + u32 divider = clk_bcm2835_i2c_calc_divider(rate, parent_rate); |
|---|
| 115 | + |
|---|
| 116 | + if (divider == -EINVAL) |
|---|
| 117 | + return -EINVAL; |
|---|
| 118 | + |
|---|
| 119 | + bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DIV, divider); |
|---|
| 105 | 120 | |
|---|
| 106 | 121 | /* |
|---|
| 107 | 122 | * Number of core clocks to wait after falling edge before |
|---|
| .. | .. |
|---|
| 116 | 131 | */ |
|---|
| 117 | 132 | redl = max(divider / 4, 1u); |
|---|
| 118 | 133 | |
|---|
| 119 | | - bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DEL, |
|---|
| 134 | + bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DEL, |
|---|
| 120 | 135 | (fedl << BCM2835_I2C_FEDL_SHIFT) | |
|---|
| 121 | 136 | (redl << BCM2835_I2C_REDL_SHIFT)); |
|---|
| 122 | 137 | return 0; |
|---|
| 138 | +} |
|---|
| 139 | + |
|---|
| 140 | +static long clk_bcm2835_i2c_round_rate(struct clk_hw *hw, unsigned long rate, |
|---|
| 141 | + unsigned long *parent_rate) |
|---|
| 142 | +{ |
|---|
| 143 | + u32 divider = clk_bcm2835_i2c_calc_divider(rate, *parent_rate); |
|---|
| 144 | + |
|---|
| 145 | + return DIV_ROUND_UP(*parent_rate, divider); |
|---|
| 146 | +} |
|---|
| 147 | + |
|---|
| 148 | +static unsigned long clk_bcm2835_i2c_recalc_rate(struct clk_hw *hw, |
|---|
| 149 | + unsigned long parent_rate) |
|---|
| 150 | +{ |
|---|
| 151 | + struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw); |
|---|
| 152 | + u32 divider = bcm2835_i2c_readl(div->i2c_dev, BCM2835_I2C_DIV); |
|---|
| 153 | + |
|---|
| 154 | + return DIV_ROUND_UP(parent_rate, divider); |
|---|
| 155 | +} |
|---|
| 156 | + |
|---|
| 157 | +static const struct clk_ops clk_bcm2835_i2c_ops = { |
|---|
| 158 | + .set_rate = clk_bcm2835_i2c_set_rate, |
|---|
| 159 | + .round_rate = clk_bcm2835_i2c_round_rate, |
|---|
| 160 | + .recalc_rate = clk_bcm2835_i2c_recalc_rate, |
|---|
| 161 | +}; |
|---|
| 162 | + |
|---|
| 163 | +static struct clk *bcm2835_i2c_register_div(struct device *dev, |
|---|
| 164 | + struct clk *mclk, |
|---|
| 165 | + struct bcm2835_i2c_dev *i2c_dev) |
|---|
| 166 | +{ |
|---|
| 167 | + struct clk_init_data init; |
|---|
| 168 | + struct clk_bcm2835_i2c *priv; |
|---|
| 169 | + char name[32]; |
|---|
| 170 | + const char *mclk_name; |
|---|
| 171 | + |
|---|
| 172 | + snprintf(name, sizeof(name), "%s_div", dev_name(dev)); |
|---|
| 173 | + |
|---|
| 174 | + mclk_name = __clk_get_name(mclk); |
|---|
| 175 | + |
|---|
| 176 | + init.ops = &clk_bcm2835_i2c_ops; |
|---|
| 177 | + init.name = name; |
|---|
| 178 | + init.parent_names = (const char* []) { mclk_name }; |
|---|
| 179 | + init.num_parents = 1; |
|---|
| 180 | + init.flags = 0; |
|---|
| 181 | + |
|---|
| 182 | + priv = devm_kzalloc(dev, sizeof(struct clk_bcm2835_i2c), GFP_KERNEL); |
|---|
| 183 | + if (priv == NULL) |
|---|
| 184 | + return ERR_PTR(-ENOMEM); |
|---|
| 185 | + |
|---|
| 186 | + priv->hw.init = &init; |
|---|
| 187 | + priv->i2c_dev = i2c_dev; |
|---|
| 188 | + |
|---|
| 189 | + clk_hw_register_clkdev(&priv->hw, "div", dev_name(dev)); |
|---|
| 190 | + return devm_clk_register(dev, &priv->hw); |
|---|
| 123 | 191 | } |
|---|
| 124 | 192 | |
|---|
| 125 | 193 | static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev) |
|---|
| .. | .. |
|---|
| 279 | 347 | { |
|---|
| 280 | 348 | struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap); |
|---|
| 281 | 349 | unsigned long time_left; |
|---|
| 282 | | - int i, ret; |
|---|
| 350 | + int i; |
|---|
| 283 | 351 | |
|---|
| 284 | 352 | for (i = 0; i < (num - 1); i++) |
|---|
| 285 | 353 | if (msgs[i].flags & I2C_M_RD) { |
|---|
| .. | .. |
|---|
| 287 | 355 | "only one read message supported, has to be last\n"); |
|---|
| 288 | 356 | return -EOPNOTSUPP; |
|---|
| 289 | 357 | } |
|---|
| 290 | | - |
|---|
| 291 | | - ret = bcm2835_i2c_set_divider(i2c_dev); |
|---|
| 292 | | - if (ret) |
|---|
| 293 | | - return ret; |
|---|
| 294 | 358 | |
|---|
| 295 | 359 | i2c_dev->curr_msg = msgs; |
|---|
| 296 | 360 | i2c_dev->num_msgs = num; |
|---|
| .. | .. |
|---|
| 332 | 396 | }; |
|---|
| 333 | 397 | |
|---|
| 334 | 398 | /* |
|---|
| 335 | | - * This HW was reported to have problems with clock stretching: |
|---|
| 336 | | - * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html |
|---|
| 399 | + * The BCM2835 was reported to have problems with clock stretching: |
|---|
| 400 | + * https://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html |
|---|
| 337 | 401 | * https://www.raspberrypi.org/forums/viewtopic.php?p=146272 |
|---|
| 338 | 402 | */ |
|---|
| 339 | 403 | static const struct i2c_adapter_quirks bcm2835_i2c_quirks = { |
|---|
| .. | .. |
|---|
| 346 | 410 | struct resource *mem, *irq; |
|---|
| 347 | 411 | int ret; |
|---|
| 348 | 412 | struct i2c_adapter *adap; |
|---|
| 413 | + struct clk *mclk; |
|---|
| 414 | + u32 bus_clk_rate; |
|---|
| 349 | 415 | |
|---|
| 350 | 416 | i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); |
|---|
| 351 | 417 | if (!i2c_dev) |
|---|
| .. | .. |
|---|
| 359 | 425 | if (IS_ERR(i2c_dev->regs)) |
|---|
| 360 | 426 | return PTR_ERR(i2c_dev->regs); |
|---|
| 361 | 427 | |
|---|
| 362 | | - i2c_dev->clk = devm_clk_get(&pdev->dev, NULL); |
|---|
| 363 | | - if (IS_ERR(i2c_dev->clk)) { |
|---|
| 364 | | - if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER) |
|---|
| 365 | | - dev_err(&pdev->dev, "Could not get clock\n"); |
|---|
| 366 | | - return PTR_ERR(i2c_dev->clk); |
|---|
| 428 | + mclk = devm_clk_get(&pdev->dev, NULL); |
|---|
| 429 | + if (IS_ERR(mclk)) |
|---|
| 430 | + return dev_err_probe(&pdev->dev, PTR_ERR(mclk), |
|---|
| 431 | + "Could not get clock\n"); |
|---|
| 432 | + |
|---|
| 433 | + i2c_dev->bus_clk = bcm2835_i2c_register_div(&pdev->dev, mclk, i2c_dev); |
|---|
| 434 | + |
|---|
| 435 | + if (IS_ERR(i2c_dev->bus_clk)) { |
|---|
| 436 | + dev_err(&pdev->dev, "Could not register clock\n"); |
|---|
| 437 | + return PTR_ERR(i2c_dev->bus_clk); |
|---|
| 367 | 438 | } |
|---|
| 368 | 439 | |
|---|
| 369 | 440 | ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency", |
|---|
| 370 | | - &i2c_dev->bus_clk_rate); |
|---|
| 441 | + &bus_clk_rate); |
|---|
| 371 | 442 | if (ret < 0) { |
|---|
| 372 | 443 | dev_warn(&pdev->dev, |
|---|
| 373 | 444 | "Could not read clock-frequency property\n"); |
|---|
| 374 | | - i2c_dev->bus_clk_rate = 100000; |
|---|
| 445 | + bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ; |
|---|
| 446 | + } |
|---|
| 447 | + |
|---|
| 448 | + ret = clk_set_rate_exclusive(i2c_dev->bus_clk, bus_clk_rate); |
|---|
| 449 | + if (ret < 0) { |
|---|
| 450 | + dev_err(&pdev->dev, "Could not set clock frequency\n"); |
|---|
| 451 | + return ret; |
|---|
| 452 | + } |
|---|
| 453 | + |
|---|
| 454 | + ret = clk_prepare_enable(i2c_dev->bus_clk); |
|---|
| 455 | + if (ret) { |
|---|
| 456 | + dev_err(&pdev->dev, "Couldn't prepare clock"); |
|---|
| 457 | + return ret; |
|---|
| 375 | 458 | } |
|---|
| 376 | 459 | |
|---|
| 377 | 460 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
|---|
| .. | .. |
|---|
| 392 | 475 | i2c_set_adapdata(adap, i2c_dev); |
|---|
| 393 | 476 | adap->owner = THIS_MODULE; |
|---|
| 394 | 477 | adap->class = I2C_CLASS_DEPRECATED; |
|---|
| 395 | | - strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name)); |
|---|
| 478 | + snprintf(adap->name, sizeof(adap->name), "bcm2835 (%s)", |
|---|
| 479 | + of_node_full_name(pdev->dev.of_node)); |
|---|
| 396 | 480 | adap->algo = &bcm2835_i2c_algo; |
|---|
| 397 | 481 | adap->dev.parent = &pdev->dev; |
|---|
| 398 | 482 | adap->dev.of_node = pdev->dev.of_node; |
|---|
| 399 | | - adap->quirks = &bcm2835_i2c_quirks; |
|---|
| 483 | + adap->quirks = of_device_get_match_data(&pdev->dev); |
|---|
| 400 | 484 | |
|---|
| 485 | + /* |
|---|
| 486 | + * Disable the hardware clock stretching timeout. SMBUS |
|---|
| 487 | + * specifies a limit for how long the device can stretch the |
|---|
| 488 | + * clock, but core I2C doesn't. |
|---|
| 489 | + */ |
|---|
| 490 | + bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_CLKT, 0); |
|---|
| 401 | 491 | bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0); |
|---|
| 402 | 492 | |
|---|
| 403 | 493 | ret = i2c_add_adapter(adap); |
|---|
| .. | .. |
|---|
| 411 | 501 | { |
|---|
| 412 | 502 | struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev); |
|---|
| 413 | 503 | |
|---|
| 504 | + clk_rate_exclusive_put(i2c_dev->bus_clk); |
|---|
| 505 | + clk_disable_unprepare(i2c_dev->bus_clk); |
|---|
| 506 | + |
|---|
| 414 | 507 | free_irq(i2c_dev->irq, i2c_dev); |
|---|
| 415 | 508 | i2c_del_adapter(&i2c_dev->adapter); |
|---|
| 416 | 509 | |
|---|
| .. | .. |
|---|
| 418 | 511 | } |
|---|
| 419 | 512 | |
|---|
| 420 | 513 | static const struct of_device_id bcm2835_i2c_of_match[] = { |
|---|
| 421 | | - { .compatible = "brcm,bcm2835-i2c" }, |
|---|
| 514 | + { .compatible = "brcm,bcm2711-i2c" }, |
|---|
| 515 | + { .compatible = "brcm,bcm2835-i2c", .data = &bcm2835_i2c_quirks }, |
|---|
| 422 | 516 | {}, |
|---|
| 423 | 517 | }; |
|---|
| 424 | 518 | MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match); |
|---|