base.c 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright 2015 Martin Peres
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Martin Peres
 */
#include "priv.h"

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
#include <subdev/bios.h>
#include <subdev/bios/extdev.h>
#include <subdev/bios/iccsense.h>
#include <subdev/i2c.h>

static bool
nvkm_iccsense_validate_device(struct i2c_adapter *i2c, u8 addr,
			      enum nvbios_extdev_type type, u8 rail)
{
	switch (type) {
	case NVBIOS_EXTDEV_INA209:
	case NVBIOS_EXTDEV_INA219:
		return rail == 0 && nv_rd16i2cr(i2c, addr, 0x0) >= 0;
	case NVBIOS_EXTDEV_INA3221:
		return rail <= 3 &&
		       nv_rd16i2cr(i2c, addr, 0xff) == 0x3220 &&
		       nv_rd16i2cr(i2c, addr, 0xfe) == 0x5449;
	default:
		return false;
	}
}

static int
nvkm_iccsense_poll_lane(struct i2c_adapter *i2c, u8 addr, u8 shunt_reg,
			u8 shunt_shift, u8 bus_reg, u8 bus_shift, u8 shunt,
			u16 lsb)
{
	int vshunt = nv_rd16i2cr(i2c, addr, shunt_reg);
	int vbus = nv_rd16i2cr(i2c, addr, bus_reg);

	if (vshunt < 0 || vbus < 0)
		return -EINVAL;

	vshunt >>= shunt_shift;
	vbus >>= bus_shift;

	return vbus * vshunt * lsb / shunt;
}

static int
nvkm_iccsense_ina2x9_read(struct nvkm_iccsense *iccsense,
                          struct nvkm_iccsense_rail *rail,
			  u8 shunt_reg, u8 bus_reg)
{
	return nvkm_iccsense_poll_lane(rail->i2c, rail->addr, shunt_reg, 0,
				       bus_reg, 3, rail->mohm, 10 * 4);
}

static int
nvkm_iccsense_ina209_read(struct nvkm_iccsense *iccsense,
			  struct nvkm_iccsense_rail *rail)
{
	return nvkm_iccsense_ina2x9_read(iccsense, rail, 3, 4);
}

static int
nvkm_iccsense_ina219_read(struct nvkm_iccsense *iccsense,
			  struct nvkm_iccsense_rail *rail)
{
	return nvkm_iccsense_ina2x9_read(iccsense, rail, 1, 2);
}

static int
nvkm_iccsense_ina3221_read(struct nvkm_iccsense *iccsense,
			   struct nvkm_iccsense_rail *rail)
{
	return nvkm_iccsense_poll_lane(rail->i2c, rail->addr,
				       1 + (rail->rail * 2), 3,
				       2 + (rail->rail * 2), 3, rail->mohm,
				       40 * 8);
}

int
nvkm_iccsense_read(struct nvkm_iccsense *iccsense, u8 idx)
{
	struct nvkm_iccsense_rail *rail;

	if (!iccsense || idx >= iccsense->rail_count)
		return -EINVAL;

	rail = &iccsense->rails[idx];
	if (!rail->read)
		return -ENODEV;

	return rail->read(iccsense, rail);
}

int
nvkm_iccsense_read_all(struct nvkm_iccsense *iccsense)
{
	int result = 0, i;
	for (i = 0; i < iccsense->rail_count; ++i) {
		int res = nvkm_iccsense_read(iccsense, i);
		if (res >= 0)
			result += res;
		else
			return res;
	}
	return result;
}

static void *
nvkm_iccsense_dtor(struct nvkm_subdev *subdev)
{
	struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev);

	if (iccsense->rails)
		kfree(iccsense->rails);

	return iccsense;
}

static int
nvkm_iccsense_oneinit(struct nvkm_subdev *subdev)
{
	struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev);
	struct nvkm_bios *bios = subdev->device->bios;
	struct nvkm_i2c *i2c = subdev->device->i2c;
	struct nvbios_iccsense stbl;
	int i;

	if (!i2c || !bios || nvbios_iccsense_parse(bios, &stbl)
	    || !stbl.nr_entry)
		return 0;

	iccsense->rails = kmalloc(sizeof(*iccsense->rails) * stbl.nr_entry,
	                          GFP_KERNEL);
	if (!iccsense->rails)
		return -ENOMEM;

	iccsense->data_valid = true;
	for (i = 0; i < stbl.nr_entry; ++i) {
		struct pwr_rail_t *r = &stbl.rail[i];
		struct nvbios_extdev_func extdev;
		struct nvkm_iccsense_rail *rail;
		struct nvkm_i2c_bus *i2c_bus;
		u8 addr;

		if (!r->mode || r->resistor_mohm == 0)
			continue;

		if (nvbios_extdev_parse(bios, r->extdev_id, &extdev))
			continue;

		if (extdev.type == 0xff)
			continue;

		if (extdev.bus)
			i2c_bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_SEC);
		else
			i2c_bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_PRI);
		if (!i2c_bus)
			continue;

		addr = extdev.addr >> 1;
		if (!nvkm_iccsense_validate_device(&i2c_bus->i2c, addr,
						   extdev.type, r->rail)) {
			iccsense->data_valid = false;
			nvkm_warn(subdev, "found unknown or invalid rail entry"
				  " type 0x%x rail %i, power reading might be"
				  " invalid\n", extdev.type, r->rail);
			continue;
		}

		rail = &iccsense->rails[iccsense->rail_count];
		switch (extdev.type) {
		case NVBIOS_EXTDEV_INA209:
			rail->read = nvkm_iccsense_ina209_read;
			break;
		case NVBIOS_EXTDEV_INA219:
			rail->read = nvkm_iccsense_ina219_read;
			break;
		case NVBIOS_EXTDEV_INA3221:
			rail->read = nvkm_iccsense_ina3221_read;
			break;
		}

		rail->addr = addr;
		rail->rail = r->rail;
		rail->mohm = r->resistor_mohm;
		rail->i2c = &i2c_bus->i2c;
		++iccsense->rail_count;
	}
	return 0;
}

struct nvkm_subdev_func iccsense_func = {
	.oneinit = nvkm_iccsense_oneinit,
	.dtor = nvkm_iccsense_dtor,
};
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

void
nvkm_iccsense_ctor(struct nvkm_device *device, int index,
		   struct nvkm_iccsense *iccsense)
{
	nvkm_subdev_ctor(&iccsense_func, device, index, 0, &iccsense->subdev);
}

int
nvkm_iccsense_new_(struct nvkm_device *device, int index,
		   struct nvkm_iccsense **iccsense)
{
	if (!(*iccsense = kzalloc(sizeof(**iccsense), GFP_KERNEL)))
		return -ENOMEM;
	nvkm_iccsense_ctor(device, index, *iccsense);
	return 0;
}