1 /* This file was copied with little or no modification from the WizPort of OpenJazz */
2 /* There's also some bits from libcastor in here (Copyright (c) 2009 Adan Scotney) */
3 
4 #ifdef WIZ
5 
6 #include "../settings.h"
7 
8 #include "wiz.h"
9 
10 
11 #include <sys/ioctl.h>
12 #include <sys/soundcard.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include "../ticks.h"
16 
17 //For clock code
18 #include <sys/mman.h>
19 
20 
21 static int volume = 100;
22 static int volume_direction;
23 
WIZ_SetVolume(int volumePercent)24 void WIZ_SetVolume( int volumePercent )
25 {
26   if(volume==volumePercent) return;
27 
28   volume=volumePercent;
29   WIZ_AdjustVolume( VOLUME_NOCHG );
30 }
31 
WIZ_GetVolume()32 int WIZ_GetVolume()
33 {
34   return(volume);
35 }
36 
37 static int countDown=0;
38 static int lastVol = 0;
WIZ_ShowVolume(SDL_Surface * screen)39 void WIZ_ShowVolume(SDL_Surface* screen)
40 {
41   char buf[11]; //vol: xxx%\0 = 10.
42 
43   if(countDown > 0)
44   {
45     countDown-=getTicks();
46     sprintf(buf, "vol: %i", setting()->wizVol);
47     txtWriteCenter(screen, FONTSMALL, buf, 160, 200);
48   }
49 }
50 
51 
WIZ_AdjustVolume(int direction)52 void WIZ_AdjustVolume( int direction )
53 {
54 	if( direction != VOLUME_NOCHG )
55 	{
56 	  countDown=1500;
57 		if( volume <= 10 )
58 		{
59 			if( direction == VOLUME_UP )   volume += VOLUME_CHANGE_RATE/2;
60 			if( direction == VOLUME_DOWN ) volume -= VOLUME_CHANGE_RATE/2;
61 		}
62 		else
63 		{
64 			if( direction == VOLUME_UP )   volume += VOLUME_CHANGE_RATE;
65 			if( direction == VOLUME_DOWN ) volume -= VOLUME_CHANGE_RATE;
66 		}
67 
68 		if( volume < VOLUME_MIN ) volume = VOLUME_MIN;
69 		if( volume > VOLUME_MAX ) volume = VOLUME_MAX;
70 	}
71 	unsigned long soundDev = open("/dev/mixer", O_RDWR);
72 	if(soundDev)
73 	{
74 		int vol = ((volume << 8) | volume);
75 		ioctl(soundDev, SOUND_MIXER_WRITE_PCM, &vol);
76 		close(soundDev);
77 	}
78 	setting()->wizVol=volume;
79 	saveSettings();
80 }
81 
82 #define PLLSETREG0		(memregs32[0xF004>>2])
83 #define PWRMODE			(memregs32[0xF07C>>2])
84 #define SYS_CLK_FREQ 27
85 static int omhz=533;
WIZ_SetClock(int mhz)86 int WIZ_SetClock(int mhz)
87 {
88   if(mhz==omhz) return(0);
89   omhz=mhz;
90   /* Open CPU reg */
91   volatile uint32_t *memregs32;
92   int memfd;
93   memfd = open("/dev/mem", O_RDWR);
94 	if(memfd < 0) return(0);
95 
96 	memregs32 = (volatile uint32_t*)mmap(0, 0x20000, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, 0xC0000000);
97 	if(memregs32 == (volatile uint32_t*)0xFFFFFFFF) return(0);
98 
99   /* Set CPU Freq */
100 	unsigned  long v;
101 	unsigned mdiv, pdiv=9, sdiv=0;
102 
103 	mdiv= (mhz * pdiv) / SYS_CLK_FREQ;
104 	mdiv &= 0x3FF;
105 	v= pdiv<<18 | mdiv<<8 | sdiv;
106 
107 	PLLSETREG0 = v;
108 	PWRMODE |= 0x8000;
109 
110   /* Close CPU reg */
111 
112 	memregs32 = NULL;
113 	close(memfd);
114   return(1);
115 }
116 
platformExit()117 void platformExit()
118 {
119     WIZ_SetClock(533);
120 }
121 #endif
122