stm32-timer-cnt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <[email protected]>
  8. *
  9. */
  10. #include <linux/counter.h>
  11. #include <linux/mfd/stm32-timers.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/pinctrl/consumer.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/types.h>
  17. #define TIM_CCMR_CCXS (BIT(8) | BIT(0))
  18. #define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
  19. TIM_CCMR_IC1F | TIM_CCMR_IC2F)
  20. #define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
  21. TIM_CCER_CC2P | TIM_CCER_CC2NP)
  22. struct stm32_timer_regs {
  23. u32 cr1;
  24. u32 cnt;
  25. u32 smcr;
  26. u32 arr;
  27. };
  28. struct stm32_timer_cnt {
  29. struct regmap *regmap;
  30. struct clk *clk;
  31. u32 max_arr;
  32. bool enabled;
  33. struct stm32_timer_regs bak;
  34. };
  35. static const enum counter_function stm32_count_functions[] = {
  36. COUNTER_FUNCTION_INCREASE,
  37. COUNTER_FUNCTION_QUADRATURE_X2_A,
  38. COUNTER_FUNCTION_QUADRATURE_X2_B,
  39. COUNTER_FUNCTION_QUADRATURE_X4,
  40. };
  41. static int stm32_count_read(struct counter_device *counter,
  42. struct counter_count *count, u64 *val)
  43. {
  44. struct stm32_timer_cnt *const priv = counter_priv(counter);
  45. u32 cnt;
  46. regmap_read(priv->regmap, TIM_CNT, &cnt);
  47. *val = cnt;
  48. return 0;
  49. }
  50. static int stm32_count_write(struct counter_device *counter,
  51. struct counter_count *count, const u64 val)
  52. {
  53. struct stm32_timer_cnt *const priv = counter_priv(counter);
  54. u32 ceiling;
  55. regmap_read(priv->regmap, TIM_ARR, &ceiling);
  56. if (val > ceiling)
  57. return -EINVAL;
  58. return regmap_write(priv->regmap, TIM_CNT, val);
  59. }
  60. static int stm32_count_function_read(struct counter_device *counter,
  61. struct counter_count *count,
  62. enum counter_function *function)
  63. {
  64. struct stm32_timer_cnt *const priv = counter_priv(counter);
  65. u32 smcr;
  66. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  67. switch (smcr & TIM_SMCR_SMS) {
  68. case TIM_SMCR_SMS_SLAVE_MODE_DISABLED:
  69. *function = COUNTER_FUNCTION_INCREASE;
  70. return 0;
  71. case TIM_SMCR_SMS_ENCODER_MODE_1:
  72. *function = COUNTER_FUNCTION_QUADRATURE_X2_A;
  73. return 0;
  74. case TIM_SMCR_SMS_ENCODER_MODE_2:
  75. *function = COUNTER_FUNCTION_QUADRATURE_X2_B;
  76. return 0;
  77. case TIM_SMCR_SMS_ENCODER_MODE_3:
  78. *function = COUNTER_FUNCTION_QUADRATURE_X4;
  79. return 0;
  80. default:
  81. return -EINVAL;
  82. }
  83. }
  84. static int stm32_count_function_write(struct counter_device *counter,
  85. struct counter_count *count,
  86. enum counter_function function)
  87. {
  88. struct stm32_timer_cnt *const priv = counter_priv(counter);
  89. u32 cr1, sms;
  90. switch (function) {
  91. case COUNTER_FUNCTION_INCREASE:
  92. sms = TIM_SMCR_SMS_SLAVE_MODE_DISABLED;
  93. break;
  94. case COUNTER_FUNCTION_QUADRATURE_X2_A:
  95. sms = TIM_SMCR_SMS_ENCODER_MODE_1;
  96. break;
  97. case COUNTER_FUNCTION_QUADRATURE_X2_B:
  98. sms = TIM_SMCR_SMS_ENCODER_MODE_2;
  99. break;
  100. case COUNTER_FUNCTION_QUADRATURE_X4:
  101. sms = TIM_SMCR_SMS_ENCODER_MODE_3;
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. /* Store enable status */
  107. regmap_read(priv->regmap, TIM_CR1, &cr1);
  108. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  109. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
  110. /* Make sure that registers are updated */
  111. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  112. /* Restore the enable status */
  113. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
  114. return 0;
  115. }
  116. static int stm32_count_direction_read(struct counter_device *counter,
  117. struct counter_count *count,
  118. enum counter_count_direction *direction)
  119. {
  120. struct stm32_timer_cnt *const priv = counter_priv(counter);
  121. u32 cr1;
  122. regmap_read(priv->regmap, TIM_CR1, &cr1);
  123. *direction = (cr1 & TIM_CR1_DIR) ? COUNTER_COUNT_DIRECTION_BACKWARD :
  124. COUNTER_COUNT_DIRECTION_FORWARD;
  125. return 0;
  126. }
  127. static int stm32_count_ceiling_read(struct counter_device *counter,
  128. struct counter_count *count, u64 *ceiling)
  129. {
  130. struct stm32_timer_cnt *const priv = counter_priv(counter);
  131. u32 arr;
  132. regmap_read(priv->regmap, TIM_ARR, &arr);
  133. *ceiling = arr;
  134. return 0;
  135. }
  136. static int stm32_count_ceiling_write(struct counter_device *counter,
  137. struct counter_count *count, u64 ceiling)
  138. {
  139. struct stm32_timer_cnt *const priv = counter_priv(counter);
  140. if (ceiling > priv->max_arr)
  141. return -ERANGE;
  142. /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
  143. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
  144. regmap_write(priv->regmap, TIM_ARR, ceiling);
  145. return 0;
  146. }
  147. static int stm32_count_enable_read(struct counter_device *counter,
  148. struct counter_count *count, u8 *enable)
  149. {
  150. struct stm32_timer_cnt *const priv = counter_priv(counter);
  151. u32 cr1;
  152. regmap_read(priv->regmap, TIM_CR1, &cr1);
  153. *enable = cr1 & TIM_CR1_CEN;
  154. return 0;
  155. }
  156. static int stm32_count_enable_write(struct counter_device *counter,
  157. struct counter_count *count, u8 enable)
  158. {
  159. struct stm32_timer_cnt *const priv = counter_priv(counter);
  160. u32 cr1;
  161. if (enable) {
  162. regmap_read(priv->regmap, TIM_CR1, &cr1);
  163. if (!(cr1 & TIM_CR1_CEN))
  164. clk_enable(priv->clk);
  165. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
  166. TIM_CR1_CEN);
  167. } else {
  168. regmap_read(priv->regmap, TIM_CR1, &cr1);
  169. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  170. if (cr1 & TIM_CR1_CEN)
  171. clk_disable(priv->clk);
  172. }
  173. /* Keep enabled state to properly handle low power states */
  174. priv->enabled = enable;
  175. return 0;
  176. }
  177. static struct counter_comp stm32_count_ext[] = {
  178. COUNTER_COMP_DIRECTION(stm32_count_direction_read),
  179. COUNTER_COMP_ENABLE(stm32_count_enable_read, stm32_count_enable_write),
  180. COUNTER_COMP_CEILING(stm32_count_ceiling_read,
  181. stm32_count_ceiling_write),
  182. };
  183. static const enum counter_synapse_action stm32_synapse_actions[] = {
  184. COUNTER_SYNAPSE_ACTION_NONE,
  185. COUNTER_SYNAPSE_ACTION_BOTH_EDGES
  186. };
  187. static int stm32_action_read(struct counter_device *counter,
  188. struct counter_count *count,
  189. struct counter_synapse *synapse,
  190. enum counter_synapse_action *action)
  191. {
  192. enum counter_function function;
  193. int err;
  194. err = stm32_count_function_read(counter, count, &function);
  195. if (err)
  196. return err;
  197. switch (function) {
  198. case COUNTER_FUNCTION_INCREASE:
  199. /* counts on internal clock when CEN=1 */
  200. *action = COUNTER_SYNAPSE_ACTION_NONE;
  201. return 0;
  202. case COUNTER_FUNCTION_QUADRATURE_X2_A:
  203. /* counts up/down on TI1FP1 edge depending on TI2FP2 level */
  204. if (synapse->signal->id == count->synapses[0].signal->id)
  205. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  206. else
  207. *action = COUNTER_SYNAPSE_ACTION_NONE;
  208. return 0;
  209. case COUNTER_FUNCTION_QUADRATURE_X2_B:
  210. /* counts up/down on TI2FP2 edge depending on TI1FP1 level */
  211. if (synapse->signal->id == count->synapses[1].signal->id)
  212. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  213. else
  214. *action = COUNTER_SYNAPSE_ACTION_NONE;
  215. return 0;
  216. case COUNTER_FUNCTION_QUADRATURE_X4:
  217. /* counts up/down on both TI1FP1 and TI2FP2 edges */
  218. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  219. return 0;
  220. default:
  221. return -EINVAL;
  222. }
  223. }
  224. static const struct counter_ops stm32_timer_cnt_ops = {
  225. .count_read = stm32_count_read,
  226. .count_write = stm32_count_write,
  227. .function_read = stm32_count_function_read,
  228. .function_write = stm32_count_function_write,
  229. .action_read = stm32_action_read,
  230. };
  231. static struct counter_signal stm32_signals[] = {
  232. {
  233. .id = 0,
  234. .name = "Channel 1 Quadrature A"
  235. },
  236. {
  237. .id = 1,
  238. .name = "Channel 1 Quadrature B"
  239. }
  240. };
  241. static struct counter_synapse stm32_count_synapses[] = {
  242. {
  243. .actions_list = stm32_synapse_actions,
  244. .num_actions = ARRAY_SIZE(stm32_synapse_actions),
  245. .signal = &stm32_signals[0]
  246. },
  247. {
  248. .actions_list = stm32_synapse_actions,
  249. .num_actions = ARRAY_SIZE(stm32_synapse_actions),
  250. .signal = &stm32_signals[1]
  251. }
  252. };
  253. static struct counter_count stm32_counts = {
  254. .id = 0,
  255. .name = "Channel 1 Count",
  256. .functions_list = stm32_count_functions,
  257. .num_functions = ARRAY_SIZE(stm32_count_functions),
  258. .synapses = stm32_count_synapses,
  259. .num_synapses = ARRAY_SIZE(stm32_count_synapses),
  260. .ext = stm32_count_ext,
  261. .num_ext = ARRAY_SIZE(stm32_count_ext)
  262. };
  263. static int stm32_timer_cnt_probe(struct platform_device *pdev)
  264. {
  265. struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
  266. struct device *dev = &pdev->dev;
  267. struct stm32_timer_cnt *priv;
  268. struct counter_device *counter;
  269. int ret;
  270. if (IS_ERR_OR_NULL(ddata))
  271. return -EINVAL;
  272. counter = devm_counter_alloc(dev, sizeof(*priv));
  273. if (!counter)
  274. return -ENOMEM;
  275. priv = counter_priv(counter);
  276. priv->regmap = ddata->regmap;
  277. priv->clk = ddata->clk;
  278. priv->max_arr = ddata->max_arr;
  279. counter->name = dev_name(dev);
  280. counter->parent = dev;
  281. counter->ops = &stm32_timer_cnt_ops;
  282. counter->counts = &stm32_counts;
  283. counter->num_counts = 1;
  284. counter->signals = stm32_signals;
  285. counter->num_signals = ARRAY_SIZE(stm32_signals);
  286. platform_set_drvdata(pdev, priv);
  287. /* Register Counter device */
  288. ret = devm_counter_add(dev, counter);
  289. if (ret < 0)
  290. dev_err_probe(dev, ret, "Failed to add counter\n");
  291. return ret;
  292. }
  293. static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
  294. {
  295. struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
  296. /* Only take care of enabled counter: don't disturb other MFD child */
  297. if (priv->enabled) {
  298. /* Backup registers that may get lost in low power mode */
  299. regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr);
  300. regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr);
  301. regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt);
  302. regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1);
  303. /* Disable the counter */
  304. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  305. clk_disable(priv->clk);
  306. }
  307. return pinctrl_pm_select_sleep_state(dev);
  308. }
  309. static int __maybe_unused stm32_timer_cnt_resume(struct device *dev)
  310. {
  311. struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
  312. int ret;
  313. ret = pinctrl_pm_select_default_state(dev);
  314. if (ret)
  315. return ret;
  316. if (priv->enabled) {
  317. clk_enable(priv->clk);
  318. /* Restore registers that may have been lost */
  319. regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr);
  320. regmap_write(priv->regmap, TIM_ARR, priv->bak.arr);
  321. regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt);
  322. /* Also re-enables the counter */
  323. regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1);
  324. }
  325. return 0;
  326. }
  327. static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend,
  328. stm32_timer_cnt_resume);
  329. static const struct of_device_id stm32_timer_cnt_of_match[] = {
  330. { .compatible = "st,stm32-timer-counter", },
  331. {},
  332. };
  333. MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
  334. static struct platform_driver stm32_timer_cnt_driver = {
  335. .probe = stm32_timer_cnt_probe,
  336. .driver = {
  337. .name = "stm32-timer-counter",
  338. .of_match_table = stm32_timer_cnt_of_match,
  339. .pm = &stm32_timer_cnt_pm_ops,
  340. },
  341. };
  342. module_platform_driver(stm32_timer_cnt_driver);
  343. MODULE_AUTHOR("Benjamin Gaignard <[email protected]>");
  344. MODULE_ALIAS("platform:stm32-timer-counter");
  345. MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
  346. MODULE_LICENSE("GPL v2");
  347. MODULE_IMPORT_NS(COUNTER);