hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
/* 
 * Copyright (C) 2014 Rockchip Corporation.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/i2c.h>
#include <linux/spi/spi.h>
#include <linux/platform_device.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/debugfs.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/iio/consumer.h>
#include <linux/of.h>
#include "nkio.h"
 
/* Debug */
#if 1
#define DBG(x...) printk(x)
#else
#define DBG(x...) do { } while (0)
#endif
 
struct ndj_io_pdata *NDJpdata_info;
u32 op0_enable,op1_enable,op2_enable,op3_enable,op4_enable,op5_enable,mic_enable;
u32 ip0_enable,ip1_enable,ip2_enable,ip3_enable,ip4_enable,ip5_enable;
u32 module_enable;
static struct class *ndj_class;
 
  
static int ndj_io_control_probe(struct platform_device *pdev)
{
   struct device_node *node = pdev->dev.of_node;
   struct ndj_io_pdata *pdata;
   int ret;
   int i=0;
   enum of_gpio_flags flags;
   int ch342_reset, ch342_reset_2;
   printk(" ####### NDJ_io_control_probe####### \n");
   
   pdata = kzalloc(sizeof(struct ndj_io_pdata), GFP_KERNEL);
   if (pdata == NULL) {
       printk("%s failed to allocate driver data\n",__FUNCTION__);
       return -ENOMEM;
   }
   memset(pdata,0,sizeof(struct ndj_io_pdata));
 
   
   //mic_switch_gpio
       ret = of_get_named_gpio_flags(node, "mic_switch_gpio", 0, &flags);
       if (ret < 0) {
               printk("%s() Can not read property mic_switch_gpio\n", __FUNCTION__);
               goto err;
       } else {
               pdata->mic_switch_gpio = ret;
               ret = devm_gpio_request(&pdev->dev, pdata->mic_switch_gpio, "mic_switch_gpio");
               if(ret < 0){
                       printk("%s() devm_gpio_request mic_switch_gpio request ERROR\n", __FUNCTION__);
                       goto err;
               }
               ret = gpio_direction_output(pdata->mic_switch_gpio,0); 
               mic_enable = 0;
               if(ret < 0){
               printk("%s() gpio_direction_input mic_switch_gpio set ERROR\n", __FUNCTION__);
               goto err;
       }
               }
 
   //vcc_5v_io
   ret = of_get_named_gpio_flags(node, "vcc_5v_io", 0, &flags);
   if (ret < 0) {
       printk("%s() Can not read property vcc_5v_io\n", __FUNCTION__);
       goto err;
   } else {
       pdata->vcc_5v_io = ret;
       ret = devm_gpio_request(&pdev->dev, pdata->vcc_5v_io, "vcc_5v_io");
       if(ret < 0){
           printk("%s() devm_gpio_request vcc_5v_io request ERROR\n", __FUNCTION__);
           goto err;
       }
 
       ret = gpio_direction_output(pdata->vcc_5v_io,1); 
       if(ret < 0){
           printk("%s() gpio_direction_output vcc_5v_io set ERROR\n", __FUNCTION__);
           goto err;
       }
   }
 
       //vcc_12v_io
   ret = of_get_named_gpio_flags(node, "vcc_12v_io", 0, &flags);
   if (ret < 0) {
       printk("%s() Can not read property vcc_12v_io\n", __FUNCTION__);
       goto err;
   } else {
       pdata->vcc_12v_io = ret;
       ret = devm_gpio_request(&pdev->dev, pdata->vcc_12v_io, "vcc_12v_io");
       if(ret < 0){
           printk("%s() devm_gpio_request vcc_12v_io request ERROR\n", __FUNCTION__);
           goto err;
       }
 
       ret = gpio_direction_output(pdata->vcc_12v_io,1); 
       if(ret < 0){
           printk("%s() gpio_direction_output vcc_12v_io set ERROR\n", __FUNCTION__);
           goto err;
       }
   }
#if 0
       //hub_5V_gpio
   ret = of_get_named_gpio_flags(node, "hub_5V_gpio", 0, &flags);
   if (ret < 0) {
       printk("%s() Can not read property hub_5V_gpio\n", __FUNCTION__);
       goto err;
   } else {
       
       pdata->hub_5V_gpio = ret;
       gpio_free(ret);
       ret = devm_gpio_request(&pdev->dev, pdata->hub_5V_gpio, "hub_5V_gpio");
       if(ret < 0){
           printk("%s() devm_gpio_request hub_5V_gpio request ERROR\n", __FUNCTION__);
           goto err;
       }
 
       ret = gpio_direction_output(pdata->hub_5V_gpio,1); 
       if(ret < 0){
           printk("%s() gpio_direction_output hub_5V_gpio set ERROR\n", __FUNCTION__);
           goto err;
       }
   }
#endif
 
   //hub_5V_rest_gpio
   ret = of_get_named_gpio_flags(node, "hub_5V_rest_gpio", 0, &flags);
   if (ret < 0) {
       printk("%s() Can not read property hub_5V_rest_gpio\n", __FUNCTION__);
       goto err;
   } else {
       pdata->hub_5V_rest_gpio = ret;
       ret = devm_gpio_request(&pdev->dev, pdata->hub_5V_rest_gpio, "hub_5V_rest_gpio");
       if(ret < 0){
           printk("%s() devm_gpio_request hub_5V_rest_gpio request ERROR\n", __FUNCTION__);
           goto err;
       }
 
       ret = gpio_direction_output(pdata->hub_5V_rest_gpio,0); 
       if(ret < 0){
           printk("%s() gpio_direction_output hub_5V_rest_gpio set ERROR\n", __FUNCTION__);
           goto err;
       }
       msleep(800);
       ret = gpio_direction_output(pdata->hub_5V_rest_gpio,1); 
       if(ret < 0){
           printk("%s() gpio_direction_output hub_5V_rest_gpio set ERROR\n", __FUNCTION__);
           goto err;
       }
   }
 
      ret = of_get_named_gpio_flags(node, "ch342_power", 0, &flags);
           if (ret < 0) {
                   printk("%s() Can not read property ch342_power\n", __FUNCTION__);
           } else {
                   pdata->ch342_power = ret;
 
                   ret = devm_gpio_request(&pdev->dev, pdata->ch342_power, "wch342_power");
                   if(ret < 0){
                           printk("%s() devm_gpio_request ch342_power request ERROR\n", __FUNCTION__);
                   }
                   ret = gpio_direction_output(pdata->ch342_power,1);
                   if(ret < 0){
                           printk("%s() gpio_direction_input wake_4g_gpio set ERROR\n", __FUNCTION__);
                   }
           }
 
   #if 1
         // ch342_reset
           ret = of_get_named_gpio_flags(node, "ch342_reset", 0, &flags);
           if (ret < 0) {
                   printk("%s() Can not read property ch342_reset\n", __FUNCTION__);
           } else {
                   pdata->reset_ch342_gpio = ret;
 
                   ret = devm_gpio_request(&pdev->dev, pdata->reset_ch342_gpio, "ch342_reset");
                   if(ret < 0){
                           printk("%s() devm_gpio_request ch342_reset request ERROR\n", __FUNCTION__);
                   }
       
           ret = gpio_direction_output(pdata->reset_ch342_gpio,0);
                if(ret < 0){
                        printk("%s() gpio_direction_output reset_ch342_gpio set ERROR\n", __FUNCTION__);
                        goto err;
                }
                msleep(200);
                ret = gpio_direction_output(pdata->reset_ch342_gpio,1);
                if(ret < 0){
                        printk("%s() gpio_direction_output reset_ch342_gpio set ERROR\n", __FUNCTION__);
                        goto err;
                }
           }
 
        ret = of_get_named_gpio_flags(node, "ch342_reset_2", 0, &flags);
           if (ret < 0) {
                   printk("%s() Can not read property ch342_reset_2\n", __FUNCTION__);
           } else {
                   pdata->reset_ch342_gpio_2 = ret;
 
                   ret = devm_gpio_request(&pdev->dev, pdata->reset_ch342_gpio_2, "ch342_reset_2");
                   if(ret < 0){
                           printk("%s() devm_gpio_request ch342_reset request ERROR\n", __FUNCTION__);
                   }
           ret = gpio_direction_output(pdata->reset_ch342_gpio_2,0);
                if(ret < 0){
                        printk("%s() gpio_direction_output reset_ch342_gpio_2 set ERROR\n", __FUNCTION__);
                        goto err;
                }
                msleep(200);
                ret = gpio_direction_output(pdata->reset_ch342_gpio_2,1);
                if(ret < 0){
                        printk("%s() gpio_direction_output reset_ch342_gpio_2 set ERROR\n", __FUNCTION__);
                        goto err;
                }
           }
#endif
#if 1
           //reset_4g_gpio
           ret = of_get_named_gpio_flags(node, "reset_4g_gpio", 0, &flags);
           if (ret < 0) {
                   printk("%s() Can not read property reset_4g_gpio\n", __FUNCTION__);
           } else {
                   pdata->reset_4g_gpio = ret;
 
                   ret = devm_gpio_request(&pdev->dev, pdata->reset_4g_gpio, "reset_4g_gpio");
                   if(ret < 0){
                           printk("%s() devm_gpio_request reset_4g_gpio request ERROR\n", __FUNCTION__);
                   }
                   ret = gpio_direction_output(pdata->reset_4g_gpio,1);
                   if(ret < 0){
                           printk("%s() gpio_direction_input reset_4g_gpio set ERROR\n", __FUNCTION__);
                   }
              mdelay(300);
                   ret = gpio_direction_output(pdata->reset_4g_gpio,0);
                   if(ret < 0){
                           printk("%s() gpio_direction_input reset_4g_gpio set ERROR\n", __FUNCTION__);
                   }
           }
#endif
 
            //en_4g_gpio
           ret = of_get_named_gpio_flags(node, "en_4g_gpio", 0, &flags);
           if (ret < 0) {
                   printk("%s() Can not read property en_4g_gpio\n", __FUNCTION__);
           } else {
                   pdata->en_4g_gpio = ret;
 
                   ret = devm_gpio_request(&pdev->dev, pdata->en_4g_gpio, "en_4g_gpio");
                   if(ret < 0){
                           printk("%s() devm_gpio_request en_4g_gpio request ERROR\n", __FUNCTION__);
                   }
                   ret = gpio_direction_output(pdata->en_4g_gpio,1);
                   if(ret < 0){
                           printk("%s() gpio_direction_input en_4g_gpio set ERROR\n", __FUNCTION__);
                   }
           }
 
 
           //air_mode_4g_gpio
           ret = of_get_named_gpio_flags(node, "air_mode_4g_gpio", 0, &flags);
           if (ret < 0) {
                   printk("%s() Can not read property air_mode_4g_gpio\n", __FUNCTION__);
           } else {
                   pdata->air_mode_4g_gpio = ret;
 
                   ret = devm_gpio_request(&pdev->dev, pdata->air_mode_4g_gpio, "air_mode_4g_gpio");
                   if(ret < 0){
                           printk("%s() devm_gpio_request air_mode_4g_gpio request ERROR\n", __FUNCTION__);
                   }
                   ret = gpio_direction_output(pdata->air_mode_4g_gpio,0);
                   if(ret < 0){
                           printk("%s() gpio_direction_input air_mode_4g_gpio set ERROR\n", __FUNCTION__);
                   }
           }
 
 
     //air_mode_4g_gpio
           ret = of_get_named_gpio_flags(node, "wake_4g_gpio", 0, &flags);
           if (ret < 0) {
                   printk("%s() Can not read property wake_4g_gpio\n", __FUNCTION__);
           } else {
                   pdata->air_mode_4g_gpio = ret;
 
                   ret = devm_gpio_request(&pdev->dev, pdata->air_mode_4g_gpio, "wake_4g_gpio");
                   if(ret < 0){
                           printk("%s() devm_gpio_request wake_4g_gpio request ERROR\n", __FUNCTION__);
                   }
                   ret = gpio_direction_output(pdata->wake_4g_gpio,0);
                   if(ret < 0){
                           printk("%s() gpio_direction_output wake_4g_gpio set ERROR\n", __FUNCTION__);
                   }
           }
 
 
 
   #if 0
       //audio_switch_gpio
   ret = of_get_named_gpio_flags(node, "audio_switch_gpio", 0, &flags);
   if (ret < 0) {
       printk("%s() Can not read property audio_switch_gpio\n", __FUNCTION__);
       goto err;
   } else {
       pdata->audio_switch_gpio = ret;
       ret = devm_gpio_request(&pdev->dev, pdata->audio_switch_gpio, "audio_switch_gpio");
       if(ret < 0){
           printk("%s() devm_gpio_request audio_switch_gpio request ERROR\n", __FUNCTION__);
           goto err;
       }
            ret = gpio_direction_output(pdata->audio_switch_gpio, !flags); 
       if(ret < 0){
       printk("%s() gpio_direction_input audio_switch_gpio set ERROR\n", __FUNCTION__);
       goto err;
   }
       }
       #endif
 
//    printk("fan_io_en ....\r\n"); 
    ret = of_get_named_gpio_flags(node, "fan_io_en", 0, &flags);
    if (ret < 0) {
        printk("%s() Can not read property fan_io_en\n", __FUNCTION__);
        goto err;
    } else {
        int gpio = ret;
        ret = devm_gpio_request(&pdev->dev, gpio, "fan_io_en");
        if(ret < 0){
            printk("%s() devm_gpio_request vcc_5v_io request ERROR\n", __FUNCTION__);
            goto err;
        }
 
        ret = gpio_direction_output(gpio, !flags); 
        if(ret < 0){
            printk("%s() gpio_direction_input fan_io_en set ERROR\n", __FUNCTION__);
            goto err;
        }
    }
 //   printk("fan_io_en ....ok!\r\n"); 
 
   
      
   NDJpdata_info = pdata;
 
   printk(" #######ndj_io_control_probe end####### \n");
   return 0;
err:
   kfree(pdata);
   return ret;
}
 
