zl6100.c 9.9 KB

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