blob: 5676b849379d43468267e8d12fc7367096a2ae4c [file] [log] [blame]
Mengdong Lin98d8fc62015-05-19 22:29:30 +08001/*
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
24static struct i915_audio_component *hdac_acomp;
25
26int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
27{
28 struct i915_audio_component *acomp = bus->audio_component;
29
Takashi Iwai692af902015-06-10 12:15:27 +020030 if (!acomp || !acomp->ops)
Mengdong Lin98d8fc62015-05-19 22:29:30 +080031 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}
46EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
47
48int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
49{
50 struct i915_audio_component *acomp = bus->audio_component;
51
Takashi Iwai692af902015-06-10 12:15:27 +020052 if (!acomp || !acomp->ops)
Mengdong Lin98d8fc62015-05-19 22:29:30 +080053 return -ENODEV;
54
55 dev_dbg(bus->dev, "display power %s\n",
56 enable ? "enable" : "disable");
57
58 if (enable) {
David Henningsson033ea342015-07-16 10:39:24 +020059 if (!bus->i915_power_refcount++) {
Mengdong Lin98d8fc62015-05-19 22:29:30 +080060 acomp->ops->get_power(acomp->dev);
David Henningsson033ea342015-07-16 10:39:24 +020061 snd_hdac_set_codec_wakeup(bus, true);
62 snd_hdac_set_codec_wakeup(bus, false);
63 }
Mengdong Lin98d8fc62015-05-19 22:29:30 +080064 } else {
65 WARN_ON(!bus->i915_power_refcount);
66 if (!--bus->i915_power_refcount)
67 acomp->ops->put_power(acomp->dev);
68 }
69
70 return 0;
71}
72EXPORT_SYMBOL_GPL(snd_hdac_display_power);
73
74int snd_hdac_get_display_clk(struct hdac_bus *bus)
75{
76 struct i915_audio_component *acomp = bus->audio_component;
77
Takashi Iwai692af902015-06-10 12:15:27 +020078 if (!acomp || !acomp->ops)
Mengdong Lin98d8fc62015-05-19 22:29:30 +080079 return -ENODEV;
80
81 return acomp->ops->get_cdclk_freq(acomp->dev);
82}
83EXPORT_SYMBOL_GPL(snd_hdac_get_display_clk);
84
85static int hdac_component_master_bind(struct device *dev)
86{
87 struct i915_audio_component *acomp = hdac_acomp;
88 int ret;
89
90 ret = component_bind_all(dev, acomp);
91 if (ret < 0)
92 return ret;
93
94 if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
95 acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
96 ret = -EINVAL;
97 goto out_unbind;
98 }
99
100 /*
101 * Atm, we don't support dynamic unbinding initiated by the child
102 * component, so pin its containing module until we unbind.
103 */
104 if (!try_module_get(acomp->ops->owner)) {
105 ret = -ENODEV;
106 goto out_unbind;
107 }
108
109 return 0;
110
111out_unbind:
112 component_unbind_all(dev, acomp);
113
114 return ret;
115}
116
117static void hdac_component_master_unbind(struct device *dev)
118{
119 struct i915_audio_component *acomp = hdac_acomp;
120
121 module_put(acomp->ops->owner);
122 component_unbind_all(dev, acomp);
123 WARN_ON(acomp->ops || acomp->dev);
124}
125
126static const struct component_master_ops hdac_component_master_ops = {
127 .bind = hdac_component_master_bind,
128 .unbind = hdac_component_master_unbind,
129};
130
131static int hdac_component_master_match(struct device *dev, void *data)
132{
133 /* i915 is the only supported component */
134 return !strcmp(dev->driver->name, "i915");
135}
136
137int snd_hdac_i915_init(struct hdac_bus *bus)
138{
139 struct component_match *match = NULL;
140 struct device *dev = bus->dev;
141 struct i915_audio_component *acomp;
142 int ret;
143
144 acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
145 if (!acomp)
146 return -ENOMEM;
147 bus->audio_component = acomp;
148 hdac_acomp = acomp;
149
150 component_match_add(dev, &match, hdac_component_master_match, bus);
151 ret = component_master_add_with_match(dev, &hdac_component_master_ops,
152 match);
153 if (ret < 0)
154 goto out_err;
155
156 /*
157 * Atm, we don't support deferring the component binding, so make sure
158 * i915 is loaded and that the binding successfully completes.
159 */
160 request_module("i915");
161
162 if (!acomp->ops) {
163 ret = -ENODEV;
164 goto out_master_del;
165 }
166 dev_dbg(dev, "bound to i915 component master\n");
167
168 return 0;
169out_master_del:
170 component_master_del(dev, &hdac_component_master_ops);
171out_err:
172 kfree(acomp);
173 bus->audio_component = NULL;
174 dev_err(dev, "failed to add i915 component master (%d)\n", ret);
175
176 return ret;
177}
178EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
179
180int snd_hdac_i915_exit(struct hdac_bus *bus)
181{
182 struct device *dev = bus->dev;
183 struct i915_audio_component *acomp = bus->audio_component;
184
Takashi Iwai692af902015-06-10 12:15:27 +0200185 if (!acomp)
186 return 0;
187
Mengdong Lin98d8fc62015-05-19 22:29:30 +0800188 WARN_ON(bus->i915_power_refcount);
Takashi Iwai692af902015-06-10 12:15:27 +0200189 if (bus->i915_power_refcount > 0 && acomp->ops)
Mengdong Lin98d8fc62015-05-19 22:29:30 +0800190 acomp->ops->put_power(acomp->dev);
191
192 component_master_del(dev, &hdac_component_master_ops);
193
194 kfree(acomp);
195 bus->audio_component = NULL;
196
197 return 0;
198}
199EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);