18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * RSB (Reduced Serial Bus) driver. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Author: Chen-Yu Tsai <wens@csie.org> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public License 78c2ecf20Sopenharmony_ci * version 2. This program is licensed "as is" without any warranty of any 88c2ecf20Sopenharmony_ci * kind, whether express or implied. 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * The RSB controller looks like an SMBus controller which only supports 118c2ecf20Sopenharmony_ci * byte and word data transfers. But, it differs from standard SMBus 128c2ecf20Sopenharmony_ci * protocol on several aspects: 138c2ecf20Sopenharmony_ci * - it uses addresses set at runtime to address slaves. Runtime addresses 148c2ecf20Sopenharmony_ci * are sent to slaves using their 12bit hardware addresses. Up to 15 158c2ecf20Sopenharmony_ci * runtime addresses are available. 168c2ecf20Sopenharmony_ci * - it adds a parity bit every 8bits of data and address for read and 178c2ecf20Sopenharmony_ci * write accesses; this replaces the ack bit 188c2ecf20Sopenharmony_ci * - only one read access is required to read a byte (instead of a write 198c2ecf20Sopenharmony_ci * followed by a read access in standard SMBus protocol) 208c2ecf20Sopenharmony_ci * - there's no Ack bit after each read access 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * This means this bus cannot be used to interface with standard SMBus 238c2ecf20Sopenharmony_ci * devices. Devices known to support this interface include the AXP223, 248c2ecf20Sopenharmony_ci * AXP809, and AXP806 PMICs, and the AC100 audio codec, all from X-Powers. 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * A description of the operation and wire protocol can be found in the 278c2ecf20Sopenharmony_ci * RSB section of Allwinner's A80 user manual, which can be found at 288c2ecf20Sopenharmony_ci * 298c2ecf20Sopenharmony_ci * https://github.com/allwinner-zh/documents/tree/master/A80 308c2ecf20Sopenharmony_ci * 318c2ecf20Sopenharmony_ci * This document is officially released by Allwinner. 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * This driver is based on i2c-sun6i-p2wi.c, the P2WI bus driver. 348c2ecf20Sopenharmony_ci * 358c2ecf20Sopenharmony_ci */ 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#include <linux/clk.h> 388c2ecf20Sopenharmony_ci#include <linux/clk/clk-conf.h> 398c2ecf20Sopenharmony_ci#include <linux/device.h> 408c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 418c2ecf20Sopenharmony_ci#include <linux/io.h> 428c2ecf20Sopenharmony_ci#include <linux/iopoll.h> 438c2ecf20Sopenharmony_ci#include <linux/module.h> 448c2ecf20Sopenharmony_ci#include <linux/of.h> 458c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 468c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 478c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 488c2ecf20Sopenharmony_ci#include <linux/regmap.h> 498c2ecf20Sopenharmony_ci#include <linux/reset.h> 508c2ecf20Sopenharmony_ci#include <linux/slab.h> 518c2ecf20Sopenharmony_ci#include <linux/sunxi-rsb.h> 528c2ecf20Sopenharmony_ci#include <linux/types.h> 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/* RSB registers */ 558c2ecf20Sopenharmony_ci#define RSB_CTRL 0x0 /* Global control */ 568c2ecf20Sopenharmony_ci#define RSB_CCR 0x4 /* Clock control */ 578c2ecf20Sopenharmony_ci#define RSB_INTE 0x8 /* Interrupt controls */ 588c2ecf20Sopenharmony_ci#define RSB_INTS 0xc /* Interrupt status */ 598c2ecf20Sopenharmony_ci#define RSB_ADDR 0x10 /* Address to send with read/write command */ 608c2ecf20Sopenharmony_ci#define RSB_DATA 0x1c /* Data to read/write */ 618c2ecf20Sopenharmony_ci#define RSB_LCR 0x24 /* Line control */ 628c2ecf20Sopenharmony_ci#define RSB_DMCR 0x28 /* Device mode (init) control */ 638c2ecf20Sopenharmony_ci#define RSB_CMD 0x2c /* RSB Command */ 648c2ecf20Sopenharmony_ci#define RSB_DAR 0x30 /* Device address / runtime address */ 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* CTRL fields */ 678c2ecf20Sopenharmony_ci#define RSB_CTRL_START_TRANS BIT(7) 688c2ecf20Sopenharmony_ci#define RSB_CTRL_ABORT_TRANS BIT(6) 698c2ecf20Sopenharmony_ci#define RSB_CTRL_GLOBAL_INT_ENB BIT(1) 708c2ecf20Sopenharmony_ci#define RSB_CTRL_SOFT_RST BIT(0) 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/* CLK CTRL fields */ 738c2ecf20Sopenharmony_ci#define RSB_CCR_SDA_OUT_DELAY(v) (((v) & 0x7) << 8) 748c2ecf20Sopenharmony_ci#define RSB_CCR_MAX_CLK_DIV 0xff 758c2ecf20Sopenharmony_ci#define RSB_CCR_CLK_DIV(v) ((v) & RSB_CCR_MAX_CLK_DIV) 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/* STATUS fields */ 788c2ecf20Sopenharmony_ci#define RSB_INTS_TRANS_ERR_ACK BIT(16) 798c2ecf20Sopenharmony_ci#define RSB_INTS_TRANS_ERR_DATA_BIT(v) (((v) >> 8) & 0xf) 808c2ecf20Sopenharmony_ci#define RSB_INTS_TRANS_ERR_DATA GENMASK(11, 8) 818c2ecf20Sopenharmony_ci#define RSB_INTS_LOAD_BSY BIT(2) 828c2ecf20Sopenharmony_ci#define RSB_INTS_TRANS_ERR BIT(1) 838c2ecf20Sopenharmony_ci#define RSB_INTS_TRANS_OVER BIT(0) 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/* LINE CTRL fields*/ 868c2ecf20Sopenharmony_ci#define RSB_LCR_SCL_STATE BIT(5) 878c2ecf20Sopenharmony_ci#define RSB_LCR_SDA_STATE BIT(4) 888c2ecf20Sopenharmony_ci#define RSB_LCR_SCL_CTL BIT(3) 898c2ecf20Sopenharmony_ci#define RSB_LCR_SCL_CTL_EN BIT(2) 908c2ecf20Sopenharmony_ci#define RSB_LCR_SDA_CTL BIT(1) 918c2ecf20Sopenharmony_ci#define RSB_LCR_SDA_CTL_EN BIT(0) 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci/* DEVICE MODE CTRL field values */ 948c2ecf20Sopenharmony_ci#define RSB_DMCR_DEVICE_START BIT(31) 958c2ecf20Sopenharmony_ci#define RSB_DMCR_MODE_DATA (0x7c << 16) 968c2ecf20Sopenharmony_ci#define RSB_DMCR_MODE_REG (0x3e << 8) 978c2ecf20Sopenharmony_ci#define RSB_DMCR_DEV_ADDR 0x00 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci/* CMD values */ 1008c2ecf20Sopenharmony_ci#define RSB_CMD_RD8 0x8b 1018c2ecf20Sopenharmony_ci#define RSB_CMD_RD16 0x9c 1028c2ecf20Sopenharmony_ci#define RSB_CMD_RD32 0xa6 1038c2ecf20Sopenharmony_ci#define RSB_CMD_WR8 0x4e 1048c2ecf20Sopenharmony_ci#define RSB_CMD_WR16 0x59 1058c2ecf20Sopenharmony_ci#define RSB_CMD_WR32 0x63 1068c2ecf20Sopenharmony_ci#define RSB_CMD_STRA 0xe8 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci/* DAR fields */ 1098c2ecf20Sopenharmony_ci#define RSB_DAR_RTA(v) (((v) & 0xff) << 16) 1108c2ecf20Sopenharmony_ci#define RSB_DAR_DA(v) ((v) & 0xffff) 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci#define RSB_MAX_FREQ 20000000 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci#define RSB_CTRL_NAME "sunxi-rsb" 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistruct sunxi_rsb_addr_map { 1178c2ecf20Sopenharmony_ci u16 hwaddr; 1188c2ecf20Sopenharmony_ci u8 rtaddr; 1198c2ecf20Sopenharmony_ci}; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistruct sunxi_rsb { 1228c2ecf20Sopenharmony_ci struct device *dev; 1238c2ecf20Sopenharmony_ci void __iomem *regs; 1248c2ecf20Sopenharmony_ci struct clk *clk; 1258c2ecf20Sopenharmony_ci struct reset_control *rstc; 1268c2ecf20Sopenharmony_ci struct completion complete; 1278c2ecf20Sopenharmony_ci struct mutex lock; 1288c2ecf20Sopenharmony_ci unsigned int status; 1298c2ecf20Sopenharmony_ci}; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci/* bus / slave device related functions */ 1328c2ecf20Sopenharmony_cistatic struct bus_type sunxi_rsb_bus; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic int sunxi_rsb_device_match(struct device *dev, struct device_driver *drv) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci return of_driver_match_device(dev, drv); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic int sunxi_rsb_device_probe(struct device *dev) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver); 1428c2ecf20Sopenharmony_ci struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev); 1438c2ecf20Sopenharmony_ci int ret; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci if (!drv->probe) 1468c2ecf20Sopenharmony_ci return -ENODEV; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (!rdev->irq) { 1498c2ecf20Sopenharmony_ci int irq = -ENOENT; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if (dev->of_node) 1528c2ecf20Sopenharmony_ci irq = of_irq_get(dev->of_node, 0); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci if (irq == -EPROBE_DEFER) 1558c2ecf20Sopenharmony_ci return irq; 1568c2ecf20Sopenharmony_ci if (irq < 0) 1578c2ecf20Sopenharmony_ci irq = 0; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci rdev->irq = irq; 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci ret = of_clk_set_defaults(dev->of_node, false); 1638c2ecf20Sopenharmony_ci if (ret < 0) 1648c2ecf20Sopenharmony_ci return ret; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci return drv->probe(rdev); 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cistatic int sunxi_rsb_device_remove(struct device *dev) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci return drv->remove(to_sunxi_rsb_device(dev)); 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic struct bus_type sunxi_rsb_bus = { 1778c2ecf20Sopenharmony_ci .name = RSB_CTRL_NAME, 1788c2ecf20Sopenharmony_ci .match = sunxi_rsb_device_match, 1798c2ecf20Sopenharmony_ci .probe = sunxi_rsb_device_probe, 1808c2ecf20Sopenharmony_ci .remove = sunxi_rsb_device_remove, 1818c2ecf20Sopenharmony_ci .uevent = of_device_uevent_modalias, 1828c2ecf20Sopenharmony_ci}; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic void sunxi_rsb_dev_release(struct device *dev) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci kfree(rdev); 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci/** 1928c2ecf20Sopenharmony_ci * sunxi_rsb_device_create() - allocate and add an RSB device 1938c2ecf20Sopenharmony_ci * @rsb: RSB controller 1948c2ecf20Sopenharmony_ci * @node: RSB slave device node 1958c2ecf20Sopenharmony_ci * @hwaddr: RSB slave hardware address 1968c2ecf20Sopenharmony_ci * @rtaddr: RSB slave runtime address 1978c2ecf20Sopenharmony_ci */ 1988c2ecf20Sopenharmony_cistatic struct sunxi_rsb_device *sunxi_rsb_device_create(struct sunxi_rsb *rsb, 1998c2ecf20Sopenharmony_ci struct device_node *node, u16 hwaddr, u8 rtaddr) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci int err; 2028c2ecf20Sopenharmony_ci struct sunxi_rsb_device *rdev; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); 2058c2ecf20Sopenharmony_ci if (!rdev) 2068c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci rdev->rsb = rsb; 2098c2ecf20Sopenharmony_ci rdev->hwaddr = hwaddr; 2108c2ecf20Sopenharmony_ci rdev->rtaddr = rtaddr; 2118c2ecf20Sopenharmony_ci rdev->dev.bus = &sunxi_rsb_bus; 2128c2ecf20Sopenharmony_ci rdev->dev.parent = rsb->dev; 2138c2ecf20Sopenharmony_ci rdev->dev.of_node = node; 2148c2ecf20Sopenharmony_ci rdev->dev.release = sunxi_rsb_dev_release; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci dev_set_name(&rdev->dev, "%s-%x", RSB_CTRL_NAME, hwaddr); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci err = device_register(&rdev->dev); 2198c2ecf20Sopenharmony_ci if (err < 0) { 2208c2ecf20Sopenharmony_ci dev_err(&rdev->dev, "Can't add %s, status %d\n", 2218c2ecf20Sopenharmony_ci dev_name(&rdev->dev), err); 2228c2ecf20Sopenharmony_ci goto err_device_add; 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci dev_dbg(&rdev->dev, "device %s registered\n", dev_name(&rdev->dev)); 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci return rdev; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_cierr_device_add: 2308c2ecf20Sopenharmony_ci put_device(&rdev->dev); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci return ERR_PTR(err); 2338c2ecf20Sopenharmony_ci} 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci/** 2368c2ecf20Sopenharmony_ci * sunxi_rsb_device_unregister(): unregister an RSB device 2378c2ecf20Sopenharmony_ci * @rdev: rsb_device to be removed 2388c2ecf20Sopenharmony_ci */ 2398c2ecf20Sopenharmony_cistatic void sunxi_rsb_device_unregister(struct sunxi_rsb_device *rdev) 2408c2ecf20Sopenharmony_ci{ 2418c2ecf20Sopenharmony_ci device_unregister(&rdev->dev); 2428c2ecf20Sopenharmony_ci} 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic int sunxi_rsb_remove_devices(struct device *dev, void *data) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci if (dev->bus == &sunxi_rsb_bus) 2498c2ecf20Sopenharmony_ci sunxi_rsb_device_unregister(rdev); 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci return 0; 2528c2ecf20Sopenharmony_ci} 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci/** 2558c2ecf20Sopenharmony_ci * sunxi_rsb_driver_register() - Register device driver with RSB core 2568c2ecf20Sopenharmony_ci * @rdrv: device driver to be associated with slave-device. 2578c2ecf20Sopenharmony_ci * 2588c2ecf20Sopenharmony_ci * This API will register the client driver with the RSB framework. 2598c2ecf20Sopenharmony_ci * It is typically called from the driver's module-init function. 2608c2ecf20Sopenharmony_ci */ 2618c2ecf20Sopenharmony_ciint sunxi_rsb_driver_register(struct sunxi_rsb_driver *rdrv) 2628c2ecf20Sopenharmony_ci{ 2638c2ecf20Sopenharmony_ci rdrv->driver.bus = &sunxi_rsb_bus; 2648c2ecf20Sopenharmony_ci return driver_register(&rdrv->driver); 2658c2ecf20Sopenharmony_ci} 2668c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sunxi_rsb_driver_register); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci/* common code that starts a transfer */ 2698c2ecf20Sopenharmony_cistatic int _sunxi_rsb_run_xfer(struct sunxi_rsb *rsb) 2708c2ecf20Sopenharmony_ci{ 2718c2ecf20Sopenharmony_ci u32 int_mask, status; 2728c2ecf20Sopenharmony_ci bool timeout; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci if (readl(rsb->regs + RSB_CTRL) & RSB_CTRL_START_TRANS) { 2758c2ecf20Sopenharmony_ci dev_dbg(rsb->dev, "RSB transfer still in progress\n"); 2768c2ecf20Sopenharmony_ci return -EBUSY; 2778c2ecf20Sopenharmony_ci } 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci reinit_completion(&rsb->complete); 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci int_mask = RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR | RSB_INTS_TRANS_OVER; 2828c2ecf20Sopenharmony_ci writel(int_mask, rsb->regs + RSB_INTE); 2838c2ecf20Sopenharmony_ci writel(RSB_CTRL_START_TRANS | RSB_CTRL_GLOBAL_INT_ENB, 2848c2ecf20Sopenharmony_ci rsb->regs + RSB_CTRL); 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci if (irqs_disabled()) { 2878c2ecf20Sopenharmony_ci timeout = readl_poll_timeout_atomic(rsb->regs + RSB_INTS, 2888c2ecf20Sopenharmony_ci status, (status & int_mask), 2898c2ecf20Sopenharmony_ci 10, 100000); 2908c2ecf20Sopenharmony_ci writel(status, rsb->regs + RSB_INTS); 2918c2ecf20Sopenharmony_ci } else { 2928c2ecf20Sopenharmony_ci timeout = !wait_for_completion_io_timeout(&rsb->complete, 2938c2ecf20Sopenharmony_ci msecs_to_jiffies(100)); 2948c2ecf20Sopenharmony_ci status = rsb->status; 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci if (timeout) { 2988c2ecf20Sopenharmony_ci dev_dbg(rsb->dev, "RSB timeout\n"); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci /* abort the transfer */ 3018c2ecf20Sopenharmony_ci writel(RSB_CTRL_ABORT_TRANS, rsb->regs + RSB_CTRL); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci /* clear any interrupt flags */ 3048c2ecf20Sopenharmony_ci writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS); 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci return -ETIMEDOUT; 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci if (status & RSB_INTS_LOAD_BSY) { 3108c2ecf20Sopenharmony_ci dev_dbg(rsb->dev, "RSB busy\n"); 3118c2ecf20Sopenharmony_ci return -EBUSY; 3128c2ecf20Sopenharmony_ci } 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci if (status & RSB_INTS_TRANS_ERR) { 3158c2ecf20Sopenharmony_ci if (status & RSB_INTS_TRANS_ERR_ACK) { 3168c2ecf20Sopenharmony_ci dev_dbg(rsb->dev, "RSB slave nack\n"); 3178c2ecf20Sopenharmony_ci return -EINVAL; 3188c2ecf20Sopenharmony_ci } 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci if (status & RSB_INTS_TRANS_ERR_DATA) { 3218c2ecf20Sopenharmony_ci dev_dbg(rsb->dev, "RSB transfer data error\n"); 3228c2ecf20Sopenharmony_ci return -EIO; 3238c2ecf20Sopenharmony_ci } 3248c2ecf20Sopenharmony_ci } 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci return 0; 3278c2ecf20Sopenharmony_ci} 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_cistatic int sunxi_rsb_read(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr, 3308c2ecf20Sopenharmony_ci u32 *buf, size_t len) 3318c2ecf20Sopenharmony_ci{ 3328c2ecf20Sopenharmony_ci u32 cmd; 3338c2ecf20Sopenharmony_ci int ret; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci if (!buf) 3368c2ecf20Sopenharmony_ci return -EINVAL; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci switch (len) { 3398c2ecf20Sopenharmony_ci case 1: 3408c2ecf20Sopenharmony_ci cmd = RSB_CMD_RD8; 3418c2ecf20Sopenharmony_ci break; 3428c2ecf20Sopenharmony_ci case 2: 3438c2ecf20Sopenharmony_ci cmd = RSB_CMD_RD16; 3448c2ecf20Sopenharmony_ci break; 3458c2ecf20Sopenharmony_ci case 4: 3468c2ecf20Sopenharmony_ci cmd = RSB_CMD_RD32; 3478c2ecf20Sopenharmony_ci break; 3488c2ecf20Sopenharmony_ci default: 3498c2ecf20Sopenharmony_ci dev_err(rsb->dev, "Invalid access width: %zd\n", len); 3508c2ecf20Sopenharmony_ci return -EINVAL; 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci mutex_lock(&rsb->lock); 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci writel(addr, rsb->regs + RSB_ADDR); 3568c2ecf20Sopenharmony_ci writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR); 3578c2ecf20Sopenharmony_ci writel(cmd, rsb->regs + RSB_CMD); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci ret = _sunxi_rsb_run_xfer(rsb); 3608c2ecf20Sopenharmony_ci if (ret) 3618c2ecf20Sopenharmony_ci goto unlock; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci *buf = readl(rsb->regs + RSB_DATA) & GENMASK(len * 8 - 1, 0); 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ciunlock: 3668c2ecf20Sopenharmony_ci mutex_unlock(&rsb->lock); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci return ret; 3698c2ecf20Sopenharmony_ci} 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_cistatic int sunxi_rsb_write(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr, 3728c2ecf20Sopenharmony_ci const u32 *buf, size_t len) 3738c2ecf20Sopenharmony_ci{ 3748c2ecf20Sopenharmony_ci u32 cmd; 3758c2ecf20Sopenharmony_ci int ret; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci if (!buf) 3788c2ecf20Sopenharmony_ci return -EINVAL; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci switch (len) { 3818c2ecf20Sopenharmony_ci case 1: 3828c2ecf20Sopenharmony_ci cmd = RSB_CMD_WR8; 3838c2ecf20Sopenharmony_ci break; 3848c2ecf20Sopenharmony_ci case 2: 3858c2ecf20Sopenharmony_ci cmd = RSB_CMD_WR16; 3868c2ecf20Sopenharmony_ci break; 3878c2ecf20Sopenharmony_ci case 4: 3888c2ecf20Sopenharmony_ci cmd = RSB_CMD_WR32; 3898c2ecf20Sopenharmony_ci break; 3908c2ecf20Sopenharmony_ci default: 3918c2ecf20Sopenharmony_ci dev_err(rsb->dev, "Invalid access width: %zd\n", len); 3928c2ecf20Sopenharmony_ci return -EINVAL; 3938c2ecf20Sopenharmony_ci } 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci mutex_lock(&rsb->lock); 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci writel(addr, rsb->regs + RSB_ADDR); 3988c2ecf20Sopenharmony_ci writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR); 3998c2ecf20Sopenharmony_ci writel(*buf, rsb->regs + RSB_DATA); 4008c2ecf20Sopenharmony_ci writel(cmd, rsb->regs + RSB_CMD); 4018c2ecf20Sopenharmony_ci ret = _sunxi_rsb_run_xfer(rsb); 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci mutex_unlock(&rsb->lock); 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci return ret; 4068c2ecf20Sopenharmony_ci} 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci/* RSB regmap functions */ 4098c2ecf20Sopenharmony_cistruct sunxi_rsb_ctx { 4108c2ecf20Sopenharmony_ci struct sunxi_rsb_device *rdev; 4118c2ecf20Sopenharmony_ci int size; 4128c2ecf20Sopenharmony_ci}; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_cistatic int regmap_sunxi_rsb_reg_read(void *context, unsigned int reg, 4158c2ecf20Sopenharmony_ci unsigned int *val) 4168c2ecf20Sopenharmony_ci{ 4178c2ecf20Sopenharmony_ci struct sunxi_rsb_ctx *ctx = context; 4188c2ecf20Sopenharmony_ci struct sunxi_rsb_device *rdev = ctx->rdev; 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci if (reg > 0xff) 4218c2ecf20Sopenharmony_ci return -EINVAL; 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci return sunxi_rsb_read(rdev->rsb, rdev->rtaddr, reg, val, ctx->size); 4248c2ecf20Sopenharmony_ci} 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_cistatic int regmap_sunxi_rsb_reg_write(void *context, unsigned int reg, 4278c2ecf20Sopenharmony_ci unsigned int val) 4288c2ecf20Sopenharmony_ci{ 4298c2ecf20Sopenharmony_ci struct sunxi_rsb_ctx *ctx = context; 4308c2ecf20Sopenharmony_ci struct sunxi_rsb_device *rdev = ctx->rdev; 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci return sunxi_rsb_write(rdev->rsb, rdev->rtaddr, reg, &val, ctx->size); 4338c2ecf20Sopenharmony_ci} 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cistatic void regmap_sunxi_rsb_free_ctx(void *context) 4368c2ecf20Sopenharmony_ci{ 4378c2ecf20Sopenharmony_ci struct sunxi_rsb_ctx *ctx = context; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci kfree(ctx); 4408c2ecf20Sopenharmony_ci} 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_cistatic struct regmap_bus regmap_sunxi_rsb = { 4438c2ecf20Sopenharmony_ci .reg_write = regmap_sunxi_rsb_reg_write, 4448c2ecf20Sopenharmony_ci .reg_read = regmap_sunxi_rsb_reg_read, 4458c2ecf20Sopenharmony_ci .free_context = regmap_sunxi_rsb_free_ctx, 4468c2ecf20Sopenharmony_ci .reg_format_endian_default = REGMAP_ENDIAN_NATIVE, 4478c2ecf20Sopenharmony_ci .val_format_endian_default = REGMAP_ENDIAN_NATIVE, 4488c2ecf20Sopenharmony_ci}; 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_cistatic struct sunxi_rsb_ctx *regmap_sunxi_rsb_init_ctx(struct sunxi_rsb_device *rdev, 4518c2ecf20Sopenharmony_ci const struct regmap_config *config) 4528c2ecf20Sopenharmony_ci{ 4538c2ecf20Sopenharmony_ci struct sunxi_rsb_ctx *ctx; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci switch (config->val_bits) { 4568c2ecf20Sopenharmony_ci case 8: 4578c2ecf20Sopenharmony_ci case 16: 4588c2ecf20Sopenharmony_ci case 32: 4598c2ecf20Sopenharmony_ci break; 4608c2ecf20Sopenharmony_ci default: 4618c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 4628c2ecf20Sopenharmony_ci } 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 4658c2ecf20Sopenharmony_ci if (!ctx) 4668c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci ctx->rdev = rdev; 4698c2ecf20Sopenharmony_ci ctx->size = config->val_bits / 8; 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci return ctx; 4728c2ecf20Sopenharmony_ci} 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_cistruct regmap *__devm_regmap_init_sunxi_rsb(struct sunxi_rsb_device *rdev, 4758c2ecf20Sopenharmony_ci const struct regmap_config *config, 4768c2ecf20Sopenharmony_ci struct lock_class_key *lock_key, 4778c2ecf20Sopenharmony_ci const char *lock_name) 4788c2ecf20Sopenharmony_ci{ 4798c2ecf20Sopenharmony_ci struct sunxi_rsb_ctx *ctx = regmap_sunxi_rsb_init_ctx(rdev, config); 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci if (IS_ERR(ctx)) 4828c2ecf20Sopenharmony_ci return ERR_CAST(ctx); 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci return __devm_regmap_init(&rdev->dev, ®map_sunxi_rsb, ctx, config, 4858c2ecf20Sopenharmony_ci lock_key, lock_name); 4868c2ecf20Sopenharmony_ci} 4878c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__devm_regmap_init_sunxi_rsb); 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci/* RSB controller driver functions */ 4908c2ecf20Sopenharmony_cistatic irqreturn_t sunxi_rsb_irq(int irq, void *dev_id) 4918c2ecf20Sopenharmony_ci{ 4928c2ecf20Sopenharmony_ci struct sunxi_rsb *rsb = dev_id; 4938c2ecf20Sopenharmony_ci u32 status; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci status = readl(rsb->regs + RSB_INTS); 4968c2ecf20Sopenharmony_ci rsb->status = status; 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci /* Clear interrupts */ 4998c2ecf20Sopenharmony_ci status &= (RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR | 5008c2ecf20Sopenharmony_ci RSB_INTS_TRANS_OVER); 5018c2ecf20Sopenharmony_ci writel(status, rsb->regs + RSB_INTS); 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci complete(&rsb->complete); 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci return IRQ_HANDLED; 5068c2ecf20Sopenharmony_ci} 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_cistatic int sunxi_rsb_init_device_mode(struct sunxi_rsb *rsb) 5098c2ecf20Sopenharmony_ci{ 5108c2ecf20Sopenharmony_ci int ret = 0; 5118c2ecf20Sopenharmony_ci u32 reg; 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci /* send init sequence */ 5148c2ecf20Sopenharmony_ci writel(RSB_DMCR_DEVICE_START | RSB_DMCR_MODE_DATA | 5158c2ecf20Sopenharmony_ci RSB_DMCR_MODE_REG | RSB_DMCR_DEV_ADDR, rsb->regs + RSB_DMCR); 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci readl_poll_timeout(rsb->regs + RSB_DMCR, reg, 5188c2ecf20Sopenharmony_ci !(reg & RSB_DMCR_DEVICE_START), 100, 250000); 5198c2ecf20Sopenharmony_ci if (reg & RSB_DMCR_DEVICE_START) 5208c2ecf20Sopenharmony_ci ret = -ETIMEDOUT; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci /* clear interrupt status bits */ 5238c2ecf20Sopenharmony_ci writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS); 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci return ret; 5268c2ecf20Sopenharmony_ci} 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci/* 5298c2ecf20Sopenharmony_ci * There are 15 valid runtime addresses, though Allwinner typically 5308c2ecf20Sopenharmony_ci * skips the first, for unknown reasons, and uses the following three. 5318c2ecf20Sopenharmony_ci * 5328c2ecf20Sopenharmony_ci * 0x17, 0x2d, 0x3a, 0x4e, 0x59, 0x63, 0x74, 0x8b, 5338c2ecf20Sopenharmony_ci * 0x9c, 0xa6, 0xb1, 0xc5, 0xd2, 0xe8, 0xff 5348c2ecf20Sopenharmony_ci * 5358c2ecf20Sopenharmony_ci * No designs with 2 RSB slave devices sharing identical hardware 5368c2ecf20Sopenharmony_ci * addresses on the same bus have been seen in the wild. All designs 5378c2ecf20Sopenharmony_ci * use 0x2d for the primary PMIC, 0x3a for the secondary PMIC if 5388c2ecf20Sopenharmony_ci * there is one, and 0x45 for peripheral ICs. 5398c2ecf20Sopenharmony_ci * 5408c2ecf20Sopenharmony_ci * The hardware does not seem to support re-setting runtime addresses. 5418c2ecf20Sopenharmony_ci * Attempts to do so result in the slave devices returning a NACK. 5428c2ecf20Sopenharmony_ci * Hence we just hardcode the mapping here, like Allwinner does. 5438c2ecf20Sopenharmony_ci */ 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_cistatic const struct sunxi_rsb_addr_map sunxi_rsb_addr_maps[] = { 5468c2ecf20Sopenharmony_ci { 0x3a3, 0x2d }, /* Primary PMIC: AXP223, AXP809, AXP81X, ... */ 5478c2ecf20Sopenharmony_ci { 0x745, 0x3a }, /* Secondary PMIC: AXP806, ... */ 5488c2ecf20Sopenharmony_ci { 0xe89, 0x4e }, /* Peripheral IC: AC100, ... */ 5498c2ecf20Sopenharmony_ci}; 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_cistatic u8 sunxi_rsb_get_rtaddr(u16 hwaddr) 5528c2ecf20Sopenharmony_ci{ 5538c2ecf20Sopenharmony_ci int i; 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(sunxi_rsb_addr_maps); i++) 5568c2ecf20Sopenharmony_ci if (hwaddr == sunxi_rsb_addr_maps[i].hwaddr) 5578c2ecf20Sopenharmony_ci return sunxi_rsb_addr_maps[i].rtaddr; 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci return 0; /* 0 is an invalid runtime address */ 5608c2ecf20Sopenharmony_ci} 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_cistatic int of_rsb_register_devices(struct sunxi_rsb *rsb) 5638c2ecf20Sopenharmony_ci{ 5648c2ecf20Sopenharmony_ci struct device *dev = rsb->dev; 5658c2ecf20Sopenharmony_ci struct device_node *child, *np = dev->of_node; 5668c2ecf20Sopenharmony_ci u32 hwaddr; 5678c2ecf20Sopenharmony_ci u8 rtaddr; 5688c2ecf20Sopenharmony_ci int ret; 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci if (!np) 5718c2ecf20Sopenharmony_ci return -EINVAL; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci /* Runtime addresses for all slaves should be set first */ 5748c2ecf20Sopenharmony_ci for_each_available_child_of_node(np, child) { 5758c2ecf20Sopenharmony_ci dev_dbg(dev, "setting child %pOF runtime address\n", 5768c2ecf20Sopenharmony_ci child); 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "reg", &hwaddr); 5798c2ecf20Sopenharmony_ci if (ret) { 5808c2ecf20Sopenharmony_ci dev_err(dev, "%pOF: invalid 'reg' property: %d\n", 5818c2ecf20Sopenharmony_ci child, ret); 5828c2ecf20Sopenharmony_ci continue; 5838c2ecf20Sopenharmony_ci } 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_ci rtaddr = sunxi_rsb_get_rtaddr(hwaddr); 5868c2ecf20Sopenharmony_ci if (!rtaddr) { 5878c2ecf20Sopenharmony_ci dev_err(dev, "%pOF: unknown hardware device address\n", 5888c2ecf20Sopenharmony_ci child); 5898c2ecf20Sopenharmony_ci continue; 5908c2ecf20Sopenharmony_ci } 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci /* 5938c2ecf20Sopenharmony_ci * Since no devices have been registered yet, we are the 5948c2ecf20Sopenharmony_ci * only ones using the bus, we can skip locking the bus. 5958c2ecf20Sopenharmony_ci */ 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci /* setup command parameters */ 5988c2ecf20Sopenharmony_ci writel(RSB_CMD_STRA, rsb->regs + RSB_CMD); 5998c2ecf20Sopenharmony_ci writel(RSB_DAR_RTA(rtaddr) | RSB_DAR_DA(hwaddr), 6008c2ecf20Sopenharmony_ci rsb->regs + RSB_DAR); 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci /* send command */ 6038c2ecf20Sopenharmony_ci ret = _sunxi_rsb_run_xfer(rsb); 6048c2ecf20Sopenharmony_ci if (ret) 6058c2ecf20Sopenharmony_ci dev_warn(dev, "%pOF: set runtime address failed: %d\n", 6068c2ecf20Sopenharmony_ci child, ret); 6078c2ecf20Sopenharmony_ci } 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci /* Then we start adding devices and probing them */ 6108c2ecf20Sopenharmony_ci for_each_available_child_of_node(np, child) { 6118c2ecf20Sopenharmony_ci struct sunxi_rsb_device *rdev; 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci dev_dbg(dev, "adding child %pOF\n", child); 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "reg", &hwaddr); 6168c2ecf20Sopenharmony_ci if (ret) 6178c2ecf20Sopenharmony_ci continue; 6188c2ecf20Sopenharmony_ci 6198c2ecf20Sopenharmony_ci rtaddr = sunxi_rsb_get_rtaddr(hwaddr); 6208c2ecf20Sopenharmony_ci if (!rtaddr) 6218c2ecf20Sopenharmony_ci continue; 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci rdev = sunxi_rsb_device_create(rsb, child, hwaddr, rtaddr); 6248c2ecf20Sopenharmony_ci if (IS_ERR(rdev)) 6258c2ecf20Sopenharmony_ci dev_err(dev, "failed to add child device %pOF: %ld\n", 6268c2ecf20Sopenharmony_ci child, PTR_ERR(rdev)); 6278c2ecf20Sopenharmony_ci } 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci return 0; 6308c2ecf20Sopenharmony_ci} 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_cistatic const struct of_device_id sunxi_rsb_of_match_table[] = { 6338c2ecf20Sopenharmony_ci { .compatible = "allwinner,sun8i-a23-rsb" }, 6348c2ecf20Sopenharmony_ci {} 6358c2ecf20Sopenharmony_ci}; 6368c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sunxi_rsb_of_match_table); 6378c2ecf20Sopenharmony_ci 6388c2ecf20Sopenharmony_cistatic int sunxi_rsb_probe(struct platform_device *pdev) 6398c2ecf20Sopenharmony_ci{ 6408c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 6418c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 6428c2ecf20Sopenharmony_ci struct resource *r; 6438c2ecf20Sopenharmony_ci struct sunxi_rsb *rsb; 6448c2ecf20Sopenharmony_ci unsigned long p_clk_freq; 6458c2ecf20Sopenharmony_ci u32 clk_delay, clk_freq = 3000000; 6468c2ecf20Sopenharmony_ci int clk_div, irq, ret; 6478c2ecf20Sopenharmony_ci u32 reg; 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci of_property_read_u32(np, "clock-frequency", &clk_freq); 6508c2ecf20Sopenharmony_ci if (clk_freq > RSB_MAX_FREQ) { 6518c2ecf20Sopenharmony_ci dev_err(dev, 6528c2ecf20Sopenharmony_ci "clock-frequency (%u Hz) is too high (max = 20MHz)\n", 6538c2ecf20Sopenharmony_ci clk_freq); 6548c2ecf20Sopenharmony_ci return -EINVAL; 6558c2ecf20Sopenharmony_ci } 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci rsb = devm_kzalloc(dev, sizeof(*rsb), GFP_KERNEL); 6588c2ecf20Sopenharmony_ci if (!rsb) 6598c2ecf20Sopenharmony_ci return -ENOMEM; 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci rsb->dev = dev; 6628c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, rsb); 6638c2ecf20Sopenharmony_ci r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 6648c2ecf20Sopenharmony_ci rsb->regs = devm_ioremap_resource(dev, r); 6658c2ecf20Sopenharmony_ci if (IS_ERR(rsb->regs)) 6668c2ecf20Sopenharmony_ci return PTR_ERR(rsb->regs); 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci irq = platform_get_irq(pdev, 0); 6698c2ecf20Sopenharmony_ci if (irq < 0) 6708c2ecf20Sopenharmony_ci return irq; 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci rsb->clk = devm_clk_get(dev, NULL); 6738c2ecf20Sopenharmony_ci if (IS_ERR(rsb->clk)) { 6748c2ecf20Sopenharmony_ci ret = PTR_ERR(rsb->clk); 6758c2ecf20Sopenharmony_ci dev_err(dev, "failed to retrieve clk: %d\n", ret); 6768c2ecf20Sopenharmony_ci return ret; 6778c2ecf20Sopenharmony_ci } 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci ret = clk_prepare_enable(rsb->clk); 6808c2ecf20Sopenharmony_ci if (ret) { 6818c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable clk: %d\n", ret); 6828c2ecf20Sopenharmony_ci return ret; 6838c2ecf20Sopenharmony_ci } 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci p_clk_freq = clk_get_rate(rsb->clk); 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci rsb->rstc = devm_reset_control_get(dev, NULL); 6888c2ecf20Sopenharmony_ci if (IS_ERR(rsb->rstc)) { 6898c2ecf20Sopenharmony_ci ret = PTR_ERR(rsb->rstc); 6908c2ecf20Sopenharmony_ci dev_err(dev, "failed to retrieve reset controller: %d\n", ret); 6918c2ecf20Sopenharmony_ci goto err_clk_disable; 6928c2ecf20Sopenharmony_ci } 6938c2ecf20Sopenharmony_ci 6948c2ecf20Sopenharmony_ci ret = reset_control_deassert(rsb->rstc); 6958c2ecf20Sopenharmony_ci if (ret) { 6968c2ecf20Sopenharmony_ci dev_err(dev, "failed to deassert reset line: %d\n", ret); 6978c2ecf20Sopenharmony_ci goto err_clk_disable; 6988c2ecf20Sopenharmony_ci } 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_ci init_completion(&rsb->complete); 7018c2ecf20Sopenharmony_ci mutex_init(&rsb->lock); 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci /* reset the controller */ 7048c2ecf20Sopenharmony_ci writel(RSB_CTRL_SOFT_RST, rsb->regs + RSB_CTRL); 7058c2ecf20Sopenharmony_ci readl_poll_timeout(rsb->regs + RSB_CTRL, reg, 7068c2ecf20Sopenharmony_ci !(reg & RSB_CTRL_SOFT_RST), 1000, 100000); 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci /* 7098c2ecf20Sopenharmony_ci * Clock frequency and delay calculation code is from 7108c2ecf20Sopenharmony_ci * Allwinner U-boot sources. 7118c2ecf20Sopenharmony_ci * 7128c2ecf20Sopenharmony_ci * From A83 user manual: 7138c2ecf20Sopenharmony_ci * bus clock frequency = parent clock frequency / (2 * (divider + 1)) 7148c2ecf20Sopenharmony_ci */ 7158c2ecf20Sopenharmony_ci clk_div = p_clk_freq / clk_freq / 2; 7168c2ecf20Sopenharmony_ci if (!clk_div) 7178c2ecf20Sopenharmony_ci clk_div = 1; 7188c2ecf20Sopenharmony_ci else if (clk_div > RSB_CCR_MAX_CLK_DIV + 1) 7198c2ecf20Sopenharmony_ci clk_div = RSB_CCR_MAX_CLK_DIV + 1; 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ci clk_delay = clk_div >> 1; 7228c2ecf20Sopenharmony_ci if (!clk_delay) 7238c2ecf20Sopenharmony_ci clk_delay = 1; 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_ci dev_info(dev, "RSB running at %lu Hz\n", p_clk_freq / clk_div / 2); 7268c2ecf20Sopenharmony_ci writel(RSB_CCR_SDA_OUT_DELAY(clk_delay) | RSB_CCR_CLK_DIV(clk_div - 1), 7278c2ecf20Sopenharmony_ci rsb->regs + RSB_CCR); 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci ret = devm_request_irq(dev, irq, sunxi_rsb_irq, 0, RSB_CTRL_NAME, rsb); 7308c2ecf20Sopenharmony_ci if (ret) { 7318c2ecf20Sopenharmony_ci dev_err(dev, "can't register interrupt handler irq %d: %d\n", 7328c2ecf20Sopenharmony_ci irq, ret); 7338c2ecf20Sopenharmony_ci goto err_reset_assert; 7348c2ecf20Sopenharmony_ci } 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ci /* initialize all devices on the bus into RSB mode */ 7378c2ecf20Sopenharmony_ci ret = sunxi_rsb_init_device_mode(rsb); 7388c2ecf20Sopenharmony_ci if (ret) 7398c2ecf20Sopenharmony_ci dev_warn(dev, "Initialize device mode failed: %d\n", ret); 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci of_rsb_register_devices(rsb); 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci return 0; 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_cierr_reset_assert: 7468c2ecf20Sopenharmony_ci reset_control_assert(rsb->rstc); 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_cierr_clk_disable: 7498c2ecf20Sopenharmony_ci clk_disable_unprepare(rsb->clk); 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_ci return ret; 7528c2ecf20Sopenharmony_ci} 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_cistatic int sunxi_rsb_remove(struct platform_device *pdev) 7558c2ecf20Sopenharmony_ci{ 7568c2ecf20Sopenharmony_ci struct sunxi_rsb *rsb = platform_get_drvdata(pdev); 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_ci device_for_each_child(rsb->dev, NULL, sunxi_rsb_remove_devices); 7598c2ecf20Sopenharmony_ci reset_control_assert(rsb->rstc); 7608c2ecf20Sopenharmony_ci clk_disable_unprepare(rsb->clk); 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_ci return 0; 7638c2ecf20Sopenharmony_ci} 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_cistatic struct platform_driver sunxi_rsb_driver = { 7668c2ecf20Sopenharmony_ci .probe = sunxi_rsb_probe, 7678c2ecf20Sopenharmony_ci .remove = sunxi_rsb_remove, 7688c2ecf20Sopenharmony_ci .driver = { 7698c2ecf20Sopenharmony_ci .name = RSB_CTRL_NAME, 7708c2ecf20Sopenharmony_ci .of_match_table = sunxi_rsb_of_match_table, 7718c2ecf20Sopenharmony_ci }, 7728c2ecf20Sopenharmony_ci}; 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_cistatic int __init sunxi_rsb_init(void) 7758c2ecf20Sopenharmony_ci{ 7768c2ecf20Sopenharmony_ci int ret; 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci ret = bus_register(&sunxi_rsb_bus); 7798c2ecf20Sopenharmony_ci if (ret) { 7808c2ecf20Sopenharmony_ci pr_err("failed to register sunxi sunxi_rsb bus: %d\n", ret); 7818c2ecf20Sopenharmony_ci return ret; 7828c2ecf20Sopenharmony_ci } 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci ret = platform_driver_register(&sunxi_rsb_driver); 7858c2ecf20Sopenharmony_ci if (ret) { 7868c2ecf20Sopenharmony_ci bus_unregister(&sunxi_rsb_bus); 7878c2ecf20Sopenharmony_ci return ret; 7888c2ecf20Sopenharmony_ci } 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci return 0; 7918c2ecf20Sopenharmony_ci} 7928c2ecf20Sopenharmony_cimodule_init(sunxi_rsb_init); 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_cistatic void __exit sunxi_rsb_exit(void) 7958c2ecf20Sopenharmony_ci{ 7968c2ecf20Sopenharmony_ci platform_driver_unregister(&sunxi_rsb_driver); 7978c2ecf20Sopenharmony_ci bus_unregister(&sunxi_rsb_bus); 7988c2ecf20Sopenharmony_ci} 7998c2ecf20Sopenharmony_cimodule_exit(sunxi_rsb_exit); 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ciMODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>"); 8028c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Allwinner sunXi Reduced Serial Bus controller driver"); 8038c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 804