usb: gadget: g_ffs: Allow to set bmAttributes of configuration
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / misc / sprd_board_type.c
1 #include <linux/module.h>
2 #include <linux/miscdevice.h>
3 #include <linux/fs.h>
4 #include <linux/uaccess.h>
5 #include <mach/adc.h>
6 #include <mach/hardware.h>
7 #include <asm/io.h>
8 #include <linux/gpio.h>
9
10
11 #define SPRD_RF_INSIDE  ("V1.0.0")
12 #define SPRD_RF_2IN1    ("V1.0.2")
13 #define SPRD_RF_3IN1    ("V1.0.4")
14 #define SPRD_RF_NO_MACH ("No match board")
15
16 #ifdef CONFIG_SPRD_BOARD_TYPE_GPIO
17 #define SPRD_BOARD_TYPE_GPIO_1  (92)
18 #define SPRD_BOARD_TYPE_GPIO_2  (93)
19 #endif
20
21 struct sprdboard_auxadc_cal {
22         uint16_t p0_vol;        //4.2V
23         uint16_t p0_adc;
24         uint16_t p1_vol;        //3.6V
25         uint16_t p1_adc;
26         uint16_t cal_type;
27 };
28
29 static struct sprdboard_auxadc_cal board_adc_cal = {
30         4200, 3310,
31         3600, 2832,
32         0,
33 };
34
35 static int board_type_open(struct inode *inode, struct file *file)
36 {
37         return 0;
38 }
39
40 static int board_type_release(struct inode *inode, struct file *filp)
41 {
42         return 0;
43 }
44
45 static int __init sprd_adc_cal_start(char *str)
46 {
47         unsigned int adc_data[2] = { 0 };
48         char *cali_data = &str[1];
49         if (str) {
50                 printk("adc_cal%s!\n", str);
51                 sscanf(cali_data, "%d,%d", &adc_data[0], &adc_data[1]);
52                 printk("adc_data: 0x%x 0x%x!\n", adc_data[0], adc_data[1]);
53                 board_adc_cal.p0_vol = adc_data[0] & 0xffff;
54                 board_adc_cal.p0_adc = (adc_data[0] >> 16) & 0xffff;
55                 board_adc_cal.p1_vol = adc_data[1] & 0xffff;
56                 board_adc_cal.p1_adc = (adc_data[1] >> 16) & 0xffff;
57                 printk("auxadc cal from cmdline ok!!! adc_data[0]: 0x%x, adc_data[1]:0x%x\n",adc_data[0], adc_data[1]);
58         }
59         return 1;
60 }
61
62 __setup("sprd_adc_cal", sprd_adc_cal_start);
63
64
65 static uint16_t sprd_adc_to_vol(uint16_t adcvalue)
66 {
67         int32_t temp;
68
69         temp = board_adc_cal.p0_vol - board_adc_cal.p1_vol;
70         temp = temp * (adcvalue - board_adc_cal.p0_adc);
71         temp = temp / (board_adc_cal.p0_adc - board_adc_cal.p1_adc);
72         temp = temp + board_adc_cal.p0_vol;
73
74         return temp;
75 }
76
77
78 static uint16_t sprd_vol_to_channel_vol(uint16_t adcvalue,uint16_t channel)
79 {
80         uint32_t result;
81         uint32_t channel_vol = sprd_adc_to_vol(adcvalue);
82         uint32_t m, n;
83         uint32_t bat_numerators, bat_denominators;
84         uint32_t adc_channel_numerators, adc_channel_denominators;
85
86         sci_adc_get_vol_ratio(ADC_CHANNEL_VBAT, 0, &bat_numerators,
87                               &bat_denominators);
88         sci_adc_get_vol_ratio(channel, 0, &adc_channel_numerators,
89                               &adc_channel_denominators);
90
91         ///v1 = vbat_vol*0.268 = vol_bat_m * r2 /(r1+r2)
92         n = bat_denominators * adc_channel_numerators;
93         m = channel_vol * bat_numerators * (adc_channel_denominators);
94         //result = (m + n / 2) / n;
95         result = m / n;
96         return result;
97
98 }
99
100 #ifdef CONFIG_SPRD_BOARD_TYPE_GPIO
101 static void get_board_type_by_gpio(char *buf_version)
102 {
103         int ret;
104         ret = gpio_request (SPRD_BOARD_TYPE_GPIO_1, "board_type_gpio_1");
105         if (ret){
106                 printk ("board_type_gpio_1 err: %d\n", SPRD_BOARD_TYPE_GPIO_1);
107         }
108
109         ret = gpio_request (SPRD_BOARD_TYPE_GPIO_2, "board_type_gpio_1");
110         if (ret){
111                 printk ("board_type_gpio_2 err: %d\n", SPRD_BOARD_TYPE_GPIO_2);
112         }
113         gpio_direction_input(SPRD_BOARD_TYPE_GPIO_1);
114         gpio_direction_input(SPRD_BOARD_TYPE_GPIO_2);
115
116         if((gpio_get_value(SPRD_BOARD_TYPE_GPIO_1) == 1) && (gpio_get_value(SPRD_BOARD_TYPE_GPIO_2) == 1))
117                 strcpy(buf_version,SPRD_RF_INSIDE);
118         else if((gpio_get_value(SPRD_BOARD_TYPE_GPIO_1) == 0) && (gpio_get_value(SPRD_BOARD_TYPE_GPIO_2) == 1))
119                 strcpy(buf_version,SPRD_RF_2IN1);//2in1
120         else if((gpio_get_value(SPRD_BOARD_TYPE_GPIO_1) == 1) && (gpio_get_value(SPRD_BOARD_TYPE_GPIO_2) == 0))
121                 strcpy(buf_version,SPRD_RF_3IN1);//3in1
122         else
123                 strcpy(buf_version,SPRD_RF_NO_MACH);
124
125         gpio_free(SPRD_BOARD_TYPE_GPIO_1);
126         gpio_free(SPRD_BOARD_TYPE_GPIO_2);
127 }
128
129 #endif
130
131 ssize_t board_type_read(struct file *file, char __user *buf,
132                 size_t count, loff_t *offset)
133 {
134         char buf_version[20] = {0};
135         u32 buf_len;
136
137         #ifdef CONFIG_SPRD_BOARD_TYPE_GPIO
138         get_board_type_by_gpio(buf_version);
139         #else
140         int adc_value;
141         uint16_t vol;//mv
142
143         #ifdef CONFIG_ARCH_SCX15
144         adc_value = sci_adc_get_value_by_isen(ADC_CHANNEL_0,0,40); //set current to 40 uA
145         vol = sprd_vol_to_channel_vol(adc_value,ADC_CHANNEL_0);
146         //printk("10 adc_value:%d,vol:%d\n", adc_value,vol);
147
148         if(vol >= 1000)
149                 strcpy(buf_version,SPRD_RF_INSIDE);
150         else if(vol >600 && vol <= 1000)
151                 strcpy(buf_version,SPRD_RF_3IN1); //3in1
152         else if(vol >200 && vol <=600)
153                 strcpy(buf_version,SPRD_RF_2IN1);//2in1
154         else
155                 strcpy(buf_version,SPRD_RF_NO_MACH);
156
157         #else
158
159         //adc_value = sci_adc_get_value(ADC_CHANNEL_1, 0);
160         adc_value = sci_adc_get_value_by_isen(ADC_CHANNEL_1,0,40); //set current to 40 uA
161         vol = sprd_vol_to_channel_vol(adc_value,ADC_CHANNEL_1);
162         //printk("10 adc_value:%d,vol:%d\n", adc_value,vol);
163
164         if(vol >= 1100)
165                 strcpy(buf_version,SPRD_RF_INSIDE);
166         else if(vol >700 && vol <= 900)
167                 strcpy(buf_version,SPRD_RF_2IN1);//2in1
168         else if(vol >300 && vol <=500)
169                 strcpy(buf_version,SPRD_RF_3IN1);//3in1
170         else
171                 strcpy(buf_version,SPRD_RF_NO_MACH);
172         #endif
173         #endif
174
175         buf_len = strlen(buf_version);
176         printk("version:%s\n",buf_version);
177         if(copy_to_user(buf, buf_version, buf_len))
178                 return -EFAULT;
179         return buf_len;
180 }
181
182
183 int sprd_kernel_get_board_type(char *buf,int read_len)
184 {
185         char buf_version[20] = {0};
186         u32 buf_len;
187
188         #ifdef CONFIG_SPRD_BOARD_TYPE_GPIO
189         get_board_type_by_gpio(buf_version);
190         #else
191         int adc_value;
192         uint16_t vol;//mv
193
194         #ifdef CONFIG_ARCH_SCX15
195         adc_value = sci_adc_get_value_by_isen(ADC_CHANNEL_0,0,40); //set current to 40 uA
196         vol = sprd_vol_to_channel_vol(adc_value,ADC_CHANNEL_0);
197         //printk("10 adc_value:%d,vol:%d\n", adc_value,vol);
198
199         if(vol >= 1000)
200                 strcpy(buf_version,SPRD_RF_INSIDE);
201         else if(vol >600 && vol <= 1000)
202                 strcpy(buf_version,SPRD_RF_3IN1); //3in1
203         else if(vol >200 && vol <=600)
204                 strcpy(buf_version,SPRD_RF_2IN1);//2in1
205         else
206                 strcpy(buf_version,SPRD_RF_NO_MACH);
207         #else
208         adc_value = sci_adc_get_value_by_isen(ADC_CHANNEL_1,0,40); //set current to 40 uA
209         vol = sprd_vol_to_channel_vol(adc_value,ADC_CHANNEL_1);
210         //printk("10 adc_value:%d,vol:%d\n", adc_value,vol);
211
212         if(vol >= 1100)
213                 strcpy(buf_version,SPRD_RF_INSIDE);
214         else if(vol >700 && vol <= 900)
215                 strcpy(buf_version,SPRD_RF_2IN1);//2in1
216         else if(vol >300 && vol <=500)
217                 strcpy(buf_version,SPRD_RF_3IN1);//3in1
218         else
219                 strcpy(buf_version,SPRD_RF_NO_MACH);
220         #endif
221         #endif
222
223         buf_len = strlen(buf_version);
224         printk("version:%s\n",buf_version);
225
226         if(read_len >= buf_len)
227                 strncpy(buf,buf_version,buf_len);
228         return buf_len;
229
230 }
231
232 EXPORT_SYMBOL_GPL(sprd_kernel_get_board_type);
233
234
235 static const struct file_operations sprd_board_type_fops = {
236         .open           =  board_type_open,
237         .read           =  board_type_read,
238         .release        =  board_type_release,
239         .owner          =  THIS_MODULE,
240 };
241
242 static struct miscdevice sprd_board_type_dev = {
243         .minor =    MISC_DYNAMIC_MINOR,
244         .name  =    "board_type",
245         .fops  =    &sprd_board_type_fops
246 };
247
248 static int sprd_board_type_init(void)
249 {
250         return misc_register(&sprd_board_type_dev);
251 }
252
253 static void __exit sprd_board_type_exit(void)
254 {
255         misc_deregister(&sprd_board_type_dev);
256 }
257
258 module_init(sprd_board_type_init);
259 module_exit(sprd_board_type_exit);
260 MODULE_LICENSE("GPL");
261 MODULE_DESCRIPTION("Driver for get board type");