Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Arche Platform driver to enable Unipro link. |
| 3 | * |
| 4 | * Copyright 2014-2015 Google Inc. |
| 5 | * Copyright 2014-2015 Linaro Ltd. |
| 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 10 | #include <linux/clk.h> |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 11 | #include <linux/delay.h> |
Viresh Kumar | 3b858df | 2016-01-11 11:29:17 +0530 | [diff] [blame] | 12 | #include <linux/gpio.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 15 | #include <linux/of_gpio.h> |
Viresh Kumar | 3b858df | 2016-01-11 11:29:17 +0530 | [diff] [blame] | 16 | #include <linux/of_platform.h> |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 17 | #include <linux/pinctrl/consumer.h> |
Viresh Kumar | 3b858df | 2016-01-11 11:29:17 +0530 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/pm.h> |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/irq.h> |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 22 | #include <linux/suspend.h> |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 23 | #include <linux/time.h> |
Greg Kroah-Hartman | 1e5dd1f | 2015-12-30 13:38:33 -0800 | [diff] [blame] | 24 | #include "arche_platform.h" |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 25 | #include "greybus.h" |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 26 | |
Vaibhav Hiremath | ad4d3f9 | 2016-02-13 02:04:20 +0530 | [diff] [blame] | 27 | #include <linux/usb/usb3613.h> |
| 28 | |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 29 | #define WD_COLDBOOT_PULSE_WIDTH_MS 30 |
| 30 | |
Vaibhav Hiremath | 685353c | 2016-02-25 04:37:35 +0530 | [diff] [blame] | 31 | enum svc_wakedetect_state { |
| 32 | WD_STATE_IDLE, /* Default state = pulled high/low */ |
| 33 | WD_STATE_BOOT_INIT, /* WD = falling edge (low) */ |
| 34 | WD_STATE_COLDBOOT_TRIG, /* WD = rising edge (high), > 30msec */ |
| 35 | WD_STATE_STANDBYBOOT_TRIG, /* As of now not used ?? */ |
| 36 | WD_STATE_COLDBOOT_START, /* Cold boot process started */ |
| 37 | WD_STATE_STANDBYBOOT_START, /* Not used */ |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 38 | WD_STATE_TIMESYNC, |
Vaibhav Hiremath | 685353c | 2016-02-25 04:37:35 +0530 | [diff] [blame] | 39 | }; |
| 40 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 41 | struct arche_platform_drvdata { |
| 42 | /* Control GPIO signals to and from AP <=> SVC */ |
| 43 | int svc_reset_gpio; |
| 44 | bool is_reset_act_hi; |
| 45 | int svc_sysboot_gpio; |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 46 | int wake_detect_gpio; /* bi-dir,maps to WAKE_MOD & WAKE_FRAME signals */ |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 47 | |
Vaibhav Hiremath | e74d04a | 2016-02-13 02:04:05 +0530 | [diff] [blame] | 48 | enum arche_platform_state state; |
| 49 | |
David Lin | 7fe9301 | 2016-03-07 21:52:54 -0800 | [diff] [blame] | 50 | int svc_refclk_req; |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 51 | struct clk *svc_ref_clk; |
| 52 | |
| 53 | struct pinctrl *pinctrl; |
| 54 | struct pinctrl_state *pin_default; |
| 55 | |
| 56 | int num_apbs; |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 57 | |
Vaibhav Hiremath | 685353c | 2016-02-25 04:37:35 +0530 | [diff] [blame] | 58 | enum svc_wakedetect_state wake_detect_state; |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 59 | int wake_detect_irq; |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 60 | spinlock_t wake_lock; /* Protect wake_detect_state */ |
| 61 | struct mutex platform_state_mutex; /* Protect state */ |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 62 | wait_queue_head_t wq; /* WQ for arche_pdata->state */ |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 63 | unsigned long wake_detect_start; |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 64 | struct notifier_block pm_notifier; |
Vaibhav Hiremath | 685353c | 2016-02-25 04:37:35 +0530 | [diff] [blame] | 65 | |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 66 | struct device *dev; |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 67 | struct gb_timesync_svc *timesync_svc_pdata; |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 68 | }; |
| 69 | |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 70 | static int arche_apb_bootret_assert(struct device *dev, void *data) |
| 71 | { |
| 72 | apb_bootret_assert(dev); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int arche_apb_bootret_deassert(struct device *dev, void *data) |
| 77 | { |
| 78 | apb_bootret_deassert(dev); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* Requires calling context to hold arche_pdata->platform_state_mutex */ |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 83 | static void arche_platform_set_state(struct arche_platform_drvdata *arche_pdata, |
| 84 | enum arche_platform_state state) |
| 85 | { |
| 86 | arche_pdata->state = state; |
| 87 | } |
| 88 | |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 89 | /* |
| 90 | * arche_platform_change_state: Change the operational state |
| 91 | * |
| 92 | * This exported function allows external drivers to change the state |
| 93 | * of the arche-platform driver. |
| 94 | * Note that this function only supports transitions between two states |
| 95 | * with limited functionality. |
| 96 | * |
| 97 | * - ARCHE_PLATFORM_STATE_TIME_SYNC: |
| 98 | * Once set, allows timesync operations between SVC <=> AP and makes |
| 99 | * sure that arche-platform driver ignores any subsequent events/pulses |
| 100 | * from SVC over wake/detect. |
| 101 | * |
| 102 | * - ARCHE_PLATFORM_STATE_ACTIVE: |
| 103 | * Puts back driver to active state, where any pulse from SVC on wake/detect |
| 104 | * line would trigger either cold/standby boot. |
| 105 | * Note: Transition request from this function does not trigger cold/standby |
| 106 | * boot. It just puts back driver book keeping variable back to ACTIVE |
| 107 | * state and restores the interrupt. |
| 108 | * |
| 109 | * Returns -ENODEV if device not found, -EAGAIN if the driver cannot currently |
| 110 | * satisfy the requested state-transition or -EINVAL for all other |
| 111 | * state-transition requests. |
| 112 | */ |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 113 | int arche_platform_change_state(enum arche_platform_state state, |
| 114 | struct gb_timesync_svc *timesync_svc_pdata) |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 115 | { |
| 116 | struct arche_platform_drvdata *arche_pdata; |
| 117 | struct platform_device *pdev; |
| 118 | struct device_node *np; |
| 119 | int ret = -EAGAIN; |
| 120 | unsigned long flags; |
| 121 | |
| 122 | np = of_find_compatible_node(NULL, NULL, "google,arche-platform"); |
| 123 | if (!np) { |
| 124 | pr_err("google,arche-platform device node not found\n"); |
| 125 | return -ENODEV; |
| 126 | } |
| 127 | |
| 128 | pdev = of_find_device_by_node(np); |
| 129 | if (!pdev) { |
| 130 | pr_err("arche-platform device not found\n"); |
| 131 | return -ENODEV; |
| 132 | } |
| 133 | |
| 134 | arche_pdata = platform_get_drvdata(pdev); |
| 135 | |
| 136 | mutex_lock(&arche_pdata->platform_state_mutex); |
| 137 | spin_lock_irqsave(&arche_pdata->wake_lock, flags); |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 138 | |
| 139 | if (arche_pdata->state == state) { |
| 140 | ret = 0; |
| 141 | goto exit; |
| 142 | } |
| 143 | |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 144 | switch (state) { |
| 145 | case ARCHE_PLATFORM_STATE_TIME_SYNC: |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 146 | if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) { |
| 147 | ret = -EINVAL; |
| 148 | goto exit; |
| 149 | } |
| 150 | if (arche_pdata->wake_detect_state != WD_STATE_IDLE) { |
| 151 | dev_err(arche_pdata->dev, |
| 152 | "driver busy with wake/detect line ops\n"); |
| 153 | goto exit; |
| 154 | } |
| 155 | device_for_each_child(arche_pdata->dev, NULL, |
| 156 | arche_apb_bootret_assert); |
| 157 | arche_pdata->wake_detect_state = WD_STATE_TIMESYNC; |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 158 | break; |
| 159 | case ARCHE_PLATFORM_STATE_ACTIVE: |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 160 | if (arche_pdata->state != ARCHE_PLATFORM_STATE_TIME_SYNC) { |
| 161 | ret = -EINVAL; |
| 162 | goto exit; |
| 163 | } |
| 164 | device_for_each_child(arche_pdata->dev, NULL, |
| 165 | arche_apb_bootret_deassert); |
| 166 | arche_pdata->wake_detect_state = WD_STATE_IDLE; |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 167 | break; |
| 168 | case ARCHE_PLATFORM_STATE_OFF: |
| 169 | case ARCHE_PLATFORM_STATE_STANDBY: |
| 170 | case ARCHE_PLATFORM_STATE_FW_FLASHING: |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 171 | dev_err(arche_pdata->dev, "busy, request to retry later\n"); |
| 172 | goto exit; |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 173 | default: |
| 174 | ret = -EINVAL; |
| 175 | dev_err(arche_pdata->dev, |
| 176 | "invalid state transition request\n"); |
| 177 | goto exit; |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 178 | } |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 179 | arche_pdata->timesync_svc_pdata = timesync_svc_pdata; |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 180 | arche_platform_set_state(arche_pdata, state); |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 181 | if (state == ARCHE_PLATFORM_STATE_ACTIVE) |
| 182 | wake_up(&arche_pdata->wq); |
| 183 | |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 184 | ret = 0; |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 185 | exit: |
| 186 | spin_unlock_irqrestore(&arche_pdata->wake_lock, flags); |
| 187 | mutex_unlock(&arche_pdata->platform_state_mutex); |
| 188 | of_node_put(np); |
| 189 | return ret; |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(arche_platform_change_state); |
| 192 | |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 193 | /* Requires arche_pdata->wake_lock is held by calling context */ |
| 194 | static void arche_platform_set_wake_detect_state( |
| 195 | struct arche_platform_drvdata *arche_pdata, |
| 196 | enum svc_wakedetect_state state) |
| 197 | { |
| 198 | arche_pdata->wake_detect_state = state; |
| 199 | } |
| 200 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 201 | static inline void svc_reset_onoff(unsigned int gpio, bool onoff) |
| 202 | { |
| 203 | gpio_set_value(gpio, onoff); |
| 204 | } |
| 205 | |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 206 | static int apb_cold_boot(struct device *dev, void *data) |
| 207 | { |
| 208 | int ret; |
| 209 | |
| 210 | ret = apb_ctrl_coldboot(dev); |
| 211 | if (ret) |
| 212 | dev_warn(dev, "failed to coldboot\n"); |
| 213 | |
| 214 | /*Child nodes are independent, so do not exit coldboot operation */ |
| 215 | return 0; |
| 216 | } |
| 217 | |
Vaibhav Hiremath | fd60ac5 | 2016-02-13 02:04:17 +0530 | [diff] [blame] | 218 | static int apb_poweroff(struct device *dev, void *data) |
| 219 | { |
| 220 | apb_ctrl_poweroff(dev); |
| 221 | |
Vaibhav Hiremath | e915ce4 | 2016-02-25 16:45:45 +0530 | [diff] [blame] | 222 | /* Enable HUB3613 into HUB mode. */ |
| 223 | if (usb3613_hub_mode_ctrl(false)) |
| 224 | dev_warn(dev, "failed to control hub device\n"); |
| 225 | |
Vaibhav Hiremath | fd60ac5 | 2016-02-13 02:04:17 +0530 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
Vaibhav Hiremath | 7ba535e | 2016-05-17 22:39:47 +0530 | [diff] [blame] | 229 | static void arche_platform_wd_irq_en(struct arche_platform_drvdata *arche_pdata) |
Vaibhav Hiremath | 16fe18c | 2016-02-25 04:37:37 +0530 | [diff] [blame] | 230 | { |
Vaibhav Hiremath | 16fe18c | 2016-02-25 04:37:37 +0530 | [diff] [blame] | 231 | /* Enable interrupt here, to read event back from SVC */ |
| 232 | gpio_direction_input(arche_pdata->wake_detect_gpio); |
| 233 | enable_irq(arche_pdata->wake_detect_irq); |
| 234 | } |
| 235 | |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 236 | static irqreturn_t arche_platform_wd_irq_thread(int irq, void *devid) |
| 237 | { |
| 238 | struct arche_platform_drvdata *arche_pdata = devid; |
| 239 | unsigned long flags; |
| 240 | |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 241 | spin_lock_irqsave(&arche_pdata->wake_lock, flags); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 242 | if (arche_pdata->wake_detect_state != WD_STATE_COLDBOOT_TRIG) { |
| 243 | /* Something is wrong */ |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 244 | spin_unlock_irqrestore(&arche_pdata->wake_lock, flags); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 245 | return IRQ_HANDLED; |
| 246 | } |
| 247 | |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 248 | arche_platform_set_wake_detect_state(arche_pdata, |
| 249 | WD_STATE_COLDBOOT_START); |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 250 | spin_unlock_irqrestore(&arche_pdata->wake_lock, flags); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 251 | |
Vaibhav Hiremath | ff788de | 2016-02-25 16:45:44 +0530 | [diff] [blame] | 252 | /* It should complete power cycle, so first make sure it is poweroff */ |
| 253 | device_for_each_child(arche_pdata->dev, NULL, apb_poweroff); |
Vaibhav Hiremath | e915ce4 | 2016-02-25 16:45:45 +0530 | [diff] [blame] | 254 | |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 255 | /* Bring APB out of reset: cold boot sequence */ |
| 256 | device_for_each_child(arche_pdata->dev, NULL, apb_cold_boot); |
| 257 | |
Vaibhav Hiremath | a7ddda1 | 2016-04-12 17:18:47 +0530 | [diff] [blame] | 258 | /* Enable HUB3613 into HUB mode. */ |
| 259 | if (usb3613_hub_mode_ctrl(true)) |
| 260 | dev_warn(arche_pdata->dev, "failed to control hub device\n"); |
| 261 | |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 262 | spin_lock_irqsave(&arche_pdata->wake_lock, flags); |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 263 | arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE); |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 264 | spin_unlock_irqrestore(&arche_pdata->wake_lock, flags); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 265 | |
| 266 | return IRQ_HANDLED; |
| 267 | } |
| 268 | |
| 269 | static irqreturn_t arche_platform_wd_irq(int irq, void *devid) |
| 270 | { |
| 271 | struct arche_platform_drvdata *arche_pdata = devid; |
| 272 | unsigned long flags; |
| 273 | |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 274 | spin_lock_irqsave(&arche_pdata->wake_lock, flags); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 275 | |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 276 | if (arche_pdata->wake_detect_state == WD_STATE_TIMESYNC) { |
| 277 | gb_timesync_irq(arche_pdata->timesync_svc_pdata); |
| 278 | goto exit; |
| 279 | } |
| 280 | |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 281 | if (gpio_get_value(arche_pdata->wake_detect_gpio)) { |
| 282 | /* wake/detect rising */ |
| 283 | |
| 284 | /* |
| 285 | * If wake/detect line goes high after low, within less than |
| 286 | * 30msec, then standby boot sequence is initiated, which is not |
| 287 | * supported/implemented as of now. So ignore it. |
| 288 | */ |
| 289 | if (arche_pdata->wake_detect_state == WD_STATE_BOOT_INIT) { |
| 290 | if (time_before(jiffies, |
| 291 | arche_pdata->wake_detect_start + |
| 292 | msecs_to_jiffies(WD_COLDBOOT_PULSE_WIDTH_MS))) { |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 293 | arche_platform_set_wake_detect_state(arche_pdata, |
| 294 | WD_STATE_IDLE); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 295 | } else { |
| 296 | /* Check we are not in middle of irq thread already */ |
| 297 | if (arche_pdata->wake_detect_state != |
| 298 | WD_STATE_COLDBOOT_START) { |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 299 | arche_platform_set_wake_detect_state(arche_pdata, |
| 300 | WD_STATE_COLDBOOT_TRIG); |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 301 | spin_unlock_irqrestore( |
| 302 | &arche_pdata->wake_lock, |
| 303 | flags); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 304 | return IRQ_WAKE_THREAD; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } else { |
| 309 | /* wake/detect falling */ |
| 310 | if (arche_pdata->wake_detect_state == WD_STATE_IDLE) { |
| 311 | arche_pdata->wake_detect_start = jiffies; |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 312 | /* |
| 313 | * In the begining, when wake/detect goes low (first time), we assume |
| 314 | * it is meant for coldboot and set the flag. If wake/detect line stays low |
| 315 | * beyond 30msec, then it is coldboot else fallback to standby boot. |
| 316 | */ |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 317 | arche_platform_set_wake_detect_state(arche_pdata, |
| 318 | WD_STATE_BOOT_INIT); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 322 | exit: |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 323 | spin_unlock_irqrestore(&arche_pdata->wake_lock, flags); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 324 | |
| 325 | return IRQ_HANDLED; |
| 326 | } |
| 327 | |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 328 | /* |
| 329 | * Requires arche_pdata->platform_state_mutex to be held |
| 330 | */ |
Vaibhav Hiremath | 758ca99 | 2016-02-13 02:04:03 +0530 | [diff] [blame] | 331 | static int arche_platform_coldboot_seq(struct arche_platform_drvdata *arche_pdata) |
| 332 | { |
| 333 | int ret; |
| 334 | |
Vaibhav Hiremath | 599159b | 2016-02-22 17:27:24 +0530 | [diff] [blame] | 335 | if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE) |
| 336 | return 0; |
| 337 | |
Vaibhav Hiremath | 758ca99 | 2016-02-13 02:04:03 +0530 | [diff] [blame] | 338 | dev_info(arche_pdata->dev, "Booting from cold boot state\n"); |
| 339 | |
| 340 | svc_reset_onoff(arche_pdata->svc_reset_gpio, |
| 341 | arche_pdata->is_reset_act_hi); |
| 342 | |
| 343 | gpio_set_value(arche_pdata->svc_sysboot_gpio, 0); |
| 344 | usleep_range(100, 200); |
| 345 | |
| 346 | ret = clk_prepare_enable(arche_pdata->svc_ref_clk); |
| 347 | if (ret) { |
| 348 | dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n", |
| 349 | ret); |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | /* bring SVC out of reset */ |
| 354 | svc_reset_onoff(arche_pdata->svc_reset_gpio, |
| 355 | !arche_pdata->is_reset_act_hi); |
| 356 | |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 357 | arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_ACTIVE); |
Vaibhav Hiremath | e74d04a | 2016-02-13 02:04:05 +0530 | [diff] [blame] | 358 | |
Vaibhav Hiremath | 758ca99 | 2016-02-13 02:04:03 +0530 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 362 | /* |
| 363 | * Requires arche_pdata->platform_state_mutex to be held |
| 364 | */ |
Vaibhav Hiremath | c61a8b4 | 2016-05-24 18:32:02 +0530 | [diff] [blame] | 365 | static int arche_platform_fw_flashing_seq(struct arche_platform_drvdata *arche_pdata) |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 366 | { |
Vaibhav Hiremath | 7a867d1 | 2016-05-24 18:32:04 +0530 | [diff] [blame] | 367 | int ret; |
| 368 | |
Vaibhav Hiremath | 599159b | 2016-02-22 17:27:24 +0530 | [diff] [blame] | 369 | if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING) |
Vaibhav Hiremath | c61a8b4 | 2016-05-24 18:32:02 +0530 | [diff] [blame] | 370 | return 0; |
Vaibhav Hiremath | 599159b | 2016-02-22 17:27:24 +0530 | [diff] [blame] | 371 | |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 372 | dev_info(arche_pdata->dev, "Switching to FW flashing state\n"); |
| 373 | |
| 374 | svc_reset_onoff(arche_pdata->svc_reset_gpio, |
| 375 | arche_pdata->is_reset_act_hi); |
| 376 | |
| 377 | gpio_set_value(arche_pdata->svc_sysboot_gpio, 1); |
| 378 | |
| 379 | usleep_range(100, 200); |
Vaibhav Hiremath | 7a867d1 | 2016-05-24 18:32:04 +0530 | [diff] [blame] | 380 | |
| 381 | ret = clk_prepare_enable(arche_pdata->svc_ref_clk); |
| 382 | if (ret) { |
| 383 | dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n", |
| 384 | ret); |
| 385 | return ret; |
| 386 | } |
| 387 | |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 388 | svc_reset_onoff(arche_pdata->svc_reset_gpio, |
| 389 | !arche_pdata->is_reset_act_hi); |
| 390 | |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 391 | arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_FW_FLASHING); |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 392 | |
Vaibhav Hiremath | c61a8b4 | 2016-05-24 18:32:02 +0530 | [diff] [blame] | 393 | return 0; |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 394 | } |
| 395 | |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 396 | /* |
| 397 | * Requires arche_pdata->platform_state_mutex to be held |
| 398 | */ |
Vaibhav Hiremath | 5993e2b | 2016-02-13 02:04:04 +0530 | [diff] [blame] | 399 | static void arche_platform_poweroff_seq(struct arche_platform_drvdata *arche_pdata) |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 400 | { |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 401 | unsigned long flags; |
| 402 | |
Vaibhav Hiremath | 599159b | 2016-02-22 17:27:24 +0530 | [diff] [blame] | 403 | if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF) |
| 404 | return; |
| 405 | |
Vaibhav Hiremath | 25847ee | 2016-02-22 17:27:25 +0530 | [diff] [blame] | 406 | /* If in fw_flashing mode, then no need to repeate things again */ |
| 407 | if (arche_pdata->state != ARCHE_PLATFORM_STATE_FW_FLASHING) { |
Vaibhav Hiremath | d2320b2 | 2016-02-25 04:37:39 +0530 | [diff] [blame] | 408 | disable_irq(arche_pdata->wake_detect_irq); |
Vaibhav Hiremath | 16fd976 | 2016-05-17 22:39:46 +0530 | [diff] [blame] | 409 | |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 410 | spin_lock_irqsave(&arche_pdata->wake_lock, flags); |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 411 | arche_platform_set_wake_detect_state(arche_pdata, |
| 412 | WD_STATE_IDLE); |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 413 | spin_unlock_irqrestore(&arche_pdata->wake_lock, flags); |
Vaibhav Hiremath | 25847ee | 2016-02-22 17:27:25 +0530 | [diff] [blame] | 414 | } |
| 415 | |
Vaibhav Hiremath | 7a867d1 | 2016-05-24 18:32:04 +0530 | [diff] [blame] | 416 | clk_disable_unprepare(arche_pdata->svc_ref_clk); |
| 417 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 418 | /* As part of exit, put APB back in reset state */ |
Viresh Kumar | 140741e | 2016-01-11 11:29:06 +0530 | [diff] [blame] | 419 | svc_reset_onoff(arche_pdata->svc_reset_gpio, |
| 420 | arche_pdata->is_reset_act_hi); |
Vaibhav Hiremath | e74d04a | 2016-02-13 02:04:05 +0530 | [diff] [blame] | 421 | |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 422 | arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF); |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 423 | } |
| 424 | |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 425 | static ssize_t state_store(struct device *dev, |
| 426 | struct device_attribute *attr, const char *buf, size_t count) |
| 427 | { |
| 428 | struct platform_device *pdev = to_platform_device(dev); |
| 429 | struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev); |
| 430 | int ret = 0; |
| 431 | |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 432 | retry: |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 433 | mutex_lock(&arche_pdata->platform_state_mutex); |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 434 | if (arche_pdata->state == ARCHE_PLATFORM_STATE_TIME_SYNC) { |
| 435 | mutex_unlock(&arche_pdata->platform_state_mutex); |
| 436 | ret = wait_event_interruptible( |
| 437 | arche_pdata->wq, |
| 438 | arche_pdata->state != ARCHE_PLATFORM_STATE_TIME_SYNC); |
| 439 | if (ret) |
| 440 | return ret; |
| 441 | goto retry; |
| 442 | } |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 443 | |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 444 | if (sysfs_streq(buf, "off")) { |
| 445 | if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF) |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 446 | goto exit; |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 447 | |
Vaibhav Hiremath | fd60ac5 | 2016-02-13 02:04:17 +0530 | [diff] [blame] | 448 | /* If SVC goes down, bring down APB's as well */ |
| 449 | device_for_each_child(arche_pdata->dev, NULL, apb_poweroff); |
| 450 | |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 451 | arche_platform_poweroff_seq(arche_pdata); |
Vaibhav Hiremath | ad4d3f9 | 2016-02-13 02:04:20 +0530 | [diff] [blame] | 452 | |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 453 | } else if (sysfs_streq(buf, "active")) { |
| 454 | if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE) |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 455 | goto exit; |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 456 | |
Vaibhav Hiremath | 0b1283e | 2016-05-24 18:32:03 +0530 | [diff] [blame] | 457 | /* First we want to make sure we power off everything |
| 458 | * and then activate back again */ |
| 459 | device_for_each_child(arche_pdata->dev, NULL, apb_poweroff); |
| 460 | arche_platform_poweroff_seq(arche_pdata); |
| 461 | |
Vaibhav Hiremath | 7ba535e | 2016-05-17 22:39:47 +0530 | [diff] [blame] | 462 | arche_platform_wd_irq_en(arche_pdata); |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 463 | ret = arche_platform_coldboot_seq(arche_pdata); |
Vaibhav Hiremath | 0b1283e | 2016-05-24 18:32:03 +0530 | [diff] [blame] | 464 | if (ret) |
| 465 | goto exit; |
Vaibhav Hiremath | 16fe18c | 2016-02-25 04:37:37 +0530 | [diff] [blame] | 466 | |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 467 | } else if (sysfs_streq(buf, "standby")) { |
| 468 | if (arche_pdata->state == ARCHE_PLATFORM_STATE_STANDBY) |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 469 | goto exit; |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 470 | |
| 471 | dev_warn(arche_pdata->dev, "standby state not supported\n"); |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 472 | } else if (sysfs_streq(buf, "fw_flashing")) { |
| 473 | if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING) |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 474 | goto exit; |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 475 | |
Vaibhav Hiremath | 921dbe5 | 2016-07-28 13:47:37 +0530 | [diff] [blame] | 476 | /* |
| 477 | * Here we only control SVC. |
| 478 | * |
| 479 | * In case of FW_FLASHING mode we do not want to control |
| 480 | * APBs, as in case of V2, SPI bus is shared between both |
| 481 | * the APBs. So let user chose which APB he wants to flash. |
| 482 | */ |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 483 | arche_platform_poweroff_seq(arche_pdata); |
Vaibhav Hiremath | fd60ac5 | 2016-02-13 02:04:17 +0530 | [diff] [blame] | 484 | |
Vaibhav Hiremath | c61a8b4 | 2016-05-24 18:32:02 +0530 | [diff] [blame] | 485 | ret = arche_platform_fw_flashing_seq(arche_pdata); |
| 486 | if (ret) |
| 487 | goto exit; |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 488 | } else { |
| 489 | dev_err(arche_pdata->dev, "unknown state\n"); |
| 490 | ret = -EINVAL; |
| 491 | } |
| 492 | |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 493 | exit: |
| 494 | mutex_unlock(&arche_pdata->platform_state_mutex); |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 495 | return ret ? ret : count; |
| 496 | } |
| 497 | |
| 498 | static ssize_t state_show(struct device *dev, |
| 499 | struct device_attribute *attr, char *buf) |
| 500 | { |
| 501 | struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev); |
| 502 | |
| 503 | switch (arche_pdata->state) { |
| 504 | case ARCHE_PLATFORM_STATE_OFF: |
| 505 | return sprintf(buf, "off\n"); |
| 506 | case ARCHE_PLATFORM_STATE_ACTIVE: |
| 507 | return sprintf(buf, "active\n"); |
| 508 | case ARCHE_PLATFORM_STATE_STANDBY: |
| 509 | return sprintf(buf, "standby\n"); |
Vaibhav Hiremath | 7691fed | 2016-02-13 02:04:08 +0530 | [diff] [blame] | 510 | case ARCHE_PLATFORM_STATE_FW_FLASHING: |
| 511 | return sprintf(buf, "fw_flashing\n"); |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 512 | case ARCHE_PLATFORM_STATE_TIME_SYNC: |
| 513 | return sprintf(buf, "time_sync\n"); |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 514 | default: |
| 515 | return sprintf(buf, "unknown state\n"); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static DEVICE_ATTR_RW(state); |
| 520 | |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 521 | static int arche_platform_pm_notifier(struct notifier_block *notifier, |
| 522 | unsigned long pm_event, void *unused) |
| 523 | { |
| 524 | struct arche_platform_drvdata *arche_pdata = |
| 525 | container_of(notifier, struct arche_platform_drvdata, |
| 526 | pm_notifier); |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 527 | int ret = NOTIFY_DONE; |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 528 | |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 529 | mutex_lock(&arche_pdata->platform_state_mutex); |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 530 | switch (pm_event) { |
| 531 | case PM_SUSPEND_PREPARE: |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 532 | if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) { |
| 533 | ret = NOTIFY_STOP; |
| 534 | break; |
| 535 | } |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 536 | device_for_each_child(arche_pdata->dev, NULL, apb_poweroff); |
| 537 | arche_platform_poweroff_seq(arche_pdata); |
| 538 | break; |
| 539 | case PM_POST_SUSPEND: |
Vaibhav Hiremath | 8ef0b53 | 2016-08-03 17:15:38 +0530 | [diff] [blame] | 540 | if (arche_pdata->state != ARCHE_PLATFORM_STATE_OFF) |
| 541 | break; |
| 542 | |
Vaibhav Hiremath | 7ba535e | 2016-05-17 22:39:47 +0530 | [diff] [blame] | 543 | arche_platform_wd_irq_en(arche_pdata); |
Vaibhav Hiremath | 16fd976 | 2016-05-17 22:39:46 +0530 | [diff] [blame] | 544 | arche_platform_coldboot_seq(arche_pdata); |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 545 | break; |
| 546 | default: |
| 547 | break; |
| 548 | } |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 549 | mutex_unlock(&arche_pdata->platform_state_mutex); |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 550 | |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 551 | return ret; |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 552 | } |
| 553 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 554 | static int arche_platform_probe(struct platform_device *pdev) |
| 555 | { |
| 556 | struct arche_platform_drvdata *arche_pdata; |
| 557 | struct device *dev = &pdev->dev; |
| 558 | struct device_node *np = dev->of_node; |
| 559 | int ret; |
| 560 | |
| 561 | arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata), GFP_KERNEL); |
| 562 | if (!arche_pdata) |
| 563 | return -ENOMEM; |
| 564 | |
| 565 | /* setup svc reset gpio */ |
| 566 | arche_pdata->is_reset_act_hi = of_property_read_bool(np, |
| 567 | "svc,reset-active-high"); |
| 568 | arche_pdata->svc_reset_gpio = of_get_named_gpio(np, "svc,reset-gpio", 0); |
| 569 | if (arche_pdata->svc_reset_gpio < 0) { |
| 570 | dev_err(dev, "failed to get reset-gpio\n"); |
Viresh Kumar | f1f251b | 2016-01-11 11:29:07 +0530 | [diff] [blame] | 571 | return arche_pdata->svc_reset_gpio; |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 572 | } |
| 573 | ret = devm_gpio_request(dev, arche_pdata->svc_reset_gpio, "svc-reset"); |
| 574 | if (ret) { |
| 575 | dev_err(dev, "failed to request svc-reset gpio:%d\n", ret); |
| 576 | return ret; |
| 577 | } |
| 578 | ret = gpio_direction_output(arche_pdata->svc_reset_gpio, |
| 579 | arche_pdata->is_reset_act_hi); |
| 580 | if (ret) { |
| 581 | dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret); |
| 582 | return ret; |
| 583 | } |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 584 | arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF); |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 585 | |
| 586 | arche_pdata->svc_sysboot_gpio = of_get_named_gpio(np, |
| 587 | "svc,sysboot-gpio", 0); |
| 588 | if (arche_pdata->svc_sysboot_gpio < 0) { |
| 589 | dev_err(dev, "failed to get sysboot gpio\n"); |
Viresh Kumar | f1f251b | 2016-01-11 11:29:07 +0530 | [diff] [blame] | 590 | return arche_pdata->svc_sysboot_gpio; |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 591 | } |
| 592 | ret = devm_gpio_request(dev, arche_pdata->svc_sysboot_gpio, "sysboot0"); |
| 593 | if (ret) { |
| 594 | dev_err(dev, "failed to request sysboot0 gpio:%d\n", ret); |
| 595 | return ret; |
| 596 | } |
| 597 | ret = gpio_direction_output(arche_pdata->svc_sysboot_gpio, 0); |
| 598 | if (ret) { |
| 599 | dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret); |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | /* setup the clock request gpio first */ |
| 604 | arche_pdata->svc_refclk_req = of_get_named_gpio(np, |
| 605 | "svc,refclk-req-gpio", 0); |
| 606 | if (arche_pdata->svc_refclk_req < 0) { |
| 607 | dev_err(dev, "failed to get svc clock-req gpio\n"); |
Viresh Kumar | f1f251b | 2016-01-11 11:29:07 +0530 | [diff] [blame] | 608 | return arche_pdata->svc_refclk_req; |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 609 | } |
| 610 | ret = devm_gpio_request(dev, arche_pdata->svc_refclk_req, "svc-clk-req"); |
| 611 | if (ret) { |
| 612 | dev_err(dev, "failed to request svc-clk-req gpio: %d\n", ret); |
| 613 | return ret; |
| 614 | } |
| 615 | ret = gpio_direction_input(arche_pdata->svc_refclk_req); |
| 616 | if (ret) { |
| 617 | dev_err(dev, "failed to set svc-clk-req gpio dir :%d\n", ret); |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | /* setup refclk2 to follow the pin */ |
| 622 | arche_pdata->svc_ref_clk = devm_clk_get(dev, "svc_ref_clk"); |
| 623 | if (IS_ERR(arche_pdata->svc_ref_clk)) { |
| 624 | ret = PTR_ERR(arche_pdata->svc_ref_clk); |
| 625 | dev_err(dev, "failed to get svc_ref_clk: %d\n", ret); |
| 626 | return ret; |
| 627 | } |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 628 | |
| 629 | platform_set_drvdata(pdev, arche_pdata); |
| 630 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 631 | arche_pdata->num_apbs = of_get_child_count(np); |
| 632 | dev_dbg(dev, "Number of APB's available - %d\n", arche_pdata->num_apbs); |
| 633 | |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 634 | arche_pdata->wake_detect_gpio = of_get_named_gpio(np, "svc,wake-detect-gpio", 0); |
| 635 | if (arche_pdata->wake_detect_gpio < 0) { |
| 636 | dev_err(dev, "failed to get wake detect gpio\n"); |
Eva Rachel Retuya | 5a420d1 | 2016-09-21 13:07:04 +0800 | [diff] [blame] | 637 | return arche_pdata->wake_detect_gpio; |
Viresh Kumar | 72a8c24 | 2016-01-11 11:29:05 +0530 | [diff] [blame] | 638 | } |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 639 | |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 640 | ret = devm_gpio_request(dev, arche_pdata->wake_detect_gpio, "wake detect"); |
| 641 | if (ret) { |
| 642 | dev_err(dev, "Failed requesting wake_detect gpio %d\n", |
| 643 | arche_pdata->wake_detect_gpio); |
Vaibhav Hiremath | 758ca99 | 2016-02-13 02:04:03 +0530 | [diff] [blame] | 644 | return ret; |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 645 | } |
Vaibhav Hiremath | 16fd976 | 2016-05-17 22:39:46 +0530 | [diff] [blame] | 646 | |
Bryan O'Donoghue | c4058b7 | 2016-05-05 15:34:59 +0100 | [diff] [blame] | 647 | arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE); |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 648 | |
| 649 | arche_pdata->dev = &pdev->dev; |
Vaibhav Hiremath | a463fc1 | 2016-01-11 17:41:24 +0530 | [diff] [blame] | 650 | |
Vaibhav Hiremath | 6c1ca56 | 2016-05-05 15:34:57 +0100 | [diff] [blame] | 651 | spin_lock_init(&arche_pdata->wake_lock); |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 652 | mutex_init(&arche_pdata->platform_state_mutex); |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 653 | init_waitqueue_head(&arche_pdata->wq); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 654 | arche_pdata->wake_detect_irq = |
| 655 | gpio_to_irq(arche_pdata->wake_detect_gpio); |
| 656 | |
| 657 | ret = devm_request_threaded_irq(dev, arche_pdata->wake_detect_irq, |
| 658 | arche_platform_wd_irq, |
| 659 | arche_platform_wd_irq_thread, |
| 660 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 661 | dev_name(dev), arche_pdata); |
| 662 | if (ret) { |
| 663 | dev_err(dev, "failed to request wake detect IRQ %d\n", ret); |
| 664 | return ret; |
| 665 | } |
Vaibhav Hiremath | d29b67d | 2016-07-19 13:31:46 +0530 | [diff] [blame] | 666 | disable_irq(arche_pdata->wake_detect_irq); |
Vaibhav Hiremath | f760bbf | 2016-02-25 04:37:36 +0530 | [diff] [blame] | 667 | |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 668 | ret = device_create_file(dev, &dev_attr_state); |
| 669 | if (ret) { |
| 670 | dev_err(dev, "failed to create state file in sysfs\n"); |
| 671 | return ret; |
| 672 | } |
| 673 | |
Vaibhav Hiremath | fd60ac5 | 2016-02-13 02:04:17 +0530 | [diff] [blame] | 674 | ret = of_platform_populate(np, NULL, NULL, dev); |
| 675 | if (ret) { |
| 676 | dev_err(dev, "failed to populate child nodes %d\n", ret); |
Vaibhav Hiremath | d29b67d | 2016-07-19 13:31:46 +0530 | [diff] [blame] | 677 | goto err_device_remove; |
Vaibhav Hiremath | fd60ac5 | 2016-02-13 02:04:17 +0530 | [diff] [blame] | 678 | } |
| 679 | |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 680 | arche_pdata->pm_notifier.notifier_call = arche_platform_pm_notifier; |
| 681 | ret = register_pm_notifier(&arche_pdata->pm_notifier); |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 682 | |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 683 | if (ret) { |
| 684 | dev_err(dev, "failed to register pm notifier %d\n", ret); |
Vaibhav Hiremath | d29b67d | 2016-07-19 13:31:46 +0530 | [diff] [blame] | 685 | goto err_device_remove; |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 686 | } |
| 687 | |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 688 | /* Register callback pointer */ |
| 689 | arche_platform_change_state_cb = arche_platform_change_state; |
| 690 | |
Vaibhav Hiremath | d29b67d | 2016-07-19 13:31:46 +0530 | [diff] [blame] | 691 | /* Explicitly power off if requested */ |
| 692 | if (!of_property_read_bool(pdev->dev.of_node, "arche,init-off")) { |
| 693 | mutex_lock(&arche_pdata->platform_state_mutex); |
| 694 | ret = arche_platform_coldboot_seq(arche_pdata); |
| 695 | if (ret) { |
| 696 | dev_err(dev, "Failed to cold boot svc %d\n", ret); |
| 697 | goto err_coldboot; |
| 698 | } |
| 699 | arche_platform_wd_irq_en(arche_pdata); |
| 700 | mutex_unlock(&arche_pdata->platform_state_mutex); |
| 701 | } |
| 702 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 703 | dev_info(dev, "Device registered successfully\n"); |
Viresh Kumar | 72a8c24 | 2016-01-11 11:29:05 +0530 | [diff] [blame] | 704 | return 0; |
Vaibhav Hiremath | 6743a6f | 2016-02-25 02:27:36 +0530 | [diff] [blame] | 705 | |
Vaibhav Hiremath | 6743a6f | 2016-02-25 02:27:36 +0530 | [diff] [blame] | 706 | err_coldboot: |
Vaibhav Hiremath | 886aba5 | 2016-05-05 15:34:58 +0100 | [diff] [blame] | 707 | mutex_unlock(&arche_pdata->platform_state_mutex); |
Vaibhav Hiremath | d29b67d | 2016-07-19 13:31:46 +0530 | [diff] [blame] | 708 | err_device_remove: |
Vaibhav Hiremath | 6743a6f | 2016-02-25 02:27:36 +0530 | [diff] [blame] | 709 | device_remove_file(&pdev->dev, &dev_attr_state); |
| 710 | return ret; |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 711 | } |
| 712 | |
Vaibhav Hiremath | bc142bb | 2015-12-28 20:06:32 +0530 | [diff] [blame] | 713 | static int arche_remove_child(struct device *dev, void *unused) |
| 714 | { |
| 715 | struct platform_device *pdev = to_platform_device(dev); |
| 716 | |
| 717 | platform_device_unregister(pdev); |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 722 | static int arche_platform_remove(struct platform_device *pdev) |
| 723 | { |
| 724 | struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev); |
| 725 | |
David Lin | 9160b7c | 2016-04-16 01:15:15 +0530 | [diff] [blame] | 726 | unregister_pm_notifier(&arche_pdata->pm_notifier); |
Vaibhav Hiremath | 2923c58 | 2016-02-13 02:04:07 +0530 | [diff] [blame] | 727 | device_remove_file(&pdev->dev, &dev_attr_state); |
Vaibhav Hiremath | bc142bb | 2015-12-28 20:06:32 +0530 | [diff] [blame] | 728 | device_for_each_child(&pdev->dev, NULL, arche_remove_child); |
Vaibhav Hiremath | 5993e2b | 2016-02-13 02:04:04 +0530 | [diff] [blame] | 729 | arche_platform_poweroff_seq(arche_pdata); |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 730 | platform_set_drvdata(pdev, NULL); |
| 731 | |
Vaibhav Hiremath | ad4d3f9 | 2016-02-13 02:04:20 +0530 | [diff] [blame] | 732 | if (usb3613_hub_mode_ctrl(false)) |
| 733 | dev_warn(arche_pdata->dev, "failed to control hub device\n"); |
| 734 | /* TODO: Should we do anything more here ?? */ |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | static int arche_platform_suspend(struct device *dev) |
| 739 | { |
| 740 | /* |
| 741 | * If timing profile premits, we may shutdown bridge |
| 742 | * completely |
| 743 | * |
| 744 | * TODO: sequence ?? |
| 745 | * |
| 746 | * Also, need to make sure we meet precondition for unipro suspend |
| 747 | * Precondition: Definition ??? |
| 748 | */ |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int arche_platform_resume(struct device *dev) |
| 753 | { |
| 754 | /* |
| 755 | * Atleast for ES2 we have to meet the delay requirement between |
| 756 | * unipro switch and AP bridge init, depending on whether bridge is in |
| 757 | * OFF state or standby state. |
| 758 | * |
| 759 | * Based on whether bridge is in standby or OFF state we may have to |
| 760 | * assert multiple signals. Please refer to WDM spec, for more info. |
| 761 | * |
| 762 | */ |
| 763 | return 0; |
| 764 | } |
| 765 | |
David Lin | 1f77b36 | 2016-07-12 17:41:21 -0700 | [diff] [blame] | 766 | static void arche_platform_shutdown(struct platform_device *pdev) |
| 767 | { |
| 768 | struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev); |
| 769 | |
| 770 | arche_platform_poweroff_seq(arche_pdata); |
| 771 | |
| 772 | usb3613_hub_mode_ctrl(false); |
| 773 | } |
| 774 | |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 775 | static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops, |
| 776 | arche_platform_suspend, |
| 777 | arche_platform_resume); |
| 778 | |
Eva Rachel Retuya | cdcffc0 | 2016-09-22 23:33:34 +0800 | [diff] [blame] | 779 | static const struct of_device_id arche_platform_of_match[] = { |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 780 | { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */ |
| 781 | { }, |
| 782 | }; |
Greg Kroah-Hartman | 1e5dd1f | 2015-12-30 13:38:33 -0800 | [diff] [blame] | 783 | |
Eva Rachel Retuya | cdcffc0 | 2016-09-22 23:33:34 +0800 | [diff] [blame] | 784 | static const struct of_device_id arche_combined_id[] = { |
Greg Kroah-Hartman | 1e5dd1f | 2015-12-30 13:38:33 -0800 | [diff] [blame] | 785 | { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */ |
| 786 | { .compatible = "usbffff,2", }, |
| 787 | { }, |
| 788 | }; |
| 789 | MODULE_DEVICE_TABLE(of, arche_combined_id); |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 790 | |
| 791 | static struct platform_driver arche_platform_device_driver = { |
| 792 | .probe = arche_platform_probe, |
| 793 | .remove = arche_platform_remove, |
David Lin | 1f77b36 | 2016-07-12 17:41:21 -0700 | [diff] [blame] | 794 | .shutdown = arche_platform_shutdown, |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 795 | .driver = { |
| 796 | .name = "arche-platform-ctrl", |
| 797 | .pm = &arche_platform_pm_ops, |
Greg Kroah-Hartman | 1e5dd1f | 2015-12-30 13:38:33 -0800 | [diff] [blame] | 798 | .of_match_table = arche_platform_of_match, |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 799 | } |
| 800 | }; |
| 801 | |
Greg Kroah-Hartman | 1e5dd1f | 2015-12-30 13:38:33 -0800 | [diff] [blame] | 802 | static int __init arche_init(void) |
| 803 | { |
| 804 | int retval; |
| 805 | |
| 806 | retval = platform_driver_register(&arche_platform_device_driver); |
| 807 | if (retval) |
| 808 | return retval; |
| 809 | |
Viresh Kumar | 7b62b61 | 2016-04-20 11:48:37 +0530 | [diff] [blame] | 810 | retval = arche_apb_init(); |
Greg Kroah-Hartman | 1e5dd1f | 2015-12-30 13:38:33 -0800 | [diff] [blame] | 811 | if (retval) |
| 812 | platform_driver_unregister(&arche_platform_device_driver); |
| 813 | |
| 814 | return retval; |
| 815 | } |
| 816 | module_init(arche_init); |
| 817 | |
| 818 | static void __exit arche_exit(void) |
| 819 | { |
Viresh Kumar | 7b62b61 | 2016-04-20 11:48:37 +0530 | [diff] [blame] | 820 | arche_apb_exit(); |
Greg Kroah-Hartman | 1e5dd1f | 2015-12-30 13:38:33 -0800 | [diff] [blame] | 821 | platform_driver_unregister(&arche_platform_device_driver); |
| 822 | } |
| 823 | module_exit(arche_exit); |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 824 | |
Sandeep Patil | a974f46 | 2016-05-13 12:00:56 -0700 | [diff] [blame] | 825 | MODULE_LICENSE("GPL v2"); |
Vaibhav Hiremath | 7fa6065 | 2015-12-16 16:29:18 +0530 | [diff] [blame] | 826 | MODULE_AUTHOR("Vaibhav Hiremath <vaibhav.hiremath@linaro.org>"); |
| 827 | MODULE_DESCRIPTION("Arche Platform Driver"); |