base.c 8.3 KB

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