blob: 5779ae7c73cfc5362ed103d64ef4a3b3325ef39f [file] [log] [blame]
Benjamin Gaignardad299372019-04-02 15:30:42 +09001// SPDX-License-Identifier: GPL-2.0
2/*
3 * STM32 Timer Encoder and Counter driver
4 *
5 * Copyright (C) STMicroelectronics 2018
6 *
7 * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
8 *
9 */
10#include <linux/counter.h>
Benjamin Gaignardad299372019-04-02 15:30:42 +090011#include <linux/mfd/stm32-timers.h>
Fabrice Gasnier15e85732020-02-11 11:56:06 +010012#include <linux/mod_devicetable.h>
Benjamin Gaignardad299372019-04-02 15:30:42 +090013#include <linux/module.h>
Fabrice Gasnierc5b84252020-02-10 18:19:58 +010014#include <linux/pinctrl/consumer.h>
Benjamin Gaignardad299372019-04-02 15:30:42 +090015#include <linux/platform_device.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090016#include <linux/types.h>
Benjamin Gaignardad299372019-04-02 15:30:42 +090017
18#define TIM_CCMR_CCXS (BIT(8) | BIT(0))
19#define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
20 TIM_CCMR_IC1F | TIM_CCMR_IC2F)
21#define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
22 TIM_CCER_CC2P | TIM_CCER_CC2NP)
23
Fabrice Gasnierc5b84252020-02-10 18:19:58 +010024struct stm32_timer_regs {
25 u32 cr1;
26 u32 cnt;
27 u32 smcr;
28 u32 arr;
29};
30
Benjamin Gaignardad299372019-04-02 15:30:42 +090031struct stm32_timer_cnt {
Benjamin Gaignardad299372019-04-02 15:30:42 +090032 struct regmap *regmap;
33 struct clk *clk;
Fabrice Gasniere4c3e132021-03-02 15:43:55 +010034 u32 max_arr;
Fabrice Gasnierc5b84252020-02-10 18:19:58 +010035 bool enabled;
36 struct stm32_timer_regs bak;
Benjamin Gaignardad299372019-04-02 15:30:42 +090037};
38
William Breathitt Gray394a0152021-08-03 21:06:15 +090039static const enum counter_function stm32_count_functions[] = {
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090040 COUNTER_FUNCTION_INCREASE,
41 COUNTER_FUNCTION_QUADRATURE_X2_A,
42 COUNTER_FUNCTION_QUADRATURE_X2_B,
43 COUNTER_FUNCTION_QUADRATURE_X4,
Benjamin Gaignardad299372019-04-02 15:30:42 +090044};
45
46static int stm32_count_read(struct counter_device *counter,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090047 struct counter_count *count, u64 *val)
Benjamin Gaignardad299372019-04-02 15:30:42 +090048{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +010049 struct stm32_timer_cnt *const priv = counter_priv(counter);
Benjamin Gaignardad299372019-04-02 15:30:42 +090050 u32 cnt;
51
52 regmap_read(priv->regmap, TIM_CNT, &cnt);
William Breathitt Grayd49e6ee2019-10-06 16:03:09 -040053 *val = cnt;
Benjamin Gaignardad299372019-04-02 15:30:42 +090054
55 return 0;
56}
57
58static int stm32_count_write(struct counter_device *counter,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090059 struct counter_count *count, const u64 val)
Benjamin Gaignardad299372019-04-02 15:30:42 +090060{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +010061 struct stm32_timer_cnt *const priv = counter_priv(counter);
Fabrice Gasnierb14d72a2021-03-03 18:49:49 +010062 u32 ceiling;
Benjamin Gaignardad299372019-04-02 15:30:42 +090063
Fabrice Gasnierb14d72a2021-03-03 18:49:49 +010064 regmap_read(priv->regmap, TIM_ARR, &ceiling);
65 if (val > ceiling)
Benjamin Gaignardad299372019-04-02 15:30:42 +090066 return -EINVAL;
67
William Breathitt Grayd49e6ee2019-10-06 16:03:09 -040068 return regmap_write(priv->regmap, TIM_CNT, val);
Benjamin Gaignardad299372019-04-02 15:30:42 +090069}
70
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090071static int stm32_count_function_read(struct counter_device *counter,
72 struct counter_count *count,
73 enum counter_function *function)
Benjamin Gaignardad299372019-04-02 15:30:42 +090074{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +010075 struct stm32_timer_cnt *const priv = counter_priv(counter);
Benjamin Gaignardad299372019-04-02 15:30:42 +090076 u32 smcr;
77
78 regmap_read(priv->regmap, TIM_SMCR, &smcr);
79
80 switch (smcr & TIM_SMCR_SMS) {
William Breathitt Grayea434ff2021-08-27 12:47:46 +090081 case TIM_SMCR_SMS_SLAVE_MODE_DISABLED:
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090082 *function = COUNTER_FUNCTION_INCREASE;
William Breathitt Grayfae6f622021-02-26 10:29:31 +090083 return 0;
William Breathitt Grayea434ff2021-08-27 12:47:46 +090084 case TIM_SMCR_SMS_ENCODER_MODE_1:
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090085 *function = COUNTER_FUNCTION_QUADRATURE_X2_A;
Benjamin Gaignardad299372019-04-02 15:30:42 +090086 return 0;
William Breathitt Grayea434ff2021-08-27 12:47:46 +090087 case TIM_SMCR_SMS_ENCODER_MODE_2:
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090088 *function = COUNTER_FUNCTION_QUADRATURE_X2_B;
Benjamin Gaignardad299372019-04-02 15:30:42 +090089 return 0;
William Breathitt Grayea434ff2021-08-27 12:47:46 +090090 case TIM_SMCR_SMS_ENCODER_MODE_3:
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090091 *function = COUNTER_FUNCTION_QUADRATURE_X4;
Benjamin Gaignardad299372019-04-02 15:30:42 +090092 return 0;
William Breathitt Grayfae6f622021-02-26 10:29:31 +090093 default:
94 return -EINVAL;
Benjamin Gaignardad299372019-04-02 15:30:42 +090095 }
Benjamin Gaignardad299372019-04-02 15:30:42 +090096}
97
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090098static int stm32_count_function_write(struct counter_device *counter,
99 struct counter_count *count,
100 enum counter_function function)
Benjamin Gaignardad299372019-04-02 15:30:42 +0900101{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +0100102 struct stm32_timer_cnt *const priv = counter_priv(counter);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900103 u32 cr1, sms;
104
105 switch (function) {
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900106 case COUNTER_FUNCTION_INCREASE:
William Breathitt Grayea434ff2021-08-27 12:47:46 +0900107 sms = TIM_SMCR_SMS_SLAVE_MODE_DISABLED;
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900108 break;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900109 case COUNTER_FUNCTION_QUADRATURE_X2_A:
William Breathitt Grayea434ff2021-08-27 12:47:46 +0900110 sms = TIM_SMCR_SMS_ENCODER_MODE_1;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900111 break;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900112 case COUNTER_FUNCTION_QUADRATURE_X2_B:
William Breathitt Grayea434ff2021-08-27 12:47:46 +0900113 sms = TIM_SMCR_SMS_ENCODER_MODE_2;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900114 break;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900115 case COUNTER_FUNCTION_QUADRATURE_X4:
William Breathitt Grayea434ff2021-08-27 12:47:46 +0900116 sms = TIM_SMCR_SMS_ENCODER_MODE_3;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900117 break;
118 default:
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900119 return -EINVAL;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900120 }
121
122 /* Store enable status */
123 regmap_read(priv->regmap, TIM_CR1, &cr1);
124
125 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
126
Benjamin Gaignardad299372019-04-02 15:30:42 +0900127 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
128
129 /* Make sure that registers are updated */
130 regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
131
132 /* Restore the enable status */
133 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
134
135 return 0;
136}
137
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900138static int stm32_count_direction_read(struct counter_device *counter,
Benjamin Gaignardad299372019-04-02 15:30:42 +0900139 struct counter_count *count,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900140 enum counter_count_direction *direction)
Benjamin Gaignardad299372019-04-02 15:30:42 +0900141{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +0100142 struct stm32_timer_cnt *const priv = counter_priv(counter);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900143 u32 cr1;
144
145 regmap_read(priv->regmap, TIM_CR1, &cr1);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900146 *direction = (cr1 & TIM_CR1_DIR) ? COUNTER_COUNT_DIRECTION_BACKWARD :
147 COUNTER_COUNT_DIRECTION_FORWARD;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900148
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900149 return 0;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900150}
151
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900152static int stm32_count_ceiling_read(struct counter_device *counter,
153 struct counter_count *count, u64 *ceiling)
Benjamin Gaignardad299372019-04-02 15:30:42 +0900154{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +0100155 struct stm32_timer_cnt *const priv = counter_priv(counter);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900156 u32 arr;
157
158 regmap_read(priv->regmap, TIM_ARR, &arr);
159
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900160 *ceiling = arr;
161
162 return 0;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900163}
164
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900165static int stm32_count_ceiling_write(struct counter_device *counter,
166 struct counter_count *count, u64 ceiling)
Benjamin Gaignardad299372019-04-02 15:30:42 +0900167{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +0100168 struct stm32_timer_cnt *const priv = counter_priv(counter);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900169
Fabrice Gasniere4c3e132021-03-02 15:43:55 +0100170 if (ceiling > priv->max_arr)
171 return -ERANGE;
172
Benjamin Gaignardad299372019-04-02 15:30:42 +0900173 /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
174 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
175 regmap_write(priv->regmap, TIM_ARR, ceiling);
176
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900177 return 0;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900178}
179
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900180static int stm32_count_enable_read(struct counter_device *counter,
181 struct counter_count *count, u8 *enable)
Benjamin Gaignardad299372019-04-02 15:30:42 +0900182{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +0100183 struct stm32_timer_cnt *const priv = counter_priv(counter);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900184 u32 cr1;
185
186 regmap_read(priv->regmap, TIM_CR1, &cr1);
187
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900188 *enable = cr1 & TIM_CR1_CEN;
189
190 return 0;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900191}
192
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900193static int stm32_count_enable_write(struct counter_device *counter,
194 struct counter_count *count, u8 enable)
Benjamin Gaignardad299372019-04-02 15:30:42 +0900195{
Uwe Kleine-Könige1528332021-12-30 16:02:49 +0100196 struct stm32_timer_cnt *const priv = counter_priv(counter);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900197 u32 cr1;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900198
199 if (enable) {
200 regmap_read(priv->regmap, TIM_CR1, &cr1);
Colin Ian King76510ec2019-09-25 10:51:26 +0100201 if (!(cr1 & TIM_CR1_CEN))
202 clk_enable(priv->clk);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900203
204 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
205 TIM_CR1_CEN);
206 } else {
207 regmap_read(priv->regmap, TIM_CR1, &cr1);
208 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
209 if (cr1 & TIM_CR1_CEN)
210 clk_disable(priv->clk);
211 }
212
Fabrice Gasnierc5b84252020-02-10 18:19:58 +0100213 /* Keep enabled state to properly handle low power states */
214 priv->enabled = enable;
215
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900216 return 0;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900217}
218
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900219static struct counter_comp stm32_count_ext[] = {
220 COUNTER_COMP_DIRECTION(stm32_count_direction_read),
221 COUNTER_COMP_ENABLE(stm32_count_enable_read, stm32_count_enable_write),
222 COUNTER_COMP_CEILING(stm32_count_ceiling_read,
223 stm32_count_ceiling_write),
Benjamin Gaignardad299372019-04-02 15:30:42 +0900224};
225
William Breathitt Grayd0ce3d52021-06-09 10:31:20 +0900226static const enum counter_synapse_action stm32_synapse_actions[] = {
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900227 COUNTER_SYNAPSE_ACTION_NONE,
228 COUNTER_SYNAPSE_ACTION_BOTH_EDGES
Benjamin Gaignardad299372019-04-02 15:30:42 +0900229};
230
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900231static int stm32_action_read(struct counter_device *counter,
232 struct counter_count *count,
233 struct counter_synapse *synapse,
234 enum counter_synapse_action *action)
Benjamin Gaignardad299372019-04-02 15:30:42 +0900235{
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900236 enum counter_function function;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900237 int err;
238
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900239 err = stm32_count_function_read(counter, count, &function);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900240 if (err)
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900241 return err;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900242
243 switch (function) {
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900244 case COUNTER_FUNCTION_INCREASE:
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900245 /* counts on internal clock when CEN=1 */
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900246 *action = COUNTER_SYNAPSE_ACTION_NONE;
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900247 return 0;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900248 case COUNTER_FUNCTION_QUADRATURE_X2_A:
Benjamin Gaignardad299372019-04-02 15:30:42 +0900249 /* counts up/down on TI1FP1 edge depending on TI2FP2 level */
250 if (synapse->signal->id == count->synapses[0].signal->id)
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900251 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900252 else
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900253 *action = COUNTER_SYNAPSE_ACTION_NONE;
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900254 return 0;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900255 case COUNTER_FUNCTION_QUADRATURE_X2_B:
Benjamin Gaignardad299372019-04-02 15:30:42 +0900256 /* counts up/down on TI2FP2 edge depending on TI1FP1 level */
257 if (synapse->signal->id == count->synapses[1].signal->id)
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900258 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900259 else
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900260 *action = COUNTER_SYNAPSE_ACTION_NONE;
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900261 return 0;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900262 case COUNTER_FUNCTION_QUADRATURE_X4:
Benjamin Gaignardad299372019-04-02 15:30:42 +0900263 /* counts up/down on both TI1FP1 and TI2FP2 edges */
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900264 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
William Breathitt Grayfae6f622021-02-26 10:29:31 +0900265 return 0;
266 default:
267 return -EINVAL;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900268 }
Benjamin Gaignardad299372019-04-02 15:30:42 +0900269}
270
271static const struct counter_ops stm32_timer_cnt_ops = {
272 .count_read = stm32_count_read,
273 .count_write = stm32_count_write,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900274 .function_read = stm32_count_function_read,
275 .function_write = stm32_count_function_write,
276 .action_read = stm32_action_read,
Benjamin Gaignardad299372019-04-02 15:30:42 +0900277};
278
279static struct counter_signal stm32_signals[] = {
280 {
281 .id = 0,
282 .name = "Channel 1 Quadrature A"
283 },
284 {
285 .id = 1,
286 .name = "Channel 1 Quadrature B"
287 }
288};
289
290static struct counter_synapse stm32_count_synapses[] = {
291 {
292 .actions_list = stm32_synapse_actions,
293 .num_actions = ARRAY_SIZE(stm32_synapse_actions),
294 .signal = &stm32_signals[0]
295 },
296 {
297 .actions_list = stm32_synapse_actions,
298 .num_actions = ARRAY_SIZE(stm32_synapse_actions),
299 .signal = &stm32_signals[1]
300 }
301};
302
303static struct counter_count stm32_counts = {
304 .id = 0,
305 .name = "Channel 1 Count",
306 .functions_list = stm32_count_functions,
307 .num_functions = ARRAY_SIZE(stm32_count_functions),
308 .synapses = stm32_count_synapses,
309 .num_synapses = ARRAY_SIZE(stm32_count_synapses),
310 .ext = stm32_count_ext,
311 .num_ext = ARRAY_SIZE(stm32_count_ext)
312};
313
314static int stm32_timer_cnt_probe(struct platform_device *pdev)
315{
316 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
317 struct device *dev = &pdev->dev;
318 struct stm32_timer_cnt *priv;
Uwe Kleine-Könige1717d22021-12-30 16:02:57 +0100319 struct counter_device *counter;
320 int ret;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900321
322 if (IS_ERR_OR_NULL(ddata))
323 return -EINVAL;
324
Uwe Kleine-Könige1717d22021-12-30 16:02:57 +0100325 counter = devm_counter_alloc(dev, sizeof(*priv));
326 if (!counter)
Benjamin Gaignardad299372019-04-02 15:30:42 +0900327 return -ENOMEM;
328
Uwe Kleine-Könige1717d22021-12-30 16:02:57 +0100329 priv = counter_priv(counter);
330
Benjamin Gaignardad299372019-04-02 15:30:42 +0900331 priv->regmap = ddata->regmap;
332 priv->clk = ddata->clk;
Fabrice Gasniere4c3e132021-03-02 15:43:55 +0100333 priv->max_arr = ddata->max_arr;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900334
Uwe Kleine-Könige1717d22021-12-30 16:02:57 +0100335 counter->name = dev_name(dev);
336 counter->parent = dev;
337 counter->ops = &stm32_timer_cnt_ops;
338 counter->counts = &stm32_counts;
339 counter->num_counts = 1;
340 counter->signals = stm32_signals;
341 counter->num_signals = ARRAY_SIZE(stm32_signals);
Benjamin Gaignardad299372019-04-02 15:30:42 +0900342
Fabrice Gasnierc5b84252020-02-10 18:19:58 +0100343 platform_set_drvdata(pdev, priv);
344
Benjamin Gaignardad299372019-04-02 15:30:42 +0900345 /* Register Counter device */
Uwe Kleine-Könige1717d22021-12-30 16:02:57 +0100346 ret = devm_counter_add(dev, counter);
347 if (ret < 0)
348 dev_err_probe(dev, ret, "Failed to add counter\n");
349
350 return ret;
Benjamin Gaignardad299372019-04-02 15:30:42 +0900351}
352
Fabrice Gasnierc5b84252020-02-10 18:19:58 +0100353static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
354{
355 struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
356
357 /* Only take care of enabled counter: don't disturb other MFD child */
358 if (priv->enabled) {
359 /* Backup registers that may get lost in low power mode */
360 regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr);
361 regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr);
362 regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt);
363 regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1);
364
365 /* Disable the counter */
366 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
367 clk_disable(priv->clk);
368 }
369
370 return pinctrl_pm_select_sleep_state(dev);
371}
372
373static int __maybe_unused stm32_timer_cnt_resume(struct device *dev)
374{
375 struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
376 int ret;
377
378 ret = pinctrl_pm_select_default_state(dev);
379 if (ret)
380 return ret;
381
382 if (priv->enabled) {
383 clk_enable(priv->clk);
384
385 /* Restore registers that may have been lost */
386 regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr);
387 regmap_write(priv->regmap, TIM_ARR, priv->bak.arr);
388 regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt);
389
390 /* Also re-enables the counter */
391 regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1);
392 }
393
394 return 0;
395}
396
397static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend,
398 stm32_timer_cnt_resume);
399
Benjamin Gaignardad299372019-04-02 15:30:42 +0900400static const struct of_device_id stm32_timer_cnt_of_match[] = {
401 { .compatible = "st,stm32-timer-counter", },
402 {},
403};
404MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
405
406static struct platform_driver stm32_timer_cnt_driver = {
407 .probe = stm32_timer_cnt_probe,
408 .driver = {
409 .name = "stm32-timer-counter",
410 .of_match_table = stm32_timer_cnt_of_match,
Fabrice Gasnierc5b84252020-02-10 18:19:58 +0100411 .pm = &stm32_timer_cnt_pm_ops,
Benjamin Gaignardad299372019-04-02 15:30:42 +0900412 },
413};
414module_platform_driver(stm32_timer_cnt_driver);
415
416MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
417MODULE_ALIAS("platform:stm32-timer-counter");
418MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
419MODULE_LICENSE("GPL v2");