1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2013-2016 Freescale Semiconductor Inc.
4  * Copyright 2016-2018 NXP
5  */
6 
7 #include <linux/fsl/mc.h>
8 
9 #include "dprtc.h"
10 #include "dprtc-cmd.h"
11 
12 /**
13  * dprtc_open() - Open a control session for the specified object.
14  * @mc_io:	Pointer to MC portal's I/O object
15  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
16  * @dprtc_id:	DPRTC unique ID
17  * @token:	Returned token; use in subsequent API calls
18  *
19  * This function can be used to open a control session for an
20  * already created object; an object may have been declared in
21  * the DPL or by calling the dprtc_create function.
22  * This function returns a unique authentication token,
23  * associated with the specific object ID and the specific MC
24  * portal; this token must be used in all subsequent commands for
25  * this specific object
26  *
27  * Return:	'0' on Success; Error code otherwise.
28  */
29 int dprtc_open(struct fsl_mc_io *mc_io,
30 	       u32 cmd_flags,
31 	       int dprtc_id,
32 	       u16 *token)
33 {
34 	struct dprtc_cmd_open *cmd_params;
35 	struct fsl_mc_command cmd = { 0 };
36 	int err;
37 
38 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_OPEN,
39 					  cmd_flags,
40 					  0);
41 	cmd_params = (struct dprtc_cmd_open *)cmd.params;
42 	cmd_params->dprtc_id = cpu_to_le32(dprtc_id);
43 
44 	err = mc_send_command(mc_io, &cmd);
45 	if (err)
46 		return err;
47 
48 	*token = mc_cmd_hdr_read_token(&cmd);
49 
50 	return 0;
51 }
52 
53 /**
54  * dprtc_close() - Close the control session of the object
55  * @mc_io:	Pointer to MC portal's I/O object
56  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
57  * @token:	Token of DPRTC object
58  *
59  * After this function is called, no further operations are
60  * allowed on the object without opening a new control session.
61  *
62  * Return:	'0' on Success; Error code otherwise.
63  */
64 int dprtc_close(struct fsl_mc_io *mc_io,
65 		u32 cmd_flags,
66 		u16 token)
67 {
68 	struct fsl_mc_command cmd = { 0 };
69 
70 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CLOSE, cmd_flags,
71 					  token);
72 
73 	return mc_send_command(mc_io, &cmd);
74 }
75 
76 /**
77  * dprtc_set_freq_compensation() - Sets a new frequency compensation value.
78  *
79  * @mc_io:		Pointer to MC portal's I/O object
80  * @cmd_flags:		Command flags; one or more of 'MC_CMD_FLAG_'
81  * @token:		Token of DPRTC object
82  * @freq_compensation:	The new frequency compensation value to set.
83  *
84  * Return:	'0' on Success; Error code otherwise.
85  */
86 int dprtc_set_freq_compensation(struct fsl_mc_io *mc_io,
87 				u32 cmd_flags,
88 				u16 token,
89 				u32 freq_compensation)
90 {
91 	struct dprtc_get_freq_compensation *cmd_params;
92 	struct fsl_mc_command cmd = { 0 };
93 
94 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_FREQ_COMPENSATION,
95 					  cmd_flags,
96 					  token);
97 	cmd_params = (struct dprtc_get_freq_compensation *)cmd.params;
98 	cmd_params->freq_compensation = cpu_to_le32(freq_compensation);
99 
100 	return mc_send_command(mc_io, &cmd);
101 }
102 
103 /**
104  * dprtc_get_freq_compensation() - Retrieves the frequency compensation value
105  *
106  * @mc_io:		Pointer to MC portal's I/O object
107  * @cmd_flags:		Command flags; one or more of 'MC_CMD_FLAG_'
108  * @token:		Token of DPRTC object
109  * @freq_compensation:	Frequency compensation value
110  *
111  * Return:	'0' on Success; Error code otherwise.
112  */
113 int dprtc_get_freq_compensation(struct fsl_mc_io *mc_io,
114 				u32 cmd_flags,
115 				u16 token,
116 				u32 *freq_compensation)
117 {
118 	struct dprtc_get_freq_compensation *rsp_params;
119 	struct fsl_mc_command cmd = { 0 };
120 	int err;
121 
122 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_FREQ_COMPENSATION,
123 					  cmd_flags,
124 					  token);
125 
126 	err = mc_send_command(mc_io, &cmd);
127 	if (err)
128 		return err;
129 
130 	rsp_params = (struct dprtc_get_freq_compensation *)cmd.params;
131 	*freq_compensation = le32_to_cpu(rsp_params->freq_compensation);
132 
133 	return 0;
134 }
135 
136 /**
137  * dprtc_get_time() - Returns the current RTC time.
138  *
139  * @mc_io:	Pointer to MC portal's I/O object
140  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
141  * @token:	Token of DPRTC object
142  * @time:	Current RTC time.
143  *
144  * Return:	'0' on Success; Error code otherwise.
145  */
146 int dprtc_get_time(struct fsl_mc_io *mc_io,
147 		   u32 cmd_flags,
148 		   u16 token,
149 		   uint64_t *time)
150 {
151 	struct dprtc_time *rsp_params;
152 	struct fsl_mc_command cmd = { 0 };
153 	int err;
154 
155 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_TIME,
156 					  cmd_flags,
157 					  token);
158 
159 	err = mc_send_command(mc_io, &cmd);
160 	if (err)
161 		return err;
162 
163 	rsp_params = (struct dprtc_time *)cmd.params;
164 	*time = le64_to_cpu(rsp_params->time);
165 
166 	return 0;
167 }
168 
169 /**
170  * dprtc_set_time() - Updates current RTC time.
171  *
172  * @mc_io:	Pointer to MC portal's I/O object
173  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
174  * @token:	Token of DPRTC object
175  * @time:	New RTC time.
176  *
177  * Return:	'0' on Success; Error code otherwise.
178  */
179 int dprtc_set_time(struct fsl_mc_io *mc_io,
180 		   u32 cmd_flags,
181 		   u16 token,
182 		   uint64_t time)
183 {
184 	struct dprtc_time *cmd_params;
185 	struct fsl_mc_command cmd = { 0 };
186 
187 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_TIME,
188 					  cmd_flags,
189 					  token);
190 	cmd_params = (struct dprtc_time *)cmd.params;
191 	cmd_params->time = cpu_to_le64(time);
192 
193 	return mc_send_command(mc_io, &cmd);
194 }
195