1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * RSB (Reduced Serial Bus) driver. 4 * 5 * Author: Chen-Yu Tsai <wens@csie.org> 6 * 7 * The RSB controller looks like an SMBus controller which only supports 8 * byte and word data transfers. But, it differs from standard SMBus 9 * protocol on several aspects: 10 * - it uses addresses set at runtime to address slaves. Runtime addresses 11 * are sent to slaves using their 12bit hardware addresses. Up to 15 12 * runtime addresses are available. 13 * - it adds a parity bit every 8bits of data and address for read and 14 * write accesses; this replaces the ack bit 15 * - only one read access is required to read a byte (instead of a write 16 * followed by a read access in standard SMBus protocol) 17 * - there's no Ack bit after each read access 18 * 19 * This means this bus cannot be used to interface with standard SMBus 20 * devices. Devices known to support this interface include the AXP223, 21 * AXP809, and AXP806 PMICs, and the AC100 audio codec, all from X-Powers. 22 * 23 * A description of the operation and wire protocol can be found in the 24 * RSB section of Allwinner's A80 user manual, which can be found at 25 * 26 * https://github.com/allwinner-zh/documents/tree/master/A80 27 * 28 * This document is officially released by Allwinner. 29 * 30 * This driver is based on i2c-sun6i-p2wi.c, the P2WI bus driver. 31 */ 32 33 #include <linux/clk.h> 34 #include <linux/clk/clk-conf.h> 35 #include <linux/device.h> 36 #include <linux/interrupt.h> 37 #include <linux/io.h> 38 #include <linux/iopoll.h> 39 #include <linux/module.h> 40 #include <linux/of.h> 41 #include <linux/of_irq.h> 42 #include <linux/of_device.h> 43 #include <linux/platform_device.h> 44 #include <linux/pm.h> 45 #include <linux/pm_runtime.h> 46 #include <linux/regmap.h> 47 #include <linux/reset.h> 48 #include <linux/slab.h> 49 #include <linux/sunxi-rsb.h> 50 #include <linux/types.h> 51 52 /* RSB registers */ 53 #define RSB_CTRL 0x0 /* Global control */ 54 #define RSB_CCR 0x4 /* Clock control */ 55 #define RSB_INTE 0x8 /* Interrupt controls */ 56 #define RSB_INTS 0xc /* Interrupt status */ 57 #define RSB_ADDR 0x10 /* Address to send with read/write command */ 58 #define RSB_DATA 0x1c /* Data to read/write */ 59 #define RSB_LCR 0x24 /* Line control */ 60 #define RSB_DMCR 0x28 /* Device mode (init) control */ 61 #define RSB_CMD 0x2c /* RSB Command */ 62 #define RSB_DAR 0x30 /* Device address / runtime address */ 63 64 /* CTRL fields */ 65 #define RSB_CTRL_START_TRANS BIT(7) 66 #define RSB_CTRL_ABORT_TRANS BIT(6) 67 #define RSB_CTRL_GLOBAL_INT_ENB BIT(1) 68 #define RSB_CTRL_SOFT_RST BIT(0) 69 70 /* CLK CTRL fields */ 71 #define RSB_CCR_SDA_OUT_DELAY(v) (((v) & 0x7) << 8) 72 #define RSB_CCR_MAX_CLK_DIV 0xff 73 #define RSB_CCR_CLK_DIV(v) ((v) & RSB_CCR_MAX_CLK_DIV) 74 75 /* STATUS fields */ 76 #define RSB_INTS_TRANS_ERR_ACK BIT(16) 77 #define RSB_INTS_TRANS_ERR_DATA_BIT(v) (((v) >> 8) & 0xf) 78 #define RSB_INTS_TRANS_ERR_DATA GENMASK(11, 8) 79 #define RSB_INTS_LOAD_BSY BIT(2) 80 #define RSB_INTS_TRANS_ERR BIT(1) 81 #define RSB_INTS_TRANS_OVER BIT(0) 82 83 /* LINE CTRL fields*/ 84 #define RSB_LCR_SCL_STATE BIT(5) 85 #define RSB_LCR_SDA_STATE BIT(4) 86 #define RSB_LCR_SCL_CTL BIT(3) 87 #define RSB_LCR_SCL_CTL_EN BIT(2) 88 #define RSB_LCR_SDA_CTL BIT(1) 89 #define RSB_LCR_SDA_CTL_EN BIT(0) 90 91 /* DEVICE MODE CTRL field values */ 92 #define RSB_DMCR_DEVICE_START BIT(31) 93 #define RSB_DMCR_MODE_DATA (0x7c << 16) 94 #define RSB_DMCR_MODE_REG (0x3e << 8) 95 #define RSB_DMCR_DEV_ADDR 0x00 96 97 /* CMD values */ 98 #define RSB_CMD_RD8 0x8b 99 #define RSB_CMD_RD16 0x9c 100 #define RSB_CMD_RD32 0xa6 101 #define RSB_CMD_WR8 0x4e 102 #define RSB_CMD_WR16 0x59 103 #define RSB_CMD_WR32 0x63 104 #define RSB_CMD_STRA 0xe8 105 106 /* DAR fields */ 107 #define RSB_DAR_RTA(v) (((v) & 0xff) << 16) 108 #define RSB_DAR_DA(v) ((v) & 0xffff) 109 110 #define RSB_MAX_FREQ 20000000 111 112 #define RSB_CTRL_NAME "sunxi-rsb" 113 114 struct sunxi_rsb_addr_map { 115 u16 hwaddr; 116 u8 rtaddr; 117 }; 118 119 struct sunxi_rsb { 120 struct device *dev; 121 void __iomem *regs; 122 struct clk *clk; 123 struct reset_control *rstc; 124 struct completion complete; 125 struct mutex lock; 126 unsigned int status; 127 u32 clk_freq; 128 }; 129 130 /* bus / slave device related functions */ 131 static const struct bus_type sunxi_rsb_bus; 132 133 static int sunxi_rsb_device_match(struct device *dev, const struct device_driver *drv) 134 { 135 return of_driver_match_device(dev, drv); 136 } 137 138 static int sunxi_rsb_device_probe(struct device *dev) 139 { 140 const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver); 141 struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev); 142 int ret; 143 144 if (!drv->probe) 145 return -ENODEV; 146 147 if (!rdev->irq) { 148 int irq = -ENOENT; 149 150 if (dev->of_node) 151 irq = of_irq_get(dev->of_node, 0); 152 153 if (irq == -EPROBE_DEFER) 154 return irq; 155 if (irq < 0) 156 irq = 0; 157 158 rdev->irq = irq; 159 } 160 161 ret = of_clk_set_defaults(dev->of_node, false); 162 if (ret < 0) 163 return ret; 164 165 return drv->probe(rdev); 166 } 167 168 static void sunxi_rsb_device_remove(struct device *dev) 169 { 170 const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver); 171 172 drv->remove(to_sunxi_rsb_device(dev)); 173 } 174 175 static int sunxi_rsb_device_modalias(const struct device *dev, struct kobj_uevent_env *env) 176 { 177 return of_device_uevent_modalias(dev, env); 178 } 179 180 static const struct bus_type sunxi_rsb_bus = { 181 .name = RSB_CTRL_NAME, 182 .match = sunxi_rsb_device_match, 183 .probe = sunxi_rsb_device_probe, 184 .remove = sunxi_rsb_device_remove, 185 .uevent = sunxi_rsb_device_modalias, 186 }; 187 188 static void sunxi_rsb_dev_release(struct device *dev) 189 { 190 struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev); 191 192 kfree(rdev); 193 } 194 195 /** 196 * sunxi_rsb_device_create() - allocate and add an RSB device 197 * @rsb: RSB controller 198 * @node: RSB slave device node 199 * @hwaddr: RSB slave hardware address 200 * @rtaddr: RSB slave runtime address 201 */ 202 static struct sunxi_rsb_device *sunxi_rsb_device_create(struct sunxi_rsb *rsb, 203 struct device_node *node, u16 hwaddr, u8 rtaddr) 204 { 205 int err; 206 struct sunxi_rsb_device *rdev; 207 208 rdev = kzalloc_obj(*rdev); 209 if (!rdev) 210 return ERR_PTR(-ENOMEM); 211 212 rdev->rsb = rsb; 213 rdev->hwaddr = hwaddr; 214 rdev->rtaddr = rtaddr; 215 rdev->dev.bus = &sunxi_rsb_bus; 216 rdev->dev.parent = rsb->dev; 217 rdev->dev.of_node = node; 218 rdev->dev.release = sunxi_rsb_dev_release; 219 220 dev_set_name(&rdev->dev, "%s-%x", RSB_CTRL_NAME, hwaddr); 221 222 err = device_register(&rdev->dev); 223 if (err < 0) { 224 dev_err(&rdev->dev, "Can't add %s, status %d\n", 225 dev_name(&rdev->dev), err); 226 goto err_device_add; 227 } 228 229 dev_dbg(&rdev->dev, "device %s registered\n", dev_name(&rdev->dev)); 230 231 return rdev; 232 233 err_device_add: 234 put_device(&rdev->dev); 235 236 return ERR_PTR(err); 237 } 238 239 /** 240 * sunxi_rsb_device_unregister(): unregister an RSB device 241 * @rdev: rsb_device to be removed 242 */ 243 static void sunxi_rsb_device_unregister(struct sunxi_rsb_device *rdev) 244 { 245 device_unregister(&rdev->dev); 246 } 247 248 static int sunxi_rsb_remove_devices(struct device *dev, void *data) 249 { 250 struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev); 251 252 if (dev->bus == &sunxi_rsb_bus) 253 sunxi_rsb_device_unregister(rdev); 254 255 return 0; 256 } 257 258 /** 259 * sunxi_rsb_driver_register() - Register device driver with RSB core 260 * @rdrv: device driver to be associated with slave-device. 261 * 262 * This API will register the client driver with the RSB framework. 263 * It is typically called from the driver's module-init function. 264 */ 265 int sunxi_rsb_driver_register(struct sunxi_rsb_driver *rdrv) 266 { 267 rdrv->driver.bus = &sunxi_rsb_bus; 268 return driver_register(&rdrv->driver); 269 } 270 EXPORT_SYMBOL_GPL(sunxi_rsb_driver_register); 271 272 /* common code that starts a transfer */ 273 static int _sunxi_rsb_run_xfer(struct sunxi_rsb *rsb) 274 { 275 u32 int_mask, status; 276 bool timeout; 277 278 if (readl(rsb->regs + RSB_CTRL) & RSB_CTRL_START_TRANS) { 279 dev_dbg(rsb->dev, "RSB transfer still in progress\n"); 280 return -EBUSY; 281 } 282 283 reinit_completion(&rsb->complete); 284 285 int_mask = RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR | RSB_INTS_TRANS_OVER; 286 writel(int_mask, rsb->regs + RSB_INTE); 287 writel(RSB_CTRL_START_TRANS | RSB_CTRL_GLOBAL_INT_ENB, 288 rsb->regs + RSB_CTRL); 289 290 if (irqs_disabled()) { 291 timeout = readl_poll_timeout_atomic(rsb->regs + RSB_INTS, 292 status, (status & int_mask), 293 10, 100000); 294 writel(status, rsb->regs + RSB_INTS); 295 } else { 296 timeout = !wait_for_completion_io_timeout(&rsb->complete, 297 msecs_to_jiffies(100)); 298 status = rsb->status; 299 } 300 301 if (timeout) { 302 dev_dbg(rsb->dev, "RSB timeout\n"); 303 304 /* abort the transfer */ 305 writel(RSB_CTRL_ABORT_TRANS, rsb->regs + RSB_CTRL); 306 307 /* clear any interrupt flags */ 308 writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS); 309 310 return -ETIMEDOUT; 311 } 312 313 if (status & RSB_INTS_LOAD_BSY) { 314 dev_dbg(rsb->dev, "RSB busy\n"); 315 return -EBUSY; 316 } 317 318 if (status & RSB_INTS_TRANS_ERR) { 319 if (status & RSB_INTS_TRANS_ERR_ACK) { 320 dev_dbg(rsb->dev, "RSB slave nack\n"); 321 return -EINVAL; 322 } 323 324 if (status & RSB_INTS_TRANS_ERR_DATA) { 325 dev_dbg(rsb->dev, "RSB transfer data error\n"); 326 return -EIO; 327 } 328 } 329 330 return 0; 331 } 332 333 static int sunxi_rsb_read(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr, 334 u32 *buf, size_t len) 335 { 336 u32 cmd; 337 int ret; 338 339 if (!buf) 340 return -EINVAL; 341 342 switch (len) { 343 case 1: 344 cmd = RSB_CMD_RD8; 345 break; 346 case 2: 347 cmd = RSB_CMD_RD16; 348 break; 349 case 4: 350 cmd = RSB_CMD_RD32; 351 break; 352 default: 353 dev_err(rsb->dev, "Invalid access width: %zd\n", len); 354 return -EINVAL; 355 } 356 357 ret = pm_runtime_resume_and_get(rsb->dev); 358 if (ret) 359 return ret; 360 361 mutex_lock(&rsb->lock); 362 363 writel(addr, rsb->regs + RSB_ADDR); 364 writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR); 365 writel(cmd, rsb->regs + RSB_CMD); 366 367 ret = _sunxi_rsb_run_xfer(rsb); 368 if (ret) 369 goto unlock; 370 371 *buf = readl(rsb->regs + RSB_DATA) & GENMASK(len * 8 - 1, 0); 372 373 unlock: 374 mutex_unlock(&rsb->lock); 375 376 pm_runtime_put_autosuspend(rsb->dev); 377 378 return ret; 379 } 380 381 static int sunxi_rsb_write(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr, 382 const u32 *buf, size_t len) 383 { 384 u32 cmd; 385 int ret; 386 387 if (!buf) 388 return -EINVAL; 389 390 switch (len) { 391 case 1: 392 cmd = RSB_CMD_WR8; 393 break; 394 case 2: 395 cmd = RSB_CMD_WR16; 396 break; 397 case 4: 398 cmd = RSB_CMD_WR32; 399 break; 400 default: 401 dev_err(rsb->dev, "Invalid access width: %zd\n", len); 402 return -EINVAL; 403 } 404 405 ret = pm_runtime_resume_and_get(rsb->dev); 406 if (ret) 407 return ret; 408 409 mutex_lock(&rsb->lock); 410 411 writel(addr, rsb->regs + RSB_ADDR); 412 writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR); 413 writel(*buf, rsb->regs + RSB_DATA); 414 writel(cmd, rsb->regs + RSB_CMD); 415 ret = _sunxi_rsb_run_xfer(rsb); 416 417 mutex_unlock(&rsb->lock); 418 419 pm_runtime_put_autosuspend(rsb->dev); 420 421 return ret; 422 } 423 424 /* RSB regmap functions */ 425 struct sunxi_rsb_ctx { 426 struct sunxi_rsb_device *rdev; 427 int size; 428 }; 429 430 static int regmap_sunxi_rsb_reg_read(void *context, unsigned int reg, 431 unsigned int *val) 432 { 433 struct sunxi_rsb_ctx *ctx = context; 434 struct sunxi_rsb_device *rdev = ctx->rdev; 435 436 if (reg > 0xff) 437 return -EINVAL; 438 439 return sunxi_rsb_read(rdev->rsb, rdev->rtaddr, reg, val, ctx->size); 440 } 441 442 static int regmap_sunxi_rsb_reg_write(void *context, unsigned int reg, 443 unsigned int val) 444 { 445 struct sunxi_rsb_ctx *ctx = context; 446 struct sunxi_rsb_device *rdev = ctx->rdev; 447 448 if (reg > 0xff) 449 return -EINVAL; 450 451 return sunxi_rsb_write(rdev->rsb, rdev->rtaddr, reg, &val, ctx->size); 452 } 453 454 static void regmap_sunxi_rsb_free_ctx(void *context) 455 { 456 struct sunxi_rsb_ctx *ctx = context; 457 458 kfree(ctx); 459 } 460 461 static const struct regmap_bus regmap_sunxi_rsb = { 462 .reg_write = regmap_sunxi_rsb_reg_write, 463 .reg_read = regmap_sunxi_rsb_reg_read, 464 .free_context = regmap_sunxi_rsb_free_ctx, 465 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE, 466 .val_format_endian_default = REGMAP_ENDIAN_NATIVE, 467 }; 468 469 static struct sunxi_rsb_ctx *regmap_sunxi_rsb_init_ctx(struct sunxi_rsb_device *rdev, 470 const struct regmap_config *config) 471 { 472 struct sunxi_rsb_ctx *ctx; 473 474 switch (config->val_bits) { 475 case 8: 476 case 16: 477 case 32: 478 break; 479 default: 480 return ERR_PTR(-EINVAL); 481 } 482 483 ctx = kzalloc_obj(*ctx); 484 if (!ctx) 485 return ERR_PTR(-ENOMEM); 486 487 ctx->rdev = rdev; 488 ctx->size = config->val_bits / 8; 489 490 return ctx; 491 } 492 493 struct regmap *__devm_regmap_init_sunxi_rsb(struct sunxi_rsb_device *rdev, 494 const struct regmap_config *config, 495 struct lock_class_key *lock_key, 496 const char *lock_name) 497 { 498 struct sunxi_rsb_ctx *ctx = regmap_sunxi_rsb_init_ctx(rdev, config); 499 500 if (IS_ERR(ctx)) 501 return ERR_CAST(ctx); 502 503 return __devm_regmap_init(&rdev->dev, ®map_sunxi_rsb, ctx, config, 504 lock_key, lock_name); 505 } 506 EXPORT_SYMBOL_GPL(__devm_regmap_init_sunxi_rsb); 507 508 /* RSB controller driver functions */ 509 static irqreturn_t sunxi_rsb_irq(int irq, void *dev_id) 510 { 511 struct sunxi_rsb *rsb = dev_id; 512 u32 status; 513 514 status = readl(rsb->regs + RSB_INTS); 515 rsb->status = status; 516 517 /* Clear interrupts */ 518 status &= (RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR | 519 RSB_INTS_TRANS_OVER); 520 writel(status, rsb->regs + RSB_INTS); 521 522 complete(&rsb->complete); 523 524 return IRQ_HANDLED; 525 } 526 527 static int sunxi_rsb_init_device_mode(struct sunxi_rsb *rsb) 528 { 529 int ret = 0; 530 u32 reg; 531 532 /* send init sequence */ 533 writel(RSB_DMCR_DEVICE_START | RSB_DMCR_MODE_DATA | 534 RSB_DMCR_MODE_REG | RSB_DMCR_DEV_ADDR, rsb->regs + RSB_DMCR); 535 536 readl_poll_timeout(rsb->regs + RSB_DMCR, reg, 537 !(reg & RSB_DMCR_DEVICE_START), 100, 250000); 538 if (reg & RSB_DMCR_DEVICE_START) 539 ret = -ETIMEDOUT; 540 541 /* clear interrupt status bits */ 542 writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS); 543 544 return ret; 545 } 546 547 /* 548 * There are 15 valid runtime addresses, though Allwinner typically 549 * skips the first, for unknown reasons, and uses the following three. 550 * 551 * 0x17, 0x2d, 0x3a, 0x4e, 0x59, 0x63, 0x74, 0x8b, 552 * 0x9c, 0xa6, 0xb1, 0xc5, 0xd2, 0xe8, 0xff 553 * 554 * No designs with 2 RSB slave devices sharing identical hardware 555 * addresses on the same bus have been seen in the wild. All designs 556 * use 0x2d for the primary PMIC, 0x3a for the secondary PMIC if 557 * there is one, and 0x45 for peripheral ICs. 558 * 559 * The hardware does not seem to support re-setting runtime addresses. 560 * Attempts to do so result in the slave devices returning a NACK. 561 * Hence we just hardcode the mapping here, like Allwinner does. 562 */ 563 564 static const struct sunxi_rsb_addr_map sunxi_rsb_addr_maps[] = { 565 { 0x3a3, 0x2d }, /* Primary PMIC: AXP223, AXP809, AXP81X, ... */ 566 { 0x745, 0x3a }, /* Secondary PMIC: AXP806, ... */ 567 { 0xe89, 0x4e }, /* Peripheral IC: AC100, ... */ 568 }; 569 570 static u8 sunxi_rsb_get_rtaddr(u16 hwaddr) 571 { 572 int i; 573 574 for (i = 0; i < ARRAY_SIZE(sunxi_rsb_addr_maps); i++) 575 if (hwaddr == sunxi_rsb_addr_maps[i].hwaddr) 576 return sunxi_rsb_addr_maps[i].rtaddr; 577 578 return 0; /* 0 is an invalid runtime address */ 579 } 580 581 static int of_rsb_register_devices(struct sunxi_rsb *rsb) 582 { 583 struct device *dev = rsb->dev; 584 struct device_node *child, *np = dev->of_node; 585 u32 hwaddr; 586 u8 rtaddr; 587 int ret; 588 589 if (!np) 590 return -EINVAL; 591 592 /* Runtime addresses for all slaves should be set first */ 593 for_each_available_child_of_node(np, child) { 594 dev_dbg(dev, "setting child %pOF runtime address\n", 595 child); 596 597 ret = of_property_read_u32(child, "reg", &hwaddr); 598 if (ret) { 599 dev_err(dev, "%pOF: invalid 'reg' property: %d\n", 600 child, ret); 601 continue; 602 } 603 604 rtaddr = sunxi_rsb_get_rtaddr(hwaddr); 605 if (!rtaddr) { 606 dev_err(dev, "%pOF: unknown hardware device address\n", 607 child); 608 continue; 609 } 610 611 /* 612 * Since no devices have been registered yet, we are the 613 * only ones using the bus, we can skip locking the bus. 614 */ 615 616 /* setup command parameters */ 617 writel(RSB_CMD_STRA, rsb->regs + RSB_CMD); 618 writel(RSB_DAR_RTA(rtaddr) | RSB_DAR_DA(hwaddr), 619 rsb->regs + RSB_DAR); 620 621 /* send command */ 622 ret = _sunxi_rsb_run_xfer(rsb); 623 if (ret) 624 dev_warn(dev, "%pOF: set runtime address failed: %d\n", 625 child, ret); 626 } 627 628 /* Then we start adding devices and probing them */ 629 for_each_available_child_of_node(np, child) { 630 struct sunxi_rsb_device *rdev; 631 632 dev_dbg(dev, "adding child %pOF\n", child); 633 634 ret = of_property_read_u32(child, "reg", &hwaddr); 635 if (ret) 636 continue; 637 638 rtaddr = sunxi_rsb_get_rtaddr(hwaddr); 639 if (!rtaddr) 640 continue; 641 642 rdev = sunxi_rsb_device_create(rsb, child, hwaddr, rtaddr); 643 if (IS_ERR(rdev)) 644 dev_err(dev, "failed to add child device %pOF: %ld\n", 645 child, PTR_ERR(rdev)); 646 } 647 648 return 0; 649 } 650 651 static int sunxi_rsb_hw_init(struct sunxi_rsb *rsb) 652 { 653 struct device *dev = rsb->dev; 654 unsigned long p_clk_freq; 655 u32 clk_delay, reg; 656 int clk_div, ret; 657 658 ret = clk_prepare_enable(rsb->clk); 659 if (ret) { 660 dev_err(dev, "failed to enable clk: %d\n", ret); 661 return ret; 662 } 663 664 ret = reset_control_deassert(rsb->rstc); 665 if (ret) { 666 dev_err(dev, "failed to deassert reset line: %d\n", ret); 667 goto err_clk_disable; 668 } 669 670 /* reset the controller */ 671 writel(RSB_CTRL_SOFT_RST, rsb->regs + RSB_CTRL); 672 readl_poll_timeout(rsb->regs + RSB_CTRL, reg, 673 !(reg & RSB_CTRL_SOFT_RST), 1000, 100000); 674 675 /* 676 * Clock frequency and delay calculation code is from 677 * Allwinner U-boot sources. 678 * 679 * From A83 user manual: 680 * bus clock frequency = parent clock frequency / (2 * (divider + 1)) 681 */ 682 p_clk_freq = clk_get_rate(rsb->clk); 683 clk_div = p_clk_freq / rsb->clk_freq / 2; 684 if (!clk_div) 685 clk_div = 1; 686 else if (clk_div > RSB_CCR_MAX_CLK_DIV + 1) 687 clk_div = RSB_CCR_MAX_CLK_DIV + 1; 688 689 clk_delay = clk_div >> 1; 690 if (!clk_delay) 691 clk_delay = 1; 692 693 dev_info(dev, "RSB running at %lu Hz\n", p_clk_freq / clk_div / 2); 694 writel(RSB_CCR_SDA_OUT_DELAY(clk_delay) | RSB_CCR_CLK_DIV(clk_div - 1), 695 rsb->regs + RSB_CCR); 696 697 return 0; 698 699 err_clk_disable: 700 clk_disable_unprepare(rsb->clk); 701 702 return ret; 703 } 704 705 static void sunxi_rsb_hw_exit(struct sunxi_rsb *rsb) 706 { 707 reset_control_assert(rsb->rstc); 708 709 /* Keep the clock and PM reference counts consistent. */ 710 if (!pm_runtime_status_suspended(rsb->dev)) 711 clk_disable_unprepare(rsb->clk); 712 } 713 714 static int __maybe_unused sunxi_rsb_runtime_suspend(struct device *dev) 715 { 716 struct sunxi_rsb *rsb = dev_get_drvdata(dev); 717 718 clk_disable_unprepare(rsb->clk); 719 720 return 0; 721 } 722 723 static int __maybe_unused sunxi_rsb_runtime_resume(struct device *dev) 724 { 725 struct sunxi_rsb *rsb = dev_get_drvdata(dev); 726 727 return clk_prepare_enable(rsb->clk); 728 } 729 730 static int __maybe_unused sunxi_rsb_suspend(struct device *dev) 731 { 732 struct sunxi_rsb *rsb = dev_get_drvdata(dev); 733 734 sunxi_rsb_hw_exit(rsb); 735 736 return 0; 737 } 738 739 static int __maybe_unused sunxi_rsb_resume(struct device *dev) 740 { 741 struct sunxi_rsb *rsb = dev_get_drvdata(dev); 742 743 return sunxi_rsb_hw_init(rsb); 744 } 745 746 static int sunxi_rsb_probe(struct platform_device *pdev) 747 { 748 struct device *dev = &pdev->dev; 749 struct device_node *np = dev->of_node; 750 struct sunxi_rsb *rsb; 751 u32 clk_freq = 3000000; 752 int irq, ret; 753 754 of_property_read_u32(np, "clock-frequency", &clk_freq); 755 if (clk_freq > RSB_MAX_FREQ) 756 return dev_err_probe(dev, -EINVAL, 757 "clock-frequency (%u Hz) is too high (max = 20MHz)\n", 758 clk_freq); 759 760 rsb = devm_kzalloc(dev, sizeof(*rsb), GFP_KERNEL); 761 if (!rsb) 762 return -ENOMEM; 763 764 rsb->dev = dev; 765 rsb->clk_freq = clk_freq; 766 platform_set_drvdata(pdev, rsb); 767 rsb->regs = devm_platform_ioremap_resource(pdev, 0); 768 if (IS_ERR(rsb->regs)) 769 return PTR_ERR(rsb->regs); 770 771 irq = platform_get_irq(pdev, 0); 772 if (irq < 0) 773 return irq; 774 775 rsb->clk = devm_clk_get(dev, NULL); 776 if (IS_ERR(rsb->clk)) 777 return dev_err_probe(dev, PTR_ERR(rsb->clk), 778 "failed to retrieve clk\n"); 779 780 rsb->rstc = devm_reset_control_get(dev, NULL); 781 if (IS_ERR(rsb->rstc)) 782 return dev_err_probe(dev, PTR_ERR(rsb->rstc), 783 "failed to retrieve reset controller\n"); 784 785 init_completion(&rsb->complete); 786 mutex_init(&rsb->lock); 787 788 ret = devm_request_irq(dev, irq, sunxi_rsb_irq, 0, RSB_CTRL_NAME, rsb); 789 if (ret) 790 return dev_err_probe(dev, ret, 791 "can't register interrupt handler irq %d\n", irq); 792 793 ret = sunxi_rsb_hw_init(rsb); 794 if (ret) 795 return ret; 796 797 /* initialize all devices on the bus into RSB mode */ 798 ret = sunxi_rsb_init_device_mode(rsb); 799 if (ret) 800 dev_warn(dev, "Initialize device mode failed: %d\n", ret); 801 802 pm_suspend_ignore_children(dev, true); 803 pm_runtime_set_active(dev); 804 pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC); 805 pm_runtime_use_autosuspend(dev); 806 pm_runtime_enable(dev); 807 808 of_rsb_register_devices(rsb); 809 810 return 0; 811 } 812 813 static void sunxi_rsb_remove(struct platform_device *pdev) 814 { 815 struct sunxi_rsb *rsb = platform_get_drvdata(pdev); 816 817 device_for_each_child(rsb->dev, NULL, sunxi_rsb_remove_devices); 818 pm_runtime_disable(&pdev->dev); 819 sunxi_rsb_hw_exit(rsb); 820 } 821 822 static const struct dev_pm_ops sunxi_rsb_dev_pm_ops = { 823 SET_RUNTIME_PM_OPS(sunxi_rsb_runtime_suspend, 824 sunxi_rsb_runtime_resume, NULL) 825 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sunxi_rsb_suspend, sunxi_rsb_resume) 826 }; 827 828 static const struct of_device_id sunxi_rsb_of_match_table[] = { 829 { .compatible = "allwinner,sun8i-a23-rsb" }, 830 {} 831 }; 832 MODULE_DEVICE_TABLE(of, sunxi_rsb_of_match_table); 833 834 static struct platform_driver sunxi_rsb_driver = { 835 .probe = sunxi_rsb_probe, 836 .remove = sunxi_rsb_remove, 837 .driver = { 838 .name = RSB_CTRL_NAME, 839 .of_match_table = sunxi_rsb_of_match_table, 840 .pm = &sunxi_rsb_dev_pm_ops, 841 }, 842 }; 843 844 static int __init sunxi_rsb_init(void) 845 { 846 int ret; 847 848 ret = bus_register(&sunxi_rsb_bus); 849 if (ret) { 850 pr_err("failed to register sunxi sunxi_rsb bus: %d\n", ret); 851 return ret; 852 } 853 854 ret = platform_driver_register(&sunxi_rsb_driver); 855 if (ret) { 856 bus_unregister(&sunxi_rsb_bus); 857 return ret; 858 } 859 860 return 0; 861 } 862 module_init(sunxi_rsb_init); 863 864 static void __exit sunxi_rsb_exit(void) 865 { 866 platform_driver_unregister(&sunxi_rsb_driver); 867 bus_unregister(&sunxi_rsb_bus); 868 } 869 module_exit(sunxi_rsb_exit); 870 871 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>"); 872 MODULE_DESCRIPTION("Allwinner sunXi Reduced Serial Bus controller driver"); 873 MODULE_LICENSE("GPL v2"); 874