exynos5-dt-types.c 5.9 KB

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