Lines Matching refs:idev
105 altr_i2c_int_enable(struct altr_i2c_dev *idev, u32 mask, bool enable) in altr_i2c_int_enable() argument
110 spin_lock_irqsave(&idev->lock, flags); in altr_i2c_int_enable()
112 int_en = readl(idev->base + ALTR_I2C_ISER); in altr_i2c_int_enable()
114 idev->isr_mask = int_en | mask; in altr_i2c_int_enable()
116 idev->isr_mask = int_en & ~mask; in altr_i2c_int_enable()
118 writel(idev->isr_mask, idev->base + ALTR_I2C_ISER); in altr_i2c_int_enable()
120 spin_unlock_irqrestore(&idev->lock, flags); in altr_i2c_int_enable()
123 static void altr_i2c_int_clear(struct altr_i2c_dev *idev, u32 mask) in altr_i2c_int_clear() argument
125 u32 int_en = readl(idev->base + ALTR_I2C_ISR); in altr_i2c_int_clear()
127 writel(int_en | mask, idev->base + ALTR_I2C_ISR); in altr_i2c_int_clear()
130 static void altr_i2c_core_disable(struct altr_i2c_dev *idev) in altr_i2c_core_disable() argument
132 u32 tmp = readl(idev->base + ALTR_I2C_CTRL); in altr_i2c_core_disable()
134 writel(tmp & ~ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL); in altr_i2c_core_disable()
137 static void altr_i2c_core_enable(struct altr_i2c_dev *idev) in altr_i2c_core_enable() argument
139 u32 tmp = readl(idev->base + ALTR_I2C_CTRL); in altr_i2c_core_enable()
141 writel(tmp | ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL); in altr_i2c_core_enable()
144 static void altr_i2c_reset(struct altr_i2c_dev *idev) in altr_i2c_reset() argument
146 altr_i2c_core_disable(idev); in altr_i2c_reset()
147 altr_i2c_core_enable(idev); in altr_i2c_reset()
150 static inline void altr_i2c_stop(struct altr_i2c_dev *idev) in altr_i2c_stop() argument
152 writel(ALTR_I2C_TFR_CMD_STO, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_stop()
155 static void altr_i2c_init(struct altr_i2c_dev *idev) in altr_i2c_init() argument
157 u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate; in altr_i2c_init()
158 u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000; in altr_i2c_init()
163 if (idev->bus_clk_rate <= 100000) { in altr_i2c_init()
174 writel(tmp, idev->base + ALTR_I2C_CTRL); in altr_i2c_init()
176 dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n", in altr_i2c_init()
177 idev->bus_clk_rate, clk_mhz, divisor); in altr_i2c_init()
180 altr_i2c_reset(idev); in altr_i2c_init()
183 writel(t_high, idev->base + ALTR_I2C_SCL_HIGH); in altr_i2c_init()
185 writel(t_low, idev->base + ALTR_I2C_SCL_LOW); in altr_i2c_init()
187 writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD); in altr_i2c_init()
190 altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false); in altr_i2c_init()
197 static void altr_i2c_transfer(struct altr_i2c_dev *idev, u32 data) in altr_i2c_transfer() argument
200 if (idev->msg_len == 1) in altr_i2c_transfer()
202 if (idev->msg_len > 0) in altr_i2c_transfer()
203 writel(data, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_transfer()
210 static void altr_i2c_empty_rx_fifo(struct altr_i2c_dev *idev) in altr_i2c_empty_rx_fifo() argument
212 size_t rx_fifo_avail = readl(idev->base + ALTR_I2C_RX_FIFO_LVL); in altr_i2c_empty_rx_fifo()
213 int bytes_to_transfer = min(rx_fifo_avail, idev->msg_len); in altr_i2c_empty_rx_fifo()
216 *idev->buf++ = readl(idev->base + ALTR_I2C_RX_DATA); in altr_i2c_empty_rx_fifo()
217 idev->msg_len--; in altr_i2c_empty_rx_fifo()
218 altr_i2c_transfer(idev, 0); in altr_i2c_empty_rx_fifo()
226 static int altr_i2c_fill_tx_fifo(struct altr_i2c_dev *idev) in altr_i2c_fill_tx_fifo() argument
228 size_t tx_fifo_avail = idev->fifo_size - readl(idev->base + in altr_i2c_fill_tx_fifo()
230 int bytes_to_transfer = min(tx_fifo_avail, idev->msg_len); in altr_i2c_fill_tx_fifo()
231 int ret = idev->msg_len - bytes_to_transfer; in altr_i2c_fill_tx_fifo()
234 altr_i2c_transfer(idev, *idev->buf++); in altr_i2c_fill_tx_fifo()
235 idev->msg_len--; in altr_i2c_fill_tx_fifo()
243 struct altr_i2c_dev *idev = _dev; in altr_i2c_isr_quick() local
247 idev->isr_status = readl(idev->base + ALTR_I2C_ISR) & idev->isr_mask; in altr_i2c_isr_quick()
248 if (idev->isr_status) in altr_i2c_isr_quick()
258 struct altr_i2c_dev *idev = _dev; in altr_i2c_isr() local
259 u32 status = idev->isr_status; in altr_i2c_isr()
261 mutex_lock(&idev->isr_mutex); in altr_i2c_isr()
262 if (!idev->msg) { in altr_i2c_isr()
263 dev_warn(idev->dev, "unexpected interrupt\n"); in altr_i2c_isr()
264 altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ); in altr_i2c_isr()
267 read = (idev->msg->flags & I2C_M_RD) != 0; in altr_i2c_isr()
271 altr_i2c_int_clear(idev, ALTR_I2C_ISR_ARB); in altr_i2c_isr()
272 idev->msg_err = -EAGAIN; in altr_i2c_isr()
275 dev_dbg(idev->dev, "Could not get ACK\n"); in altr_i2c_isr()
276 idev->msg_err = -ENXIO; in altr_i2c_isr()
277 altr_i2c_int_clear(idev, ALTR_I2C_ISR_NACK); in altr_i2c_isr()
278 altr_i2c_stop(idev); in altr_i2c_isr()
282 altr_i2c_empty_rx_fifo(idev); in altr_i2c_isr()
283 altr_i2c_int_clear(idev, ALTR_I2C_ISR_RXRDY); in altr_i2c_isr()
284 altr_i2c_stop(idev); in altr_i2c_isr()
285 dev_err(idev->dev, "RX FIFO Overflow\n"); in altr_i2c_isr()
289 altr_i2c_empty_rx_fifo(idev); in altr_i2c_isr()
290 altr_i2c_int_clear(idev, ALTR_I2C_ISR_RXRDY); in altr_i2c_isr()
291 if (!idev->msg_len) in altr_i2c_isr()
295 altr_i2c_int_clear(idev, ALTR_I2C_ISR_TXRDY); in altr_i2c_isr()
296 if (idev->msg_len > 0) in altr_i2c_isr()
297 altr_i2c_fill_tx_fifo(idev); in altr_i2c_isr()
301 dev_warn(idev->dev, "Unexpected interrupt: 0x%x\n", status); in altr_i2c_isr()
302 altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ); in altr_i2c_isr()
307 ret = readl_poll_timeout_atomic(idev->base + ALTR_I2C_STATUS, in altr_i2c_isr()
312 dev_err(idev->dev, "message timeout\n"); in altr_i2c_isr()
313 altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false); in altr_i2c_isr()
314 altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ); in altr_i2c_isr()
315 complete(&idev->msg_complete); in altr_i2c_isr()
316 dev_dbg(idev->dev, "Message Complete\n"); in altr_i2c_isr()
319 mutex_unlock(&idev->isr_mutex); in altr_i2c_isr()
324 static int altr_i2c_xfer_msg(struct altr_i2c_dev *idev, struct i2c_msg *msg) in altr_i2c_xfer_msg() argument
331 mutex_lock(&idev->isr_mutex); in altr_i2c_xfer_msg()
332 idev->msg = msg; in altr_i2c_xfer_msg()
333 idev->msg_len = msg->len; in altr_i2c_xfer_msg()
334 idev->buf = msg->buf; in altr_i2c_xfer_msg()
335 idev->msg_err = 0; in altr_i2c_xfer_msg()
336 reinit_completion(&idev->msg_complete); in altr_i2c_xfer_msg()
337 altr_i2c_core_enable(idev); in altr_i2c_xfer_msg()
341 readl(idev->base + ALTR_I2C_RX_DATA); in altr_i2c_xfer_msg()
342 } while (readl(idev->base + ALTR_I2C_RX_FIFO_LVL)); in altr_i2c_xfer_msg()
344 writel(ALTR_I2C_TFR_CMD_STA | addr, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_xfer_msg()
348 altr_i2c_int_enable(idev, imask, true); in altr_i2c_xfer_msg()
350 altr_i2c_transfer(idev, 0); in altr_i2c_xfer_msg()
353 altr_i2c_int_enable(idev, imask, true); in altr_i2c_xfer_msg()
354 altr_i2c_fill_tx_fifo(idev); in altr_i2c_xfer_msg()
356 mutex_unlock(&idev->isr_mutex); in altr_i2c_xfer_msg()
358 time_left = wait_for_completion_timeout(&idev->msg_complete, in altr_i2c_xfer_msg()
360 altr_i2c_int_enable(idev, imask, false); in altr_i2c_xfer_msg()
362 value = readl(idev->base + ALTR_I2C_STATUS) & ALTR_I2C_STAT_CORE; in altr_i2c_xfer_msg()
364 dev_err(idev->dev, "Core Status not IDLE...\n"); in altr_i2c_xfer_msg()
367 idev->msg_err = -ETIMEDOUT; in altr_i2c_xfer_msg()
368 dev_dbg(idev->dev, "Transaction timed out.\n"); in altr_i2c_xfer_msg()
371 altr_i2c_core_disable(idev); in altr_i2c_xfer_msg()
373 return idev->msg_err; in altr_i2c_xfer_msg()
379 struct altr_i2c_dev *idev = i2c_get_adapdata(adap); in altr_i2c_xfer() local
383 ret = altr_i2c_xfer_msg(idev, msgs++); in altr_i2c_xfer()
402 struct altr_i2c_dev *idev = NULL; in altr_i2c_probe() local
406 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); in altr_i2c_probe()
407 if (!idev) in altr_i2c_probe()
411 idev->base = devm_ioremap_resource(&pdev->dev, res); in altr_i2c_probe()
412 if (IS_ERR(idev->base)) in altr_i2c_probe()
413 return PTR_ERR(idev->base); in altr_i2c_probe()
421 idev->i2c_clk = devm_clk_get(&pdev->dev, NULL); in altr_i2c_probe()
422 if (IS_ERR(idev->i2c_clk)) { in altr_i2c_probe()
424 return PTR_ERR(idev->i2c_clk); in altr_i2c_probe()
427 idev->dev = &pdev->dev; in altr_i2c_probe()
428 init_completion(&idev->msg_complete); in altr_i2c_probe()
429 spin_lock_init(&idev->lock); in altr_i2c_probe()
430 mutex_init(&idev->isr_mutex); in altr_i2c_probe()
432 ret = device_property_read_u32(idev->dev, "fifo-size", in altr_i2c_probe()
433 &idev->fifo_size); in altr_i2c_probe()
437 idev->fifo_size = ALTR_I2C_DFLT_FIFO_SZ; in altr_i2c_probe()
440 ret = device_property_read_u32(idev->dev, "clock-frequency", in altr_i2c_probe()
441 &idev->bus_clk_rate); in altr_i2c_probe()
444 idev->bus_clk_rate = 100000; /* default clock rate */ in altr_i2c_probe()
447 if (idev->bus_clk_rate > 400000) { in altr_i2c_probe()
449 idev->bus_clk_rate); in altr_i2c_probe()
455 pdev->name, idev); in altr_i2c_probe()
461 ret = clk_prepare_enable(idev->i2c_clk); in altr_i2c_probe()
467 altr_i2c_init(idev); in altr_i2c_probe()
469 i2c_set_adapdata(&idev->adapter, idev); in altr_i2c_probe()
470 strlcpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name)); in altr_i2c_probe()
471 idev->adapter.owner = THIS_MODULE; in altr_i2c_probe()
472 idev->adapter.algo = &altr_i2c_algo; in altr_i2c_probe()
473 idev->adapter.dev.parent = &pdev->dev; in altr_i2c_probe()
474 idev->adapter.dev.of_node = pdev->dev.of_node; in altr_i2c_probe()
476 platform_set_drvdata(pdev, idev); in altr_i2c_probe()
478 ret = i2c_add_adapter(&idev->adapter); in altr_i2c_probe()
480 clk_disable_unprepare(idev->i2c_clk); in altr_i2c_probe()
490 struct altr_i2c_dev *idev = platform_get_drvdata(pdev); in altr_i2c_remove() local
492 clk_disable_unprepare(idev->i2c_clk); in altr_i2c_remove()
493 i2c_del_adapter(&idev->adapter); in altr_i2c_remove()