1 /*
2  * Author: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
3  * Copyright (c) 2014 Intel Corporation.
4  * Copyright (c) 2015 Linaro Limited.
5  *
6  * Copied from src/arm/banana.c
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/mman.h>
31 #include <mraa/common.h>
32 
33 #include "common.h"
34 #include "arm/96boards.h"
35 
36 #define DT_BASE "/sys/firmware/devicetree/base"
37 
38 #define PLATFORM_NAME_DB410C "DB410C"
39 #define PLATFORM_NAME_HIKEY "HIKEY"
40 
41 int db410c_ls_gpio_pins[MRAA_96BOARDS_LS_GPIO_COUNT] = {
42   36, 12, 13, 69, 115, 4, 24, 25, 35, 34, 28, 33,
43 };
44 
45 const char* db410c_serialdev[MRAA_96BOARDS_LS_UART_COUNT] = { "/dev/ttyMSM0", "/dev/ttyMSM1"};
46 
47 int hikey_ls_gpio_pins[MRAA_96BOARDS_LS_GPIO_COUNT] = {
48  488, 489, 490, 491, 492, 415, 463, 495, 426, 433, 427, 434,
49 };
50 
51 const char* hikey_serialdev[MRAA_96BOARDS_LS_UART_COUNT] = { "/dev/ttyAMA2", "/dev/ttyAMA3"};
52 
mraa_96boards()53 mraa_board_t* mraa_96boards()
54 {
55    int i, pin;
56    int *ls_gpio_pins;
57    char ch;
58    char name[MRAA_PIN_NAME_SIZE];
59 
60    mraa_board_t* b = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
61    if (b == NULL) {
62       return NULL;
63    }
64 
65    // pin mux for buses are setup by default by kernel so tell mraa to ignore them
66    b->no_bus_mux = 1;
67 
68    if (mraa_file_exist(DT_BASE "/model")) {
69         // We are on a modern kernel, great!!!!
70         if (mraa_file_contains(DT_BASE "/model", "Qualcomm Technologies, Inc. APQ 8016 SBC")) {
71             b->platform_name = PLATFORM_NAME_DB410C;
72             b->phy_pin_count = DB410C_PINCOUNT;
73             ls_gpio_pins = db410c_ls_gpio_pins;
74             b->uart_dev[0].device_path = db410c_serialdev[0];
75             b->uart_dev[1].device_path = db410c_serialdev[1];
76         } else if (mraa_file_contains(DT_BASE "/model", "HiKey Development Board")) {
77             b->platform_name = PLATFORM_NAME_HIKEY;
78             b->phy_pin_count = HIKEY_PINCOUNT;
79             ls_gpio_pins = hikey_ls_gpio_pins;
80             b->uart_dev[0].device_path = hikey_serialdev[0];
81             b->uart_dev[1].device_path = hikey_serialdev[1];
82 	}
83    }
84 
85    //UART
86    b->uart_dev_count = MRAA_96BOARDS_LS_UART_COUNT;
87    b->def_uart_dev = 0;
88 
89    //I2C
90    b->i2c_bus_count = MRAA_96BOARDS_LS_I2C_COUNT;
91    b->def_i2c_bus = 0;
92    b->i2c_bus[0].bus_id = 0;
93    b->i2c_bus[1].bus_id= 1;
94 
95    //SPI
96    b->spi_bus_count = MRAA_96BOARDS_LS_SPI_COUNT;
97    b->spi_bus[0].bus_id = 0;
98    b->def_spi_bus = 0;
99 
100    b->adv_func = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
101    if (b->adv_func == NULL) {
102       free(b);
103       return NULL;
104    }
105 
106    b->pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * b->phy_pin_count);
107    if (b->pins == NULL) {
108       free(b->adv_func);
109       free(b);
110       return NULL;
111    }
112 
113    // gpio lable starts from GPIO-A to GPIO-F
114    ch = 'A';
115    for (i = 0; i < MRAA_96BOARDS_LS_GPIO_COUNT; i++)
116    {
117       pin = ls_gpio_pins[i];
118       sprintf(name, "GPIO-%c", ch++);
119       strncpy(b->pins[pin].name, name, MRAA_PIN_NAME_SIZE);
120       b->pins[pin].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
121       b->pins[pin].gpio.pinmap = pin;
122       b->pins[pin].gpio.mux_total = 0;
123    }
124 
125    b->gpio_count = MRAA_96BOARDS_LS_GPIO_COUNT;
126 
127    b->aio_count = 0;
128    b->adc_raw = 0;
129    b->adc_supported = 0;
130 
131    return b;
132 }
133