stm32-lptimer-cnt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * STM32 Low-Power Timer Encoder and Counter driver
  4. *
  5. * Copyright (C) STMicroelectronics 2017
  6. *
  7. * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
  8. *
  9. * Inspired by 104-quad-8 and stm32-timer-trigger drivers.
  10. *
  11. */
  12. #include <linux/bitfield.h>
  13. #include <linux/counter.h>
  14. #include <linux/mfd/stm32-lptimer.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/types.h>
  20. struct stm32_lptim_cnt {
  21. struct device *dev;
  22. struct regmap *regmap;
  23. struct clk *clk;
  24. u32 ceiling;
  25. u32 polarity;
  26. u32 quadrature_mode;
  27. bool enabled;
  28. };
  29. static int stm32_lptim_is_enabled(struct stm32_lptim_cnt *priv)
  30. {
  31. u32 val;
  32. int ret;
  33. ret = regmap_read(priv->regmap, STM32_LPTIM_CR, &val);
  34. if (ret)
  35. return ret;
  36. return FIELD_GET(STM32_LPTIM_ENABLE, val);
  37. }
  38. static int stm32_lptim_set_enable_state(struct stm32_lptim_cnt *priv,
  39. int enable)
  40. {
  41. int ret;
  42. u32 val;
  43. val = FIELD_PREP(STM32_LPTIM_ENABLE, enable);
  44. ret = regmap_write(priv->regmap, STM32_LPTIM_CR, val);
  45. if (ret)
  46. return ret;
  47. if (!enable) {
  48. clk_disable(priv->clk);
  49. priv->enabled = false;
  50. return 0;
  51. }
  52. ret = clk_enable(priv->clk);
  53. if (ret)
  54. goto disable_cnt;
  55. /* LP timer must be enabled before writing CMP & ARR */
  56. ret = regmap_write(priv->regmap, STM32_LPTIM_ARR, priv->ceiling);
  57. if (ret)
  58. goto disable_clk;
  59. ret = regmap_write(priv->regmap, STM32_LPTIM_CMP, 0);
  60. if (ret)
  61. goto disable_clk;
  62. /* ensure CMP & ARR registers are properly written */
  63. ret = regmap_read_poll_timeout(priv->regmap, STM32_LPTIM_ISR, val,
  64. (val & STM32_LPTIM_CMPOK_ARROK) == STM32_LPTIM_CMPOK_ARROK,
  65. 100, 1000);
  66. if (ret)
  67. goto disable_clk;
  68. ret = regmap_write(priv->regmap, STM32_LPTIM_ICR,
  69. STM32_LPTIM_CMPOKCF_ARROKCF);
  70. if (ret)
  71. goto disable_clk;
  72. priv->enabled = true;
  73. /* Start LP timer in continuous mode */
  74. return regmap_update_bits(priv->regmap, STM32_LPTIM_CR,
  75. STM32_LPTIM_CNTSTRT, STM32_LPTIM_CNTSTRT);
  76. disable_clk:
  77. clk_disable(priv->clk);
  78. disable_cnt:
  79. regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
  80. return ret;
  81. }
  82. static int stm32_lptim_setup(struct stm32_lptim_cnt *priv, int enable)
  83. {
  84. u32 mask = STM32_LPTIM_ENC | STM32_LPTIM_COUNTMODE |
  85. STM32_LPTIM_CKPOL | STM32_LPTIM_PRESC;
  86. u32 val;
  87. /* Setup LP timer encoder/counter and polarity, without prescaler */
  88. if (priv->quadrature_mode)
  89. val = enable ? STM32_LPTIM_ENC : 0;
  90. else
  91. val = enable ? STM32_LPTIM_COUNTMODE : 0;
  92. val |= FIELD_PREP(STM32_LPTIM_CKPOL, enable ? priv->polarity : 0);
  93. return regmap_update_bits(priv->regmap, STM32_LPTIM_CFGR, mask, val);
  94. }
  95. /*
  96. * In non-quadrature mode, device counts up on active edge.
  97. * In quadrature mode, encoder counting scenarios are as follows:
  98. * +---------+----------+--------------------+--------------------+
  99. * | Active | Level on | IN1 signal | IN2 signal |
  100. * | edge | opposite +----------+---------+----------+---------+
  101. * | | signal | Rising | Falling | Rising | Falling |
  102. * +---------+----------+----------+---------+----------+---------+
  103. * | Rising | High -> | Down | - | Up | - |
  104. * | edge | Low -> | Up | - | Down | - |
  105. * +---------+----------+----------+---------+----------+---------+
  106. * | Falling | High -> | - | Up | - | Down |
  107. * | edge | Low -> | - | Down | - | Up |
  108. * +---------+----------+----------+---------+----------+---------+
  109. * | Both | High -> | Down | Up | Up | Down |
  110. * | edges | Low -> | Up | Down | Down | Up |
  111. * +---------+----------+----------+---------+----------+---------+
  112. */
  113. static const enum counter_function stm32_lptim_cnt_functions[] = {
  114. COUNTER_FUNCTION_INCREASE,
  115. COUNTER_FUNCTION_QUADRATURE_X4,
  116. };
  117. static const enum counter_synapse_action stm32_lptim_cnt_synapse_actions[] = {
  118. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  119. COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
  120. COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
  121. COUNTER_SYNAPSE_ACTION_NONE,
  122. };
  123. static int stm32_lptim_cnt_read(struct counter_device *counter,
  124. struct counter_count *count, u64 *val)
  125. {
  126. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  127. u32 cnt;
  128. int ret;
  129. ret = regmap_read(priv->regmap, STM32_LPTIM_CNT, &cnt);
  130. if (ret)
  131. return ret;
  132. *val = cnt;
  133. return 0;
  134. }
  135. static int stm32_lptim_cnt_function_read(struct counter_device *counter,
  136. struct counter_count *count,
  137. enum counter_function *function)
  138. {
  139. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  140. if (!priv->quadrature_mode) {
  141. *function = COUNTER_FUNCTION_INCREASE;
  142. return 0;
  143. }
  144. if (priv->polarity == STM32_LPTIM_CKPOL_BOTH_EDGES) {
  145. *function = COUNTER_FUNCTION_QUADRATURE_X4;
  146. return 0;
  147. }
  148. return -EINVAL;
  149. }
  150. static int stm32_lptim_cnt_function_write(struct counter_device *counter,
  151. struct counter_count *count,
  152. enum counter_function function)
  153. {
  154. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  155. if (stm32_lptim_is_enabled(priv))
  156. return -EBUSY;
  157. switch (function) {
  158. case COUNTER_FUNCTION_INCREASE:
  159. priv->quadrature_mode = 0;
  160. return 0;
  161. case COUNTER_FUNCTION_QUADRATURE_X4:
  162. priv->quadrature_mode = 1;
  163. priv->polarity = STM32_LPTIM_CKPOL_BOTH_EDGES;
  164. return 0;
  165. default:
  166. /* should never reach this path */
  167. return -EINVAL;
  168. }
  169. }
  170. static int stm32_lptim_cnt_enable_read(struct counter_device *counter,
  171. struct counter_count *count,
  172. u8 *enable)
  173. {
  174. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  175. int ret;
  176. ret = stm32_lptim_is_enabled(priv);
  177. if (ret < 0)
  178. return ret;
  179. *enable = ret;
  180. return 0;
  181. }
  182. static int stm32_lptim_cnt_enable_write(struct counter_device *counter,
  183. struct counter_count *count,
  184. u8 enable)
  185. {
  186. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  187. int ret;
  188. /* Check nobody uses the timer, or already disabled/enabled */
  189. ret = stm32_lptim_is_enabled(priv);
  190. if ((ret < 0) || (!ret && !enable))
  191. return ret;
  192. if (enable && ret)
  193. return -EBUSY;
  194. ret = stm32_lptim_setup(priv, enable);
  195. if (ret)
  196. return ret;
  197. ret = stm32_lptim_set_enable_state(priv, enable);
  198. if (ret)
  199. return ret;
  200. return 0;
  201. }
  202. static int stm32_lptim_cnt_ceiling_read(struct counter_device *counter,
  203. struct counter_count *count,
  204. u64 *ceiling)
  205. {
  206. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  207. *ceiling = priv->ceiling;
  208. return 0;
  209. }
  210. static int stm32_lptim_cnt_ceiling_write(struct counter_device *counter,
  211. struct counter_count *count,
  212. u64 ceiling)
  213. {
  214. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  215. if (stm32_lptim_is_enabled(priv))
  216. return -EBUSY;
  217. if (ceiling > STM32_LPTIM_MAX_ARR)
  218. return -ERANGE;
  219. priv->ceiling = ceiling;
  220. return 0;
  221. }
  222. static struct counter_comp stm32_lptim_cnt_ext[] = {
  223. COUNTER_COMP_ENABLE(stm32_lptim_cnt_enable_read,
  224. stm32_lptim_cnt_enable_write),
  225. COUNTER_COMP_CEILING(stm32_lptim_cnt_ceiling_read,
  226. stm32_lptim_cnt_ceiling_write),
  227. };
  228. static int stm32_lptim_cnt_action_read(struct counter_device *counter,
  229. struct counter_count *count,
  230. struct counter_synapse *synapse,
  231. enum counter_synapse_action *action)
  232. {
  233. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  234. enum counter_function function;
  235. int err;
  236. err = stm32_lptim_cnt_function_read(counter, count, &function);
  237. if (err)
  238. return err;
  239. switch (function) {
  240. case COUNTER_FUNCTION_INCREASE:
  241. /* LP Timer acts as up-counter on input 1 */
  242. if (synapse->signal->id != count->synapses[0].signal->id) {
  243. *action = COUNTER_SYNAPSE_ACTION_NONE;
  244. return 0;
  245. }
  246. switch (priv->polarity) {
  247. case STM32_LPTIM_CKPOL_RISING_EDGE:
  248. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  249. return 0;
  250. case STM32_LPTIM_CKPOL_FALLING_EDGE:
  251. *action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
  252. return 0;
  253. case STM32_LPTIM_CKPOL_BOTH_EDGES:
  254. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  255. return 0;
  256. default:
  257. /* should never reach this path */
  258. return -EINVAL;
  259. }
  260. case COUNTER_FUNCTION_QUADRATURE_X4:
  261. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  262. return 0;
  263. default:
  264. /* should never reach this path */
  265. return -EINVAL;
  266. }
  267. }
  268. static int stm32_lptim_cnt_action_write(struct counter_device *counter,
  269. struct counter_count *count,
  270. struct counter_synapse *synapse,
  271. enum counter_synapse_action action)
  272. {
  273. struct stm32_lptim_cnt *const priv = counter_priv(counter);
  274. enum counter_function function;
  275. int err;
  276. if (stm32_lptim_is_enabled(priv))
  277. return -EBUSY;
  278. err = stm32_lptim_cnt_function_read(counter, count, &function);
  279. if (err)
  280. return err;
  281. /* only set polarity when in counter mode (on input 1) */
  282. if (function != COUNTER_FUNCTION_INCREASE
  283. || synapse->signal->id != count->synapses[0].signal->id)
  284. return -EINVAL;
  285. switch (action) {
  286. case COUNTER_SYNAPSE_ACTION_RISING_EDGE:
  287. priv->polarity = STM32_LPTIM_CKPOL_RISING_EDGE;
  288. return 0;
  289. case COUNTER_SYNAPSE_ACTION_FALLING_EDGE:
  290. priv->polarity = STM32_LPTIM_CKPOL_FALLING_EDGE;
  291. return 0;
  292. case COUNTER_SYNAPSE_ACTION_BOTH_EDGES:
  293. priv->polarity = STM32_LPTIM_CKPOL_BOTH_EDGES;
  294. return 0;
  295. default:
  296. return -EINVAL;
  297. }
  298. }
  299. static const struct counter_ops stm32_lptim_cnt_ops = {
  300. .count_read = stm32_lptim_cnt_read,
  301. .function_read = stm32_lptim_cnt_function_read,
  302. .function_write = stm32_lptim_cnt_function_write,
  303. .action_read = stm32_lptim_cnt_action_read,
  304. .action_write = stm32_lptim_cnt_action_write,
  305. };
  306. static struct counter_signal stm32_lptim_cnt_signals[] = {
  307. {
  308. .id = 0,
  309. .name = "Channel 1 Quadrature A"
  310. },
  311. {
  312. .id = 1,
  313. .name = "Channel 1 Quadrature B"
  314. }
  315. };
  316. static struct counter_synapse stm32_lptim_cnt_synapses[] = {
  317. {
  318. .actions_list = stm32_lptim_cnt_synapse_actions,
  319. .num_actions = ARRAY_SIZE(stm32_lptim_cnt_synapse_actions),
  320. .signal = &stm32_lptim_cnt_signals[0]
  321. },
  322. {
  323. .actions_list = stm32_lptim_cnt_synapse_actions,
  324. .num_actions = ARRAY_SIZE(stm32_lptim_cnt_synapse_actions),
  325. .signal = &stm32_lptim_cnt_signals[1]
  326. }
  327. };
  328. /* LP timer with encoder */
  329. static struct counter_count stm32_lptim_enc_counts = {
  330. .id = 0,
  331. .name = "LPTimer Count",
  332. .functions_list = stm32_lptim_cnt_functions,
  333. .num_functions = ARRAY_SIZE(stm32_lptim_cnt_functions),
  334. .synapses = stm32_lptim_cnt_synapses,
  335. .num_synapses = ARRAY_SIZE(stm32_lptim_cnt_synapses),
  336. .ext = stm32_lptim_cnt_ext,
  337. .num_ext = ARRAY_SIZE(stm32_lptim_cnt_ext)
  338. };
  339. /* LP timer without encoder (counter only) */
  340. static struct counter_count stm32_lptim_in1_counts = {
  341. .id = 0,
  342. .name = "LPTimer Count",
  343. .functions_list = stm32_lptim_cnt_functions,
  344. .num_functions = 1,
  345. .synapses = stm32_lptim_cnt_synapses,
  346. .num_synapses = 1,
  347. .ext = stm32_lptim_cnt_ext,
  348. .num_ext = ARRAY_SIZE(stm32_lptim_cnt_ext)
  349. };
  350. static int stm32_lptim_cnt_probe(struct platform_device *pdev)
  351. {
  352. struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent);
  353. struct counter_device *counter;
  354. struct stm32_lptim_cnt *priv;
  355. int ret;
  356. if (IS_ERR_OR_NULL(ddata))
  357. return -EINVAL;
  358. counter = devm_counter_alloc(&pdev->dev, sizeof(*priv));
  359. if (!counter)
  360. return -ENOMEM;
  361. priv = counter_priv(counter);
  362. priv->dev = &pdev->dev;
  363. priv->regmap = ddata->regmap;
  364. priv->clk = ddata->clk;
  365. priv->ceiling = STM32_LPTIM_MAX_ARR;
  366. /* Initialize Counter device */
  367. counter->name = dev_name(&pdev->dev);
  368. counter->parent = &pdev->dev;
  369. counter->ops = &stm32_lptim_cnt_ops;
  370. if (ddata->has_encoder) {
  371. counter->counts = &stm32_lptim_enc_counts;
  372. counter->num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals);
  373. } else {
  374. counter->counts = &stm32_lptim_in1_counts;
  375. counter->num_signals = 1;
  376. }
  377. counter->num_counts = 1;
  378. counter->signals = stm32_lptim_cnt_signals;
  379. platform_set_drvdata(pdev, priv);
  380. ret = devm_counter_add(&pdev->dev, counter);
  381. if (ret < 0)
  382. return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
  383. return 0;
  384. }
  385. #ifdef CONFIG_PM_SLEEP
  386. static int stm32_lptim_cnt_suspend(struct device *dev)
  387. {
  388. struct stm32_lptim_cnt *priv = dev_get_drvdata(dev);
  389. int ret;
  390. /* Only take care of enabled counter: don't disturb other MFD child */
  391. if (priv->enabled) {
  392. ret = stm32_lptim_setup(priv, 0);
  393. if (ret)
  394. return ret;
  395. ret = stm32_lptim_set_enable_state(priv, 0);
  396. if (ret)
  397. return ret;
  398. /* Force enable state for later resume */
  399. priv->enabled = true;
  400. }
  401. return pinctrl_pm_select_sleep_state(dev);
  402. }
  403. static int stm32_lptim_cnt_resume(struct device *dev)
  404. {
  405. struct stm32_lptim_cnt *priv = dev_get_drvdata(dev);
  406. int ret;
  407. ret = pinctrl_pm_select_default_state(dev);
  408. if (ret)
  409. return ret;
  410. if (priv->enabled) {
  411. priv->enabled = false;
  412. ret = stm32_lptim_setup(priv, 1);
  413. if (ret)
  414. return ret;
  415. ret = stm32_lptim_set_enable_state(priv, 1);
  416. if (ret)
  417. return ret;
  418. }
  419. return 0;
  420. }
  421. #endif
  422. static SIMPLE_DEV_PM_OPS(stm32_lptim_cnt_pm_ops, stm32_lptim_cnt_suspend,
  423. stm32_lptim_cnt_resume);
  424. static const struct of_device_id stm32_lptim_cnt_of_match[] = {
  425. { .compatible = "st,stm32-lptimer-counter", },
  426. {},
  427. };
  428. MODULE_DEVICE_TABLE(of, stm32_lptim_cnt_of_match);
  429. static struct platform_driver stm32_lptim_cnt_driver = {
  430. .probe = stm32_lptim_cnt_probe,
  431. .driver = {
  432. .name = "stm32-lptimer-counter",
  433. .of_match_table = stm32_lptim_cnt_of_match,
  434. .pm = &stm32_lptim_cnt_pm_ops,
  435. },
  436. };
  437. module_platform_driver(stm32_lptim_cnt_driver);
  438. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  439. MODULE_ALIAS("platform:stm32-lptimer-counter");
  440. MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM counter driver");
  441. MODULE_LICENSE("GPL v2");
  442. MODULE_IMPORT_NS("COUNTER");