Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 1 | // 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 Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 11 | #include <linux/mfd/stm32-timers.h> |
Fabrice Gasnier | 15e8573 | 2020-02-11 11:56:06 +0100 | [diff] [blame] | 12 | #include <linux/mod_devicetable.h> |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 13 | #include <linux/module.h> |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 14 | #include <linux/pinctrl/consumer.h> |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 16 | #include <linux/types.h> |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 17 | |
| 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 Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 24 | struct stm32_timer_regs { |
| 25 | u32 cr1; |
| 26 | u32 cnt; |
| 27 | u32 smcr; |
| 28 | u32 arr; |
| 29 | }; |
| 30 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 31 | struct stm32_timer_cnt { |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 32 | struct regmap *regmap; |
| 33 | struct clk *clk; |
Fabrice Gasnier | e4c3e13 | 2021-03-02 15:43:55 +0100 | [diff] [blame] | 34 | u32 max_arr; |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 35 | bool enabled; |
| 36 | struct stm32_timer_regs bak; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 37 | }; |
| 38 | |
William Breathitt Gray | 394a015 | 2021-08-03 21:06:15 +0900 | [diff] [blame] | 39 | static const enum counter_function stm32_count_functions[] = { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 40 | COUNTER_FUNCTION_INCREASE, |
| 41 | COUNTER_FUNCTION_QUADRATURE_X2_A, |
| 42 | COUNTER_FUNCTION_QUADRATURE_X2_B, |
| 43 | COUNTER_FUNCTION_QUADRATURE_X4, |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static int stm32_count_read(struct counter_device *counter, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 47 | struct counter_count *count, u64 *val) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 48 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 49 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 50 | u32 cnt; |
| 51 | |
| 52 | regmap_read(priv->regmap, TIM_CNT, &cnt); |
William Breathitt Gray | d49e6ee | 2019-10-06 16:03:09 -0400 | [diff] [blame] | 53 | *val = cnt; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int stm32_count_write(struct counter_device *counter, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 59 | struct counter_count *count, const u64 val) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 60 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 61 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Fabrice Gasnier | b14d72a | 2021-03-03 18:49:49 +0100 | [diff] [blame] | 62 | u32 ceiling; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 63 | |
Fabrice Gasnier | b14d72a | 2021-03-03 18:49:49 +0100 | [diff] [blame] | 64 | regmap_read(priv->regmap, TIM_ARR, &ceiling); |
| 65 | if (val > ceiling) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 66 | return -EINVAL; |
| 67 | |
William Breathitt Gray | d49e6ee | 2019-10-06 16:03:09 -0400 | [diff] [blame] | 68 | return regmap_write(priv->regmap, TIM_CNT, val); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 69 | } |
| 70 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 71 | static int stm32_count_function_read(struct counter_device *counter, |
| 72 | struct counter_count *count, |
| 73 | enum counter_function *function) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 74 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 75 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 76 | u32 smcr; |
| 77 | |
| 78 | regmap_read(priv->regmap, TIM_SMCR, &smcr); |
| 79 | |
| 80 | switch (smcr & TIM_SMCR_SMS) { |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 81 | case TIM_SMCR_SMS_SLAVE_MODE_DISABLED: |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 82 | *function = COUNTER_FUNCTION_INCREASE; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 83 | return 0; |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 84 | case TIM_SMCR_SMS_ENCODER_MODE_1: |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 85 | *function = COUNTER_FUNCTION_QUADRATURE_X2_A; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 86 | return 0; |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 87 | case TIM_SMCR_SMS_ENCODER_MODE_2: |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 88 | *function = COUNTER_FUNCTION_QUADRATURE_X2_B; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 89 | return 0; |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 90 | case TIM_SMCR_SMS_ENCODER_MODE_3: |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 91 | *function = COUNTER_FUNCTION_QUADRATURE_X4; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 92 | return 0; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 93 | default: |
| 94 | return -EINVAL; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 95 | } |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 96 | } |
| 97 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 98 | static int stm32_count_function_write(struct counter_device *counter, |
| 99 | struct counter_count *count, |
| 100 | enum counter_function function) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 101 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 102 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 103 | u32 cr1, sms; |
| 104 | |
| 105 | switch (function) { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 106 | case COUNTER_FUNCTION_INCREASE: |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 107 | sms = TIM_SMCR_SMS_SLAVE_MODE_DISABLED; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 108 | break; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 109 | case COUNTER_FUNCTION_QUADRATURE_X2_A: |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 110 | sms = TIM_SMCR_SMS_ENCODER_MODE_1; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 111 | break; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 112 | case COUNTER_FUNCTION_QUADRATURE_X2_B: |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 113 | sms = TIM_SMCR_SMS_ENCODER_MODE_2; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 114 | break; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 115 | case COUNTER_FUNCTION_QUADRATURE_X4: |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 116 | sms = TIM_SMCR_SMS_ENCODER_MODE_3; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 117 | break; |
| 118 | default: |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 119 | return -EINVAL; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 120 | } |
| 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 Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 127 | 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 Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 138 | static int stm32_count_direction_read(struct counter_device *counter, |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 139 | struct counter_count *count, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 140 | enum counter_count_direction *direction) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 141 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 142 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 143 | u32 cr1; |
| 144 | |
| 145 | regmap_read(priv->regmap, TIM_CR1, &cr1); |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 146 | *direction = (cr1 & TIM_CR1_DIR) ? COUNTER_COUNT_DIRECTION_BACKWARD : |
| 147 | COUNTER_COUNT_DIRECTION_FORWARD; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 148 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 149 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 150 | } |
| 151 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 152 | static int stm32_count_ceiling_read(struct counter_device *counter, |
| 153 | struct counter_count *count, u64 *ceiling) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 154 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 155 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 156 | u32 arr; |
| 157 | |
| 158 | regmap_read(priv->regmap, TIM_ARR, &arr); |
| 159 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 160 | *ceiling = arr; |
| 161 | |
| 162 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 163 | } |
| 164 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 165 | static int stm32_count_ceiling_write(struct counter_device *counter, |
| 166 | struct counter_count *count, u64 ceiling) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 167 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 168 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 169 | |
Fabrice Gasnier | e4c3e13 | 2021-03-02 15:43:55 +0100 | [diff] [blame] | 170 | if (ceiling > priv->max_arr) |
| 171 | return -ERANGE; |
| 172 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 173 | /* 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 Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 177 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 178 | } |
| 179 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 180 | static int stm32_count_enable_read(struct counter_device *counter, |
| 181 | struct counter_count *count, u8 *enable) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 182 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 183 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 184 | u32 cr1; |
| 185 | |
| 186 | regmap_read(priv->regmap, TIM_CR1, &cr1); |
| 187 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 188 | *enable = cr1 & TIM_CR1_CEN; |
| 189 | |
| 190 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 191 | } |
| 192 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 193 | static int stm32_count_enable_write(struct counter_device *counter, |
| 194 | struct counter_count *count, u8 enable) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 195 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame] | 196 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 197 | u32 cr1; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 198 | |
| 199 | if (enable) { |
| 200 | regmap_read(priv->regmap, TIM_CR1, &cr1); |
Colin Ian King | 76510ec | 2019-09-25 10:51:26 +0100 | [diff] [blame] | 201 | if (!(cr1 & TIM_CR1_CEN)) |
| 202 | clk_enable(priv->clk); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 203 | |
| 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 Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 213 | /* Keep enabled state to properly handle low power states */ |
| 214 | priv->enabled = enable; |
| 215 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 216 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 217 | } |
| 218 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 219 | static 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 Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 224 | }; |
| 225 | |
William Breathitt Gray | d0ce3d5 | 2021-06-09 10:31:20 +0900 | [diff] [blame] | 226 | static const enum counter_synapse_action stm32_synapse_actions[] = { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 227 | COUNTER_SYNAPSE_ACTION_NONE, |
| 228 | COUNTER_SYNAPSE_ACTION_BOTH_EDGES |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 229 | }; |
| 230 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 231 | static 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 Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 235 | { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 236 | enum counter_function function; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 237 | int err; |
| 238 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 239 | err = stm32_count_function_read(counter, count, &function); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 240 | if (err) |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 241 | return err; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 242 | |
| 243 | switch (function) { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 244 | case COUNTER_FUNCTION_INCREASE: |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 245 | /* counts on internal clock when CEN=1 */ |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 246 | *action = COUNTER_SYNAPSE_ACTION_NONE; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 247 | return 0; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 248 | case COUNTER_FUNCTION_QUADRATURE_X2_A: |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 249 | /* counts up/down on TI1FP1 edge depending on TI2FP2 level */ |
| 250 | if (synapse->signal->id == count->synapses[0].signal->id) |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 251 | *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 252 | else |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 253 | *action = COUNTER_SYNAPSE_ACTION_NONE; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 254 | return 0; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 255 | case COUNTER_FUNCTION_QUADRATURE_X2_B: |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 256 | /* counts up/down on TI2FP2 edge depending on TI1FP1 level */ |
| 257 | if (synapse->signal->id == count->synapses[1].signal->id) |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 258 | *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 259 | else |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 260 | *action = COUNTER_SYNAPSE_ACTION_NONE; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 261 | return 0; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 262 | case COUNTER_FUNCTION_QUADRATURE_X4: |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 263 | /* counts up/down on both TI1FP1 and TI2FP2 edges */ |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 264 | *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 265 | return 0; |
| 266 | default: |
| 267 | return -EINVAL; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 268 | } |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static const struct counter_ops stm32_timer_cnt_ops = { |
| 272 | .count_read = stm32_count_read, |
| 273 | .count_write = stm32_count_write, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 274 | .function_read = stm32_count_function_read, |
| 275 | .function_write = stm32_count_function_write, |
| 276 | .action_read = stm32_action_read, |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | static 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 | |
| 290 | static 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 | |
| 303 | static 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 | |
| 314 | static 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önig | e1717d2 | 2021-12-30 16:02:57 +0100 | [diff] [blame] | 319 | struct counter_device *counter; |
| 320 | int ret; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 321 | |
| 322 | if (IS_ERR_OR_NULL(ddata)) |
| 323 | return -EINVAL; |
| 324 | |
Uwe Kleine-König | e1717d2 | 2021-12-30 16:02:57 +0100 | [diff] [blame] | 325 | counter = devm_counter_alloc(dev, sizeof(*priv)); |
| 326 | if (!counter) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 327 | return -ENOMEM; |
| 328 | |
Uwe Kleine-König | e1717d2 | 2021-12-30 16:02:57 +0100 | [diff] [blame] | 329 | priv = counter_priv(counter); |
| 330 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 331 | priv->regmap = ddata->regmap; |
| 332 | priv->clk = ddata->clk; |
Fabrice Gasnier | e4c3e13 | 2021-03-02 15:43:55 +0100 | [diff] [blame] | 333 | priv->max_arr = ddata->max_arr; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 334 | |
Uwe Kleine-König | e1717d2 | 2021-12-30 16:02:57 +0100 | [diff] [blame] | 335 | 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 Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 342 | |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 343 | platform_set_drvdata(pdev, priv); |
| 344 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 345 | /* Register Counter device */ |
Uwe Kleine-König | e1717d2 | 2021-12-30 16:02:57 +0100 | [diff] [blame] | 346 | 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 Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 351 | } |
| 352 | |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 353 | static 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 | |
| 373 | static 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 | |
| 397 | static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend, |
| 398 | stm32_timer_cnt_resume); |
| 399 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 400 | static const struct of_device_id stm32_timer_cnt_of_match[] = { |
| 401 | { .compatible = "st,stm32-timer-counter", }, |
| 402 | {}, |
| 403 | }; |
| 404 | MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match); |
| 405 | |
| 406 | static 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 Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 411 | .pm = &stm32_timer_cnt_pm_ops, |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 412 | }, |
| 413 | }; |
| 414 | module_platform_driver(stm32_timer_cnt_driver); |
| 415 | |
| 416 | MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>"); |
| 417 | MODULE_ALIAS("platform:stm32-timer-counter"); |
| 418 | MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver"); |
| 419 | MODULE_LICENSE("GPL v2"); |