microchip-tcb-capture.c 10 KB

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