1 // SPDX-License-Identifier: GPL-2.0-only
3 * cec-notifier.c - notify CEC drivers of physical address changes
5 * Copyright 2016 Russell King.
6 * Copyright 2016-2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
9 #include <linux/export.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/list.h>
14 #include <linux/kref.h>
15 #include <linux/of_platform.h>
17 #include <media/cec.h>
18 #include <media/cec-notifier.h>
19 #include <drm/drm_edid.h>
23 struct list_head head;
25 struct device *hdmi_dev;
26 struct cec_connector_info conn_info;
27 const char *port_name;
28 struct cec_adapter *cec_adap;
33 static LIST_HEAD(cec_notifiers);
34 static DEFINE_MUTEX(cec_notifiers_lock);
37 * cec_notifier_get_conn - find or create a new cec_notifier for the given
38 * device and connector tuple.
39 * @hdmi_dev: device that sends the events.
40 * @port_name: the connector name from which the event occurs
42 * If a notifier for device @dev already exists, then increase the refcount
43 * and return that notifier.
45 * If it doesn't exist, then allocate a new notifier struct and return a
46 * pointer to that new struct.
48 * Return NULL if the memory could not be allocated.
50 static struct cec_notifier *
51 cec_notifier_get_conn(struct device *hdmi_dev, const char *port_name)
53 struct cec_notifier *n;
55 mutex_lock(&cec_notifiers_lock);
56 list_for_each_entry(n, &cec_notifiers, head) {
57 if (n->hdmi_dev == hdmi_dev &&
59 (n->port_name && !strcmp(n->port_name, port_name)))) {
61 mutex_unlock(&cec_notifiers_lock);
65 n = kzalloc(sizeof(*n), GFP_KERNEL);
68 n->hdmi_dev = hdmi_dev;
70 n->port_name = kstrdup(port_name, GFP_KERNEL);
77 n->phys_addr = CEC_PHYS_ADDR_INVALID;
81 list_add_tail(&n->head, &cec_notifiers);
83 mutex_unlock(&cec_notifiers_lock);
87 static void cec_notifier_release(struct kref *kref)
89 struct cec_notifier *n =
90 container_of(kref, struct cec_notifier, kref);
97 static void cec_notifier_put(struct cec_notifier *n)
99 mutex_lock(&cec_notifiers_lock);
100 kref_put(&n->kref, cec_notifier_release);
101 mutex_unlock(&cec_notifiers_lock);
104 struct cec_notifier *
105 cec_notifier_conn_register(struct device *hdmi_dev, const char *port_name,
106 const struct cec_connector_info *conn_info)
108 struct cec_notifier *n = cec_notifier_get_conn(hdmi_dev, port_name);
113 mutex_lock(&n->lock);
114 n->phys_addr = CEC_PHYS_ADDR_INVALID;
116 n->conn_info = *conn_info;
118 memset(&n->conn_info, 0, sizeof(n->conn_info));
120 if (!n->cec_adap->adap_controls_phys_addr)
121 cec_phys_addr_invalidate(n->cec_adap);
122 cec_s_conn_info(n->cec_adap, conn_info);
124 mutex_unlock(&n->lock);
127 EXPORT_SYMBOL_GPL(cec_notifier_conn_register);
129 void cec_notifier_conn_unregister(struct cec_notifier *n)
134 mutex_lock(&n->lock);
135 memset(&n->conn_info, 0, sizeof(n->conn_info));
136 n->phys_addr = CEC_PHYS_ADDR_INVALID;
138 if (!n->cec_adap->adap_controls_phys_addr)
139 cec_phys_addr_invalidate(n->cec_adap);
140 cec_s_conn_info(n->cec_adap, NULL);
142 mutex_unlock(&n->lock);
145 EXPORT_SYMBOL_GPL(cec_notifier_conn_unregister);
147 struct cec_notifier *
148 cec_notifier_cec_adap_register(struct device *hdmi_dev, const char *port_name,
149 struct cec_adapter *adap)
151 struct cec_notifier *n;
156 n = cec_notifier_get_conn(hdmi_dev, port_name);
160 mutex_lock(&n->lock);
162 adap->conn_info = n->conn_info;
164 if (!adap->adap_controls_phys_addr)
165 cec_s_phys_addr(adap, n->phys_addr, false);
166 mutex_unlock(&n->lock);
169 EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_register);
171 void cec_notifier_cec_adap_unregister(struct cec_notifier *n,
172 struct cec_adapter *adap)
177 mutex_lock(&n->lock);
178 adap->notifier = NULL;
180 mutex_unlock(&n->lock);
183 EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_unregister);
185 void cec_notifier_set_phys_addr(struct cec_notifier *n, u16 pa)
190 mutex_lock(&n->lock);
192 if (n->cec_adap && !n->cec_adap->adap_controls_phys_addr)
193 cec_s_phys_addr(n->cec_adap, n->phys_addr, false);
194 mutex_unlock(&n->lock);
196 EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr);
198 void cec_notifier_set_phys_addr_from_edid(struct cec_notifier *n,
199 const struct edid *edid)
201 u16 pa = CEC_PHYS_ADDR_INVALID;
206 if (edid && edid->extensions)
207 pa = cec_get_edid_phys_addr((const u8 *)edid,
208 EDID_LENGTH * (edid->extensions + 1), NULL);
209 cec_notifier_set_phys_addr(n, pa);
211 EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr_from_edid);
213 struct device *cec_notifier_parse_hdmi_phandle(struct device *dev)
215 struct platform_device *hdmi_pdev;
216 struct device *hdmi_dev = NULL;
217 struct device_node *np;
219 np = of_parse_phandle(dev->of_node, "hdmi-phandle", 0);
222 dev_err(dev, "Failed to find HDMI node in device tree\n");
223 return ERR_PTR(-ENODEV);
226 hdmi_pdev = of_find_device_by_node(np);
228 hdmi_dev = &hdmi_pdev->dev;
229 #if IS_REACHABLE(CONFIG_I2C)
231 struct i2c_client *hdmi_client = of_find_i2c_device_by_node(np);
234 hdmi_dev = &hdmi_client->dev;
239 return ERR_PTR(-EPROBE_DEFER);
242 * Note that the device struct is only used as a key into the
243 * cec_notifiers list, it is never actually accessed.
244 * So we decrement the reference here so we don't leak
247 put_device(hdmi_dev);
250 EXPORT_SYMBOL_GPL(cec_notifier_parse_hdmi_phandle);