1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 Microchip
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;
35 static const enum counter_function mchp_tc_count_functions[] = {
36 COUNTER_FUNCTION_INCREASE,
37 COUNTER_FUNCTION_QUADRATURE_X4,
40 static const enum counter_synapse_action mchp_tc_synapse_actions[] = {
41 COUNTER_SYNAPSE_ACTION_NONE,
42 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
43 COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
44 COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
47 static struct counter_signal mchp_tc_count_signals[] = {
58 static struct counter_synapse mchp_tc_count_synapses[] = {
60 .actions_list = mchp_tc_synapse_actions,
61 .num_actions = ARRAY_SIZE(mchp_tc_synapse_actions),
62 .signal = &mchp_tc_count_signals[0]
65 .actions_list = mchp_tc_synapse_actions,
66 .num_actions = ARRAY_SIZE(mchp_tc_synapse_actions),
67 .signal = &mchp_tc_count_signals[1]
71 static int mchp_tc_count_function_read(struct counter_device *counter,
72 struct counter_count *count,
73 enum counter_function *function)
75 struct mchp_tc_data *const priv = counter->priv;
78 *function = COUNTER_FUNCTION_QUADRATURE_X4;
80 *function = COUNTER_FUNCTION_INCREASE;
85 static int mchp_tc_count_function_write(struct counter_device *counter,
86 struct counter_count *count,
87 enum counter_function function)
89 struct mchp_tc_data *const priv = counter->priv;
92 regmap_read(priv->regmap, ATMEL_TC_BMR, &bmr);
93 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
95 /* Set capture mode */
96 cmr &= ~ATMEL_TC_WAVE;
99 case COUNTER_FUNCTION_INCREASE:
101 /* Set highest rate based on whether soc has gclk or not */
102 bmr &= ~(ATMEL_TC_QDEN | ATMEL_TC_POSEN);
103 if (priv->tc_cfg->has_gclk)
104 cmr |= ATMEL_TC_TIMER_CLOCK2;
106 cmr |= ATMEL_TC_TIMER_CLOCK1;
107 /* Setup the period capture mode */
108 cmr |= ATMEL_TC_CMR_MASK;
109 cmr &= ~(ATMEL_TC_ABETRG | ATMEL_TC_XC0);
111 case COUNTER_FUNCTION_QUADRATURE_X4:
112 if (!priv->tc_cfg->has_qdec)
114 /* In QDEC mode settings both channels 0 and 1 are required */
115 if (priv->num_channels < 2 || priv->channel[0] != 0 ||
116 priv->channel[1] != 1) {
117 pr_err("Invalid channels number or id for quadrature mode\n");
121 bmr |= ATMEL_TC_QDEN | ATMEL_TC_POSEN;
122 cmr |= ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_ABETRG | ATMEL_TC_XC0;
125 /* should never reach this path */
129 regmap_write(priv->regmap, ATMEL_TC_BMR, bmr);
130 regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), cmr);
132 /* Enable clock and trigger counter */
133 regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], CCR),
134 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG);
136 if (priv->qdec_mode) {
137 regmap_write(priv->regmap,
138 ATMEL_TC_REG(priv->channel[1], CMR), cmr);
139 regmap_write(priv->regmap,
140 ATMEL_TC_REG(priv->channel[1], CCR),
141 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG);
147 static int mchp_tc_count_signal_read(struct counter_device *counter,
148 struct counter_signal *signal,
149 enum counter_signal_level *lvl)
151 struct mchp_tc_data *const priv = counter->priv;
155 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
157 if (priv->trig_inverted)
158 sigstatus = (sr & ATMEL_TC_MTIOB);
160 sigstatus = (sr & ATMEL_TC_MTIOA);
162 *lvl = sigstatus ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
167 static int mchp_tc_count_action_read(struct counter_device *counter,
168 struct counter_count *count,
169 struct counter_synapse *synapse,
170 enum counter_synapse_action *action)
172 struct mchp_tc_data *const priv = counter->priv;
175 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
177 switch (cmr & ATMEL_TC_ETRGEDG) {
179 *action = COUNTER_SYNAPSE_ACTION_NONE;
181 case ATMEL_TC_ETRGEDG_RISING:
182 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
184 case ATMEL_TC_ETRGEDG_FALLING:
185 *action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
187 case ATMEL_TC_ETRGEDG_BOTH:
188 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
195 static int mchp_tc_count_action_write(struct counter_device *counter,
196 struct counter_count *count,
197 struct counter_synapse *synapse,
198 enum counter_synapse_action action)
200 struct mchp_tc_data *const priv = counter->priv;
201 u32 edge = ATMEL_TC_ETRGEDG_NONE;
203 /* QDEC mode is rising edge only */
208 case COUNTER_SYNAPSE_ACTION_NONE:
209 edge = ATMEL_TC_ETRGEDG_NONE;
211 case COUNTER_SYNAPSE_ACTION_RISING_EDGE:
212 edge = ATMEL_TC_ETRGEDG_RISING;
214 case COUNTER_SYNAPSE_ACTION_FALLING_EDGE:
215 edge = ATMEL_TC_ETRGEDG_FALLING;
217 case COUNTER_SYNAPSE_ACTION_BOTH_EDGES:
218 edge = ATMEL_TC_ETRGEDG_BOTH;
221 /* should never reach this path */
225 return regmap_write_bits(priv->regmap,
226 ATMEL_TC_REG(priv->channel[0], CMR),
227 ATMEL_TC_ETRGEDG, edge);
230 static int mchp_tc_count_read(struct counter_device *counter,
231 struct counter_count *count, u64 *val)
233 struct mchp_tc_data *const priv = counter->priv;
236 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CV), &cnt);
242 static struct counter_count mchp_tc_counts[] = {
245 .name = "Timer Counter",
246 .functions_list = mchp_tc_count_functions,
247 .num_functions = ARRAY_SIZE(mchp_tc_count_functions),
248 .synapses = mchp_tc_count_synapses,
249 .num_synapses = ARRAY_SIZE(mchp_tc_count_synapses),
253 static const struct counter_ops mchp_tc_ops = {
254 .signal_read = mchp_tc_count_signal_read,
255 .count_read = mchp_tc_count_read,
256 .function_read = mchp_tc_count_function_read,
257 .function_write = mchp_tc_count_function_write,
258 .action_read = mchp_tc_count_action_read,
259 .action_write = mchp_tc_count_action_write
262 static const struct atmel_tcb_config tcb_rm9200_config = {
266 static const struct atmel_tcb_config tcb_sam9x5_config = {
270 static const struct atmel_tcb_config tcb_sama5d2_config = {
276 static const struct atmel_tcb_config tcb_sama5d3_config = {
281 static const struct of_device_id atmel_tc_of_match[] = {
282 { .compatible = "atmel,at91rm9200-tcb", .data = &tcb_rm9200_config, },
283 { .compatible = "atmel,at91sam9x5-tcb", .data = &tcb_sam9x5_config, },
284 { .compatible = "atmel,sama5d2-tcb", .data = &tcb_sama5d2_config, },
285 { .compatible = "atmel,sama5d3-tcb", .data = &tcb_sama5d3_config, },
289 static void mchp_tc_clk_remove(void *ptr)
291 clk_disable_unprepare((struct clk *)ptr);
294 static int mchp_tc_probe(struct platform_device *pdev)
296 struct device_node *np = pdev->dev.of_node;
297 const struct atmel_tcb_config *tcb_config;
298 const struct of_device_id *match;
299 struct mchp_tc_data *priv;
301 struct regmap *regmap;
306 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
310 platform_set_drvdata(pdev, priv);
312 match = of_match_node(atmel_tc_of_match, np->parent);
313 tcb_config = match->data;
315 dev_err(&pdev->dev, "No matching parent node found\n");
319 regmap = syscon_node_to_regmap(np->parent);
321 return PTR_ERR(regmap);
323 /* max. channels number is 2 when in QDEC mode */
324 priv->num_channels = of_property_count_u32_elems(np, "reg");
325 if (priv->num_channels < 0) {
326 dev_err(&pdev->dev, "Invalid or missing channel\n");
330 /* Register channels and initialize clocks */
331 for (i = 0; i < priv->num_channels; i++) {
332 ret = of_property_read_u32_index(np, "reg", i, &channel);
333 if (ret < 0 || channel > 2)
336 priv->channel[i] = channel;
338 snprintf(clk_name, sizeof(clk_name), "t%d_clk", channel);
340 clk[i] = of_clk_get_by_name(np->parent, clk_name);
341 if (IS_ERR(clk[i])) {
342 /* Fallback to t0_clk */
343 clk[i] = of_clk_get_by_name(np->parent, "t0_clk");
345 return PTR_ERR(clk[i]);
348 ret = clk_prepare_enable(clk[i]);
352 ret = devm_add_action_or_reset(&pdev->dev,
359 "Initialized capture mode on channel %d\n",
363 priv->tc_cfg = tcb_config;
364 priv->regmap = regmap;
365 priv->counter.name = dev_name(&pdev->dev);
366 priv->counter.parent = &pdev->dev;
367 priv->counter.ops = &mchp_tc_ops;
368 priv->counter.num_counts = ARRAY_SIZE(mchp_tc_counts);
369 priv->counter.counts = mchp_tc_counts;
370 priv->counter.num_signals = ARRAY_SIZE(mchp_tc_count_signals);
371 priv->counter.signals = mchp_tc_count_signals;
372 priv->counter.priv = priv;
374 return devm_counter_register(&pdev->dev, &priv->counter);
377 static const struct of_device_id mchp_tc_dt_ids[] = {
378 { .compatible = "microchip,tcb-capture", },
381 MODULE_DEVICE_TABLE(of, mchp_tc_dt_ids);
383 static struct platform_driver mchp_tc_driver = {
384 .probe = mchp_tc_probe,
386 .name = "microchip-tcb-capture",
387 .of_match_table = mchp_tc_dt_ids,
390 module_platform_driver(mchp_tc_driver);
393 MODULE_DESCRIPTION("Microchip TCB Capture driver");
394 MODULE_LICENSE("GPL v2");