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" |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 10 | #include "trace.h" |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 11 | #include "wwan_perst.h" |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 12 | #include <osmocom/core/timer.h> |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 13 | |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 14 | struct wwan_perst { |
Harald Welte | 514c6d1 | 2017-11-03 20:40:56 +0100 | [diff] [blame] | 15 | uint8_t idx; |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 16 | const Pin pin; |
| 17 | struct osmo_timer_list timer; |
| 18 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 19 | |
| 20 | #ifdef PIN_PERST1 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 21 | static struct wwan_perst perst1 = { |
Harald Welte | 514c6d1 | 2017-11-03 20:40:56 +0100 | [diff] [blame] | 22 | .idx = 0, |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 23 | .pin = PIN_PERST1, |
| 24 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | #ifdef PIN_PERST2 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 28 | static struct wwan_perst perst2 = { |
Harald Welte | 514c6d1 | 2017-11-03 20:40:56 +0100 | [diff] [blame] | 29 | .idx = 1, |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 30 | .pin = PIN_PERST2, |
| 31 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 32 | #endif |
| 33 | |
Harald Welte | 114e74d | 2017-05-09 06:45:27 +0200 | [diff] [blame] | 34 | static int initialized = 0; |
| 35 | |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 36 | static void perst_tmr_cb(void *data) |
| 37 | { |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 38 | struct wwan_perst *perst = data; |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 39 | /* release the (low-active) reset */ |
Harald Welte | 514c6d1 | 2017-11-03 20:40:56 +0100 | [diff] [blame] | 40 | TRACE_INFO("%u: De-asserting modem reset\r\n", perst->idx); |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 41 | PIO_Clear(&perst->pin); |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 44 | static struct wwan_perst *get_perst_for_modem(int modem_nr) |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 45 | { |
Harald Welte | 114e74d | 2017-05-09 06:45:27 +0200 | [diff] [blame] | 46 | if (!initialized) { |
| 47 | TRACE_ERROR("Somebody forgot to call wwan_perst_init()\r\n"); |
| 48 | wwan_perst_init(); |
| 49 | } |
| 50 | |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 51 | switch (modem_nr) { |
| 52 | #ifdef PIN_PERST1 |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 53 | case 0: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 54 | return &perst1; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 55 | #endif |
| 56 | #ifdef PIN_PERST2 |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 57 | case 1: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 58 | return &perst2; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 59 | #endif |
| 60 | default: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 61 | return NULL; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 62 | } |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int wwan_perst_do_reset_pulse(int modem_nr, unsigned int duration_ms) |
| 66 | { |
| 67 | struct wwan_perst *perst = get_perst_for_modem(modem_nr); |
| 68 | if (!perst) |
| 69 | return -1; |
| 70 | |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 71 | TRACE_INFO("%u: Asserting modem reset\r\n", modem_nr); |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 72 | PIO_Set(&perst->pin); |
| 73 | osmo_timer_schedule(&perst->timer, duration_ms/1000, (duration_ms%1000)*1000); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int wwan_perst_set(int modem_nr, int active) |
| 79 | { |
| 80 | struct wwan_perst *perst = get_perst_for_modem(modem_nr); |
| 81 | if (!perst) |
| 82 | return -1; |
| 83 | |
| 84 | osmo_timer_del(&perst->timer); |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 85 | if (active) { |
| 86 | TRACE_INFO("%u: Asserting modem reset\r\n", modem_nr); |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 87 | PIO_Set(&perst->pin); |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 88 | } else { |
| 89 | TRACE_INFO("%u: De-asserting modem reset\r\n", modem_nr); |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 90 | PIO_Clear(&perst->pin); |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 91 | } |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 92 | |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int wwan_perst_init(void) |
| 97 | { |
| 98 | int num_perst = 0; |
| 99 | #ifdef PIN_PERST1 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 100 | PIO_Configure(&perst1.pin, 1); |
| 101 | perst1.timer.cb = perst_tmr_cb; |
| 102 | perst1.timer.data = (void *) &perst1; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 103 | num_perst++; |
| 104 | #endif |
| 105 | |
| 106 | #ifdef PIN_PERST2 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 107 | PIO_Configure(&perst2.pin, 1); |
| 108 | perst2.timer.cb = perst_tmr_cb; |
| 109 | perst2.timer.data = (void *) &perst2; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 110 | num_perst++; |
| 111 | #endif |
Harald Welte | eb50c9f | 2017-11-03 20:50:47 +0100 | [diff] [blame] | 112 | initialized = 1; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 113 | return num_perst; |
| 114 | } |