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
182 * Configures the three command request GPIOs to the specified value.
184 static void phy_mdm6600_cmd(struct phy_mdm6600
*ddata
, int val
)
186 DECLARE_BITMAP(values
, PHY_MDM6600_NR_CMD_LINES
);
190 gpiod_set_array_value_cansleep(PHY_MDM6600_NR_CMD_LINES
,
191 ddata
->cmd_gpios
->desc
,
192 ddata
->cmd_gpios
->info
, values
);
196 * phy_mdm6600_status() - read mdm6600 status lines
197 * @ddata: device driver data
199 static void phy_mdm6600_status(struct work_struct
*work
)
201 struct phy_mdm6600
*ddata
;
203 DECLARE_BITMAP(values
, PHY_MDM6600_NR_STATUS_LINES
);
206 ddata
= container_of(work
, struct phy_mdm6600
, status_work
.work
);
209 error
= gpiod_get_array_value_cansleep(PHY_MDM6600_NR_STATUS_LINES
,
210 ddata
->status_gpios
->desc
,
211 ddata
->status_gpios
->info
,
216 ddata
->status
= values
[0] & ((1 << PHY_MDM6600_NR_STATUS_LINES
) - 1);
218 dev_info(dev
, "modem status: %i %s\n",
220 phy_mdm6600_status_name
[ddata
->status
]);
221 complete(&ddata
->ack
);
224 static irqreturn_t
phy_mdm6600_irq_thread(int irq
, void *data
)
226 struct phy_mdm6600
*ddata
= data
;
228 schedule_delayed_work(&ddata
->status_work
, msecs_to_jiffies(10));
234 * phy_mdm6600_wakeirq_thread - handle mode1 line OOB wake after booting
236 * @data: interrupt handler data
238 * GPIO mode1 is used initially as output to configure the USB boot
239 * mode for mdm6600. After booting it is used as input for OOB wake
240 * signal from mdm6600 to the SoC. Just use it for debug info only
243 static irqreturn_t
phy_mdm6600_wakeirq_thread(int irq
, void *data
)
245 struct phy_mdm6600
*ddata
= data
;
246 struct gpio_desc
*mode_gpio1
;
249 mode_gpio1
= ddata
->mode_gpios
->desc
[PHY_MDM6600_MODE1
];
250 wakeup
= gpiod_get_value(mode_gpio1
);
254 dev_dbg(ddata
->dev
, "OOB wake on mode_gpio1: %i\n", wakeup
);
255 error
= pm_runtime_get_sync(ddata
->dev
);
257 pm_runtime_put_noidle(ddata
->dev
);
262 /* Just wake-up and kick the autosuspend timer */
263 pm_runtime_mark_last_busy(ddata
->dev
);
264 pm_runtime_put_autosuspend(ddata
->dev
);
270 * phy_mdm6600_init_irq() - initialize mdm6600 status IRQ lines
271 * @ddata: device driver data
273 static void phy_mdm6600_init_irq(struct phy_mdm6600
*ddata
)
275 struct device
*dev
= ddata
->dev
;
278 for (i
= PHY_MDM6600_STATUS0
;
279 i
<= PHY_MDM6600_STATUS2
; i
++) {
280 struct gpio_desc
*gpio
= ddata
->status_gpios
->desc
[i
];
282 irq
= gpiod_to_irq(gpio
);
286 error
= devm_request_threaded_irq(dev
, irq
, NULL
,
287 phy_mdm6600_irq_thread
,
288 IRQF_TRIGGER_RISING
|
289 IRQF_TRIGGER_FALLING
|
294 dev_warn(dev
, "no modem status irq%i: %i\n",
299 struct phy_mdm6600_map
{
304 static const struct phy_mdm6600_map
305 phy_mdm6600_ctrl_gpio_map
[PHY_MDM6600_NR_CTRL_LINES
] = {
306 { "enable", GPIOD_OUT_LOW
, }, /* low = phy disabled */
307 { "power", GPIOD_OUT_LOW
, }, /* low = off */
308 { "reset", GPIOD_OUT_HIGH
, }, /* high = reset */
312 * phy_mdm6600_init_lines() - initialize mdm6600 GPIO lines
313 * @ddata: device driver data
315 static int phy_mdm6600_init_lines(struct phy_mdm6600
*ddata
)
317 struct device
*dev
= ddata
->dev
;
320 /* MDM6600 control lines */
321 for (i
= 0; i
< ARRAY_SIZE(phy_mdm6600_ctrl_gpio_map
); i
++) {
322 const struct phy_mdm6600_map
*map
=
323 &phy_mdm6600_ctrl_gpio_map
[i
];
324 struct gpio_desc
**gpio
= &ddata
->ctrl_gpios
[i
];
326 *gpio
= devm_gpiod_get(dev
, map
->name
, map
->direction
);
328 dev_info(dev
, "gpio %s error %li\n",
329 map
->name
, PTR_ERR(*gpio
));
330 return PTR_ERR(*gpio
);
334 /* MDM6600 USB start-up mode output lines */
335 ddata
->mode_gpios
= devm_gpiod_get_array(dev
, "motorola,mode",
337 if (IS_ERR(ddata
->mode_gpios
))
338 return PTR_ERR(ddata
->mode_gpios
);
340 if (ddata
->mode_gpios
->ndescs
!= PHY_MDM6600_NR_MODE_LINES
)
343 /* MDM6600 status input lines */
344 ddata
->status_gpios
= devm_gpiod_get_array(dev
, "motorola,status",
346 if (IS_ERR(ddata
->status_gpios
))
347 return PTR_ERR(ddata
->status_gpios
);
349 if (ddata
->status_gpios
->ndescs
!= PHY_MDM6600_NR_STATUS_LINES
)
352 /* MDM6600 cmd output lines */
353 ddata
->cmd_gpios
= devm_gpiod_get_array(dev
, "motorola,cmd",
355 if (IS_ERR(ddata
->cmd_gpios
))
356 return PTR_ERR(ddata
->cmd_gpios
);
358 if (ddata
->cmd_gpios
->ndescs
!= PHY_MDM6600_NR_CMD_LINES
)
365 * phy_mdm6600_device_power_on() - power on mdm6600 device
366 * @ddata: device driver data
368 * To get the integrated USB phy in MDM6600 takes some hoops. We must ensure
369 * the shared USB bootmode GPIOs are configured, then request modem start-up,
370 * reset and power-up.. And then we need to recycle the shared USB bootmode
371 * GPIOs as they are also used for Out of Band (OOB) wake for the USB and
372 * TS 27.010 serial mux.
374 static int phy_mdm6600_device_power_on(struct phy_mdm6600
*ddata
)
376 struct gpio_desc
*mode_gpio0
, *mode_gpio1
, *reset_gpio
, *power_gpio
;
377 int error
= 0, wakeirq
;
379 mode_gpio0
= ddata
->mode_gpios
->desc
[PHY_MDM6600_MODE0
];
380 mode_gpio1
= ddata
->mode_gpios
->desc
[PHY_MDM6600_MODE1
];
381 reset_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_RESET
];
382 power_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_POWER
];
385 * Shared GPIOs must be low for normal USB mode. After booting
386 * they are used for OOB wake signaling. These can be also used
387 * to configure USB flashing mode later on based on a module
390 gpiod_set_value_cansleep(mode_gpio0
, 0);
391 gpiod_set_value_cansleep(mode_gpio1
, 0);
393 /* Request start-up mode */
394 phy_mdm6600_cmd(ddata
, PHY_MDM6600_CMD_NO_BYPASS
);
396 /* Request a reset first */
397 gpiod_set_value_cansleep(reset_gpio
, 0);
400 /* Toggle power GPIO to request mdm6600 to start */
401 gpiod_set_value_cansleep(power_gpio
, 1);
403 gpiod_set_value_cansleep(power_gpio
, 0);
406 * Looks like the USB PHY needs between 2.2 to 4 seconds.
407 * If we try to use it before that, we will get L3 errors
408 * from omap-usb-host trying to access the PHY. See also
409 * phy_mdm6600_init() for -EPROBE_DEFER.
411 msleep(PHY_MDM6600_PHY_DELAY_MS
);
412 ddata
->enabled
= true;
414 /* Booting up the rest of MDM6600 will take total about 8 seconds */
415 dev_info(ddata
->dev
, "Waiting for power up request to complete..\n");
416 if (wait_for_completion_timeout(&ddata
->ack
,
417 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS
))) {
418 if (ddata
->status
> PHY_MDM6600_STATUS_PANIC
&&
419 ddata
->status
< PHY_MDM6600_STATUS_SHUTDOWN_ACK
)
420 dev_info(ddata
->dev
, "Powered up OK\n");
422 ddata
->enabled
= false;
424 dev_err(ddata
->dev
, "Timed out powering up\n");
427 /* Reconfigure mode1 GPIO as input for OOB wake */
428 gpiod_direction_input(mode_gpio1
);
430 wakeirq
= gpiod_to_irq(mode_gpio1
);
434 error
= devm_request_threaded_irq(ddata
->dev
, wakeirq
, NULL
,
435 phy_mdm6600_wakeirq_thread
,
436 IRQF_TRIGGER_RISING
|
437 IRQF_TRIGGER_FALLING
|
442 dev_warn(ddata
->dev
, "no modem wakeirq irq%i: %i\n",
445 ddata
->running
= true;
451 * phy_mdm6600_device_power_off() - power off mdm6600 device
452 * @ddata: device driver data
454 static void phy_mdm6600_device_power_off(struct phy_mdm6600
*ddata
)
456 struct gpio_desc
*reset_gpio
=
457 ddata
->ctrl_gpios
[PHY_MDM6600_RESET
];
459 ddata
->enabled
= false;
460 phy_mdm6600_cmd(ddata
, PHY_MDM6600_CMD_BP_SHUTDOWN_REQ
);
463 gpiod_set_value_cansleep(reset_gpio
, 1);
465 dev_info(ddata
->dev
, "Waiting for power down request to complete.. ");
466 if (wait_for_completion_timeout(&ddata
->ack
,
467 msecs_to_jiffies(5000))) {
468 if (ddata
->status
== PHY_MDM6600_STATUS_PANIC
)
469 dev_info(ddata
->dev
, "Powered down OK\n");
471 dev_err(ddata
->dev
, "Timed out powering down\n");
475 static void phy_mdm6600_deferred_power_on(struct work_struct
*work
)
477 struct phy_mdm6600
*ddata
;
480 ddata
= container_of(work
, struct phy_mdm6600
, bootup_work
.work
);
482 error
= phy_mdm6600_device_power_on(ddata
);
484 dev_err(ddata
->dev
, "Device not functional\n");
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
494 static void phy_mdm6600_wake_modem(struct phy_mdm6600
*ddata
)
496 struct gpio_desc
*mode_gpio0
;
498 mode_gpio0
= ddata
->mode_gpios
->desc
[PHY_MDM6600_MODE0
];
499 gpiod_set_value_cansleep(mode_gpio0
, 1);
501 gpiod_set_value_cansleep(mode_gpio0
, 0);
505 msleep(MDM6600_MODEM_WAKE_DELAY_MS
);
508 static void phy_mdm6600_modem_wake(struct work_struct
*work
)
510 struct phy_mdm6600
*ddata
;
512 ddata
= container_of(work
, struct phy_mdm6600
, modem_wake_work
.work
);
513 phy_mdm6600_wake_modem(ddata
);
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.
520 schedule_delayed_work(&ddata
->modem_wake_work
,
521 msecs_to_jiffies(PHY_MDM6600_WAKE_KICK_MS
));
524 static int __maybe_unused
phy_mdm6600_runtime_suspend(struct device
*dev
)
526 struct phy_mdm6600
*ddata
= dev_get_drvdata(dev
);
528 cancel_delayed_work_sync(&ddata
->modem_wake_work
);
529 ddata
->awake
= false;
534 static int __maybe_unused
phy_mdm6600_runtime_resume(struct device
*dev
)
536 struct phy_mdm6600
*ddata
= dev_get_drvdata(dev
);
538 phy_mdm6600_modem_wake(&ddata
->modem_wake_work
.work
);
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
)
549 static const struct of_device_id phy_mdm6600_id_table
[] = {
550 { .compatible
= "motorola,mapphone-mdm6600", },
553 MODULE_DEVICE_TABLE(of
, phy_mdm6600_id_table
);
555 static int phy_mdm6600_probe(struct platform_device
*pdev
)
557 struct phy_mdm6600
*ddata
;
560 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
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
);
570 ddata
->dev
= &pdev
->dev
;
571 platform_set_drvdata(pdev
, ddata
);
573 /* Active state selected in phy_mdm6600_power_on() */
574 error
= pinctrl_pm_select_sleep_state(ddata
->dev
);
576 dev_warn(ddata
->dev
, "%s: error with sleep_state: %i\n",
579 error
= phy_mdm6600_init_lines(ddata
);
583 phy_mdm6600_init_irq(ddata
);
584 schedule_delayed_work(&ddata
->bootup_work
, 0);
587 * See phy_mdm6600_device_power_on(). We should be able
588 * to remove this eventually when ohci-platform can deal
589 * with -EPROBE_DEFER.
591 msleep(PHY_MDM6600_PHY_DELAY_MS
+ 500);
594 * Enable PM runtime only after PHY has been powered up properly.
595 * It is currently only needed after USB suspends mdm6600 and n_gsm
596 * needs to access the device. We don't want to do this earlier as
597 * gpio mode0 pin doubles as mdm6600 wake-up gpio.
599 pm_runtime_use_autosuspend(ddata
->dev
);
600 pm_runtime_set_autosuspend_delay(ddata
->dev
,
601 MDM6600_MODEM_IDLE_DELAY_MS
);
602 pm_runtime_enable(ddata
->dev
);
603 error
= pm_runtime_get_sync(ddata
->dev
);
605 dev_warn(ddata
->dev
, "failed to wake modem: %i\n", error
);
606 pm_runtime_put_noidle(ddata
->dev
);
610 ddata
->generic_phy
= devm_phy_create(ddata
->dev
, NULL
, &gpio_usb_ops
);
611 if (IS_ERR(ddata
->generic_phy
)) {
612 error
= PTR_ERR(ddata
->generic_phy
);
616 phy_set_drvdata(ddata
->generic_phy
, ddata
);
618 ddata
->phy_provider
=
619 devm_of_phy_provider_register(ddata
->dev
,
620 of_phy_simple_xlate
);
621 if (IS_ERR(ddata
->phy_provider
))
622 error
= PTR_ERR(ddata
->phy_provider
);
625 pm_runtime_mark_last_busy(ddata
->dev
);
626 pm_runtime_put_autosuspend(ddata
->dev
);
630 phy_mdm6600_device_power_off(ddata
);
635 static int phy_mdm6600_remove(struct platform_device
*pdev
)
637 struct phy_mdm6600
*ddata
= platform_get_drvdata(pdev
);
638 struct gpio_desc
*reset_gpio
= ddata
->ctrl_gpios
[PHY_MDM6600_RESET
];
640 pm_runtime_dont_use_autosuspend(ddata
->dev
);
641 pm_runtime_put_sync(ddata
->dev
);
642 pm_runtime_disable(ddata
->dev
);
645 wait_for_completion_timeout(&ddata
->ack
,
646 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS
));
648 gpiod_set_value_cansleep(reset_gpio
, 1);
649 phy_mdm6600_device_power_off(ddata
);
651 cancel_delayed_work_sync(&ddata
->modem_wake_work
);
652 cancel_delayed_work_sync(&ddata
->bootup_work
);
653 cancel_delayed_work_sync(&ddata
->status_work
);
658 static struct platform_driver phy_mdm6600_driver
= {
659 .probe
= phy_mdm6600_probe
,
660 .remove
= phy_mdm6600_remove
,
662 .name
= "phy-mapphone-mdm6600",
663 .pm
= &phy_mdm6600_pm_ops
,
664 .of_match_table
= of_match_ptr(phy_mdm6600_id_table
),
668 module_platform_driver(phy_mdm6600_driver
);
670 MODULE_ALIAS("platform:gpio_usb");
671 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
672 MODULE_DESCRIPTION("mdm6600 gpio usb phy driver");
673 MODULE_LICENSE("GPL v2");