tsens-8996.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. #include "tsens.h"
  17. #define STATUS_OFFSET 0x10a0
  18. #define LAST_TEMP_MASK 0xfff
  19. #define STATUS_VALID_BIT BIT(21)
  20. #define CODE_SIGN_BIT BIT(11)
  21. static int get_temp_8996(struct tsens_device *tmdev, int id, int *temp)
  22. {
  23. struct tsens_sensor *s = &tmdev->sensor[id];
  24. u32 code;
  25. unsigned int sensor_addr;
  26. int last_temp = 0, last_temp2 = 0, last_temp3 = 0, ret;
  27. sensor_addr = STATUS_OFFSET + s->hw_id * 4;
  28. ret = regmap_read(tmdev->map, sensor_addr, &code);
  29. if (ret)
  30. return ret;
  31. last_temp = code & LAST_TEMP_MASK;
  32. if (code & STATUS_VALID_BIT)
  33. goto done;
  34. /* Try a second time */
  35. ret = regmap_read(tmdev->map, sensor_addr, &code);
  36. if (ret)
  37. return ret;
  38. if (code & STATUS_VALID_BIT) {
  39. last_temp = code & LAST_TEMP_MASK;
  40. goto done;
  41. } else {
  42. last_temp2 = code & LAST_TEMP_MASK;
  43. }
  44. /* Try a third/last time */
  45. ret = regmap_read(tmdev->map, sensor_addr, &code);
  46. if (ret)
  47. return ret;
  48. if (code & STATUS_VALID_BIT) {
  49. last_temp = code & LAST_TEMP_MASK;
  50. goto done;
  51. } else {
  52. last_temp3 = code & LAST_TEMP_MASK;
  53. }
  54. if (last_temp == last_temp2)
  55. last_temp = last_temp2;
  56. else if (last_temp2 == last_temp3)
  57. last_temp = last_temp3;
  58. done:
  59. /* Code sign bit is the sign extension for a negative value */
  60. if (last_temp & CODE_SIGN_BIT)
  61. last_temp |= ~CODE_SIGN_BIT;
  62. /* Temperatures are in deciCelicius */
  63. *temp = last_temp * 100;
  64. return 0;
  65. }
  66. static const struct tsens_ops ops_8996 = {
  67. .init = init_common,
  68. .get_temp = get_temp_8996,
  69. };
  70. const struct tsens_data data_8996 = {
  71. .num_sensors = 13,
  72. .ops = &ops_8996,
  73. };