# PM_SetAmpGainLevel*

## Syntax

      #include <twl/spi.h>

      u32 PM_SetAmpGainLevel( u8 level );

      u32 PM_SetAmpGainLevelAsync( u8 level, PMCallback callback, void* arg );

## Arguments

level | Setting value that determines the amp gain.
callback | Callback that is called when the command finishes.
arg | Argument that is used when calling the callback.

## Return Values

If `PM_RESULT_SUCCESS`, the command execution has succeeded (for synchronous functions), or the command was successfully sent to the ARM7 processor (for asynchronous functions).

If `PM_RESULT_BUSY`, the SPI was occupied by other processing and unable to process this function.

## Description

Sets the gain of the programmable amp gain. This function can only be used when the operating mode is TWL mode.

The level argument is a `u8` value; specify a value in the range of 0 to 119.

Before you use this function, initialize the PM library with the `PM_Init` function. (The `PM_Init` function has to be called only once. Also, when you call the `OS_Init` function, there is no need to call the `PM_Init` function separately because it is called from inside `OS_Init`.)

Differences Between the `PM_SetAmpGain*` and `PM_SetAmpGainLevel*` Functions

There are two types of setter functions: `PM_SetAmpGain*` and `PM_SetAmpGainLevel*`. The former has existed since the old Nintendo DS and can specify four gain levels. The latter is explained in this reference page; it is new, has been added for the TWL system, and can set 120 gain levels.

The `PM_SetAmpGain*` functions can specify four levels and are usable in any mode. The specified value will always be set unchanged.

The `PM_SetAmpGainLevel*` functions can specify 120 levels and are usable only in TWL HYBRID and TWL LIMITED mode. The CODEC mode decides whether the value is actually configured as one of 120 levels, as specified. A 120-level setting will be applied as specified when the CODEC is running in TWL mode (in this case the operation mode should always be TWL mode). However, at all other times, values specified for 120 levels will be averaged to 4 levels. These four levels conform to the four levels set with the `PM_SetAmpGain` function.

Value of level and the Gain to Set

The value of the level argument and the actual amp gain that is set are shown below.

> When the codec is in TWL mode:

> level | Gain to Set
> --- | ---
> 0 | 10.5 dB
> 1 | 11.0 dB
> 2 | 11.5 dB
> : | :
> n | 10.5 + (n x 0.5) dB
> : | :
> 119 | 70.0 dB

> When the codec is in DS mode:

> level | Gain to Set
> --- | ---
> 0 ~ 36 | 26.0 dB (equivalent to 31 in the 120-level notation)
> 37 ~ 48 | 32.0 dB (equivalent to 43 in the 120-level notation)
> 49 ~ 60 | 38.0 dB (equivalent to 55 in the 120-level notation)
> 61 ~ 119 | 44.0 dB (equivalent to 67 in the 120-level notation)

Asynchronous Functions

This function uses PXI to send the command that performs the corresponding operation in the ARM7 processor. The ARM7 side that receives that command is executed by operating the PMIC. Therefore, this function may not operate instantly after you call it. A synchronous function that waits for the operation to finish, as well as an asynchronous function that only sends commands to the ARM7, are provided. Use either of the functions depending on your operational requirements. (The asynchronous function has "Async" appended to the function name.))

When an asynchronous function is called, the specified callback is called when processing on the ARM7 side finishes. The callback type `PMCallback` is defined by:

> ``typedef void ( *PMCallback )( u32 result, void* arg )`;`

This callback is called from within the PXI interrupt handler.

The callback's first argument, result, indicates the result of the command. This is either `PM_RESULT_SUCCESS` or `PM_RESULT_BUSY`. The second argument in the callback returns the value arg.

About `PM_RESULT_BUSY`

The SPI is used for various other processes besides power management. If you call this function while another process is using it, this function sends a command to the ARM7; there, the SPI is determined to be BUSY, and `PM_RESULT_BUSY` is notified to the ARM9 without actually processing this function. Likewise, if you call this function while another PM process is running, that fact is determined on the ARM9 side and this function returns `PM_RESULT_BUSY`. (In this case, the determination is made before notification is sent to the ARM7.)

Accordingly, if you want to make certain that this function will succeed, make it loop until it succeeds as shown below. (This example does not take into account mistakes such as wrong arguments.)

> Example
> `while( PM_SetAmpGainLevel( ... ) != PM_RESULT_SUCCESS )
> {
> }`

When using the `Async` version of this function, you could do this with code similar to the following.

> Example:

>     void setResult( u32 result, void* arg )
>     {
>       if ( arg )
>       {
>         *(u32*)arg = result;
>       }
>     }

>     while(1)
>     {
>       volatile u32 result = PM_RESULT_NONE; //A value that will not be returned

>       while ( PM_SetAmpGainLevelAsync(..., setResult, (void*)&result ) != PM_RESULT_SUCCESS )
>       {
>       }

>       // Some other process
>       otherProcess();

>       // Wait for completion
>       while( result == PM_RESULT_NONE )
>       {
>       }

>       // Exit the loop on success
>       if ( result == PM_RESULT_SUCCESS )
>       {
>         break;
>       }
>     }

## Internal Operation

On NITRO hardware, this function manipulates the PMIC register PGA_GAIN. On TWL hardware it manipulates the codec.

## See Also

`PM_Init`
`PM_SetAmp*`
`PM_SetAmpGain*`
`PM_GetAmpGainLevel`

## Revision History

2008/05/01 Initial version. Branched off from the `PM_SetAmpGain*` functions.

CONFIDENTIAL