2 * Copyright (C) STMicroelectronics 2016
4 * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
6 * License terms: GNU General Public License (GPL), version 2
9 #include <linux/iio/iio.h>
10 #include <linux/iio/sysfs.h>
11 #include <linux/iio/timer/stm32-timer-trigger.h>
12 #include <linux/iio/trigger.h>
13 #include <linux/mfd/stm32-timers.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
17 #define MAX_TRIGGERS 7
20 /* List the triggers created by each timer */
21 static const void *triggers_table[][MAX_TRIGGERS] = {
22 { TIM1_TRGO, TIM1_TRGO2, TIM1_CH1, TIM1_CH2, TIM1_CH3, TIM1_CH4,},
23 { TIM2_TRGO, TIM2_CH1, TIM2_CH2, TIM2_CH3, TIM2_CH4,},
24 { TIM3_TRGO, TIM3_CH1, TIM3_CH2, TIM3_CH3, TIM3_CH4,},
25 { TIM4_TRGO, TIM4_CH1, TIM4_CH2, TIM4_CH3, TIM4_CH4,},
26 { TIM5_TRGO, TIM5_CH1, TIM5_CH2, TIM5_CH3, TIM5_CH4,},
29 { TIM8_TRGO, TIM8_TRGO2, TIM8_CH1, TIM8_CH2, TIM8_CH3, TIM8_CH4,},
30 { TIM9_TRGO, TIM9_CH1, TIM9_CH2,},
33 { TIM12_TRGO, TIM12_CH1, TIM12_CH2,},
36 /* List the triggers accepted by each timer */
37 static const void *valids_table[][MAX_VALIDS] = {
38 { TIM5_TRGO, TIM2_TRGO, TIM3_TRGO, TIM4_TRGO,},
39 { TIM1_TRGO, TIM8_TRGO, TIM3_TRGO, TIM4_TRGO,},
40 { TIM1_TRGO, TIM2_TRGO, TIM5_TRGO, TIM4_TRGO,},
41 { TIM1_TRGO, TIM2_TRGO, TIM3_TRGO, TIM8_TRGO,},
42 { TIM2_TRGO, TIM3_TRGO, TIM4_TRGO, TIM8_TRGO,},
45 { TIM1_TRGO, TIM2_TRGO, TIM4_TRGO, TIM5_TRGO,},
46 { TIM2_TRGO, TIM3_TRGO,},
49 { TIM4_TRGO, TIM5_TRGO,},
52 struct stm32_timer_trigger {
54 struct regmap *regmap;
62 static bool stm32_timer_is_trgo2_name(const char *name)
64 return !!strstr(name, "trgo2");
67 static int stm32_timer_start(struct stm32_timer_trigger *priv,
68 struct iio_trigger *trig,
69 unsigned int frequency)
71 unsigned long long prd, div;
75 /* Period and prescaler values depends of clock rate */
76 div = (unsigned long long)clk_get_rate(priv->clk);
78 do_div(div, frequency);
83 * Increase prescaler value until we get a result that fit
84 * with auto reload register maximum value.
86 while (div > priv->max_arr) {
89 do_div(div, (prescaler + 1));
93 if (prescaler > MAX_TIM_PSC) {
94 dev_err(priv->dev, "prescaler exceeds the maximum value\n");
98 /* Check if nobody else use the timer */
99 regmap_read(priv->regmap, TIM_CCER, &ccer);
100 if (ccer & TIM_CCER_CCXE)
103 regmap_read(priv->regmap, TIM_CR1, &cr1);
104 if (!(cr1 & TIM_CR1_CEN))
105 clk_enable(priv->clk);
107 regmap_write(priv->regmap, TIM_PSC, prescaler);
108 regmap_write(priv->regmap, TIM_ARR, prd - 1);
109 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
111 /* Force master mode to update mode */
112 if (stm32_timer_is_trgo2_name(trig->name))
113 regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2,
114 0x2 << TIM_CR2_MMS2_SHIFT);
116 regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS,
117 0x2 << TIM_CR2_MMS_SHIFT);
119 /* Make sure that registers are updated */
120 regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
122 /* Enable controller */
123 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
128 static void stm32_timer_stop(struct stm32_timer_trigger *priv)
132 regmap_read(priv->regmap, TIM_CCER, &ccer);
133 if (ccer & TIM_CCER_CCXE)
136 regmap_read(priv->regmap, TIM_CR1, &cr1);
137 if (cr1 & TIM_CR1_CEN)
138 clk_disable(priv->clk);
141 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
142 regmap_write(priv->regmap, TIM_PSC, 0);
143 regmap_write(priv->regmap, TIM_ARR, 0);
145 /* Make sure that registers are updated */
146 regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
149 static ssize_t stm32_tt_store_frequency(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t len)
153 struct iio_trigger *trig = to_iio_trigger(dev);
154 struct stm32_timer_trigger *priv = iio_trigger_get_drvdata(trig);
158 ret = kstrtouint(buf, 10, &freq);
163 stm32_timer_stop(priv);
165 ret = stm32_timer_start(priv, trig, freq);
173 static ssize_t stm32_tt_read_frequency(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct iio_trigger *trig = to_iio_trigger(dev);
177 struct stm32_timer_trigger *priv = iio_trigger_get_drvdata(trig);
179 unsigned long long freq = 0;
181 regmap_read(priv->regmap, TIM_CR1, &cr1);
182 regmap_read(priv->regmap, TIM_PSC, &psc);
183 regmap_read(priv->regmap, TIM_ARR, &arr);
185 if (cr1 & TIM_CR1_CEN) {
186 freq = (unsigned long long)clk_get_rate(priv->clk);
187 do_div(freq, psc + 1);
188 do_div(freq, arr + 1);
191 return sprintf(buf, "%d\n", (unsigned int)freq);
194 static IIO_DEV_ATTR_SAMP_FREQ(0660,
195 stm32_tt_read_frequency,
196 stm32_tt_store_frequency);
198 #define MASTER_MODE_MAX 7
199 #define MASTER_MODE2_MAX 15
201 static char *master_mode_table[] = {
210 /* Master mode selection 2 only */
213 "compare_pulse_OC4REF",
214 "compare_pulse_OC6REF",
215 "compare_pulse_OC4REF_r_or_OC6REF_r",
216 "compare_pulse_OC4REF_r_or_OC6REF_f",
217 "compare_pulse_OC5REF_r_or_OC6REF_r",
218 "compare_pulse_OC5REF_r_or_OC6REF_f",
221 static ssize_t stm32_tt_show_master_mode(struct device *dev,
222 struct device_attribute *attr,
225 struct stm32_timer_trigger *priv = dev_get_drvdata(dev);
226 struct iio_trigger *trig = to_iio_trigger(dev);
229 regmap_read(priv->regmap, TIM_CR2, &cr2);
231 if (stm32_timer_is_trgo2_name(trig->name))
232 cr2 = (cr2 & TIM_CR2_MMS2) >> TIM_CR2_MMS2_SHIFT;
234 cr2 = (cr2 & TIM_CR2_MMS) >> TIM_CR2_MMS_SHIFT;
236 return snprintf(buf, PAGE_SIZE, "%s\n", master_mode_table[cr2]);
239 static ssize_t stm32_tt_store_master_mode(struct device *dev,
240 struct device_attribute *attr,
241 const char *buf, size_t len)
243 struct stm32_timer_trigger *priv = dev_get_drvdata(dev);
244 struct iio_trigger *trig = to_iio_trigger(dev);
245 u32 mask, shift, master_mode_max;
248 if (stm32_timer_is_trgo2_name(trig->name)) {
250 shift = TIM_CR2_MMS2_SHIFT;
251 master_mode_max = MASTER_MODE2_MAX;
254 shift = TIM_CR2_MMS_SHIFT;
255 master_mode_max = MASTER_MODE_MAX;
258 for (i = 0; i <= master_mode_max; i++) {
259 if (!strncmp(master_mode_table[i], buf,
260 strlen(master_mode_table[i]))) {
261 regmap_update_bits(priv->regmap, TIM_CR2, mask,
263 /* Make sure that registers are updated */
264 regmap_update_bits(priv->regmap, TIM_EGR,
265 TIM_EGR_UG, TIM_EGR_UG);
273 static ssize_t stm32_tt_show_master_mode_avail(struct device *dev,
274 struct device_attribute *attr,
277 struct iio_trigger *trig = to_iio_trigger(dev);
278 unsigned int i, master_mode_max;
281 if (stm32_timer_is_trgo2_name(trig->name))
282 master_mode_max = MASTER_MODE2_MAX;
284 master_mode_max = MASTER_MODE_MAX;
286 for (i = 0; i <= master_mode_max; i++)
287 len += scnprintf(buf + len, PAGE_SIZE - len,
288 "%s ", master_mode_table[i]);
290 /* replace trailing space by newline */
296 static IIO_DEVICE_ATTR(master_mode_available, 0444,
297 stm32_tt_show_master_mode_avail, NULL, 0);
299 static IIO_DEVICE_ATTR(master_mode, 0660,
300 stm32_tt_show_master_mode,
301 stm32_tt_store_master_mode,
304 static struct attribute *stm32_trigger_attrs[] = {
305 &iio_dev_attr_sampling_frequency.dev_attr.attr,
306 &iio_dev_attr_master_mode.dev_attr.attr,
307 &iio_dev_attr_master_mode_available.dev_attr.attr,
311 static const struct attribute_group stm32_trigger_attr_group = {
312 .attrs = stm32_trigger_attrs,
315 static const struct attribute_group *stm32_trigger_attr_groups[] = {
316 &stm32_trigger_attr_group,
320 static const struct iio_trigger_ops timer_trigger_ops = {
321 .owner = THIS_MODULE,
324 static int stm32_setup_iio_triggers(struct stm32_timer_trigger *priv)
327 const char * const *cur = priv->triggers;
329 while (cur && *cur) {
330 struct iio_trigger *trig;
331 bool cur_is_trgo2 = stm32_timer_is_trgo2_name(*cur);
333 if (cur_is_trgo2 && !priv->has_trgo2) {
338 trig = devm_iio_trigger_alloc(priv->dev, "%s", *cur);
342 trig->dev.parent = priv->dev->parent;
343 trig->ops = &timer_trigger_ops;
346 * sampling frequency and master mode attributes
347 * should only be available on trgo trigger which
348 * is always the first in the list.
350 if (cur == priv->triggers || cur_is_trgo2)
351 trig->dev.groups = stm32_trigger_attr_groups;
353 iio_trigger_set_drvdata(trig, priv);
355 ret = devm_iio_trigger_register(priv->dev, trig);
364 static int stm32_counter_read_raw(struct iio_dev *indio_dev,
365 struct iio_chan_spec const *chan,
366 int *val, int *val2, long mask)
368 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
371 case IIO_CHAN_INFO_RAW:
375 regmap_read(priv->regmap, TIM_CNT, &cnt);
380 case IIO_CHAN_INFO_SCALE:
384 regmap_read(priv->regmap, TIM_SMCR, &smcr);
385 smcr &= TIM_SMCR_SMS;
390 /* in quadrature case scale = 0.25 */
394 return IIO_VAL_FRACTIONAL_LOG2;
401 static int stm32_counter_write_raw(struct iio_dev *indio_dev,
402 struct iio_chan_spec const *chan,
403 int val, int val2, long mask)
405 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
408 case IIO_CHAN_INFO_RAW:
409 regmap_write(priv->regmap, TIM_CNT, val);
412 case IIO_CHAN_INFO_SCALE:
420 static int stm32_counter_validate_trigger(struct iio_dev *indio_dev,
421 struct iio_trigger *trig)
423 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
424 const char * const *cur = priv->valids;
427 if (!is_stm32_timer_trigger(trig))
430 while (cur && *cur) {
431 if (!strncmp(trig->name, *cur, strlen(trig->name))) {
432 regmap_update_bits(priv->regmap,
433 TIM_SMCR, TIM_SMCR_TS,
434 i << TIM_SMCR_TS_SHIFT);
444 static const struct iio_info stm32_trigger_info = {
445 .driver_module = THIS_MODULE,
446 .validate_trigger = stm32_counter_validate_trigger,
447 .read_raw = stm32_counter_read_raw,
448 .write_raw = stm32_counter_write_raw
451 static const char *const stm32_trigger_modes[] = {
455 static int stm32_set_trigger_mode(struct iio_dev *indio_dev,
456 const struct iio_chan_spec *chan,
459 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
461 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, TIM_SMCR_SMS);
466 static int stm32_get_trigger_mode(struct iio_dev *indio_dev,
467 const struct iio_chan_spec *chan)
469 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
472 regmap_read(priv->regmap, TIM_SMCR, &smcr);
474 return smcr == TIM_SMCR_SMS ? 0 : -EINVAL;
477 static const struct iio_enum stm32_trigger_mode_enum = {
478 .items = stm32_trigger_modes,
479 .num_items = ARRAY_SIZE(stm32_trigger_modes),
480 .set = stm32_set_trigger_mode,
481 .get = stm32_get_trigger_mode
484 static const char *const stm32_enable_modes[] = {
490 static int stm32_enable_mode2sms(int mode)
504 static int stm32_set_enable_mode(struct iio_dev *indio_dev,
505 const struct iio_chan_spec *chan,
508 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
509 int sms = stm32_enable_mode2sms(mode);
514 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
519 static int stm32_sms2enable_mode(int mode)
533 static int stm32_get_enable_mode(struct iio_dev *indio_dev,
534 const struct iio_chan_spec *chan)
536 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
539 regmap_read(priv->regmap, TIM_SMCR, &smcr);
540 smcr &= TIM_SMCR_SMS;
542 return stm32_sms2enable_mode(smcr);
545 static const struct iio_enum stm32_enable_mode_enum = {
546 .items = stm32_enable_modes,
547 .num_items = ARRAY_SIZE(stm32_enable_modes),
548 .set = stm32_set_enable_mode,
549 .get = stm32_get_enable_mode
552 static const char *const stm32_quadrature_modes[] = {
558 static int stm32_set_quadrature_mode(struct iio_dev *indio_dev,
559 const struct iio_chan_spec *chan,
562 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
564 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, mode + 1);
569 static int stm32_get_quadrature_mode(struct iio_dev *indio_dev,
570 const struct iio_chan_spec *chan)
572 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
575 regmap_read(priv->regmap, TIM_SMCR, &smcr);
576 smcr &= TIM_SMCR_SMS;
581 static const struct iio_enum stm32_quadrature_mode_enum = {
582 .items = stm32_quadrature_modes,
583 .num_items = ARRAY_SIZE(stm32_quadrature_modes),
584 .set = stm32_set_quadrature_mode,
585 .get = stm32_get_quadrature_mode
588 static const char *const stm32_count_direction_states[] = {
593 static int stm32_set_count_direction(struct iio_dev *indio_dev,
594 const struct iio_chan_spec *chan,
597 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
599 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_DIR, mode);
604 static int stm32_get_count_direction(struct iio_dev *indio_dev,
605 const struct iio_chan_spec *chan)
607 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
610 regmap_read(priv->regmap, TIM_CR1, &cr1);
612 return (cr1 & TIM_CR1_DIR);
615 static const struct iio_enum stm32_count_direction_enum = {
616 .items = stm32_count_direction_states,
617 .num_items = ARRAY_SIZE(stm32_count_direction_states),
618 .set = stm32_set_count_direction,
619 .get = stm32_get_count_direction
622 static ssize_t stm32_count_get_preset(struct iio_dev *indio_dev,
624 const struct iio_chan_spec *chan,
627 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
630 regmap_read(priv->regmap, TIM_ARR, &arr);
632 return snprintf(buf, PAGE_SIZE, "%u\n", arr);
635 static ssize_t stm32_count_set_preset(struct iio_dev *indio_dev,
637 const struct iio_chan_spec *chan,
638 const char *buf, size_t len)
640 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
644 ret = kstrtouint(buf, 0, &preset);
648 regmap_write(priv->regmap, TIM_ARR, preset);
649 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
654 static const struct iio_chan_spec_ext_info stm32_trigger_count_info[] = {
657 .shared = IIO_SEPARATE,
658 .read = stm32_count_get_preset,
659 .write = stm32_count_set_preset
661 IIO_ENUM("count_direction", IIO_SEPARATE, &stm32_count_direction_enum),
662 IIO_ENUM_AVAILABLE("count_direction", &stm32_count_direction_enum),
663 IIO_ENUM("quadrature_mode", IIO_SEPARATE, &stm32_quadrature_mode_enum),
664 IIO_ENUM_AVAILABLE("quadrature_mode", &stm32_quadrature_mode_enum),
665 IIO_ENUM("enable_mode", IIO_SEPARATE, &stm32_enable_mode_enum),
666 IIO_ENUM_AVAILABLE("enable_mode", &stm32_enable_mode_enum),
667 IIO_ENUM("trigger_mode", IIO_SEPARATE, &stm32_trigger_mode_enum),
668 IIO_ENUM_AVAILABLE("trigger_mode", &stm32_trigger_mode_enum),
672 static const struct iio_chan_spec stm32_trigger_channel = {
675 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
676 .ext_info = stm32_trigger_count_info,
680 static struct stm32_timer_trigger *stm32_setup_counter_device(struct device *dev)
682 struct iio_dev *indio_dev;
685 indio_dev = devm_iio_device_alloc(dev,
686 sizeof(struct stm32_timer_trigger));
690 indio_dev->name = dev_name(dev);
691 indio_dev->dev.parent = dev;
692 indio_dev->info = &stm32_trigger_info;
693 indio_dev->modes = INDIO_HARDWARE_TRIGGERED;
694 indio_dev->num_channels = 1;
695 indio_dev->channels = &stm32_trigger_channel;
696 indio_dev->dev.of_node = dev->of_node;
698 ret = devm_iio_device_register(dev, indio_dev);
702 return iio_priv(indio_dev);
706 * is_stm32_timer_trigger
707 * @trig: trigger to be checked
709 * return true if the trigger is a valid stm32 iio timer trigger
710 * either return false
712 bool is_stm32_timer_trigger(struct iio_trigger *trig)
714 return (trig->ops == &timer_trigger_ops);
716 EXPORT_SYMBOL(is_stm32_timer_trigger);
718 static void stm32_timer_detect_trgo2(struct stm32_timer_trigger *priv)
723 * Master mode selection 2 bits can only be written and read back when
726 regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2, TIM_CR2_MMS2);
727 regmap_read(priv->regmap, TIM_CR2, &val);
728 regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2, 0);
729 priv->has_trgo2 = !!val;
732 static int stm32_timer_trigger_probe(struct platform_device *pdev)
734 struct device *dev = &pdev->dev;
735 struct stm32_timer_trigger *priv;
736 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
740 if (of_property_read_u32(dev->of_node, "reg", &index))
743 if (index >= ARRAY_SIZE(triggers_table) ||
744 index >= ARRAY_SIZE(valids_table))
747 /* Create an IIO device only if we have triggers to be validated */
748 if (*valids_table[index])
749 priv = stm32_setup_counter_device(dev);
751 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
757 priv->regmap = ddata->regmap;
758 priv->clk = ddata->clk;
759 priv->max_arr = ddata->max_arr;
760 priv->triggers = triggers_table[index];
761 priv->valids = valids_table[index];
762 stm32_timer_detect_trgo2(priv);
764 ret = stm32_setup_iio_triggers(priv);
768 platform_set_drvdata(pdev, priv);
773 static const struct of_device_id stm32_trig_of_match[] = {
774 { .compatible = "st,stm32-timer-trigger", },
777 MODULE_DEVICE_TABLE(of, stm32_trig_of_match);
779 static struct platform_driver stm32_timer_trigger_driver = {
780 .probe = stm32_timer_trigger_probe,
782 .name = "stm32-timer-trigger",
783 .of_match_table = stm32_trig_of_match,
786 module_platform_driver(stm32_timer_trigger_driver);
788 MODULE_ALIAS("platform: stm32-timer-trigger");
789 MODULE_DESCRIPTION("STMicroelectronics STM32 Timer Trigger driver");
790 MODULE_LICENSE("GPL v2");