03eef9d2872ce9a12f4f42f2eb2abc1990ad63ee
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / iccsense / base.c
1 /*
2 * Copyright 2015 Martin Peres
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: Martin Peres
23 */
24 #include "priv.h"
25
26 #include <subdev/bios.h>
27 #include <subdev/bios/extdev.h>
28 #include <subdev/bios/iccsense.h>
29 #include <subdev/i2c.h>
30
31 static bool
32 nvkm_iccsense_validate_device(struct i2c_adapter *i2c, u8 addr,
33 enum nvbios_extdev_type type)
34 {
35 switch (type) {
36 case NVBIOS_EXTDEV_INA209:
37 case NVBIOS_EXTDEV_INA219:
38 return nv_rd16i2cr(i2c, addr, 0x0) >= 0;
39 case NVBIOS_EXTDEV_INA3221:
40 return nv_rd16i2cr(i2c, addr, 0xff) == 0x3220 &&
41 nv_rd16i2cr(i2c, addr, 0xfe) == 0x5449;
42 default:
43 return false;
44 }
45 }
46
47 static int
48 nvkm_iccsense_poll_lane(struct i2c_adapter *i2c, u8 addr, u8 shunt_reg,
49 u8 shunt_shift, u8 bus_reg, u8 bus_shift, u8 shunt,
50 u16 lsb)
51 {
52 int vshunt = nv_rd16i2cr(i2c, addr, shunt_reg);
53 int vbus = nv_rd16i2cr(i2c, addr, bus_reg);
54
55 if (vshunt < 0 || vbus < 0)
56 return -EINVAL;
57
58 vshunt >>= shunt_shift;
59 vbus >>= bus_shift;
60
61 return vbus * vshunt * lsb / shunt;
62 }
63
64 static int
65 nvkm_iccsense_ina2x9_read(struct nvkm_iccsense *iccsense,
66 struct nvkm_iccsense_rail *rail,
67 u8 shunt_reg, u8 bus_reg)
68 {
69 return nvkm_iccsense_poll_lane(rail->sensor->i2c, rail->sensor->addr,
70 shunt_reg, 0, bus_reg, 3, rail->mohm,
71 10 * 4);
72 }
73
74 static int
75 nvkm_iccsense_ina209_read(struct nvkm_iccsense *iccsense,
76 struct nvkm_iccsense_rail *rail)
77 {
78 return nvkm_iccsense_ina2x9_read(iccsense, rail, 3, 4);
79 }
80
81 static int
82 nvkm_iccsense_ina219_read(struct nvkm_iccsense *iccsense,
83 struct nvkm_iccsense_rail *rail)
84 {
85 return nvkm_iccsense_ina2x9_read(iccsense, rail, 1, 2);
86 }
87
88 static int
89 nvkm_iccsense_ina3221_read(struct nvkm_iccsense *iccsense,
90 struct nvkm_iccsense_rail *rail)
91 {
92 return nvkm_iccsense_poll_lane(rail->sensor->i2c, rail->sensor->addr,
93 1 + (rail->idx * 2), 3,
94 2 + (rail->idx * 2), 3, rail->mohm,
95 40 * 8);
96 }
97
98 int
99 nvkm_iccsense_read_all(struct nvkm_iccsense *iccsense)
100 {
101 int result = 0;
102 struct nvkm_iccsense_rail *rail;
103
104 if (!iccsense)
105 return -EINVAL;
106
107 list_for_each_entry(rail, &iccsense->rails, head) {
108 int res;
109 if (!rail->read)
110 return -ENODEV;
111
112 res = rail->read(iccsense, rail);
113 if (res < 0)
114 return res;
115 result += res;
116 }
117 return result;
118 }
119
120 static void *
121 nvkm_iccsense_dtor(struct nvkm_subdev *subdev)
122 {
123 struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev);
124 struct nvkm_iccsense_sensor *sensor, *tmps;
125 struct nvkm_iccsense_rail *rail, *tmpr;
126
127 list_for_each_entry_safe(sensor, tmps, &iccsense->sensors, head) {
128 list_del(&sensor->head);
129 kfree(sensor);
130 }
131 list_for_each_entry_safe(rail, tmpr, &iccsense->rails, head) {
132 list_del(&rail->head);
133 kfree(rail);
134 }
135
136 return iccsense;
137 }
138
139 static struct nvkm_iccsense_sensor*
140 nvkm_iccsense_create_sensor(struct nvkm_iccsense *iccsense, u8 id)
141 {
142
143 struct nvkm_subdev *subdev = &iccsense->subdev;
144 struct nvkm_bios *bios = subdev->device->bios;
145 struct nvkm_i2c *i2c = subdev->device->i2c;
146 struct nvbios_extdev_func extdev;
147 struct nvkm_i2c_bus *i2c_bus;
148 struct nvkm_iccsense_sensor *sensor;
149 u8 addr;
150
151 if (!i2c || !bios || nvbios_extdev_parse(bios, id, &extdev))
152 return NULL;
153
154 if (extdev.type == 0xff)
155 return NULL;
156
157 if (extdev.type != NVBIOS_EXTDEV_INA209 &&
158 extdev.type != NVBIOS_EXTDEV_INA219 &&
159 extdev.type != NVBIOS_EXTDEV_INA3221) {
160 iccsense->data_valid = false;
161 nvkm_error(subdev, "Unknown sensor type %x, power reading "
162 "disabled\n", extdev.type);
163 return NULL;
164 }
165
166 if (extdev.bus)
167 i2c_bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_SEC);
168 else
169 i2c_bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_PRI);
170 if (!i2c_bus)
171 return NULL;
172
173 addr = extdev.addr >> 1;
174 if (!nvkm_iccsense_validate_device(&i2c_bus->i2c, addr,
175 extdev.type)) {
176 iccsense->data_valid = false;
177 nvkm_warn(subdev, "found invalid sensor id: %i, power reading"
178 "might be invalid\n", id);
179 return NULL;
180 }
181
182 sensor = kmalloc(sizeof(*sensor), GFP_KERNEL);
183 if (!sensor)
184 return NULL;
185
186 list_add_tail(&sensor->head, &iccsense->sensors);
187 sensor->id = id;
188 sensor->type = extdev.type;
189 sensor->i2c = &i2c_bus->i2c;
190 sensor->addr = addr;
191 sensor->rail_mask = 0x0;
192 return sensor;
193 }
194
195 static struct nvkm_iccsense_sensor*
196 nvkm_iccsense_get_sensor(struct nvkm_iccsense *iccsense, u8 id)
197 {
198 struct nvkm_iccsense_sensor *sensor;
199 list_for_each_entry(sensor, &iccsense->sensors, head) {
200 if (sensor->id == id)
201 return sensor;
202 }
203 return nvkm_iccsense_create_sensor(iccsense, id);
204 }
205
206 static int
207 nvkm_iccsense_oneinit(struct nvkm_subdev *subdev)
208 {
209 struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev);
210 struct nvkm_bios *bios = subdev->device->bios;
211 struct nvbios_iccsense stbl;
212 int i;
213
214 if (!bios || nvbios_iccsense_parse(bios, &stbl) || !stbl.nr_entry)
215 return 0;
216
217 iccsense->data_valid = true;
218 for (i = 0; i < stbl.nr_entry; ++i) {
219 struct pwr_rail_t *r = &stbl.rail[i];
220 struct nvkm_iccsense_rail *rail;
221 struct nvkm_iccsense_sensor *sensor;
222
223 if (!r->mode || r->resistor_mohm == 0)
224 continue;
225
226 sensor = nvkm_iccsense_get_sensor(iccsense, r->extdev_id);
227 if (!sensor)
228 continue;
229
230 rail = kmalloc(sizeof(*rail), GFP_KERNEL);
231 if (!rail)
232 return -ENOMEM;
233
234 switch (sensor->type) {
235 case NVBIOS_EXTDEV_INA209:
236 if (r->rail != 0)
237 continue;
238 rail->read = nvkm_iccsense_ina209_read;
239 break;
240 case NVBIOS_EXTDEV_INA219:
241 if (r->rail != 0)
242 continue;
243 rail->read = nvkm_iccsense_ina219_read;
244 break;
245 case NVBIOS_EXTDEV_INA3221:
246 if (r->rail >= 3)
247 continue;
248 rail->read = nvkm_iccsense_ina3221_read;
249 break;
250 default:
251 continue;
252 }
253
254 sensor->rail_mask |= 1 << r->rail;
255 rail->sensor = sensor;
256 rail->idx = r->rail;
257 rail->mohm = r->resistor_mohm;
258 list_add_tail(&rail->head, &iccsense->rails);
259 }
260 return 0;
261 }
262
263 struct nvkm_subdev_func iccsense_func = {
264 .oneinit = nvkm_iccsense_oneinit,
265 .dtor = nvkm_iccsense_dtor,
266 };
267
268 void
269 nvkm_iccsense_ctor(struct nvkm_device *device, int index,
270 struct nvkm_iccsense *iccsense)
271 {
272 nvkm_subdev_ctor(&iccsense_func, device, index, 0, &iccsense->subdev);
273 }
274
275 int
276 nvkm_iccsense_new_(struct nvkm_device *device, int index,
277 struct nvkm_iccsense **iccsense)
278 {
279 if (!(*iccsense = kzalloc(sizeof(**iccsense), GFP_KERNEL)))
280 return -ENOMEM;
281 INIT_LIST_HEAD(&(*iccsense)->sensors);
282 INIT_LIST_HEAD(&(*iccsense)->rails);
283 nvkm_iccsense_ctor(device, index, *iccsense);
284 return 0;
285 }
This page took 0.037613 seconds and 4 git commands to generate.