Lines Matching +full:stm32 +full:- +full:timer +full:- +full:counter
1 // SPDX-License-Identifier: GPL-2.0
11 #include <linux/iio/timer/stm32-timer-trigger.h>
13 #include <linux/mfd/stm32-timers.h>
22 /* List the triggers created by each timer */
43 /* List the triggers accepted by each timer */
50 { }, /* timer 6 */
51 { }, /* timer 7 */
54 { }, /* timer 10 */
55 { }, /* timer 11 */
65 { }, /* timer 6 */
66 { }, /* timer 7 */
68 { }, /* timer 9 */
69 { }, /* timer 10 */
70 { }, /* timer 11 */
72 { }, /* timer 13 */
73 { }, /* timer 14 */
75 { }, /* timer 16 */
76 { }, /* timer 17 */
126 div = (unsigned long long)clk_get_rate(priv->clk); in stm32_timer_start()
136 while (div > priv->max_arr) { in stm32_timer_start()
144 dev_err(priv->dev, "prescaler exceeds the maximum value\n"); in stm32_timer_start()
145 return -EINVAL; in stm32_timer_start()
148 /* Check if nobody else use the timer */ in stm32_timer_start()
149 regmap_read(priv->regmap, TIM_CCER, &ccer); in stm32_timer_start()
151 return -EBUSY; in stm32_timer_start()
153 mutex_lock(&priv->lock); in stm32_timer_start()
154 if (!priv->enabled) { in stm32_timer_start()
155 priv->enabled = true; in stm32_timer_start()
156 clk_enable(priv->clk); in stm32_timer_start()
159 regmap_write(priv->regmap, TIM_PSC, prescaler); in stm32_timer_start()
160 regmap_write(priv->regmap, TIM_ARR, prd - 1); in stm32_timer_start()
161 regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE); in stm32_timer_start()
164 if (stm32_timer_is_trgo2_name(trig->name)) in stm32_timer_start()
165 regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2, in stm32_timer_start()
168 regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS, in stm32_timer_start()
172 regmap_set_bits(priv->regmap, TIM_EGR, TIM_EGR_UG); in stm32_timer_start()
175 regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); in stm32_timer_start()
176 mutex_unlock(&priv->lock); in stm32_timer_start()
186 regmap_read(priv->regmap, TIM_CCER, &ccer); in stm32_timer_stop()
190 mutex_lock(&priv->lock); in stm32_timer_stop()
191 /* Stop timer */ in stm32_timer_stop()
192 regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE); in stm32_timer_stop()
193 regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); in stm32_timer_stop()
194 regmap_write(priv->regmap, TIM_PSC, 0); in stm32_timer_stop()
195 regmap_write(priv->regmap, TIM_ARR, 0); in stm32_timer_stop()
198 if (stm32_timer_is_trgo2_name(trig->name)) in stm32_timer_stop()
199 regmap_clear_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2); in stm32_timer_stop()
201 regmap_clear_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS); in stm32_timer_stop()
204 regmap_set_bits(priv->regmap, TIM_EGR, TIM_EGR_UG); in stm32_timer_stop()
206 if (priv->enabled) { in stm32_timer_stop()
207 priv->enabled = false; in stm32_timer_stop()
208 clk_disable(priv->clk); in stm32_timer_stop()
210 mutex_unlock(&priv->lock); in stm32_timer_stop()
245 regmap_read(priv->regmap, TIM_CR1, &cr1); in stm32_tt_read_frequency()
246 regmap_read(priv->regmap, TIM_PSC, &psc); in stm32_tt_read_frequency()
247 regmap_read(priv->regmap, TIM_ARR, &arr); in stm32_tt_read_frequency()
250 freq = (unsigned long long)clk_get_rate(priv->clk); in stm32_tt_read_frequency()
293 regmap_read(priv->regmap, TIM_CR2, &cr2); in stm32_tt_show_master_mode()
295 if (stm32_timer_is_trgo2_name(trig->name)) in stm32_tt_show_master_mode()
312 if (stm32_timer_is_trgo2_name(trig->name)) { in stm32_tt_store_master_mode()
325 mutex_lock(&priv->lock); in stm32_tt_store_master_mode()
326 if (!priv->enabled) { in stm32_tt_store_master_mode()
328 priv->enabled = true; in stm32_tt_store_master_mode()
329 clk_enable(priv->clk); in stm32_tt_store_master_mode()
331 regmap_update_bits(priv->regmap, TIM_CR2, mask, in stm32_tt_store_master_mode()
333 mutex_unlock(&priv->lock); in stm32_tt_store_master_mode()
338 return -EINVAL; in stm32_tt_store_master_mode()
349 if (stm32_timer_is_trgo2_name(trig->name)) in stm32_tt_show_master_mode_avail()
355 len += scnprintf(buf + len, PAGE_SIZE - len, in stm32_tt_show_master_mode_avail()
359 buf[len - 1] = '\n'; in stm32_tt_show_master_mode_avail()
395 list_for_each_entry(tr, &priv->tr_list, alloc_list) in stm32_unregister_iio_triggers()
402 const char * const *cur = priv->triggers; in stm32_register_iio_triggers()
404 INIT_LIST_HEAD(&priv->tr_list); in stm32_register_iio_triggers()
411 if (cur_is_trgo2 && !priv->has_trgo2) { in stm32_register_iio_triggers()
416 trig = devm_iio_trigger_alloc(priv->dev, "%s", *cur); in stm32_register_iio_triggers()
418 return -ENOMEM; in stm32_register_iio_triggers()
420 trig->dev.parent = priv->dev->parent; in stm32_register_iio_triggers()
421 trig->ops = &timer_trigger_ops; in stm32_register_iio_triggers()
428 trig->dev.groups = stm32_trigger_attr_groups; in stm32_register_iio_triggers()
438 list_add_tail(&trig->alloc_list, &priv->tr_list); in stm32_register_iio_triggers()
454 regmap_read(priv->regmap, TIM_CNT, &dat); in stm32_counter_read_raw()
459 regmap_read(priv->regmap, TIM_CR1, &dat); in stm32_counter_read_raw()
464 regmap_read(priv->regmap, TIM_SMCR, &dat); in stm32_counter_read_raw()
477 return -EINVAL; in stm32_counter_read_raw()
488 return regmap_write(priv->regmap, TIM_CNT, val); in stm32_counter_write_raw()
492 return -EINVAL; in stm32_counter_write_raw()
495 mutex_lock(&priv->lock); in stm32_counter_write_raw()
497 if (!priv->enabled) { in stm32_counter_write_raw()
498 priv->enabled = true; in stm32_counter_write_raw()
499 clk_enable(priv->clk); in stm32_counter_write_raw()
501 regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); in stm32_counter_write_raw()
503 regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); in stm32_counter_write_raw()
504 if (priv->enabled) { in stm32_counter_write_raw()
505 priv->enabled = false; in stm32_counter_write_raw()
506 clk_disable(priv->clk); in stm32_counter_write_raw()
509 mutex_unlock(&priv->lock); in stm32_counter_write_raw()
513 return -EINVAL; in stm32_counter_write_raw()
520 const char * const *cur = priv->valids; in stm32_counter_validate_trigger()
524 return -EINVAL; in stm32_counter_validate_trigger()
527 if (!strncmp(trig->name, *cur, strlen(trig->name))) { in stm32_counter_validate_trigger()
528 regmap_update_bits(priv->regmap, in stm32_counter_validate_trigger()
537 return -EINVAL; in stm32_counter_validate_trigger()
556 regmap_set_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS); in stm32_set_trigger_mode()
567 regmap_read(priv->regmap, TIM_SMCR, &smcr); in stm32_get_trigger_mode()
569 return (smcr & TIM_SMCR_SMS) == TIM_SMCR_SMS ? 0 : -EINVAL; in stm32_get_trigger_mode()
596 return -EINVAL; in stm32_enable_mode2sms()
610 * enable counter clock, so it can use it. Keeps it in sync with CEN. in stm32_set_enable_mode()
612 mutex_lock(&priv->lock); in stm32_set_enable_mode()
613 if (sms == 6 && !priv->enabled) { in stm32_set_enable_mode()
614 clk_enable(priv->clk); in stm32_set_enable_mode()
615 priv->enabled = true; in stm32_set_enable_mode()
617 mutex_unlock(&priv->lock); in stm32_set_enable_mode()
619 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms); in stm32_set_enable_mode()
635 return -EINVAL; in stm32_sms2enable_mode()
644 regmap_read(priv->regmap, TIM_SMCR, &smcr); in stm32_get_enable_mode()
665 regmap_read(priv->regmap, TIM_ARR, &arr); in stm32_count_get_preset()
684 regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE); in stm32_count_set_preset()
685 regmap_write(priv->regmap, TIM_ARR, preset); in stm32_count_set_preset()
724 indio_dev->name = dev_name(dev); in stm32_setup_counter_device()
725 indio_dev->info = &stm32_trigger_info; in stm32_setup_counter_device()
726 indio_dev->modes = INDIO_HARDWARE_TRIGGERED; in stm32_setup_counter_device()
727 indio_dev->num_channels = 1; in stm32_setup_counter_device()
728 indio_dev->channels = &stm32_trigger_channel; in stm32_setup_counter_device()
741 * return true if the trigger is a valid stm32 iio timer trigger
746 return (trig->ops == &timer_trigger_ops); in is_stm32_timer_trigger()
756 * timer supports it. in stm32_timer_detect_trgo2()
758 regmap_set_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2); in stm32_timer_detect_trgo2()
759 regmap_read(priv->regmap, TIM_CR2, &val); in stm32_timer_detect_trgo2()
760 regmap_clear_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2); in stm32_timer_detect_trgo2()
761 priv->has_trgo2 = !!val; in stm32_timer_detect_trgo2()
766 struct device *dev = &pdev->dev; in stm32_timer_trigger_probe()
768 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent); in stm32_timer_trigger_probe()
780 index >= cfg->num_valids_table) in stm32_timer_trigger_probe()
781 return -EINVAL; in stm32_timer_trigger_probe()
784 if (*cfg->valids_table[index]) in stm32_timer_trigger_probe()
790 return -ENOMEM; in stm32_timer_trigger_probe()
792 priv->dev = dev; in stm32_timer_trigger_probe()
793 priv->regmap = ddata->regmap; in stm32_timer_trigger_probe()
794 priv->clk = ddata->clk; in stm32_timer_trigger_probe()
795 priv->max_arr = ddata->max_arr; in stm32_timer_trigger_probe()
796 priv->triggers = triggers_table[index]; in stm32_timer_trigger_probe()
797 priv->valids = cfg->valids_table[index]; in stm32_timer_trigger_probe()
799 mutex_init(&priv->lock); in stm32_timer_trigger_probe()
818 /* Check if nobody else use the timer, then disable it */ in stm32_timer_trigger_remove()
819 regmap_read(priv->regmap, TIM_CCER, &val); in stm32_timer_trigger_remove()
821 regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); in stm32_timer_trigger_remove()
823 if (priv->enabled) in stm32_timer_trigger_remove()
824 clk_disable(priv->clk); in stm32_timer_trigger_remove()
831 /* Only take care of enabled timer: don't disturb other MFD child */ in stm32_timer_trigger_suspend()
832 if (priv->enabled) { in stm32_timer_trigger_suspend()
834 regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1); in stm32_timer_trigger_suspend()
835 regmap_read(priv->regmap, TIM_CR2, &priv->bak.cr2); in stm32_timer_trigger_suspend()
836 regmap_read(priv->regmap, TIM_PSC, &priv->bak.psc); in stm32_timer_trigger_suspend()
837 regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr); in stm32_timer_trigger_suspend()
838 regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt); in stm32_timer_trigger_suspend()
839 regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr); in stm32_timer_trigger_suspend()
841 /* Disable the timer */ in stm32_timer_trigger_suspend()
842 regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); in stm32_timer_trigger_suspend()
843 clk_disable(priv->clk); in stm32_timer_trigger_suspend()
854 if (priv->enabled) { in stm32_timer_trigger_resume()
855 ret = clk_enable(priv->clk); in stm32_timer_trigger_resume()
860 regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr); in stm32_timer_trigger_resume()
861 regmap_write(priv->regmap, TIM_CR2, priv->bak.cr2); in stm32_timer_trigger_resume()
864 regmap_write(priv->regmap, TIM_PSC, priv->bak.psc); in stm32_timer_trigger_resume()
865 regmap_write(priv->regmap, TIM_ARR, priv->bak.arr); in stm32_timer_trigger_resume()
866 regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt); in stm32_timer_trigger_resume()
868 /* Also re-enables the timer */ in stm32_timer_trigger_resume()
869 regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1); in stm32_timer_trigger_resume()
891 .compatible = "st,stm32-timer-trigger",
894 .compatible = "st,stm32h7-timer-trigger",
905 .name = "stm32-timer-trigger",
912 MODULE_ALIAS("platform:stm32-timer-trigger");
913 MODULE_DESCRIPTION("STMicroelectronics STM32 Timer Trigger driver");