static int ndj_io_control_remove(struct platform_device *pdev)
{
   if(NDJpdata_info)
       kfree(NDJpdata_info);
   return 0;
}
 
static int ndj_io_control_suspend(struct platform_device *pdev, pm_message_t state)
{
   printk("LED_early_suspend LED_early_suspend LED_early_suspend !!!!\n");
 
   //enable = 0;
   //LED_SET(0);
   return 0;
}
 
static int ndj_io_control_resume(struct platform_device *pdev)
{
   printk("LED_early_resume LED_early_resume LED_early_resume !!!!\n");
 
   //enable = 1;
    //LED_SET(11);
   return 0;
}
 
static const struct of_device_id ndj_io_control_of_match[] = {
        { .compatible = "ndj_io_control", },
        {},
};
MODULE_DEVICE_TABLE(of, ndj_io_control_of_match);
 
static struct platform_driver ndj_io_control_driver = {
   .probe    = ndj_io_control_probe,
   .remove = ndj_io_control_remove,
   .resume = ndj_io_control_resume,    
   .suspend = ndj_io_control_suspend,    
   .driver    = {
       .name    = "ndj_io_control",
       .owner    = THIS_MODULE,
       .of_match_table = of_match_ptr(ndj_io_control_of_match),        
   },
};
 
static int __init ndj_io_control_init(void)
{
   platform_driver_register(&ndj_io_control_driver);
   return 0;
}
 
static void __exit ndj_io_control_exit(void)
{
   platform_driver_unregister(&ndj_io_control_driver);
}
 
subsys_initcall(ndj_io_control_init);
 
//late_initcall(ndj_io_control_init);
MODULE_DESCRIPTION("ndj io Core Driver");
MODULE_LICENSE("GPL");