Merge branch 'topic/hda' into for-next
[cascardo/linux.git] / sound / pci / hda / hda_i915.c
1 /*
2  *  hda_i915.c - routines for Haswell HDA controller power well support
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  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software Foundation,
16  *  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/component.h>
23 #include <drm/i915_component.h>
24 #include <sound/core.h>
25 #include "hda_controller.h"
26 #include "hda_intel.h"
27
28 /* Intel HSW/BDW display HDA controller Extended Mode registers.
29  * EM4 (M value) and EM5 (N Value) are used to convert CDClk (Core Display
30  * Clock) to 24MHz BCLK: BCLK = CDCLK * M / N
31  * The values will be lost when the display power well is disabled.
32  */
33 #define AZX_REG_EM4                     0x100c
34 #define AZX_REG_EM5                     0x1010
35
36 int hda_display_power(struct hda_intel *hda, bool enable)
37 {
38         struct i915_audio_component *acomp = &hda->audio_component;
39
40         if (!acomp->ops)
41                 return -ENODEV;
42
43         dev_dbg(&hda->chip.pci->dev, "display power %s\n",
44                 enable ? "enable" : "disable");
45
46         if (enable) {
47                 if (!hda->i915_power_refcount++)
48                         acomp->ops->get_power(acomp->dev);
49         } else {
50                 WARN_ON(!hda->i915_power_refcount);
51                 if (!--hda->i915_power_refcount)
52                         acomp->ops->put_power(acomp->dev);
53         }
54
55         return 0;
56 }
57
58 void haswell_set_bclk(struct hda_intel *hda)
59 {
60         int cdclk_freq;
61         unsigned int bclk_m, bclk_n;
62         struct i915_audio_component *acomp = &hda->audio_component;
63         struct pci_dev *pci = hda->chip.pci;
64
65         /* Only Haswell/Broadwell need set BCLK */
66         if (pci->device != 0x0a0c && pci->device != 0x0c0c
67            && pci->device != 0x0d0c && pci->device != 0x160c)
68                 return;
69
70         if (!acomp->ops)
71                 return;
72
73         cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev);
74         switch (cdclk_freq) {
75         case 337500:
76                 bclk_m = 16;
77                 bclk_n = 225;
78                 break;
79
80         case 450000:
81         default: /* default CDCLK 450MHz */
82                 bclk_m = 4;
83                 bclk_n = 75;
84                 break;
85
86         case 540000:
87                 bclk_m = 4;
88                 bclk_n = 90;
89                 break;
90
91         case 675000:
92                 bclk_m = 8;
93                 bclk_n = 225;
94                 break;
95         }
96
97         azx_writew(&hda->chip, EM4, bclk_m);
98         azx_writew(&hda->chip, EM5, bclk_n);
99 }
100
101 static int hda_component_master_bind(struct device *dev)
102 {
103         struct snd_card *card = dev_get_drvdata(dev);
104         struct azx *chip = card->private_data;
105         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
106         struct i915_audio_component *acomp = &hda->audio_component;
107         int ret;
108
109         ret = component_bind_all(dev, acomp);
110         if (ret < 0)
111                 return ret;
112
113         if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
114                       acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
115                 ret = -EINVAL;
116                 goto out_unbind;
117         }
118
119         /*
120          * Atm, we don't support dynamic unbinding initiated by the child
121          * component, so pin its containing module until we unbind.
122          */
123         if (!try_module_get(acomp->ops->owner)) {
124                 ret = -ENODEV;
125                 goto out_unbind;
126         }
127
128         return 0;
129
130 out_unbind:
131         component_unbind_all(dev, acomp);
132
133         return ret;
134 }
135
136 static void hda_component_master_unbind(struct device *dev)
137 {
138         struct snd_card *card = dev_get_drvdata(dev);
139         struct azx *chip = card->private_data;
140         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
141         struct i915_audio_component *acomp = &hda->audio_component;
142
143         module_put(acomp->ops->owner);
144         component_unbind_all(dev, acomp);
145         WARN_ON(acomp->ops || acomp->dev);
146 }
147
148 static const struct component_master_ops hda_component_master_ops = {
149         .bind = hda_component_master_bind,
150         .unbind = hda_component_master_unbind,
151 };
152
153 static int hda_component_master_match(struct device *dev, void *data)
154 {
155         /* i915 is the only supported component */
156         return !strcmp(dev->driver->name, "i915");
157 }
158
159 int hda_i915_init(struct hda_intel *hda)
160 {
161         struct component_match *match = NULL;
162         struct device *dev = &hda->chip.pci->dev;
163         struct i915_audio_component *acomp = &hda->audio_component;
164         int ret;
165
166         component_match_add(dev, &match, hda_component_master_match, hda);
167         ret = component_master_add_with_match(dev, &hda_component_master_ops,
168                                               match);
169         if (ret < 0)
170                 goto out_err;
171
172         /*
173          * Atm, we don't support deferring the component binding, so make sure
174          * i915 is loaded and that the binding successfully completes.
175          */
176         request_module("i915");
177
178         if (!acomp->ops) {
179                 ret = -ENODEV;
180                 goto out_master_del;
181         }
182
183         dev_dbg(dev, "bound to i915 component master\n");
184
185         return 0;
186 out_master_del:
187         component_master_del(dev, &hda_component_master_ops);
188 out_err:
189         dev_err(dev, "failed to add i915 component master (%d)\n", ret);
190
191         return ret;
192 }
193
194 int hda_i915_exit(struct hda_intel *hda)
195 {
196         struct device *dev = &hda->chip.pci->dev;
197         struct i915_audio_component *acomp = &hda->audio_component;
198
199         WARN_ON(hda->i915_power_refcount);
200         if (hda->i915_power_refcount > 0 && acomp->ops)
201                 acomp->ops->put_power(acomp->dev);
202
203         component_master_del(dev, &hda_component_master_ops);
204
205         return 0;
206 }