stm32-timer-cnt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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/interrupt.h>
  12. #include <linux/mfd/stm32-timers.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/types.h>
  19. #define TIM_CCMR_CCXS (BIT(8) | BIT(0))
  20. #define TIM_CCMR_MASK (TIM_CCMR_CC1S | TIM_CCMR_CC2S | \
  21. TIM_CCMR_IC1F | TIM_CCMR_IC2F)
  22. #define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
  23. TIM_CCER_CC2P | TIM_CCER_CC2NP)
  24. #define STM32_CH1_SIG 0
  25. #define STM32_CH2_SIG 1
  26. #define STM32_CLOCK_SIG 2
  27. #define STM32_CH3_SIG 3
  28. #define STM32_CH4_SIG 4
  29. struct stm32_timer_regs {
  30. u32 cr1;
  31. u32 cnt;
  32. u32 smcr;
  33. u32 arr;
  34. };
  35. struct stm32_timer_cnt {
  36. struct regmap *regmap;
  37. struct clk *clk;
  38. u32 max_arr;
  39. bool enabled;
  40. struct stm32_timer_regs bak;
  41. bool has_encoder;
  42. unsigned int nchannels;
  43. unsigned int nr_irqs;
  44. spinlock_t lock; /* protects nb_ovf */
  45. u64 nb_ovf;
  46. };
  47. static const enum counter_function stm32_count_functions[] = {
  48. COUNTER_FUNCTION_INCREASE,
  49. COUNTER_FUNCTION_QUADRATURE_X2_A,
  50. COUNTER_FUNCTION_QUADRATURE_X2_B,
  51. COUNTER_FUNCTION_QUADRATURE_X4,
  52. };
  53. static int stm32_count_read(struct counter_device *counter,
  54. struct counter_count *count, u64 *val)
  55. {
  56. struct stm32_timer_cnt *const priv = counter_priv(counter);
  57. u32 cnt;
  58. regmap_read(priv->regmap, TIM_CNT, &cnt);
  59. *val = cnt;
  60. return 0;
  61. }
  62. static int stm32_count_write(struct counter_device *counter,
  63. struct counter_count *count, const u64 val)
  64. {
  65. struct stm32_timer_cnt *const priv = counter_priv(counter);
  66. u32 ceiling;
  67. regmap_read(priv->regmap, TIM_ARR, &ceiling);
  68. if (val > ceiling)
  69. return -EINVAL;
  70. return regmap_write(priv->regmap, TIM_CNT, val);
  71. }
  72. static int stm32_count_function_read(struct counter_device *counter,
  73. struct counter_count *count,
  74. enum counter_function *function)
  75. {
  76. struct stm32_timer_cnt *const priv = counter_priv(counter);
  77. u32 smcr;
  78. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  79. switch (smcr & TIM_SMCR_SMS) {
  80. case TIM_SMCR_SMS_SLAVE_MODE_DISABLED:
  81. *function = COUNTER_FUNCTION_INCREASE;
  82. return 0;
  83. case TIM_SMCR_SMS_ENCODER_MODE_1:
  84. *function = COUNTER_FUNCTION_QUADRATURE_X2_A;
  85. return 0;
  86. case TIM_SMCR_SMS_ENCODER_MODE_2:
  87. *function = COUNTER_FUNCTION_QUADRATURE_X2_B;
  88. return 0;
  89. case TIM_SMCR_SMS_ENCODER_MODE_3:
  90. *function = COUNTER_FUNCTION_QUADRATURE_X4;
  91. return 0;
  92. default:
  93. return -EINVAL;
  94. }
  95. }
  96. static int stm32_count_function_write(struct counter_device *counter,
  97. struct counter_count *count,
  98. enum counter_function function)
  99. {
  100. struct stm32_timer_cnt *const priv = counter_priv(counter);
  101. u32 cr1, sms;
  102. switch (function) {
  103. case COUNTER_FUNCTION_INCREASE:
  104. sms = TIM_SMCR_SMS_SLAVE_MODE_DISABLED;
  105. break;
  106. case COUNTER_FUNCTION_QUADRATURE_X2_A:
  107. if (!priv->has_encoder)
  108. return -EOPNOTSUPP;
  109. sms = TIM_SMCR_SMS_ENCODER_MODE_1;
  110. break;
  111. case COUNTER_FUNCTION_QUADRATURE_X2_B:
  112. if (!priv->has_encoder)
  113. return -EOPNOTSUPP;
  114. sms = TIM_SMCR_SMS_ENCODER_MODE_2;
  115. break;
  116. case COUNTER_FUNCTION_QUADRATURE_X4:
  117. if (!priv->has_encoder)
  118. return -EOPNOTSUPP;
  119. sms = TIM_SMCR_SMS_ENCODER_MODE_3;
  120. break;
  121. default:
  122. return -EINVAL;
  123. }
  124. /* Store enable status */
  125. regmap_read(priv->regmap, TIM_CR1, &cr1);
  126. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  127. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
  128. /* Make sure that registers are updated */
  129. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  130. /* Restore the enable status */
  131. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, cr1);
  132. return 0;
  133. }
  134. static int stm32_count_direction_read(struct counter_device *counter,
  135. struct counter_count *count,
  136. enum counter_count_direction *direction)
  137. {
  138. struct stm32_timer_cnt *const priv = counter_priv(counter);
  139. u32 cr1;
  140. regmap_read(priv->regmap, TIM_CR1, &cr1);
  141. *direction = (cr1 & TIM_CR1_DIR) ? COUNTER_COUNT_DIRECTION_BACKWARD :
  142. COUNTER_COUNT_DIRECTION_FORWARD;
  143. return 0;
  144. }
  145. static int stm32_count_ceiling_read(struct counter_device *counter,
  146. struct counter_count *count, u64 *ceiling)
  147. {
  148. struct stm32_timer_cnt *const priv = counter_priv(counter);
  149. u32 arr;
  150. regmap_read(priv->regmap, TIM_ARR, &arr);
  151. *ceiling = arr;
  152. return 0;
  153. }
  154. static int stm32_count_ceiling_write(struct counter_device *counter,
  155. struct counter_count *count, u64 ceiling)
  156. {
  157. struct stm32_timer_cnt *const priv = counter_priv(counter);
  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 0;
  164. }
  165. static int stm32_count_enable_read(struct counter_device *counter,
  166. struct counter_count *count, u8 *enable)
  167. {
  168. struct stm32_timer_cnt *const priv = counter_priv(counter);
  169. u32 cr1;
  170. regmap_read(priv->regmap, TIM_CR1, &cr1);
  171. *enable = cr1 & TIM_CR1_CEN;
  172. return 0;
  173. }
  174. static int stm32_count_enable_write(struct counter_device *counter,
  175. struct counter_count *count, u8 enable)
  176. {
  177. struct stm32_timer_cnt *const priv = counter_priv(counter);
  178. u32 cr1;
  179. int ret;
  180. if (enable) {
  181. regmap_read(priv->regmap, TIM_CR1, &cr1);
  182. if (!(cr1 & TIM_CR1_CEN)) {
  183. ret = clk_enable(priv->clk);
  184. if (ret) {
  185. dev_err(counter->parent, "Cannot enable clock %d\n", ret);
  186. return ret;
  187. }
  188. }
  189. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
  190. TIM_CR1_CEN);
  191. } else {
  192. regmap_read(priv->regmap, TIM_CR1, &cr1);
  193. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  194. if (cr1 & TIM_CR1_CEN)
  195. clk_disable(priv->clk);
  196. }
  197. /* Keep enabled state to properly handle low power states */
  198. priv->enabled = enable;
  199. return 0;
  200. }
  201. static int stm32_count_prescaler_read(struct counter_device *counter,
  202. struct counter_count *count, u64 *prescaler)
  203. {
  204. struct stm32_timer_cnt *const priv = counter_priv(counter);
  205. u32 psc;
  206. regmap_read(priv->regmap, TIM_PSC, &psc);
  207. *prescaler = psc + 1;
  208. return 0;
  209. }
  210. static int stm32_count_prescaler_write(struct counter_device *counter,
  211. struct counter_count *count, u64 prescaler)
  212. {
  213. struct stm32_timer_cnt *const priv = counter_priv(counter);
  214. u32 psc;
  215. if (!prescaler || prescaler > MAX_TIM_PSC + 1)
  216. return -ERANGE;
  217. psc = prescaler - 1;
  218. return regmap_write(priv->regmap, TIM_PSC, psc);
  219. }
  220. static int stm32_count_cap_read(struct counter_device *counter,
  221. struct counter_count *count,
  222. size_t ch, u64 *cap)
  223. {
  224. struct stm32_timer_cnt *const priv = counter_priv(counter);
  225. u32 ccrx;
  226. if (ch >= priv->nchannels)
  227. return -EOPNOTSUPP;
  228. switch (ch) {
  229. case 0:
  230. regmap_read(priv->regmap, TIM_CCR1, &ccrx);
  231. break;
  232. case 1:
  233. regmap_read(priv->regmap, TIM_CCR2, &ccrx);
  234. break;
  235. case 2:
  236. regmap_read(priv->regmap, TIM_CCR3, &ccrx);
  237. break;
  238. case 3:
  239. regmap_read(priv->regmap, TIM_CCR4, &ccrx);
  240. break;
  241. default:
  242. return -EINVAL;
  243. }
  244. dev_dbg(counter->parent, "CCR%zu: 0x%08x\n", ch + 1, ccrx);
  245. *cap = ccrx;
  246. return 0;
  247. }
  248. static int stm32_count_nb_ovf_read(struct counter_device *counter,
  249. struct counter_count *count, u64 *val)
  250. {
  251. struct stm32_timer_cnt *const priv = counter_priv(counter);
  252. unsigned long irqflags;
  253. spin_lock_irqsave(&priv->lock, irqflags);
  254. *val = priv->nb_ovf;
  255. spin_unlock_irqrestore(&priv->lock, irqflags);
  256. return 0;
  257. }
  258. static int stm32_count_nb_ovf_write(struct counter_device *counter,
  259. struct counter_count *count, u64 val)
  260. {
  261. struct stm32_timer_cnt *const priv = counter_priv(counter);
  262. unsigned long irqflags;
  263. spin_lock_irqsave(&priv->lock, irqflags);
  264. priv->nb_ovf = val;
  265. spin_unlock_irqrestore(&priv->lock, irqflags);
  266. return 0;
  267. }
  268. static DEFINE_COUNTER_ARRAY_CAPTURE(stm32_count_cap_array, 4);
  269. static struct counter_comp stm32_count_ext[] = {
  270. COUNTER_COMP_DIRECTION(stm32_count_direction_read),
  271. COUNTER_COMP_ENABLE(stm32_count_enable_read, stm32_count_enable_write),
  272. COUNTER_COMP_CEILING(stm32_count_ceiling_read,
  273. stm32_count_ceiling_write),
  274. COUNTER_COMP_COUNT_U64("prescaler", stm32_count_prescaler_read,
  275. stm32_count_prescaler_write),
  276. COUNTER_COMP_ARRAY_CAPTURE(stm32_count_cap_read, NULL, stm32_count_cap_array),
  277. COUNTER_COMP_COUNT_U64("num_overflows", stm32_count_nb_ovf_read, stm32_count_nb_ovf_write),
  278. };
  279. static const enum counter_synapse_action stm32_clock_synapse_actions[] = {
  280. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  281. };
  282. static const enum counter_synapse_action stm32_synapse_actions[] = {
  283. COUNTER_SYNAPSE_ACTION_NONE,
  284. COUNTER_SYNAPSE_ACTION_BOTH_EDGES
  285. };
  286. static int stm32_action_read(struct counter_device *counter,
  287. struct counter_count *count,
  288. struct counter_synapse *synapse,
  289. enum counter_synapse_action *action)
  290. {
  291. enum counter_function function;
  292. int err;
  293. err = stm32_count_function_read(counter, count, &function);
  294. if (err)
  295. return err;
  296. switch (function) {
  297. case COUNTER_FUNCTION_INCREASE:
  298. /* counts on internal clock when CEN=1 */
  299. if (synapse->signal->id == STM32_CLOCK_SIG)
  300. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  301. else
  302. *action = COUNTER_SYNAPSE_ACTION_NONE;
  303. return 0;
  304. case COUNTER_FUNCTION_QUADRATURE_X2_A:
  305. /* counts up/down on TI1FP1 edge depending on TI2FP2 level */
  306. if (synapse->signal->id == STM32_CH1_SIG)
  307. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  308. else
  309. *action = COUNTER_SYNAPSE_ACTION_NONE;
  310. return 0;
  311. case COUNTER_FUNCTION_QUADRATURE_X2_B:
  312. /* counts up/down on TI2FP2 edge depending on TI1FP1 level */
  313. if (synapse->signal->id == STM32_CH2_SIG)
  314. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  315. else
  316. *action = COUNTER_SYNAPSE_ACTION_NONE;
  317. return 0;
  318. case COUNTER_FUNCTION_QUADRATURE_X4:
  319. /* counts up/down on both TI1FP1 and TI2FP2 edges */
  320. if (synapse->signal->id == STM32_CH1_SIG || synapse->signal->id == STM32_CH2_SIG)
  321. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  322. else
  323. *action = COUNTER_SYNAPSE_ACTION_NONE;
  324. return 0;
  325. default:
  326. return -EINVAL;
  327. }
  328. }
  329. struct stm32_count_cc_regs {
  330. u32 ccmr_reg;
  331. u32 ccmr_mask;
  332. u32 ccmr_bits;
  333. u32 ccer_bits;
  334. };
  335. static const struct stm32_count_cc_regs stm32_cc[] = {
  336. { TIM_CCMR1, TIM_CCMR_CC1S, TIM_CCMR_CC1S_TI1,
  337. TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC1NP },
  338. { TIM_CCMR1, TIM_CCMR_CC2S, TIM_CCMR_CC2S_TI2,
  339. TIM_CCER_CC2E | TIM_CCER_CC2P | TIM_CCER_CC2NP },
  340. { TIM_CCMR2, TIM_CCMR_CC3S, TIM_CCMR_CC3S_TI3,
  341. TIM_CCER_CC3E | TIM_CCER_CC3P | TIM_CCER_CC3NP },
  342. { TIM_CCMR2, TIM_CCMR_CC4S, TIM_CCMR_CC4S_TI4,
  343. TIM_CCER_CC4E | TIM_CCER_CC4P | TIM_CCER_CC4NP },
  344. };
  345. static int stm32_count_capture_configure(struct counter_device *counter, unsigned int ch,
  346. bool enable)
  347. {
  348. struct stm32_timer_cnt *const priv = counter_priv(counter);
  349. const struct stm32_count_cc_regs *cc;
  350. u32 ccmr, ccer;
  351. if (ch >= ARRAY_SIZE(stm32_cc) || ch >= priv->nchannels) {
  352. dev_err(counter->parent, "invalid ch: %d\n", ch);
  353. return -EINVAL;
  354. }
  355. cc = &stm32_cc[ch];
  356. /*
  357. * configure channel in input capture mode, map channel 1 on TI1, channel2 on TI2...
  358. * Select both edges / non-inverted to trigger a capture.
  359. */
  360. if (enable) {
  361. /* first clear possibly latched capture flag upon enabling */
  362. if (!regmap_test_bits(priv->regmap, TIM_CCER, cc->ccer_bits))
  363. regmap_write(priv->regmap, TIM_SR, ~TIM_SR_CC_IF(ch));
  364. regmap_update_bits(priv->regmap, cc->ccmr_reg, cc->ccmr_mask,
  365. cc->ccmr_bits);
  366. regmap_set_bits(priv->regmap, TIM_CCER, cc->ccer_bits);
  367. } else {
  368. regmap_clear_bits(priv->regmap, TIM_CCER, cc->ccer_bits);
  369. regmap_clear_bits(priv->regmap, cc->ccmr_reg, cc->ccmr_mask);
  370. }
  371. regmap_read(priv->regmap, cc->ccmr_reg, &ccmr);
  372. regmap_read(priv->regmap, TIM_CCER, &ccer);
  373. dev_dbg(counter->parent, "%s(%s) ch%d 0x%08x 0x%08x\n", __func__, enable ? "ena" : "dis",
  374. ch, ccmr, ccer);
  375. return 0;
  376. }
  377. static int stm32_count_events_configure(struct counter_device *counter)
  378. {
  379. struct stm32_timer_cnt *const priv = counter_priv(counter);
  380. struct counter_event_node *event_node;
  381. u32 dier = 0;
  382. int i, ret;
  383. list_for_each_entry(event_node, &counter->events_list, l) {
  384. switch (event_node->event) {
  385. case COUNTER_EVENT_OVERFLOW_UNDERFLOW:
  386. /* first clear possibly latched UIF before enabling */
  387. if (!regmap_test_bits(priv->regmap, TIM_DIER, TIM_DIER_UIE))
  388. regmap_write(priv->regmap, TIM_SR, (u32)~TIM_SR_UIF);
  389. dier |= TIM_DIER_UIE;
  390. break;
  391. case COUNTER_EVENT_CAPTURE:
  392. ret = stm32_count_capture_configure(counter, event_node->channel, true);
  393. if (ret)
  394. return ret;
  395. dier |= TIM_DIER_CCxIE(event_node->channel + 1);
  396. break;
  397. default:
  398. /* should never reach this path */
  399. return -EINVAL;
  400. }
  401. }
  402. /* Enable / disable all events at once, from events_list, so write all DIER bits */
  403. regmap_write(priv->regmap, TIM_DIER, dier);
  404. /* check for disabled capture events */
  405. for (i = 0 ; i < priv->nchannels; i++) {
  406. if (!(dier & TIM_DIER_CCxIE(i + 1))) {
  407. ret = stm32_count_capture_configure(counter, i, false);
  408. if (ret)
  409. return ret;
  410. }
  411. }
  412. return 0;
  413. }
  414. static int stm32_count_watch_validate(struct counter_device *counter,
  415. const struct counter_watch *watch)
  416. {
  417. struct stm32_timer_cnt *const priv = counter_priv(counter);
  418. /* Interrupts are optional */
  419. if (!priv->nr_irqs)
  420. return -EOPNOTSUPP;
  421. switch (watch->event) {
  422. case COUNTER_EVENT_CAPTURE:
  423. if (watch->channel >= priv->nchannels) {
  424. dev_err(counter->parent, "Invalid channel %d\n", watch->channel);
  425. return -EINVAL;
  426. }
  427. return 0;
  428. case COUNTER_EVENT_OVERFLOW_UNDERFLOW:
  429. return 0;
  430. default:
  431. return -EINVAL;
  432. }
  433. }
  434. static const struct counter_ops stm32_timer_cnt_ops = {
  435. .count_read = stm32_count_read,
  436. .count_write = stm32_count_write,
  437. .function_read = stm32_count_function_read,
  438. .function_write = stm32_count_function_write,
  439. .action_read = stm32_action_read,
  440. .events_configure = stm32_count_events_configure,
  441. .watch_validate = stm32_count_watch_validate,
  442. };
  443. static int stm32_count_clk_get_freq(struct counter_device *counter,
  444. struct counter_signal *signal, u64 *freq)
  445. {
  446. struct stm32_timer_cnt *const priv = counter_priv(counter);
  447. *freq = clk_get_rate(priv->clk);
  448. return 0;
  449. }
  450. static struct counter_comp stm32_count_clock_ext[] = {
  451. COUNTER_COMP_FREQUENCY(stm32_count_clk_get_freq),
  452. };
  453. static struct counter_signal stm32_signals[] = {
  454. /*
  455. * Need to declare all the signals as a static array, and keep the signals order here,
  456. * even if they're unused or unexisting on some timer instances. It's an abstraction,
  457. * e.g. high level view of the counter features.
  458. *
  459. * Userspace programs may rely on signal0 to be "Channel 1", signal1 to be "Channel 2",
  460. * and so on. When a signal is unexisting, the COUNTER_SYNAPSE_ACTION_NONE can be used,
  461. * to indicate that a signal doesn't affect the counter.
  462. */
  463. {
  464. .id = STM32_CH1_SIG,
  465. .name = "Channel 1"
  466. },
  467. {
  468. .id = STM32_CH2_SIG,
  469. .name = "Channel 2"
  470. },
  471. {
  472. .id = STM32_CLOCK_SIG,
  473. .name = "Clock",
  474. .ext = stm32_count_clock_ext,
  475. .num_ext = ARRAY_SIZE(stm32_count_clock_ext),
  476. },
  477. {
  478. .id = STM32_CH3_SIG,
  479. .name = "Channel 3"
  480. },
  481. {
  482. .id = STM32_CH4_SIG,
  483. .name = "Channel 4"
  484. },
  485. };
  486. static struct counter_synapse stm32_count_synapses[] = {
  487. {
  488. .actions_list = stm32_synapse_actions,
  489. .num_actions = ARRAY_SIZE(stm32_synapse_actions),
  490. .signal = &stm32_signals[STM32_CH1_SIG]
  491. },
  492. {
  493. .actions_list = stm32_synapse_actions,
  494. .num_actions = ARRAY_SIZE(stm32_synapse_actions),
  495. .signal = &stm32_signals[STM32_CH2_SIG]
  496. },
  497. {
  498. .actions_list = stm32_clock_synapse_actions,
  499. .num_actions = ARRAY_SIZE(stm32_clock_synapse_actions),
  500. .signal = &stm32_signals[STM32_CLOCK_SIG]
  501. },
  502. {
  503. .actions_list = stm32_synapse_actions,
  504. .num_actions = ARRAY_SIZE(stm32_synapse_actions),
  505. .signal = &stm32_signals[STM32_CH3_SIG]
  506. },
  507. {
  508. .actions_list = stm32_synapse_actions,
  509. .num_actions = ARRAY_SIZE(stm32_synapse_actions),
  510. .signal = &stm32_signals[STM32_CH4_SIG]
  511. },
  512. };
  513. static struct counter_count stm32_counts = {
  514. .id = 0,
  515. .name = "STM32 Timer Counter",
  516. .functions_list = stm32_count_functions,
  517. .num_functions = ARRAY_SIZE(stm32_count_functions),
  518. .synapses = stm32_count_synapses,
  519. .num_synapses = ARRAY_SIZE(stm32_count_synapses),
  520. .ext = stm32_count_ext,
  521. .num_ext = ARRAY_SIZE(stm32_count_ext)
  522. };
  523. static irqreturn_t stm32_timer_cnt_isr(int irq, void *ptr)
  524. {
  525. struct counter_device *counter = ptr;
  526. struct stm32_timer_cnt *const priv = counter_priv(counter);
  527. u32 clr = GENMASK(31, 0); /* SR flags can be cleared by writing 0 (wr 1 has no effect) */
  528. u32 sr, dier;
  529. int i;
  530. regmap_read(priv->regmap, TIM_SR, &sr);
  531. regmap_read(priv->regmap, TIM_DIER, &dier);
  532. /*
  533. * Some status bits in SR don't match with the enable bits in DIER. Only take care of
  534. * the possibly enabled bits in DIER (that matches in between SR and DIER).
  535. */
  536. dier &= (TIM_DIER_UIE | TIM_DIER_CC1IE | TIM_DIER_CC2IE | TIM_DIER_CC3IE | TIM_DIER_CC4IE);
  537. sr &= dier;
  538. if (sr & TIM_SR_UIF) {
  539. spin_lock(&priv->lock);
  540. priv->nb_ovf++;
  541. spin_unlock(&priv->lock);
  542. counter_push_event(counter, COUNTER_EVENT_OVERFLOW_UNDERFLOW, 0);
  543. dev_dbg(counter->parent, "COUNTER_EVENT_OVERFLOW_UNDERFLOW\n");
  544. /* SR flags can be cleared by writing 0, only clear relevant flag */
  545. clr &= ~TIM_SR_UIF;
  546. }
  547. /* Check capture events */
  548. for (i = 0 ; i < priv->nchannels; i++) {
  549. if (sr & TIM_SR_CC_IF(i)) {
  550. counter_push_event(counter, COUNTER_EVENT_CAPTURE, i);
  551. clr &= ~TIM_SR_CC_IF(i);
  552. dev_dbg(counter->parent, "COUNTER_EVENT_CAPTURE, %d\n", i);
  553. }
  554. }
  555. regmap_write(priv->regmap, TIM_SR, clr);
  556. return IRQ_HANDLED;
  557. };
  558. static void stm32_timer_cnt_detect_channels(struct device *dev,
  559. struct stm32_timer_cnt *priv)
  560. {
  561. u32 ccer, ccer_backup;
  562. regmap_read(priv->regmap, TIM_CCER, &ccer_backup);
  563. regmap_set_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE);
  564. regmap_read(priv->regmap, TIM_CCER, &ccer);
  565. regmap_write(priv->regmap, TIM_CCER, ccer_backup);
  566. priv->nchannels = hweight32(ccer & TIM_CCER_CCXE);
  567. dev_dbg(dev, "has %d cc channels\n", priv->nchannels);
  568. }
  569. /* encoder supported on TIM1 TIM2 TIM3 TIM4 TIM5 TIM8 */
  570. #define STM32_TIM_ENCODER_SUPPORTED (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(7))
  571. static const char * const stm32_timer_trigger_compat[] = {
  572. "st,stm32-timer-trigger",
  573. "st,stm32h7-timer-trigger",
  574. };
  575. static int stm32_timer_cnt_probe_encoder(struct device *dev,
  576. struct stm32_timer_cnt *priv)
  577. {
  578. struct device *parent = dev->parent;
  579. struct device_node *tnode = NULL, *pnode = parent->of_node;
  580. int i, ret;
  581. u32 idx;
  582. /*
  583. * Need to retrieve the trigger node index from DT, to be able
  584. * to determine if the counter supports encoder mode. It also
  585. * enforce backward compatibility, and allow to support other
  586. * counter modes in this driver (when the timer doesn't support
  587. * encoder).
  588. */
  589. for (i = 0; i < ARRAY_SIZE(stm32_timer_trigger_compat) && !tnode; i++)
  590. tnode = of_get_compatible_child(pnode, stm32_timer_trigger_compat[i]);
  591. if (!tnode) {
  592. dev_err(dev, "Can't find trigger node\n");
  593. return -ENODATA;
  594. }
  595. ret = of_property_read_u32(tnode, "reg", &idx);
  596. of_node_put(tnode);
  597. if (ret) {
  598. dev_err(dev, "Can't get index (%d)\n", ret);
  599. return ret;
  600. }
  601. priv->has_encoder = !!(STM32_TIM_ENCODER_SUPPORTED & BIT(idx));
  602. dev_dbg(dev, "encoder support: %s\n", priv->has_encoder ? "yes" : "no");
  603. return 0;
  604. }
  605. static int stm32_timer_cnt_probe(struct platform_device *pdev)
  606. {
  607. struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
  608. struct device *dev = &pdev->dev;
  609. struct stm32_timer_cnt *priv;
  610. struct counter_device *counter;
  611. int i, ret;
  612. if (IS_ERR_OR_NULL(ddata))
  613. return -EINVAL;
  614. counter = devm_counter_alloc(dev, sizeof(*priv));
  615. if (!counter)
  616. return -ENOMEM;
  617. priv = counter_priv(counter);
  618. priv->regmap = ddata->regmap;
  619. priv->clk = ddata->clk;
  620. priv->max_arr = ddata->max_arr;
  621. priv->nr_irqs = ddata->nr_irqs;
  622. ret = stm32_timer_cnt_probe_encoder(dev, priv);
  623. if (ret)
  624. return ret;
  625. stm32_timer_cnt_detect_channels(dev, priv);
  626. counter->name = dev_name(dev);
  627. counter->parent = dev;
  628. counter->ops = &stm32_timer_cnt_ops;
  629. counter->counts = &stm32_counts;
  630. counter->num_counts = 1;
  631. counter->signals = stm32_signals;
  632. counter->num_signals = ARRAY_SIZE(stm32_signals);
  633. spin_lock_init(&priv->lock);
  634. platform_set_drvdata(pdev, priv);
  635. /* STM32 Timers can have either 1 global, or 4 dedicated interrupts (optional) */
  636. if (priv->nr_irqs == 1) {
  637. /* All events reported through the global interrupt */
  638. ret = devm_request_irq(&pdev->dev, ddata->irq[0], stm32_timer_cnt_isr,
  639. 0, dev_name(dev), counter);
  640. if (ret) {
  641. dev_err(dev, "Failed to request irq %d (err %d)\n",
  642. ddata->irq[0], ret);
  643. return ret;
  644. }
  645. } else {
  646. for (i = 0; i < priv->nr_irqs; i++) {
  647. /*
  648. * Only take care of update IRQ for overflow events, and cc for
  649. * capture events.
  650. */
  651. if (i != STM32_TIMERS_IRQ_UP && i != STM32_TIMERS_IRQ_CC)
  652. continue;
  653. ret = devm_request_irq(&pdev->dev, ddata->irq[i], stm32_timer_cnt_isr,
  654. 0, dev_name(dev), counter);
  655. if (ret) {
  656. dev_err(dev, "Failed to request irq %d (err %d)\n",
  657. ddata->irq[i], ret);
  658. return ret;
  659. }
  660. }
  661. }
  662. /* Reset input selector to its default input */
  663. regmap_write(priv->regmap, TIM_TISEL, 0x0);
  664. /* Register Counter device */
  665. ret = devm_counter_add(dev, counter);
  666. if (ret < 0)
  667. dev_err_probe(dev, ret, "Failed to add counter\n");
  668. return ret;
  669. }
  670. static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
  671. {
  672. struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
  673. /* Only take care of enabled counter: don't disturb other MFD child */
  674. if (priv->enabled) {
  675. /* Backup registers that may get lost in low power mode */
  676. regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr);
  677. regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr);
  678. regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt);
  679. regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1);
  680. /* Disable the counter */
  681. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  682. clk_disable(priv->clk);
  683. }
  684. return pinctrl_pm_select_sleep_state(dev);
  685. }
  686. static int __maybe_unused stm32_timer_cnt_resume(struct device *dev)
  687. {
  688. struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
  689. int ret;
  690. ret = pinctrl_pm_select_default_state(dev);
  691. if (ret)
  692. return ret;
  693. if (priv->enabled) {
  694. ret = clk_enable(priv->clk);
  695. if (ret) {
  696. dev_err(dev, "Cannot enable clock %d\n", ret);
  697. return ret;
  698. }
  699. /* Restore registers that may have been lost */
  700. regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr);
  701. regmap_write(priv->regmap, TIM_ARR, priv->bak.arr);
  702. regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt);
  703. /* Also re-enables the counter */
  704. regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1);
  705. }
  706. return 0;
  707. }
  708. static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend,
  709. stm32_timer_cnt_resume);
  710. static const struct of_device_id stm32_timer_cnt_of_match[] = {
  711. { .compatible = "st,stm32-timer-counter", },
  712. {},
  713. };
  714. MODULE_DEVICE_TABLE(of, stm32_timer_cnt_of_match);
  715. static struct platform_driver stm32_timer_cnt_driver = {
  716. .probe = stm32_timer_cnt_probe,
  717. .driver = {
  718. .name = "stm32-timer-counter",
  719. .of_match_table = stm32_timer_cnt_of_match,
  720. .pm = &stm32_timer_cnt_pm_ops,
  721. },
  722. };
  723. module_platform_driver(stm32_timer_cnt_driver);
  724. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  725. MODULE_ALIAS("platform:stm32-timer-counter");
  726. MODULE_DESCRIPTION("STMicroelectronics STM32 TIMER counter driver");
  727. MODULE_LICENSE("GPL v2");
  728. MODULE_IMPORT_NS(COUNTER);