2 * Synopsys DesignWare I2C adapter driver (slave only).
4 * Based on the Synopsys DesignWare I2C adapter driver (master).
6 * Copyright (C) 2016 Synopsys Inc.
8 * ----------------------------------------------------------------------------
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * ----------------------------------------------------------------------------
22 #include <linux/delay.h>
23 #include <linux/err.h>
24 #include <linux/errno.h>
25 #include <linux/i2c.h>
26 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/pm_runtime.h>
31 #include "i2c-designware-core.h"
33 static void i2c_dw_configure_fifo_slave(struct dw_i2c_dev
*dev
)
35 /* Configure Tx/Rx FIFO threshold levels. */
36 dw_writel(dev
, 0, DW_IC_TX_TL
);
37 dw_writel(dev
, 0, DW_IC_RX_TL
);
39 /* Configure the I2C slave. */
40 dw_writel(dev
, dev
->slave_cfg
, DW_IC_CON
);
41 dw_writel(dev
, DW_IC_INTR_SLAVE_MASK
, DW_IC_INTR_MASK
);
45 * i2c_dw_init_slave() - Initialize the designware i2c slave hardware
46 * @dev: device private data
48 * This function configures and enables the I2C in slave mode.
49 * This function is called during I2C init function, and in case of timeout at
52 static int i2c_dw_init_slave(struct dw_i2c_dev
*dev
)
57 ret
= i2c_dw_acquire_lock(dev
);
61 reg
= dw_readl(dev
, DW_IC_COMP_TYPE
);
62 if (reg
== ___constant_swab32(DW_IC_COMP_TYPE_VALUE
)) {
63 /* Configure register endianness access. */
64 dev
->flags
|= ACCESS_SWAP
;
65 } else if (reg
== (DW_IC_COMP_TYPE_VALUE
& 0x0000ffff)) {
66 /* Configure register access mode 16bit. */
67 dev
->flags
|= ACCESS_16BIT
;
68 } else if (reg
!= DW_IC_COMP_TYPE_VALUE
) {
70 "Unknown Synopsys component type: 0x%08x\n", reg
);
71 i2c_dw_release_lock(dev
);
75 comp_param1
= dw_readl(dev
, DW_IC_COMP_PARAM_1
);
77 /* Disable the adapter. */
78 __i2c_dw_enable_and_wait(dev
, false);
80 /* Configure SDA Hold Time if required. */
81 reg
= dw_readl(dev
, DW_IC_COMP_VERSION
);
82 if (reg
>= DW_IC_SDA_HOLD_MIN_VERS
) {
83 if (!dev
->sda_hold_time
) {
84 /* Keep previous hold time setting if no one set it. */
85 dev
->sda_hold_time
= dw_readl(dev
, DW_IC_SDA_HOLD
);
88 * Workaround for avoiding TX arbitration lost in case I2C
89 * slave pulls SDA down "too quickly" after falling egde of
90 * SCL by enabling non-zero SDA RX hold. Specification says it
91 * extends incoming SDA low to high transition while SCL is
92 * high but it apprears to help also above issue.
94 if (!(dev
->sda_hold_time
& DW_IC_SDA_HOLD_RX_MASK
))
95 dev
->sda_hold_time
|= 1 << DW_IC_SDA_HOLD_RX_SHIFT
;
96 dw_writel(dev
, dev
->sda_hold_time
, DW_IC_SDA_HOLD
);
99 "Hardware too old to adjust SDA hold time.\n");
102 i2c_dw_configure_fifo_slave(dev
);
103 i2c_dw_release_lock(dev
);
108 static int i2c_dw_reg_slave(struct i2c_client
*slave
)
110 struct dw_i2c_dev
*dev
= i2c_get_adapdata(slave
->adapter
);
114 if (slave
->flags
& I2C_CLIENT_TEN
)
115 return -EAFNOSUPPORT
;
116 pm_runtime_get_sync(dev
->dev
);
119 * Set slave address in the IC_SAR register,
120 * the address to which the DW_apb_i2c responds.
122 __i2c_dw_enable(dev
, false);
123 dw_writel(dev
, slave
->addr
, DW_IC_SAR
);
126 __i2c_dw_enable(dev
, true);
129 dev
->msg_write_idx
= 0;
130 dev
->msg_read_idx
= 0;
132 dev
->status
= STATUS_IDLE
;
133 dev
->abort_source
= 0;
134 dev
->rx_outstanding
= 0;
139 static int i2c_dw_unreg_slave(struct i2c_client
*slave
)
141 struct dw_i2c_dev
*dev
= i2c_get_adapdata(slave
->adapter
);
143 dev
->disable_int(dev
);
146 pm_runtime_put(dev
->dev
);
151 static u32
i2c_dw_read_clear_intrbits_slave(struct dw_i2c_dev
*dev
)
156 * The IC_INTR_STAT register just indicates "enabled" interrupts.
157 * Ths unmasked raw version of interrupt status bits are available
158 * in the IC_RAW_INTR_STAT register.
161 * stat = dw_readl(IC_INTR_STAT);
163 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
165 * The raw version might be useful for debugging purposes.
167 stat
= dw_readl(dev
, DW_IC_INTR_STAT
);
170 * Do not use the IC_CLR_INTR register to clear interrupts, or
171 * you'll miss some interrupts, triggered during the period from
172 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
174 * Instead, use the separately-prepared IC_CLR_* registers.
176 if (stat
& DW_IC_INTR_TX_ABRT
)
177 dw_readl(dev
, DW_IC_CLR_TX_ABRT
);
178 if (stat
& DW_IC_INTR_RX_UNDER
)
179 dw_readl(dev
, DW_IC_CLR_RX_UNDER
);
180 if (stat
& DW_IC_INTR_RX_OVER
)
181 dw_readl(dev
, DW_IC_CLR_RX_OVER
);
182 if (stat
& DW_IC_INTR_TX_OVER
)
183 dw_readl(dev
, DW_IC_CLR_TX_OVER
);
184 if (stat
& DW_IC_INTR_RX_DONE
)
185 dw_readl(dev
, DW_IC_CLR_RX_DONE
);
186 if (stat
& DW_IC_INTR_ACTIVITY
)
187 dw_readl(dev
, DW_IC_CLR_ACTIVITY
);
188 if (stat
& DW_IC_INTR_STOP_DET
)
189 dw_readl(dev
, DW_IC_CLR_STOP_DET
);
190 if (stat
& DW_IC_INTR_START_DET
)
191 dw_readl(dev
, DW_IC_CLR_START_DET
);
192 if (stat
& DW_IC_INTR_GEN_CALL
)
193 dw_readl(dev
, DW_IC_CLR_GEN_CALL
);
199 * Interrupt service routine. This gets called whenever an I2C slave interrupt
203 static int i2c_dw_irq_handler_slave(struct dw_i2c_dev
*dev
)
205 u32 raw_stat
, stat
, enabled
;
206 u8 val
, slave_activity
;
208 stat
= dw_readl(dev
, DW_IC_INTR_STAT
);
209 enabled
= dw_readl(dev
, DW_IC_ENABLE
);
210 raw_stat
= dw_readl(dev
, DW_IC_RAW_INTR_STAT
);
211 slave_activity
= ((dw_readl(dev
, DW_IC_STATUS
) &
212 DW_IC_STATUS_SLAVE_ACTIVITY
) >> 6);
214 if (!enabled
|| !(raw_stat
& ~DW_IC_INTR_ACTIVITY
) || !dev
->slave
)
218 "%#x STATUS SLAVE_ACTIVITY=%#x : RAW_INTR_STAT=%#x : INTR_STAT=%#x\n",
219 enabled
, slave_activity
, raw_stat
, stat
);
221 if ((stat
& DW_IC_INTR_RX_FULL
) && (stat
& DW_IC_INTR_STOP_DET
))
222 i2c_slave_event(dev
->slave
, I2C_SLAVE_WRITE_REQUESTED
, &val
);
224 if (stat
& DW_IC_INTR_RD_REQ
) {
225 if (slave_activity
) {
226 if (stat
& DW_IC_INTR_RX_FULL
) {
227 val
= dw_readl(dev
, DW_IC_DATA_CMD
);
229 if (!i2c_slave_event(dev
->slave
,
230 I2C_SLAVE_WRITE_RECEIVED
,
232 dev_vdbg(dev
->dev
, "Byte %X acked!",
235 dw_readl(dev
, DW_IC_CLR_RD_REQ
);
236 stat
= i2c_dw_read_clear_intrbits_slave(dev
);
238 dw_readl(dev
, DW_IC_CLR_RD_REQ
);
239 dw_readl(dev
, DW_IC_CLR_RX_UNDER
);
240 stat
= i2c_dw_read_clear_intrbits_slave(dev
);
242 if (!i2c_slave_event(dev
->slave
,
243 I2C_SLAVE_READ_REQUESTED
,
245 dw_writel(dev
, val
, DW_IC_DATA_CMD
);
249 if (stat
& DW_IC_INTR_RX_DONE
) {
250 if (!i2c_slave_event(dev
->slave
, I2C_SLAVE_READ_PROCESSED
,
252 dw_readl(dev
, DW_IC_CLR_RX_DONE
);
254 i2c_slave_event(dev
->slave
, I2C_SLAVE_STOP
, &val
);
255 stat
= i2c_dw_read_clear_intrbits_slave(dev
);
259 if (stat
& DW_IC_INTR_RX_FULL
) {
260 val
= dw_readl(dev
, DW_IC_DATA_CMD
);
261 if (!i2c_slave_event(dev
->slave
, I2C_SLAVE_WRITE_RECEIVED
,
263 dev_vdbg(dev
->dev
, "Byte %X acked!", val
);
265 i2c_slave_event(dev
->slave
, I2C_SLAVE_STOP
, &val
);
266 stat
= i2c_dw_read_clear_intrbits_slave(dev
);
272 static irqreturn_t
i2c_dw_isr_slave(int this_irq
, void *dev_id
)
274 struct dw_i2c_dev
*dev
= dev_id
;
277 i2c_dw_read_clear_intrbits_slave(dev
);
278 ret
= i2c_dw_irq_handler_slave(dev
);
280 complete(&dev
->cmd_complete
);
282 return IRQ_RETVAL(ret
);
285 static const struct i2c_algorithm i2c_dw_algo
= {
286 .functionality
= i2c_dw_func
,
287 .reg_slave
= i2c_dw_reg_slave
,
288 .unreg_slave
= i2c_dw_unreg_slave
,
291 int i2c_dw_probe_slave(struct dw_i2c_dev
*dev
)
293 struct i2c_adapter
*adap
= &dev
->adapter
;
296 init_completion(&dev
->cmd_complete
);
298 dev
->init
= i2c_dw_init_slave
;
299 dev
->disable
= i2c_dw_disable
;
300 dev
->disable_int
= i2c_dw_disable_int
;
302 ret
= dev
->init(dev
);
306 snprintf(adap
->name
, sizeof(adap
->name
),
307 "Synopsys DesignWare I2C Slave adapter");
309 adap
->algo
= &i2c_dw_algo
;
310 adap
->dev
.parent
= dev
->dev
;
311 i2c_set_adapdata(adap
, dev
);
313 ret
= devm_request_irq(dev
->dev
, dev
->irq
, i2c_dw_isr_slave
,
314 IRQF_SHARED
, dev_name(dev
->dev
), dev
);
316 dev_err(dev
->dev
, "failure requesting irq %i: %d\n",
321 ret
= i2c_add_numbered_adapter(adap
);
323 dev_err(dev
->dev
, "failure adding adapter: %d\n", ret
);
327 EXPORT_SYMBOL_GPL(i2c_dw_probe_slave
);
329 MODULE_AUTHOR("Luis Oliveira <lolivei@synopsys.com>");
330 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus slave adapter");
331 MODULE_LICENSE("GPL v2");