1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Motorola Mapphone MDM6600 modem GPIO controlled USB PHY driver
4  * Copyright (C) 2018 Tony Lindgren <tony@atomide.com>
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 
16 #include <linux/gpio/consumer.h>
17 #include <linux/of_platform.h>
18 #include <linux/phy/phy.h>
19 #include <linux/pinctrl/consumer.h>
20 
21 #define PHY_MDM6600_PHY_DELAY_MS	4000	/* PHY enable 2.2s to 3.5s */
22 #define PHY_MDM6600_ENABLED_DELAY_MS	8000	/* 8s more total for MDM6600 */
23 #define PHY_MDM6600_WAKE_KICK_MS	600	/* time on after GPIO toggle */
24 #define MDM6600_MODEM_IDLE_DELAY_MS	1000	/* modem after USB suspend */
25 #define MDM6600_MODEM_WAKE_DELAY_MS	200	/* modem response after idle */
26 
27 enum phy_mdm6600_ctrl_lines {
28 	PHY_MDM6600_ENABLE,			/* USB PHY enable */
29 	PHY_MDM6600_POWER,			/* Device power */
30 	PHY_MDM6600_RESET,			/* Device reset */
31 	PHY_MDM6600_NR_CTRL_LINES,
32 };
33 
34 enum phy_mdm6600_bootmode_lines {
35 	PHY_MDM6600_MODE0,			/* out USB mode0 and OOB wake */
36 	PHY_MDM6600_MODE1,			/* out USB mode1, in OOB wake */
37 	PHY_MDM6600_NR_MODE_LINES,
38 };
39 
40 enum phy_mdm6600_cmd_lines {
41 	PHY_MDM6600_CMD0,
42 	PHY_MDM6600_CMD1,
43 	PHY_MDM6600_CMD2,
44 	PHY_MDM6600_NR_CMD_LINES,
45 };
46 
47 enum phy_mdm6600_status_lines {
48 	PHY_MDM6600_STATUS0,
49 	PHY_MDM6600_STATUS1,
50 	PHY_MDM6600_STATUS2,
51 	PHY_MDM6600_NR_STATUS_LINES,
52 };
53 
54 /*
55  * MDM6600 command codes. These are based on Motorola Mapphone Linux
56  * kernel tree.
57  */
58 enum phy_mdm6600_cmd {
59 	PHY_MDM6600_CMD_BP_PANIC_ACK,
60 	PHY_MDM6600_CMD_DATA_ONLY_BYPASS,	/* Reroute USB to CPCAP PHY */
61 	PHY_MDM6600_CMD_FULL_BYPASS,		/* Reroute USB to CPCAP PHY */
62 	PHY_MDM6600_CMD_NO_BYPASS,		/* Request normal USB mode */
63 	PHY_MDM6600_CMD_BP_SHUTDOWN_REQ,	/* Request device power off */
64 	PHY_MDM6600_CMD_BP_UNKNOWN_5,
65 	PHY_MDM6600_CMD_BP_UNKNOWN_6,
66 	PHY_MDM6600_CMD_UNDEFINED,
67 };
68 
69 /*
70  * MDM6600 status codes. These are based on Motorola Mapphone Linux
71  * kernel tree.
72  */
73 enum phy_mdm6600_status {
74 	PHY_MDM6600_STATUS_PANIC,		/* Seems to be really off */
75 	PHY_MDM6600_STATUS_PANIC_BUSY_WAIT,
76 	PHY_MDM6600_STATUS_QC_DLOAD,
77 	PHY_MDM6600_STATUS_RAM_DOWNLOADER,	/* MDM6600 USB flashing mode */
78 	PHY_MDM6600_STATUS_PHONE_CODE_AWAKE,	/* MDM6600 normal USB mode */
79 	PHY_MDM6600_STATUS_PHONE_CODE_ASLEEP,
80 	PHY_MDM6600_STATUS_SHUTDOWN_ACK,
81 	PHY_MDM6600_STATUS_UNDEFINED,
82 };
83 
84 static const char * const
85 phy_mdm6600_status_name[] = {
86 	"off", "busy", "qc_dl", "ram_dl", "awake",
87 	"asleep", "shutdown", "undefined",
88 };
89 
90 struct phy_mdm6600 {
91 	struct device *dev;
92 	struct phy *generic_phy;
93 	struct phy_provider *phy_provider;
94 	struct gpio_desc *ctrl_gpios[PHY_MDM6600_NR_CTRL_LINES];
95 	struct gpio_descs *mode_gpios;
96 	struct gpio_descs *status_gpios;
97 	struct gpio_descs *cmd_gpios;
98 	struct delayed_work bootup_work;
99 	struct delayed_work status_work;
100 	struct delayed_work modem_wake_work;
101 	struct completion ack;
102 	bool enabled;				/* mdm6600 phy enabled */
103 	bool running;				/* mdm6600 boot done */
104 	bool awake;				/* mdm6600 respnds on n_gsm */
105 	int status;
106 };
107 
phy_mdm6600_init(struct phy * x)108 static int phy_mdm6600_init(struct phy *x)
109 {
110 	struct phy_mdm6600 *ddata = phy_get_drvdata(x);
111 	struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
112 
113 	if (!ddata->enabled)
114 		return -EPROBE_DEFER;
115 
116 	gpiod_set_value_cansleep(enable_gpio, 0);
117 
118 	return 0;
119 }
120 
phy_mdm6600_power_on(struct phy * x)121 static int phy_mdm6600_power_on(struct phy *x)
122 {
123 	struct phy_mdm6600 *ddata = phy_get_drvdata(x);
124 	struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
125 
126 	if (!ddata->enabled)
127 		return -ENODEV;
128 
129 	gpiod_set_value_cansleep(enable_gpio, 1);
130 
131 	/* Allow aggressive PM for USB, it's only needed for n_gsm port */
132 	if (pm_runtime_enabled(&x->dev))
133 		phy_pm_runtime_put(x);
134 
135 	return 0;
136 }
137 
phy_mdm6600_power_off(struct phy * x)138 static int phy_mdm6600_power_off(struct phy *x)
139 {
140 	struct phy_mdm6600 *ddata = phy_get_drvdata(x);
141 	struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
142 	int error;
143 
144 	if (!ddata->enabled)
145 		return -ENODEV;
146 
147 	/* Paired with phy_pm_runtime_put() in phy_mdm6600_power_on() */
148 	if (pm_runtime_enabled(&x->dev)) {
149 		error = phy_pm_runtime_get(x);
150 		if (error < 0 && error != -EINPROGRESS)
151 			dev_warn(ddata->dev, "%s: phy_pm_runtime_get: %i\n",
152 				 __func__, error);
153 	}
154 
155 	gpiod_set_value_cansleep(enable_gpio, 0);
156 
157 	return 0;
158 }
159 
160 static const struct phy_ops gpio_usb_ops = {
161 	.init = phy_mdm6600_init,
162 	.power_on = phy_mdm6600_power_on,
163 	.power_off = phy_mdm6600_power_off,
164 	.owner = THIS_MODULE,
165 };
166 
167 /**
168  * phy_mdm6600_cmd() - send a command request to mdm6600
169  * @ddata: device driver data
170  * @val: value of cmd to be set
171  *
172  * Configures the three command request GPIOs to the specified value.
173  */
phy_mdm6600_cmd(struct phy_mdm6600 * ddata,int val)174 static void phy_mdm6600_cmd(struct phy_mdm6600 *ddata, int val)
175 {
176 	DECLARE_BITMAP(values, PHY_MDM6600_NR_CMD_LINES);
177 
178 	values[0] = val;
179 
180 	gpiod_multi_set_value_cansleep(ddata->cmd_gpios, values);
181 }
182 
183 /**
184  * phy_mdm6600_status() - read mdm6600 status lines
185  * @work: work structure
186  */
phy_mdm6600_status(struct work_struct * work)187 static void phy_mdm6600_status(struct work_struct *work)
188 {
189 	struct phy_mdm6600 *ddata;
190 	struct device *dev;
191 	DECLARE_BITMAP(values, PHY_MDM6600_NR_STATUS_LINES);
192 	int error;
193 
194 	ddata = container_of(work, struct phy_mdm6600, status_work.work);
195 	dev = ddata->dev;
196 
197 	error = gpiod_get_array_value_cansleep(PHY_MDM6600_NR_STATUS_LINES,
198 					       ddata->status_gpios->desc,
199 					       ddata->status_gpios->info,
200 					       values);
201 	if (error)
202 		return;
203 
204 	ddata->status = values[0] & ((1 << PHY_MDM6600_NR_STATUS_LINES) - 1);
205 
206 	dev_info(dev, "modem status: %i %s\n",
207 		 ddata->status,
208 		 phy_mdm6600_status_name[ddata->status]);
209 	complete(&ddata->ack);
210 }
211 
phy_mdm6600_irq_thread(int irq,void * data)212 static irqreturn_t phy_mdm6600_irq_thread(int irq, void *data)
213 {
214 	struct phy_mdm6600 *ddata = data;
215 
216 	schedule_delayed_work(&ddata->status_work, msecs_to_jiffies(10));
217 
218 	return IRQ_HANDLED;
219 }
220 
221 /**
222  * phy_mdm6600_wakeirq_thread - handle mode1 line OOB wake after booting
223  * @irq: interrupt
224  * @data: interrupt handler data
225  *
226  * GPIO mode1 is used initially as output to configure the USB boot
227  * mode for mdm6600. After booting it is used as input for OOB wake
228  * signal from mdm6600 to the SoC. Just use it for debug info only
229  * for now.
230  */
phy_mdm6600_wakeirq_thread(int irq,void * data)231 static irqreturn_t phy_mdm6600_wakeirq_thread(int irq, void *data)
232 {
233 	struct phy_mdm6600 *ddata = data;
234 	struct gpio_desc *mode_gpio1;
235 	int error, wakeup;
236 
237 	mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
238 	wakeup = gpiod_get_value(mode_gpio1);
239 	if (!wakeup)
240 		return IRQ_NONE;
241 
242 	dev_dbg(ddata->dev, "OOB wake on mode_gpio1: %i\n", wakeup);
243 	error = pm_runtime_get_sync(ddata->dev);
244 	if (error < 0) {
245 		pm_runtime_put_noidle(ddata->dev);
246 
247 		return IRQ_NONE;
248 	}
249 
250 	/* Just wake-up and kick the autosuspend timer */
251 	pm_runtime_mark_last_busy(ddata->dev);
252 	pm_runtime_put_autosuspend(ddata->dev);
253 
254 	return IRQ_HANDLED;
255 }
256 
257 /**
258  * phy_mdm6600_init_irq() - initialize mdm6600 status IRQ lines
259  * @ddata: device driver data
260  */
phy_mdm6600_init_irq(struct phy_mdm6600 * ddata)261 static void phy_mdm6600_init_irq(struct phy_mdm6600 *ddata)
262 {
263 	struct device *dev = ddata->dev;
264 	int i, error, irq;
265 
266 	for (i = PHY_MDM6600_STATUS0;
267 	     i <= PHY_MDM6600_STATUS2; i++) {
268 		struct gpio_desc *gpio = ddata->status_gpios->desc[i];
269 
270 		irq = gpiod_to_irq(gpio);
271 		if (irq <= 0)
272 			continue;
273 
274 		error = devm_request_threaded_irq(dev, irq, NULL,
275 					phy_mdm6600_irq_thread,
276 					IRQF_TRIGGER_RISING |
277 					IRQF_TRIGGER_FALLING |
278 					IRQF_ONESHOT,
279 					"mdm6600",
280 					ddata);
281 		if (error)
282 			dev_warn(dev, "no modem status irq%i: %i\n",
283 				 irq, error);
284 	}
285 }
286 
287 struct phy_mdm6600_map {
288 	const char *name;
289 	int direction;
290 };
291 
292 static const struct phy_mdm6600_map
293 phy_mdm6600_ctrl_gpio_map[PHY_MDM6600_NR_CTRL_LINES] = {
294 	{ "enable", GPIOD_OUT_LOW, },		/* low = phy disabled */
295 	{ "power", GPIOD_OUT_LOW, },		/* low = off */
296 	{ "reset", GPIOD_OUT_HIGH, },		/* high = reset */
297 };
298 
299 /**
300  * phy_mdm6600_init_lines() - initialize mdm6600 GPIO lines
301  * @ddata: device driver data
302  */
phy_mdm6600_init_lines(struct phy_mdm6600 * ddata)303 static int phy_mdm6600_init_lines(struct phy_mdm6600 *ddata)
304 {
305 	struct device *dev = ddata->dev;
306 	int i;
307 
308 	/* MDM6600 control lines */
309 	for (i = 0; i < ARRAY_SIZE(phy_mdm6600_ctrl_gpio_map); i++) {
310 		const struct phy_mdm6600_map *map =
311 			&phy_mdm6600_ctrl_gpio_map[i];
312 		struct gpio_desc **gpio = &ddata->ctrl_gpios[i];
313 
314 		*gpio = devm_gpiod_get(dev, map->name, map->direction);
315 		if (IS_ERR(*gpio)) {
316 			dev_info(dev, "gpio %s error %li\n",
317 				 map->name, PTR_ERR(*gpio));
318 			return PTR_ERR(*gpio);
319 		}
320 	}
321 
322 	/* MDM6600 USB start-up mode output lines */
323 	ddata->mode_gpios = devm_gpiod_get_array(dev, "motorola,mode",
324 						 GPIOD_OUT_LOW);
325 	if (IS_ERR(ddata->mode_gpios))
326 		return PTR_ERR(ddata->mode_gpios);
327 
328 	if (ddata->mode_gpios->ndescs != PHY_MDM6600_NR_MODE_LINES)
329 		return -EINVAL;
330 
331 	/* MDM6600 status input lines */
332 	ddata->status_gpios = devm_gpiod_get_array(dev, "motorola,status",
333 						   GPIOD_IN);
334 	if (IS_ERR(ddata->status_gpios))
335 		return PTR_ERR(ddata->status_gpios);
336 
337 	if (ddata->status_gpios->ndescs != PHY_MDM6600_NR_STATUS_LINES)
338 		return -EINVAL;
339 
340 	/* MDM6600 cmd output lines */
341 	ddata->cmd_gpios = devm_gpiod_get_array(dev, "motorola,cmd",
342 						GPIOD_OUT_LOW);
343 	if (IS_ERR(ddata->cmd_gpios))
344 		return PTR_ERR(ddata->cmd_gpios);
345 
346 	if (ddata->cmd_gpios->ndescs != PHY_MDM6600_NR_CMD_LINES)
347 		return -EINVAL;
348 
349 	return 0;
350 }
351 
352 /**
353  * phy_mdm6600_device_power_on() - power on mdm6600 device
354  * @ddata: device driver data
355  *
356  * To get the integrated USB phy in MDM6600 takes some hoops. We must ensure
357  * the shared USB bootmode GPIOs are configured, then request modem start-up,
358  * reset and power-up.. And then we need to recycle the shared USB bootmode
359  * GPIOs as they are also used for Out of Band (OOB) wake for the USB and
360  * TS 27.010 serial mux.
361  */
phy_mdm6600_device_power_on(struct phy_mdm6600 * ddata)362 static int phy_mdm6600_device_power_on(struct phy_mdm6600 *ddata)
363 {
364 	struct gpio_desc *mode_gpio0, *mode_gpio1, *reset_gpio, *power_gpio;
365 	int error = 0, wakeirq;
366 
367 	mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
368 	mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
369 	reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
370 	power_gpio = ddata->ctrl_gpios[PHY_MDM6600_POWER];
371 
372 	/*
373 	 * Shared GPIOs must be low for normal USB mode. After booting
374 	 * they are used for OOB wake signaling. These can be also used
375 	 * to configure USB flashing mode later on based on a module
376 	 * parameter.
377 	 */
378 	gpiod_set_value_cansleep(mode_gpio0, 0);
379 	gpiod_set_value_cansleep(mode_gpio1, 0);
380 
381 	/* Request start-up mode */
382 	phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_NO_BYPASS);
383 
384 	/* Request a reset first */
385 	gpiod_set_value_cansleep(reset_gpio, 0);
386 	msleep(100);
387 
388 	/* Toggle power GPIO to request mdm6600 to start */
389 	gpiod_set_value_cansleep(power_gpio, 1);
390 	msleep(100);
391 	gpiod_set_value_cansleep(power_gpio, 0);
392 
393 	/*
394 	 * Looks like the USB PHY needs between 2.2 to 4 seconds.
395 	 * If we try to use it before that, we will get L3 errors
396 	 * from omap-usb-host trying to access the PHY. See also
397 	 * phy_mdm6600_init() for -EPROBE_DEFER.
398 	 */
399 	msleep(PHY_MDM6600_PHY_DELAY_MS);
400 	ddata->enabled = true;
401 
402 	/* Booting up the rest of MDM6600 will take total about 8 seconds */
403 	dev_info(ddata->dev, "Waiting for power up request to complete..\n");
404 	if (wait_for_completion_timeout(&ddata->ack,
405 			msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS))) {
406 		if (ddata->status > PHY_MDM6600_STATUS_PANIC &&
407 		    ddata->status < PHY_MDM6600_STATUS_SHUTDOWN_ACK)
408 			dev_info(ddata->dev, "Powered up OK\n");
409 	} else {
410 		ddata->enabled = false;
411 		error = -ETIMEDOUT;
412 		dev_err(ddata->dev, "Timed out powering up\n");
413 	}
414 
415 	/* Reconfigure mode1 GPIO as input for OOB wake */
416 	gpiod_direction_input(mode_gpio1);
417 
418 	wakeirq = gpiod_to_irq(mode_gpio1);
419 	if (wakeirq <= 0)
420 		return wakeirq;
421 
422 	error = devm_request_threaded_irq(ddata->dev, wakeirq, NULL,
423 					  phy_mdm6600_wakeirq_thread,
424 					  IRQF_TRIGGER_RISING |
425 					  IRQF_TRIGGER_FALLING |
426 					  IRQF_ONESHOT,
427 					  "mdm6600-wake",
428 					  ddata);
429 	if (error)
430 		dev_warn(ddata->dev, "no modem wakeirq irq%i: %i\n",
431 			 wakeirq, error);
432 
433 	ddata->running = true;
434 
435 	return error;
436 }
437 
438 /**
439  * phy_mdm6600_device_power_off() - power off mdm6600 device
440  * @ddata: device driver data
441  */
phy_mdm6600_device_power_off(struct phy_mdm6600 * ddata)442 static void phy_mdm6600_device_power_off(struct phy_mdm6600 *ddata)
443 {
444 	struct gpio_desc *reset_gpio =
445 		ddata->ctrl_gpios[PHY_MDM6600_RESET];
446 	int error;
447 
448 	ddata->enabled = false;
449 	phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_BP_SHUTDOWN_REQ);
450 	msleep(100);
451 
452 	gpiod_set_value_cansleep(reset_gpio, 1);
453 
454 	dev_info(ddata->dev, "Waiting for power down request to complete.. ");
455 	if (wait_for_completion_timeout(&ddata->ack,
456 					msecs_to_jiffies(5000))) {
457 		if (ddata->status == PHY_MDM6600_STATUS_PANIC)
458 			dev_info(ddata->dev, "Powered down OK\n");
459 	} else {
460 		dev_err(ddata->dev, "Timed out powering down\n");
461 	}
462 
463 	/*
464 	 * Keep reset gpio high with padconf internal pull-up resistor to
465 	 * prevent modem from waking up during deeper SoC idle states. The
466 	 * gpio bank lines can have glitches if not in the always-on wkup
467 	 * domain.
468 	 */
469 	error = pinctrl_pm_select_sleep_state(ddata->dev);
470 	if (error)
471 		dev_warn(ddata->dev, "%s: error with sleep_state: %i\n",
472 			 __func__, error);
473 }
474 
phy_mdm6600_deferred_power_on(struct work_struct * work)475 static void phy_mdm6600_deferred_power_on(struct work_struct *work)
476 {
477 	struct phy_mdm6600 *ddata;
478 	int error;
479 
480 	ddata = container_of(work, struct phy_mdm6600, bootup_work.work);
481 
482 	error = phy_mdm6600_device_power_on(ddata);
483 	if (error)
484 		dev_err(ddata->dev, "Device not functional\n");
485 }
486 
487 /*
488  * USB suspend puts mdm6600 into low power mode. For any n_gsm using apps,
489  * we need to keep the modem awake by kicking it's mode0 GPIO. This will
490  * keep the modem awake for about 1.2 seconds. When no n_gsm apps are using
491  * the modem, runtime PM auto mode can be enabled so modem can enter low
492  * power mode.
493  */
phy_mdm6600_wake_modem(struct phy_mdm6600 * ddata)494 static void phy_mdm6600_wake_modem(struct phy_mdm6600 *ddata)
495 {
496 	struct gpio_desc *mode_gpio0;
497 
498 	mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
499 	gpiod_set_value_cansleep(mode_gpio0, 1);
500 	usleep_range(5, 15);
501 	gpiod_set_value_cansleep(mode_gpio0, 0);
502 	if (ddata->awake)
503 		usleep_range(5, 15);
504 	else
505 		msleep(MDM6600_MODEM_WAKE_DELAY_MS);
506 }
507 
phy_mdm6600_modem_wake(struct work_struct * work)508 static void phy_mdm6600_modem_wake(struct work_struct *work)
509 {
510 	struct phy_mdm6600 *ddata;
511 
512 	ddata = container_of(work, struct phy_mdm6600, modem_wake_work.work);
513 	phy_mdm6600_wake_modem(ddata);
514 
515 	/*
516 	 * The modem does not always stay awake 1.2 seconds after toggling
517 	 * the wake GPIO, and sometimes it idles after about some 600 ms
518 	 * making writes time out.
519 	 */
520 	schedule_delayed_work(&ddata->modem_wake_work,
521 			      msecs_to_jiffies(PHY_MDM6600_WAKE_KICK_MS));
522 }
523 
phy_mdm6600_runtime_suspend(struct device * dev)524 static int __maybe_unused phy_mdm6600_runtime_suspend(struct device *dev)
525 {
526 	struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
527 
528 	cancel_delayed_work_sync(&ddata->modem_wake_work);
529 	ddata->awake = false;
530 
531 	return 0;
532 }
533 
phy_mdm6600_runtime_resume(struct device * dev)534 static int __maybe_unused phy_mdm6600_runtime_resume(struct device *dev)
535 {
536 	struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
537 
538 	phy_mdm6600_modem_wake(&ddata->modem_wake_work.work);
539 	ddata->awake = true;
540 
541 	return 0;
542 }
543 
544 static const struct dev_pm_ops phy_mdm6600_pm_ops = {
545 	SET_RUNTIME_PM_OPS(phy_mdm6600_runtime_suspend,
546 			   phy_mdm6600_runtime_resume, NULL)
547 };
548 
549 static const struct of_device_id phy_mdm6600_id_table[] = {
550 	{ .compatible = "motorola,mapphone-mdm6600", },
551 	{},
552 };
553 MODULE_DEVICE_TABLE(of, phy_mdm6600_id_table);
554 
phy_mdm6600_probe(struct platform_device * pdev)555 static int phy_mdm6600_probe(struct platform_device *pdev)
556 {
557 	struct phy_mdm6600 *ddata;
558 	int error;
559 
560 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
561 	if (!ddata)
562 		return -ENOMEM;
563 
564 	INIT_DELAYED_WORK(&ddata->bootup_work,
565 			  phy_mdm6600_deferred_power_on);
566 	INIT_DELAYED_WORK(&ddata->status_work, phy_mdm6600_status);
567 	INIT_DELAYED_WORK(&ddata->modem_wake_work, phy_mdm6600_modem_wake);
568 	init_completion(&ddata->ack);
569 
570 	ddata->dev = &pdev->dev;
571 	platform_set_drvdata(pdev, ddata);
572 
573 	error = phy_mdm6600_init_lines(ddata);
574 	if (error)
575 		return error;
576 
577 	phy_mdm6600_init_irq(ddata);
578 	schedule_delayed_work(&ddata->bootup_work, 0);
579 
580 	/*
581 	 * See phy_mdm6600_device_power_on(). We should be able
582 	 * to remove this eventually when ohci-platform can deal
583 	 * with -EPROBE_DEFER.
584 	 */
585 	msleep(PHY_MDM6600_PHY_DELAY_MS + 500);
586 
587 	/*
588 	 * Enable PM runtime only after PHY has been powered up properly.
589 	 * It is currently only needed after USB suspends mdm6600 and n_gsm
590 	 * needs to access the device. We don't want to do this earlier as
591 	 * gpio mode0 pin doubles as mdm6600 wake-up gpio.
592 	 */
593 	pm_runtime_use_autosuspend(ddata->dev);
594 	pm_runtime_set_autosuspend_delay(ddata->dev,
595 					 MDM6600_MODEM_IDLE_DELAY_MS);
596 	pm_runtime_enable(ddata->dev);
597 	error = pm_runtime_get_sync(ddata->dev);
598 	if (error < 0) {
599 		dev_warn(ddata->dev, "failed to wake modem: %i\n", error);
600 		pm_runtime_put_noidle(ddata->dev);
601 		goto cleanup;
602 	}
603 
604 	ddata->generic_phy = devm_phy_create(ddata->dev, NULL, &gpio_usb_ops);
605 	if (IS_ERR(ddata->generic_phy)) {
606 		error = PTR_ERR(ddata->generic_phy);
607 		goto idle;
608 	}
609 
610 	phy_set_drvdata(ddata->generic_phy, ddata);
611 
612 	ddata->phy_provider =
613 		devm_of_phy_provider_register(ddata->dev,
614 					      of_phy_simple_xlate);
615 	if (IS_ERR(ddata->phy_provider))
616 		error = PTR_ERR(ddata->phy_provider);
617 
618 idle:
619 	pm_runtime_mark_last_busy(ddata->dev);
620 	pm_runtime_put_autosuspend(ddata->dev);
621 
622 cleanup:
623 	if (error < 0) {
624 		phy_mdm6600_device_power_off(ddata);
625 		pm_runtime_disable(ddata->dev);
626 		pm_runtime_dont_use_autosuspend(ddata->dev);
627 	}
628 
629 	return error;
630 }
631 
phy_mdm6600_remove(struct platform_device * pdev)632 static void phy_mdm6600_remove(struct platform_device *pdev)
633 {
634 	struct phy_mdm6600 *ddata = platform_get_drvdata(pdev);
635 	struct gpio_desc *reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
636 
637 	pm_runtime_get_noresume(ddata->dev);
638 	pm_runtime_dont_use_autosuspend(ddata->dev);
639 	pm_runtime_put_sync(ddata->dev);
640 	pm_runtime_disable(ddata->dev);
641 
642 	if (!ddata->running)
643 		wait_for_completion_timeout(&ddata->ack,
644 			msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS));
645 
646 	gpiod_set_value_cansleep(reset_gpio, 1);
647 	phy_mdm6600_device_power_off(ddata);
648 
649 	cancel_delayed_work_sync(&ddata->modem_wake_work);
650 	cancel_delayed_work_sync(&ddata->bootup_work);
651 	cancel_delayed_work_sync(&ddata->status_work);
652 }
653 
654 static struct platform_driver phy_mdm6600_driver = {
655 	.probe = phy_mdm6600_probe,
656 	.remove = phy_mdm6600_remove,
657 	.driver = {
658 		.name = "phy-mapphone-mdm6600",
659 		.pm = &phy_mdm6600_pm_ops,
660 		.of_match_table = of_match_ptr(phy_mdm6600_id_table),
661 	},
662 };
663 
664 module_platform_driver(phy_mdm6600_driver);
665 
666 MODULE_ALIAS("platform:gpio_usb");
667 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
668 MODULE_DESCRIPTION("mdm6600 gpio usb phy driver");
669 MODULE_LICENSE("GPL v2");
670