18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* linux/drivers/i2c/busses/i2c-s3c2410.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2004,2005,2009 Simtec Electronics 58c2ecf20Sopenharmony_ci * Ben Dooks <ben@simtec.co.uk> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * S3C2410 I2C Controller 88c2ecf20Sopenharmony_ci*/ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <linux/init.h> 158c2ecf20Sopenharmony_ci#include <linux/time.h> 168c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 178c2ecf20Sopenharmony_ci#include <linux/delay.h> 188c2ecf20Sopenharmony_ci#include <linux/errno.h> 198c2ecf20Sopenharmony_ci#include <linux/err.h> 208c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 218c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 228c2ecf20Sopenharmony_ci#include <linux/clk.h> 238c2ecf20Sopenharmony_ci#include <linux/cpufreq.h> 248c2ecf20Sopenharmony_ci#include <linux/slab.h> 258c2ecf20Sopenharmony_ci#include <linux/io.h> 268c2ecf20Sopenharmony_ci#include <linux/of.h> 278c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 288c2ecf20Sopenharmony_ci#include <linux/pinctrl/consumer.h> 298c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 308c2ecf20Sopenharmony_ci#include <linux/regmap.h> 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#include <asm/irq.h> 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#include <linux/platform_data/i2c-s3c2410.h> 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* see s3c2410x user guide, v1.1, section 9 (p447) for more info */ 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define S3C2410_IICCON 0x00 398c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT 0x04 408c2ecf20Sopenharmony_ci#define S3C2410_IICADD 0x08 418c2ecf20Sopenharmony_ci#define S3C2410_IICDS 0x0C 428c2ecf20Sopenharmony_ci#define S3C2440_IICLC 0x10 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci#define S3C2410_IICCON_ACKEN (1 << 7) 458c2ecf20Sopenharmony_ci#define S3C2410_IICCON_TXDIV_16 (0 << 6) 468c2ecf20Sopenharmony_ci#define S3C2410_IICCON_TXDIV_512 (1 << 6) 478c2ecf20Sopenharmony_ci#define S3C2410_IICCON_IRQEN (1 << 5) 488c2ecf20Sopenharmony_ci#define S3C2410_IICCON_IRQPEND (1 << 4) 498c2ecf20Sopenharmony_ci#define S3C2410_IICCON_SCALE(x) ((x) & 0xf) 508c2ecf20Sopenharmony_ci#define S3C2410_IICCON_SCALEMASK (0xf) 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_MASTER_RX (2 << 6) 538c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_MASTER_TX (3 << 6) 548c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_SLAVE_RX (0 << 6) 558c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_SLAVE_TX (1 << 6) 568c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_MODEMASK (3 << 6) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_START (1 << 5) 598c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_BUSBUSY (1 << 5) 608c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_TXRXEN (1 << 4) 618c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_ARBITR (1 << 3) 628c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_ASSLAVE (1 << 2) 638c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_ADDR0 (1 << 1) 648c2ecf20Sopenharmony_ci#define S3C2410_IICSTAT_LASTBIT (1 << 0) 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci#define S3C2410_IICLC_SDA_DELAY0 (0 << 0) 678c2ecf20Sopenharmony_ci#define S3C2410_IICLC_SDA_DELAY5 (1 << 0) 688c2ecf20Sopenharmony_ci#define S3C2410_IICLC_SDA_DELAY10 (2 << 0) 698c2ecf20Sopenharmony_ci#define S3C2410_IICLC_SDA_DELAY15 (3 << 0) 708c2ecf20Sopenharmony_ci#define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0) 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci#define S3C2410_IICLC_FILTER_ON (1 << 2) 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci/* Treat S3C2410 as baseline hardware, anything else is supported via quirks */ 758c2ecf20Sopenharmony_ci#define QUIRK_S3C2440 (1 << 0) 768c2ecf20Sopenharmony_ci#define QUIRK_HDMIPHY (1 << 1) 778c2ecf20Sopenharmony_ci#define QUIRK_NO_GPIO (1 << 2) 788c2ecf20Sopenharmony_ci#define QUIRK_POLL (1 << 3) 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci/* Max time to wait for bus to become idle after a xfer (in us) */ 818c2ecf20Sopenharmony_ci#define S3C2410_IDLE_TIMEOUT 5000 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci/* Exynos5 Sysreg offset */ 848c2ecf20Sopenharmony_ci#define EXYNOS5_SYS_I2C_CFG 0x0234 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci/* i2c controller state */ 878c2ecf20Sopenharmony_cienum s3c24xx_i2c_state { 888c2ecf20Sopenharmony_ci STATE_IDLE, 898c2ecf20Sopenharmony_ci STATE_START, 908c2ecf20Sopenharmony_ci STATE_READ, 918c2ecf20Sopenharmony_ci STATE_WRITE, 928c2ecf20Sopenharmony_ci STATE_STOP 938c2ecf20Sopenharmony_ci}; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistruct s3c24xx_i2c { 968c2ecf20Sopenharmony_ci wait_queue_head_t wait; 978c2ecf20Sopenharmony_ci kernel_ulong_t quirks; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci struct i2c_msg *msg; 1008c2ecf20Sopenharmony_ci unsigned int msg_num; 1018c2ecf20Sopenharmony_ci unsigned int msg_idx; 1028c2ecf20Sopenharmony_ci unsigned int msg_ptr; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci unsigned int tx_setup; 1058c2ecf20Sopenharmony_ci unsigned int irq; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci enum s3c24xx_i2c_state state; 1088c2ecf20Sopenharmony_ci unsigned long clkrate; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci void __iomem *regs; 1118c2ecf20Sopenharmony_ci struct clk *clk; 1128c2ecf20Sopenharmony_ci struct device *dev; 1138c2ecf20Sopenharmony_ci struct i2c_adapter adap; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci struct s3c2410_platform_i2c *pdata; 1168c2ecf20Sopenharmony_ci struct gpio_desc *gpios[2]; 1178c2ecf20Sopenharmony_ci struct pinctrl *pctrl; 1188c2ecf20Sopenharmony_ci#if defined(CONFIG_ARM_S3C24XX_CPUFREQ) 1198c2ecf20Sopenharmony_ci struct notifier_block freq_transition; 1208c2ecf20Sopenharmony_ci#endif 1218c2ecf20Sopenharmony_ci struct regmap *sysreg; 1228c2ecf20Sopenharmony_ci unsigned int sys_i2c_cfg; 1238c2ecf20Sopenharmony_ci}; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic const struct platform_device_id s3c24xx_driver_ids[] = { 1268c2ecf20Sopenharmony_ci { 1278c2ecf20Sopenharmony_ci .name = "s3c2410-i2c", 1288c2ecf20Sopenharmony_ci .driver_data = 0, 1298c2ecf20Sopenharmony_ci }, { 1308c2ecf20Sopenharmony_ci .name = "s3c2440-i2c", 1318c2ecf20Sopenharmony_ci .driver_data = QUIRK_S3C2440, 1328c2ecf20Sopenharmony_ci }, { 1338c2ecf20Sopenharmony_ci .name = "s3c2440-hdmiphy-i2c", 1348c2ecf20Sopenharmony_ci .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO, 1358c2ecf20Sopenharmony_ci }, { }, 1368c2ecf20Sopenharmony_ci}; 1378c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 1428c2ecf20Sopenharmony_cistatic const struct of_device_id s3c24xx_i2c_match[] = { 1438c2ecf20Sopenharmony_ci { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 }, 1448c2ecf20Sopenharmony_ci { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 }, 1458c2ecf20Sopenharmony_ci { .compatible = "samsung,s3c2440-hdmiphy-i2c", 1468c2ecf20Sopenharmony_ci .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) }, 1478c2ecf20Sopenharmony_ci { .compatible = "samsung,exynos5-sata-phy-i2c", 1488c2ecf20Sopenharmony_ci .data = (void *)(QUIRK_S3C2440 | QUIRK_POLL | QUIRK_NO_GPIO) }, 1498c2ecf20Sopenharmony_ci {}, 1508c2ecf20Sopenharmony_ci}; 1518c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, s3c24xx_i2c_match); 1528c2ecf20Sopenharmony_ci#endif 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci/* 1558c2ecf20Sopenharmony_ci * Get controller type either from device tree or platform device variant. 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_cistatic inline kernel_ulong_t s3c24xx_get_device_quirks(struct platform_device *pdev) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci if (pdev->dev.of_node) { 1608c2ecf20Sopenharmony_ci const struct of_device_id *match; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node); 1638c2ecf20Sopenharmony_ci return (kernel_ulong_t)match->data; 1648c2ecf20Sopenharmony_ci } 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci return platform_get_device_id(pdev)->driver_data; 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci/* 1708c2ecf20Sopenharmony_ci * Complete the message and wake up the caller, using the given return code, 1718c2ecf20Sopenharmony_ci * or zero to mean ok. 1728c2ecf20Sopenharmony_ci */ 1738c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) 1748c2ecf20Sopenharmony_ci{ 1758c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "master_complete %d\n", ret); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci i2c->msg_ptr = 0; 1788c2ecf20Sopenharmony_ci i2c->msg = NULL; 1798c2ecf20Sopenharmony_ci i2c->msg_idx++; 1808c2ecf20Sopenharmony_ci i2c->msg_num = 0; 1818c2ecf20Sopenharmony_ci if (ret) 1828c2ecf20Sopenharmony_ci i2c->msg_idx = ret; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci if (!(i2c->quirks & QUIRK_POLL)) 1858c2ecf20Sopenharmony_ci wake_up(&i2c->wait); 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci unsigned long tmp; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci tmp = readl(i2c->regs + S3C2410_IICCON); 1938c2ecf20Sopenharmony_ci writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 1948c2ecf20Sopenharmony_ci} 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci unsigned long tmp; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci tmp = readl(i2c->regs + S3C2410_IICCON); 2018c2ecf20Sopenharmony_ci writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci/* irq enable/disable functions */ 2058c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci unsigned long tmp; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci tmp = readl(i2c->regs + S3C2410_IICCON); 2108c2ecf20Sopenharmony_ci writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci unsigned long tmp; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci tmp = readl(i2c->regs + S3C2410_IICCON); 2188c2ecf20Sopenharmony_ci writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistatic bool is_ack(struct s3c24xx_i2c *i2c) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci int tries; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci for (tries = 50; tries; --tries) { 2268c2ecf20Sopenharmony_ci unsigned long tmp = readl(i2c->regs + S3C2410_IICCON); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci if (!(tmp & S3C2410_IICCON_ACKEN)) { 2298c2ecf20Sopenharmony_ci /* 2308c2ecf20Sopenharmony_ci * Wait a bit for the bus to stabilize, 2318c2ecf20Sopenharmony_ci * delay estimated experimentally. 2328c2ecf20Sopenharmony_ci */ 2338c2ecf20Sopenharmony_ci usleep_range(100, 200); 2348c2ecf20Sopenharmony_ci return true; 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci if (tmp & S3C2410_IICCON_IRQPEND) { 2378c2ecf20Sopenharmony_ci if (!(readl(i2c->regs + S3C2410_IICSTAT) 2388c2ecf20Sopenharmony_ci & S3C2410_IICSTAT_LASTBIT)) 2398c2ecf20Sopenharmony_ci return true; 2408c2ecf20Sopenharmony_ci } 2418c2ecf20Sopenharmony_ci usleep_range(1000, 2000); 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci dev_err(i2c->dev, "ack was not received\n"); 2448c2ecf20Sopenharmony_ci return false; 2458c2ecf20Sopenharmony_ci} 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci/* 2488c2ecf20Sopenharmony_ci * put the start of a message onto the bus 2498c2ecf20Sopenharmony_ci */ 2508c2ecf20Sopenharmony_cistatic void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 2518c2ecf20Sopenharmony_ci struct i2c_msg *msg) 2528c2ecf20Sopenharmony_ci{ 2538c2ecf20Sopenharmony_ci unsigned int addr = (msg->addr & 0x7f) << 1; 2548c2ecf20Sopenharmony_ci unsigned long stat; 2558c2ecf20Sopenharmony_ci unsigned long iiccon; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci stat = 0; 2588c2ecf20Sopenharmony_ci stat |= S3C2410_IICSTAT_TXRXEN; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci if (msg->flags & I2C_M_RD) { 2618c2ecf20Sopenharmony_ci stat |= S3C2410_IICSTAT_MASTER_RX; 2628c2ecf20Sopenharmony_ci addr |= 1; 2638c2ecf20Sopenharmony_ci } else 2648c2ecf20Sopenharmony_ci stat |= S3C2410_IICSTAT_MASTER_TX; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci if (msg->flags & I2C_M_REV_DIR_ADDR) 2678c2ecf20Sopenharmony_ci addr ^= 1; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci /* todo - check for whether ack wanted or not */ 2708c2ecf20Sopenharmony_ci s3c24xx_i2c_enable_ack(i2c); 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci iiccon = readl(i2c->regs + S3C2410_IICCON); 2738c2ecf20Sopenharmony_ci writel(stat, i2c->regs + S3C2410_IICSTAT); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); 2768c2ecf20Sopenharmony_ci writeb(addr, i2c->regs + S3C2410_IICDS); 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci /* 2798c2ecf20Sopenharmony_ci * delay here to ensure the data byte has gotten onto the bus 2808c2ecf20Sopenharmony_ci * before the transaction is started 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_ci ndelay(i2c->tx_setup); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); 2858c2ecf20Sopenharmony_ci writel(iiccon, i2c->regs + S3C2410_IICCON); 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci stat |= S3C2410_IICSTAT_START; 2888c2ecf20Sopenharmony_ci writel(stat, i2c->regs + S3C2410_IICSTAT); 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) 2928c2ecf20Sopenharmony_ci{ 2938c2ecf20Sopenharmony_ci unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "STOP\n"); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci /* 2988c2ecf20Sopenharmony_ci * The datasheet says that the STOP sequence should be: 2998c2ecf20Sopenharmony_ci * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP') 3008c2ecf20Sopenharmony_ci * 2) I2CCON.4 = 0 - Clear IRQPEND 3018c2ecf20Sopenharmony_ci * 3) Wait until the stop condition takes effect. 3028c2ecf20Sopenharmony_ci * 4*) I2CSTAT.4 = 0 - Clear TXRXEN 3038c2ecf20Sopenharmony_ci * 3048c2ecf20Sopenharmony_ci * Where, step "4*" is only for buses with the "HDMIPHY" quirk. 3058c2ecf20Sopenharmony_ci * 3068c2ecf20Sopenharmony_ci * However, after much experimentation, it appears that: 3078c2ecf20Sopenharmony_ci * a) normal buses automatically clear BUSY and transition from 3088c2ecf20Sopenharmony_ci * Master->Slave when they complete generating a STOP condition. 3098c2ecf20Sopenharmony_ci * Therefore, step (3) can be done in doxfer() by polling I2CCON.4 3108c2ecf20Sopenharmony_ci * after starting the STOP generation here. 3118c2ecf20Sopenharmony_ci * b) HDMIPHY bus does neither, so there is no way to do step 3. 3128c2ecf20Sopenharmony_ci * There is no indication when this bus has finished generating 3138c2ecf20Sopenharmony_ci * STOP. 3148c2ecf20Sopenharmony_ci * 3158c2ecf20Sopenharmony_ci * In fact, we have found that as soon as the IRQPEND bit is cleared in 3168c2ecf20Sopenharmony_ci * step 2, the HDMIPHY bus generates the STOP condition, and then 3178c2ecf20Sopenharmony_ci * immediately starts transferring another data byte, even though the 3188c2ecf20Sopenharmony_ci * bus is supposedly stopped. This is presumably because the bus is 3198c2ecf20Sopenharmony_ci * still in "Master" mode, and its BUSY bit is still set. 3208c2ecf20Sopenharmony_ci * 3218c2ecf20Sopenharmony_ci * To avoid these extra post-STOP transactions on HDMI phy devices, we 3228c2ecf20Sopenharmony_ci * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly, 3238c2ecf20Sopenharmony_ci * instead of first generating a proper STOP condition. This should 3248c2ecf20Sopenharmony_ci * float SDA & SCK terminating the transfer. Subsequent transfers 3258c2ecf20Sopenharmony_ci * start with a proper START condition, and proceed normally. 3268c2ecf20Sopenharmony_ci * 3278c2ecf20Sopenharmony_ci * The HDMIPHY bus is an internal bus that always has exactly two 3288c2ecf20Sopenharmony_ci * devices, the host as Master and the HDMIPHY device as the slave. 3298c2ecf20Sopenharmony_ci * Skipping the STOP condition has been tested on this bus and works. 3308c2ecf20Sopenharmony_ci */ 3318c2ecf20Sopenharmony_ci if (i2c->quirks & QUIRK_HDMIPHY) { 3328c2ecf20Sopenharmony_ci /* Stop driving the I2C pins */ 3338c2ecf20Sopenharmony_ci iicstat &= ~S3C2410_IICSTAT_TXRXEN; 3348c2ecf20Sopenharmony_ci } else { 3358c2ecf20Sopenharmony_ci /* stop the transfer */ 3368c2ecf20Sopenharmony_ci iicstat &= ~S3C2410_IICSTAT_START; 3378c2ecf20Sopenharmony_ci } 3388c2ecf20Sopenharmony_ci writel(iicstat, i2c->regs + S3C2410_IICSTAT); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci i2c->state = STATE_STOP; 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci s3c24xx_i2c_master_complete(i2c, ret); 3438c2ecf20Sopenharmony_ci s3c24xx_i2c_disable_irq(i2c); 3448c2ecf20Sopenharmony_ci} 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci/* 3478c2ecf20Sopenharmony_ci * helper functions to determine the current state in the set of 3488c2ecf20Sopenharmony_ci * messages we are sending 3498c2ecf20Sopenharmony_ci */ 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci/* 3528c2ecf20Sopenharmony_ci * returns TRUE if the current message is the last in the set 3538c2ecf20Sopenharmony_ci */ 3548c2ecf20Sopenharmony_cistatic inline int is_lastmsg(struct s3c24xx_i2c *i2c) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci return i2c->msg_idx >= (i2c->msg_num - 1); 3578c2ecf20Sopenharmony_ci} 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci/* 3608c2ecf20Sopenharmony_ci * returns TRUE if we this is the last byte in the current message 3618c2ecf20Sopenharmony_ci */ 3628c2ecf20Sopenharmony_cistatic inline int is_msglast(struct s3c24xx_i2c *i2c) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci /* 3658c2ecf20Sopenharmony_ci * msg->len is always 1 for the first byte of smbus block read. 3668c2ecf20Sopenharmony_ci * Actual length will be read from slave. More bytes will be 3678c2ecf20Sopenharmony_ci * read according to the length then. 3688c2ecf20Sopenharmony_ci */ 3698c2ecf20Sopenharmony_ci if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1) 3708c2ecf20Sopenharmony_ci return 0; 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci return i2c->msg_ptr == i2c->msg->len-1; 3738c2ecf20Sopenharmony_ci} 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci/* 3768c2ecf20Sopenharmony_ci * returns TRUE if we reached the end of the current message 3778c2ecf20Sopenharmony_ci */ 3788c2ecf20Sopenharmony_cistatic inline int is_msgend(struct s3c24xx_i2c *i2c) 3798c2ecf20Sopenharmony_ci{ 3808c2ecf20Sopenharmony_ci return i2c->msg_ptr >= i2c->msg->len; 3818c2ecf20Sopenharmony_ci} 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci/* 3848c2ecf20Sopenharmony_ci * process an interrupt and work out what to do 3858c2ecf20Sopenharmony_ci */ 3868c2ecf20Sopenharmony_cistatic int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci unsigned long tmp; 3898c2ecf20Sopenharmony_ci unsigned char byte; 3908c2ecf20Sopenharmony_ci int ret = 0; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci switch (i2c->state) { 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci case STATE_IDLE: 3958c2ecf20Sopenharmony_ci dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); 3968c2ecf20Sopenharmony_ci goto out; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci case STATE_STOP: 3998c2ecf20Sopenharmony_ci dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); 4008c2ecf20Sopenharmony_ci s3c24xx_i2c_disable_irq(i2c); 4018c2ecf20Sopenharmony_ci goto out_ack; 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci case STATE_START: 4048c2ecf20Sopenharmony_ci /* 4058c2ecf20Sopenharmony_ci * last thing we did was send a start condition on the 4068c2ecf20Sopenharmony_ci * bus, or started a new i2c message 4078c2ecf20Sopenharmony_ci */ 4088c2ecf20Sopenharmony_ci if (iicstat & S3C2410_IICSTAT_LASTBIT && 4098c2ecf20Sopenharmony_ci !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 4108c2ecf20Sopenharmony_ci /* ack was not received... */ 4118c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "ack was not received\n"); 4128c2ecf20Sopenharmony_ci s3c24xx_i2c_stop(i2c, -ENXIO); 4138c2ecf20Sopenharmony_ci goto out_ack; 4148c2ecf20Sopenharmony_ci } 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci if (i2c->msg->flags & I2C_M_RD) 4178c2ecf20Sopenharmony_ci i2c->state = STATE_READ; 4188c2ecf20Sopenharmony_ci else 4198c2ecf20Sopenharmony_ci i2c->state = STATE_WRITE; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci /* 4228c2ecf20Sopenharmony_ci * Terminate the transfer if there is nothing to do 4238c2ecf20Sopenharmony_ci * as this is used by the i2c probe to find devices. 4248c2ecf20Sopenharmony_ci */ 4258c2ecf20Sopenharmony_ci if (is_lastmsg(i2c) && i2c->msg->len == 0) { 4268c2ecf20Sopenharmony_ci s3c24xx_i2c_stop(i2c, 0); 4278c2ecf20Sopenharmony_ci goto out_ack; 4288c2ecf20Sopenharmony_ci } 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci if (i2c->state == STATE_READ) 4318c2ecf20Sopenharmony_ci goto prepare_read; 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci /* 4348c2ecf20Sopenharmony_ci * fall through to the write state, as we will need to 4358c2ecf20Sopenharmony_ci * send a byte as well 4368c2ecf20Sopenharmony_ci */ 4378c2ecf20Sopenharmony_ci fallthrough; 4388c2ecf20Sopenharmony_ci case STATE_WRITE: 4398c2ecf20Sopenharmony_ci /* 4408c2ecf20Sopenharmony_ci * we are writing data to the device... check for the 4418c2ecf20Sopenharmony_ci * end of the message, and if so, work out what to do 4428c2ecf20Sopenharmony_ci */ 4438c2ecf20Sopenharmony_ci if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 4448c2ecf20Sopenharmony_ci if (iicstat & S3C2410_IICSTAT_LASTBIT) { 4458c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "WRITE: No Ack\n"); 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci s3c24xx_i2c_stop(i2c, -ECONNREFUSED); 4488c2ecf20Sopenharmony_ci goto out_ack; 4498c2ecf20Sopenharmony_ci } 4508c2ecf20Sopenharmony_ci } 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci retry_write: 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci if (!is_msgend(i2c)) { 4558c2ecf20Sopenharmony_ci byte = i2c->msg->buf[i2c->msg_ptr++]; 4568c2ecf20Sopenharmony_ci writeb(byte, i2c->regs + S3C2410_IICDS); 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci /* 4598c2ecf20Sopenharmony_ci * delay after writing the byte to allow the 4608c2ecf20Sopenharmony_ci * data setup time on the bus, as writing the 4618c2ecf20Sopenharmony_ci * data to the register causes the first bit 4628c2ecf20Sopenharmony_ci * to appear on SDA, and SCL will change as 4638c2ecf20Sopenharmony_ci * soon as the interrupt is acknowledged 4648c2ecf20Sopenharmony_ci */ 4658c2ecf20Sopenharmony_ci ndelay(i2c->tx_setup); 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci } else if (!is_lastmsg(i2c)) { 4688c2ecf20Sopenharmony_ci /* we need to go to the next i2c message */ 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "WRITE: Next Message\n"); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci i2c->msg_ptr = 0; 4738c2ecf20Sopenharmony_ci i2c->msg_idx++; 4748c2ecf20Sopenharmony_ci i2c->msg++; 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci /* check to see if we need to do another message */ 4778c2ecf20Sopenharmony_ci if (i2c->msg->flags & I2C_M_NOSTART) { 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci if (i2c->msg->flags & I2C_M_RD) { 4808c2ecf20Sopenharmony_ci /* 4818c2ecf20Sopenharmony_ci * cannot do this, the controller 4828c2ecf20Sopenharmony_ci * forces us to send a new START 4838c2ecf20Sopenharmony_ci * when we change direction 4848c2ecf20Sopenharmony_ci */ 4858c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, 4868c2ecf20Sopenharmony_ci "missing START before write->read\n"); 4878c2ecf20Sopenharmony_ci s3c24xx_i2c_stop(i2c, -EINVAL); 4888c2ecf20Sopenharmony_ci break; 4898c2ecf20Sopenharmony_ci } 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci goto retry_write; 4928c2ecf20Sopenharmony_ci } else { 4938c2ecf20Sopenharmony_ci /* send the new start */ 4948c2ecf20Sopenharmony_ci s3c24xx_i2c_message_start(i2c, i2c->msg); 4958c2ecf20Sopenharmony_ci i2c->state = STATE_START; 4968c2ecf20Sopenharmony_ci } 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci } else { 4998c2ecf20Sopenharmony_ci /* send stop */ 5008c2ecf20Sopenharmony_ci s3c24xx_i2c_stop(i2c, 0); 5018c2ecf20Sopenharmony_ci } 5028c2ecf20Sopenharmony_ci break; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci case STATE_READ: 5058c2ecf20Sopenharmony_ci /* 5068c2ecf20Sopenharmony_ci * we have a byte of data in the data register, do 5078c2ecf20Sopenharmony_ci * something with it, and then work out whether we are 5088c2ecf20Sopenharmony_ci * going to do any more read/write 5098c2ecf20Sopenharmony_ci */ 5108c2ecf20Sopenharmony_ci byte = readb(i2c->regs + S3C2410_IICDS); 5118c2ecf20Sopenharmony_ci i2c->msg->buf[i2c->msg_ptr++] = byte; 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci /* Add actual length to read for smbus block read */ 5148c2ecf20Sopenharmony_ci if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1) 5158c2ecf20Sopenharmony_ci i2c->msg->len += byte; 5168c2ecf20Sopenharmony_ci prepare_read: 5178c2ecf20Sopenharmony_ci if (is_msglast(i2c)) { 5188c2ecf20Sopenharmony_ci /* last byte of buffer */ 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci if (is_lastmsg(i2c)) 5218c2ecf20Sopenharmony_ci s3c24xx_i2c_disable_ack(i2c); 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci } else if (is_msgend(i2c)) { 5248c2ecf20Sopenharmony_ci /* 5258c2ecf20Sopenharmony_ci * ok, we've read the entire buffer, see if there 5268c2ecf20Sopenharmony_ci * is anything else we need to do 5278c2ecf20Sopenharmony_ci */ 5288c2ecf20Sopenharmony_ci if (is_lastmsg(i2c)) { 5298c2ecf20Sopenharmony_ci /* last message, send stop and complete */ 5308c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "READ: Send Stop\n"); 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci s3c24xx_i2c_stop(i2c, 0); 5338c2ecf20Sopenharmony_ci } else { 5348c2ecf20Sopenharmony_ci /* go to the next transfer */ 5358c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "READ: Next Transfer\n"); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci i2c->msg_ptr = 0; 5388c2ecf20Sopenharmony_ci i2c->msg_idx++; 5398c2ecf20Sopenharmony_ci i2c->msg++; 5408c2ecf20Sopenharmony_ci } 5418c2ecf20Sopenharmony_ci } 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci break; 5448c2ecf20Sopenharmony_ci } 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci /* acknowlegde the IRQ and get back on with the work */ 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci out_ack: 5498c2ecf20Sopenharmony_ci tmp = readl(i2c->regs + S3C2410_IICCON); 5508c2ecf20Sopenharmony_ci tmp &= ~S3C2410_IICCON_IRQPEND; 5518c2ecf20Sopenharmony_ci writel(tmp, i2c->regs + S3C2410_IICCON); 5528c2ecf20Sopenharmony_ci out: 5538c2ecf20Sopenharmony_ci return ret; 5548c2ecf20Sopenharmony_ci} 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci/* 5578c2ecf20Sopenharmony_ci * top level IRQ servicing routine 5588c2ecf20Sopenharmony_ci */ 5598c2ecf20Sopenharmony_cistatic irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id) 5608c2ecf20Sopenharmony_ci{ 5618c2ecf20Sopenharmony_ci struct s3c24xx_i2c *i2c = dev_id; 5628c2ecf20Sopenharmony_ci unsigned long status; 5638c2ecf20Sopenharmony_ci unsigned long tmp; 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci status = readl(i2c->regs + S3C2410_IICSTAT); 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci if (status & S3C2410_IICSTAT_ARBITR) { 5688c2ecf20Sopenharmony_ci /* deal with arbitration loss */ 5698c2ecf20Sopenharmony_ci dev_err(i2c->dev, "deal with arbitration loss\n"); 5708c2ecf20Sopenharmony_ci } 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci if (i2c->state == STATE_IDLE) { 5738c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci tmp = readl(i2c->regs + S3C2410_IICCON); 5768c2ecf20Sopenharmony_ci tmp &= ~S3C2410_IICCON_IRQPEND; 5778c2ecf20Sopenharmony_ci writel(tmp, i2c->regs + S3C2410_IICCON); 5788c2ecf20Sopenharmony_ci goto out; 5798c2ecf20Sopenharmony_ci } 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci /* 5828c2ecf20Sopenharmony_ci * pretty much this leaves us with the fact that we've 5838c2ecf20Sopenharmony_ci * transmitted or received whatever byte we last sent 5848c2ecf20Sopenharmony_ci */ 5858c2ecf20Sopenharmony_ci i2c_s3c_irq_nextbyte(i2c, status); 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci out: 5888c2ecf20Sopenharmony_ci return IRQ_HANDLED; 5898c2ecf20Sopenharmony_ci} 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci/* 5928c2ecf20Sopenharmony_ci * Disable the bus so that we won't get any interrupts from now on, or try 5938c2ecf20Sopenharmony_ci * to drive any lines. This is the default state when we don't have 5948c2ecf20Sopenharmony_ci * anything to send/receive. 5958c2ecf20Sopenharmony_ci * 5968c2ecf20Sopenharmony_ci * If there is an event on the bus, or we have a pre-existing event at 5978c2ecf20Sopenharmony_ci * kernel boot time, we may not notice the event and the I2C controller 5988c2ecf20Sopenharmony_ci * will lock the bus with the I2C clock line low indefinitely. 5998c2ecf20Sopenharmony_ci */ 6008c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_disable_bus(struct s3c24xx_i2c *i2c) 6018c2ecf20Sopenharmony_ci{ 6028c2ecf20Sopenharmony_ci unsigned long tmp; 6038c2ecf20Sopenharmony_ci 6048c2ecf20Sopenharmony_ci /* Stop driving the I2C pins */ 6058c2ecf20Sopenharmony_ci tmp = readl(i2c->regs + S3C2410_IICSTAT); 6068c2ecf20Sopenharmony_ci tmp &= ~S3C2410_IICSTAT_TXRXEN; 6078c2ecf20Sopenharmony_ci writel(tmp, i2c->regs + S3C2410_IICSTAT); 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci /* We don't expect any interrupts now, and don't want send acks */ 6108c2ecf20Sopenharmony_ci tmp = readl(i2c->regs + S3C2410_IICCON); 6118c2ecf20Sopenharmony_ci tmp &= ~(S3C2410_IICCON_IRQEN | S3C2410_IICCON_IRQPEND | 6128c2ecf20Sopenharmony_ci S3C2410_IICCON_ACKEN); 6138c2ecf20Sopenharmony_ci writel(tmp, i2c->regs + S3C2410_IICCON); 6148c2ecf20Sopenharmony_ci} 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci/* 6188c2ecf20Sopenharmony_ci * get the i2c bus for a master transaction 6198c2ecf20Sopenharmony_ci */ 6208c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) 6218c2ecf20Sopenharmony_ci{ 6228c2ecf20Sopenharmony_ci unsigned long iicstat; 6238c2ecf20Sopenharmony_ci int timeout = 400; 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_ci while (timeout-- > 0) { 6268c2ecf20Sopenharmony_ci iicstat = readl(i2c->regs + S3C2410_IICSTAT); 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) 6298c2ecf20Sopenharmony_ci return 0; 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci msleep(1); 6328c2ecf20Sopenharmony_ci } 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci return -ETIMEDOUT; 6358c2ecf20Sopenharmony_ci} 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci/* 6388c2ecf20Sopenharmony_ci * wait for the i2c bus to become idle. 6398c2ecf20Sopenharmony_ci */ 6408c2ecf20Sopenharmony_cistatic void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c) 6418c2ecf20Sopenharmony_ci{ 6428c2ecf20Sopenharmony_ci unsigned long iicstat; 6438c2ecf20Sopenharmony_ci ktime_t start, now; 6448c2ecf20Sopenharmony_ci unsigned long delay; 6458c2ecf20Sopenharmony_ci int spins; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci /* ensure the stop has been through the bus */ 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "waiting for bus idle\n"); 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci start = now = ktime_get(); 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci /* 6548c2ecf20Sopenharmony_ci * Most of the time, the bus is already idle within a few usec of the 6558c2ecf20Sopenharmony_ci * end of a transaction. However, really slow i2c devices can stretch 6568c2ecf20Sopenharmony_ci * the clock, delaying STOP generation. 6578c2ecf20Sopenharmony_ci * 6588c2ecf20Sopenharmony_ci * On slower SoCs this typically happens within a very small number of 6598c2ecf20Sopenharmony_ci * instructions so busy wait briefly to avoid scheduling overhead. 6608c2ecf20Sopenharmony_ci */ 6618c2ecf20Sopenharmony_ci spins = 3; 6628c2ecf20Sopenharmony_ci iicstat = readl(i2c->regs + S3C2410_IICSTAT); 6638c2ecf20Sopenharmony_ci while ((iicstat & S3C2410_IICSTAT_START) && --spins) { 6648c2ecf20Sopenharmony_ci cpu_relax(); 6658c2ecf20Sopenharmony_ci iicstat = readl(i2c->regs + S3C2410_IICSTAT); 6668c2ecf20Sopenharmony_ci } 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci /* 6698c2ecf20Sopenharmony_ci * If we do get an appreciable delay as a compromise between idle 6708c2ecf20Sopenharmony_ci * detection latency for the normal, fast case, and system load in the 6718c2ecf20Sopenharmony_ci * slow device case, use an exponential back off in the polling loop, 6728c2ecf20Sopenharmony_ci * up to 1/10th of the total timeout, then continue to poll at a 6738c2ecf20Sopenharmony_ci * constant rate up to the timeout. 6748c2ecf20Sopenharmony_ci */ 6758c2ecf20Sopenharmony_ci delay = 1; 6768c2ecf20Sopenharmony_ci while ((iicstat & S3C2410_IICSTAT_START) && 6778c2ecf20Sopenharmony_ci ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) { 6788c2ecf20Sopenharmony_ci usleep_range(delay, 2 * delay); 6798c2ecf20Sopenharmony_ci if (delay < S3C2410_IDLE_TIMEOUT / 10) 6808c2ecf20Sopenharmony_ci delay <<= 1; 6818c2ecf20Sopenharmony_ci now = ktime_get(); 6828c2ecf20Sopenharmony_ci iicstat = readl(i2c->regs + S3C2410_IICSTAT); 6838c2ecf20Sopenharmony_ci } 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci if (iicstat & S3C2410_IICSTAT_START) 6868c2ecf20Sopenharmony_ci dev_warn(i2c->dev, "timeout waiting for bus idle\n"); 6878c2ecf20Sopenharmony_ci} 6888c2ecf20Sopenharmony_ci 6898c2ecf20Sopenharmony_ci/* 6908c2ecf20Sopenharmony_ci * this starts an i2c transfer 6918c2ecf20Sopenharmony_ci */ 6928c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, 6938c2ecf20Sopenharmony_ci struct i2c_msg *msgs, int num) 6948c2ecf20Sopenharmony_ci{ 6958c2ecf20Sopenharmony_ci unsigned long timeout = 0; 6968c2ecf20Sopenharmony_ci int ret; 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci ret = s3c24xx_i2c_set_master(i2c); 6998c2ecf20Sopenharmony_ci if (ret != 0) { 7008c2ecf20Sopenharmony_ci dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); 7018c2ecf20Sopenharmony_ci ret = -EAGAIN; 7028c2ecf20Sopenharmony_ci goto out; 7038c2ecf20Sopenharmony_ci } 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci i2c->msg = msgs; 7068c2ecf20Sopenharmony_ci i2c->msg_num = num; 7078c2ecf20Sopenharmony_ci i2c->msg_ptr = 0; 7088c2ecf20Sopenharmony_ci i2c->msg_idx = 0; 7098c2ecf20Sopenharmony_ci i2c->state = STATE_START; 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci s3c24xx_i2c_enable_irq(i2c); 7128c2ecf20Sopenharmony_ci s3c24xx_i2c_message_start(i2c, msgs); 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_ci if (i2c->quirks & QUIRK_POLL) { 7158c2ecf20Sopenharmony_ci while ((i2c->msg_num != 0) && is_ack(i2c)) { 7168c2ecf20Sopenharmony_ci unsigned long stat = readl(i2c->regs + S3C2410_IICSTAT); 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci i2c_s3c_irq_nextbyte(i2c, stat); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci stat = readl(i2c->regs + S3C2410_IICSTAT); 7218c2ecf20Sopenharmony_ci if (stat & S3C2410_IICSTAT_ARBITR) 7228c2ecf20Sopenharmony_ci dev_err(i2c->dev, "deal with arbitration loss\n"); 7238c2ecf20Sopenharmony_ci } 7248c2ecf20Sopenharmony_ci } else { 7258c2ecf20Sopenharmony_ci timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); 7268c2ecf20Sopenharmony_ci } 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci ret = i2c->msg_idx; 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_ci /* 7318c2ecf20Sopenharmony_ci * Having these next two as dev_err() makes life very 7328c2ecf20Sopenharmony_ci * noisy when doing an i2cdetect 7338c2ecf20Sopenharmony_ci */ 7348c2ecf20Sopenharmony_ci if (timeout == 0) 7358c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "timeout\n"); 7368c2ecf20Sopenharmony_ci else if (ret != num) 7378c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci /* For QUIRK_HDMIPHY, bus is already disabled */ 7408c2ecf20Sopenharmony_ci if (i2c->quirks & QUIRK_HDMIPHY) 7418c2ecf20Sopenharmony_ci goto out; 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci s3c24xx_i2c_wait_idle(i2c); 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ci s3c24xx_i2c_disable_bus(i2c); 7468c2ecf20Sopenharmony_ci 7478c2ecf20Sopenharmony_ci out: 7488c2ecf20Sopenharmony_ci i2c->state = STATE_IDLE; 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci return ret; 7518c2ecf20Sopenharmony_ci} 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_ci/* 7548c2ecf20Sopenharmony_ci * first port of call from the i2c bus code when an message needs 7558c2ecf20Sopenharmony_ci * transferring across the i2c bus. 7568c2ecf20Sopenharmony_ci */ 7578c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_xfer(struct i2c_adapter *adap, 7588c2ecf20Sopenharmony_ci struct i2c_msg *msgs, int num) 7598c2ecf20Sopenharmony_ci{ 7608c2ecf20Sopenharmony_ci struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; 7618c2ecf20Sopenharmony_ci int retry; 7628c2ecf20Sopenharmony_ci int ret; 7638c2ecf20Sopenharmony_ci 7648c2ecf20Sopenharmony_ci ret = clk_enable(i2c->clk); 7658c2ecf20Sopenharmony_ci if (ret) 7668c2ecf20Sopenharmony_ci return ret; 7678c2ecf20Sopenharmony_ci 7688c2ecf20Sopenharmony_ci for (retry = 0; retry < adap->retries; retry++) { 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci ret = s3c24xx_i2c_doxfer(i2c, msgs, num); 7718c2ecf20Sopenharmony_ci 7728c2ecf20Sopenharmony_ci if (ret != -EAGAIN) { 7738c2ecf20Sopenharmony_ci clk_disable(i2c->clk); 7748c2ecf20Sopenharmony_ci return ret; 7758c2ecf20Sopenharmony_ci } 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci udelay(100); 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci clk_disable(i2c->clk); 7838c2ecf20Sopenharmony_ci return -EREMOTEIO; 7848c2ecf20Sopenharmony_ci} 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci/* declare our i2c functionality */ 7878c2ecf20Sopenharmony_cistatic u32 s3c24xx_i2c_func(struct i2c_adapter *adap) 7888c2ecf20Sopenharmony_ci{ 7898c2ecf20Sopenharmony_ci return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART | 7908c2ecf20Sopenharmony_ci I2C_FUNC_PROTOCOL_MANGLING; 7918c2ecf20Sopenharmony_ci} 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_ci/* i2c bus registration info */ 7948c2ecf20Sopenharmony_cistatic const struct i2c_algorithm s3c24xx_i2c_algorithm = { 7958c2ecf20Sopenharmony_ci .master_xfer = s3c24xx_i2c_xfer, 7968c2ecf20Sopenharmony_ci .functionality = s3c24xx_i2c_func, 7978c2ecf20Sopenharmony_ci}; 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci/* 8008c2ecf20Sopenharmony_ci * return the divisor settings for a given frequency 8018c2ecf20Sopenharmony_ci */ 8028c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted, 8038c2ecf20Sopenharmony_ci unsigned int *div1, unsigned int *divs) 8048c2ecf20Sopenharmony_ci{ 8058c2ecf20Sopenharmony_ci unsigned int calc_divs = clkin / wanted; 8068c2ecf20Sopenharmony_ci unsigned int calc_div1; 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci if (calc_divs > (16*16)) 8098c2ecf20Sopenharmony_ci calc_div1 = 512; 8108c2ecf20Sopenharmony_ci else 8118c2ecf20Sopenharmony_ci calc_div1 = 16; 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_ci calc_divs += calc_div1-1; 8148c2ecf20Sopenharmony_ci calc_divs /= calc_div1; 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci if (calc_divs == 0) 8178c2ecf20Sopenharmony_ci calc_divs = 1; 8188c2ecf20Sopenharmony_ci if (calc_divs > 17) 8198c2ecf20Sopenharmony_ci calc_divs = 17; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci *divs = calc_divs; 8228c2ecf20Sopenharmony_ci *div1 = calc_div1; 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_ci return clkin / (calc_divs * calc_div1); 8258c2ecf20Sopenharmony_ci} 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci/* 8288c2ecf20Sopenharmony_ci * work out a divisor for the user requested frequency setting, 8298c2ecf20Sopenharmony_ci * either by the requested frequency, or scanning the acceptable 8308c2ecf20Sopenharmony_ci * range of frequencies until something is found 8318c2ecf20Sopenharmony_ci */ 8328c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) 8338c2ecf20Sopenharmony_ci{ 8348c2ecf20Sopenharmony_ci struct s3c2410_platform_i2c *pdata = i2c->pdata; 8358c2ecf20Sopenharmony_ci unsigned long clkin = clk_get_rate(i2c->clk); 8368c2ecf20Sopenharmony_ci unsigned int divs, div1; 8378c2ecf20Sopenharmony_ci unsigned long target_frequency; 8388c2ecf20Sopenharmony_ci u32 iiccon; 8398c2ecf20Sopenharmony_ci int freq; 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_ci i2c->clkrate = clkin; 8428c2ecf20Sopenharmony_ci clkin /= 1000; /* clkin now in KHz */ 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency); 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci target_frequency = pdata->frequency ?: I2C_MAX_STANDARD_MODE_FREQ; 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci target_frequency /= 1000; /* Target frequency now in KHz */ 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs); 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ci if (freq > target_frequency) { 8538c2ecf20Sopenharmony_ci dev_err(i2c->dev, 8548c2ecf20Sopenharmony_ci "Unable to achieve desired frequency %luKHz." \ 8558c2ecf20Sopenharmony_ci " Lowest achievable %dKHz\n", target_frequency, freq); 8568c2ecf20Sopenharmony_ci return -EINVAL; 8578c2ecf20Sopenharmony_ci } 8588c2ecf20Sopenharmony_ci 8598c2ecf20Sopenharmony_ci *got = freq; 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_ci iiccon = readl(i2c->regs + S3C2410_IICCON); 8628c2ecf20Sopenharmony_ci iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); 8638c2ecf20Sopenharmony_ci iiccon |= (divs-1); 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_ci if (div1 == 512) 8668c2ecf20Sopenharmony_ci iiccon |= S3C2410_IICCON_TXDIV_512; 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ci if (i2c->quirks & QUIRK_POLL) 8698c2ecf20Sopenharmony_ci iiccon |= S3C2410_IICCON_SCALE(2); 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci writel(iiccon, i2c->regs + S3C2410_IICCON); 8728c2ecf20Sopenharmony_ci 8738c2ecf20Sopenharmony_ci if (i2c->quirks & QUIRK_S3C2440) { 8748c2ecf20Sopenharmony_ci unsigned long sda_delay; 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ci if (pdata->sda_delay) { 8778c2ecf20Sopenharmony_ci sda_delay = clkin * pdata->sda_delay; 8788c2ecf20Sopenharmony_ci sda_delay = DIV_ROUND_UP(sda_delay, 1000000); 8798c2ecf20Sopenharmony_ci sda_delay = DIV_ROUND_UP(sda_delay, 5); 8808c2ecf20Sopenharmony_ci if (sda_delay > 3) 8818c2ecf20Sopenharmony_ci sda_delay = 3; 8828c2ecf20Sopenharmony_ci sda_delay |= S3C2410_IICLC_FILTER_ON; 8838c2ecf20Sopenharmony_ci } else 8848c2ecf20Sopenharmony_ci sda_delay = 0; 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay); 8878c2ecf20Sopenharmony_ci writel(sda_delay, i2c->regs + S3C2440_IICLC); 8888c2ecf20Sopenharmony_ci } 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci return 0; 8918c2ecf20Sopenharmony_ci} 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci#if defined(CONFIG_ARM_S3C24XX_CPUFREQ) 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci#define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition) 8968c2ecf20Sopenharmony_ci 8978c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb, 8988c2ecf20Sopenharmony_ci unsigned long val, void *data) 8998c2ecf20Sopenharmony_ci{ 9008c2ecf20Sopenharmony_ci struct s3c24xx_i2c *i2c = freq_to_i2c(nb); 9018c2ecf20Sopenharmony_ci unsigned int got; 9028c2ecf20Sopenharmony_ci int delta_f; 9038c2ecf20Sopenharmony_ci int ret; 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_ci delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; 9068c2ecf20Sopenharmony_ci 9078c2ecf20Sopenharmony_ci /* if we're post-change and the input clock has slowed down 9088c2ecf20Sopenharmony_ci * or at pre-change and the clock is about to speed up, then 9098c2ecf20Sopenharmony_ci * adjust our clock rate. <0 is slow, >0 speedup. 9108c2ecf20Sopenharmony_ci */ 9118c2ecf20Sopenharmony_ci 9128c2ecf20Sopenharmony_ci if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || 9138c2ecf20Sopenharmony_ci (val == CPUFREQ_PRECHANGE && delta_f > 0)) { 9148c2ecf20Sopenharmony_ci i2c_lock_bus(&i2c->adap, I2C_LOCK_ROOT_ADAPTER); 9158c2ecf20Sopenharmony_ci ret = s3c24xx_i2c_clockrate(i2c, &got); 9168c2ecf20Sopenharmony_ci i2c_unlock_bus(&i2c->adap, I2C_LOCK_ROOT_ADAPTER); 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci if (ret < 0) 9198c2ecf20Sopenharmony_ci dev_err(i2c->dev, "cannot find frequency (%d)\n", ret); 9208c2ecf20Sopenharmony_ci else 9218c2ecf20Sopenharmony_ci dev_info(i2c->dev, "setting freq %d\n", got); 9228c2ecf20Sopenharmony_ci } 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_ci return 0; 9258c2ecf20Sopenharmony_ci} 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_cistatic inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 9288c2ecf20Sopenharmony_ci{ 9298c2ecf20Sopenharmony_ci i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; 9308c2ecf20Sopenharmony_ci 9318c2ecf20Sopenharmony_ci return cpufreq_register_notifier(&i2c->freq_transition, 9328c2ecf20Sopenharmony_ci CPUFREQ_TRANSITION_NOTIFIER); 9338c2ecf20Sopenharmony_ci} 9348c2ecf20Sopenharmony_ci 9358c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 9368c2ecf20Sopenharmony_ci{ 9378c2ecf20Sopenharmony_ci cpufreq_unregister_notifier(&i2c->freq_transition, 9388c2ecf20Sopenharmony_ci CPUFREQ_TRANSITION_NOTIFIER); 9398c2ecf20Sopenharmony_ci} 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci#else 9428c2ecf20Sopenharmony_cistatic inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 9438c2ecf20Sopenharmony_ci{ 9448c2ecf20Sopenharmony_ci return 0; 9458c2ecf20Sopenharmony_ci} 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_cistatic inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 9488c2ecf20Sopenharmony_ci{ 9498c2ecf20Sopenharmony_ci} 9508c2ecf20Sopenharmony_ci#endif 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 9538c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 9548c2ecf20Sopenharmony_ci{ 9558c2ecf20Sopenharmony_ci int i; 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_ci if (i2c->quirks & QUIRK_NO_GPIO) 9588c2ecf20Sopenharmony_ci return 0; 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci for (i = 0; i < 2; i++) { 9618c2ecf20Sopenharmony_ci i2c->gpios[i] = devm_gpiod_get_index(i2c->dev, NULL, 9628c2ecf20Sopenharmony_ci i, GPIOD_ASIS); 9638c2ecf20Sopenharmony_ci if (IS_ERR(i2c->gpios[i])) { 9648c2ecf20Sopenharmony_ci dev_err(i2c->dev, "i2c gpio invalid at index %d\n", i); 9658c2ecf20Sopenharmony_ci return -EINVAL; 9668c2ecf20Sopenharmony_ci } 9678c2ecf20Sopenharmony_ci } 9688c2ecf20Sopenharmony_ci return 0; 9698c2ecf20Sopenharmony_ci} 9708c2ecf20Sopenharmony_ci 9718c2ecf20Sopenharmony_ci#else 9728c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 9738c2ecf20Sopenharmony_ci{ 9748c2ecf20Sopenharmony_ci return 0; 9758c2ecf20Sopenharmony_ci} 9768c2ecf20Sopenharmony_ci#endif 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ci/* 9798c2ecf20Sopenharmony_ci * initialise the controller, set the IO lines and frequency 9808c2ecf20Sopenharmony_ci */ 9818c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) 9828c2ecf20Sopenharmony_ci{ 9838c2ecf20Sopenharmony_ci struct s3c2410_platform_i2c *pdata; 9848c2ecf20Sopenharmony_ci unsigned int freq; 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ci /* get the plafrom data */ 9878c2ecf20Sopenharmony_ci 9888c2ecf20Sopenharmony_ci pdata = i2c->pdata; 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_ci /* write slave address */ 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_ci writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); 9938c2ecf20Sopenharmony_ci 9948c2ecf20Sopenharmony_ci dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci writel(0, i2c->regs + S3C2410_IICCON); 9978c2ecf20Sopenharmony_ci writel(0, i2c->regs + S3C2410_IICSTAT); 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci /* we need to work out the divisors for the clock... */ 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ci if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { 10028c2ecf20Sopenharmony_ci dev_err(i2c->dev, "cannot meet bus frequency required\n"); 10038c2ecf20Sopenharmony_ci return -EINVAL; 10048c2ecf20Sopenharmony_ci } 10058c2ecf20Sopenharmony_ci 10068c2ecf20Sopenharmony_ci /* todo - check that the i2c lines aren't being dragged anywhere */ 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_ci dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); 10098c2ecf20Sopenharmony_ci dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02x\n", 10108c2ecf20Sopenharmony_ci readl(i2c->regs + S3C2410_IICCON)); 10118c2ecf20Sopenharmony_ci 10128c2ecf20Sopenharmony_ci return 0; 10138c2ecf20Sopenharmony_ci} 10148c2ecf20Sopenharmony_ci 10158c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 10168c2ecf20Sopenharmony_ci/* 10178c2ecf20Sopenharmony_ci * Parse the device tree node and retreive the platform data. 10188c2ecf20Sopenharmony_ci */ 10198c2ecf20Sopenharmony_cistatic void 10208c2ecf20Sopenharmony_cis3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) 10218c2ecf20Sopenharmony_ci{ 10228c2ecf20Sopenharmony_ci struct s3c2410_platform_i2c *pdata = i2c->pdata; 10238c2ecf20Sopenharmony_ci int id; 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ci if (!np) 10268c2ecf20Sopenharmony_ci return; 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ci pdata->bus_num = -1; /* i2c bus number is dynamically assigned */ 10298c2ecf20Sopenharmony_ci of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay); 10308c2ecf20Sopenharmony_ci of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr); 10318c2ecf20Sopenharmony_ci of_property_read_u32(np, "samsung,i2c-max-bus-freq", 10328c2ecf20Sopenharmony_ci (u32 *)&pdata->frequency); 10338c2ecf20Sopenharmony_ci /* 10348c2ecf20Sopenharmony_ci * Exynos5's legacy i2c controller and new high speed i2c 10358c2ecf20Sopenharmony_ci * controller have muxed interrupt sources. By default the 10368c2ecf20Sopenharmony_ci * interrupts for 4-channel HS-I2C controller are enabled. 10378c2ecf20Sopenharmony_ci * If nodes for first four channels of legacy i2c controller 10388c2ecf20Sopenharmony_ci * are available then re-configure the interrupts via the 10398c2ecf20Sopenharmony_ci * system register. 10408c2ecf20Sopenharmony_ci */ 10418c2ecf20Sopenharmony_ci id = of_alias_get_id(np, "i2c"); 10428c2ecf20Sopenharmony_ci i2c->sysreg = syscon_regmap_lookup_by_phandle(np, 10438c2ecf20Sopenharmony_ci "samsung,sysreg-phandle"); 10448c2ecf20Sopenharmony_ci if (IS_ERR(i2c->sysreg)) 10458c2ecf20Sopenharmony_ci return; 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_ci regmap_update_bits(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, BIT(id), 0); 10488c2ecf20Sopenharmony_ci} 10498c2ecf20Sopenharmony_ci#else 10508c2ecf20Sopenharmony_cistatic void 10518c2ecf20Sopenharmony_cis3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) { } 10528c2ecf20Sopenharmony_ci#endif 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_probe(struct platform_device *pdev) 10558c2ecf20Sopenharmony_ci{ 10568c2ecf20Sopenharmony_ci struct s3c24xx_i2c *i2c; 10578c2ecf20Sopenharmony_ci struct s3c2410_platform_i2c *pdata = NULL; 10588c2ecf20Sopenharmony_ci struct resource *res; 10598c2ecf20Sopenharmony_ci int ret; 10608c2ecf20Sopenharmony_ci 10618c2ecf20Sopenharmony_ci if (!pdev->dev.of_node) { 10628c2ecf20Sopenharmony_ci pdata = dev_get_platdata(&pdev->dev); 10638c2ecf20Sopenharmony_ci if (!pdata) { 10648c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "no platform data\n"); 10658c2ecf20Sopenharmony_ci return -EINVAL; 10668c2ecf20Sopenharmony_ci } 10678c2ecf20Sopenharmony_ci } 10688c2ecf20Sopenharmony_ci 10698c2ecf20Sopenharmony_ci i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL); 10708c2ecf20Sopenharmony_ci if (!i2c) 10718c2ecf20Sopenharmony_ci return -ENOMEM; 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 10748c2ecf20Sopenharmony_ci if (!i2c->pdata) 10758c2ecf20Sopenharmony_ci return -ENOMEM; 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci i2c->quirks = s3c24xx_get_device_quirks(pdev); 10788c2ecf20Sopenharmony_ci i2c->sysreg = ERR_PTR(-ENOENT); 10798c2ecf20Sopenharmony_ci if (pdata) 10808c2ecf20Sopenharmony_ci memcpy(i2c->pdata, pdata, sizeof(*pdata)); 10818c2ecf20Sopenharmony_ci else 10828c2ecf20Sopenharmony_ci s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c); 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_ci strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); 10858c2ecf20Sopenharmony_ci i2c->adap.owner = THIS_MODULE; 10868c2ecf20Sopenharmony_ci i2c->adap.algo = &s3c24xx_i2c_algorithm; 10878c2ecf20Sopenharmony_ci i2c->adap.retries = 2; 10888c2ecf20Sopenharmony_ci i2c->adap.class = I2C_CLASS_DEPRECATED; 10898c2ecf20Sopenharmony_ci i2c->tx_setup = 50; 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci init_waitqueue_head(&i2c->wait); 10928c2ecf20Sopenharmony_ci 10938c2ecf20Sopenharmony_ci /* find the clock and enable it */ 10948c2ecf20Sopenharmony_ci i2c->dev = &pdev->dev; 10958c2ecf20Sopenharmony_ci i2c->clk = devm_clk_get(&pdev->dev, "i2c"); 10968c2ecf20Sopenharmony_ci if (IS_ERR(i2c->clk)) { 10978c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "cannot get clock\n"); 10988c2ecf20Sopenharmony_ci return -ENOENT; 10998c2ecf20Sopenharmony_ci } 11008c2ecf20Sopenharmony_ci 11018c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); 11028c2ecf20Sopenharmony_ci 11038c2ecf20Sopenharmony_ci /* map the registers */ 11048c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 11058c2ecf20Sopenharmony_ci i2c->regs = devm_ioremap_resource(&pdev->dev, res); 11068c2ecf20Sopenharmony_ci 11078c2ecf20Sopenharmony_ci if (IS_ERR(i2c->regs)) 11088c2ecf20Sopenharmony_ci return PTR_ERR(i2c->regs); 11098c2ecf20Sopenharmony_ci 11108c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "registers %p (%p)\n", 11118c2ecf20Sopenharmony_ci i2c->regs, res); 11128c2ecf20Sopenharmony_ci 11138c2ecf20Sopenharmony_ci /* setup info block for the i2c core */ 11148c2ecf20Sopenharmony_ci i2c->adap.algo_data = i2c; 11158c2ecf20Sopenharmony_ci i2c->adap.dev.parent = &pdev->dev; 11168c2ecf20Sopenharmony_ci i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev); 11178c2ecf20Sopenharmony_ci 11188c2ecf20Sopenharmony_ci /* inititalise the i2c gpio lines */ 11198c2ecf20Sopenharmony_ci if (i2c->pdata->cfg_gpio) 11208c2ecf20Sopenharmony_ci i2c->pdata->cfg_gpio(to_platform_device(i2c->dev)); 11218c2ecf20Sopenharmony_ci else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) 11228c2ecf20Sopenharmony_ci return -EINVAL; 11238c2ecf20Sopenharmony_ci 11248c2ecf20Sopenharmony_ci /* initialise the i2c controller */ 11258c2ecf20Sopenharmony_ci ret = clk_prepare_enable(i2c->clk); 11268c2ecf20Sopenharmony_ci if (ret) { 11278c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "I2C clock enable failed\n"); 11288c2ecf20Sopenharmony_ci return ret; 11298c2ecf20Sopenharmony_ci } 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci ret = s3c24xx_i2c_init(i2c); 11328c2ecf20Sopenharmony_ci clk_disable(i2c->clk); 11338c2ecf20Sopenharmony_ci if (ret != 0) { 11348c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "I2C controller init failed\n"); 11358c2ecf20Sopenharmony_ci clk_unprepare(i2c->clk); 11368c2ecf20Sopenharmony_ci return ret; 11378c2ecf20Sopenharmony_ci } 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_ci /* 11408c2ecf20Sopenharmony_ci * find the IRQ for this unit (note, this relies on the init call to 11418c2ecf20Sopenharmony_ci * ensure no current IRQs pending 11428c2ecf20Sopenharmony_ci */ 11438c2ecf20Sopenharmony_ci if (!(i2c->quirks & QUIRK_POLL)) { 11448c2ecf20Sopenharmony_ci i2c->irq = ret = platform_get_irq(pdev, 0); 11458c2ecf20Sopenharmony_ci if (ret < 0) { 11468c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "cannot find IRQ\n"); 11478c2ecf20Sopenharmony_ci clk_unprepare(i2c->clk); 11488c2ecf20Sopenharmony_ci return ret; 11498c2ecf20Sopenharmony_ci } 11508c2ecf20Sopenharmony_ci 11518c2ecf20Sopenharmony_ci ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 11528c2ecf20Sopenharmony_ci 0, dev_name(&pdev->dev), i2c); 11538c2ecf20Sopenharmony_ci if (ret != 0) { 11548c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); 11558c2ecf20Sopenharmony_ci clk_unprepare(i2c->clk); 11568c2ecf20Sopenharmony_ci return ret; 11578c2ecf20Sopenharmony_ci } 11588c2ecf20Sopenharmony_ci } 11598c2ecf20Sopenharmony_ci 11608c2ecf20Sopenharmony_ci ret = s3c24xx_i2c_register_cpufreq(i2c); 11618c2ecf20Sopenharmony_ci if (ret < 0) { 11628c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); 11638c2ecf20Sopenharmony_ci clk_unprepare(i2c->clk); 11648c2ecf20Sopenharmony_ci return ret; 11658c2ecf20Sopenharmony_ci } 11668c2ecf20Sopenharmony_ci 11678c2ecf20Sopenharmony_ci /* 11688c2ecf20Sopenharmony_ci * Note, previous versions of the driver used i2c_add_adapter() 11698c2ecf20Sopenharmony_ci * to add the bus at any number. We now pass the bus number via 11708c2ecf20Sopenharmony_ci * the platform data, so if unset it will now default to always 11718c2ecf20Sopenharmony_ci * being bus 0. 11728c2ecf20Sopenharmony_ci */ 11738c2ecf20Sopenharmony_ci i2c->adap.nr = i2c->pdata->bus_num; 11748c2ecf20Sopenharmony_ci i2c->adap.dev.of_node = pdev->dev.of_node; 11758c2ecf20Sopenharmony_ci 11768c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, i2c); 11778c2ecf20Sopenharmony_ci 11788c2ecf20Sopenharmony_ci pm_runtime_enable(&pdev->dev); 11798c2ecf20Sopenharmony_ci 11808c2ecf20Sopenharmony_ci ret = i2c_add_numbered_adapter(&i2c->adap); 11818c2ecf20Sopenharmony_ci if (ret < 0) { 11828c2ecf20Sopenharmony_ci pm_runtime_disable(&pdev->dev); 11838c2ecf20Sopenharmony_ci s3c24xx_i2c_deregister_cpufreq(i2c); 11848c2ecf20Sopenharmony_ci clk_unprepare(i2c->clk); 11858c2ecf20Sopenharmony_ci return ret; 11868c2ecf20Sopenharmony_ci } 11878c2ecf20Sopenharmony_ci 11888c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev)); 11898c2ecf20Sopenharmony_ci return 0; 11908c2ecf20Sopenharmony_ci} 11918c2ecf20Sopenharmony_ci 11928c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_remove(struct platform_device *pdev) 11938c2ecf20Sopenharmony_ci{ 11948c2ecf20Sopenharmony_ci struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 11958c2ecf20Sopenharmony_ci 11968c2ecf20Sopenharmony_ci clk_unprepare(i2c->clk); 11978c2ecf20Sopenharmony_ci 11988c2ecf20Sopenharmony_ci pm_runtime_disable(&pdev->dev); 11998c2ecf20Sopenharmony_ci 12008c2ecf20Sopenharmony_ci s3c24xx_i2c_deregister_cpufreq(i2c); 12018c2ecf20Sopenharmony_ci 12028c2ecf20Sopenharmony_ci i2c_del_adapter(&i2c->adap); 12038c2ecf20Sopenharmony_ci 12048c2ecf20Sopenharmony_ci return 0; 12058c2ecf20Sopenharmony_ci} 12068c2ecf20Sopenharmony_ci 12078c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 12088c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_suspend_noirq(struct device *dev) 12098c2ecf20Sopenharmony_ci{ 12108c2ecf20Sopenharmony_ci struct s3c24xx_i2c *i2c = dev_get_drvdata(dev); 12118c2ecf20Sopenharmony_ci 12128c2ecf20Sopenharmony_ci i2c_mark_adapter_suspended(&i2c->adap); 12138c2ecf20Sopenharmony_ci 12148c2ecf20Sopenharmony_ci if (!IS_ERR(i2c->sysreg)) 12158c2ecf20Sopenharmony_ci regmap_read(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, &i2c->sys_i2c_cfg); 12168c2ecf20Sopenharmony_ci 12178c2ecf20Sopenharmony_ci return 0; 12188c2ecf20Sopenharmony_ci} 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_cistatic int s3c24xx_i2c_resume_noirq(struct device *dev) 12218c2ecf20Sopenharmony_ci{ 12228c2ecf20Sopenharmony_ci struct s3c24xx_i2c *i2c = dev_get_drvdata(dev); 12238c2ecf20Sopenharmony_ci int ret; 12248c2ecf20Sopenharmony_ci 12258c2ecf20Sopenharmony_ci if (!IS_ERR(i2c->sysreg)) 12268c2ecf20Sopenharmony_ci regmap_write(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, i2c->sys_i2c_cfg); 12278c2ecf20Sopenharmony_ci 12288c2ecf20Sopenharmony_ci ret = clk_enable(i2c->clk); 12298c2ecf20Sopenharmony_ci if (ret) 12308c2ecf20Sopenharmony_ci return ret; 12318c2ecf20Sopenharmony_ci s3c24xx_i2c_init(i2c); 12328c2ecf20Sopenharmony_ci clk_disable(i2c->clk); 12338c2ecf20Sopenharmony_ci i2c_mark_adapter_resumed(&i2c->adap); 12348c2ecf20Sopenharmony_ci 12358c2ecf20Sopenharmony_ci return 0; 12368c2ecf20Sopenharmony_ci} 12378c2ecf20Sopenharmony_ci#endif 12388c2ecf20Sopenharmony_ci 12398c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 12408c2ecf20Sopenharmony_cistatic const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = { 12418c2ecf20Sopenharmony_ci SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(s3c24xx_i2c_suspend_noirq, 12428c2ecf20Sopenharmony_ci s3c24xx_i2c_resume_noirq) 12438c2ecf20Sopenharmony_ci}; 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_ci#define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops) 12468c2ecf20Sopenharmony_ci#else 12478c2ecf20Sopenharmony_ci#define S3C24XX_DEV_PM_OPS NULL 12488c2ecf20Sopenharmony_ci#endif 12498c2ecf20Sopenharmony_ci 12508c2ecf20Sopenharmony_cistatic struct platform_driver s3c24xx_i2c_driver = { 12518c2ecf20Sopenharmony_ci .probe = s3c24xx_i2c_probe, 12528c2ecf20Sopenharmony_ci .remove = s3c24xx_i2c_remove, 12538c2ecf20Sopenharmony_ci .id_table = s3c24xx_driver_ids, 12548c2ecf20Sopenharmony_ci .driver = { 12558c2ecf20Sopenharmony_ci .name = "s3c-i2c", 12568c2ecf20Sopenharmony_ci .pm = S3C24XX_DEV_PM_OPS, 12578c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(s3c24xx_i2c_match), 12588c2ecf20Sopenharmony_ci }, 12598c2ecf20Sopenharmony_ci}; 12608c2ecf20Sopenharmony_ci 12618c2ecf20Sopenharmony_cistatic int __init i2c_adap_s3c_init(void) 12628c2ecf20Sopenharmony_ci{ 12638c2ecf20Sopenharmony_ci return platform_driver_register(&s3c24xx_i2c_driver); 12648c2ecf20Sopenharmony_ci} 12658c2ecf20Sopenharmony_cisubsys_initcall(i2c_adap_s3c_init); 12668c2ecf20Sopenharmony_ci 12678c2ecf20Sopenharmony_cistatic void __exit i2c_adap_s3c_exit(void) 12688c2ecf20Sopenharmony_ci{ 12698c2ecf20Sopenharmony_ci platform_driver_unregister(&s3c24xx_i2c_driver); 12708c2ecf20Sopenharmony_ci} 12718c2ecf20Sopenharmony_cimodule_exit(i2c_adap_s3c_exit); 12728c2ecf20Sopenharmony_ci 12738c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("S3C24XX I2C Bus driver"); 12748c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); 12758c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1276