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 static void 99 nvkm_iccsense_sensor_config(struct nvkm_iccsense *iccsense, 100 struct nvkm_iccsense_sensor *sensor) 101 { 102 struct nvkm_subdev *subdev = &iccsense->subdev; 103 nvkm_trace(subdev, "write config of extdev %i: 0x%04x\n", sensor->id, sensor->config); 104 nv_wr16i2cr(sensor->i2c, sensor->addr, 0x00, sensor->config); 105 } 106 107 int 108 nvkm_iccsense_read_all(struct nvkm_iccsense *iccsense) 109 { 110 int result = 0; 111 struct nvkm_iccsense_rail *rail; 112 113 if (!iccsense) 114 return -EINVAL; 115 116 list_for_each_entry(rail, &iccsense->rails, head) { 117 int res; 118 if (!rail->read) 119 return -ENODEV; 120 121 res = rail->read(iccsense, rail); 122 if (res < 0) 123 return res; 124 result += res; 125 } 126 return result; 127 } 128 129 static void * 130 nvkm_iccsense_dtor(struct nvkm_subdev *subdev) 131 { 132 struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev); 133 struct nvkm_iccsense_sensor *sensor, *tmps; 134 struct nvkm_iccsense_rail *rail, *tmpr; 135 136 list_for_each_entry_safe(sensor, tmps, &iccsense->sensors, head) { 137 list_del(&sensor->head); 138 kfree(sensor); 139 } 140 list_for_each_entry_safe(rail, tmpr, &iccsense->rails, head) { 141 list_del(&rail->head); 142 kfree(rail); 143 } 144 145 return iccsense; 146 } 147 148 static struct nvkm_iccsense_sensor* 149 nvkm_iccsense_create_sensor(struct nvkm_iccsense *iccsense, u8 id) 150 { 151 struct nvkm_subdev *subdev = &iccsense->subdev; 152 struct nvkm_bios *bios = subdev->device->bios; 153 struct nvkm_i2c *i2c = subdev->device->i2c; 154 struct nvbios_extdev_func extdev; 155 struct nvkm_i2c_bus *i2c_bus; 156 struct nvkm_iccsense_sensor *sensor; 157 u8 addr; 158 159 if (!i2c || !bios || nvbios_extdev_parse(bios, id, &extdev)) 160 return NULL; 161 162 if (extdev.type == 0xff) 163 return NULL; 164 165 if (extdev.type != NVBIOS_EXTDEV_INA209 && 166 extdev.type != NVBIOS_EXTDEV_INA219 && 167 extdev.type != NVBIOS_EXTDEV_INA3221) { 168 iccsense->data_valid = false; 169 nvkm_error(subdev, "Unknown sensor type %x, power reading " 170 "disabled\n", extdev.type); 171 return NULL; 172 } 173 174 if (extdev.bus) 175 i2c_bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_SEC); 176 else 177 i2c_bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_PRI); 178 if (!i2c_bus) 179 return NULL; 180 181 addr = extdev.addr >> 1; 182 if (!nvkm_iccsense_validate_device(&i2c_bus->i2c, addr, 183 extdev.type)) { 184 iccsense->data_valid = false; 185 nvkm_warn(subdev, "found invalid sensor id: %i, power reading" 186 "might be invalid\n", id); 187 return NULL; 188 } 189 190 sensor = kmalloc(sizeof(*sensor), GFP_KERNEL); 191 if (!sensor) 192 return NULL; 193 194 list_add_tail(&sensor->head, &iccsense->sensors); 195 sensor->id = id; 196 sensor->type = extdev.type; 197 sensor->i2c = &i2c_bus->i2c; 198 sensor->addr = addr; 199 sensor->config = 0x0; 200 return sensor; 201 } 202 203 static struct nvkm_iccsense_sensor* 204 nvkm_iccsense_get_sensor(struct nvkm_iccsense *iccsense, u8 id) 205 { 206 struct nvkm_iccsense_sensor *sensor; 207 list_for_each_entry(sensor, &iccsense->sensors, head) { 208 if (sensor->id == id) 209 return sensor; 210 } 211 return nvkm_iccsense_create_sensor(iccsense, id); 212 } 213 214 static int 215 nvkm_iccsense_oneinit(struct nvkm_subdev *subdev) 216 { 217 struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev); 218 struct nvkm_bios *bios = subdev->device->bios; 219 struct nvbios_iccsense stbl; 220 int i; 221 222 if (!bios || nvbios_iccsense_parse(bios, &stbl) || !stbl.nr_entry) 223 return 0; 224 225 iccsense->data_valid = true; 226 for (i = 0; i < stbl.nr_entry; ++i) { 227 struct pwr_rail_t *pwr_rail = &stbl.rail[i]; 228 struct nvkm_iccsense_sensor *sensor; 229 int r; 230 231 if (pwr_rail->mode != 1 || !pwr_rail->resistor_count) 232 continue; 233 234 sensor = nvkm_iccsense_get_sensor(iccsense, pwr_rail->extdev_id); 235 if (!sensor) 236 continue; 237 238 if (!sensor->config) 239 sensor->config = pwr_rail->config; 240 else if (sensor->config != pwr_rail->config) 241 nvkm_error(subdev, "config mismatch found for extdev %i\n", pwr_rail->extdev_id); 242 243 for (r = 0; r < pwr_rail->resistor_count; ++r) { 244 struct nvkm_iccsense_rail *rail; 245 struct pwr_rail_resistor_t *res = &pwr_rail->resistors[r]; 246 int (*read)(struct nvkm_iccsense *, 247 struct nvkm_iccsense_rail *); 248 249 if (!res->mohm || !res->enabled) 250 continue; 251 252 switch (sensor->type) { 253 case NVBIOS_EXTDEV_INA209: 254 read = nvkm_iccsense_ina209_read; 255 break; 256 case NVBIOS_EXTDEV_INA219: 257 read = nvkm_iccsense_ina219_read; 258 break; 259 case NVBIOS_EXTDEV_INA3221: 260 read = nvkm_iccsense_ina3221_read; 261 break; 262 default: 263 continue; 264 } 265 266 rail = kmalloc(sizeof(*rail), GFP_KERNEL); 267 if (!rail) 268 return -ENOMEM; 269 270 rail->read = read; 271 rail->sensor = sensor; 272 rail->idx = r; 273 rail->mohm = res->mohm; 274 nvkm_debug(subdev, "create rail for extdev %i: { idx: %i, mohm: %i }\n", pwr_rail->extdev_id, r, rail->mohm); 275 list_add_tail(&rail->head, &iccsense->rails); 276 } 277 } 278 return 0; 279 } 280 281 static int 282 nvkm_iccsense_init(struct nvkm_subdev *subdev) 283 { 284 struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev); 285 struct nvkm_iccsense_sensor *sensor; 286 list_for_each_entry(sensor, &iccsense->sensors, head) 287 nvkm_iccsense_sensor_config(iccsense, sensor); 288 return 0; 289 } 290 291 static const struct nvkm_subdev_func 292 iccsense_func = { 293 .oneinit = nvkm_iccsense_oneinit, 294 .init = nvkm_iccsense_init, 295 .dtor = nvkm_iccsense_dtor, 296 }; 297 298 void 299 nvkm_iccsense_ctor(struct nvkm_device *device, int index, 300 struct nvkm_iccsense *iccsense) 301 { 302 nvkm_subdev_ctor(&iccsense_func, device, index, &iccsense->subdev); 303 } 304 305 int 306 nvkm_iccsense_new_(struct nvkm_device *device, int index, 307 struct nvkm_iccsense **iccsense) 308 { 309 if (!(*iccsense = kzalloc(sizeof(**iccsense), GFP_KERNEL))) 310 return -ENOMEM; 311 INIT_LIST_HEAD(&(*iccsense)->sensors); 312 INIT_LIST_HEAD(&(*iccsense)->rails); 313 nvkm_iccsense_ctor(device, index, *iccsense); 314 return 0; 315 } 316