1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <ddk/debug.h>
6 #include <ddk/device.h>
7 #include <ddk/platform-defs.h>
8 #include <ddk/protocol/platform/bus.h>
9 #include <ddktl/protocol/gpioimpl.h>
10 
11 #include <soc/aml-t931/t931-gpio.h>
12 #include <soc/aml-t931/t931-hw.h>
13 
14 #include "sherlock.h"
15 
16 namespace sherlock {
17 
18 static const pbus_mmio_t i2c_mmios[] = {
19     {
20         .base = T931_I2C_AOBUS_BASE,
21         .length = 0x20,
22     },
23     {
24         .base = T931_I2C2_BASE,
25         .length = 0x20,
26     },
27     {
28         .base = T931_I2C3_BASE,
29         .length = 0x20,
30     },
31 };
32 
33 static const pbus_irq_t i2c_irqs[] = {
34     {
35         .irq = T931_I2C_AO_0_IRQ,
36         .mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
37     },
38     {
39         .irq = T931_I2C2_IRQ,
40         .mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
41     },
42     {
43         .irq = T931_I2C3_IRQ,
44         .mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
45     },
46 };
47 
__anon587b7e450102() 48 static pbus_dev_t i2c_dev = []() {
49     pbus_dev_t dev;
50     dev.name = "gpio";
51     dev.vid = PDEV_VID_AMLOGIC;
52     dev.pid = PDEV_PID_GENERIC;
53     dev.did = PDEV_DID_AMLOGIC_I2C;
54     dev.mmio_list = i2c_mmios;
55     dev.mmio_count = countof(i2c_mmios);
56     dev.irq_list = i2c_irqs;
57     dev.irq_count = countof(i2c_irqs);
58     return dev;
59 }();
60 
I2cInit()61 zx_status_t Sherlock::I2cInit() {
62 
63     // setup pinmux for our I2C busses
64     //i2c_ao_0
65     gpio_impl_.SetAltFunction(T931_GPIOAO(2), 1);
66     gpio_impl_.SetAltFunction(T931_GPIOAO(3), 1);
67     //i2c2
68     gpio_impl_.SetAltFunction(T931_GPIOZ(14), 3);
69     gpio_impl_.SetAltFunction(T931_GPIOZ(15), 3);
70     //i2c3
71     gpio_impl_.SetAltFunction(T931_GPIOA(14), 2);
72     gpio_impl_.SetAltFunction(T931_GPIOA(15), 2);
73 
74     zx_status_t status = pbus_.ProtocolDeviceAdd(ZX_PROTOCOL_I2C_IMPL, &i2c_dev);
75     if (status != ZX_OK) {
76         zxlogf(ERROR, "%s: ProtocolDeviceAdd failed %d\n", __func__, status);
77         return status;
78     }
79 
80     return ZX_OK;
81 }
82 
83 } // namespace sherlock
84