12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
215ef2775SWei Yan /*
315ef2775SWei Yan * Copyright (c) 2014 Linaro Ltd.
4629a411fSHao Fang * 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 enum hix5hd2_i2c_state {
7215ef2775SWei Yan HIX5I2C_STAT_RW_ERR = -1,
7315ef2775SWei Yan HIX5I2C_STAT_INIT,
7415ef2775SWei Yan HIX5I2C_STAT_RW,
7515ef2775SWei Yan HIX5I2C_STAT_SND_STOP,
7615ef2775SWei Yan HIX5I2C_STAT_RW_SUCCESS,
7715ef2775SWei Yan };
7815ef2775SWei Yan
7915ef2775SWei Yan struct hix5hd2_i2c_priv {
8015ef2775SWei Yan struct i2c_adapter adap;
8115ef2775SWei Yan struct i2c_msg *msg;
8215ef2775SWei Yan struct completion msg_complete;
8315ef2775SWei Yan unsigned int msg_idx;
8415ef2775SWei Yan unsigned int msg_len;
8515ef2775SWei Yan int stop;
8615ef2775SWei Yan void __iomem *regs;
8715ef2775SWei Yan struct clk *clk;
8815ef2775SWei Yan struct device *dev;
8915ef2775SWei Yan spinlock_t lock; /* IRQ synchronization */
9015ef2775SWei Yan int err;
9115ef2775SWei Yan unsigned int freq;
9215ef2775SWei Yan enum hix5hd2_i2c_state state;
9315ef2775SWei Yan };
9415ef2775SWei Yan
hix5hd2_i2c_clr_pend_irq(struct hix5hd2_i2c_priv * priv)9515ef2775SWei Yan static u32 hix5hd2_i2c_clr_pend_irq(struct hix5hd2_i2c_priv *priv)
9615ef2775SWei Yan {
9715ef2775SWei Yan u32 val = readl_relaxed(priv->regs + HIX5I2C_SR);
9815ef2775SWei Yan
9915ef2775SWei Yan writel_relaxed(val, priv->regs + HIX5I2C_ICR);
10015ef2775SWei Yan
10115ef2775SWei Yan return val;
10215ef2775SWei Yan }
10315ef2775SWei Yan
hix5hd2_i2c_clr_all_irq(struct hix5hd2_i2c_priv * priv)10415ef2775SWei Yan static void hix5hd2_i2c_clr_all_irq(struct hix5hd2_i2c_priv *priv)
10515ef2775SWei Yan {
10615ef2775SWei Yan writel_relaxed(I2C_CLEAR_ALL, priv->regs + HIX5I2C_ICR);
10715ef2775SWei Yan }
10815ef2775SWei Yan
hix5hd2_i2c_disable_irq(struct hix5hd2_i2c_priv * priv)10915ef2775SWei Yan static void hix5hd2_i2c_disable_irq(struct hix5hd2_i2c_priv *priv)
11015ef2775SWei Yan {
11115ef2775SWei Yan writel_relaxed(0, priv->regs + HIX5I2C_CTRL);
11215ef2775SWei Yan }
11315ef2775SWei Yan
hix5hd2_i2c_enable_irq(struct hix5hd2_i2c_priv * priv)11415ef2775SWei Yan static void hix5hd2_i2c_enable_irq(struct hix5hd2_i2c_priv *priv)
11515ef2775SWei Yan {
11615ef2775SWei Yan writel_relaxed(I2C_ENABLE | I2C_UNMASK_TOTAL | I2C_UNMASK_ALL,
11715ef2775SWei Yan priv->regs + HIX5I2C_CTRL);
11815ef2775SWei Yan }
11915ef2775SWei Yan
hix5hd2_i2c_drv_setrate(struct hix5hd2_i2c_priv * priv)12015ef2775SWei Yan static void hix5hd2_i2c_drv_setrate(struct hix5hd2_i2c_priv *priv)
12115ef2775SWei Yan {
12215ef2775SWei Yan u32 rate, val;
12315ef2775SWei Yan u32 scl, sysclock;
12415ef2775SWei Yan
12515ef2775SWei Yan /* close all i2c interrupt */
12615ef2775SWei Yan val = readl_relaxed(priv->regs + HIX5I2C_CTRL);
12715ef2775SWei Yan writel_relaxed(val & (~I2C_UNMASK_TOTAL), priv->regs + HIX5I2C_CTRL);
12815ef2775SWei Yan
12915ef2775SWei Yan rate = priv->freq;
13015ef2775SWei Yan sysclock = clk_get_rate(priv->clk);
13115ef2775SWei Yan scl = (sysclock / (rate * 2)) / 2 - 1;
13215ef2775SWei Yan writel_relaxed(scl, priv->regs + HIX5I2C_SCL_H);
13315ef2775SWei Yan writel_relaxed(scl, priv->regs + HIX5I2C_SCL_L);
13415ef2775SWei Yan
13515ef2775SWei Yan /* restore original interrupt*/
13615ef2775SWei Yan writel_relaxed(val, priv->regs + HIX5I2C_CTRL);
13715ef2775SWei Yan
13815ef2775SWei Yan dev_dbg(priv->dev, "%s: sysclock=%d, rate=%d, scl=%d\n",
13915ef2775SWei Yan __func__, sysclock, rate, scl);
14015ef2775SWei Yan }
14115ef2775SWei Yan
hix5hd2_i2c_init(struct hix5hd2_i2c_priv * priv)14215ef2775SWei Yan static void hix5hd2_i2c_init(struct hix5hd2_i2c_priv *priv)
14315ef2775SWei Yan {
14415ef2775SWei Yan hix5hd2_i2c_disable_irq(priv);
14515ef2775SWei Yan hix5hd2_i2c_drv_setrate(priv);
14615ef2775SWei Yan hix5hd2_i2c_clr_all_irq(priv);
14715ef2775SWei Yan hix5hd2_i2c_enable_irq(priv);
14815ef2775SWei Yan }
14915ef2775SWei Yan
hix5hd2_i2c_reset(struct hix5hd2_i2c_priv * priv)15015ef2775SWei Yan static void hix5hd2_i2c_reset(struct hix5hd2_i2c_priv *priv)
15115ef2775SWei Yan {
15215ef2775SWei Yan clk_disable_unprepare(priv->clk);
15315ef2775SWei Yan msleep(20);
15415ef2775SWei Yan clk_prepare_enable(priv->clk);
15515ef2775SWei Yan hix5hd2_i2c_init(priv);
15615ef2775SWei Yan }
15715ef2775SWei Yan
hix5hd2_i2c_wait_bus_idle(struct hix5hd2_i2c_priv * priv)15815ef2775SWei Yan static int hix5hd2_i2c_wait_bus_idle(struct hix5hd2_i2c_priv *priv)
15915ef2775SWei Yan {
16015ef2775SWei Yan unsigned long stop_time;
16115ef2775SWei Yan u32 int_status;
16215ef2775SWei Yan
16315ef2775SWei Yan /* wait for 100 milli seconds for the bus to be idle */
16415ef2775SWei Yan stop_time = jiffies + msecs_to_jiffies(100);
16515ef2775SWei Yan do {
16615ef2775SWei Yan int_status = hix5hd2_i2c_clr_pend_irq(priv);
16715ef2775SWei Yan if (!(int_status & I2C_BUSY))
16815ef2775SWei Yan return 0;
16915ef2775SWei Yan
17015ef2775SWei Yan usleep_range(50, 200);
17115ef2775SWei Yan } while (time_before(jiffies, stop_time));
17215ef2775SWei Yan
17315ef2775SWei Yan return -EBUSY;
17415ef2775SWei Yan }
17515ef2775SWei Yan
hix5hd2_rw_over(struct hix5hd2_i2c_priv * priv)17615ef2775SWei Yan static void hix5hd2_rw_over(struct hix5hd2_i2c_priv *priv)
17715ef2775SWei Yan {
17815ef2775SWei Yan if (priv->state == HIX5I2C_STAT_SND_STOP)
17915ef2775SWei Yan dev_dbg(priv->dev, "%s: rw and send stop over\n", __func__);
18015ef2775SWei Yan else
18115ef2775SWei Yan dev_dbg(priv->dev, "%s: have not data to send\n", __func__);
18215ef2775SWei Yan
18315ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_SUCCESS;
18415ef2775SWei Yan priv->err = 0;
18515ef2775SWei Yan }
18615ef2775SWei Yan
hix5hd2_rw_handle_stop(struct hix5hd2_i2c_priv * priv)18715ef2775SWei Yan static void hix5hd2_rw_handle_stop(struct hix5hd2_i2c_priv *priv)
18815ef2775SWei Yan {
18915ef2775SWei Yan if (priv->stop) {
19015ef2775SWei Yan priv->state = HIX5I2C_STAT_SND_STOP;
19115ef2775SWei Yan writel_relaxed(I2C_STOP, priv->regs + HIX5I2C_COM);
19215ef2775SWei Yan } else {
19315ef2775SWei Yan hix5hd2_rw_over(priv);
19415ef2775SWei Yan }
19515ef2775SWei Yan }
19615ef2775SWei Yan
hix5hd2_read_handle(struct hix5hd2_i2c_priv * priv)19715ef2775SWei Yan static void hix5hd2_read_handle(struct hix5hd2_i2c_priv *priv)
19815ef2775SWei Yan {
19915ef2775SWei Yan if (priv->msg_len == 1) {
20015ef2775SWei Yan /* the last byte don't need send ACK */
20115ef2775SWei Yan writel_relaxed(I2C_READ | I2C_NO_ACK, priv->regs + HIX5I2C_COM);
20215ef2775SWei Yan } else if (priv->msg_len > 1) {
203*7c9e6705SWolfram Sang /* if i2c controller receive data will send ACK */
20415ef2775SWei Yan writel_relaxed(I2C_READ, priv->regs + HIX5I2C_COM);
20515ef2775SWei Yan } else {
20615ef2775SWei Yan hix5hd2_rw_handle_stop(priv);
20715ef2775SWei Yan }
20815ef2775SWei Yan }
20915ef2775SWei Yan
hix5hd2_write_handle(struct hix5hd2_i2c_priv * priv)21015ef2775SWei Yan static void hix5hd2_write_handle(struct hix5hd2_i2c_priv *priv)
21115ef2775SWei Yan {
21215ef2775SWei Yan u8 data;
21315ef2775SWei Yan
21415ef2775SWei Yan if (priv->msg_len > 0) {
21515ef2775SWei Yan data = priv->msg->buf[priv->msg_idx++];
21615ef2775SWei Yan writel_relaxed(data, priv->regs + HIX5I2C_TXR);
21715ef2775SWei Yan writel_relaxed(I2C_WRITE, priv->regs + HIX5I2C_COM);
21815ef2775SWei Yan } else {
21915ef2775SWei Yan hix5hd2_rw_handle_stop(priv);
22015ef2775SWei Yan }
22115ef2775SWei Yan }
22215ef2775SWei Yan
hix5hd2_rw_preprocess(struct hix5hd2_i2c_priv * priv)22315ef2775SWei Yan static int hix5hd2_rw_preprocess(struct hix5hd2_i2c_priv *priv)
22415ef2775SWei Yan {
22515ef2775SWei Yan u8 data;
22615ef2775SWei Yan
22715ef2775SWei Yan if (priv->state == HIX5I2C_STAT_INIT) {
22815ef2775SWei Yan priv->state = HIX5I2C_STAT_RW;
22915ef2775SWei Yan } else if (priv->state == HIX5I2C_STAT_RW) {
23015ef2775SWei Yan if (priv->msg->flags & I2C_M_RD) {
23115ef2775SWei Yan data = readl_relaxed(priv->regs + HIX5I2C_RXR);
23215ef2775SWei Yan priv->msg->buf[priv->msg_idx++] = data;
23315ef2775SWei Yan }
23415ef2775SWei Yan priv->msg_len--;
23515ef2775SWei Yan } else {
23615ef2775SWei Yan dev_dbg(priv->dev, "%s: error: priv->state = %d, msg_len = %d\n",
23715ef2775SWei Yan __func__, priv->state, priv->msg_len);
23815ef2775SWei Yan return -EAGAIN;
23915ef2775SWei Yan }
24015ef2775SWei Yan return 0;
24115ef2775SWei Yan }
24215ef2775SWei Yan
hix5hd2_i2c_irq(int irqno,void * dev_id)24315ef2775SWei Yan static irqreturn_t hix5hd2_i2c_irq(int irqno, void *dev_id)
24415ef2775SWei Yan {
24515ef2775SWei Yan struct hix5hd2_i2c_priv *priv = dev_id;
24615ef2775SWei Yan u32 int_status;
24715ef2775SWei Yan int ret;
24815ef2775SWei Yan
24915ef2775SWei Yan spin_lock(&priv->lock);
25015ef2775SWei Yan
25115ef2775SWei Yan int_status = hix5hd2_i2c_clr_pend_irq(priv);
25215ef2775SWei Yan
25315ef2775SWei Yan /* handle error */
25415ef2775SWei Yan if (int_status & I2C_ARBITRATE_INTR) {
25515ef2775SWei Yan /* bus error */
25615ef2775SWei Yan dev_dbg(priv->dev, "ARB bus loss\n");
25715ef2775SWei Yan priv->err = -EAGAIN;
25815ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_ERR;
25915ef2775SWei Yan goto stop;
26015ef2775SWei Yan } else if (int_status & I2C_ACK_INTR) {
26115ef2775SWei Yan /* ack error */
26215ef2775SWei Yan dev_dbg(priv->dev, "No ACK from device\n");
26315ef2775SWei Yan priv->err = -ENXIO;
26415ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_ERR;
26515ef2775SWei Yan goto stop;
26615ef2775SWei Yan }
26715ef2775SWei Yan
26815ef2775SWei Yan if (int_status & I2C_OVER_INTR) {
26915ef2775SWei Yan if (priv->msg_len > 0) {
27015ef2775SWei Yan ret = hix5hd2_rw_preprocess(priv);
27115ef2775SWei Yan if (ret) {
27215ef2775SWei Yan priv->err = ret;
27315ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_ERR;
27415ef2775SWei Yan goto stop;
27515ef2775SWei Yan }
27615ef2775SWei Yan if (priv->msg->flags & I2C_M_RD)
27715ef2775SWei Yan hix5hd2_read_handle(priv);
27815ef2775SWei Yan else
27915ef2775SWei Yan hix5hd2_write_handle(priv);
28015ef2775SWei Yan } else {
28115ef2775SWei Yan hix5hd2_rw_over(priv);
28215ef2775SWei Yan }
28315ef2775SWei Yan }
28415ef2775SWei Yan
28515ef2775SWei Yan stop:
28615ef2775SWei Yan if ((priv->state == HIX5I2C_STAT_RW_SUCCESS &&
28715ef2775SWei Yan priv->msg->len == priv->msg_idx) ||
28815ef2775SWei Yan (priv->state == HIX5I2C_STAT_RW_ERR)) {
28915ef2775SWei Yan hix5hd2_i2c_disable_irq(priv);
29015ef2775SWei Yan hix5hd2_i2c_clr_pend_irq(priv);
29115ef2775SWei Yan complete(&priv->msg_complete);
29215ef2775SWei Yan }
29315ef2775SWei Yan
29415ef2775SWei Yan spin_unlock(&priv->lock);
29515ef2775SWei Yan
29615ef2775SWei Yan return IRQ_HANDLED;
29715ef2775SWei Yan }
29815ef2775SWei Yan
hix5hd2_i2c_message_start(struct hix5hd2_i2c_priv * priv,int stop)29915ef2775SWei Yan static void hix5hd2_i2c_message_start(struct hix5hd2_i2c_priv *priv, int stop)
30015ef2775SWei Yan {
30115ef2775SWei Yan unsigned long flags;
30215ef2775SWei Yan
30315ef2775SWei Yan spin_lock_irqsave(&priv->lock, flags);
30415ef2775SWei Yan hix5hd2_i2c_clr_all_irq(priv);
30515ef2775SWei Yan hix5hd2_i2c_enable_irq(priv);
30615ef2775SWei Yan
30730a64757SPeter Rosin writel_relaxed(i2c_8bit_addr_from_msg(priv->msg),
30815ef2775SWei Yan priv->regs + HIX5I2C_TXR);
30915ef2775SWei Yan
31015ef2775SWei Yan writel_relaxed(I2C_WRITE | I2C_START, priv->regs + HIX5I2C_COM);
31115ef2775SWei Yan spin_unlock_irqrestore(&priv->lock, flags);
31215ef2775SWei Yan }
31315ef2775SWei Yan
hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv * priv,struct i2c_msg * msgs,int stop)31415ef2775SWei Yan static int hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv *priv,
31515ef2775SWei Yan struct i2c_msg *msgs, int stop)
31615ef2775SWei Yan {
317b557e267SWolfram Sang unsigned long time_left;
31815ef2775SWei Yan int ret;
31915ef2775SWei Yan
32015ef2775SWei Yan priv->msg = msgs;
32115ef2775SWei Yan priv->msg_idx = 0;
32215ef2775SWei Yan priv->msg_len = priv->msg->len;
32315ef2775SWei Yan priv->stop = stop;
32415ef2775SWei Yan priv->err = 0;
32515ef2775SWei Yan priv->state = HIX5I2C_STAT_INIT;
32615ef2775SWei Yan
32715ef2775SWei Yan reinit_completion(&priv->msg_complete);
32815ef2775SWei Yan hix5hd2_i2c_message_start(priv, stop);
32915ef2775SWei Yan
330b557e267SWolfram Sang time_left = wait_for_completion_timeout(&priv->msg_complete,
33115ef2775SWei Yan priv->adap.timeout);
332b557e267SWolfram Sang if (time_left == 0) {
33315ef2775SWei Yan priv->state = HIX5I2C_STAT_RW_ERR;
33415ef2775SWei Yan priv->err = -ETIMEDOUT;
33515ef2775SWei Yan dev_warn(priv->dev, "%s timeout=%d\n",
33615ef2775SWei Yan msgs->flags & I2C_M_RD ? "rx" : "tx",
33715ef2775SWei Yan priv->adap.timeout);
33815ef2775SWei Yan }
33915ef2775SWei Yan ret = priv->state;
34015ef2775SWei Yan
34115ef2775SWei Yan /*
34215ef2775SWei Yan * If this is the last message to be transfered (stop == 1)
34315ef2775SWei Yan * Then check if the bus can be brought back to idle.
34415ef2775SWei Yan */
34515ef2775SWei Yan if (priv->state == HIX5I2C_STAT_RW_SUCCESS && stop)
34615ef2775SWei Yan ret = hix5hd2_i2c_wait_bus_idle(priv);
34715ef2775SWei Yan
34815ef2775SWei Yan if (ret < 0)
34915ef2775SWei Yan hix5hd2_i2c_reset(priv);
35015ef2775SWei Yan
35115ef2775SWei Yan return priv->err;
35215ef2775SWei Yan }
35315ef2775SWei Yan
hix5hd2_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)35415ef2775SWei Yan static int hix5hd2_i2c_xfer(struct i2c_adapter *adap,
35515ef2775SWei Yan struct i2c_msg *msgs, int num)
35615ef2775SWei Yan {
35715ef2775SWei Yan struct hix5hd2_i2c_priv *priv = i2c_get_adapdata(adap);
35815ef2775SWei Yan int i, ret, stop;
35915ef2775SWei Yan
36015ef2775SWei Yan pm_runtime_get_sync(priv->dev);
36115ef2775SWei Yan
36215ef2775SWei Yan for (i = 0; i < num; i++, msgs++) {
3630520628eStaolan if ((i == num - 1) || (msgs->flags & I2C_M_STOP))
3640520628eStaolan stop = 1;
3650520628eStaolan else
3660520628eStaolan stop = 0;
3670520628eStaolan
36815ef2775SWei Yan ret = hix5hd2_i2c_xfer_msg(priv, msgs, stop);
36915ef2775SWei Yan if (ret < 0)
37015ef2775SWei Yan goto out;
37115ef2775SWei Yan }
37215ef2775SWei Yan
37315ef2775SWei Yan ret = num;
37415ef2775SWei Yan
37515ef2775SWei Yan out:
37615ef2775SWei Yan pm_runtime_mark_last_busy(priv->dev);
37715ef2775SWei Yan pm_runtime_put_autosuspend(priv->dev);
37815ef2775SWei Yan return ret;
37915ef2775SWei Yan }
38015ef2775SWei Yan
hix5hd2_i2c_func(struct i2c_adapter * adap)38115ef2775SWei Yan static u32 hix5hd2_i2c_func(struct i2c_adapter *adap)
38215ef2775SWei Yan {
38315ef2775SWei Yan return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
38415ef2775SWei Yan }
38515ef2775SWei Yan
38615ef2775SWei Yan static const struct i2c_algorithm hix5hd2_i2c_algorithm = {
387*7c9e6705SWolfram Sang .xfer = hix5hd2_i2c_xfer,
38815ef2775SWei Yan .functionality = hix5hd2_i2c_func,
38915ef2775SWei Yan };
39015ef2775SWei Yan
hix5hd2_i2c_probe(struct platform_device * pdev)39115ef2775SWei Yan static int hix5hd2_i2c_probe(struct platform_device *pdev)
39215ef2775SWei Yan {
39315ef2775SWei Yan struct device_node *np = pdev->dev.of_node;
39415ef2775SWei Yan struct hix5hd2_i2c_priv *priv;
39515ef2775SWei Yan unsigned int freq;
39615ef2775SWei Yan int irq, ret;
39715ef2775SWei Yan
39815ef2775SWei Yan priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
39915ef2775SWei Yan if (!priv)
40015ef2775SWei Yan return -ENOMEM;
40115ef2775SWei Yan
40215ef2775SWei Yan if (of_property_read_u32(np, "clock-frequency", &freq)) {
40315ef2775SWei Yan /* use 100k as default value */
40490224e64SAndy Shevchenko priv->freq = I2C_MAX_STANDARD_MODE_FREQ;
40515ef2775SWei Yan } else {
40690224e64SAndy Shevchenko if (freq > I2C_MAX_FAST_MODE_FREQ) {
40790224e64SAndy Shevchenko priv->freq = I2C_MAX_FAST_MODE_FREQ;
40815ef2775SWei Yan dev_warn(priv->dev, "use max freq %d instead\n",
40990224e64SAndy Shevchenko I2C_MAX_FAST_MODE_FREQ);
41015ef2775SWei Yan } else {
41115ef2775SWei Yan priv->freq = freq;
41215ef2775SWei Yan }
41315ef2775SWei Yan }
41415ef2775SWei Yan
415e0442d76SDejin Zheng priv->regs = devm_platform_ioremap_resource(pdev, 0);
41615ef2775SWei Yan if (IS_ERR(priv->regs))
41715ef2775SWei Yan return PTR_ERR(priv->regs);
41815ef2775SWei Yan
41915ef2775SWei Yan irq = platform_get_irq(pdev, 0);
420f9b459c2SSergey Shtylyov if (irq < 0)
42115ef2775SWei Yan return irq;
42215ef2775SWei Yan
4232153244bSAndi Shyti priv->clk = devm_clk_get_enabled(&pdev->dev, NULL);
42415ef2775SWei Yan if (IS_ERR(priv->clk)) {
4252153244bSAndi Shyti dev_err(&pdev->dev, "cannot enable clock\n");
42615ef2775SWei Yan return PTR_ERR(priv->clk);
42715ef2775SWei Yan }
42815ef2775SWei Yan
429ea1558ceSWolfram Sang strscpy(priv->adap.name, "hix5hd2-i2c", sizeof(priv->adap.name));
43015ef2775SWei Yan priv->dev = &pdev->dev;
43115ef2775SWei Yan priv->adap.owner = THIS_MODULE;
43215ef2775SWei Yan priv->adap.algo = &hix5hd2_i2c_algorithm;
43315ef2775SWei Yan priv->adap.retries = 3;
43415ef2775SWei Yan priv->adap.dev.of_node = np;
43515ef2775SWei Yan priv->adap.algo_data = priv;
43615ef2775SWei Yan priv->adap.dev.parent = &pdev->dev;
43715ef2775SWei Yan i2c_set_adapdata(&priv->adap, priv);
43815ef2775SWei Yan platform_set_drvdata(pdev, priv);
43915ef2775SWei Yan spin_lock_init(&priv->lock);
44015ef2775SWei Yan init_completion(&priv->msg_complete);
44115ef2775SWei Yan
44215ef2775SWei Yan hix5hd2_i2c_init(priv);
44315ef2775SWei Yan
44415ef2775SWei Yan ret = devm_request_irq(&pdev->dev, irq, hix5hd2_i2c_irq,
445f8c274e4SSebastian Andrzej Siewior IRQF_NO_SUSPEND, dev_name(&pdev->dev), priv);
44615ef2775SWei Yan if (ret != 0) {
44715ef2775SWei Yan dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", irq);
4482153244bSAndi Shyti return ret;
44915ef2775SWei Yan }
45015ef2775SWei Yan
45115ef2775SWei Yan pm_runtime_set_autosuspend_delay(priv->dev, MSEC_PER_SEC);
45215ef2775SWei Yan pm_runtime_use_autosuspend(priv->dev);
45315ef2775SWei Yan pm_runtime_set_active(priv->dev);
45415ef2775SWei Yan pm_runtime_enable(priv->dev);
45515ef2775SWei Yan
45615ef2775SWei Yan ret = i2c_add_adapter(&priv->adap);
457ea734404SWolfram Sang if (ret < 0)
45815ef2775SWei Yan goto err_runtime;
45915ef2775SWei Yan
46015ef2775SWei Yan return ret;
46115ef2775SWei Yan
46215ef2775SWei Yan err_runtime:
46315ef2775SWei Yan pm_runtime_disable(priv->dev);
46415ef2775SWei Yan pm_runtime_set_suspended(priv->dev);
4652153244bSAndi Shyti
46615ef2775SWei Yan return ret;
46715ef2775SWei Yan }
46815ef2775SWei Yan
hix5hd2_i2c_remove(struct platform_device * pdev)469e190a0c3SUwe Kleine-König static void hix5hd2_i2c_remove(struct platform_device *pdev)
47015ef2775SWei Yan {
47115ef2775SWei Yan struct hix5hd2_i2c_priv *priv = platform_get_drvdata(pdev);
47215ef2775SWei Yan
47315ef2775SWei Yan i2c_del_adapter(&priv->adap);
47415ef2775SWei Yan pm_runtime_disable(priv->dev);
47515ef2775SWei Yan pm_runtime_set_suspended(priv->dev);
47615ef2775SWei Yan }
47715ef2775SWei Yan
hix5hd2_i2c_runtime_suspend(struct device * dev)47815ef2775SWei Yan static int hix5hd2_i2c_runtime_suspend(struct device *dev)
47915ef2775SWei Yan {
4809242e72aSMasahiro Yamada struct hix5hd2_i2c_priv *priv = dev_get_drvdata(dev);
48115ef2775SWei Yan
48215ef2775SWei Yan clk_disable_unprepare(priv->clk);
48315ef2775SWei Yan
48415ef2775SWei Yan return 0;
48515ef2775SWei Yan }
48615ef2775SWei Yan
hix5hd2_i2c_runtime_resume(struct device * dev)48715ef2775SWei Yan static int hix5hd2_i2c_runtime_resume(struct device *dev)
48815ef2775SWei Yan {
4899242e72aSMasahiro Yamada struct hix5hd2_i2c_priv *priv = dev_get_drvdata(dev);
49015ef2775SWei Yan
49115ef2775SWei Yan clk_prepare_enable(priv->clk);
49215ef2775SWei Yan hix5hd2_i2c_init(priv);
49315ef2775SWei Yan
49415ef2775SWei Yan return 0;
49515ef2775SWei Yan }
49615ef2775SWei Yan
49715ef2775SWei Yan static const struct dev_pm_ops hix5hd2_i2c_pm_ops = {
49828f3fb1cSPaul Cercueil RUNTIME_PM_OPS(hix5hd2_i2c_runtime_suspend,
49915ef2775SWei Yan hix5hd2_i2c_runtime_resume,
50015ef2775SWei Yan NULL)
50115ef2775SWei Yan };
50215ef2775SWei Yan
50315ef2775SWei Yan static const struct of_device_id hix5hd2_i2c_match[] = {
50415ef2775SWei Yan { .compatible = "hisilicon,hix5hd2-i2c" },
50515ef2775SWei Yan {},
50615ef2775SWei Yan };
50715ef2775SWei Yan MODULE_DEVICE_TABLE(of, hix5hd2_i2c_match);
50815ef2775SWei Yan
50915ef2775SWei Yan static struct platform_driver hix5hd2_i2c_driver = {
51015ef2775SWei Yan .probe = hix5hd2_i2c_probe,
511e190a0c3SUwe Kleine-König .remove_new = hix5hd2_i2c_remove,
51215ef2775SWei Yan .driver = {
51315ef2775SWei Yan .name = "hix5hd2-i2c",
51428f3fb1cSPaul Cercueil .pm = pm_ptr(&hix5hd2_i2c_pm_ops),
51515ef2775SWei Yan .of_match_table = hix5hd2_i2c_match,
51615ef2775SWei Yan },
51715ef2775SWei Yan };
51815ef2775SWei Yan
51915ef2775SWei Yan module_platform_driver(hix5hd2_i2c_driver);
52015ef2775SWei Yan
52115ef2775SWei Yan MODULE_DESCRIPTION("Hix5hd2 I2C Bus driver");
52215ef2775SWei Yan MODULE_AUTHOR("Wei Yan <sledge.yanwei@huawei.com>");
52315ef2775SWei Yan MODULE_LICENSE("GPL");
5243e59ae4aSAxel Lin MODULE_ALIAS("platform:hix5hd2-i2c");
525