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 MDM6600_MODEM_IDLE_DELAY_MS 1000 /* modem after USB suspend */
23 #define MDM6600_MODEM_WAKE_DELAY_MS 200 /* modem response after idle */
25 enum phy_mdm6600_ctrl_lines {
26 PHY_MDM6600_ENABLE, /* USB PHY enable */
27 PHY_MDM6600_POWER, /* Device power */
28 PHY_MDM6600_RESET, /* Device reset */
29 PHY_MDM6600_NR_CTRL_LINES,
32 enum phy_mdm6600_bootmode_lines {
33 PHY_MDM6600_MODE0, /* out USB mode0 and OOB wake */
34 PHY_MDM6600_MODE1, /* out USB mode1, in OOB wake */
35 PHY_MDM6600_NR_MODE_LINES,
38 enum phy_mdm6600_cmd_lines {
42 PHY_MDM6600_NR_CMD_LINES,
45 enum phy_mdm6600_status_lines {
49 PHY_MDM6600_NR_STATUS_LINES,
53 * MDM6600 command codes. These are based on Motorola Mapphone Linux
56 enum phy_mdm6600_cmd {
57 PHY_MDM6600_CMD_BP_PANIC_ACK,
58 PHY_MDM6600_CMD_DATA_ONLY_BYPASS, /* Reroute USB to CPCAP PHY */
59 PHY_MDM6600_CMD_FULL_BYPASS, /* Reroute USB to CPCAP PHY */
60 PHY_MDM6600_CMD_NO_BYPASS, /* Request normal USB mode */
61 PHY_MDM6600_CMD_BP_SHUTDOWN_REQ, /* Request device power off */
62 PHY_MDM6600_CMD_BP_UNKNOWN_5,
63 PHY_MDM6600_CMD_BP_UNKNOWN_6,
64 PHY_MDM6600_CMD_UNDEFINED,
68 * MDM6600 status codes. These are based on Motorola Mapphone Linux
71 enum phy_mdm6600_status {
72 PHY_MDM6600_STATUS_PANIC, /* Seems to be really off */
73 PHY_MDM6600_STATUS_PANIC_BUSY_WAIT,
74 PHY_MDM6600_STATUS_QC_DLOAD,
75 PHY_MDM6600_STATUS_RAM_DOWNLOADER, /* MDM6600 USB flashing mode */
76 PHY_MDM6600_STATUS_PHONE_CODE_AWAKE, /* MDM6600 normal USB mode */
77 PHY_MDM6600_STATUS_PHONE_CODE_ASLEEP,
78 PHY_MDM6600_STATUS_SHUTDOWN_ACK,
79 PHY_MDM6600_STATUS_UNDEFINED,
82 static const char * const
83 phy_mdm6600_status_name[] = {
84 "off", "busy", "qc_dl", "ram_dl", "awake",
85 "asleep", "shutdown", "undefined",
90 struct phy *generic_phy;
91 struct phy_provider *phy_provider;
92 struct gpio_desc *ctrl_gpios[PHY_MDM6600_NR_CTRL_LINES];
93 struct gpio_descs *mode_gpios;
94 struct gpio_descs *status_gpios;
95 struct gpio_descs *cmd_gpios;
96 struct delayed_work bootup_work;
97 struct delayed_work status_work;
98 struct delayed_work modem_wake_work;
99 struct completion ack;
100 bool enabled; /* mdm6600 phy enabled */
101 bool running; /* mdm6600 boot done */
102 bool awake; /* mdm6600 respnds on n_gsm */
106 static int phy_mdm6600_init(struct phy *x)
108 struct phy_mdm6600 *ddata = phy_get_drvdata(x);
109 struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
112 return -EPROBE_DEFER;
114 gpiod_set_value_cansleep(enable_gpio, 0);
119 static int phy_mdm6600_power_on(struct phy *x)
121 struct phy_mdm6600 *ddata = phy_get_drvdata(x);
122 struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
127 gpiod_set_value_cansleep(enable_gpio, 1);
132 static int phy_mdm6600_power_off(struct phy *x)
134 struct phy_mdm6600 *ddata = phy_get_drvdata(x);
135 struct gpio_desc *enable_gpio = ddata->ctrl_gpios[PHY_MDM6600_ENABLE];
140 gpiod_set_value_cansleep(enable_gpio, 0);
145 static const struct phy_ops gpio_usb_ops = {
146 .init = phy_mdm6600_init,
147 .power_on = phy_mdm6600_power_on,
148 .power_off = phy_mdm6600_power_off,
149 .owner = THIS_MODULE,
153 * phy_mdm6600_cmd() - send a command request to mdm6600
154 * @ddata: device driver data
156 * Configures the three command request GPIOs to the specified value.
158 static void phy_mdm6600_cmd(struct phy_mdm6600 *ddata, int val)
160 DECLARE_BITMAP(values, PHY_MDM6600_NR_CMD_LINES);
164 gpiod_set_array_value_cansleep(PHY_MDM6600_NR_CMD_LINES,
165 ddata->cmd_gpios->desc, values);
169 * phy_mdm6600_status() - read mdm6600 status lines
170 * @ddata: device driver data
172 static void phy_mdm6600_status(struct work_struct *work)
174 struct phy_mdm6600 *ddata;
176 DECLARE_BITMAP(values, PHY_MDM6600_NR_STATUS_LINES);
177 int error, i, val = 0;
179 ddata = container_of(work, struct phy_mdm6600, status_work.work);
182 error = gpiod_get_array_value_cansleep(PHY_MDM6600_NR_STATUS_LINES,
183 ddata->status_gpios->desc,
188 for (i = 0; i < PHY_MDM6600_NR_STATUS_LINES; i++) {
189 val |= test_bit(i, values) << i;
190 dev_dbg(ddata->dev, "XXX %s: i: %i values[i]: %i val: %i\n",
191 __func__, i, test_bit(i, values), val);
193 ddata->status = values[0];
195 dev_info(dev, "modem status: %i %s\n",
197 phy_mdm6600_status_name[ddata->status & 7]);
198 complete(&ddata->ack);
201 static irqreturn_t phy_mdm6600_irq_thread(int irq, void *data)
203 struct phy_mdm6600 *ddata = data;
205 schedule_delayed_work(&ddata->status_work, msecs_to_jiffies(10));
211 * phy_mdm6600_wakeirq_thread - handle mode1 line OOB wake after booting
213 * @data: interrupt handler data
215 * GPIO mode1 is used initially as output to configure the USB boot
216 * mode for mdm6600. After booting it is used as input for OOB wake
217 * signal from mdm6600 to the SoC. Just use it for debug info only
220 static irqreturn_t phy_mdm6600_wakeirq_thread(int irq, void *data)
222 struct phy_mdm6600 *ddata = data;
223 struct gpio_desc *mode_gpio1;
225 mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
226 dev_dbg(ddata->dev, "OOB wake on mode_gpio1: %i\n",
227 gpiod_get_value(mode_gpio1));
233 * phy_mdm6600_init_irq() - initialize mdm6600 status IRQ lines
234 * @ddata: device driver data
236 static void phy_mdm6600_init_irq(struct phy_mdm6600 *ddata)
238 struct device *dev = ddata->dev;
241 for (i = PHY_MDM6600_STATUS0;
242 i <= PHY_MDM6600_STATUS2; i++) {
243 struct gpio_desc *gpio = ddata->status_gpios->desc[i];
245 irq = gpiod_to_irq(gpio);
249 error = devm_request_threaded_irq(dev, irq, NULL,
250 phy_mdm6600_irq_thread,
251 IRQF_TRIGGER_RISING |
252 IRQF_TRIGGER_FALLING |
257 dev_warn(dev, "no modem status irq%i: %i\n",
262 struct phy_mdm6600_map {
267 static const struct phy_mdm6600_map
268 phy_mdm6600_ctrl_gpio_map[PHY_MDM6600_NR_CTRL_LINES] = {
269 { "enable", GPIOD_OUT_LOW, }, /* low = phy disabled */
270 { "power", GPIOD_OUT_LOW, }, /* low = off */
271 { "reset", GPIOD_OUT_HIGH, }, /* high = reset */
275 * phy_mdm6600_init_lines() - initialize mdm6600 GPIO lines
276 * @ddata: device driver data
278 static int phy_mdm6600_init_lines(struct phy_mdm6600 *ddata)
280 struct device *dev = ddata->dev;
283 /* MDM6600 control lines */
284 for (i = 0; i < ARRAY_SIZE(phy_mdm6600_ctrl_gpio_map); i++) {
285 const struct phy_mdm6600_map *map =
286 &phy_mdm6600_ctrl_gpio_map[i];
287 struct gpio_desc **gpio = &ddata->ctrl_gpios[i];
289 *gpio = devm_gpiod_get(dev, map->name, map->direction);
291 dev_info(dev, "gpio %s error %li\n",
292 map->name, PTR_ERR(*gpio));
293 return PTR_ERR(*gpio);
297 /* MDM6600 USB start-up mode output lines */
298 ddata->mode_gpios = devm_gpiod_get_array(dev, "motorola,mode",
300 if (IS_ERR(ddata->mode_gpios))
301 return PTR_ERR(ddata->mode_gpios);
303 if (ddata->mode_gpios->ndescs != PHY_MDM6600_NR_MODE_LINES)
306 /* MDM6600 status input lines */
307 ddata->status_gpios = devm_gpiod_get_array(dev, "motorola,status",
309 if (IS_ERR(ddata->status_gpios))
310 return PTR_ERR(ddata->status_gpios);
312 if (ddata->status_gpios->ndescs != PHY_MDM6600_NR_STATUS_LINES)
315 /* MDM6600 cmd output lines */
316 ddata->cmd_gpios = devm_gpiod_get_array(dev, "motorola,cmd",
318 if (IS_ERR(ddata->cmd_gpios))
319 return PTR_ERR(ddata->cmd_gpios);
321 if (ddata->cmd_gpios->ndescs != PHY_MDM6600_NR_CMD_LINES)
328 * phy_mdm6600_device_power_on() - power on mdm6600 device
329 * @ddata: device driver data
331 * To get the integrated USB phy in MDM6600 takes some hoops. We must ensure
332 * the shared USB bootmode GPIOs are configured, then request modem start-up,
333 * reset and power-up.. And then we need to recycle the shared USB bootmode
334 * GPIOs as they are also used for Out of Band (OOB) wake for the USB and
335 * TS 27.010 serial mux.
337 static int phy_mdm6600_device_power_on(struct phy_mdm6600 *ddata)
339 struct gpio_desc *mode_gpio0, *mode_gpio1, *reset_gpio, *power_gpio;
340 int error = 0, wakeirq;
342 mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
343 mode_gpio1 = ddata->mode_gpios->desc[PHY_MDM6600_MODE1];
344 reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
345 power_gpio = ddata->ctrl_gpios[PHY_MDM6600_POWER];
348 * Shared GPIOs must be low for normal USB mode. After booting
349 * they are used for OOB wake signaling. These can be also used
350 * to configure USB flashing mode later on based on a module
353 gpiod_set_value_cansleep(mode_gpio0, 0);
354 gpiod_set_value_cansleep(mode_gpio1, 0);
356 /* Request start-up mode */
357 phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_NO_BYPASS);
359 /* Request a reset first */
360 gpiod_set_value_cansleep(reset_gpio, 0);
363 /* Toggle power GPIO to request mdm6600 to start */
364 gpiod_set_value_cansleep(power_gpio, 1);
366 gpiod_set_value_cansleep(power_gpio, 0);
369 * Looks like the USB PHY needs between 2.2 to 4 seconds.
370 * If we try to use it before that, we will get L3 errors
371 * from omap-usb-host trying to access the PHY. See also
372 * phy_mdm6600_init() for -EPROBE_DEFER.
374 msleep(PHY_MDM6600_PHY_DELAY_MS);
375 ddata->enabled = true;
377 /* Booting up the rest of MDM6600 will take total about 8 seconds */
378 dev_info(ddata->dev, "Waiting for power up request to complete..\n");
379 if (wait_for_completion_timeout(&ddata->ack,
380 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS))) {
381 if (ddata->status > PHY_MDM6600_STATUS_PANIC &&
382 ddata->status < PHY_MDM6600_STATUS_SHUTDOWN_ACK)
383 dev_info(ddata->dev, "Powered up OK\n");
385 ddata->enabled = false;
387 dev_err(ddata->dev, "Timed out powering up\n");
390 /* Reconfigure mode1 GPIO as input for OOB wake */
391 gpiod_direction_input(mode_gpio1);
393 wakeirq = gpiod_to_irq(mode_gpio1);
397 error = devm_request_threaded_irq(ddata->dev, wakeirq, NULL,
398 phy_mdm6600_wakeirq_thread,
399 IRQF_TRIGGER_RISING |
400 IRQF_TRIGGER_FALLING |
405 dev_warn(ddata->dev, "no modem wakeirq irq%i: %i\n",
408 ddata->running = true;
414 * phy_mdm6600_device_power_off() - power off mdm6600 device
415 * @ddata: device driver data
417 static void phy_mdm6600_device_power_off(struct phy_mdm6600 *ddata)
419 struct gpio_desc *reset_gpio =
420 ddata->ctrl_gpios[PHY_MDM6600_RESET];
422 ddata->enabled = false;
423 phy_mdm6600_cmd(ddata, PHY_MDM6600_CMD_BP_SHUTDOWN_REQ);
426 gpiod_set_value_cansleep(reset_gpio, 1);
428 dev_info(ddata->dev, "Waiting for power down request to complete.. ");
429 if (wait_for_completion_timeout(&ddata->ack,
430 msecs_to_jiffies(5000))) {
431 if (ddata->status == PHY_MDM6600_STATUS_PANIC)
432 dev_info(ddata->dev, "Powered down OK\n");
434 dev_err(ddata->dev, "Timed out powering down\n");
438 static void phy_mdm6600_deferred_power_on(struct work_struct *work)
440 struct phy_mdm6600 *ddata;
443 ddata = container_of(work, struct phy_mdm6600, bootup_work.work);
445 error = phy_mdm6600_device_power_on(ddata);
447 dev_err(ddata->dev, "Device not functional\n");
451 * USB suspend puts mdm6600 into low power mode. For any n_gsm using apps,
452 * we need to keep the modem awake by kicking it's mode0 GPIO. This will
453 * keep the modem awake for about 1.2 seconds. When no n_gsm apps are using
454 * the modem, runtime PM auto mode can be enabled so modem can enter low
457 static void phy_mdm6600_wake_modem(struct phy_mdm6600 *ddata)
459 struct gpio_desc *mode_gpio0;
461 mode_gpio0 = ddata->mode_gpios->desc[PHY_MDM6600_MODE0];
462 gpiod_set_value_cansleep(mode_gpio0, 1);
464 gpiod_set_value_cansleep(mode_gpio0, 0);
468 msleep(MDM6600_MODEM_WAKE_DELAY_MS);
471 static void phy_mdm6600_modem_wake(struct work_struct *work)
473 struct phy_mdm6600 *ddata;
475 ddata = container_of(work, struct phy_mdm6600, modem_wake_work.work);
476 phy_mdm6600_wake_modem(ddata);
477 schedule_delayed_work(&ddata->modem_wake_work,
478 msecs_to_jiffies(MDM6600_MODEM_IDLE_DELAY_MS));
481 static int __maybe_unused phy_mdm6600_runtime_suspend(struct device *dev)
483 struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
485 cancel_delayed_work_sync(&ddata->modem_wake_work);
486 ddata->awake = false;
491 static int __maybe_unused phy_mdm6600_runtime_resume(struct device *dev)
493 struct phy_mdm6600 *ddata = dev_get_drvdata(dev);
495 phy_mdm6600_modem_wake(&ddata->modem_wake_work.work);
501 static const struct dev_pm_ops phy_mdm6600_pm_ops = {
502 SET_RUNTIME_PM_OPS(phy_mdm6600_runtime_suspend,
503 phy_mdm6600_runtime_resume, NULL)
506 static const struct of_device_id phy_mdm6600_id_table[] = {
507 { .compatible = "motorola,mapphone-mdm6600", },
510 MODULE_DEVICE_TABLE(of, phy_mdm6600_id_table);
512 static int phy_mdm6600_probe(struct platform_device *pdev)
514 struct phy_mdm6600 *ddata;
517 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
521 INIT_DELAYED_WORK(&ddata->bootup_work,
522 phy_mdm6600_deferred_power_on);
523 INIT_DELAYED_WORK(&ddata->status_work, phy_mdm6600_status);
524 INIT_DELAYED_WORK(&ddata->modem_wake_work, phy_mdm6600_modem_wake);
525 init_completion(&ddata->ack);
527 ddata->dev = &pdev->dev;
528 platform_set_drvdata(pdev, ddata);
530 error = phy_mdm6600_init_lines(ddata);
534 phy_mdm6600_init_irq(ddata);
536 ddata->generic_phy = devm_phy_create(ddata->dev, NULL, &gpio_usb_ops);
537 if (IS_ERR(ddata->generic_phy)) {
538 error = PTR_ERR(ddata->generic_phy);
542 phy_set_drvdata(ddata->generic_phy, ddata);
544 ddata->phy_provider =
545 devm_of_phy_provider_register(ddata->dev,
546 of_phy_simple_xlate);
547 if (IS_ERR(ddata->phy_provider)) {
548 error = PTR_ERR(ddata->phy_provider);
552 schedule_delayed_work(&ddata->bootup_work, 0);
555 * See phy_mdm6600_device_power_on(). We should be able
556 * to remove this eventually when ohci-platform can deal
557 * with -EPROBE_DEFER.
559 msleep(PHY_MDM6600_PHY_DELAY_MS + 500);
562 * Enable PM runtime only after PHY has been powered up properly.
563 * It is currently only needed after USB suspends mdm6600 and n_gsm
564 * needs to access the device. We don't want to do this earlier as
565 * gpio mode0 pin doubles as mdm6600 wake-up gpio.
567 pm_runtime_use_autosuspend(ddata->dev);
568 pm_runtime_set_autosuspend_delay(ddata->dev,
569 MDM6600_MODEM_IDLE_DELAY_MS);
570 pm_runtime_enable(ddata->dev);
571 error = pm_runtime_get_sync(ddata->dev);
573 dev_warn(ddata->dev, "failed to wake modem: %i\n", error);
574 pm_runtime_put_noidle(ddata->dev);
576 pm_runtime_mark_last_busy(ddata->dev);
577 pm_runtime_put_autosuspend(ddata->dev);
582 phy_mdm6600_device_power_off(ddata);
586 static int phy_mdm6600_remove(struct platform_device *pdev)
588 struct phy_mdm6600 *ddata = platform_get_drvdata(pdev);
589 struct gpio_desc *reset_gpio = ddata->ctrl_gpios[PHY_MDM6600_RESET];
591 pm_runtime_dont_use_autosuspend(ddata->dev);
592 pm_runtime_put_sync(ddata->dev);
593 pm_runtime_disable(ddata->dev);
596 wait_for_completion_timeout(&ddata->ack,
597 msecs_to_jiffies(PHY_MDM6600_ENABLED_DELAY_MS));
599 gpiod_set_value_cansleep(reset_gpio, 1);
600 phy_mdm6600_device_power_off(ddata);
602 cancel_delayed_work_sync(&ddata->modem_wake_work);
603 cancel_delayed_work_sync(&ddata->bootup_work);
604 cancel_delayed_work_sync(&ddata->status_work);
609 static struct platform_driver phy_mdm6600_driver = {
610 .probe = phy_mdm6600_probe,
611 .remove = phy_mdm6600_remove,
613 .name = "phy-mapphone-mdm6600",
614 .pm = &phy_mdm6600_pm_ops,
615 .of_match_table = of_match_ptr(phy_mdm6600_id_table),
619 module_platform_driver(phy_mdm6600_driver);
621 MODULE_ALIAS("platform:gpio_usb");
622 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
623 MODULE_DESCRIPTION("mdm6600 gpio usb phy driver");
624 MODULE_LICENSE("GPL v2");