Lines Matching refs:dev
217 static int flush_i2c_fifo(struct nmk_i2c_dev *dev) in flush_i2c_fifo() argument
230 writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR); in flush_i2c_fifo()
233 timeout = jiffies + dev->adap.timeout; in flush_i2c_fifo()
236 if ((readl(dev->virtbase + I2C_CR) & in flush_i2c_fifo()
242 dev_err(&dev->adev->dev, in flush_i2c_fifo()
253 static void disable_all_interrupts(struct nmk_i2c_dev *dev) in disable_all_interrupts() argument
256 writel(mask, dev->virtbase + I2C_IMSCR); in disable_all_interrupts()
263 static void clear_all_interrupts(struct nmk_i2c_dev *dev) in clear_all_interrupts() argument
267 writel(mask, dev->virtbase + I2C_ICR); in clear_all_interrupts()
274 static int init_hw(struct nmk_i2c_dev *dev) in init_hw() argument
278 stat = flush_i2c_fifo(dev); in init_hw()
283 i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE); in init_hw()
285 disable_all_interrupts(dev); in init_hw()
287 clear_all_interrupts(dev); in init_hw()
289 dev->cli.operation = I2C_NO_OPERATION; in init_hw()
303 static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev, u16 flags) in load_i2c_mcr_reg() argument
308 mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1); in load_i2c_mcr_reg()
319 slave_adr_3msb_bits = (dev->cli.slave_adr >> 7) & 0x7; in load_i2c_mcr_reg()
331 if (dev->cli.operation == I2C_WRITE) in load_i2c_mcr_reg()
337 if (dev->stop) in load_i2c_mcr_reg()
342 mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15); in load_i2c_mcr_reg()
351 static void setup_i2c_controller(struct nmk_i2c_dev *dev) in setup_i2c_controller() argument
358 writel(0x0, dev->virtbase + I2C_CR); in setup_i2c_controller()
359 writel(0x0, dev->virtbase + I2C_HSMCR); in setup_i2c_controller()
360 writel(0x0, dev->virtbase + I2C_TFTR); in setup_i2c_controller()
361 writel(0x0, dev->virtbase + I2C_RFTR); in setup_i2c_controller()
362 writel(0x0, dev->virtbase + I2C_DMAR); in setup_i2c_controller()
364 i2c_clk = clk_get_rate(dev->clk); in setup_i2c_controller()
379 switch (dev->sm) { in setup_i2c_controller()
394 dev_dbg(&dev->adev->dev, "calculated SLSU = %04x\n", slsu); in setup_i2c_controller()
395 writel(slsu << 16, dev->virtbase + I2C_SCR); in setup_i2c_controller()
402 div = (dev->clk_freq > 100000) ? 3 : 2; in setup_i2c_controller()
412 brcr2 = (i2c_clk/(dev->clk_freq * div)) & 0xffff; in setup_i2c_controller()
415 writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR); in setup_i2c_controller()
423 if (dev->sm > I2C_FREQ_MODE_FAST) { in setup_i2c_controller()
424 dev_err(&dev->adev->dev, in setup_i2c_controller()
427 writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR); in setup_i2c_controller()
429 dev->virtbase + I2C_CR); in setup_i2c_controller()
431 writel(dev->sm << 4, dev->virtbase + I2C_CR); in setup_i2c_controller()
434 writel(dev->tft, dev->virtbase + I2C_TFTR); in setup_i2c_controller()
435 writel(dev->rft, dev->virtbase + I2C_RFTR); in setup_i2c_controller()
447 static int read_i2c(struct nmk_i2c_dev *dev, u16 flags) in read_i2c() argument
453 mcr = load_i2c_mcr_reg(dev, flags); in read_i2c()
454 writel(mcr, dev->virtbase + I2C_MCR); in read_i2c()
457 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR, in read_i2c()
458 dev->virtbase + I2C_CR); in read_i2c()
461 i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE); in read_i2c()
463 init_completion(&dev->xfer_complete); in read_i2c()
469 if (dev->stop || !dev->vendor->has_mtdws) in read_i2c()
476 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask, in read_i2c()
477 dev->virtbase + I2C_IMSCR); in read_i2c()
480 &dev->xfer_complete, dev->adap.timeout); in read_i2c()
484 dev_err(&dev->adev->dev, "read from slave 0x%x timed out\n", in read_i2c()
485 dev->cli.slave_adr); in read_i2c()
491 static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes) in fill_tx_fifo() argument
497 (dev->cli.count != 0); in fill_tx_fifo()
500 writeb(*dev->cli.buffer, in fill_tx_fifo()
501 dev->virtbase + I2C_TFR); in fill_tx_fifo()
502 dev->cli.buffer++; in fill_tx_fifo()
503 dev->cli.count--; in fill_tx_fifo()
504 dev->cli.xfer_bytes++; in fill_tx_fifo()
516 static int write_i2c(struct nmk_i2c_dev *dev, u16 flags) in write_i2c() argument
522 mcr = load_i2c_mcr_reg(dev, flags); in write_i2c()
524 writel(mcr, dev->virtbase + I2C_MCR); in write_i2c()
527 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR, in write_i2c()
528 dev->virtbase + I2C_CR); in write_i2c()
531 i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE); in write_i2c()
533 init_completion(&dev->xfer_complete); in write_i2c()
539 fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD); in write_i2c()
541 if (dev->cli.count != 0) in write_i2c()
549 if (dev->stop || !dev->vendor->has_mtdws) in write_i2c()
556 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask, in write_i2c()
557 dev->virtbase + I2C_IMSCR); in write_i2c()
560 &dev->xfer_complete, dev->adap.timeout); in write_i2c()
564 dev_err(&dev->adev->dev, "write to slave 0x%x timed out\n", in write_i2c()
565 dev->cli.slave_adr); in write_i2c()
577 static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags) in nmk_i2c_xfer_one() argument
583 dev->cli.operation = I2C_READ; in nmk_i2c_xfer_one()
584 status = read_i2c(dev, flags); in nmk_i2c_xfer_one()
587 dev->cli.operation = I2C_WRITE; in nmk_i2c_xfer_one()
588 status = write_i2c(dev, flags); in nmk_i2c_xfer_one()
591 if (status || (dev->result)) { in nmk_i2c_xfer_one()
595 i2c_sr = readl(dev->virtbase + I2C_SR); in nmk_i2c_xfer_one()
603 dev_err(&dev->adev->dev, "%s\n", in nmk_i2c_xfer_one()
609 (void) init_hw(dev); in nmk_i2c_xfer_one()
611 status = status ? status : dev->result; in nmk_i2c_xfer_one()
669 struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap); in nmk_i2c_xfer() local
672 pm_runtime_get_sync(&dev->adev->dev); in nmk_i2c_xfer()
677 setup_i2c_controller(dev); in nmk_i2c_xfer()
680 dev->cli.slave_adr = msgs[i].addr; in nmk_i2c_xfer()
681 dev->cli.buffer = msgs[i].buf; in nmk_i2c_xfer()
682 dev->cli.count = msgs[i].len; in nmk_i2c_xfer()
683 dev->stop = (i < (num_msgs - 1)) ? 0 : 1; in nmk_i2c_xfer()
684 dev->result = 0; in nmk_i2c_xfer()
686 status = nmk_i2c_xfer_one(dev, msgs[i].flags); in nmk_i2c_xfer()
694 pm_runtime_put_sync(&dev->adev->dev); in nmk_i2c_xfer()
708 static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq) in disable_interrupts() argument
711 writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq), in disable_interrupts()
712 dev->virtbase + I2C_IMSCR); in disable_interrupts()
729 struct nmk_i2c_dev *dev = arg; in i2c_irq_handler() local
735 tft = readl(dev->virtbase + I2C_TFTR); in i2c_irq_handler()
736 rft = readl(dev->virtbase + I2C_RFTR); in i2c_irq_handler()
739 misr = readl(dev->virtbase + I2C_MISR); in i2c_irq_handler()
747 if (dev->cli.operation == I2C_READ) { in i2c_irq_handler()
752 disable_interrupts(dev, I2C_IT_TXFNE); in i2c_irq_handler()
754 fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft)); in i2c_irq_handler()
759 if (dev->cli.count == 0) in i2c_irq_handler()
760 disable_interrupts(dev, I2C_IT_TXFNE); in i2c_irq_handler()
774 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR); in i2c_irq_handler()
775 dev->cli.buffer++; in i2c_irq_handler()
777 dev->cli.count -= rft; in i2c_irq_handler()
778 dev->cli.xfer_bytes += rft; in i2c_irq_handler()
784 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR); in i2c_irq_handler()
785 dev->cli.buffer++; in i2c_irq_handler()
787 dev->cli.count -= MAX_I2C_FIFO_THRESHOLD; in i2c_irq_handler()
788 dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD; in i2c_irq_handler()
794 if (dev->cli.operation == I2C_READ) { in i2c_irq_handler()
795 while (!(readl(dev->virtbase + I2C_RISR) in i2c_irq_handler()
797 if (dev->cli.count == 0) in i2c_irq_handler()
799 *dev->cli.buffer = in i2c_irq_handler()
800 readb(dev->virtbase + I2C_RFR); in i2c_irq_handler()
801 dev->cli.buffer++; in i2c_irq_handler()
802 dev->cli.count--; in i2c_irq_handler()
803 dev->cli.xfer_bytes++; in i2c_irq_handler()
807 disable_all_interrupts(dev); in i2c_irq_handler()
808 clear_all_interrupts(dev); in i2c_irq_handler()
810 if (dev->cli.count) { in i2c_irq_handler()
811 dev->result = -EIO; in i2c_irq_handler()
812 dev_err(&dev->adev->dev, in i2c_irq_handler()
814 dev->cli.count); in i2c_irq_handler()
815 (void) init_hw(dev); in i2c_irq_handler()
817 complete(&dev->xfer_complete); in i2c_irq_handler()
823 dev->result = -EIO; in i2c_irq_handler()
824 (void) init_hw(dev); in i2c_irq_handler()
826 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL); in i2c_irq_handler()
827 complete(&dev->xfer_complete); in i2c_irq_handler()
837 dev->result = -EIO; in i2c_irq_handler()
839 if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT) in i2c_irq_handler()
840 (void) init_hw(dev); in i2c_irq_handler()
842 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR); in i2c_irq_handler()
843 complete(&dev->xfer_complete); in i2c_irq_handler()
853 dev->result = -EIO; in i2c_irq_handler()
854 (void) init_hw(dev); in i2c_irq_handler()
856 dev_err(&dev->adev->dev, "Tx Fifo Over run\n"); in i2c_irq_handler()
857 complete(&dev->xfer_complete); in i2c_irq_handler()
869 dev_err(&dev->adev->dev, "unhandled Interrupt\n"); in i2c_irq_handler()
872 dev_err(&dev->adev->dev, "spurious Interrupt..\n"); in i2c_irq_handler()
880 static int nmk_i2c_suspend_late(struct device *dev) in nmk_i2c_suspend_late() argument
884 ret = pm_runtime_force_suspend(dev); in nmk_i2c_suspend_late()
888 pinctrl_pm_select_sleep_state(dev); in nmk_i2c_suspend_late()
892 static int nmk_i2c_resume_early(struct device *dev) in nmk_i2c_resume_early() argument
894 return pm_runtime_force_resume(dev); in nmk_i2c_resume_early()
899 static int nmk_i2c_runtime_suspend(struct device *dev) in nmk_i2c_runtime_suspend() argument
901 struct amba_device *adev = to_amba_device(dev); in nmk_i2c_runtime_suspend()
905 pinctrl_pm_select_idle_state(dev); in nmk_i2c_runtime_suspend()
909 static int nmk_i2c_runtime_resume(struct device *dev) in nmk_i2c_runtime_resume() argument
911 struct amba_device *adev = to_amba_device(dev); in nmk_i2c_runtime_resume()
917 dev_err(dev, "can't prepare_enable clock\n"); in nmk_i2c_runtime_resume()
921 pinctrl_pm_select_default_state(dev); in nmk_i2c_runtime_resume()
926 pinctrl_pm_select_idle_state(dev); in nmk_i2c_runtime_resume()
970 struct device_node *np = adev->dev.of_node; in nmk_i2c_probe()
971 struct nmk_i2c_dev *dev; in nmk_i2c_probe() local
976 dev = devm_kzalloc(&adev->dev, sizeof(struct nmk_i2c_dev), GFP_KERNEL); in nmk_i2c_probe()
977 if (!dev) { in nmk_i2c_probe()
978 dev_err(&adev->dev, "cannot allocate memory\n"); in nmk_i2c_probe()
982 dev->vendor = vendor; in nmk_i2c_probe()
983 dev->adev = adev; in nmk_i2c_probe()
984 nmk_i2c_of_probe(np, dev); in nmk_i2c_probe()
986 if (dev->tft > max_fifo_threshold) { in nmk_i2c_probe()
987 dev_warn(&adev->dev, "requested TX FIFO threshold %u, adjusted down to %u\n", in nmk_i2c_probe()
988 dev->tft, max_fifo_threshold); in nmk_i2c_probe()
989 dev->tft = max_fifo_threshold; in nmk_i2c_probe()
992 if (dev->rft > max_fifo_threshold) { in nmk_i2c_probe()
993 dev_warn(&adev->dev, "requested RX FIFO threshold %u, adjusted down to %u\n", in nmk_i2c_probe()
994 dev->rft, max_fifo_threshold); in nmk_i2c_probe()
995 dev->rft = max_fifo_threshold; in nmk_i2c_probe()
998 amba_set_drvdata(adev, dev); in nmk_i2c_probe()
1000 dev->virtbase = devm_ioremap(&adev->dev, adev->res.start, in nmk_i2c_probe()
1002 if (!dev->virtbase) { in nmk_i2c_probe()
1007 dev->irq = adev->irq[0]; in nmk_i2c_probe()
1008 ret = devm_request_irq(&adev->dev, dev->irq, i2c_irq_handler, 0, in nmk_i2c_probe()
1009 DRIVER_NAME, dev); in nmk_i2c_probe()
1011 dev_err(&adev->dev, "cannot claim the irq %d\n", dev->irq); in nmk_i2c_probe()
1015 dev->clk = devm_clk_get(&adev->dev, NULL); in nmk_i2c_probe()
1016 if (IS_ERR(dev->clk)) { in nmk_i2c_probe()
1017 dev_err(&adev->dev, "could not get i2c clock\n"); in nmk_i2c_probe()
1018 ret = PTR_ERR(dev->clk); in nmk_i2c_probe()
1022 ret = clk_prepare_enable(dev->clk); in nmk_i2c_probe()
1024 dev_err(&adev->dev, "can't prepare_enable clock\n"); in nmk_i2c_probe()
1028 init_hw(dev); in nmk_i2c_probe()
1030 adap = &dev->adap; in nmk_i2c_probe()
1031 adap->dev.of_node = np; in nmk_i2c_probe()
1032 adap->dev.parent = &adev->dev; in nmk_i2c_probe()
1036 adap->timeout = msecs_to_jiffies(dev->timeout); in nmk_i2c_probe()
1040 i2c_set_adapdata(adap, dev); in nmk_i2c_probe()
1042 dev_info(&adev->dev, in nmk_i2c_probe()
1044 adap->name, dev->virtbase); in nmk_i2c_probe()
1050 pm_runtime_put(&adev->dev); in nmk_i2c_probe()
1055 clk_disable_unprepare(dev->clk); in nmk_i2c_probe()
1064 struct nmk_i2c_dev *dev = amba_get_drvdata(adev); in nmk_i2c_remove() local
1066 i2c_del_adapter(&dev->adap); in nmk_i2c_remove()
1067 flush_i2c_fifo(dev); in nmk_i2c_remove()
1068 disable_all_interrupts(dev); in nmk_i2c_remove()
1069 clear_all_interrupts(dev); in nmk_i2c_remove()
1071 i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE); in nmk_i2c_remove()
1072 clk_disable_unprepare(dev->clk); in nmk_i2c_remove()