1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2019 David Lechner <david@lechnology.com>
5 * Counter driver for Texas Instruments Enhanced Quadrature Encoder Pulse (eQEP)
8 #include <linux/bitops.h>
9 #include <linux/counter.h>
10 #include <linux/kernel.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
17 /* 32-bit registers */
28 /* 16-bit registers */
29 #define QWDTMR 0x0 /* 0x24 */
30 #define QWDPRD 0x2 /* 0x26 */
31 #define QDECCTL 0x4 /* 0x28 */
32 #define QEPCTL 0x6 /* 0x2a */
33 #define QCAPCTL 0x8 /* 0x2c */
34 #define QPOSCTL 0xa /* 0x2e */
35 #define QEINT 0xc /* 0x30 */
36 #define QFLG 0xe /* 0x32 */
37 #define QCLR 0x10 /* 0x34 */
38 #define QFRC 0x12 /* 0x36 */
39 #define QEPSTS 0x14 /* 0x38 */
40 #define QCTMR 0x16 /* 0x3a */
41 #define QCPRD 0x18 /* 0x3c */
42 #define QCTMRLAT 0x1a /* 0x3e */
43 #define QCPRDLAT 0x1c /* 0x40 */
45 #define QDECCTL_QSRC_SHIFT 14
46 #define QDECCTL_QSRC GENMASK(15, 14)
47 #define QDECCTL_SOEN BIT(13)
48 #define QDECCTL_SPSEL BIT(12)
49 #define QDECCTL_XCR BIT(11)
50 #define QDECCTL_SWAP BIT(10)
51 #define QDECCTL_IGATE BIT(9)
52 #define QDECCTL_QAP BIT(8)
53 #define QDECCTL_QBP BIT(7)
54 #define QDECCTL_QIP BIT(6)
55 #define QDECCTL_QSP BIT(5)
57 #define QEPCTL_FREE_SOFT GENMASK(15, 14)
58 #define QEPCTL_PCRM GENMASK(13, 12)
59 #define QEPCTL_SEI GENMASK(11, 10)
60 #define QEPCTL_IEI GENMASK(9, 8)
61 #define QEPCTL_SWI BIT(7)
62 #define QEPCTL_SEL BIT(6)
63 #define QEPCTL_IEL GENMASK(5, 4)
64 #define QEPCTL_PHEN BIT(3)
65 #define QEPCTL_QCLM BIT(2)
66 #define QEPCTL_UTE BIT(1)
67 #define QEPCTL_WDE BIT(0)
71 TI_EQEP_SIGNAL_QEPA
, /* QEPA/XCLK */
72 TI_EQEP_SIGNAL_QEPB
, /* QEPB/XDIR */
75 /* Position Counter Input Modes */
77 TI_EQEP_COUNT_FUNC_QUAD_COUNT
,
78 TI_EQEP_COUNT_FUNC_DIR_COUNT
,
79 TI_EQEP_COUNT_FUNC_UP_COUNT
,
80 TI_EQEP_COUNT_FUNC_DOWN_COUNT
,
84 TI_EQEP_SYNAPSE_ACTION_BOTH_EDGES
,
85 TI_EQEP_SYNAPSE_ACTION_RISING_EDGE
,
86 TI_EQEP_SYNAPSE_ACTION_NONE
,
90 struct counter_device counter
;
91 struct regmap
*regmap32
;
92 struct regmap
*regmap16
;
95 static int ti_eqep_count_read(struct counter_device
*counter
,
96 struct counter_count
*count
, unsigned long *val
)
98 struct ti_eqep_cnt
*priv
= counter
->priv
;
101 regmap_read(priv
->regmap32
, QPOSCNT
, &cnt
);
107 static int ti_eqep_count_write(struct counter_device
*counter
,
108 struct counter_count
*count
, unsigned long val
)
110 struct ti_eqep_cnt
*priv
= counter
->priv
;
113 regmap_read(priv
->regmap32
, QPOSMAX
, &max
);
117 return regmap_write(priv
->regmap32
, QPOSCNT
, val
);
120 static int ti_eqep_function_get(struct counter_device
*counter
,
121 struct counter_count
*count
, size_t *function
)
123 struct ti_eqep_cnt
*priv
= counter
->priv
;
126 regmap_read(priv
->regmap16
, QDECCTL
, &qdecctl
);
127 *function
= (qdecctl
& QDECCTL_QSRC
) >> QDECCTL_QSRC_SHIFT
;
132 static int ti_eqep_function_set(struct counter_device
*counter
,
133 struct counter_count
*count
, size_t function
)
135 struct ti_eqep_cnt
*priv
= counter
->priv
;
137 return regmap_write_bits(priv
->regmap16
, QDECCTL
, QDECCTL_QSRC
,
138 function
<< QDECCTL_QSRC_SHIFT
);
141 static int ti_eqep_action_get(struct counter_device
*counter
,
142 struct counter_count
*count
,
143 struct counter_synapse
*synapse
, size_t *action
)
145 struct ti_eqep_cnt
*priv
= counter
->priv
;
150 err
= ti_eqep_function_get(counter
, count
, &function
);
155 case TI_EQEP_COUNT_FUNC_QUAD_COUNT
:
156 /* In quadrature mode, the rising and falling edge of both
157 * QEPA and QEPB trigger QCLK.
159 *action
= TI_EQEP_SYNAPSE_ACTION_BOTH_EDGES
;
161 case TI_EQEP_COUNT_FUNC_DIR_COUNT
:
162 /* In direction-count mode only rising edge of QEPA is counted
163 * and QEPB gives direction.
165 switch (synapse
->signal
->id
) {
166 case TI_EQEP_SIGNAL_QEPA
:
167 *action
= TI_EQEP_SYNAPSE_ACTION_RISING_EDGE
;
170 *action
= TI_EQEP_SYNAPSE_ACTION_NONE
;
174 case TI_EQEP_COUNT_FUNC_UP_COUNT
:
175 case TI_EQEP_COUNT_FUNC_DOWN_COUNT
:
176 /* In up/down-count modes only QEPA is counted and QEPB is not
179 switch (synapse
->signal
->id
) {
180 case TI_EQEP_SIGNAL_QEPA
:
181 err
= regmap_read(priv
->regmap16
, QDECCTL
, &qdecctl
);
185 if (qdecctl
& QDECCTL_XCR
)
186 *action
= TI_EQEP_SYNAPSE_ACTION_BOTH_EDGES
;
188 *action
= TI_EQEP_SYNAPSE_ACTION_RISING_EDGE
;
191 *action
= TI_EQEP_SYNAPSE_ACTION_NONE
;
200 static const struct counter_ops ti_eqep_counter_ops
= {
201 .count_read
= ti_eqep_count_read
,
202 .count_write
= ti_eqep_count_write
,
203 .function_get
= ti_eqep_function_get
,
204 .function_set
= ti_eqep_function_set
,
205 .action_get
= ti_eqep_action_get
,
208 static ssize_t
ti_eqep_position_ceiling_read(struct counter_device
*counter
,
209 struct counter_count
*count
,
210 void *ext_priv
, char *buf
)
212 struct ti_eqep_cnt
*priv
= counter
->priv
;
215 regmap_read(priv
->regmap32
, QPOSMAX
, &qposmax
);
217 return sprintf(buf
, "%u\n", qposmax
);
220 static ssize_t
ti_eqep_position_ceiling_write(struct counter_device
*counter
,
221 struct counter_count
*count
,
222 void *ext_priv
, const char *buf
,
225 struct ti_eqep_cnt
*priv
= counter
->priv
;
229 err
= kstrtouint(buf
, 0, &res
);
233 regmap_write(priv
->regmap32
, QPOSMAX
, res
);
238 static ssize_t
ti_eqep_position_floor_read(struct counter_device
*counter
,
239 struct counter_count
*count
,
240 void *ext_priv
, char *buf
)
242 struct ti_eqep_cnt
*priv
= counter
->priv
;
245 regmap_read(priv
->regmap32
, QPOSINIT
, &qposinit
);
247 return sprintf(buf
, "%u\n", qposinit
);
250 static ssize_t
ti_eqep_position_floor_write(struct counter_device
*counter
,
251 struct counter_count
*count
,
252 void *ext_priv
, const char *buf
,
255 struct ti_eqep_cnt
*priv
= counter
->priv
;
259 err
= kstrtouint(buf
, 0, &res
);
263 regmap_write(priv
->regmap32
, QPOSINIT
, res
);
268 static ssize_t
ti_eqep_position_enable_read(struct counter_device
*counter
,
269 struct counter_count
*count
,
270 void *ext_priv
, char *buf
)
272 struct ti_eqep_cnt
*priv
= counter
->priv
;
275 regmap_read(priv
->regmap16
, QEPCTL
, &qepctl
);
277 return sprintf(buf
, "%u\n", !!(qepctl
& QEPCTL_PHEN
));
280 static ssize_t
ti_eqep_position_enable_write(struct counter_device
*counter
,
281 struct counter_count
*count
,
282 void *ext_priv
, const char *buf
,
285 struct ti_eqep_cnt
*priv
= counter
->priv
;
289 err
= kstrtobool(buf
, &res
);
293 regmap_write_bits(priv
->regmap16
, QEPCTL
, QEPCTL_PHEN
, res
? -1 : 0);
298 static struct counter_count_ext ti_eqep_position_ext
[] = {
301 .read
= ti_eqep_position_ceiling_read
,
302 .write
= ti_eqep_position_ceiling_write
,
306 .read
= ti_eqep_position_floor_read
,
307 .write
= ti_eqep_position_floor_write
,
311 .read
= ti_eqep_position_enable_read
,
312 .write
= ti_eqep_position_enable_write
,
316 static struct counter_signal ti_eqep_signals
[] = {
317 [TI_EQEP_SIGNAL_QEPA
] = {
318 .id
= TI_EQEP_SIGNAL_QEPA
,
321 [TI_EQEP_SIGNAL_QEPB
] = {
322 .id
= TI_EQEP_SIGNAL_QEPB
,
327 static const enum counter_count_function ti_eqep_position_functions
[] = {
328 [TI_EQEP_COUNT_FUNC_QUAD_COUNT
] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4
,
329 [TI_EQEP_COUNT_FUNC_DIR_COUNT
] = COUNTER_COUNT_FUNCTION_PULSE_DIRECTION
,
330 [TI_EQEP_COUNT_FUNC_UP_COUNT
] = COUNTER_COUNT_FUNCTION_INCREASE
,
331 [TI_EQEP_COUNT_FUNC_DOWN_COUNT
] = COUNTER_COUNT_FUNCTION_DECREASE
,
334 static const enum counter_synapse_action ti_eqep_position_synapse_actions
[] = {
335 [TI_EQEP_SYNAPSE_ACTION_BOTH_EDGES
] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES
,
336 [TI_EQEP_SYNAPSE_ACTION_RISING_EDGE
] = COUNTER_SYNAPSE_ACTION_RISING_EDGE
,
337 [TI_EQEP_SYNAPSE_ACTION_NONE
] = COUNTER_SYNAPSE_ACTION_NONE
,
340 static struct counter_synapse ti_eqep_position_synapses
[] = {
342 .actions_list
= ti_eqep_position_synapse_actions
,
343 .num_actions
= ARRAY_SIZE(ti_eqep_position_synapse_actions
),
344 .signal
= &ti_eqep_signals
[TI_EQEP_SIGNAL_QEPA
],
347 .actions_list
= ti_eqep_position_synapse_actions
,
348 .num_actions
= ARRAY_SIZE(ti_eqep_position_synapse_actions
),
349 .signal
= &ti_eqep_signals
[TI_EQEP_SIGNAL_QEPB
],
353 static struct counter_count ti_eqep_counts
[] = {
357 .functions_list
= ti_eqep_position_functions
,
358 .num_functions
= ARRAY_SIZE(ti_eqep_position_functions
),
359 .synapses
= ti_eqep_position_synapses
,
360 .num_synapses
= ARRAY_SIZE(ti_eqep_position_synapses
),
361 .ext
= ti_eqep_position_ext
,
362 .num_ext
= ARRAY_SIZE(ti_eqep_position_ext
),
366 static const struct regmap_config ti_eqep_regmap32_config
= {
371 .max_register
= QUPRD
,
374 static const struct regmap_config ti_eqep_regmap16_config
= {
379 .max_register
= QCPRDLAT
,
382 static int ti_eqep_probe(struct platform_device
*pdev
)
384 struct device
*dev
= &pdev
->dev
;
385 struct ti_eqep_cnt
*priv
;
389 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
393 base
= devm_platform_ioremap_resource(pdev
, 0);
395 return PTR_ERR(base
);
397 priv
->regmap32
= devm_regmap_init_mmio(dev
, base
,
398 &ti_eqep_regmap32_config
);
399 if (IS_ERR(priv
->regmap32
))
400 return PTR_ERR(priv
->regmap32
);
402 priv
->regmap16
= devm_regmap_init_mmio(dev
, base
+ 0x24,
403 &ti_eqep_regmap16_config
);
404 if (IS_ERR(priv
->regmap16
))
405 return PTR_ERR(priv
->regmap16
);
407 priv
->counter
.name
= dev_name(dev
);
408 priv
->counter
.parent
= dev
;
409 priv
->counter
.ops
= &ti_eqep_counter_ops
;
410 priv
->counter
.counts
= ti_eqep_counts
;
411 priv
->counter
.num_counts
= ARRAY_SIZE(ti_eqep_counts
);
412 priv
->counter
.signals
= ti_eqep_signals
;
413 priv
->counter
.num_signals
= ARRAY_SIZE(ti_eqep_signals
);
414 priv
->counter
.priv
= priv
;
416 platform_set_drvdata(pdev
, priv
);
419 * Need to make sure power is turned on. On AM33xx, this comes from the
420 * parent PWMSS bus driver. On AM17xx, this comes from the PSC power
423 pm_runtime_enable(dev
);
424 pm_runtime_get_sync(dev
);
426 err
= counter_register(&priv
->counter
);
428 pm_runtime_put_sync(dev
);
429 pm_runtime_disable(dev
);
436 static int ti_eqep_remove(struct platform_device
*pdev
)
438 struct ti_eqep_cnt
*priv
= platform_get_drvdata(pdev
);
439 struct device
*dev
= &pdev
->dev
;
441 counter_unregister(&priv
->counter
);
442 pm_runtime_put_sync(dev
);
443 pm_runtime_disable(dev
);
448 static const struct of_device_id ti_eqep_of_match
[] = {
449 { .compatible
= "ti,am3352-eqep", },
452 MODULE_DEVICE_TABLE(of
, ti_eqep_of_match
);
454 static struct platform_driver ti_eqep_driver
= {
455 .probe
= ti_eqep_probe
,
456 .remove
= ti_eqep_remove
,
458 .name
= "ti-eqep-cnt",
459 .of_match_table
= ti_eqep_of_match
,
462 module_platform_driver(ti_eqep_driver
);
464 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
465 MODULE_DESCRIPTION("TI eQEP counter driver");
466 MODULE_LICENSE("GPL v2");