15d1ebbdaSTony Lindgren // SPDX-License-Identifier: GPL-2.0
25d1ebbdaSTony Lindgren /*
35d1ebbdaSTony Lindgren  * Motorola Mapphone MDM6600 modem GPIO controlled USB PHY driver
45d1ebbdaSTony Lindgren  * Copyright (C) 2018 Tony Lindgren <tony@atomide.com>
55d1ebbdaSTony Lindgren  */
65d1ebbdaSTony Lindgren 
75d1ebbdaSTony Lindgren #include <linux/delay.h>
85d1ebbdaSTony Lindgren #include <linux/err.h>
95d1ebbdaSTony Lindgren #include <linux/io.h>
105d1ebbdaSTony Lindgren #include <linux/interrupt.h>
115d1ebbdaSTony Lindgren #include <linux/module.h>
125d1ebbdaSTony Lindgren #include <linux/of.h>
135d1ebbdaSTony Lindgren #include <linux/platform_device.h>
145d1ebbdaSTony Lindgren #include <linux/slab.h>
155d1ebbdaSTony Lindgren 
165d1ebbdaSTony Lindgren #include <linux/gpio/consumer.h>
175d1ebbdaSTony Lindgren #include <linux/of_platform.h>
185d1ebbdaSTony Lindgren #include <linux/phy/phy.h>
192ad2af08STony Lindgren #include <linux/pinctrl/consumer.h>
205d1ebbdaSTony Lindgren 
215d1ebbdaSTony Lindgren #define PHY_MDM6600_PHY_DELAY_MS	4000	/* PHY enable 2.2s to 3.5s */
225d1ebbdaSTony Lindgren #define PHY_MDM6600_ENABLED_DELAY_MS	8000	/* 8s more total for MDM6600 */
2346b7edf1STony Lindgren #define PHY_MDM6600_WAKE_KICK_MS	600	/* time on after GPIO toggle */
24f7f50b2aSTony Lindgren #define MDM6600_MODEM_IDLE_DELAY_MS	1000	/* modem after USB suspend */
25f7f50b2aSTony Lindgren #define MDM6600_MODEM_WAKE_DELAY_MS	200	/* modem response after idle */
265d1ebbdaSTony Lindgren 
275d1ebbdaSTony Lindgren enum phy_mdm6600_ctrl_lines {
285d1ebbdaSTony Lindgren 	PHY_MDM6600_ENABLE,			/* USB PHY enable */
295d1ebbdaSTony Lindgren 	PHY_MDM6600_POWER,			/* Device power */
305d1ebbdaSTony Lindgren 	PHY_MDM6600_RESET,			/* Device reset */
315d1ebbdaSTony Lindgren 	PHY_MDM6600_NR_CTRL_LINES,
325d1ebbdaSTony Lindgren };
335d1ebbdaSTony Lindgren 
345d1ebbdaSTony Lindgren enum phy_mdm6600_bootmode_lines {
355d1ebbdaSTony Lindgren 	PHY_MDM6600_MODE0,			/* out USB mode0 and OOB wake */
365d1ebbdaSTony Lindgren 	PHY_MDM6600_MODE1,			/* out USB mode1, in OOB wake */
375d1ebbdaSTony Lindgren 	PHY_MDM6600_NR_MODE_LINES,
385d1ebbdaSTony Lindgren };
395d1ebbdaSTony Lindgren 
405d1ebbdaSTony Lindgren enum phy_mdm6600_cmd_lines {
415d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD0,
425d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD1,
435d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD2,
445d1ebbdaSTony Lindgren 	PHY_MDM6600_NR_CMD_LINES,
455d1ebbdaSTony Lindgren };
465d1ebbdaSTony Lindgren 
475d1ebbdaSTony Lindgren enum phy_mdm6600_status_lines {
485d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS0,
495d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS1,
505d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS2,
515d1ebbdaSTony Lindgren 	PHY_MDM6600_NR_STATUS_LINES,
525d1ebbdaSTony Lindgren };
535d1ebbdaSTony Lindgren 
545d1ebbdaSTony Lindgren /*
555d1ebbdaSTony Lindgren  * MDM6600 command codes. These are based on Motorola Mapphone Linux
565d1ebbdaSTony Lindgren  * kernel tree.
575d1ebbdaSTony Lindgren  */
585d1ebbdaSTony Lindgren enum phy_mdm6600_cmd {
595d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD_BP_PANIC_ACK,
605d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD_DATA_ONLY_BYPASS,	/* Reroute USB to CPCAP PHY */
615d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD_FULL_BYPASS,		/* Reroute USB to CPCAP PHY */
625d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD_NO_BYPASS,		/* Request normal USB mode */
635d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD_BP_SHUTDOWN_REQ,	/* Request device power off */
645d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD_BP_UNKNOWN_5,
655d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD_BP_UNKNOWN_6,
665d1ebbdaSTony Lindgren 	PHY_MDM6600_CMD_UNDEFINED,
675d1ebbdaSTony Lindgren };
685d1ebbdaSTony Lindgren 
695d1ebbdaSTony Lindgren /*
705d1ebbdaSTony Lindgren  * MDM6600 status codes. These are based on Motorola Mapphone Linux
715d1ebbdaSTony Lindgren  * kernel tree.
725d1ebbdaSTony Lindgren  */
735d1ebbdaSTony Lindgren enum phy_mdm6600_status {
745d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS_PANIC,		/* Seems to be really off */
755d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS_PANIC_BUSY_WAIT,
765d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS_QC_DLOAD,
775d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS_RAM_DOWNLOADER,	/* MDM6600 USB flashing mode */
785d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS_PHONE_CODE_AWAKE,	/* MDM6600 normal USB mode */
795d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS_PHONE_CODE_ASLEEP,
805d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS_SHUTDOWN_ACK,
815d1ebbdaSTony Lindgren 	PHY_MDM6600_STATUS_UNDEFINED,
825d1ebbdaSTony Lindgren };
835d1ebbdaSTony Lindgren 
845d1ebbdaSTony Lindgren static const char * const
855d1ebbdaSTony Lindgren phy_mdm6600_status_name[] = {
865d1ebbdaSTony Lindgren 	"off", "busy", "qc_dl", "ram_dl", "awake",
875d1ebbdaSTony Lindgren 	"asleep", "shutdown", "undefined",
885d1ebbdaSTony Lindgren };
895d1ebbdaSTony Lindgren 
905d1ebbdaSTony Lindgren struct phy_mdm6600 {
915d1ebbdaSTony Lindgren 	struct device *dev;
925d1ebbdaSTony Lindgren 	struct phy *generic_phy;
935d1ebbdaSTony Lindgren 	struct phy_provider *phy_provider;
945d1ebbdaSTony Lindgren 	struct gpio_desc *ctrl_gpios[PHY_MDM6600_NR_CTRL_LINES];
955d1ebbdaSTony Lindgren 	struct gpio_descs *mode_gpios;
965d1ebbdaSTony Lindgren 	struct gpio_descs *status_gpios;
975d1ebbdaSTony Lindgren 	struct gpio_descs *cmd_gpios;
985d1ebbdaSTony Lindgren 	struct delayed_work bootup_work;
995d1ebbdaSTony Lindgren 	struct delayed_work status_work;
100f7f50b2aSTony Lindgren 	struct delayed_work modem_wake_work;
1015d1ebbdaSTony Lindgren 	struct completion ack;
1025d1ebbdaSTony Lindgren 	bool enabled;				/* mdm6600 phy enabled */
1035d1ebbdaSTony Lindgren 	bool running;				/* mdm6600 boot done */
104f7f50b2aSTony Lindgren 	bool awake;				/* mdm6600 respnds on n_gsm */
1055d1ebbdaSTony Lindgren 	int status;
1065d1ebbdaSTony Lindgren };
1075d1ebbdaSTony Lindgren 
phy_mdm6600_init(struct phy * x)1085d1ebbdaSTony Lindgren static int phy_mdm6600_init(struct phy *x)
1095d1ebbdaSTony Lindgren {
1105d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata = phy_get_drvdata(x);
1115d1ebbdaSTony Lindgren 	struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
1125d1ebbdaSTony Lindgren 
1135d1ebbdaSTony Lindgren 	if (!ddata->enabled)
1145d1ebbdaSTony Lindgren 		return -EPROBE_DEFER;
1155d1ebbdaSTony Lindgren 
1165d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(enable_gpio, 0);
1175d1ebbdaSTony Lindgren 
1185d1ebbdaSTony Lindgren 	return 0;
1195d1ebbdaSTony Lindgren }
1205d1ebbdaSTony Lindgren 
phy_mdm6600_power_on(struct phy * x)1215d1ebbdaSTony Lindgren static int phy_mdm6600_power_on(struct phy *x)
1225d1ebbdaSTony Lindgren {
1235d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata = phy_get_drvdata(x);
1245d1ebbdaSTony Lindgren 	struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
1255d1ebbdaSTony Lindgren 
1265d1ebbdaSTony Lindgren 	if (!ddata->enabled)
1275d1ebbdaSTony Lindgren 		return -ENODEV;
1285d1ebbdaSTony Lindgren 
1295d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(enable_gpio, 1);
1305d1ebbdaSTony Lindgren 
1312ad2af08STony Lindgren 	/* Allow aggressive PM for USB, it's only needed for n_gsm port */
1322ad2af08STony Lindgren 	if (pm_runtime_enabled(&x->dev))
1332ad2af08STony Lindgren 		phy_pm_runtime_put(x);
1342ad2af08STony Lindgren 
1355d1ebbdaSTony Lindgren 	return 0;
1365d1ebbdaSTony Lindgren }
1375d1ebbdaSTony Lindgren 
phy_mdm6600_power_off(struct phy * x)1385d1ebbdaSTony Lindgren static int phy_mdm6600_power_off(struct phy *x)
1395d1ebbdaSTony Lindgren {
1405d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata = phy_get_drvdata(x);
1415d1ebbdaSTony Lindgren 	struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
1422ad2af08STony Lindgren 	int error;
1435d1ebbdaSTony Lindgren 
1445d1ebbdaSTony Lindgren 	if (!ddata->enabled)
1455d1ebbdaSTony Lindgren 		return -ENODEV;
1465d1ebbdaSTony Lindgren 
1472ad2af08STony Lindgren 	/* Paired with phy_pm_runtime_put() in phy_mdm6600_power_on() */
1482ad2af08STony Lindgren 	if (pm_runtime_enabled(&x->dev)) {
1492ad2af08STony Lindgren 		error = phy_pm_runtime_get(x);
1502ad2af08STony Lindgren 		if (error < 0 && error != -EINPROGRESS)
1512ad2af08STony Lindgren 			dev_warn(ddata->dev, "%s: phy_pm_runtime_get: %i\n",
1522ad2af08STony Lindgren 				 __func__, error);
1532ad2af08STony Lindgren 	}
1542ad2af08STony Lindgren 
1555d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(enable_gpio, 0);
1565d1ebbdaSTony Lindgren 
1575d1ebbdaSTony Lindgren 	return 0;
1585d1ebbdaSTony Lindgren }
1595d1ebbdaSTony Lindgren 
1605d1ebbdaSTony Lindgren static const struct phy_ops gpio_usb_ops = {
1615d1ebbdaSTony Lindgren 	.init = phy_mdm6600_init,
1625d1ebbdaSTony Lindgren 	.power_on = phy_mdm6600_power_on,
1635d1ebbdaSTony Lindgren 	.power_off = phy_mdm6600_power_off,
1645d1ebbdaSTony Lindgren 	.owner = THIS_MODULE,
1655d1ebbdaSTony Lindgren };
1665d1ebbdaSTony Lindgren 
1675d1ebbdaSTony Lindgren /**
1685d1ebbdaSTony Lindgren  * phy_mdm6600_cmd() - send a command request to mdm6600
1695d1ebbdaSTony Lindgren  * @ddata: device driver data
170c988b7c5SVinod Koul  * @val: value of cmd to be set
1715d1ebbdaSTony Lindgren  *
1725d1ebbdaSTony Lindgren  * Configures the three command request GPIOs to the specified value.
1735d1ebbdaSTony Lindgren  */
phy_mdm6600_cmd(struct phy_mdm6600 * ddata,int val)1745d1ebbdaSTony Lindgren static void phy_mdm6600_cmd(struct phy_mdm6600 *ddata, int val)
1755d1ebbdaSTony Lindgren {
176b9762bebSJanusz Krzysztofik 	DECLARE_BITMAP(values, PHY_MDM6600_NR_CMD_LINES);
1775d1ebbdaSTony Lindgren 
178b9762bebSJanusz Krzysztofik 	values[0] = val;
1795d1ebbdaSTony Lindgren 
1805d1ebbdaSTony Lindgren 	gpiod_set_array_value_cansleep(PHY_MDM6600_NR_CMD_LINES,
18177588c14SJanusz Krzysztofik 				       ddata->cmd_gpios->desc,
18277588c14SJanusz Krzysztofik 				       ddata->cmd_gpios->info, values);
1835d1ebbdaSTony Lindgren }
1845d1ebbdaSTony Lindgren 
1855d1ebbdaSTony Lindgren /**
1865d1ebbdaSTony Lindgren  * phy_mdm6600_status() - read mdm6600 status lines
187c988b7c5SVinod Koul  * @work: work structure
1885d1ebbdaSTony Lindgren  */
phy_mdm6600_status(struct work_struct * work)1895d1ebbdaSTony Lindgren static void phy_mdm6600_status(struct work_struct *work)
1905d1ebbdaSTony Lindgren {
1915d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata;
1925d1ebbdaSTony Lindgren 	struct device *dev;
193b9762bebSJanusz Krzysztofik 	DECLARE_BITMAP(values, PHY_MDM6600_NR_STATUS_LINES);
194af5d44deSTony Lindgren 	int error;
1955d1ebbdaSTony Lindgren 
1965d1ebbdaSTony Lindgren 	ddata = container_of(work, struct phy_mdm6600, status_work.work);
1975d1ebbdaSTony Lindgren 	dev = ddata->dev;
1985d1ebbdaSTony Lindgren 
199ad500330STony Lindgren 	error = gpiod_get_array_value_cansleep(PHY_MDM6600_NR_STATUS_LINES,
2005d1ebbdaSTony Lindgren 					       ddata->status_gpios->desc,
20177588c14SJanusz Krzysztofik 					       ddata->status_gpios->info,
2025d1ebbdaSTony Lindgren 					       values);
2035d1ebbdaSTony Lindgren 	if (error)
2045d1ebbdaSTony Lindgren 		return;
2055d1ebbdaSTony Lindgren 
206af5d44deSTony Lindgren 	ddata->status = values[0] & ((1 << PHY_MDM6600_NR_STATUS_LINES) - 1);
2075d1ebbdaSTony Lindgren 
2085d1ebbdaSTony Lindgren 	dev_info(dev, "modem status: %i %s\n",
2095d1ebbdaSTony Lindgren 		 ddata->status,
210af5d44deSTony Lindgren 		 phy_mdm6600_status_name[ddata->status]);
2115d1ebbdaSTony Lindgren 	complete(&ddata->ack);
2125d1ebbdaSTony Lindgren }
2135d1ebbdaSTony Lindgren 
phy_mdm6600_irq_thread(int irq,void * data)2145d1ebbdaSTony Lindgren static irqreturn_t phy_mdm6600_irq_thread(int irq, void *data)
2155d1ebbdaSTony Lindgren {
2165d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata = data;
2175d1ebbdaSTony Lindgren 
2185d1ebbdaSTony Lindgren 	schedule_delayed_work(&ddata->status_work, msecs_to_jiffies(10));
2195d1ebbdaSTony Lindgren 
2205d1ebbdaSTony Lindgren 	return IRQ_HANDLED;
2215d1ebbdaSTony Lindgren }
2225d1ebbdaSTony Lindgren 
2235d1ebbdaSTony Lindgren /**
2245d1ebbdaSTony Lindgren  * phy_mdm6600_wakeirq_thread - handle mode1 line OOB wake after booting
2255d1ebbdaSTony Lindgren  * @irq: interrupt
2265d1ebbdaSTony Lindgren  * @data: interrupt handler data
2275d1ebbdaSTony Lindgren  *
2285d1ebbdaSTony Lindgren  * GPIO mode1 is used initially as output to configure the USB boot
2295d1ebbdaSTony Lindgren  * mode for mdm6600. After booting it is used as input for OOB wake
2305d1ebbdaSTony Lindgren  * signal from mdm6600 to the SoC. Just use it for debug info only
2315d1ebbdaSTony Lindgren  * for now.
2325d1ebbdaSTony Lindgren  */
phy_mdm6600_wakeirq_thread(int irq,void * data)2335d1ebbdaSTony Lindgren static irqreturn_t phy_mdm6600_wakeirq_thread(int irq, void *data)
2345d1ebbdaSTony Lindgren {
2355d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata = data;
2365d1ebbdaSTony Lindgren 	struct gpio_desc *mode_gpio1;
237be4e3c73STony Lindgren 	int error, wakeup;
2385d1ebbdaSTony Lindgren 
2395d1ebbdaSTony Lindgren 	mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
240be4e3c73STony Lindgren 	wakeup = gpiod_get_value(mode_gpio1);
241be4e3c73STony Lindgren 	if (!wakeup)
242be4e3c73STony Lindgren 		return IRQ_NONE;
243be4e3c73STony Lindgren 
244be4e3c73STony Lindgren 	dev_dbg(ddata->dev, "OOB wake on mode_gpio1: %i\n", wakeup);
245be4e3c73STony Lindgren 	error = pm_runtime_get_sync(ddata->dev);
246be4e3c73STony Lindgren 	if (error < 0) {
247be4e3c73STony Lindgren 		pm_runtime_put_noidle(ddata->dev);
248be4e3c73STony Lindgren 
249be4e3c73STony Lindgren 		return IRQ_NONE;
250be4e3c73STony Lindgren 	}
251be4e3c73STony Lindgren 
252be4e3c73STony Lindgren 	/* Just wake-up and kick the autosuspend timer */
253be4e3c73STony Lindgren 	pm_runtime_mark_last_busy(ddata->dev);
254be4e3c73STony Lindgren 	pm_runtime_put_autosuspend(ddata->dev);
2555d1ebbdaSTony Lindgren 
2565d1ebbdaSTony Lindgren 	return IRQ_HANDLED;
2575d1ebbdaSTony Lindgren }
2585d1ebbdaSTony Lindgren 
2595d1ebbdaSTony Lindgren /**
2605d1ebbdaSTony Lindgren  * phy_mdm6600_init_irq() - initialize mdm6600 status IRQ lines
2615d1ebbdaSTony Lindgren  * @ddata: device driver data
2625d1ebbdaSTony Lindgren  */
phy_mdm6600_init_irq(struct phy_mdm6600 * ddata)2635d1ebbdaSTony Lindgren static void phy_mdm6600_init_irq(struct phy_mdm6600 *ddata)
2645d1ebbdaSTony Lindgren {
2655d1ebbdaSTony Lindgren 	struct device *dev = ddata->dev;
2665d1ebbdaSTony Lindgren 	int i, error, irq;
2675d1ebbdaSTony Lindgren 
2685d1ebbdaSTony Lindgren 	for (i = PHY_MDM6600_STATUS0;
2695d1ebbdaSTony Lindgren 	     i <= PHY_MDM6600_STATUS2; i++) {
2705d1ebbdaSTony Lindgren 		struct gpio_desc *gpio = ddata->status_gpios->desc[i];
2715d1ebbdaSTony Lindgren 
2725d1ebbdaSTony Lindgren 		irq = gpiod_to_irq(gpio);
2735d1ebbdaSTony Lindgren 		if (irq <= 0)
2745d1ebbdaSTony Lindgren 			continue;
2755d1ebbdaSTony Lindgren 
2765d1ebbdaSTony Lindgren 		error = devm_request_threaded_irq(dev, irq, NULL,
2775d1ebbdaSTony Lindgren 					phy_mdm6600_irq_thread,
2785d1ebbdaSTony Lindgren 					IRQF_TRIGGER_RISING |
2795d1ebbdaSTony Lindgren 					IRQF_TRIGGER_FALLING |
2805d1ebbdaSTony Lindgren 					IRQF_ONESHOT,
2815d1ebbdaSTony Lindgren 					"mdm6600",
2825d1ebbdaSTony Lindgren 					ddata);
2835d1ebbdaSTony Lindgren 		if (error)
2845d1ebbdaSTony Lindgren 			dev_warn(dev, "no modem status irq%i: %i\n",
2855d1ebbdaSTony Lindgren 				 irq, error);
2865d1ebbdaSTony Lindgren 	}
2875d1ebbdaSTony Lindgren }
2885d1ebbdaSTony Lindgren 
2895d1ebbdaSTony Lindgren struct phy_mdm6600_map {
2905d1ebbdaSTony Lindgren 	const char *name;
2915d1ebbdaSTony Lindgren 	int direction;
2925d1ebbdaSTony Lindgren };
2935d1ebbdaSTony Lindgren 
2945d1ebbdaSTony Lindgren static const struct phy_mdm6600_map
2955d1ebbdaSTony Lindgren phy_mdm6600_ctrl_gpio_map[PHY_MDM6600_NR_CTRL_LINES] = {
2965d1ebbdaSTony Lindgren 	{ "enable", GPIOD_OUT_LOW, },		/* low = phy disabled */
2975d1ebbdaSTony Lindgren 	{ "power", GPIOD_OUT_LOW, },		/* low = off */
2985d1ebbdaSTony Lindgren 	{ "reset", GPIOD_OUT_HIGH, },		/* high = reset */
2995d1ebbdaSTony Lindgren };
3005d1ebbdaSTony Lindgren 
3015d1ebbdaSTony Lindgren /**
3025d1ebbdaSTony Lindgren  * phy_mdm6600_init_lines() - initialize mdm6600 GPIO lines
3035d1ebbdaSTony Lindgren  * @ddata: device driver data
3045d1ebbdaSTony Lindgren  */
phy_mdm6600_init_lines(struct phy_mdm6600 * ddata)3055d1ebbdaSTony Lindgren static int phy_mdm6600_init_lines(struct phy_mdm6600 *ddata)
3065d1ebbdaSTony Lindgren {
3075d1ebbdaSTony Lindgren 	struct device *dev = ddata->dev;
3085d1ebbdaSTony Lindgren 	int i;
3095d1ebbdaSTony Lindgren 
3105d1ebbdaSTony Lindgren 	/* MDM6600 control lines */
3115d1ebbdaSTony Lindgren 	for (i = 0; i < ARRAY_SIZE(phy_mdm6600_ctrl_gpio_map); i++) {
3125d1ebbdaSTony Lindgren 		const struct phy_mdm6600_map *map =
3135d1ebbdaSTony Lindgren 			&phy_mdm6600_ctrl_gpio_map[i];
3145d1ebbdaSTony Lindgren 		struct gpio_desc **gpio = &ddata->ctrl_gpios[i];
3155d1ebbdaSTony Lindgren 
3165d1ebbdaSTony Lindgren 		*gpio = devm_gpiod_get(dev, map->name, map->direction);
3175d1ebbdaSTony Lindgren 		if (IS_ERR(*gpio)) {
3185d1ebbdaSTony Lindgren 			dev_info(dev, "gpio %s error %li\n",
3195d1ebbdaSTony Lindgren 				 map->name, PTR_ERR(*gpio));
3205d1ebbdaSTony Lindgren 			return PTR_ERR(*gpio);
3215d1ebbdaSTony Lindgren 		}
3225d1ebbdaSTony Lindgren 	}
3235d1ebbdaSTony Lindgren 
3245d1ebbdaSTony Lindgren 	/* MDM6600 USB start-up mode output lines */
3255d1ebbdaSTony Lindgren 	ddata->mode_gpios = devm_gpiod_get_array(dev, "motorola,mode",
3265d1ebbdaSTony Lindgren 						 GPIOD_OUT_LOW);
3275d1ebbdaSTony Lindgren 	if (IS_ERR(ddata->mode_gpios))
3285d1ebbdaSTony Lindgren 		return PTR_ERR(ddata->mode_gpios);
3295d1ebbdaSTony Lindgren 
3305d1ebbdaSTony Lindgren 	if (ddata->mode_gpios->ndescs != PHY_MDM6600_NR_MODE_LINES)
3315d1ebbdaSTony Lindgren 		return -EINVAL;
3325d1ebbdaSTony Lindgren 
3335d1ebbdaSTony Lindgren 	/* MDM6600 status input lines */
3345d1ebbdaSTony Lindgren 	ddata->status_gpios = devm_gpiod_get_array(dev, "motorola,status",
3355d1ebbdaSTony Lindgren 						   GPIOD_IN);
3365d1ebbdaSTony Lindgren 	if (IS_ERR(ddata->status_gpios))
3375d1ebbdaSTony Lindgren 		return PTR_ERR(ddata->status_gpios);
3385d1ebbdaSTony Lindgren 
3395d1ebbdaSTony Lindgren 	if (ddata->status_gpios->ndescs != PHY_MDM6600_NR_STATUS_LINES)
3405d1ebbdaSTony Lindgren 		return -EINVAL;
3415d1ebbdaSTony Lindgren 
3425d1ebbdaSTony Lindgren 	/* MDM6600 cmd output lines */
3435d1ebbdaSTony Lindgren 	ddata->cmd_gpios = devm_gpiod_get_array(dev, "motorola,cmd",
3445d1ebbdaSTony Lindgren 						GPIOD_OUT_LOW);
3455d1ebbdaSTony Lindgren 	if (IS_ERR(ddata->cmd_gpios))
3465d1ebbdaSTony Lindgren 		return PTR_ERR(ddata->cmd_gpios);
3475d1ebbdaSTony Lindgren 
3485d1ebbdaSTony Lindgren 	if (ddata->cmd_gpios->ndescs != PHY_MDM6600_NR_CMD_LINES)
3495d1ebbdaSTony Lindgren 		return -EINVAL;
3505d1ebbdaSTony Lindgren 
3515d1ebbdaSTony Lindgren 	return 0;
3525d1ebbdaSTony Lindgren }
3535d1ebbdaSTony Lindgren 
3545d1ebbdaSTony Lindgren /**
3555d1ebbdaSTony Lindgren  * phy_mdm6600_device_power_on() - power on mdm6600 device
3565d1ebbdaSTony Lindgren  * @ddata: device driver data
3575d1ebbdaSTony Lindgren  *
3585d1ebbdaSTony Lindgren  * To get the integrated USB phy in MDM6600 takes some hoops. We must ensure
3595d1ebbdaSTony Lindgren  * the shared USB bootmode GPIOs are configured, then request modem start-up,
3605d1ebbdaSTony Lindgren  * reset and power-up.. And then we need to recycle the shared USB bootmode
3615d1ebbdaSTony Lindgren  * GPIOs as they are also used for Out of Band (OOB) wake for the USB and
3625d1ebbdaSTony Lindgren  * TS 27.010 serial mux.
3635d1ebbdaSTony Lindgren  */
phy_mdm6600_device_power_on(struct phy_mdm6600 * ddata)3645d1ebbdaSTony Lindgren static int phy_mdm6600_device_power_on(struct phy_mdm6600 *ddata)
3655d1ebbdaSTony Lindgren {
3665d1ebbdaSTony Lindgren 	struct gpio_desc *mode_gpio0, *mode_gpio1, *reset_gpio, *power_gpio;
3675d1ebbdaSTony Lindgren 	int error = 0, wakeirq;
3685d1ebbdaSTony Lindgren 
3695d1ebbdaSTony Lindgren 	mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
3705d1ebbdaSTony Lindgren 	mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
3715d1ebbdaSTony Lindgren 	reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
3725d1ebbdaSTony Lindgren 	power_gpio = ddata->ctrl_gpios[PHY_MDM6600_POWER];
3735d1ebbdaSTony Lindgren 
3745d1ebbdaSTony Lindgren 	/*
3755d1ebbdaSTony Lindgren 	 * Shared GPIOs must be low for normal USB mode. After booting
3765d1ebbdaSTony Lindgren 	 * they are used for OOB wake signaling. These can be also used
3775d1ebbdaSTony Lindgren 	 * to configure USB flashing mode later on based on a module
3785d1ebbdaSTony Lindgren 	 * parameter.
3795d1ebbdaSTony Lindgren 	 */
3805d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(mode_gpio0, 0);
3815d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(mode_gpio1, 0);
3825d1ebbdaSTony Lindgren 
3835d1ebbdaSTony Lindgren 	/* Request start-up mode */
3845d1ebbdaSTony Lindgren 	phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_NO_BYPASS);
3855d1ebbdaSTony Lindgren 
3865d1ebbdaSTony Lindgren 	/* Request a reset first */
3875d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(reset_gpio, 0);
3885d1ebbdaSTony Lindgren 	msleep(100);
3895d1ebbdaSTony Lindgren 
3905d1ebbdaSTony Lindgren 	/* Toggle power GPIO to request mdm6600 to start */
3915d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(power_gpio, 1);
3925d1ebbdaSTony Lindgren 	msleep(100);
3935d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(power_gpio, 0);
3945d1ebbdaSTony Lindgren 
3955d1ebbdaSTony Lindgren 	/*
3965d1ebbdaSTony Lindgren 	 * Looks like the USB PHY needs between 2.2 to 4 seconds.
3975d1ebbdaSTony Lindgren 	 * If we try to use it before that, we will get L3 errors
3985d1ebbdaSTony Lindgren 	 * from omap-usb-host trying to access the PHY. See also
3995d1ebbdaSTony Lindgren 	 * phy_mdm6600_init() for -EPROBE_DEFER.
4005d1ebbdaSTony Lindgren 	 */
4015d1ebbdaSTony Lindgren 	msleep(PHY_MDM6600_PHY_DELAY_MS);
4025d1ebbdaSTony Lindgren 	ddata->enabled = true;
4035d1ebbdaSTony Lindgren 
4045d1ebbdaSTony Lindgren 	/* Booting up the rest of MDM6600 will take total about 8 seconds */
4055d1ebbdaSTony Lindgren 	dev_info(ddata->dev, "Waiting for power up request to complete..\n");
4065d1ebbdaSTony Lindgren 	if (wait_for_completion_timeout(&ddata->ack,
4075d1ebbdaSTony Lindgren 			msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS))) {
4085d1ebbdaSTony Lindgren 		if (ddata->status > PHY_MDM6600_STATUS_PANIC &&
4095d1ebbdaSTony Lindgren 		    ddata->status < PHY_MDM6600_STATUS_SHUTDOWN_ACK)
4105d1ebbdaSTony Lindgren 			dev_info(ddata->dev, "Powered up OK\n");
4115d1ebbdaSTony Lindgren 	} else {
4125d1ebbdaSTony Lindgren 		ddata->enabled = false;
4135d1ebbdaSTony Lindgren 		error = -ETIMEDOUT;
4145d1ebbdaSTony Lindgren 		dev_err(ddata->dev, "Timed out powering up\n");
4155d1ebbdaSTony Lindgren 	}
4165d1ebbdaSTony Lindgren 
4175d1ebbdaSTony Lindgren 	/* Reconfigure mode1 GPIO as input for OOB wake */
4185d1ebbdaSTony Lindgren 	gpiod_direction_input(mode_gpio1);
4195d1ebbdaSTony Lindgren 
4205d1ebbdaSTony Lindgren 	wakeirq = gpiod_to_irq(mode_gpio1);
4215d1ebbdaSTony Lindgren 	if (wakeirq <= 0)
4225d1ebbdaSTony Lindgren 		return wakeirq;
4235d1ebbdaSTony Lindgren 
4245d1ebbdaSTony Lindgren 	error = devm_request_threaded_irq(ddata->dev, wakeirq, NULL,
4255d1ebbdaSTony Lindgren 					  phy_mdm6600_wakeirq_thread,
4265d1ebbdaSTony Lindgren 					  IRQF_TRIGGER_RISING |
4275d1ebbdaSTony Lindgren 					  IRQF_TRIGGER_FALLING |
4285d1ebbdaSTony Lindgren 					  IRQF_ONESHOT,
4295d1ebbdaSTony Lindgren 					  "mdm6600-wake",
4305d1ebbdaSTony Lindgren 					  ddata);
4315d1ebbdaSTony Lindgren 	if (error)
4325d1ebbdaSTony Lindgren 		dev_warn(ddata->dev, "no modem wakeirq irq%i: %i\n",
4335d1ebbdaSTony Lindgren 			 wakeirq, error);
4345d1ebbdaSTony Lindgren 
4355d1ebbdaSTony Lindgren 	ddata->running = true;
4365d1ebbdaSTony Lindgren 
4375d1ebbdaSTony Lindgren 	return error;
4385d1ebbdaSTony Lindgren }
4395d1ebbdaSTony Lindgren 
4405d1ebbdaSTony Lindgren /**
4415d1ebbdaSTony Lindgren  * phy_mdm6600_device_power_off() - power off mdm6600 device
4425d1ebbdaSTony Lindgren  * @ddata: device driver data
4435d1ebbdaSTony Lindgren  */
phy_mdm6600_device_power_off(struct phy_mdm6600 * ddata)4445d1ebbdaSTony Lindgren static void phy_mdm6600_device_power_off(struct phy_mdm6600 *ddata)
4455d1ebbdaSTony Lindgren {
4465d1ebbdaSTony Lindgren 	struct gpio_desc *reset_gpio =
4475d1ebbdaSTony Lindgren 		ddata->ctrl_gpios[PHY_MDM6600_RESET];
448*3b384cc7STony Lindgren 	int error;
4495d1ebbdaSTony Lindgren 
4505d1ebbdaSTony Lindgren 	ddata->enabled = false;
4515d1ebbdaSTony Lindgren 	phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_BP_SHUTDOWN_REQ);
4525d1ebbdaSTony Lindgren 	msleep(100);
4535d1ebbdaSTony Lindgren 
4545d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(reset_gpio, 1);
4555d1ebbdaSTony Lindgren 
4565d1ebbdaSTony Lindgren 	dev_info(ddata->dev, "Waiting for power down request to complete.. ");
4575d1ebbdaSTony Lindgren 	if (wait_for_completion_timeout(&ddata->ack,
4585d1ebbdaSTony Lindgren 					msecs_to_jiffies(5000))) {
4595d1ebbdaSTony Lindgren 		if (ddata->status == PHY_MDM6600_STATUS_PANIC)
4605d1ebbdaSTony Lindgren 			dev_info(ddata->dev, "Powered down OK\n");
4615d1ebbdaSTony Lindgren 	} else {
4625d1ebbdaSTony Lindgren 		dev_err(ddata->dev, "Timed out powering down\n");
4635d1ebbdaSTony Lindgren 	}
464*3b384cc7STony Lindgren 
465*3b384cc7STony Lindgren 	/*
466*3b384cc7STony Lindgren 	 * Keep reset gpio high with padconf internal pull-up resistor to
467*3b384cc7STony Lindgren 	 * prevent modem from waking up during deeper SoC idle states. The
468*3b384cc7STony Lindgren 	 * gpio bank lines can have glitches if not in the always-on wkup
469*3b384cc7STony Lindgren 	 * domain.
470*3b384cc7STony Lindgren 	 */
471*3b384cc7STony Lindgren 	error = pinctrl_pm_select_sleep_state(ddata->dev);
472*3b384cc7STony Lindgren 	if (error)
473*3b384cc7STony Lindgren 		dev_warn(ddata->dev, "%s: error with sleep_state: %i\n",
474*3b384cc7STony Lindgren 			 __func__, error);
4755d1ebbdaSTony Lindgren }
4765d1ebbdaSTony Lindgren 
phy_mdm6600_deferred_power_on(struct work_struct * work)4775d1ebbdaSTony Lindgren static void phy_mdm6600_deferred_power_on(struct work_struct *work)
4785d1ebbdaSTony Lindgren {
4795d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata;
4805d1ebbdaSTony Lindgren 	int error;
4815d1ebbdaSTony Lindgren 
4825d1ebbdaSTony Lindgren 	ddata = container_of(work, struct phy_mdm6600, bootup_work.work);
4835d1ebbdaSTony Lindgren 
4845d1ebbdaSTony Lindgren 	error = phy_mdm6600_device_power_on(ddata);
4855d1ebbdaSTony Lindgren 	if (error)
4865d1ebbdaSTony Lindgren 		dev_err(ddata->dev, "Device not functional\n");
4875d1ebbdaSTony Lindgren }
4885d1ebbdaSTony Lindgren 
489f7f50b2aSTony Lindgren /*
490f7f50b2aSTony Lindgren  * USB suspend puts mdm6600 into low power mode. For any n_gsm using apps,
491f7f50b2aSTony Lindgren  * we need to keep the modem awake by kicking it's mode0 GPIO. This will
492f7f50b2aSTony Lindgren  * keep the modem awake for about 1.2 seconds. When no n_gsm apps are using
493f7f50b2aSTony Lindgren  * the modem, runtime PM auto mode can be enabled so modem can enter low
494f7f50b2aSTony Lindgren  * power mode.
495f7f50b2aSTony Lindgren  */
phy_mdm6600_wake_modem(struct phy_mdm6600 * ddata)496f7f50b2aSTony Lindgren static void phy_mdm6600_wake_modem(struct phy_mdm6600 *ddata)
497f7f50b2aSTony Lindgren {
498f7f50b2aSTony Lindgren 	struct gpio_desc *mode_gpio0;
499f7f50b2aSTony Lindgren 
500f7f50b2aSTony Lindgren 	mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
501f7f50b2aSTony Lindgren 	gpiod_set_value_cansleep(mode_gpio0, 1);
502f7f50b2aSTony Lindgren 	usleep_range(5, 15);
503f7f50b2aSTony Lindgren 	gpiod_set_value_cansleep(mode_gpio0, 0);
504f7f50b2aSTony Lindgren 	if (ddata->awake)
505f7f50b2aSTony Lindgren 		usleep_range(5, 15);
506f7f50b2aSTony Lindgren 	else
507f7f50b2aSTony Lindgren 		msleep(MDM6600_MODEM_WAKE_DELAY_MS);
508f7f50b2aSTony Lindgren }
509f7f50b2aSTony Lindgren 
phy_mdm6600_modem_wake(struct work_struct * work)510f7f50b2aSTony Lindgren static void phy_mdm6600_modem_wake(struct work_struct *work)
511f7f50b2aSTony Lindgren {
512f7f50b2aSTony Lindgren 	struct phy_mdm6600 *ddata;
513f7f50b2aSTony Lindgren 
514f7f50b2aSTony Lindgren 	ddata = container_of(work, struct phy_mdm6600, modem_wake_work.work);
515f7f50b2aSTony Lindgren 	phy_mdm6600_wake_modem(ddata);
51646b7edf1STony Lindgren 
51746b7edf1STony Lindgren 	/*
51846b7edf1STony Lindgren 	 * The modem does not always stay awake 1.2 seconds after toggling
51946b7edf1STony Lindgren 	 * the wake GPIO, and sometimes it idles after about some 600 ms
52046b7edf1STony Lindgren 	 * making writes time out.
52146b7edf1STony Lindgren 	 */
522f7f50b2aSTony Lindgren 	schedule_delayed_work(&ddata->modem_wake_work,
52346b7edf1STony Lindgren 			      msecs_to_jiffies(PHY_MDM6600_WAKE_KICK_MS));
524f7f50b2aSTony Lindgren }
525f7f50b2aSTony Lindgren 
phy_mdm6600_runtime_suspend(struct device * dev)526f7f50b2aSTony Lindgren static int __maybe_unused phy_mdm6600_runtime_suspend(struct device *dev)
527f7f50b2aSTony Lindgren {
528f7f50b2aSTony Lindgren 	struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
529f7f50b2aSTony Lindgren 
530f7f50b2aSTony Lindgren 	cancel_delayed_work_sync(&ddata->modem_wake_work);
531f7f50b2aSTony Lindgren 	ddata->awake = false;
532f7f50b2aSTony Lindgren 
533f7f50b2aSTony Lindgren 	return 0;
534f7f50b2aSTony Lindgren }
535f7f50b2aSTony Lindgren 
phy_mdm6600_runtime_resume(struct device * dev)536f7f50b2aSTony Lindgren static int __maybe_unused phy_mdm6600_runtime_resume(struct device *dev)
537f7f50b2aSTony Lindgren {
538f7f50b2aSTony Lindgren 	struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
539f7f50b2aSTony Lindgren 
540f7f50b2aSTony Lindgren 	phy_mdm6600_modem_wake(&ddata->modem_wake_work.work);
541f7f50b2aSTony Lindgren 	ddata->awake = true;
542f7f50b2aSTony Lindgren 
543f7f50b2aSTony Lindgren 	return 0;
544f7f50b2aSTony Lindgren }
545f7f50b2aSTony Lindgren 
546f7f50b2aSTony Lindgren static const struct dev_pm_ops phy_mdm6600_pm_ops = {
547f7f50b2aSTony Lindgren 	SET_RUNTIME_PM_OPS(phy_mdm6600_runtime_suspend,
548f7f50b2aSTony Lindgren 			   phy_mdm6600_runtime_resume, NULL)
549f7f50b2aSTony Lindgren };
550f7f50b2aSTony Lindgren 
5515d1ebbdaSTony Lindgren static const struct of_device_id phy_mdm6600_id_table[] = {
5525d1ebbdaSTony Lindgren 	{ .compatible = "motorola,mapphone-mdm6600", },
5535d1ebbdaSTony Lindgren 	{},
5545d1ebbdaSTony Lindgren };
5555d1ebbdaSTony Lindgren MODULE_DEVICE_TABLE(of, phy_mdm6600_id_table);
5565d1ebbdaSTony Lindgren 
phy_mdm6600_probe(struct platform_device * pdev)5575d1ebbdaSTony Lindgren static int phy_mdm6600_probe(struct platform_device *pdev)
5585d1ebbdaSTony Lindgren {
5595d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata;
5605d1ebbdaSTony Lindgren 	int error;
5615d1ebbdaSTony Lindgren 
5625d1ebbdaSTony Lindgren 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
5635d1ebbdaSTony Lindgren 	if (!ddata)
5645d1ebbdaSTony Lindgren 		return -ENOMEM;
5655d1ebbdaSTony Lindgren 
5665d1ebbdaSTony Lindgren 	INIT_DELAYED_WORK(&ddata->bootup_work,
5675d1ebbdaSTony Lindgren 			  phy_mdm6600_deferred_power_on);
5685d1ebbdaSTony Lindgren 	INIT_DELAYED_WORK(&ddata->status_work, phy_mdm6600_status);
569f7f50b2aSTony Lindgren 	INIT_DELAYED_WORK(&ddata->modem_wake_work, phy_mdm6600_modem_wake);
5705d1ebbdaSTony Lindgren 	init_completion(&ddata->ack);
5715d1ebbdaSTony Lindgren 
5725d1ebbdaSTony Lindgren 	ddata->dev = &pdev->dev;
5735d1ebbdaSTony Lindgren 	platform_set_drvdata(pdev, ddata);
5745d1ebbdaSTony Lindgren 
5755d1ebbdaSTony Lindgren 	error = phy_mdm6600_init_lines(ddata);
5765d1ebbdaSTony Lindgren 	if (error)
5775d1ebbdaSTony Lindgren 		return error;
5785d1ebbdaSTony Lindgren 
5795d1ebbdaSTony Lindgren 	phy_mdm6600_init_irq(ddata);
5805d1ebbdaSTony Lindgren 	schedule_delayed_work(&ddata->bootup_work, 0);
5815d1ebbdaSTony Lindgren 
5825d1ebbdaSTony Lindgren 	/*
5835d1ebbdaSTony Lindgren 	 * See phy_mdm6600_device_power_on(). We should be able
5845d1ebbdaSTony Lindgren 	 * to remove this eventually when ohci-platform can deal
5855d1ebbdaSTony Lindgren 	 * with -EPROBE_DEFER.
5865d1ebbdaSTony Lindgren 	 */
5875d1ebbdaSTony Lindgren 	msleep(PHY_MDM6600_PHY_DELAY_MS + 500);
5885d1ebbdaSTony Lindgren 
589f7f50b2aSTony Lindgren 	/*
590f7f50b2aSTony Lindgren 	 * Enable PM runtime only after PHY has been powered up properly.
591f7f50b2aSTony Lindgren 	 * It is currently only needed after USB suspends mdm6600 and n_gsm
592f7f50b2aSTony Lindgren 	 * needs to access the device. We don't want to do this earlier as
593f7f50b2aSTony Lindgren 	 * gpio mode0 pin doubles as mdm6600 wake-up gpio.
594f7f50b2aSTony Lindgren 	 */
595f7f50b2aSTony Lindgren 	pm_runtime_use_autosuspend(ddata->dev);
596f7f50b2aSTony Lindgren 	pm_runtime_set_autosuspend_delay(ddata->dev,
597f7f50b2aSTony Lindgren 					 MDM6600_MODEM_IDLE_DELAY_MS);
598f7f50b2aSTony Lindgren 	pm_runtime_enable(ddata->dev);
599f7f50b2aSTony Lindgren 	error = pm_runtime_get_sync(ddata->dev);
600f7f50b2aSTony Lindgren 	if (error < 0) {
601f7f50b2aSTony Lindgren 		dev_warn(ddata->dev, "failed to wake modem: %i\n", error);
602f7f50b2aSTony Lindgren 		pm_runtime_put_noidle(ddata->dev);
6032ad2af08STony Lindgren 		goto cleanup;
604f7f50b2aSTony Lindgren 	}
6052ad2af08STony Lindgren 
6062ad2af08STony Lindgren 	ddata->generic_phy = devm_phy_create(ddata->dev, NULL, &gpio_usb_ops);
6072ad2af08STony Lindgren 	if (IS_ERR(ddata->generic_phy)) {
6082ad2af08STony Lindgren 		error = PTR_ERR(ddata->generic_phy);
6092ad2af08STony Lindgren 		goto idle;
6102ad2af08STony Lindgren 	}
6112ad2af08STony Lindgren 
6122ad2af08STony Lindgren 	phy_set_drvdata(ddata->generic_phy, ddata);
6132ad2af08STony Lindgren 
6142ad2af08STony Lindgren 	ddata->phy_provider =
6152ad2af08STony Lindgren 		devm_of_phy_provider_register(ddata->dev,
6162ad2af08STony Lindgren 					      of_phy_simple_xlate);
6172ad2af08STony Lindgren 	if (IS_ERR(ddata->phy_provider))
6182ad2af08STony Lindgren 		error = PTR_ERR(ddata->phy_provider);
6192ad2af08STony Lindgren 
6202ad2af08STony Lindgren idle:
621f7f50b2aSTony Lindgren 	pm_runtime_mark_last_busy(ddata->dev);
622f7f50b2aSTony Lindgren 	pm_runtime_put_autosuspend(ddata->dev);
623f7f50b2aSTony Lindgren 
6245d1ebbdaSTony Lindgren cleanup:
62571960615STony Lindgren 	if (error < 0) {
6265d1ebbdaSTony Lindgren 		phy_mdm6600_device_power_off(ddata);
627d644e0d7SMiaoqian Lin 		pm_runtime_disable(ddata->dev);
628d644e0d7SMiaoqian Lin 		pm_runtime_dont_use_autosuspend(ddata->dev);
62971960615STony Lindgren 	}
63071960615STony Lindgren 
6315d1ebbdaSTony Lindgren 	return error;
6325d1ebbdaSTony Lindgren }
6335d1ebbdaSTony Lindgren 
phy_mdm6600_remove(struct platform_device * pdev)63491537c86SUwe Kleine-König static void phy_mdm6600_remove(struct platform_device *pdev)
6355d1ebbdaSTony Lindgren {
6365d1ebbdaSTony Lindgren 	struct phy_mdm6600 *ddata = platform_get_drvdata(pdev);
6375d1ebbdaSTony Lindgren 	struct gpio_desc *reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
6385d1ebbdaSTony Lindgren 
639b99e0ba9STony Lindgren 	pm_runtime_get_noresume(ddata->dev);
640f7f50b2aSTony Lindgren 	pm_runtime_dont_use_autosuspend(ddata->dev);
641f7f50b2aSTony Lindgren 	pm_runtime_put_sync(ddata->dev);
642f7f50b2aSTony Lindgren 	pm_runtime_disable(ddata->dev);
643f7f50b2aSTony Lindgren 
6445d1ebbdaSTony Lindgren 	if (!ddata->running)
6455d1ebbdaSTony Lindgren 		wait_for_completion_timeout(&ddata->ack,
6465d1ebbdaSTony Lindgren 			msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS));
6475d1ebbdaSTony Lindgren 
6485d1ebbdaSTony Lindgren 	gpiod_set_value_cansleep(reset_gpio, 1);
6495d1ebbdaSTony Lindgren 	phy_mdm6600_device_power_off(ddata);
6505d1ebbdaSTony Lindgren 
651f7f50b2aSTony Lindgren 	cancel_delayed_work_sync(&ddata->modem_wake_work);
6525d1ebbdaSTony Lindgren 	cancel_delayed_work_sync(&ddata->bootup_work);
6535d1ebbdaSTony Lindgren 	cancel_delayed_work_sync(&ddata->status_work);
6545d1ebbdaSTony Lindgren }
6555d1ebbdaSTony Lindgren 
6565d1ebbdaSTony Lindgren static struct platform_driver phy_mdm6600_driver = {
6575d1ebbdaSTony Lindgren 	.probe = phy_mdm6600_probe,
65891537c86SUwe Kleine-König 	.remove_new = phy_mdm6600_remove,
6595d1ebbdaSTony Lindgren 	.driver = {
6605d1ebbdaSTony Lindgren 		.name = "phy-mapphone-mdm6600",
661f7f50b2aSTony Lindgren 		.pm = &phy_mdm6600_pm_ops,
6625d1ebbdaSTony Lindgren 		.of_match_table = of_match_ptr(phy_mdm6600_id_table),
6635d1ebbdaSTony Lindgren 	},
6645d1ebbdaSTony Lindgren };
6655d1ebbdaSTony Lindgren 
6665d1ebbdaSTony Lindgren module_platform_driver(phy_mdm6600_driver);
6675d1ebbdaSTony Lindgren 
6685d1ebbdaSTony Lindgren MODULE_ALIAS("platform:gpio_usb");
6695d1ebbdaSTony Lindgren MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
6705d1ebbdaSTony Lindgren MODULE_DESCRIPTION("mdm6600 gpio usb phy driver");
6715d1ebbdaSTony Lindgren MODULE_LICENSE("GPL v2");
672