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>
19 #include <linux/pinctrl/consumer.h>
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 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];
129 error = pinctrl_pm_select_default_state(ddata->dev);
131 dev_warn(ddata->dev, "%s: error with default_state: %i\n",
134 gpiod_set_value_cansleep(enable_gpio, 1);
136 /* Allow aggressive PM for USB, it's only needed for n_gsm port */
137 if (pm_runtime_enabled(&x->dev))
138 phy_pm_runtime_put(x);
143 static int phy_mdm6600_power_off(struct phy *x)
145 struct phy_mdm6600 *ddata = phy_get_drvdata(x);
146 struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
152 /* Paired with phy_pm_runtime_put() in phy_mdm6600_power_on() */
153 if (pm_runtime_enabled(&x->dev)) {
154 error = phy_pm_runtime_get(x);
155 if (error < 0 && error != -EINPROGRESS)
156 dev_warn(ddata->dev, "%s: phy_pm_runtime_get: %i\n",
160 gpiod_set_value_cansleep(enable_gpio, 0);
162 error = pinctrl_pm_select_sleep_state(ddata->dev);
164 dev_warn(ddata->dev, "%s: error with sleep_state: %i\n",
170 static const struct phy_ops gpio_usb_ops = {
171 .init = phy_mdm6600_init,
172 .power_on = phy_mdm6600_power_on,
173 .power_off = phy_mdm6600_power_off,
174 .owner = THIS_MODULE,
178 * phy_mdm6600_cmd() - send a command request to mdm6600
179 * @ddata: device driver data
181 * Configures the three command request GPIOs to the specified value.
183 static void phy_mdm6600_cmd(struct phy_mdm6600 *ddata, int val)
185 DECLARE_BITMAP(values, PHY_MDM6600_NR_CMD_LINES);
189 gpiod_set_array_value_cansleep(PHY_MDM6600_NR_CMD_LINES,
190 ddata->cmd_gpios->desc,
191 ddata->cmd_gpios->info, values);
195 * phy_mdm6600_status() - read mdm6600 status lines
196 * @ddata: device driver data
198 static void phy_mdm6600_status(struct work_struct *work)
200 struct phy_mdm6600 *ddata;
202 DECLARE_BITMAP(values, PHY_MDM6600_NR_STATUS_LINES);
203 int error, i, val = 0;
205 ddata = container_of(work, struct phy_mdm6600, status_work.work);
208 error = gpiod_get_array_value_cansleep(PHY_MDM6600_NR_STATUS_LINES,
209 ddata->status_gpios->desc,
210 ddata->status_gpios->info,
215 for (i = 0; i < PHY_MDM6600_NR_STATUS_LINES; i++) {
216 val |= test_bit(i, values) << i;
217 dev_dbg(ddata->dev, "XXX %s: i: %i values[i]: %i val: %i\n",
218 __func__, i, test_bit(i, values), val);
220 ddata->status = values[0];
222 dev_info(dev, "modem status: %i %s\n",
224 phy_mdm6600_status_name[ddata->status & 7]);
225 complete(&ddata->ack);
228 static irqreturn_t phy_mdm6600_irq_thread(int irq, void *data)
230 struct phy_mdm6600 *ddata = data;
232 schedule_delayed_work(&ddata->status_work, msecs_to_jiffies(10));
238 * phy_mdm6600_wakeirq_thread - handle mode1 line OOB wake after booting
240 * @data: interrupt handler data
242 * GPIO mode1 is used initially as output to configure the USB boot
243 * mode for mdm6600. After booting it is used as input for OOB wake
244 * signal from mdm6600 to the SoC. Just use it for debug info only
247 static irqreturn_t phy_mdm6600_wakeirq_thread(int irq, void *data)
249 struct phy_mdm6600 *ddata = data;
250 struct gpio_desc *mode_gpio1;
252 mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
253 dev_dbg(ddata->dev, "OOB wake on mode_gpio1: %i\n",
254 gpiod_get_value(mode_gpio1));
260 * phy_mdm6600_init_irq() - initialize mdm6600 status IRQ lines
261 * @ddata: device driver data
263 static void phy_mdm6600_init_irq(struct phy_mdm6600 *ddata)
265 struct device *dev = ddata->dev;
268 for (i = PHY_MDM6600_STATUS0;
269 i <= PHY_MDM6600_STATUS2; i++) {
270 struct gpio_desc *gpio = ddata->status_gpios->desc[i];
272 irq = gpiod_to_irq(gpio);
276 error = devm_request_threaded_irq(dev, irq, NULL,
277 phy_mdm6600_irq_thread,
278 IRQF_TRIGGER_RISING |
279 IRQF_TRIGGER_FALLING |
284 dev_warn(dev, "no modem status irq%i: %i\n",
289 struct phy_mdm6600_map {
294 static const struct phy_mdm6600_map
295 phy_mdm6600_ctrl_gpio_map[PHY_MDM6600_NR_CTRL_LINES] = {
296 { "enable", GPIOD_OUT_LOW, }, /* low = phy disabled */
297 { "power", GPIOD_OUT_LOW, }, /* low = off */
298 { "reset", GPIOD_OUT_HIGH, }, /* high = reset */
302 * phy_mdm6600_init_lines() - initialize mdm6600 GPIO lines
303 * @ddata: device driver data
305 static int phy_mdm6600_init_lines(struct phy_mdm6600 *ddata)
307 struct device *dev = ddata->dev;
310 /* MDM6600 control lines */
311 for (i = 0; i < ARRAY_SIZE(phy_mdm6600_ctrl_gpio_map); i++) {
312 const struct phy_mdm6600_map *map =
313 &phy_mdm6600_ctrl_gpio_map[i];
314 struct gpio_desc **gpio = &ddata->ctrl_gpios[i];
316 *gpio = devm_gpiod_get(dev, map->name, map->direction);
318 dev_info(dev, "gpio %s error %li\n",
319 map->name, PTR_ERR(*gpio));
320 return PTR_ERR(*gpio);
324 /* MDM6600 USB start-up mode output lines */
325 ddata->mode_gpios = devm_gpiod_get_array(dev, "motorola,mode",
327 if (IS_ERR(ddata->mode_gpios))
328 return PTR_ERR(ddata->mode_gpios);
330 if (ddata->mode_gpios->ndescs != PHY_MDM6600_NR_MODE_LINES)
333 /* MDM6600 status input lines */
334 ddata->status_gpios = devm_gpiod_get_array(dev, "motorola,status",
336 if (IS_ERR(ddata->status_gpios))
337 return PTR_ERR(ddata->status_gpios);
339 if (ddata->status_gpios->ndescs != PHY_MDM6600_NR_STATUS_LINES)
342 /* MDM6600 cmd output lines */
343 ddata->cmd_gpios = devm_gpiod_get_array(dev, "motorola,cmd",
345 if (IS_ERR(ddata->cmd_gpios))
346 return PTR_ERR(ddata->cmd_gpios);
348 if (ddata->cmd_gpios->ndescs != PHY_MDM6600_NR_CMD_LINES)
355 * phy_mdm6600_device_power_on() - power on mdm6600 device
356 * @ddata: device driver data
358 * To get the integrated USB phy in MDM6600 takes some hoops. We must ensure
359 * the shared USB bootmode GPIOs are configured, then request modem start-up,
360 * reset and power-up.. And then we need to recycle the shared USB bootmode
361 * GPIOs as they are also used for Out of Band (OOB) wake for the USB and
362 * TS 27.010 serial mux.
364 static int phy_mdm6600_device_power_on(struct phy_mdm6600 *ddata)
366 struct gpio_desc *mode_gpio0, *mode_gpio1, *reset_gpio, *power_gpio;
367 int error = 0, wakeirq;
369 mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
370 mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
371 reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
372 power_gpio = ddata->ctrl_gpios[PHY_MDM6600_POWER];
375 * Shared GPIOs must be low for normal USB mode. After booting
376 * they are used for OOB wake signaling. These can be also used
377 * to configure USB flashing mode later on based on a module
380 gpiod_set_value_cansleep(mode_gpio0, 0);
381 gpiod_set_value_cansleep(mode_gpio1, 0);
383 /* Request start-up mode */
384 phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_NO_BYPASS);
386 /* Request a reset first */
387 gpiod_set_value_cansleep(reset_gpio, 0);
390 /* Toggle power GPIO to request mdm6600 to start */
391 gpiod_set_value_cansleep(power_gpio, 1);
393 gpiod_set_value_cansleep(power_gpio, 0);
396 * Looks like the USB PHY needs between 2.2 to 4 seconds.
397 * If we try to use it before that, we will get L3 errors
398 * from omap-usb-host trying to access the PHY. See also
399 * phy_mdm6600_init() for -EPROBE_DEFER.
401 msleep(PHY_MDM6600_PHY_DELAY_MS);
402 ddata->enabled = true;
404 /* Booting up the rest of MDM6600 will take total about 8 seconds */
405 dev_info(ddata->dev, "Waiting for power up request to complete..\n");
406 if (wait_for_completion_timeout(&ddata->ack,
407 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS))) {
408 if (ddata->status > PHY_MDM6600_STATUS_PANIC &&
409 ddata->status < PHY_MDM6600_STATUS_SHUTDOWN_ACK)
410 dev_info(ddata->dev, "Powered up OK\n");
412 ddata->enabled = false;
414 dev_err(ddata->dev, "Timed out powering up\n");
417 /* Reconfigure mode1 GPIO as input for OOB wake */
418 gpiod_direction_input(mode_gpio1);
420 wakeirq = gpiod_to_irq(mode_gpio1);
424 error = devm_request_threaded_irq(ddata->dev, wakeirq, NULL,
425 phy_mdm6600_wakeirq_thread,
426 IRQF_TRIGGER_RISING |
427 IRQF_TRIGGER_FALLING |
432 dev_warn(ddata->dev, "no modem wakeirq irq%i: %i\n",
435 ddata->running = true;
441 * phy_mdm6600_device_power_off() - power off mdm6600 device
442 * @ddata: device driver data
444 static void phy_mdm6600_device_power_off(struct phy_mdm6600 *ddata)
446 struct gpio_desc *reset_gpio =
447 ddata->ctrl_gpios[PHY_MDM6600_RESET];
449 ddata->enabled = false;
450 phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_BP_SHUTDOWN_REQ);
453 gpiod_set_value_cansleep(reset_gpio, 1);
455 dev_info(ddata->dev, "Waiting for power down request to complete.. ");
456 if (wait_for_completion_timeout(&ddata->ack,
457 msecs_to_jiffies(5000))) {
458 if (ddata->status == PHY_MDM6600_STATUS_PANIC)
459 dev_info(ddata->dev, "Powered down OK\n");
461 dev_err(ddata->dev, "Timed out powering down\n");
465 static void phy_mdm6600_deferred_power_on(struct work_struct *work)
467 struct phy_mdm6600 *ddata;
470 ddata = container_of(work, struct phy_mdm6600, bootup_work.work);
472 error = phy_mdm6600_device_power_on(ddata);
474 dev_err(ddata->dev, "Device not functional\n");
478 * USB suspend puts mdm6600 into low power mode. For any n_gsm using apps,
479 * we need to keep the modem awake by kicking it's mode0 GPIO. This will
480 * keep the modem awake for about 1.2 seconds. When no n_gsm apps are using
481 * the modem, runtime PM auto mode can be enabled so modem can enter low
484 static void phy_mdm6600_wake_modem(struct phy_mdm6600 *ddata)
486 struct gpio_desc *mode_gpio0;
488 mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
489 gpiod_set_value_cansleep(mode_gpio0, 1);
491 gpiod_set_value_cansleep(mode_gpio0, 0);
495 msleep(MDM6600_MODEM_WAKE_DELAY_MS);
498 static void phy_mdm6600_modem_wake(struct work_struct *work)
500 struct phy_mdm6600 *ddata;
502 ddata = container_of(work, struct phy_mdm6600, modem_wake_work.work);
503 phy_mdm6600_wake_modem(ddata);
504 schedule_delayed_work(&ddata->modem_wake_work,
505 msecs_to_jiffies(MDM6600_MODEM_IDLE_DELAY_MS));
508 static int __maybe_unused phy_mdm6600_runtime_suspend(struct device *dev)
510 struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
512 cancel_delayed_work_sync(&ddata->modem_wake_work);
513 ddata->awake = false;
518 static int __maybe_unused phy_mdm6600_runtime_resume(struct device *dev)
520 struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
522 phy_mdm6600_modem_wake(&ddata->modem_wake_work.work);
528 static const struct dev_pm_ops phy_mdm6600_pm_ops = {
529 SET_RUNTIME_PM_OPS(phy_mdm6600_runtime_suspend,
530 phy_mdm6600_runtime_resume, NULL)
533 static const struct of_device_id phy_mdm6600_id_table[] = {
534 { .compatible = "motorola,mapphone-mdm6600", },
537 MODULE_DEVICE_TABLE(of, phy_mdm6600_id_table);
539 static int phy_mdm6600_probe(struct platform_device *pdev)
541 struct phy_mdm6600 *ddata;
544 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
548 INIT_DELAYED_WORK(&ddata->bootup_work,
549 phy_mdm6600_deferred_power_on);
550 INIT_DELAYED_WORK(&ddata->status_work, phy_mdm6600_status);
551 INIT_DELAYED_WORK(&ddata->modem_wake_work, phy_mdm6600_modem_wake);
552 init_completion(&ddata->ack);
554 ddata->dev = &pdev->dev;
555 platform_set_drvdata(pdev, ddata);
557 /* Active state selected in phy_mdm6600_power_on() */
558 error = pinctrl_pm_select_sleep_state(ddata->dev);
560 dev_warn(ddata->dev, "%s: error with sleep_state: %i\n",
563 error = phy_mdm6600_init_lines(ddata);
567 phy_mdm6600_init_irq(ddata);
568 schedule_delayed_work(&ddata->bootup_work, 0);
571 * See phy_mdm6600_device_power_on(). We should be able
572 * to remove this eventually when ohci-platform can deal
573 * with -EPROBE_DEFER.
575 msleep(PHY_MDM6600_PHY_DELAY_MS + 500);
578 * Enable PM runtime only after PHY has been powered up properly.
579 * It is currently only needed after USB suspends mdm6600 and n_gsm
580 * needs to access the device. We don't want to do this earlier as
581 * gpio mode0 pin doubles as mdm6600 wake-up gpio.
583 pm_runtime_use_autosuspend(ddata->dev);
584 pm_runtime_set_autosuspend_delay(ddata->dev,
585 MDM6600_MODEM_IDLE_DELAY_MS);
586 pm_runtime_enable(ddata->dev);
587 error = pm_runtime_get_sync(ddata->dev);
589 dev_warn(ddata->dev, "failed to wake modem: %i\n", error);
590 pm_runtime_put_noidle(ddata->dev);
594 ddata->generic_phy = devm_phy_create(ddata->dev, NULL, &gpio_usb_ops);
595 if (IS_ERR(ddata->generic_phy)) {
596 error = PTR_ERR(ddata->generic_phy);
600 phy_set_drvdata(ddata->generic_phy, ddata);
602 ddata->phy_provider =
603 devm_of_phy_provider_register(ddata->dev,
604 of_phy_simple_xlate);
605 if (IS_ERR(ddata->phy_provider))
606 error = PTR_ERR(ddata->phy_provider);
609 pm_runtime_mark_last_busy(ddata->dev);
610 pm_runtime_put_autosuspend(ddata->dev);
614 phy_mdm6600_device_power_off(ddata);
619 static int phy_mdm6600_remove(struct platform_device *pdev)
621 struct phy_mdm6600 *ddata = platform_get_drvdata(pdev);
622 struct gpio_desc *reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
624 pm_runtime_dont_use_autosuspend(ddata->dev);
625 pm_runtime_put_sync(ddata->dev);
626 pm_runtime_disable(ddata->dev);
629 wait_for_completion_timeout(&ddata->ack,
630 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS));
632 gpiod_set_value_cansleep(reset_gpio, 1);
633 phy_mdm6600_device_power_off(ddata);
635 cancel_delayed_work_sync(&ddata->modem_wake_work);
636 cancel_delayed_work_sync(&ddata->bootup_work);
637 cancel_delayed_work_sync(&ddata->status_work);
642 static struct platform_driver phy_mdm6600_driver = {
643 .probe = phy_mdm6600_probe,
644 .remove = phy_mdm6600_remove,
646 .name = "phy-mapphone-mdm6600",
647 .pm = &phy_mdm6600_pm_ops,
648 .of_match_table = of_match_ptr(phy_mdm6600_id_table),
652 module_platform_driver(phy_mdm6600_driver);
654 MODULE_ALIAS("platform:gpio_usb");
655 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
656 MODULE_DESCRIPTION("mdm6600 gpio usb phy driver");
657 MODULE_LICENSE("GPL v2");