1d94ba80eSRichard Cochran /*
2d94ba80eSRichard Cochran  * PTP 1588 clock support
3d94ba80eSRichard Cochran  *
4d94ba80eSRichard Cochran  * Copyright (C) 2010 OMICRON electronics GmbH
5d94ba80eSRichard Cochran  *
6d94ba80eSRichard Cochran  *  This program is free software; you can redistribute it and/or modify
7d94ba80eSRichard Cochran  *  it under the terms of the GNU General Public License as published by
8d94ba80eSRichard Cochran  *  the Free Software Foundation; either version 2 of the License, or
9d94ba80eSRichard Cochran  *  (at your option) any later version.
10d94ba80eSRichard Cochran  *
11d94ba80eSRichard Cochran  *  This program is distributed in the hope that it will be useful,
12d94ba80eSRichard Cochran  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13d94ba80eSRichard Cochran  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14d94ba80eSRichard Cochran  *  GNU General Public License for more details.
15d94ba80eSRichard Cochran  *
16d94ba80eSRichard Cochran  *  You should have received a copy of the GNU General Public License
17d94ba80eSRichard Cochran  *  along with this program; if not, write to the Free Software
18d94ba80eSRichard Cochran  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19d94ba80eSRichard Cochran  */
20d94ba80eSRichard Cochran 
21d94ba80eSRichard Cochran #ifndef _PTP_CLOCK_KERNEL_H_
22d94ba80eSRichard Cochran #define _PTP_CLOCK_KERNEL_H_
23d94ba80eSRichard Cochran 
241ef76158SRichard Cochran #include <linux/device.h>
25220a60a4SBen Hutchings #include <linux/pps_kernel.h>
26d94ba80eSRichard Cochran #include <linux/ptp_clock.h>
27d94ba80eSRichard Cochran 
28d94ba80eSRichard Cochran 
29d94ba80eSRichard Cochran struct ptp_clock_request {
30d94ba80eSRichard Cochran 	enum {
31d94ba80eSRichard Cochran 		PTP_CLK_REQ_EXTTS,
32d94ba80eSRichard Cochran 		PTP_CLK_REQ_PEROUT,
33d94ba80eSRichard Cochran 		PTP_CLK_REQ_PPS,
34d94ba80eSRichard Cochran 	} type;
35d94ba80eSRichard Cochran 	union {
36d94ba80eSRichard Cochran 		struct ptp_extts_request extts;
37d94ba80eSRichard Cochran 		struct ptp_perout_request perout;
38d94ba80eSRichard Cochran 	};
39d94ba80eSRichard Cochran };
40d94ba80eSRichard Cochran 
41d94ba80eSRichard Cochran /**
42d94ba80eSRichard Cochran  * struct ptp_clock_info - decribes a PTP hardware clock
43d94ba80eSRichard Cochran  *
44d94ba80eSRichard Cochran  * @owner:     The clock driver should set to THIS_MODULE.
45*de465846SRichard Cochran  * @name:      A short "friendly name" to identify the clock and to
46*de465846SRichard Cochran  *             help distinguish PHY based devices from MAC based ones.
47*de465846SRichard Cochran  *             The string is not meant to be a unique id.
48d94ba80eSRichard Cochran  * @max_adj:   The maximum possible frequency adjustment, in parts per billon.
49d94ba80eSRichard Cochran  * @n_alarm:   The number of programmable alarms.
50d94ba80eSRichard Cochran  * @n_ext_ts:  The number of external time stamp channels.
51d94ba80eSRichard Cochran  * @n_per_out: The number of programmable periodic signals.
52d94ba80eSRichard Cochran  * @pps:       Indicates whether the clock supports a PPS callback.
53d94ba80eSRichard Cochran  *
54d94ba80eSRichard Cochran  * clock operations
55d94ba80eSRichard Cochran  *
56d94ba80eSRichard Cochran  * @adjfreq:  Adjusts the frequency of the hardware clock.
57d94ba80eSRichard Cochran  *            parameter delta: Desired period change in parts per billion.
58d94ba80eSRichard Cochran  *
59d94ba80eSRichard Cochran  * @adjtime:  Shifts the time of the hardware clock.
60d94ba80eSRichard Cochran  *            parameter delta: Desired change in nanoseconds.
61d94ba80eSRichard Cochran  *
62d94ba80eSRichard Cochran  * @gettime:  Reads the current time from the hardware clock.
63d94ba80eSRichard Cochran  *            parameter ts: Holds the result.
64d94ba80eSRichard Cochran  *
65d94ba80eSRichard Cochran  * @settime:  Set the current time on the hardware clock.
66d94ba80eSRichard Cochran  *            parameter ts: Time value to set.
67d94ba80eSRichard Cochran  *
68d94ba80eSRichard Cochran  * @enable:   Request driver to enable or disable an ancillary feature.
69d94ba80eSRichard Cochran  *            parameter request: Desired resource to enable or disable.
70d94ba80eSRichard Cochran  *            parameter on: Caller passes one to enable or zero to disable.
71d94ba80eSRichard Cochran  *
72d94ba80eSRichard Cochran  * Drivers should embed their ptp_clock_info within a private
73d94ba80eSRichard Cochran  * structure, obtaining a reference to it using container_of().
74d94ba80eSRichard Cochran  *
75d94ba80eSRichard Cochran  * The callbacks must all return zero on success, non-zero otherwise.
76d94ba80eSRichard Cochran  */
77d94ba80eSRichard Cochran 
78d94ba80eSRichard Cochran struct ptp_clock_info {
79d94ba80eSRichard Cochran 	struct module *owner;
80d94ba80eSRichard Cochran 	char name[16];
81d94ba80eSRichard Cochran 	s32 max_adj;
82d94ba80eSRichard Cochran 	int n_alarm;
83d94ba80eSRichard Cochran 	int n_ext_ts;
84d94ba80eSRichard Cochran 	int n_per_out;
85d94ba80eSRichard Cochran 	int pps;
86d94ba80eSRichard Cochran 	int (*adjfreq)(struct ptp_clock_info *ptp, s32 delta);
87d94ba80eSRichard Cochran 	int (*adjtime)(struct ptp_clock_info *ptp, s64 delta);
88d94ba80eSRichard Cochran 	int (*gettime)(struct ptp_clock_info *ptp, struct timespec *ts);
89d94ba80eSRichard Cochran 	int (*settime)(struct ptp_clock_info *ptp, const struct timespec *ts);
90d94ba80eSRichard Cochran 	int (*enable)(struct ptp_clock_info *ptp,
91d94ba80eSRichard Cochran 		      struct ptp_clock_request *request, int on);
92d94ba80eSRichard Cochran };
93d94ba80eSRichard Cochran 
94d94ba80eSRichard Cochran struct ptp_clock;
95d94ba80eSRichard Cochran 
96d94ba80eSRichard Cochran /**
97d94ba80eSRichard Cochran  * ptp_clock_register() - register a PTP hardware clock driver
98d94ba80eSRichard Cochran  *
99d94ba80eSRichard Cochran  * @info:   Structure describing the new clock.
1001ef76158SRichard Cochran  * @parent: Pointer to the parent device of the new clock.
101d94ba80eSRichard Cochran  */
102d94ba80eSRichard Cochran 
1031ef76158SRichard Cochran extern struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
1041ef76158SRichard Cochran 					    struct device *parent);
105d94ba80eSRichard Cochran 
106d94ba80eSRichard Cochran /**
107d94ba80eSRichard Cochran  * ptp_clock_unregister() - unregister a PTP hardware clock driver
108d94ba80eSRichard Cochran  *
109d94ba80eSRichard Cochran  * @ptp:  The clock to remove from service.
110d94ba80eSRichard Cochran  */
111d94ba80eSRichard Cochran 
112d94ba80eSRichard Cochran extern int ptp_clock_unregister(struct ptp_clock *ptp);
113d94ba80eSRichard Cochran 
114d94ba80eSRichard Cochran 
115d94ba80eSRichard Cochran enum ptp_clock_events {
116d94ba80eSRichard Cochran 	PTP_CLOCK_ALARM,
117d94ba80eSRichard Cochran 	PTP_CLOCK_EXTTS,
118d94ba80eSRichard Cochran 	PTP_CLOCK_PPS,
119220a60a4SBen Hutchings 	PTP_CLOCK_PPSUSR,
120d94ba80eSRichard Cochran };
121d94ba80eSRichard Cochran 
122d94ba80eSRichard Cochran /**
123d94ba80eSRichard Cochran  * struct ptp_clock_event - decribes a PTP hardware clock event
124d94ba80eSRichard Cochran  *
125d94ba80eSRichard Cochran  * @type:  One of the ptp_clock_events enumeration values.
126d94ba80eSRichard Cochran  * @index: Identifies the source of the event.
127220a60a4SBen Hutchings  * @timestamp: When the event occurred (%PTP_CLOCK_EXTTS only).
128220a60a4SBen Hutchings  * @pps_times: When the event occurred (%PTP_CLOCK_PPSUSR only).
129d94ba80eSRichard Cochran  */
130d94ba80eSRichard Cochran 
131d94ba80eSRichard Cochran struct ptp_clock_event {
132d94ba80eSRichard Cochran 	int type;
133d94ba80eSRichard Cochran 	int index;
134220a60a4SBen Hutchings 	union {
135d94ba80eSRichard Cochran 		u64 timestamp;
136220a60a4SBen Hutchings 		struct pps_event_time pps_times;
137220a60a4SBen Hutchings 	};
138d94ba80eSRichard Cochran };
139d94ba80eSRichard Cochran 
140d94ba80eSRichard Cochran /**
141d94ba80eSRichard Cochran  * ptp_clock_event() - notify the PTP layer about an event
142d94ba80eSRichard Cochran  *
143d94ba80eSRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
144d94ba80eSRichard Cochran  * @event:  Message structure describing the event.
145d94ba80eSRichard Cochran  */
146d94ba80eSRichard Cochran 
147d94ba80eSRichard Cochran extern void ptp_clock_event(struct ptp_clock *ptp,
148d94ba80eSRichard Cochran 			    struct ptp_clock_event *event);
149d94ba80eSRichard Cochran 
150995a9090SRichard Cochran /**
151995a9090SRichard Cochran  * ptp_clock_index() - obtain the device index of a PTP clock
152995a9090SRichard Cochran  *
153995a9090SRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
154995a9090SRichard Cochran  */
155995a9090SRichard Cochran 
156995a9090SRichard Cochran extern int ptp_clock_index(struct ptp_clock *ptp);
157995a9090SRichard Cochran 
158d94ba80eSRichard Cochran #endif
159