12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 215ef2775SWei Yan /* 315ef2775SWei Yan * Copyright (c) 2014 Linaro Ltd. 415ef2775SWei Yan * Copyright (c) 2014 Hisilicon Limited. 515ef2775SWei Yan * 615ef2775SWei Yan * Now only support 7 bit address. 715ef2775SWei Yan */ 815ef2775SWei Yan 915ef2775SWei Yan #include <linux/clk.h> 1015ef2775SWei Yan #include <linux/delay.h> 1115ef2775SWei Yan #include <linux/i2c.h> 1215ef2775SWei Yan #include <linux/io.h> 1315ef2775SWei Yan #include <linux/interrupt.h> 1415ef2775SWei Yan #include <linux/module.h> 1515ef2775SWei Yan #include <linux/of.h> 1615ef2775SWei Yan #include <linux/platform_device.h> 1715ef2775SWei Yan #include <linux/pm_runtime.h> 1815ef2775SWei Yan 1915ef2775SWei Yan /* Register Map */ 2015ef2775SWei Yan #define HIX5I2C_CTRL 0x00 2115ef2775SWei Yan #define HIX5I2C_COM 0x04 2215ef2775SWei Yan #define HIX5I2C_ICR 0x08 2315ef2775SWei Yan #define HIX5I2C_SR 0x0c 2415ef2775SWei Yan #define HIX5I2C_SCL_H 0x10 2515ef2775SWei Yan #define HIX5I2C_SCL_L 0x14 2615ef2775SWei Yan #define HIX5I2C_TXR 0x18 2715ef2775SWei Yan #define HIX5I2C_RXR 0x1c 2815ef2775SWei Yan 2915ef2775SWei Yan /* I2C_CTRL_REG */ 3015ef2775SWei Yan #define I2C_ENABLE BIT(8) 3115ef2775SWei Yan #define I2C_UNMASK_TOTAL BIT(7) 3215ef2775SWei Yan #define I2C_UNMASK_START BIT(6) 3315ef2775SWei Yan #define I2C_UNMASK_END BIT(5) 3415ef2775SWei Yan #define I2C_UNMASK_SEND BIT(4) 3515ef2775SWei Yan #define I2C_UNMASK_RECEIVE BIT(3) 3615ef2775SWei Yan #define I2C_UNMASK_ACK BIT(2) 3715ef2775SWei Yan #define I2C_UNMASK_ARBITRATE BIT(1) 3815ef2775SWei Yan #define I2C_UNMASK_OVER BIT(0) 3915ef2775SWei Yan #define I2C_UNMASK_ALL (I2C_UNMASK_ACK | I2C_UNMASK_OVER) 4015ef2775SWei Yan 4115ef2775SWei Yan /* I2C_COM_REG */ 4215ef2775SWei Yan #define I2C_NO_ACK BIT(4) 4315ef2775SWei Yan #define I2C_START BIT(3) 4415ef2775SWei Yan #define I2C_READ BIT(2) 4515ef2775SWei Yan #define I2C_WRITE BIT(1) 4615ef2775SWei Yan #define I2C_STOP BIT(0) 4715ef2775SWei Yan 4815ef2775SWei Yan /* I2C_ICR_REG */ 4915ef2775SWei Yan #define I2C_CLEAR_START BIT(6) 5015ef2775SWei Yan #define I2C_CLEAR_END BIT(5) 5115ef2775SWei Yan #define I2C_CLEAR_SEND BIT(4) 5215ef2775SWei Yan #define I2C_CLEAR_RECEIVE BIT(3) 5315ef2775SWei Yan #define I2C_CLEAR_ACK BIT(2) 5415ef2775SWei Yan #define I2C_CLEAR_ARBITRATE BIT(1) 5515ef2775SWei Yan #define I2C_CLEAR_OVER BIT(0) 5615ef2775SWei Yan #define I2C_CLEAR_ALL (I2C_CLEAR_START | I2C_CLEAR_END | \ 5715ef2775SWei Yan I2C_CLEAR_SEND | I2C_CLEAR_RECEIVE | \ 5815ef2775SWei Yan I2C_CLEAR_ACK | I2C_CLEAR_ARBITRATE | \ 5915ef2775SWei Yan I2C_CLEAR_OVER) 6015ef2775SWei Yan 6115ef2775SWei Yan /* I2C_SR_REG */ 6215ef2775SWei Yan #define I2C_BUSY BIT(7) 6315ef2775SWei Yan #define I2C_START_INTR BIT(6) 6415ef2775SWei Yan #define I2C_END_INTR BIT(5) 6515ef2775SWei Yan #define I2C_SEND_INTR BIT(4) 6615ef2775SWei Yan #define I2C_RECEIVE_INTR BIT(3) 6715ef2775SWei Yan #define I2C_ACK_INTR BIT(2) 6815ef2775SWei Yan #define I2C_ARBITRATE_INTR BIT(1) 6915ef2775SWei Yan #define I2C_OVER_INTR BIT(0) 7015ef2775SWei Yan 7115ef2775SWei Yan #define HIX5I2C_MAX_FREQ 400000 /* 400k */ 7215ef2775SWei Yan 7315ef2775SWei Yan enum hix5hd2_i2c_state { 7415ef2775SWei Yan HIX5I2C_STAT_RW_ERR = -1, 7515ef2775SWei Yan HIX5I2C_STAT_INIT, 7615ef2775SWei Yan HIX5I2C_STAT_RW, 7715ef2775SWei Yan HIX5I2C_STAT_SND_STOP, 7815ef2775SWei Yan HIX5I2C_STAT_RW_SUCCESS, 7915ef2775SWei Yan }; 8015ef2775SWei Yan 8115ef2775SWei Yan struct hix5hd2_i2c_priv { 8215ef2775SWei Yan struct i2c_adapter adap; 8315ef2775SWei Yan struct i2c_msg *msg; 8415ef2775SWei Yan struct completion msg_complete; 8515ef2775SWei Yan unsigned int msg_idx; 8615ef2775SWei Yan unsigned int msg_len; 8715ef2775SWei Yan int stop; 8815ef2775SWei Yan void __iomem *regs; 8915ef2775SWei Yan struct clk *clk; 9015ef2775SWei Yan struct device *dev; 9115ef2775SWei Yan spinlock_t lock; /* IRQ synchronization */ 9215ef2775SWei Yan int err; 9315ef2775SWei Yan unsigned int freq; 9415ef2775SWei Yan enum hix5hd2_i2c_state state; 9515ef2775SWei Yan }; 9615ef2775SWei Yan 9715ef2775SWei Yan static u32 hix5hd2_i2c_clr_pend_irq(struct hix5hd2_i2c_priv *priv) 9815ef2775SWei Yan { 9915ef2775SWei Yan u32 val = readl_relaxed(priv->regs + HIX5I2C_SR); 10015ef2775SWei Yan 10115ef2775SWei Yan writel_relaxed(val, priv->regs + HIX5I2C_ICR); 10215ef2775SWei Yan 10315ef2775SWei Yan return val; 10415ef2775SWei Yan } 10515ef2775SWei Yan 10615ef2775SWei Yan static void hix5hd2_i2c_clr_all_irq(struct hix5hd2_i2c_priv *priv) 10715ef2775SWei Yan { 10815ef2775SWei Yan writel_relaxed(I2C_CLEAR_ALL, priv->regs + HIX5I2C_ICR); 10915ef2775SWei Yan } 11015ef2775SWei Yan 11115ef2775SWei Yan static void hix5hd2_i2c_disable_irq(struct hix5hd2_i2c_priv *priv) 11215ef2775SWei Yan { 11315ef2775SWei Yan writel_relaxed(0, priv->regs + HIX5I2C_CTRL); 11415ef2775SWei Yan } 11515ef2775SWei Yan 11615ef2775SWei Yan static void hix5hd2_i2c_enable_irq(struct hix5hd2_i2c_priv *priv) 11715ef2775SWei Yan { 11815ef2775SWei Yan writel_relaxed(I2C_ENABLE | I2C_UNMASK_TOTAL | I2C_UNMASK_ALL, 11915ef2775SWei Yan priv->regs + HIX5I2C_CTRL); 12015ef2775SWei Yan } 12115ef2775SWei Yan 12215ef2775SWei Yan static void hix5hd2_i2c_drv_setrate(struct hix5hd2_i2c_priv *priv) 12315ef2775SWei Yan { 12415ef2775SWei Yan u32 rate, val; 12515ef2775SWei Yan u32 scl, sysclock; 12615ef2775SWei Yan 12715ef2775SWei Yan /* close all i2c interrupt */ 12815ef2775SWei Yan val = readl_relaxed(priv->regs + HIX5I2C_CTRL); 12915ef2775SWei Yan writel_relaxed(val & (~I2C_UNMASK_TOTAL), priv->regs + HIX5I2C_CTRL); 13015ef2775SWei Yan 13115ef2775SWei Yan rate = priv->freq; 13215ef2775SWei Yan sysclock = clk_get_rate(priv->clk); 13315ef2775SWei Yan scl = (sysclock / (rate * 2)) / 2 - 1; 13415ef2775SWei Yan writel_relaxed(scl, priv->regs + HIX5I2C_SCL_H); 13515ef2775SWei Yan writel_relaxed(scl, priv->regs + HIX5I2C_SCL_L); 13615ef2775SWei Yan 13715ef2775SWei Yan /* restore original interrupt*/ 13815ef2775SWei Yan writel_relaxed(val, priv->regs + HIX5I2C_CTRL); 13915ef2775SWei Yan 14015ef2775SWei Yan dev_dbg(priv->dev, "%s: sysclock=%d, rate=%d, scl=%d\n", 14115ef2775SWei Yan __func__, sysclock, rate, scl); 14215ef2775SWei Yan } 14315ef2775SWei Yan 14415ef2775SWei Yan static void hix5hd2_i2c_init(struct hix5hd2_i2c_priv *priv) 14515ef2775SWei Yan { 14615ef2775SWei Yan hix5hd2_i2c_disable_irq(priv); 14715ef2775SWei Yan hix5hd2_i2c_drv_setrate(priv); 14815ef2775SWei Yan hix5hd2_i2c_clr_all_irq(priv); 14915ef2775SWei Yan hix5hd2_i2c_enable_irq(priv); 15015ef2775SWei Yan } 15115ef2775SWei Yan 15215ef2775SWei Yan static void hix5hd2_i2c_reset(struct hix5hd2_i2c_priv *priv) 15315ef2775SWei Yan { 15415ef2775SWei Yan clk_disable_unprepare(priv->clk); 15515ef2775SWei Yan msleep(20); 15615ef2775SWei Yan clk_prepare_enable(priv->clk); 15715ef2775SWei Yan hix5hd2_i2c_init(priv); 15815ef2775SWei Yan } 15915ef2775SWei Yan 16015ef2775SWei Yan static int hix5hd2_i2c_wait_bus_idle(struct hix5hd2_i2c_priv *priv) 16115ef2775SWei Yan { 16215ef2775SWei Yan unsigned long stop_time; 16315ef2775SWei Yan u32 int_status; 16415ef2775SWei Yan 16515ef2775SWei Yan /* wait for 100 milli seconds for the bus to be idle */ 16615ef2775SWei Yan stop_time = jiffies + msecs_to_jiffies(100); 16715ef2775SWei Yan do { 16815ef2775SWei Yan int_status = hix5hd2_i2c_clr_pend_irq(priv); 16915ef2775SWei Yan if (!(int_status & I2C_BUSY)) 17015ef2775SWei Yan return 0; 17115ef2775SWei Yan 17215ef2775SWei Yan usleep_range(50, 200); 17315ef2775SWei Yan } while (time_before(jiffies, stop_time)); 17415ef2775SWei Yan 17515ef2775SWei Yan return -EBUSY; 17615ef2775SWei Yan } 17715ef2775SWei Yan 17815ef2775SWei Yan static void hix5hd2_rw_over(struct hix5hd2_i2c_priv *priv) 17915ef2775SWei Yan { 18015ef2775SWei Yan if (priv->state == HIX5I2C_STAT_SND_STOP) 18115ef2775SWei Yan dev_dbg(priv->dev, "%s: rw and send stop over\n", __func__); 18215ef2775SWei Yan else 18315ef2775SWei Yan dev_dbg(priv->dev, "%s: have not data to send\n", __func__); 18415ef2775SWei Yan 18515ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_SUCCESS; 18615ef2775SWei Yan priv->err = 0; 18715ef2775SWei Yan } 18815ef2775SWei Yan 18915ef2775SWei Yan static void hix5hd2_rw_handle_stop(struct hix5hd2_i2c_priv *priv) 19015ef2775SWei Yan { 19115ef2775SWei Yan if (priv->stop) { 19215ef2775SWei Yan priv->state = HIX5I2C_STAT_SND_STOP; 19315ef2775SWei Yan writel_relaxed(I2C_STOP, priv->regs + HIX5I2C_COM); 19415ef2775SWei Yan } else { 19515ef2775SWei Yan hix5hd2_rw_over(priv); 19615ef2775SWei Yan } 19715ef2775SWei Yan } 19815ef2775SWei Yan 19915ef2775SWei Yan static void hix5hd2_read_handle(struct hix5hd2_i2c_priv *priv) 20015ef2775SWei Yan { 20115ef2775SWei Yan if (priv->msg_len == 1) { 20215ef2775SWei Yan /* the last byte don't need send ACK */ 20315ef2775SWei Yan writel_relaxed(I2C_READ | I2C_NO_ACK, priv->regs + HIX5I2C_COM); 20415ef2775SWei Yan } else if (priv->msg_len > 1) { 20515ef2775SWei Yan /* if i2c master receive data will send ACK */ 20615ef2775SWei Yan writel_relaxed(I2C_READ, priv->regs + HIX5I2C_COM); 20715ef2775SWei Yan } else { 20815ef2775SWei Yan hix5hd2_rw_handle_stop(priv); 20915ef2775SWei Yan } 21015ef2775SWei Yan } 21115ef2775SWei Yan 21215ef2775SWei Yan static void hix5hd2_write_handle(struct hix5hd2_i2c_priv *priv) 21315ef2775SWei Yan { 21415ef2775SWei Yan u8 data; 21515ef2775SWei Yan 21615ef2775SWei Yan if (priv->msg_len > 0) { 21715ef2775SWei Yan data = priv->msg->buf[priv->msg_idx++]; 21815ef2775SWei Yan writel_relaxed(data, priv->regs + HIX5I2C_TXR); 21915ef2775SWei Yan writel_relaxed(I2C_WRITE, priv->regs + HIX5I2C_COM); 22015ef2775SWei Yan } else { 22115ef2775SWei Yan hix5hd2_rw_handle_stop(priv); 22215ef2775SWei Yan } 22315ef2775SWei Yan } 22415ef2775SWei Yan 22515ef2775SWei Yan static int hix5hd2_rw_preprocess(struct hix5hd2_i2c_priv *priv) 22615ef2775SWei Yan { 22715ef2775SWei Yan u8 data; 22815ef2775SWei Yan 22915ef2775SWei Yan if (priv->state == HIX5I2C_STAT_INIT) { 23015ef2775SWei Yan priv->state = HIX5I2C_STAT_RW; 23115ef2775SWei Yan } else if (priv->state == HIX5I2C_STAT_RW) { 23215ef2775SWei Yan if (priv->msg->flags & I2C_M_RD) { 23315ef2775SWei Yan data = readl_relaxed(priv->regs + HIX5I2C_RXR); 23415ef2775SWei Yan priv->msg->buf[priv->msg_idx++] = data; 23515ef2775SWei Yan } 23615ef2775SWei Yan priv->msg_len--; 23715ef2775SWei Yan } else { 23815ef2775SWei Yan dev_dbg(priv->dev, "%s: error: priv->state = %d, msg_len = %d\n", 23915ef2775SWei Yan __func__, priv->state, priv->msg_len); 24015ef2775SWei Yan return -EAGAIN; 24115ef2775SWei Yan } 24215ef2775SWei Yan return 0; 24315ef2775SWei Yan } 24415ef2775SWei Yan 24515ef2775SWei Yan static irqreturn_t hix5hd2_i2c_irq(int irqno, void *dev_id) 24615ef2775SWei Yan { 24715ef2775SWei Yan struct hix5hd2_i2c_priv *priv = dev_id; 24815ef2775SWei Yan u32 int_status; 24915ef2775SWei Yan int ret; 25015ef2775SWei Yan 25115ef2775SWei Yan spin_lock(&priv->lock); 25215ef2775SWei Yan 25315ef2775SWei Yan int_status = hix5hd2_i2c_clr_pend_irq(priv); 25415ef2775SWei Yan 25515ef2775SWei Yan /* handle error */ 25615ef2775SWei Yan if (int_status & I2C_ARBITRATE_INTR) { 25715ef2775SWei Yan /* bus error */ 25815ef2775SWei Yan dev_dbg(priv->dev, "ARB bus loss\n"); 25915ef2775SWei Yan priv->err = -EAGAIN; 26015ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_ERR; 26115ef2775SWei Yan goto stop; 26215ef2775SWei Yan } else if (int_status & I2C_ACK_INTR) { 26315ef2775SWei Yan /* ack error */ 26415ef2775SWei Yan dev_dbg(priv->dev, "No ACK from device\n"); 26515ef2775SWei Yan priv->err = -ENXIO; 26615ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_ERR; 26715ef2775SWei Yan goto stop; 26815ef2775SWei Yan } 26915ef2775SWei Yan 27015ef2775SWei Yan if (int_status & I2C_OVER_INTR) { 27115ef2775SWei Yan if (priv->msg_len > 0) { 27215ef2775SWei Yan ret = hix5hd2_rw_preprocess(priv); 27315ef2775SWei Yan if (ret) { 27415ef2775SWei Yan priv->err = ret; 27515ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_ERR; 27615ef2775SWei Yan goto stop; 27715ef2775SWei Yan } 27815ef2775SWei Yan if (priv->msg->flags & I2C_M_RD) 27915ef2775SWei Yan hix5hd2_read_handle(priv); 28015ef2775SWei Yan else 28115ef2775SWei Yan hix5hd2_write_handle(priv); 28215ef2775SWei Yan } else { 28315ef2775SWei Yan hix5hd2_rw_over(priv); 28415ef2775SWei Yan } 28515ef2775SWei Yan } 28615ef2775SWei Yan 28715ef2775SWei Yan stop: 28815ef2775SWei Yan if ((priv->state == HIX5I2C_STAT_RW_SUCCESS && 28915ef2775SWei Yan priv->msg->len == priv->msg_idx) || 29015ef2775SWei Yan (priv->state == HIX5I2C_STAT_RW_ERR)) { 29115ef2775SWei Yan hix5hd2_i2c_disable_irq(priv); 29215ef2775SWei Yan hix5hd2_i2c_clr_pend_irq(priv); 29315ef2775SWei Yan complete(&priv->msg_complete); 29415ef2775SWei Yan } 29515ef2775SWei Yan 29615ef2775SWei Yan spin_unlock(&priv->lock); 29715ef2775SWei Yan 29815ef2775SWei Yan return IRQ_HANDLED; 29915ef2775SWei Yan } 30015ef2775SWei Yan 30115ef2775SWei Yan static void hix5hd2_i2c_message_start(struct hix5hd2_i2c_priv *priv, int stop) 30215ef2775SWei Yan { 30315ef2775SWei Yan unsigned long flags; 30415ef2775SWei Yan 30515ef2775SWei Yan spin_lock_irqsave(&priv->lock, flags); 30615ef2775SWei Yan hix5hd2_i2c_clr_all_irq(priv); 30715ef2775SWei Yan hix5hd2_i2c_enable_irq(priv); 30815ef2775SWei Yan 30930a64757SPeter Rosin writel_relaxed(i2c_8bit_addr_from_msg(priv->msg), 31015ef2775SWei Yan priv->regs + HIX5I2C_TXR); 31115ef2775SWei Yan 31215ef2775SWei Yan writel_relaxed(I2C_WRITE | I2C_START, priv->regs + HIX5I2C_COM); 31315ef2775SWei Yan spin_unlock_irqrestore(&priv->lock, flags); 31415ef2775SWei Yan } 31515ef2775SWei Yan 31615ef2775SWei Yan static int hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv *priv, 31715ef2775SWei Yan struct i2c_msg *msgs, int stop) 31815ef2775SWei Yan { 31915ef2775SWei Yan unsigned long timeout; 32015ef2775SWei Yan int ret; 32115ef2775SWei Yan 32215ef2775SWei Yan priv->msg = msgs; 32315ef2775SWei Yan priv->msg_idx = 0; 32415ef2775SWei Yan priv->msg_len = priv->msg->len; 32515ef2775SWei Yan priv->stop = stop; 32615ef2775SWei Yan priv->err = 0; 32715ef2775SWei Yan priv->state = HIX5I2C_STAT_INIT; 32815ef2775SWei Yan 32915ef2775SWei Yan reinit_completion(&priv->msg_complete); 33015ef2775SWei Yan hix5hd2_i2c_message_start(priv, stop); 33115ef2775SWei Yan 33215ef2775SWei Yan timeout = wait_for_completion_timeout(&priv->msg_complete, 33315ef2775SWei Yan priv->adap.timeout); 33415ef2775SWei Yan if (timeout == 0) { 33515ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_ERR; 33615ef2775SWei Yan priv->err = -ETIMEDOUT; 33715ef2775SWei Yan dev_warn(priv->dev, "%s timeout=%d\n", 33815ef2775SWei Yan msgs->flags & I2C_M_RD ? "rx" : "tx", 33915ef2775SWei Yan priv->adap.timeout); 34015ef2775SWei Yan } 34115ef2775SWei Yan ret = priv->state; 34215ef2775SWei Yan 34315ef2775SWei Yan /* 34415ef2775SWei Yan * If this is the last message to be transfered (stop == 1) 34515ef2775SWei Yan * Then check if the bus can be brought back to idle. 34615ef2775SWei Yan */ 34715ef2775SWei Yan if (priv->state == HIX5I2C_STAT_RW_SUCCESS && stop) 34815ef2775SWei Yan ret = hix5hd2_i2c_wait_bus_idle(priv); 34915ef2775SWei Yan 35015ef2775SWei Yan if (ret < 0) 35115ef2775SWei Yan hix5hd2_i2c_reset(priv); 35215ef2775SWei Yan 35315ef2775SWei Yan return priv->err; 35415ef2775SWei Yan } 35515ef2775SWei Yan 35615ef2775SWei Yan static int hix5hd2_i2c_xfer(struct i2c_adapter *adap, 35715ef2775SWei Yan struct i2c_msg *msgs, int num) 35815ef2775SWei Yan { 35915ef2775SWei Yan struct hix5hd2_i2c_priv *priv = i2c_get_adapdata(adap); 36015ef2775SWei Yan int i, ret, stop; 36115ef2775SWei Yan 36215ef2775SWei Yan pm_runtime_get_sync(priv->dev); 36315ef2775SWei Yan 36415ef2775SWei Yan for (i = 0; i < num; i++, msgs++) { 36515ef2775SWei Yan stop = (i == num - 1); 36615ef2775SWei Yan ret = hix5hd2_i2c_xfer_msg(priv, msgs, stop); 36715ef2775SWei Yan if (ret < 0) 36815ef2775SWei Yan goto out; 36915ef2775SWei Yan } 37015ef2775SWei Yan 37115ef2775SWei Yan ret = num; 37215ef2775SWei Yan 37315ef2775SWei Yan out: 37415ef2775SWei Yan pm_runtime_mark_last_busy(priv->dev); 37515ef2775SWei Yan pm_runtime_put_autosuspend(priv->dev); 37615ef2775SWei Yan return ret; 37715ef2775SWei Yan } 37815ef2775SWei Yan 37915ef2775SWei Yan static u32 hix5hd2_i2c_func(struct i2c_adapter *adap) 38015ef2775SWei Yan { 38115ef2775SWei Yan return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); 38215ef2775SWei Yan } 38315ef2775SWei Yan 38415ef2775SWei Yan static const struct i2c_algorithm hix5hd2_i2c_algorithm = { 38515ef2775SWei Yan .master_xfer = hix5hd2_i2c_xfer, 38615ef2775SWei Yan .functionality = hix5hd2_i2c_func, 38715ef2775SWei Yan }; 38815ef2775SWei Yan 38915ef2775SWei Yan static int hix5hd2_i2c_probe(struct platform_device *pdev) 39015ef2775SWei Yan { 39115ef2775SWei Yan struct device_node *np = pdev->dev.of_node; 39215ef2775SWei Yan struct hix5hd2_i2c_priv *priv; 39315ef2775SWei Yan struct resource *mem; 39415ef2775SWei Yan unsigned int freq; 39515ef2775SWei Yan int irq, ret; 39615ef2775SWei Yan 39715ef2775SWei Yan priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 39815ef2775SWei Yan if (!priv) 39915ef2775SWei Yan return -ENOMEM; 40015ef2775SWei Yan 40115ef2775SWei Yan if (of_property_read_u32(np, "clock-frequency", &freq)) { 40215ef2775SWei Yan /* use 100k as default value */ 40315ef2775SWei Yan priv->freq = 100000; 40415ef2775SWei Yan } else { 40515ef2775SWei Yan if (freq > HIX5I2C_MAX_FREQ) { 40615ef2775SWei Yan priv->freq = HIX5I2C_MAX_FREQ; 40715ef2775SWei Yan dev_warn(priv->dev, "use max freq %d instead\n", 40815ef2775SWei Yan HIX5I2C_MAX_FREQ); 40915ef2775SWei Yan } else { 41015ef2775SWei Yan priv->freq = freq; 41115ef2775SWei Yan } 41215ef2775SWei Yan } 41315ef2775SWei Yan 41415ef2775SWei Yan mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 41515ef2775SWei Yan priv->regs = devm_ioremap_resource(&pdev->dev, mem); 41615ef2775SWei Yan if (IS_ERR(priv->regs)) 41715ef2775SWei Yan return PTR_ERR(priv->regs); 41815ef2775SWei Yan 41915ef2775SWei Yan irq = platform_get_irq(pdev, 0); 42015ef2775SWei Yan if (irq <= 0) { 42115ef2775SWei Yan dev_err(&pdev->dev, "cannot find HS-I2C IRQ\n"); 42215ef2775SWei Yan return irq; 42315ef2775SWei Yan } 42415ef2775SWei Yan 42515ef2775SWei Yan priv->clk = devm_clk_get(&pdev->dev, NULL); 42615ef2775SWei Yan if (IS_ERR(priv->clk)) { 42715ef2775SWei Yan dev_err(&pdev->dev, "cannot get clock\n"); 42815ef2775SWei Yan return PTR_ERR(priv->clk); 42915ef2775SWei Yan } 43015ef2775SWei Yan clk_prepare_enable(priv->clk); 43115ef2775SWei Yan 43215ef2775SWei Yan strlcpy(priv->adap.name, "hix5hd2-i2c", sizeof(priv->adap.name)); 43315ef2775SWei Yan priv->dev = &pdev->dev; 43415ef2775SWei Yan priv->adap.owner = THIS_MODULE; 43515ef2775SWei Yan priv->adap.algo = &hix5hd2_i2c_algorithm; 43615ef2775SWei Yan priv->adap.retries = 3; 43715ef2775SWei Yan priv->adap.dev.of_node = np; 43815ef2775SWei Yan priv->adap.algo_data = priv; 43915ef2775SWei Yan priv->adap.dev.parent = &pdev->dev; 44015ef2775SWei Yan i2c_set_adapdata(&priv->adap, priv); 44115ef2775SWei Yan platform_set_drvdata(pdev, priv); 44215ef2775SWei Yan spin_lock_init(&priv->lock); 44315ef2775SWei Yan init_completion(&priv->msg_complete); 44415ef2775SWei Yan 44515ef2775SWei Yan hix5hd2_i2c_init(priv); 44615ef2775SWei Yan 44715ef2775SWei Yan ret = devm_request_irq(&pdev->dev, irq, hix5hd2_i2c_irq, 448*f8c274e4SSebastian Andrzej Siewior IRQF_NO_SUSPEND, dev_name(&pdev->dev), priv); 44915ef2775SWei Yan if (ret != 0) { 45015ef2775SWei Yan dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", irq); 45115ef2775SWei Yan goto err_clk; 45215ef2775SWei Yan } 45315ef2775SWei Yan 45415ef2775SWei Yan pm_runtime_set_autosuspend_delay(priv->dev, MSEC_PER_SEC); 45515ef2775SWei Yan pm_runtime_use_autosuspend(priv->dev); 45615ef2775SWei Yan pm_runtime_set_active(priv->dev); 45715ef2775SWei Yan pm_runtime_enable(priv->dev); 45815ef2775SWei Yan 45915ef2775SWei Yan ret = i2c_add_adapter(&priv->adap); 460ea734404SWolfram Sang if (ret < 0) 46115ef2775SWei Yan goto err_runtime; 46215ef2775SWei Yan 46315ef2775SWei Yan return ret; 46415ef2775SWei Yan 46515ef2775SWei Yan err_runtime: 46615ef2775SWei Yan pm_runtime_disable(priv->dev); 46715ef2775SWei Yan pm_runtime_set_suspended(priv->dev); 46815ef2775SWei Yan err_clk: 46915ef2775SWei Yan clk_disable_unprepare(priv->clk); 47015ef2775SWei Yan return ret; 47115ef2775SWei Yan } 47215ef2775SWei Yan 47315ef2775SWei Yan static int hix5hd2_i2c_remove(struct platform_device *pdev) 47415ef2775SWei Yan { 47515ef2775SWei Yan struct hix5hd2_i2c_priv *priv = platform_get_drvdata(pdev); 47615ef2775SWei Yan 47715ef2775SWei Yan i2c_del_adapter(&priv->adap); 47815ef2775SWei Yan pm_runtime_disable(priv->dev); 47915ef2775SWei Yan pm_runtime_set_suspended(priv->dev); 48015ef2775SWei Yan 48115ef2775SWei Yan return 0; 48215ef2775SWei Yan } 48315ef2775SWei Yan 48415ef2775SWei Yan #ifdef CONFIG_PM 48515ef2775SWei Yan static int hix5hd2_i2c_runtime_suspend(struct device *dev) 48615ef2775SWei Yan { 4879242e72aSMasahiro Yamada struct hix5hd2_i2c_priv *priv = dev_get_drvdata(dev); 48815ef2775SWei Yan 48915ef2775SWei Yan clk_disable_unprepare(priv->clk); 49015ef2775SWei Yan 49115ef2775SWei Yan return 0; 49215ef2775SWei Yan } 49315ef2775SWei Yan 49415ef2775SWei Yan static int hix5hd2_i2c_runtime_resume(struct device *dev) 49515ef2775SWei Yan { 4969242e72aSMasahiro Yamada struct hix5hd2_i2c_priv *priv = dev_get_drvdata(dev); 49715ef2775SWei Yan 49815ef2775SWei Yan clk_prepare_enable(priv->clk); 49915ef2775SWei Yan hix5hd2_i2c_init(priv); 50015ef2775SWei Yan 50115ef2775SWei Yan return 0; 50215ef2775SWei Yan } 50315ef2775SWei Yan #endif 50415ef2775SWei Yan 50515ef2775SWei Yan static const struct dev_pm_ops hix5hd2_i2c_pm_ops = { 5066ed23b80SRafael J. Wysocki SET_RUNTIME_PM_OPS(hix5hd2_i2c_runtime_suspend, 50715ef2775SWei Yan hix5hd2_i2c_runtime_resume, 50815ef2775SWei Yan NULL) 50915ef2775SWei Yan }; 51015ef2775SWei Yan 51115ef2775SWei Yan static const struct of_device_id hix5hd2_i2c_match[] = { 51215ef2775SWei Yan { .compatible = "hisilicon,hix5hd2-i2c" }, 51315ef2775SWei Yan {}, 51415ef2775SWei Yan }; 51515ef2775SWei Yan MODULE_DEVICE_TABLE(of, hix5hd2_i2c_match); 51615ef2775SWei Yan 51715ef2775SWei Yan static struct platform_driver hix5hd2_i2c_driver = { 51815ef2775SWei Yan .probe = hix5hd2_i2c_probe, 51915ef2775SWei Yan .remove = hix5hd2_i2c_remove, 52015ef2775SWei Yan .driver = { 52115ef2775SWei Yan .name = "hix5hd2-i2c", 52215ef2775SWei Yan .pm = &hix5hd2_i2c_pm_ops, 52315ef2775SWei Yan .of_match_table = hix5hd2_i2c_match, 52415ef2775SWei Yan }, 52515ef2775SWei Yan }; 52615ef2775SWei Yan 52715ef2775SWei Yan module_platform_driver(hix5hd2_i2c_driver); 52815ef2775SWei Yan 52915ef2775SWei Yan MODULE_DESCRIPTION("Hix5hd2 I2C Bus driver"); 53015ef2775SWei Yan MODULE_AUTHOR("Wei Yan <sledge.yanwei@huawei.com>"); 53115ef2775SWei Yan MODULE_LICENSE("GPL"); 5323e59ae4aSAxel Lin MODULE_ALIAS("platform:hix5hd2-i2c"); 533