1e6a578e2SVinod Koul // SPDX-License-Identifier: GPL-2.0 2e6a578e2SVinod Koul // Copyright (c) 2017-18 Linaro Limited 3e6a578e2SVinod Koul 4e6a578e2SVinod Koul #include <linux/delay.h> 5e6a578e2SVinod Koul #include <linux/errno.h> 6e6a578e2SVinod Koul #include <linux/kernel.h> 7e6a578e2SVinod Koul #include <linux/module.h> 8e6a578e2SVinod Koul #include <linux/of.h> 9e6a578e2SVinod Koul #include <linux/of_platform.h> 10e6a578e2SVinod Koul #include <linux/platform_device.h> 11e6a578e2SVinod Koul #include <linux/reboot.h> 12e6a578e2SVinod Koul #include <linux/reboot-mode.h> 13e6a578e2SVinod Koul #include <linux/regmap.h> 14e6a578e2SVinod Koul 15e6a578e2SVinod Koul #define PON_SOFT_RB_SPARE 0x8f 16e6a578e2SVinod Koul 17fce5430fSJohn Stultz #define GEN1_REASON_SHIFT 2 18fce5430fSJohn Stultz #define GEN2_REASON_SHIFT 1 19fce5430fSJohn Stultz 20e6a578e2SVinod Koul struct pm8916_pon { 21e6a578e2SVinod Koul struct device *dev; 22e6a578e2SVinod Koul struct regmap *regmap; 23e6a578e2SVinod Koul u32 baseaddr; 24e6a578e2SVinod Koul struct reboot_mode_driver reboot_mode; 25fce5430fSJohn Stultz long reason_shift; 26e6a578e2SVinod Koul }; 27e6a578e2SVinod Koul 28e6a578e2SVinod Koul static int pm8916_reboot_mode_write(struct reboot_mode_driver *reboot, 29e6a578e2SVinod Koul unsigned int magic) 30e6a578e2SVinod Koul { 31e6a578e2SVinod Koul struct pm8916_pon *pon = container_of 32e6a578e2SVinod Koul (reboot, struct pm8916_pon, reboot_mode); 33e6a578e2SVinod Koul int ret; 34e6a578e2SVinod Koul 35e6a578e2SVinod Koul ret = regmap_update_bits(pon->regmap, 36e6a578e2SVinod Koul pon->baseaddr + PON_SOFT_RB_SPARE, 37fc5be29fSKejia Hu GENMASK(7, pon->reason_shift), 38fc5be29fSKejia Hu magic << pon->reason_shift); 39e6a578e2SVinod Koul if (ret < 0) 40e6a578e2SVinod Koul dev_err(pon->dev, "update reboot mode bits failed\n"); 41e6a578e2SVinod Koul 42e6a578e2SVinod Koul return ret; 43e6a578e2SVinod Koul } 44e6a578e2SVinod Koul 45e6a578e2SVinod Koul static int pm8916_pon_probe(struct platform_device *pdev) 46e6a578e2SVinod Koul { 47e6a578e2SVinod Koul struct pm8916_pon *pon; 48e6a578e2SVinod Koul int error; 49e6a578e2SVinod Koul 50e6a578e2SVinod Koul pon = devm_kzalloc(&pdev->dev, sizeof(*pon), GFP_KERNEL); 51e6a578e2SVinod Koul if (!pon) 52e6a578e2SVinod Koul return -ENOMEM; 53e6a578e2SVinod Koul 54e6a578e2SVinod Koul pon->dev = &pdev->dev; 55e6a578e2SVinod Koul 56e6a578e2SVinod Koul pon->regmap = dev_get_regmap(pdev->dev.parent, NULL); 57e6a578e2SVinod Koul if (!pon->regmap) { 58e6a578e2SVinod Koul dev_err(&pdev->dev, "failed to locate regmap\n"); 59e6a578e2SVinod Koul return -ENODEV; 60e6a578e2SVinod Koul } 61e6a578e2SVinod Koul 62e6a578e2SVinod Koul error = of_property_read_u32(pdev->dev.of_node, "reg", 63e6a578e2SVinod Koul &pon->baseaddr); 64e6a578e2SVinod Koul if (error) 65e6a578e2SVinod Koul return error; 66e6a578e2SVinod Koul 67e6a578e2SVinod Koul pon->reboot_mode.dev = &pdev->dev; 68fce5430fSJohn Stultz pon->reason_shift = (long)of_device_get_match_data(&pdev->dev); 69e6a578e2SVinod Koul pon->reboot_mode.write = pm8916_reboot_mode_write; 70e6a578e2SVinod Koul error = devm_reboot_mode_register(&pdev->dev, &pon->reboot_mode); 71e6a578e2SVinod Koul if (error) { 72e6a578e2SVinod Koul dev_err(&pdev->dev, "can't register reboot mode\n"); 73e6a578e2SVinod Koul return error; 74e6a578e2SVinod Koul } 75e6a578e2SVinod Koul 76e6a578e2SVinod Koul platform_set_drvdata(pdev, pon); 77e6a578e2SVinod Koul 78e6a578e2SVinod Koul return devm_of_platform_populate(&pdev->dev); 79e6a578e2SVinod Koul } 80e6a578e2SVinod Koul 81e6a578e2SVinod Koul static const struct of_device_id pm8916_pon_id_table[] = { 82fce5430fSJohn Stultz { .compatible = "qcom,pm8916-pon", .data = (void *)GEN1_REASON_SHIFT }, 83fce5430fSJohn Stultz { .compatible = "qcom,pms405-pon", .data = (void *)GEN1_REASON_SHIFT }, 84fce5430fSJohn Stultz { .compatible = "qcom,pm8998-pon", .data = (void *)GEN2_REASON_SHIFT }, 85*955d095aSAnjelique Melendez { .compatible = "qcom,pmk8350-pon", .data = (void *)GEN2_REASON_SHIFT }, 86e6a578e2SVinod Koul { } 87e6a578e2SVinod Koul }; 88e6a578e2SVinod Koul MODULE_DEVICE_TABLE(of, pm8916_pon_id_table); 89e6a578e2SVinod Koul 90e6a578e2SVinod Koul static struct platform_driver pm8916_pon_driver = { 91e6a578e2SVinod Koul .probe = pm8916_pon_probe, 92e6a578e2SVinod Koul .driver = { 93e6a578e2SVinod Koul .name = "pm8916-pon", 94e6a578e2SVinod Koul .of_match_table = of_match_ptr(pm8916_pon_id_table), 95e6a578e2SVinod Koul }, 96e6a578e2SVinod Koul }; 97e6a578e2SVinod Koul module_platform_driver(pm8916_pon_driver); 98e6a578e2SVinod Koul 99e6a578e2SVinod Koul MODULE_DESCRIPTION("pm8916 Power On driver"); 100e6a578e2SVinod Koul MODULE_LICENSE("GPL v2"); 101