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 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 */
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
,
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
,
40 enum phy_mdm6600_cmd_lines
{
44 PHY_MDM6600_NR_CMD_LINES
,
47 enum phy_mdm6600_status_lines
{
51 PHY_MDM6600_NR_STATUS_LINES
,
55 * MDM6600 command codes. These are based on Motorola Mapphone Linux
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
,
70 * MDM6600 status codes. These are based on Motorola Mapphone Linux
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
,
84 static const char * const
85 phy_mdm6600_status_name
[] = {
86 "off", "busy", "qc_dl", "ram_dl", "awake",
87 "asleep", "shutdown", "undefined",
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 */
108 static int phy_mdm6600_init(struct phy
*x
)
110 struct phy_mdm6600
*ddata
= phy_get_drvdata(x
);
111 struct gpio_desc
*enable_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_ENABLE
];
114 return -EPROBE_DEFER
;
116 gpiod_set_value_cansleep(enable_gpio
, 0);
121 static int phy_mdm6600_power_on(struct phy
*x
)
123 struct phy_mdm6600
*ddata
= phy_get_drvdata(x
);
124 struct gpio_desc
*enable_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_ENABLE
];
130 error
= pinctrl_pm_select_default_state(ddata
->dev
);
132 dev_warn(ddata
->dev
, "%s: error with default_state: %i\n",
135 gpiod_set_value_cansleep(enable_gpio
, 1);
137 /* Allow aggressive PM for USB, it's only needed for n_gsm port */
138 if (pm_runtime_enabled(&x
->dev
))
139 phy_pm_runtime_put(x
);
144 static int phy_mdm6600_power_off(struct phy
*x
)
146 struct phy_mdm6600
*ddata
= phy_get_drvdata(x
);
147 struct gpio_desc
*enable_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_ENABLE
];
153 /* Paired with phy_pm_runtime_put() in phy_mdm6600_power_on() */
154 if (pm_runtime_enabled(&x
->dev
)) {
155 error
= phy_pm_runtime_get(x
);
156 if (error
< 0 && error
!= -EINPROGRESS
)
157 dev_warn(ddata
->dev
, "%s: phy_pm_runtime_get: %i\n",
161 gpiod_set_value_cansleep(enable_gpio
, 0);
163 error
= pinctrl_pm_select_sleep_state(ddata
->dev
);
165 dev_warn(ddata
->dev
, "%s: error with sleep_state: %i\n",
171 static const struct phy_ops gpio_usb_ops
= {
172 .init
= phy_mdm6600_init
,
173 .power_on
= phy_mdm6600_power_on
,
174 .power_off
= phy_mdm6600_power_off
,
175 .owner
= THIS_MODULE
,
179 * phy_mdm6600_cmd() - send a command request to mdm6600
180 * @ddata: device driver data
181 * @val: value of cmd to be set
183 * Configures the three command request GPIOs to the specified value.
185 static void phy_mdm6600_cmd(struct phy_mdm6600
*ddata
, int val
)
187 DECLARE_BITMAP(values
, PHY_MDM6600_NR_CMD_LINES
);
191 gpiod_set_array_value_cansleep(PHY_MDM6600_NR_CMD_LINES
,
192 ddata
->cmd_gpios
->desc
,
193 ddata
->cmd_gpios
->info
, values
);
197 * phy_mdm6600_status() - read mdm6600 status lines
198 * @work: work structure
200 static void phy_mdm6600_status(struct work_struct
*work
)
202 struct phy_mdm6600
*ddata
;
204 DECLARE_BITMAP(values
, PHY_MDM6600_NR_STATUS_LINES
);
207 ddata
= container_of(work
, struct phy_mdm6600
, status_work
.work
);
210 error
= gpiod_get_array_value_cansleep(PHY_MDM6600_NR_STATUS_LINES
,
211 ddata
->status_gpios
->desc
,
212 ddata
->status_gpios
->info
,
217 ddata
->status
= values
[0] & ((1 << PHY_MDM6600_NR_STATUS_LINES
) - 1);
219 dev_info(dev
, "modem status: %i %s\n",
221 phy_mdm6600_status_name
[ddata
->status
]);
222 complete(&ddata
->ack
);
225 static irqreturn_t
phy_mdm6600_irq_thread(int irq
, void *data
)
227 struct phy_mdm6600
*ddata
= data
;
229 schedule_delayed_work(&ddata
->status_work
, msecs_to_jiffies(10));
235 * phy_mdm6600_wakeirq_thread - handle mode1 line OOB wake after booting
237 * @data: interrupt handler data
239 * GPIO mode1 is used initially as output to configure the USB boot
240 * mode for mdm6600. After booting it is used as input for OOB wake
241 * signal from mdm6600 to the SoC. Just use it for debug info only
244 static irqreturn_t
phy_mdm6600_wakeirq_thread(int irq
, void *data
)
246 struct phy_mdm6600
*ddata
= data
;
247 struct gpio_desc
*mode_gpio1
;
250 mode_gpio1
= ddata
->mode_gpios
->desc
[PHY_MDM6600_MODE1
];
251 wakeup
= gpiod_get_value(mode_gpio1
);
255 dev_dbg(ddata
->dev
, "OOB wake on mode_gpio1: %i\n", wakeup
);
256 error
= pm_runtime_get_sync(ddata
->dev
);
258 pm_runtime_put_noidle(ddata
->dev
);
263 /* Just wake-up and kick the autosuspend timer */
264 pm_runtime_mark_last_busy(ddata
->dev
);
265 pm_runtime_put_autosuspend(ddata
->dev
);
271 * phy_mdm6600_init_irq() - initialize mdm6600 status IRQ lines
272 * @ddata: device driver data
274 static void phy_mdm6600_init_irq(struct phy_mdm6600
*ddata
)
276 struct device
*dev
= ddata
->dev
;
279 for (i
= PHY_MDM6600_STATUS0
;
280 i
<= PHY_MDM6600_STATUS2
; i
++) {
281 struct gpio_desc
*gpio
= ddata
->status_gpios
->desc
[i
];
283 irq
= gpiod_to_irq(gpio
);
287 error
= devm_request_threaded_irq(dev
, irq
, NULL
,
288 phy_mdm6600_irq_thread
,
289 IRQF_TRIGGER_RISING
|
290 IRQF_TRIGGER_FALLING
|
295 dev_warn(dev
, "no modem status irq%i: %i\n",
300 struct phy_mdm6600_map
{
305 static const struct phy_mdm6600_map
306 phy_mdm6600_ctrl_gpio_map
[PHY_MDM6600_NR_CTRL_LINES
] = {
307 { "enable", GPIOD_OUT_LOW
, }, /* low = phy disabled */
308 { "power", GPIOD_OUT_LOW
, }, /* low = off */
309 { "reset", GPIOD_OUT_HIGH
, }, /* high = reset */
313 * phy_mdm6600_init_lines() - initialize mdm6600 GPIO lines
314 * @ddata: device driver data
316 static int phy_mdm6600_init_lines(struct phy_mdm6600
*ddata
)
318 struct device
*dev
= ddata
->dev
;
321 /* MDM6600 control lines */
322 for (i
= 0; i
< ARRAY_SIZE(phy_mdm6600_ctrl_gpio_map
); i
++) {
323 const struct phy_mdm6600_map
*map
=
324 &phy_mdm6600_ctrl_gpio_map
[i
];
325 struct gpio_desc
**gpio
= &ddata
->ctrl_gpios
[i
];
327 *gpio
= devm_gpiod_get(dev
, map
->name
, map
->direction
);
329 dev_info(dev
, "gpio %s error %li\n",
330 map
->name
, PTR_ERR(*gpio
));
331 return PTR_ERR(*gpio
);
335 /* MDM6600 USB start-up mode output lines */
336 ddata
->mode_gpios
= devm_gpiod_get_array(dev
, "motorola,mode",
338 if (IS_ERR(ddata
->mode_gpios
))
339 return PTR_ERR(ddata
->mode_gpios
);
341 if (ddata
->mode_gpios
->ndescs
!= PHY_MDM6600_NR_MODE_LINES
)
344 /* MDM6600 status input lines */
345 ddata
->status_gpios
= devm_gpiod_get_array(dev
, "motorola,status",
347 if (IS_ERR(ddata
->status_gpios
))
348 return PTR_ERR(ddata
->status_gpios
);
350 if (ddata
->status_gpios
->ndescs
!= PHY_MDM6600_NR_STATUS_LINES
)
353 /* MDM6600 cmd output lines */
354 ddata
->cmd_gpios
= devm_gpiod_get_array(dev
, "motorola,cmd",
356 if (IS_ERR(ddata
->cmd_gpios
))
357 return PTR_ERR(ddata
->cmd_gpios
);
359 if (ddata
->cmd_gpios
->ndescs
!= PHY_MDM6600_NR_CMD_LINES
)
366 * phy_mdm6600_device_power_on() - power on mdm6600 device
367 * @ddata: device driver data
369 * To get the integrated USB phy in MDM6600 takes some hoops. We must ensure
370 * the shared USB bootmode GPIOs are configured, then request modem start-up,
371 * reset and power-up.. And then we need to recycle the shared USB bootmode
372 * GPIOs as they are also used for Out of Band (OOB) wake for the USB and
373 * TS 27.010 serial mux.
375 static int phy_mdm6600_device_power_on(struct phy_mdm6600
*ddata
)
377 struct gpio_desc
*mode_gpio0
, *mode_gpio1
, *reset_gpio
, *power_gpio
;
378 int error
= 0, wakeirq
;
380 mode_gpio0
= ddata
->mode_gpios
->desc
[PHY_MDM6600_MODE0
];
381 mode_gpio1
= ddata
->mode_gpios
->desc
[PHY_MDM6600_MODE1
];
382 reset_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_RESET
];
383 power_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_POWER
];
386 * Shared GPIOs must be low for normal USB mode. After booting
387 * they are used for OOB wake signaling. These can be also used
388 * to configure USB flashing mode later on based on a module
391 gpiod_set_value_cansleep(mode_gpio0
, 0);
392 gpiod_set_value_cansleep(mode_gpio1
, 0);
394 /* Request start-up mode */
395 phy_mdm6600_cmd(ddata
, PHY_MDM6600_CMD_NO_BYPASS
);
397 /* Request a reset first */
398 gpiod_set_value_cansleep(reset_gpio
, 0);
401 /* Toggle power GPIO to request mdm6600 to start */
402 gpiod_set_value_cansleep(power_gpio
, 1);
404 gpiod_set_value_cansleep(power_gpio
, 0);
407 * Looks like the USB PHY needs between 2.2 to 4 seconds.
408 * If we try to use it before that, we will get L3 errors
409 * from omap-usb-host trying to access the PHY. See also
410 * phy_mdm6600_init() for -EPROBE_DEFER.
412 msleep(PHY_MDM6600_PHY_DELAY_MS
);
413 ddata
->enabled
= true;
415 /* Booting up the rest of MDM6600 will take total about 8 seconds */
416 dev_info(ddata
->dev
, "Waiting for power up request to complete..\n");
417 if (wait_for_completion_timeout(&ddata
->ack
,
418 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS
))) {
419 if (ddata
->status
> PHY_MDM6600_STATUS_PANIC
&&
420 ddata
->status
< PHY_MDM6600_STATUS_SHUTDOWN_ACK
)
421 dev_info(ddata
->dev
, "Powered up OK\n");
423 ddata
->enabled
= false;
425 dev_err(ddata
->dev
, "Timed out powering up\n");
428 /* Reconfigure mode1 GPIO as input for OOB wake */
429 gpiod_direction_input(mode_gpio1
);
431 wakeirq
= gpiod_to_irq(mode_gpio1
);
435 error
= devm_request_threaded_irq(ddata
->dev
, wakeirq
, NULL
,
436 phy_mdm6600_wakeirq_thread
,
437 IRQF_TRIGGER_RISING
|
438 IRQF_TRIGGER_FALLING
|
443 dev_warn(ddata
->dev
, "no modem wakeirq irq%i: %i\n",
446 ddata
->running
= true;
452 * phy_mdm6600_device_power_off() - power off mdm6600 device
453 * @ddata: device driver data
455 static void phy_mdm6600_device_power_off(struct phy_mdm6600
*ddata
)
457 struct gpio_desc
*reset_gpio
=
458 ddata
->ctrl_gpios
[PHY_MDM6600_RESET
];
460 ddata
->enabled
= false;
461 phy_mdm6600_cmd(ddata
, PHY_MDM6600_CMD_BP_SHUTDOWN_REQ
);
464 gpiod_set_value_cansleep(reset_gpio
, 1);
466 dev_info(ddata
->dev
, "Waiting for power down request to complete.. ");
467 if (wait_for_completion_timeout(&ddata
->ack
,
468 msecs_to_jiffies(5000))) {
469 if (ddata
->status
== PHY_MDM6600_STATUS_PANIC
)
470 dev_info(ddata
->dev
, "Powered down OK\n");
472 dev_err(ddata
->dev
, "Timed out powering down\n");
476 static void phy_mdm6600_deferred_power_on(struct work_struct
*work
)
478 struct phy_mdm6600
*ddata
;
481 ddata
= container_of(work
, struct phy_mdm6600
, bootup_work
.work
);
483 error
= phy_mdm6600_device_power_on(ddata
);
485 dev_err(ddata
->dev
, "Device not functional\n");
489 * USB suspend puts mdm6600 into low power mode. For any n_gsm using apps,
490 * we need to keep the modem awake by kicking it's mode0 GPIO. This will
491 * keep the modem awake for about 1.2 seconds. When no n_gsm apps are using
492 * the modem, runtime PM auto mode can be enabled so modem can enter low
495 static void phy_mdm6600_wake_modem(struct phy_mdm6600
*ddata
)
497 struct gpio_desc
*mode_gpio0
;
499 mode_gpio0
= ddata
->mode_gpios
->desc
[PHY_MDM6600_MODE0
];
500 gpiod_set_value_cansleep(mode_gpio0
, 1);
502 gpiod_set_value_cansleep(mode_gpio0
, 0);
506 msleep(MDM6600_MODEM_WAKE_DELAY_MS
);
509 static void phy_mdm6600_modem_wake(struct work_struct
*work
)
511 struct phy_mdm6600
*ddata
;
513 ddata
= container_of(work
, struct phy_mdm6600
, modem_wake_work
.work
);
514 phy_mdm6600_wake_modem(ddata
);
517 * The modem does not always stay awake 1.2 seconds after toggling
518 * the wake GPIO, and sometimes it idles after about some 600 ms
519 * making writes time out.
521 schedule_delayed_work(&ddata
->modem_wake_work
,
522 msecs_to_jiffies(PHY_MDM6600_WAKE_KICK_MS
));
525 static int __maybe_unused
phy_mdm6600_runtime_suspend(struct device
*dev
)
527 struct phy_mdm6600
*ddata
= dev_get_drvdata(dev
);
529 cancel_delayed_work_sync(&ddata
->modem_wake_work
);
530 ddata
->awake
= false;
535 static int __maybe_unused
phy_mdm6600_runtime_resume(struct device
*dev
)
537 struct phy_mdm6600
*ddata
= dev_get_drvdata(dev
);
539 phy_mdm6600_modem_wake(&ddata
->modem_wake_work
.work
);
545 static const struct dev_pm_ops phy_mdm6600_pm_ops
= {
546 SET_RUNTIME_PM_OPS(phy_mdm6600_runtime_suspend
,
547 phy_mdm6600_runtime_resume
, NULL
)
550 static const struct of_device_id phy_mdm6600_id_table
[] = {
551 { .compatible
= "motorola,mapphone-mdm6600", },
554 MODULE_DEVICE_TABLE(of
, phy_mdm6600_id_table
);
556 static int phy_mdm6600_probe(struct platform_device
*pdev
)
558 struct phy_mdm6600
*ddata
;
561 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
565 INIT_DELAYED_WORK(&ddata
->bootup_work
,
566 phy_mdm6600_deferred_power_on
);
567 INIT_DELAYED_WORK(&ddata
->status_work
, phy_mdm6600_status
);
568 INIT_DELAYED_WORK(&ddata
->modem_wake_work
, phy_mdm6600_modem_wake
);
569 init_completion(&ddata
->ack
);
571 ddata
->dev
= &pdev
->dev
;
572 platform_set_drvdata(pdev
, ddata
);
574 /* Active state selected in phy_mdm6600_power_on() */
575 error
= pinctrl_pm_select_sleep_state(ddata
->dev
);
577 dev_warn(ddata
->dev
, "%s: error with sleep_state: %i\n",
580 error
= phy_mdm6600_init_lines(ddata
);
584 phy_mdm6600_init_irq(ddata
);
585 schedule_delayed_work(&ddata
->bootup_work
, 0);
588 * See phy_mdm6600_device_power_on(). We should be able
589 * to remove this eventually when ohci-platform can deal
590 * with -EPROBE_DEFER.
592 msleep(PHY_MDM6600_PHY_DELAY_MS
+ 500);
595 * Enable PM runtime only after PHY has been powered up properly.
596 * It is currently only needed after USB suspends mdm6600 and n_gsm
597 * needs to access the device. We don't want to do this earlier as
598 * gpio mode0 pin doubles as mdm6600 wake-up gpio.
600 pm_runtime_use_autosuspend(ddata
->dev
);
601 pm_runtime_set_autosuspend_delay(ddata
->dev
,
602 MDM6600_MODEM_IDLE_DELAY_MS
);
603 pm_runtime_enable(ddata
->dev
);
604 error
= pm_runtime_get_sync(ddata
->dev
);
606 dev_warn(ddata
->dev
, "failed to wake modem: %i\n", error
);
607 pm_runtime_put_noidle(ddata
->dev
);
611 ddata
->generic_phy
= devm_phy_create(ddata
->dev
, NULL
, &gpio_usb_ops
);
612 if (IS_ERR(ddata
->generic_phy
)) {
613 error
= PTR_ERR(ddata
->generic_phy
);
617 phy_set_drvdata(ddata
->generic_phy
, ddata
);
619 ddata
->phy_provider
=
620 devm_of_phy_provider_register(ddata
->dev
,
621 of_phy_simple_xlate
);
622 if (IS_ERR(ddata
->phy_provider
))
623 error
= PTR_ERR(ddata
->phy_provider
);
626 pm_runtime_mark_last_busy(ddata
->dev
);
627 pm_runtime_put_autosuspend(ddata
->dev
);
631 phy_mdm6600_device_power_off(ddata
);
636 static int phy_mdm6600_remove(struct platform_device
*pdev
)
638 struct phy_mdm6600
*ddata
= platform_get_drvdata(pdev
);
639 struct gpio_desc
*reset_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_RESET
];
641 pm_runtime_dont_use_autosuspend(ddata
->dev
);
642 pm_runtime_put_sync(ddata
->dev
);
643 pm_runtime_disable(ddata
->dev
);
646 wait_for_completion_timeout(&ddata
->ack
,
647 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS
));
649 gpiod_set_value_cansleep(reset_gpio
, 1);
650 phy_mdm6600_device_power_off(ddata
);
652 cancel_delayed_work_sync(&ddata
->modem_wake_work
);
653 cancel_delayed_work_sync(&ddata
->bootup_work
);
654 cancel_delayed_work_sync(&ddata
->status_work
);
659 static struct platform_driver phy_mdm6600_driver
= {
660 .probe
= phy_mdm6600_probe
,
661 .remove
= phy_mdm6600_remove
,
663 .name
= "phy-mapphone-mdm6600",
664 .pm
= &phy_mdm6600_pm_ops
,
665 .of_match_table
= of_match_ptr(phy_mdm6600_id_table
),
669 module_platform_driver(phy_mdm6600_driver
);
671 MODULE_ALIAS("platform:gpio_usb");
672 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
673 MODULE_DESCRIPTION("mdm6600 gpio usb phy driver");
674 MODULE_LICENSE("GPL v2");