Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nv40.c
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  * Martin Peres
24  */
25 
26 #include "priv.h"
27 
28 static int
29 nv40_sensor_setup(struct nouveau_therm *therm)
30 {
31  struct nouveau_device *device = nv_device(therm);
32 
33  /* enable ADC readout and disable the ALARM threshold */
34  if (device->chipset >= 0x46) {
35  nv_mask(therm, 0x15b8, 0x80000000, 0);
36  nv_wr32(therm, 0x15b0, 0x80003fff);
37  return nv_rd32(therm, 0x15b4) & 0x3fff;
38  } else {
39  nv_wr32(therm, 0x15b0, 0xff);
40  return nv_rd32(therm, 0x15b4) & 0xff;
41  }
42 }
43 
44 static int
45 nv40_temp_get(struct nouveau_therm *therm)
46 {
47  struct nouveau_therm_priv *priv = (void *)therm;
48  struct nouveau_device *device = nv_device(therm);
49  struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
50  int core_temp;
51 
52  if (device->chipset >= 0x46) {
53  nv_wr32(therm, 0x15b0, 0x80003fff);
54  core_temp = nv_rd32(therm, 0x15b4) & 0x3fff;
55  } else {
56  nv_wr32(therm, 0x15b0, 0xff);
57  core_temp = nv_rd32(therm, 0x15b4) & 0xff;
58  }
59 
60  /* Setup the sensor if the temperature is 0 */
61  if (core_temp == 0)
62  core_temp = nv40_sensor_setup(therm);
63 
64  if (sensor->slope_div == 0)
65  sensor->slope_div = 1;
66  if (sensor->offset_den == 0)
67  sensor->offset_den = 1;
68  if (sensor->slope_mult < 1)
69  sensor->slope_mult = 1;
70 
71  core_temp = core_temp * sensor->slope_mult / sensor->slope_div;
72  core_temp = core_temp + sensor->offset_num / sensor->offset_den;
73  core_temp = core_temp + sensor->offset_constant - 8;
74 
75  return core_temp;
76 }
77 
78 int
79 nv40_fan_pwm_get(struct nouveau_therm *therm, int line, u32 *divs, u32 *duty)
80 {
81  if (line == 2) {
82  u32 reg = nv_rd32(therm, 0x0010f0);
83  if (reg & 0x80000000) {
84  *duty = (reg & 0x7fff0000) >> 16;
85  *divs = (reg & 0x00007fff);
86  return 0;
87  }
88  } else
89  if (line == 9) {
90  u32 reg = nv_rd32(therm, 0x0015f4);
91  if (reg & 0x80000000) {
92  *divs = nv_rd32(therm, 0x0015f8);
93  *duty = (reg & 0x7fffffff);
94  return 0;
95  }
96  } else {
97  nv_error(therm, "unknown pwm ctrl for gpio %d\n", line);
98  return -ENODEV;
99  }
100 
101  return -EINVAL;
102 }
103 
104 int
105 nv40_fan_pwm_set(struct nouveau_therm *therm, int line, u32 divs, u32 duty)
106 {
107  if (line == 2) {
108  nv_wr32(therm, 0x0010f0, 0x80000000 | (duty << 16) | divs);
109  } else
110  if (line == 9) {
111  nv_wr32(therm, 0x0015f8, divs);
112  nv_wr32(therm, 0x0015f4, duty | 0x80000000);
113  } else {
114  nv_error(therm, "unknown pwm ctrl for gpio %d\n", line);
115  return -ENODEV;
116  }
117 
118  return 0;
119 }
120 
121 static int
122 nv40_therm_ctor(struct nouveau_object *parent,
123  struct nouveau_object *engine,
124  struct nouveau_oclass *oclass, void *data, u32 size,
125  struct nouveau_object **pobject)
126 {
127  struct nouveau_therm_priv *priv;
128  struct nouveau_therm *therm;
129  int ret;
130 
131  ret = nouveau_therm_create(parent, engine, oclass, &priv);
132  *pobject = nv_object(priv);
133  therm = (void *) priv;
134  if (ret)
135  return ret;
136 
137  nouveau_therm_ic_ctor(therm);
139  nouveau_therm_fan_ctor(therm);
140 
141  priv->fan.pwm_get = nv40_fan_pwm_get;
142  priv->fan.pwm_set = nv40_fan_pwm_set;
143 
144  therm->temp_get = nv40_temp_get;
150 
151  return 0;
152 }
153 
154 struct nouveau_oclass
156  .handle = NV_SUBDEV(THERM, 0x40),
157  .ofuncs = &(struct nouveau_ofuncs) {
158  .ctor = nv40_therm_ctor,
159  .dtor = _nouveau_therm_dtor,
160  .init = nouveau_therm_init,
161  .fini = nouveau_therm_fini,
162  },
163 };