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 { |
| 32 | struct counter_device counter; |
| 33 | struct regmap *regmap; |
| 34 | struct clk *clk; |
Fabrice Gasnier | e4c3e13 | 2021-03-02 15:43:55 +0100 | [diff] [blame] | 35 | u32 max_arr; |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 36 | bool enabled; |
| 37 | struct stm32_timer_regs bak; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 38 | }; |
| 39 | |
William Breathitt Gray | 394a015 | 2021-08-03 21:06:15 +0900 | [diff] [blame] | 40 | static const enum counter_function stm32_count_functions[] = { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 41 | COUNTER_FUNCTION_INCREASE, |
| 42 | COUNTER_FUNCTION_QUADRATURE_X2_A, |
| 43 | COUNTER_FUNCTION_QUADRATURE_X2_B, |
| 44 | COUNTER_FUNCTION_QUADRATURE_X4, |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static int stm32_count_read(struct counter_device *counter, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 48 | struct counter_count *count, u64 *val) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 49 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 50 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 51 | u32 cnt; |
| 52 | |
| 53 | regmap_read(priv->regmap, TIM_CNT, &cnt); |
William Breathitt Gray | d49e6ee | 2019-10-06 16:03:09 -0400 | [diff] [blame] | 54 | *val = cnt; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int stm32_count_write(struct counter_device *counter, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 60 | struct counter_count *count, const u64 val) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 61 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 62 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Fabrice Gasnier | b14d72a | 2021-03-03 18:49:49 +0100 | [diff] [blame] | 63 | u32 ceiling; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 64 | |
Fabrice Gasnier | b14d72a | 2021-03-03 18:49:49 +0100 | [diff] [blame] | 65 | regmap_read(priv->regmap, TIM_ARR, &ceiling); |
| 66 | if (val > ceiling) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 67 | return -EINVAL; |
| 68 | |
William Breathitt Gray | d49e6ee | 2019-10-06 16:03:09 -0400 | [diff] [blame] | 69 | return regmap_write(priv->regmap, TIM_CNT, val); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 70 | } |
| 71 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 72 | static int stm32_count_function_read(struct counter_device *counter, |
| 73 | struct counter_count *count, |
| 74 | enum counter_function *function) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 75 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 76 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 77 | u32 smcr; |
| 78 | |
| 79 | regmap_read(priv->regmap, TIM_SMCR, &smcr); |
| 80 | |
| 81 | switch (smcr & TIM_SMCR_SMS) { |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 82 | case TIM_SMCR_SMS_SLAVE_MODE_DISABLED: |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 83 | *function = COUNTER_FUNCTION_INCREASE; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 84 | return 0; |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 85 | case TIM_SMCR_SMS_ENCODER_MODE_1: |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 86 | *function = COUNTER_FUNCTION_QUADRATURE_X2_A; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 87 | return 0; |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 88 | case TIM_SMCR_SMS_ENCODER_MODE_2: |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 89 | *function = COUNTER_FUNCTION_QUADRATURE_X2_B; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 90 | return 0; |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 91 | case TIM_SMCR_SMS_ENCODER_MODE_3: |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 92 | *function = COUNTER_FUNCTION_QUADRATURE_X4; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 93 | return 0; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 94 | default: |
| 95 | return -EINVAL; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 96 | } |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 97 | } |
| 98 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 99 | static int stm32_count_function_write(struct counter_device *counter, |
| 100 | struct counter_count *count, |
| 101 | enum counter_function function) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 102 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 103 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 104 | u32 cr1, sms; |
| 105 | |
| 106 | switch (function) { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 107 | case COUNTER_FUNCTION_INCREASE: |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 108 | sms = TIM_SMCR_SMS_SLAVE_MODE_DISABLED; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 109 | break; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 110 | case COUNTER_FUNCTION_QUADRATURE_X2_A: |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 111 | sms = TIM_SMCR_SMS_ENCODER_MODE_1; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 112 | break; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 113 | case COUNTER_FUNCTION_QUADRATURE_X2_B: |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 114 | sms = TIM_SMCR_SMS_ENCODER_MODE_2; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 115 | break; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 116 | case COUNTER_FUNCTION_QUADRATURE_X4: |
William Breathitt Gray | ea434ff | 2021-08-27 12:47:46 +0900 | [diff] [blame] | 117 | sms = TIM_SMCR_SMS_ENCODER_MODE_3; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 118 | break; |
| 119 | default: |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 120 | return -EINVAL; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* Store enable status */ |
| 124 | regmap_read(priv->regmap, TIM_CR1, &cr1); |
| 125 | |
| 126 | regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0); |
| 127 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 128 | regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms); |
| 129 | |
| 130 | /* Make sure that registers are updated */ |
| 131 | regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG); |
| 132 | |
| 133 | /* Restore the enable status */ |
| 134 | regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 139 | static int stm32_count_direction_read(struct counter_device *counter, |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 140 | struct counter_count *count, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 141 | enum counter_count_direction *direction) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 142 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 143 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 144 | u32 cr1; |
| 145 | |
| 146 | regmap_read(priv->regmap, TIM_CR1, &cr1); |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 147 | *direction = (cr1 & TIM_CR1_DIR) ? COUNTER_COUNT_DIRECTION_BACKWARD : |
| 148 | COUNTER_COUNT_DIRECTION_FORWARD; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 149 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 150 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 151 | } |
| 152 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 153 | static int stm32_count_ceiling_read(struct counter_device *counter, |
| 154 | struct counter_count *count, u64 *ceiling) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 155 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 156 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 157 | u32 arr; |
| 158 | |
| 159 | regmap_read(priv->regmap, TIM_ARR, &arr); |
| 160 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 161 | *ceiling = arr; |
| 162 | |
| 163 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 164 | } |
| 165 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 166 | static int stm32_count_ceiling_write(struct counter_device *counter, |
| 167 | struct counter_count *count, u64 ceiling) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 168 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 169 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 170 | |
Fabrice Gasnier | e4c3e13 | 2021-03-02 15:43:55 +0100 | [diff] [blame] | 171 | if (ceiling > priv->max_arr) |
| 172 | return -ERANGE; |
| 173 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 174 | /* TIMx_ARR register shouldn't be buffered (ARPE=0) */ |
| 175 | regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0); |
| 176 | regmap_write(priv->regmap, TIM_ARR, ceiling); |
| 177 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 178 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 179 | } |
| 180 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 181 | static int stm32_count_enable_read(struct counter_device *counter, |
| 182 | struct counter_count *count, u8 *enable) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 183 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 184 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 185 | u32 cr1; |
| 186 | |
| 187 | regmap_read(priv->regmap, TIM_CR1, &cr1); |
| 188 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 189 | *enable = cr1 & TIM_CR1_CEN; |
| 190 | |
| 191 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 192 | } |
| 193 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 194 | static int stm32_count_enable_write(struct counter_device *counter, |
| 195 | struct counter_count *count, u8 enable) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 196 | { |
Uwe Kleine-König | e152833 | 2021-12-30 16:02:49 +0100 | [diff] [blame^] | 197 | struct stm32_timer_cnt *const priv = counter_priv(counter); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 198 | u32 cr1; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 199 | |
| 200 | if (enable) { |
| 201 | regmap_read(priv->regmap, TIM_CR1, &cr1); |
Colin Ian King | 76510ec | 2019-09-25 10:51:26 +0100 | [diff] [blame] | 202 | if (!(cr1 & TIM_CR1_CEN)) |
| 203 | clk_enable(priv->clk); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 204 | |
| 205 | regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, |
| 206 | TIM_CR1_CEN); |
| 207 | } else { |
| 208 | regmap_read(priv->regmap, TIM_CR1, &cr1); |
| 209 | regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0); |
| 210 | if (cr1 & TIM_CR1_CEN) |
| 211 | clk_disable(priv->clk); |
| 212 | } |
| 213 | |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 214 | /* Keep enabled state to properly handle low power states */ |
| 215 | priv->enabled = enable; |
| 216 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 217 | return 0; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 218 | } |
| 219 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 220 | static struct counter_comp stm32_count_ext[] = { |
| 221 | COUNTER_COMP_DIRECTION(stm32_count_direction_read), |
| 222 | COUNTER_COMP_ENABLE(stm32_count_enable_read, stm32_count_enable_write), |
| 223 | COUNTER_COMP_CEILING(stm32_count_ceiling_read, |
| 224 | stm32_count_ceiling_write), |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 225 | }; |
| 226 | |
William Breathitt Gray | d0ce3d5 | 2021-06-09 10:31:20 +0900 | [diff] [blame] | 227 | static const enum counter_synapse_action stm32_synapse_actions[] = { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 228 | COUNTER_SYNAPSE_ACTION_NONE, |
| 229 | COUNTER_SYNAPSE_ACTION_BOTH_EDGES |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 230 | }; |
| 231 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 232 | static int stm32_action_read(struct counter_device *counter, |
| 233 | struct counter_count *count, |
| 234 | struct counter_synapse *synapse, |
| 235 | enum counter_synapse_action *action) |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 236 | { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 237 | enum counter_function function; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 238 | int err; |
| 239 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 240 | err = stm32_count_function_read(counter, count, &function); |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 241 | if (err) |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 242 | return err; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 243 | |
| 244 | switch (function) { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 245 | case COUNTER_FUNCTION_INCREASE: |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 246 | /* counts on internal clock when CEN=1 */ |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 247 | *action = COUNTER_SYNAPSE_ACTION_NONE; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 248 | return 0; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 249 | case COUNTER_FUNCTION_QUADRATURE_X2_A: |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 250 | /* counts up/down on TI1FP1 edge depending on TI2FP2 level */ |
| 251 | if (synapse->signal->id == count->synapses[0].signal->id) |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 252 | *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 253 | else |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 254 | *action = COUNTER_SYNAPSE_ACTION_NONE; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 255 | return 0; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 256 | case COUNTER_FUNCTION_QUADRATURE_X2_B: |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 257 | /* counts up/down on TI2FP2 edge depending on TI1FP1 level */ |
| 258 | if (synapse->signal->id == count->synapses[1].signal->id) |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 259 | *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 260 | else |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 261 | *action = COUNTER_SYNAPSE_ACTION_NONE; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 262 | return 0; |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 263 | case COUNTER_FUNCTION_QUADRATURE_X4: |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 264 | /* counts up/down on both TI1FP1 and TI2FP2 edges */ |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 265 | *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES; |
William Breathitt Gray | fae6f62 | 2021-02-26 10:29:31 +0900 | [diff] [blame] | 266 | return 0; |
| 267 | default: |
| 268 | return -EINVAL; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 269 | } |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static const struct counter_ops stm32_timer_cnt_ops = { |
| 273 | .count_read = stm32_count_read, |
| 274 | .count_write = stm32_count_write, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 275 | .function_read = stm32_count_function_read, |
| 276 | .function_write = stm32_count_function_write, |
| 277 | .action_read = stm32_action_read, |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | static struct counter_signal stm32_signals[] = { |
| 281 | { |
| 282 | .id = 0, |
| 283 | .name = "Channel 1 Quadrature A" |
| 284 | }, |
| 285 | { |
| 286 | .id = 1, |
| 287 | .name = "Channel 1 Quadrature B" |
| 288 | } |
| 289 | }; |
| 290 | |
| 291 | static struct counter_synapse stm32_count_synapses[] = { |
| 292 | { |
| 293 | .actions_list = stm32_synapse_actions, |
| 294 | .num_actions = ARRAY_SIZE(stm32_synapse_actions), |
| 295 | .signal = &stm32_signals[0] |
| 296 | }, |
| 297 | { |
| 298 | .actions_list = stm32_synapse_actions, |
| 299 | .num_actions = ARRAY_SIZE(stm32_synapse_actions), |
| 300 | .signal = &stm32_signals[1] |
| 301 | } |
| 302 | }; |
| 303 | |
| 304 | static struct counter_count stm32_counts = { |
| 305 | .id = 0, |
| 306 | .name = "Channel 1 Count", |
| 307 | .functions_list = stm32_count_functions, |
| 308 | .num_functions = ARRAY_SIZE(stm32_count_functions), |
| 309 | .synapses = stm32_count_synapses, |
| 310 | .num_synapses = ARRAY_SIZE(stm32_count_synapses), |
| 311 | .ext = stm32_count_ext, |
| 312 | .num_ext = ARRAY_SIZE(stm32_count_ext) |
| 313 | }; |
| 314 | |
| 315 | static int stm32_timer_cnt_probe(struct platform_device *pdev) |
| 316 | { |
| 317 | struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent); |
| 318 | struct device *dev = &pdev->dev; |
| 319 | struct stm32_timer_cnt *priv; |
| 320 | |
| 321 | if (IS_ERR_OR_NULL(ddata)) |
| 322 | return -EINVAL; |
| 323 | |
| 324 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 325 | if (!priv) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | priv->regmap = ddata->regmap; |
| 329 | priv->clk = ddata->clk; |
Fabrice Gasnier | e4c3e13 | 2021-03-02 15:43:55 +0100 | [diff] [blame] | 330 | priv->max_arr = ddata->max_arr; |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 331 | |
| 332 | priv->counter.name = dev_name(dev); |
| 333 | priv->counter.parent = dev; |
| 334 | priv->counter.ops = &stm32_timer_cnt_ops; |
| 335 | priv->counter.counts = &stm32_counts; |
| 336 | priv->counter.num_counts = 1; |
| 337 | priv->counter.signals = stm32_signals; |
| 338 | priv->counter.num_signals = ARRAY_SIZE(stm32_signals); |
| 339 | priv->counter.priv = priv; |
| 340 | |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 341 | platform_set_drvdata(pdev, priv); |
| 342 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 343 | /* Register Counter device */ |
| 344 | return devm_counter_register(dev, &priv->counter); |
| 345 | } |
| 346 | |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 347 | static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev) |
| 348 | { |
| 349 | struct stm32_timer_cnt *priv = dev_get_drvdata(dev); |
| 350 | |
| 351 | /* Only take care of enabled counter: don't disturb other MFD child */ |
| 352 | if (priv->enabled) { |
| 353 | /* Backup registers that may get lost in low power mode */ |
| 354 | regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr); |
| 355 | regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr); |
| 356 | regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt); |
| 357 | regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1); |
| 358 | |
| 359 | /* Disable the counter */ |
| 360 | regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0); |
| 361 | clk_disable(priv->clk); |
| 362 | } |
| 363 | |
| 364 | return pinctrl_pm_select_sleep_state(dev); |
| 365 | } |
| 366 | |
| 367 | static int __maybe_unused stm32_timer_cnt_resume(struct device *dev) |
| 368 | { |
| 369 | struct stm32_timer_cnt *priv = dev_get_drvdata(dev); |
| 370 | int ret; |
| 371 | |
| 372 | ret = pinctrl_pm_select_default_state(dev); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | |
| 376 | if (priv->enabled) { |
| 377 | clk_enable(priv->clk); |
| 378 | |
| 379 | /* Restore registers that may have been lost */ |
| 380 | regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr); |
| 381 | regmap_write(priv->regmap, TIM_ARR, priv->bak.arr); |
| 382 | regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt); |
| 383 | |
| 384 | /* Also re-enables the counter */ |
| 385 | regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1); |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend, |
| 392 | stm32_timer_cnt_resume); |
| 393 | |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 394 | static const struct of_device_id stm32_timer_cnt_of_match[] = { |
| 395 | { .compatible = "st,stm32-timer-counter", }, |
| 396 | {}, |
| 397 | }; |
| 398 | MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match); |
| 399 | |
| 400 | static struct platform_driver stm32_timer_cnt_driver = { |
| 401 | .probe = stm32_timer_cnt_probe, |
| 402 | .driver = { |
| 403 | .name = "stm32-timer-counter", |
| 404 | .of_match_table = stm32_timer_cnt_of_match, |
Fabrice Gasnier | c5b8425 | 2020-02-10 18:19:58 +0100 | [diff] [blame] | 405 | .pm = &stm32_timer_cnt_pm_ops, |
Benjamin Gaignard | ad29937 | 2019-04-02 15:30:42 +0900 | [diff] [blame] | 406 | }, |
| 407 | }; |
| 408 | module_platform_driver(stm32_timer_cnt_driver); |
| 409 | |
| 410 | MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>"); |
| 411 | MODULE_ALIAS("platform:stm32-timer-counter"); |
| 412 | MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver"); |
| 413 | MODULE_LICENSE("GPL v2"); |