rz-mtu3-cnt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas RZ/G2L MTU3a Counter driver
  4. *
  5. * Copyright (C) 2022 Renesas Electronics Corporation
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/counter.h>
  9. #include <linux/mfd/rz-mtu3.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/types.h>
  14. /*
  15. * Register descriptions
  16. * TSR: Timer Status Register
  17. * TMDR1: Timer Mode Register 1
  18. * TMDR3: Timer Mode Register 3
  19. * TIOR: Timer I/O Control Register
  20. * TCR: Timer Control Register
  21. * TCNT: Timer Counter
  22. * TGRA: Timer general register A
  23. * TCNTLW: Timer Longword Counter
  24. * TGRALW: Timer longword general register A
  25. */
  26. #define RZ_MTU3_TSR_TCFD BIT(7) /* Count Direction Flag */
  27. #define RZ_MTU3_TMDR1_PH_CNT_MODE_1 (4) /* Phase counting mode 1 */
  28. #define RZ_MTU3_TMDR1_PH_CNT_MODE_2 (5) /* Phase counting mode 2 */
  29. #define RZ_MTU3_TMDR1_PH_CNT_MODE_3 (6) /* Phase counting mode 3 */
  30. #define RZ_MTU3_TMDR1_PH_CNT_MODE_4 (7) /* Phase counting mode 4 */
  31. #define RZ_MTU3_TMDR1_PH_CNT_MODE_5 (9) /* Phase counting mode 5 */
  32. #define RZ_MTU3_TMDR1_PH_CNT_MODE_MASK (0xf)
  33. /*
  34. * LWA: MTU1/MTU2 Combination Longword Access Control
  35. * 0: 16-bit, 1: 32-bit
  36. */
  37. #define RZ_MTU3_TMDR3_LWA (0)
  38. /*
  39. * PHCKSEL: External Input Phase Clock Select
  40. * 0: MTCLKA and MTCLKB, 1: MTCLKC and MTCLKD
  41. */
  42. #define RZ_MTU3_TMDR3_PHCKSEL (1)
  43. #define RZ_MTU3_16_BIT_MTU1_CH (0)
  44. #define RZ_MTU3_16_BIT_MTU2_CH (1)
  45. #define RZ_MTU3_32_BIT_CH (2)
  46. #define RZ_MTU3_TIOR_NO_OUTPUT (0) /* Output prohibited */
  47. #define RZ_MTU3_TIOR_IC_BOTH (10) /* Input capture at both edges */
  48. #define SIGNAL_A_ID (0)
  49. #define SIGNAL_B_ID (1)
  50. #define SIGNAL_C_ID (2)
  51. #define SIGNAL_D_ID (3)
  52. #define RZ_MTU3_MAX_HW_CNTR_CHANNELS (2)
  53. #define RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS (3)
  54. /**
  55. * struct rz_mtu3_cnt - MTU3 counter private data
  56. *
  57. * @clk: MTU3 module clock
  58. * @lock: Lock to prevent concurrent access for ceiling and count
  59. * @ch: HW channels for the counters
  60. * @count_is_enabled: Enabled state of Counter value channel
  61. * @mtu_16bit_max: Cache for 16-bit counters
  62. * @mtu_32bit_max: Cache for 32-bit counters
  63. */
  64. struct rz_mtu3_cnt {
  65. struct clk *clk;
  66. struct mutex lock;
  67. struct rz_mtu3_channel *ch;
  68. bool count_is_enabled[RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS];
  69. union {
  70. u16 mtu_16bit_max[RZ_MTU3_MAX_HW_CNTR_CHANNELS];
  71. u32 mtu_32bit_max;
  72. };
  73. };
  74. static const enum counter_function rz_mtu3_count_functions[] = {
  75. COUNTER_FUNCTION_QUADRATURE_X4,
  76. COUNTER_FUNCTION_PULSE_DIRECTION,
  77. COUNTER_FUNCTION_QUADRATURE_X2_B,
  78. };
  79. static inline size_t rz_mtu3_get_hw_ch(const size_t id)
  80. {
  81. return (id == RZ_MTU3_32_BIT_CH) ? 0 : id;
  82. }
  83. static inline struct rz_mtu3_channel *rz_mtu3_get_ch(struct counter_device *counter, int id)
  84. {
  85. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  86. const size_t ch_id = rz_mtu3_get_hw_ch(id);
  87. return &priv->ch[ch_id];
  88. }
  89. static bool rz_mtu3_is_counter_invalid(struct counter_device *counter, int id)
  90. {
  91. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  92. unsigned long tmdr;
  93. pm_runtime_get_sync(priv->ch->dev);
  94. tmdr = rz_mtu3_shared_reg_read(priv->ch, RZ_MTU3_TMDR3);
  95. pm_runtime_put(priv->ch->dev);
  96. if (id == RZ_MTU3_32_BIT_CH && test_bit(RZ_MTU3_TMDR3_LWA, &tmdr))
  97. return false;
  98. if (id != RZ_MTU3_32_BIT_CH && !test_bit(RZ_MTU3_TMDR3_LWA, &tmdr))
  99. return false;
  100. return true;
  101. }
  102. static int rz_mtu3_lock_if_counter_is_valid(struct counter_device *counter,
  103. struct rz_mtu3_channel *const ch,
  104. struct rz_mtu3_cnt *const priv,
  105. int id)
  106. {
  107. mutex_lock(&priv->lock);
  108. if (ch->is_busy && !priv->count_is_enabled[id]) {
  109. mutex_unlock(&priv->lock);
  110. return -EINVAL;
  111. }
  112. if (rz_mtu3_is_counter_invalid(counter, id)) {
  113. mutex_unlock(&priv->lock);
  114. return -EBUSY;
  115. }
  116. return 0;
  117. }
  118. static int rz_mtu3_lock_if_count_is_enabled(struct rz_mtu3_channel *const ch,
  119. struct rz_mtu3_cnt *const priv,
  120. int id)
  121. {
  122. mutex_lock(&priv->lock);
  123. if (ch->is_busy && !priv->count_is_enabled[id]) {
  124. mutex_unlock(&priv->lock);
  125. return -EINVAL;
  126. }
  127. return 0;
  128. }
  129. static int rz_mtu3_count_read(struct counter_device *counter,
  130. struct counter_count *count, u64 *val)
  131. {
  132. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  133. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  134. int ret;
  135. ret = rz_mtu3_lock_if_counter_is_valid(counter, ch, priv, count->id);
  136. if (ret)
  137. return ret;
  138. pm_runtime_get_sync(ch->dev);
  139. if (count->id == RZ_MTU3_32_BIT_CH)
  140. *val = rz_mtu3_32bit_ch_read(ch, RZ_MTU3_TCNTLW);
  141. else
  142. *val = rz_mtu3_16bit_ch_read(ch, RZ_MTU3_TCNT);
  143. pm_runtime_put(ch->dev);
  144. mutex_unlock(&priv->lock);
  145. return 0;
  146. }
  147. static int rz_mtu3_count_write(struct counter_device *counter,
  148. struct counter_count *count, const u64 val)
  149. {
  150. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  151. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  152. int ret;
  153. ret = rz_mtu3_lock_if_counter_is_valid(counter, ch, priv, count->id);
  154. if (ret)
  155. return ret;
  156. pm_runtime_get_sync(ch->dev);
  157. if (count->id == RZ_MTU3_32_BIT_CH)
  158. rz_mtu3_32bit_ch_write(ch, RZ_MTU3_TCNTLW, val);
  159. else
  160. rz_mtu3_16bit_ch_write(ch, RZ_MTU3_TCNT, val);
  161. pm_runtime_put(ch->dev);
  162. mutex_unlock(&priv->lock);
  163. return 0;
  164. }
  165. static int rz_mtu3_count_function_read_helper(struct rz_mtu3_channel *const ch,
  166. struct rz_mtu3_cnt *const priv,
  167. enum counter_function *function)
  168. {
  169. u8 timer_mode;
  170. pm_runtime_get_sync(ch->dev);
  171. timer_mode = rz_mtu3_8bit_ch_read(ch, RZ_MTU3_TMDR1);
  172. pm_runtime_put(ch->dev);
  173. switch (timer_mode & RZ_MTU3_TMDR1_PH_CNT_MODE_MASK) {
  174. case RZ_MTU3_TMDR1_PH_CNT_MODE_1:
  175. *function = COUNTER_FUNCTION_QUADRATURE_X4;
  176. return 0;
  177. case RZ_MTU3_TMDR1_PH_CNT_MODE_2:
  178. *function = COUNTER_FUNCTION_PULSE_DIRECTION;
  179. return 0;
  180. case RZ_MTU3_TMDR1_PH_CNT_MODE_4:
  181. *function = COUNTER_FUNCTION_QUADRATURE_X2_B;
  182. return 0;
  183. default:
  184. /*
  185. * TODO:
  186. * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_3
  187. * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_5
  188. */
  189. return -EINVAL;
  190. }
  191. }
  192. static int rz_mtu3_count_function_read(struct counter_device *counter,
  193. struct counter_count *count,
  194. enum counter_function *function)
  195. {
  196. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  197. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  198. int ret;
  199. ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
  200. if (ret)
  201. return ret;
  202. ret = rz_mtu3_count_function_read_helper(ch, priv, function);
  203. mutex_unlock(&priv->lock);
  204. return ret;
  205. }
  206. static int rz_mtu3_count_function_write(struct counter_device *counter,
  207. struct counter_count *count,
  208. enum counter_function function)
  209. {
  210. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  211. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  212. u8 timer_mode;
  213. int ret;
  214. ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
  215. if (ret)
  216. return ret;
  217. switch (function) {
  218. case COUNTER_FUNCTION_QUADRATURE_X4:
  219. timer_mode = RZ_MTU3_TMDR1_PH_CNT_MODE_1;
  220. break;
  221. case COUNTER_FUNCTION_PULSE_DIRECTION:
  222. timer_mode = RZ_MTU3_TMDR1_PH_CNT_MODE_2;
  223. break;
  224. case COUNTER_FUNCTION_QUADRATURE_X2_B:
  225. timer_mode = RZ_MTU3_TMDR1_PH_CNT_MODE_4;
  226. break;
  227. default:
  228. /*
  229. * TODO:
  230. * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_3
  231. * - need to add RZ_MTU3_TMDR1_PH_CNT_MODE_5
  232. */
  233. mutex_unlock(&priv->lock);
  234. return -EINVAL;
  235. }
  236. pm_runtime_get_sync(ch->dev);
  237. rz_mtu3_8bit_ch_write(ch, RZ_MTU3_TMDR1, timer_mode);
  238. pm_runtime_put(ch->dev);
  239. mutex_unlock(&priv->lock);
  240. return 0;
  241. }
  242. static int rz_mtu3_count_direction_read(struct counter_device *counter,
  243. struct counter_count *count,
  244. enum counter_count_direction *direction)
  245. {
  246. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  247. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  248. int ret;
  249. u8 tsr;
  250. ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
  251. if (ret)
  252. return ret;
  253. pm_runtime_get_sync(ch->dev);
  254. tsr = rz_mtu3_8bit_ch_read(ch, RZ_MTU3_TSR);
  255. pm_runtime_put(ch->dev);
  256. *direction = (tsr & RZ_MTU3_TSR_TCFD) ?
  257. COUNTER_COUNT_DIRECTION_FORWARD : COUNTER_COUNT_DIRECTION_BACKWARD;
  258. mutex_unlock(&priv->lock);
  259. return 0;
  260. }
  261. static int rz_mtu3_count_ceiling_read(struct counter_device *counter,
  262. struct counter_count *count,
  263. u64 *ceiling)
  264. {
  265. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  266. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  267. const size_t ch_id = rz_mtu3_get_hw_ch(count->id);
  268. int ret;
  269. ret = rz_mtu3_lock_if_counter_is_valid(counter, ch, priv, count->id);
  270. if (ret)
  271. return ret;
  272. switch (count->id) {
  273. case RZ_MTU3_16_BIT_MTU1_CH:
  274. case RZ_MTU3_16_BIT_MTU2_CH:
  275. *ceiling = priv->mtu_16bit_max[ch_id];
  276. break;
  277. case RZ_MTU3_32_BIT_CH:
  278. *ceiling = priv->mtu_32bit_max;
  279. break;
  280. default:
  281. /* should never reach this path */
  282. mutex_unlock(&priv->lock);
  283. return -EINVAL;
  284. }
  285. mutex_unlock(&priv->lock);
  286. return 0;
  287. }
  288. static int rz_mtu3_count_ceiling_write(struct counter_device *counter,
  289. struct counter_count *count,
  290. u64 ceiling)
  291. {
  292. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  293. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  294. const size_t ch_id = rz_mtu3_get_hw_ch(count->id);
  295. int ret;
  296. ret = rz_mtu3_lock_if_counter_is_valid(counter, ch, priv, count->id);
  297. if (ret)
  298. return ret;
  299. switch (count->id) {
  300. case RZ_MTU3_16_BIT_MTU1_CH:
  301. case RZ_MTU3_16_BIT_MTU2_CH:
  302. if (ceiling > U16_MAX) {
  303. mutex_unlock(&priv->lock);
  304. return -ERANGE;
  305. }
  306. priv->mtu_16bit_max[ch_id] = ceiling;
  307. break;
  308. case RZ_MTU3_32_BIT_CH:
  309. if (ceiling > U32_MAX) {
  310. mutex_unlock(&priv->lock);
  311. return -ERANGE;
  312. }
  313. priv->mtu_32bit_max = ceiling;
  314. break;
  315. default:
  316. /* should never reach this path */
  317. mutex_unlock(&priv->lock);
  318. return -EINVAL;
  319. }
  320. pm_runtime_get_sync(ch->dev);
  321. if (count->id == RZ_MTU3_32_BIT_CH)
  322. rz_mtu3_32bit_ch_write(ch, RZ_MTU3_TGRALW, ceiling);
  323. else
  324. rz_mtu3_16bit_ch_write(ch, RZ_MTU3_TGRA, ceiling);
  325. rz_mtu3_8bit_ch_write(ch, RZ_MTU3_TCR, RZ_MTU3_TCR_CCLR_TGRA);
  326. pm_runtime_put(ch->dev);
  327. mutex_unlock(&priv->lock);
  328. return 0;
  329. }
  330. static void rz_mtu3_32bit_cnt_setting(struct counter_device *counter)
  331. {
  332. struct rz_mtu3_channel *const ch1 = rz_mtu3_get_ch(counter, 0);
  333. struct rz_mtu3_channel *const ch2 = rz_mtu3_get_ch(counter, 1);
  334. /* Phase counting mode 1 is used as default in initialization. */
  335. rz_mtu3_8bit_ch_write(ch1, RZ_MTU3_TMDR1, RZ_MTU3_TMDR1_PH_CNT_MODE_1);
  336. rz_mtu3_8bit_ch_write(ch1, RZ_MTU3_TCR, RZ_MTU3_TCR_CCLR_TGRA);
  337. rz_mtu3_8bit_ch_write(ch1, RZ_MTU3_TIOR, RZ_MTU3_TIOR_IC_BOTH);
  338. rz_mtu3_enable(ch1);
  339. rz_mtu3_enable(ch2);
  340. }
  341. static void rz_mtu3_16bit_cnt_setting(struct counter_device *counter, int id)
  342. {
  343. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, id);
  344. /* Phase counting mode 1 is used as default in initialization. */
  345. rz_mtu3_8bit_ch_write(ch, RZ_MTU3_TMDR1, RZ_MTU3_TMDR1_PH_CNT_MODE_1);
  346. rz_mtu3_8bit_ch_write(ch, RZ_MTU3_TCR, RZ_MTU3_TCR_CCLR_TGRA);
  347. rz_mtu3_8bit_ch_write(ch, RZ_MTU3_TIOR, RZ_MTU3_TIOR_NO_OUTPUT);
  348. rz_mtu3_enable(ch);
  349. }
  350. static int rz_mtu3_initialize_counter(struct counter_device *counter, int id)
  351. {
  352. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, id);
  353. struct rz_mtu3_channel *const ch1 = rz_mtu3_get_ch(counter, 0);
  354. struct rz_mtu3_channel *const ch2 = rz_mtu3_get_ch(counter, 1);
  355. switch (id) {
  356. case RZ_MTU3_16_BIT_MTU1_CH:
  357. case RZ_MTU3_16_BIT_MTU2_CH:
  358. if (!rz_mtu3_request_channel(ch))
  359. return -EBUSY;
  360. rz_mtu3_16bit_cnt_setting(counter, id);
  361. return 0;
  362. case RZ_MTU3_32_BIT_CH:
  363. /*
  364. * 32-bit phase counting need MTU1 and MTU2 to create 32-bit
  365. * cascade counter.
  366. */
  367. if (!rz_mtu3_request_channel(ch1))
  368. return -EBUSY;
  369. if (!rz_mtu3_request_channel(ch2)) {
  370. rz_mtu3_release_channel(ch1);
  371. return -EBUSY;
  372. }
  373. rz_mtu3_32bit_cnt_setting(counter);
  374. return 0;
  375. default:
  376. /* should never reach this path */
  377. return -EINVAL;
  378. }
  379. }
  380. static void rz_mtu3_terminate_counter(struct counter_device *counter, int id)
  381. {
  382. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, id);
  383. struct rz_mtu3_channel *const ch1 = rz_mtu3_get_ch(counter, 0);
  384. struct rz_mtu3_channel *const ch2 = rz_mtu3_get_ch(counter, 1);
  385. if (id == RZ_MTU3_32_BIT_CH) {
  386. rz_mtu3_release_channel(ch2);
  387. rz_mtu3_release_channel(ch1);
  388. rz_mtu3_disable(ch2);
  389. rz_mtu3_disable(ch1);
  390. } else {
  391. rz_mtu3_release_channel(ch);
  392. rz_mtu3_disable(ch);
  393. }
  394. }
  395. static int rz_mtu3_count_enable_read(struct counter_device *counter,
  396. struct counter_count *count, u8 *enable)
  397. {
  398. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  399. struct rz_mtu3_channel *const ch1 = rz_mtu3_get_ch(counter, 0);
  400. struct rz_mtu3_channel *const ch2 = rz_mtu3_get_ch(counter, 1);
  401. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  402. int ret;
  403. ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
  404. if (ret)
  405. return ret;
  406. if (count->id == RZ_MTU3_32_BIT_CH)
  407. *enable = rz_mtu3_is_enabled(ch1) && rz_mtu3_is_enabled(ch2);
  408. else
  409. *enable = rz_mtu3_is_enabled(ch);
  410. mutex_unlock(&priv->lock);
  411. return 0;
  412. }
  413. static int rz_mtu3_count_enable_write(struct counter_device *counter,
  414. struct counter_count *count, u8 enable)
  415. {
  416. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  417. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  418. int ret = 0;
  419. if (enable) {
  420. mutex_lock(&priv->lock);
  421. pm_runtime_get_sync(ch->dev);
  422. ret = rz_mtu3_initialize_counter(counter, count->id);
  423. if (ret == 0)
  424. priv->count_is_enabled[count->id] = true;
  425. mutex_unlock(&priv->lock);
  426. } else {
  427. mutex_lock(&priv->lock);
  428. rz_mtu3_terminate_counter(counter, count->id);
  429. priv->count_is_enabled[count->id] = false;
  430. pm_runtime_put(ch->dev);
  431. mutex_unlock(&priv->lock);
  432. }
  433. return ret;
  434. }
  435. static int rz_mtu3_lock_if_ch0_is_enabled(struct rz_mtu3_cnt *const priv)
  436. {
  437. mutex_lock(&priv->lock);
  438. if (priv->ch->is_busy && !(priv->count_is_enabled[RZ_MTU3_16_BIT_MTU1_CH] ||
  439. priv->count_is_enabled[RZ_MTU3_32_BIT_CH])) {
  440. mutex_unlock(&priv->lock);
  441. return -EINVAL;
  442. }
  443. return 0;
  444. }
  445. static int rz_mtu3_cascade_counts_enable_get(struct counter_device *counter,
  446. u8 *cascade_enable)
  447. {
  448. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  449. unsigned long tmdr;
  450. int ret;
  451. ret = rz_mtu3_lock_if_ch0_is_enabled(priv);
  452. if (ret)
  453. return ret;
  454. pm_runtime_get_sync(priv->ch->dev);
  455. tmdr = rz_mtu3_shared_reg_read(priv->ch, RZ_MTU3_TMDR3);
  456. pm_runtime_put(priv->ch->dev);
  457. *cascade_enable = test_bit(RZ_MTU3_TMDR3_LWA, &tmdr);
  458. mutex_unlock(&priv->lock);
  459. return 0;
  460. }
  461. static int rz_mtu3_cascade_counts_enable_set(struct counter_device *counter,
  462. u8 cascade_enable)
  463. {
  464. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  465. int ret;
  466. ret = rz_mtu3_lock_if_ch0_is_enabled(priv);
  467. if (ret)
  468. return ret;
  469. pm_runtime_get_sync(priv->ch->dev);
  470. rz_mtu3_shared_reg_update_bit(priv->ch, RZ_MTU3_TMDR3,
  471. RZ_MTU3_TMDR3_LWA, cascade_enable);
  472. pm_runtime_put(priv->ch->dev);
  473. mutex_unlock(&priv->lock);
  474. return 0;
  475. }
  476. static int rz_mtu3_ext_input_phase_clock_select_get(struct counter_device *counter,
  477. u32 *ext_input_phase_clock_select)
  478. {
  479. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  480. unsigned long tmdr;
  481. int ret;
  482. ret = rz_mtu3_lock_if_ch0_is_enabled(priv);
  483. if (ret)
  484. return ret;
  485. pm_runtime_get_sync(priv->ch->dev);
  486. tmdr = rz_mtu3_shared_reg_read(priv->ch, RZ_MTU3_TMDR3);
  487. pm_runtime_put(priv->ch->dev);
  488. *ext_input_phase_clock_select = test_bit(RZ_MTU3_TMDR3_PHCKSEL, &tmdr);
  489. mutex_unlock(&priv->lock);
  490. return 0;
  491. }
  492. static int rz_mtu3_ext_input_phase_clock_select_set(struct counter_device *counter,
  493. u32 ext_input_phase_clock_select)
  494. {
  495. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  496. int ret;
  497. ret = rz_mtu3_lock_if_ch0_is_enabled(priv);
  498. if (ret)
  499. return ret;
  500. pm_runtime_get_sync(priv->ch->dev);
  501. rz_mtu3_shared_reg_update_bit(priv->ch, RZ_MTU3_TMDR3,
  502. RZ_MTU3_TMDR3_PHCKSEL,
  503. ext_input_phase_clock_select);
  504. pm_runtime_put(priv->ch->dev);
  505. mutex_unlock(&priv->lock);
  506. return 0;
  507. }
  508. static struct counter_comp rz_mtu3_count_ext[] = {
  509. COUNTER_COMP_DIRECTION(rz_mtu3_count_direction_read),
  510. COUNTER_COMP_ENABLE(rz_mtu3_count_enable_read,
  511. rz_mtu3_count_enable_write),
  512. COUNTER_COMP_CEILING(rz_mtu3_count_ceiling_read,
  513. rz_mtu3_count_ceiling_write),
  514. };
  515. static const enum counter_synapse_action rz_mtu3_synapse_actions[] = {
  516. COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
  517. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  518. COUNTER_SYNAPSE_ACTION_NONE,
  519. };
  520. static int rz_mtu3_action_read(struct counter_device *counter,
  521. struct counter_count *count,
  522. struct counter_synapse *synapse,
  523. enum counter_synapse_action *action)
  524. {
  525. const bool is_signal_ab = (synapse->signal->id == SIGNAL_A_ID) ||
  526. (synapse->signal->id == SIGNAL_B_ID);
  527. struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
  528. struct rz_mtu3_cnt *const priv = counter_priv(counter);
  529. enum counter_function function;
  530. bool mtclkc_mtclkd;
  531. unsigned long tmdr;
  532. int ret;
  533. ret = rz_mtu3_lock_if_count_is_enabled(ch, priv, count->id);
  534. if (ret)
  535. return ret;
  536. ret = rz_mtu3_count_function_read_helper(ch, priv, &function);
  537. if (ret) {
  538. mutex_unlock(&priv->lock);
  539. return ret;
  540. }
  541. /* Default action mode */
  542. *action = COUNTER_SYNAPSE_ACTION_NONE;
  543. if (count->id != RZ_MTU3_16_BIT_MTU1_CH) {
  544. tmdr = rz_mtu3_shared_reg_read(priv->ch, RZ_MTU3_TMDR3);
  545. mtclkc_mtclkd = test_bit(RZ_MTU3_TMDR3_PHCKSEL, &tmdr);
  546. if ((mtclkc_mtclkd && is_signal_ab) ||
  547. (!mtclkc_mtclkd && !is_signal_ab)) {
  548. mutex_unlock(&priv->lock);
  549. return 0;
  550. }
  551. }
  552. switch (function) {
  553. case COUNTER_FUNCTION_PULSE_DIRECTION:
  554. /*
  555. * Rising edges on signal A (signal C) updates the respective
  556. * count. The input level of signal B (signal D) determines
  557. * direction.
  558. */
  559. if (synapse->signal->id == SIGNAL_A_ID ||
  560. synapse->signal->id == SIGNAL_C_ID)
  561. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  562. break;
  563. case COUNTER_FUNCTION_QUADRATURE_X2_B:
  564. /*
  565. * Any state transition on quadrature pair signal B (signal D)
  566. * updates the respective count.
  567. */
  568. if (synapse->signal->id == SIGNAL_B_ID ||
  569. synapse->signal->id == SIGNAL_D_ID)
  570. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  571. break;
  572. case COUNTER_FUNCTION_QUADRATURE_X4:
  573. /* counts up/down on both edges of A (C) and B (D) signal */
  574. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  575. break;
  576. default:
  577. /* should never reach this path */
  578. mutex_unlock(&priv->lock);
  579. return -EINVAL;
  580. }
  581. mutex_unlock(&priv->lock);
  582. return 0;
  583. }
  584. static const struct counter_ops rz_mtu3_cnt_ops = {
  585. .count_read = rz_mtu3_count_read,
  586. .count_write = rz_mtu3_count_write,
  587. .function_read = rz_mtu3_count_function_read,
  588. .function_write = rz_mtu3_count_function_write,
  589. .action_read = rz_mtu3_action_read,
  590. };
  591. #define RZ_MTU3_PHASE_SIGNAL(_id, _name) { \
  592. .id = (_id), \
  593. .name = (_name), \
  594. }
  595. static struct counter_signal rz_mtu3_signals[] = {
  596. RZ_MTU3_PHASE_SIGNAL(SIGNAL_A_ID, "MTU1 MTCLKA"),
  597. RZ_MTU3_PHASE_SIGNAL(SIGNAL_B_ID, "MTU1 MTCLKB"),
  598. RZ_MTU3_PHASE_SIGNAL(SIGNAL_C_ID, "MTU2 MTCLKC"),
  599. RZ_MTU3_PHASE_SIGNAL(SIGNAL_D_ID, "MTU2 MTCLKD"),
  600. };
  601. static struct counter_synapse rz_mtu3_mtu1_count_synapses[] = {
  602. {
  603. .actions_list = rz_mtu3_synapse_actions,
  604. .num_actions = ARRAY_SIZE(rz_mtu3_synapse_actions),
  605. .signal = rz_mtu3_signals,
  606. },
  607. {
  608. .actions_list = rz_mtu3_synapse_actions,
  609. .num_actions = ARRAY_SIZE(rz_mtu3_synapse_actions),
  610. .signal = rz_mtu3_signals + 1,
  611. }
  612. };
  613. static struct counter_synapse rz_mtu3_mtu2_count_synapses[] = {
  614. {
  615. .actions_list = rz_mtu3_synapse_actions,
  616. .num_actions = ARRAY_SIZE(rz_mtu3_synapse_actions),
  617. .signal = rz_mtu3_signals,
  618. },
  619. {
  620. .actions_list = rz_mtu3_synapse_actions,
  621. .num_actions = ARRAY_SIZE(rz_mtu3_synapse_actions),
  622. .signal = rz_mtu3_signals + 1,
  623. },
  624. {
  625. .actions_list = rz_mtu3_synapse_actions,
  626. .num_actions = ARRAY_SIZE(rz_mtu3_synapse_actions),
  627. .signal = rz_mtu3_signals + 2,
  628. },
  629. {
  630. .actions_list = rz_mtu3_synapse_actions,
  631. .num_actions = ARRAY_SIZE(rz_mtu3_synapse_actions),
  632. .signal = rz_mtu3_signals + 3,
  633. }
  634. };
  635. static struct counter_count rz_mtu3_counts[] = {
  636. {
  637. .id = RZ_MTU3_16_BIT_MTU1_CH,
  638. .name = "Channel 1 Count",
  639. .functions_list = rz_mtu3_count_functions,
  640. .num_functions = ARRAY_SIZE(rz_mtu3_count_functions),
  641. .synapses = rz_mtu3_mtu1_count_synapses,
  642. .num_synapses = ARRAY_SIZE(rz_mtu3_mtu1_count_synapses),
  643. .ext = rz_mtu3_count_ext,
  644. .num_ext = ARRAY_SIZE(rz_mtu3_count_ext),
  645. },
  646. {
  647. .id = RZ_MTU3_16_BIT_MTU2_CH,
  648. .name = "Channel 2 Count",
  649. .functions_list = rz_mtu3_count_functions,
  650. .num_functions = ARRAY_SIZE(rz_mtu3_count_functions),
  651. .synapses = rz_mtu3_mtu2_count_synapses,
  652. .num_synapses = ARRAY_SIZE(rz_mtu3_mtu2_count_synapses),
  653. .ext = rz_mtu3_count_ext,
  654. .num_ext = ARRAY_SIZE(rz_mtu3_count_ext),
  655. },
  656. {
  657. .id = RZ_MTU3_32_BIT_CH,
  658. .name = "Channel 1 and 2 (cascaded) Count",
  659. .functions_list = rz_mtu3_count_functions,
  660. .num_functions = ARRAY_SIZE(rz_mtu3_count_functions),
  661. .synapses = rz_mtu3_mtu2_count_synapses,
  662. .num_synapses = ARRAY_SIZE(rz_mtu3_mtu2_count_synapses),
  663. .ext = rz_mtu3_count_ext,
  664. .num_ext = ARRAY_SIZE(rz_mtu3_count_ext),
  665. }
  666. };
  667. static const char *const rz_mtu3_ext_input_phase_clock_select[] = {
  668. "MTCLKA-MTCLKB",
  669. "MTCLKC-MTCLKD",
  670. };
  671. static DEFINE_COUNTER_ENUM(rz_mtu3_ext_input_phase_clock_select_enum,
  672. rz_mtu3_ext_input_phase_clock_select);
  673. static struct counter_comp rz_mtu3_device_ext[] = {
  674. COUNTER_COMP_DEVICE_BOOL("cascade_counts_enable",
  675. rz_mtu3_cascade_counts_enable_get,
  676. rz_mtu3_cascade_counts_enable_set),
  677. COUNTER_COMP_DEVICE_ENUM("external_input_phase_clock_select",
  678. rz_mtu3_ext_input_phase_clock_select_get,
  679. rz_mtu3_ext_input_phase_clock_select_set,
  680. rz_mtu3_ext_input_phase_clock_select_enum),
  681. };
  682. static int rz_mtu3_cnt_pm_runtime_suspend(struct device *dev)
  683. {
  684. struct clk *const clk = dev_get_drvdata(dev);
  685. clk_disable_unprepare(clk);
  686. return 0;
  687. }
  688. static int rz_mtu3_cnt_pm_runtime_resume(struct device *dev)
  689. {
  690. struct clk *const clk = dev_get_drvdata(dev);
  691. clk_prepare_enable(clk);
  692. return 0;
  693. }
  694. static DEFINE_RUNTIME_DEV_PM_OPS(rz_mtu3_cnt_pm_ops,
  695. rz_mtu3_cnt_pm_runtime_suspend,
  696. rz_mtu3_cnt_pm_runtime_resume, NULL);
  697. static void rz_mtu3_cnt_pm_disable(void *data)
  698. {
  699. struct device *dev = data;
  700. pm_runtime_disable(dev);
  701. pm_runtime_set_suspended(dev);
  702. }
  703. static int rz_mtu3_cnt_probe(struct platform_device *pdev)
  704. {
  705. struct rz_mtu3 *ddata = dev_get_drvdata(pdev->dev.parent);
  706. struct device *dev = &pdev->dev;
  707. struct counter_device *counter;
  708. struct rz_mtu3_channel *ch;
  709. struct rz_mtu3_cnt *priv;
  710. unsigned int i;
  711. int ret;
  712. counter = devm_counter_alloc(dev, sizeof(*priv));
  713. if (!counter)
  714. return -ENOMEM;
  715. priv = counter_priv(counter);
  716. priv->clk = ddata->clk;
  717. priv->mtu_32bit_max = U32_MAX;
  718. priv->ch = &ddata->channels[RZ_MTU3_CHAN_1];
  719. ch = &priv->ch[0];
  720. for (i = 0; i < RZ_MTU3_MAX_HW_CNTR_CHANNELS; i++) {
  721. ch->dev = dev;
  722. priv->mtu_16bit_max[i] = U16_MAX;
  723. ch++;
  724. }
  725. mutex_init(&priv->lock);
  726. platform_set_drvdata(pdev, priv->clk);
  727. clk_prepare_enable(priv->clk);
  728. pm_runtime_set_active(&pdev->dev);
  729. pm_runtime_enable(&pdev->dev);
  730. ret = devm_add_action_or_reset(&pdev->dev, rz_mtu3_cnt_pm_disable, dev);
  731. if (ret < 0)
  732. goto disable_clock;
  733. counter->name = dev_name(dev);
  734. counter->parent = dev;
  735. counter->ops = &rz_mtu3_cnt_ops;
  736. counter->counts = rz_mtu3_counts;
  737. counter->num_counts = ARRAY_SIZE(rz_mtu3_counts);
  738. counter->signals = rz_mtu3_signals;
  739. counter->num_signals = ARRAY_SIZE(rz_mtu3_signals);
  740. counter->ext = rz_mtu3_device_ext;
  741. counter->num_ext = ARRAY_SIZE(rz_mtu3_device_ext);
  742. /* Register Counter device */
  743. ret = devm_counter_add(dev, counter);
  744. if (ret < 0) {
  745. dev_err_probe(dev, ret, "Failed to add counter\n");
  746. goto disable_clock;
  747. }
  748. return 0;
  749. disable_clock:
  750. clk_disable_unprepare(priv->clk);
  751. return ret;
  752. }
  753. static struct platform_driver rz_mtu3_cnt_driver = {
  754. .probe = rz_mtu3_cnt_probe,
  755. .driver = {
  756. .name = "rz-mtu3-counter",
  757. .pm = pm_ptr(&rz_mtu3_cnt_pm_ops),
  758. },
  759. };
  760. module_platform_driver(rz_mtu3_cnt_driver);
  761. MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
  762. MODULE_ALIAS("platform:rz-mtu3-counter");
  763. MODULE_DESCRIPTION("Renesas RZ/G2L MTU3a counter driver");
  764. MODULE_LICENSE("GPL");
  765. MODULE_IMPORT_NS(COUNTER);