Lines Matching +full:max +full:- +full:clk +full:- +full:rate +full:- +full:hz
1 // SPDX-License-Identifier: GPL-2.0+
7 #include <clk.h>
130 * struct stm32_i2c_spec - private i2c specification timing
131 * @rate: I2C bus speed (Hz)
132 * @rate_min: 80% of I2C bus speed (Hz)
133 * @rate_max: 120% of I2C bus speed (Hz)
134 * @fall_max: Max fall time of both SDA and SCL signals (ns)
135 * @rise_max: Max rise time of both SDA and SCL signals (ns)
137 * @vddat_max: Max data valid time (ns)
144 u32 rate; member
157 * struct stm32_i2c_setup - private I2C timing setup parameters
159 * @speed_freq: I2C speed frequency (Hz)
160 * @clock_src: I2C clock source frequency (Hz)
163 * @dnf: Digital filter coefficient (0-16)
177 * struct stm32_i2c_timings - private I2C output parameters
195 struct clk clk; member
202 .rate = STANDARD_RATE,
214 .rate = FAST_RATE,
226 .rate = FAST_PLUS_RATE,
248 struct stm32_i2c_regs *regs = i2c_priv->regs; in stm32_i2c_check_device_busy()
249 u32 status = readl(®s->isr); in stm32_i2c_check_device_busy()
252 return -EBUSY; in stm32_i2c_check_device_busy()
260 struct stm32_i2c_regs *regs = i2c_priv->regs; in stm32_i2c_message_start()
261 u32 cr2 = readl(®s->cr2); in stm32_i2c_message_start()
265 if (msg->flags & I2C_M_RD) in stm32_i2c_message_start()
270 if (msg->flags & I2C_M_TEN) { in stm32_i2c_message_start()
272 cr2 |= STM32_I2C_CR2_SADD10(msg->addr); in stm32_i2c_message_start()
276 cr2 |= STM32_I2C_CR2_SADD7(msg->addr); in stm32_i2c_message_start()
282 if (msg->len > STM32_I2C_MAX_LEN) { in stm32_i2c_message_start()
286 cr2 |= STM32_I2C_CR2_NBYTES(msg->len); in stm32_i2c_message_start()
290 writel(cr2, ®s->cr2); in stm32_i2c_message_start()
293 setbits_le32(®s->cr2, STM32_I2C_CR2_START); in stm32_i2c_message_start()
304 struct stm32_i2c_regs *regs = i2c_priv->regs; in stm32_i2c_handle_reload()
305 u32 cr2 = readl(®s->cr2); in stm32_i2c_handle_reload()
309 if (msg->len > STM32_I2C_MAX_LEN) { in stm32_i2c_handle_reload()
313 cr2 |= STM32_I2C_CR2_NBYTES(msg->len); in stm32_i2c_handle_reload()
316 writel(cr2, ®s->cr2); in stm32_i2c_handle_reload()
322 struct stm32_i2c_regs *regs = i2c_priv->regs; in stm32_i2c_wait_flags()
325 *status = readl(®s->isr); in stm32_i2c_wait_flags()
329 return -ETIMEDOUT; in stm32_i2c_wait_flags()
332 *status = readl(®s->isr); in stm32_i2c_wait_flags()
340 struct stm32_i2c_regs *regs = i2c_priv->regs; in stm32_i2c_check_end_of_message()
354 setbits_le32(®s->icr, STM32_I2C_ICR_BERRCF); in stm32_i2c_check_end_of_message()
356 return -EIO; in stm32_i2c_check_end_of_message()
363 setbits_le32(®s->icr, STM32_I2C_ICR_ARLOCF); in stm32_i2c_check_end_of_message()
365 return -EAGAIN; in stm32_i2c_check_end_of_message()
372 setbits_le32(®s->icr, STM32_I2C_ICR_NACKCF); in stm32_i2c_check_end_of_message()
380 ret = -EIO; in stm32_i2c_check_end_of_message()
385 setbits_le32(®s->icr, STM32_I2C_ICR_STOPCF); in stm32_i2c_check_end_of_message()
388 setbits_le32(®s->cr2, STM32_I2C_CR2_RESET_MASK); in stm32_i2c_check_end_of_message()
397 struct stm32_i2c_regs *regs = i2c_priv->regs; in stm32_i2c_message_xfer()
399 u32 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE : in stm32_i2c_message_xfer()
401 int bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ? in stm32_i2c_message_xfer()
402 STM32_I2C_MAX_LEN : msg->len; in stm32_i2c_message_xfer()
410 while (msg->len) { in stm32_i2c_message_xfer()
423 *msg->buf++ = readb(®s->rxdr); in stm32_i2c_message_xfer()
424 msg->len--; in stm32_i2c_message_xfer()
425 bytes_to_rw--; in stm32_i2c_message_xfer()
429 writeb(*msg->buf++, ®s->txdr); in stm32_i2c_message_xfer()
430 msg->len--; in stm32_i2c_message_xfer()
431 bytes_to_rw--; in stm32_i2c_message_xfer()
434 if (!bytes_to_rw && msg->len) { in stm32_i2c_message_xfer()
441 bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ? in stm32_i2c_message_xfer()
442 STM32_I2C_MAX_LEN : msg->len; in stm32_i2c_message_xfer()
443 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE : in stm32_i2c_message_xfer()
462 setbits_le32(®s->cr2, mask); in stm32_i2c_message_xfer()
477 for (; nmsgs > 0; nmsgs--, msg++) { in stm32_i2c_xfer()
492 setup->clock_src); in stm32_i2c_compute_solutions()
498 af_delay_min = setup->analog_filter ? in stm32_i2c_compute_solutions()
500 af_delay_max = setup->analog_filter ? in stm32_i2c_compute_solutions()
503 sdadel_min = setup->fall_time - i2c_specs[setup->speed].hddat_min - in stm32_i2c_compute_solutions()
504 af_delay_min - (setup->dnf + 3) * i2cclk; in stm32_i2c_compute_solutions()
506 sdadel_max = i2c_specs[setup->speed].vddat_max - setup->rise_time - in stm32_i2c_compute_solutions()
507 af_delay_max - (setup->dnf + 4) * i2cclk; in stm32_i2c_compute_solutions()
509 scldel_min = setup->rise_time + i2c_specs[setup->speed].sudat_min; in stm32_i2c_compute_solutions()
516 debug("%s: SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n", __func__, in stm32_i2c_compute_solutions()
535 return -ENOMEM; in stm32_i2c_compute_solutions()
537 v->presc = p; in stm32_i2c_compute_solutions()
538 v->scldel = l; in stm32_i2c_compute_solutions()
539 v->sdadel = a; in stm32_i2c_compute_solutions()
542 list_add_tail(&v->node, solutions); in stm32_i2c_compute_solutions()
550 ret = -EPERM; in stm32_i2c_compute_solutions()
562 setup->speed_freq); in stm32_i2c_choose_solution()
565 setup->clock_src); in stm32_i2c_choose_solution()
574 af_delay_min = setup->analog_filter ? in stm32_i2c_choose_solution()
576 dnf_delay = setup->dnf * i2cclk; in stm32_i2c_choose_solution()
579 clk_max = STM32_NSEC_PER_SEC / i2c_specs[setup->speed].rate_min; in stm32_i2c_choose_solution()
580 clk_min = STM32_NSEC_PER_SEC / i2c_specs[setup->speed].rate_max; in stm32_i2c_choose_solution()
585 * - SCL Low Period has to be higher than Low Period of the SCL Clock in stm32_i2c_choose_solution()
587 * (SCL Low Period - Analog/Digital filters) / 4. in stm32_i2c_choose_solution()
588 * - SCL High Period has to be lower than High Period of the SCL Clock in stm32_i2c_choose_solution()
590 * - I2C Clock has to be lower than SCL High Period in stm32_i2c_choose_solution()
593 u32 prescaler = (v->presc + 1) * i2cclk; in stm32_i2c_choose_solution()
598 if ((tscl_l < i2c_specs[setup->speed].l_min) || in stm32_i2c_choose_solution()
600 ((tscl_l - af_delay_min - dnf_delay) / 4))) { in stm32_i2c_choose_solution()
607 setup->rise_time + setup->fall_time; in stm32_i2c_choose_solution()
610 (tscl_h >= i2c_specs[setup->speed].h_min) && in stm32_i2c_choose_solution()
612 int clk_error = tscl - i2cbus; in stm32_i2c_choose_solution()
615 clk_error = -clk_error; in stm32_i2c_choose_solution()
619 v->scll = l; in stm32_i2c_choose_solution()
620 v->sclh = h; in stm32_i2c_choose_solution()
631 ret = -EPERM; in stm32_i2c_choose_solution()
645 if (setup->speed >= STM32_I2C_SPEED_END) { in stm32_i2c_compute_timing()
647 setup->speed, STM32_I2C_SPEED_END - 1); in stm32_i2c_compute_timing()
648 return -EINVAL; in stm32_i2c_compute_timing()
651 if ((setup->rise_time > i2c_specs[setup->speed].rise_max) || in stm32_i2c_compute_timing()
652 (setup->fall_time > i2c_specs[setup->speed].fall_max)) { in stm32_i2c_compute_timing()
655 setup->rise_time, i2c_specs[setup->speed].rise_max, in stm32_i2c_compute_timing()
656 setup->fall_time, i2c_specs[setup->speed].fall_max); in stm32_i2c_compute_timing()
657 return -EINVAL; in stm32_i2c_compute_timing()
660 if (setup->dnf > STM32_I2C_DNF_MAX) { in stm32_i2c_compute_timing()
662 setup->dnf, STM32_I2C_DNF_MAX); in stm32_i2c_compute_timing()
663 return -EINVAL; in stm32_i2c_compute_timing()
666 if (setup->speed_freq > i2c_specs[setup->speed].rate) { in stm32_i2c_compute_timing()
668 setup->speed_freq, i2c_specs[setup->speed].rate); in stm32_i2c_compute_timing()
669 return -EINVAL; in stm32_i2c_compute_timing()
682 __func__, output->presc, in stm32_i2c_compute_timing()
683 output->scldel, output->sdadel, in stm32_i2c_compute_timing()
684 output->scll, output->sclh); in stm32_i2c_compute_timing()
689 list_del(&v->node); in stm32_i2c_compute_timing()
699 struct stm32_i2c_setup *setup = i2c_priv->setup; in stm32_i2c_setup_timing()
702 setup->speed = i2c_priv->speed; in stm32_i2c_setup_timing()
703 setup->speed_freq = i2c_specs[setup->speed].rate; in stm32_i2c_setup_timing()
704 setup->clock_src = clk_get_rate(&i2c_priv->clk); in stm32_i2c_setup_timing()
706 if (!setup->clock_src) { in stm32_i2c_setup_timing()
707 pr_err("%s: clock rate is 0\n", __func__); in stm32_i2c_setup_timing()
708 return -EINVAL; in stm32_i2c_setup_timing()
716 if (i2c_priv->speed > STM32_I2C_SPEED_STANDARD) { in stm32_i2c_setup_timing()
717 i2c_priv->speed--; in stm32_i2c_setup_timing()
718 setup->speed = i2c_priv->speed; in stm32_i2c_setup_timing()
719 setup->speed_freq = in stm32_i2c_setup_timing()
720 i2c_specs[setup->speed].rate; in stm32_i2c_setup_timing()
722 __func__, i2c_specs[setup->speed].rate); in stm32_i2c_setup_timing()
734 debug("%s: I2C Speed(%i), Freq(%i), Clk Source(%i)\n", __func__, in stm32_i2c_setup_timing()
735 setup->speed, setup->speed_freq, setup->clock_src); in stm32_i2c_setup_timing()
737 setup->rise_time, setup->fall_time); in stm32_i2c_setup_timing()
739 setup->analog_filter ? "On" : "Off", setup->dnf); in stm32_i2c_setup_timing()
746 struct stm32_i2c_regs *regs = i2c_priv->regs; in stm32_i2c_hw_config()
756 clrbits_le32(®s->cr1, STM32_I2C_CR1_PE); in stm32_i2c_hw_config()
764 writel(timing, ®s->timingr); in stm32_i2c_hw_config()
767 if (i2c_priv->setup->analog_filter) in stm32_i2c_hw_config()
768 clrbits_le32(®s->cr1, STM32_I2C_CR1_ANFOFF); in stm32_i2c_hw_config()
770 setbits_le32(®s->cr1, STM32_I2C_CR1_ANFOFF); in stm32_i2c_hw_config()
771 setbits_le32(®s->cr1, STM32_I2C_CR1_PE); in stm32_i2c_hw_config()
782 i2c_priv->speed = STM32_I2C_SPEED_STANDARD; in stm32_i2c_set_bus_speed()
785 i2c_priv->speed = STM32_I2C_SPEED_FAST; in stm32_i2c_set_bus_speed()
788 i2c_priv->speed = STM32_I2C_SPEED_FAST_PLUS; in stm32_i2c_set_bus_speed()
792 return -EINVAL; in stm32_i2c_set_bus_speed()
807 return -EINVAL; in stm32_i2c_probe()
809 i2c_priv->regs = (struct stm32_i2c_regs *)addr; in stm32_i2c_probe()
811 ret = clk_get_by_index(dev, 0, &i2c_priv->clk); in stm32_i2c_probe()
815 ret = clk_enable(&i2c_priv->clk); in stm32_i2c_probe()
830 clk_disable(&i2c_priv->clk); in stm32_i2c_probe()
832 clk_free(&i2c_priv->clk); in stm32_i2c_probe()
842 i2c_priv->setup = (struct stm32_i2c_setup *)dev_get_driver_data(dev); in stm32_ofdata_to_platdata()
843 if (!i2c_priv->setup) in stm32_ofdata_to_platdata()
844 return -EINVAL; in stm32_ofdata_to_platdata()
846 rise_time = dev_read_u32_default(dev, "i2c-scl-rising-time-ns", 0); in stm32_ofdata_to_platdata()
848 i2c_priv->setup->rise_time = rise_time; in stm32_ofdata_to_platdata()
850 fall_time = dev_read_u32_default(dev, "i2c-scl-falling-time-ns", 0); in stm32_ofdata_to_platdata()
852 i2c_priv->setup->fall_time = fall_time; in stm32_ofdata_to_platdata()
863 { .compatible = "st,stm32f7-i2c", .data = (ulong)&stm32f7_setup },
868 .name = "stm32f7-i2c",