ad5dea7307ab9880685152ca055b204b32086f43
[oweals/u-boot.git] / board / samsung / trats2 / trats2.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
4  * Sanghee Kim <sh0130.kim@samsung.com>
5  * Piotr Wilczek <p.wilczek@samsung.com>
6  */
7
8 #include <common.h>
9 #include <lcd.h>
10 #include <log.h>
11 #include <asm/gpio.h>
12 #include <asm/arch/pinmux.h>
13 #include <asm/arch/power.h>
14 #include <asm/arch/mipi_dsim.h>
15 #include <power/pmic.h>
16 #include <power/max77686_pmic.h>
17 #include <power/battery.h>
18 #include <power/max77693_pmic.h>
19 #include <power/max77693_muic.h>
20 #include <power/max77693_fg.h>
21 #include <libtizen.h>
22 #include <errno.h>
23 #include <usb.h>
24 #include <usb/dwc2_udc.h>
25 #include <usb_mass_storage.h>
26
27 static unsigned int board_rev = -1;
28
29 static inline u32 get_model_rev(void);
30
31 static void check_hw_revision(void)
32 {
33         int modelrev = 0;
34         char str[12];
35         int i;
36
37         /*
38          * GPM1[1:0]: MODEL_REV[1:0]
39          * Don't set as pull-none for these N/C pin.
40          * TRM say that it may cause unexcepted state and leakage current.
41          * and pull-none is only for output function.
42          */
43         for (i = 0; i < 2; i++) {
44                 int pin = i + EXYNOS4X12_GPIO_M10;
45
46                 sprintf(str, "model_rev%d", i);
47                 gpio_request(pin, str);
48                 gpio_cfg_pin(pin, S5P_GPIO_INPUT);
49         }
50
51         /* GPM1[5:2]: HW_REV[3:0] */
52         for (i = 0; i < 4; i++) {
53                 int pin = i + EXYNOS4X12_GPIO_M12;
54
55                 sprintf(str, "hw_rev%d", i);
56                 gpio_request(pin, str);
57                 gpio_cfg_pin(pin, S5P_GPIO_INPUT);
58                 gpio_set_pull(pin, S5P_GPIO_PULL_NONE);
59         }
60
61         /* GPM1[1:0]: MODEL_REV[1:0] */
62         for (i = 0; i < 2; i++)
63                 modelrev |= (gpio_get_value(EXYNOS4X12_GPIO_M10 + i) << i);
64
65         /* board_rev[15:8] = model */
66         board_rev = modelrev << 8;
67 }
68
69 u32 get_board_rev(void)
70 {
71         return board_rev;
72 }
73
74 static inline u32 get_model_rev(void)
75 {
76         return (board_rev >> 8) & 0xff;
77 }
78
79 static void board_external_gpio_init(void)
80 {
81         /*
82          * some pins which in alive block are connected with external pull-up
83          * but it's default setting is pull-down.
84          * if that pin set as input then that floated
85          */
86
87         gpio_set_pull(EXYNOS4X12_GPIO_X02, S5P_GPIO_PULL_NONE); /* PS_ALS_INT */
88         gpio_set_pull(EXYNOS4X12_GPIO_X04, S5P_GPIO_PULL_NONE); /* TSP_nINT */
89         gpio_set_pull(EXYNOS4X12_GPIO_X07, S5P_GPIO_PULL_NONE); /* AP_PMIC_IRQ*/
90         gpio_set_pull(EXYNOS4X12_GPIO_X15, S5P_GPIO_PULL_NONE); /* IF_PMIC_IRQ*/
91         gpio_set_pull(EXYNOS4X12_GPIO_X20, S5P_GPIO_PULL_NONE); /* VOL_UP */
92         gpio_set_pull(EXYNOS4X12_GPIO_X21, S5P_GPIO_PULL_NONE); /* VOL_DOWN */
93         gpio_set_pull(EXYNOS4X12_GPIO_X23, S5P_GPIO_PULL_NONE); /* FUEL_ALERT */
94         gpio_set_pull(EXYNOS4X12_GPIO_X24, S5P_GPIO_PULL_NONE); /* ADC_INT */
95         gpio_set_pull(EXYNOS4X12_GPIO_X27, S5P_GPIO_PULL_NONE); /* nPOWER */
96         gpio_set_pull(EXYNOS4X12_GPIO_X30, S5P_GPIO_PULL_NONE); /* WPC_INT */
97         gpio_set_pull(EXYNOS4X12_GPIO_X35, S5P_GPIO_PULL_NONE); /* OK_KEY */
98         gpio_set_pull(EXYNOS4X12_GPIO_X37, S5P_GPIO_PULL_NONE); /* HDMI_HPD */
99 }
100
101 int exynos_early_init_f(void)
102 {
103         board_external_gpio_init();
104
105         return 0;
106 }
107
108 int exynos_init(void)
109 {
110         struct exynos4_power *pwr =
111                 (struct exynos4_power *)samsung_get_base_power();
112
113         check_hw_revision();
114         printf("HW Revision:\t0x%04x\n", board_rev);
115
116         /*
117          * First bootloader on the TRATS2 platform uses
118          * INFORM4 and INFORM5 registers for recovery
119          *
120          * To indicate correct boot chain - those two
121          * registers must be cleared out
122          */
123         writel(0, &pwr->inform4);
124         writel(0, &pwr->inform5);
125
126         return 0;
127 }
128
129 int exynos_power_init(void)
130 {
131 #ifndef CONFIG_DM_I2C /* TODO(maintainer): Convert to driver model */
132         int chrg;
133         struct power_battery *pb;
134         struct pmic *p_chrg, *p_muic, *p_fg, *p_bat;
135
136         pmic_init_max77693(I2C_10);     /* I2C adapter 10 - bus name soft1 */
137         power_muic_init(I2C_10);        /* I2C adapter 10 - bus name soft1 */
138         power_fg_init(I2C_9);           /* I2C adapter 9 - bus name soft0 */
139         power_bat_init(0);
140
141         p_chrg = pmic_get("MAX77693_PMIC");
142         if (!p_chrg) {
143                 puts("MAX77693_PMIC: Not found\n");
144                 return -ENODEV;
145         }
146
147         p_muic = pmic_get("MAX77693_MUIC");
148         if (!p_muic) {
149                 puts("MAX77693_MUIC: Not found\n");
150                 return -ENODEV;
151         }
152
153         p_fg = pmic_get("MAX77693_FG");
154         if (!p_fg) {
155                 puts("MAX17042_FG: Not found\n");
156                 return -ENODEV;
157         }
158
159         if (p_chrg->chrg->chrg_bat_present(p_chrg) == 0)
160                 puts("No battery detected\n");
161
162         p_bat = pmic_get("BAT_TRATS2");
163         if (!p_bat) {
164                 puts("BAT_TRATS2: Not found\n");
165                 return -ENODEV;
166         }
167
168         p_fg->parent =  p_bat;
169         p_chrg->parent = p_bat;
170         p_muic->parent = p_bat;
171
172         p_bat->pbat->battery_init(p_bat, p_fg, p_chrg, p_muic);
173
174         pb = p_bat->pbat;
175         chrg = p_muic->chrg->chrg_type(p_muic);
176         debug("CHARGER TYPE: %d\n", chrg);
177
178         if (!p_chrg->chrg->chrg_bat_present(p_chrg)) {
179                 puts("No battery detected\n");
180                 return 0;
181         }
182
183         p_fg->fg->fg_battery_check(p_fg, p_bat);
184
185         if (pb->bat->state == CHARGE && chrg == CHARGER_USB)
186                 puts("CHARGE Battery !\n");
187 #endif
188         return 0;
189 }
190
191 #ifdef CONFIG_USB_GADGET
192 static int s5pc210_phy_control(int on)
193 {
194 #ifndef CONFIG_DM_I2C /* TODO(maintainer): Convert to driver model */
195         int ret = 0;
196         unsigned int val;
197         struct pmic *p, *p_pmic, *p_muic;
198
199         p_pmic = pmic_get("MAX77686_PMIC");
200         if (!p_pmic)
201                 return -ENODEV;
202
203         if (pmic_probe(p_pmic))
204                 return -1;
205
206         p_muic = pmic_get("MAX77693_MUIC");
207         if (!p_muic)
208                 return -ENODEV;
209
210         if (pmic_probe(p_muic))
211                 return -1;
212
213         if (on) {
214                 ret = max77686_set_ldo_mode(p_pmic, 12, OPMODE_ON);
215                 if (ret)
216                         return -1;
217
218                 p = pmic_get("MAX77693_PMIC");
219                 if (!p)
220                         return -ENODEV;
221
222                 if (pmic_probe(p))
223                         return -1;
224
225                 /* SAFEOUT */
226                 ret = pmic_reg_read(p, MAX77693_SAFEOUT, &val);
227                 if (ret)
228                         return -1;
229
230                 val |= MAX77693_ENSAFEOUT1;
231                 ret = pmic_reg_write(p, MAX77693_SAFEOUT, val);
232                 if (ret)
233                         return -1;
234
235                 /* PATH: USB */
236                 ret = pmic_reg_write(p_muic, MAX77693_MUIC_CONTROL1,
237                         MAX77693_MUIC_CTRL1_DN1DP2);
238
239         } else {
240                 ret = max77686_set_ldo_mode(p_pmic, 12, OPMODE_LPM);
241                 if (ret)
242                         return -1;
243
244                 /* PATH: UART */
245                 ret = pmic_reg_write(p_muic, MAX77693_MUIC_CONTROL1,
246                         MAX77693_MUIC_CTRL1_UT1UR2);
247         }
248
249         if (ret)
250                 return -1;
251 #endif
252         return 0;
253 }
254
255 struct dwc2_plat_otg_data s5pc210_otg_data = {
256         .phy_control    = s5pc210_phy_control,
257         .regs_phy       = EXYNOS4X12_USBPHY_BASE,
258         .regs_otg       = EXYNOS4X12_USBOTG_BASE,
259         .usb_phy_ctrl   = EXYNOS4X12_USBPHY_CONTROL,
260         .usb_flags      = PHY0_SLEEP,
261 };
262
263 int board_usb_init(int index, enum usb_init_type init)
264 {
265         debug("USB_udc_probe\n");
266         return dwc2_udc_probe(&s5pc210_otg_data);
267 }
268
269 int g_dnl_board_usb_cable_connected(void)
270 {
271 #ifndef CONFIG_DM_I2C /* TODO(maintainer): Convert to driver model */
272         struct pmic *muic = pmic_get("MAX77693_MUIC");
273         if (!muic)
274                 return 0;
275
276         return !!muic->chrg->chrg_type(muic);
277 #else
278         return false;
279 #endif
280 }
281 #endif
282
283 /*
284  * LCD
285  */
286
287 #ifdef CONFIG_LCD
288 int mipi_power(void)
289 {
290 #ifndef CONFIG_DM_I2C /* TODO(maintainer): Convert to driver model */
291         struct pmic *p = pmic_get("MAX77686_PMIC");
292
293         /* LDO8 VMIPI_1.0V_AP */
294         max77686_set_ldo_mode(p, 8, OPMODE_ON);
295         /* LDO10 VMIPI_1.8V_AP */
296         max77686_set_ldo_mode(p, 10, OPMODE_ON);
297 #endif
298
299         return 0;
300 }
301
302 void exynos_lcd_power_on(void)
303 {
304 #ifndef CONFIG_DM_I2C /* TODO(maintainer): Convert to driver model */
305         struct pmic *p = pmic_get("MAX77686_PMIC");
306
307         /* LCD_2.2V_EN: GPC0[1] */
308         gpio_request(EXYNOS4X12_GPIO_C01, "lcd_2v2_en");
309         gpio_set_pull(EXYNOS4X12_GPIO_C01, S5P_GPIO_PULL_UP);
310         gpio_direction_output(EXYNOS4X12_GPIO_C01, 1);
311
312         /* LDO25 VCC_3.1V_LCD */
313         pmic_probe(p);
314         max77686_set_ldo_voltage(p, 25, 3100000);
315         max77686_set_ldo_mode(p, 25, OPMODE_LPM);
316 #endif
317 }
318
319 void exynos_reset_lcd(void)
320 {
321         /* reset lcd */
322         gpio_request(EXYNOS4X12_GPIO_F21, "lcd_reset");
323         gpio_direction_output(EXYNOS4X12_GPIO_F21, 0);
324         udelay(10);
325         gpio_set_value(EXYNOS4X12_GPIO_F21, 1);
326 }
327
328 void exynos_lcd_misc_init(vidinfo_t *vid)
329 {
330 #ifdef CONFIG_TIZEN
331         get_tizen_logo_info(vid);
332 #endif
333 #ifdef CONFIG_S6E8AX0
334         s6e8ax0_init();
335 #endif
336 }
337 #endif /* LCD */