1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 Microchip
5 * Author: Kamel Bouhara <kamel.bouhara@bootlin.com>
8 #include <linux/counter.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/module.h>
11 #include <linux/mutex.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>
18 #define ATMEL_TC_CMR_MASK (ATMEL_TC_LDRA_RISING | ATMEL_TC_LDRB_FALLING | \
19 ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_LDBDIS | \
22 #define ATMEL_TC_QDEN BIT(8)
23 #define ATMEL_TC_POSEN BIT(9)
26 const struct atmel_tcb_config *tc_cfg;
27 struct counter_device counter;
28 struct regmap *regmap;
34 enum mchp_tc_count_function {
35 MCHP_TC_FUNCTION_INCREASE,
36 MCHP_TC_FUNCTION_QUADRATURE,
39 static enum counter_count_function mchp_tc_count_functions[] = {
40 [MCHP_TC_FUNCTION_INCREASE] = COUNTER_COUNT_FUNCTION_INCREASE,
41 [MCHP_TC_FUNCTION_QUADRATURE] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4,
44 enum mchp_tc_synapse_action {
45 MCHP_TC_SYNAPSE_ACTION_NONE = 0,
46 MCHP_TC_SYNAPSE_ACTION_RISING_EDGE,
47 MCHP_TC_SYNAPSE_ACTION_FALLING_EDGE,
48 MCHP_TC_SYNAPSE_ACTION_BOTH_EDGE
51 static enum counter_synapse_action mchp_tc_synapse_actions[] = {
52 [MCHP_TC_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
53 [MCHP_TC_SYNAPSE_ACTION_RISING_EDGE] = COUNTER_SYNAPSE_ACTION_RISING_EDGE,
54 [MCHP_TC_SYNAPSE_ACTION_FALLING_EDGE] = COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
55 [MCHP_TC_SYNAPSE_ACTION_BOTH_EDGE] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
58 static struct counter_signal mchp_tc_count_signals[] = {
69 static struct counter_synapse mchp_tc_count_synapses[] = {
71 .actions_list = mchp_tc_synapse_actions,
72 .num_actions = ARRAY_SIZE(mchp_tc_synapse_actions),
73 .signal = &mchp_tc_count_signals[0]
76 .actions_list = mchp_tc_synapse_actions,
77 .num_actions = ARRAY_SIZE(mchp_tc_synapse_actions),
78 .signal = &mchp_tc_count_signals[1]
82 static int mchp_tc_count_function_get(struct counter_device *counter,
83 struct counter_count *count,
86 struct mchp_tc_data *const priv = counter->priv;
89 *function = MCHP_TC_FUNCTION_QUADRATURE;
91 *function = MCHP_TC_FUNCTION_INCREASE;
96 static int mchp_tc_count_function_set(struct counter_device *counter,
97 struct counter_count *count,
100 struct mchp_tc_data *const priv = counter->priv;
103 regmap_read(priv->regmap, ATMEL_TC_BMR, &bmr);
104 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
106 /* Set capture mode */
107 cmr &= ~ATMEL_TC_WAVE;
110 case MCHP_TC_FUNCTION_INCREASE:
112 /* Set highest rate based on whether soc has gclk or not */
113 bmr &= ~(ATMEL_TC_QDEN | ATMEL_TC_POSEN);
114 if (!priv->tc_cfg->has_gclk)
115 cmr |= ATMEL_TC_TIMER_CLOCK2;
117 cmr |= ATMEL_TC_TIMER_CLOCK1;
118 /* Setup the period capture mode */
119 cmr |= ATMEL_TC_CMR_MASK;
120 cmr &= ~(ATMEL_TC_ABETRG | ATMEL_TC_XC0);
122 case MCHP_TC_FUNCTION_QUADRATURE:
123 if (!priv->tc_cfg->has_qdec)
125 /* In QDEC mode settings both channels 0 and 1 are required */
126 if (priv->num_channels < 2 || priv->channel[0] != 0 ||
127 priv->channel[1] != 1) {
128 pr_err("Invalid channels number or id for quadrature mode\n");
132 bmr |= ATMEL_TC_QDEN | ATMEL_TC_POSEN;
133 cmr |= ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_ABETRG | ATMEL_TC_XC0;
137 regmap_write(priv->regmap, ATMEL_TC_BMR, bmr);
138 regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), cmr);
140 /* Enable clock and trigger counter */
141 regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], CCR),
142 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG);
144 if (priv->qdec_mode) {
145 regmap_write(priv->regmap,
146 ATMEL_TC_REG(priv->channel[1], CMR), cmr);
147 regmap_write(priv->regmap,
148 ATMEL_TC_REG(priv->channel[1], CCR),
149 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG);
155 static int mchp_tc_count_signal_read(struct counter_device *counter,
156 struct counter_signal *signal,
157 enum counter_signal_value *val)
159 struct mchp_tc_data *const priv = counter->priv;
163 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
166 sigstatus = (sr & ATMEL_TC_MTIOB);
168 sigstatus = (sr & ATMEL_TC_MTIOA);
170 *val = sigstatus ? COUNTER_SIGNAL_HIGH : COUNTER_SIGNAL_LOW;
175 static int mchp_tc_count_action_get(struct counter_device *counter,
176 struct counter_count *count,
177 struct counter_synapse *synapse,
180 struct mchp_tc_data *const priv = counter->priv;
183 if (priv->qdec_mode) {
184 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
188 /* Only TIOA signal is evaluated in non-QDEC mode */
189 if (synapse->signal->id != 0) {
190 *action = COUNTER_SYNAPSE_ACTION_NONE;
194 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
196 switch (cmr & ATMEL_TC_ETRGEDG) {
198 *action = MCHP_TC_SYNAPSE_ACTION_NONE;
200 case ATMEL_TC_ETRGEDG_RISING:
201 *action = MCHP_TC_SYNAPSE_ACTION_RISING_EDGE;
203 case ATMEL_TC_ETRGEDG_FALLING:
204 *action = MCHP_TC_SYNAPSE_ACTION_FALLING_EDGE;
206 case ATMEL_TC_ETRGEDG_BOTH:
207 *action = MCHP_TC_SYNAPSE_ACTION_BOTH_EDGE;
214 static int mchp_tc_count_action_set(struct counter_device *counter,
215 struct counter_count *count,
216 struct counter_synapse *synapse,
219 struct mchp_tc_data *const priv = counter->priv;
220 u32 edge = ATMEL_TC_ETRGEDG_NONE;
222 /* QDEC mode is rising edge only; only TIOA handled in non-QDEC mode */
223 if (priv->qdec_mode || synapse->signal->id != 0)
227 case MCHP_TC_SYNAPSE_ACTION_NONE:
228 edge = ATMEL_TC_ETRGEDG_NONE;
230 case MCHP_TC_SYNAPSE_ACTION_RISING_EDGE:
231 edge = ATMEL_TC_ETRGEDG_RISING;
233 case MCHP_TC_SYNAPSE_ACTION_FALLING_EDGE:
234 edge = ATMEL_TC_ETRGEDG_FALLING;
236 case MCHP_TC_SYNAPSE_ACTION_BOTH_EDGE:
237 edge = ATMEL_TC_ETRGEDG_BOTH;
241 return regmap_write_bits(priv->regmap,
242 ATMEL_TC_REG(priv->channel[0], CMR),
243 ATMEL_TC_ETRGEDG, edge);
246 static int mchp_tc_count_read(struct counter_device *counter,
247 struct counter_count *count,
250 struct mchp_tc_data *const priv = counter->priv;
253 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CV), &cnt);
259 static struct counter_count mchp_tc_counts[] = {
262 .name = "Timer Counter",
263 .functions_list = mchp_tc_count_functions,
264 .num_functions = ARRAY_SIZE(mchp_tc_count_functions),
265 .synapses = mchp_tc_count_synapses,
266 .num_synapses = ARRAY_SIZE(mchp_tc_count_synapses),
270 static const struct counter_ops mchp_tc_ops = {
271 .signal_read = mchp_tc_count_signal_read,
272 .count_read = mchp_tc_count_read,
273 .function_get = mchp_tc_count_function_get,
274 .function_set = mchp_tc_count_function_set,
275 .action_get = mchp_tc_count_action_get,
276 .action_set = mchp_tc_count_action_set
279 static const struct atmel_tcb_config tcb_rm9200_config = {
283 static const struct atmel_tcb_config tcb_sam9x5_config = {
287 static const struct atmel_tcb_config tcb_sama5d2_config = {
293 static const struct atmel_tcb_config tcb_sama5d3_config = {
298 static const struct of_device_id atmel_tc_of_match[] = {
299 { .compatible = "atmel,at91rm9200-tcb", .data = &tcb_rm9200_config, },
300 { .compatible = "atmel,at91sam9x5-tcb", .data = &tcb_sam9x5_config, },
301 { .compatible = "atmel,sama5d2-tcb", .data = &tcb_sama5d2_config, },
302 { .compatible = "atmel,sama5d3-tcb", .data = &tcb_sama5d3_config, },
306 static void mchp_tc_clk_remove(void *ptr)
308 clk_disable_unprepare((struct clk *)ptr);
311 static int mchp_tc_probe(struct platform_device *pdev)
313 struct device_node *np = pdev->dev.of_node;
314 const struct atmel_tcb_config *tcb_config;
315 const struct of_device_id *match;
316 struct mchp_tc_data *priv;
318 struct regmap *regmap;
323 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
327 platform_set_drvdata(pdev, priv);
329 match = of_match_node(atmel_tc_of_match, np->parent);
330 tcb_config = match->data;
332 dev_err(&pdev->dev, "No matching parent node found\n");
336 regmap = syscon_node_to_regmap(np->parent);
338 return PTR_ERR(regmap);
340 /* max. channels number is 2 when in QDEC mode */
341 priv->num_channels = of_property_count_u32_elems(np, "reg");
342 if (priv->num_channels < 0) {
343 dev_err(&pdev->dev, "Invalid or missing channel\n");
347 /* Register channels and initialize clocks */
348 for (i = 0; i < priv->num_channels; i++) {
349 ret = of_property_read_u32_index(np, "reg", i, &channel);
350 if (ret < 0 || channel > 2)
353 priv->channel[i] = channel;
355 snprintf(clk_name, sizeof(clk_name), "t%d_clk", channel);
357 clk[i] = of_clk_get_by_name(np->parent, clk_name);
358 if (IS_ERR(clk[i])) {
359 /* Fallback to t0_clk */
360 clk[i] = of_clk_get_by_name(np->parent, "t0_clk");
362 return PTR_ERR(clk[i]);
365 ret = clk_prepare_enable(clk[i]);
369 ret = devm_add_action_or_reset(&pdev->dev,
376 "Initialized capture mode on channel %d\n",
380 priv->tc_cfg = tcb_config;
381 priv->regmap = regmap;
382 priv->counter.name = dev_name(&pdev->dev);
383 priv->counter.parent = &pdev->dev;
384 priv->counter.ops = &mchp_tc_ops;
385 priv->counter.num_counts = ARRAY_SIZE(mchp_tc_counts);
386 priv->counter.counts = mchp_tc_counts;
387 priv->counter.num_signals = ARRAY_SIZE(mchp_tc_count_signals);
388 priv->counter.signals = mchp_tc_count_signals;
389 priv->counter.priv = priv;
391 return devm_counter_register(&pdev->dev, &priv->counter);
394 static const struct of_device_id mchp_tc_dt_ids[] = {
395 { .compatible = "microchip,tcb-capture", },
398 MODULE_DEVICE_TABLE(of, mchp_tc_dt_ids);
400 static struct platform_driver mchp_tc_driver = {
401 .probe = mchp_tc_probe,
403 .name = "microchip-tcb-capture",
404 .of_match_table = mchp_tc_dt_ids,
407 module_platform_driver(mchp_tc_driver);
409 MODULE_AUTHOR("Kamel Bouhara <kamel.bouhara@bootlin.com>");
410 MODULE_DESCRIPTION("Microchip TCB Capture driver");
411 MODULE_LICENSE("GPL v2");