Lines Matching refs:i2c_dev
423 static void stm32f7_i2c_disable_irq(struct stm32f7_i2c_dev *i2c_dev, u32 mask) in stm32f7_i2c_disable_irq() argument
425 stm32f7_i2c_clr_bits(i2c_dev->base + STM32F7_I2C_CR1, mask); in stm32f7_i2c_disable_irq()
440 static int stm32f7_i2c_compute_timing(struct stm32f7_i2c_dev *i2c_dev, in stm32f7_i2c_compute_timing() argument
464 dev_err(i2c_dev->dev, "speed out of bound {%d}\n", in stm32f7_i2c_compute_timing()
471 dev_err(i2c_dev->dev, in stm32f7_i2c_compute_timing()
478 i2c_dev->dnf = DIV_ROUND_CLOSEST(i2c_dev->dnf_dt, i2cclk); in stm32f7_i2c_compute_timing()
479 if (i2c_dev->dnf > STM32F7_I2C_DNF_MAX) { in stm32f7_i2c_compute_timing()
480 dev_err(i2c_dev->dev, in stm32f7_i2c_compute_timing()
482 i2c_dev->dnf * i2cclk, STM32F7_I2C_DNF_MAX * i2cclk); in stm32f7_i2c_compute_timing()
488 (i2c_dev->analog_filter ? in stm32f7_i2c_compute_timing()
491 (i2c_dev->analog_filter ? in stm32f7_i2c_compute_timing()
493 dnf_delay = i2c_dev->dnf * i2cclk; in stm32f7_i2c_compute_timing()
496 af_delay_min - (i2c_dev->dnf + 3) * i2cclk; in stm32f7_i2c_compute_timing()
499 af_delay_max - (i2c_dev->dnf + 4) * i2cclk; in stm32f7_i2c_compute_timing()
508 dev_dbg(i2c_dev->dev, "SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n", in stm32f7_i2c_compute_timing()
549 dev_err(i2c_dev->dev, "no Prescaler solution\n"); in stm32f7_i2c_compute_timing()
606 dev_err(i2c_dev->dev, "no solution at all\n"); in stm32f7_i2c_compute_timing()
617 dev_dbg(i2c_dev->dev, in stm32f7_i2c_compute_timing()
644 static int stm32f7_i2c_setup_timing(struct stm32f7_i2c_dev *i2c_dev, in stm32f7_i2c_setup_timing() argument
651 t->scl_rise_ns = i2c_dev->setup.rise_time; in stm32f7_i2c_setup_timing()
652 t->scl_fall_ns = i2c_dev->setup.fall_time; in stm32f7_i2c_setup_timing()
654 i2c_parse_fw_timings(i2c_dev->dev, t, false); in stm32f7_i2c_setup_timing()
657 dev_err(i2c_dev->dev, "Invalid bus speed (%i>%i)\n", in stm32f7_i2c_setup_timing()
663 i2c_dev->setup.rise_time = t->scl_rise_ns; in stm32f7_i2c_setup_timing()
664 i2c_dev->setup.fall_time = t->scl_fall_ns; in stm32f7_i2c_setup_timing()
665 i2c_dev->dnf_dt = t->digital_filter_width_ns; in stm32f7_i2c_setup_timing()
666 setup->clock_src = clk_get_rate(i2c_dev->clk); in stm32f7_i2c_setup_timing()
669 dev_err(i2c_dev->dev, "clock rate is 0\n"); in stm32f7_i2c_setup_timing()
673 if (!of_property_read_bool(i2c_dev->dev->of_node, "i2c-digital-filter")) in stm32f7_i2c_setup_timing()
674 i2c_dev->dnf_dt = STM32F7_I2C_DNF_DEFAULT; in stm32f7_i2c_setup_timing()
677 ret = stm32f7_i2c_compute_timing(i2c_dev, setup, in stm32f7_i2c_setup_timing()
678 &i2c_dev->timing); in stm32f7_i2c_setup_timing()
680 dev_err(i2c_dev->dev, in stm32f7_i2c_setup_timing()
686 dev_warn(i2c_dev->dev, in stm32f7_i2c_setup_timing()
693 dev_err(i2c_dev->dev, "Impossible to compute I2C timings.\n"); in stm32f7_i2c_setup_timing()
697 i2c_dev->analog_filter = of_property_read_bool(i2c_dev->dev->of_node, in stm32f7_i2c_setup_timing()
700 dev_dbg(i2c_dev->dev, "I2C Speed(%i), Clk Source(%i)\n", in stm32f7_i2c_setup_timing()
702 dev_dbg(i2c_dev->dev, "I2C Rise(%i) and Fall(%i) Time\n", in stm32f7_i2c_setup_timing()
704 dev_dbg(i2c_dev->dev, "I2C Analog Filter(%s), DNF(%i)\n", in stm32f7_i2c_setup_timing()
705 (i2c_dev->analog_filter ? "On" : "Off"), i2c_dev->dnf); in stm32f7_i2c_setup_timing()
707 i2c_dev->bus_rate = setup->speed_freq; in stm32f7_i2c_setup_timing()
712 static void stm32f7_i2c_disable_dma_req(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_disable_dma_req() argument
714 void __iomem *base = i2c_dev->base; in stm32f7_i2c_disable_dma_req()
722 struct stm32f7_i2c_dev *i2c_dev = (struct stm32f7_i2c_dev *)arg; in stm32f7_i2c_dma_callback() local
723 struct stm32_i2c_dma *dma = i2c_dev->dma; in stm32f7_i2c_dma_callback()
726 stm32f7_i2c_disable_dma_req(i2c_dev); in stm32f7_i2c_dma_callback()
731 static void stm32f7_i2c_hw_config(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_hw_config() argument
733 struct stm32f7_i2c_timings *t = &i2c_dev->timing; in stm32f7_i2c_hw_config()
742 writel_relaxed(timing, i2c_dev->base + STM32F7_I2C_TIMINGR); in stm32f7_i2c_hw_config()
745 if (i2c_dev->analog_filter) in stm32f7_i2c_hw_config()
746 stm32f7_i2c_clr_bits(i2c_dev->base + STM32F7_I2C_CR1, in stm32f7_i2c_hw_config()
749 stm32f7_i2c_set_bits(i2c_dev->base + STM32F7_I2C_CR1, in stm32f7_i2c_hw_config()
753 stm32f7_i2c_clr_bits(i2c_dev->base + STM32F7_I2C_CR1, in stm32f7_i2c_hw_config()
755 stm32f7_i2c_set_bits(i2c_dev->base + STM32F7_I2C_CR1, in stm32f7_i2c_hw_config()
756 STM32F7_I2C_CR1_DNF(i2c_dev->dnf)); in stm32f7_i2c_hw_config()
758 stm32f7_i2c_set_bits(i2c_dev->base + STM32F7_I2C_CR1, in stm32f7_i2c_hw_config()
762 static void stm32f7_i2c_write_tx_data(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_write_tx_data() argument
764 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_write_tx_data()
765 void __iomem *base = i2c_dev->base; in stm32f7_i2c_write_tx_data()
773 static void stm32f7_i2c_read_rx_data(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_read_rx_data() argument
775 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_read_rx_data()
776 void __iomem *base = i2c_dev->base; in stm32f7_i2c_read_rx_data()
787 static void stm32f7_i2c_reload(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_reload() argument
789 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_reload()
792 if (i2c_dev->use_dma) in stm32f7_i2c_reload()
795 cr2 = readl_relaxed(i2c_dev->base + STM32F7_I2C_CR2); in stm32f7_i2c_reload()
805 writel_relaxed(cr2, i2c_dev->base + STM32F7_I2C_CR2); in stm32f7_i2c_reload()
808 static void stm32f7_i2c_smbus_reload(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_smbus_reload() argument
810 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_smbus_reload()
818 stm32f7_i2c_read_rx_data(i2c_dev); in stm32f7_i2c_smbus_reload()
825 cr2 = readl_relaxed(i2c_dev->base + STM32F7_I2C_CR2); in stm32f7_i2c_smbus_reload()
828 writel_relaxed(cr2, i2c_dev->base + STM32F7_I2C_CR2); in stm32f7_i2c_smbus_reload()
833 struct stm32f7_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in stm32f7_i2c_release_bus() local
835 dev_info(i2c_dev->dev, "Trying to recover bus\n"); in stm32f7_i2c_release_bus()
837 stm32f7_i2c_clr_bits(i2c_dev->base + STM32F7_I2C_CR1, in stm32f7_i2c_release_bus()
840 stm32f7_i2c_hw_config(i2c_dev); in stm32f7_i2c_release_bus()
845 static int stm32f7_i2c_wait_free_bus(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_wait_free_bus() argument
850 ret = readl_relaxed_poll_timeout(i2c_dev->base + STM32F7_I2C_ISR, in stm32f7_i2c_wait_free_bus()
857 dev_info(i2c_dev->dev, "bus busy\n"); in stm32f7_i2c_wait_free_bus()
859 ret = stm32f7_i2c_release_bus(&i2c_dev->adap); in stm32f7_i2c_wait_free_bus()
861 dev_err(i2c_dev->dev, "Failed to recover the bus (%d)\n", ret); in stm32f7_i2c_wait_free_bus()
868 static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev, in stm32f7_i2c_xfer_msg() argument
871 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_xfer_msg()
872 void __iomem *base = i2c_dev->base; in stm32f7_i2c_xfer_msg()
880 f7_msg->stop = (i2c_dev->msg_id >= i2c_dev->msg_num - 1); in stm32f7_i2c_xfer_msg()
882 reinit_completion(&i2c_dev->complete); in stm32f7_i2c_xfer_msg()
921 i2c_dev->use_dma = false; in stm32f7_i2c_xfer_msg()
922 if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN) { in stm32f7_i2c_xfer_msg()
923 ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma, in stm32f7_i2c_xfer_msg()
927 i2c_dev); in stm32f7_i2c_xfer_msg()
929 i2c_dev->use_dma = true; in stm32f7_i2c_xfer_msg()
931 dev_warn(i2c_dev->dev, "can't use DMA\n"); in stm32f7_i2c_xfer_msg()
934 if (!i2c_dev->use_dma) { in stm32f7_i2c_xfer_msg()
949 i2c_dev->master_mode = true; in stm32f7_i2c_xfer_msg()
956 static int stm32f7_i2c_smbus_xfer_msg(struct stm32f7_i2c_dev *i2c_dev, in stm32f7_i2c_smbus_xfer_msg() argument
960 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_smbus_xfer_msg()
961 struct device *dev = i2c_dev->dev; in stm32f7_i2c_smbus_xfer_msg()
962 void __iomem *base = i2c_dev->base; in stm32f7_i2c_smbus_xfer_msg()
967 reinit_completion(&i2c_dev->complete); in stm32f7_i2c_smbus_xfer_msg()
1087 i2c_dev->use_dma = false; in stm32f7_i2c_smbus_xfer_msg()
1088 if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN) { in stm32f7_i2c_smbus_xfer_msg()
1089 ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma, in stm32f7_i2c_smbus_xfer_msg()
1093 i2c_dev); in stm32f7_i2c_smbus_xfer_msg()
1095 i2c_dev->use_dma = true; in stm32f7_i2c_smbus_xfer_msg()
1097 dev_warn(i2c_dev->dev, "can't use DMA\n"); in stm32f7_i2c_smbus_xfer_msg()
1100 if (!i2c_dev->use_dma) { in stm32f7_i2c_smbus_xfer_msg()
1115 i2c_dev->master_mode = true; in stm32f7_i2c_smbus_xfer_msg()
1124 static void stm32f7_i2c_smbus_rep_start(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_smbus_rep_start() argument
1126 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_smbus_rep_start()
1127 void __iomem *base = i2c_dev->base; in stm32f7_i2c_smbus_rep_start()
1177 i2c_dev->use_dma = false; in stm32f7_i2c_smbus_rep_start()
1178 if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN && in stm32f7_i2c_smbus_rep_start()
1181 ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma, in stm32f7_i2c_smbus_rep_start()
1185 i2c_dev); in stm32f7_i2c_smbus_rep_start()
1188 i2c_dev->use_dma = true; in stm32f7_i2c_smbus_rep_start()
1190 dev_warn(i2c_dev->dev, "can't use DMA\n"); in stm32f7_i2c_smbus_rep_start()
1193 if (!i2c_dev->use_dma) in stm32f7_i2c_smbus_rep_start()
1206 static int stm32f7_i2c_smbus_check_pec(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_smbus_check_pec() argument
1208 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_smbus_check_pec()
1211 internal_pec = readl_relaxed(i2c_dev->base + STM32F7_I2C_PECR); in stm32f7_i2c_smbus_check_pec()
1228 dev_err(i2c_dev->dev, "Unsupported smbus protocol for PEC\n"); in stm32f7_i2c_smbus_check_pec()
1233 dev_err(i2c_dev->dev, "Bad PEC 0x%02x vs. 0x%02x\n", in stm32f7_i2c_smbus_check_pec()
1267 static void stm32f7_i2c_slave_start(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_slave_start() argument
1269 struct i2c_client *slave = i2c_dev->slave_running; in stm32f7_i2c_slave_start()
1270 void __iomem *base = i2c_dev->base; in stm32f7_i2c_slave_start()
1274 if (i2c_dev->slave_dir) { in stm32f7_i2c_slave_start()
1315 static void stm32f7_i2c_slave_addr(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_slave_addr() argument
1317 void __iomem *base = i2c_dev->base; in stm32f7_i2c_slave_addr()
1321 isr = readl_relaxed(i2c_dev->base + STM32F7_I2C_ISR); in stm32f7_i2c_slave_addr()
1326 if (stm32f7_i2c_is_addr_match(i2c_dev->slave[i], addcode)) { in stm32f7_i2c_slave_addr()
1327 i2c_dev->slave_running = i2c_dev->slave[i]; in stm32f7_i2c_slave_addr()
1328 i2c_dev->slave_dir = dir; in stm32f7_i2c_slave_addr()
1331 stm32f7_i2c_slave_start(i2c_dev); in stm32f7_i2c_slave_addr()
1341 static int stm32f7_i2c_get_slave_id(struct stm32f7_i2c_dev *i2c_dev, in stm32f7_i2c_get_slave_id() argument
1347 if (i2c_dev->slave[i] == slave) { in stm32f7_i2c_get_slave_id()
1353 dev_err(i2c_dev->dev, "Slave 0x%x not registered\n", slave->addr); in stm32f7_i2c_get_slave_id()
1358 static int stm32f7_i2c_get_free_slave_id(struct stm32f7_i2c_dev *i2c_dev, in stm32f7_i2c_get_free_slave_id() argument
1361 struct device *dev = i2c_dev->dev; in stm32f7_i2c_get_free_slave_id()
1369 if (i2c_dev->smbus_mode && (slave->addr == 0x08)) { in stm32f7_i2c_get_free_slave_id()
1370 if (i2c_dev->slave[STM32F7_SLAVE_HOSTNOTIFY]) in stm32f7_i2c_get_free_slave_id()
1380 if (!i2c_dev->slave[i]) { in stm32f7_i2c_get_free_slave_id()
1392 static bool stm32f7_i2c_is_slave_registered(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_is_slave_registered() argument
1397 if (i2c_dev->slave[i]) in stm32f7_i2c_is_slave_registered()
1404 static bool stm32f7_i2c_is_slave_busy(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_is_slave_busy() argument
1410 if (i2c_dev->slave[i]) in stm32f7_i2c_is_slave_busy()
1417 static irqreturn_t stm32f7_i2c_slave_isr_event(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_slave_isr_event() argument
1419 void __iomem *base = i2c_dev->base; in stm32f7_i2c_slave_isr_event()
1424 status = readl_relaxed(i2c_dev->base + STM32F7_I2C_ISR); in stm32f7_i2c_slave_isr_event()
1428 i2c_slave_event(i2c_dev->slave_running, in stm32f7_i2c_slave_isr_event()
1442 val = readb_relaxed(i2c_dev->base + STM32F7_I2C_RXDR); in stm32f7_i2c_slave_isr_event()
1443 ret = i2c_slave_event(i2c_dev->slave_running, in stm32f7_i2c_slave_isr_event()
1447 cr2 = readl_relaxed(i2c_dev->base + STM32F7_I2C_CR2); in stm32f7_i2c_slave_isr_event()
1449 writel_relaxed(cr2, i2c_dev->base + STM32F7_I2C_CR2); in stm32f7_i2c_slave_isr_event()
1458 dev_dbg(i2c_dev->dev, "<%s>: Receive NACK\n", __func__); in stm32f7_i2c_slave_isr_event()
1465 stm32f7_i2c_disable_irq(i2c_dev, STM32F7_I2C_XFER_IRQ_MASK); in stm32f7_i2c_slave_isr_event()
1467 if (i2c_dev->slave_dir) { in stm32f7_i2c_slave_isr_event()
1480 i2c_slave_event(i2c_dev->slave_running, I2C_SLAVE_STOP, &val); in stm32f7_i2c_slave_isr_event()
1482 i2c_dev->slave_running = NULL; in stm32f7_i2c_slave_isr_event()
1487 stm32f7_i2c_slave_addr(i2c_dev); in stm32f7_i2c_slave_isr_event()
1494 struct stm32f7_i2c_dev *i2c_dev = data; in stm32f7_i2c_isr_event() local
1495 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_isr_event()
1496 void __iomem *base = i2c_dev->base; in stm32f7_i2c_isr_event()
1501 if (!i2c_dev->master_mode) { in stm32f7_i2c_isr_event()
1502 ret = stm32f7_i2c_slave_isr_event(i2c_dev); in stm32f7_i2c_isr_event()
1506 status = readl_relaxed(i2c_dev->base + STM32F7_I2C_ISR); in stm32f7_i2c_isr_event()
1510 stm32f7_i2c_write_tx_data(i2c_dev); in stm32f7_i2c_isr_event()
1514 stm32f7_i2c_read_rx_data(i2c_dev); in stm32f7_i2c_isr_event()
1518 dev_dbg(i2c_dev->dev, "<%s>: Receive NACK (addr %x)\n", in stm32f7_i2c_isr_event()
1527 if (stm32f7_i2c_is_slave_registered(i2c_dev)) in stm32f7_i2c_isr_event()
1531 stm32f7_i2c_disable_irq(i2c_dev, mask); in stm32f7_i2c_isr_event()
1536 if (i2c_dev->use_dma) { in stm32f7_i2c_isr_event()
1539 i2c_dev->master_mode = false; in stm32f7_i2c_isr_event()
1540 complete(&i2c_dev->complete); in stm32f7_i2c_isr_event()
1549 } else if (i2c_dev->use_dma) { in stm32f7_i2c_isr_event()
1552 stm32f7_i2c_smbus_rep_start(i2c_dev); in stm32f7_i2c_isr_event()
1554 i2c_dev->msg_id++; in stm32f7_i2c_isr_event()
1555 i2c_dev->msg++; in stm32f7_i2c_isr_event()
1556 stm32f7_i2c_xfer_msg(i2c_dev, i2c_dev->msg); in stm32f7_i2c_isr_event()
1562 stm32f7_i2c_smbus_reload(i2c_dev); in stm32f7_i2c_isr_event()
1564 stm32f7_i2c_reload(i2c_dev); in stm32f7_i2c_isr_event()
1572 struct stm32f7_i2c_dev *i2c_dev = data; in stm32f7_i2c_isr_event_thread() local
1573 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_isr_event_thread()
1574 struct stm32_i2c_dma *dma = i2c_dev->dma; in stm32f7_i2c_isr_event_thread()
1582 ret = wait_for_completion_timeout(&i2c_dev->dma->dma_complete, HZ); in stm32f7_i2c_isr_event_thread()
1584 dev_dbg(i2c_dev->dev, "<%s>: Timed out\n", __func__); in stm32f7_i2c_isr_event_thread()
1585 stm32f7_i2c_disable_dma_req(i2c_dev); in stm32f7_i2c_isr_event_thread()
1590 status = readl_relaxed(i2c_dev->base + STM32F7_I2C_ISR); in stm32f7_i2c_isr_event_thread()
1594 stm32f7_i2c_smbus_rep_start(i2c_dev); in stm32f7_i2c_isr_event_thread()
1596 i2c_dev->msg_id++; in stm32f7_i2c_isr_event_thread()
1597 i2c_dev->msg++; in stm32f7_i2c_isr_event_thread()
1598 stm32f7_i2c_xfer_msg(i2c_dev, i2c_dev->msg); in stm32f7_i2c_isr_event_thread()
1601 i2c_dev->master_mode = false; in stm32f7_i2c_isr_event_thread()
1602 complete(&i2c_dev->complete); in stm32f7_i2c_isr_event_thread()
1610 struct stm32f7_i2c_dev *i2c_dev = data; in stm32f7_i2c_isr_error() local
1611 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_isr_error()
1612 void __iomem *base = i2c_dev->base; in stm32f7_i2c_isr_error()
1613 struct device *dev = i2c_dev->dev; in stm32f7_i2c_isr_error()
1614 struct stm32_i2c_dma *dma = i2c_dev->dma; in stm32f7_i2c_isr_error()
1617 status = readl_relaxed(i2c_dev->base + STM32F7_I2C_ISR); in stm32f7_i2c_isr_error()
1624 stm32f7_i2c_release_bus(&i2c_dev->adap); in stm32f7_i2c_isr_error()
1646 i2c_handle_smbus_alert(i2c_dev->alert->ara); in stm32f7_i2c_isr_error()
1650 if (!i2c_dev->slave_running) { in stm32f7_i2c_isr_error()
1653 if (stm32f7_i2c_is_slave_registered(i2c_dev)) in stm32f7_i2c_isr_error()
1657 stm32f7_i2c_disable_irq(i2c_dev, mask); in stm32f7_i2c_isr_error()
1661 if (i2c_dev->use_dma) { in stm32f7_i2c_isr_error()
1662 stm32f7_i2c_disable_dma_req(i2c_dev); in stm32f7_i2c_isr_error()
1666 i2c_dev->master_mode = false; in stm32f7_i2c_isr_error()
1667 complete(&i2c_dev->complete); in stm32f7_i2c_isr_error()
1675 struct stm32f7_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); in stm32f7_i2c_xfer() local
1676 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_xfer()
1677 struct stm32_i2c_dma *dma = i2c_dev->dma; in stm32f7_i2c_xfer()
1681 i2c_dev->msg = msgs; in stm32f7_i2c_xfer()
1682 i2c_dev->msg_num = num; in stm32f7_i2c_xfer()
1683 i2c_dev->msg_id = 0; in stm32f7_i2c_xfer()
1686 ret = pm_runtime_resume_and_get(i2c_dev->dev); in stm32f7_i2c_xfer()
1690 ret = stm32f7_i2c_wait_free_bus(i2c_dev); in stm32f7_i2c_xfer()
1694 stm32f7_i2c_xfer_msg(i2c_dev, msgs); in stm32f7_i2c_xfer()
1696 time_left = wait_for_completion_timeout(&i2c_dev->complete, in stm32f7_i2c_xfer()
1697 i2c_dev->adap.timeout); in stm32f7_i2c_xfer()
1701 dev_dbg(i2c_dev->dev, "Access to slave 0x%x timed out\n", in stm32f7_i2c_xfer()
1702 i2c_dev->msg->addr); in stm32f7_i2c_xfer()
1703 if (i2c_dev->use_dma) in stm32f7_i2c_xfer()
1709 pm_runtime_mark_last_busy(i2c_dev->dev); in stm32f7_i2c_xfer()
1710 pm_runtime_put_autosuspend(i2c_dev->dev); in stm32f7_i2c_xfer()
1720 struct stm32f7_i2c_dev *i2c_dev = i2c_get_adapdata(adapter); in stm32f7_i2c_smbus_xfer() local
1721 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; in stm32f7_i2c_smbus_xfer()
1722 struct stm32_i2c_dma *dma = i2c_dev->dma; in stm32f7_i2c_smbus_xfer()
1723 struct device *dev = i2c_dev->dev; in stm32f7_i2c_smbus_xfer()
1736 ret = stm32f7_i2c_wait_free_bus(i2c_dev); in stm32f7_i2c_smbus_xfer()
1740 ret = stm32f7_i2c_smbus_xfer_msg(i2c_dev, flags, command, data); in stm32f7_i2c_smbus_xfer()
1744 timeout = wait_for_completion_timeout(&i2c_dev->complete, in stm32f7_i2c_smbus_xfer()
1745 i2c_dev->adap.timeout); in stm32f7_i2c_smbus_xfer()
1752 if (i2c_dev->use_dma) in stm32f7_i2c_smbus_xfer()
1760 ret = stm32f7_i2c_smbus_check_pec(i2c_dev); in stm32f7_i2c_smbus_xfer()
1793 static void stm32f7_i2c_enable_wakeup(struct stm32f7_i2c_dev *i2c_dev, in stm32f7_i2c_enable_wakeup() argument
1796 void __iomem *base = i2c_dev->base; in stm32f7_i2c_enable_wakeup()
1799 if (!i2c_dev->wakeup_src) in stm32f7_i2c_enable_wakeup()
1803 device_set_wakeup_enable(i2c_dev->dev, true); in stm32f7_i2c_enable_wakeup()
1806 device_set_wakeup_enable(i2c_dev->dev, false); in stm32f7_i2c_enable_wakeup()
1813 struct stm32f7_i2c_dev *i2c_dev = i2c_get_adapdata(slave->adapter); in stm32f7_i2c_reg_slave() local
1814 void __iomem *base = i2c_dev->base; in stm32f7_i2c_reg_slave()
1815 struct device *dev = i2c_dev->dev; in stm32f7_i2c_reg_slave()
1824 if (stm32f7_i2c_is_slave_busy(i2c_dev)) { in stm32f7_i2c_reg_slave()
1829 ret = stm32f7_i2c_get_free_slave_id(i2c_dev, slave, &id); in stm32f7_i2c_reg_slave()
1837 if (!stm32f7_i2c_is_slave_registered(i2c_dev)) in stm32f7_i2c_reg_slave()
1838 stm32f7_i2c_enable_wakeup(i2c_dev, true); in stm32f7_i2c_reg_slave()
1843 i2c_dev->slave[id] = slave; in stm32f7_i2c_reg_slave()
1848 oar1 = readl_relaxed(i2c_dev->base + STM32F7_I2C_OAR1); in stm32f7_i2c_reg_slave()
1857 i2c_dev->slave[id] = slave; in stm32f7_i2c_reg_slave()
1858 writel_relaxed(oar1, i2c_dev->base + STM32F7_I2C_OAR1); in stm32f7_i2c_reg_slave()
1863 oar2 = readl_relaxed(i2c_dev->base + STM32F7_I2C_OAR2); in stm32f7_i2c_reg_slave()
1872 i2c_dev->slave[id] = slave; in stm32f7_i2c_reg_slave()
1873 writel_relaxed(oar2, i2c_dev->base + STM32F7_I2C_OAR2); in stm32f7_i2c_reg_slave()
1892 if (!stm32f7_i2c_is_slave_registered(i2c_dev)) in stm32f7_i2c_reg_slave()
1893 stm32f7_i2c_enable_wakeup(i2c_dev, false); in stm32f7_i2c_reg_slave()
1903 struct stm32f7_i2c_dev *i2c_dev = i2c_get_adapdata(slave->adapter); in stm32f7_i2c_unreg_slave() local
1904 void __iomem *base = i2c_dev->base; in stm32f7_i2c_unreg_slave()
1908 ret = stm32f7_i2c_get_slave_id(i2c_dev, slave, &id); in stm32f7_i2c_unreg_slave()
1912 WARN_ON(!i2c_dev->slave[id]); in stm32f7_i2c_unreg_slave()
1914 ret = pm_runtime_resume_and_get(i2c_dev->dev); in stm32f7_i2c_unreg_slave()
1926 i2c_dev->slave[id] = NULL; in stm32f7_i2c_unreg_slave()
1928 if (!stm32f7_i2c_is_slave_registered(i2c_dev)) { in stm32f7_i2c_unreg_slave()
1929 stm32f7_i2c_disable_irq(i2c_dev, STM32F7_I2C_ALL_IRQ_MASK); in stm32f7_i2c_unreg_slave()
1930 stm32f7_i2c_enable_wakeup(i2c_dev, false); in stm32f7_i2c_unreg_slave()
1933 pm_runtime_mark_last_busy(i2c_dev->dev); in stm32f7_i2c_unreg_slave()
1934 pm_runtime_put_autosuspend(i2c_dev->dev); in stm32f7_i2c_unreg_slave()
1939 static int stm32f7_i2c_write_fm_plus_bits(struct stm32f7_i2c_dev *i2c_dev, in stm32f7_i2c_write_fm_plus_bits() argument
1944 if (i2c_dev->bus_rate <= I2C_MAX_FAST_MODE_FREQ || in stm32f7_i2c_write_fm_plus_bits()
1945 IS_ERR_OR_NULL(i2c_dev->regmap)) in stm32f7_i2c_write_fm_plus_bits()
1949 if (i2c_dev->fmp_sreg == i2c_dev->fmp_creg) in stm32f7_i2c_write_fm_plus_bits()
1950 ret = regmap_update_bits(i2c_dev->regmap, in stm32f7_i2c_write_fm_plus_bits()
1951 i2c_dev->fmp_sreg, in stm32f7_i2c_write_fm_plus_bits()
1952 i2c_dev->fmp_mask, in stm32f7_i2c_write_fm_plus_bits()
1953 enable ? i2c_dev->fmp_mask : 0); in stm32f7_i2c_write_fm_plus_bits()
1955 ret = regmap_write(i2c_dev->regmap, in stm32f7_i2c_write_fm_plus_bits()
1956 enable ? i2c_dev->fmp_sreg : in stm32f7_i2c_write_fm_plus_bits()
1957 i2c_dev->fmp_creg, in stm32f7_i2c_write_fm_plus_bits()
1958 i2c_dev->fmp_mask); in stm32f7_i2c_write_fm_plus_bits()
1964 struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_setup_fm_plus_bits() argument
1969 i2c_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg-fmp"); in stm32f7_i2c_setup_fm_plus_bits()
1970 if (IS_ERR(i2c_dev->regmap)) in stm32f7_i2c_setup_fm_plus_bits()
1975 &i2c_dev->fmp_sreg); in stm32f7_i2c_setup_fm_plus_bits()
1979 i2c_dev->fmp_creg = i2c_dev->fmp_sreg + in stm32f7_i2c_setup_fm_plus_bits()
1980 i2c_dev->setup.fmp_clr_offset; in stm32f7_i2c_setup_fm_plus_bits()
1983 &i2c_dev->fmp_mask); in stm32f7_i2c_setup_fm_plus_bits()
1986 static int stm32f7_i2c_enable_smbus_host(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_enable_smbus_host() argument
1988 struct i2c_adapter *adap = &i2c_dev->adap; in stm32f7_i2c_enable_smbus_host()
1989 void __iomem *base = i2c_dev->base; in stm32f7_i2c_enable_smbus_host()
1996 i2c_dev->host_notify_client = client; in stm32f7_i2c_enable_smbus_host()
2004 static void stm32f7_i2c_disable_smbus_host(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_disable_smbus_host() argument
2006 void __iomem *base = i2c_dev->base; in stm32f7_i2c_disable_smbus_host()
2008 if (i2c_dev->host_notify_client) { in stm32f7_i2c_disable_smbus_host()
2012 i2c_free_slave_host_notify_device(i2c_dev->host_notify_client); in stm32f7_i2c_disable_smbus_host()
2016 static int stm32f7_i2c_enable_smbus_alert(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_enable_smbus_alert() argument
2019 struct i2c_adapter *adap = &i2c_dev->adap; in stm32f7_i2c_enable_smbus_alert()
2020 struct device *dev = i2c_dev->dev; in stm32f7_i2c_enable_smbus_alert()
2021 void __iomem *base = i2c_dev->base; in stm32f7_i2c_enable_smbus_alert()
2031 i2c_dev->alert = alert; in stm32f7_i2c_enable_smbus_alert()
2039 static void stm32f7_i2c_disable_smbus_alert(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_disable_smbus_alert() argument
2041 struct stm32f7_i2c_alert *alert = i2c_dev->alert; in stm32f7_i2c_disable_smbus_alert()
2042 void __iomem *base = i2c_dev->base; in stm32f7_i2c_disable_smbus_alert()
2054 struct stm32f7_i2c_dev *i2c_dev = i2c_get_adapdata(adap); in stm32f7_i2c_func() local
2063 if (i2c_dev->smbus_mode) in stm32f7_i2c_func()
2079 struct stm32f7_i2c_dev *i2c_dev; in stm32f7_i2c_probe() local
2087 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); in stm32f7_i2c_probe()
2088 if (!i2c_dev) in stm32f7_i2c_probe()
2091 i2c_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); in stm32f7_i2c_probe()
2092 if (IS_ERR(i2c_dev->base)) in stm32f7_i2c_probe()
2093 return PTR_ERR(i2c_dev->base); in stm32f7_i2c_probe()
2104 i2c_dev->wakeup_src = of_property_read_bool(pdev->dev.of_node, in stm32f7_i2c_probe()
2107 i2c_dev->clk = devm_clk_get(&pdev->dev, NULL); in stm32f7_i2c_probe()
2108 if (IS_ERR(i2c_dev->clk)) in stm32f7_i2c_probe()
2109 return dev_err_probe(&pdev->dev, PTR_ERR(i2c_dev->clk), in stm32f7_i2c_probe()
2112 ret = clk_prepare_enable(i2c_dev->clk); in stm32f7_i2c_probe()
2128 i2c_dev->dev = &pdev->dev; in stm32f7_i2c_probe()
2134 pdev->name, i2c_dev); in stm32f7_i2c_probe()
2142 pdev->name, i2c_dev); in stm32f7_i2c_probe()
2155 i2c_dev->setup = *setup; in stm32f7_i2c_probe()
2157 ret = stm32f7_i2c_setup_timing(i2c_dev, &i2c_dev->setup); in stm32f7_i2c_probe()
2162 if (i2c_dev->bus_rate > I2C_MAX_FAST_MODE_FREQ) { in stm32f7_i2c_probe()
2163 ret = stm32f7_i2c_setup_fm_plus_bits(pdev, i2c_dev); in stm32f7_i2c_probe()
2166 ret = stm32f7_i2c_write_fm_plus_bits(i2c_dev, true); in stm32f7_i2c_probe()
2171 adap = &i2c_dev->adap; in stm32f7_i2c_probe()
2172 i2c_set_adapdata(adap, i2c_dev); in stm32f7_i2c_probe()
2182 init_completion(&i2c_dev->complete); in stm32f7_i2c_probe()
2185 i2c_dev->dma = stm32_i2c_dma_request(i2c_dev->dev, phy_addr, in stm32f7_i2c_probe()
2188 if (IS_ERR(i2c_dev->dma)) { in stm32f7_i2c_probe()
2189 ret = PTR_ERR(i2c_dev->dma); in stm32f7_i2c_probe()
2193 dev_dbg(i2c_dev->dev, "No DMA option: fallback using interrupts\n"); in stm32f7_i2c_probe()
2194 i2c_dev->dma = NULL; in stm32f7_i2c_probe()
2197 if (i2c_dev->wakeup_src) { in stm32f7_i2c_probe()
2198 device_set_wakeup_capable(i2c_dev->dev, true); in stm32f7_i2c_probe()
2200 ret = dev_pm_set_wake_irq(i2c_dev->dev, irq_event); in stm32f7_i2c_probe()
2202 dev_err(i2c_dev->dev, "Failed to set wake up irq\n"); in stm32f7_i2c_probe()
2207 platform_set_drvdata(pdev, i2c_dev); in stm32f7_i2c_probe()
2209 pm_runtime_set_autosuspend_delay(i2c_dev->dev, in stm32f7_i2c_probe()
2211 pm_runtime_use_autosuspend(i2c_dev->dev); in stm32f7_i2c_probe()
2212 pm_runtime_set_active(i2c_dev->dev); in stm32f7_i2c_probe()
2213 pm_runtime_enable(i2c_dev->dev); in stm32f7_i2c_probe()
2217 stm32f7_i2c_hw_config(i2c_dev); in stm32f7_i2c_probe()
2219 i2c_dev->smbus_mode = of_property_read_bool(pdev->dev.of_node, "smbus"); in stm32f7_i2c_probe()
2225 if (i2c_dev->smbus_mode) { in stm32f7_i2c_probe()
2226 ret = stm32f7_i2c_enable_smbus_host(i2c_dev); in stm32f7_i2c_probe()
2228 dev_err(i2c_dev->dev, in stm32f7_i2c_probe()
2236 ret = stm32f7_i2c_enable_smbus_alert(i2c_dev); in stm32f7_i2c_probe()
2238 dev_err(i2c_dev->dev, in stm32f7_i2c_probe()
2245 dev_info(i2c_dev->dev, "STM32F7 I2C-%d bus adapter\n", adap->nr); in stm32f7_i2c_probe()
2247 pm_runtime_mark_last_busy(i2c_dev->dev); in stm32f7_i2c_probe()
2248 pm_runtime_put_autosuspend(i2c_dev->dev); in stm32f7_i2c_probe()
2253 stm32f7_i2c_disable_smbus_host(i2c_dev); in stm32f7_i2c_probe()
2259 pm_runtime_put_noidle(i2c_dev->dev); in stm32f7_i2c_probe()
2260 pm_runtime_disable(i2c_dev->dev); in stm32f7_i2c_probe()
2261 pm_runtime_set_suspended(i2c_dev->dev); in stm32f7_i2c_probe()
2262 pm_runtime_dont_use_autosuspend(i2c_dev->dev); in stm32f7_i2c_probe()
2264 if (i2c_dev->wakeup_src) in stm32f7_i2c_probe()
2265 dev_pm_clear_wake_irq(i2c_dev->dev); in stm32f7_i2c_probe()
2268 if (i2c_dev->wakeup_src) in stm32f7_i2c_probe()
2269 device_set_wakeup_capable(i2c_dev->dev, false); in stm32f7_i2c_probe()
2271 if (i2c_dev->dma) { in stm32f7_i2c_probe()
2272 stm32_i2c_dma_free(i2c_dev->dma); in stm32f7_i2c_probe()
2273 i2c_dev->dma = NULL; in stm32f7_i2c_probe()
2277 stm32f7_i2c_write_fm_plus_bits(i2c_dev, false); in stm32f7_i2c_probe()
2280 clk_disable_unprepare(i2c_dev->clk); in stm32f7_i2c_probe()
2287 struct stm32f7_i2c_dev *i2c_dev = platform_get_drvdata(pdev); in stm32f7_i2c_remove() local
2289 stm32f7_i2c_disable_smbus_alert(i2c_dev); in stm32f7_i2c_remove()
2290 stm32f7_i2c_disable_smbus_host(i2c_dev); in stm32f7_i2c_remove()
2292 i2c_del_adapter(&i2c_dev->adap); in stm32f7_i2c_remove()
2293 pm_runtime_get_sync(i2c_dev->dev); in stm32f7_i2c_remove()
2295 if (i2c_dev->wakeup_src) { in stm32f7_i2c_remove()
2296 dev_pm_clear_wake_irq(i2c_dev->dev); in stm32f7_i2c_remove()
2301 device_init_wakeup(i2c_dev->dev, false); in stm32f7_i2c_remove()
2304 pm_runtime_put_noidle(i2c_dev->dev); in stm32f7_i2c_remove()
2305 pm_runtime_disable(i2c_dev->dev); in stm32f7_i2c_remove()
2306 pm_runtime_set_suspended(i2c_dev->dev); in stm32f7_i2c_remove()
2307 pm_runtime_dont_use_autosuspend(i2c_dev->dev); in stm32f7_i2c_remove()
2309 if (i2c_dev->dma) { in stm32f7_i2c_remove()
2310 stm32_i2c_dma_free(i2c_dev->dma); in stm32f7_i2c_remove()
2311 i2c_dev->dma = NULL; in stm32f7_i2c_remove()
2314 stm32f7_i2c_write_fm_plus_bits(i2c_dev, false); in stm32f7_i2c_remove()
2316 clk_disable_unprepare(i2c_dev->clk); in stm32f7_i2c_remove()
2323 struct stm32f7_i2c_dev *i2c_dev = dev_get_drvdata(dev); in stm32f7_i2c_runtime_suspend() local
2325 if (!stm32f7_i2c_is_slave_registered(i2c_dev)) in stm32f7_i2c_runtime_suspend()
2326 clk_disable_unprepare(i2c_dev->clk); in stm32f7_i2c_runtime_suspend()
2333 struct stm32f7_i2c_dev *i2c_dev = dev_get_drvdata(dev); in stm32f7_i2c_runtime_resume() local
2336 if (!stm32f7_i2c_is_slave_registered(i2c_dev)) { in stm32f7_i2c_runtime_resume()
2337 ret = clk_prepare_enable(i2c_dev->clk); in stm32f7_i2c_runtime_resume()
2347 static int __maybe_unused stm32f7_i2c_regs_backup(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_regs_backup() argument
2350 struct stm32f7_i2c_regs *backup_regs = &i2c_dev->backup_regs; in stm32f7_i2c_regs_backup()
2352 ret = pm_runtime_resume_and_get(i2c_dev->dev); in stm32f7_i2c_regs_backup()
2356 backup_regs->cr1 = readl_relaxed(i2c_dev->base + STM32F7_I2C_CR1); in stm32f7_i2c_regs_backup()
2357 backup_regs->cr2 = readl_relaxed(i2c_dev->base + STM32F7_I2C_CR2); in stm32f7_i2c_regs_backup()
2358 backup_regs->oar1 = readl_relaxed(i2c_dev->base + STM32F7_I2C_OAR1); in stm32f7_i2c_regs_backup()
2359 backup_regs->oar2 = readl_relaxed(i2c_dev->base + STM32F7_I2C_OAR2); in stm32f7_i2c_regs_backup()
2360 backup_regs->tmgr = readl_relaxed(i2c_dev->base + STM32F7_I2C_TIMINGR); in stm32f7_i2c_regs_backup()
2361 stm32f7_i2c_write_fm_plus_bits(i2c_dev, false); in stm32f7_i2c_regs_backup()
2363 pm_runtime_put_sync(i2c_dev->dev); in stm32f7_i2c_regs_backup()
2368 static int __maybe_unused stm32f7_i2c_regs_restore(struct stm32f7_i2c_dev *i2c_dev) in stm32f7_i2c_regs_restore() argument
2372 struct stm32f7_i2c_regs *backup_regs = &i2c_dev->backup_regs; in stm32f7_i2c_regs_restore()
2374 ret = pm_runtime_resume_and_get(i2c_dev->dev); in stm32f7_i2c_regs_restore()
2378 cr1 = readl_relaxed(i2c_dev->base + STM32F7_I2C_CR1); in stm32f7_i2c_regs_restore()
2380 stm32f7_i2c_clr_bits(i2c_dev->base + STM32F7_I2C_CR1, in stm32f7_i2c_regs_restore()
2383 writel_relaxed(backup_regs->tmgr, i2c_dev->base + STM32F7_I2C_TIMINGR); in stm32f7_i2c_regs_restore()
2385 i2c_dev->base + STM32F7_I2C_CR1); in stm32f7_i2c_regs_restore()
2387 stm32f7_i2c_set_bits(i2c_dev->base + STM32F7_I2C_CR1, in stm32f7_i2c_regs_restore()
2389 writel_relaxed(backup_regs->cr2, i2c_dev->base + STM32F7_I2C_CR2); in stm32f7_i2c_regs_restore()
2390 writel_relaxed(backup_regs->oar1, i2c_dev->base + STM32F7_I2C_OAR1); in stm32f7_i2c_regs_restore()
2391 writel_relaxed(backup_regs->oar2, i2c_dev->base + STM32F7_I2C_OAR2); in stm32f7_i2c_regs_restore()
2392 stm32f7_i2c_write_fm_plus_bits(i2c_dev, true); in stm32f7_i2c_regs_restore()
2394 pm_runtime_put_sync(i2c_dev->dev); in stm32f7_i2c_regs_restore()
2401 struct stm32f7_i2c_dev *i2c_dev = dev_get_drvdata(dev); in stm32f7_i2c_suspend() local
2404 i2c_mark_adapter_suspended(&i2c_dev->adap); in stm32f7_i2c_suspend()
2407 ret = stm32f7_i2c_regs_backup(i2c_dev); in stm32f7_i2c_suspend()
2409 i2c_mark_adapter_resumed(&i2c_dev->adap); in stm32f7_i2c_suspend()
2422 struct stm32f7_i2c_dev *i2c_dev = dev_get_drvdata(dev); in stm32f7_i2c_resume() local
2431 ret = stm32f7_i2c_regs_restore(i2c_dev); in stm32f7_i2c_resume()
2436 i2c_mark_adapter_resumed(&i2c_dev->adap); in stm32f7_i2c_resume()