oak/elm: Add support for anx7688 as PD device.
[depthcharge.git] / src / board / oak / board.c
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but without any warranty; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <assert.h>
19 #include <libpayload.h>
20
21 #include "base/init_funcs.h"
22 #include "boot/ramoops.h"
23 #include "drivers/bus/i2c/cros_ec_tunnel.h"
24 #include "drivers/bus/i2c/mtk_i2c.h"
25 #include "drivers/bus/i2s/mt8173.h"
26 #include "drivers/bus/spi/mt8173.h"
27 #include "drivers/bus/usb/usb.h"
28 #include "drivers/ec/anx7688/anx7688.h"
29 #include "drivers/ec/cros/ec.h"
30 #include "drivers/ec/cros/spi.h"
31 #include "drivers/flash/spi.h"
32 #include "drivers/gpio/sysinfo.h"
33 #include "drivers/gpio/mtk_gpio.h"
34 #include "drivers/power/mt6397.h"
35 #include "drivers/sound/i2s.h"
36 #include "drivers/sound/rt5645.h"
37 #include "drivers/sound/rt5677.h"
38 #include "drivers/storage/mtk_mmc.h"
39 #include "drivers/flash/mtk_nor_flash.h"
40 #include "drivers/tpm/slb9635_i2c.h"
41 #include "drivers/tpm/tpm.h"
42 #include "vboot/util/flag.h"
43
44 #include "drivers/video/display.h"
45 #include "drivers/video/mt8173_ddp.h"
46
47
48
49 int oak_backlight_update(DisplayOps *me, uint8_t enable)
50 {
51         static GpioOps *panel_lcd_power_en, *disp_pwm0, *panel_power_en;
52
53         if (!panel_power_en) {
54                 switch (lib_sysinfo.board_id + CONFIG_BOARD_ID_ADJUSTMENT) {
55                 case 1:
56                 case 2:
57                         panel_lcd_power_en = NULL;
58                         break;
59                 case 3:
60                         panel_lcd_power_en = new_mtk_gpio_output(PAD_UCTS2);
61                         break;
62                 case 4:
63                         panel_lcd_power_en = new_mtk_gpio_output(PAD_SRCLKENAI);
64                         break;
65                 default:
66                         panel_lcd_power_en = new_mtk_gpio_output(PAD_UTXD2);
67                 }
68
69                 disp_pwm0 = new_mtk_gpio_output(PAD_DISP_PWM0);
70                 panel_power_en = new_mtk_gpio_output(PAD_PCM_TX);
71         }
72
73         if (enable) {
74                 if (panel_lcd_power_en) {
75                         panel_lcd_power_en->set(panel_lcd_power_en, 1);
76                         mdelay(1);
77                 }
78
79                 disp_pwm0->set(disp_pwm0, 1);
80                 panel_power_en->set(panel_power_en, 1);
81         } else {
82                 panel_power_en->set(panel_power_en, 0);
83                 disp_pwm0->set(disp_pwm0, 0);
84                 if (panel_lcd_power_en)
85                         panel_lcd_power_en->set(panel_lcd_power_en, 0);
86         }
87
88         return 0;
89 }
90
91 static int sound_setup(void)
92 {
93         MtkI2s *i2s0 = new_mtk_i2s(0x11220000, 2, 48000);
94         I2sSource *i2s_source = new_i2s_source(&i2s0->ops, 48000, 2, 16000);
95         SoundRoute *sound_route = new_sound_route(&i2s_source->ops);
96         MTKI2c *i2c0 = new_mtk_i2c(0x11007000, 0x11000100);
97         rt5645Codec *rt5645 = new_rt5645_codec(&i2c0->ops, 0x1a);
98
99         list_insert_after(&rt5645->component.list_node, &sound_route->components);
100
101         if (lib_sysinfo.board_id + CONFIG_BOARD_ID_ADJUSTMENT < 5) {
102                 rt5677Codec *rt5677 = new_rt5677_codec(&i2c0->ops, 0x2c, 16,
103                                                        48000, 256, 0, 1);
104                 list_insert_after(&rt5677->component.list_node,
105                                   &sound_route->components);
106         }
107
108         /*
109          * Realtek codecs need SoC's I2S on before codecs are enabled
110          * or codecs' PLL will be in wrong state. Make i2s a route component
111          * so it can be enabled before codecs during route_enable_components()
112          * by inserting i2s before codec nodes
113          */
114         list_insert_after(&i2s0->component.list_node, &sound_route->components);
115         sound_set_ops(&sound_route->ops);
116         return 0;
117 }
118
119 static int board_setup(void)
120 {
121         MtkMmcTuneReg emmc_tune_reg = {.msdc_iocon = 0, .pad_tune = 0x10 << 16};
122         MtkMmcTuneReg sd_card_tune_reg = {.msdc_iocon = 0, .pad_tune = 0};
123
124         sysinfo_install_flags(new_mtk_gpio_input);
125
126         MTKI2c *i2c2 = new_mtk_i2c(0x11009000, 0x11000200);
127         tpm_set_ops(&new_slb9635_i2c(&i2c2->ops, 0x20)->base.ops);
128
129         MtkSpi *spibus = new_mtk_spi(0x1100A000);
130         CrosEcSpiBus *cros_ec_spi_bus = new_cros_ec_spi_bus(&spibus->ops);
131         GpioOps *ec_int = sysinfo_lookup_gpio("EC interrupt", 1,
132                                               new_mtk_gpio_input);
133         CrosEc *cros_ec = new_cros_ec(&cros_ec_spi_bus->ops, 0, ec_int);
134         register_vboot_ec(&cros_ec->vboot, 0);
135
136         /* oak-rev7 / elm-rev0 onwards use ANX7688. */
137         if (lib_sysinfo.board_id + CONFIG_BOARD_ID_ADJUSTMENT < 7) {
138                 CrosEc *cros_pd = new_cros_ec(&cros_ec_spi_bus->ops, 1, NULL);
139
140                 register_vboot_ec(&cros_pd->vboot, 1);
141         } else {
142                 CrosECTunnelI2c *cros_ec_i2c_tunnel =
143                         new_cros_ec_tunnel_i2c(cros_ec, 1);
144
145                 Anx7688 *anx7688 = new_anx7688(cros_ec_i2c_tunnel);
146
147                 register_vboot_ec(&anx7688->vboot, 1);
148         }
149
150         Mt6397Pmic *pmic = new_mt6397_power(0x1000D000, 0x10007000);
151         power_set_ops(&pmic->ops);
152
153         MtkMmcHost *emmc = new_mtk_mmc_host(0x11230000, 200 * MHz, 50 * MHz,
154                                             emmc_tune_reg, 8, 0, NULL);
155         GpioOps *card_detect_ops = new_gpio_not(new_mtk_gpio_input(PAD_EINT1));
156         MtkMmcHost *sd_card = new_mtk_mmc_host(0x11240000, 200 * MHz, 25 * MHz,
157                                                sd_card_tune_reg, 4, 1,
158                                                card_detect_ops);
159
160         list_insert_after(&emmc->mmc.ctrlr.list_node,
161                           &fixed_block_dev_controllers);
162         list_insert_after(&sd_card->mmc.ctrlr.list_node,
163                           &removable_block_dev_controllers);
164
165         /* Set display ops */
166         if (lib_sysinfo.framebuffer)
167                 display_set_ops(new_mt8173_display(oak_backlight_update));
168
169         UsbHostController *usb_host = new_usb_hc(XHCI, 0x11270000);
170
171         list_insert_after(&usb_host->list_node, &usb_host_controllers);
172
173         sound_setup();
174
175         ramoops_buffer(0xb1f00000, 0x100000, 0x20000);
176
177         /* Setup Nor flash ops */
178         MtkNorFlash *nor_flash = new_mtk_nor_flash(0x1100D000);
179         flash_set_ops(&nor_flash->ops);
180
181         return 0;
182 }
183
184 INIT_FUNC(board_setup);