1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * cec - HDMI Consumer Electronics Control support header
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef _MEDIA_CEC_H
9*4882a593Smuzhiyun #define _MEDIA_CEC_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/poll.h>
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/debugfs.h>
14*4882a593Smuzhiyun #include <linux/device.h>
15*4882a593Smuzhiyun #include <linux/cdev.h>
16*4882a593Smuzhiyun #include <linux/kthread.h>
17*4882a593Smuzhiyun #include <linux/timer.h>
18*4882a593Smuzhiyun #include <linux/cec-funcs.h>
19*4882a593Smuzhiyun #include <media/rc-core.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
22*4882a593Smuzhiyun CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun * struct cec_devnode - cec device node
26*4882a593Smuzhiyun * @dev: cec device
27*4882a593Smuzhiyun * @cdev: cec character device
28*4882a593Smuzhiyun * @minor: device node minor number
29*4882a593Smuzhiyun * @registered: the device was correctly registered
30*4882a593Smuzhiyun * @unregistered: the device was unregistered
31*4882a593Smuzhiyun * @fhs_lock: lock to control access to the filehandle list
32*4882a593Smuzhiyun * @fhs: the list of open filehandles (cec_fh)
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * This structure represents a cec-related device node.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * The @parent is a physical device. It must be set by core or device drivers
37*4882a593Smuzhiyun * before registering the node.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun struct cec_devnode {
40*4882a593Smuzhiyun /* sysfs */
41*4882a593Smuzhiyun struct device dev;
42*4882a593Smuzhiyun struct cdev cdev;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /* device info */
45*4882a593Smuzhiyun int minor;
46*4882a593Smuzhiyun bool registered;
47*4882a593Smuzhiyun bool unregistered;
48*4882a593Smuzhiyun struct list_head fhs;
49*4882a593Smuzhiyun struct mutex lock;
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun struct cec_adapter;
53*4882a593Smuzhiyun struct cec_data;
54*4882a593Smuzhiyun struct cec_pin;
55*4882a593Smuzhiyun struct cec_notifier;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun struct cec_data {
58*4882a593Smuzhiyun struct list_head list;
59*4882a593Smuzhiyun struct list_head xfer_list;
60*4882a593Smuzhiyun struct cec_adapter *adap;
61*4882a593Smuzhiyun struct cec_msg msg;
62*4882a593Smuzhiyun struct cec_fh *fh;
63*4882a593Smuzhiyun struct delayed_work work;
64*4882a593Smuzhiyun struct completion c;
65*4882a593Smuzhiyun u8 attempts;
66*4882a593Smuzhiyun bool blocking;
67*4882a593Smuzhiyun bool completed;
68*4882a593Smuzhiyun };
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun struct cec_msg_entry {
71*4882a593Smuzhiyun struct list_head list;
72*4882a593Smuzhiyun struct cec_msg msg;
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun struct cec_event_entry {
76*4882a593Smuzhiyun struct list_head list;
77*4882a593Smuzhiyun struct cec_event ev;
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun #define CEC_NUM_CORE_EVENTS 2
81*4882a593Smuzhiyun #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun struct cec_fh {
84*4882a593Smuzhiyun struct list_head list;
85*4882a593Smuzhiyun struct list_head xfer_list;
86*4882a593Smuzhiyun struct cec_adapter *adap;
87*4882a593Smuzhiyun u8 mode_initiator;
88*4882a593Smuzhiyun u8 mode_follower;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* Events */
91*4882a593Smuzhiyun wait_queue_head_t wait;
92*4882a593Smuzhiyun struct mutex lock;
93*4882a593Smuzhiyun struct list_head events[CEC_NUM_EVENTS]; /* queued events */
94*4882a593Smuzhiyun u16 queued_events[CEC_NUM_EVENTS];
95*4882a593Smuzhiyun unsigned int total_queued_events;
96*4882a593Smuzhiyun struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
97*4882a593Smuzhiyun struct list_head msgs; /* queued messages */
98*4882a593Smuzhiyun unsigned int queued_msgs;
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun #define CEC_SIGNAL_FREE_TIME_RETRY 3
102*4882a593Smuzhiyun #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
103*4882a593Smuzhiyun #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /* The nominal data bit period is 2.4 ms */
106*4882a593Smuzhiyun #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun struct cec_adap_ops {
109*4882a593Smuzhiyun /* Low-level callbacks */
110*4882a593Smuzhiyun int (*adap_enable)(struct cec_adapter *adap, bool enable);
111*4882a593Smuzhiyun int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
112*4882a593Smuzhiyun int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
113*4882a593Smuzhiyun int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
114*4882a593Smuzhiyun int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
115*4882a593Smuzhiyun u32 signal_free_time, struct cec_msg *msg);
116*4882a593Smuzhiyun void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
117*4882a593Smuzhiyun void (*adap_free)(struct cec_adapter *adap);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* Error injection callbacks */
120*4882a593Smuzhiyun int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
121*4882a593Smuzhiyun bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /* High-level CEC message callback */
124*4882a593Smuzhiyun int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun * The minimum message length you can receive (excepting poll messages) is 2.
129*4882a593Smuzhiyun * With a transfer rate of at most 36 bytes per second this makes 18 messages
130*4882a593Smuzhiyun * per second worst case.
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * We queue at most 3 seconds worth of received messages. The CEC specification
133*4882a593Smuzhiyun * requires that messages are replied to within a second, so 3 seconds should
134*4882a593Smuzhiyun * give more than enough margin. Since most messages are actually more than 2
135*4882a593Smuzhiyun * bytes, this is in practice a lot more than 3 seconds.
136*4882a593Smuzhiyun */
137*4882a593Smuzhiyun #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun * The transmit queue is limited to 1 second worth of messages (worst case).
141*4882a593Smuzhiyun * Messages can be transmitted by userspace and kernel space. But for both it
142*4882a593Smuzhiyun * makes no sense to have a lot of messages queued up. One second seems
143*4882a593Smuzhiyun * reasonable.
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /**
148*4882a593Smuzhiyun * struct cec_adapter - cec adapter structure
149*4882a593Smuzhiyun * @owner: module owner
150*4882a593Smuzhiyun * @name: name of the CEC adapter
151*4882a593Smuzhiyun * @devnode: device node for the /dev/cecX device
152*4882a593Smuzhiyun * @lock: mutex controlling access to this structure
153*4882a593Smuzhiyun * @rc: remote control device
154*4882a593Smuzhiyun * @transmit_queue: queue of pending transmits
155*4882a593Smuzhiyun * @transmit_queue_sz: number of pending transmits
156*4882a593Smuzhiyun * @wait_queue: queue of transmits waiting for a reply
157*4882a593Smuzhiyun * @transmitting: CEC messages currently being transmitted
158*4882a593Smuzhiyun * @transmit_in_progress: true if a transmit is in progress
159*4882a593Smuzhiyun * @kthread_config: kthread used to configure a CEC adapter
160*4882a593Smuzhiyun * @config_completion: used to signal completion of the config kthread
161*4882a593Smuzhiyun * @kthread: main CEC processing thread
162*4882a593Smuzhiyun * @kthread_waitq: main CEC processing wait_queue
163*4882a593Smuzhiyun * @ops: cec adapter ops
164*4882a593Smuzhiyun * @priv: cec driver's private data
165*4882a593Smuzhiyun * @capabilities: cec adapter capabilities
166*4882a593Smuzhiyun * @available_log_addrs: maximum number of available logical addresses
167*4882a593Smuzhiyun * @phys_addr: the current physical address
168*4882a593Smuzhiyun * @needs_hpd: if true, then the HDMI HotPlug Detect pin must be high
169*4882a593Smuzhiyun * in order to transmit or receive CEC messages. This is usually a HW
170*4882a593Smuzhiyun * limitation.
171*4882a593Smuzhiyun * @is_configuring: the CEC adapter is configuring (i.e. claiming LAs)
172*4882a593Smuzhiyun * @is_configured: the CEC adapter is configured (i.e. has claimed LAs)
173*4882a593Smuzhiyun * @cec_pin_is_high: if true then the CEC pin is high. Only used with the
174*4882a593Smuzhiyun * CEC pin framework.
175*4882a593Smuzhiyun * @adap_controls_phys_addr: if true, then the CEC adapter controls the
176*4882a593Smuzhiyun * physical address, i.e. the CEC hardware can detect HPD changes and
177*4882a593Smuzhiyun * read the EDID and is not dependent on an external HDMI driver.
178*4882a593Smuzhiyun * Drivers that need this can set this field to true after the
179*4882a593Smuzhiyun * cec_allocate_adapter() call.
180*4882a593Smuzhiyun * @last_initiator: the initiator of the last transmitted message.
181*4882a593Smuzhiyun * @monitor_all_cnt: number of filehandles monitoring all msgs
182*4882a593Smuzhiyun * @monitor_pin_cnt: number of filehandles monitoring pin changes
183*4882a593Smuzhiyun * @follower_cnt: number of filehandles in follower mode
184*4882a593Smuzhiyun * @cec_follower: filehandle of the exclusive follower
185*4882a593Smuzhiyun * @cec_initiator: filehandle of the exclusive initiator
186*4882a593Smuzhiyun * @passthrough: if true, then the exclusive follower is in
187*4882a593Smuzhiyun * passthrough mode.
188*4882a593Smuzhiyun * @log_addrs: current logical addresses
189*4882a593Smuzhiyun * @conn_info: current connector info
190*4882a593Smuzhiyun * @tx_timeouts: number of transmit timeouts
191*4882a593Smuzhiyun * @notifier: CEC notifier
192*4882a593Smuzhiyun * @pin: CEC pin status struct
193*4882a593Smuzhiyun * @cec_dir: debugfs cec directory
194*4882a593Smuzhiyun * @status_file: debugfs cec status file
195*4882a593Smuzhiyun * @error_inj_file: debugfs cec error injection file
196*4882a593Smuzhiyun * @sequence: transmit sequence counter
197*4882a593Smuzhiyun * @input_phys: remote control input_phys name
198*4882a593Smuzhiyun *
199*4882a593Smuzhiyun * This structure represents a cec adapter.
200*4882a593Smuzhiyun */
201*4882a593Smuzhiyun struct cec_adapter {
202*4882a593Smuzhiyun struct module *owner;
203*4882a593Smuzhiyun char name[32];
204*4882a593Smuzhiyun struct cec_devnode devnode;
205*4882a593Smuzhiyun struct mutex lock;
206*4882a593Smuzhiyun struct rc_dev *rc;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun struct list_head transmit_queue;
209*4882a593Smuzhiyun unsigned int transmit_queue_sz;
210*4882a593Smuzhiyun struct list_head wait_queue;
211*4882a593Smuzhiyun struct cec_data *transmitting;
212*4882a593Smuzhiyun bool transmit_in_progress;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun struct task_struct *kthread_config;
215*4882a593Smuzhiyun struct completion config_completion;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun struct task_struct *kthread;
218*4882a593Smuzhiyun wait_queue_head_t kthread_waitq;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun const struct cec_adap_ops *ops;
221*4882a593Smuzhiyun void *priv;
222*4882a593Smuzhiyun u32 capabilities;
223*4882a593Smuzhiyun u8 available_log_addrs;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun u16 phys_addr;
226*4882a593Smuzhiyun bool needs_hpd;
227*4882a593Smuzhiyun bool is_configuring;
228*4882a593Smuzhiyun bool is_configured;
229*4882a593Smuzhiyun bool cec_pin_is_high;
230*4882a593Smuzhiyun bool adap_controls_phys_addr;
231*4882a593Smuzhiyun u8 last_initiator;
232*4882a593Smuzhiyun u32 monitor_all_cnt;
233*4882a593Smuzhiyun u32 monitor_pin_cnt;
234*4882a593Smuzhiyun u32 follower_cnt;
235*4882a593Smuzhiyun struct cec_fh *cec_follower;
236*4882a593Smuzhiyun struct cec_fh *cec_initiator;
237*4882a593Smuzhiyun bool passthrough;
238*4882a593Smuzhiyun struct cec_log_addrs log_addrs;
239*4882a593Smuzhiyun struct cec_connector_info conn_info;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun u32 tx_timeouts;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun #ifdef CONFIG_CEC_NOTIFIER
244*4882a593Smuzhiyun struct cec_notifier *notifier;
245*4882a593Smuzhiyun #endif
246*4882a593Smuzhiyun #ifdef CONFIG_CEC_PIN
247*4882a593Smuzhiyun struct cec_pin *pin;
248*4882a593Smuzhiyun #endif
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun struct dentry *cec_dir;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun u32 sequence;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun char input_phys[32];
255*4882a593Smuzhiyun };
256*4882a593Smuzhiyun
cec_get_drvdata(const struct cec_adapter * adap)257*4882a593Smuzhiyun static inline void *cec_get_drvdata(const struct cec_adapter *adap)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun return adap->priv;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
cec_has_log_addr(const struct cec_adapter * adap,u8 log_addr)262*4882a593Smuzhiyun static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun return adap->log_addrs.log_addr_mask & (1 << log_addr);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
cec_is_sink(const struct cec_adapter * adap)267*4882a593Smuzhiyun static inline bool cec_is_sink(const struct cec_adapter *adap)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun return adap->phys_addr == 0;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /**
273*4882a593Smuzhiyun * cec_is_registered() - is the CEC adapter registered?
274*4882a593Smuzhiyun *
275*4882a593Smuzhiyun * @adap: the CEC adapter, may be NULL.
276*4882a593Smuzhiyun *
277*4882a593Smuzhiyun * Return: true if the adapter is registered, false otherwise.
278*4882a593Smuzhiyun */
cec_is_registered(const struct cec_adapter * adap)279*4882a593Smuzhiyun static inline bool cec_is_registered(const struct cec_adapter *adap)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun return adap && adap->devnode.registered;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun #define cec_phys_addr_exp(pa) \
285*4882a593Smuzhiyun ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun struct edid;
288*4882a593Smuzhiyun struct drm_connector;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun #if IS_REACHABLE(CONFIG_CEC_CORE)
291*4882a593Smuzhiyun struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
292*4882a593Smuzhiyun void *priv, const char *name, u32 caps, u8 available_las);
293*4882a593Smuzhiyun int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
294*4882a593Smuzhiyun void cec_unregister_adapter(struct cec_adapter *adap);
295*4882a593Smuzhiyun void cec_delete_adapter(struct cec_adapter *adap);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
298*4882a593Smuzhiyun bool block);
299*4882a593Smuzhiyun void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
300*4882a593Smuzhiyun bool block);
301*4882a593Smuzhiyun void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
302*4882a593Smuzhiyun const struct edid *edid);
303*4882a593Smuzhiyun void cec_s_conn_info(struct cec_adapter *adap,
304*4882a593Smuzhiyun const struct cec_connector_info *conn_info);
305*4882a593Smuzhiyun int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
306*4882a593Smuzhiyun bool block);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun /* Called by the adapter */
309*4882a593Smuzhiyun void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
310*4882a593Smuzhiyun u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
311*4882a593Smuzhiyun u8 error_cnt, ktime_t ts);
312*4882a593Smuzhiyun
cec_transmit_done(struct cec_adapter * adap,u8 status,u8 arb_lost_cnt,u8 nack_cnt,u8 low_drive_cnt,u8 error_cnt)313*4882a593Smuzhiyun static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
314*4882a593Smuzhiyun u8 arb_lost_cnt, u8 nack_cnt,
315*4882a593Smuzhiyun u8 low_drive_cnt, u8 error_cnt)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
318*4882a593Smuzhiyun low_drive_cnt, error_cnt, ktime_get());
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun /*
321*4882a593Smuzhiyun * Simplified version of cec_transmit_done for hardware that doesn't retry
322*4882a593Smuzhiyun * failed transmits. So this is always just one attempt in which case
323*4882a593Smuzhiyun * the status is sufficient.
324*4882a593Smuzhiyun */
325*4882a593Smuzhiyun void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
326*4882a593Smuzhiyun u8 status, ktime_t ts);
327*4882a593Smuzhiyun
cec_transmit_attempt_done(struct cec_adapter * adap,u8 status)328*4882a593Smuzhiyun static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
329*4882a593Smuzhiyun u8 status)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun cec_transmit_attempt_done_ts(adap, status, ktime_get());
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun void cec_received_msg_ts(struct cec_adapter *adap,
335*4882a593Smuzhiyun struct cec_msg *msg, ktime_t ts);
336*4882a593Smuzhiyun
cec_received_msg(struct cec_adapter * adap,struct cec_msg * msg)337*4882a593Smuzhiyun static inline void cec_received_msg(struct cec_adapter *adap,
338*4882a593Smuzhiyun struct cec_msg *msg)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun cec_received_msg_ts(adap, msg, ktime_get());
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /**
344*4882a593Smuzhiyun * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
345*4882a593Smuzhiyun *
346*4882a593Smuzhiyun * @adap: pointer to the cec adapter
347*4882a593Smuzhiyun * @is_high: when true the CEC pin is high, otherwise it is low
348*4882a593Smuzhiyun * @dropped_events: when true some events were dropped
349*4882a593Smuzhiyun * @ts: the timestamp for this event
350*4882a593Smuzhiyun *
351*4882a593Smuzhiyun */
352*4882a593Smuzhiyun void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
353*4882a593Smuzhiyun bool dropped_events, ktime_t ts);
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /**
356*4882a593Smuzhiyun * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
357*4882a593Smuzhiyun *
358*4882a593Smuzhiyun * @adap: pointer to the cec adapter
359*4882a593Smuzhiyun * @is_high: when true the HPD pin is high, otherwise it is low
360*4882a593Smuzhiyun * @ts: the timestamp for this event
361*4882a593Smuzhiyun *
362*4882a593Smuzhiyun */
363*4882a593Smuzhiyun void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /**
366*4882a593Smuzhiyun * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
367*4882a593Smuzhiyun *
368*4882a593Smuzhiyun * @adap: pointer to the cec adapter
369*4882a593Smuzhiyun * @is_high: when true the 5V pin is high, otherwise it is low
370*4882a593Smuzhiyun * @ts: the timestamp for this event
371*4882a593Smuzhiyun *
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun /**
376*4882a593Smuzhiyun * cec_get_edid_phys_addr() - find and return the physical address
377*4882a593Smuzhiyun *
378*4882a593Smuzhiyun * @edid: pointer to the EDID data
379*4882a593Smuzhiyun * @size: size in bytes of the EDID data
380*4882a593Smuzhiyun * @offset: If not %NULL then the location of the physical address
381*4882a593Smuzhiyun * bytes in the EDID will be returned here. This is set to 0
382*4882a593Smuzhiyun * if there is no physical address found.
383*4882a593Smuzhiyun *
384*4882a593Smuzhiyun * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
385*4882a593Smuzhiyun */
386*4882a593Smuzhiyun u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
387*4882a593Smuzhiyun unsigned int *offset);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
390*4882a593Smuzhiyun const struct drm_connector *connector);
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun #else
393*4882a593Smuzhiyun
cec_register_adapter(struct cec_adapter * adap,struct device * parent)394*4882a593Smuzhiyun static inline int cec_register_adapter(struct cec_adapter *adap,
395*4882a593Smuzhiyun struct device *parent)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun return 0;
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
cec_unregister_adapter(struct cec_adapter * adap)400*4882a593Smuzhiyun static inline void cec_unregister_adapter(struct cec_adapter *adap)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
cec_delete_adapter(struct cec_adapter * adap)404*4882a593Smuzhiyun static inline void cec_delete_adapter(struct cec_adapter *adap)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
cec_s_phys_addr(struct cec_adapter * adap,u16 phys_addr,bool block)408*4882a593Smuzhiyun static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
409*4882a593Smuzhiyun bool block)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
cec_s_phys_addr_from_edid(struct cec_adapter * adap,const struct edid * edid)413*4882a593Smuzhiyun static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
414*4882a593Smuzhiyun const struct edid *edid)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
cec_get_edid_phys_addr(const u8 * edid,unsigned int size,unsigned int * offset)418*4882a593Smuzhiyun static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
419*4882a593Smuzhiyun unsigned int *offset)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun if (offset)
422*4882a593Smuzhiyun *offset = 0;
423*4882a593Smuzhiyun return CEC_PHYS_ADDR_INVALID;
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
cec_s_conn_info(struct cec_adapter * adap,const struct cec_connector_info * conn_info)426*4882a593Smuzhiyun static inline void cec_s_conn_info(struct cec_adapter *adap,
427*4882a593Smuzhiyun const struct cec_connector_info *conn_info)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun static inline void
cec_fill_conn_info_from_drm(struct cec_connector_info * conn_info,const struct drm_connector * connector)432*4882a593Smuzhiyun cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
433*4882a593Smuzhiyun const struct drm_connector *connector)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun memset(conn_info, 0, sizeof(*conn_info));
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun #endif
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun /**
441*4882a593Smuzhiyun * cec_phys_addr_invalidate() - set the physical address to INVALID
442*4882a593Smuzhiyun *
443*4882a593Smuzhiyun * @adap: the CEC adapter
444*4882a593Smuzhiyun *
445*4882a593Smuzhiyun * This is a simple helper function to invalidate the physical
446*4882a593Smuzhiyun * address.
447*4882a593Smuzhiyun */
cec_phys_addr_invalidate(struct cec_adapter * adap)448*4882a593Smuzhiyun static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /**
454*4882a593Smuzhiyun * cec_get_edid_spa_location() - find location of the Source Physical Address
455*4882a593Smuzhiyun *
456*4882a593Smuzhiyun * @edid: the EDID
457*4882a593Smuzhiyun * @size: the size of the EDID
458*4882a593Smuzhiyun *
459*4882a593Smuzhiyun * This EDID is expected to be a CEA-861 compliant, which means that there are
460*4882a593Smuzhiyun * at least two blocks and one or more of the extensions blocks are CEA-861
461*4882a593Smuzhiyun * blocks.
462*4882a593Smuzhiyun *
463*4882a593Smuzhiyun * The returned location is guaranteed to be <= size-2.
464*4882a593Smuzhiyun *
465*4882a593Smuzhiyun * This is an inline function since it is used by both CEC and V4L2.
466*4882a593Smuzhiyun * Ideally this would go in a module shared by both, but it is overkill to do
467*4882a593Smuzhiyun * that for just a single function.
468*4882a593Smuzhiyun */
cec_get_edid_spa_location(const u8 * edid,unsigned int size)469*4882a593Smuzhiyun static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
470*4882a593Smuzhiyun unsigned int size)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun unsigned int blocks = size / 128;
473*4882a593Smuzhiyun unsigned int block;
474*4882a593Smuzhiyun u8 d;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun /* Sanity check: at least 2 blocks and a multiple of the block size */
477*4882a593Smuzhiyun if (blocks < 2 || size % 128)
478*4882a593Smuzhiyun return 0;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /*
481*4882a593Smuzhiyun * If there are fewer extension blocks than the size, then update
482*4882a593Smuzhiyun * 'blocks'. It is allowed to have more extension blocks than the size,
483*4882a593Smuzhiyun * since some hardware can only read e.g. 256 bytes of the EDID, even
484*4882a593Smuzhiyun * though more blocks are present. The first CEA-861 extension block
485*4882a593Smuzhiyun * should normally be in block 1 anyway.
486*4882a593Smuzhiyun */
487*4882a593Smuzhiyun if (edid[0x7e] + 1 < blocks)
488*4882a593Smuzhiyun blocks = edid[0x7e] + 1;
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun for (block = 1; block < blocks; block++) {
491*4882a593Smuzhiyun unsigned int offset = block * 128;
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun /* Skip any non-CEA-861 extension blocks */
494*4882a593Smuzhiyun if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
495*4882a593Smuzhiyun continue;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /* search Vendor Specific Data Block (tag 3) */
498*4882a593Smuzhiyun d = edid[offset + 2] & 0x7f;
499*4882a593Smuzhiyun /* Check if there are Data Blocks */
500*4882a593Smuzhiyun if (d <= 4)
501*4882a593Smuzhiyun continue;
502*4882a593Smuzhiyun if (d > 4) {
503*4882a593Smuzhiyun unsigned int i = offset + 4;
504*4882a593Smuzhiyun unsigned int end = offset + d;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /* Note: 'end' is always < 'size' */
507*4882a593Smuzhiyun do {
508*4882a593Smuzhiyun u8 tag = edid[i] >> 5;
509*4882a593Smuzhiyun u8 len = edid[i] & 0x1f;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun if (tag == 3 && len >= 5 && i + len <= end &&
512*4882a593Smuzhiyun edid[i + 1] == 0x03 &&
513*4882a593Smuzhiyun edid[i + 2] == 0x0c &&
514*4882a593Smuzhiyun edid[i + 3] == 0x00)
515*4882a593Smuzhiyun return i + 4;
516*4882a593Smuzhiyun i += len + 1;
517*4882a593Smuzhiyun } while (i < end);
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun return 0;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun #endif /* _MEDIA_CEC_H */
524