microchip-tcb-capture.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Microchip
  4. *
  5. * Author: Kamel Bouhara <kamel.bouhara@bootlin.com>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/counter.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <soc/at91/atmel_tcb.h>
  16. #define ATMEL_TC_CMR_MASK (ATMEL_TC_LDRA_RISING | ATMEL_TC_LDRB_FALLING | \
  17. ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_LDBDIS | \
  18. ATMEL_TC_LDBSTOP)
  19. #define ATMEL_TC_QDEN BIT(8)
  20. #define ATMEL_TC_POSEN BIT(9)
  21. struct mchp_tc_data {
  22. const struct atmel_tcb_config *tc_cfg;
  23. struct regmap *regmap;
  24. int qdec_mode;
  25. int num_channels;
  26. int channel[2];
  27. };
  28. static const enum counter_function mchp_tc_count_functions[] = {
  29. COUNTER_FUNCTION_INCREASE,
  30. COUNTER_FUNCTION_QUADRATURE_X4,
  31. };
  32. static const enum counter_synapse_action mchp_tc_synapse_actions[] = {
  33. COUNTER_SYNAPSE_ACTION_NONE,
  34. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  35. COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
  36. COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
  37. };
  38. static struct counter_signal mchp_tc_count_signals[] = {
  39. {
  40. .id = 0,
  41. .name = "Channel A",
  42. },
  43. {
  44. .id = 1,
  45. .name = "Channel B",
  46. }
  47. };
  48. static struct counter_synapse mchp_tc_count_synapses[] = {
  49. {
  50. .actions_list = mchp_tc_synapse_actions,
  51. .num_actions = ARRAY_SIZE(mchp_tc_synapse_actions),
  52. .signal = &mchp_tc_count_signals[0]
  53. },
  54. {
  55. .actions_list = mchp_tc_synapse_actions,
  56. .num_actions = ARRAY_SIZE(mchp_tc_synapse_actions),
  57. .signal = &mchp_tc_count_signals[1]
  58. }
  59. };
  60. static int mchp_tc_count_function_read(struct counter_device *counter,
  61. struct counter_count *count,
  62. enum counter_function *function)
  63. {
  64. struct mchp_tc_data *const priv = counter_priv(counter);
  65. if (priv->qdec_mode)
  66. *function = COUNTER_FUNCTION_QUADRATURE_X4;
  67. else
  68. *function = COUNTER_FUNCTION_INCREASE;
  69. return 0;
  70. }
  71. static int mchp_tc_count_function_write(struct counter_device *counter,
  72. struct counter_count *count,
  73. enum counter_function function)
  74. {
  75. struct mchp_tc_data *const priv = counter_priv(counter);
  76. u32 bmr, cmr;
  77. regmap_read(priv->regmap, ATMEL_TC_BMR, &bmr);
  78. regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
  79. /* Set capture mode */
  80. cmr &= ~ATMEL_TC_WAVE;
  81. switch (function) {
  82. case COUNTER_FUNCTION_INCREASE:
  83. priv->qdec_mode = 0;
  84. /* Set highest rate based on whether soc has gclk or not */
  85. bmr &= ~(ATMEL_TC_QDEN | ATMEL_TC_POSEN);
  86. if (!priv->tc_cfg->has_gclk)
  87. cmr |= ATMEL_TC_TIMER_CLOCK2;
  88. else
  89. cmr |= ATMEL_TC_TIMER_CLOCK1;
  90. /* Setup the period capture mode */
  91. cmr |= ATMEL_TC_CMR_MASK;
  92. cmr &= ~(ATMEL_TC_ABETRG | ATMEL_TC_XC0);
  93. break;
  94. case COUNTER_FUNCTION_QUADRATURE_X4:
  95. if (!priv->tc_cfg->has_qdec)
  96. return -EINVAL;
  97. /* In QDEC mode settings both channels 0 and 1 are required */
  98. if (priv->num_channels < 2 || priv->channel[0] != 0 ||
  99. priv->channel[1] != 1) {
  100. pr_err("Invalid channels number or id for quadrature mode\n");
  101. return -EINVAL;
  102. }
  103. priv->qdec_mode = 1;
  104. bmr |= ATMEL_TC_QDEN | ATMEL_TC_POSEN;
  105. cmr |= ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_ABETRG | ATMEL_TC_XC0;
  106. break;
  107. default:
  108. /* should never reach this path */
  109. return -EINVAL;
  110. }
  111. regmap_write(priv->regmap, ATMEL_TC_BMR, bmr);
  112. regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), cmr);
  113. /* Enable clock and trigger counter */
  114. regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], CCR),
  115. ATMEL_TC_CLKEN | ATMEL_TC_SWTRG);
  116. if (priv->qdec_mode) {
  117. regmap_write(priv->regmap,
  118. ATMEL_TC_REG(priv->channel[1], CMR), cmr);
  119. regmap_write(priv->regmap,
  120. ATMEL_TC_REG(priv->channel[1], CCR),
  121. ATMEL_TC_CLKEN | ATMEL_TC_SWTRG);
  122. }
  123. return 0;
  124. }
  125. static int mchp_tc_count_signal_read(struct counter_device *counter,
  126. struct counter_signal *signal,
  127. enum counter_signal_level *lvl)
  128. {
  129. struct mchp_tc_data *const priv = counter_priv(counter);
  130. bool sigstatus;
  131. u32 sr;
  132. regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
  133. if (signal->id == 1)
  134. sigstatus = (sr & ATMEL_TC_MTIOB);
  135. else
  136. sigstatus = (sr & ATMEL_TC_MTIOA);
  137. *lvl = sigstatus ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
  138. return 0;
  139. }
  140. static int mchp_tc_count_action_read(struct counter_device *counter,
  141. struct counter_count *count,
  142. struct counter_synapse *synapse,
  143. enum counter_synapse_action *action)
  144. {
  145. struct mchp_tc_data *const priv = counter_priv(counter);
  146. u32 cmr;
  147. if (priv->qdec_mode) {
  148. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  149. return 0;
  150. }
  151. /* Only TIOA signal is evaluated in non-QDEC mode */
  152. if (synapse->signal->id != 0) {
  153. *action = COUNTER_SYNAPSE_ACTION_NONE;
  154. return 0;
  155. }
  156. regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
  157. switch (cmr & ATMEL_TC_ETRGEDG) {
  158. default:
  159. *action = COUNTER_SYNAPSE_ACTION_NONE;
  160. break;
  161. case ATMEL_TC_ETRGEDG_RISING:
  162. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  163. break;
  164. case ATMEL_TC_ETRGEDG_FALLING:
  165. *action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
  166. break;
  167. case ATMEL_TC_ETRGEDG_BOTH:
  168. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  169. break;
  170. }
  171. return 0;
  172. }
  173. static int mchp_tc_count_action_write(struct counter_device *counter,
  174. struct counter_count *count,
  175. struct counter_synapse *synapse,
  176. enum counter_synapse_action action)
  177. {
  178. struct mchp_tc_data *const priv = counter_priv(counter);
  179. u32 edge = ATMEL_TC_ETRGEDG_NONE;
  180. /* QDEC mode is rising edge only; only TIOA handled in non-QDEC mode */
  181. if (priv->qdec_mode || synapse->signal->id != 0)
  182. return -EINVAL;
  183. switch (action) {
  184. case COUNTER_SYNAPSE_ACTION_NONE:
  185. edge = ATMEL_TC_ETRGEDG_NONE;
  186. break;
  187. case COUNTER_SYNAPSE_ACTION_RISING_EDGE:
  188. edge = ATMEL_TC_ETRGEDG_RISING;
  189. break;
  190. case COUNTER_SYNAPSE_ACTION_FALLING_EDGE:
  191. edge = ATMEL_TC_ETRGEDG_FALLING;
  192. break;
  193. case COUNTER_SYNAPSE_ACTION_BOTH_EDGES:
  194. edge = ATMEL_TC_ETRGEDG_BOTH;
  195. break;
  196. default:
  197. /* should never reach this path */
  198. return -EINVAL;
  199. }
  200. return regmap_write_bits(priv->regmap,
  201. ATMEL_TC_REG(priv->channel[0], CMR),
  202. ATMEL_TC_ETRGEDG, edge);
  203. }
  204. static int mchp_tc_count_read(struct counter_device *counter,
  205. struct counter_count *count, u64 *val)
  206. {
  207. struct mchp_tc_data *const priv = counter_priv(counter);
  208. u32 cnt;
  209. regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CV), &cnt);
  210. *val = cnt;
  211. return 0;
  212. }
  213. static struct counter_count mchp_tc_counts[] = {
  214. {
  215. .id = 0,
  216. .name = "Timer Counter",
  217. .functions_list = mchp_tc_count_functions,
  218. .num_functions = ARRAY_SIZE(mchp_tc_count_functions),
  219. .synapses = mchp_tc_count_synapses,
  220. .num_synapses = ARRAY_SIZE(mchp_tc_count_synapses),
  221. },
  222. };
  223. static const struct counter_ops mchp_tc_ops = {
  224. .signal_read = mchp_tc_count_signal_read,
  225. .count_read = mchp_tc_count_read,
  226. .function_read = mchp_tc_count_function_read,
  227. .function_write = mchp_tc_count_function_write,
  228. .action_read = mchp_tc_count_action_read,
  229. .action_write = mchp_tc_count_action_write
  230. };
  231. static const struct atmel_tcb_config tcb_rm9200_config = {
  232. .counter_width = 16,
  233. };
  234. static const struct atmel_tcb_config tcb_sam9x5_config = {
  235. .counter_width = 32,
  236. };
  237. static const struct atmel_tcb_config tcb_sama5d2_config = {
  238. .counter_width = 32,
  239. .has_gclk = true,
  240. .has_qdec = true,
  241. };
  242. static const struct atmel_tcb_config tcb_sama5d3_config = {
  243. .counter_width = 32,
  244. .has_qdec = true,
  245. };
  246. static const struct of_device_id atmel_tc_of_match[] = {
  247. { .compatible = "atmel,at91rm9200-tcb", .data = &tcb_rm9200_config, },
  248. { .compatible = "atmel,at91sam9x5-tcb", .data = &tcb_sam9x5_config, },
  249. { .compatible = "atmel,sama5d2-tcb", .data = &tcb_sama5d2_config, },
  250. { .compatible = "atmel,sama5d3-tcb", .data = &tcb_sama5d3_config, },
  251. { /* sentinel */ }
  252. };
  253. static void mchp_tc_clk_remove(void *ptr)
  254. {
  255. clk_disable_unprepare((struct clk *)ptr);
  256. }
  257. static int mchp_tc_probe(struct platform_device *pdev)
  258. {
  259. struct device_node *np = pdev->dev.of_node;
  260. const struct atmel_tcb_config *tcb_config;
  261. const struct of_device_id *match;
  262. struct counter_device *counter;
  263. struct mchp_tc_data *priv;
  264. char clk_name[7];
  265. struct regmap *regmap;
  266. struct clk *clk[3];
  267. int channel;
  268. int ret, i;
  269. counter = devm_counter_alloc(&pdev->dev, sizeof(*priv));
  270. if (!counter)
  271. return -ENOMEM;
  272. priv = counter_priv(counter);
  273. match = of_match_node(atmel_tc_of_match, np->parent);
  274. tcb_config = match->data;
  275. if (!tcb_config) {
  276. dev_err(&pdev->dev, "No matching parent node found\n");
  277. return -ENODEV;
  278. }
  279. regmap = syscon_node_to_regmap(np->parent);
  280. if (IS_ERR(regmap))
  281. return PTR_ERR(regmap);
  282. /* max. channels number is 2 when in QDEC mode */
  283. priv->num_channels = of_property_count_u32_elems(np, "reg");
  284. if (priv->num_channels < 0) {
  285. dev_err(&pdev->dev, "Invalid or missing channel\n");
  286. return -EINVAL;
  287. }
  288. /* Register channels and initialize clocks */
  289. for (i = 0; i < priv->num_channels; i++) {
  290. ret = of_property_read_u32_index(np, "reg", i, &channel);
  291. if (ret < 0 || channel > 2)
  292. return -ENODEV;
  293. priv->channel[i] = channel;
  294. snprintf(clk_name, sizeof(clk_name), "t%d_clk", channel);
  295. clk[i] = of_clk_get_by_name(np->parent, clk_name);
  296. if (IS_ERR(clk[i])) {
  297. /* Fallback to t0_clk */
  298. clk[i] = of_clk_get_by_name(np->parent, "t0_clk");
  299. if (IS_ERR(clk[i]))
  300. return PTR_ERR(clk[i]);
  301. }
  302. ret = clk_prepare_enable(clk[i]);
  303. if (ret)
  304. return ret;
  305. ret = devm_add_action_or_reset(&pdev->dev,
  306. mchp_tc_clk_remove,
  307. clk[i]);
  308. if (ret)
  309. return ret;
  310. dev_dbg(&pdev->dev,
  311. "Initialized capture mode on channel %d\n",
  312. channel);
  313. }
  314. priv->tc_cfg = tcb_config;
  315. priv->regmap = regmap;
  316. counter->name = dev_name(&pdev->dev);
  317. counter->parent = &pdev->dev;
  318. counter->ops = &mchp_tc_ops;
  319. counter->num_counts = ARRAY_SIZE(mchp_tc_counts);
  320. counter->counts = mchp_tc_counts;
  321. counter->num_signals = ARRAY_SIZE(mchp_tc_count_signals);
  322. counter->signals = mchp_tc_count_signals;
  323. ret = devm_counter_add(&pdev->dev, counter);
  324. if (ret < 0)
  325. return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
  326. return 0;
  327. }
  328. static const struct of_device_id mchp_tc_dt_ids[] = {
  329. { .compatible = "microchip,tcb-capture", },
  330. { /* sentinel */ },
  331. };
  332. MODULE_DEVICE_TABLE(of, mchp_tc_dt_ids);
  333. static struct platform_driver mchp_tc_driver = {
  334. .probe = mchp_tc_probe,
  335. .driver = {
  336. .name = "microchip-tcb-capture",
  337. .of_match_table = mchp_tc_dt_ids,
  338. },
  339. };
  340. module_platform_driver(mchp_tc_driver);
  341. MODULE_AUTHOR("Kamel Bouhara <kamel.bouhara@bootlin.com>");
  342. MODULE_DESCRIPTION("Microchip TCB Capture driver");
  343. MODULE_LICENSE("GPL v2");
  344. MODULE_IMPORT_NS(COUNTER);