Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 1 | /* Code to control the PERST lines of attached modems |
| 2 | * |
| 3 | * Depending on the board this is running on, it might be possible |
| 4 | * for the controller to set the status of the PERST input line of |
| 5 | * the cellular modem. If the board supports this, it sets the |
| 6 | * PIN_PERST1 and/or PIN_PERST2 defines in its board.h file. |
| 7 | */ |
| 8 | |
| 9 | #include "board.h" |
| 10 | #include "wwan_perst.h" |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 11 | #include "osmocom/core/timer.h" |
| 12 | |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 13 | struct wwan_perst { |
| 14 | const Pin pin; |
| 15 | struct osmo_timer_list timer; |
| 16 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 17 | |
| 18 | #ifdef PIN_PERST1 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 19 | static struct wwan_perst perst1 = { |
| 20 | .pin = PIN_PERST1, |
| 21 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | #ifdef PIN_PERST2 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 25 | static struct wwan_perst perst2 = { |
| 26 | .pin = PIN_PERST2, |
| 27 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 28 | #endif |
| 29 | |
Harald Welte | 114e74d | 2017-05-09 06:45:27 +0200 | [diff] [blame^] | 30 | static int initialized = 0; |
| 31 | |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 32 | static void perst_tmr_cb(void *data) |
| 33 | { |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 34 | struct wwan_perst *perst = data; |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 35 | /* release the (low-active) reset */ |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 36 | PIO_Clear(&perst->pin); |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 39 | static struct wwan_perst *get_perst_for_modem(int modem_nr) |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 40 | { |
Harald Welte | 114e74d | 2017-05-09 06:45:27 +0200 | [diff] [blame^] | 41 | if (!initialized) { |
| 42 | TRACE_ERROR("Somebody forgot to call wwan_perst_init()\r\n"); |
| 43 | wwan_perst_init(); |
| 44 | } |
| 45 | |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 46 | switch (modem_nr) { |
| 47 | #ifdef PIN_PERST1 |
| 48 | case 1: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 49 | return &perst1; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 50 | #endif |
| 51 | #ifdef PIN_PERST2 |
| 52 | case 2: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 53 | return &perst2; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 54 | #endif |
| 55 | default: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 56 | return NULL; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 57 | } |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int wwan_perst_do_reset_pulse(int modem_nr, unsigned int duration_ms) |
| 61 | { |
| 62 | struct wwan_perst *perst = get_perst_for_modem(modem_nr); |
| 63 | if (!perst) |
| 64 | return -1; |
| 65 | |
| 66 | PIO_Set(&perst->pin); |
| 67 | osmo_timer_schedule(&perst->timer, duration_ms/1000, (duration_ms%1000)*1000); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int wwan_perst_set(int modem_nr, int active) |
| 73 | { |
| 74 | struct wwan_perst *perst = get_perst_for_modem(modem_nr); |
| 75 | if (!perst) |
| 76 | return -1; |
| 77 | |
| 78 | osmo_timer_del(&perst->timer); |
| 79 | if (active) |
| 80 | PIO_Set(&perst->pin); |
| 81 | else |
| 82 | PIO_Clear(&perst->pin); |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 83 | |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int wwan_perst_init(void) |
| 88 | { |
| 89 | int num_perst = 0; |
| 90 | #ifdef PIN_PERST1 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 91 | PIO_Configure(&perst1.pin, 1); |
| 92 | perst1.timer.cb = perst_tmr_cb; |
| 93 | perst1.timer.data = (void *) &perst1; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 94 | num_perst++; |
| 95 | #endif |
| 96 | |
| 97 | #ifdef PIN_PERST2 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 98 | PIO_Configure(&perst2.pin, 1); |
| 99 | perst2.timer.cb = perst_tmr_cb; |
| 100 | perst2.timer.data = (void *) &perst2; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 101 | num_perst++; |
| 102 | #endif |
| 103 | return num_perst; |
| 104 | } |