stm32-lptimer-cnt.c 13 KB

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