ptp: add debugfs support for ptp_qoriq
[linux-2.6-block.git] / drivers / ptp / ptp_qoriq_debugfs.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright 2019 NXP
3  */
4 #include <linux/device.h>
5 #include <linux/debugfs.h>
6 #include <linux/fsl/ptp_qoriq.h>
7
8 static int ptp_qoriq_fiper1_lpbk_get(void *data, u64 *val)
9 {
10         struct qoriq_ptp *qoriq_ptp = data;
11         struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
12         u32 ctrl;
13
14         ctrl = qoriq_read(&regs->ctrl_regs->tmr_ctrl);
15         *val = ctrl & PP1L ? 1 : 0;
16
17         return 0;
18 }
19
20 static int ptp_qoriq_fiper1_lpbk_set(void *data, u64 val)
21 {
22         struct qoriq_ptp *qoriq_ptp = data;
23         struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
24         u32 ctrl;
25
26         ctrl = qoriq_read(&regs->ctrl_regs->tmr_ctrl);
27         if (val == 0)
28                 ctrl &= ~PP1L;
29         else
30                 ctrl |= PP1L;
31
32         qoriq_write(&regs->ctrl_regs->tmr_ctrl, ctrl);
33         return 0;
34 }
35
36 DEFINE_SIMPLE_ATTRIBUTE(ptp_qoriq_fiper1_fops, ptp_qoriq_fiper1_lpbk_get,
37                         ptp_qoriq_fiper1_lpbk_set, "%llu\n");
38
39 static int ptp_qoriq_fiper2_lpbk_get(void *data, u64 *val)
40 {
41         struct qoriq_ptp *qoriq_ptp = data;
42         struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
43         u32 ctrl;
44
45         ctrl = qoriq_read(&regs->ctrl_regs->tmr_ctrl);
46         *val = ctrl & PP2L ? 1 : 0;
47
48         return 0;
49 }
50
51 static int ptp_qoriq_fiper2_lpbk_set(void *data, u64 val)
52 {
53         struct qoriq_ptp *qoriq_ptp = data;
54         struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
55         u32 ctrl;
56
57         ctrl = qoriq_read(&regs->ctrl_regs->tmr_ctrl);
58         if (val == 0)
59                 ctrl &= ~PP2L;
60         else
61                 ctrl |= PP2L;
62
63         qoriq_write(&regs->ctrl_regs->tmr_ctrl, ctrl);
64         return 0;
65 }
66
67 DEFINE_SIMPLE_ATTRIBUTE(ptp_qoriq_fiper2_fops, ptp_qoriq_fiper2_lpbk_get,
68                         ptp_qoriq_fiper2_lpbk_set, "%llu\n");
69
70 void ptp_qoriq_create_debugfs(struct qoriq_ptp *qoriq_ptp)
71 {
72         struct dentry *root;
73
74         root = debugfs_create_dir(dev_name(qoriq_ptp->dev), NULL);
75         if (IS_ERR(root))
76                 return;
77         if (!root)
78                 goto err_root;
79
80         qoriq_ptp->debugfs_root = root;
81
82         if (!debugfs_create_file("fiper1-loopback", 0600, root, qoriq_ptp,
83                                  &ptp_qoriq_fiper1_fops))
84                 goto err_node;
85         if (!debugfs_create_file("fiper2-loopback", 0600, root, qoriq_ptp,
86                                  &ptp_qoriq_fiper2_fops))
87                 goto err_node;
88         return;
89
90 err_node:
91         debugfs_remove_recursive(root);
92         qoriq_ptp->debugfs_root = NULL;
93 err_root:
94         dev_err(qoriq_ptp->dev, "failed to initialize debugfs\n");
95 }
96
97 void ptp_qoriq_remove_debugfs(struct qoriq_ptp *qoriq_ptp)
98 {
99         debugfs_remove_recursive(qoriq_ptp->debugfs_root);
100         qoriq_ptp->debugfs_root = NULL;
101 }