stm32-timer-cnt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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>
  11. #include <linux/iio/iio.h>
  12. #include <linux/iio/types.h>
  13. #include <linux/mfd/stm32-timers.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #define TIM_CCMR_CCXS (BIT(8) | BIT(0))
  17. #define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
  18. TIM_CCMR_IC1F | TIM_CCMR_IC2F)
  19. #define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
  20. TIM_CCER_CC2P | TIM_CCER_CC2NP)
  21. struct stm32_timer_cnt {
  22. struct counter_device counter;
  23. struct regmap *regmap;
  24. struct clk *clk;
  25. u32 max_arr;
  26. };
  27. /**
  28. * stm32_count_function - enumerates stm32 timer counter encoder modes
  29. * @STM32_COUNT_SLAVE_MODE_DISABLED: counts on internal clock when CEN=1
  30. * @STM32_COUNT_ENCODER_MODE_1: counts TI1FP1 edges, depending on TI2FP2 level
  31. * @STM32_COUNT_ENCODER_MODE_2: counts TI2FP2 edges, depending on TI1FP1 level
  32. * @STM32_COUNT_ENCODER_MODE_3: counts on both TI1FP1 and TI2FP2 edges
  33. */
  34. enum stm32_count_function {
  35. STM32_COUNT_SLAVE_MODE_DISABLED,
  36. STM32_COUNT_ENCODER_MODE_1,
  37. STM32_COUNT_ENCODER_MODE_2,
  38. STM32_COUNT_ENCODER_MODE_3,
  39. };
  40. static enum counter_count_function stm32_count_functions[] = {
  41. [STM32_COUNT_SLAVE_MODE_DISABLED] = COUNTER_COUNT_FUNCTION_INCREASE,
  42. [STM32_COUNT_ENCODER_MODE_1] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_A,
  43. [STM32_COUNT_ENCODER_MODE_2] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_B,
  44. [STM32_COUNT_ENCODER_MODE_3] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4,
  45. };
  46. static int stm32_count_read(struct counter_device *counter,
  47. struct counter_count *count,
  48. struct counter_count_read_value *val)
  49. {
  50. struct stm32_timer_cnt *const priv = counter->priv;
  51. u32 cnt;
  52. regmap_read(priv->regmap, TIM_CNT, &cnt);
  53. counter_count_read_value_set(val, COUNTER_COUNT_POSITION, &cnt);
  54. return 0;
  55. }
  56. static int stm32_count_write(struct counter_device *counter,
  57. struct counter_count *count,
  58. struct counter_count_write_value *val)
  59. {
  60. struct stm32_timer_cnt *const priv = counter->priv;
  61. u32 cnt, ceiling;
  62. int err;
  63. err = counter_count_write_value_get(&cnt, COUNTER_COUNT_POSITION, val);
  64. if (err)
  65. return err;
  66. regmap_read(priv->regmap, TIM_ARR, &ceiling);
  67. if (cnt > ceiling)
  68. return -EINVAL;
  69. return regmap_write(priv->regmap, TIM_CNT, cnt);
  70. }
  71. static int stm32_count_function_get(struct counter_device *counter,
  72. struct counter_count *count,
  73. size_t *function)
  74. {
  75. struct stm32_timer_cnt *const priv = counter->priv;
  76. u32 smcr;
  77. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  78. switch (smcr & TIM_SMCR_SMS) {
  79. case 0:
  80. *function = STM32_COUNT_SLAVE_MODE_DISABLED;
  81. return 0;
  82. case 1:
  83. *function = STM32_COUNT_ENCODER_MODE_1;
  84. return 0;
  85. case 2:
  86. *function = STM32_COUNT_ENCODER_MODE_2;
  87. return 0;
  88. case 3:
  89. *function = STM32_COUNT_ENCODER_MODE_3;
  90. return 0;
  91. default:
  92. return -EINVAL;
  93. }
  94. }
  95. static int stm32_count_function_set(struct counter_device *counter,
  96. struct counter_count *count,
  97. size_t function)
  98. {
  99. struct stm32_timer_cnt *const priv = counter->priv;
  100. u32 cr1, sms;
  101. switch (function) {
  102. case STM32_COUNT_SLAVE_MODE_DISABLED:
  103. sms = 0;
  104. break;
  105. case STM32_COUNT_ENCODER_MODE_1:
  106. sms = 1;
  107. break;
  108. case STM32_COUNT_ENCODER_MODE_2:
  109. sms = 2;
  110. break;
  111. case STM32_COUNT_ENCODER_MODE_3:
  112. sms = 3;
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. /* Store enable status */
  118. regmap_read(priv->regmap, TIM_CR1, &cr1);
  119. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  120. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
  121. /* Make sure that registers are updated */
  122. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  123. /* Restore the enable status */
  124. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
  125. return 0;
  126. }
  127. static ssize_t stm32_count_direction_read(struct counter_device *counter,
  128. struct counter_count *count,
  129. void *private, char *buf)
  130. {
  131. struct stm32_timer_cnt *const priv = counter->priv;
  132. const char *direction;
  133. u32 cr1;
  134. regmap_read(priv->regmap, TIM_CR1, &cr1);
  135. direction = (cr1 & TIM_CR1_DIR) ? "backward" : "forward";
  136. return scnprintf(buf, PAGE_SIZE, "%s\n", direction);
  137. }
  138. static ssize_t stm32_count_ceiling_read(struct counter_device *counter,
  139. struct counter_count *count,
  140. void *private, char *buf)
  141. {
  142. struct stm32_timer_cnt *const priv = counter->priv;
  143. u32 arr;
  144. regmap_read(priv->regmap, TIM_ARR, &arr);
  145. return snprintf(buf, PAGE_SIZE, "%u\n", arr);
  146. }
  147. static ssize_t stm32_count_ceiling_write(struct counter_device *counter,
  148. struct counter_count *count,
  149. void *private,
  150. const char *buf, size_t len)
  151. {
  152. struct stm32_timer_cnt *const priv = counter->priv;
  153. unsigned int ceiling;
  154. int ret;
  155. ret = kstrtouint(buf, 0, &ceiling);
  156. if (ret)
  157. return ret;
  158. if (ceiling > priv->max_arr)
  159. return -ERANGE;
  160. /* TIMx_ARR register shouldn't be buffered (ARPE=0) */
  161. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
  162. regmap_write(priv->regmap, TIM_ARR, ceiling);
  163. return len;
  164. }
  165. static ssize_t stm32_count_enable_read(struct counter_device *counter,
  166. struct counter_count *count,
  167. void *private, char *buf)
  168. {
  169. struct stm32_timer_cnt *const priv = counter->priv;
  170. u32 cr1;
  171. regmap_read(priv->regmap, TIM_CR1, &cr1);
  172. return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)(cr1 & TIM_CR1_CEN));
  173. }
  174. static ssize_t stm32_count_enable_write(struct counter_device *counter,
  175. struct counter_count *count,
  176. void *private,
  177. const char *buf, size_t len)
  178. {
  179. struct stm32_timer_cnt *const priv = counter->priv;
  180. int err;
  181. u32 cr1;
  182. bool enable;
  183. err = kstrtobool(buf, &enable);
  184. if (err)
  185. return err;
  186. if (enable) {
  187. regmap_read(priv->regmap, TIM_CR1, &cr1);
  188. if (!(cr1 & TIM_CR1_CEN))
  189. clk_enable(priv->clk);
  190. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
  191. TIM_CR1_CEN);
  192. } else {
  193. regmap_read(priv->regmap, TIM_CR1, &cr1);
  194. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  195. if (cr1 & TIM_CR1_CEN)
  196. clk_disable(priv->clk);
  197. }
  198. return len;
  199. }
  200. static const struct counter_count_ext stm32_count_ext[] = {
  201. {
  202. .name = "direction",
  203. .read = stm32_count_direction_read,
  204. },
  205. {
  206. .name = "enable",
  207. .read = stm32_count_enable_read,
  208. .write = stm32_count_enable_write
  209. },
  210. {
  211. .name = "ceiling",
  212. .read = stm32_count_ceiling_read,
  213. .write = stm32_count_ceiling_write
  214. },
  215. };
  216. enum stm32_synapse_action {
  217. STM32_SYNAPSE_ACTION_NONE,
  218. STM32_SYNAPSE_ACTION_BOTH_EDGES
  219. };
  220. static enum counter_synapse_action stm32_synapse_actions[] = {
  221. [STM32_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
  222. [STM32_SYNAPSE_ACTION_BOTH_EDGES] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES
  223. };
  224. static int stm32_action_get(struct counter_device *counter,
  225. struct counter_count *count,
  226. struct counter_synapse *synapse,
  227. size_t *action)
  228. {
  229. size_t function;
  230. int err;
  231. err = stm32_count_function_get(counter, count, &function);
  232. if (err)
  233. return err;
  234. switch (function) {
  235. case STM32_COUNT_SLAVE_MODE_DISABLED:
  236. /* counts on internal clock when CEN=1 */
  237. *action = STM32_SYNAPSE_ACTION_NONE;
  238. return 0;
  239. case STM32_COUNT_ENCODER_MODE_1:
  240. /* counts up/down on TI1FP1 edge depending on TI2FP2 level */
  241. if (synapse->signal->id == count->synapses[0].signal->id)
  242. *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
  243. else
  244. *action = STM32_SYNAPSE_ACTION_NONE;
  245. return 0;
  246. case STM32_COUNT_ENCODER_MODE_2:
  247. /* counts up/down on TI2FP2 edge depending on TI1FP1 level */
  248. if (synapse->signal->id == count->synapses[1].signal->id)
  249. *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
  250. else
  251. *action = STM32_SYNAPSE_ACTION_NONE;
  252. return 0;
  253. case STM32_COUNT_ENCODER_MODE_3:
  254. /* counts up/down on both TI1FP1 and TI2FP2 edges */
  255. *action = STM32_SYNAPSE_ACTION_BOTH_EDGES;
  256. return 0;
  257. default:
  258. return -EINVAL;
  259. }
  260. }
  261. static const struct counter_ops stm32_timer_cnt_ops = {
  262. .count_read = stm32_count_read,
  263. .count_write = stm32_count_write,
  264. .function_get = stm32_count_function_get,
  265. .function_set = stm32_count_function_set,
  266. .action_get = stm32_action_get,
  267. };
  268. static struct counter_signal stm32_signals[] = {
  269. {
  270. .id = 0,
  271. .name = "Channel 1 Quadrature A"
  272. },
  273. {
  274. .id = 1,
  275. .name = "Channel 1 Quadrature B"
  276. }
  277. };
  278. static struct counter_synapse stm32_count_synapses[] = {
  279. {
  280. .actions_list = stm32_synapse_actions,
  281. .num_actions = ARRAY_SIZE(stm32_synapse_actions),
  282. .signal = &stm32_signals[0]
  283. },
  284. {
  285. .actions_list = stm32_synapse_actions,
  286. .num_actions = ARRAY_SIZE(stm32_synapse_actions),
  287. .signal = &stm32_signals[1]
  288. }
  289. };
  290. static struct counter_count stm32_counts = {
  291. .id = 0,
  292. .name = "Channel 1 Count",
  293. .functions_list = stm32_count_functions,
  294. .num_functions = ARRAY_SIZE(stm32_count_functions),
  295. .synapses = stm32_count_synapses,
  296. .num_synapses = ARRAY_SIZE(stm32_count_synapses),
  297. .ext = stm32_count_ext,
  298. .num_ext = ARRAY_SIZE(stm32_count_ext)
  299. };
  300. static int stm32_timer_cnt_probe(struct platform_device *pdev)
  301. {
  302. struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
  303. struct device *dev = &pdev->dev;
  304. struct stm32_timer_cnt *priv;
  305. if (IS_ERR_OR_NULL(ddata))
  306. return -EINVAL;
  307. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  308. if (!priv)
  309. return -ENOMEM;
  310. priv->regmap = ddata->regmap;
  311. priv->clk = ddata->clk;
  312. priv->max_arr = ddata->max_arr;
  313. priv->counter.name = dev_name(dev);
  314. priv->counter.parent = dev;
  315. priv->counter.ops = &stm32_timer_cnt_ops;
  316. priv->counter.counts = &stm32_counts;
  317. priv->counter.num_counts = 1;
  318. priv->counter.signals = stm32_signals;
  319. priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
  320. priv->counter.priv = priv;
  321. /* Register Counter device */
  322. return devm_counter_register(dev, &priv->counter);
  323. }
  324. static const struct of_device_id stm32_timer_cnt_of_match[] = {
  325. { .compatible = "st,stm32-timer-counter", },
  326. {},
  327. };
  328. MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
  329. static struct platform_driver stm32_timer_cnt_driver = {
  330. .probe = stm32_timer_cnt_probe,
  331. .driver = {
  332. .name = "stm32-timer-counter",
  333. .of_match_table = stm32_timer_cnt_of_match,
  334. },
  335. };
  336. module_platform_driver(stm32_timer_cnt_driver);
  337. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  338. MODULE_ALIAS("platform:stm32-timer-counter");
  339. MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
  340. MODULE_LICENSE("GPL v2");