zl6100.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Hardware monitoring driver for ZL6100 and compatibles
  3. *
  4. * Copyright (c) 2011 Ericsson AB.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/ktime.h>
  27. #include <linux/delay.h>
  28. #include "pmbus.h"
  29. enum chips { zl2004, zl2005, zl2006, zl2008, zl2105, zl2106, zl6100, zl6105 };
  30. struct zl6100_data {
  31. int id;
  32. ktime_t access; /* chip access time */
  33. struct pmbus_driver_info info;
  34. };
  35. #define to_zl6100_data(x) container_of(x, struct zl6100_data, info)
  36. #define ZL6100_MFR_CONFIG 0xd0
  37. #define ZL6100_DEVICE_ID 0xe4
  38. #define ZL6100_MFR_XTEMP_ENABLE (1 << 7)
  39. #define ZL6100_WAIT_TIME 1000 /* uS */
  40. static ushort delay = ZL6100_WAIT_TIME;
  41. module_param(delay, ushort, 0644);
  42. MODULE_PARM_DESC(delay, "Delay between chip accesses in uS");
  43. /* Some chips need a delay between accesses */
  44. static inline void zl6100_wait(const struct zl6100_data *data)
  45. {
  46. if (delay) {
  47. s64 delta = ktime_us_delta(ktime_get(), data->access);
  48. if (delta < delay)
  49. udelay(delay - delta);
  50. }
  51. }
  52. static int zl6100_read_word_data(struct i2c_client *client, int page, int reg)
  53. {
  54. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  55. struct zl6100_data *data = to_zl6100_data(info);
  56. int ret;
  57. if (page || reg >= PMBUS_VIRT_BASE)
  58. return -ENXIO;
  59. if (data->id == zl2005) {
  60. /*
  61. * Limit register detection is not reliable on ZL2005.
  62. * Make sure registers are not erroneously detected.
  63. */
  64. switch (reg) {
  65. case PMBUS_VOUT_OV_WARN_LIMIT:
  66. case PMBUS_VOUT_UV_WARN_LIMIT:
  67. case PMBUS_IOUT_OC_WARN_LIMIT:
  68. return -ENXIO;
  69. }
  70. }
  71. zl6100_wait(data);
  72. ret = pmbus_read_word_data(client, page, reg);
  73. data->access = ktime_get();
  74. return ret;
  75. }
  76. static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
  77. {
  78. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  79. struct zl6100_data *data = to_zl6100_data(info);
  80. int ret;
  81. if (page > 0)
  82. return -ENXIO;
  83. zl6100_wait(data);
  84. ret = pmbus_read_byte_data(client, page, reg);
  85. data->access = ktime_get();
  86. return ret;
  87. }
  88. static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
  89. u16 word)
  90. {
  91. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  92. struct zl6100_data *data = to_zl6100_data(info);
  93. int ret;
  94. if (page || reg >= PMBUS_VIRT_BASE)
  95. return -ENXIO;
  96. zl6100_wait(data);
  97. ret = pmbus_write_word_data(client, page, reg, word);
  98. data->access = ktime_get();
  99. return ret;
  100. }
  101. static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
  102. {
  103. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  104. struct zl6100_data *data = to_zl6100_data(info);
  105. int ret;
  106. if (page > 0)
  107. return -ENXIO;
  108. zl6100_wait(data);
  109. ret = pmbus_write_byte(client, page, value);
  110. data->access = ktime_get();
  111. return ret;
  112. }
  113. static const struct i2c_device_id zl6100_id[] = {
  114. {"bmr450", zl2005},
  115. {"bmr451", zl2005},
  116. {"bmr462", zl2008},
  117. {"bmr463", zl2008},
  118. {"bmr464", zl2008},
  119. {"zl2004", zl2004},
  120. {"zl2005", zl2005},
  121. {"zl2006", zl2006},
  122. {"zl2008", zl2008},
  123. {"zl2105", zl2105},
  124. {"zl2106", zl2106},
  125. {"zl6100", zl6100},
  126. {"zl6105", zl6105},
  127. { }
  128. };
  129. MODULE_DEVICE_TABLE(i2c, zl6100_id);
  130. static int zl6100_probe(struct i2c_client *client,
  131. const struct i2c_device_id *id)
  132. {
  133. int ret;
  134. struct zl6100_data *data;
  135. struct pmbus_driver_info *info;
  136. u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
  137. const struct i2c_device_id *mid;
  138. if (!i2c_check_functionality(client->adapter,
  139. I2C_FUNC_SMBUS_READ_WORD_DATA
  140. | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
  141. return -ENODEV;
  142. ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
  143. device_id);
  144. if (ret < 0) {
  145. dev_err(&client->dev, "Failed to read device ID\n");
  146. return ret;
  147. }
  148. device_id[ret] = '\0';
  149. dev_info(&client->dev, "Device ID %s\n", device_id);
  150. mid = NULL;
  151. for (mid = zl6100_id; mid->name[0]; mid++) {
  152. if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
  153. break;
  154. }
  155. if (!mid->name[0]) {
  156. dev_err(&client->dev, "Unsupported device\n");
  157. return -ENODEV;
  158. }
  159. if (id->driver_data != mid->driver_data)
  160. dev_notice(&client->dev,
  161. "Device mismatch: Configured %s, detected %s\n",
  162. id->name, mid->name);
  163. data = kzalloc(sizeof(struct zl6100_data), GFP_KERNEL);
  164. if (!data)
  165. return -ENOMEM;
  166. data->id = mid->driver_data;
  167. /*
  168. * ZL2005, ZL2008, ZL2105, and ZL6100 are known to require a wait time
  169. * between I2C accesses. ZL2004 and ZL6105 are known to be safe.
  170. * Other chips have not yet been tested.
  171. *
  172. * Only clear the wait time for chips known to be safe. The wait time
  173. * can be cleared later for additional chips if tests show that it
  174. * is not needed (in other words, better be safe than sorry).
  175. */
  176. if (data->id == zl2004 || data->id == zl6105)
  177. delay = 0;
  178. /*
  179. * Since there was a direct I2C device access above, wait before
  180. * accessing the chip again.
  181. */
  182. data->access = ktime_get();
  183. zl6100_wait(data);
  184. info = &data->info;
  185. info->pages = 1;
  186. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
  187. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  188. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  189. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  190. ret = i2c_smbus_read_word_data(client, ZL6100_MFR_CONFIG);
  191. if (ret < 0)
  192. goto err_mem;
  193. if (ret & ZL6100_MFR_XTEMP_ENABLE)
  194. info->func[0] |= PMBUS_HAVE_TEMP2;
  195. data->access = ktime_get();
  196. zl6100_wait(data);
  197. info->read_word_data = zl6100_read_word_data;
  198. info->read_byte_data = zl6100_read_byte_data;
  199. info->write_word_data = zl6100_write_word_data;
  200. info->write_byte = zl6100_write_byte;
  201. ret = pmbus_do_probe(client, mid, info);
  202. if (ret)
  203. goto err_mem;
  204. return 0;
  205. err_mem:
  206. kfree(data);
  207. return ret;
  208. }
  209. static int zl6100_remove(struct i2c_client *client)
  210. {
  211. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  212. const struct zl6100_data *data = to_zl6100_data(info);
  213. pmbus_do_remove(client);
  214. kfree(data);
  215. return 0;
  216. }
  217. static struct i2c_driver zl6100_driver = {
  218. .driver = {
  219. .name = "zl6100",
  220. },
  221. .probe = zl6100_probe,
  222. .remove = zl6100_remove,
  223. .id_table = zl6100_id,
  224. };
  225. static int __init zl6100_init(void)
  226. {
  227. return i2c_add_driver(&zl6100_driver);
  228. }
  229. static void __exit zl6100_exit(void)
  230. {
  231. i2c_del_driver(&zl6100_driver);
  232. }
  233. MODULE_AUTHOR("Guenter Roeck");
  234. MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
  235. MODULE_LICENSE("GPL");
  236. module_init(zl6100_init);
  237. module_exit(zl6100_exit);