Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * RSB (Reduced Serial Bus) driver.
0004  *
0005  * Author: Chen-Yu Tsai <wens@csie.org>
0006  *
0007  * The RSB controller looks like an SMBus controller which only supports
0008  * byte and word data transfers. But, it differs from standard SMBus
0009  * protocol on several aspects:
0010  * - it uses addresses set at runtime to address slaves. Runtime addresses
0011  *   are sent to slaves using their 12bit hardware addresses. Up to 15
0012  *   runtime addresses are available.
0013  * - it adds a parity bit every 8bits of data and address for read and
0014  *   write accesses; this replaces the ack bit
0015  * - only one read access is required to read a byte (instead of a write
0016  *   followed by a read access in standard SMBus protocol)
0017  * - there's no Ack bit after each read access
0018  *
0019  * This means this bus cannot be used to interface with standard SMBus
0020  * devices. Devices known to support this interface include the AXP223,
0021  * AXP809, and AXP806 PMICs, and the AC100 audio codec, all from X-Powers.
0022  *
0023  * A description of the operation and wire protocol can be found in the
0024  * RSB section of Allwinner's A80 user manual, which can be found at
0025  *
0026  *     https://github.com/allwinner-zh/documents/tree/master/A80
0027  *
0028  * This document is officially released by Allwinner.
0029  *
0030  * This driver is based on i2c-sun6i-p2wi.c, the P2WI bus driver.
0031  */
0032 
0033 #include <linux/clk.h>
0034 #include <linux/clk/clk-conf.h>
0035 #include <linux/device.h>
0036 #include <linux/interrupt.h>
0037 #include <linux/io.h>
0038 #include <linux/iopoll.h>
0039 #include <linux/module.h>
0040 #include <linux/of.h>
0041 #include <linux/of_irq.h>
0042 #include <linux/of_platform.h>
0043 #include <linux/platform_device.h>
0044 #include <linux/pm.h>
0045 #include <linux/pm_runtime.h>
0046 #include <linux/regmap.h>
0047 #include <linux/reset.h>
0048 #include <linux/slab.h>
0049 #include <linux/sunxi-rsb.h>
0050 #include <linux/types.h>
0051 
0052 /* RSB registers */
0053 #define RSB_CTRL    0x0 /* Global control */
0054 #define RSB_CCR     0x4 /* Clock control */
0055 #define RSB_INTE    0x8 /* Interrupt controls */
0056 #define RSB_INTS    0xc /* Interrupt status */
0057 #define RSB_ADDR    0x10    /* Address to send with read/write command */
0058 #define RSB_DATA    0x1c    /* Data to read/write */
0059 #define RSB_LCR     0x24    /* Line control */
0060 #define RSB_DMCR    0x28    /* Device mode (init) control */
0061 #define RSB_CMD     0x2c    /* RSB Command */
0062 #define RSB_DAR     0x30    /* Device address / runtime address */
0063 
0064 /* CTRL fields */
0065 #define RSB_CTRL_START_TRANS        BIT(7)
0066 #define RSB_CTRL_ABORT_TRANS        BIT(6)
0067 #define RSB_CTRL_GLOBAL_INT_ENB     BIT(1)
0068 #define RSB_CTRL_SOFT_RST       BIT(0)
0069 
0070 /* CLK CTRL fields */
0071 #define RSB_CCR_SDA_OUT_DELAY(v)    (((v) & 0x7) << 8)
0072 #define RSB_CCR_MAX_CLK_DIV     0xff
0073 #define RSB_CCR_CLK_DIV(v)      ((v) & RSB_CCR_MAX_CLK_DIV)
0074 
0075 /* STATUS fields */
0076 #define RSB_INTS_TRANS_ERR_ACK      BIT(16)
0077 #define RSB_INTS_TRANS_ERR_DATA_BIT(v)  (((v) >> 8) & 0xf)
0078 #define RSB_INTS_TRANS_ERR_DATA     GENMASK(11, 8)
0079 #define RSB_INTS_LOAD_BSY       BIT(2)
0080 #define RSB_INTS_TRANS_ERR      BIT(1)
0081 #define RSB_INTS_TRANS_OVER     BIT(0)
0082 
0083 /* LINE CTRL fields*/
0084 #define RSB_LCR_SCL_STATE       BIT(5)
0085 #define RSB_LCR_SDA_STATE       BIT(4)
0086 #define RSB_LCR_SCL_CTL         BIT(3)
0087 #define RSB_LCR_SCL_CTL_EN      BIT(2)
0088 #define RSB_LCR_SDA_CTL         BIT(1)
0089 #define RSB_LCR_SDA_CTL_EN      BIT(0)
0090 
0091 /* DEVICE MODE CTRL field values */
0092 #define RSB_DMCR_DEVICE_START       BIT(31)
0093 #define RSB_DMCR_MODE_DATA      (0x7c << 16)
0094 #define RSB_DMCR_MODE_REG       (0x3e << 8)
0095 #define RSB_DMCR_DEV_ADDR       0x00
0096 
0097 /* CMD values */
0098 #define RSB_CMD_RD8         0x8b
0099 #define RSB_CMD_RD16            0x9c
0100 #define RSB_CMD_RD32            0xa6
0101 #define RSB_CMD_WR8         0x4e
0102 #define RSB_CMD_WR16            0x59
0103 #define RSB_CMD_WR32            0x63
0104 #define RSB_CMD_STRA            0xe8
0105 
0106 /* DAR fields */
0107 #define RSB_DAR_RTA(v)          (((v) & 0xff) << 16)
0108 #define RSB_DAR_DA(v)           ((v) & 0xffff)
0109 
0110 #define RSB_MAX_FREQ            20000000
0111 
0112 #define RSB_CTRL_NAME           "sunxi-rsb"
0113 
0114 struct sunxi_rsb_addr_map {
0115     u16 hwaddr;
0116     u8 rtaddr;
0117 };
0118 
0119 struct sunxi_rsb {
0120     struct device *dev;
0121     void __iomem *regs;
0122     struct clk *clk;
0123     struct reset_control *rstc;
0124     struct completion complete;
0125     struct mutex lock;
0126     unsigned int status;
0127     u32 clk_freq;
0128 };
0129 
0130 /* bus / slave device related functions */
0131 static struct bus_type sunxi_rsb_bus;
0132 
0133 static int sunxi_rsb_device_match(struct device *dev, struct device_driver *drv)
0134 {
0135     return of_driver_match_device(dev, drv);
0136 }
0137 
0138 static int sunxi_rsb_device_probe(struct device *dev)
0139 {
0140     const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver);
0141     struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
0142     int ret;
0143 
0144     if (!drv->probe)
0145         return -ENODEV;
0146 
0147     if (!rdev->irq) {
0148         int irq = -ENOENT;
0149 
0150         if (dev->of_node)
0151             irq = of_irq_get(dev->of_node, 0);
0152 
0153         if (irq == -EPROBE_DEFER)
0154             return irq;
0155         if (irq < 0)
0156             irq = 0;
0157 
0158         rdev->irq = irq;
0159     }
0160 
0161     ret = of_clk_set_defaults(dev->of_node, false);
0162     if (ret < 0)
0163         return ret;
0164 
0165     return drv->probe(rdev);
0166 }
0167 
0168 static void sunxi_rsb_device_remove(struct device *dev)
0169 {
0170     const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver);
0171 
0172     drv->remove(to_sunxi_rsb_device(dev));
0173 }
0174 
0175 static struct bus_type sunxi_rsb_bus = {
0176     .name       = RSB_CTRL_NAME,
0177     .match      = sunxi_rsb_device_match,
0178     .probe      = sunxi_rsb_device_probe,
0179     .remove     = sunxi_rsb_device_remove,
0180     .uevent     = of_device_uevent_modalias,
0181 };
0182 
0183 static void sunxi_rsb_dev_release(struct device *dev)
0184 {
0185     struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
0186 
0187     kfree(rdev);
0188 }
0189 
0190 /**
0191  * sunxi_rsb_device_create() - allocate and add an RSB device
0192  * @rsb:    RSB controller
0193  * @node:   RSB slave device node
0194  * @hwaddr: RSB slave hardware address
0195  * @rtaddr: RSB slave runtime address
0196  */
0197 static struct sunxi_rsb_device *sunxi_rsb_device_create(struct sunxi_rsb *rsb,
0198         struct device_node *node, u16 hwaddr, u8 rtaddr)
0199 {
0200     int err;
0201     struct sunxi_rsb_device *rdev;
0202 
0203     rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
0204     if (!rdev)
0205         return ERR_PTR(-ENOMEM);
0206 
0207     rdev->rsb = rsb;
0208     rdev->hwaddr = hwaddr;
0209     rdev->rtaddr = rtaddr;
0210     rdev->dev.bus = &sunxi_rsb_bus;
0211     rdev->dev.parent = rsb->dev;
0212     rdev->dev.of_node = node;
0213     rdev->dev.release = sunxi_rsb_dev_release;
0214 
0215     dev_set_name(&rdev->dev, "%s-%x", RSB_CTRL_NAME, hwaddr);
0216 
0217     err = device_register(&rdev->dev);
0218     if (err < 0) {
0219         dev_err(&rdev->dev, "Can't add %s, status %d\n",
0220             dev_name(&rdev->dev), err);
0221         goto err_device_add;
0222     }
0223 
0224     dev_dbg(&rdev->dev, "device %s registered\n", dev_name(&rdev->dev));
0225 
0226     return rdev;
0227 
0228 err_device_add:
0229     put_device(&rdev->dev);
0230 
0231     return ERR_PTR(err);
0232 }
0233 
0234 /**
0235  * sunxi_rsb_device_unregister(): unregister an RSB device
0236  * @rdev:   rsb_device to be removed
0237  */
0238 static void sunxi_rsb_device_unregister(struct sunxi_rsb_device *rdev)
0239 {
0240     device_unregister(&rdev->dev);
0241 }
0242 
0243 static int sunxi_rsb_remove_devices(struct device *dev, void *data)
0244 {
0245     struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
0246 
0247     if (dev->bus == &sunxi_rsb_bus)
0248         sunxi_rsb_device_unregister(rdev);
0249 
0250     return 0;
0251 }
0252 
0253 /**
0254  * sunxi_rsb_driver_register() - Register device driver with RSB core
0255  * @rdrv:   device driver to be associated with slave-device.
0256  *
0257  * This API will register the client driver with the RSB framework.
0258  * It is typically called from the driver's module-init function.
0259  */
0260 int sunxi_rsb_driver_register(struct sunxi_rsb_driver *rdrv)
0261 {
0262     rdrv->driver.bus = &sunxi_rsb_bus;
0263     return driver_register(&rdrv->driver);
0264 }
0265 EXPORT_SYMBOL_GPL(sunxi_rsb_driver_register);
0266 
0267 /* common code that starts a transfer */
0268 static int _sunxi_rsb_run_xfer(struct sunxi_rsb *rsb)
0269 {
0270     if (readl(rsb->regs + RSB_CTRL) & RSB_CTRL_START_TRANS) {
0271         dev_dbg(rsb->dev, "RSB transfer still in progress\n");
0272         return -EBUSY;
0273     }
0274 
0275     reinit_completion(&rsb->complete);
0276 
0277     writel(RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR | RSB_INTS_TRANS_OVER,
0278            rsb->regs + RSB_INTE);
0279     writel(RSB_CTRL_START_TRANS | RSB_CTRL_GLOBAL_INT_ENB,
0280            rsb->regs + RSB_CTRL);
0281 
0282     if (!wait_for_completion_io_timeout(&rsb->complete,
0283                         msecs_to_jiffies(100))) {
0284         dev_dbg(rsb->dev, "RSB timeout\n");
0285 
0286         /* abort the transfer */
0287         writel(RSB_CTRL_ABORT_TRANS, rsb->regs + RSB_CTRL);
0288 
0289         /* clear any interrupt flags */
0290         writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS);
0291 
0292         return -ETIMEDOUT;
0293     }
0294 
0295     if (rsb->status & RSB_INTS_LOAD_BSY) {
0296         dev_dbg(rsb->dev, "RSB busy\n");
0297         return -EBUSY;
0298     }
0299 
0300     if (rsb->status & RSB_INTS_TRANS_ERR) {
0301         if (rsb->status & RSB_INTS_TRANS_ERR_ACK) {
0302             dev_dbg(rsb->dev, "RSB slave nack\n");
0303             return -EINVAL;
0304         }
0305 
0306         if (rsb->status & RSB_INTS_TRANS_ERR_DATA) {
0307             dev_dbg(rsb->dev, "RSB transfer data error\n");
0308             return -EIO;
0309         }
0310     }
0311 
0312     return 0;
0313 }
0314 
0315 static int sunxi_rsb_read(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr,
0316               u32 *buf, size_t len)
0317 {
0318     u32 cmd;
0319     int ret;
0320 
0321     if (!buf)
0322         return -EINVAL;
0323 
0324     switch (len) {
0325     case 1:
0326         cmd = RSB_CMD_RD8;
0327         break;
0328     case 2:
0329         cmd = RSB_CMD_RD16;
0330         break;
0331     case 4:
0332         cmd = RSB_CMD_RD32;
0333         break;
0334     default:
0335         dev_err(rsb->dev, "Invalid access width: %zd\n", len);
0336         return -EINVAL;
0337     }
0338 
0339     ret = pm_runtime_resume_and_get(rsb->dev);
0340     if (ret)
0341         return ret;
0342 
0343     mutex_lock(&rsb->lock);
0344 
0345     writel(addr, rsb->regs + RSB_ADDR);
0346     writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR);
0347     writel(cmd, rsb->regs + RSB_CMD);
0348 
0349     ret = _sunxi_rsb_run_xfer(rsb);
0350     if (ret)
0351         goto unlock;
0352 
0353     *buf = readl(rsb->regs + RSB_DATA) & GENMASK(len * 8 - 1, 0);
0354 
0355 unlock:
0356     mutex_unlock(&rsb->lock);
0357 
0358     pm_runtime_mark_last_busy(rsb->dev);
0359     pm_runtime_put_autosuspend(rsb->dev);
0360 
0361     return ret;
0362 }
0363 
0364 static int sunxi_rsb_write(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr,
0365                const u32 *buf, size_t len)
0366 {
0367     u32 cmd;
0368     int ret;
0369 
0370     if (!buf)
0371         return -EINVAL;
0372 
0373     switch (len) {
0374     case 1:
0375         cmd = RSB_CMD_WR8;
0376         break;
0377     case 2:
0378         cmd = RSB_CMD_WR16;
0379         break;
0380     case 4:
0381         cmd = RSB_CMD_WR32;
0382         break;
0383     default:
0384         dev_err(rsb->dev, "Invalid access width: %zd\n", len);
0385         return -EINVAL;
0386     }
0387 
0388     ret = pm_runtime_resume_and_get(rsb->dev);
0389     if (ret)
0390         return ret;
0391 
0392     mutex_lock(&rsb->lock);
0393 
0394     writel(addr, rsb->regs + RSB_ADDR);
0395     writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR);
0396     writel(*buf, rsb->regs + RSB_DATA);
0397     writel(cmd, rsb->regs + RSB_CMD);
0398     ret = _sunxi_rsb_run_xfer(rsb);
0399 
0400     mutex_unlock(&rsb->lock);
0401 
0402     pm_runtime_mark_last_busy(rsb->dev);
0403     pm_runtime_put_autosuspend(rsb->dev);
0404 
0405     return ret;
0406 }
0407 
0408 /* RSB regmap functions */
0409 struct sunxi_rsb_ctx {
0410     struct sunxi_rsb_device *rdev;
0411     int size;
0412 };
0413 
0414 static int regmap_sunxi_rsb_reg_read(void *context, unsigned int reg,
0415                      unsigned int *val)
0416 {
0417     struct sunxi_rsb_ctx *ctx = context;
0418     struct sunxi_rsb_device *rdev = ctx->rdev;
0419 
0420     if (reg > 0xff)
0421         return -EINVAL;
0422 
0423     return sunxi_rsb_read(rdev->rsb, rdev->rtaddr, reg, val, ctx->size);
0424 }
0425 
0426 static int regmap_sunxi_rsb_reg_write(void *context, unsigned int reg,
0427                       unsigned int val)
0428 {
0429     struct sunxi_rsb_ctx *ctx = context;
0430     struct sunxi_rsb_device *rdev = ctx->rdev;
0431 
0432     return sunxi_rsb_write(rdev->rsb, rdev->rtaddr, reg, &val, ctx->size);
0433 }
0434 
0435 static void regmap_sunxi_rsb_free_ctx(void *context)
0436 {
0437     struct sunxi_rsb_ctx *ctx = context;
0438 
0439     kfree(ctx);
0440 }
0441 
0442 static struct regmap_bus regmap_sunxi_rsb = {
0443     .reg_write = regmap_sunxi_rsb_reg_write,
0444     .reg_read = regmap_sunxi_rsb_reg_read,
0445     .free_context = regmap_sunxi_rsb_free_ctx,
0446     .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
0447     .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
0448 };
0449 
0450 static struct sunxi_rsb_ctx *regmap_sunxi_rsb_init_ctx(struct sunxi_rsb_device *rdev,
0451         const struct regmap_config *config)
0452 {
0453     struct sunxi_rsb_ctx *ctx;
0454 
0455     switch (config->val_bits) {
0456     case 8:
0457     case 16:
0458     case 32:
0459         break;
0460     default:
0461         return ERR_PTR(-EINVAL);
0462     }
0463 
0464     ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
0465     if (!ctx)
0466         return ERR_PTR(-ENOMEM);
0467 
0468     ctx->rdev = rdev;
0469     ctx->size = config->val_bits / 8;
0470 
0471     return ctx;
0472 }
0473 
0474 struct regmap *__devm_regmap_init_sunxi_rsb(struct sunxi_rsb_device *rdev,
0475                         const struct regmap_config *config,
0476                         struct lock_class_key *lock_key,
0477                         const char *lock_name)
0478 {
0479     struct sunxi_rsb_ctx *ctx = regmap_sunxi_rsb_init_ctx(rdev, config);
0480 
0481     if (IS_ERR(ctx))
0482         return ERR_CAST(ctx);
0483 
0484     return __devm_regmap_init(&rdev->dev, &regmap_sunxi_rsb, ctx, config,
0485                   lock_key, lock_name);
0486 }
0487 EXPORT_SYMBOL_GPL(__devm_regmap_init_sunxi_rsb);
0488 
0489 /* RSB controller driver functions */
0490 static irqreturn_t sunxi_rsb_irq(int irq, void *dev_id)
0491 {
0492     struct sunxi_rsb *rsb = dev_id;
0493     u32 status;
0494 
0495     status = readl(rsb->regs + RSB_INTS);
0496     rsb->status = status;
0497 
0498     /* Clear interrupts */
0499     status &= (RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR |
0500            RSB_INTS_TRANS_OVER);
0501     writel(status, rsb->regs + RSB_INTS);
0502 
0503     complete(&rsb->complete);
0504 
0505     return IRQ_HANDLED;
0506 }
0507 
0508 static int sunxi_rsb_init_device_mode(struct sunxi_rsb *rsb)
0509 {
0510     int ret = 0;
0511     u32 reg;
0512 
0513     /* send init sequence */
0514     writel(RSB_DMCR_DEVICE_START | RSB_DMCR_MODE_DATA |
0515            RSB_DMCR_MODE_REG | RSB_DMCR_DEV_ADDR, rsb->regs + RSB_DMCR);
0516 
0517     readl_poll_timeout(rsb->regs + RSB_DMCR, reg,
0518                !(reg & RSB_DMCR_DEVICE_START), 100, 250000);
0519     if (reg & RSB_DMCR_DEVICE_START)
0520         ret = -ETIMEDOUT;
0521 
0522     /* clear interrupt status bits */
0523     writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS);
0524 
0525     return ret;
0526 }
0527 
0528 /*
0529  * There are 15 valid runtime addresses, though Allwinner typically
0530  * skips the first, for unknown reasons, and uses the following three.
0531  *
0532  * 0x17, 0x2d, 0x3a, 0x4e, 0x59, 0x63, 0x74, 0x8b,
0533  * 0x9c, 0xa6, 0xb1, 0xc5, 0xd2, 0xe8, 0xff
0534  *
0535  * No designs with 2 RSB slave devices sharing identical hardware
0536  * addresses on the same bus have been seen in the wild. All designs
0537  * use 0x2d for the primary PMIC, 0x3a for the secondary PMIC if
0538  * there is one, and 0x45 for peripheral ICs.
0539  *
0540  * The hardware does not seem to support re-setting runtime addresses.
0541  * Attempts to do so result in the slave devices returning a NACK.
0542  * Hence we just hardcode the mapping here, like Allwinner does.
0543  */
0544 
0545 static const struct sunxi_rsb_addr_map sunxi_rsb_addr_maps[] = {
0546     { 0x3a3, 0x2d }, /* Primary PMIC: AXP223, AXP809, AXP81X, ... */
0547     { 0x745, 0x3a }, /* Secondary PMIC: AXP806, ... */
0548     { 0xe89, 0x4e }, /* Peripheral IC: AC100, ... */
0549 };
0550 
0551 static u8 sunxi_rsb_get_rtaddr(u16 hwaddr)
0552 {
0553     int i;
0554 
0555     for (i = 0; i < ARRAY_SIZE(sunxi_rsb_addr_maps); i++)
0556         if (hwaddr == sunxi_rsb_addr_maps[i].hwaddr)
0557             return sunxi_rsb_addr_maps[i].rtaddr;
0558 
0559     return 0; /* 0 is an invalid runtime address */
0560 }
0561 
0562 static int of_rsb_register_devices(struct sunxi_rsb *rsb)
0563 {
0564     struct device *dev = rsb->dev;
0565     struct device_node *child, *np = dev->of_node;
0566     u32 hwaddr;
0567     u8 rtaddr;
0568     int ret;
0569 
0570     if (!np)
0571         return -EINVAL;
0572 
0573     /* Runtime addresses for all slaves should be set first */
0574     for_each_available_child_of_node(np, child) {
0575         dev_dbg(dev, "setting child %pOF runtime address\n",
0576             child);
0577 
0578         ret = of_property_read_u32(child, "reg", &hwaddr);
0579         if (ret) {
0580             dev_err(dev, "%pOF: invalid 'reg' property: %d\n",
0581                 child, ret);
0582             continue;
0583         }
0584 
0585         rtaddr = sunxi_rsb_get_rtaddr(hwaddr);
0586         if (!rtaddr) {
0587             dev_err(dev, "%pOF: unknown hardware device address\n",
0588                 child);
0589             continue;
0590         }
0591 
0592         /*
0593          * Since no devices have been registered yet, we are the
0594          * only ones using the bus, we can skip locking the bus.
0595          */
0596 
0597         /* setup command parameters */
0598         writel(RSB_CMD_STRA, rsb->regs + RSB_CMD);
0599         writel(RSB_DAR_RTA(rtaddr) | RSB_DAR_DA(hwaddr),
0600                rsb->regs + RSB_DAR);
0601 
0602         /* send command */
0603         ret = _sunxi_rsb_run_xfer(rsb);
0604         if (ret)
0605             dev_warn(dev, "%pOF: set runtime address failed: %d\n",
0606                  child, ret);
0607     }
0608 
0609     /* Then we start adding devices and probing them */
0610     for_each_available_child_of_node(np, child) {
0611         struct sunxi_rsb_device *rdev;
0612 
0613         dev_dbg(dev, "adding child %pOF\n", child);
0614 
0615         ret = of_property_read_u32(child, "reg", &hwaddr);
0616         if (ret)
0617             continue;
0618 
0619         rtaddr = sunxi_rsb_get_rtaddr(hwaddr);
0620         if (!rtaddr)
0621             continue;
0622 
0623         rdev = sunxi_rsb_device_create(rsb, child, hwaddr, rtaddr);
0624         if (IS_ERR(rdev))
0625             dev_err(dev, "failed to add child device %pOF: %ld\n",
0626                 child, PTR_ERR(rdev));
0627     }
0628 
0629     return 0;
0630 }
0631 
0632 static int sunxi_rsb_hw_init(struct sunxi_rsb *rsb)
0633 {
0634     struct device *dev = rsb->dev;
0635     unsigned long p_clk_freq;
0636     u32 clk_delay, reg;
0637     int clk_div, ret;
0638 
0639     ret = clk_prepare_enable(rsb->clk);
0640     if (ret) {
0641         dev_err(dev, "failed to enable clk: %d\n", ret);
0642         return ret;
0643     }
0644 
0645     ret = reset_control_deassert(rsb->rstc);
0646     if (ret) {
0647         dev_err(dev, "failed to deassert reset line: %d\n", ret);
0648         goto err_clk_disable;
0649     }
0650 
0651     /* reset the controller */
0652     writel(RSB_CTRL_SOFT_RST, rsb->regs + RSB_CTRL);
0653     readl_poll_timeout(rsb->regs + RSB_CTRL, reg,
0654                !(reg & RSB_CTRL_SOFT_RST), 1000, 100000);
0655 
0656     /*
0657      * Clock frequency and delay calculation code is from
0658      * Allwinner U-boot sources.
0659      *
0660      * From A83 user manual:
0661      * bus clock frequency = parent clock frequency / (2 * (divider + 1))
0662      */
0663     p_clk_freq = clk_get_rate(rsb->clk);
0664     clk_div = p_clk_freq / rsb->clk_freq / 2;
0665     if (!clk_div)
0666         clk_div = 1;
0667     else if (clk_div > RSB_CCR_MAX_CLK_DIV + 1)
0668         clk_div = RSB_CCR_MAX_CLK_DIV + 1;
0669 
0670     clk_delay = clk_div >> 1;
0671     if (!clk_delay)
0672         clk_delay = 1;
0673 
0674     dev_info(dev, "RSB running at %lu Hz\n", p_clk_freq / clk_div / 2);
0675     writel(RSB_CCR_SDA_OUT_DELAY(clk_delay) | RSB_CCR_CLK_DIV(clk_div - 1),
0676            rsb->regs + RSB_CCR);
0677 
0678     return 0;
0679 
0680 err_clk_disable:
0681     clk_disable_unprepare(rsb->clk);
0682 
0683     return ret;
0684 }
0685 
0686 static void sunxi_rsb_hw_exit(struct sunxi_rsb *rsb)
0687 {
0688     reset_control_assert(rsb->rstc);
0689 
0690     /* Keep the clock and PM reference counts consistent. */
0691     if (!pm_runtime_status_suspended(rsb->dev))
0692         clk_disable_unprepare(rsb->clk);
0693 }
0694 
0695 static int __maybe_unused sunxi_rsb_runtime_suspend(struct device *dev)
0696 {
0697     struct sunxi_rsb *rsb = dev_get_drvdata(dev);
0698 
0699     clk_disable_unprepare(rsb->clk);
0700 
0701     return 0;
0702 }
0703 
0704 static int __maybe_unused sunxi_rsb_runtime_resume(struct device *dev)
0705 {
0706     struct sunxi_rsb *rsb = dev_get_drvdata(dev);
0707 
0708     return clk_prepare_enable(rsb->clk);
0709 }
0710 
0711 static int __maybe_unused sunxi_rsb_suspend(struct device *dev)
0712 {
0713     struct sunxi_rsb *rsb = dev_get_drvdata(dev);
0714 
0715     sunxi_rsb_hw_exit(rsb);
0716 
0717     return 0;
0718 }
0719 
0720 static int __maybe_unused sunxi_rsb_resume(struct device *dev)
0721 {
0722     struct sunxi_rsb *rsb = dev_get_drvdata(dev);
0723 
0724     return sunxi_rsb_hw_init(rsb);
0725 }
0726 
0727 static int sunxi_rsb_probe(struct platform_device *pdev)
0728 {
0729     struct device *dev = &pdev->dev;
0730     struct device_node *np = dev->of_node;
0731     struct resource *r;
0732     struct sunxi_rsb *rsb;
0733     u32 clk_freq = 3000000;
0734     int irq, ret;
0735 
0736     of_property_read_u32(np, "clock-frequency", &clk_freq);
0737     if (clk_freq > RSB_MAX_FREQ) {
0738         dev_err(dev,
0739             "clock-frequency (%u Hz) is too high (max = 20MHz)\n",
0740             clk_freq);
0741         return -EINVAL;
0742     }
0743 
0744     rsb = devm_kzalloc(dev, sizeof(*rsb), GFP_KERNEL);
0745     if (!rsb)
0746         return -ENOMEM;
0747 
0748     rsb->dev = dev;
0749     rsb->clk_freq = clk_freq;
0750     platform_set_drvdata(pdev, rsb);
0751     r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0752     rsb->regs = devm_ioremap_resource(dev, r);
0753     if (IS_ERR(rsb->regs))
0754         return PTR_ERR(rsb->regs);
0755 
0756     irq = platform_get_irq(pdev, 0);
0757     if (irq < 0)
0758         return irq;
0759 
0760     rsb->clk = devm_clk_get(dev, NULL);
0761     if (IS_ERR(rsb->clk)) {
0762         ret = PTR_ERR(rsb->clk);
0763         dev_err(dev, "failed to retrieve clk: %d\n", ret);
0764         return ret;
0765     }
0766 
0767     rsb->rstc = devm_reset_control_get(dev, NULL);
0768     if (IS_ERR(rsb->rstc)) {
0769         ret = PTR_ERR(rsb->rstc);
0770         dev_err(dev, "failed to retrieve reset controller: %d\n", ret);
0771         return ret;
0772     }
0773 
0774     init_completion(&rsb->complete);
0775     mutex_init(&rsb->lock);
0776 
0777     ret = devm_request_irq(dev, irq, sunxi_rsb_irq, 0, RSB_CTRL_NAME, rsb);
0778     if (ret) {
0779         dev_err(dev, "can't register interrupt handler irq %d: %d\n",
0780             irq, ret);
0781         return ret;
0782     }
0783 
0784     ret = sunxi_rsb_hw_init(rsb);
0785     if (ret)
0786         return ret;
0787 
0788     /* initialize all devices on the bus into RSB mode */
0789     ret = sunxi_rsb_init_device_mode(rsb);
0790     if (ret)
0791         dev_warn(dev, "Initialize device mode failed: %d\n", ret);
0792 
0793     pm_suspend_ignore_children(dev, true);
0794     pm_runtime_set_active(dev);
0795     pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
0796     pm_runtime_use_autosuspend(dev);
0797     pm_runtime_enable(dev);
0798 
0799     of_rsb_register_devices(rsb);
0800 
0801     return 0;
0802 }
0803 
0804 static int sunxi_rsb_remove(struct platform_device *pdev)
0805 {
0806     struct sunxi_rsb *rsb = platform_get_drvdata(pdev);
0807 
0808     device_for_each_child(rsb->dev, NULL, sunxi_rsb_remove_devices);
0809     pm_runtime_disable(&pdev->dev);
0810     sunxi_rsb_hw_exit(rsb);
0811 
0812     return 0;
0813 }
0814 
0815 static void sunxi_rsb_shutdown(struct platform_device *pdev)
0816 {
0817     struct sunxi_rsb *rsb = platform_get_drvdata(pdev);
0818 
0819     pm_runtime_disable(&pdev->dev);
0820     sunxi_rsb_hw_exit(rsb);
0821 }
0822 
0823 static const struct dev_pm_ops sunxi_rsb_dev_pm_ops = {
0824     SET_RUNTIME_PM_OPS(sunxi_rsb_runtime_suspend,
0825                sunxi_rsb_runtime_resume, NULL)
0826     SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sunxi_rsb_suspend, sunxi_rsb_resume)
0827 };
0828 
0829 static const struct of_device_id sunxi_rsb_of_match_table[] = {
0830     { .compatible = "allwinner,sun8i-a23-rsb" },
0831     {}
0832 };
0833 MODULE_DEVICE_TABLE(of, sunxi_rsb_of_match_table);
0834 
0835 static struct platform_driver sunxi_rsb_driver = {
0836     .probe = sunxi_rsb_probe,
0837     .remove = sunxi_rsb_remove,
0838     .shutdown = sunxi_rsb_shutdown,
0839     .driver = {
0840         .name = RSB_CTRL_NAME,
0841         .of_match_table = sunxi_rsb_of_match_table,
0842         .pm = &sunxi_rsb_dev_pm_ops,
0843     },
0844 };
0845 
0846 static int __init sunxi_rsb_init(void)
0847 {
0848     int ret;
0849 
0850     ret = bus_register(&sunxi_rsb_bus);
0851     if (ret) {
0852         pr_err("failed to register sunxi sunxi_rsb bus: %d\n", ret);
0853         return ret;
0854     }
0855 
0856     return platform_driver_register(&sunxi_rsb_driver);
0857 }
0858 module_init(sunxi_rsb_init);
0859 
0860 static void __exit sunxi_rsb_exit(void)
0861 {
0862     platform_driver_unregister(&sunxi_rsb_driver);
0863     bus_unregister(&sunxi_rsb_bus);
0864 }
0865 module_exit(sunxi_rsb_exit);
0866 
0867 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
0868 MODULE_DESCRIPTION("Allwinner sunXi Reduced Serial Bus controller driver");
0869 MODULE_LICENSE("GPL v2");