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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd.
*/
#include "hal_base.h"
#if defined(HAL_PINCTRL_MODULE_ENABLED) && (defined(RKMCU_PISCES) || defined(RKMCU_KOALA) || defined(SOC_RK1808) || defined(RKMCU_RK2108) || defined(RKMCU_RK2206))
/** @addtogroup RK_HAL_Driver
* @{
*/
/** @addtogroup PINCTRL
* @{
*/
/** @defgroup PINCTRL_How_To_Use How To Use
* @{
The pinctrl setting registers actually is bus grf registers, which include
iomux, drive strength, pull mode, slew rate and schmitt trigger.
The pinctrl driver provides APIs:
- HAL_PINCTRL_SetPinIOMUX to set pin iomux
- HAL_PINCTRL_SetPinParam to set pin drive/pull/slewrate/schmitt
Example:
HAL_PINCTRL_SetIOMUX(GPIO_BANK0,
GPIO_PIN_A0 | // I2S_IN_SCLK
GPIO_PIN_A1 | // I2S_IN_LRCK
GPIO_PIN_A2 | // I2S_IN_SDI0
GPIO_PIN_A3, // I2S_IN_SDI1
PIN_CONFIG_MUX_FUNC2);
HAL_PINCTRL_SetParam(GPIO_BANK0,
GPIO_PIN_A0 | // I2S_IN_SCLK
GPIO_PIN_A1 | // I2S_IN_LRCK
GPIO_PIN_A2 | // I2S_IN_SDI0
GPIO_PIN_A3, // I2S_IN_SDI1
PIN_CONFIG_PUL_DOWN |
PIN_CONFIG_DRV_LEVEL2 |
PIN_CONFIG_SRT_FAST |
PIN_CONFIG_SMT_ENABLE);
@} */
/** @defgroup PINCTRL_Private_Definition Private Definition
* @{
*/
/********************* Private MACRO Definition ******************************/
#define PIN_READ(r) (*(volatile uint32_t *)(r))
#define PIN_WRITE(r, b, w, v) (*(volatile uint32_t *)(r) = (((w) << (16) | (v)) << (b)))
/********************* Private Variable Definition ***************************/
__WEAK const struct HAL_PINCTRL_DEV g_pinDev;
/********************* Private Function Definition ***************************/
static const struct HAL_PINCTRL_DEV *PINCTRL_GetInfo(void)
{
return &g_pinDev;
}
/**
* @brief Acquire params for iomux setting.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param mux: func data.
* @return HAL_Status.
*/
static HAL_Status PINCTRL_AcquireMuxRoute(const struct PINCTRL_BANK_INFO *pBank, uint8_t pin, uint32_t mux)
{
const struct HAL_PINCTRL_DEV *ctrl = PINCTRL_GetInfo();
const struct PINCTRL_MUX_ROUTE_DATA *data;
uint32_t i, reg = 0;
HAL_Status rc = HAL_OK;
if (ctrl->muxRouteDataNum == 0) {
rc = HAL_ERROR;
goto exit;
}
for (i = 0; i < ctrl->muxRouteDataNum; i++) {
data = &ctrl->muxRouteData[i];
if (data->bank == pBank->channel &&
data->pin & (1 << pin) && data->func == mux) {
break;
}
}
if (i >= ctrl->muxRouteDataNum) {
rc = HAL_ERROR;
goto exit;
}
reg = pBank->grfBase + data->routeReg;
*(volatile uint32_t *)(reg) = data->routeVal;
HAL_DBG("setting route %08lx = %08lx, %08lx\n", reg, data->routeVal, *(volatile uint32_t *)(reg));
exit:
return rc;
}
/**
* @brief Rectify params for iomux setting.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param reg: to get reg offset of the pin.
* @param bit: to get bit offset of the pin.
* @param mask: to get mask bits of the pin.
* @return HAL_Status.
*/
static HAL_Status PINCTRL_RectifyMuxParams(const struct PINCTRL_BANK_INFO *pBank, uint8_t pin,
uint32_t *reg, uint8_t *bit, uint32_t *mask)
{
const struct HAL_PINCTRL_DEV *ctrl = PINCTRL_GetInfo();
const struct PINCTRL_MUX_RECAL_DATA *data;
uint32_t i;
HAL_Status rc = HAL_OK;
if (ctrl->muxRecalDataNum == 0) {
rc = HAL_ERROR;
goto exit;
}
for (i = 0; i < ctrl->muxRecalDataNum; i++) {
data = &ctrl->muxRecalData[i];
if (data->bank == pBank->channel &&
data->pin == pin) {
break;
}
}
if (i >= ctrl->muxRecalDataNum) {
rc = HAL_ERROR;
goto exit;
}
*reg = data->reg;
*mask = data->mask;
*bit = data->bit;
exit:
return rc;
}
/**
* @brief Core function to get params.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param id: get params for @ref ePIN_GRF_INFO_ID.
* @param reg: to get reg offset of the pin.
* @param bit: to get bit offset of the pin.
* @param mask: to get mask bits of the pin.
* @return HAL_Status: HAL_OK for success to get param.
*/
static HAL_Status PINCTRL_AcquireParam(const struct PINCTRL_BANK_INFO *pBank, uint8_t pin,
ePIN_GRF_INFO_ID id,
uint32_t *reg, uint8_t *bit, uint32_t *mask)
{
uint8_t paramBit = pBank->GRFInfo[id].bitsPerPin;
uint8_t paramPin = pBank->GRFInfo[id].pinsPerReg;
uint32_t regVal, mskVal;
uint8_t bitVal;
/* The '0' value means nothing to set */
if (paramBit <= 0) {
return HAL_ERROR;
}
/* GRF(PMUGRF) address + mux offset + pin offset */
regVal = pBank->grfBase;
regVal += pBank->GRFInfo[id].offset;
regVal += (pin / paramPin) * 4;
bitVal = (pin % paramPin) * paramBit;
mskVal = (0x1U << paramBit) - 0x1U;
*reg = regVal;
*bit = bitVal;
*mask = mskVal;
return HAL_OK;
}
#if defined(RKMCU_RK2206)
#define GRF_SARADC_IEN_EN(x) ((0x0U << GRF_SOC_CON15_GRF_SARADC_IEN_SHIFT) << (x))
#define GRF_SARADC_IEN_DIS(x) ((0x1U << GRF_SOC_CON15_GRF_SARADC_IEN_SHIFT) << (x))
#define GRF_SARADC_IEN_MASK(x) ((0x1U << GRF_SOC_CON15_GRF_SARADC_IEN_SHIFT) << (x))
#define IS_MUX_SARADC(m) ((m) == PIN_CONFIG_MUX_FUNC1)
/**
* @brief Extra iomux for GPIO0_C0-GPIO0_C7 on rk2206 SoCs.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param arg: multi params defined in @ref ePINCTRL_configParam,
*/
static void PINCTRL_ExtraSet(const struct PINCTRL_BANK_INFO *pBank, uint8_t pin, uint32_t arg)
{
if (pin < 16 || pin > 23 || (pBank->channel != GPIO_BANK0)) {
return;
}
if (IS_MUX_SARADC(arg)) {
WRITE_REG_MASK_WE(GRF->SOC_CON15,
GRF_SARADC_IEN_MASK(pin - 16),
GRF_SARADC_IEN_DIS(pin - 16));
} else {
WRITE_REG_MASK_WE(GRF->SOC_CON15,
GRF_SARADC_IEN_MASK(pin - 16),
GRF_SARADC_IEN_EN(pin - 16));
}
}
#else
static inline void PINCTRL_ExtraSet(const struct PINCTRL_BANK_INFO *pBank, uint8_t pin, uint32_t arg)
{
}
#endif
/**
* @brief Private function to set iomux for one pin.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param param: mux value to set.
* @return HAL_Status.
*/
static HAL_Status PINCTRL_SetMux(const struct PINCTRL_BANK_INFO *pBank,
uint8_t pin, uint8_t param)
{
uint32_t reg = 0, mask = 0;
uint8_t bit = 0;
HAL_Status rc = HAL_OK;
HAL_DBG("setting GPIO%d-%d to %d\n", pBank->channel, pin, param);
rc = PINCTRL_AcquireParam(pBank, pin, GRF_MUX_INFO, ®, &bit, &mask);
if (rc) {
HAL_DBG("Warning: Acquire Mux Param failed\n");
}
rc = PINCTRL_RectifyMuxParams(pBank, pin, ®, &bit, &mask);
if (rc == HAL_OK) {
HAL_DBG("Warning: Param is rectified\n");
}
PIN_WRITE(reg, bit, mask, param);
rc = PINCTRL_AcquireMuxRoute(pBank, pin, param);
if (rc == HAL_OK) {
HAL_DBG("setting route for GPIO%d-%d\n", pBank->channel, pin);
}
return HAL_OK;
}
/**
* @brief Set a new drive strength for a pin.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param param: drive strength to set.
* @return HAL_Status.
*/
static uint32_t PINCTRL_SetDrive(const struct PINCTRL_BANK_INFO *pBank,
uint8_t pin, uint8_t param)
{
uint32_t reg = 0, mask = 0;
uint8_t bit = 0;
HAL_Status rc = HAL_OK;
HAL_DBG("setting GPIO%d-%d drive to %d\n", pBank->channel, pin, param);
rc = PINCTRL_AcquireParam(pBank, pin, GRF_DRV_INFO, ®, &bit, &mask);
if (rc) {
goto exit;
}
PIN_WRITE(reg, bit, mask, param);
exit:
return rc;
}
/**
* @brief Set a new pull mode for a pin.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param param: pull mode to set.
* @return HAL_Status.
*/
static HAL_Status PINCTRL_SetPull(const struct PINCTRL_BANK_INFO *pBank,
uint8_t pin, uint8_t param)
{
uint32_t reg = 0, mask = 0;
uint8_t bit = 0;
HAL_Status rc = HAL_OK;
HAL_DBG("setting GPIO%d-%d pull to %d\n", pBank->channel, pin, param);
rc = PINCTRL_AcquireParam(pBank, pin, GRF_PUL_INFO, ®, &bit, &mask);
if (rc) {
goto exit;
}
PIN_WRITE(reg, bit, mask, param);
exit:
return rc;
}
/**
* @brief Enable schmitt trigger for a pin.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param param: schmitt trigger enable or not.
* @return HAL_Status.
*/
static HAL_Status PINCTRL_SetSchmitt(const struct PINCTRL_BANK_INFO *pBank,
uint8_t pin, uint8_t param)
{
uint32_t reg = 0, mask = 0;
uint8_t bit = 0;
HAL_Status rc = HAL_OK;
HAL_DBG("setting GPIO%d-%d schmitt to %d\n", pBank->channel, pin, param);
rc = PINCTRL_AcquireParam(pBank, pin, GRF_SMT_INFO, ®, &bit, &mask);
if (rc) {
goto exit;
}
PIN_WRITE(reg, bit, mask, param);
exit:
return rc;
}
/**
* @brief Change slew rate for a pin.
* @param pBank: point to pin bank info.
* @param pin: pin index, 0~31.
* @param param: slew rate fast or slow
* @return HAL_Status.
*/
static HAL_Status PINCTRL_SetSlewRate(const struct PINCTRL_BANK_INFO *pBank,
uint8_t pin, uint8_t param)
{
uint32_t reg = 0, mask = 0;
uint8_t bit = 0;
HAL_Status rc = HAL_OK;
HAL_DBG("setting GPIO%d-%d slewrate to %d\n", pBank->channel, pin, param);
rc = PINCTRL_AcquireParam(pBank, pin, GRF_SRT_INFO, ®, &bit, &mask);
if (rc) {
goto exit;
}
PIN_WRITE(reg, bit, mask, param);
exit:
return rc;
}
/**
* @brief Private function to configure one pin.
* @param bank: pin bank channel defined in @ref eGPIO_bankId.
* @param pin: pin index, 0~31.
* @param param: multi params defined in @ref ePINCTRL_configParam,
* @return HAL_Status.
*/
static HAL_Status PINCTRL_SetParam(const struct PINCTRL_BANK_INFO *pBank,
uint8_t pin, uint32_t param)
{
HAL_Status rc = HAL_OK;
if (param & FLAG_PUL) {
rc |= PINCTRL_SetPull(pBank, pin, (uint8_t)((param & MASK_PUL) >> SHIFT_PUL));
}
if (param & FLAG_DRV) {
rc |= PINCTRL_SetDrive(pBank, pin, (uint8_t)((param & MASK_DRV) >> SHIFT_DRV));
}
if (param & FLAG_SMT) {
rc |= PINCTRL_SetSchmitt(pBank, pin, (uint8_t)((param & MASK_SMT) >> SHIFT_SMT));
}
if (param & FLAG_SRT) {
rc |= PINCTRL_SetSlewRate(pBank, pin, (uint8_t)((param & MASK_SRT) >> SHIFT_SRT));
}
return rc;
}
/**
* @brief Private function to configure one pin.
* @param bank: pin bank channel defined in @ref eGPIO_bankId.
* @param pin: pin index, 0~31.
* @param param: multi params defined in @ref ePINCTRL_configParam,
* @return HAL_Status.
*/
static HAL_Status PINCTRL_SetPinParam(eGPIO_bankId bank, uint8_t pin, uint32_t param)
{
const struct HAL_PINCTRL_DEV *ctrl = PINCTRL_GetInfo();
return PINCTRL_SetParam(&ctrl->banks[bank], pin, param);
}
/**
* @brief Private function to set iomux for one pin.
* @param bank: pin bank channel defined in @ref eGPIO_bankId.
* @param pin: pin index, 0~31.
* @param param: PIN_CONFIG_MUX_* defined in @ref ePINCTRL_configParam.
* @return HAL_Status.
*/
static HAL_Status PINCTRL_SetPinIOMUX(eGPIO_bankId bank, uint8_t pin, uint32_t param)
{
const struct HAL_PINCTRL_DEV *ctrl = PINCTRL_GetInfo();
PINCTRL_ExtraSet(&ctrl->banks[bank], pin, param);
return PINCTRL_SetMux(&ctrl->banks[bank], pin, (uint8_t)((param & MASK_MUX) >> SHIFT_MUX));
}
/** @} */
/********************* Public Function Definition ****************************/
/** @defgroup PINCTRL_Exported_Functions_Group1 Suspend and Resume Functions
This section provides functions allowing to suspend and resume the module:
* @{
*/
HAL_Status HAL_PINCTRL_Suspend(void)
{
return HAL_OK;
}
HAL_Status HAL_PINCTRL_Resume(void)
{
return HAL_OK;
}
/** @} */
/** @defgroup PINCTRL_Exported_Functions_Group2 Init and DeInit Functions
This section provides functions allowing to init and deinit the module:
* @{
*/
HAL_Status HAL_PINCTRL_Init(void)
{
#ifdef RKMCU_PISCES
HAL_PINCTRL_SetIOMUX(GPIO_BANK0, GPIO_PIN_ALL & ~(
GPIO_PIN_C7 | // UART0_RX
GPIO_PIN_D0 | // UART0_TX
GPIO_PIN_D5 | // MIPI_SWITCH_CTRL
GPIO_PIN_D6), // AUD_BY_CTRL
PIN_CONFIG_MUX_FUNC0);
HAL_PINCTRL_SetIOMUX(GPIO_BANK1, GPIO_PIN_ALL & ~(
GPIO_PIN_A0 | // LCD_IN_RESET_N
GPIO_PIN_A1 | // LCD_IN_TE
GPIO_PIN_A2 | // LCD_OUT_RESETN
GPIO_PIN_A3 | // LCD_OUT_TE
GPIO_PIN_A5 | // BOOT_DEV_SEL
GPIO_PIN_A6 | // M4_DSP_JTAG_SEL
GPIO_PIN_B2), // CLK_IN_SEL
PIN_CONFIG_MUX_FUNC0);
HAL_PINCTRL_SetParam(GPIO_BANK0, GPIO_PIN_ALL & ~(
GPIO_PIN_D5 | // MIPI_SWITCH_CTRL
GPIO_PIN_D6), // AUD_BY_CTRL
PIN_CONFIG_PUL_NORMAL);
HAL_PINCTRL_SetParam(GPIO_BANK1, GPIO_PIN_ALL & ~(
GPIO_PIN_A5 | // BOOT_DEV_SEL
GPIO_PIN_A6 | // M4_DSP_JTAG_SEL
GPIO_PIN_B2), // CLK_IN_SEL
PIN_CONFIG_PUL_NORMAL);
#endif
return HAL_OK;
}
HAL_Status HAL_PINCTRL_DeInit(void)
{
return HAL_OK;
}
/** @} */
/** @defgroup PINCTRL_Exported_Functions_Group3 State and Errors Functions
This section provides functions allowing to get the status of the module:
* @{
*/
/**
* @brief Public function to configure for multi pins.
* @param bank: pin bank channel defined in ref eGPIO_bankId.
* @param mPins: multi pins defined in @ref ePINCTRL_GPIO_PINS.
* @param param: multi params defined in @ref ePINCTRL_configParam,
* @return HAL_Status.
*/
HAL_Status HAL_PINCTRL_SetParam(eGPIO_bankId bank, uint32_t mPins, ePINCTRL_configParam param)
{
uint8_t pin;
HAL_Status rc = HAL_OK;
HAL_ASSERT(bank < GPIO_BANK_NUM);
HAL_ASSERT(IS_GPIO_PIN(mPins));
for (pin = 0; pin < 32; pin++) {
if (mPins & (1 << pin)) {
rc = PINCTRL_SetPinParam(bank, pin, param);
if (rc) {
return rc;
}
}
}
return rc;
}
/**
* @brief Public function to set iomux for multi pins.
* @param bank: pin bank channel defined in ref eGPIO_bankId.
* @param mPins: multi pins defined in @ref ePINCTRL_GPIO_PINS.
* @param param: param PIN_CONFIG_MUX_* defined in @ref ePINCTRL_configParam.
* @return HAL_Status.
*/
HAL_Status HAL_PINCTRL_SetIOMUX(eGPIO_bankId bank, uint32_t mPins, ePINCTRL_configParam param)
{
uint8_t pin;
HAL_Status rc;
HAL_ASSERT(bank < GPIO_BANK_NUM);
for (pin = 0; pin < 32; pin++) {
if (mPins & (1 << pin)) {
rc = PINCTRL_SetPinIOMUX(bank, pin, param);
if (rc) {
return rc;
}
}
}
return HAL_OK;
}
/** @} */
/** @defgroup PINCTRL_Exported_Functions_Group4 IO Functions
This section provides functions allowing to IO controlling:
* @{
*/
/** @} */
/** @} */
/** @} */
#endif /* HAL_PINCTRL_MODULE_ENABLED */
|