Set volume on both channel. Semplfy the code.
authorasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Mon, 18 Jul 2011 14:32:50 +0000 (14:32 +0000)
committerasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Mon, 18 Jul 2011 14:32:50 +0000 (14:32 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4974 38d2e660-2303-0410-9eaa-f027e97ec537

bertos/drv/wm8731.c
bertos/drv/wm8731.h

index 3bb09f28dcc88f2777c63d8028ad755399d24537..0b56af962531f84b525eef261d459ffe042dd811 100644 (file)
@@ -66,48 +66,33 @@ static void wm8731_write(Wm8731 *ctx, uint8_t reg, uint16_t value)
 
 }
 
+#define RANGECONV(data, y1, y2) (((((int32_t)(data)) * ((y2) - (y1))) / ((1 << 8) - 1)) + (y1))
 void wm8731_setVolume(Wm8731 *ctx, uint16_t device, uint8_t volume)
 {
        uint16_t value;
 
-       if (device & WM8731_LINE_IN_RX)
+       if (device & WM8731_LINE_IN)
        {
                if (!volume)
-                       wm8731_write(ctx, WM8731_REG_RIGHT_LINEIN, BV(WM8731_LINMUTE_BIT));
+               {
+                       wm8731_write(ctx, WM8731_REG_RIGHT_LINEIN, BV(WM8731_LINMUTE_BIT) | BV(WM8731_RLINBOTH_BIT));
+                       wm8731_write(ctx, WM8731_REG_LEFT_LINEIN, BV(WM8731_LINMUTE_BIT) | BV(WM8731_LRINBOTH_BIT));
+               }
 
-               value = DIV_ROUND(volume * WM8731_LINVOL_BITS_MASK, 100) & WM8731_LINVOL_BITS_MASK;
+               value = DIV_ROUND(volume * WM8731_LINVOL_BITS_MASK, 100);
 
-               wm8731_write(ctx, WM8731_REG_RIGHT_LINEIN, ~BV(WM8731_LINMUTE_BIT));
-               wm8731_write(ctx, WM8731_REG_RIGHT_LINEIN, value);
-               LOG_INFO("Set LINE IN Rx vol[%d]%% raw[%d]\n", volume, value);
+               wm8731_write(ctx, WM8731_REG_RIGHT_LINEIN, ~BV(WM8731_LINMUTE_BIT) | value);
+               wm8731_write(ctx, WM8731_REG_LEFT_LINEIN, ~BV(WM8731_RINMUTE_BIT) | value);
+               LOG_INFO("Set LINE IN vol[%d]%% raw[%d]\n", volume, value);
        }
 
-       if (device & WM8731_LINE_IN_LX)
+       if (device & WM8731_HEADPHONE)
        {
-               if (!volume)
-                       wm8731_write(ctx, WM8731_REG_LEFT_LINEIN, BV(WM8731_LINMUTE_BIT));
-
-               value = DIV_ROUND(volume * WM8731_RINVOL_BITS_MASK, 100) & WM8731_RINVOL_BITS_MASK;
-
-               wm8731_write(ctx, WM8731_REG_LEFT_LINEIN, ~BV(WM8731_LINMUTE_BIT));
-               wm8731_write(ctx, WM8731_REG_LEFT_LINEIN, value);
-               LOG_INFO("Set LINE IN Lx vol[%d]%% raw[%d]\n", volume, value);
-       }
-
+               value = DIV_ROUND(volume * WM8731_RHPVOL_BITS_MASK, 100);
 
-       if (device & WM8731_HEADPHONE_RX)
-       {
-               value = DIV_ROUND(volume * WM8731_RHPVOL_BITS_MASK, 100) & WM8731_LHPVOL_BITS_MASK;
-               wm8731_write(ctx, WM8731_REG_RIGHT_HPOUT, value | BV(WM8731_RZCEN_BIT));
-               LOG_INFO("Set HEADPHONE Rx vol[%d]%% raw[%d]\n", volume, value);
-       }
-
-
-       if (device & WM8731_HEADPHONE_LX)
-       {
-               value = DIV_ROUND(volume * WM8731_LHPVOL_BITS_MASK, 100) & WM8731_LHPVOL_BITS_MASK;
-               wm8731_write(ctx, WM8731_REG_LEFT_HPOUT, value  | BV(WM8731_LZCEN_BIT));
-               LOG_INFO("Set HEADPHONE Lx vol[%d]%% raw[%d]\n", volume, value);
+               wm8731_write(ctx, WM8731_REG_RIGHT_HPOUT, value | BV(WM8731_RZCEN_BIT) | BV(WM8731_RLHPBOTH_BIT));
+               wm8731_write(ctx, WM8731_REG_LEFT_HPOUT, value  | BV(WM8731_LZCEN_BIT) | BV(WM8731_LRHPBOTH_BIT));
+               LOG_INFO("Set HEADPHONE vol[%d]%% raw[%d]\n", volume, value);
        }
 
 }
