summaryrefslogtreecommitdiff
path: root/io-module/mths.c
blob: df7fa27a6d6d50f6120e3879d148cd541a0bf12b (plain)
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
 * mths.c
 *
 *  Created on: Apr 26, 2018
 *      Author: leonid
 */

#include "buttons.h"

static struct gpio_pin gpio_pins_mths_0_0[] = {
    {
        .name = "RADIO_POWER_MONITOR",
        .pin = {
            .gpio = AT91_PIN_PA23,
            .flags = GPIOF_IN,
            .label = "radio-power-monitor",
        },
        .active_low = 0,
    },
    {
        .name = "RADIO_RESET",
        .pin = {
            .gpio = AT91_PIN_PA22,
            .flags = GPIOF_OUT_INIT_HIGH | GPIOF_OPEN_DRAIN,
            .label = "radio-reset",
        },
        .active_low = 0,
    },
    {
        .name = "RADIO_POWER",
        .pin = {
            .gpio = AT91_PIN_PA21,
            .flags = GPIOF_OUT_INIT_HIGH | GPIOF_OPEN_DRAIN,
            .label = "radio-power",
        },
        .active_low = 0,
    },
    {
        .name = "DEVICE_RESET",
        .pin = {
            .gpio = AT91_PIN_PC4,
            .flags = GPIOF_IN,
            .label = "reset",
        },
        .active_low = 1,
    },
    {
        .name = "WPS_BUTTON",
        .pin = {
            .gpio = AT91_PIN_PA13,
            .flags = GPIOF_IN,
            .label = "wps_button",
        },
        .active_low = 1,
    },
    {
        .name = "BT_BUTTON",
        .pin = {
            //TODO return back .gpio = AT91_PIN_PC4,
            .gpio = AT91_PIN_PA12,
            .flags = GPIOF_IN,
            .label = "bt_button",
        },
        .active_low = 1,
    },
    {
        .name = "LS_LED",
        .pin = {
            .gpio = AT91_PIN_PC4,
#if LED_LS_CONTROLLABLE
            .flags = GPIOF_OUT_INIT_HIGH,
#else
            .flags = GPIOF_IN,
#endif
            .label = "led-ls",
        },
        .active_low = 1,
    },
    {
        .name = "LED3",
        .pin = {
            .gpio = AT91_PIN_PC15,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-wifi",
        },
        .active_low = 1,
    },
    {
        .name = "LED3",
        .pin = {
            .gpio = AT91_PIN_PC15,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-b",
        },
        .active_low = 1,
    },
    {
        .name = "LED4",
        .pin = {
            .gpio = AT91_PIN_PC20,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-bt",
        },
        .active_low = 1,
    },
    {
        .name = "LED4",
        .pin = {
            .gpio = AT91_PIN_PC20,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-cd",
        },
        .active_low = 1,
    },
    {
	.name = "LED5",
	.pin = {
	    .gpio = AT91_PIN_PC16,
	    .flags = GPIOF_OUT_INIT_HIGH,
	    .label = "led-status",
	},
	.active_low = 1,
    },
    {
	.name = "LED5",
	.pin = {
	    .gpio = AT91_PIN_PC16,
	    .flags = GPIOF_OUT_INIT_HIGH,
	    .label = "led-c",
	},
	.active_low = 1,
    },
    {
        .name = "LED6",
        .pin = {
            .gpio = AT91_PIN_PC19,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-sig1",
        },
        .active_low = 1,
    },
    {
        .name = "LED6",
        .pin = {
            .gpio = AT91_PIN_PC19,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-d",
        },
        .active_low = 1,
    },
    {
        .name = "LED7",
        .pin = {
            .gpio = AT91_PIN_PC18,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-sig2",
        },
        .active_low = 1,
    },
    {
        .name = "LED7",
        .pin = {
            .gpio = AT91_PIN_PC18,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-e",
        },
        .active_low = 1,
    },
    {
        .name = "LED8",
        .pin = {
            .gpio = AT91_PIN_PC17,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-sig3",
        },
        .active_low = 1,
    },
    {
        .name = "LED8",
        .pin = {
            .gpio = AT91_PIN_PC17,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "led-f",
        },
        .active_low = 1,
    },

