1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2016
4 * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
7 #include <linux/bitfield.h>
8 #include <linux/mfd/stm32-timers.h>
9 #include <linux/module.h>
10 #include <linux/of_platform.h>
11 #include <linux/reset.h>
13 #define STM32_TIMERS_MAX_REGISTERS 0x3fc
15 /* DIER register DMA enable bits */
16 static const u32 stm32_timers_dier_dmaen
[STM32_TIMERS_MAX_DMAS
] = {
26 static void stm32_timers_dma_done(void *p
)
28 struct stm32_timers_dma
*dma
= p
;
29 struct dma_tx_state state
;
30 enum dma_status status
;
32 status
= dmaengine_tx_status(dma
->chan
, dma
->chan
->cookie
, &state
);
33 if (status
== DMA_COMPLETE
)
34 complete(&dma
->completion
);
38 * stm32_timers_dma_burst_read - Read from timers registers using DMA.
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)
49 int 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
,
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
;
68 if (id
< STM32_TIMERS_DMA_CH1
|| id
>= STM32_TIMERS_MAX_DMAS
)
71 if (!num_reg
|| !bursts
|| reg
> STM32_TIMERS_MAX_REGISTERS
||
72 (reg
+ num_reg
* sizeof(u32
)) > STM32_TIMERS_MAX_REGISTERS
)
77 mutex_lock(&dma
->lock
);
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
)) {
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
);
95 desc
= dmaengine_prep_slave_single(dma
->chan
, dma_buf
, len
,
96 DMA_DEV_TO_MEM
, DMA_PREP_INTERRUPT
);
102 desc
->callback
= stm32_timers_dma_done
;
103 desc
->callback_param
= dma
;
104 cookie
= dmaengine_submit(desc
);
105 ret
= dma_submit_error(cookie
);
109 reinit_completion(&dma
->completion
);
110 dma_async_issue_pending(dma
->chan
);
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
);
119 /* Clear pending flags before enabling DMA request */
120 ret
= regmap_write(regmap
, TIM_SR
, 0);
124 ret
= regmap_update_bits(regmap
, TIM_DIER
, stm32_timers_dier_dmaen
[id
],
125 stm32_timers_dier_dmaen
[id
]);
129 err
= wait_for_completion_interruptible_timeout(&dma
->completion
,
136 regmap_update_bits(regmap
, TIM_DIER
, stm32_timers_dier_dmaen
[id
], 0);
137 regmap_write(regmap
, TIM_SR
, 0);
139 regmap_write(regmap
, TIM_DCR
, 0);
141 dmaengine_terminate_all(dma
->chan
);
143 dma_unmap_single(dev
, dma_buf
, len
, DMA_FROM_DEVICE
);
146 mutex_unlock(&dma
->lock
);
150 EXPORT_SYMBOL_GPL(stm32_timers_dma_burst_read
);
152 static const struct regmap_config stm32_timers_regmap_cfg
= {
155 .reg_stride
= sizeof(u32
),
156 .max_register
= STM32_TIMERS_MAX_REGISTERS
,
159 static void stm32_timers_get_arr_size(struct stm32_timers
*ddata
)
162 * Only the available bits will be written so when readback
163 * we get the maximum value of auto reload register
165 regmap_write(ddata
->regmap
, TIM_ARR
, ~0L);
166 regmap_read(ddata
->regmap
, TIM_ARR
, &ddata
->max_arr
);
167 regmap_write(ddata
->regmap
, TIM_ARR
, 0x0);
170 static int stm32_timers_dma_probe(struct device
*dev
,
171 struct stm32_timers
*ddata
)
177 init_completion(&ddata
->dma
.completion
);
178 mutex_init(&ddata
->dma
.lock
);
180 /* Optional DMA support: get valid DMA channel(s) or NULL */
181 for (i
= STM32_TIMERS_DMA_CH1
; i
<= STM32_TIMERS_DMA_CH4
; i
++) {
182 snprintf(name
, ARRAY_SIZE(name
), "ch%1d", i
+ 1);
183 ddata
->dma
.chans
[i
] = dma_request_chan(dev
, name
);
185 ddata
->dma
.chans
[STM32_TIMERS_DMA_UP
] = dma_request_chan(dev
, "up");
186 ddata
->dma
.chans
[STM32_TIMERS_DMA_TRIG
] = dma_request_chan(dev
, "trig");
187 ddata
->dma
.chans
[STM32_TIMERS_DMA_COM
] = dma_request_chan(dev
, "com");
189 for (i
= STM32_TIMERS_DMA_CH1
; i
< STM32_TIMERS_MAX_DMAS
; i
++) {
190 if (IS_ERR(ddata
->dma
.chans
[i
])) {
191 /* Save the first error code to return */
192 if (PTR_ERR(ddata
->dma
.chans
[i
]) != -ENODEV
&& !ret
)
193 ret
= PTR_ERR(ddata
->dma
.chans
[i
]);
195 ddata
->dma
.chans
[i
] = NULL
;
202 static void stm32_timers_dma_remove(struct device
*dev
,
203 struct stm32_timers
*ddata
)
207 for (i
= STM32_TIMERS_DMA_CH1
; i
< STM32_TIMERS_MAX_DMAS
; i
++)
208 if (ddata
->dma
.chans
[i
])
209 dma_release_channel(ddata
->dma
.chans
[i
]);
212 static int stm32_timers_probe(struct platform_device
*pdev
)
214 struct device
*dev
= &pdev
->dev
;
215 struct stm32_timers
*ddata
;
216 struct resource
*res
;
220 ddata
= devm_kzalloc(dev
, sizeof(*ddata
), GFP_KERNEL
);
224 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
225 mmio
= devm_ioremap_resource(dev
, res
);
227 return PTR_ERR(mmio
);
229 /* Timer physical addr for DMA */
230 ddata
->dma
.phys_base
= res
->start
;
232 ddata
->regmap
= devm_regmap_init_mmio_clk(dev
, "int", mmio
,
233 &stm32_timers_regmap_cfg
);
234 if (IS_ERR(ddata
->regmap
))
235 return PTR_ERR(ddata
->regmap
);
237 ddata
->clk
= devm_clk_get(dev
, NULL
);
238 if (IS_ERR(ddata
->clk
))
239 return PTR_ERR(ddata
->clk
);
241 stm32_timers_get_arr_size(ddata
);
243 ret
= stm32_timers_dma_probe(dev
, ddata
);
245 stm32_timers_dma_remove(dev
, ddata
);
249 platform_set_drvdata(pdev
, ddata
);
251 ret
= of_platform_populate(pdev
->dev
.of_node
, NULL
, NULL
, &pdev
->dev
);
253 stm32_timers_dma_remove(dev
, ddata
);
258 static int stm32_timers_remove(struct platform_device
*pdev
)
260 struct stm32_timers
*ddata
= platform_get_drvdata(pdev
);
263 * Don't use devm_ here: enfore of_platform_depopulate() happens before
264 * DMA are released, to avoid race on DMA.
266 of_platform_depopulate(&pdev
->dev
);
267 stm32_timers_dma_remove(&pdev
->dev
, ddata
);
272 static const struct of_device_id stm32_timers_of_match
[] = {
273 { .compatible
= "st,stm32-timers", },
276 MODULE_DEVICE_TABLE(of
, stm32_timers_of_match
);
278 static struct platform_driver stm32_timers_driver
= {
279 .probe
= stm32_timers_probe
,
280 .remove
= stm32_timers_remove
,
282 .name
= "stm32-timers",
283 .of_match_table
= stm32_timers_of_match
,
286 module_platform_driver(stm32_timers_driver
);
288 MODULE_DESCRIPTION("STMicroelectronics STM32 Timers");
289 MODULE_LICENSE("GPL v2");