Lines Matching refs:idev
92 altr_i2c_int_enable(struct altr_i2c_dev *idev, u32 mask, bool enable) in altr_i2c_int_enable() argument
96 int_en = readl(idev->base + ALTR_I2C_ISER); in altr_i2c_int_enable()
98 idev->isr_mask = int_en | mask; in altr_i2c_int_enable()
100 idev->isr_mask = int_en & ~mask; in altr_i2c_int_enable()
102 writel(idev->isr_mask, idev->base + ALTR_I2C_ISER); in altr_i2c_int_enable()
105 static void altr_i2c_int_clear(struct altr_i2c_dev *idev, u32 mask) in altr_i2c_int_clear() argument
107 u32 int_en = readl(idev->base + ALTR_I2C_ISR); in altr_i2c_int_clear()
109 writel(int_en | mask, idev->base + ALTR_I2C_ISR); in altr_i2c_int_clear()
112 static void altr_i2c_core_disable(struct altr_i2c_dev *idev) in altr_i2c_core_disable() argument
114 u32 tmp = readl(idev->base + ALTR_I2C_CTRL); in altr_i2c_core_disable()
116 writel(tmp & ~ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL); in altr_i2c_core_disable()
119 static void altr_i2c_core_enable(struct altr_i2c_dev *idev) in altr_i2c_core_enable() argument
121 u32 tmp = readl(idev->base + ALTR_I2C_CTRL); in altr_i2c_core_enable()
123 writel(tmp | ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL); in altr_i2c_core_enable()
126 static void altr_i2c_reset(struct altr_i2c_dev *idev) in altr_i2c_reset() argument
128 altr_i2c_core_disable(idev); in altr_i2c_reset()
129 altr_i2c_core_enable(idev); in altr_i2c_reset()
132 static inline void altr_i2c_stop(struct altr_i2c_dev *idev) in altr_i2c_stop() argument
134 writel(ALTR_I2C_TFR_CMD_STO, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_stop()
137 static void altr_i2c_init(struct altr_i2c_dev *idev) in altr_i2c_init() argument
139 u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate; in altr_i2c_init()
140 u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000; in altr_i2c_init()
145 if (idev->bus_clk_rate <= I2C_MAX_STANDARD_MODE_FREQ) { in altr_i2c_init()
156 writel(tmp, idev->base + ALTR_I2C_CTRL); in altr_i2c_init()
158 dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n", in altr_i2c_init()
159 idev->bus_clk_rate, clk_mhz, divisor); in altr_i2c_init()
162 altr_i2c_reset(idev); in altr_i2c_init()
165 writel(t_high, idev->base + ALTR_I2C_SCL_HIGH); in altr_i2c_init()
167 writel(t_low, idev->base + ALTR_I2C_SCL_LOW); in altr_i2c_init()
169 writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD); in altr_i2c_init()
172 altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false); in altr_i2c_init()
179 static void altr_i2c_transfer(struct altr_i2c_dev *idev, u32 data) in altr_i2c_transfer() argument
182 if (idev->msg_len == 1) in altr_i2c_transfer()
184 if (idev->msg_len > 0) in altr_i2c_transfer()
185 writel(data, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_transfer()
192 static void altr_i2c_empty_rx_fifo(struct altr_i2c_dev *idev) in altr_i2c_empty_rx_fifo() argument
194 size_t rx_fifo_avail = readl(idev->base + ALTR_I2C_RX_FIFO_LVL); in altr_i2c_empty_rx_fifo()
195 int bytes_to_transfer = min(rx_fifo_avail, idev->msg_len); in altr_i2c_empty_rx_fifo()
198 *idev->buf++ = readl(idev->base + ALTR_I2C_RX_DATA); in altr_i2c_empty_rx_fifo()
199 idev->msg_len--; in altr_i2c_empty_rx_fifo()
200 altr_i2c_transfer(idev, 0); in altr_i2c_empty_rx_fifo()
207 static int altr_i2c_fill_tx_fifo(struct altr_i2c_dev *idev) in altr_i2c_fill_tx_fifo() argument
209 size_t tx_fifo_avail = idev->fifo_size - readl(idev->base + in altr_i2c_fill_tx_fifo()
211 int bytes_to_transfer = min(tx_fifo_avail, idev->msg_len); in altr_i2c_fill_tx_fifo()
212 int ret = idev->msg_len - bytes_to_transfer; in altr_i2c_fill_tx_fifo()
215 altr_i2c_transfer(idev, *idev->buf++); in altr_i2c_fill_tx_fifo()
216 idev->msg_len--; in altr_i2c_fill_tx_fifo()
224 struct altr_i2c_dev *idev = _dev; in altr_i2c_isr_quick() local
228 idev->isr_status = readl(idev->base + ALTR_I2C_ISR) & idev->isr_mask; in altr_i2c_isr_quick()
229 if (idev->isr_status) in altr_i2c_isr_quick()
239 struct altr_i2c_dev *idev = _dev; in altr_i2c_isr() local
240 u32 status = idev->isr_status; in altr_i2c_isr()
242 mutex_lock(&idev->isr_mutex); in altr_i2c_isr()
243 if (!idev->msg) { in altr_i2c_isr()
244 dev_warn(idev->dev, "unexpected interrupt\n"); in altr_i2c_isr()
245 altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ); in altr_i2c_isr()
248 read = (idev->msg->flags & I2C_M_RD) != 0; in altr_i2c_isr()
252 altr_i2c_int_clear(idev, ALTR_I2C_ISR_ARB); in altr_i2c_isr()
253 idev->msg_err = -EAGAIN; in altr_i2c_isr()
256 dev_dbg(idev->dev, "Could not get ACK\n"); in altr_i2c_isr()
257 idev->msg_err = -ENXIO; in altr_i2c_isr()
258 altr_i2c_int_clear(idev, ALTR_I2C_ISR_NACK); in altr_i2c_isr()
259 altr_i2c_stop(idev); in altr_i2c_isr()
263 altr_i2c_empty_rx_fifo(idev); in altr_i2c_isr()
264 altr_i2c_int_clear(idev, ALTR_I2C_ISR_RXRDY); in altr_i2c_isr()
265 altr_i2c_stop(idev); in altr_i2c_isr()
266 dev_err(idev->dev, "RX FIFO Overflow\n"); in altr_i2c_isr()
270 altr_i2c_empty_rx_fifo(idev); in altr_i2c_isr()
271 altr_i2c_int_clear(idev, ALTR_I2C_ISR_RXRDY); in altr_i2c_isr()
272 if (!idev->msg_len) in altr_i2c_isr()
276 altr_i2c_int_clear(idev, ALTR_I2C_ISR_TXRDY); in altr_i2c_isr()
277 if (idev->msg_len > 0) in altr_i2c_isr()
278 altr_i2c_fill_tx_fifo(idev); in altr_i2c_isr()
282 dev_warn(idev->dev, "Unexpected interrupt: 0x%x\n", status); in altr_i2c_isr()
283 altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ); in altr_i2c_isr()
288 ret = readl_poll_timeout_atomic(idev->base + ALTR_I2C_STATUS, in altr_i2c_isr()
293 dev_err(idev->dev, "message timeout\n"); in altr_i2c_isr()
294 altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false); in altr_i2c_isr()
295 altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ); in altr_i2c_isr()
296 complete(&idev->msg_complete); in altr_i2c_isr()
297 dev_dbg(idev->dev, "Message Complete\n"); in altr_i2c_isr()
300 mutex_unlock(&idev->isr_mutex); in altr_i2c_isr()
305 static int altr_i2c_xfer_msg(struct altr_i2c_dev *idev, struct i2c_msg *msg) in altr_i2c_xfer_msg() argument
312 mutex_lock(&idev->isr_mutex); in altr_i2c_xfer_msg()
313 idev->msg = msg; in altr_i2c_xfer_msg()
314 idev->msg_len = msg->len; in altr_i2c_xfer_msg()
315 idev->buf = msg->buf; in altr_i2c_xfer_msg()
316 idev->msg_err = 0; in altr_i2c_xfer_msg()
317 reinit_completion(&idev->msg_complete); in altr_i2c_xfer_msg()
318 altr_i2c_core_enable(idev); in altr_i2c_xfer_msg()
322 readl(idev->base + ALTR_I2C_RX_DATA); in altr_i2c_xfer_msg()
323 } while (readl(idev->base + ALTR_I2C_RX_FIFO_LVL)); in altr_i2c_xfer_msg()
325 writel(ALTR_I2C_TFR_CMD_STA | addr, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_xfer_msg()
329 altr_i2c_int_enable(idev, imask, true); in altr_i2c_xfer_msg()
331 altr_i2c_transfer(idev, 0); in altr_i2c_xfer_msg()
334 altr_i2c_int_enable(idev, imask, true); in altr_i2c_xfer_msg()
335 altr_i2c_fill_tx_fifo(idev); in altr_i2c_xfer_msg()
337 mutex_unlock(&idev->isr_mutex); in altr_i2c_xfer_msg()
339 time_left = wait_for_completion_timeout(&idev->msg_complete, in altr_i2c_xfer_msg()
341 mutex_lock(&idev->isr_mutex); in altr_i2c_xfer_msg()
342 altr_i2c_int_enable(idev, imask, false); in altr_i2c_xfer_msg()
344 value = readl(idev->base + ALTR_I2C_STATUS) & ALTR_I2C_STAT_CORE; in altr_i2c_xfer_msg()
346 dev_err(idev->dev, "Core Status not IDLE...\n"); in altr_i2c_xfer_msg()
349 idev->msg_err = -ETIMEDOUT; in altr_i2c_xfer_msg()
350 dev_dbg(idev->dev, "Transaction timed out.\n"); in altr_i2c_xfer_msg()
353 altr_i2c_core_disable(idev); in altr_i2c_xfer_msg()
354 mutex_unlock(&idev->isr_mutex); in altr_i2c_xfer_msg()
356 return idev->msg_err; in altr_i2c_xfer_msg()
362 struct altr_i2c_dev *idev = i2c_get_adapdata(adap); in altr_i2c_xfer() local
366 ret = altr_i2c_xfer_msg(idev, msgs++); in altr_i2c_xfer()
385 struct altr_i2c_dev *idev = NULL; in altr_i2c_probe() local
388 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); in altr_i2c_probe()
389 if (!idev) in altr_i2c_probe()
392 idev->base = devm_platform_ioremap_resource(pdev, 0); in altr_i2c_probe()
393 if (IS_ERR(idev->base)) in altr_i2c_probe()
394 return PTR_ERR(idev->base); in altr_i2c_probe()
400 idev->i2c_clk = devm_clk_get(&pdev->dev, NULL); in altr_i2c_probe()
401 if (IS_ERR(idev->i2c_clk)) { in altr_i2c_probe()
403 return PTR_ERR(idev->i2c_clk); in altr_i2c_probe()
406 idev->dev = &pdev->dev; in altr_i2c_probe()
407 init_completion(&idev->msg_complete); in altr_i2c_probe()
408 mutex_init(&idev->isr_mutex); in altr_i2c_probe()
410 ret = device_property_read_u32(idev->dev, "fifo-size", in altr_i2c_probe()
411 &idev->fifo_size); in altr_i2c_probe()
415 idev->fifo_size = ALTR_I2C_DFLT_FIFO_SZ; in altr_i2c_probe()
418 ret = device_property_read_u32(idev->dev, "clock-frequency", in altr_i2c_probe()
419 &idev->bus_clk_rate); in altr_i2c_probe()
422 idev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ; /* default clock rate */ in altr_i2c_probe()
425 if (idev->bus_clk_rate > I2C_MAX_FAST_MODE_FREQ) { in altr_i2c_probe()
427 idev->bus_clk_rate); in altr_i2c_probe()
433 pdev->name, idev); in altr_i2c_probe()
439 ret = clk_prepare_enable(idev->i2c_clk); in altr_i2c_probe()
445 mutex_lock(&idev->isr_mutex); in altr_i2c_probe()
446 altr_i2c_init(idev); in altr_i2c_probe()
447 mutex_unlock(&idev->isr_mutex); in altr_i2c_probe()
449 i2c_set_adapdata(&idev->adapter, idev); in altr_i2c_probe()
450 strlcpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name)); in altr_i2c_probe()
451 idev->adapter.owner = THIS_MODULE; in altr_i2c_probe()
452 idev->adapter.algo = &altr_i2c_algo; in altr_i2c_probe()
453 idev->adapter.dev.parent = &pdev->dev; in altr_i2c_probe()
454 idev->adapter.dev.of_node = pdev->dev.of_node; in altr_i2c_probe()
456 platform_set_drvdata(pdev, idev); in altr_i2c_probe()
458 ret = i2c_add_adapter(&idev->adapter); in altr_i2c_probe()
460 clk_disable_unprepare(idev->i2c_clk); in altr_i2c_probe()
470 struct altr_i2c_dev *idev = platform_get_drvdata(pdev); in altr_i2c_remove() local
472 clk_disable_unprepare(idev->i2c_clk); in altr_i2c_remove()
473 i2c_del_adapter(&idev->adapter); in altr_i2c_remove()