Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Arche Platform driver to control APB.
0004  *
0005  * Copyright 2014-2015 Google Inc.
0006  * Copyright 2014-2015 Linaro Ltd.
0007  */
0008 
0009 #include <linux/clk.h>
0010 #include <linux/delay.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/of_irq.h>
0014 #include <linux/module.h>
0015 #include <linux/pinctrl/consumer.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/pm.h>
0018 #include <linux/regulator/consumer.h>
0019 #include <linux/spinlock.h>
0020 #include "arche_platform.h"
0021 
0022 static void apb_bootret_deassert(struct device *dev);
0023 
0024 struct arche_apb_ctrl_drvdata {
0025     /* Control GPIO signals to and from AP <=> AP Bridges */
0026     struct gpio_desc *resetn;
0027     struct gpio_desc *boot_ret;
0028     struct gpio_desc *pwroff;
0029     struct gpio_desc *wake_in;
0030     struct gpio_desc *wake_out;
0031     struct gpio_desc *pwrdn;
0032 
0033     enum arche_platform_state state;
0034     bool init_disabled;
0035 
0036     struct regulator *vcore;
0037     struct regulator *vio;
0038 
0039     struct gpio_desc *clk_en;
0040     struct clk *clk;
0041 
0042     struct pinctrl *pinctrl;
0043     struct pinctrl_state *pin_default;
0044 
0045     /* V2: SPI Bus control  */
0046     struct gpio_desc *spi_en;
0047     bool spi_en_polarity_high;
0048 };
0049 
0050 /*
0051  * Note that these low level api's are active high
0052  */
0053 static inline void deassert_reset(struct gpio_desc *gpio)
0054 {
0055     gpiod_set_raw_value(gpio, 1);
0056 }
0057 
0058 static inline void assert_reset(struct gpio_desc *gpio)
0059 {
0060     gpiod_set_raw_value(gpio, 0);
0061 }
0062 
0063 /*
0064  * Note: Please do not modify the below sequence, as it is as per the spec
0065  */
0066 static int coldboot_seq(struct platform_device *pdev)
0067 {
0068     struct device *dev = &pdev->dev;
0069     struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
0070     int ret;
0071 
0072     if (apb->init_disabled ||
0073         apb->state == ARCHE_PLATFORM_STATE_ACTIVE)
0074         return 0;
0075 
0076     /* Hold APB in reset state */
0077     assert_reset(apb->resetn);
0078 
0079     if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING && apb->spi_en)
0080         devm_gpiod_put(dev, apb->spi_en);
0081 
0082     /* Enable power to APB */
0083     if (!IS_ERR(apb->vcore)) {
0084         ret = regulator_enable(apb->vcore);
0085         if (ret) {
0086             dev_err(dev, "failed to enable core regulator\n");
0087             return ret;
0088         }
0089     }
0090 
0091     if (!IS_ERR(apb->vio)) {
0092         ret = regulator_enable(apb->vio);
0093         if (ret) {
0094             dev_err(dev, "failed to enable IO regulator\n");
0095             return ret;
0096         }
0097     }
0098 
0099     apb_bootret_deassert(dev);
0100 
0101     /* On DB3 clock was not mandatory */
0102     if (apb->clk_en)
0103         gpiod_set_value(apb->clk_en, 1);
0104 
0105     usleep_range(100, 200);
0106 
0107     /* deassert reset to APB : Active-low signal */
0108     deassert_reset(apb->resetn);
0109 
0110     apb->state = ARCHE_PLATFORM_STATE_ACTIVE;
0111 
0112     return 0;
0113 }
0114 
0115 static int fw_flashing_seq(struct platform_device *pdev)
0116 {
0117     struct device *dev = &pdev->dev;
0118     struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
0119     int ret;
0120 
0121     if (apb->init_disabled ||
0122         apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
0123         return 0;
0124 
0125     ret = regulator_enable(apb->vcore);
0126     if (ret) {
0127         dev_err(dev, "failed to enable core regulator\n");
0128         return ret;
0129     }
0130 
0131     ret = regulator_enable(apb->vio);
0132     if (ret) {
0133         dev_err(dev, "failed to enable IO regulator\n");
0134         return ret;
0135     }
0136 
0137     if (apb->spi_en) {
0138         unsigned long flags;
0139 
0140         if (apb->spi_en_polarity_high)
0141             flags = GPIOD_OUT_HIGH;
0142         else
0143             flags = GPIOD_OUT_LOW;
0144 
0145         apb->spi_en = devm_gpiod_get(dev, "spi-en", flags);
0146         if (IS_ERR(apb->spi_en)) {
0147             ret = PTR_ERR(apb->spi_en);
0148             dev_err(dev, "Failed requesting SPI bus en GPIO: %d\n",
0149                 ret);
0150             return ret;
0151         }
0152     }
0153 
0154     /* for flashing device should be in reset state */
0155     assert_reset(apb->resetn);
0156     apb->state = ARCHE_PLATFORM_STATE_FW_FLASHING;
0157 
0158     return 0;
0159 }
0160 
0161 static int standby_boot_seq(struct platform_device *pdev)
0162 {
0163     struct device *dev = &pdev->dev;
0164     struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
0165 
0166     if (apb->init_disabled)
0167         return 0;
0168 
0169     /*
0170      * Even if it is in OFF state,
0171      * then we do not want to change the state
0172      */
0173     if (apb->state == ARCHE_PLATFORM_STATE_STANDBY ||
0174         apb->state == ARCHE_PLATFORM_STATE_OFF)
0175         return 0;
0176 
0177     if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING && apb->spi_en)
0178         devm_gpiod_put(dev, apb->spi_en);
0179 
0180     /*
0181      * As per WDM spec, do nothing
0182      *
0183      * Pasted from WDM spec,
0184      *  - A falling edge on POWEROFF_L is detected (a)
0185      *  - WDM enters standby mode, but no output signals are changed
0186      */
0187 
0188     /* TODO: POWEROFF_L is input to WDM module  */
0189     apb->state = ARCHE_PLATFORM_STATE_STANDBY;
0190     return 0;
0191 }
0192 
0193 static void poweroff_seq(struct platform_device *pdev)
0194 {
0195     struct device *dev = &pdev->dev;
0196     struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
0197 
0198     if (apb->init_disabled || apb->state == ARCHE_PLATFORM_STATE_OFF)
0199         return;
0200 
0201     if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING && apb->spi_en)
0202         devm_gpiod_put(dev, apb->spi_en);
0203 
0204     /* disable the clock */
0205     if (apb->clk_en)
0206         gpiod_set_value(apb->clk_en, 0);
0207 
0208     if (!IS_ERR(apb->vcore) && regulator_is_enabled(apb->vcore) > 0)
0209         regulator_disable(apb->vcore);
0210 
0211     if (!IS_ERR(apb->vio) && regulator_is_enabled(apb->vio) > 0)
0212         regulator_disable(apb->vio);
0213 
0214     /* As part of exit, put APB back in reset state */
0215     assert_reset(apb->resetn);
0216     apb->state = ARCHE_PLATFORM_STATE_OFF;
0217 
0218     /* TODO: May have to send an event to SVC about this exit */
0219 }
0220 
0221 static void apb_bootret_deassert(struct device *dev)
0222 {
0223     struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
0224 
0225     gpiod_set_value(apb->boot_ret, 0);
0226 }
0227 
0228 int apb_ctrl_coldboot(struct device *dev)
0229 {
0230     return coldboot_seq(to_platform_device(dev));
0231 }
0232 
0233 int apb_ctrl_fw_flashing(struct device *dev)
0234 {
0235     return fw_flashing_seq(to_platform_device(dev));
0236 }
0237 
0238 int apb_ctrl_standby_boot(struct device *dev)
0239 {
0240     return standby_boot_seq(to_platform_device(dev));
0241 }
0242 
0243 void apb_ctrl_poweroff(struct device *dev)
0244 {
0245     poweroff_seq(to_platform_device(dev));
0246 }
0247 
0248 static ssize_t state_store(struct device *dev,
0249                struct device_attribute *attr,
0250                const char *buf, size_t count)
0251 {
0252     struct platform_device *pdev = to_platform_device(dev);
0253     struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
0254     int ret = 0;
0255     bool is_disabled;
0256 
0257     if (sysfs_streq(buf, "off")) {
0258         if (apb->state == ARCHE_PLATFORM_STATE_OFF)
0259             return count;
0260 
0261         poweroff_seq(pdev);
0262     } else if (sysfs_streq(buf, "active")) {
0263         if (apb->state == ARCHE_PLATFORM_STATE_ACTIVE)
0264             return count;
0265 
0266         poweroff_seq(pdev);
0267         is_disabled = apb->init_disabled;
0268         apb->init_disabled = false;
0269         ret = coldboot_seq(pdev);
0270         if (ret)
0271             apb->init_disabled = is_disabled;
0272     } else if (sysfs_streq(buf, "standby")) {
0273         if (apb->state == ARCHE_PLATFORM_STATE_STANDBY)
0274             return count;
0275 
0276         ret = standby_boot_seq(pdev);
0277     } else if (sysfs_streq(buf, "fw_flashing")) {
0278         if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
0279             return count;
0280 
0281         /*
0282          * First we want to make sure we power off everything
0283          * and then enter FW flashing state
0284          */
0285         poweroff_seq(pdev);
0286         ret = fw_flashing_seq(pdev);
0287     } else {
0288         dev_err(dev, "unknown state\n");
0289         ret = -EINVAL;
0290     }
0291 
0292     return ret ? ret : count;
0293 }
0294 
0295 static ssize_t state_show(struct device *dev,
0296               struct device_attribute *attr, char *buf)
0297 {
0298     struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
0299 
0300     switch (apb->state) {
0301     case ARCHE_PLATFORM_STATE_OFF:
0302         return sprintf(buf, "off%s\n",
0303                 apb->init_disabled ? ",disabled" : "");
0304     case ARCHE_PLATFORM_STATE_ACTIVE:
0305         return sprintf(buf, "active\n");
0306     case ARCHE_PLATFORM_STATE_STANDBY:
0307         return sprintf(buf, "standby\n");
0308     case ARCHE_PLATFORM_STATE_FW_FLASHING:
0309         return sprintf(buf, "fw_flashing\n");
0310     default:
0311         return sprintf(buf, "unknown state\n");
0312     }
0313 }
0314 
0315 static DEVICE_ATTR_RW(state);
0316 
0317 static int apb_ctrl_get_devtree_data(struct platform_device *pdev,
0318                      struct arche_apb_ctrl_drvdata *apb)
0319 {
0320     struct device *dev = &pdev->dev;
0321     int ret;
0322 
0323     apb->resetn = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
0324     if (IS_ERR(apb->resetn)) {
0325         ret = PTR_ERR(apb->resetn);
0326         dev_err(dev, "Failed requesting reset GPIO: %d\n", ret);
0327         return ret;
0328     }
0329 
0330     apb->boot_ret = devm_gpiod_get(dev, "boot-ret", GPIOD_OUT_LOW);
0331     if (IS_ERR(apb->boot_ret)) {
0332         ret = PTR_ERR(apb->boot_ret);
0333         dev_err(dev, "Failed requesting bootret GPIO: %d\n", ret);
0334         return ret;
0335     }
0336 
0337     /* It's not mandatory to support power management interface */
0338     apb->pwroff = devm_gpiod_get_optional(dev, "pwr-off", GPIOD_IN);
0339     if (IS_ERR(apb->pwroff)) {
0340         ret = PTR_ERR(apb->pwroff);
0341         dev_err(dev, "Failed requesting pwroff_n GPIO: %d\n", ret);
0342         return ret;
0343     }
0344 
0345     /* Do not make clock mandatory as of now (for DB3) */
0346     apb->clk_en = devm_gpiod_get_optional(dev, "clock-en", GPIOD_OUT_LOW);
0347     if (IS_ERR(apb->clk_en)) {
0348         ret = PTR_ERR(apb->clk_en);
0349         dev_err(dev, "Failed requesting APB clock en GPIO: %d\n", ret);
0350         return ret;
0351     }
0352 
0353     apb->pwrdn = devm_gpiod_get(dev, "pwr-down", GPIOD_OUT_LOW);
0354     if (IS_ERR(apb->pwrdn)) {
0355         ret = PTR_ERR(apb->pwrdn);
0356         dev_warn(dev, "Failed requesting power down GPIO: %d\n", ret);
0357         return ret;
0358     }
0359 
0360     /* Regulators are optional, as we may have fixed supply coming in */
0361     apb->vcore = devm_regulator_get(dev, "vcore");
0362     if (IS_ERR(apb->vcore))
0363         dev_warn(dev, "no core regulator found\n");
0364 
0365     apb->vio = devm_regulator_get(dev, "vio");
0366     if (IS_ERR(apb->vio))
0367         dev_warn(dev, "no IO regulator found\n");
0368 
0369     apb->pinctrl = devm_pinctrl_get(&pdev->dev);
0370     if (IS_ERR(apb->pinctrl)) {
0371         dev_err(&pdev->dev, "could not get pinctrl handle\n");
0372         return PTR_ERR(apb->pinctrl);
0373     }
0374     apb->pin_default = pinctrl_lookup_state(apb->pinctrl, "default");
0375     if (IS_ERR(apb->pin_default)) {
0376         dev_err(&pdev->dev, "could not get default pin state\n");
0377         return PTR_ERR(apb->pin_default);
0378     }
0379 
0380     /* Only applicable for platform >= V2 */
0381     if (of_property_read_bool(pdev->dev.of_node, "gb,spi-en-active-high"))
0382         apb->spi_en_polarity_high = true;
0383 
0384     return 0;
0385 }
0386 
0387 static int arche_apb_ctrl_probe(struct platform_device *pdev)
0388 {
0389     int ret;
0390     struct arche_apb_ctrl_drvdata *apb;
0391     struct device *dev = &pdev->dev;
0392 
0393     apb = devm_kzalloc(&pdev->dev, sizeof(*apb), GFP_KERNEL);
0394     if (!apb)
0395         return -ENOMEM;
0396 
0397     ret = apb_ctrl_get_devtree_data(pdev, apb);
0398     if (ret) {
0399         dev_err(dev, "failed to get apb devicetree data %d\n", ret);
0400         return ret;
0401     }
0402 
0403     /* Initially set APB to OFF state */
0404     apb->state = ARCHE_PLATFORM_STATE_OFF;
0405     /* Check whether device needs to be enabled on boot */
0406     if (of_property_read_bool(pdev->dev.of_node, "arche,init-disable"))
0407         apb->init_disabled = true;
0408 
0409     platform_set_drvdata(pdev, apb);
0410 
0411     /* Create sysfs interface to allow user to change state dynamically */
0412     ret = device_create_file(dev, &dev_attr_state);
0413     if (ret) {
0414         dev_err(dev, "failed to create state file in sysfs\n");
0415         return ret;
0416     }
0417 
0418     dev_info(&pdev->dev, "Device registered successfully\n");
0419     return 0;
0420 }
0421 
0422 static int arche_apb_ctrl_remove(struct platform_device *pdev)
0423 {
0424     device_remove_file(&pdev->dev, &dev_attr_state);
0425     poweroff_seq(pdev);
0426     platform_set_drvdata(pdev, NULL);
0427 
0428     return 0;
0429 }
0430 
0431 static int __maybe_unused arche_apb_ctrl_suspend(struct device *dev)
0432 {
0433     /*
0434      * If timing profile permits, we may shutdown bridge
0435      * completely
0436      *
0437      * TODO: sequence ??
0438      *
0439      * Also, need to make sure we meet precondition for unipro suspend
0440      * Precondition: Definition ???
0441      */
0442     return 0;
0443 }
0444 
0445 static int __maybe_unused arche_apb_ctrl_resume(struct device *dev)
0446 {
0447     /*
0448      * At least for ES2 we have to meet the delay requirement between
0449      * unipro switch and AP bridge init, depending on whether bridge is in
0450      * OFF state or standby state.
0451      *
0452      * Based on whether bridge is in standby or OFF state we may have to
0453      * assert multiple signals. Please refer to WDM spec, for more info.
0454      *
0455      */
0456     return 0;
0457 }
0458 
0459 static void arche_apb_ctrl_shutdown(struct platform_device *pdev)
0460 {
0461     apb_ctrl_poweroff(&pdev->dev);
0462 }
0463 
0464 static SIMPLE_DEV_PM_OPS(arche_apb_ctrl_pm_ops, arche_apb_ctrl_suspend,
0465              arche_apb_ctrl_resume);
0466 
0467 static const struct of_device_id arche_apb_ctrl_of_match[] = {
0468     { .compatible = "usbffff,2", },
0469     { },
0470 };
0471 
0472 static struct platform_driver arche_apb_ctrl_device_driver = {
0473     .probe      = arche_apb_ctrl_probe,
0474     .remove     = arche_apb_ctrl_remove,
0475     .shutdown   = arche_apb_ctrl_shutdown,
0476     .driver     = {
0477         .name   = "arche-apb-ctrl",
0478         .pm = &arche_apb_ctrl_pm_ops,
0479         .of_match_table = arche_apb_ctrl_of_match,
0480     }
0481 };
0482 
0483 int __init arche_apb_init(void)
0484 {
0485     return platform_driver_register(&arche_apb_ctrl_device_driver);
0486 }
0487 
0488 void __exit arche_apb_exit(void)
0489 {
0490     platform_driver_unregister(&arche_apb_ctrl_device_driver);
0491 }