1 // SPDX-License-Identifier: GPL-2.0
3 * Motorola Mapphone MDM6600 modem GPIO controlled USB PHY driver
4 * Copyright (C) 2018 Tony Lindgren <tony@atomide.com>
7 #include <linux/delay.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/of_platform.h>
18 #include <linux/phy/phy.h>
20 #define PHY_MDM6600_PHY_DELAY_MS 4000 /* PHY enable 2.2s to 3.5s */
21 #define PHY_MDM6600_ENABLED_DELAY_MS 8000 /* 8s more total for MDM6600 */
22 #define PHY_MDM6600_WAKE_KICK_MS 600 /* time on after GPIO toggle */
23 #define MDM6600_MODEM_IDLE_DELAY_MS 1000 /* modem after USB suspend */
24 #define MDM6600_MODEM_WAKE_DELAY_MS 200 /* modem response after idle */
26 enum phy_mdm6600_ctrl_lines {
27 PHY_MDM6600_ENABLE, /* USB PHY enable */
28 PHY_MDM6600_POWER, /* Device power */
29 PHY_MDM6600_RESET, /* Device reset */
30 PHY_MDM6600_NR_CTRL_LINES,
33 enum phy_mdm6600_bootmode_lines {
34 PHY_MDM6600_MODE0, /* out USB mode0 and OOB wake */
35 PHY_MDM6600_MODE1, /* out USB mode1, in OOB wake */
36 PHY_MDM6600_NR_MODE_LINES,
39 enum phy_mdm6600_cmd_lines {
43 PHY_MDM6600_NR_CMD_LINES,
46 enum phy_mdm6600_status_lines {
50 PHY_MDM6600_NR_STATUS_LINES,
54 * MDM6600 command codes. These are based on Motorola Mapphone Linux
57 enum phy_mdm6600_cmd {
58 PHY_MDM6600_CMD_BP_PANIC_ACK,
59 PHY_MDM6600_CMD_DATA_ONLY_BYPASS, /* Reroute USB to CPCAP PHY */
60 PHY_MDM6600_CMD_FULL_BYPASS, /* Reroute USB to CPCAP PHY */
61 PHY_MDM6600_CMD_NO_BYPASS, /* Request normal USB mode */
62 PHY_MDM6600_CMD_BP_SHUTDOWN_REQ, /* Request device power off */
63 PHY_MDM6600_CMD_BP_UNKNOWN_5,
64 PHY_MDM6600_CMD_BP_UNKNOWN_6,
65 PHY_MDM6600_CMD_UNDEFINED,
69 * MDM6600 status codes. These are based on Motorola Mapphone Linux
72 enum phy_mdm6600_status {
73 PHY_MDM6600_STATUS_PANIC, /* Seems to be really off */
74 PHY_MDM6600_STATUS_PANIC_BUSY_WAIT,
75 PHY_MDM6600_STATUS_QC_DLOAD,
76 PHY_MDM6600_STATUS_RAM_DOWNLOADER, /* MDM6600 USB flashing mode */
77 PHY_MDM6600_STATUS_PHONE_CODE_AWAKE, /* MDM6600 normal USB mode */
78 PHY_MDM6600_STATUS_PHONE_CODE_ASLEEP,
79 PHY_MDM6600_STATUS_SHUTDOWN_ACK,
80 PHY_MDM6600_STATUS_UNDEFINED,
83 static const char * const
84 phy_mdm6600_status_name[] = {
85 "off", "busy", "qc_dl", "ram_dl", "awake",
86 "asleep", "shutdown", "undefined",
91 struct phy *generic_phy;
92 struct phy_provider *phy_provider;
93 struct gpio_desc *ctrl_gpios[PHY_MDM6600_NR_CTRL_LINES];
94 struct gpio_descs *mode_gpios;
95 struct gpio_descs *status_gpios;
96 struct gpio_descs *cmd_gpios;
97 struct delayed_work bootup_work;
98 struct delayed_work status_work;
99 struct delayed_work modem_wake_work;
100 struct completion ack;
101 bool enabled; /* mdm6600 phy enabled */
102 bool running; /* mdm6600 boot done */
103 bool awake; /* mdm6600 respnds on n_gsm */
107 static int phy_mdm6600_init(struct phy *x)
109 struct phy_mdm6600 *ddata = phy_get_drvdata(x);
110 struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
113 return -EPROBE_DEFER;
115 gpiod_set_value_cansleep(enable_gpio, 0);
120 static int phy_mdm6600_power_on(struct phy *x)
122 struct phy_mdm6600 *ddata = phy_get_drvdata(x);
123 struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
128 gpiod_set_value_cansleep(enable_gpio, 1);
133 static int phy_mdm6600_power_off(struct phy *x)
135 struct phy_mdm6600 *ddata = phy_get_drvdata(x);
136 struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
141 gpiod_set_value_cansleep(enable_gpio, 0);
146 static const struct phy_ops gpio_usb_ops = {
147 .init = phy_mdm6600_init,
148 .power_on = phy_mdm6600_power_on,
149 .power_off = phy_mdm6600_power_off,
150 .owner = THIS_MODULE,
154 * phy_mdm6600_cmd() - send a command request to mdm6600
155 * @ddata: device driver data
157 * Configures the three command request GPIOs to the specified value.
159 static void phy_mdm6600_cmd(struct phy_mdm6600 *ddata, int val)
161 int values[PHY_MDM6600_NR_CMD_LINES];
164 val &= (1 << PHY_MDM6600_NR_CMD_LINES) - 1;
165 for (i = 0; i < PHY_MDM6600_NR_CMD_LINES; i++)
166 values[i] = (val & BIT(i)) >> i;
168 gpiod_set_array_value_cansleep(PHY_MDM6600_NR_CMD_LINES,
169 ddata->cmd_gpios->desc, values);
173 * phy_mdm6600_status() - read mdm6600 status lines
174 * @ddata: device driver data
176 static void phy_mdm6600_status(struct work_struct *work)
178 struct phy_mdm6600 *ddata;
180 int values[PHY_MDM6600_NR_STATUS_LINES];
181 int error, i, val = 0;
183 ddata = container_of(work, struct phy_mdm6600, status_work.work);
186 error = gpiod_get_array_value_cansleep(PHY_MDM6600_NR_STATUS_LINES,
187 ddata->status_gpios->desc,
192 for (i = 0; i < PHY_MDM6600_NR_STATUS_LINES; i++) {
193 val |= values[i] << i;
194 dev_dbg(ddata->dev, "XXX %s: i: %i values[i]: %i val: %i\n",
195 __func__, i, values[i], val);
199 dev_info(dev, "modem status: %i %s\n",
201 phy_mdm6600_status_name[ddata->status & 7]);
202 complete(&ddata->ack);
205 static irqreturn_t phy_mdm6600_irq_thread(int irq, void *data)
207 struct phy_mdm6600 *ddata = data;
209 schedule_delayed_work(&ddata->status_work, msecs_to_jiffies(10));
215 * phy_mdm6600_wakeirq_thread - handle mode1 line OOB wake after booting
217 * @data: interrupt handler data
219 * GPIO mode1 is used initially as output to configure the USB boot
220 * mode for mdm6600. After booting it is used as input for OOB wake
221 * signal from mdm6600 to the SoC. Just use it for debug info only
224 static irqreturn_t phy_mdm6600_wakeirq_thread(int irq, void *data)
226 struct phy_mdm6600 *ddata = data;
227 struct gpio_desc *mode_gpio1;
230 mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
231 wakeup = gpiod_get_value(mode_gpio1);
235 dev_dbg(ddata->dev, "OOB wake on mode_gpio1: %i\n", wakeup);
236 error = pm_runtime_get_sync(ddata->dev);
238 pm_runtime_put_noidle(ddata->dev);
243 /* Just wake-up and kick the autosuspend timer */
244 pm_runtime_mark_last_busy(ddata->dev);
245 pm_runtime_put_autosuspend(ddata->dev);
251 * phy_mdm6600_init_irq() - initialize mdm6600 status IRQ lines
252 * @ddata: device driver data
254 static void phy_mdm6600_init_irq(struct phy_mdm6600 *ddata)
256 struct device *dev = ddata->dev;
259 for (i = PHY_MDM6600_STATUS0;
260 i <= PHY_MDM6600_STATUS2; i++) {
261 struct gpio_desc *gpio = ddata->status_gpios->desc[i];
263 irq = gpiod_to_irq(gpio);
267 error = devm_request_threaded_irq(dev, irq, NULL,
268 phy_mdm6600_irq_thread,
269 IRQF_TRIGGER_RISING |
270 IRQF_TRIGGER_FALLING |
275 dev_warn(dev, "no modem status irq%i: %i\n",
280 struct phy_mdm6600_map {
285 static const struct phy_mdm6600_map
286 phy_mdm6600_ctrl_gpio_map[PHY_MDM6600_NR_CTRL_LINES] = {
287 { "enable", GPIOD_OUT_LOW, }, /* low = phy disabled */
288 { "power", GPIOD_OUT_LOW, }, /* low = off */
289 { "reset", GPIOD_OUT_HIGH, }, /* high = reset */
293 * phy_mdm6600_init_lines() - initialize mdm6600 GPIO lines
294 * @ddata: device driver data
296 static int phy_mdm6600_init_lines(struct phy_mdm6600 *ddata)
298 struct device *dev = ddata->dev;
301 /* MDM6600 control lines */
302 for (i = 0; i < ARRAY_SIZE(phy_mdm6600_ctrl_gpio_map); i++) {
303 const struct phy_mdm6600_map *map =
304 &phy_mdm6600_ctrl_gpio_map[i];
305 struct gpio_desc **gpio = &ddata->ctrl_gpios[i];
307 *gpio = devm_gpiod_get(dev, map->name, map->direction);
309 dev_info(dev, "gpio %s error %li\n",
310 map->name, PTR_ERR(*gpio));
311 return PTR_ERR(*gpio);
315 /* MDM6600 USB start-up mode output lines */
316 ddata->mode_gpios = devm_gpiod_get_array(dev, "motorola,mode",
318 if (IS_ERR(ddata->mode_gpios))
319 return PTR_ERR(ddata->mode_gpios);
321 if (ddata->mode_gpios->ndescs != PHY_MDM6600_NR_MODE_LINES)
324 /* MDM6600 status input lines */
325 ddata->status_gpios = devm_gpiod_get_array(dev, "motorola,status",
327 if (IS_ERR(ddata->status_gpios))
328 return PTR_ERR(ddata->status_gpios);
330 if (ddata->status_gpios->ndescs != PHY_MDM6600_NR_STATUS_LINES)
333 /* MDM6600 cmd output lines */
334 ddata->cmd_gpios = devm_gpiod_get_array(dev, "motorola,cmd",
336 if (IS_ERR(ddata->cmd_gpios))
337 return PTR_ERR(ddata->cmd_gpios);
339 if (ddata->cmd_gpios->ndescs != PHY_MDM6600_NR_CMD_LINES)
346 * phy_mdm6600_device_power_on() - power on mdm6600 device
347 * @ddata: device driver data
349 * To get the integrated USB phy in MDM6600 takes some hoops. We must ensure
350 * the shared USB bootmode GPIOs are configured, then request modem start-up,
351 * reset and power-up.. And then we need to recycle the shared USB bootmode
352 * GPIOs as they are also used for Out of Band (OOB) wake for the USB and
353 * TS 27.010 serial mux.
355 static int phy_mdm6600_device_power_on(struct phy_mdm6600 *ddata)
357 struct gpio_desc *mode_gpio0, *mode_gpio1, *reset_gpio, *power_gpio;
358 int error = 0, wakeirq;
360 mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
361 mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
362 reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
363 power_gpio = ddata->ctrl_gpios[PHY_MDM6600_POWER];
366 * Shared GPIOs must be low for normal USB mode. After booting
367 * they are used for OOB wake signaling. These can be also used
368 * to configure USB flashing mode later on based on a module
371 gpiod_set_value_cansleep(mode_gpio0, 0);
372 gpiod_set_value_cansleep(mode_gpio1, 0);
374 /* Request start-up mode */
375 phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_NO_BYPASS);
377 /* Request a reset first */
378 gpiod_set_value_cansleep(reset_gpio, 0);
381 /* Toggle power GPIO to request mdm6600 to start */
382 gpiod_set_value_cansleep(power_gpio, 1);
384 gpiod_set_value_cansleep(power_gpio, 0);
387 * Looks like the USB PHY needs between 2.2 to 4 seconds.
388 * If we try to use it before that, we will get L3 errors
389 * from omap-usb-host trying to access the PHY. See also
390 * phy_mdm6600_init() for -EPROBE_DEFER.
392 msleep(PHY_MDM6600_PHY_DELAY_MS);
393 ddata->enabled = true;
395 /* Booting up the rest of MDM6600 will take total about 8 seconds */
396 dev_info(ddata->dev, "Waiting for power up request to complete..\n");
397 if (wait_for_completion_timeout(&ddata->ack,
398 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS))) {
399 if (ddata->status > PHY_MDM6600_STATUS_PANIC &&
400 ddata->status < PHY_MDM6600_STATUS_SHUTDOWN_ACK)
401 dev_info(ddata->dev, "Powered up OK\n");
403 ddata->enabled = false;
405 dev_err(ddata->dev, "Timed out powering up\n");
408 /* Reconfigure mode1 GPIO as input for OOB wake */
409 gpiod_direction_input(mode_gpio1);
411 wakeirq = gpiod_to_irq(mode_gpio1);
415 error = devm_request_threaded_irq(ddata->dev, wakeirq, NULL,
416 phy_mdm6600_wakeirq_thread,
417 IRQF_TRIGGER_RISING |
418 IRQF_TRIGGER_FALLING |
423 dev_warn(ddata->dev, "no modem wakeirq irq%i: %i\n",
426 ddata->running = true;
432 * phy_mdm6600_device_power_off() - power off mdm6600 device
433 * @ddata: device driver data
435 static void phy_mdm6600_device_power_off(struct phy_mdm6600 *ddata)
437 struct gpio_desc *reset_gpio =
438 ddata->ctrl_gpios[PHY_MDM6600_RESET];
440 ddata->enabled = false;
441 phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_BP_SHUTDOWN_REQ);
444 gpiod_set_value_cansleep(reset_gpio, 1);
446 dev_info(ddata->dev, "Waiting for power down request to complete.. ");
447 if (wait_for_completion_timeout(&ddata->ack,
448 msecs_to_jiffies(5000))) {
449 if (ddata->status == PHY_MDM6600_STATUS_PANIC)
450 dev_info(ddata->dev, "Powered down OK\n");
452 dev_err(ddata->dev, "Timed out powering down\n");
456 static void phy_mdm6600_deferred_power_on(struct work_struct *work)
458 struct phy_mdm6600 *ddata;
461 ddata = container_of(work, struct phy_mdm6600, bootup_work.work);
463 error = phy_mdm6600_device_power_on(ddata);
465 dev_err(ddata->dev, "Device not functional\n");
469 * USB suspend puts mdm6600 into low power mode. For any n_gsm using apps,
470 * we need to keep the modem awake by kicking it's mode0 GPIO. This will
471 * keep the modem awake for about 1.2 seconds. When no n_gsm apps are using
472 * the modem, runtime PM auto mode can be enabled so modem can enter low
475 static void phy_mdm6600_wake_modem(struct phy_mdm6600 *ddata)
477 struct gpio_desc *mode_gpio0;
479 mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
480 gpiod_set_value_cansleep(mode_gpio0, 1);
482 gpiod_set_value_cansleep(mode_gpio0, 0);
486 msleep(MDM6600_MODEM_WAKE_DELAY_MS);
489 static void phy_mdm6600_modem_wake(struct work_struct *work)
491 struct phy_mdm6600 *ddata;
493 ddata = container_of(work, struct phy_mdm6600, modem_wake_work.work);
494 phy_mdm6600_wake_modem(ddata);
497 * The modem does not always stay awake 1.2 seconds after toggling
498 * the wake GPIO, and sometimes it idles after about some 600 ms
499 * making writes time out.
501 schedule_delayed_work(&ddata->modem_wake_work,
502 msecs_to_jiffies(PHY_MDM6600_WAKE_KICK_MS));
505 static int __maybe_unused phy_mdm6600_runtime_suspend(struct device *dev)
507 struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
509 cancel_delayed_work_sync(&ddata->modem_wake_work);
510 ddata->awake = false;
515 static int __maybe_unused phy_mdm6600_runtime_resume(struct device *dev)
517 struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
519 phy_mdm6600_modem_wake(&ddata->modem_wake_work.work);
525 static const struct dev_pm_ops phy_mdm6600_pm_ops = {
526 SET_RUNTIME_PM_OPS(phy_mdm6600_runtime_suspend,
527 phy_mdm6600_runtime_resume, NULL)
530 static const struct of_device_id phy_mdm6600_id_table[] = {
531 { .compatible = "motorola,mapphone-mdm6600", },
534 MODULE_DEVICE_TABLE(of, phy_mdm6600_id_table);
536 static int phy_mdm6600_probe(struct platform_device *pdev)
538 struct phy_mdm6600 *ddata;
541 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
545 INIT_DELAYED_WORK(&ddata->bootup_work,
546 phy_mdm6600_deferred_power_on);
547 INIT_DELAYED_WORK(&ddata->status_work, phy_mdm6600_status);
548 INIT_DELAYED_WORK(&ddata->modem_wake_work, phy_mdm6600_modem_wake);
549 init_completion(&ddata->ack);
551 ddata->dev = &pdev->dev;
552 platform_set_drvdata(pdev, ddata);
554 error = phy_mdm6600_init_lines(ddata);
558 phy_mdm6600_init_irq(ddata);
560 ddata->generic_phy = devm_phy_create(ddata->dev, NULL, &gpio_usb_ops);
561 if (IS_ERR(ddata->generic_phy)) {
562 error = PTR_ERR(ddata->generic_phy);
566 phy_set_drvdata(ddata->generic_phy, ddata);
568 ddata->phy_provider =
569 devm_of_phy_provider_register(ddata->dev,
570 of_phy_simple_xlate);
571 if (IS_ERR(ddata->phy_provider)) {
572 error = PTR_ERR(ddata->phy_provider);
576 schedule_delayed_work(&ddata->bootup_work, 0);
579 * See phy_mdm6600_device_power_on(). We should be able
580 * to remove this eventually when ohci-platform can deal
581 * with -EPROBE_DEFER.
583 msleep(PHY_MDM6600_PHY_DELAY_MS + 500);
586 * Enable PM runtime only after PHY has been powered up properly.
587 * It is currently only needed after USB suspends mdm6600 and n_gsm
588 * needs to access the device. We don't want to do this earlier as
589 * gpio mode0 pin doubles as mdm6600 wake-up gpio.
591 pm_runtime_use_autosuspend(ddata->dev);
592 pm_runtime_set_autosuspend_delay(ddata->dev,
593 MDM6600_MODEM_IDLE_DELAY_MS);
594 pm_runtime_enable(ddata->dev);
595 error = pm_runtime_get_sync(ddata->dev);
597 dev_warn(ddata->dev, "failed to wake modem: %i\n", error);
598 pm_runtime_put_noidle(ddata->dev);
600 pm_runtime_mark_last_busy(ddata->dev);
601 pm_runtime_put_autosuspend(ddata->dev);
606 phy_mdm6600_device_power_off(ddata);
610 static int phy_mdm6600_remove(struct platform_device *pdev)
612 struct phy_mdm6600 *ddata = platform_get_drvdata(pdev);
613 struct gpio_desc *reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
615 pm_runtime_dont_use_autosuspend(ddata->dev);
616 pm_runtime_put_sync(ddata->dev);
617 pm_runtime_disable(ddata->dev);
620 wait_for_completion_timeout(&ddata->ack,
621 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS));
623 gpiod_set_value_cansleep(reset_gpio, 1);
624 phy_mdm6600_device_power_off(ddata);
626 cancel_delayed_work_sync(&ddata->modem_wake_work);
627 cancel_delayed_work_sync(&ddata->bootup_work);
628 cancel_delayed_work_sync(&ddata->status_work);
633 static struct platform_driver phy_mdm6600_driver = {
634 .probe = phy_mdm6600_probe,
635 .remove = phy_mdm6600_remove,
637 .name = "phy-mapphone-mdm6600",
638 .pm = &phy_mdm6600_pm_ops,
639 .of_match_table = of_match_ptr(phy_mdm6600_id_table),
643 module_platform_driver(phy_mdm6600_driver);
645 MODULE_ALIAS("platform:gpio_usb");
646 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
647 MODULE_DESCRIPTION("mdm6600 gpio usb phy driver");
648 MODULE_LICENSE("GPL v2");