stm32-lptimer-cnt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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/iio/iio.h>
  15. #include <linux/mfd/stm32-lptimer.h>
  16. #include <linux/module.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include <linux/platform_device.h>
  19. struct stm32_lptim_cnt {
  20. struct counter_device counter;
  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),
  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. static int stm32_lptim_write_raw(struct iio_dev *indio_dev,
  93. struct iio_chan_spec const *chan,
  94. int val, int val2, long mask)
  95. {
  96. struct stm32_lptim_cnt *priv = iio_priv(indio_dev);
  97. int ret;
  98. switch (mask) {
  99. case IIO_CHAN_INFO_ENABLE:
  100. if (val < 0 || val > 1)
  101. return -EINVAL;
  102. /* Check nobody uses the timer, or already disabled/enabled */
  103. ret = stm32_lptim_is_enabled(priv);
  104. if ((ret < 0) || (!ret && !val))
  105. return ret;
  106. if (val && ret)
  107. return -EBUSY;
  108. ret = stm32_lptim_setup(priv, val);
  109. if (ret)
  110. return ret;
  111. return stm32_lptim_set_enable_state(priv, val);
  112. default:
  113. return -EINVAL;
  114. }
  115. }
  116. static int stm32_lptim_read_raw(struct iio_dev *indio_dev,
  117. struct iio_chan_spec const *chan,
  118. int *val, int *val2, long mask)
  119. {
  120. struct stm32_lptim_cnt *priv = iio_priv(indio_dev);
  121. u32 dat;
  122. int ret;
  123. switch (mask) {
  124. case IIO_CHAN_INFO_RAW:
  125. ret = regmap_read(priv->regmap, STM32_LPTIM_CNT, &dat);
  126. if (ret)
  127. return ret;
  128. *val = dat;
  129. return IIO_VAL_INT;
  130. case IIO_CHAN_INFO_ENABLE:
  131. ret = stm32_lptim_is_enabled(priv);
  132. if (ret < 0)
  133. return ret;
  134. *val = ret;
  135. return IIO_VAL_INT;
  136. case IIO_CHAN_INFO_SCALE:
  137. /* Non-quadrature mode: scale = 1 */
  138. *val = 1;
  139. *val2 = 0;
  140. if (priv->quadrature_mode) {
  141. /*
  142. * Quadrature encoder mode:
  143. * - both edges, quarter cycle, scale is 0.25
  144. * - either rising/falling edge scale is 0.5
  145. */
  146. if (priv->polarity > 1)
  147. *val2 = 2;
  148. else
  149. *val2 = 1;
  150. }
  151. return IIO_VAL_FRACTIONAL_LOG2;
  152. default:
  153. return -EINVAL;
  154. }
  155. }
  156. static const struct iio_info stm32_lptim_cnt_iio_info = {
  157. .read_raw = stm32_lptim_read_raw,
  158. .write_raw = stm32_lptim_write_raw,
  159. };
  160. static const char *const stm32_lptim_quadrature_modes[] = {
  161. "non-quadrature",
  162. "quadrature",
  163. };
  164. static int stm32_lptim_get_quadrature_mode(struct iio_dev *indio_dev,
  165. const struct iio_chan_spec *chan)
  166. {
  167. struct stm32_lptim_cnt *priv = iio_priv(indio_dev);
  168. return priv->quadrature_mode;
  169. }
  170. static int stm32_lptim_set_quadrature_mode(struct iio_dev *indio_dev,
  171. const struct iio_chan_spec *chan,
  172. unsigned int type)
  173. {
  174. struct stm32_lptim_cnt *priv = iio_priv(indio_dev);
  175. if (stm32_lptim_is_enabled(priv))
  176. return -EBUSY;
  177. priv->quadrature_mode = type;
  178. return 0;
  179. }
  180. static const struct iio_enum stm32_lptim_quadrature_mode_en = {
  181. .items = stm32_lptim_quadrature_modes,
  182. .num_items = ARRAY_SIZE(stm32_lptim_quadrature_modes),
  183. .get = stm32_lptim_get_quadrature_mode,
  184. .set = stm32_lptim_set_quadrature_mode,
  185. };
  186. static const char * const stm32_lptim_cnt_polarity[] = {
  187. "rising-edge", "falling-edge", "both-edges",
  188. };
  189. static int stm32_lptim_cnt_get_polarity(struct iio_dev *indio_dev,
  190. const struct iio_chan_spec *chan)
  191. {
  192. struct stm32_lptim_cnt *priv = iio_priv(indio_dev);
  193. return priv->polarity;
  194. }
  195. static int stm32_lptim_cnt_set_polarity(struct iio_dev *indio_dev,
  196. const struct iio_chan_spec *chan,
  197. unsigned int type)
  198. {
  199. struct stm32_lptim_cnt *priv = iio_priv(indio_dev);
  200. if (stm32_lptim_is_enabled(priv))
  201. return -EBUSY;
  202. priv->polarity = type;
  203. return 0;
  204. }
  205. static const struct iio_enum stm32_lptim_cnt_polarity_en = {
  206. .items = stm32_lptim_cnt_polarity,
  207. .num_items = ARRAY_SIZE(stm32_lptim_cnt_polarity),
  208. .get = stm32_lptim_cnt_get_polarity,
  209. .set = stm32_lptim_cnt_set_polarity,
  210. };
  211. static ssize_t stm32_lptim_cnt_get_ceiling(struct stm32_lptim_cnt *priv,
  212. char *buf)
  213. {
  214. return snprintf(buf, PAGE_SIZE, "%u\n", priv->ceiling);
  215. }
  216. static ssize_t stm32_lptim_cnt_set_ceiling(struct stm32_lptim_cnt *priv,
  217. const char *buf, size_t len)
  218. {
  219. int ret;
  220. if (stm32_lptim_is_enabled(priv))
  221. return -EBUSY;
  222. ret = kstrtouint(buf, 0, &priv->ceiling);
  223. if (ret)
  224. return ret;
  225. if (priv->ceiling > STM32_LPTIM_MAX_ARR)
  226. return -EINVAL;
  227. return len;
  228. }
  229. static ssize_t stm32_lptim_cnt_get_preset_iio(struct iio_dev *indio_dev,
  230. uintptr_t private,
  231. const struct iio_chan_spec *chan,
  232. char *buf)
  233. {
  234. struct stm32_lptim_cnt *priv = iio_priv(indio_dev);
  235. return stm32_lptim_cnt_get_ceiling(priv, buf);
  236. }
  237. static ssize_t stm32_lptim_cnt_set_preset_iio(struct iio_dev *indio_dev,
  238. uintptr_t private,
  239. const struct iio_chan_spec *chan,
  240. const char *buf, size_t len)
  241. {
  242. struct stm32_lptim_cnt *priv = iio_priv(indio_dev);
  243. return stm32_lptim_cnt_set_ceiling(priv, buf, len);
  244. }
  245. /* LP timer with encoder */
  246. static const struct iio_chan_spec_ext_info stm32_lptim_enc_ext_info[] = {
  247. {
  248. .name = "preset",
  249. .shared = IIO_SEPARATE,
  250. .read = stm32_lptim_cnt_get_preset_iio,
  251. .write = stm32_lptim_cnt_set_preset_iio,
  252. },
  253. IIO_ENUM("polarity", IIO_SEPARATE, &stm32_lptim_cnt_polarity_en),
  254. IIO_ENUM_AVAILABLE("polarity", &stm32_lptim_cnt_polarity_en),
  255. IIO_ENUM("quadrature_mode", IIO_SEPARATE,
  256. &stm32_lptim_quadrature_mode_en),
  257. IIO_ENUM_AVAILABLE("quadrature_mode", &stm32_lptim_quadrature_mode_en),
  258. {}
  259. };
  260. static const struct iio_chan_spec stm32_lptim_enc_channels = {
  261. .type = IIO_COUNT,
  262. .channel = 0,
  263. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  264. BIT(IIO_CHAN_INFO_ENABLE) |
  265. BIT(IIO_CHAN_INFO_SCALE),
  266. .ext_info = stm32_lptim_enc_ext_info,
  267. .indexed = 1,
  268. };
  269. /* LP timer without encoder (counter only) */
  270. static const struct iio_chan_spec_ext_info stm32_lptim_cnt_ext_info[] = {
  271. {
  272. .name = "preset",
  273. .shared = IIO_SEPARATE,
  274. .read = stm32_lptim_cnt_get_preset_iio,
  275. .write = stm32_lptim_cnt_set_preset_iio,
  276. },
  277. IIO_ENUM("polarity", IIO_SEPARATE, &stm32_lptim_cnt_polarity_en),
  278. IIO_ENUM_AVAILABLE("polarity", &stm32_lptim_cnt_polarity_en),
  279. {}
  280. };
  281. static const struct iio_chan_spec stm32_lptim_cnt_channels = {
  282. .type = IIO_COUNT,
  283. .channel = 0,
  284. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  285. BIT(IIO_CHAN_INFO_ENABLE) |
  286. BIT(IIO_CHAN_INFO_SCALE),
  287. .ext_info = stm32_lptim_cnt_ext_info,
  288. .indexed = 1,
  289. };
  290. /**
  291. * stm32_lptim_cnt_function - enumerates stm32 LPTimer counter & encoder modes
  292. * @STM32_LPTIM_COUNTER_INCREASE: up count on IN1 rising, falling or both edges
  293. * @STM32_LPTIM_ENCODER_BOTH_EDGE: count on both edges (IN1 & IN2 quadrature)
  294. */
  295. enum stm32_lptim_cnt_function {
  296. STM32_LPTIM_COUNTER_INCREASE,
  297. STM32_LPTIM_ENCODER_BOTH_EDGE,
  298. };
  299. static enum counter_count_function stm32_lptim_cnt_functions[] = {
  300. [STM32_LPTIM_COUNTER_INCREASE] = COUNTER_COUNT_FUNCTION_INCREASE,
  301. [STM32_LPTIM_ENCODER_BOTH_EDGE] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4,
  302. };
  303. enum stm32_lptim_synapse_action {
  304. STM32_LPTIM_SYNAPSE_ACTION_RISING_EDGE,
  305. STM32_LPTIM_SYNAPSE_ACTION_FALLING_EDGE,
  306. STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES,
  307. STM32_LPTIM_SYNAPSE_ACTION_NONE,
  308. };
  309. static enum counter_synapse_action stm32_lptim_cnt_synapse_actions[] = {
  310. /* Index must match with stm32_lptim_cnt_polarity[] (priv->polarity) */
  311. [STM32_LPTIM_SYNAPSE_ACTION_RISING_EDGE] = COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  312. [STM32_LPTIM_SYNAPSE_ACTION_FALLING_EDGE] = COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
  313. [STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
  314. [STM32_LPTIM_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
  315. };
  316. static int stm32_lptim_cnt_read(struct counter_device *counter,
  317. struct counter_count *count,
  318. struct counter_count_read_value *val)
  319. {
  320. struct stm32_lptim_cnt *const priv = counter->priv;
  321. u32 cnt;
  322. int ret;
  323. ret = regmap_read(priv->regmap, STM32_LPTIM_CNT, &cnt);
  324. if (ret)
  325. return ret;
  326. counter_count_read_value_set(val, COUNTER_COUNT_POSITION, &cnt);
  327. return 0;
  328. }
  329. static int stm32_lptim_cnt_function_get(struct counter_device *counter,
  330. struct counter_count *count,
  331. size_t *function)
  332. {
  333. struct stm32_lptim_cnt *const priv = counter->priv;
  334. if (!priv->quadrature_mode) {
  335. *function = STM32_LPTIM_COUNTER_INCREASE;
  336. return 0;
  337. }
  338. if (priv->polarity == STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES) {
  339. *function = STM32_LPTIM_ENCODER_BOTH_EDGE;
  340. return 0;
  341. }
  342. return -EINVAL;
  343. }
  344. static int stm32_lptim_cnt_function_set(struct counter_device *counter,
  345. struct counter_count *count,
  346. size_t function)
  347. {
  348. struct stm32_lptim_cnt *const priv = counter->priv;
  349. if (stm32_lptim_is_enabled(priv))
  350. return -EBUSY;
  351. switch (function) {
  352. case STM32_LPTIM_COUNTER_INCREASE:
  353. priv->quadrature_mode = 0;
  354. return 0;
  355. case STM32_LPTIM_ENCODER_BOTH_EDGE:
  356. priv->quadrature_mode = 1;
  357. priv->polarity = STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES;
  358. return 0;
  359. }
  360. return -EINVAL;
  361. }
  362. static ssize_t stm32_lptim_cnt_enable_read(struct counter_device *counter,
  363. struct counter_count *count,
  364. void *private, char *buf)
  365. {
  366. struct stm32_lptim_cnt *const priv = counter->priv;
  367. int ret;
  368. ret = stm32_lptim_is_enabled(priv);
  369. if (ret < 0)
  370. return ret;
  371. return scnprintf(buf, PAGE_SIZE, "%u\n", ret);
  372. }
  373. static ssize_t stm32_lptim_cnt_enable_write(struct counter_device *counter,
  374. struct counter_count *count,
  375. void *private,
  376. const char *buf, size_t len)
  377. {
  378. struct stm32_lptim_cnt *const priv = counter->priv;
  379. bool enable;
  380. int ret;
  381. ret = kstrtobool(buf, &enable);
  382. if (ret)
  383. return ret;
  384. /* Check nobody uses the timer, or already disabled/enabled */
  385. ret = stm32_lptim_is_enabled(priv);
  386. if ((ret < 0) || (!ret && !enable))
  387. return ret;
  388. if (enable && ret)
  389. return -EBUSY;
  390. ret = stm32_lptim_setup(priv, enable);
  391. if (ret)
  392. return ret;
  393. ret = stm32_lptim_set_enable_state(priv, enable);
  394. if (ret)
  395. return ret;
  396. return len;
  397. }
  398. static ssize_t stm32_lptim_cnt_ceiling_read(struct counter_device *counter,
  399. struct counter_count *count,
  400. void *private, char *buf)
  401. {
  402. struct stm32_lptim_cnt *const priv = counter->priv;
  403. return stm32_lptim_cnt_get_ceiling(priv, buf);
  404. }
  405. static ssize_t stm32_lptim_cnt_ceiling_write(struct counter_device *counter,
  406. struct counter_count *count,
  407. void *private,
  408. const char *buf, size_t len)
  409. {
  410. struct stm32_lptim_cnt *const priv = counter->priv;
  411. return stm32_lptim_cnt_set_ceiling(priv, buf, len);
  412. }
  413. static const struct counter_count_ext stm32_lptim_cnt_ext[] = {
  414. {
  415. .name = "enable",
  416. .read = stm32_lptim_cnt_enable_read,
  417. .write = stm32_lptim_cnt_enable_write
  418. },
  419. {
  420. .name = "ceiling",
  421. .read = stm32_lptim_cnt_ceiling_read,
  422. .write = stm32_lptim_cnt_ceiling_write
  423. },
  424. };
  425. static int stm32_lptim_cnt_action_get(struct counter_device *counter,
  426. struct counter_count *count,
  427. struct counter_synapse *synapse,
  428. size_t *action)
  429. {
  430. struct stm32_lptim_cnt *const priv = counter->priv;
  431. size_t function;
  432. int err;
  433. err = stm32_lptim_cnt_function_get(counter, count, &function);
  434. if (err)
  435. return err;
  436. switch (function) {
  437. case STM32_LPTIM_COUNTER_INCREASE:
  438. /* LP Timer acts as up-counter on input 1 */
  439. if (synapse->signal->id == count->synapses[0].signal->id)
  440. *action = priv->polarity;
  441. else
  442. *action = STM32_LPTIM_SYNAPSE_ACTION_NONE;
  443. return 0;
  444. case STM32_LPTIM_ENCODER_BOTH_EDGE:
  445. *action = priv->polarity;
  446. return 0;
  447. }
  448. return -EINVAL;
  449. }
  450. static int stm32_lptim_cnt_action_set(struct counter_device *counter,
  451. struct counter_count *count,
  452. struct counter_synapse *synapse,
  453. size_t action)
  454. {
  455. struct stm32_lptim_cnt *const priv = counter->priv;
  456. size_t function;
  457. int err;
  458. if (stm32_lptim_is_enabled(priv))
  459. return -EBUSY;
  460. err = stm32_lptim_cnt_function_get(counter, count, &function);
  461. if (err)
  462. return err;
  463. /* only set polarity when in counter mode (on input 1) */
  464. if (function == STM32_LPTIM_COUNTER_INCREASE
  465. && synapse->signal->id == count->synapses[0].signal->id) {
  466. switch (action) {
  467. case STM32_LPTIM_SYNAPSE_ACTION_RISING_EDGE:
  468. case STM32_LPTIM_SYNAPSE_ACTION_FALLING_EDGE:
  469. case STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES:
  470. priv->polarity = action;
  471. return 0;
  472. }
  473. }
  474. return -EINVAL;
  475. }
  476. static const struct counter_ops stm32_lptim_cnt_ops = {
  477. .count_read = stm32_lptim_cnt_read,
  478. .function_get = stm32_lptim_cnt_function_get,
  479. .function_set = stm32_lptim_cnt_function_set,
  480. .action_get = stm32_lptim_cnt_action_get,
  481. .action_set = stm32_lptim_cnt_action_set,
  482. };
  483. static struct counter_signal stm32_lptim_cnt_signals[] = {
  484. {
  485. .id = 0,
  486. .name = "Channel 1 Quadrature A"
  487. },
  488. {
  489. .id = 1,
  490. .name = "Channel 1 Quadrature B"
  491. }
  492. };
  493. static struct counter_synapse stm32_lptim_cnt_synapses[] = {
  494. {
  495. .actions_list = stm32_lptim_cnt_synapse_actions,
  496. .num_actions = ARRAY_SIZE(stm32_lptim_cnt_synapse_actions),
  497. .signal = &stm32_lptim_cnt_signals[0]
  498. },
  499. {
  500. .actions_list = stm32_lptim_cnt_synapse_actions,
  501. .num_actions = ARRAY_SIZE(stm32_lptim_cnt_synapse_actions),
  502. .signal = &stm32_lptim_cnt_signals[1]
  503. }
  504. };
  505. /* LP timer with encoder */
  506. static struct counter_count stm32_lptim_enc_counts = {
  507. .id = 0,
  508. .name = "LPTimer Count",
  509. .functions_list = stm32_lptim_cnt_functions,
  510. .num_functions = ARRAY_SIZE(stm32_lptim_cnt_functions),
  511. .synapses = stm32_lptim_cnt_synapses,
  512. .num_synapses = ARRAY_SIZE(stm32_lptim_cnt_synapses),
  513. .ext = stm32_lptim_cnt_ext,
  514. .num_ext = ARRAY_SIZE(stm32_lptim_cnt_ext)
  515. };
  516. /* LP timer without encoder (counter only) */
  517. static struct counter_count stm32_lptim_in1_counts = {
  518. .id = 0,
  519. .name = "LPTimer Count",
  520. .functions_list = stm32_lptim_cnt_functions,
  521. .num_functions = 1,
  522. .synapses = stm32_lptim_cnt_synapses,
  523. .num_synapses = 1,
  524. .ext = stm32_lptim_cnt_ext,
  525. .num_ext = ARRAY_SIZE(stm32_lptim_cnt_ext)
  526. };
  527. static int stm32_lptim_cnt_probe(struct platform_device *pdev)
  528. {
  529. struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent);
  530. struct stm32_lptim_cnt *priv;
  531. struct iio_dev *indio_dev;
  532. int ret;
  533. if (IS_ERR_OR_NULL(ddata))
  534. return -EINVAL;
  535. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv));
  536. if (!indio_dev)
  537. return -ENOMEM;
  538. priv = iio_priv(indio_dev);
  539. priv->dev = &pdev->dev;
  540. priv->regmap = ddata->regmap;
  541. priv->clk = ddata->clk;
  542. priv->ceiling = STM32_LPTIM_MAX_ARR;
  543. /* Initialize IIO device */
  544. indio_dev->name = dev_name(&pdev->dev);
  545. indio_dev->dev.parent = &pdev->dev;
  546. indio_dev->dev.of_node = pdev->dev.of_node;
  547. indio_dev->info = &stm32_lptim_cnt_iio_info;
  548. if (ddata->has_encoder)
  549. indio_dev->channels = &stm32_lptim_enc_channels;
  550. else
  551. indio_dev->channels = &stm32_lptim_cnt_channels;
  552. indio_dev->num_channels = 1;
  553. /* Initialize Counter device */
  554. priv->counter.name = dev_name(&pdev->dev);
  555. priv->counter.parent = &pdev->dev;
  556. priv->counter.ops = &stm32_lptim_cnt_ops;
  557. if (ddata->has_encoder) {
  558. priv->counter.counts = &stm32_lptim_enc_counts;
  559. priv->counter.num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals);
  560. } else {
  561. priv->counter.counts = &stm32_lptim_in1_counts;
  562. priv->counter.num_signals = 1;
  563. }
  564. priv->counter.num_counts = 1;
  565. priv->counter.signals = stm32_lptim_cnt_signals;
  566. priv->counter.priv = priv;
  567. platform_set_drvdata(pdev, priv);
  568. ret = devm_iio_device_register(&pdev->dev, indio_dev);
  569. if (ret)
  570. return ret;
  571. return devm_counter_register(&pdev->dev, &priv->counter);
  572. }
  573. #ifdef CONFIG_PM_SLEEP
  574. static int stm32_lptim_cnt_suspend(struct device *dev)
  575. {
  576. struct stm32_lptim_cnt *priv = dev_get_drvdata(dev);
  577. int ret;
  578. /* Only take care of enabled counter: don't disturb other MFD child */
  579. if (priv->enabled) {
  580. ret = stm32_lptim_setup(priv, 0);
  581. if (ret)
  582. return ret;
  583. ret = stm32_lptim_set_enable_state(priv, 0);
  584. if (ret)
  585. return ret;
  586. /* Force enable state for later resume */
  587. priv->enabled = true;
  588. }
  589. return pinctrl_pm_select_sleep_state(dev);
  590. }
  591. static int stm32_lptim_cnt_resume(struct device *dev)
  592. {
  593. struct stm32_lptim_cnt *priv = dev_get_drvdata(dev);
  594. int ret;
  595. ret = pinctrl_pm_select_default_state(dev);
  596. if (ret)
  597. return ret;
  598. if (priv->enabled) {
  599. priv->enabled = false;
  600. ret = stm32_lptim_setup(priv, 1);
  601. if (ret)
  602. return ret;
  603. ret = stm32_lptim_set_enable_state(priv, 1);
  604. if (ret)
  605. return ret;
  606. }
  607. return 0;
  608. }
  609. #endif
  610. static SIMPLE_DEV_PM_OPS(stm32_lptim_cnt_pm_ops, stm32_lptim_cnt_suspend,
  611. stm32_lptim_cnt_resume);
  612. static const struct of_device_id stm32_lptim_cnt_of_match[] = {
  613. { .compatible = "st,stm32-lptimer-counter", },
  614. {},
  615. };
  616. MODULE_DEVICE_TABLE(of, stm32_lptim_cnt_of_match);
  617. static struct platform_driver stm32_lptim_cnt_driver = {
  618. .probe = stm32_lptim_cnt_probe,
  619. .driver = {
  620. .name = "stm32-lptimer-counter",
  621. .of_match_table = stm32_lptim_cnt_of_match,
  622. .pm = &stm32_lptim_cnt_pm_ops,
  623. },
  624. };
  625. module_platform_driver(stm32_lptim_cnt_driver);
  626. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  627. MODULE_ALIAS("platform:stm32-lptimer-counter");
  628. MODULE_DESCRIPTION("STMicroelectronics STM32 LPTIM counter driver");
  629. MODULE_LICENSE("GPL v2");