zl6100.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Hardware monitoring driver for ZL6100 and compatibles
  3. *
  4. * Copyright (c) 2011 Ericsson AB.
  5. * Copyright (c) 2012 Guenter Roeck
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/err.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/ktime.h>
  28. #include <linux/delay.h>
  29. #include "pmbus.h"
  30. enum chips { zl2004, zl2005, zl2006, zl2008, zl2105, zl2106, zl6100, zl6105,
  31. zl9101, zl9117 };
  32. struct zl6100_data {
  33. int id;
  34. ktime_t access; /* chip access time */
  35. int delay; /* Delay between chip accesses in uS */
  36. struct pmbus_driver_info info;
  37. };
  38. #define to_zl6100_data(x) container_of(x, struct zl6100_data, info)
  39. #define ZL6100_MFR_CONFIG 0xd0
  40. #define ZL6100_DEVICE_ID 0xe4
  41. #define ZL6100_MFR_XTEMP_ENABLE (1 << 7)
  42. #define MFR_VMON_OV_FAULT_LIMIT 0xf5
  43. #define MFR_VMON_UV_FAULT_LIMIT 0xf6
  44. #define MFR_READ_VMON 0xf7
  45. #define VMON_UV_WARNING (1 << 5)
  46. #define VMON_OV_WARNING (1 << 4)
  47. #define VMON_UV_FAULT (1 << 1)
  48. #define VMON_OV_FAULT (1 << 0)
  49. #define ZL6100_WAIT_TIME 1000 /* uS */
  50. static ushort delay = ZL6100_WAIT_TIME;
  51. module_param(delay, ushort, 0644);
  52. MODULE_PARM_DESC(delay, "Delay between chip accesses in uS");
  53. /* Convert linear sensor value to milli-units */
  54. static long zl6100_l2d(s16 l)
  55. {
  56. s16 exponent;
  57. s32 mantissa;
  58. long val;
  59. exponent = l >> 11;
  60. mantissa = ((s16)((l & 0x7ff) << 5)) >> 5;
  61. val = mantissa;
  62. /* scale result to milli-units */
  63. val = val * 1000L;
  64. if (exponent >= 0)
  65. val <<= exponent;
  66. else
  67. val >>= -exponent;
  68. return val;
  69. }
  70. #define MAX_MANTISSA (1023 * 1000)
  71. #define MIN_MANTISSA (511 * 1000)
  72. static u16 zl6100_d2l(long val)
  73. {
  74. s16 exponent = 0, mantissa;
  75. bool negative = false;
  76. /* simple case */
  77. if (val == 0)
  78. return 0;
  79. if (val < 0) {
  80. negative = true;
  81. val = -val;
  82. }
  83. /* Reduce large mantissa until it fits into 10 bit */
  84. while (val >= MAX_MANTISSA && exponent < 15) {
  85. exponent++;
  86. val >>= 1;
  87. }
  88. /* Increase small mantissa to improve precision */
  89. while (val < MIN_MANTISSA && exponent > -15) {
  90. exponent--;
  91. val <<= 1;
  92. }
  93. /* Convert mantissa from milli-units to units */
  94. mantissa = DIV_ROUND_CLOSEST(val, 1000);
  95. /* Ensure that resulting number is within range */
  96. if (mantissa > 0x3ff)
  97. mantissa = 0x3ff;
  98. /* restore sign */
  99. if (negative)
  100. mantissa = -mantissa;
  101. /* Convert to 5 bit exponent, 11 bit mantissa */
  102. return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
  103. }
  104. /* Some chips need a delay between accesses */
  105. static inline void zl6100_wait(const struct zl6100_data *data)
  106. {
  107. if (data->delay) {
  108. s64 delta = ktime_us_delta(ktime_get(), data->access);
  109. if (delta < data->delay)
  110. udelay(data->delay - delta);
  111. }
  112. }
  113. static int zl6100_read_word_data(struct i2c_client *client, int page, int reg)
  114. {
  115. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  116. struct zl6100_data *data = to_zl6100_data(info);
  117. int ret, vreg;
  118. if (page > 0)
  119. return -ENXIO;
  120. if (data->id == zl2005) {
  121. /*
  122. * Limit register detection is not reliable on ZL2005.
  123. * Make sure registers are not erroneously detected.
  124. */
  125. switch (reg) {
  126. case PMBUS_VOUT_OV_WARN_LIMIT:
  127. case PMBUS_VOUT_UV_WARN_LIMIT:
  128. case PMBUS_IOUT_OC_WARN_LIMIT:
  129. return -ENXIO;
  130. }
  131. }
  132. switch (reg) {
  133. case PMBUS_VIRT_READ_VMON:
  134. vreg = MFR_READ_VMON;
  135. break;
  136. case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
  137. case PMBUS_VIRT_VMON_OV_FAULT_LIMIT:
  138. vreg = MFR_VMON_OV_FAULT_LIMIT;
  139. break;
  140. case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
  141. case PMBUS_VIRT_VMON_UV_FAULT_LIMIT:
  142. vreg = MFR_VMON_UV_FAULT_LIMIT;
  143. break;
  144. default:
  145. if (reg >= PMBUS_VIRT_BASE)
  146. return -ENXIO;
  147. vreg = reg;
  148. break;
  149. }
  150. zl6100_wait(data);
  151. ret = pmbus_read_word_data(client, page, vreg);
  152. data->access = ktime_get();
  153. if (ret < 0)
  154. return ret;
  155. switch (reg) {
  156. case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
  157. ret = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret) * 9, 10));
  158. break;
  159. case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
  160. ret = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(ret) * 11, 10));
  161. break;
  162. }
  163. return ret;
  164. }
  165. static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
  166. {
  167. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  168. struct zl6100_data *data = to_zl6100_data(info);
  169. int ret, status;
  170. if (page > 0)
  171. return -ENXIO;
  172. zl6100_wait(data);
  173. switch (reg) {
  174. case PMBUS_VIRT_STATUS_VMON:
  175. ret = pmbus_read_byte_data(client, 0,
  176. PMBUS_STATUS_MFR_SPECIFIC);
  177. if (ret < 0)
  178. break;
  179. status = 0;
  180. if (ret & VMON_UV_WARNING)
  181. status |= PB_VOLTAGE_UV_WARNING;
  182. if (ret & VMON_OV_WARNING)
  183. status |= PB_VOLTAGE_OV_WARNING;
  184. if (ret & VMON_UV_FAULT)
  185. status |= PB_VOLTAGE_UV_FAULT;
  186. if (ret & VMON_OV_FAULT)
  187. status |= PB_VOLTAGE_OV_FAULT;
  188. ret = status;
  189. break;
  190. default:
  191. ret = pmbus_read_byte_data(client, page, reg);
  192. break;
  193. }
  194. data->access = ktime_get();
  195. return ret;
  196. }
  197. static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
  198. u16 word)
  199. {
  200. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  201. struct zl6100_data *data = to_zl6100_data(info);
  202. int ret, vreg;
  203. if (page > 0)
  204. return -ENXIO;
  205. switch (reg) {
  206. case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
  207. word = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word) * 10, 9));
  208. vreg = MFR_VMON_OV_FAULT_LIMIT;
  209. pmbus_clear_cache(client);
  210. break;
  211. case PMBUS_VIRT_VMON_OV_FAULT_LIMIT:
  212. vreg = MFR_VMON_OV_FAULT_LIMIT;
  213. pmbus_clear_cache(client);
  214. break;
  215. case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
  216. word = zl6100_d2l(DIV_ROUND_CLOSEST(zl6100_l2d(word) * 10, 11));
  217. vreg = MFR_VMON_UV_FAULT_LIMIT;
  218. pmbus_clear_cache(client);
  219. break;
  220. case PMBUS_VIRT_VMON_UV_FAULT_LIMIT:
  221. vreg = MFR_VMON_UV_FAULT_LIMIT;
  222. pmbus_clear_cache(client);
  223. break;
  224. default:
  225. if (reg >= PMBUS_VIRT_BASE)
  226. return -ENXIO;
  227. vreg = reg;
  228. }
  229. zl6100_wait(data);
  230. ret = pmbus_write_word_data(client, page, vreg, word);
  231. data->access = ktime_get();
  232. return ret;
  233. }
  234. static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
  235. {
  236. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  237. struct zl6100_data *data = to_zl6100_data(info);
  238. int ret;
  239. if (page > 0)
  240. return -ENXIO;
  241. zl6100_wait(data);
  242. ret = pmbus_write_byte(client, page, value);
  243. data->access = ktime_get();
  244. return ret;
  245. }
  246. static const struct i2c_device_id zl6100_id[] = {
  247. {"bmr450", zl2005},
  248. {"bmr451", zl2005},
  249. {"bmr462", zl2008},
  250. {"bmr463", zl2008},
  251. {"bmr464", zl2008},
  252. {"zl2004", zl2004},
  253. {"zl2005", zl2005},
  254. {"zl2006", zl2006},
  255. {"zl2008", zl2008},
  256. {"zl2105", zl2105},
  257. {"zl2106", zl2106},
  258. {"zl6100", zl6100},
  259. {"zl6105", zl6105},
  260. {"zl9101", zl9101},
  261. {"zl9117", zl9117},
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(i2c, zl6100_id);
  265. static int zl6100_probe(struct i2c_client *client,
  266. const struct i2c_device_id *id)
  267. {
  268. int ret;
  269. struct zl6100_data *data;
  270. struct pmbus_driver_info *info;
  271. u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
  272. const struct i2c_device_id *mid;
  273. if (!i2c_check_functionality(client->adapter,
  274. I2C_FUNC_SMBUS_READ_WORD_DATA
  275. | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
  276. return -ENODEV;
  277. ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
  278. device_id);
  279. if (ret < 0) {
  280. dev_err(&client->dev, "Failed to read device ID\n");
  281. return ret;
  282. }
  283. device_id[ret] = '\0';
  284. dev_info(&client->dev, "Device ID %s\n", device_id);
  285. mid = NULL;
  286. for (mid = zl6100_id; mid->name[0]; mid++) {
  287. if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
  288. break;
  289. }
  290. if (!mid->name[0]) {
  291. dev_err(&client->dev, "Unsupported device\n");
  292. return -ENODEV;
  293. }
  294. if (id->driver_data != mid->driver_data)
  295. dev_notice(&client->dev,
  296. "Device mismatch: Configured %s, detected %s\n",
  297. id->name, mid->name);
  298. data = devm_kzalloc(&client->dev, sizeof(struct zl6100_data),
  299. GFP_KERNEL);
  300. if (!data)
  301. return -ENOMEM;
  302. data->id = mid->driver_data;
  303. /*
  304. * According to information from the chip vendor, all currently
  305. * supported chips are known to require a wait time between I2C
  306. * accesses.
  307. */
  308. data->delay = delay;
  309. /*
  310. * Since there was a direct I2C device access above, wait before
  311. * accessing the chip again.
  312. */
  313. data->access = ktime_get();
  314. zl6100_wait(data);
  315. info = &data->info;
  316. info->pages = 1;
  317. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
  318. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  319. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  320. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  321. /*
  322. * ZL2004, ZL9101M, and ZL9117M support monitoring an extra voltage
  323. * (VMON for ZL2004, VDRV for ZL9101M and ZL9117M). Report it as vmon.
  324. */
  325. if (data->id == zl2004 || data->id == zl9101 || data->id == zl9117)
  326. info->func[0] |= PMBUS_HAVE_VMON | PMBUS_HAVE_STATUS_VMON;
  327. ret = i2c_smbus_read_word_data(client, ZL6100_MFR_CONFIG);
  328. if (ret < 0)
  329. return ret;
  330. if (ret & ZL6100_MFR_XTEMP_ENABLE)
  331. info->func[0] |= PMBUS_HAVE_TEMP2;
  332. data->access = ktime_get();
  333. zl6100_wait(data);
  334. info->read_word_data = zl6100_read_word_data;
  335. info->read_byte_data = zl6100_read_byte_data;
  336. info->write_word_data = zl6100_write_word_data;
  337. info->write_byte = zl6100_write_byte;
  338. return pmbus_do_probe(client, mid, info);
  339. }
  340. static struct i2c_driver zl6100_driver = {
  341. .driver = {
  342. .name = "zl6100",
  343. },
  344. .probe = zl6100_probe,
  345. .remove = pmbus_do_remove,
  346. .id_table = zl6100_id,
  347. };
  348. module_i2c_driver(zl6100_driver);
  349. MODULE_AUTHOR("Guenter Roeck");
  350. MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
  351. MODULE_LICENSE("GPL");