treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / cec / cec-notifier.c
blob4a841bee5cc2502c3d14c0db012d42eedb9197cb
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * cec-notifier.c - notify CEC drivers of physical address changes
5 * Copyright 2016 Russell King <rmk+kernel@arm.linux.org.uk>
6 * Copyright 2016-2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
7 */
9 #include <linux/export.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <linux/kref.h>
14 #include <linux/of_platform.h>
16 #include <media/cec.h>
17 #include <media/cec-notifier.h>
18 #include <drm/drm_edid.h>
20 struct cec_notifier {
21 struct mutex lock;
22 struct list_head head;
23 struct kref kref;
24 struct device *hdmi_dev;
25 struct cec_connector_info conn_info;
26 const char *conn_name;
27 struct cec_adapter *cec_adap;
29 u16 phys_addr;
32 static LIST_HEAD(cec_notifiers);
33 static DEFINE_MUTEX(cec_notifiers_lock);
35 struct cec_notifier *
36 cec_notifier_get_conn(struct device *hdmi_dev, const char *conn_name)
38 struct cec_notifier *n;
40 mutex_lock(&cec_notifiers_lock);
41 list_for_each_entry(n, &cec_notifiers, head) {
42 if (n->hdmi_dev == hdmi_dev &&
43 (!conn_name ||
44 (n->conn_name && !strcmp(n->conn_name, conn_name)))) {
45 kref_get(&n->kref);
46 mutex_unlock(&cec_notifiers_lock);
47 return n;
50 n = kzalloc(sizeof(*n), GFP_KERNEL);
51 if (!n)
52 goto unlock;
53 n->hdmi_dev = hdmi_dev;
54 if (conn_name) {
55 n->conn_name = kstrdup(conn_name, GFP_KERNEL);
56 if (!n->conn_name) {
57 kfree(n);
58 n = NULL;
59 goto unlock;
62 n->phys_addr = CEC_PHYS_ADDR_INVALID;
64 mutex_init(&n->lock);
65 kref_init(&n->kref);
66 list_add_tail(&n->head, &cec_notifiers);
67 unlock:
68 mutex_unlock(&cec_notifiers_lock);
69 return n;
71 EXPORT_SYMBOL_GPL(cec_notifier_get_conn);
73 static void cec_notifier_release(struct kref *kref)
75 struct cec_notifier *n =
76 container_of(kref, struct cec_notifier, kref);
78 list_del(&n->head);
79 kfree(n->conn_name);
80 kfree(n);
83 static void cec_notifier_put(struct cec_notifier *n)
85 mutex_lock(&cec_notifiers_lock);
86 kref_put(&n->kref, cec_notifier_release);
87 mutex_unlock(&cec_notifiers_lock);
90 struct cec_notifier *
91 cec_notifier_conn_register(struct device *hdmi_dev, const char *conn_name,
92 const struct cec_connector_info *conn_info)
94 struct cec_notifier *n = cec_notifier_get_conn(hdmi_dev, conn_name);
96 if (!n)
97 return n;
99 mutex_lock(&n->lock);
100 n->phys_addr = CEC_PHYS_ADDR_INVALID;
101 if (conn_info)
102 n->conn_info = *conn_info;
103 else
104 memset(&n->conn_info, 0, sizeof(n->conn_info));
105 if (n->cec_adap) {
106 cec_phys_addr_invalidate(n->cec_adap);
107 cec_s_conn_info(n->cec_adap, conn_info);
109 mutex_unlock(&n->lock);
110 return n;
112 EXPORT_SYMBOL_GPL(cec_notifier_conn_register);
114 void cec_notifier_conn_unregister(struct cec_notifier *n)
116 if (!n)
117 return;
119 mutex_lock(&n->lock);
120 memset(&n->conn_info, 0, sizeof(n->conn_info));
121 n->phys_addr = CEC_PHYS_ADDR_INVALID;
122 if (n->cec_adap) {
123 cec_phys_addr_invalidate(n->cec_adap);
124 cec_s_conn_info(n->cec_adap, NULL);
126 mutex_unlock(&n->lock);
127 cec_notifier_put(n);
129 EXPORT_SYMBOL_GPL(cec_notifier_conn_unregister);
131 struct cec_notifier *
132 cec_notifier_cec_adap_register(struct device *hdmi_dev, const char *conn_name,
133 struct cec_adapter *adap)
135 struct cec_notifier *n;
137 if (WARN_ON(!adap))
138 return NULL;
140 n = cec_notifier_get_conn(hdmi_dev, conn_name);
141 if (!n)
142 return n;
144 mutex_lock(&n->lock);
145 n->cec_adap = adap;
146 adap->conn_info = n->conn_info;
147 adap->notifier = n;
148 cec_s_phys_addr(adap, n->phys_addr, false);
149 mutex_unlock(&n->lock);
150 return n;
152 EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_register);
154 void cec_notifier_cec_adap_unregister(struct cec_notifier *n,
155 struct cec_adapter *adap)
157 if (!n)
158 return;
160 mutex_lock(&n->lock);
161 adap->notifier = NULL;
162 n->cec_adap = NULL;
163 mutex_unlock(&n->lock);
164 cec_notifier_put(n);
166 EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_unregister);
168 void cec_notifier_set_phys_addr(struct cec_notifier *n, u16 pa)
170 if (n == NULL)
171 return;
173 mutex_lock(&n->lock);
174 n->phys_addr = pa;
175 if (n->cec_adap)
176 cec_s_phys_addr(n->cec_adap, n->phys_addr, false);
177 mutex_unlock(&n->lock);
179 EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr);
181 void cec_notifier_set_phys_addr_from_edid(struct cec_notifier *n,
182 const struct edid *edid)
184 u16 pa = CEC_PHYS_ADDR_INVALID;
186 if (n == NULL)
187 return;
189 if (edid && edid->extensions)
190 pa = cec_get_edid_phys_addr((const u8 *)edid,
191 EDID_LENGTH * (edid->extensions + 1), NULL);
192 cec_notifier_set_phys_addr(n, pa);
194 EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr_from_edid);
196 struct device *cec_notifier_parse_hdmi_phandle(struct device *dev)
198 struct platform_device *hdmi_pdev;
199 struct device *hdmi_dev = NULL;
200 struct device_node *np;
202 np = of_parse_phandle(dev->of_node, "hdmi-phandle", 0);
204 if (!np) {
205 dev_err(dev, "Failed to find HDMI node in device tree\n");
206 return ERR_PTR(-ENODEV);
208 hdmi_pdev = of_find_device_by_node(np);
209 of_node_put(np);
210 if (hdmi_pdev) {
211 hdmi_dev = &hdmi_pdev->dev;
213 * Note that the device struct is only used as a key into the
214 * cec_notifiers list, it is never actually accessed.
215 * So we decrement the reference here so we don't leak
216 * memory.
218 put_device(hdmi_dev);
219 return hdmi_dev;
221 return ERR_PTR(-EPROBE_DEFER);
223 EXPORT_SYMBOL_GPL(cec_notifier_parse_hdmi_phandle);