    // The difference between MTRv1_0_0 and MTRv1_0_1 starts here
    {
        .name = "WIFI_BT_ULPWKUP",
        .pin = {
            .gpio = AT91_PIN_PB9,
            .flags = GPIOF_IN,
            .label = "wifi-bt-ulpwkup",
        }
    },
    {
        .name = "WIFI_BT_LPWKUP",
        .pin = {
            .gpio = AT91_PIN_PB11,
            .flags = GPIOF_IN,
            .label = "wifi-bt-lpwkup",
        }
    },
    {
        .name = "WIFI_BT_INT",
        .pin = {
            .gpio = AT91_PIN_PB13,
            .flags = GPIOF_IN,
            .label = "wifi-bt-int",
        }
    },
    {
        .name = "WIFI_BT_RESET",
        .pin = {
            .gpio = AT91_PIN_PB10,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "wifi-bt-reset",
        }
    },
    {
        .name = "WIFI_BT_LPMODE",
        .pin = {
            .gpio = AT91_PIN_PB12,
            .flags = GPIOF_IN,
            .label = "wifi-bt-lpmode",
        }
    },
    {
        .name = "GNSS_RESET",
        .pin = {
            .gpio = AT91_PIN_PC6,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "gnss-reset",
        }
    },
    {
        .name = "USBHUB_RESET",
        .pin = {
            .gpio = AT91_PIN_PB6,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "usbhub-reset",
        }
    },
    {
        .name = "GNSS_INT",
        .pin = {
            .gpio = AT91_PIN_PC7,
            .flags = GPIOF_OUT_INIT_HIGH,
            .label = "gnss-int",
        }
    },


    { },
};

button_info_t mths_wps_button = {
        .name = "User Push Button WPS",
        .label_pin = "wps_button",
        .label_monitor = "wps_button-monitor",
        .label_monitor_intervals = "wps_button-monitor-intervals",

        /* Signals */
        .short_signal = SIGUSR1,
        .long_signal  = SIGUSR2,
        .short_signal = SIGHUP,

        /* Intervals */
        .short_interval = BUTTON_HOLD_COUNT,
        .long_interval = BUTTON_LONG_HOLD_COUNT,
};

button_info_t mths_bt_button = {
        .name = "User Push Button BT",
        .label_pin = "bt_button",
        .label_monitor = "bt_button-monitor",
        .label_monitor_intervals = "bt_button-monitor-intervals",

        /* Signals */
        .short_signal = SIGUSR1,
        .long_signal  = SIGUSR2,
        .short_signal = SIGHUP,

        /* Intervals */
        .short_interval = BUTTON_HOLD_COUNT,
        .long_interval = BUTTON_LONG_HOLD_COUNT,
};

static button_info_pt mths_buttons_0_0[] = {
        &reset_button,
        &mths_wps_button,
        &mths_bt_button,
        NULL,
};

static DEVICE_ATTR_MTS(dev_attr_wps_button_monitor_intervals_mths,
        mths_wps_button.label_monitor_intervals,
        mts_attr_show_button_monitor_intervals,
        mts_attr_store_button_monitor_intervals);

static DEVICE_ATTR_MTS(dev_attr_wps_button_monitor_mths,
        mths_wps_button.label_monitor,
        mts_attr_show_button_monitor,
        mts_attr_store_button_monitor);

static DEVICE_ATTR_RO_MTS(dev_attr_wps_button_pin_mths,
        mths_wps_button.label_pin,
        mts_attr_show_gpio_pin);


static DEVICE_ATTR_MTS(dev_attr_bt_button_monitor_intervals_mths,
        mths_bt_button.label_monitor_intervals,
        mts_attr_show_button_monitor_intervals,
        mts_attr_store_button_monitor_intervals);

static DEVICE_ATTR_MTS(dev_attr_bt_button_monitor_mths,
        mths_bt_button.label_monitor,
        mts_attr_show_button_monitor,
        mts_attr_store_button_monitor);

static DEVICE_ATTR_RO_MTS(dev_attr_bt_button_pin_mths,
        mths_bt_button.label_pin,
        mts_attr_show_gpio_pin);


/* radio control (power/reset) for mtr */
static int radio_off_mths(void)
{
    int value;
    struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor");
    struct gpio_pin *onoff_pin = gpio_pin_by_attr_name("radio-power");
    struct gpio_pin *rst_pin = gpio_pin_by_attr_name("radio-reset");

    if (!onoff_pin || !pwrmon_pin || !rst_pin) {
        return -ENODEV;
    }

    value = gpio_get_value(pwrmon_pin->pin.gpio);
    if(value == 0) {
        log_error("radio is already off");
        return -EINVAL;
    }

    // drive on/off pin low for at least 3 sec
    log_info("shutting down radio");
    gpio_set_value(onoff_pin->pin.gpio, 0);

    msleep(3500);

    // set on/off pin high
    gpio_set_value(onoff_pin->pin.gpio, 1);

    msleep(200);

    // check that power is low
    value = gpio_get_value(pwrmon_pin->pin.gpio);
    if(value != 0) {
        log_warning("radio is still on. performing radio reset.");
        //Perform Hard Reset
        gpio_set_value(rst_pin->pin.gpio, 0);

        msleep(500);

        // set pin high
        gpio_set_value(rst_pin->pin.gpio, 1);
    } else {
        log_info("radio has been shut down");
    }

    return 0;
}

