Hans Verkuil | b4e30a9 | 2018-02-07 09:34:03 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 2 | /* |
| 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 Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 7 | */ |
| 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 Verkuil | fbbd403 | 2019-04-10 05:13:28 -0400 | [diff] [blame] | 14 | #include <linux/of_platform.h> |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 15 | |
Hans Verkuil | ee7e9871 | 2017-04-17 07:54:37 -0300 | [diff] [blame] | 16 | #include <media/cec.h> |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 17 | #include <media/cec-notifier.h> |
| 18 | #include <drm/drm_edid.h> |
| 19 | |
| 20 | struct cec_notifier { |
| 21 | struct mutex lock; |
| 22 | struct list_head head; |
| 23 | struct kref kref; |
Hans Verkuil | 3d51dc0 | 2019-06-20 04:39:00 -0400 | [diff] [blame] | 24 | struct device *hdmi_dev; |
Hans Verkuil | b48cb35 | 2019-06-20 06:10:00 -0400 | [diff] [blame] | 25 | struct cec_connector_info conn_info; |
Hans Verkuil | 3d51dc0 | 2019-06-20 04:39:00 -0400 | [diff] [blame] | 26 | const char *conn_name; |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 27 | struct cec_adapter *cec_adap; |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 28 | |
| 29 | u16 phys_addr; |
| 30 | }; |
| 31 | |
| 32 | static LIST_HEAD(cec_notifiers); |
| 33 | static DEFINE_MUTEX(cec_notifiers_lock); |
| 34 | |
Hans Verkuil | 3d51dc0 | 2019-06-20 04:39:00 -0400 | [diff] [blame] | 35 | struct cec_notifier * |
| 36 | cec_notifier_get_conn(struct device *hdmi_dev, const char *conn_name) |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 37 | { |
| 38 | struct cec_notifier *n; |
| 39 | |
| 40 | mutex_lock(&cec_notifiers_lock); |
| 41 | list_for_each_entry(n, &cec_notifiers, head) { |
Hans Verkuil | 3d51dc0 | 2019-06-20 04:39:00 -0400 | [diff] [blame] | 42 | if (n->hdmi_dev == hdmi_dev && |
| 43 | (!conn_name || |
| 44 | (n->conn_name && !strcmp(n->conn_name, conn_name)))) { |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 45 | 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 Verkuil | 3d51dc0 | 2019-06-20 04:39:00 -0400 | [diff] [blame] | 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; |
| 60 | } |
| 61 | } |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 62 | n->phys_addr = CEC_PHYS_ADDR_INVALID; |
Hans Verkuil | 3d51dc0 | 2019-06-20 04:39:00 -0400 | [diff] [blame] | 63 | |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 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; |
| 70 | } |
Neil Armstrong | 7a78c1e | 2018-07-04 17:08:16 +0200 | [diff] [blame] | 71 | EXPORT_SYMBOL_GPL(cec_notifier_get_conn); |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 72 | |
| 73 | static 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 Verkuil | 3d51dc0 | 2019-06-20 04:39:00 -0400 | [diff] [blame] | 79 | kfree(n->conn_name); |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 80 | kfree(n); |
| 81 | } |
| 82 | |
Hans Verkuil | e611164 | 2020-01-06 12:04:17 +0100 | [diff] [blame] | 83 | static void cec_notifier_put(struct cec_notifier *n) |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 84 | { |
| 85 | mutex_lock(&cec_notifiers_lock); |
| 86 | kref_put(&n->kref, cec_notifier_release); |
| 87 | mutex_unlock(&cec_notifiers_lock); |
| 88 | } |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 89 | |
Hans Verkuil | b48cb35 | 2019-06-20 06:10:00 -0400 | [diff] [blame] | 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) |
| 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 | } |
| 112 | EXPORT_SYMBOL_GPL(cec_notifier_conn_register); |
| 113 | |
| 114 | void 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 | } |
| 129 | EXPORT_SYMBOL_GPL(cec_notifier_conn_unregister); |
| 130 | |
| 131 | struct cec_notifier * |
| 132 | cec_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 | } |
| 152 | EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_register); |
| 153 | |
Hans Verkuil | 10d8f30 | 2019-10-04 13:04:24 +0200 | [diff] [blame] | 154 | void cec_notifier_cec_adap_unregister(struct cec_notifier *n, |
| 155 | struct cec_adapter *adap) |
Hans Verkuil | b48cb35 | 2019-06-20 06:10:00 -0400 | [diff] [blame] | 156 | { |
| 157 | if (!n) |
| 158 | return; |
| 159 | |
| 160 | mutex_lock(&n->lock); |
Hans Verkuil | 10d8f30 | 2019-10-04 13:04:24 +0200 | [diff] [blame] | 161 | adap->notifier = NULL; |
Hans Verkuil | b48cb35 | 2019-06-20 06:10:00 -0400 | [diff] [blame] | 162 | n->cec_adap = NULL; |
Hans Verkuil | b48cb35 | 2019-06-20 06:10:00 -0400 | [diff] [blame] | 163 | mutex_unlock(&n->lock); |
| 164 | cec_notifier_put(n); |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_unregister); |
| 167 | |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 168 | void cec_notifier_set_phys_addr(struct cec_notifier *n, u16 pa) |
| 169 | { |
Hans Verkuil | fc1ff45 | 2017-07-15 09:32:56 -0300 | [diff] [blame] | 170 | if (n == NULL) |
| 171 | return; |
| 172 | |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 173 | mutex_lock(&n->lock); |
| 174 | n->phys_addr = pa; |
Hans Verkuil | e611164 | 2020-01-06 12:04:17 +0100 | [diff] [blame] | 175 | if (n->cec_adap) |
Hans Verkuil | b48cb35 | 2019-06-20 06:10:00 -0400 | [diff] [blame] | 176 | cec_s_phys_addr(n->cec_adap, n->phys_addr, false); |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 177 | mutex_unlock(&n->lock); |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr); |
| 180 | |
| 181 | void 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 Verkuil | fc1ff45 | 2017-07-15 09:32:56 -0300 | [diff] [blame] | 186 | if (n == NULL) |
| 187 | return; |
| 188 | |
Hans Verkuil | 6917a7b | 2016-11-14 11:55:20 -0200 | [diff] [blame] | 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); |
| 193 | } |
| 194 | EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr_from_edid); |
| 195 | |
Hans Verkuil | fbbd403 | 2019-04-10 05:13:28 -0400 | [diff] [blame] | 196 | struct 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 | } |
| 223 | EXPORT_SYMBOL_GPL(cec_notifier_parse_hdmi_phandle); |