blob: 4a841bee5cc2502c3d14c0db012d42eedb9197cb [file] [log] [blame]
Hans Verkuilb4e30a92018-02-07 09:34:03 -05001// SPDX-License-Identifier: GPL-2.0-only
Hans Verkuil6917a7b2016-11-14 11:55:20 -02002/*
3 * cec-notifier.c - notify CEC drivers of physical address changes
4 *
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.
Hans Verkuil6917a7b2016-11-14 11:55:20 -02007 */
8
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>
Hans Verkuilfbbd4032019-04-10 05:13:28 -040014#include <linux/of_platform.h>
Hans Verkuil6917a7b2016-11-14 11:55:20 -020015
Hans Verkuilee7e98712017-04-17 07:54:37 -030016#include <media/cec.h>
Hans Verkuil6917a7b2016-11-14 11:55:20 -020017#include <media/cec-notifier.h>
18#include <drm/drm_edid.h>
19
20struct cec_notifier {
21 struct mutex lock;
22 struct list_head head;
23 struct kref kref;
Hans Verkuil3d51dc02019-06-20 04:39:00 -040024 struct device *hdmi_dev;
Hans Verkuilb48cb352019-06-20 06:10:00 -040025 struct cec_connector_info conn_info;
Hans Verkuil3d51dc02019-06-20 04:39:00 -040026 const char *conn_name;
Hans Verkuil6917a7b2016-11-14 11:55:20 -020027 struct cec_adapter *cec_adap;
Hans Verkuil6917a7b2016-11-14 11:55:20 -020028
29 u16 phys_addr;
30};
31
32static LIST_HEAD(cec_notifiers);
33static DEFINE_MUTEX(cec_notifiers_lock);
34
Hans Verkuil3d51dc02019-06-20 04:39:00 -040035struct cec_notifier *
36cec_notifier_get_conn(struct device *hdmi_dev, const char *conn_name)
Hans Verkuil6917a7b2016-11-14 11:55:20 -020037{
38 struct cec_notifier *n;
39
40 mutex_lock(&cec_notifiers_lock);
41 list_for_each_entry(n, &cec_notifiers, head) {
Hans Verkuil3d51dc02019-06-20 04:39:00 -040042 if (n->hdmi_dev == hdmi_dev &&
43 (!conn_name ||
44 (n->conn_name && !strcmp(n->conn_name, conn_name)))) {
Hans Verkuil6917a7b2016-11-14 11:55:20 -020045 kref_get(&n->kref);
46 mutex_unlock(&cec_notifiers_lock);
47 return n;
48 }
49 }
50 n = kzalloc(sizeof(*n), GFP_KERNEL);
51 if (!n)
52 goto unlock;
Hans Verkuil3d51dc02019-06-20 04:39:00 -040053 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;
60 }
61 }
Hans Verkuil6917a7b2016-11-14 11:55:20 -020062 n->phys_addr = CEC_PHYS_ADDR_INVALID;
Hans Verkuil3d51dc02019-06-20 04:39:00 -040063
Hans Verkuil6917a7b2016-11-14 11:55:20 -020064 mutex_init(&n->lock);
65 kref_init(&n->kref);
66 list_add_tail(&n->head, &cec_notifiers);
67unlock:
68 mutex_unlock(&cec_notifiers_lock);
69 return n;
70}
Neil Armstrong7a78c1e2018-07-04 17:08:16 +020071EXPORT_SYMBOL_GPL(cec_notifier_get_conn);
Hans Verkuil6917a7b2016-11-14 11:55:20 -020072
73static void cec_notifier_release(struct kref *kref)
74{
75 struct cec_notifier *n =
76 container_of(kref, struct cec_notifier, kref);
77
78 list_del(&n->head);
Hans Verkuil3d51dc02019-06-20 04:39:00 -040079 kfree(n->conn_name);
Hans Verkuil6917a7b2016-11-14 11:55:20 -020080 kfree(n);
81}
82
Hans Verkuile6111642020-01-06 12:04:17 +010083static void cec_notifier_put(struct cec_notifier *n)
Hans Verkuil6917a7b2016-11-14 11:55:20 -020084{
85 mutex_lock(&cec_notifiers_lock);
86 kref_put(&n->kref, cec_notifier_release);
87 mutex_unlock(&cec_notifiers_lock);
88}
Hans Verkuil6917a7b2016-11-14 11:55:20 -020089
Hans Verkuilb48cb352019-06-20 06:10:00 -040090struct cec_notifier *
91cec_notifier_conn_register(struct device *hdmi_dev, const char *conn_name,
92 const struct cec_connector_info *conn_info)
93{
94 struct cec_notifier *n = cec_notifier_get_conn(hdmi_dev, conn_name);
95
96 if (!n)
97 return n;
98
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);
108 }
109 mutex_unlock(&n->lock);
110 return n;
111}
112EXPORT_SYMBOL_GPL(cec_notifier_conn_register);
113
114void cec_notifier_conn_unregister(struct cec_notifier *n)
115{
116 if (!n)
117 return;
118
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);
125 }
126 mutex_unlock(&n->lock);
127 cec_notifier_put(n);
128}
129EXPORT_SYMBOL_GPL(cec_notifier_conn_unregister);
130
131struct cec_notifier *
132cec_notifier_cec_adap_register(struct device *hdmi_dev, const char *conn_name,
133 struct cec_adapter *adap)
134{
135 struct cec_notifier *n;
136
137 if (WARN_ON(!adap))
138 return NULL;
139
140 n = cec_notifier_get_conn(hdmi_dev, conn_name);
141 if (!n)
142 return n;
143
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;
151}
152EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_register);
153
Hans Verkuil10d8f302019-10-04 13:04:24 +0200154void cec_notifier_cec_adap_unregister(struct cec_notifier *n,
155 struct cec_adapter *adap)
Hans Verkuilb48cb352019-06-20 06:10:00 -0400156{
157 if (!n)
158 return;
159
160 mutex_lock(&n->lock);
Hans Verkuil10d8f302019-10-04 13:04:24 +0200161 adap->notifier = NULL;
Hans Verkuilb48cb352019-06-20 06:10:00 -0400162 n->cec_adap = NULL;
Hans Verkuilb48cb352019-06-20 06:10:00 -0400163 mutex_unlock(&n->lock);
164 cec_notifier_put(n);
165}
166EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_unregister);
167
Hans Verkuil6917a7b2016-11-14 11:55:20 -0200168void cec_notifier_set_phys_addr(struct cec_notifier *n, u16 pa)
169{
Hans Verkuilfc1ff452017-07-15 09:32:56 -0300170 if (n == NULL)
171 return;
172
Hans Verkuil6917a7b2016-11-14 11:55:20 -0200173 mutex_lock(&n->lock);
174 n->phys_addr = pa;
Hans Verkuile6111642020-01-06 12:04:17 +0100175 if (n->cec_adap)
Hans Verkuilb48cb352019-06-20 06:10:00 -0400176 cec_s_phys_addr(n->cec_adap, n->phys_addr, false);
Hans Verkuil6917a7b2016-11-14 11:55:20 -0200177 mutex_unlock(&n->lock);
178}
179EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr);
180
181void cec_notifier_set_phys_addr_from_edid(struct cec_notifier *n,
182 const struct edid *edid)
183{
184 u16 pa = CEC_PHYS_ADDR_INVALID;
185
Hans Verkuilfc1ff452017-07-15 09:32:56 -0300186 if (n == NULL)
187 return;
188
Hans Verkuil6917a7b2016-11-14 11:55:20 -0200189 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);
193}
194EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr_from_edid);
195
Hans Verkuilfbbd4032019-04-10 05:13:28 -0400196struct device *cec_notifier_parse_hdmi_phandle(struct device *dev)
197{
198 struct platform_device *hdmi_pdev;
199 struct device *hdmi_dev = NULL;
200 struct device_node *np;
201
202 np = of_parse_phandle(dev->of_node, "hdmi-phandle", 0);
203
204 if (!np) {
205 dev_err(dev, "Failed to find HDMI node in device tree\n");
206 return ERR_PTR(-ENODEV);
207 }
208 hdmi_pdev = of_find_device_by_node(np);
209 of_node_put(np);
210 if (hdmi_pdev) {
211 hdmi_dev = &hdmi_pdev->dev;
212 /*
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.
217 */
218 put_device(hdmi_dev);
219 return hdmi_dev;
220 }
221 return ERR_PTR(-EPROBE_DEFER);
222}
223EXPORT_SYMBOL_GPL(cec_notifier_parse_hdmi_phandle);