zl6100.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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, 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. zl6100_wait(data);
  58. ret = pmbus_read_word_data(client, page, reg);
  59. data->access = ktime_get();
  60. return ret;
  61. }
  62. static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
  63. {
  64. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  65. struct zl6100_data *data = to_zl6100_data(info);
  66. int ret;
  67. if (page > 0)
  68. return -ENXIO;
  69. zl6100_wait(data);
  70. ret = pmbus_read_byte_data(client, page, reg);
  71. data->access = ktime_get();
  72. return ret;
  73. }
  74. static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
  75. u16 word)
  76. {
  77. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  78. struct zl6100_data *data = to_zl6100_data(info);
  79. int ret;
  80. if (page || reg >= PMBUS_VIRT_BASE)
  81. return -ENXIO;
  82. zl6100_wait(data);
  83. ret = pmbus_write_word_data(client, page, reg, word);
  84. data->access = ktime_get();
  85. return ret;
  86. }
  87. static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
  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 > 0)
  93. return -ENXIO;
  94. zl6100_wait(data);
  95. ret = pmbus_write_byte(client, page, value);
  96. data->access = ktime_get();
  97. return ret;
  98. }
  99. static const struct i2c_device_id zl6100_id[] = {
  100. {"zl2004", zl2004},
  101. {"zl2006", zl2006},
  102. {"zl2008", zl2008},
  103. {"zl2105", zl2105},
  104. {"zl2106", zl2106},
  105. {"zl6100", zl6100},
  106. {"zl6105", zl6105},
  107. { }
  108. };
  109. MODULE_DEVICE_TABLE(i2c, zl6100_id);
  110. static int zl6100_probe(struct i2c_client *client,
  111. const struct i2c_device_id *id)
  112. {
  113. int ret;
  114. struct zl6100_data *data;
  115. struct pmbus_driver_info *info;
  116. u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
  117. const struct i2c_device_id *mid;
  118. if (!i2c_check_functionality(client->adapter,
  119. I2C_FUNC_SMBUS_READ_BYTE_DATA
  120. | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
  121. return -ENODEV;
  122. ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
  123. device_id);
  124. if (ret < 0) {
  125. dev_err(&client->dev, "Failed to read device ID\n");
  126. return ret;
  127. }
  128. device_id[ret] = '\0';
  129. dev_info(&client->dev, "Device ID %s\n", device_id);
  130. mid = NULL;
  131. for (mid = zl6100_id; mid->name[0]; mid++) {
  132. if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
  133. break;
  134. }
  135. if (!mid->name[0]) {
  136. dev_err(&client->dev, "Unsupported device\n");
  137. return -ENODEV;
  138. }
  139. if (id->driver_data != mid->driver_data)
  140. dev_notice(&client->dev,
  141. "Device mismatch: Configured %s, detected %s\n",
  142. id->name, mid->name);
  143. data = kzalloc(sizeof(struct zl6100_data), GFP_KERNEL);
  144. if (!data)
  145. return -ENOMEM;
  146. data->id = mid->driver_data;
  147. /*
  148. * ZL2008, ZL2105, and ZL6100 are known to require a wait time
  149. * between I2C accesses. ZL2004 and ZL6105 are known to be safe.
  150. *
  151. * Only clear the wait time for chips known to be safe. The wait time
  152. * can be cleared later for additional chips if tests show that it
  153. * is not needed (in other words, better be safe than sorry).
  154. */
  155. if (data->id == zl2004 || data->id == zl6105)
  156. delay = 0;
  157. /*
  158. * Since there was a direct I2C device access above, wait before
  159. * accessing the chip again.
  160. * Set the timestamp, wait, then set it again. This should provide
  161. * enough buffer time to be safe.
  162. */
  163. data->access = ktime_get();
  164. zl6100_wait(data);
  165. data->access = ktime_get();
  166. info = &data->info;
  167. info->pages = 1;
  168. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
  169. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  170. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  171. | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  172. info->read_word_data = zl6100_read_word_data;
  173. info->read_byte_data = zl6100_read_byte_data;
  174. info->write_word_data = zl6100_write_word_data;
  175. info->write_byte = zl6100_write_byte;
  176. ret = pmbus_do_probe(client, mid, info);
  177. if (ret)
  178. goto err_mem;
  179. return 0;
  180. err_mem:
  181. kfree(data);
  182. return ret;
  183. }
  184. static int zl6100_remove(struct i2c_client *client)
  185. {
  186. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  187. const struct zl6100_data *data = to_zl6100_data(info);
  188. pmbus_do_remove(client);
  189. kfree(data);
  190. return 0;
  191. }
  192. static struct i2c_driver zl6100_driver = {
  193. .driver = {
  194. .name = "zl6100",
  195. },
  196. .probe = zl6100_probe,
  197. .remove = zl6100_remove,
  198. .id_table = zl6100_id,
  199. };
  200. static int __init zl6100_init(void)
  201. {
  202. return i2c_add_driver(&zl6100_driver);
  203. }
  204. static void __exit zl6100_exit(void)
  205. {
  206. i2c_del_driver(&zl6100_driver);
  207. }
  208. MODULE_AUTHOR("Guenter Roeck");
  209. MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
  210. MODULE_LICENSE("GPL");
  211. module_init(zl6100_init);
  212. module_exit(zl6100_exit);