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