1 /*
2 * Author: Henry Bruce <henry.bruce@intel.com>
3 * Evan Steele <evan.steele@intel.com>
4 * Copyright (c) 2014 Intel Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/utsname.h>
29 #include <ctype.h>
30
31 #include "common.h"
32 #include "x86/intel_minnow_byt_compatible.h"
33
34 #define PLATFORM_NAME "MinnowBoard MAX"
35 #define I2C_BUS_DEFAULT 7
36 #define MAX_LENGTH 8
37 #define I2CNAME "designware"
38
39 int arch_nr_gpios_adjust = 0x100;
40
41 mraa_result_t
mraa_set_pininfo(mraa_board_t * board,int mraa_index,char * name,mraa_pincapabilities_t caps,int sysfs_pin)42 mraa_set_pininfo(mraa_board_t* board, int mraa_index, char* name, mraa_pincapabilities_t caps, int sysfs_pin)
43 {
44 if (mraa_index < board->phy_pin_count) {
45 // adjust mraa_index for ARCH_NR_GPIOS value
46 mraa_pininfo_t* pin_info = &board->pins[mraa_index];
47 strncpy(pin_info->name, name, MAX_LENGTH);
48 pin_info->capabilites = caps;
49 if (caps.gpio) {
50 pin_info->gpio.pinmap = sysfs_pin | arch_nr_gpios_adjust;
51 pin_info->gpio.mux_total = 0;
52 }
53 if (caps.i2c) {
54 pin_info->i2c.pinmap = 1;
55 pin_info->i2c.mux_total = 0;
56 }
57 if (caps.pwm) {
58 int controller = 0;
59 if (strncmp(name, "PWM", 3) == 0 && strlen(name) > 3 && isdigit(name[3]))
60 controller = name[3] - '0';
61 pin_info->pwm.parent_id = controller;
62 pin_info->pwm.pinmap = 0;
63 pin_info->pwm.mux_total = 0;
64 }
65 if (caps.spi) {
66 pin_info->spi.mux_total = 0;
67 }
68 return MRAA_SUCCESS;
69 }
70 return MRAA_ERROR_INVALID_RESOURCE;
71 }
72
73 mraa_result_t
mraa_get_pin_index(mraa_board_t * board,char * name,int * pin_index)74 mraa_get_pin_index(mraa_board_t* board, char* name, int* pin_index)
75 {
76 int i;
77 for (i = 0; i < board->phy_pin_count; ++i) {
78 if (strncmp(name, board->pins[i].name, MAX_LENGTH) == 0) {
79 *pin_index = i;
80 return MRAA_SUCCESS;
81 }
82 }
83 return MRAA_ERROR_INVALID_RESOURCE;
84 }
85
86 mraa_board_t*
mraa_intel_minnowboard_byt_compatible(mraa_boolean_t turbot)87 mraa_intel_minnowboard_byt_compatible(mraa_boolean_t turbot)
88 {
89 mraa_board_t* b = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
90
91 struct utsname running_uname;
92 int uname_major, uname_minor, max_pins[28];
93
94 if (b == NULL) {
95 return NULL;
96 }
97
98 b->platform_name = PLATFORM_NAME;
99 if (turbot) {
100 b->platform_version = "Turbot";
101 b->gpio_count = b->phy_pin_count = MRAA_INTEL_MINNOW_TURBOT_PINCOUNT;
102 } else {
103 b->platform_version = "Ax";
104 b->gpio_count = b->phy_pin_count = MRAA_INTEL_MINNOW_MAX_PINCOUNT;
105 }
106
107 b->pins = (mraa_pininfo_t*) calloc(b->phy_pin_count, sizeof(mraa_pininfo_t));
108 if (b->pins == NULL) {
109 goto error;
110 }
111
112 b->adv_func = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
113 if (b->adv_func == NULL) {
114 free(b->pins);
115 goto error;
116 }
117
118 if (uname(&running_uname) != 0) {
119 free(b->pins);
120 free(b->adv_func);
121 goto error;
122 }
123
124 sscanf(running_uname.release, "%d.%d", &uname_major, &uname_minor);
125
126 /* if we are on Linux 3.17 or lower they use a 256 max and number the GPIOs down
127 * if we are on 3.18 or higher (ea584595fc85e65796335033dfca25ed655cd0ed) (for now)
128 * they start at 512 and number down, at some point this is going to change again when
129 * GPIO moves to a radix.
130 */
131 if (uname_major <= 3 && uname_minor <= 17) {
132 arch_nr_gpios_adjust = 0;
133 }
134
135 mraa_set_pininfo(b, 0, "INVALID", (mraa_pincapabilities_t){ 0, 0, 0, 0, 0, 0, 0, 0 }, -1);
136 mraa_set_pininfo(b, 1, "GND", (mraa_pincapabilities_t){ 0, 0, 0, 0, 0, 0, 0, 0 }, -1);
137 mraa_set_pininfo(b, 2, "GND", (mraa_pincapabilities_t){ 0, 0, 0, 0, 0, 0, 0, 0 }, -1);
138 mraa_set_pininfo(b, 3, "5v", (mraa_pincapabilities_t){ 0, 0, 0, 0, 0, 0, 0, 0 }, -1);
139 mraa_set_pininfo(b, 4, "3.3v", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 }, -1);
140 mraa_set_pininfo(b, 5, "SPI_CS", (mraa_pincapabilities_t){ 1, 0, 0, 0, 1, 0, 0, 0 }, 220);
141 mraa_set_pininfo(b, 6, "UART1TX", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 1 }, 225);
142 mraa_set_pininfo(b, 7, "SPIMISO", (mraa_pincapabilities_t){ 1, 0, 0, 0, 1, 0, 0, 0 }, 221);
143 mraa_set_pininfo(b, 8, "UART1RX", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 1 }, 224);
144 mraa_set_pininfo(b, 9, "SPIMOSI", (mraa_pincapabilities_t){ 1, 0, 0, 0, 1, 0, 0, 0 }, 222);
145 mraa_set_pininfo(b, 10, "UART1CT", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 227);
146 mraa_set_pininfo(b, 11, "SPI_CLK", (mraa_pincapabilities_t){ 1, 0, 0, 0, 1, 0, 0, 0 }, 223);
147 mraa_set_pininfo(b, 12, "UART1RT", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 226);
148 mraa_set_pininfo(b, 13, "I2C_SCL", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 }, 243);
149 mraa_set_pininfo(b, 14, "I2S_CLK", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 216);
150 mraa_set_pininfo(b, 15, "I2C_SDA", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 }, 242);
151 mraa_set_pininfo(b, 16, "I2S_FRM", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 217);
152 mraa_set_pininfo(b, 17, "UART2TX", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 1 }, 229);
153 mraa_set_pininfo(b, 18, "I2S_DO", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 219);
154 mraa_set_pininfo(b, 19, "UART2RX", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 1 }, 228);
155 mraa_set_pininfo(b, 20, "I2S_DI", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 218);
156 mraa_set_pininfo(b, 21, "S5_0", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 82);
157 mraa_set_pininfo(b, 22, "PWM0", (mraa_pincapabilities_t){ 1, 0, 1, 0, 0, 0, 0, 0 },
158 248); // Assume BIOS configured for PWM
159 mraa_set_pininfo(b, 23, "S5_1", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 83);
160 mraa_set_pininfo(b, 24, "PWM1", (mraa_pincapabilities_t){ 1, 0, 1, 0, 0, 0, 0, 0 },
161 249); // Assume BIOS configured for PWM
162 mraa_set_pininfo(b, 25, "S5_4", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 84);
163 if (turbot) {
164 mraa_set_pininfo(b, 26, "I2S_MCLK", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 253);
165 mraa_set_pininfo(b, 27, "S5_22", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 104);
166 } else {
167 mraa_set_pininfo(b, 26, "IBL8254", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 208);
168 }
169
170 // Set number of i2c adaptors usable from userspace
171 b->i2c_bus_count = 1;
172
173 // Configure i2c adaptor #7 and make it the default
174 int pin_index_sda, pin_index_scl;
175 if (mraa_get_pin_index(b, "I2C_SDA", &pin_index_sda) == MRAA_SUCCESS &&
176 mraa_get_pin_index(b, "I2C_SCL", &pin_index_scl) == MRAA_SUCCESS) {
177 int bus = mraa_find_i2c_bus(I2CNAME, 0);
178 if (bus == -1) {
179 b->i2c_bus_count = 0;
180 } else {
181 b->def_i2c_bus = bus;
182 b->i2c_bus[0].bus_id = b->def_i2c_bus;
183 b->i2c_bus[0].sda = pin_index_sda;
184 b->i2c_bus[0].scl = pin_index_scl;
185 }
186 }
187
188 // Configure PWM
189 b->pwm_default_period = 500;
190 b->pwm_max_period = 1000000000;
191 b->pwm_min_period = 1;
192
193 b->spi_bus_count = 1;
194 b->def_spi_bus = 0;
195 b->spi_bus[0].bus_id = 0;
196 b->spi_bus[0].slave_s = 0;
197 b->spi_bus[0].cs = 5;
198 b->spi_bus[0].mosi = 9;
199 b->spi_bus[0].miso = 7;
200 b->spi_bus[0].sclk = 11;
201
202 b->uart_dev_count = 1;
203 b->def_uart_dev = 0;
204 b->uart_dev[0].rx = -1;
205 b->uart_dev[0].tx = -1;
206 b->uart_dev[0].device_path = "/dev/ttyS0";
207
208 return b;
209 error:
210 syslog(LOG_CRIT, "minnowmax: Platform failed to initialise");
211 free(b);
212 return NULL;
213 }
214