1 // SPDX-License-Identifier: GPL-2.0
3 * Synopsys DesignWare I2C adapter driver (slave only).
5 * Based on the Synopsys DesignWare I2C adapter driver (master).
7 * Copyright (C) 2016 Synopsys Inc.
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
19 #define DEFAULT_SYMBOL_NAMESPACE "I2C_DW"
21 #include "i2c-designware-core.h"
23 static void i2c_dw_configure_fifo_slave(struct dw_i2c_dev
*dev
)
25 /* Configure Tx/Rx FIFO threshold levels. */
26 regmap_write(dev
->map
, DW_IC_TX_TL
, 0);
27 regmap_write(dev
->map
, DW_IC_RX_TL
, 0);
29 /* Configure the I2C slave. */
30 regmap_write(dev
->map
, DW_IC_CON
, dev
->slave_cfg
);
31 regmap_write(dev
->map
, DW_IC_INTR_MASK
, DW_IC_INTR_SLAVE_MASK
);
35 * i2c_dw_init_slave() - Initialize the DesignWare i2c slave hardware
36 * @dev: device private data
38 * This function configures and enables the I2C in slave mode.
39 * This function is called during I2C init function, and in case of timeout at
42 * Return: 0 on success, or negative errno otherwise.
44 static int i2c_dw_init_slave(struct dw_i2c_dev
*dev
)
48 ret
= i2c_dw_acquire_lock(dev
);
52 /* Disable the adapter. */
53 __i2c_dw_disable(dev
);
55 /* Write SDA hold time if supported */
56 if (dev
->sda_hold_time
)
57 regmap_write(dev
->map
, DW_IC_SDA_HOLD
, dev
->sda_hold_time
);
59 i2c_dw_configure_fifo_slave(dev
);
60 i2c_dw_release_lock(dev
);
65 static int i2c_dw_reg_slave(struct i2c_client
*slave
)
67 struct dw_i2c_dev
*dev
= i2c_get_adapdata(slave
->adapter
);
71 if (slave
->flags
& I2C_CLIENT_TEN
)
73 pm_runtime_get_sync(dev
->dev
);
76 * Set slave address in the IC_SAR register,
77 * the address to which the DW_apb_i2c responds.
79 __i2c_dw_disable_nowait(dev
);
80 regmap_write(dev
->map
, DW_IC_SAR
, slave
->addr
);
90 static int i2c_dw_unreg_slave(struct i2c_client
*slave
)
92 struct dw_i2c_dev
*dev
= i2c_get_adapdata(slave
->adapter
);
94 regmap_write(dev
->map
, DW_IC_INTR_MASK
, 0);
96 synchronize_irq(dev
->irq
);
98 pm_runtime_put(dev
->dev
);
103 static u32
i2c_dw_read_clear_intrbits_slave(struct dw_i2c_dev
*dev
)
105 unsigned int stat
, dummy
;
108 * The IC_INTR_STAT register just indicates "enabled" interrupts.
109 * The unmasked raw version of interrupt status bits is available
110 * in the IC_RAW_INTR_STAT register.
113 * stat = readl(IC_INTR_STAT);
115 * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
117 * The raw version might be useful for debugging purposes.
119 regmap_read(dev
->map
, DW_IC_INTR_STAT
, &stat
);
122 * Do not use the IC_CLR_INTR register to clear interrupts, or
123 * you'll miss some interrupts, triggered during the period from
124 * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
126 * Instead, use the separately-prepared IC_CLR_* registers.
128 if (stat
& DW_IC_INTR_TX_ABRT
)
129 regmap_read(dev
->map
, DW_IC_CLR_TX_ABRT
, &dummy
);
130 if (stat
& DW_IC_INTR_RX_UNDER
)
131 regmap_read(dev
->map
, DW_IC_CLR_RX_UNDER
, &dummy
);
132 if (stat
& DW_IC_INTR_RX_OVER
)
133 regmap_read(dev
->map
, DW_IC_CLR_RX_OVER
, &dummy
);
134 if (stat
& DW_IC_INTR_TX_OVER
)
135 regmap_read(dev
->map
, DW_IC_CLR_TX_OVER
, &dummy
);
136 if (stat
& DW_IC_INTR_RX_DONE
)
137 regmap_read(dev
->map
, DW_IC_CLR_RX_DONE
, &dummy
);
138 if (stat
& DW_IC_INTR_ACTIVITY
)
139 regmap_read(dev
->map
, DW_IC_CLR_ACTIVITY
, &dummy
);
140 if (stat
& DW_IC_INTR_STOP_DET
)
141 regmap_read(dev
->map
, DW_IC_CLR_STOP_DET
, &dummy
);
142 if (stat
& DW_IC_INTR_START_DET
)
143 regmap_read(dev
->map
, DW_IC_CLR_START_DET
, &dummy
);
144 if (stat
& DW_IC_INTR_GEN_CALL
)
145 regmap_read(dev
->map
, DW_IC_CLR_GEN_CALL
, &dummy
);
151 * Interrupt service routine. This gets called whenever an I2C slave interrupt
154 static irqreturn_t
i2c_dw_isr_slave(int this_irq
, void *dev_id
)
156 struct dw_i2c_dev
*dev
= dev_id
;
157 unsigned int raw_stat
, stat
, enabled
, tmp
;
158 u8 val
= 0, slave_activity
;
160 regmap_read(dev
->map
, DW_IC_ENABLE
, &enabled
);
161 regmap_read(dev
->map
, DW_IC_RAW_INTR_STAT
, &raw_stat
);
162 regmap_read(dev
->map
, DW_IC_STATUS
, &tmp
);
163 slave_activity
= ((tmp
& DW_IC_STATUS_SLAVE_ACTIVITY
) >> 6);
165 if (!enabled
|| !(raw_stat
& ~DW_IC_INTR_ACTIVITY
) || !dev
->slave
)
168 stat
= i2c_dw_read_clear_intrbits_slave(dev
);
170 "%#x STATUS SLAVE_ACTIVITY=%#x : RAW_INTR_STAT=%#x : INTR_STAT=%#x\n",
171 enabled
, slave_activity
, raw_stat
, stat
);
173 if (stat
& DW_IC_INTR_RX_FULL
) {
174 if (!(dev
->status
& STATUS_WRITE_IN_PROGRESS
)) {
175 dev
->status
|= STATUS_WRITE_IN_PROGRESS
;
176 dev
->status
&= ~STATUS_READ_IN_PROGRESS
;
177 i2c_slave_event(dev
->slave
, I2C_SLAVE_WRITE_REQUESTED
,
182 regmap_read(dev
->map
, DW_IC_DATA_CMD
, &tmp
);
183 if (tmp
& DW_IC_DATA_CMD_FIRST_DATA_BYTE
)
184 i2c_slave_event(dev
->slave
,
185 I2C_SLAVE_WRITE_REQUESTED
,
188 i2c_slave_event(dev
->slave
, I2C_SLAVE_WRITE_RECEIVED
,
190 regmap_read(dev
->map
, DW_IC_STATUS
, &tmp
);
191 } while (tmp
& DW_IC_STATUS_RFNE
);
194 if (stat
& DW_IC_INTR_RD_REQ
) {
195 if (slave_activity
) {
196 regmap_read(dev
->map
, DW_IC_CLR_RD_REQ
, &tmp
);
198 if (!(dev
->status
& STATUS_READ_IN_PROGRESS
)) {
199 i2c_slave_event(dev
->slave
,
200 I2C_SLAVE_READ_REQUESTED
,
202 dev
->status
|= STATUS_READ_IN_PROGRESS
;
203 dev
->status
&= ~STATUS_WRITE_IN_PROGRESS
;
205 i2c_slave_event(dev
->slave
,
206 I2C_SLAVE_READ_PROCESSED
,
209 regmap_write(dev
->map
, DW_IC_DATA_CMD
, val
);
213 if (stat
& DW_IC_INTR_STOP_DET
)
214 i2c_slave_event(dev
->slave
, I2C_SLAVE_STOP
, &val
);
219 static const struct i2c_algorithm i2c_dw_algo
= {
220 .functionality
= i2c_dw_func
,
221 .reg_slave
= i2c_dw_reg_slave
,
222 .unreg_slave
= i2c_dw_unreg_slave
,
225 void i2c_dw_configure_slave(struct dw_i2c_dev
*dev
)
227 dev
->functionality
= I2C_FUNC_SLAVE
;
229 dev
->slave_cfg
= DW_IC_CON_RX_FIFO_FULL_HLD_CTRL
|
230 DW_IC_CON_RESTART_EN
| DW_IC_CON_STOP_DET_IFADDRESSED
;
232 dev
->mode
= DW_IC_SLAVE
;
234 EXPORT_SYMBOL_GPL(i2c_dw_configure_slave
);
236 int i2c_dw_probe_slave(struct dw_i2c_dev
*dev
)
238 struct i2c_adapter
*adap
= &dev
->adapter
;
241 dev
->init
= i2c_dw_init_slave
;
243 ret
= i2c_dw_init_regmap(dev
);
247 ret
= i2c_dw_set_sda_hold(dev
);
251 ret
= i2c_dw_set_fifo_size(dev
);
255 ret
= dev
->init(dev
);
259 snprintf(adap
->name
, sizeof(adap
->name
),
260 "Synopsys DesignWare I2C Slave adapter");
262 adap
->algo
= &i2c_dw_algo
;
263 adap
->dev
.parent
= dev
->dev
;
264 i2c_set_adapdata(adap
, dev
);
266 ret
= devm_request_irq(dev
->dev
, dev
->irq
, i2c_dw_isr_slave
,
267 IRQF_SHARED
, dev_name(dev
->dev
), dev
);
269 dev_err(dev
->dev
, "failure requesting IRQ %i: %d\n",
274 ret
= i2c_add_numbered_adapter(adap
);
276 dev_err(dev
->dev
, "failure adding adapter: %d\n", ret
);
280 EXPORT_SYMBOL_GPL(i2c_dw_probe_slave
);
282 MODULE_AUTHOR("Luis Oliveira <lolivei@synopsys.com>");
283 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus slave adapter");
284 MODULE_LICENSE("GPL v2");
285 MODULE_IMPORT_NS("I2C_DW_COMMON");