Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 1 | /* |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 2 | * Greybus interface code |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2014 Google Inc. |
Alex Elder | a46e967 | 2014-12-12 12:08:42 -0600 | [diff] [blame] | 5 | * Copyright 2014 Linaro Ltd. |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 10 | #include <linux/delay.h> |
| 11 | |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 12 | #include "greybus.h" |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 13 | #include "greybus_trace.h" |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 14 | |
Viresh Kumar | 7dea1d5 | 2016-05-30 13:05:10 +0530 | [diff] [blame] | 15 | #define GB_INTERFACE_MODE_SWITCH_TIMEOUT 2000 |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 16 | |
Johan Hovold | e9f2f68 | 2016-03-29 18:56:06 -0400 | [diff] [blame] | 17 | #define GB_INTERFACE_DEVICE_ID_BAD 0xff |
| 18 | |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 19 | #define GB_INTERFACE_AUTOSUSPEND_MS 3000 |
| 20 | |
| 21 | /* Time required for interface to enter standby before disabling REFCLK */ |
| 22 | #define GB_INTERFACE_SUSPEND_HIBERNATE_DELAY_MS 20 |
| 23 | |
Johan Hovold | 50ad416 | 2016-03-29 18:56:11 -0400 | [diff] [blame] | 24 | /* Don't-care selector index */ |
| 25 | #define DME_SELECTOR_INDEX_NULL 0 |
| 26 | |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 27 | /* DME attributes */ |
Johan Hovold | 50ad416 | 2016-03-29 18:56:11 -0400 | [diff] [blame] | 28 | /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */ |
| 29 | #define DME_T_TST_SRC_INCREMENT 0x4083 |
| 30 | |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 31 | #define DME_DDBL1_MANUFACTURERID 0x5003 |
| 32 | #define DME_DDBL1_PRODUCTID 0x5004 |
| 33 | |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 34 | #define DME_TOSHIBA_GMP_VID 0x6000 |
| 35 | #define DME_TOSHIBA_GMP_PID 0x6001 |
| 36 | #define DME_TOSHIBA_GMP_SN0 0x6002 |
| 37 | #define DME_TOSHIBA_GMP_SN1 0x6003 |
| 38 | #define DME_TOSHIBA_GMP_INIT_STATUS 0x6101 |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 39 | |
| 40 | /* DDBL1 Manufacturer and Product ids */ |
| 41 | #define TOSHIBA_DMID 0x0126 |
| 42 | #define TOSHIBA_ES2_BRIDGE_DPID 0x1000 |
| 43 | #define TOSHIBA_ES3_APBRIDGE_DPID 0x1001 |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 44 | #define TOSHIBA_ES3_GBPHY_DPID 0x1002 |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 45 | |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 46 | static int gb_interface_hibernate_link(struct gb_interface *intf); |
| 47 | static int gb_interface_refclk_set(struct gb_interface *intf, bool enable); |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 48 | |
| 49 | static int gb_interface_dme_attr_get(struct gb_interface *intf, |
| 50 | u16 attr, u32 *val) |
| 51 | { |
| 52 | return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id, |
Johan Hovold | 50ad416 | 2016-03-29 18:56:11 -0400 | [diff] [blame] | 53 | attr, DME_SELECTOR_INDEX_NULL, val); |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static int gb_interface_read_ara_dme(struct gb_interface *intf) |
| 57 | { |
Johan Hovold | a7be846 | 2016-03-29 18:56:12 -0400 | [diff] [blame] | 58 | u32 sn0, sn1; |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 59 | int ret; |
| 60 | |
| 61 | /* |
| 62 | * Unless this is a Toshiba bridge, bail out until we have defined |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 63 | * standard GMP attributes. |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 64 | */ |
| 65 | if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) { |
| 66 | dev_err(&intf->dev, "unknown manufacturer %08x\n", |
| 67 | intf->ddbl1_manufacturer_id); |
| 68 | return -ENODEV; |
| 69 | } |
| 70 | |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 71 | ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_VID, |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 72 | &intf->vendor_id); |
| 73 | if (ret) |
| 74 | return ret; |
| 75 | |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 76 | ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_PID, |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 77 | &intf->product_id); |
| 78 | if (ret) |
| 79 | return ret; |
| 80 | |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 81 | ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_SN0, &sn0); |
Johan Hovold | a7be846 | 2016-03-29 18:56:12 -0400 | [diff] [blame] | 82 | if (ret) |
| 83 | return ret; |
| 84 | |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 85 | ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_SN1, &sn1); |
Johan Hovold | a7be846 | 2016-03-29 18:56:12 -0400 | [diff] [blame] | 86 | if (ret) |
| 87 | return ret; |
| 88 | |
| 89 | intf->serial_number = (u64)sn1 << 32 | sn0; |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int gb_interface_read_dme(struct gb_interface *intf) |
| 95 | { |
| 96 | int ret; |
| 97 | |
Johan Hovold | 27b9e25 | 2016-07-20 16:40:21 +0200 | [diff] [blame] | 98 | /* DME attributes have already been read */ |
| 99 | if (intf->dme_read) |
| 100 | return 0; |
| 101 | |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 102 | ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID, |
| 103 | &intf->ddbl1_manufacturer_id); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | |
| 107 | ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID, |
| 108 | &intf->ddbl1_product_id); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
Johan Hovold | 7d7acc0 | 2016-03-29 18:56:09 -0400 | [diff] [blame] | 112 | if (intf->ddbl1_manufacturer_id == TOSHIBA_DMID && |
| 113 | intf->ddbl1_product_id == TOSHIBA_ES2_BRIDGE_DPID) { |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 114 | intf->quirks |= GB_INTERFACE_QUIRK_NO_GMP_IDS; |
Johan Hovold | 7d7acc0 | 2016-03-29 18:56:09 -0400 | [diff] [blame] | 115 | intf->quirks |= GB_INTERFACE_QUIRK_NO_INIT_STATUS; |
| 116 | } |
| 117 | |
Johan Hovold | 27b9e25 | 2016-07-20 16:40:21 +0200 | [diff] [blame] | 118 | ret = gb_interface_read_ara_dme(intf); |
| 119 | if (ret) |
| 120 | return ret; |
| 121 | |
| 122 | intf->dme_read = true; |
| 123 | |
| 124 | return 0; |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 125 | } |
Johan Hovold | e9f2f68 | 2016-03-29 18:56:06 -0400 | [diff] [blame] | 126 | |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 127 | static int gb_interface_route_create(struct gb_interface *intf) |
| 128 | { |
| 129 | struct gb_svc *svc = intf->hd->svc; |
| 130 | u8 intf_id = intf->interface_id; |
| 131 | u8 device_id; |
| 132 | int ret; |
| 133 | |
Johan Hovold | e9f2f68 | 2016-03-29 18:56:06 -0400 | [diff] [blame] | 134 | /* Allocate an interface device id. */ |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 135 | ret = ida_simple_get(&svc->device_id_map, |
Johan Hovold | e9f2f68 | 2016-03-29 18:56:06 -0400 | [diff] [blame] | 136 | GB_SVC_DEVICE_ID_MIN, GB_SVC_DEVICE_ID_MAX + 1, |
| 137 | GFP_KERNEL); |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 138 | if (ret < 0) { |
| 139 | dev_err(&intf->dev, "failed to allocate device id: %d\n", ret); |
| 140 | return ret; |
| 141 | } |
| 142 | device_id = ret; |
| 143 | |
| 144 | ret = gb_svc_intf_device_id(svc, intf_id, device_id); |
| 145 | if (ret) { |
| 146 | dev_err(&intf->dev, "failed to set device id %u: %d\n", |
| 147 | device_id, ret); |
| 148 | goto err_ida_remove; |
| 149 | } |
| 150 | |
Johan Hovold | e9f2f68 | 2016-03-29 18:56:06 -0400 | [diff] [blame] | 151 | /* FIXME: Hard-coded AP device id. */ |
| 152 | ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP, |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 153 | intf_id, device_id); |
| 154 | if (ret) { |
| 155 | dev_err(&intf->dev, "failed to create route: %d\n", ret); |
| 156 | goto err_svc_id_free; |
| 157 | } |
| 158 | |
| 159 | intf->device_id = device_id; |
| 160 | |
| 161 | return 0; |
| 162 | |
| 163 | err_svc_id_free: |
| 164 | /* |
| 165 | * XXX Should we tell SVC that this id doesn't belong to interface |
| 166 | * XXX anymore. |
| 167 | */ |
| 168 | err_ida_remove: |
| 169 | ida_simple_remove(&svc->device_id_map, device_id); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static void gb_interface_route_destroy(struct gb_interface *intf) |
| 175 | { |
| 176 | struct gb_svc *svc = intf->hd->svc; |
| 177 | |
Johan Hovold | e9f2f68 | 2016-03-29 18:56:06 -0400 | [diff] [blame] | 178 | if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD) |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 179 | return; |
| 180 | |
| 181 | gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id); |
| 182 | ida_simple_remove(&svc->device_id_map, intf->device_id); |
Johan Hovold | e9f2f68 | 2016-03-29 18:56:06 -0400 | [diff] [blame] | 183 | intf->device_id = GB_INTERFACE_DEVICE_ID_BAD; |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 186 | /* Locking: Caller holds the interface mutex. */ |
| 187 | static int gb_interface_legacy_mode_switch(struct gb_interface *intf) |
| 188 | { |
| 189 | int ret; |
| 190 | |
| 191 | dev_info(&intf->dev, "legacy mode switch detected\n"); |
| 192 | |
| 193 | /* Mark as disconnected to prevent I/O during disable. */ |
| 194 | intf->disconnected = true; |
| 195 | gb_interface_disable(intf); |
| 196 | intf->disconnected = false; |
| 197 | |
| 198 | ret = gb_interface_enable(intf); |
| 199 | if (ret) { |
| 200 | dev_err(&intf->dev, "failed to re-enable interface: %d\n", ret); |
| 201 | gb_interface_deactivate(intf); |
| 202 | } |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | void gb_interface_mailbox_event(struct gb_interface *intf, u16 result, |
| 208 | u32 mailbox) |
| 209 | { |
| 210 | mutex_lock(&intf->mutex); |
| 211 | |
| 212 | if (result) { |
| 213 | dev_warn(&intf->dev, |
| 214 | "mailbox event with UniPro error: 0x%04x\n", |
| 215 | result); |
| 216 | goto err_disable; |
| 217 | } |
| 218 | |
| 219 | if (mailbox != GB_SVC_INTF_MAILBOX_GREYBUS) { |
| 220 | dev_warn(&intf->dev, |
| 221 | "mailbox event with unexpected value: 0x%08x\n", |
| 222 | mailbox); |
| 223 | goto err_disable; |
| 224 | } |
| 225 | |
| 226 | if (intf->quirks & GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH) { |
| 227 | gb_interface_legacy_mode_switch(intf); |
| 228 | goto out_unlock; |
| 229 | } |
| 230 | |
| 231 | if (!intf->mode_switch) { |
| 232 | dev_warn(&intf->dev, "unexpected mailbox event: 0x%08x\n", |
| 233 | mailbox); |
| 234 | goto err_disable; |
| 235 | } |
| 236 | |
| 237 | dev_info(&intf->dev, "mode switch detected\n"); |
| 238 | |
| 239 | complete(&intf->mode_switch_completion); |
| 240 | |
| 241 | out_unlock: |
| 242 | mutex_unlock(&intf->mutex); |
| 243 | |
| 244 | return; |
| 245 | |
| 246 | err_disable: |
| 247 | gb_interface_disable(intf); |
| 248 | gb_interface_deactivate(intf); |
| 249 | mutex_unlock(&intf->mutex); |
| 250 | } |
| 251 | |
| 252 | static void gb_interface_mode_switch_work(struct work_struct *work) |
| 253 | { |
| 254 | struct gb_interface *intf; |
| 255 | struct gb_control *control; |
| 256 | unsigned long timeout; |
| 257 | int ret; |
| 258 | |
| 259 | intf = container_of(work, struct gb_interface, mode_switch_work); |
| 260 | |
| 261 | mutex_lock(&intf->mutex); |
| 262 | /* Make sure interface is still enabled. */ |
| 263 | if (!intf->enabled) { |
| 264 | dev_dbg(&intf->dev, "mode switch aborted\n"); |
| 265 | intf->mode_switch = false; |
| 266 | mutex_unlock(&intf->mutex); |
| 267 | goto out_interface_put; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Prepare the control device for mode switch and make sure to get an |
| 272 | * extra reference before it goes away during interface disable. |
| 273 | */ |
| 274 | control = gb_control_get(intf->control); |
| 275 | gb_control_mode_switch_prepare(control); |
| 276 | gb_interface_disable(intf); |
| 277 | mutex_unlock(&intf->mutex); |
| 278 | |
| 279 | timeout = msecs_to_jiffies(GB_INTERFACE_MODE_SWITCH_TIMEOUT); |
| 280 | ret = wait_for_completion_interruptible_timeout( |
| 281 | &intf->mode_switch_completion, timeout); |
| 282 | |
| 283 | /* Finalise control-connection mode switch. */ |
| 284 | gb_control_mode_switch_complete(control); |
| 285 | gb_control_put(control); |
| 286 | |
| 287 | if (ret < 0) { |
| 288 | dev_err(&intf->dev, "mode switch interrupted\n"); |
| 289 | goto err_deactivate; |
| 290 | } else if (ret == 0) { |
| 291 | dev_err(&intf->dev, "mode switch timed out\n"); |
| 292 | goto err_deactivate; |
| 293 | } |
| 294 | |
| 295 | /* Re-enable (re-enumerate) interface if still active. */ |
| 296 | mutex_lock(&intf->mutex); |
| 297 | intf->mode_switch = false; |
| 298 | if (intf->active) { |
| 299 | ret = gb_interface_enable(intf); |
| 300 | if (ret) { |
| 301 | dev_err(&intf->dev, "failed to re-enable interface: %d\n", |
| 302 | ret); |
| 303 | gb_interface_deactivate(intf); |
| 304 | } |
| 305 | } |
| 306 | mutex_unlock(&intf->mutex); |
| 307 | |
| 308 | out_interface_put: |
| 309 | gb_interface_put(intf); |
| 310 | |
| 311 | return; |
| 312 | |
| 313 | err_deactivate: |
| 314 | mutex_lock(&intf->mutex); |
| 315 | intf->mode_switch = false; |
| 316 | gb_interface_deactivate(intf); |
| 317 | mutex_unlock(&intf->mutex); |
| 318 | |
| 319 | gb_interface_put(intf); |
| 320 | } |
| 321 | |
| 322 | int gb_interface_request_mode_switch(struct gb_interface *intf) |
| 323 | { |
| 324 | int ret = 0; |
| 325 | |
| 326 | mutex_lock(&intf->mutex); |
| 327 | if (intf->mode_switch) { |
| 328 | ret = -EBUSY; |
| 329 | goto out_unlock; |
| 330 | } |
| 331 | |
| 332 | intf->mode_switch = true; |
| 333 | reinit_completion(&intf->mode_switch_completion); |
| 334 | |
| 335 | /* |
| 336 | * Get a reference to the interface device, which will be put once the |
| 337 | * mode switch is complete. |
| 338 | */ |
| 339 | get_device(&intf->dev); |
| 340 | |
| 341 | if (!queue_work(system_long_wq, &intf->mode_switch_work)) { |
| 342 | put_device(&intf->dev); |
| 343 | ret = -EBUSY; |
| 344 | goto out_unlock; |
| 345 | } |
| 346 | |
| 347 | out_unlock: |
| 348 | mutex_unlock(&intf->mutex); |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | EXPORT_SYMBOL_GPL(gb_interface_request_mode_switch); |
| 353 | |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 354 | /* |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 355 | * T_TstSrcIncrement is written by the module on ES2 as a stand-in for the |
Johan Hovold | 50ad416 | 2016-03-29 18:56:11 -0400 | [diff] [blame] | 356 | * init-status attribute DME_TOSHIBA_INIT_STATUS. The AP needs to read and |
| 357 | * clear it after reading a non-zero value from it. |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 358 | * |
| 359 | * FIXME: This is module-hardware dependent and needs to be extended for every |
| 360 | * type of module we want to support. |
| 361 | */ |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 362 | static int gb_interface_read_and_clear_init_status(struct gb_interface *intf) |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 363 | { |
| 364 | struct gb_host_device *hd = intf->hd; |
Johan Hovold | a4b08df | 2016-05-27 17:26:23 +0200 | [diff] [blame] | 365 | unsigned long bootrom_quirks; |
Viresh Kumar | 0c543f9 | 2016-07-26 13:41:04 -0700 | [diff] [blame] | 366 | unsigned long s2l_quirks; |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 367 | int ret; |
| 368 | u32 value; |
| 369 | u16 attr; |
| 370 | u8 init_status; |
| 371 | |
| 372 | /* |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 373 | * ES2 bridges use T_TstSrcIncrement for the init status. |
| 374 | * |
| 375 | * FIXME: Remove ES2 support |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 376 | */ |
Johan Hovold | 7d7acc0 | 2016-03-29 18:56:09 -0400 | [diff] [blame] | 377 | if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS) |
Johan Hovold | 50ad416 | 2016-03-29 18:56:11 -0400 | [diff] [blame] | 378 | attr = DME_T_TST_SRC_INCREMENT; |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 379 | else |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 380 | attr = DME_TOSHIBA_GMP_INIT_STATUS; |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 381 | |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 382 | ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr, |
Johan Hovold | 50ad416 | 2016-03-29 18:56:11 -0400 | [diff] [blame] | 383 | DME_SELECTOR_INDEX_NULL, &value); |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 384 | if (ret) |
| 385 | return ret; |
| 386 | |
| 387 | /* |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 388 | * A nonzero init status indicates the module has finished |
| 389 | * initializing. |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 390 | */ |
| 391 | if (!value) { |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 392 | dev_err(&intf->dev, "invalid init status\n"); |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 393 | return -ENODEV; |
| 394 | } |
| 395 | |
| 396 | /* |
Johan Hovold | ec199cc | 2016-03-29 18:56:03 -0400 | [diff] [blame] | 397 | * Extract the init status. |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 398 | * |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 399 | * For ES2: We need to check lowest 8 bits of 'value'. |
| 400 | * For ES3: We need to check highest 8 bits out of 32 of 'value'. |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 401 | * |
| 402 | * FIXME: Remove ES2 support |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 403 | */ |
Johan Hovold | 7d7acc0 | 2016-03-29 18:56:09 -0400 | [diff] [blame] | 404 | if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS) |
Johan Hovold | e12811e | 2016-03-29 18:56:01 -0400 | [diff] [blame] | 405 | init_status = value & 0xff; |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 406 | else |
| 407 | init_status = value >> 24; |
| 408 | |
Johan Hovold | ec199cc | 2016-03-29 18:56:03 -0400 | [diff] [blame] | 409 | /* |
Johan Hovold | a4b08df | 2016-05-27 17:26:23 +0200 | [diff] [blame] | 410 | * Check if the interface is executing the quirky ES3 bootrom that, |
| 411 | * for example, requires E2EFC, CSD and CSV to be disabled. |
Johan Hovold | ec199cc | 2016-03-29 18:56:03 -0400 | [diff] [blame] | 412 | */ |
Johan Hovold | d9fa349 | 2016-05-27 17:26:24 +0200 | [diff] [blame] | 413 | bootrom_quirks = GB_INTERFACE_QUIRK_NO_CPORT_FEATURES | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 414 | GB_INTERFACE_QUIRK_FORCED_DISABLE | |
Johan Hovold | 2358024 | 2016-07-21 14:24:11 +0200 | [diff] [blame] | 415 | GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH | |
Viresh Kumar | 47cbaf5 | 2016-07-26 13:41:03 -0700 | [diff] [blame] | 416 | GB_INTERFACE_QUIRK_NO_BUNDLE_ACTIVATE; |
Viresh Kumar | 0c543f9 | 2016-07-26 13:41:04 -0700 | [diff] [blame] | 417 | |
| 418 | s2l_quirks = GB_INTERFACE_QUIRK_NO_PM; |
| 419 | |
Johan Hovold | ec199cc | 2016-03-29 18:56:03 -0400 | [diff] [blame] | 420 | switch (init_status) { |
Johan Hovold | 50ad416 | 2016-03-29 18:56:11 -0400 | [diff] [blame] | 421 | case GB_INIT_BOOTROM_UNIPRO_BOOT_STARTED: |
| 422 | case GB_INIT_BOOTROM_FALLBACK_UNIPRO_BOOT_STARTED: |
Johan Hovold | a4b08df | 2016-05-27 17:26:23 +0200 | [diff] [blame] | 423 | intf->quirks |= bootrom_quirks; |
Johan Hovold | ec199cc | 2016-03-29 18:56:03 -0400 | [diff] [blame] | 424 | break; |
Viresh Kumar | 0c543f9 | 2016-07-26 13:41:04 -0700 | [diff] [blame] | 425 | case GB_INIT_S2_LOADER_BOOT_STARTED: |
| 426 | /* S2 Loader doesn't support runtime PM */ |
| 427 | intf->quirks &= ~bootrom_quirks; |
| 428 | intf->quirks |= s2l_quirks; |
| 429 | break; |
Johan Hovold | 6ddbed5 | 2016-04-28 16:19:39 +0200 | [diff] [blame] | 430 | default: |
Johan Hovold | a4b08df | 2016-05-27 17:26:23 +0200 | [diff] [blame] | 431 | intf->quirks &= ~bootrom_quirks; |
Viresh Kumar | 0c543f9 | 2016-07-26 13:41:04 -0700 | [diff] [blame] | 432 | intf->quirks &= ~s2l_quirks; |
Johan Hovold | ec199cc | 2016-03-29 18:56:03 -0400 | [diff] [blame] | 433 | } |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 434 | |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 435 | /* Clear the init status. */ |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 436 | return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr, |
Johan Hovold | 50ad416 | 2016-03-29 18:56:11 -0400 | [diff] [blame] | 437 | DME_SELECTOR_INDEX_NULL, 0); |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 440 | /* interface sysfs attributes */ |
| 441 | #define gb_interface_attr(field, type) \ |
| 442 | static ssize_t field##_show(struct device *dev, \ |
| 443 | struct device_attribute *attr, \ |
| 444 | char *buf) \ |
Greg Kroah-Hartman | ab88eb5 | 2014-12-11 17:10:59 -0500 | [diff] [blame] | 445 | { \ |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 446 | struct gb_interface *intf = to_gb_interface(dev); \ |
Viresh Kumar | 2c7df74 | 2015-12-18 15:04:27 +0530 | [diff] [blame] | 447 | return scnprintf(buf, PAGE_SIZE, type"\n", intf->field); \ |
Greg Kroah-Hartman | ab88eb5 | 2014-12-11 17:10:59 -0500 | [diff] [blame] | 448 | } \ |
| 449 | static DEVICE_ATTR_RO(field) |
| 450 | |
Viresh Kumar | 0e9403a0 | 2015-12-22 22:04:34 +0530 | [diff] [blame] | 451 | gb_interface_attr(ddbl1_manufacturer_id, "0x%08x"); |
| 452 | gb_interface_attr(ddbl1_product_id, "0x%08x"); |
Viresh Kumar | 2c7df74 | 2015-12-18 15:04:27 +0530 | [diff] [blame] | 453 | gb_interface_attr(interface_id, "%u"); |
Greg Kroah-Hartman | 611924d | 2016-02-26 21:54:38 -0800 | [diff] [blame] | 454 | gb_interface_attr(vendor_id, "0x%08x"); |
| 455 | gb_interface_attr(product_id, "0x%08x"); |
Viresh Kumar | 57c6bcc | 2015-12-28 11:59:00 +0530 | [diff] [blame] | 456 | gb_interface_attr(serial_number, "0x%016llx"); |
Greg Kroah-Hartman | ab88eb5 | 2014-12-11 17:10:59 -0500 | [diff] [blame] | 457 | |
David Lin | ddb10c8 | 2016-04-07 20:15:30 -0700 | [diff] [blame] | 458 | static ssize_t voltage_now_show(struct device *dev, |
| 459 | struct device_attribute *attr, char *buf) |
| 460 | { |
| 461 | struct gb_interface *intf = to_gb_interface(dev); |
| 462 | int ret; |
| 463 | u32 measurement; |
| 464 | |
| 465 | ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id, |
| 466 | GB_SVC_PWRMON_TYPE_VOL, |
| 467 | &measurement); |
| 468 | if (ret) { |
| 469 | dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret); |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | return sprintf(buf, "%u\n", measurement); |
| 474 | } |
| 475 | static DEVICE_ATTR_RO(voltage_now); |
| 476 | |
| 477 | static ssize_t current_now_show(struct device *dev, |
| 478 | struct device_attribute *attr, char *buf) |
| 479 | { |
| 480 | struct gb_interface *intf = to_gb_interface(dev); |
| 481 | int ret; |
| 482 | u32 measurement; |
| 483 | |
| 484 | ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id, |
| 485 | GB_SVC_PWRMON_TYPE_CURR, |
| 486 | &measurement); |
| 487 | if (ret) { |
| 488 | dev_err(&intf->dev, "failed to get current sample (%d)\n", ret); |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | return sprintf(buf, "%u\n", measurement); |
| 493 | } |
| 494 | static DEVICE_ATTR_RO(current_now); |
| 495 | |
| 496 | static ssize_t power_now_show(struct device *dev, |
| 497 | struct device_attribute *attr, char *buf) |
| 498 | { |
| 499 | struct gb_interface *intf = to_gb_interface(dev); |
| 500 | int ret; |
| 501 | u32 measurement; |
| 502 | |
| 503 | ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id, |
| 504 | GB_SVC_PWRMON_TYPE_PWR, |
| 505 | &measurement); |
| 506 | if (ret) { |
| 507 | dev_err(&intf->dev, "failed to get power sample (%d)\n", ret); |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | return sprintf(buf, "%u\n", measurement); |
| 512 | } |
| 513 | static DEVICE_ATTR_RO(power_now); |
| 514 | |
Viresh Kumar | 93e29c8 | 2016-07-20 16:40:25 +0200 | [diff] [blame] | 515 | static ssize_t power_state_show(struct device *dev, |
| 516 | struct device_attribute *attr, char *buf) |
| 517 | { |
| 518 | struct gb_interface *intf = to_gb_interface(dev); |
| 519 | |
| 520 | if (intf->active) |
| 521 | return scnprintf(buf, PAGE_SIZE, "on\n"); |
| 522 | else |
| 523 | return scnprintf(buf, PAGE_SIZE, "off\n"); |
| 524 | } |
| 525 | |
| 526 | static ssize_t power_state_store(struct device *dev, |
| 527 | struct device_attribute *attr, const char *buf, |
| 528 | size_t len) |
| 529 | { |
| 530 | struct gb_interface *intf = to_gb_interface(dev); |
| 531 | bool activate; |
| 532 | int ret = 0; |
| 533 | |
| 534 | if (kstrtobool(buf, &activate)) |
| 535 | return -EINVAL; |
| 536 | |
| 537 | mutex_lock(&intf->mutex); |
| 538 | |
| 539 | if (activate == intf->active) |
| 540 | goto unlock; |
| 541 | |
| 542 | if (activate) { |
| 543 | ret = gb_interface_activate(intf); |
| 544 | if (ret) { |
| 545 | dev_err(&intf->dev, |
| 546 | "failed to activate interface: %d\n", ret); |
| 547 | goto unlock; |
| 548 | } |
| 549 | |
| 550 | ret = gb_interface_enable(intf); |
| 551 | if (ret) { |
| 552 | dev_err(&intf->dev, |
| 553 | "failed to enable interface: %d\n", ret); |
| 554 | gb_interface_deactivate(intf); |
| 555 | goto unlock; |
| 556 | } |
| 557 | } else { |
| 558 | gb_interface_disable(intf); |
| 559 | gb_interface_deactivate(intf); |
| 560 | } |
| 561 | |
| 562 | unlock: |
| 563 | mutex_unlock(&intf->mutex); |
| 564 | |
| 565 | if (ret) |
| 566 | return ret; |
| 567 | |
| 568 | return len; |
| 569 | } |
| 570 | static DEVICE_ATTR_RW(power_state); |
| 571 | |
Johan Hovold | 1bb6184 | 2016-07-19 15:24:46 +0200 | [diff] [blame] | 572 | static const char *gb_interface_type_string(struct gb_interface *intf) |
| 573 | { |
| 574 | static const char * const types[] = { |
Johan Hovold | a212b75 | 2016-07-20 16:40:20 +0200 | [diff] [blame] | 575 | [GB_INTERFACE_TYPE_INVALID] = "invalid", |
| 576 | [GB_INTERFACE_TYPE_UNKNOWN] = "unknown", |
| 577 | [GB_INTERFACE_TYPE_DUMMY] = "dummy", |
| 578 | [GB_INTERFACE_TYPE_UNIPRO] = "unipro", |
| 579 | [GB_INTERFACE_TYPE_GREYBUS] = "greybus", |
Johan Hovold | 1bb6184 | 2016-07-19 15:24:46 +0200 | [diff] [blame] | 580 | }; |
| 581 | |
| 582 | return types[intf->type]; |
| 583 | } |
| 584 | |
| 585 | static ssize_t interface_type_show(struct device *dev, |
| 586 | struct device_attribute *attr, char *buf) |
| 587 | { |
| 588 | struct gb_interface *intf = to_gb_interface(dev); |
| 589 | |
| 590 | return sprintf(buf, "%s\n", gb_interface_type_string(intf)); |
| 591 | } |
| 592 | static DEVICE_ATTR_RO(interface_type); |
| 593 | |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 594 | static struct attribute *interface_unipro_attrs[] = { |
Viresh Kumar | 0e9403a0 | 2015-12-22 22:04:34 +0530 | [diff] [blame] | 595 | &dev_attr_ddbl1_manufacturer_id.attr, |
| 596 | &dev_attr_ddbl1_product_id.attr, |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 597 | NULL |
| 598 | }; |
| 599 | |
| 600 | static struct attribute *interface_greybus_attrs[] = { |
Johan Hovold | 9f59263 | 2015-11-25 15:58:56 +0100 | [diff] [blame] | 601 | &dev_attr_vendor_id.attr, |
| 602 | &dev_attr_product_id.attr, |
Viresh Kumar | 57c6bcc | 2015-12-28 11:59:00 +0530 | [diff] [blame] | 603 | &dev_attr_serial_number.attr, |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 604 | NULL |
| 605 | }; |
| 606 | |
| 607 | static struct attribute *interface_power_attrs[] = { |
David Lin | ddb10c8 | 2016-04-07 20:15:30 -0700 | [diff] [blame] | 608 | &dev_attr_voltage_now.attr, |
| 609 | &dev_attr_current_now.attr, |
| 610 | &dev_attr_power_now.attr, |
Viresh Kumar | 93e29c8 | 2016-07-20 16:40:25 +0200 | [diff] [blame] | 611 | &dev_attr_power_state.attr, |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 612 | NULL |
Greg Kroah-Hartman | ab88eb5 | 2014-12-11 17:10:59 -0500 | [diff] [blame] | 613 | }; |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 614 | |
| 615 | static struct attribute *interface_common_attrs[] = { |
Johan Hovold | b5d1d12 | 2016-07-21 12:48:57 +0200 | [diff] [blame] | 616 | &dev_attr_interface_id.attr, |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 617 | &dev_attr_interface_type.attr, |
| 618 | NULL |
| 619 | }; |
| 620 | |
Johan Hovold | 8356425 | 2016-07-19 15:24:48 +0200 | [diff] [blame] | 621 | static umode_t interface_unipro_is_visible(struct kobject *kobj, |
| 622 | struct attribute *attr, int n) |
| 623 | { |
| 624 | struct device *dev = container_of(kobj, struct device, kobj); |
| 625 | struct gb_interface *intf = to_gb_interface(dev); |
| 626 | |
| 627 | switch (intf->type) { |
Johan Hovold | a212b75 | 2016-07-20 16:40:20 +0200 | [diff] [blame] | 628 | case GB_INTERFACE_TYPE_UNIPRO: |
| 629 | case GB_INTERFACE_TYPE_GREYBUS: |
Johan Hovold | 8356425 | 2016-07-19 15:24:48 +0200 | [diff] [blame] | 630 | return attr->mode; |
| 631 | default: |
| 632 | return 0; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | static umode_t interface_greybus_is_visible(struct kobject *kobj, |
| 637 | struct attribute *attr, int n) |
| 638 | { |
| 639 | struct device *dev = container_of(kobj, struct device, kobj); |
| 640 | struct gb_interface *intf = to_gb_interface(dev); |
| 641 | |
| 642 | switch (intf->type) { |
Johan Hovold | a212b75 | 2016-07-20 16:40:20 +0200 | [diff] [blame] | 643 | case GB_INTERFACE_TYPE_GREYBUS: |
Johan Hovold | 8356425 | 2016-07-19 15:24:48 +0200 | [diff] [blame] | 644 | return attr->mode; |
| 645 | default: |
| 646 | return 0; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | static umode_t interface_power_is_visible(struct kobject *kobj, |
| 651 | struct attribute *attr, int n) |
| 652 | { |
| 653 | struct device *dev = container_of(kobj, struct device, kobj); |
| 654 | struct gb_interface *intf = to_gb_interface(dev); |
| 655 | |
| 656 | switch (intf->type) { |
Johan Hovold | a212b75 | 2016-07-20 16:40:20 +0200 | [diff] [blame] | 657 | case GB_INTERFACE_TYPE_UNIPRO: |
| 658 | case GB_INTERFACE_TYPE_GREYBUS: |
Johan Hovold | 8356425 | 2016-07-19 15:24:48 +0200 | [diff] [blame] | 659 | return attr->mode; |
| 660 | default: |
| 661 | return 0; |
| 662 | } |
| 663 | } |
| 664 | |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 665 | static const struct attribute_group interface_unipro_group = { |
Johan Hovold | 8356425 | 2016-07-19 15:24:48 +0200 | [diff] [blame] | 666 | .is_visible = interface_unipro_is_visible, |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 667 | .attrs = interface_unipro_attrs, |
| 668 | }; |
| 669 | |
| 670 | static const struct attribute_group interface_greybus_group = { |
Johan Hovold | 8356425 | 2016-07-19 15:24:48 +0200 | [diff] [blame] | 671 | .is_visible = interface_greybus_is_visible, |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 672 | .attrs = interface_greybus_attrs, |
| 673 | }; |
| 674 | |
| 675 | static const struct attribute_group interface_power_group = { |
Johan Hovold | 8356425 | 2016-07-19 15:24:48 +0200 | [diff] [blame] | 676 | .is_visible = interface_power_is_visible, |
Johan Hovold | 441ac1f | 2016-07-19 15:24:47 +0200 | [diff] [blame] | 677 | .attrs = interface_power_attrs, |
| 678 | }; |
| 679 | |
| 680 | static const struct attribute_group interface_common_group = { |
| 681 | .attrs = interface_common_attrs, |
| 682 | }; |
| 683 | |
| 684 | static const struct attribute_group *interface_groups[] = { |
| 685 | &interface_unipro_group, |
| 686 | &interface_greybus_group, |
| 687 | &interface_power_group, |
| 688 | &interface_common_group, |
| 689 | NULL |
| 690 | }; |
Greg Kroah-Hartman | ab88eb5 | 2014-12-11 17:10:59 -0500 | [diff] [blame] | 691 | |
Viresh Kumar | 51b5d8d | 2015-05-20 17:33:51 +0530 | [diff] [blame] | 692 | static void gb_interface_release(struct device *dev) |
Alex Elder | 697e55d | 2014-10-20 23:01:04 -0500 | [diff] [blame] | 693 | { |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 694 | struct gb_interface *intf = to_gb_interface(dev); |
Alex Elder | 697e55d | 2014-10-20 23:01:04 -0500 | [diff] [blame] | 695 | |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 696 | trace_gb_interface_release(intf); |
| 697 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 698 | kfree(intf); |
Alex Elder | 697e55d | 2014-10-20 23:01:04 -0500 | [diff] [blame] | 699 | } |
| 700 | |
Greg Kroah-Hartman | 948c622 | 2016-09-09 09:47:01 +0200 | [diff] [blame] | 701 | #ifdef CONFIG_PM |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 702 | static int gb_interface_suspend(struct device *dev) |
| 703 | { |
| 704 | struct gb_interface *intf = to_gb_interface(dev); |
| 705 | int ret, timesync_ret; |
| 706 | |
| 707 | ret = gb_control_interface_suspend_prepare(intf->control); |
| 708 | if (ret) |
| 709 | return ret; |
| 710 | |
| 711 | gb_timesync_interface_remove(intf); |
| 712 | |
| 713 | ret = gb_control_suspend(intf->control); |
| 714 | if (ret) |
| 715 | goto err_hibernate_abort; |
| 716 | |
| 717 | ret = gb_interface_hibernate_link(intf); |
| 718 | if (ret) |
| 719 | return ret; |
| 720 | |
| 721 | /* Delay to allow interface to enter standby before disabling refclk */ |
| 722 | msleep(GB_INTERFACE_SUSPEND_HIBERNATE_DELAY_MS); |
| 723 | |
| 724 | ret = gb_interface_refclk_set(intf, false); |
| 725 | if (ret) |
| 726 | return ret; |
| 727 | |
| 728 | return 0; |
| 729 | |
| 730 | err_hibernate_abort: |
| 731 | gb_control_interface_hibernate_abort(intf->control); |
| 732 | |
| 733 | timesync_ret = gb_timesync_interface_add(intf); |
| 734 | if (timesync_ret) { |
| 735 | dev_err(dev, "failed to add to timesync: %d\n", timesync_ret); |
| 736 | return timesync_ret; |
| 737 | } |
| 738 | |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | static int gb_interface_resume(struct device *dev) |
| 743 | { |
| 744 | struct gb_interface *intf = to_gb_interface(dev); |
| 745 | struct gb_svc *svc = intf->hd->svc; |
| 746 | int ret; |
| 747 | |
| 748 | ret = gb_interface_refclk_set(intf, true); |
| 749 | if (ret) |
| 750 | return ret; |
| 751 | |
| 752 | ret = gb_svc_intf_resume(svc, intf->interface_id); |
| 753 | if (ret) |
| 754 | return ret; |
| 755 | |
| 756 | ret = gb_control_resume(intf->control); |
| 757 | if (ret) |
| 758 | return ret; |
| 759 | |
| 760 | ret = gb_timesync_interface_add(intf); |
| 761 | if (ret) { |
| 762 | dev_err(dev, "failed to add to timesync: %d\n", ret); |
| 763 | return ret; |
| 764 | } |
| 765 | |
Bryan O'Donoghue | fddd7ea | 2016-07-15 18:01:49 +0100 | [diff] [blame] | 766 | ret = gb_timesync_schedule_synchronous(intf); |
| 767 | if (ret) { |
| 768 | dev_err(dev, "failed to synchronize FrameTime: %d\n", ret); |
| 769 | return ret; |
| 770 | } |
| 771 | |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static int gb_interface_runtime_idle(struct device *dev) |
| 776 | { |
| 777 | pm_runtime_mark_last_busy(dev); |
| 778 | pm_request_autosuspend(dev); |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | #endif |
| 783 | |
| 784 | static const struct dev_pm_ops gb_interface_pm_ops = { |
| 785 | SET_RUNTIME_PM_OPS(gb_interface_suspend, gb_interface_resume, |
| 786 | gb_interface_runtime_idle) |
| 787 | }; |
| 788 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 789 | struct device_type greybus_interface_type = { |
| 790 | .name = "greybus_interface", |
Viresh Kumar | 51b5d8d | 2015-05-20 17:33:51 +0530 | [diff] [blame] | 791 | .release = gb_interface_release, |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 792 | .pm = &gb_interface_pm_ops, |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 793 | }; |
| 794 | |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 795 | /* |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 796 | * A Greybus module represents a user-replaceable component on a GMP |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 797 | * phone. An interface is the physical connection on that module. A |
| 798 | * module may have more than one interface. |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 799 | * |
Viresh Kumar | c9d9d0d | 2015-04-01 20:31:58 +0530 | [diff] [blame] | 800 | * Create a gb_interface structure to represent a discovered interface. |
| 801 | * The position of interface within the Endo is encoded in "interface_id" |
| 802 | * argument. |
| 803 | * |
Greg Kroah-Hartman | df67155 | 2014-12-21 14:10:26 -0800 | [diff] [blame] | 804 | * Returns a pointer to the new interfce or a null pointer if a |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 805 | * failure occurs due to memory exhaustion. |
| 806 | */ |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 807 | struct gb_interface *gb_interface_create(struct gb_module *module, |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 808 | u8 interface_id) |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 809 | { |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 810 | struct gb_host_device *hd = module->hd; |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 811 | struct gb_interface *intf; |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 812 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 813 | intf = kzalloc(sizeof(*intf), GFP_KERNEL); |
| 814 | if (!intf) |
Johan Hovold | 8b0df4b | 2015-11-25 15:59:04 +0100 | [diff] [blame] | 815 | return NULL; |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 816 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 817 | intf->hd = hd; /* XXX refcount? */ |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 818 | intf->module = module; |
Viresh Kumar | c9d9d0d | 2015-04-01 20:31:58 +0530 | [diff] [blame] | 819 | intf->interface_id = interface_id; |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 820 | INIT_LIST_HEAD(&intf->bundles); |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 821 | INIT_LIST_HEAD(&intf->manifest_descs); |
Johan Hovold | 36602a2 | 2016-04-23 18:47:25 +0200 | [diff] [blame] | 822 | mutex_init(&intf->mutex); |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 823 | INIT_WORK(&intf->mode_switch_work, gb_interface_mode_switch_work); |
| 824 | init_completion(&intf->mode_switch_completion); |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 825 | |
Viresh Kumar | c3add78 | 2015-07-01 12:13:58 +0530 | [diff] [blame] | 826 | /* Invalid device id to start with */ |
Johan Hovold | e9f2f68 | 2016-03-29 18:56:06 -0400 | [diff] [blame] | 827 | intf->device_id = GB_INTERFACE_DEVICE_ID_BAD; |
Viresh Kumar | c3add78 | 2015-07-01 12:13:58 +0530 | [diff] [blame] | 828 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 829 | intf->dev.parent = &module->dev; |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 830 | intf->dev.bus = &greybus_bus_type; |
| 831 | intf->dev.type = &greybus_interface_type; |
| 832 | intf->dev.groups = interface_groups; |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 833 | intf->dev.dma_mask = module->dev.dma_mask; |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 834 | device_initialize(&intf->dev); |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 835 | dev_set_name(&intf->dev, "%s.%u", dev_name(&module->dev), |
| 836 | interface_id); |
Viresh Kumar | 0a68a16 | 2014-11-13 18:14:37 +0530 | [diff] [blame] | 837 | |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 838 | pm_runtime_set_autosuspend_delay(&intf->dev, |
| 839 | GB_INTERFACE_AUTOSUSPEND_MS); |
| 840 | |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 841 | trace_gb_interface_create(intf); |
| 842 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 843 | return intf; |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 844 | } |
| 845 | |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 846 | static int gb_interface_vsys_set(struct gb_interface *intf, bool enable) |
| 847 | { |
| 848 | struct gb_svc *svc = intf->hd->svc; |
| 849 | int ret; |
| 850 | |
| 851 | dev_dbg(&intf->dev, "%s - %d\n", __func__, enable); |
| 852 | |
| 853 | ret = gb_svc_intf_vsys_set(svc, intf->interface_id, enable); |
| 854 | if (ret) { |
Johan Hovold | ae36e81 | 2016-05-25 22:18:14 +0200 | [diff] [blame] | 855 | dev_err(&intf->dev, "failed to set v_sys: %d\n", ret); |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 856 | return ret; |
| 857 | } |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static int gb_interface_refclk_set(struct gb_interface *intf, bool enable) |
| 863 | { |
| 864 | struct gb_svc *svc = intf->hd->svc; |
| 865 | int ret; |
| 866 | |
| 867 | dev_dbg(&intf->dev, "%s - %d\n", __func__, enable); |
| 868 | |
| 869 | ret = gb_svc_intf_refclk_set(svc, intf->interface_id, enable); |
| 870 | if (ret) { |
Johan Hovold | ae36e81 | 2016-05-25 22:18:14 +0200 | [diff] [blame] | 871 | dev_err(&intf->dev, "failed to set refclk: %d\n", ret); |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 872 | return ret; |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | static int gb_interface_unipro_set(struct gb_interface *intf, bool enable) |
| 879 | { |
| 880 | struct gb_svc *svc = intf->hd->svc; |
| 881 | int ret; |
| 882 | |
| 883 | dev_dbg(&intf->dev, "%s - %d\n", __func__, enable); |
| 884 | |
| 885 | ret = gb_svc_intf_unipro_set(svc, intf->interface_id, enable); |
| 886 | if (ret) { |
Johan Hovold | ae36e81 | 2016-05-25 22:18:14 +0200 | [diff] [blame] | 887 | dev_err(&intf->dev, "failed to set UniPro: %d\n", ret); |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 888 | return ret; |
| 889 | } |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 894 | static int gb_interface_activate_operation(struct gb_interface *intf, |
| 895 | enum gb_interface_type *intf_type) |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 896 | { |
| 897 | struct gb_svc *svc = intf->hd->svc; |
| 898 | u8 type; |
| 899 | int ret; |
| 900 | |
| 901 | dev_dbg(&intf->dev, "%s\n", __func__); |
| 902 | |
| 903 | ret = gb_svc_intf_activate(svc, intf->interface_id, &type); |
| 904 | if (ret) { |
| 905 | dev_err(&intf->dev, "failed to activate: %d\n", ret); |
| 906 | return ret; |
| 907 | } |
| 908 | |
| 909 | switch (type) { |
| 910 | case GB_SVC_INTF_TYPE_DUMMY: |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 911 | *intf_type = GB_INTERFACE_TYPE_DUMMY; |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 912 | /* FIXME: handle as an error for now */ |
| 913 | return -ENODEV; |
| 914 | case GB_SVC_INTF_TYPE_UNIPRO: |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 915 | *intf_type = GB_INTERFACE_TYPE_UNIPRO; |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 916 | dev_err(&intf->dev, "interface type UniPro not supported\n"); |
Johan Hovold | 3e93cb6 | 2016-07-20 16:40:22 +0200 | [diff] [blame] | 917 | /* FIXME: handle as an error for now */ |
| 918 | return -ENODEV; |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 919 | case GB_SVC_INTF_TYPE_GREYBUS: |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 920 | *intf_type = GB_INTERFACE_TYPE_GREYBUS; |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 921 | break; |
| 922 | default: |
| 923 | dev_err(&intf->dev, "unknown interface type: %u\n", type); |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 924 | *intf_type = GB_INTERFACE_TYPE_UNKNOWN; |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 925 | return -ENODEV; |
| 926 | } |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | static int gb_interface_hibernate_link(struct gb_interface *intf) |
| 932 | { |
David Lin | cc28c2c | 2016-07-07 22:07:00 -0500 | [diff] [blame] | 933 | struct gb_svc *svc = intf->hd->svc; |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 934 | |
David Lin | cc28c2c | 2016-07-07 22:07:00 -0500 | [diff] [blame] | 935 | return gb_svc_intf_set_power_mode_hibernate(svc, intf->interface_id); |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 936 | } |
| 937 | |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 938 | static int _gb_interface_activate(struct gb_interface *intf, |
| 939 | enum gb_interface_type *type) |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 940 | { |
| 941 | int ret; |
| 942 | |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 943 | *type = GB_INTERFACE_TYPE_UNKNOWN; |
| 944 | |
Johan Hovold | 12169bc | 2016-07-20 16:40:24 +0200 | [diff] [blame] | 945 | if (intf->ejected || intf->removed) |
Johan Hovold | 36602a2 | 2016-04-23 18:47:25 +0200 | [diff] [blame] | 946 | return -ENODEV; |
| 947 | |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 948 | ret = gb_interface_vsys_set(intf, true); |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 949 | if (ret) |
| 950 | return ret; |
| 951 | |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 952 | ret = gb_interface_refclk_set(intf, true); |
| 953 | if (ret) |
| 954 | goto err_vsys_disable; |
| 955 | |
| 956 | ret = gb_interface_unipro_set(intf, true); |
| 957 | if (ret) |
| 958 | goto err_refclk_disable; |
| 959 | |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 960 | ret = gb_interface_activate_operation(intf, type); |
Johan Hovold | 4f7e413 | 2016-07-20 16:40:27 +0200 | [diff] [blame] | 961 | if (ret) { |
| 962 | switch (*type) { |
| 963 | case GB_INTERFACE_TYPE_UNIPRO: |
| 964 | case GB_INTERFACE_TYPE_GREYBUS: |
| 965 | goto err_hibernate_link; |
| 966 | default: |
| 967 | goto err_unipro_disable; |
| 968 | } |
| 969 | } |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 970 | |
| 971 | ret = gb_interface_read_dme(intf); |
| 972 | if (ret) |
| 973 | goto err_hibernate_link; |
| 974 | |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 975 | ret = gb_interface_route_create(intf); |
| 976 | if (ret) |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 977 | goto err_hibernate_link; |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 978 | |
Johan Hovold | 1e1565e | 2016-04-23 18:47:26 +0200 | [diff] [blame] | 979 | intf->active = true; |
| 980 | |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 981 | trace_gb_interface_activate(intf); |
| 982 | |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 983 | return 0; |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 984 | |
| 985 | err_hibernate_link: |
| 986 | gb_interface_hibernate_link(intf); |
| 987 | err_unipro_disable: |
| 988 | gb_interface_unipro_set(intf, false); |
| 989 | err_refclk_disable: |
| 990 | gb_interface_refclk_set(intf, false); |
| 991 | err_vsys_disable: |
| 992 | gb_interface_vsys_set(intf, false); |
| 993 | |
| 994 | return ret; |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 995 | } |
| 996 | |
Johan Hovold | 36602a2 | 2016-04-23 18:47:25 +0200 | [diff] [blame] | 997 | /* |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 998 | * At present, we assume a UniPro-only module to be a Greybus module that |
| 999 | * failed to send its mailbox poke. There is some reason to believe that this |
| 1000 | * is because of a bug in the ES3 bootrom. |
| 1001 | * |
| 1002 | * FIXME: Check if this is a Toshiba bridge before retrying? |
| 1003 | */ |
| 1004 | static int _gb_interface_activate_es3_hack(struct gb_interface *intf, |
| 1005 | enum gb_interface_type *type) |
| 1006 | { |
| 1007 | int retries = 3; |
| 1008 | int ret; |
| 1009 | |
| 1010 | while (retries--) { |
| 1011 | ret = _gb_interface_activate(intf, type); |
| 1012 | if (ret == -ENODEV && *type == GB_INTERFACE_TYPE_UNIPRO) |
| 1013 | continue; |
| 1014 | |
| 1015 | break; |
| 1016 | } |
| 1017 | |
| 1018 | return ret; |
| 1019 | } |
| 1020 | |
| 1021 | /* |
Johan Hovold | 3e93cb6 | 2016-07-20 16:40:22 +0200 | [diff] [blame] | 1022 | * Activate an interface. |
| 1023 | * |
| 1024 | * Locking: Caller holds the interface mutex. |
| 1025 | */ |
| 1026 | int gb_interface_activate(struct gb_interface *intf) |
| 1027 | { |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 1028 | enum gb_interface_type type; |
Johan Hovold | 3e93cb6 | 2016-07-20 16:40:22 +0200 | [diff] [blame] | 1029 | int ret; |
| 1030 | |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 1031 | switch (intf->type) { |
| 1032 | case GB_INTERFACE_TYPE_INVALID: |
| 1033 | case GB_INTERFACE_TYPE_GREYBUS: |
| 1034 | ret = _gb_interface_activate_es3_hack(intf, &type); |
Johan Hovold | 3e93cb6 | 2016-07-20 16:40:22 +0200 | [diff] [blame] | 1035 | break; |
Johan Hovold | 6249162 | 2016-07-20 16:40:23 +0200 | [diff] [blame] | 1036 | default: |
| 1037 | ret = _gb_interface_activate(intf, &type); |
| 1038 | } |
| 1039 | |
| 1040 | /* Make sure type is detected correctly during reactivation. */ |
| 1041 | if (intf->type != GB_INTERFACE_TYPE_INVALID) { |
| 1042 | if (type != intf->type) { |
| 1043 | dev_err(&intf->dev, "failed to detect interface type\n"); |
| 1044 | |
| 1045 | if (!ret) |
| 1046 | gb_interface_deactivate(intf); |
| 1047 | |
| 1048 | return -EIO; |
| 1049 | } |
| 1050 | } else { |
| 1051 | intf->type = type; |
Johan Hovold | 3e93cb6 | 2016-07-20 16:40:22 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
Johan Hovold | 36602a2 | 2016-04-23 18:47:25 +0200 | [diff] [blame] | 1058 | * Deactivate an interface. |
| 1059 | * |
| 1060 | * Locking: Caller holds the interface mutex. |
| 1061 | */ |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 1062 | void gb_interface_deactivate(struct gb_interface *intf) |
| 1063 | { |
Johan Hovold | 1e1565e | 2016-04-23 18:47:26 +0200 | [diff] [blame] | 1064 | if (!intf->active) |
| 1065 | return; |
| 1066 | |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 1067 | trace_gb_interface_deactivate(intf); |
| 1068 | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 1069 | /* Abort any ongoing mode switch. */ |
| 1070 | if (intf->mode_switch) |
| 1071 | complete(&intf->mode_switch_completion); |
| 1072 | |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 1073 | gb_interface_route_destroy(intf); |
Johan Hovold | ec562f2 | 2016-04-23 18:47:29 +0200 | [diff] [blame] | 1074 | gb_interface_hibernate_link(intf); |
| 1075 | gb_interface_unipro_set(intf, false); |
| 1076 | gb_interface_refclk_set(intf, false); |
| 1077 | gb_interface_vsys_set(intf, false); |
Johan Hovold | 1e1565e | 2016-04-23 18:47:26 +0200 | [diff] [blame] | 1078 | |
| 1079 | intf->active = false; |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 1080 | } |
| 1081 | |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 1082 | /* |
Johan Hovold | 96fb6c3 | 2016-04-13 19:19:08 +0200 | [diff] [blame] | 1083 | * Enable an interface by enabling its control connection, fetching the |
| 1084 | * manifest and other information over it, and finally registering its child |
| 1085 | * devices. |
Johan Hovold | 36602a2 | 2016-04-23 18:47:25 +0200 | [diff] [blame] | 1086 | * |
| 1087 | * Locking: Caller holds the interface mutex. |
Viresh Kumar | 676daaf | 2014-11-14 17:25:07 +0530 | [diff] [blame] | 1088 | */ |
Johan Hovold | 35580af | 2016-03-09 12:20:42 +0100 | [diff] [blame] | 1089 | int gb_interface_enable(struct gb_interface *intf) |
Viresh Kumar | 676daaf | 2014-11-14 17:25:07 +0530 | [diff] [blame] | 1090 | { |
Johan Hovold | 4960583 | 2016-04-13 19:19:06 +0200 | [diff] [blame] | 1091 | struct gb_control *control; |
Johan Hovold | a77660a | 2016-03-09 12:20:37 +0100 | [diff] [blame] | 1092 | struct gb_bundle *bundle, *tmp; |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1093 | int ret, size; |
| 1094 | void *manifest; |
Viresh Kumar | 676daaf | 2014-11-14 17:25:07 +0530 | [diff] [blame] | 1095 | |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 1096 | ret = gb_interface_read_and_clear_init_status(intf); |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 1097 | if (ret) { |
Johan Hovold | 133e366 | 2016-03-29 18:56:02 -0400 | [diff] [blame] | 1098 | dev_err(&intf->dev, "failed to clear init status: %d\n", ret); |
Johan Hovold | c2d8090 | 2016-03-29 18:56:00 -0400 | [diff] [blame] | 1099 | return ret; |
| 1100 | } |
| 1101 | |
Johan Hovold | c634650 | 2015-12-15 15:28:56 +0100 | [diff] [blame] | 1102 | /* Establish control connection */ |
Johan Hovold | 4960583 | 2016-04-13 19:19:06 +0200 | [diff] [blame] | 1103 | control = gb_control_create(intf); |
| 1104 | if (IS_ERR(control)) { |
Johan Hovold | ae0f645 | 2016-05-09 14:40:09 +0200 | [diff] [blame] | 1105 | dev_err(&intf->dev, "failed to create control device: %ld\n", |
Johan Hovold | 4960583 | 2016-04-13 19:19:06 +0200 | [diff] [blame] | 1106 | PTR_ERR(control)); |
| 1107 | return PTR_ERR(control); |
| 1108 | } |
| 1109 | intf->control = control; |
| 1110 | |
Johan Hovold | c634650 | 2015-12-15 15:28:56 +0100 | [diff] [blame] | 1111 | ret = gb_control_enable(intf->control); |
| 1112 | if (ret) |
Johan Hovold | 4960583 | 2016-04-13 19:19:06 +0200 | [diff] [blame] | 1113 | goto err_put_control; |
Johan Hovold | 0bf1f24 | 2015-12-07 15:05:34 +0100 | [diff] [blame] | 1114 | |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1115 | /* Get manifest size using control protocol on CPort */ |
| 1116 | size = gb_control_get_manifest_size_operation(intf); |
| 1117 | if (size <= 0) { |
Johan Hovold | 5626e0b | 2015-12-07 15:05:46 +0100 | [diff] [blame] | 1118 | dev_err(&intf->dev, "failed to get manifest size: %d\n", size); |
Johan Hovold | 11548c8 | 2016-03-09 12:20:39 +0100 | [diff] [blame] | 1119 | |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1120 | if (size) |
Johan Hovold | 11548c8 | 2016-03-09 12:20:39 +0100 | [diff] [blame] | 1121 | ret = size; |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1122 | else |
Johan Hovold | 11548c8 | 2016-03-09 12:20:39 +0100 | [diff] [blame] | 1123 | ret = -EINVAL; |
| 1124 | |
| 1125 | goto err_disable_control; |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | manifest = kmalloc(size, GFP_KERNEL); |
Johan Hovold | 11548c8 | 2016-03-09 12:20:39 +0100 | [diff] [blame] | 1129 | if (!manifest) { |
| 1130 | ret = -ENOMEM; |
| 1131 | goto err_disable_control; |
| 1132 | } |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1133 | |
| 1134 | /* Get manifest using control protocol on CPort */ |
| 1135 | ret = gb_control_get_manifest_operation(intf, manifest, size); |
| 1136 | if (ret) { |
Johan Hovold | 5626e0b | 2015-12-07 15:05:46 +0100 | [diff] [blame] | 1137 | dev_err(&intf->dev, "failed to get manifest: %d\n", ret); |
Johan Hovold | a77660a | 2016-03-09 12:20:37 +0100 | [diff] [blame] | 1138 | goto err_free_manifest; |
Viresh Kumar | 676daaf | 2014-11-14 17:25:07 +0530 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /* |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1142 | * Parse the manifest and build up our data structures representing |
| 1143 | * what's in it. |
Viresh Kumar | 676daaf | 2014-11-14 17:25:07 +0530 | [diff] [blame] | 1144 | */ |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1145 | if (!gb_manifest_parse(intf, manifest, size)) { |
Johan Hovold | 5626e0b | 2015-12-07 15:05:46 +0100 | [diff] [blame] | 1146 | dev_err(&intf->dev, "failed to parse manifest\n"); |
Viresh Kumar | 6c68da2 | 2015-06-22 16:42:27 +0530 | [diff] [blame] | 1147 | ret = -EINVAL; |
Johan Hovold | a77660a | 2016-03-09 12:20:37 +0100 | [diff] [blame] | 1148 | goto err_destroy_bundles; |
Viresh Kumar | 676daaf | 2014-11-14 17:25:07 +0530 | [diff] [blame] | 1149 | } |
| 1150 | |
Johan Hovold | b807aa7 | 2016-01-19 12:51:21 +0100 | [diff] [blame] | 1151 | ret = gb_control_get_bundle_versions(intf->control); |
| 1152 | if (ret) |
Johan Hovold | a77660a | 2016-03-09 12:20:37 +0100 | [diff] [blame] | 1153 | goto err_destroy_bundles; |
Johan Hovold | b807aa7 | 2016-01-19 12:51:21 +0100 | [diff] [blame] | 1154 | |
Bryan O'Donoghue | 4a44842 | 2016-06-05 14:03:27 +0100 | [diff] [blame] | 1155 | ret = gb_timesync_interface_add(intf); |
| 1156 | if (ret) { |
| 1157 | dev_err(&intf->dev, "failed to add to timesync: %d\n", ret); |
David Lin | 4523eae | 2016-08-05 15:08:40 -0700 | [diff] [blame] | 1158 | goto err_destroy_bundles; |
Bryan O'Donoghue | 4a44842 | 2016-06-05 14:03:27 +0100 | [diff] [blame] | 1159 | } |
| 1160 | |
David Lin | 4523eae | 2016-08-05 15:08:40 -0700 | [diff] [blame] | 1161 | /* Register the control device and any bundles */ |
| 1162 | ret = gb_control_add(intf->control); |
| 1163 | if (ret) |
| 1164 | goto err_remove_timesync; |
| 1165 | |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 1166 | pm_runtime_use_autosuspend(&intf->dev); |
| 1167 | pm_runtime_get_noresume(&intf->dev); |
| 1168 | pm_runtime_set_active(&intf->dev); |
| 1169 | pm_runtime_enable(&intf->dev); |
| 1170 | |
Johan Hovold | 96fb6c3 | 2016-04-13 19:19:08 +0200 | [diff] [blame] | 1171 | list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) { |
| 1172 | ret = gb_bundle_add(bundle); |
| 1173 | if (ret) { |
| 1174 | gb_bundle_destroy(bundle); |
| 1175 | continue; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Johan Hovold | a77660a | 2016-03-09 12:20:37 +0100 | [diff] [blame] | 1179 | kfree(manifest); |
| 1180 | |
Johan Hovold | 4960583 | 2016-04-13 19:19:06 +0200 | [diff] [blame] | 1181 | intf->enabled = true; |
| 1182 | |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 1183 | pm_runtime_put(&intf->dev); |
| 1184 | |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 1185 | trace_gb_interface_enable(intf); |
| 1186 | |
Johan Hovold | a77660a | 2016-03-09 12:20:37 +0100 | [diff] [blame] | 1187 | return 0; |
| 1188 | |
David Lin | 4523eae | 2016-08-05 15:08:40 -0700 | [diff] [blame] | 1189 | err_remove_timesync: |
| 1190 | gb_timesync_interface_remove(intf); |
Johan Hovold | a77660a | 2016-03-09 12:20:37 +0100 | [diff] [blame] | 1191 | err_destroy_bundles: |
| 1192 | list_for_each_entry_safe(bundle, tmp, &intf->bundles, links) |
| 1193 | gb_bundle_destroy(bundle); |
| 1194 | err_free_manifest: |
Johan Hovold | 7a137fb | 2016-03-09 12:20:36 +0100 | [diff] [blame] | 1195 | kfree(manifest); |
Johan Hovold | 11548c8 | 2016-03-09 12:20:39 +0100 | [diff] [blame] | 1196 | err_disable_control: |
| 1197 | gb_control_disable(intf->control); |
Johan Hovold | 4960583 | 2016-04-13 19:19:06 +0200 | [diff] [blame] | 1198 | err_put_control: |
| 1199 | gb_control_put(intf->control); |
| 1200 | intf->control = NULL; |
Johan Hovold | 7a137fb | 2016-03-09 12:20:36 +0100 | [diff] [blame] | 1201 | |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
Johan Hovold | 36602a2 | 2016-04-23 18:47:25 +0200 | [diff] [blame] | 1205 | /* |
| 1206 | * Disable an interface and destroy its bundles. |
| 1207 | * |
| 1208 | * Locking: Caller holds the interface mutex. |
| 1209 | */ |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1210 | void gb_interface_disable(struct gb_interface *intf) |
| 1211 | { |
| 1212 | struct gb_bundle *bundle; |
| 1213 | struct gb_bundle *next; |
| 1214 | |
Johan Hovold | 4960583 | 2016-04-13 19:19:06 +0200 | [diff] [blame] | 1215 | if (!intf->enabled) |
| 1216 | return; |
| 1217 | |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 1218 | trace_gb_interface_disable(intf); |
| 1219 | |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 1220 | pm_runtime_get_sync(&intf->dev); |
| 1221 | |
Johan Hovold | d9fa349 | 2016-05-27 17:26:24 +0200 | [diff] [blame] | 1222 | /* Set disconnected flag to avoid I/O during connection tear down. */ |
| 1223 | if (intf->quirks & GB_INTERFACE_QUIRK_FORCED_DISABLE) |
| 1224 | intf->disconnected = true; |
| 1225 | |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1226 | list_for_each_entry_safe(bundle, next, &intf->bundles, links) |
| 1227 | gb_bundle_destroy(bundle); |
| 1228 | |
David Lin | 576bffb | 2016-07-07 22:07:00 -0500 | [diff] [blame] | 1229 | if (!intf->mode_switch && !intf->disconnected) |
| 1230 | gb_control_interface_deactivate_prepare(intf->control); |
| 1231 | |
Johan Hovold | 7326e07 | 2016-04-13 19:19:03 +0200 | [diff] [blame] | 1232 | gb_control_del(intf->control); |
David Lin | 4523eae | 2016-08-05 15:08:40 -0700 | [diff] [blame] | 1233 | gb_timesync_interface_remove(intf); |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1234 | gb_control_disable(intf->control); |
Johan Hovold | 4960583 | 2016-04-13 19:19:06 +0200 | [diff] [blame] | 1235 | gb_control_put(intf->control); |
| 1236 | intf->control = NULL; |
| 1237 | |
| 1238 | intf->enabled = false; |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 1239 | |
| 1240 | pm_runtime_disable(&intf->dev); |
| 1241 | pm_runtime_set_suspended(&intf->dev); |
| 1242 | pm_runtime_dont_use_autosuspend(&intf->dev); |
| 1243 | pm_runtime_put_noidle(&intf->dev); |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1244 | } |
| 1245 | |
Bryan O'Donoghue | 1211915 | 2016-06-23 16:26:00 +0100 | [diff] [blame] | 1246 | /* Enable TimeSync on an Interface control connection. */ |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 1247 | int gb_interface_timesync_enable(struct gb_interface *intf, u8 count, |
| 1248 | u64 frame_time, u32 strobe_delay, u32 refclk) |
| 1249 | { |
Bryan O'Donoghue | 1211915 | 2016-06-23 16:26:00 +0100 | [diff] [blame] | 1250 | return gb_control_timesync_enable(intf->control, count, |
| 1251 | frame_time, strobe_delay, |
| 1252 | refclk); |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
Bryan O'Donoghue | 1211915 | 2016-06-23 16:26:00 +0100 | [diff] [blame] | 1255 | /* Disable TimeSync on an Interface control connection. */ |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 1256 | int gb_interface_timesync_disable(struct gb_interface *intf) |
| 1257 | { |
Bryan O'Donoghue | 1211915 | 2016-06-23 16:26:00 +0100 | [diff] [blame] | 1258 | return gb_control_timesync_disable(intf->control); |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
Bryan O'Donoghue | 1211915 | 2016-06-23 16:26:00 +0100 | [diff] [blame] | 1261 | /* Transmit the Authoritative FrameTime via an Interface control connection. */ |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 1262 | int gb_interface_timesync_authoritative(struct gb_interface *intf, |
| 1263 | u64 *frame_time) |
| 1264 | { |
Bryan O'Donoghue | 1211915 | 2016-06-23 16:26:00 +0100 | [diff] [blame] | 1265 | return gb_control_timesync_authoritative(intf->control, |
| 1266 | frame_time); |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 1267 | } |
| 1268 | |
Johan Hovold | 96fb6c3 | 2016-04-13 19:19:08 +0200 | [diff] [blame] | 1269 | /* Register an interface. */ |
Johan Hovold | 7a137fb | 2016-03-09 12:20:36 +0100 | [diff] [blame] | 1270 | int gb_interface_add(struct gb_interface *intf) |
| 1271 | { |
Johan Hovold | 7a137fb | 2016-03-09 12:20:36 +0100 | [diff] [blame] | 1272 | int ret; |
| 1273 | |
Johan Hovold | ab66dd7 | 2015-12-07 15:05:45 +0100 | [diff] [blame] | 1274 | ret = device_add(&intf->dev); |
| 1275 | if (ret) { |
| 1276 | dev_err(&intf->dev, "failed to register interface: %d\n", ret); |
Johan Hovold | 7a137fb | 2016-03-09 12:20:36 +0100 | [diff] [blame] | 1277 | return ret; |
Johan Hovold | ab66dd7 | 2015-12-07 15:05:45 +0100 | [diff] [blame] | 1278 | } |
| 1279 | |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 1280 | trace_gb_interface_add(intf); |
| 1281 | |
Johan Hovold | c80a982 | 2016-07-19 15:24:49 +0200 | [diff] [blame] | 1282 | dev_info(&intf->dev, "Interface added (%s)\n", |
| 1283 | gb_interface_type_string(intf)); |
| 1284 | |
| 1285 | switch (intf->type) { |
Johan Hovold | a212b75 | 2016-07-20 16:40:20 +0200 | [diff] [blame] | 1286 | case GB_INTERFACE_TYPE_GREYBUS: |
Johan Hovold | 23931ff | 2016-07-28 11:40:53 +0200 | [diff] [blame] | 1287 | dev_info(&intf->dev, "GMP VID=0x%08x, PID=0x%08x\n", |
Johan Hovold | c80a982 | 2016-07-19 15:24:49 +0200 | [diff] [blame] | 1288 | intf->vendor_id, intf->product_id); |
| 1289 | /* fall-through */ |
Johan Hovold | a212b75 | 2016-07-20 16:40:20 +0200 | [diff] [blame] | 1290 | case GB_INTERFACE_TYPE_UNIPRO: |
Johan Hovold | c80a982 | 2016-07-19 15:24:49 +0200 | [diff] [blame] | 1291 | dev_info(&intf->dev, "DDBL1 Manufacturer=0x%08x, Product=0x%08x\n", |
| 1292 | intf->ddbl1_manufacturer_id, |
| 1293 | intf->ddbl1_product_id); |
| 1294 | break; |
Johan Hovold | a212b75 | 2016-07-20 16:40:20 +0200 | [diff] [blame] | 1295 | default: |
| 1296 | break; |
Johan Hovold | c80a982 | 2016-07-19 15:24:49 +0200 | [diff] [blame] | 1297 | } |
Viresh Kumar | 907d1e1 | 2016-02-19 15:57:46 +0530 | [diff] [blame] | 1298 | |
Johan Hovold | 7a137fb | 2016-03-09 12:20:36 +0100 | [diff] [blame] | 1299 | return 0; |
Viresh Kumar | 676daaf | 2014-11-14 17:25:07 +0530 | [diff] [blame] | 1300 | } |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1301 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 1302 | /* Deregister an interface. */ |
| 1303 | void gb_interface_del(struct gb_interface *intf) |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1304 | { |
| 1305 | if (device_is_registered(&intf->dev)) { |
Alex Elder | cb4c8441 | 2016-05-23 23:05:32 -0500 | [diff] [blame] | 1306 | trace_gb_interface_del(intf); |
| 1307 | |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1308 | device_del(&intf->dev); |
| 1309 | dev_info(&intf->dev, "Interface removed\n"); |
| 1310 | } |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 1311 | } |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1312 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 1313 | void gb_interface_put(struct gb_interface *intf) |
| 1314 | { |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1315 | put_device(&intf->dev); |
| 1316 | } |