static int radio_on_mths(void)
{
    int value;
    struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor");
    struct gpio_pin *onoff_pin = gpio_pin_by_attr_name("radio-power");
    struct gpio_pin *rst_pin = gpio_pin_by_attr_name("radio-reset");

    if (!onoff_pin || !pwrmon_pin || !rst_pin) {
        return -ENODEV;
    }

    value = gpio_get_value(pwrmon_pin->pin.gpio);
    if(value != 0) {
        log_error("radio is already on");
        return -EINVAL;
    }

    // drive on/off pin low for at least 5 sec
    log_info("turning on radio");
    gpio_set_value(onoff_pin->pin.gpio, 0);

    msleep(5500);

    // set on/off pin high
    gpio_set_value(onoff_pin->pin.gpio, 1);

    msleep(200);

    // check that power is high
    value = gpio_get_value(pwrmon_pin->pin.gpio);
    if(value == 0) {
        log_warning("radio is still off. performing radio reset");
        //Perform Hard Reset
        gpio_set_value(rst_pin->pin.gpio, 0);

        msleep(500);

        // set pin high
        gpio_set_value(rst_pin->pin.gpio, 1);
    } else {
        log_info("radio has been turned on");
    }

    return 0;
}

static int radio_reset_mths(void)
{
    struct gpio_pin *onoff_pin = gpio_pin_by_attr_name("radio-power");
    struct gpio_pin *rst_pin = gpio_pin_by_attr_name("radio-reset");

    if (!rst_pin || !onoff_pin) {
        return -ENODEV;
    }

    // drive reset pin low for 500ms
    gpio_set_value(rst_pin->pin.gpio, 0);

    msleep(500);

    // set pin high
    gpio_set_value(rst_pin->pin.gpio, 1);

    // wait for 2 sec before toggling on/off pin
    msleep(2000);

    // drive on/off pin low for 6 sec
    gpio_set_value(onoff_pin->pin.gpio, 0);

    msleep(6000);

    // set on/off pin high
    gpio_set_value(onoff_pin->pin.gpio, 1);
    return 0;
}

static ssize_t mts_attr_store_radio_power_mths(struct device *dev,
        struct device_attribute *attr, const char *buf, size_t count)
{
    int value;
    int err;

    if (sscanf(buf, "%i", &value) != 1) {
        return -EINVAL;
    }
    if (value == 0) {
        mutex_lock(&mts_io_mutex);
        err = radio_off_mths();
        mutex_unlock(&mts_io_mutex);
    } else {
        mutex_lock(&mts_io_mutex);
        err = radio_on_mths();
        mutex_unlock(&mts_io_mutex);
    }

    if (err) {
        return err;
    }

    return count;
}

static ssize_t mts_attr_store_radio_reset_mths(struct device *dev,
        struct device_attribute *attr, const char *buf, size_t count)
{
    int value; /* 0 = normal reset; -1 = forced reset */
    int err;

    if (sscanf(buf, "%i", &value) != 1) {
        return -EINVAL;
    }
    if (value != 0 && value != -1) {
        return -EINVAL;
    }

    /* check reset timings is enabled */
    if (value != -1 && NULL != timings_data) {
        /* check reset timer is started */
        if (radio_reset_timer_is_start == 1) {
            log_info("radio reset timer is running. \n");
            return count;
        }

        /* check reset timer available is started */
        if (radio_reset_available_timer_is_start == 1) {
            del_timer(&radio_reset_available_timer);
            radio_reset_available_timer_is_start = 0;
        }

        /* reset timer not started, start it */
        mod_timer(&radio_reset_timer, jiffies + msecs_to_jiffies((timings_data[timings_data_index]) * 1000));
        //log_info("radio reset timer is start = [%d]\n", time_now_secs());
        /* save timings_data_stop_seconds */
        timings_data_stop_seconds = timings_data[timings_data_index] + time_now_secs();
        radio_reset_timer_is_start = 1;
    }

    log_info("radio is reset\n");

    mutex_lock(&mts_io_mutex);

    err = radio_reset_mths();

    mutex_unlock(&mts_io_mutex);

    if (err) {
        return err;
    }

    return count;
}

