Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

watchdog: Convert max63xx_wdt driver to watchdog framework

This patch converts max63xx_wdt driver to watchdog framework.
Also use devm_* APIs to save a few error handling code.

Signed-off-by: Axel Lin <axel.lin@gmail.com>
Signed-off-by: Wim Van Sebroeck <wim@iguana.be>

authored by

Axel Lin and committed by
Wim Van Sebroeck
a0f36833 6b1e8386

+34 -161
+1
drivers/watchdog/Kconfig
··· 330 330 config MAX63XX_WATCHDOG 331 331 tristate "Max63xx watchdog" 332 332 depends on ARM && HAS_IOMEM 333 + select WATCHDOG_CORE 333 334 help 334 335 Support for memory mapped max63{69,70,71,72,73,74} watchdog timer. 335 336
+33 -161
drivers/watchdog/max63xx_wdt.c
··· 18 18 #include <linux/moduleparam.h> 19 19 #include <linux/types.h> 20 20 #include <linux/kernel.h> 21 - #include <linux/fs.h> 22 21 #include <linux/miscdevice.h> 23 22 #include <linux/watchdog.h> 24 23 #include <linux/init.h> 25 24 #include <linux/bitops.h> 26 25 #include <linux/platform_device.h> 27 26 #include <linux/spinlock.h> 28 - #include <linux/uaccess.h> 29 27 #include <linux/io.h> 30 - #include <linux/device.h> 31 28 #include <linux/slab.h> 32 29 33 30 #define DEFAULT_HEARTBEAT 60 34 31 #define MAX_HEARTBEAT 60 35 32 36 - static int heartbeat = DEFAULT_HEARTBEAT; 33 + static unsigned int heartbeat = DEFAULT_HEARTBEAT; 37 34 static bool nowayout = WATCHDOG_NOWAYOUT; 38 35 39 36 /* ··· 42 45 43 46 static DEFINE_SPINLOCK(io_lock); 44 47 45 - static unsigned long wdt_status; 46 - #define WDT_IN_USE 0 47 - #define WDT_RUNNING 1 48 - #define WDT_OK_TO_CLOSE 2 49 - 50 48 static int nodelay; 51 - static struct resource *wdt_mem; 52 49 static void __iomem *wdt_base; 53 - static struct platform_device *max63xx_pdev; 54 50 55 51 /* 56 52 * The timeout values used are actually the absolute minimum the chip ··· 107 117 return NULL; 108 118 } 109 119 110 - static void max63xx_wdt_ping(void) 120 + static int max63xx_wdt_ping(struct watchdog_device *wdd) 111 121 { 112 122 u8 val; 113 123 ··· 119 129 __raw_writeb(val & ~MAX6369_WDI, wdt_base); 120 130 121 131 spin_unlock(&io_lock); 132 + return 0; 122 133 } 123 134 124 - static void max63xx_wdt_enable(struct max63xx_timeout *entry) 135 + static int max63xx_wdt_start(struct watchdog_device *wdd) 125 136 { 137 + struct max63xx_timeout *entry = watchdog_get_drvdata(wdd); 126 138 u8 val; 127 - 128 - if (test_and_set_bit(WDT_RUNNING, &wdt_status)) 129 - return; 130 139 131 140 spin_lock(&io_lock); 132 141 ··· 138 149 139 150 /* check for a edge triggered startup */ 140 151 if (entry->tdelay == 0) 141 - max63xx_wdt_ping(); 152 + max63xx_wdt_ping(wdd); 153 + return 0; 142 154 } 143 155 144 - static void max63xx_wdt_disable(void) 156 + static int max63xx_wdt_stop(struct watchdog_device *wdd) 145 157 { 146 158 u8 val; 147 159 ··· 154 164 __raw_writeb(val, wdt_base); 155 165 156 166 spin_unlock(&io_lock); 157 - 158 - clear_bit(WDT_RUNNING, &wdt_status); 159 - } 160 - 161 - static int max63xx_wdt_open(struct inode *inode, struct file *file) 162 - { 163 - if (test_and_set_bit(WDT_IN_USE, &wdt_status)) 164 - return -EBUSY; 165 - 166 - max63xx_wdt_enable(current_timeout); 167 - clear_bit(WDT_OK_TO_CLOSE, &wdt_status); 168 - 169 - return nonseekable_open(inode, file); 170 - } 171 - 172 - static ssize_t max63xx_wdt_write(struct file *file, const char *data, 173 - size_t len, loff_t *ppos) 174 - { 175 - if (len) { 176 - if (!nowayout) { 177 - size_t i; 178 - 179 - clear_bit(WDT_OK_TO_CLOSE, &wdt_status); 180 - for (i = 0; i != len; i++) { 181 - char c; 182 - 183 - if (get_user(c, data + i)) 184 - return -EFAULT; 185 - 186 - if (c == 'V') 187 - set_bit(WDT_OK_TO_CLOSE, &wdt_status); 188 - } 189 - } 190 - 191 - max63xx_wdt_ping(); 192 - } 193 - 194 - return len; 195 - } 196 - 197 - static const struct watchdog_info ident = { 198 - .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, 199 - .identity = "max63xx Watchdog", 200 - }; 201 - 202 - static long max63xx_wdt_ioctl(struct file *file, unsigned int cmd, 203 - unsigned long arg) 204 - { 205 - int ret = -ENOTTY; 206 - 207 - switch (cmd) { 208 - case WDIOC_GETSUPPORT: 209 - ret = copy_to_user((struct watchdog_info *)arg, &ident, 210 - sizeof(ident)) ? -EFAULT : 0; 211 - break; 212 - 213 - case WDIOC_GETSTATUS: 214 - case WDIOC_GETBOOTSTATUS: 215 - ret = put_user(0, (int *)arg); 216 - break; 217 - 218 - case WDIOC_KEEPALIVE: 219 - max63xx_wdt_ping(); 220 - ret = 0; 221 - break; 222 - 223 - case WDIOC_GETTIMEOUT: 224 - ret = put_user(heartbeat, (int *)arg); 225 - break; 226 - } 227 - return ret; 228 - } 229 - 230 - static int max63xx_wdt_release(struct inode *inode, struct file *file) 231 - { 232 - if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) 233 - max63xx_wdt_disable(); 234 - else 235 - dev_crit(&max63xx_pdev->dev, 236 - "device closed unexpectedly - timer will not stop\n"); 237 - 238 - clear_bit(WDT_IN_USE, &wdt_status); 239 - clear_bit(WDT_OK_TO_CLOSE, &wdt_status); 240 - 241 167 return 0; 242 168 } 243 169 244 - static const struct file_operations max63xx_wdt_fops = { 245 - .owner = THIS_MODULE, 246 - .llseek = no_llseek, 247 - .write = max63xx_wdt_write, 248 - .unlocked_ioctl = max63xx_wdt_ioctl, 249 - .open = max63xx_wdt_open, 250 - .release = max63xx_wdt_release, 170 + static const struct watchdog_info max63xx_wdt_info = { 171 + .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, 172 + .identity = "max63xx Watchdog", 251 173 }; 252 174 253 - static struct miscdevice max63xx_wdt_miscdev = { 254 - .minor = WATCHDOG_MINOR, 255 - .name = "watchdog", 256 - .fops = &max63xx_wdt_fops, 175 + static const struct watchdog_ops max63xx_wdt_ops = { 176 + .owner = THIS_MODULE, 177 + .start = max63xx_wdt_start, 178 + .stop = max63xx_wdt_stop, 179 + .ping = max63xx_wdt_ping, 180 + }; 181 + 182 + static struct watchdog_device max63xx_wdt_dev = { 183 + .info = &max63xx_wdt_info, 184 + .ops = &max63xx_wdt_ops, 257 185 }; 258 186 259 187 static int __devinit max63xx_wdt_probe(struct platform_device *pdev) 260 188 { 261 - int ret = 0; 262 - int size; 263 - struct device *dev = &pdev->dev; 189 + struct resource *wdt_mem; 264 190 struct max63xx_timeout *table; 265 191 266 192 table = (struct max63xx_timeout *)pdev->id_entry->driver_data; ··· 184 278 if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT) 185 279 heartbeat = DEFAULT_HEARTBEAT; 186 280 187 - dev_info(dev, "requesting %ds heartbeat\n", heartbeat); 281 + dev_info(&pdev->dev, "requesting %ds heartbeat\n", heartbeat); 188 282 current_timeout = max63xx_select_timeout(table, heartbeat); 189 283 190 284 if (!current_timeout) { 191 - dev_err(dev, "unable to satisfy heartbeat request\n"); 285 + dev_err(&pdev->dev, "unable to satisfy heartbeat request\n"); 192 286 return -EINVAL; 193 287 } 194 288 195 - dev_info(dev, "using %ds heartbeat with %ds initial delay\n", 289 + dev_info(&pdev->dev, "using %ds heartbeat with %ds initial delay\n", 196 290 current_timeout->twd, current_timeout->tdelay); 197 291 198 292 heartbeat = current_timeout->twd; 199 293 200 - max63xx_pdev = pdev; 201 - 202 294 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 203 - if (wdt_mem == NULL) { 204 - dev_err(dev, "failed to get memory region resource\n"); 205 - return -ENOENT; 206 - } 295 + wdt_base = devm_request_and_ioremap(&pdev->dev, wdt_mem); 296 + if (!wdt_base) 297 + return -ENOMEM; 207 298 208 - size = resource_size(wdt_mem); 209 - if (!request_mem_region(wdt_mem->start, size, pdev->name)) { 210 - dev_err(dev, "failed to get memory region\n"); 211 - return -ENOENT; 212 - } 299 + max63xx_wdt_dev.timeout = heartbeat; 300 + watchdog_set_nowayout(&max63xx_wdt_dev, nowayout); 301 + watchdog_set_drvdata(&max63xx_wdt_dev, current_timeout); 213 302 214 - wdt_base = ioremap(wdt_mem->start, size); 215 - if (!wdt_base) { 216 - dev_err(dev, "failed to map memory region\n"); 217 - ret = -ENOMEM; 218 - goto out_request; 219 - } 220 - 221 - ret = misc_register(&max63xx_wdt_miscdev); 222 - if (ret < 0) { 223 - dev_err(dev, "cannot register misc device\n"); 224 - goto out_unmap; 225 - } 226 - 227 - return 0; 228 - 229 - out_unmap: 230 - iounmap(wdt_base); 231 - out_request: 232 - release_mem_region(wdt_mem->start, size); 233 - wdt_mem = NULL; 234 - 235 - return ret; 303 + return watchdog_register_device(&max63xx_wdt_dev); 236 304 } 237 305 238 306 static int __devexit max63xx_wdt_remove(struct platform_device *pdev) 239 307 { 240 - misc_deregister(&max63xx_wdt_miscdev); 241 - if (wdt_mem) { 242 - release_mem_region(wdt_mem->start, resource_size(wdt_mem)); 243 - wdt_mem = NULL; 244 - } 245 - 246 - if (wdt_base) 247 - iounmap(wdt_base); 248 - 308 + watchdog_unregister_device(&max63xx_wdt_dev); 249 309 return 0; 250 310 } 251 311