zl6100.c 6.6 KB

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