zl6100.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_DEVICE_ID 0xe4
  37. #define ZL6100_WAIT_TIME 1000 /* uS */
  38. static ushort delay = ZL6100_WAIT_TIME;
  39. module_param(delay, ushort, 0644);
  40. MODULE_PARM_DESC(delay, "Delay between chip accesses in uS");
  41. /* Some chips need a delay between accesses */
  42. static inline void zl6100_wait(const struct zl6100_data *data)
  43. {
  44. if (delay) {
  45. s64 delta = ktime_us_delta(ktime_get(), data->access);
  46. if (delta < delay)
  47. udelay(delay - delta);
  48. }
  49. }
  50. static int zl6100_read_word_data(struct i2c_client *client, int page, int reg)
  51. {
  52. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  53. struct zl6100_data *data = to_zl6100_data(info);
  54. int ret;
  55. if (page || reg >= PMBUS_VIRT_BASE)
  56. return -ENXIO;
  57. if (data->id == zl2005) {
  58. /*
  59. * Limit register detection is not reliable on ZL2005.
  60. * Make sure registers are not erroneously detected.
  61. */
  62. switch (reg) {
  63. case PMBUS_VOUT_OV_WARN_LIMIT:
  64. case PMBUS_VOUT_UV_WARN_LIMIT:
  65. case PMBUS_IOUT_OC_WARN_LIMIT:
  66. return -ENXIO;
  67. }
  68. }
  69. zl6100_wait(data);
  70. ret = pmbus_read_word_data(client, page, reg);
  71. data->access = ktime_get();
  72. return ret;
  73. }
  74. static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
  75. {
  76. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  77. struct zl6100_data *data = to_zl6100_data(info);
  78. int ret;
  79. if (page > 0)
  80. return -ENXIO;
  81. zl6100_wait(data);
  82. ret = pmbus_read_byte_data(client, page, reg);
  83. data->access = ktime_get();
  84. return ret;
  85. }
  86. static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
  87. u16 word)
  88. {
  89. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  90. struct zl6100_data *data = to_zl6100_data(info);
  91. int ret;
  92. if (page || reg >= PMBUS_VIRT_BASE)
  93. return -ENXIO;
  94. zl6100_wait(data);
  95. ret = pmbus_write_word_data(client, page, reg, word);
  96. data->access = ktime_get();
  97. return ret;
  98. }
  99. static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
  100. {
  101. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  102. struct zl6100_data *data = to_zl6100_data(info);
  103. int ret;
  104. if (page > 0)
  105. return -ENXIO;
  106. zl6100_wait(data);
  107. ret = pmbus_write_byte(client, page, value);
  108. data->access = ktime_get();
  109. return ret;
  110. }
  111. static const struct i2c_device_id zl6100_id[] = {
  112. {"bmr450", zl2005},
  113. {"bmr451", zl2005},
  114. {"bmr462", zl2008},
  115. {"bmr463", zl2008},
  116. {"bmr464", zl2008},
  117. {"zl2004", zl2004},
  118. {"zl2005", zl2005},
  119. {"zl2006", zl2006},
  120. {"zl2008", zl2008},
  121. {"zl2105", zl2105},
  122. {"zl2106", zl2106},
  123. {"zl6100", zl6100},
  124. {"zl6105", zl6105},
  125. { }
  126. };
  127. MODULE_DEVICE_TABLE(i2c, zl6100_id);
  128. static int zl6100_probe(struct i2c_client *client,
  129. const struct i2c_device_id *id)
  130. {
  131. int ret;
  132. struct zl6100_data *data;
  133. struct pmbus_driver_info *info;
  134. u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
  135. const struct i2c_device_id *mid;
  136. if (!i2c_check_functionality(client->adapter,
  137. I2C_FUNC_SMBUS_READ_BYTE_DATA
  138. | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
  139. return -ENODEV;
  140. ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
  141. device_id);
  142. if (ret < 0) {
  143. dev_err(&client->dev, "Failed to read device ID\n");
  144. return ret;
  145. }
  146. device_id[ret] = '\0';
  147. dev_info(&client->dev, "Device ID %s\n", device_id);
  148. mid = NULL;
  149. for (mid = zl6100_id; mid->name[0]; mid++) {
  150. if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
  151. break;
  152. }
  153. if (!mid->name[0]) {
  154. dev_err(&client->dev, "Unsupported device\n");
  155. return -ENODEV;
  156. }
  157. if (id->driver_data != mid->driver_data)
  158. dev_notice(&client->dev,
  159. "Device mismatch: Configured %s, detected %s\n",
  160. id->name, mid->name);
  161. data = kzalloc(sizeof(struct zl6100_data), GFP_KERNEL);
  162. if (!data)
  163. return -ENOMEM;
  164. data->id = mid->driver_data;
  165. /*
  166. * ZL2005, ZL2008, ZL2105, and ZL6100 are known to require a wait time
  167. * between I2C accesses. ZL2004 and ZL6105 are known to be safe.
  168. * Other chips have not yet been tested.
  169. *
  170. * Only clear the wait time for chips known to be safe. The wait time
  171. * can be cleared later for additional chips if tests show that it
  172. * is not needed (in other words, better be safe than sorry).
  173. */
  174. if (data->id == zl2004 || data->id == zl6105)
  175. delay = 0;
  176. /*
  177. * Since there was a direct I2C device access above, wait before
  178. * accessing the chip again.
  179. * Set the timestamp, wait, then set it again. This should provide
  180. * enough buffer time to be safe.
  181. */
  182. data->access = ktime_get();
  183. zl6100_wait(data);
  184. data->access = ktime_get();
  185. info = &data->info;
  186. info->pages = 1;
  187. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
  188. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  189. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  190. | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  191. info->read_word_data = zl6100_read_word_data;
  192. info->read_byte_data = zl6100_read_byte_data;
  193. info->write_word_data = zl6100_write_word_data;
  194. info->write_byte = zl6100_write_byte;
  195. ret = pmbus_do_probe(client, mid, info);
  196. if (ret)
  197. goto err_mem;
  198. return 0;
  199. err_mem:
  200. kfree(data);
  201. return ret;
  202. }
  203. static int zl6100_remove(struct i2c_client *client)
  204. {
  205. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  206. const struct zl6100_data *data = to_zl6100_data(info);
  207. pmbus_do_remove(client);
  208. kfree(data);
  209. return 0;
  210. }
  211. static struct i2c_driver zl6100_driver = {
  212. .driver = {
  213. .name = "zl6100",
  214. },
  215. .probe = zl6100_probe,
  216. .remove = zl6100_remove,
  217. .id_table = zl6100_id,
  218. };
  219. static int __init zl6100_init(void)
  220. {
  221. return i2c_add_driver(&zl6100_driver);
  222. }
  223. static void __exit zl6100_exit(void)
  224. {
  225. i2c_del_driver(&zl6100_driver);
  226. }
  227. MODULE_AUTHOR("Guenter Roeck");
  228. MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
  229. MODULE_LICENSE("GPL");
  230. module_init(zl6100_init);
  231. module_exit(zl6100_exit);