static ssize_t mts_attr_show_radio_power_mths(struct device *dev,
            struct device_attribute *attr, char *buf)
{
    int value;
    struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor");

    if ( !pwrmon_pin ) {
        return -ENODEV;
    }

    value = gpio_get_value(pwrmon_pin->pin.gpio);
    return sprintf(buf, "%d\n", value);
}

static DEVICE_ATTR_MTS(dev_attr_radio_power_mths, "radio-power",
    mts_attr_show_radio_power_mths, mts_attr_store_radio_power_mths);

static DEVICE_ATTR_MTS(dev_attr_radio_reset_mths, "radio-reset",
    mts_attr_show_gpio_pin, mts_attr_store_radio_reset_mths);


static DEVICE_ATTR_MTS(dev_attr_led_wifi_gpio_mths, "led-wifi",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_MTS(dev_attr_led_bt_gpio_mths, "led-bt",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_MTS(dev_attr_led_f_gpio_mths, "led-f",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_MTS(dev_attr_led_e_gpio_mths, "led-e",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_RO_MTS(dev_attr_wifi_mac_mths, "mac-wifi",
    mts_attr_show_product_info);

static DEVICE_ATTR_MTS(dev_attr_wifi_bt_lpwkup_mths, "wifi-bt-lpwkup",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_MTS(dev_attr_wifi_bt_ulpwkup_mths, "wifi-bt-ulpwkup",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_MTS(dev_attr_wifi_bt_reset_mths, "wifi-bt-reset",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_RO_MTS(dev_attr_wifi_bt_lpmode_mths, "wifi-bt-lpmode",
    mts_attr_show_gpio_pin);

static DEVICE_ATTR_RO_MTS(dev_attr_wifi_bt_int_mths, "wifi-bt-int",
    mts_attr_show_gpio_pin);

static DEVICE_ATTR_MTS(dev_attr_gnss_reset_mths, "gnss-reset",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_MTS(dev_attr_usbhub_reset_mths, "usbhub-reset",
    mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);

static DEVICE_ATTR_RO_MTS(dev_attr_gnss_int_mths, "gnss-int",
    mts_attr_show_gpio_pin);



static struct attribute *mths_0_0_platform_attributes[] = {
    &dev_attr_vendor_id.attr,
    &dev_attr_product_id.attr,
    &dev_attr_device_id.attr,
    &dev_attr_uuid.attr,
    &dev_attr_hw_version.attr,
    &dev_attr_imei.attr,
    &dev_attr_wifi_mac_mths.attr,
    &dev_attr_has_radio.attr,

    &dev_attr_reset.attr,
    &dev_attr_reset_monitor.attr,
    &dev_attr_reset_monitor_intervals.attr,
    &dev_attr_wps_button_pin_mths.attr,
    &dev_attr_wps_button_monitor_mths.attr,
    &dev_attr_wps_button_monitor_intervals_mths.attr,
    &dev_attr_bt_button_pin_mths.attr,
    &dev_attr_bt_button_monitor_mths.attr,
    &dev_attr_bt_button_monitor_intervals_mths.attr,

    &dev_attr_radio_power_mths.attr,
    &dev_attr_radio_reset_mths.attr,

    &dev_attr_radio_reset_backoffs.attr,
    &dev_attr_radio_reset_backoff_index.attr,
    &dev_attr_radio_reset_backoff_seconds.attr,

    &dev_attr_wifi_bt_lpwkup_mths.attr,
    &dev_attr_wifi_bt_ulpwkup_mths.attr,
    &dev_attr_wifi_bt_reset_mths.attr,
    &dev_attr_wifi_bt_lpmode_mths.attr,
    &dev_attr_wifi_bt_int_mths.attr,
    &dev_attr_gnss_reset_mths.attr,
    &dev_attr_usbhub_reset_mths.attr,
    &dev_attr_gnss_int_mths.attr,

    &dev_attr_led_status.attr,
    &dev_attr_led_cd_gpio.attr,
    &dev_attr_led_sig1_gpio.attr,
    &dev_attr_led_sig2_gpio.attr,
    &dev_attr_led_sig3_gpio.attr,
    &dev_attr_led_wifi_gpio_mths.attr,
    &dev_attr_led_bt_gpio_mths.attr,

    &dev_attr_led_b_gpio.attr,
    &dev_attr_led_c_gpio.attr,
    &dev_attr_led_d_gpio.attr,
    &dev_attr_led_e_gpio.attr,
    &dev_attr_led_f_gpio_mths.attr,

    NULL,
};

static struct attribute_group mths_0_0_platform_attribute_group = {
    .attrs = mths_0_0_platform_attributes
};