Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 1 | /* |
| 2 | * System monitoring driver for DA9055 PMICs. |
| 3 | * |
| 4 | * Copyright(c) 2012 Dialog Semiconductor Ltd. |
| 5 | * |
| 6 | * Author: David Dajun Chen <dchen@diasemi.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/watchdog.h> |
| 21 | #include <linux/delay.h> |
| 22 | |
| 23 | #include <linux/mfd/da9055/core.h> |
| 24 | #include <linux/mfd/da9055/reg.h> |
| 25 | |
| 26 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 27 | module_param(nowayout, bool, 0); |
| 28 | MODULE_PARM_DESC(nowayout, |
| 29 | "Watchdog cannot be stopped once started (default=" |
| 30 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 31 | |
| 32 | #define DA9055_DEF_TIMEOUT 4 |
| 33 | #define DA9055_TWDMIN 256 |
| 34 | |
| 35 | struct da9055_wdt_data { |
| 36 | struct watchdog_device wdt; |
| 37 | struct da9055 *da9055; |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static const struct { |
| 41 | u8 reg_val; |
| 42 | int user_time; /* In seconds */ |
| 43 | } da9055_wdt_maps[] = { |
| 44 | { 0, 0 }, |
| 45 | { 1, 2 }, |
| 46 | { 2, 4 }, |
| 47 | { 3, 8 }, |
| 48 | { 4, 16 }, |
| 49 | { 5, 32 }, |
| 50 | { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */ |
| 51 | { 6, 65 }, |
| 52 | { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */ |
| 53 | { 7, 131 }, |
| 54 | }; |
| 55 | |
| 56 | static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev, |
| 57 | unsigned int timeout) |
| 58 | { |
| 59 | struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev); |
| 60 | struct da9055 *da9055 = driver_data->da9055; |
| 61 | int ret, i; |
| 62 | |
| 63 | for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++) |
| 64 | if (da9055_wdt_maps[i].user_time == timeout) |
| 65 | break; |
| 66 | |
| 67 | if (i == ARRAY_SIZE(da9055_wdt_maps)) |
| 68 | ret = -EINVAL; |
| 69 | else |
| 70 | ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B, |
| 71 | DA9055_TWDSCALE_MASK, |
| 72 | da9055_wdt_maps[i].reg_val << |
| 73 | DA9055_TWDSCALE_SHIFT); |
Axel Lin | 98e4a29 | 2012-12-22 11:07:01 +0800 | [diff] [blame] | 74 | if (ret < 0) { |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 75 | dev_err(da9055->dev, |
| 76 | "Failed to update timescale bit, %d\n", ret); |
Axel Lin | 98e4a29 | 2012-12-22 11:07:01 +0800 | [diff] [blame] | 77 | return ret; |
| 78 | } |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 79 | |
| 80 | wdt_dev->timeout = timeout; |
| 81 | |
Axel Lin | 98e4a29 | 2012-12-22 11:07:01 +0800 | [diff] [blame] | 82 | return 0; |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static int da9055_wdt_ping(struct watchdog_device *wdt_dev) |
| 86 | { |
| 87 | struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev); |
| 88 | struct da9055 *da9055 = driver_data->da9055; |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * We have a minimum time for watchdog window called TWDMIN. A write |
| 92 | * to the watchdog before this elapsed time will cause an error. |
| 93 | */ |
| 94 | mdelay(DA9055_TWDMIN); |
| 95 | |
| 96 | /* Reset the watchdog timer */ |
Axel Lin | 98e4a29 | 2012-12-22 11:07:01 +0800 | [diff] [blame] | 97 | return da9055_reg_update(da9055, DA9055_REG_CONTROL_E, |
| 98 | DA9055_WATCHDOG_MASK, 1); |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 99 | } |
| 100 | |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 101 | static int da9055_wdt_start(struct watchdog_device *wdt_dev) |
| 102 | { |
| 103 | return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout); |
| 104 | } |
| 105 | |
| 106 | static int da9055_wdt_stop(struct watchdog_device *wdt_dev) |
| 107 | { |
| 108 | return da9055_wdt_set_timeout(wdt_dev, 0); |
| 109 | } |
| 110 | |
| 111 | static struct watchdog_info da9055_wdt_info = { |
| 112 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 113 | .identity = "DA9055 Watchdog", |
| 114 | }; |
| 115 | |
| 116 | static const struct watchdog_ops da9055_wdt_ops = { |
| 117 | .owner = THIS_MODULE, |
| 118 | .start = da9055_wdt_start, |
| 119 | .stop = da9055_wdt_stop, |
| 120 | .ping = da9055_wdt_ping, |
| 121 | .set_timeout = da9055_wdt_set_timeout, |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static int da9055_wdt_probe(struct platform_device *pdev) |
| 125 | { |
| 126 | struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent); |
| 127 | struct da9055_wdt_data *driver_data; |
| 128 | struct watchdog_device *da9055_wdt; |
| 129 | int ret; |
| 130 | |
| 131 | driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), |
| 132 | GFP_KERNEL); |
Jingoo Han | 8deea83 | 2014-02-11 15:46:43 +0900 | [diff] [blame] | 133 | if (!driver_data) |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 134 | return -ENOMEM; |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 135 | |
| 136 | driver_data->da9055 = da9055; |
| 137 | |
| 138 | da9055_wdt = &driver_data->wdt; |
| 139 | |
| 140 | da9055_wdt->timeout = DA9055_DEF_TIMEOUT; |
| 141 | da9055_wdt->info = &da9055_wdt_info; |
| 142 | da9055_wdt->ops = &da9055_wdt_ops; |
Pratyush Anand | 6551881 | 2015-08-20 14:05:01 +0530 | [diff] [blame] | 143 | da9055_wdt->parent = &pdev->dev; |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 144 | watchdog_set_nowayout(da9055_wdt, nowayout); |
| 145 | watchdog_set_drvdata(da9055_wdt, driver_data); |
| 146 | |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 147 | ret = da9055_wdt_stop(da9055_wdt); |
| 148 | if (ret < 0) { |
| 149 | dev_err(&pdev->dev, "Failed to stop watchdog, %d\n", ret); |
| 150 | goto err; |
| 151 | } |
| 152 | |
Jingoo Han | 76692c9 | 2013-05-23 19:44:23 +0900 | [diff] [blame] | 153 | platform_set_drvdata(pdev, driver_data); |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 154 | |
| 155 | ret = watchdog_register_device(&driver_data->wdt); |
| 156 | if (ret != 0) |
| 157 | dev_err(da9055->dev, "watchdog_register_device() failed: %d\n", |
| 158 | ret); |
| 159 | |
| 160 | err: |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | static int da9055_wdt_remove(struct platform_device *pdev) |
| 165 | { |
Jingoo Han | 76692c9 | 2013-05-23 19:44:23 +0900 | [diff] [blame] | 166 | struct da9055_wdt_data *driver_data = platform_get_drvdata(pdev); |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 167 | |
| 168 | watchdog_unregister_device(&driver_data->wdt); |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static struct platform_driver da9055_wdt_driver = { |
| 174 | .probe = da9055_wdt_probe, |
| 175 | .remove = da9055_wdt_remove, |
| 176 | .driver = { |
| 177 | .name = "da9055-watchdog", |
| 178 | }, |
| 179 | }; |
| 180 | |
| 181 | module_platform_driver(da9055_wdt_driver); |
| 182 | |
| 183 | MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); |
| 184 | MODULE_DESCRIPTION("DA9055 watchdog"); |
| 185 | MODULE_LICENSE("GPL"); |
| 186 | MODULE_ALIAS("platform:da9055-watchdog"); |