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