]>
Commit | Line | Data |
---|---|---|
dc06e366 MP |
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 | ||
b71c0892 KH |
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, u8 rail) | |
34 | { | |
35 | switch (type) { | |
36 | case NVBIOS_EXTDEV_INA209: | |
37 | case NVBIOS_EXTDEV_INA219: | |
38 | return rail == 0 && nv_rd16i2cr(i2c, addr, 0x0) >= 0; | |
39 | case NVBIOS_EXTDEV_INA3221: | |
40 | return rail <= 3 && | |
41 | nv_rd16i2cr(i2c, addr, 0xff) == 0x3220 && | |
42 | nv_rd16i2cr(i2c, addr, 0xfe) == 0x5449; | |
43 | default: | |
44 | return false; | |
45 | } | |
46 | } | |
47 | ||
48 | static int | |
49 | nvkm_iccsense_poll_lane(struct i2c_adapter *i2c, u8 addr, u8 shunt_reg, | |
50 | u8 shunt_shift, u8 bus_reg, u8 bus_shift, u8 shunt, | |
51 | u16 lsb) | |
52 | { | |
53 | int vshunt = nv_rd16i2cr(i2c, addr, shunt_reg); | |
54 | int vbus = nv_rd16i2cr(i2c, addr, bus_reg); | |
55 | ||
56 | if (vshunt < 0 || vbus < 0) | |
57 | return -EINVAL; | |
58 | ||
59 | vshunt >>= shunt_shift; | |
60 | vbus >>= bus_shift; | |
61 | ||
62 | return vbus * vshunt * lsb / shunt; | |
63 | } | |
64 | ||
65 | static int | |
66 | nvkm_iccsense_ina2x9_read(struct nvkm_iccsense *iccsense, | |
67 | struct nvkm_iccsense_rail *rail, | |
68 | u8 shunt_reg, u8 bus_reg) | |
69 | { | |
70 | return nvkm_iccsense_poll_lane(rail->i2c, rail->addr, shunt_reg, 0, | |
71 | bus_reg, 3, rail->mohm, 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->i2c, rail->addr, | |
93 | 1 + (rail->rail * 2), 3, | |
94 | 2 + (rail->rail * 2), 3, rail->mohm, | |
95 | 40 * 8); | |
96 | } | |
97 | ||
98 | int | |
d03e0f27 | 99 | nvkm_iccsense_read_all(struct nvkm_iccsense *iccsense) |
b71c0892 | 100 | { |
92224e75 KH |
101 | int result = 0; |
102 | struct nvkm_iccsense_rail *rail; | |
b71c0892 | 103 | |
d03e0f27 | 104 | if (!iccsense) |
b71c0892 KH |
105 | return -EINVAL; |
106 | ||
92224e75 | 107 | list_for_each_entry(rail, &iccsense->rails, head) { |
d03e0f27 | 108 | int res; |
d03e0f27 KH |
109 | if (!rail->read) |
110 | return -ENODEV; | |
111 | ||
112 | res = rail->read(iccsense, rail); | |
92224e75 | 113 | if (res < 0) |
b71c0892 | 114 | return res; |
92224e75 | 115 | result += res; |
b71c0892 KH |
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); | |
92224e75 | 124 | struct nvkm_iccsense_rail *rail, *tmp; |
b71c0892 | 125 | |
92224e75 KH |
126 | list_for_each_entry_safe(rail, tmp, &iccsense->rails, head) { |
127 | list_del(&rail->head); | |
128 | kfree(rail); | |
129 | } | |
b71c0892 KH |
130 | |
131 | return iccsense; | |
132 | } | |
133 | ||
134 | static int | |
135 | nvkm_iccsense_oneinit(struct nvkm_subdev *subdev) | |
136 | { | |
137 | struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev); | |
138 | struct nvkm_bios *bios = subdev->device->bios; | |
139 | struct nvkm_i2c *i2c = subdev->device->i2c; | |
140 | struct nvbios_iccsense stbl; | |
141 | int i; | |
142 | ||
143 | if (!i2c || !bios || nvbios_iccsense_parse(bios, &stbl) | |
144 | || !stbl.nr_entry) | |
145 | return 0; | |
146 | ||
b71c0892 KH |
147 | iccsense->data_valid = true; |
148 | for (i = 0; i < stbl.nr_entry; ++i) { | |
149 | struct pwr_rail_t *r = &stbl.rail[i]; | |
150 | struct nvbios_extdev_func extdev; | |
151 | struct nvkm_iccsense_rail *rail; | |
152 | struct nvkm_i2c_bus *i2c_bus; | |
153 | u8 addr; | |
154 | ||
155 | if (!r->mode || r->resistor_mohm == 0) | |
156 | continue; | |
157 | ||
158 | if (nvbios_extdev_parse(bios, r->extdev_id, &extdev)) | |
159 | continue; | |
160 | ||
161 | if (extdev.type == 0xff) | |
162 | continue; | |
163 | ||
164 | if (extdev.bus) | |
165 | i2c_bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_SEC); | |
166 | else | |
167 | i2c_bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_PRI); | |
168 | if (!i2c_bus) | |
169 | continue; | |
170 | ||
171 | addr = extdev.addr >> 1; | |
172 | if (!nvkm_iccsense_validate_device(&i2c_bus->i2c, addr, | |
173 | extdev.type, r->rail)) { | |
174 | iccsense->data_valid = false; | |
175 | nvkm_warn(subdev, "found unknown or invalid rail entry" | |
176 | " type 0x%x rail %i, power reading might be" | |
177 | " invalid\n", extdev.type, r->rail); | |
178 | continue; | |
179 | } | |
180 | ||
92224e75 KH |
181 | rail = kmalloc(sizeof(*rail), GFP_KERNEL); |
182 | if (!rail) | |
183 | return -ENOMEM; | |
184 | ||
b71c0892 KH |
185 | switch (extdev.type) { |
186 | case NVBIOS_EXTDEV_INA209: | |
187 | rail->read = nvkm_iccsense_ina209_read; | |
188 | break; | |
189 | case NVBIOS_EXTDEV_INA219: | |
190 | rail->read = nvkm_iccsense_ina219_read; | |
191 | break; | |
192 | case NVBIOS_EXTDEV_INA3221: | |
193 | rail->read = nvkm_iccsense_ina3221_read; | |
194 | break; | |
195 | } | |
196 | ||
197 | rail->addr = addr; | |
198 | rail->rail = r->rail; | |
199 | rail->mohm = r->resistor_mohm; | |
200 | rail->i2c = &i2c_bus->i2c; | |
92224e75 | 201 | list_add_tail(&rail->head, &iccsense->rails); |
b71c0892 KH |
202 | } |
203 | return 0; | |
204 | } | |
205 | ||
206 | struct nvkm_subdev_func iccsense_func = { | |
207 | .oneinit = nvkm_iccsense_oneinit, | |
208 | .dtor = nvkm_iccsense_dtor, | |
209 | }; | |
dc06e366 MP |
210 | |
211 | void | |
212 | nvkm_iccsense_ctor(struct nvkm_device *device, int index, | |
213 | struct nvkm_iccsense *iccsense) | |
214 | { | |
215 | nvkm_subdev_ctor(&iccsense_func, device, index, 0, &iccsense->subdev); | |
216 | } | |
217 | ||
218 | int | |
219 | nvkm_iccsense_new_(struct nvkm_device *device, int index, | |
220 | struct nvkm_iccsense **iccsense) | |
221 | { | |
222 | if (!(*iccsense = kzalloc(sizeof(**iccsense), GFP_KERNEL))) | |
223 | return -ENOMEM; | |
92224e75 | 224 | INIT_LIST_HEAD(&(*iccsense)->rails); |
dc06e366 MP |
225 | nvkm_iccsense_ctor(device, index, *iccsense); |
226 | return 0; | |
227 | } |