exynos5-dt-types.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Samsung Electronics
  4. * Przemyslaw Marczak <p.marczak@samsung.com>
  5. */
  6. #include <common.h>
  7. #include <adc.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <fdtdec.h>
  11. #include <linux/delay.h>
  12. #include <power/pmic.h>
  13. #include <power/regulator.h>
  14. #include <power/s2mps11.h>
  15. #include <samsung/exynos5-dt-types.h>
  16. #include <samsung/misc.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. static const struct udevice_id board_ids[] = {
  19. { .compatible = "samsung,odroidxu3", .data = EXYNOS5_BOARD_ODROID_XU3 },
  20. { .compatible = "samsung,exynos5", .data = EXYNOS5_BOARD_GENERIC },
  21. { },
  22. };
  23. /**
  24. * Odroix XU3/XU4/HC1/HC2 board revisions (from HC1+_HC2_MAIN_REV0.1_20171017.pdf):
  25. * Rev ADCmax Board
  26. * 0.1 0 XU3 0.1
  27. * 0.2 372 XU3 0.2 | XU3L - no DISPLAYPORT (probe I2C0:0x40 / INA231)
  28. * 0.3 1280 XU4 0.1
  29. * 0.4 739 XU4 0.2
  30. * 0.5 1016 XU4+Air0.1 (Passive cooling)
  31. * 0.6 1309 XU4-HC1 0.1
  32. * 0.7 1470 XU4-HC1+ 0.1 (HC2)
  33. * Use +1% for ADC value tolerance in the array below, the code loops until
  34. * the measured ADC value is lower than then ADCmax from the array.
  35. */
  36. struct odroid_rev_info odroid_info[] = {
  37. { EXYNOS5_BOARD_ODROID_XU3_REV01, 1, 10, "xu3" },
  38. { EXYNOS5_BOARD_ODROID_XU3_REV02, 2, 375, "xu3" },
  39. { EXYNOS5_BOARD_ODROID_XU4_REV01, 1, 1293, "xu4" },
  40. { EXYNOS5_BOARD_ODROID_HC1_REV01, 1, 1322, "hc1" },
  41. { EXYNOS5_BOARD_ODROID_HC2_REV01, 1, 1484, "hc1" },
  42. { EXYNOS5_BOARD_ODROID_UNKNOWN, 0, 4095, "unknown" },
  43. };
  44. static unsigned int odroid_get_rev(void)
  45. {
  46. int i;
  47. for (i = 0; i < ARRAY_SIZE(odroid_info); i++) {
  48. if (odroid_info[i].board_type == gd->board_type)
  49. return odroid_info[i].board_rev;
  50. }
  51. return 0;
  52. }
  53. /*
  54. * Read ADC at least twice and check the resuls. If regulator providing voltage
  55. * on to measured point was just turned on, first reads might require time
  56. * to stabilize.
  57. */
  58. static int odroid_get_adc_val(unsigned int *adcval)
  59. {
  60. unsigned int adcval_prev = 0;
  61. int ret, retries = 20;
  62. ret = adc_channel_single_shot("adc@12D10000", CONFIG_ODROID_REV_AIN,
  63. &adcval_prev);
  64. if (ret)
  65. return ret;
  66. while (retries--) {
  67. mdelay(5);
  68. ret = adc_channel_single_shot("adc@12D10000",
  69. CONFIG_ODROID_REV_AIN, adcval);
  70. if (ret)
  71. return ret;
  72. /*
  73. * If difference between ADC reads is less than 3%,
  74. * accept the result
  75. */
  76. if ((100 * abs(*adcval - adcval_prev) / adcval_prev) < 3)
  77. return ret;
  78. adcval_prev = *adcval;
  79. }
  80. return ret;
  81. }
  82. static int odroid_get_board_type(void)
  83. {
  84. unsigned int adcval;
  85. int ret, i;
  86. ret = odroid_get_adc_val(&adcval);
  87. if (ret)
  88. goto rev_default;
  89. for (i = 0; i < ARRAY_SIZE(odroid_info); i++) {
  90. /* ADC tolerance: +1% */
  91. if (adcval < odroid_info[i].adc_val)
  92. return odroid_info[i].board_type;
  93. }
  94. rev_default:
  95. return EXYNOS5_BOARD_ODROID_XU3;
  96. }
  97. /**
  98. * odroid_get_type_str - returns pointer to one of the board type string.
  99. * Board types: "xu3", "xu3-lite", "xu4". However the "xu3lite" can be
  100. * detected only when the i2c controller is ready to use. Fortunately,
  101. * XU3 and XU3L are compatible, and the information about board lite
  102. * revision is needed before booting the linux, to set proper environment
  103. * variable: $fdtfile.
  104. */
  105. static const char *odroid_get_type_str(void)
  106. {
  107. const char *type_xu3l = "xu3-lite";
  108. struct udevice *dev, *chip;
  109. int i, ret;
  110. if (gd->board_type != EXYNOS5_BOARD_ODROID_XU3_REV02)
  111. goto exit;
  112. ret = pmic_get("s2mps11_pmic@66", &dev);
  113. if (ret)
  114. goto exit;
  115. /* Enable LDO26: 3.0V */
  116. ret = pmic_reg_write(dev, S2MPS11_REG_L26CTRL,
  117. S2MPS11_LDO26_ENABLE);
  118. if (ret)
  119. goto exit;
  120. /* Check XU3Lite by probe INA231 I2C0:0x40 */
  121. ret = uclass_get_device(UCLASS_I2C, 0, &dev);
  122. if (ret)
  123. goto exit;
  124. ret = dm_i2c_probe(dev, 0x40, 0x0, &chip);
  125. if (ret)
  126. return type_xu3l;
  127. exit:
  128. for (i = 0; i < ARRAY_SIZE(odroid_info); i++) {
  129. if (odroid_info[i].board_type == gd->board_type)
  130. return odroid_info[i].name;
  131. }
  132. return NULL;
  133. }
  134. bool board_is_odroidxu3(void)
  135. {
  136. if (gd->board_type >= EXYNOS5_BOARD_ODROID_XU3 &&
  137. gd->board_type <= EXYNOS5_BOARD_ODROID_XU3_REV02)
  138. return true;
  139. return false;
  140. }
  141. bool board_is_odroidxu4(void)
  142. {
  143. if (gd->board_type == EXYNOS5_BOARD_ODROID_XU4_REV01)
  144. return true;
  145. return false;
  146. }
  147. bool board_is_odroidhc1(void)
  148. {
  149. if (gd->board_type == EXYNOS5_BOARD_ODROID_HC1_REV01)
  150. return true;
  151. return false;
  152. }
  153. bool board_is_odroidhc2(void)
  154. {
  155. if (gd->board_type == EXYNOS5_BOARD_ODROID_HC2_REV01)
  156. return true;
  157. return false;
  158. }
  159. bool board_is_generic(void)
  160. {
  161. if (gd->board_type == EXYNOS5_BOARD_GENERIC)
  162. return true;
  163. return false;
  164. }
  165. /**
  166. * get_board_rev() - return detected board revision.
  167. *
  168. * @return: return board revision number for XU3 or 0 for generic
  169. */
  170. u32 get_board_rev(void)
  171. {
  172. if (board_is_generic())
  173. return 0;
  174. return odroid_get_rev();
  175. }
  176. /**
  177. * get_board_type() - returns board type string.
  178. *
  179. * @return: return board type string for XU3 or empty string for generic
  180. */
  181. const char *get_board_type(void)
  182. {
  183. const char *generic = "";
  184. if (board_is_generic())
  185. return generic;
  186. return odroid_get_type_str();
  187. }
  188. /**
  189. * set_board_type() - set board type in gd->board_type.
  190. * As default type set EXYNOS5_BOARD_GENERIC. If Odroid is detected,
  191. * set its proper type based on device tree.
  192. *
  193. * This might be called early when some more specific ways to detect revision
  194. * are not yet available.
  195. */
  196. void set_board_type(void)
  197. {
  198. const struct udevice_id *of_match = board_ids;
  199. int ret;
  200. gd->board_type = EXYNOS5_BOARD_GENERIC;
  201. while (of_match->compatible) {
  202. ret = fdt_node_check_compatible(gd->fdt_blob, 0,
  203. of_match->compatible);
  204. if (ret)
  205. of_match++;
  206. gd->board_type = of_match->data;
  207. break;
  208. }
  209. }
  210. /**
  211. * set_board_revision() - set detailed board type in gd->board_type.
  212. * Should be called when resources (e.g. regulators) are available
  213. * so ADC can be used to detect the specific revision of a board.
  214. */
  215. void set_board_revision(void)
  216. {
  217. if (board_is_odroidxu3())
  218. gd->board_type = odroid_get_board_type();
  219. }