aboutsummaryrefslogtreecommitdiff
path: root/hal/hardware/peripheral_io.h
blob: 624e83bb462b5cff71db33d4d5a482497f8cb34d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_PERIPHERAL_IO_INTERFACE_H
#define ANDROID_PERIPHERAL_IO_INTERFACE_H

#include <stdint.h>
#include <sys/cdefs.h>
#include <sys/types.h>

#include <hardware/hardware.h>

__BEGIN_DECLS

/**
 * Id of this module.
 */
#define PERIPHERAL_IO_HARDWARE_MODULE_ID "peripheral_io"

/**
 * Possible GPIO direction configuration.
 */
typedef enum {
  GPIO_DIRECTION_IN,      /** Input */
  GPIO_DIRECTION_OUT_LOW, /** Output, initially low */
  GPIO_DIRECTION_OUT_HIGH /** Output, initially high */
} gpio_direction_t;

/**
 * Callback to enable a given pinmux source on a specific pin.
 *
 * Args:
 *  pin: Name of the pin.
 *  source: Name of the pinmux source to enable. When NULL, setup the pinmux
 *          to enable GPIO on this pin.
 *
 * Returns:
 *  0 on success, errno on error.
 */
typedef int (*pin_mux_cb)(const char* pin, const char* source);

/**
 * Callback to set the direction of a given GPIO.
 *
 * Args:
 *  pin: Name of the pin to configure.
 *  dir: Direction to set. One of gpio_direction_t.
 *
 * Returns:
 *  0 on success, errno on error.
 */
typedef int (*pin_mux_direction_cb)(const char* pin, int dir);

/**
 * Callbacks used by peripheral manager to configure given pins.
 */
typedef struct pin_mux_callbacks {
  pin_mux_cb mux_cb;
  pin_mux_direction_cb direction_cb;
} pin_mux_callbacks;

/**
 * Callbacks into peripheral manager.
 *
 * Those callbacks must be used in register_devices to declare the available
 * interfaces to peripheral manager.
 *
 * All peripherals are referred to by their friendly name.
 * The friendly name is a string, used to refer to a given peripheral (ex: gpio
 * name, spi bus name) that is simple to understand, well documented for the
 * board and unambiguous.
 *
 * Before coming up with a new naming scheme, consider:
 * - Using an existing naming scheme: if the board provides an arduino pinout, a
 *   raspberry Pi pinout or a 96 boards pinout, use it.
 * - Using the names written on the board next to the physical ports if any.
 * - Using the documented name when widely available.
 *   Referring to the same pin or peripheral by two different names in the
 *   documentation and in peripheral manager would be confusing to the user.
 *
 * If coming up with a new name, use only simple characters (ideally, A-Z, 0-9,
 * -, _).
 */
typedef struct peripheral_registration_cb_t {
  /**
   * Register a pin.
   *
   * Args:
   *  name: Friendly name (see definition above) of the pin.
   *  callbacks: Callbacks used to set up pinmuxes.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_pin)(const char* name, int gpio, pin_mux_callbacks callbacks);

  /**
   * Register a pin group.
   *
   * Args:
   *  name: Name of the group.
   *  pins: List of pin names that are part of the group.
   *  nr_pins: Size of |pins|.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_pin_group)(const char* name, char** pins, size_t nr_pins);

  /**
   * Register a pinmux source.
   *
   * Args:
   *  name: Name of the pinmux source.
   *  groups: List of possible pinmux groups this source can come up on.
   *  nr_groups: Size of |groups|.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_source)(const char* name, char** groups, size_t nr_groups);

  /**
   * Register a simple source, mapped to a single pin group.
   *
   * This convenience function replaces the two calls to register_pin_group and
   * register_source in most cases.
   *
   * Args:
   *  name: Name of the pinmux source.
   *  pins: List of pins this source comes up on.
   *  nr_pins: Size of |pins|.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_simple_source)(const char* name,
                                const char** pins,
                                size_t nr_pins);

  /**
   * Register a sysfs backed GPIO.
   *
   * Args:
   *  name: Friendly name of the GPIO.
   *  index: Index of the gpio in sysfs.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_gpio_sysfs)(const char* name, uint32_t index);

  /**
   * Set the pinmux for a given GPIO.
   *
   * Args:
   *  name: Friendly name of the pin.
   *  source: Name of the pinmux source.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*set_gpio_pin_mux)(const char* name, const char* source);

  /**
   * Register a SPI device.
   *
   * Args:
   *  name: Friendly name of the device.
   *  bus: Bus number.
   *  cs: Chip select.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_spi_dev_bus)(const char* name, uint32_t bus, uint32_t cs);

  /**
   * Set the pinmux for a given SPI device.
   *
   * Args:
   *  name: Friendly name of the device.
   *  source: Name of the pinmuxing source.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*set_spi_pin_mux)(const char* name, const char* source);

  /**
   * Register a sysfs-backed LED.
   *
   * Args:
   *  name: Friendly name of the LED.
   *  sysfs_name: Name of the device in sysfs.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_led_sysfs)(const char* name, const char* sysfs_name);

  /**
   * Register a UART bus.
   *
   * Args:
   *  name: Friendly name of the bus.
   *  dev_name: Name of the device in sysfs.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_uart_bus)(const char* name, const char* dev_name);

  /**
   * Set the pinmux for a given UART bus.
   *
   * Args:
   *  name: Friendly name of the UART bus.
   *  source: Name of the pinmuxing source.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*set_uart_pin_mux)(const char* name, const char* source);

  /**
   * Register an I2C bus.
   *
   * Args:
   *  name: Friendly name of the I2C device.
   *  bus: I2C bus number.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_i2c_dev_bus)(const char* name, uint32_t bus);

  /**
   * Set the pinmux for a given I2C bus.
   *
   * Args:
   *  name: Friendly name of the I2C bus.
   *  source: Name of the pinmuxing source.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*set_i2c_pin_mux)(const char* name, const char* source);

} peripheral_registration_cb_t;

typedef struct peripheral_io_module_t peripheral_io_module_t;

struct peripheral_io_module_t {
  struct hw_module_t common;

  /**
   * Register all available devices with the peripheral manager.
   *
   * Arguments:
   *  dev: Pointer to the peripheral_io module.
   *  callbacks: Callbacks into peripheral manager to register devices.
   *
   * Returns:
   *  0 on success, errno on error.
   */
  int (*register_devices)(const peripheral_io_module_t* dev,
                          const peripheral_registration_cb_t* callbacks);
};

__END_DECLS

#endif  // ANDROID_PERIPHERAL_IO_INTERFACE_H