xref: /openbmc/linux/sound/hda/hdac_i915.c (revision d3597236)
1 /*
2  *  hdac_i915.c - routines for sync between HD-A core and i915 display driver
3  *
4  *  This program is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU General Public License as published by the Free
6  *  Software Foundation; either version 2 of the License, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  for more details.
13  */
14 
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/component.h>
19 #include <drm/i915_component.h>
20 #include <sound/core.h>
21 #include <sound/hdaudio.h>
22 #include <sound/hda_i915.h>
23 
24 static struct i915_audio_component *hdac_acomp;
25 
26 int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
27 {
28 	struct i915_audio_component *acomp = bus->audio_component;
29 
30 	if (!acomp || !acomp->ops)
31 		return -ENODEV;
32 
33 	if (!acomp->ops->codec_wake_override) {
34 		dev_warn(bus->dev,
35 			"Invalid codec wake callback\n");
36 		return 0;
37 	}
38 
39 	dev_dbg(bus->dev, "%s codec wakeup\n",
40 		enable ? "enable" : "disable");
41 
42 	acomp->ops->codec_wake_override(acomp->dev, enable);
43 
44 	return 0;
45 }
46 EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
47 
48 int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
49 {
50 	struct i915_audio_component *acomp = bus->audio_component;
51 
52 	if (!acomp || !acomp->ops)
53 		return -ENODEV;
54 
55 	dev_dbg(bus->dev, "display power %s\n",
56 		enable ? "enable" : "disable");
57 
58 	if (enable) {
59 		if (!bus->i915_power_refcount++)
60 			acomp->ops->get_power(acomp->dev);
61 	} else {
62 		WARN_ON(!bus->i915_power_refcount);
63 		if (!--bus->i915_power_refcount)
64 			acomp->ops->put_power(acomp->dev);
65 	}
66 
67 	return 0;
68 }
69 EXPORT_SYMBOL_GPL(snd_hdac_display_power);
70 
71 int snd_hdac_get_display_clk(struct hdac_bus *bus)
72 {
73 	struct i915_audio_component *acomp = bus->audio_component;
74 
75 	if (!acomp || !acomp->ops)
76 		return -ENODEV;
77 
78 	return acomp->ops->get_cdclk_freq(acomp->dev);
79 }
80 EXPORT_SYMBOL_GPL(snd_hdac_get_display_clk);
81 
82 static int hdac_component_master_bind(struct device *dev)
83 {
84 	struct i915_audio_component *acomp = hdac_acomp;
85 	int ret;
86 
87 	ret = component_bind_all(dev, acomp);
88 	if (ret < 0)
89 		return ret;
90 
91 	if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
92 		      acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
93 		ret = -EINVAL;
94 		goto out_unbind;
95 	}
96 
97 	/*
98 	 * Atm, we don't support dynamic unbinding initiated by the child
99 	 * component, so pin its containing module until we unbind.
100 	 */
101 	if (!try_module_get(acomp->ops->owner)) {
102 		ret = -ENODEV;
103 		goto out_unbind;
104 	}
105 
106 	return 0;
107 
108 out_unbind:
109 	component_unbind_all(dev, acomp);
110 
111 	return ret;
112 }
113 
114 static void hdac_component_master_unbind(struct device *dev)
115 {
116 	struct i915_audio_component *acomp = hdac_acomp;
117 
118 	module_put(acomp->ops->owner);
119 	component_unbind_all(dev, acomp);
120 	WARN_ON(acomp->ops || acomp->dev);
121 }
122 
123 static const struct component_master_ops hdac_component_master_ops = {
124 	.bind = hdac_component_master_bind,
125 	.unbind = hdac_component_master_unbind,
126 };
127 
128 static int hdac_component_master_match(struct device *dev, void *data)
129 {
130 	/* i915 is the only supported component */
131 	return !strcmp(dev->driver->name, "i915");
132 }
133 
134 int snd_hdac_i915_init(struct hdac_bus *bus)
135 {
136 	struct component_match *match = NULL;
137 	struct device *dev = bus->dev;
138 	struct i915_audio_component *acomp;
139 	int ret;
140 
141 	acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
142 	if (!acomp)
143 		return -ENOMEM;
144 	bus->audio_component = acomp;
145 	hdac_acomp = acomp;
146 
147 	component_match_add(dev, &match, hdac_component_master_match, bus);
148 	ret = component_master_add_with_match(dev, &hdac_component_master_ops,
149 					      match);
150 	if (ret < 0)
151 		goto out_err;
152 
153 	/*
154 	 * Atm, we don't support deferring the component binding, so make sure
155 	 * i915 is loaded and that the binding successfully completes.
156 	 */
157 	request_module("i915");
158 
159 	if (!acomp->ops) {
160 		ret = -ENODEV;
161 		goto out_master_del;
162 	}
163 	dev_dbg(dev, "bound to i915 component master\n");
164 
165 	return 0;
166 out_master_del:
167 	component_master_del(dev, &hdac_component_master_ops);
168 out_err:
169 	kfree(acomp);
170 	bus->audio_component = NULL;
171 	dev_err(dev, "failed to add i915 component master (%d)\n", ret);
172 
173 	return ret;
174 }
175 EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
176 
177 int snd_hdac_i915_exit(struct hdac_bus *bus)
178 {
179 	struct device *dev = bus->dev;
180 	struct i915_audio_component *acomp = bus->audio_component;
181 
182 	if (!acomp)
183 		return 0;
184 
185 	WARN_ON(bus->i915_power_refcount);
186 	if (bus->i915_power_refcount > 0 && acomp->ops)
187 		acomp->ops->put_power(acomp->dev);
188 
189 	component_master_del(dev, &hdac_component_master_ops);
190 
191 	kfree(acomp);
192 	bus->audio_component = NULL;
193 
194 	return 0;
195 }
196 EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);
197