opal-sensor.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * PowerNV sensor code
  3. *
  4. * Copyright (C) 2013 IBM
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/of_platform.h>
  22. #include <asm/opal.h>
  23. #include <asm/machdep.h>
  24. /*
  25. * This will return sensor information to driver based on the requested sensor
  26. * handle. A handle is an opaque id for the powernv, read by the driver from the
  27. * device tree..
  28. */
  29. int opal_get_sensor_data(u32 sensor_hndl, u32 *sensor_data)
  30. {
  31. int ret, token;
  32. struct opal_msg msg;
  33. __be32 data;
  34. token = opal_async_get_token_interruptible();
  35. if (token < 0)
  36. return token;
  37. ret = opal_sensor_read(sensor_hndl, token, &data);
  38. switch (ret) {
  39. case OPAL_ASYNC_COMPLETION:
  40. ret = opal_async_wait_response(token, &msg);
  41. if (ret) {
  42. pr_err("%s: Failed to wait for the async response, %d\n",
  43. __func__, ret);
  44. goto out;
  45. }
  46. ret = opal_error_code(opal_get_async_rc(msg));
  47. *sensor_data = be32_to_cpu(data);
  48. break;
  49. case OPAL_SUCCESS:
  50. ret = 0;
  51. *sensor_data = be32_to_cpu(data);
  52. break;
  53. case OPAL_WRONG_STATE:
  54. ret = -EIO;
  55. break;
  56. default:
  57. ret = opal_error_code(ret);
  58. break;
  59. }
  60. out:
  61. opal_async_release_token(token);
  62. return ret;
  63. }
  64. EXPORT_SYMBOL_GPL(opal_get_sensor_data);
  65. int opal_get_sensor_data_u64(u32 sensor_hndl, u64 *sensor_data)
  66. {
  67. int ret, token;
  68. struct opal_msg msg;
  69. __be64 data;
  70. if (!opal_check_token(OPAL_SENSOR_READ_U64)) {
  71. u32 sdata;
  72. ret = opal_get_sensor_data(sensor_hndl, &sdata);
  73. if (!ret)
  74. *sensor_data = sdata;
  75. return ret;
  76. }
  77. token = opal_async_get_token_interruptible();
  78. if (token < 0)
  79. return token;
  80. ret = opal_sensor_read_u64(sensor_hndl, token, &data);
  81. switch (ret) {
  82. case OPAL_ASYNC_COMPLETION:
  83. ret = opal_async_wait_response(token, &msg);
  84. if (ret) {
  85. pr_err("%s: Failed to wait for the async response, %d\n",
  86. __func__, ret);
  87. goto out_token;
  88. }
  89. ret = opal_error_code(opal_get_async_rc(msg));
  90. *sensor_data = be64_to_cpu(data);
  91. break;
  92. case OPAL_SUCCESS:
  93. ret = 0;
  94. *sensor_data = be64_to_cpu(data);
  95. break;
  96. case OPAL_WRONG_STATE:
  97. ret = -EIO;
  98. break;
  99. default:
  100. ret = opal_error_code(ret);
  101. break;
  102. }
  103. out_token:
  104. opal_async_release_token(token);
  105. return ret;
  106. }
  107. EXPORT_SYMBOL_GPL(opal_get_sensor_data_u64);
  108. int __init opal_sensor_init(void)
  109. {
  110. struct platform_device *pdev;
  111. struct device_node *sensor;
  112. sensor = of_find_node_by_path("/ibm,opal/sensors");
  113. if (!sensor) {
  114. pr_err("Opal node 'sensors' not found\n");
  115. return -ENODEV;
  116. }
  117. pdev = of_platform_device_create(sensor, "opal-sensor", NULL);
  118. of_node_put(sensor);
  119. return PTR_ERR_OR_ZERO(pdev);
  120. }