aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThierry Reding <thierry.reding@gmail.com>2018-06-06 04:15:04 -0400
committerThierry Reding <thierry.reding@gmail.com>2018-06-06 04:15:04 -0400
commit91348b1453a350da6bb7dda3adf6ea9d662e83b9 (patch)
treeb69616da4ec0aef809d22ff18a5fdd3c1e4c72a2
parentd968e5041fefbc7c4d545cabbc692b11cc49050d (diff)
parentacc8e22f5d41558c90519aadc011b6d2839aedfe (diff)
Merge tag 'ib-mfd-pwm-v4.18-1' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd into for-next
Immutable branch between MFD and PWM due for the v4.18 merge window (v2)
-rw-r--r--Documentation/devicetree/bindings/mfd/stm32-timers.txt20
-rw-r--r--drivers/mfd/stm32-timers.c201
-rw-r--r--drivers/pwm/pwm-stm32.c257
-rw-r--r--include/linux/mfd/stm32-timers.h58
4 files changed, 534 insertions, 2 deletions
diff --git a/Documentation/devicetree/bindings/mfd/stm32-timers.txt b/Documentation/devicetree/bindings/mfd/stm32-timers.txt
index 1db6e0057a63..0e900b52e895 100644
--- a/Documentation/devicetree/bindings/mfd/stm32-timers.txt
+++ b/Documentation/devicetree/bindings/mfd/stm32-timers.txt
@@ -19,6 +19,11 @@ Required parameters:
19Optional parameters: 19Optional parameters:
20- resets: Phandle to the parent reset controller. 20- resets: Phandle to the parent reset controller.
21 See ../reset/st,stm32-rcc.txt 21 See ../reset/st,stm32-rcc.txt
22- dmas: List of phandle to dma channels that can be used for
23 this timer instance. There may be up to 7 dma channels.
24- dma-names: List of dma names. Must match 'dmas' property. Valid
25 names are: "ch1", "ch2", "ch3", "ch4", "up", "trig",
26 "com".
22 27
23Optional subnodes: 28Optional subnodes:
24- pwm: See ../pwm/pwm-stm32.txt 29- pwm: See ../pwm/pwm-stm32.txt
@@ -44,3 +49,18 @@ Example:
44 reg = <0>; 49 reg = <0>;
45 }; 50 };
46 }; 51 };
52
53Example with all dmas:
54 timer@40010000 {
55 ...
56 dmas = <&dmamux1 11 0x400 0x0>,
57 <&dmamux1 12 0x400 0x0>,
58 <&dmamux1 13 0x400 0x0>,
59 <&dmamux1 14 0x400 0x0>,
60 <&dmamux1 15 0x400 0x0>,
61 <&dmamux1 16 0x400 0x0>,
62 <&dmamux1 17 0x400 0x0>;
63 dma-names = "ch1", "ch2", "ch3", "ch4", "up", "trig", "com";
64 ...
65 child nodes...
66 };
diff --git a/drivers/mfd/stm32-timers.c b/drivers/mfd/stm32-timers.c
index 1d347e5dfa79..efcd4b980c94 100644
--- a/drivers/mfd/stm32-timers.c
+++ b/drivers/mfd/stm32-timers.c
@@ -4,16 +4,156 @@
4 * Author: Benjamin Gaignard <benjamin.gaignard@st.com> 4 * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
5 */ 5 */
6 6
7#include <linux/bitfield.h>
7#include <linux/mfd/stm32-timers.h> 8#include <linux/mfd/stm32-timers.h>
8#include <linux/module.h> 9#include <linux/module.h>
9#include <linux/of_platform.h> 10#include <linux/of_platform.h>
10#include <linux/reset.h> 11#include <linux/reset.h>
11 12
13#define STM32_TIMERS_MAX_REGISTERS 0x3fc
14
15/* DIER register DMA enable bits */
16static const u32 stm32_timers_dier_dmaen[STM32_TIMERS_MAX_DMAS] = {
17 TIM_DIER_CC1DE,
18 TIM_DIER_CC2DE,
19 TIM_DIER_CC3DE,
20 TIM_DIER_CC4DE,
21 TIM_DIER_UIE,
22 TIM_DIER_TDE,
23 TIM_DIER_COMDE
24};
25
26static void stm32_timers_dma_done(void *p)
27{
28 struct stm32_timers_dma *dma = p;
29 struct dma_tx_state state;
30 enum dma_status status;
31
32 status = dmaengine_tx_status(dma->chan, dma->chan->cookie, &state);
33 if (status == DMA_COMPLETE)
34 complete(&dma->completion);
35}
36
37/**
38 * stm32_timers_dma_burst_read - Read from timers registers using DMA.
39 *
40 * Read from STM32 timers registers using DMA on a single event.
41 * @dev: reference to stm32_timers MFD device
42 * @buf: DMA'able destination buffer
43 * @id: stm32_timers_dmas event identifier (ch[1..4], up, trig or com)
44 * @reg: registers start offset for DMA to read from (like CCRx for capture)
45 * @num_reg: number of registers to read upon each DMA request, starting @reg.
46 * @bursts: number of bursts to read (e.g. like two for pwm period capture)
47 * @tmo_ms: timeout (milliseconds)
48 */
49int stm32_timers_dma_burst_read(struct device *dev, u32 *buf,
50 enum stm32_timers_dmas id, u32 reg,
51 unsigned int num_reg, unsigned int bursts,
52 unsigned long tmo_ms)
53{
54 struct stm32_timers *ddata = dev_get_drvdata(dev);
55 unsigned long timeout = msecs_to_jiffies(tmo_ms);
56 struct regmap *regmap = ddata->regmap;
57 struct stm32_timers_dma *dma = &ddata->dma;
58 size_t len = num_reg * bursts * sizeof(u32);
59 struct dma_async_tx_descriptor *desc;
60 struct dma_slave_config config;
61 dma_cookie_t cookie;
62 dma_addr_t dma_buf;
63 u32 dbl, dba;
64 long err;
65 int ret;
66
67 /* Sanity check */
68 if (id < STM32_TIMERS_DMA_CH1 || id >= STM32_TIMERS_MAX_DMAS)
69 return -EINVAL;
70
71 if (!num_reg || !bursts || reg > STM32_TIMERS_MAX_REGISTERS ||
72 (reg + num_reg * sizeof(u32)) > STM32_TIMERS_MAX_REGISTERS)
73 return -EINVAL;
74
75 if (!dma->chans[id])
76 return -ENODEV;
77 mutex_lock(&dma->lock);
78
79 /* Select DMA channel in use */
80 dma->chan = dma->chans[id];
81 dma_buf = dma_map_single(dev, buf, len, DMA_FROM_DEVICE);
82 if (dma_mapping_error(dev, dma_buf)) {
83 ret = -ENOMEM;
84 goto unlock;
85 }
86
87 /* Prepare DMA read from timer registers, using DMA burst mode */
88 memset(&config, 0, sizeof(config));
89 config.src_addr = (dma_addr_t)dma->phys_base + TIM_DMAR;
90 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
91 ret = dmaengine_slave_config(dma->chan, &config);
92 if (ret)
93 goto unmap;
94
95 desc = dmaengine_prep_slave_single(dma->chan, dma_buf, len,
96 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
97 if (!desc) {
98 ret = -EBUSY;
99 goto unmap;
100 }
101
102 desc->callback = stm32_timers_dma_done;
103 desc->callback_param = dma;
104 cookie = dmaengine_submit(desc);
105 ret = dma_submit_error(cookie);
106 if (ret)
107 goto dma_term;
108
109 reinit_completion(&dma->completion);
110 dma_async_issue_pending(dma->chan);
111
112 /* Setup and enable timer DMA burst mode */
113 dbl = FIELD_PREP(TIM_DCR_DBL, bursts - 1);
114 dba = FIELD_PREP(TIM_DCR_DBA, reg >> 2);
115 ret = regmap_write(regmap, TIM_DCR, dbl | dba);
116 if (ret)
117 goto dma_term;
118
119 /* Clear pending flags before enabling DMA request */
120 ret = regmap_write(regmap, TIM_SR, 0);
121 if (ret)
122 goto dcr_clr;
123
124 ret = regmap_update_bits(regmap, TIM_DIER, stm32_timers_dier_dmaen[id],
125 stm32_timers_dier_dmaen[id]);
126 if (ret)
127 goto dcr_clr;
128
129 err = wait_for_completion_interruptible_timeout(&dma->completion,
130 timeout);
131 if (err == 0)
132 ret = -ETIMEDOUT;
133 else if (err < 0)
134 ret = err;
135
136 regmap_update_bits(regmap, TIM_DIER, stm32_timers_dier_dmaen[id], 0);
137 regmap_write(regmap, TIM_SR, 0);
138dcr_clr:
139 regmap_write(regmap, TIM_DCR, 0);
140dma_term:
141 dmaengine_terminate_all(dma->chan);
142unmap:
143 dma_unmap_single(dev, dma_buf, len, DMA_FROM_DEVICE);
144unlock:
145 dma->chan = NULL;
146 mutex_unlock(&dma->lock);
147
148 return ret;
149}
150EXPORT_SYMBOL_GPL(stm32_timers_dma_burst_read);
151
12static const struct regmap_config stm32_timers_regmap_cfg = { 152static const struct regmap_config stm32_timers_regmap_cfg = {
13 .reg_bits = 32, 153 .reg_bits = 32,
14 .val_bits = 32, 154 .val_bits = 32,
15 .reg_stride = sizeof(u32), 155 .reg_stride = sizeof(u32),
16 .max_register = 0x3fc, 156 .max_register = STM32_TIMERS_MAX_REGISTERS,
17}; 157};
18 158
19static void stm32_timers_get_arr_size(struct stm32_timers *ddata) 159static void stm32_timers_get_arr_size(struct stm32_timers *ddata)
@@ -27,12 +167,45 @@ static void stm32_timers_get_arr_size(struct stm32_timers *ddata)
27 regmap_write(ddata->regmap, TIM_ARR, 0x0); 167 regmap_write(ddata->regmap, TIM_ARR, 0x0);
28} 168}
29 169
170static void stm32_timers_dma_probe(struct device *dev,
171 struct stm32_timers *ddata)
172{
173 int i;
174 char name[4];
175
176 init_completion(&ddata->dma.completion);
177 mutex_init(&ddata->dma.lock);
178
179 /* Optional DMA support: get valid DMA channel(s) or NULL */
180 for (i = STM32_TIMERS_DMA_CH1; i <= STM32_TIMERS_DMA_CH4; i++) {
181 snprintf(name, ARRAY_SIZE(name), "ch%1d", i + 1);
182 ddata->dma.chans[i] = dma_request_slave_channel(dev, name);
183 }
184 ddata->dma.chans[STM32_TIMERS_DMA_UP] =
185 dma_request_slave_channel(dev, "up");
186 ddata->dma.chans[STM32_TIMERS_DMA_TRIG] =
187 dma_request_slave_channel(dev, "trig");
188 ddata->dma.chans[STM32_TIMERS_DMA_COM] =
189 dma_request_slave_channel(dev, "com");
190}
191
192static void stm32_timers_dma_remove(struct device *dev,
193 struct stm32_timers *ddata)
194{
195 int i;
196
197 for (i = STM32_TIMERS_DMA_CH1; i < STM32_TIMERS_MAX_DMAS; i++)
198 if (ddata->dma.chans[i])
199 dma_release_channel(ddata->dma.chans[i]);
200}
201
30static int stm32_timers_probe(struct platform_device *pdev) 202static int stm32_timers_probe(struct platform_device *pdev)
31{ 203{
32 struct device *dev = &pdev->dev; 204 struct device *dev = &pdev->dev;
33 struct stm32_timers *ddata; 205 struct stm32_timers *ddata;
34 struct resource *res; 206 struct resource *res;
35 void __iomem *mmio; 207 void __iomem *mmio;
208 int ret;
36 209
37 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL); 210 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
38 if (!ddata) 211 if (!ddata)
@@ -43,6 +216,9 @@ static int stm32_timers_probe(struct platform_device *pdev)
43 if (IS_ERR(mmio)) 216 if (IS_ERR(mmio))
44 return PTR_ERR(mmio); 217 return PTR_ERR(mmio);
45 218
219 /* Timer physical addr for DMA */
220 ddata->dma.phys_base = res->start;
221
46 ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio, 222 ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio,
47 &stm32_timers_regmap_cfg); 223 &stm32_timers_regmap_cfg);
48 if (IS_ERR(ddata->regmap)) 224 if (IS_ERR(ddata->regmap))
@@ -54,9 +230,29 @@ static int stm32_timers_probe(struct platform_device *pdev)
54 230
55 stm32_timers_get_arr_size(ddata); 231 stm32_timers_get_arr_size(ddata);
56 232
233 stm32_timers_dma_probe(dev, ddata);
234
57 platform_set_drvdata(pdev, ddata); 235 platform_set_drvdata(pdev, ddata);
58 236
59 return devm_of_platform_populate(&pdev->dev); 237 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
238 if (ret)
239 stm32_timers_dma_remove(dev, ddata);
240
241 return ret;
242}
243
244static int stm32_timers_remove(struct platform_device *pdev)
245{
246 struct stm32_timers *ddata = platform_get_drvdata(pdev);
247
248 /*
249 * Don't use devm_ here: enfore of_platform_depopulate() happens before
250 * DMA are released, to avoid race on DMA.
251 */
252 of_platform_depopulate(&pdev->dev);
253 stm32_timers_dma_remove(&pdev->dev, ddata);
254
255 return 0;
60} 256}
61 257
62static const struct of_device_id stm32_timers_of_match[] = { 258static const struct of_device_id stm32_timers_of_match[] = {
@@ -67,6 +263,7 @@ MODULE_DEVICE_TABLE(of, stm32_timers_of_match);
67 263
68static struct platform_driver stm32_timers_driver = { 264static struct platform_driver stm32_timers_driver = {
69 .probe = stm32_timers_probe, 265 .probe = stm32_timers_probe,
266 .remove = stm32_timers_remove,
70 .driver = { 267 .driver = {
71 .name = "stm32-timers", 268 .name = "stm32-timers",
72 .of_match_table = stm32_timers_of_match, 269 .of_match_table = stm32_timers_of_match,
diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
index 2708212933f7..60bfc07c4912 100644
--- a/drivers/pwm/pwm-stm32.c
+++ b/drivers/pwm/pwm-stm32.c
@@ -8,6 +8,7 @@
8 * pwm-atmel.c from Bo Shen 8 * pwm-atmel.c from Bo Shen
9 */ 9 */
10 10
11#include <linux/bitfield.h>
11#include <linux/mfd/stm32-timers.h> 12#include <linux/mfd/stm32-timers.h>
12#include <linux/module.h> 13#include <linux/module.h>
13#include <linux/of.h> 14#include <linux/of.h>
@@ -25,6 +26,7 @@ struct stm32_pwm {
25 struct regmap *regmap; 26 struct regmap *regmap;
26 u32 max_arr; 27 u32 max_arr;
27 bool have_complementary_output; 28 bool have_complementary_output;
29 u32 capture[4] ____cacheline_aligned; /* DMA'able buffer */
28}; 30};
29 31
30struct stm32_breakinput { 32struct stm32_breakinput {
@@ -62,6 +64,258 @@ static int write_ccrx(struct stm32_pwm *dev, int ch, u32 value)
62 return -EINVAL; 64 return -EINVAL;
63} 65}
64 66
67#define TIM_CCER_CC12P (TIM_CCER_CC1P | TIM_CCER_CC2P)
68#define TIM_CCER_CC12E (TIM_CCER_CC1E | TIM_CCER_CC2E)
69#define TIM_CCER_CC34P (TIM_CCER_CC3P | TIM_CCER_CC4P)
70#define TIM_CCER_CC34E (TIM_CCER_CC3E | TIM_CCER_CC4E)
71
72/*
73 * Capture using PWM input mode:
74 * ___ ___
75 * TI[1, 2, 3 or 4]: ........._| |________|
76 * ^0 ^1 ^2
77 * . . .
78 * . . XXXXX
79 * . . XXXXX |
80 * . XXXXX . |
81 * XXXXX . . |
82 * COUNTER: ______XXXXX . . . |_XXX
83 * start^ . . . ^stop
84 * . . . .
85 * v v . v
86 * v
87 * CCR1/CCR3: tx..........t0...........t2
88 * CCR2/CCR4: tx..............t1.........
89 *
90 * DMA burst transfer: | |
91 * v v
92 * DMA buffer: { t0, tx } { t2, t1 }
93 * DMA done: ^
94 *
95 * 0: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3
96 * + DMA transfer CCR[1/3] & CCR[2/4] values (t0, tx: doesn't care)
97 * 1: IC2/4 snapchot on falling edge: counter value -> CCR2/CCR4
98 * 2: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3
99 * + DMA transfer CCR[1/3] & CCR[2/4] values (t2, t1)
100 *
101 * DMA done, compute:
102 * - Period = t2 - t0
103 * - Duty cycle = t1 - t0
104 */
105static int stm32_pwm_raw_capture(struct stm32_pwm *priv, struct pwm_device *pwm,
106 unsigned long tmo_ms, u32 *raw_prd,
107 u32 *raw_dty)
108{
109 struct device *parent = priv->chip.dev->parent;
110 enum stm32_timers_dmas dma_id;
111 u32 ccen, ccr;
112 int ret;
113
114 /* Ensure registers have been updated, enable counter and capture */
115 regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
116 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
117
118 /* Use cc1 or cc3 DMA resp for PWM input channels 1 & 2 or 3 & 4 */
119 dma_id = pwm->hwpwm < 2 ? STM32_TIMERS_DMA_CH1 : STM32_TIMERS_DMA_CH3;
120 ccen = pwm->hwpwm < 2 ? TIM_CCER_CC12E : TIM_CCER_CC34E;
121 ccr = pwm->hwpwm < 2 ? TIM_CCR1 : TIM_CCR3;
122 regmap_update_bits(priv->regmap, TIM_CCER, ccen, ccen);
123
124 /*
125 * Timer DMA burst mode. Request 2 registers, 2 bursts, to get both
126 * CCR1 & CCR2 (or CCR3 & CCR4) on each capture event.
127 * We'll get two capture snapchots: { CCR1, CCR2 }, { CCR1, CCR2 }
128 * or { CCR3, CCR4 }, { CCR3, CCR4 }
129 */
130 ret = stm32_timers_dma_burst_read(parent, priv->capture, dma_id, ccr, 2,
131 2, tmo_ms);
132 if (ret)
133 goto stop;
134
135 /* Period: t2 - t0 (take care of counter overflow) */
136 if (priv->capture[0] <= priv->capture[2])
137 *raw_prd = priv->capture[2] - priv->capture[0];
138 else
139 *raw_prd = priv->max_arr - priv->capture[0] + priv->capture[2];
140
141 /* Duty cycle capture requires at least two capture units */
142 if (pwm->chip->npwm < 2)
143 *raw_dty = 0;
144 else if (priv->capture[0] <= priv->capture[3])
145 *raw_dty = priv->capture[3] - priv->capture[0];
146 else
147 *raw_dty = priv->max_arr - priv->capture[0] + priv->capture[3];
148
149 if (*raw_dty > *raw_prd) {
150 /*
151 * Race beetween PWM input and DMA: it may happen
152 * falling edge triggers new capture on TI2/4 before DMA
153 * had a chance to read CCR2/4. It means capture[1]
154 * contains period + duty_cycle. So, subtract period.
155 */
156 *raw_dty -= *raw_prd;
157 }
158
159stop:
160 regmap_update_bits(priv->regmap, TIM_CCER, ccen, 0);
161 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
162
163 return ret;
164}
165
166static int stm32_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm,
167 struct pwm_capture *result, unsigned long tmo_ms)
168{
169 struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
170 unsigned long long prd, div, dty;
171 unsigned long rate;
172 unsigned int psc = 0, icpsc, scale;
173 u32 raw_prd, raw_dty;
174 int ret = 0;
175
176 mutex_lock(&priv->lock);
177
178 if (active_channels(priv)) {
179 ret = -EBUSY;
180 goto unlock;
181 }
182
183 ret = clk_enable(priv->clk);
184 if (ret) {
185 dev_err(priv->chip.dev, "failed to enable counter clock\n");
186 goto unlock;
187 }
188
189 rate = clk_get_rate(priv->clk);
190 if (!rate) {
191 ret = -EINVAL;
192 goto clk_dis;
193 }
194
195 /* prescaler: fit timeout window provided by upper layer */
196 div = (unsigned long long)rate * (unsigned long long)tmo_ms;
197 do_div(div, MSEC_PER_SEC);
198 prd = div;
199 while ((div > priv->max_arr) && (psc < MAX_TIM_PSC)) {
200 psc++;
201 div = prd;
202 do_div(div, psc + 1);
203 }
204 regmap_write(priv->regmap, TIM_ARR, priv->max_arr);
205 regmap_write(priv->regmap, TIM_PSC, psc);
206
207 /* Map TI1 or TI2 PWM input to IC1 & IC2 (or TI3/4 to IC3 & IC4) */
208 regmap_update_bits(priv->regmap,
209 pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2,
210 TIM_CCMR_CC1S | TIM_CCMR_CC2S, pwm->hwpwm & 0x1 ?
211 TIM_CCMR_CC1S_TI2 | TIM_CCMR_CC2S_TI2 :
212 TIM_CCMR_CC1S_TI1 | TIM_CCMR_CC2S_TI1);
213
214 /* Capture period on IC1/3 rising edge, duty cycle on IC2/4 falling. */
215 regmap_update_bits(priv->regmap, TIM_CCER, pwm->hwpwm < 2 ?
216 TIM_CCER_CC12P : TIM_CCER_CC34P, pwm->hwpwm < 2 ?
217 TIM_CCER_CC2P : TIM_CCER_CC4P);
218
219 ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, &raw_dty);
220 if (ret)
221 goto stop;
222
223 /*
224 * Got a capture. Try to improve accuracy at high rates:
225 * - decrease counter clock prescaler, scale up to max rate.
226 * - use input prescaler, capture once every /2 /4 or /8 edges.
227 */
228 if (raw_prd) {
229 u32 max_arr = priv->max_arr - 0x1000; /* arbitrary margin */
230
231 scale = max_arr / min(max_arr, raw_prd);
232 } else {
233 scale = priv->max_arr; /* bellow resolution, use max scale */
234 }
235
236 if (psc && scale > 1) {
237 /* 2nd measure with new scale */
238 psc /= scale;
239 regmap_write(priv->regmap, TIM_PSC, psc);
240 ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd,
241 &raw_dty);
242 if (ret)
243 goto stop;
244 }
245
246 /* Compute intermediate period not to exceed timeout at low rates */
247 prd = (unsigned long long)raw_prd * (psc + 1) * NSEC_PER_SEC;
248 do_div(prd, rate);
249
250 for (icpsc = 0; icpsc < MAX_TIM_ICPSC ; icpsc++) {
251 /* input prescaler: also keep arbitrary margin */
252 if (raw_prd >= (priv->max_arr - 0x1000) >> (icpsc + 1))
253 break;
254 if (prd >= (tmo_ms * NSEC_PER_MSEC) >> (icpsc + 2))
255 break;
256 }
257
258 if (!icpsc)
259 goto done;
260
261 /* Last chance to improve period accuracy, using input prescaler */
262 regmap_update_bits(priv->regmap,
263 pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2,
264 TIM_CCMR_IC1PSC | TIM_CCMR_IC2PSC,
265 FIELD_PREP(TIM_CCMR_IC1PSC, icpsc) |
266 FIELD_PREP(TIM_CCMR_IC2PSC, icpsc));
267
268 ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, &raw_dty);
269 if (ret)
270 goto stop;
271
272 if (raw_dty >= (raw_prd >> icpsc)) {
273 /*
274 * We may fall here using input prescaler, when input
275 * capture starts on high side (before falling edge).
276 * Example with icpsc to capture on each 4 events:
277 *
278 * start 1st capture 2nd capture
279 * v v v
280 * ___ _____ _____ _____ _____ ____
281 * TI1..4 |__| |__| |__| |__| |__|
282 * v v . . . . . v v
283 * icpsc1/3: . 0 . 1 . 2 . 3 . 0
284 * icpsc2/4: 0 1 2 3 0
285 * v v v v
286 * CCR1/3 ......t0..............................t2
287 * CCR2/4 ..t1..............................t1'...
288 * . . .
289 * Capture0: .<----------------------------->.
290 * Capture1: .<-------------------------->. .
291 * . . .
292 * Period: .<------> . .
293 * Low side: .<>.
294 *
295 * Result:
296 * - Period = Capture0 / icpsc
297 * - Duty = Period - Low side = Period - (Capture0 - Capture1)
298 */
299 raw_dty = (raw_prd >> icpsc) - (raw_prd - raw_dty);
300 }
301
302done:
303 prd = (unsigned long long)raw_prd * (psc + 1) * NSEC_PER_SEC;
304 result->period = DIV_ROUND_UP_ULL(prd, rate << icpsc);
305 dty = (unsigned long long)raw_dty * (psc + 1) * NSEC_PER_SEC;
306 result->duty_cycle = DIV_ROUND_UP_ULL(dty, rate);
307stop:
308 regmap_write(priv->regmap, TIM_CCER, 0);
309 regmap_write(priv->regmap, pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2, 0);
310 regmap_write(priv->regmap, TIM_PSC, 0);
311clk_dis:
312 clk_disable(priv->clk);
313unlock:
314 mutex_unlock(&priv->lock);
315
316 return ret;
317}
318
65static int stm32_pwm_config(struct stm32_pwm *priv, int ch, 319static int stm32_pwm_config(struct stm32_pwm *priv, int ch,
66 int duty_ns, int period_ns) 320 int duty_ns, int period_ns)
67{ 321{
@@ -230,6 +484,9 @@ static int stm32_pwm_apply_locked(struct pwm_chip *chip, struct pwm_device *pwm,
230static const struct pwm_ops stm32pwm_ops = { 484static const struct pwm_ops stm32pwm_ops = {
231 .owner = THIS_MODULE, 485 .owner = THIS_MODULE,
232 .apply = stm32_pwm_apply_locked, 486 .apply = stm32_pwm_apply_locked,
487#if IS_ENABLED(CONFIG_DMA_ENGINE)
488 .capture = stm32_pwm_capture,
489#endif
233}; 490};
234 491
235static int stm32_pwm_set_breakinput(struct stm32_pwm *priv, 492static int stm32_pwm_set_breakinput(struct stm32_pwm *priv,
diff --git a/include/linux/mfd/stm32-timers.h b/include/linux/mfd/stm32-timers.h
index 2aadab6f34a1..9da1d7ece079 100644
--- a/include/linux/mfd/stm32-timers.h
+++ b/include/linux/mfd/stm32-timers.h
@@ -8,6 +8,8 @@
8#define _LINUX_STM32_GPTIMER_H_ 8#define _LINUX_STM32_GPTIMER_H_
9 9
10#include <linux/clk.h> 10#include <linux/clk.h>
11#include <linux/dmaengine.h>
12#include <linux/dma-mapping.h>
11#include <linux/regmap.h> 13#include <linux/regmap.h>
12 14
13#define TIM_CR1 0x00 /* Control Register 1 */ 15#define TIM_CR1 0x00 /* Control Register 1 */
@@ -27,6 +29,8 @@
27#define TIM_CCR3 0x3C /* Capt/Comp Register 3 */ 29#define TIM_CCR3 0x3C /* Capt/Comp Register 3 */
28#define TIM_CCR4 0x40 /* Capt/Comp Register 4 */ 30#define TIM_CCR4 0x40 /* Capt/Comp Register 4 */
29#define TIM_BDTR 0x44 /* Break and Dead-Time Reg */ 31#define TIM_BDTR 0x44 /* Break and Dead-Time Reg */
32#define TIM_DCR 0x48 /* DMA control register */
33#define TIM_DMAR 0x4C /* DMA register for transfer */
30 34
31#define TIM_CR1_CEN BIT(0) /* Counter Enable */ 35#define TIM_CR1_CEN BIT(0) /* Counter Enable */
32#define TIM_CR1_DIR BIT(4) /* Counter Direction */ 36#define TIM_CR1_DIR BIT(4) /* Counter Direction */
@@ -36,17 +40,35 @@
36#define TIM_SMCR_SMS (BIT(0) | BIT(1) | BIT(2)) /* Slave mode selection */ 40#define TIM_SMCR_SMS (BIT(0) | BIT(1) | BIT(2)) /* Slave mode selection */
37#define TIM_SMCR_TS (BIT(4) | BIT(5) | BIT(6)) /* Trigger selection */ 41#define TIM_SMCR_TS (BIT(4) | BIT(5) | BIT(6)) /* Trigger selection */
38#define TIM_DIER_UIE BIT(0) /* Update interrupt */ 42#define TIM_DIER_UIE BIT(0) /* Update interrupt */
43#define TIM_DIER_UDE BIT(8) /* Update DMA request Enable */
44#define TIM_DIER_CC1DE BIT(9) /* CC1 DMA request Enable */
45#define TIM_DIER_CC2DE BIT(10) /* CC2 DMA request Enable */
46#define TIM_DIER_CC3DE BIT(11) /* CC3 DMA request Enable */
47#define TIM_DIER_CC4DE BIT(12) /* CC4 DMA request Enable */
48#define TIM_DIER_COMDE BIT(13) /* COM DMA request Enable */
49#define TIM_DIER_TDE BIT(14) /* Trigger DMA request Enable */
39#define TIM_SR_UIF BIT(0) /* Update interrupt flag */ 50#define TIM_SR_UIF BIT(0) /* Update interrupt flag */
40#define TIM_EGR_UG BIT(0) /* Update Generation */ 51#define TIM_EGR_UG BIT(0) /* Update Generation */
41#define TIM_CCMR_PE BIT(3) /* Channel Preload Enable */ 52#define TIM_CCMR_PE BIT(3) /* Channel Preload Enable */
42#define TIM_CCMR_M1 (BIT(6) | BIT(5)) /* Channel PWM Mode 1 */ 53#define TIM_CCMR_M1 (BIT(6) | BIT(5)) /* Channel PWM Mode 1 */
54#define TIM_CCMR_CC1S (BIT(0) | BIT(1)) /* Capture/compare 1 sel */
55#define TIM_CCMR_IC1PSC GENMASK(3, 2) /* Input capture 1 prescaler */
56#define TIM_CCMR_CC2S (BIT(8) | BIT(9)) /* Capture/compare 2 sel */
57#define TIM_CCMR_IC2PSC GENMASK(11, 10) /* Input capture 2 prescaler */
58#define TIM_CCMR_CC1S_TI1 BIT(0) /* IC1/IC3 selects TI1/TI3 */
59#define TIM_CCMR_CC1S_TI2 BIT(1) /* IC1/IC3 selects TI2/TI4 */
60#define TIM_CCMR_CC2S_TI2 BIT(8) /* IC2/IC4 selects TI2/TI4 */
61#define TIM_CCMR_CC2S_TI1 BIT(9) /* IC2/IC4 selects TI1/TI3 */
43#define TIM_CCER_CC1E BIT(0) /* Capt/Comp 1 out Ena */ 62#define TIM_CCER_CC1E BIT(0) /* Capt/Comp 1 out Ena */
44#define TIM_CCER_CC1P BIT(1) /* Capt/Comp 1 Polarity */ 63#define TIM_CCER_CC1P BIT(1) /* Capt/Comp 1 Polarity */
45#define TIM_CCER_CC1NE BIT(2) /* Capt/Comp 1N out Ena */ 64#define TIM_CCER_CC1NE BIT(2) /* Capt/Comp 1N out Ena */
46#define TIM_CCER_CC1NP BIT(3) /* Capt/Comp 1N Polarity */ 65#define TIM_CCER_CC1NP BIT(3) /* Capt/Comp 1N Polarity */
47#define TIM_CCER_CC2E BIT(4) /* Capt/Comp 2 out Ena */ 66#define TIM_CCER_CC2E BIT(4) /* Capt/Comp 2 out Ena */
67#define TIM_CCER_CC2P BIT(5) /* Capt/Comp 2 Polarity */
48#define TIM_CCER_CC3E BIT(8) /* Capt/Comp 3 out Ena */ 68#define TIM_CCER_CC3E BIT(8) /* Capt/Comp 3 out Ena */
69#define TIM_CCER_CC3P BIT(9) /* Capt/Comp 3 Polarity */
49#define TIM_CCER_CC4E BIT(12) /* Capt/Comp 4 out Ena */ 70#define TIM_CCER_CC4E BIT(12) /* Capt/Comp 4 out Ena */
71#define TIM_CCER_CC4P BIT(13) /* Capt/Comp 4 Polarity */
50#define TIM_CCER_CCXE (BIT(0) | BIT(4) | BIT(8) | BIT(12)) 72#define TIM_CCER_CCXE (BIT(0) | BIT(4) | BIT(8) | BIT(12))
51#define TIM_BDTR_BKE BIT(12) /* Break input enable */ 73#define TIM_BDTR_BKE BIT(12) /* Break input enable */
52#define TIM_BDTR_BKP BIT(13) /* Break input polarity */ 74#define TIM_BDTR_BKP BIT(13) /* Break input polarity */
@@ -56,8 +78,11 @@
56#define TIM_BDTR_BK2F (BIT(20) | BIT(21) | BIT(22) | BIT(23)) 78#define TIM_BDTR_BK2F (BIT(20) | BIT(21) | BIT(22) | BIT(23))
57#define TIM_BDTR_BK2E BIT(24) /* Break 2 input enable */ 79#define TIM_BDTR_BK2E BIT(24) /* Break 2 input enable */
58#define TIM_BDTR_BK2P BIT(25) /* Break 2 input polarity */ 80#define TIM_BDTR_BK2P BIT(25) /* Break 2 input polarity */
81#define TIM_DCR_DBA GENMASK(4, 0) /* DMA base addr */
82#define TIM_DCR_DBL GENMASK(12, 8) /* DMA burst len */
59 83
60#define MAX_TIM_PSC 0xFFFF 84#define MAX_TIM_PSC 0xFFFF
85#define MAX_TIM_ICPSC 0x3
61#define TIM_CR2_MMS_SHIFT 4 86#define TIM_CR2_MMS_SHIFT 4
62#define TIM_CR2_MMS2_SHIFT 20 87#define TIM_CR2_MMS2_SHIFT 20
63#define TIM_SMCR_TS_SHIFT 4 88#define TIM_SMCR_TS_SHIFT 4
@@ -65,9 +90,42 @@
65#define TIM_BDTR_BKF_SHIFT 16 90#define TIM_BDTR_BKF_SHIFT 16
66#define TIM_BDTR_BK2F_SHIFT 20 91#define TIM_BDTR_BK2F_SHIFT 20
67 92
93enum stm32_timers_dmas {
94 STM32_TIMERS_DMA_CH1,
95 STM32_TIMERS_DMA_CH2,
96 STM32_TIMERS_DMA_CH3,
97 STM32_TIMERS_DMA_CH4,
98 STM32_TIMERS_DMA_UP,
99 STM32_TIMERS_DMA_TRIG,
100 STM32_TIMERS_DMA_COM,
101 STM32_TIMERS_MAX_DMAS,
102};
103
104/**
105 * struct stm32_timers_dma - STM32 timer DMA handling.
106 * @completion: end of DMA transfer completion
107 * @phys_base: control registers physical base address
108 * @lock: protect DMA access
109 * @chan: DMA channel in use
110 * @chans: DMA channels available for this timer instance
111 */
112struct stm32_timers_dma {
113 struct completion completion;
114 phys_addr_t phys_base;
115 struct mutex lock;
116 struct dma_chan *chan;
117 struct dma_chan *chans[STM32_TIMERS_MAX_DMAS];
118};
119
68struct stm32_timers { 120struct stm32_timers {
69 struct clk *clk; 121 struct clk *clk;
70 struct regmap *regmap; 122 struct regmap *regmap;
71 u32 max_arr; 123 u32 max_arr;
124 struct stm32_timers_dma dma; /* Only to be used by the parent */
72}; 125};
126
127int stm32_timers_dma_burst_read(struct device *dev, u32 *buf,
128 enum stm32_timers_dmas id, u32 reg,
129 unsigned int num_reg, unsigned int bursts,
130 unsigned long tmo_ms);
73#endif 131#endif