Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'ib-mfd-iio-pwm-4.11' into test

Immutable branch between MFD, IIO and PWM due for the v4.11 merge window

Pulled into IIO to allow follow up series of triggered capture for the
STM32 ADCs.

+1118
+29
Documentation/ABI/testing/sysfs-bus-iio-timer-stm32
··· 1 + What: /sys/bus/iio/devices/triggerX/master_mode_available 2 + KernelVersion: 4.11 3 + Contact: benjamin.gaignard@st.com 4 + Description: 5 + Reading returns the list possible master modes which are: 6 + - "reset" : The UG bit from the TIMx_EGR register is used as trigger output (TRGO). 7 + - "enable" : The Counter Enable signal CNT_EN is used as trigger output. 8 + - "update" : The update event is selected as trigger output. 9 + For instance a master timer can then be used as a prescaler for a slave timer. 10 + - "compare_pulse" : The trigger output send a positive pulse when the CC1IF flag is to be set. 11 + - "OC1REF" : OC1REF signal is used as trigger output. 12 + - "OC2REF" : OC2REF signal is used as trigger output. 13 + - "OC3REF" : OC3REF signal is used as trigger output. 14 + - "OC4REF" : OC4REF signal is used as trigger output. 15 + 16 + What: /sys/bus/iio/devices/triggerX/master_mode 17 + KernelVersion: 4.11 18 + Contact: benjamin.gaignard@st.com 19 + Description: 20 + Reading returns the current master modes. 21 + Writing set the master mode 22 + 23 + What: /sys/bus/iio/devices/triggerX/sampling_frequency 24 + KernelVersion: 4.11 25 + Contact: benjamin.gaignard@st.com 26 + Description: 27 + Reading returns the current sampling frequency. 28 + Writing an value different of 0 set and start sampling. 29 + Writing 0 stop sampling.
+23
Documentation/devicetree/bindings/iio/timer/stm32-timer-trigger.txt
··· 1 + STMicroelectronics STM32 Timers IIO timer bindings 2 + 3 + Must be a sub-node of an STM32 Timers device tree node. 4 + See ../mfd/stm32-timers.txt for details about the parent node. 5 + 6 + Required parameters: 7 + - compatible: Must be "st,stm32-timer-trigger". 8 + - reg: Identify trigger hardware block. 9 + 10 + Example: 11 + timers@40010000 { 12 + #address-cells = <1>; 13 + #size-cells = <0>; 14 + compatible = "st,stm32-timers"; 15 + reg = <0x40010000 0x400>; 16 + clocks = <&rcc 0 160>; 17 + clock-names = "clk_int"; 18 + 19 + timer@0 { 20 + compatible = "st,stm32-timer-trigger"; 21 + reg = <0>; 22 + }; 23 + };
+46
Documentation/devicetree/bindings/mfd/stm32-timers.txt
··· 1 + STM32 Timers driver bindings 2 + 3 + This IP provides 3 types of timer along with PWM functionality: 4 + - advanced-control timers consist of a 16-bit auto-reload counter driven by a programmable 5 + prescaler, break input feature, PWM outputs and complementary PWM ouputs channels. 6 + - general-purpose timers consist of a 16-bit or 32-bit auto-reload counter driven by a 7 + programmable prescaler and PWM outputs. 8 + - basic timers consist of a 16-bit auto-reload counter driven by a programmable prescaler. 9 + 10 + Required parameters: 11 + - compatible: must be "st,stm32-timers" 12 + 13 + - reg: Physical base address and length of the controller's 14 + registers. 15 + - clock-names: Set to "int". 16 + - clocks: Phandle to the clock used by the timer module. 17 + For Clk properties, please refer to ../clock/clock-bindings.txt 18 + 19 + Optional parameters: 20 + - resets: Phandle to the parent reset controller. 21 + See ../reset/st,stm32-rcc.txt 22 + 23 + Optional subnodes: 24 + - pwm: See ../pwm/pwm-stm32.txt 25 + - timer: See ../iio/timer/stm32-timer-trigger.txt 26 + 27 + Example: 28 + timers@40010000 { 29 + #address-cells = <1>; 30 + #size-cells = <0>; 31 + compatible = "st,stm32-timers"; 32 + reg = <0x40010000 0x400>; 33 + clocks = <&rcc 0 160>; 34 + clock-names = "clk_int"; 35 + 36 + pwm { 37 + compatible = "st,stm32-pwm"; 38 + pinctrl-0 = <&pwm1_pins>; 39 + pinctrl-names = "default"; 40 + }; 41 + 42 + timer@0 { 43 + compatible = "st,stm32-timer-trigger"; 44 + reg = <0>; 45 + }; 46 + };
+35
Documentation/devicetree/bindings/pwm/pwm-stm32.txt
··· 1 + STMicroelectronics STM32 Timers PWM bindings 2 + 3 + Must be a sub-node of an STM32 Timers device tree node. 4 + See ../mfd/stm32-timers.txt for details about the parent node. 5 + 6 + Required parameters: 7 + - compatible: Must be "st,stm32-pwm". 8 + - pinctrl-names: Set to "default". 9 + - pinctrl-0: List of phandles pointing to pin configuration nodes for PWM module. 10 + For Pinctrl properties see ../pinctrl/pinctrl-bindings.txt 11 + 12 + Optional parameters: 13 + - st,breakinput: One or two <index level filter> to describe break input configurations. 14 + "index" indicates on which break input (0 or 1) the configuration 15 + should be applied. 16 + "level" gives the active level (0=low or 1=high) of the input signal 17 + for this configuration. 18 + "filter" gives the filtering value to be applied. 19 + 20 + Example: 21 + timers@40010000 { 22 + #address-cells = <1>; 23 + #size-cells = <0>; 24 + compatible = "st,stm32-timers"; 25 + reg = <0x40010000 0x400>; 26 + clocks = <&rcc 0 160>; 27 + clock-names = "clk_int"; 28 + 29 + pwm { 30 + compatible = "st,stm32-pwm"; 31 + pinctrl-0 = <&pwm1_pins>; 32 + pinctrl-names = "default"; 33 + st,breakinput = <0 1 5>; 34 + }; 35 + };
+9
drivers/iio/trigger/Kconfig
··· 24 24 To compile this driver as a module, choose M here: the 25 25 module will be called iio-trig-interrupt. 26 26 27 + config IIO_STM32_TIMER_TRIGGER 28 + tristate "STM32 Timer Trigger" 29 + depends on (ARCH_STM32 && OF && MFD_STM32_TIMERS) || COMPILE_TEST 30 + help 31 + Select this option to enable STM32 Timer Trigger 32 + 33 + To compile this driver as a module, choose M here: the 34 + module will be called stm32-timer-trigger. 35 + 27 36 config IIO_TIGHTLOOP_TRIGGER 28 37 tristate "A kthread based hammering loop trigger" 29 38 depends on IIO_SW_TRIGGER
+1
drivers/iio/trigger/Makefile
··· 6 6 7 7 obj-$(CONFIG_IIO_HRTIMER_TRIGGER) += iio-trig-hrtimer.o 8 8 obj-$(CONFIG_IIO_INTERRUPT_TRIGGER) += iio-trig-interrupt.o 9 + obj-$(CONFIG_IIO_STM32_TIMER_TRIGGER) += stm32-timer-trigger.o 9 10 obj-$(CONFIG_IIO_SYSFS_TRIGGER) += iio-trig-sysfs.o 10 11 obj-$(CONFIG_IIO_TIGHTLOOP_TRIGGER) += iio-trig-loop.o
+342
drivers/iio/trigger/stm32-timer-trigger.c
··· 1 + /* 2 + * Copyright (C) STMicroelectronics 2016 3 + * 4 + * Author: Benjamin Gaignard <benjamin.gaignard@st.com> 5 + * 6 + * License terms: GNU General Public License (GPL), version 2 7 + */ 8 + 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> 16 + 17 + #define MAX_TRIGGERS 6 18 + 19 + /* List the triggers created by each timer */ 20 + static const void *triggers_table[][MAX_TRIGGERS] = { 21 + { TIM1_TRGO, TIM1_CH1, TIM1_CH2, TIM1_CH3, TIM1_CH4,}, 22 + { TIM2_TRGO, TIM2_CH1, TIM2_CH2, TIM2_CH3, TIM2_CH4,}, 23 + { TIM3_TRGO, TIM3_CH1, TIM3_CH2, TIM3_CH3, TIM3_CH4,}, 24 + { TIM4_TRGO, TIM4_CH1, TIM4_CH2, TIM4_CH3, TIM4_CH4,}, 25 + { TIM5_TRGO, TIM5_CH1, TIM5_CH2, TIM5_CH3, TIM5_CH4,}, 26 + { TIM6_TRGO,}, 27 + { TIM7_TRGO,}, 28 + { TIM8_TRGO, TIM8_CH1, TIM8_CH2, TIM8_CH3, TIM8_CH4,}, 29 + { TIM9_TRGO, TIM9_CH1, TIM9_CH2,}, 30 + { }, /* timer 10 */ 31 + { }, /* timer 11 */ 32 + { TIM12_TRGO, TIM12_CH1, TIM12_CH2,}, 33 + }; 34 + 35 + struct stm32_timer_trigger { 36 + struct device *dev; 37 + struct regmap *regmap; 38 + struct clk *clk; 39 + u32 max_arr; 40 + const void *triggers; 41 + }; 42 + 43 + static int stm32_timer_start(struct stm32_timer_trigger *priv, 44 + unsigned int frequency) 45 + { 46 + unsigned long long prd, div; 47 + int prescaler = 0; 48 + u32 ccer, cr1; 49 + 50 + /* Period and prescaler values depends of clock rate */ 51 + div = (unsigned long long)clk_get_rate(priv->clk); 52 + 53 + do_div(div, frequency); 54 + 55 + prd = div; 56 + 57 + /* 58 + * Increase prescaler value until we get a result that fit 59 + * with auto reload register maximum value. 60 + */ 61 + while (div > priv->max_arr) { 62 + prescaler++; 63 + div = prd; 64 + do_div(div, (prescaler + 1)); 65 + } 66 + prd = div; 67 + 68 + if (prescaler > MAX_TIM_PSC) { 69 + dev_err(priv->dev, "prescaler exceeds the maximum value\n"); 70 + return -EINVAL; 71 + } 72 + 73 + /* Check if nobody else use the timer */ 74 + regmap_read(priv->regmap, TIM_CCER, &ccer); 75 + if (ccer & TIM_CCER_CCXE) 76 + return -EBUSY; 77 + 78 + regmap_read(priv->regmap, TIM_CR1, &cr1); 79 + if (!(cr1 & TIM_CR1_CEN)) 80 + clk_enable(priv->clk); 81 + 82 + regmap_write(priv->regmap, TIM_PSC, prescaler); 83 + regmap_write(priv->regmap, TIM_ARR, prd - 1); 84 + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE); 85 + 86 + /* Force master mode to update mode */ 87 + regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS, 0x20); 88 + 89 + /* Make sure that registers are updated */ 90 + regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG); 91 + 92 + /* Enable controller */ 93 + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN); 94 + 95 + return 0; 96 + } 97 + 98 + static void stm32_timer_stop(struct stm32_timer_trigger *priv) 99 + { 100 + u32 ccer, cr1; 101 + 102 + regmap_read(priv->regmap, TIM_CCER, &ccer); 103 + if (ccer & TIM_CCER_CCXE) 104 + return; 105 + 106 + regmap_read(priv->regmap, TIM_CR1, &cr1); 107 + if (cr1 & TIM_CR1_CEN) 108 + clk_disable(priv->clk); 109 + 110 + /* Stop timer */ 111 + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0); 112 + regmap_write(priv->regmap, TIM_PSC, 0); 113 + regmap_write(priv->regmap, TIM_ARR, 0); 114 + 115 + /* Make sure that registers are updated */ 116 + regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG); 117 + } 118 + 119 + static ssize_t stm32_tt_store_frequency(struct device *dev, 120 + struct device_attribute *attr, 121 + const char *buf, size_t len) 122 + { 123 + struct iio_trigger *trig = to_iio_trigger(dev); 124 + struct stm32_timer_trigger *priv = iio_trigger_get_drvdata(trig); 125 + unsigned int freq; 126 + int ret; 127 + 128 + ret = kstrtouint(buf, 10, &freq); 129 + if (ret) 130 + return ret; 131 + 132 + if (freq == 0) { 133 + stm32_timer_stop(priv); 134 + } else { 135 + ret = stm32_timer_start(priv, freq); 136 + if (ret) 137 + return ret; 138 + } 139 + 140 + return len; 141 + } 142 + 143 + static ssize_t stm32_tt_read_frequency(struct device *dev, 144 + struct device_attribute *attr, char *buf) 145 + { 146 + struct iio_trigger *trig = to_iio_trigger(dev); 147 + struct stm32_timer_trigger *priv = iio_trigger_get_drvdata(trig); 148 + u32 psc, arr, cr1; 149 + unsigned long long freq = 0; 150 + 151 + regmap_read(priv->regmap, TIM_CR1, &cr1); 152 + regmap_read(priv->regmap, TIM_PSC, &psc); 153 + regmap_read(priv->regmap, TIM_ARR, &arr); 154 + 155 + if (psc && arr && (cr1 & TIM_CR1_CEN)) { 156 + freq = (unsigned long long)clk_get_rate(priv->clk); 157 + do_div(freq, psc); 158 + do_div(freq, arr); 159 + } 160 + 161 + return sprintf(buf, "%d\n", (unsigned int)freq); 162 + } 163 + 164 + static IIO_DEV_ATTR_SAMP_FREQ(0660, 165 + stm32_tt_read_frequency, 166 + stm32_tt_store_frequency); 167 + 168 + static char *master_mode_table[] = { 169 + "reset", 170 + "enable", 171 + "update", 172 + "compare_pulse", 173 + "OC1REF", 174 + "OC2REF", 175 + "OC3REF", 176 + "OC4REF" 177 + }; 178 + 179 + static ssize_t stm32_tt_show_master_mode(struct device *dev, 180 + struct device_attribute *attr, 181 + char *buf) 182 + { 183 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 184 + struct stm32_timer_trigger *priv = iio_priv(indio_dev); 185 + u32 cr2; 186 + 187 + regmap_read(priv->regmap, TIM_CR2, &cr2); 188 + cr2 = (cr2 & TIM_CR2_MMS) >> TIM_CR2_MMS_SHIFT; 189 + 190 + return snprintf(buf, PAGE_SIZE, "%s\n", master_mode_table[cr2]); 191 + } 192 + 193 + static ssize_t stm32_tt_store_master_mode(struct device *dev, 194 + struct device_attribute *attr, 195 + const char *buf, size_t len) 196 + { 197 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 198 + struct stm32_timer_trigger *priv = iio_priv(indio_dev); 199 + int i; 200 + 201 + for (i = 0; i < ARRAY_SIZE(master_mode_table); i++) { 202 + if (!strncmp(master_mode_table[i], buf, 203 + strlen(master_mode_table[i]))) { 204 + regmap_update_bits(priv->regmap, TIM_CR2, 205 + TIM_CR2_MMS, i << TIM_CR2_MMS_SHIFT); 206 + /* Make sure that registers are updated */ 207 + regmap_update_bits(priv->regmap, TIM_EGR, 208 + TIM_EGR_UG, TIM_EGR_UG); 209 + return len; 210 + } 211 + } 212 + 213 + return -EINVAL; 214 + } 215 + 216 + static IIO_CONST_ATTR(master_mode_available, 217 + "reset enable update compare_pulse OC1REF OC2REF OC3REF OC4REF"); 218 + 219 + static IIO_DEVICE_ATTR(master_mode, 0660, 220 + stm32_tt_show_master_mode, 221 + stm32_tt_store_master_mode, 222 + 0); 223 + 224 + static struct attribute *stm32_trigger_attrs[] = { 225 + &iio_dev_attr_sampling_frequency.dev_attr.attr, 226 + &iio_dev_attr_master_mode.dev_attr.attr, 227 + &iio_const_attr_master_mode_available.dev_attr.attr, 228 + NULL, 229 + }; 230 + 231 + static const struct attribute_group stm32_trigger_attr_group = { 232 + .attrs = stm32_trigger_attrs, 233 + }; 234 + 235 + static const struct attribute_group *stm32_trigger_attr_groups[] = { 236 + &stm32_trigger_attr_group, 237 + NULL, 238 + }; 239 + 240 + static const struct iio_trigger_ops timer_trigger_ops = { 241 + .owner = THIS_MODULE, 242 + }; 243 + 244 + static int stm32_setup_iio_triggers(struct stm32_timer_trigger *priv) 245 + { 246 + int ret; 247 + const char * const *cur = priv->triggers; 248 + 249 + while (cur && *cur) { 250 + struct iio_trigger *trig; 251 + 252 + trig = devm_iio_trigger_alloc(priv->dev, "%s", *cur); 253 + if (!trig) 254 + return -ENOMEM; 255 + 256 + trig->dev.parent = priv->dev->parent; 257 + trig->ops = &timer_trigger_ops; 258 + 259 + /* 260 + * sampling frequency and master mode attributes 261 + * should only be available on trgo trigger which 262 + * is always the first in the list. 263 + */ 264 + if (cur == priv->triggers) 265 + trig->dev.groups = stm32_trigger_attr_groups; 266 + 267 + iio_trigger_set_drvdata(trig, priv); 268 + 269 + ret = devm_iio_trigger_register(priv->dev, trig); 270 + if (ret) 271 + return ret; 272 + cur++; 273 + } 274 + 275 + return 0; 276 + } 277 + 278 + /** 279 + * is_stm32_timer_trigger 280 + * @trig: trigger to be checked 281 + * 282 + * return true if the trigger is a valid stm32 iio timer trigger 283 + * either return false 284 + */ 285 + bool is_stm32_timer_trigger(struct iio_trigger *trig) 286 + { 287 + return (trig->ops == &timer_trigger_ops); 288 + } 289 + EXPORT_SYMBOL(is_stm32_timer_trigger); 290 + 291 + static int stm32_timer_trigger_probe(struct platform_device *pdev) 292 + { 293 + struct device *dev = &pdev->dev; 294 + struct stm32_timer_trigger *priv; 295 + struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent); 296 + unsigned int index; 297 + int ret; 298 + 299 + if (of_property_read_u32(dev->of_node, "reg", &index)) 300 + return -EINVAL; 301 + 302 + if (index >= ARRAY_SIZE(triggers_table)) 303 + return -EINVAL; 304 + 305 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 306 + 307 + if (!priv) 308 + return -ENOMEM; 309 + 310 + priv->dev = dev; 311 + priv->regmap = ddata->regmap; 312 + priv->clk = ddata->clk; 313 + priv->max_arr = ddata->max_arr; 314 + priv->triggers = triggers_table[index]; 315 + 316 + ret = stm32_setup_iio_triggers(priv); 317 + if (ret) 318 + return ret; 319 + 320 + platform_set_drvdata(pdev, priv); 321 + 322 + return 0; 323 + } 324 + 325 + static const struct of_device_id stm32_trig_of_match[] = { 326 + { .compatible = "st,stm32-timer-trigger", }, 327 + { /* end node */ }, 328 + }; 329 + MODULE_DEVICE_TABLE(of, stm32_trig_of_match); 330 + 331 + static struct platform_driver stm32_timer_trigger_driver = { 332 + .probe = stm32_timer_trigger_probe, 333 + .driver = { 334 + .name = "stm32-timer-trigger", 335 + .of_match_table = stm32_trig_of_match, 336 + }, 337 + }; 338 + module_platform_driver(stm32_timer_trigger_driver); 339 + 340 + MODULE_ALIAS("platform: stm32-timer-trigger"); 341 + MODULE_DESCRIPTION("STMicroelectronics STM32 Timer Trigger driver"); 342 + MODULE_LICENSE("GPL v2");
+11
drivers/mfd/Kconfig
··· 1621 1621 in various ST Microelectronics and ST-Ericsson embedded 1622 1622 Nomadik series. 1623 1623 1624 + config MFD_STM32_TIMERS 1625 + tristate "Support for STM32 Timers" 1626 + depends on (ARCH_STM32 && OF) || COMPILE_TEST 1627 + select MFD_CORE 1628 + select REGMAP 1629 + select REGMAP_MMIO 1630 + help 1631 + Select this option to enable STM32 timers driver used 1632 + for PWM and IIO Timer. This driver allow to share the 1633 + registers between the others drivers. 1634 + 1624 1635 menu "Multimedia Capabilities Port drivers" 1625 1636 depends on ARCH_SA1100 1626 1637
+2
drivers/mfd/Makefile
··· 212 212 213 213 obj-$(CONFIG_MFD_ALTERA_A10SR) += altera-a10sr.o 214 214 obj-$(CONFIG_MFD_SUN4I_GPADC) += sun4i-gpadc.o 215 + 216 + obj-$(CONFIG_MFD_STM32_TIMERS) += stm32-timers.o
+80
drivers/mfd/stm32-timers.c
··· 1 + /* 2 + * Copyright (C) STMicroelectronics 2016 3 + * 4 + * Author: Benjamin Gaignard <benjamin.gaignard@st.com> 5 + * 6 + * License terms: GNU General Public License (GPL), version 2 7 + */ 8 + 9 + #include <linux/mfd/stm32-timers.h> 10 + #include <linux/module.h> 11 + #include <linux/of_platform.h> 12 + #include <linux/reset.h> 13 + 14 + static const struct regmap_config stm32_timers_regmap_cfg = { 15 + .reg_bits = 32, 16 + .val_bits = 32, 17 + .reg_stride = sizeof(u32), 18 + .max_register = 0x400, 19 + }; 20 + 21 + static void stm32_timers_get_arr_size(struct stm32_timers *ddata) 22 + { 23 + /* 24 + * Only the available bits will be written so when readback 25 + * we get the maximum value of auto reload register 26 + */ 27 + regmap_write(ddata->regmap, TIM_ARR, ~0L); 28 + regmap_read(ddata->regmap, TIM_ARR, &ddata->max_arr); 29 + regmap_write(ddata->regmap, TIM_ARR, 0x0); 30 + } 31 + 32 + static int stm32_timers_probe(struct platform_device *pdev) 33 + { 34 + struct device *dev = &pdev->dev; 35 + struct stm32_timers *ddata; 36 + struct resource *res; 37 + void __iomem *mmio; 38 + 39 + ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL); 40 + if (!ddata) 41 + return -ENOMEM; 42 + 43 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 44 + mmio = devm_ioremap_resource(dev, res); 45 + if (IS_ERR(mmio)) 46 + return PTR_ERR(mmio); 47 + 48 + ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio, 49 + &stm32_timers_regmap_cfg); 50 + if (IS_ERR(ddata->regmap)) 51 + return PTR_ERR(ddata->regmap); 52 + 53 + ddata->clk = devm_clk_get(dev, NULL); 54 + if (IS_ERR(ddata->clk)) 55 + return PTR_ERR(ddata->clk); 56 + 57 + stm32_timers_get_arr_size(ddata); 58 + 59 + platform_set_drvdata(pdev, ddata); 60 + 61 + return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); 62 + } 63 + 64 + static const struct of_device_id stm32_timers_of_match[] = { 65 + { .compatible = "st,stm32-timers", }, 66 + { /* end node */ }, 67 + }; 68 + MODULE_DEVICE_TABLE(of, stm32_timers_of_match); 69 + 70 + static struct platform_driver stm32_timers_driver = { 71 + .probe = stm32_timers_probe, 72 + .driver = { 73 + .name = "stm32-timers", 74 + .of_match_table = stm32_timers_of_match, 75 + }, 76 + }; 77 + module_platform_driver(stm32_timers_driver); 78 + 79 + MODULE_DESCRIPTION("STMicroelectronics STM32 Timers"); 80 + MODULE_LICENSE("GPL v2");
+9
drivers/pwm/Kconfig
··· 397 397 To compile this driver as a module, choose M here: the module 398 398 will be called pwm-sti. 399 399 400 + config PWM_STM32 401 + tristate "STMicroelectronics STM32 PWM" 402 + depends on MFD_STM32_TIMERS || COMPILE_TEST 403 + help 404 + Generic PWM framework driver for STM32 SoCs. 405 + 406 + To compile this driver as a module, choose M here: the module 407 + will be called pwm-stm32. 408 + 400 409 config PWM_STMPE 401 410 bool "STMPE expander PWM export" 402 411 depends on MFD_STMPE
+1
drivers/pwm/Makefile
··· 38 38 obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o 39 39 obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o 40 40 obj-$(CONFIG_PWM_STI) += pwm-sti.o 41 + obj-$(CONFIG_PWM_STM32) += pwm-stm32.o 41 42 obj-$(CONFIG_PWM_STMPE) += pwm-stmpe.o 42 43 obj-$(CONFIG_PWM_SUN4I) += pwm-sun4i.o 43 44 obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
+397
drivers/pwm/pwm-stm32.c
··· 1 + /* 2 + * Copyright (C) STMicroelectronics 2016 3 + * 4 + * Author: Gerald Baeza <gerald.baeza@st.com> 5 + * 6 + * License terms: GNU General Public License (GPL), version 2 7 + * 8 + * Inspired by timer-stm32.c from Maxime Coquelin 9 + * pwm-atmel.c from Bo Shen 10 + */ 11 + 12 + #include <linux/mfd/stm32-timers.h> 13 + #include <linux/module.h> 14 + #include <linux/of.h> 15 + #include <linux/platform_device.h> 16 + #include <linux/pwm.h> 17 + 18 + #define CCMR_CHANNEL_SHIFT 8 19 + #define CCMR_CHANNEL_MASK 0xFF 20 + #define MAX_BREAKINPUT 2 21 + 22 + struct stm32_pwm { 23 + struct pwm_chip chip; 24 + struct device *dev; 25 + struct clk *clk; 26 + struct regmap *regmap; 27 + u32 max_arr; 28 + bool have_complementary_output; 29 + }; 30 + 31 + struct stm32_breakinput { 32 + u32 index; 33 + u32 level; 34 + u32 filter; 35 + }; 36 + 37 + static inline struct stm32_pwm *to_stm32_pwm_dev(struct pwm_chip *chip) 38 + { 39 + return container_of(chip, struct stm32_pwm, chip); 40 + } 41 + 42 + static u32 active_channels(struct stm32_pwm *dev) 43 + { 44 + u32 ccer; 45 + 46 + regmap_read(dev->regmap, TIM_CCER, &ccer); 47 + 48 + return ccer & TIM_CCER_CCXE; 49 + } 50 + 51 + static int write_ccrx(struct stm32_pwm *dev, int ch, u32 value) 52 + { 53 + switch (ch) { 54 + case 0: 55 + return regmap_write(dev->regmap, TIM_CCR1, value); 56 + case 1: 57 + return regmap_write(dev->regmap, TIM_CCR2, value); 58 + case 2: 59 + return regmap_write(dev->regmap, TIM_CCR3, value); 60 + case 3: 61 + return regmap_write(dev->regmap, TIM_CCR4, value); 62 + } 63 + return -EINVAL; 64 + } 65 + 66 + static int stm32_pwm_config(struct stm32_pwm *priv, int ch, 67 + int duty_ns, int period_ns) 68 + { 69 + unsigned long long prd, div, dty; 70 + unsigned int prescaler = 0; 71 + u32 ccmr, mask, shift; 72 + 73 + /* Period and prescaler values depends on clock rate */ 74 + div = (unsigned long long)clk_get_rate(priv->clk) * period_ns; 75 + 76 + do_div(div, NSEC_PER_SEC); 77 + prd = div; 78 + 79 + while (div > priv->max_arr) { 80 + prescaler++; 81 + div = prd; 82 + do_div(div, prescaler + 1); 83 + } 84 + 85 + prd = div; 86 + 87 + if (prescaler > MAX_TIM_PSC) 88 + return -EINVAL; 89 + 90 + /* 91 + * All channels share the same prescaler and counter so when two 92 + * channels are active at the same time we can't change them 93 + */ 94 + if (active_channels(priv) & ~(1 << ch * 4)) { 95 + u32 psc, arr; 96 + 97 + regmap_read(priv->regmap, TIM_PSC, &psc); 98 + regmap_read(priv->regmap, TIM_ARR, &arr); 99 + 100 + if ((psc != prescaler) || (arr != prd - 1)) 101 + return -EBUSY; 102 + } 103 + 104 + regmap_write(priv->regmap, TIM_PSC, prescaler); 105 + regmap_write(priv->regmap, TIM_ARR, prd - 1); 106 + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE); 107 + 108 + /* Calculate the duty cycles */ 109 + dty = prd * duty_ns; 110 + do_div(dty, period_ns); 111 + 112 + write_ccrx(priv, ch, dty); 113 + 114 + /* Configure output mode */ 115 + shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT; 116 + ccmr = (TIM_CCMR_PE | TIM_CCMR_M1) << shift; 117 + mask = CCMR_CHANNEL_MASK << shift; 118 + 119 + if (ch < 2) 120 + regmap_update_bits(priv->regmap, TIM_CCMR1, mask, ccmr); 121 + else 122 + regmap_update_bits(priv->regmap, TIM_CCMR2, mask, ccmr); 123 + 124 + regmap_update_bits(priv->regmap, TIM_BDTR, 125 + TIM_BDTR_MOE | TIM_BDTR_AOE, 126 + TIM_BDTR_MOE | TIM_BDTR_AOE); 127 + 128 + return 0; 129 + } 130 + 131 + static int stm32_pwm_set_polarity(struct stm32_pwm *priv, int ch, 132 + enum pwm_polarity polarity) 133 + { 134 + u32 mask; 135 + 136 + mask = TIM_CCER_CC1P << (ch * 4); 137 + if (priv->have_complementary_output) 138 + mask |= TIM_CCER_CC1NP << (ch * 4); 139 + 140 + regmap_update_bits(priv->regmap, TIM_CCER, mask, 141 + polarity == PWM_POLARITY_NORMAL ? 0 : mask); 142 + 143 + return 0; 144 + } 145 + 146 + static int stm32_pwm_enable(struct stm32_pwm *priv, int ch) 147 + { 148 + u32 mask; 149 + int ret; 150 + 151 + ret = clk_enable(priv->clk); 152 + if (ret) 153 + return ret; 154 + 155 + /* Enable channel */ 156 + mask = TIM_CCER_CC1E << (ch * 4); 157 + if (priv->have_complementary_output) 158 + mask |= TIM_CCER_CC1NE << (ch * 4); 159 + 160 + regmap_update_bits(priv->regmap, TIM_CCER, mask, mask); 161 + 162 + /* Make sure that registers are updated */ 163 + regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG); 164 + 165 + /* Enable controller */ 166 + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN); 167 + 168 + return 0; 169 + } 170 + 171 + static void stm32_pwm_disable(struct stm32_pwm *priv, int ch) 172 + { 173 + u32 mask; 174 + 175 + /* Disable channel */ 176 + mask = TIM_CCER_CC1E << (ch * 4); 177 + if (priv->have_complementary_output) 178 + mask |= TIM_CCER_CC1NE << (ch * 4); 179 + 180 + regmap_update_bits(priv->regmap, TIM_CCER, mask, 0); 181 + 182 + /* When all channels are disabled, we can disable the controller */ 183 + if (!active_channels(priv)) 184 + regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0); 185 + 186 + clk_disable(priv->clk); 187 + } 188 + 189 + static int stm32_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 190 + struct pwm_state *state) 191 + { 192 + bool enabled; 193 + struct stm32_pwm *priv = to_stm32_pwm_dev(chip); 194 + int ret; 195 + 196 + enabled = pwm->state.enabled; 197 + 198 + if (enabled && !state->enabled) { 199 + stm32_pwm_disable(priv, pwm->hwpwm); 200 + return 0; 201 + } 202 + 203 + if (state->polarity != pwm->state.polarity) 204 + stm32_pwm_set_polarity(priv, pwm->hwpwm, state->polarity); 205 + 206 + ret = stm32_pwm_config(priv, pwm->hwpwm, 207 + state->duty_cycle, state->period); 208 + if (ret) 209 + return ret; 210 + 211 + if (!enabled && state->enabled) 212 + ret = stm32_pwm_enable(priv, pwm->hwpwm); 213 + 214 + return ret; 215 + } 216 + 217 + static const struct pwm_ops stm32pwm_ops = { 218 + .owner = THIS_MODULE, 219 + .apply = stm32_pwm_apply, 220 + }; 221 + 222 + static int stm32_pwm_set_breakinput(struct stm32_pwm *priv, 223 + int index, int level, int filter) 224 + { 225 + u32 bke = (index == 0) ? TIM_BDTR_BKE : TIM_BDTR_BK2E; 226 + int shift = (index == 0) ? TIM_BDTR_BKF_SHIFT : TIM_BDTR_BK2F_SHIFT; 227 + u32 mask = (index == 0) ? TIM_BDTR_BKE | TIM_BDTR_BKP | TIM_BDTR_BKF 228 + : TIM_BDTR_BK2E | TIM_BDTR_BK2P | TIM_BDTR_BK2F; 229 + u32 bdtr = bke; 230 + 231 + /* 232 + * The both bits could be set since only one will be wrote 233 + * due to mask value. 234 + */ 235 + if (level) 236 + bdtr |= TIM_BDTR_BKP | TIM_BDTR_BK2P; 237 + 238 + bdtr |= (filter & TIM_BDTR_BKF_MASK) << shift; 239 + 240 + regmap_update_bits(priv->regmap, TIM_BDTR, mask, bdtr); 241 + 242 + regmap_read(priv->regmap, TIM_BDTR, &bdtr); 243 + 244 + return (bdtr & bke) ? 0 : -EINVAL; 245 + } 246 + 247 + static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv, 248 + struct device_node *np) 249 + { 250 + struct stm32_breakinput breakinput[MAX_BREAKINPUT]; 251 + int nb, ret, i, array_size; 252 + 253 + nb = of_property_count_elems_of_size(np, "st,breakinput", 254 + sizeof(struct stm32_breakinput)); 255 + 256 + /* 257 + * Because "st,breakinput" parameter is optional do not make probe 258 + * failed if it doesn't exist. 259 + */ 260 + if (nb <= 0) 261 + return 0; 262 + 263 + if (nb > MAX_BREAKINPUT) 264 + return -EINVAL; 265 + 266 + array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32); 267 + ret = of_property_read_u32_array(np, "st,breakinput", 268 + (u32 *)breakinput, array_size); 269 + if (ret) 270 + return ret; 271 + 272 + for (i = 0; i < nb && !ret; i++) { 273 + ret = stm32_pwm_set_breakinput(priv, 274 + breakinput[i].index, 275 + breakinput[i].level, 276 + breakinput[i].filter); 277 + } 278 + 279 + return ret; 280 + } 281 + 282 + static void stm32_pwm_detect_complementary(struct stm32_pwm *priv) 283 + { 284 + u32 ccer; 285 + 286 + /* 287 + * If complementary bit doesn't exist writing 1 will have no 288 + * effect so we can detect it. 289 + */ 290 + regmap_update_bits(priv->regmap, 291 + TIM_CCER, TIM_CCER_CC1NE, TIM_CCER_CC1NE); 292 + regmap_read(priv->regmap, TIM_CCER, &ccer); 293 + regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CC1NE, 0); 294 + 295 + priv->have_complementary_output = (ccer != 0); 296 + } 297 + 298 + static int stm32_pwm_detect_channels(struct stm32_pwm *priv) 299 + { 300 + u32 ccer; 301 + int npwm = 0; 302 + 303 + /* 304 + * If channels enable bits don't exist writing 1 will have no 305 + * effect so we can detect and count them. 306 + */ 307 + regmap_update_bits(priv->regmap, 308 + TIM_CCER, TIM_CCER_CCXE, TIM_CCER_CCXE); 309 + regmap_read(priv->regmap, TIM_CCER, &ccer); 310 + regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE, 0); 311 + 312 + if (ccer & TIM_CCER_CC1E) 313 + npwm++; 314 + 315 + if (ccer & TIM_CCER_CC2E) 316 + npwm++; 317 + 318 + if (ccer & TIM_CCER_CC3E) 319 + npwm++; 320 + 321 + if (ccer & TIM_CCER_CC4E) 322 + npwm++; 323 + 324 + return npwm; 325 + } 326 + 327 + static int stm32_pwm_probe(struct platform_device *pdev) 328 + { 329 + struct device *dev = &pdev->dev; 330 + struct device_node *np = dev->of_node; 331 + struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent); 332 + struct stm32_pwm *priv; 333 + int ret; 334 + 335 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 336 + if (!priv) 337 + return -ENOMEM; 338 + 339 + priv->regmap = ddata->regmap; 340 + priv->clk = ddata->clk; 341 + priv->max_arr = ddata->max_arr; 342 + 343 + if (!priv->regmap || !priv->clk) 344 + return -EINVAL; 345 + 346 + ret = stm32_pwm_apply_breakinputs(priv, np); 347 + if (ret) 348 + return ret; 349 + 350 + stm32_pwm_detect_complementary(priv); 351 + 352 + priv->chip.base = -1; 353 + priv->chip.dev = dev; 354 + priv->chip.ops = &stm32pwm_ops; 355 + priv->chip.npwm = stm32_pwm_detect_channels(priv); 356 + 357 + ret = pwmchip_add(&priv->chip); 358 + if (ret < 0) 359 + return ret; 360 + 361 + platform_set_drvdata(pdev, priv); 362 + 363 + return 0; 364 + } 365 + 366 + static int stm32_pwm_remove(struct platform_device *pdev) 367 + { 368 + struct stm32_pwm *priv = platform_get_drvdata(pdev); 369 + unsigned int i; 370 + 371 + for (i = 0; i < priv->chip.npwm; i++) 372 + pwm_disable(&priv->chip.pwms[i]); 373 + 374 + pwmchip_remove(&priv->chip); 375 + 376 + return 0; 377 + } 378 + 379 + static const struct of_device_id stm32_pwm_of_match[] = { 380 + { .compatible = "st,stm32-pwm", }, 381 + { /* end node */ }, 382 + }; 383 + MODULE_DEVICE_TABLE(of, stm32_pwm_of_match); 384 + 385 + static struct platform_driver stm32_pwm_driver = { 386 + .probe = stm32_pwm_probe, 387 + .remove = stm32_pwm_remove, 388 + .driver = { 389 + .name = "stm32-pwm", 390 + .of_match_table = stm32_pwm_of_match, 391 + }, 392 + }; 393 + module_platform_driver(stm32_pwm_driver); 394 + 395 + MODULE_ALIAS("platform:stm32-pwm"); 396 + MODULE_DESCRIPTION("STMicroelectronics STM32 PWM driver"); 397 + MODULE_LICENSE("GPL v2");
+62
include/linux/iio/timer/stm32-timer-trigger.h
··· 1 + /* 2 + * Copyright (C) STMicroelectronics 2016 3 + * 4 + * Author: Benjamin Gaignard <benjamin.gaignard@st.com> 5 + * 6 + * License terms: GNU General Public License (GPL), version 2 7 + */ 8 + 9 + #ifndef _STM32_TIMER_TRIGGER_H_ 10 + #define _STM32_TIMER_TRIGGER_H_ 11 + 12 + #define TIM1_TRGO "tim1_trgo" 13 + #define TIM1_CH1 "tim1_ch1" 14 + #define TIM1_CH2 "tim1_ch2" 15 + #define TIM1_CH3 "tim1_ch3" 16 + #define TIM1_CH4 "tim1_ch4" 17 + 18 + #define TIM2_TRGO "tim2_trgo" 19 + #define TIM2_CH1 "tim2_ch1" 20 + #define TIM2_CH2 "tim2_ch2" 21 + #define TIM2_CH3 "tim2_ch3" 22 + #define TIM2_CH4 "tim2_ch4" 23 + 24 + #define TIM3_TRGO "tim3_trgo" 25 + #define TIM3_CH1 "tim3_ch1" 26 + #define TIM3_CH2 "tim3_ch2" 27 + #define TIM3_CH3 "tim3_ch3" 28 + #define TIM3_CH4 "tim3_ch4" 29 + 30 + #define TIM4_TRGO "tim4_trgo" 31 + #define TIM4_CH1 "tim4_ch1" 32 + #define TIM4_CH2 "tim4_ch2" 33 + #define TIM4_CH3 "tim4_ch3" 34 + #define TIM4_CH4 "tim4_ch4" 35 + 36 + #define TIM5_TRGO "tim5_trgo" 37 + #define TIM5_CH1 "tim5_ch1" 38 + #define TIM5_CH2 "tim5_ch2" 39 + #define TIM5_CH3 "tim5_ch3" 40 + #define TIM5_CH4 "tim5_ch4" 41 + 42 + #define TIM6_TRGO "tim6_trgo" 43 + 44 + #define TIM7_TRGO "tim7_trgo" 45 + 46 + #define TIM8_TRGO "tim8_trgo" 47 + #define TIM8_CH1 "tim8_ch1" 48 + #define TIM8_CH2 "tim8_ch2" 49 + #define TIM8_CH3 "tim8_ch3" 50 + #define TIM8_CH4 "tim8_ch4" 51 + 52 + #define TIM9_TRGO "tim9_trgo" 53 + #define TIM9_CH1 "tim9_ch1" 54 + #define TIM9_CH2 "tim9_ch2" 55 + 56 + #define TIM12_TRGO "tim12_trgo" 57 + #define TIM12_CH1 "tim12_ch1" 58 + #define TIM12_CH2 "tim12_ch2" 59 + 60 + bool is_stm32_timer_trigger(struct iio_trigger *trig); 61 + 62 + #endif
+71
include/linux/mfd/stm32-timers.h
··· 1 + /* 2 + * Copyright (C) STMicroelectronics 2016 3 + * 4 + * Author: Benjamin Gaignard <benjamin.gaignard@st.com> 5 + * 6 + * License terms: GNU General Public License (GPL), version 2 7 + */ 8 + 9 + #ifndef _LINUX_STM32_GPTIMER_H_ 10 + #define _LINUX_STM32_GPTIMER_H_ 11 + 12 + #include <linux/clk.h> 13 + #include <linux/regmap.h> 14 + 15 + #define TIM_CR1 0x00 /* Control Register 1 */ 16 + #define TIM_CR2 0x04 /* Control Register 2 */ 17 + #define TIM_SMCR 0x08 /* Slave mode control reg */ 18 + #define TIM_DIER 0x0C /* DMA/interrupt register */ 19 + #define TIM_SR 0x10 /* Status register */ 20 + #define TIM_EGR 0x14 /* Event Generation Reg */ 21 + #define TIM_CCMR1 0x18 /* Capt/Comp 1 Mode Reg */ 22 + #define TIM_CCMR2 0x1C /* Capt/Comp 2 Mode Reg */ 23 + #define TIM_CCER 0x20 /* Capt/Comp Enable Reg */ 24 + #define TIM_PSC 0x28 /* Prescaler */ 25 + #define TIM_ARR 0x2c /* Auto-Reload Register */ 26 + #define TIM_CCR1 0x34 /* Capt/Comp Register 1 */ 27 + #define TIM_CCR2 0x38 /* Capt/Comp Register 2 */ 28 + #define TIM_CCR3 0x3C /* Capt/Comp Register 3 */ 29 + #define TIM_CCR4 0x40 /* Capt/Comp Register 4 */ 30 + #define TIM_BDTR 0x44 /* Break and Dead-Time Reg */ 31 + 32 + #define TIM_CR1_CEN BIT(0) /* Counter Enable */ 33 + #define TIM_CR1_ARPE BIT(7) /* Auto-reload Preload Ena */ 34 + #define TIM_CR2_MMS (BIT(4) | BIT(5) | BIT(6)) /* Master mode selection */ 35 + #define TIM_SMCR_SMS (BIT(0) | BIT(1) | BIT(2)) /* Slave mode selection */ 36 + #define TIM_SMCR_TS (BIT(4) | BIT(5) | BIT(6)) /* Trigger selection */ 37 + #define TIM_DIER_UIE BIT(0) /* Update interrupt */ 38 + #define TIM_SR_UIF BIT(0) /* Update interrupt flag */ 39 + #define TIM_EGR_UG BIT(0) /* Update Generation */ 40 + #define TIM_CCMR_PE BIT(3) /* Channel Preload Enable */ 41 + #define TIM_CCMR_M1 (BIT(6) | BIT(5)) /* Channel PWM Mode 1 */ 42 + #define TIM_CCER_CC1E BIT(0) /* Capt/Comp 1 out Ena */ 43 + #define TIM_CCER_CC1P BIT(1) /* Capt/Comp 1 Polarity */ 44 + #define TIM_CCER_CC1NE BIT(2) /* Capt/Comp 1N out Ena */ 45 + #define TIM_CCER_CC1NP BIT(3) /* Capt/Comp 1N Polarity */ 46 + #define TIM_CCER_CC2E BIT(4) /* Capt/Comp 2 out Ena */ 47 + #define TIM_CCER_CC3E BIT(8) /* Capt/Comp 3 out Ena */ 48 + #define TIM_CCER_CC4E BIT(12) /* Capt/Comp 4 out Ena */ 49 + #define TIM_CCER_CCXE (BIT(0) | BIT(4) | BIT(8) | BIT(12)) 50 + #define TIM_BDTR_BKE BIT(12) /* Break input enable */ 51 + #define TIM_BDTR_BKP BIT(13) /* Break input polarity */ 52 + #define TIM_BDTR_AOE BIT(14) /* Automatic Output Enable */ 53 + #define TIM_BDTR_MOE BIT(15) /* Main Output Enable */ 54 + #define TIM_BDTR_BKF (BIT(16) | BIT(17) | BIT(18) | BIT(19)) 55 + #define TIM_BDTR_BK2F (BIT(20) | BIT(21) | BIT(22) | BIT(23)) 56 + #define TIM_BDTR_BK2E BIT(24) /* Break 2 input enable */ 57 + #define TIM_BDTR_BK2P BIT(25) /* Break 2 input polarity */ 58 + 59 + #define MAX_TIM_PSC 0xFFFF 60 + #define TIM_CR2_MMS_SHIFT 4 61 + #define TIM_SMCR_TS_SHIFT 4 62 + #define TIM_BDTR_BKF_MASK 0xF 63 + #define TIM_BDTR_BKF_SHIFT 16 64 + #define TIM_BDTR_BK2F_SHIFT 20 65 + 66 + struct stm32_timers { 67 + struct clk *clk; 68 + struct regmap *regmap; 69 + u32 max_arr; 70 + }; 71 + #endif