Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 1 | /* Code to control the PERST lines of attached modems |
| 2 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 16 | */ |
| 17 | /* Depending on the board this is running on, it might be possible |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 18 | * for the controller to set the status of the PERST input line of |
| 19 | * the cellular modem. If the board supports this, it sets the |
| 20 | * PIN_PERST1 and/or PIN_PERST2 defines in its board.h file. |
| 21 | */ |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 22 | #include "board.h" |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 23 | #include "trace.h" |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 24 | #include "wwan_perst.h" |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 25 | #include <osmocom/core/timer.h> |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 26 | |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 27 | struct wwan_perst { |
Harald Welte | 514c6d1 | 2017-11-03 20:40:56 +0100 | [diff] [blame] | 28 | uint8_t idx; |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 29 | const Pin pin; |
| 30 | struct osmo_timer_list timer; |
| 31 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 32 | |
| 33 | #ifdef PIN_PERST1 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 34 | static struct wwan_perst perst1 = { |
Harald Welte | 514c6d1 | 2017-11-03 20:40:56 +0100 | [diff] [blame] | 35 | .idx = 0, |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 36 | .pin = PIN_PERST1, |
| 37 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | #ifdef PIN_PERST2 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 41 | static struct wwan_perst perst2 = { |
Harald Welte | 514c6d1 | 2017-11-03 20:40:56 +0100 | [diff] [blame] | 42 | .idx = 1, |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 43 | .pin = PIN_PERST2, |
| 44 | }; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 45 | #endif |
| 46 | |
Harald Welte | 114e74d | 2017-05-09 06:45:27 +0200 | [diff] [blame] | 47 | static int initialized = 0; |
| 48 | |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 49 | static void perst_tmr_cb(void *data) |
| 50 | { |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 51 | struct wwan_perst *perst = data; |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 52 | /* release the (low-active) reset */ |
Harald Welte | 514c6d1 | 2017-11-03 20:40:56 +0100 | [diff] [blame] | 53 | TRACE_INFO("%u: De-asserting modem reset\r\n", perst->idx); |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 54 | PIO_Clear(&perst->pin); |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 57 | static struct wwan_perst *get_perst_for_modem(int modem_nr) |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 58 | { |
Harald Welte | 114e74d | 2017-05-09 06:45:27 +0200 | [diff] [blame] | 59 | if (!initialized) { |
| 60 | TRACE_ERROR("Somebody forgot to call wwan_perst_init()\r\n"); |
| 61 | wwan_perst_init(); |
| 62 | } |
| 63 | |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 64 | switch (modem_nr) { |
| 65 | #ifdef PIN_PERST1 |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 66 | case 0: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 67 | return &perst1; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 68 | #endif |
| 69 | #ifdef PIN_PERST2 |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 70 | case 1: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 71 | return &perst2; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 72 | #endif |
| 73 | default: |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 74 | return NULL; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 75 | } |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int wwan_perst_do_reset_pulse(int modem_nr, unsigned int duration_ms) |
| 79 | { |
| 80 | struct wwan_perst *perst = get_perst_for_modem(modem_nr); |
| 81 | if (!perst) |
| 82 | return -1; |
| 83 | |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 84 | TRACE_INFO("%u: Asserting modem reset\r\n", modem_nr); |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 85 | PIO_Set(&perst->pin); |
| 86 | osmo_timer_schedule(&perst->timer, duration_ms/1000, (duration_ms%1000)*1000); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int wwan_perst_set(int modem_nr, int active) |
| 92 | { |
| 93 | struct wwan_perst *perst = get_perst_for_modem(modem_nr); |
| 94 | if (!perst) |
| 95 | return -1; |
| 96 | |
| 97 | osmo_timer_del(&perst->timer); |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 98 | if (active) { |
| 99 | TRACE_INFO("%u: Asserting modem reset\r\n", modem_nr); |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 100 | PIO_Set(&perst->pin); |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 101 | } else { |
| 102 | TRACE_INFO("%u: De-asserting modem reset\r\n", modem_nr); |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 103 | PIO_Clear(&perst->pin); |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 104 | } |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 105 | |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int wwan_perst_init(void) |
| 110 | { |
| 111 | int num_perst = 0; |
| 112 | #ifdef PIN_PERST1 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 113 | PIO_Configure(&perst1.pin, 1); |
| 114 | perst1.timer.cb = perst_tmr_cb; |
| 115 | perst1.timer.data = (void *) &perst1; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 116 | num_perst++; |
| 117 | #endif |
| 118 | |
| 119 | #ifdef PIN_PERST2 |
Harald Welte | cb093ce | 2017-05-08 22:28:41 +0200 | [diff] [blame] | 120 | PIO_Configure(&perst2.pin, 1); |
| 121 | perst2.timer.cb = perst_tmr_cb; |
| 122 | perst2.timer.data = (void *) &perst2; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 123 | num_perst++; |
| 124 | #endif |
Harald Welte | eb50c9f | 2017-11-03 20:50:47 +0100 | [diff] [blame] | 125 | initialized = 1; |
Harald Welte | 31f817c | 2017-02-03 22:21:32 +0100 | [diff] [blame] | 126 | return num_perst; |
| 127 | } |