···11+What: /sys/bus/iio/devices/triggerX/master_mode_available22+KernelVersion: 4.1133+Contact: benjamin.gaignard@st.com44+Description:55+ Reading returns the list possible master modes which are:66+ - "reset" : The UG bit from the TIMx_EGR register is used as trigger output (TRGO).77+ - "enable" : The Counter Enable signal CNT_EN is used as trigger output.88+ - "update" : The update event is selected as trigger output.99+ For instance a master timer can then be used as a prescaler for a slave timer.1010+ - "compare_pulse" : The trigger output send a positive pulse when the CC1IF flag is to be set.1111+ - "OC1REF" : OC1REF signal is used as trigger output.1212+ - "OC2REF" : OC2REF signal is used as trigger output.1313+ - "OC3REF" : OC3REF signal is used as trigger output.1414+ - "OC4REF" : OC4REF signal is used as trigger output.1515+1616+What: /sys/bus/iio/devices/triggerX/master_mode1717+KernelVersion: 4.111818+Contact: benjamin.gaignard@st.com1919+Description:2020+ Reading returns the current master modes.2121+ Writing set the master mode2222+2323+What: /sys/bus/iio/devices/triggerX/sampling_frequency2424+KernelVersion: 4.112525+Contact: benjamin.gaignard@st.com2626+Description:2727+ Reading returns the current sampling frequency.2828+ Writing an value different of 0 set and start sampling.2929+ Writing 0 stop sampling.
···11+STM32 Timers driver bindings22+33+This IP provides 3 types of timer along with PWM functionality:44+- advanced-control timers consist of a 16-bit auto-reload counter driven by a programmable55+ prescaler, break input feature, PWM outputs and complementary PWM ouputs channels.66+- general-purpose timers consist of a 16-bit or 32-bit auto-reload counter driven by a77+ programmable prescaler and PWM outputs.88+- basic timers consist of a 16-bit auto-reload counter driven by a programmable prescaler.99+1010+Required parameters:1111+- compatible: must be "st,stm32-timers"1212+1313+- reg: Physical base address and length of the controller's1414+ registers.1515+- clock-names: Set to "int".1616+- clocks: Phandle to the clock used by the timer module.1717+ For Clk properties, please refer to ../clock/clock-bindings.txt1818+1919+Optional parameters:2020+- resets: Phandle to the parent reset controller.2121+ See ../reset/st,stm32-rcc.txt2222+2323+Optional subnodes:2424+- pwm: See ../pwm/pwm-stm32.txt2525+- timer: See ../iio/timer/stm32-timer-trigger.txt2626+2727+Example:2828+ timers@40010000 {2929+ #address-cells = <1>;3030+ #size-cells = <0>;3131+ compatible = "st,stm32-timers";3232+ reg = <0x40010000 0x400>;3333+ clocks = <&rcc 0 160>;3434+ clock-names = "clk_int";3535+3636+ pwm {3737+ compatible = "st,stm32-pwm";3838+ pinctrl-0 = <&pwm1_pins>;3939+ pinctrl-names = "default";4040+ };4141+4242+ timer@0 {4343+ compatible = "st,stm32-timer-trigger";4444+ reg = <0>;4545+ };4646+ };
···11+STMicroelectronics STM32 Timers PWM bindings22+33+Must be a sub-node of an STM32 Timers device tree node.44+See ../mfd/stm32-timers.txt for details about the parent node.55+66+Required parameters:77+- compatible: Must be "st,stm32-pwm".88+- pinctrl-names: Set to "default".99+- pinctrl-0: List of phandles pointing to pin configuration nodes for PWM module.1010+ For Pinctrl properties see ../pinctrl/pinctrl-bindings.txt1111+1212+Optional parameters:1313+- st,breakinput: One or two <index level filter> to describe break input configurations.1414+ "index" indicates on which break input (0 or 1) the configuration1515+ should be applied.1616+ "level" gives the active level (0=low or 1=high) of the input signal1717+ for this configuration.1818+ "filter" gives the filtering value to be applied.1919+2020+Example:2121+ timers@40010000 {2222+ #address-cells = <1>;2323+ #size-cells = <0>;2424+ compatible = "st,stm32-timers";2525+ reg = <0x40010000 0x400>;2626+ clocks = <&rcc 0 160>;2727+ clock-names = "clk_int";2828+2929+ pwm {3030+ compatible = "st,stm32-pwm";3131+ pinctrl-0 = <&pwm1_pins>;3232+ pinctrl-names = "default";3333+ st,breakinput = <0 1 5>;3434+ };3535+ };
+9
drivers/iio/trigger/Kconfig
···2424 To compile this driver as a module, choose M here: the2525 module will be called iio-trig-interrupt.26262727+config IIO_STM32_TIMER_TRIGGER2828+ tristate "STM32 Timer Trigger"2929+ depends on (ARCH_STM32 && OF && MFD_STM32_TIMERS) || COMPILE_TEST3030+ help3131+ Select this option to enable STM32 Timer Trigger3232+3333+ To compile this driver as a module, choose M here: the3434+ module will be called stm32-timer-trigger.3535+2736config IIO_TIGHTLOOP_TRIGGER2837 tristate "A kthread based hammering loop trigger"2938 depends on IIO_SW_TRIGGER
···11+/*22+ * Copyright (C) STMicroelectronics 201633+ *44+ * Author: Benjamin Gaignard <benjamin.gaignard@st.com>55+ *66+ * License terms: GNU General Public License (GPL), version 277+ */88+99+#include <linux/iio/iio.h>1010+#include <linux/iio/sysfs.h>1111+#include <linux/iio/timer/stm32-timer-trigger.h>1212+#include <linux/iio/trigger.h>1313+#include <linux/mfd/stm32-timers.h>1414+#include <linux/module.h>1515+#include <linux/platform_device.h>1616+1717+#define MAX_TRIGGERS 61818+1919+/* List the triggers created by each timer */2020+static const void *triggers_table[][MAX_TRIGGERS] = {2121+ { TIM1_TRGO, TIM1_CH1, TIM1_CH2, TIM1_CH3, TIM1_CH4,},2222+ { TIM2_TRGO, TIM2_CH1, TIM2_CH2, TIM2_CH3, TIM2_CH4,},2323+ { TIM3_TRGO, TIM3_CH1, TIM3_CH2, TIM3_CH3, TIM3_CH4,},2424+ { TIM4_TRGO, TIM4_CH1, TIM4_CH2, TIM4_CH3, TIM4_CH4,},2525+ { TIM5_TRGO, TIM5_CH1, TIM5_CH2, TIM5_CH3, TIM5_CH4,},2626+ { TIM6_TRGO,},2727+ { TIM7_TRGO,},2828+ { TIM8_TRGO, TIM8_CH1, TIM8_CH2, TIM8_CH3, TIM8_CH4,},2929+ { TIM9_TRGO, TIM9_CH1, TIM9_CH2,},3030+ { }, /* timer 10 */3131+ { }, /* timer 11 */3232+ { TIM12_TRGO, TIM12_CH1, TIM12_CH2,},3333+};3434+3535+struct stm32_timer_trigger {3636+ struct device *dev;3737+ struct regmap *regmap;3838+ struct clk *clk;3939+ u32 max_arr;4040+ const void *triggers;4141+};4242+4343+static int stm32_timer_start(struct stm32_timer_trigger *priv,4444+ unsigned int frequency)4545+{4646+ unsigned long long prd, div;4747+ int prescaler = 0;4848+ u32 ccer, cr1;4949+5050+ /* Period and prescaler values depends of clock rate */5151+ div = (unsigned long long)clk_get_rate(priv->clk);5252+5353+ do_div(div, frequency);5454+5555+ prd = div;5656+5757+ /*5858+ * Increase prescaler value until we get a result that fit5959+ * with auto reload register maximum value.6060+ */6161+ while (div > priv->max_arr) {6262+ prescaler++;6363+ div = prd;6464+ do_div(div, (prescaler + 1));6565+ }6666+ prd = div;6767+6868+ if (prescaler > MAX_TIM_PSC) {6969+ dev_err(priv->dev, "prescaler exceeds the maximum value\n");7070+ return -EINVAL;7171+ }7272+7373+ /* Check if nobody else use the timer */7474+ regmap_read(priv->regmap, TIM_CCER, &ccer);7575+ if (ccer & TIM_CCER_CCXE)7676+ return -EBUSY;7777+7878+ regmap_read(priv->regmap, TIM_CR1, &cr1);7979+ if (!(cr1 & TIM_CR1_CEN))8080+ clk_enable(priv->clk);8181+8282+ regmap_write(priv->regmap, TIM_PSC, prescaler);8383+ regmap_write(priv->regmap, TIM_ARR, prd - 1);8484+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);8585+8686+ /* Force master mode to update mode */8787+ regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS, 0x20);8888+8989+ /* Make sure that registers are updated */9090+ regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);9191+9292+ /* Enable controller */9393+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);9494+9595+ return 0;9696+}9797+9898+static void stm32_timer_stop(struct stm32_timer_trigger *priv)9999+{100100+ u32 ccer, cr1;101101+102102+ regmap_read(priv->regmap, TIM_CCER, &ccer);103103+ if (ccer & TIM_CCER_CCXE)104104+ return;105105+106106+ regmap_read(priv->regmap, TIM_CR1, &cr1);107107+ if (cr1 & TIM_CR1_CEN)108108+ clk_disable(priv->clk);109109+110110+ /* Stop timer */111111+ regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);112112+ regmap_write(priv->regmap, TIM_PSC, 0);113113+ regmap_write(priv->regmap, TIM_ARR, 0);114114+115115+ /* Make sure that registers are updated */116116+ regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);117117+}118118+119119+static ssize_t stm32_tt_store_frequency(struct device *dev,120120+ struct device_attribute *attr,121121+ const char *buf, size_t len)122122+{123123+ struct iio_trigger *trig = to_iio_trigger(dev);124124+ struct stm32_timer_trigger *priv = iio_trigger_get_drvdata(trig);125125+ unsigned int freq;126126+ int ret;127127+128128+ ret = kstrtouint(buf, 10, &freq);129129+ if (ret)130130+ return ret;131131+132132+ if (freq == 0) {133133+ stm32_timer_stop(priv);134134+ } else {135135+ ret = stm32_timer_start(priv, freq);136136+ if (ret)137137+ return ret;138138+ }139139+140140+ return len;141141+}142142+143143+static ssize_t stm32_tt_read_frequency(struct device *dev,144144+ struct device_attribute *attr, char *buf)145145+{146146+ struct iio_trigger *trig = to_iio_trigger(dev);147147+ struct stm32_timer_trigger *priv = iio_trigger_get_drvdata(trig);148148+ u32 psc, arr, cr1;149149+ unsigned long long freq = 0;150150+151151+ regmap_read(priv->regmap, TIM_CR1, &cr1);152152+ regmap_read(priv->regmap, TIM_PSC, &psc);153153+ regmap_read(priv->regmap, TIM_ARR, &arr);154154+155155+ if (psc && arr && (cr1 & TIM_CR1_CEN)) {156156+ freq = (unsigned long long)clk_get_rate(priv->clk);157157+ do_div(freq, psc);158158+ do_div(freq, arr);159159+ }160160+161161+ return sprintf(buf, "%d\n", (unsigned int)freq);162162+}163163+164164+static IIO_DEV_ATTR_SAMP_FREQ(0660,165165+ stm32_tt_read_frequency,166166+ stm32_tt_store_frequency);167167+168168+static char *master_mode_table[] = {169169+ "reset",170170+ "enable",171171+ "update",172172+ "compare_pulse",173173+ "OC1REF",174174+ "OC2REF",175175+ "OC3REF",176176+ "OC4REF"177177+};178178+179179+static ssize_t stm32_tt_show_master_mode(struct device *dev,180180+ struct device_attribute *attr,181181+ char *buf)182182+{183183+ struct iio_dev *indio_dev = dev_to_iio_dev(dev);184184+ struct stm32_timer_trigger *priv = iio_priv(indio_dev);185185+ u32 cr2;186186+187187+ regmap_read(priv->regmap, TIM_CR2, &cr2);188188+ cr2 = (cr2 & TIM_CR2_MMS) >> TIM_CR2_MMS_SHIFT;189189+190190+ return snprintf(buf, PAGE_SIZE, "%s\n", master_mode_table[cr2]);191191+}192192+193193+static ssize_t stm32_tt_store_master_mode(struct device *dev,194194+ struct device_attribute *attr,195195+ const char *buf, size_t len)196196+{197197+ struct iio_dev *indio_dev = dev_to_iio_dev(dev);198198+ struct stm32_timer_trigger *priv = iio_priv(indio_dev);199199+ int i;200200+201201+ for (i = 0; i < ARRAY_SIZE(master_mode_table); i++) {202202+ if (!strncmp(master_mode_table[i], buf,203203+ strlen(master_mode_table[i]))) {204204+ regmap_update_bits(priv->regmap, TIM_CR2,205205+ TIM_CR2_MMS, i << TIM_CR2_MMS_SHIFT);206206+ /* Make sure that registers are updated */207207+ regmap_update_bits(priv->regmap, TIM_EGR,208208+ TIM_EGR_UG, TIM_EGR_UG);209209+ return len;210210+ }211211+ }212212+213213+ return -EINVAL;214214+}215215+216216+static IIO_CONST_ATTR(master_mode_available,217217+ "reset enable update compare_pulse OC1REF OC2REF OC3REF OC4REF");218218+219219+static IIO_DEVICE_ATTR(master_mode, 0660,220220+ stm32_tt_show_master_mode,221221+ stm32_tt_store_master_mode,222222+ 0);223223+224224+static struct attribute *stm32_trigger_attrs[] = {225225+ &iio_dev_attr_sampling_frequency.dev_attr.attr,226226+ &iio_dev_attr_master_mode.dev_attr.attr,227227+ &iio_const_attr_master_mode_available.dev_attr.attr,228228+ NULL,229229+};230230+231231+static const struct attribute_group stm32_trigger_attr_group = {232232+ .attrs = stm32_trigger_attrs,233233+};234234+235235+static const struct attribute_group *stm32_trigger_attr_groups[] = {236236+ &stm32_trigger_attr_group,237237+ NULL,238238+};239239+240240+static const struct iio_trigger_ops timer_trigger_ops = {241241+ .owner = THIS_MODULE,242242+};243243+244244+static int stm32_setup_iio_triggers(struct stm32_timer_trigger *priv)245245+{246246+ int ret;247247+ const char * const *cur = priv->triggers;248248+249249+ while (cur && *cur) {250250+ struct iio_trigger *trig;251251+252252+ trig = devm_iio_trigger_alloc(priv->dev, "%s", *cur);253253+ if (!trig)254254+ return -ENOMEM;255255+256256+ trig->dev.parent = priv->dev->parent;257257+ trig->ops = &timer_trigger_ops;258258+259259+ /*260260+ * sampling frequency and master mode attributes261261+ * should only be available on trgo trigger which262262+ * is always the first in the list.263263+ */264264+ if (cur == priv->triggers)265265+ trig->dev.groups = stm32_trigger_attr_groups;266266+267267+ iio_trigger_set_drvdata(trig, priv);268268+269269+ ret = devm_iio_trigger_register(priv->dev, trig);270270+ if (ret)271271+ return ret;272272+ cur++;273273+ }274274+275275+ return 0;276276+}277277+278278+/**279279+ * is_stm32_timer_trigger280280+ * @trig: trigger to be checked281281+ *282282+ * return true if the trigger is a valid stm32 iio timer trigger283283+ * either return false284284+ */285285+bool is_stm32_timer_trigger(struct iio_trigger *trig)286286+{287287+ return (trig->ops == &timer_trigger_ops);288288+}289289+EXPORT_SYMBOL(is_stm32_timer_trigger);290290+291291+static int stm32_timer_trigger_probe(struct platform_device *pdev)292292+{293293+ struct device *dev = &pdev->dev;294294+ struct stm32_timer_trigger *priv;295295+ struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);296296+ unsigned int index;297297+ int ret;298298+299299+ if (of_property_read_u32(dev->of_node, "reg", &index))300300+ return -EINVAL;301301+302302+ if (index >= ARRAY_SIZE(triggers_table))303303+ return -EINVAL;304304+305305+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);306306+307307+ if (!priv)308308+ return -ENOMEM;309309+310310+ priv->dev = dev;311311+ priv->regmap = ddata->regmap;312312+ priv->clk = ddata->clk;313313+ priv->max_arr = ddata->max_arr;314314+ priv->triggers = triggers_table[index];315315+316316+ ret = stm32_setup_iio_triggers(priv);317317+ if (ret)318318+ return ret;319319+320320+ platform_set_drvdata(pdev, priv);321321+322322+ return 0;323323+}324324+325325+static const struct of_device_id stm32_trig_of_match[] = {326326+ { .compatible = "st,stm32-timer-trigger", },327327+ { /* end node */ },328328+};329329+MODULE_DEVICE_TABLE(of, stm32_trig_of_match);330330+331331+static struct platform_driver stm32_timer_trigger_driver = {332332+ .probe = stm32_timer_trigger_probe,333333+ .driver = {334334+ .name = "stm32-timer-trigger",335335+ .of_match_table = stm32_trig_of_match,336336+ },337337+};338338+module_platform_driver(stm32_timer_trigger_driver);339339+340340+MODULE_ALIAS("platform: stm32-timer-trigger");341341+MODULE_DESCRIPTION("STMicroelectronics STM32 Timer Trigger driver");342342+MODULE_LICENSE("GPL v2");
+11
drivers/mfd/Kconfig
···16211621 in various ST Microelectronics and ST-Ericsson embedded16221622 Nomadik series.1623162316241624+config MFD_STM32_TIMERS16251625+ tristate "Support for STM32 Timers"16261626+ depends on (ARCH_STM32 && OF) || COMPILE_TEST16271627+ select MFD_CORE16281628+ select REGMAP16291629+ select REGMAP_MMIO16301630+ help16311631+ Select this option to enable STM32 timers driver used16321632+ for PWM and IIO Timer. This driver allow to share the16331633+ registers between the others drivers.16341634+16241635menu "Multimedia Capabilities Port drivers"16251636 depends on ARCH_SA110016261637
···11+/*22+ * Copyright (C) STMicroelectronics 201633+ *44+ * Author: Benjamin Gaignard <benjamin.gaignard@st.com>55+ *66+ * License terms: GNU General Public License (GPL), version 277+ */88+99+#include <linux/mfd/stm32-timers.h>1010+#include <linux/module.h>1111+#include <linux/of_platform.h>1212+#include <linux/reset.h>1313+1414+static const struct regmap_config stm32_timers_regmap_cfg = {1515+ .reg_bits = 32,1616+ .val_bits = 32,1717+ .reg_stride = sizeof(u32),1818+ .max_register = 0x400,1919+};2020+2121+static void stm32_timers_get_arr_size(struct stm32_timers *ddata)2222+{2323+ /*2424+ * Only the available bits will be written so when readback2525+ * we get the maximum value of auto reload register2626+ */2727+ regmap_write(ddata->regmap, TIM_ARR, ~0L);2828+ regmap_read(ddata->regmap, TIM_ARR, &ddata->max_arr);2929+ regmap_write(ddata->regmap, TIM_ARR, 0x0);3030+}3131+3232+static int stm32_timers_probe(struct platform_device *pdev)3333+{3434+ struct device *dev = &pdev->dev;3535+ struct stm32_timers *ddata;3636+ struct resource *res;3737+ void __iomem *mmio;3838+3939+ ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);4040+ if (!ddata)4141+ return -ENOMEM;4242+4343+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);4444+ mmio = devm_ioremap_resource(dev, res);4545+ if (IS_ERR(mmio))4646+ return PTR_ERR(mmio);4747+4848+ ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio,4949+ &stm32_timers_regmap_cfg);5050+ if (IS_ERR(ddata->regmap))5151+ return PTR_ERR(ddata->regmap);5252+5353+ ddata->clk = devm_clk_get(dev, NULL);5454+ if (IS_ERR(ddata->clk))5555+ return PTR_ERR(ddata->clk);5656+5757+ stm32_timers_get_arr_size(ddata);5858+5959+ platform_set_drvdata(pdev, ddata);6060+6161+ return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);6262+}6363+6464+static const struct of_device_id stm32_timers_of_match[] = {6565+ { .compatible = "st,stm32-timers", },6666+ { /* end node */ },6767+};6868+MODULE_DEVICE_TABLE(of, stm32_timers_of_match);6969+7070+static struct platform_driver stm32_timers_driver = {7171+ .probe = stm32_timers_probe,7272+ .driver = {7373+ .name = "stm32-timers",7474+ .of_match_table = stm32_timers_of_match,7575+ },7676+};7777+module_platform_driver(stm32_timers_driver);7878+7979+MODULE_DESCRIPTION("STMicroelectronics STM32 Timers");8080+MODULE_LICENSE("GPL v2");
+9
drivers/pwm/Kconfig
···397397 To compile this driver as a module, choose M here: the module398398 will be called pwm-sti.399399400400+config PWM_STM32401401+ tristate "STMicroelectronics STM32 PWM"402402+ depends on MFD_STM32_TIMERS || COMPILE_TEST403403+ help404404+ Generic PWM framework driver for STM32 SoCs.405405+406406+ To compile this driver as a module, choose M here: the module407407+ will be called pwm-stm32.408408+400409config PWM_STMPE401410 bool "STMPE expander PWM export"402411 depends on MFD_STMPE