1 /* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License version 2 as 4 * published by the Free Software Foundation. 5 * 6 * This program is distributed in the hope that it will be useful, 7 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * GNU General Public License for more details. 10 * 11 * Copyright (C) 2012 ARM Limited 12 */ 13 14 #include <linux/jiffies.h> 15 #include <linux/of.h> 16 #include <linux/of_device.h> 17 #include <linux/platform_device.h> 18 #include <linux/stat.h> 19 #include <linux/vexpress.h> 20 21 static void vexpress_reset_do(struct device *dev, const char *what) 22 { 23 int err = -ENOENT; 24 struct vexpress_config_func *func = 25 vexpress_config_func_get_by_dev(dev); 26 27 if (func) { 28 unsigned long timeout; 29 30 err = vexpress_config_write(func, 0, 0); 31 32 timeout = jiffies + HZ; 33 while (time_before(jiffies, timeout)) 34 cpu_relax(); 35 } 36 37 dev_emerg(dev, "Unable to %s (%d)\n", what, err); 38 } 39 40 static struct device *vexpress_power_off_device; 41 42 void vexpress_power_off(void) 43 { 44 vexpress_reset_do(vexpress_power_off_device, "power off"); 45 } 46 47 static struct device *vexpress_restart_device; 48 49 void vexpress_restart(char str, const char *cmd) 50 { 51 vexpress_reset_do(vexpress_restart_device, "restart"); 52 } 53 54 static ssize_t vexpress_reset_active_show(struct device *dev, 55 struct device_attribute *attr, char *buf) 56 { 57 return sprintf(buf, "%d\n", vexpress_restart_device == dev); 58 } 59 60 static ssize_t vexpress_reset_active_store(struct device *dev, 61 struct device_attribute *attr, const char *buf, size_t count) 62 { 63 long value; 64 int err = kstrtol(buf, 0, &value); 65 66 if (!err && value) 67 vexpress_restart_device = dev; 68 69 return err ? err : count; 70 } 71 72 DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show, 73 vexpress_reset_active_store); 74 75 76 enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT }; 77 78 static struct of_device_id vexpress_reset_of_match[] = { 79 { 80 .compatible = "arm,vexpress-reset", 81 .data = (void *)FUNC_RESET, 82 }, { 83 .compatible = "arm,vexpress-shutdown", 84 .data = (void *)FUNC_SHUTDOWN 85 }, { 86 .compatible = "arm,vexpress-reboot", 87 .data = (void *)FUNC_REBOOT 88 }, 89 {} 90 }; 91 92 static int vexpress_reset_probe(struct platform_device *pdev) 93 { 94 enum vexpress_reset_func func; 95 const struct of_device_id *match = 96 of_match_device(vexpress_reset_of_match, &pdev->dev); 97 98 if (match) 99 func = (enum vexpress_reset_func)match->data; 100 else 101 func = pdev->id_entry->driver_data; 102 103 switch (func) { 104 case FUNC_SHUTDOWN: 105 vexpress_power_off_device = &pdev->dev; 106 break; 107 case FUNC_RESET: 108 if (!vexpress_restart_device) 109 vexpress_restart_device = &pdev->dev; 110 device_create_file(&pdev->dev, &dev_attr_active); 111 break; 112 case FUNC_REBOOT: 113 vexpress_restart_device = &pdev->dev; 114 device_create_file(&pdev->dev, &dev_attr_active); 115 break; 116 }; 117 118 return 0; 119 } 120 121 static const struct platform_device_id vexpress_reset_id_table[] = { 122 { .name = "vexpress-reset", .driver_data = FUNC_RESET, }, 123 { .name = "vexpress-shutdown", .driver_data = FUNC_SHUTDOWN, }, 124 { .name = "vexpress-reboot", .driver_data = FUNC_REBOOT, }, 125 {} 126 }; 127 128 static struct platform_driver vexpress_reset_driver = { 129 .probe = vexpress_reset_probe, 130 .driver = { 131 .name = "vexpress-reset", 132 .of_match_table = vexpress_reset_of_match, 133 }, 134 .id_table = vexpress_reset_id_table, 135 }; 136 137 static int __init vexpress_reset_init(void) 138 { 139 return platform_driver_register(&vexpress_reset_driver); 140 } 141 device_initcall(vexpress_reset_init); 142