Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 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> |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 21 | #include <linux/pci.h> |
| 22 | #include <linux/component.h> |
| 23 | #include <drm/i915_component.h> |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 24 | #include <sound/core.h> |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 25 | #include "hda_priv.h" |
Imre Deak | 347de1f | 2015-01-08 17:54:15 +0200 | [diff] [blame] | 26 | #include "hda_intel.h" |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 27 | |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 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 | */ |
Takashi Iwai | 1a0e3f9 | 2014-07-04 07:48:57 +0200 | [diff] [blame] | 33 | #define AZX_REG_EM4 0x100c |
| 34 | #define AZX_REG_EM5 0x1010 |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 35 | |
Imre Deak | 926981a | 2015-01-08 17:54:16 +0200 | [diff] [blame] | 36 | int hda_display_power(struct hda_intel *hda, bool enable) |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 37 | { |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 38 | struct i915_audio_component *acomp = &hda->audio_component; |
| 39 | |
| 40 | if (!acomp->ops) |
Takashi Iwai | 74b0c2d | 2014-06-13 15:14:34 +0200 | [diff] [blame] | 41 | return -ENODEV; |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 42 | |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 43 | dev_dbg(&hda->chip.pci->dev, "display power %s\n", |
| 44 | enable ? "enable" : "disable"); |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 45 | if (enable) |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 46 | acomp->ops->get_power(acomp->dev); |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 47 | else |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 48 | acomp->ops->put_power(acomp->dev); |
| 49 | |
| 50 | return 0; |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Imre Deak | 926981a | 2015-01-08 17:54:16 +0200 | [diff] [blame] | 53 | void haswell_set_bclk(struct hda_intel *hda) |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 54 | { |
| 55 | int cdclk_freq; |
| 56 | unsigned int bclk_m, bclk_n; |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 57 | struct i915_audio_component *acomp = &hda->audio_component; |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 58 | |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 59 | if (!acomp->ops) |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 60 | return; |
| 61 | |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 62 | cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev); |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 63 | switch (cdclk_freq) { |
| 64 | case 337500: |
| 65 | bclk_m = 16; |
| 66 | bclk_n = 225; |
| 67 | break; |
| 68 | |
| 69 | case 450000: |
| 70 | default: /* default CDCLK 450MHz */ |
| 71 | bclk_m = 4; |
| 72 | bclk_n = 75; |
| 73 | break; |
| 74 | |
| 75 | case 540000: |
| 76 | bclk_m = 4; |
| 77 | bclk_n = 90; |
| 78 | break; |
| 79 | |
| 80 | case 675000: |
| 81 | bclk_m = 8; |
| 82 | bclk_n = 225; |
| 83 | break; |
| 84 | } |
| 85 | |
Imre Deak | 926981a | 2015-01-08 17:54:16 +0200 | [diff] [blame] | 86 | azx_writew(&hda->chip, EM4, bclk_m); |
| 87 | azx_writew(&hda->chip, EM5, bclk_n); |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 90 | static int hda_component_master_bind(struct device *dev) |
| 91 | { |
| 92 | struct snd_card *card = dev_get_drvdata(dev); |
| 93 | struct azx *chip = card->private_data; |
| 94 | struct hda_intel *hda = container_of(chip, struct hda_intel, chip); |
| 95 | struct i915_audio_component *acomp = &hda->audio_component; |
| 96 | int ret; |
| 97 | |
| 98 | ret = component_bind_all(dev, acomp); |
| 99 | if (ret < 0) |
| 100 | return ret; |
| 101 | |
| 102 | if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power && |
| 103 | acomp->ops->put_power && acomp->ops->get_cdclk_freq))) { |
| 104 | ret = -EINVAL; |
| 105 | goto out_unbind; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Atm, we don't support dynamic unbinding initiated by the child |
| 110 | * component, so pin its containing module until we unbind. |
| 111 | */ |
| 112 | if (!try_module_get(acomp->ops->owner)) { |
| 113 | ret = -ENODEV; |
| 114 | goto out_unbind; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | |
| 119 | out_unbind: |
| 120 | component_unbind_all(dev, acomp); |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static void hda_component_master_unbind(struct device *dev) |
| 126 | { |
| 127 | struct snd_card *card = dev_get_drvdata(dev); |
| 128 | struct azx *chip = card->private_data; |
| 129 | struct hda_intel *hda = container_of(chip, struct hda_intel, chip); |
| 130 | struct i915_audio_component *acomp = &hda->audio_component; |
| 131 | |
| 132 | module_put(acomp->ops->owner); |
| 133 | component_unbind_all(dev, acomp); |
| 134 | WARN_ON(acomp->ops || acomp->dev); |
| 135 | } |
| 136 | |
| 137 | static const struct component_master_ops hda_component_master_ops = { |
| 138 | .bind = hda_component_master_bind, |
| 139 | .unbind = hda_component_master_unbind, |
| 140 | }; |
| 141 | |
| 142 | static int hda_component_master_match(struct device *dev, void *data) |
| 143 | { |
| 144 | /* i915 is the only supported component */ |
| 145 | return !strcmp(dev->driver->name, "i915"); |
| 146 | } |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 147 | |
Imre Deak | 926981a | 2015-01-08 17:54:16 +0200 | [diff] [blame] | 148 | int hda_i915_init(struct hda_intel *hda) |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 149 | { |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 150 | struct component_match *match = NULL; |
| 151 | struct device *dev = &hda->chip.pci->dev; |
| 152 | struct i915_audio_component *acomp = &hda->audio_component; |
| 153 | int ret; |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 154 | |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 155 | component_match_add(dev, &match, hda_component_master_match, hda); |
| 156 | ret = component_master_add_with_match(dev, &hda_component_master_ops, |
| 157 | match); |
| 158 | if (ret < 0) |
| 159 | goto out_err; |
| 160 | |
| 161 | /* |
| 162 | * Atm, we don't support deferring the component binding, so make sure |
| 163 | * i915 is loaded and that the binding successfully completes. |
| 164 | */ |
| 165 | request_module("i915"); |
| 166 | |
| 167 | if (!acomp->ops) { |
| 168 | ret = -ENODEV; |
| 169 | goto out_master_del; |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 170 | } |
| 171 | |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 172 | dev_dbg(dev, "bound to i915 component master\n"); |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 173 | |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 174 | return 0; |
| 175 | out_master_del: |
| 176 | component_master_del(dev, &hda_component_master_ops); |
| 177 | out_err: |
| 178 | dev_err(dev, "failed to add i915 component master (%d)\n", ret); |
Mengdong Lin | e4d9e51 | 2014-07-03 17:02:23 +0800 | [diff] [blame] | 179 | |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 180 | return ret; |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 181 | } |
| 182 | |
Imre Deak | 926981a | 2015-01-08 17:54:16 +0200 | [diff] [blame] | 183 | int hda_i915_exit(struct hda_intel *hda) |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 184 | { |
Imre Deak | d7055bd | 2015-01-08 17:54:17 +0200 | [diff] [blame] | 185 | struct device *dev = &hda->chip.pci->dev; |
| 186 | |
| 187 | component_master_del(dev, &hda_component_master_ops); |
Wang Xingchao | 99a2008 | 2013-05-30 22:07:10 +0800 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |