2 * Copyright 2015 Martin Peres
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Martin Peres
26 #include <subdev/bios.h>
27 #include <subdev/bios/extdev.h>
28 #include <subdev/bios/iccsense.h>
29 #include <subdev/i2c.h>
32 nvkm_iccsense_validate_device(struct i2c_adapter
*i2c
, u8 addr
,
33 enum nvbios_extdev_type type
, u8 rail
)
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
:
41 nv_rd16i2cr(i2c
, addr
, 0xff) == 0x3220 &&
42 nv_rd16i2cr(i2c
, addr
, 0xfe) == 0x5449;
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
,
53 int vshunt
= nv_rd16i2cr(i2c
, addr
, shunt_reg
);
54 int vbus
= nv_rd16i2cr(i2c
, addr
, bus_reg
);
56 if (vshunt
< 0 || vbus
< 0)
59 vshunt
>>= shunt_shift
;
62 return vbus
* vshunt
* lsb
/ shunt
;
66 nvkm_iccsense_ina2x9_read(struct nvkm_iccsense
*iccsense
,
67 struct nvkm_iccsense_rail
*rail
,
68 u8 shunt_reg
, u8 bus_reg
)
70 return nvkm_iccsense_poll_lane(rail
->i2c
, rail
->addr
, shunt_reg
, 0,
71 bus_reg
, 3, rail
->mohm
, 10 * 4);
75 nvkm_iccsense_ina209_read(struct nvkm_iccsense
*iccsense
,
76 struct nvkm_iccsense_rail
*rail
)
78 return nvkm_iccsense_ina2x9_read(iccsense
, rail
, 3, 4);
82 nvkm_iccsense_ina219_read(struct nvkm_iccsense
*iccsense
,
83 struct nvkm_iccsense_rail
*rail
)
85 return nvkm_iccsense_ina2x9_read(iccsense
, rail
, 1, 2);
89 nvkm_iccsense_ina3221_read(struct nvkm_iccsense
*iccsense
,
90 struct nvkm_iccsense_rail
*rail
)
92 return nvkm_iccsense_poll_lane(rail
->i2c
, rail
->addr
,
93 1 + (rail
->rail
* 2), 3,
94 2 + (rail
->rail
* 2), 3, rail
->mohm
,
99 nvkm_iccsense_read(struct nvkm_iccsense
*iccsense
, u8 idx
)
101 struct nvkm_iccsense_rail
*rail
;
103 if (!iccsense
|| idx
>= iccsense
->rail_count
)
106 rail
= &iccsense
->rails
[idx
];
110 return rail
->read(iccsense
, rail
);
114 nvkm_iccsense_read_all(struct nvkm_iccsense
*iccsense
)
117 for (i
= 0; i
< iccsense
->rail_count
; ++i
) {
118 int res
= nvkm_iccsense_read(iccsense
, i
);
128 nvkm_iccsense_dtor(struct nvkm_subdev
*subdev
)
130 struct nvkm_iccsense
*iccsense
= nvkm_iccsense(subdev
);
133 kfree(iccsense
->rails
);
139 nvkm_iccsense_oneinit(struct nvkm_subdev
*subdev
)
141 struct nvkm_iccsense
*iccsense
= nvkm_iccsense(subdev
);
142 struct nvkm_bios
*bios
= subdev
->device
->bios
;
143 struct nvkm_i2c
*i2c
= subdev
->device
->i2c
;
144 struct nvbios_iccsense stbl
;
147 if (!i2c
|| !bios
|| nvbios_iccsense_parse(bios
, &stbl
)
151 iccsense
->rails
= kmalloc(sizeof(*iccsense
->rails
) * stbl
.nr_entry
,
153 if (!iccsense
->rails
)
156 iccsense
->data_valid
= true;
157 for (i
= 0; i
< stbl
.nr_entry
; ++i
) {
158 struct pwr_rail_t
*r
= &stbl
.rail
[i
];
159 struct nvbios_extdev_func extdev
;
160 struct nvkm_iccsense_rail
*rail
;
161 struct nvkm_i2c_bus
*i2c_bus
;
164 if (!r
->mode
|| r
->resistor_mohm
== 0)
167 if (nvbios_extdev_parse(bios
, r
->extdev_id
, &extdev
))
170 if (extdev
.type
== 0xff)
174 i2c_bus
= nvkm_i2c_bus_find(i2c
, NVKM_I2C_BUS_SEC
);
176 i2c_bus
= nvkm_i2c_bus_find(i2c
, NVKM_I2C_BUS_PRI
);
180 addr
= extdev
.addr
>> 1;
181 if (!nvkm_iccsense_validate_device(&i2c_bus
->i2c
, addr
,
182 extdev
.type
, r
->rail
)) {
183 iccsense
->data_valid
= false;
184 nvkm_warn(subdev
, "found unknown or invalid rail entry"
185 " type 0x%x rail %i, power reading might be"
186 " invalid\n", extdev
.type
, r
->rail
);
190 rail
= &iccsense
->rails
[iccsense
->rail_count
];
191 switch (extdev
.type
) {
192 case NVBIOS_EXTDEV_INA209
:
193 rail
->read
= nvkm_iccsense_ina209_read
;
195 case NVBIOS_EXTDEV_INA219
:
196 rail
->read
= nvkm_iccsense_ina219_read
;
198 case NVBIOS_EXTDEV_INA3221
:
199 rail
->read
= nvkm_iccsense_ina3221_read
;
204 rail
->rail
= r
->rail
;
205 rail
->mohm
= r
->resistor_mohm
;
206 rail
->i2c
= &i2c_bus
->i2c
;
207 ++iccsense
->rail_count
;
212 struct nvkm_subdev_func iccsense_func
= {
213 .oneinit
= nvkm_iccsense_oneinit
,
214 .dtor
= nvkm_iccsense_dtor
,
218 nvkm_iccsense_ctor(struct nvkm_device
*device
, int index
,
219 struct nvkm_iccsense
*iccsense
)
221 nvkm_subdev_ctor(&iccsense_func
, device
, index
, 0, &iccsense
->subdev
);
225 nvkm_iccsense_new_(struct nvkm_device
*device
, int index
,
226 struct nvkm_iccsense
**iccsense
)
228 if (!(*iccsense
= kzalloc(sizeof(**iccsense
), GFP_KERNEL
)))
230 nvkm_iccsense_ctor(device
, index
, *iccsense
);