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 regmap *regmap;
34 static const enum counter_function mchp_tc_count_functions[] = {
35 COUNTER_FUNCTION_INCREASE,
36 COUNTER_FUNCTION_QUADRATURE_X4,
39 static const enum counter_synapse_action mchp_tc_synapse_actions[] = {
40 COUNTER_SYNAPSE_ACTION_NONE,
41 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
42 COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
43 COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
46 static struct counter_signal mchp_tc_count_signals[] = {
57 static struct counter_synapse mchp_tc_count_synapses[] = {
59 .actions_list = mchp_tc_synapse_actions,
60 .num_actions = ARRAY_SIZE(mchp_tc_synapse_actions),
61 .signal = &mchp_tc_count_signals[0]
64 .actions_list = mchp_tc_synapse_actions,
65 .num_actions = ARRAY_SIZE(mchp_tc_synapse_actions),
66 .signal = &mchp_tc_count_signals[1]
70 static int mchp_tc_count_function_read(struct counter_device *counter,
71 struct counter_count *count,
72 enum counter_function *function)
74 struct mchp_tc_data *const priv = counter_priv(counter);
77 *function = COUNTER_FUNCTION_QUADRATURE_X4;
79 *function = COUNTER_FUNCTION_INCREASE;
84 static int mchp_tc_count_function_write(struct counter_device *counter,
85 struct counter_count *count,
86 enum counter_function function)
88 struct mchp_tc_data *const priv = counter_priv(counter);
91 regmap_read(priv->regmap, ATMEL_TC_BMR, &bmr);
92 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
94 /* Set capture mode */
95 cmr &= ~ATMEL_TC_WAVE;
98 case COUNTER_FUNCTION_INCREASE:
100 /* Set highest rate based on whether soc has gclk or not */
101 bmr &= ~(ATMEL_TC_QDEN | ATMEL_TC_POSEN);
102 if (priv->tc_cfg->has_gclk)
103 cmr |= ATMEL_TC_TIMER_CLOCK2;
105 cmr |= ATMEL_TC_TIMER_CLOCK1;
106 /* Setup the period capture mode */
107 cmr |= ATMEL_TC_CMR_MASK;
108 cmr &= ~(ATMEL_TC_ABETRG | ATMEL_TC_XC0);
110 case COUNTER_FUNCTION_QUADRATURE_X4:
111 if (!priv->tc_cfg->has_qdec)
113 /* In QDEC mode settings both channels 0 and 1 are required */
114 if (priv->num_channels < 2 || priv->channel[0] != 0 ||
115 priv->channel[1] != 1) {
116 pr_err("Invalid channels number or id for quadrature mode\n");
120 bmr |= ATMEL_TC_QDEN | ATMEL_TC_POSEN;
121 cmr |= ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_ABETRG | ATMEL_TC_XC0;
124 /* should never reach this path */
128 regmap_write(priv->regmap, ATMEL_TC_BMR, bmr);
129 regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), cmr);
131 /* Enable clock and trigger counter */
132 regmap_write(priv->regmap, ATMEL_TC_REG(priv->channel[0], CCR),
133 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG);
135 if (priv->qdec_mode) {
136 regmap_write(priv->regmap,
137 ATMEL_TC_REG(priv->channel[1], CMR), cmr);
138 regmap_write(priv->regmap,
139 ATMEL_TC_REG(priv->channel[1], CCR),
140 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG);
146 static int mchp_tc_count_signal_read(struct counter_device *counter,
147 struct counter_signal *signal,
148 enum counter_signal_level *lvl)
150 struct mchp_tc_data *const priv = counter_priv(counter);
154 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], SR), &sr);
156 if (priv->trig_inverted)
157 sigstatus = (sr & ATMEL_TC_MTIOB);
159 sigstatus = (sr & ATMEL_TC_MTIOA);
161 *lvl = sigstatus ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
166 static int mchp_tc_count_action_read(struct counter_device *counter,
167 struct counter_count *count,
168 struct counter_synapse *synapse,
169 enum counter_synapse_action *action)
171 struct mchp_tc_data *const priv = counter_priv(counter);
174 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
176 switch (cmr & ATMEL_TC_ETRGEDG) {
178 *action = COUNTER_SYNAPSE_ACTION_NONE;
180 case ATMEL_TC_ETRGEDG_RISING:
181 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
183 case ATMEL_TC_ETRGEDG_FALLING:
184 *action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
186 case ATMEL_TC_ETRGEDG_BOTH:
187 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
194 static int mchp_tc_count_action_write(struct counter_device *counter,
195 struct counter_count *count,
196 struct counter_synapse *synapse,
197 enum counter_synapse_action action)
199 struct mchp_tc_data *const priv = counter_priv(counter);
200 u32 edge = ATMEL_TC_ETRGEDG_NONE;
202 /* QDEC mode is rising edge only */
207 case COUNTER_SYNAPSE_ACTION_NONE:
208 edge = ATMEL_TC_ETRGEDG_NONE;
210 case COUNTER_SYNAPSE_ACTION_RISING_EDGE:
211 edge = ATMEL_TC_ETRGEDG_RISING;
213 case COUNTER_SYNAPSE_ACTION_FALLING_EDGE:
214 edge = ATMEL_TC_ETRGEDG_FALLING;
216 case COUNTER_SYNAPSE_ACTION_BOTH_EDGES:
217 edge = ATMEL_TC_ETRGEDG_BOTH;
220 /* should never reach this path */
224 return regmap_write_bits(priv->regmap,
225 ATMEL_TC_REG(priv->channel[0], CMR),
226 ATMEL_TC_ETRGEDG, edge);
229 static int mchp_tc_count_read(struct counter_device *counter,
230 struct counter_count *count, u64 *val)
232 struct mchp_tc_data *const priv = counter_priv(counter);
235 regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CV), &cnt);
241 static struct counter_count mchp_tc_counts[] = {
244 .name = "Timer Counter",
245 .functions_list = mchp_tc_count_functions,
246 .num_functions = ARRAY_SIZE(mchp_tc_count_functions),
247 .synapses = mchp_tc_count_synapses,
248 .num_synapses = ARRAY_SIZE(mchp_tc_count_synapses),
252 static const struct counter_ops mchp_tc_ops = {
253 .signal_read = mchp_tc_count_signal_read,
254 .count_read = mchp_tc_count_read,
255 .function_read = mchp_tc_count_function_read,
256 .function_write = mchp_tc_count_function_write,
257 .action_read = mchp_tc_count_action_read,
258 .action_write = mchp_tc_count_action_write
261 static const struct atmel_tcb_config tcb_rm9200_config = {
265 static const struct atmel_tcb_config tcb_sam9x5_config = {
269 static const struct atmel_tcb_config tcb_sama5d2_config = {
275 static const struct atmel_tcb_config tcb_sama5d3_config = {
280 static const struct of_device_id atmel_tc_of_match[] = {
281 { .compatible = "atmel,at91rm9200-tcb", .data = &tcb_rm9200_config, },
282 { .compatible = "atmel,at91sam9x5-tcb", .data = &tcb_sam9x5_config, },
283 { .compatible = "atmel,sama5d2-tcb", .data = &tcb_sama5d2_config, },
284 { .compatible = "atmel,sama5d3-tcb", .data = &tcb_sama5d3_config, },
288 static void mchp_tc_clk_remove(void *ptr)
290 clk_disable_unprepare((struct clk *)ptr);
293 static int mchp_tc_probe(struct platform_device *pdev)
295 struct device_node *np = pdev->dev.of_node;
296 const struct atmel_tcb_config *tcb_config;
297 const struct of_device_id *match;
298 struct counter_device *counter;
299 struct mchp_tc_data *priv;
301 struct regmap *regmap;
306 counter = devm_counter_alloc(&pdev->dev, sizeof(*priv));
309 priv = counter_priv(counter);
311 match = of_match_node(atmel_tc_of_match, np->parent);
312 tcb_config = match->data;
314 dev_err(&pdev->dev, "No matching parent node found\n");
318 regmap = syscon_node_to_regmap(np->parent);
320 return PTR_ERR(regmap);
322 /* max. channels number is 2 when in QDEC mode */
323 priv->num_channels = of_property_count_u32_elems(np, "reg");
324 if (priv->num_channels < 0) {
325 dev_err(&pdev->dev, "Invalid or missing channel\n");
329 /* Register channels and initialize clocks */
330 for (i = 0; i < priv->num_channels; i++) {
331 ret = of_property_read_u32_index(np, "reg", i, &channel);
332 if (ret < 0 || channel > 2)
335 priv->channel[i] = channel;
337 snprintf(clk_name, sizeof(clk_name), "t%d_clk", channel);
339 clk[i] = of_clk_get_by_name(np->parent, clk_name);
340 if (IS_ERR(clk[i])) {
341 /* Fallback to t0_clk */
342 clk[i] = of_clk_get_by_name(np->parent, "t0_clk");
344 return PTR_ERR(clk[i]);
347 ret = clk_prepare_enable(clk[i]);
351 ret = devm_add_action_or_reset(&pdev->dev,
358 "Initialized capture mode on channel %d\n",
362 priv->tc_cfg = tcb_config;
363 priv->regmap = regmap;
364 counter->name = dev_name(&pdev->dev);
365 counter->parent = &pdev->dev;
366 counter->ops = &mchp_tc_ops;
367 counter->num_counts = ARRAY_SIZE(mchp_tc_counts);
368 counter->counts = mchp_tc_counts;
369 counter->num_signals = ARRAY_SIZE(mchp_tc_count_signals);
370 counter->signals = mchp_tc_count_signals;
372 ret = devm_counter_add(&pdev->dev, counter);
374 return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
379 static const struct of_device_id mchp_tc_dt_ids[] = {
380 { .compatible = "microchip,tcb-capture", },
383 MODULE_DEVICE_TABLE(of, mchp_tc_dt_ids);
385 static struct platform_driver mchp_tc_driver = {
386 .probe = mchp_tc_probe,
388 .name = "microchip-tcb-capture",
389 .of_match_table = mchp_tc_dt_ids,
392 module_platform_driver(mchp_tc_driver);
394 MODULE_AUTHOR("Kamel Bouhara <kamel.bouhara@bootlin.com>");
395 MODULE_DESCRIPTION("Microchip TCB Capture driver");
396 MODULE_LICENSE("GPL v2");
397 MODULE_IMPORT_NS(COUNTER);