index 2cccc691d04a20a642640feacd322a71e055cbcf..aaf98228cf1baa04ef59d4b361641df48cec5430 100644 (file)
 
 /* Left Line in register */
 #define WM8731_REG_LEFT_LINEIN                     0x0
-#define WM8731_LINVOL_BITS_MASK                    0xF // Left line input volume control
+#define WM8731_LINVOL_BITS_MASK                   0x1F // Left line input volume control
 #define WM8731_LINMUTE_BIT                           7 // Left line input mute to ADC
 #define WM8731_LRINBOTH_BIT                          8 // Left to right channel line input volume and mute data load control
 
 /* Right Line in register */
 #define WM8731_REG_RIGHT_LINEIN                    0x1
-#define WM8731_RINVOL_BITS_MASK                    0xF // Right line input volume control
+#define WM8731_RINVOL_BITS_MASK                   0x1F // Right line input volume control
 #define WM8731_RINMUTE_BIT                           7 // Right line input mute to ADC
 #define WM8731_RLINBOTH_BIT                          8 // Right to right channel line input volume and mute data load control
 
 /* Left Headphone out register*/
 #define WM8731_REG_LEFT_HPOUT                      0x2
-#define WM8731_LHPVOL_BITS_MASK                    0x7 // Left channel headphone output volume control
+#define WM8731_LHPVOL_BITS_MASK                   0x7F // Left channel headphone output volume control
 #define WM8731_LZCEN_BIT                             7 // Left channel zero cross detect enable
 #define WM8731_LRHPBOTH_BIT                          8 // Left to right channel headphone volume, mute and zero cross data load control
 
 /* Right Headphone out register*/
 #define WM8731_REG_RIGHT_HPOUT                      0x3
-#define WM8731_RHPVOL_BITS_MASK                     0x7 // Right channel headphone output volume control
+#define WM8731_RHPVOL_BITS_MASK                    0x7F // Right channel headphone output volume control
 #define WM8731_RZCEN_BIT                              7 // Right channel zero cross detect enable
 #define WM8731_RLHPBOTH_BIT                           8 // Right to right channel headphone volume, mute and zero cross data load control
 
@@ -174,11 +174,13 @@ typedef struct Wm8731
 } Wm8731;
 
 
-
-#define        WM8731_LINE_IN_RX     BV(0)
-#define        WM8731_LINE_IN_LX     BV(1)
-#define        WM8731_HEADPHONE_RX   BV(2)
-#define        WM8731_HEADPHONE_LX   BV(3)
+/**
+ * Devices which is possible to set volume
+ * \}
+ */
+#define        WM8731_LINE_IN        BV(0)
+#define        WM8731_HEADPHONE      BV(1)
+/* \} */
 
 /**
  * Set the volume of select device (line in Rx/Lx, Headphone Rx/Lx).