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, 37fce5430fSJohn Stultz 0xfc, magic << pon->reason_shift); 38e6a578e2SVinod Koul if (ret < 0) 39e6a578e2SVinod Koul dev_err(pon->dev, "update reboot mode bits failed\n"); 40e6a578e2SVinod Koul 41e6a578e2SVinod Koul return ret; 42e6a578e2SVinod Koul } 43e6a578e2SVinod Koul 44e6a578e2SVinod Koul static int pm8916_pon_probe(struct platform_device *pdev) 45e6a578e2SVinod Koul { 46e6a578e2SVinod Koul struct pm8916_pon *pon; 47e6a578e2SVinod Koul int error; 48e6a578e2SVinod Koul 49e6a578e2SVinod Koul pon = devm_kzalloc(&pdev->dev, sizeof(*pon), GFP_KERNEL); 50e6a578e2SVinod Koul if (!pon) 51e6a578e2SVinod Koul return -ENOMEM; 52e6a578e2SVinod Koul 53e6a578e2SVinod Koul pon->dev = &pdev->dev; 54e6a578e2SVinod Koul 55e6a578e2SVinod Koul pon->regmap = dev_get_regmap(pdev->dev.parent, NULL); 56e6a578e2SVinod Koul if (!pon->regmap) { 57e6a578e2SVinod Koul dev_err(&pdev->dev, "failed to locate regmap\n"); 58e6a578e2SVinod Koul return -ENODEV; 59e6a578e2SVinod Koul } 60e6a578e2SVinod Koul 61e6a578e2SVinod Koul error = of_property_read_u32(pdev->dev.of_node, "reg", 62e6a578e2SVinod Koul &pon->baseaddr); 63e6a578e2SVinod Koul if (error) 64e6a578e2SVinod Koul return error; 65e6a578e2SVinod Koul 66e6a578e2SVinod Koul pon->reboot_mode.dev = &pdev->dev; 67fce5430fSJohn Stultz pon->reason_shift = (long)of_device_get_match_data(&pdev->dev); 68e6a578e2SVinod Koul pon->reboot_mode.write = pm8916_reboot_mode_write; 69e6a578e2SVinod Koul error = devm_reboot_mode_register(&pdev->dev, &pon->reboot_mode); 70e6a578e2SVinod Koul if (error) { 71e6a578e2SVinod Koul dev_err(&pdev->dev, "can't register reboot mode\n"); 72e6a578e2SVinod Koul return error; 73e6a578e2SVinod Koul } 74e6a578e2SVinod Koul 75e6a578e2SVinod Koul platform_set_drvdata(pdev, pon); 76e6a578e2SVinod Koul 77e6a578e2SVinod Koul return devm_of_platform_populate(&pdev->dev); 78e6a578e2SVinod Koul } 79e6a578e2SVinod Koul 80e6a578e2SVinod Koul static const struct of_device_id pm8916_pon_id_table[] = { 81fce5430fSJohn Stultz { .compatible = "qcom,pm8916-pon", .data = (void *)GEN1_REASON_SHIFT }, 82fce5430fSJohn Stultz { .compatible = "qcom,pms405-pon", .data = (void *)GEN1_REASON_SHIFT }, 83fce5430fSJohn Stultz { .compatible = "qcom,pm8998-pon", .data = (void *)GEN2_REASON_SHIFT }, 84e6a578e2SVinod Koul { } 85e6a578e2SVinod Koul }; 86e6a578e2SVinod Koul MODULE_DEVICE_TABLE(of, pm8916_pon_id_table); 87e6a578e2SVinod Koul 88e6a578e2SVinod Koul static struct platform_driver pm8916_pon_driver = { 89e6a578e2SVinod Koul .probe = pm8916_pon_probe, 90e6a578e2SVinod Koul .driver = { 91e6a578e2SVinod Koul .name = "pm8916-pon", 92e6a578e2SVinod Koul .of_match_table = of_match_ptr(pm8916_pon_id_table), 93e6a578e2SVinod Koul }, 94e6a578e2SVinod Koul }; 95e6a578e2SVinod Koul module_platform_driver(pm8916_pon_driver); 96e6a578e2SVinod Koul 97e6a578e2SVinod Koul MODULE_DESCRIPTION("pm8916 Power On driver"); 98e6a578e2SVinod Koul MODULE_LICENSE("GPL v2"); 99