zl6100.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. zl9101, zl9117 };
  31. struct zl6100_data {
  32. int id;
  33. ktime_t access; /* chip access time */
  34. int delay; /* Delay between chip accesses in uS */
  35. struct pmbus_driver_info info;
  36. };
  37. #define to_zl6100_data(x) container_of(x, struct zl6100_data, info)
  38. #define ZL6100_MFR_CONFIG 0xd0
  39. #define ZL6100_DEVICE_ID 0xe4
  40. #define ZL6100_MFR_XTEMP_ENABLE (1 << 7)
  41. #define ZL6100_WAIT_TIME 1000 /* uS */
  42. static ushort delay = ZL6100_WAIT_TIME;
  43. module_param(delay, ushort, 0644);
  44. MODULE_PARM_DESC(delay, "Delay between chip accesses in uS");
  45. /* Some chips need a delay between accesses */
  46. static inline void zl6100_wait(const struct zl6100_data *data)
  47. {
  48. if (data->delay) {
  49. s64 delta = ktime_us_delta(ktime_get(), data->access);
  50. if (delta < data->delay)
  51. udelay(data->delay - delta);
  52. }
  53. }
  54. static int zl6100_read_word_data(struct i2c_client *client, int page, int reg)
  55. {
  56. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  57. struct zl6100_data *data = to_zl6100_data(info);
  58. int ret;
  59. if (page || reg >= PMBUS_VIRT_BASE)
  60. return -ENXIO;
  61. if (data->id == zl2005) {
  62. /*
  63. * Limit register detection is not reliable on ZL2005.
  64. * Make sure registers are not erroneously detected.
  65. */
  66. switch (reg) {
  67. case PMBUS_VOUT_OV_WARN_LIMIT:
  68. case PMBUS_VOUT_UV_WARN_LIMIT:
  69. case PMBUS_IOUT_OC_WARN_LIMIT:
  70. return -ENXIO;
  71. }
  72. }
  73. zl6100_wait(data);
  74. ret = pmbus_read_word_data(client, page, reg);
  75. data->access = ktime_get();
  76. return ret;
  77. }
  78. static int zl6100_read_byte_data(struct i2c_client *client, int page, int reg)
  79. {
  80. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  81. struct zl6100_data *data = to_zl6100_data(info);
  82. int ret;
  83. if (page > 0)
  84. return -ENXIO;
  85. zl6100_wait(data);
  86. ret = pmbus_read_byte_data(client, page, reg);
  87. data->access = ktime_get();
  88. return ret;
  89. }
  90. static int zl6100_write_word_data(struct i2c_client *client, int page, int reg,
  91. u16 word)
  92. {
  93. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  94. struct zl6100_data *data = to_zl6100_data(info);
  95. int ret;
  96. if (page || reg >= PMBUS_VIRT_BASE)
  97. return -ENXIO;
  98. zl6100_wait(data);
  99. ret = pmbus_write_word_data(client, page, reg, word);
  100. data->access = ktime_get();
  101. return ret;
  102. }
  103. static int zl6100_write_byte(struct i2c_client *client, int page, u8 value)
  104. {
  105. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  106. struct zl6100_data *data = to_zl6100_data(info);
  107. int ret;
  108. if (page > 0)
  109. return -ENXIO;
  110. zl6100_wait(data);
  111. ret = pmbus_write_byte(client, page, value);
  112. data->access = ktime_get();
  113. return ret;
  114. }
  115. static const struct i2c_device_id zl6100_id[] = {
  116. {"bmr450", zl2005},
  117. {"bmr451", zl2005},
  118. {"bmr462", zl2008},
  119. {"bmr463", zl2008},
  120. {"bmr464", zl2008},
  121. {"zl2004", zl2004},
  122. {"zl2005", zl2005},
  123. {"zl2006", zl2006},
  124. {"zl2008", zl2008},
  125. {"zl2105", zl2105},
  126. {"zl2106", zl2106},
  127. {"zl6100", zl6100},
  128. {"zl6105", zl6105},
  129. {"zl9101", zl9101},
  130. {"zl9117", zl9117},
  131. { }
  132. };
  133. MODULE_DEVICE_TABLE(i2c, zl6100_id);
  134. static int zl6100_probe(struct i2c_client *client,
  135. const struct i2c_device_id *id)
  136. {
  137. int ret;
  138. struct zl6100_data *data;
  139. struct pmbus_driver_info *info;
  140. u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
  141. const struct i2c_device_id *mid;
  142. if (!i2c_check_functionality(client->adapter,
  143. I2C_FUNC_SMBUS_READ_WORD_DATA
  144. | I2C_FUNC_SMBUS_READ_BLOCK_DATA))
  145. return -ENODEV;
  146. ret = i2c_smbus_read_block_data(client, ZL6100_DEVICE_ID,
  147. device_id);
  148. if (ret < 0) {
  149. dev_err(&client->dev, "Failed to read device ID\n");
  150. return ret;
  151. }
  152. device_id[ret] = '\0';
  153. dev_info(&client->dev, "Device ID %s\n", device_id);
  154. mid = NULL;
  155. for (mid = zl6100_id; mid->name[0]; mid++) {
  156. if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
  157. break;
  158. }
  159. if (!mid->name[0]) {
  160. dev_err(&client->dev, "Unsupported device\n");
  161. return -ENODEV;
  162. }
  163. if (id->driver_data != mid->driver_data)
  164. dev_notice(&client->dev,
  165. "Device mismatch: Configured %s, detected %s\n",
  166. id->name, mid->name);
  167. data = devm_kzalloc(&client->dev, sizeof(struct zl6100_data),
  168. GFP_KERNEL);
  169. if (!data)
  170. return -ENOMEM;
  171. data->id = mid->driver_data;
  172. /*
  173. * According to information from the chip vendor, all currently
  174. * supported chips are known to require a wait time between I2C
  175. * accesses.
  176. */
  177. data->delay = delay;
  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. return ret;
  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. return pmbus_do_probe(client, mid, info);
  202. }
  203. static struct i2c_driver zl6100_driver = {
  204. .driver = {
  205. .name = "zl6100",
  206. },
  207. .probe = zl6100_probe,
  208. .remove = pmbus_do_remove,
  209. .id_table = zl6100_id,
  210. };
  211. module_i2c_driver(zl6100_driver);
  212. MODULE_AUTHOR("Guenter Roeck");
  213. MODULE_DESCRIPTION("PMBus driver for ZL6100 and compatibles");
  214. MODULE_LICENSE("GPL");