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; |
| 38 | struct kref kref; |
| 39 | }; |
| 40 | |
| 41 | static const struct { |
| 42 | u8 reg_val; |
| 43 | int user_time; /* In seconds */ |
| 44 | } da9055_wdt_maps[] = { |
| 45 | { 0, 0 }, |
| 46 | { 1, 2 }, |
| 47 | { 2, 4 }, |
| 48 | { 3, 8 }, |
| 49 | { 4, 16 }, |
| 50 | { 5, 32 }, |
| 51 | { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */ |
| 52 | { 6, 65 }, |
| 53 | { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */ |
| 54 | { 7, 131 }, |
| 55 | }; |
| 56 | |
| 57 | static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev, |
| 58 | unsigned int timeout) |
| 59 | { |
| 60 | struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev); |
| 61 | struct da9055 *da9055 = driver_data->da9055; |
| 62 | int ret, i; |
| 63 | |
| 64 | for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++) |
| 65 | if (da9055_wdt_maps[i].user_time == timeout) |
| 66 | break; |
| 67 | |
| 68 | if (i == ARRAY_SIZE(da9055_wdt_maps)) |
| 69 | ret = -EINVAL; |
| 70 | else |
| 71 | ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B, |
| 72 | DA9055_TWDSCALE_MASK, |
| 73 | da9055_wdt_maps[i].reg_val << |
| 74 | DA9055_TWDSCALE_SHIFT); |
Axel Lin | 98e4a29 | 2012-12-22 11:07:01 +0800 | [diff] [blame] | 75 | if (ret < 0) { |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 76 | dev_err(da9055->dev, |
| 77 | "Failed to update timescale bit, %d\n", ret); |
Axel Lin | 98e4a29 | 2012-12-22 11:07:01 +0800 | [diff] [blame] | 78 | return ret; |
| 79 | } |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 80 | |
| 81 | wdt_dev->timeout = timeout; |
| 82 | |
Axel Lin | 98e4a29 | 2012-12-22 11:07:01 +0800 | [diff] [blame] | 83 | return 0; |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static int da9055_wdt_ping(struct watchdog_device *wdt_dev) |
| 87 | { |
| 88 | struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev); |
| 89 | struct da9055 *da9055 = driver_data->da9055; |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * We have a minimum time for watchdog window called TWDMIN. A write |
| 93 | * to the watchdog before this elapsed time will cause an error. |
| 94 | */ |
| 95 | mdelay(DA9055_TWDMIN); |
| 96 | |
| 97 | /* Reset the watchdog timer */ |
Axel Lin | 98e4a29 | 2012-12-22 11:07:01 +0800 | [diff] [blame] | 98 | return da9055_reg_update(da9055, DA9055_REG_CONTROL_E, |
| 99 | DA9055_WATCHDOG_MASK, 1); |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void da9055_wdt_release_resources(struct kref *r) |
| 103 | { |
Ashish Jangam | 312b00e1 | 2012-10-12 15:00:03 +0530 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void da9055_wdt_ref(struct watchdog_device *wdt_dev) |
| 107 | { |
| 108 | struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev); |
| 109 | |
| 110 | kref_get(&driver_data->kref); |
| 111 | } |
| 112 | |
| 113 | static void da9055_wdt_unref(struct watchdog_device *wdt_dev) |
| 114 | { |
| 115 | struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev); |
| 116 | |
| 117 | kref_put(&driver_data->kref, da9055_wdt_release_resources); |
| 118 | } |
| 119 | |
| 120 | static int da9055_wdt_start(struct watchdog_device *wdt_dev) |
| 121 | { |
| 122 | return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout); |
| 123 | } |
| 124 | |
| 125 | static int da9055_wdt_stop(struct watchdog_device *wdt_dev) |
| 126 | { |
| 127 | return da9055_wdt_set_timeout(wdt_dev, 0); |
| 128 | } |
| 129 | |
| 130 | static struct watchdog_info da9055_wdt_info = { |
| 131 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 132 | .identity = "DA9055 Watchdog", |
| 133 | }; |
| 134 | |
| 135 | static const struct watchdog_ops da9055_wdt_ops = { |
| 136 | .owner = THIS_MODULE, |
| 137 | .start = da9055_wdt_start, |
| 138 | .stop = da9055_wdt_stop, |
| 139 | .ping = da9055_wdt_ping, |
| 140 | .set_timeout = da9055_wdt_set_timeout, |
| 141 | .ref = da9055_wdt_ref, |
| 142 | .unref = da9055_wdt_unref, |
| 143 | }; |
| 144 | |
| 145 | static int da9055_wdt_probe(struct platform_device *pdev) |
| 146 | { |
| 147 | struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent); |
| 148 | struct da9055_wdt_data *driver_data; |
| 149 | struct watchdog_device *da9055_wdt; |
| 150 | int ret; |
| 151 | |
| 152 | driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), |
| 153 | GFP_KERNEL); |
| 154 | if (!driver_data) { |
| 155 | dev_err(da9055->dev, "Failed to allocate watchdog device\n"); |
| 156 | return -ENOMEM; |
| 157 | } |
| 158 | |
| 159 | driver_data->da9055 = da9055; |
| 160 | |
| 161 | da9055_wdt = &driver_data->wdt; |
| 162 | |
| 163 | da9055_wdt->timeout = DA9055_DEF_TIMEOUT; |
| 164 | da9055_wdt->info = &da9055_wdt_info; |
| 165 | da9055_wdt->ops = &da9055_wdt_ops; |
| 166 | watchdog_set_nowayout(da9055_wdt, nowayout); |
| 167 | watchdog_set_drvdata(da9055_wdt, driver_data); |
| 168 | |
| 169 | kref_init(&driver_data->kref); |
| 170 | |
| 171 | ret = da9055_wdt_stop(da9055_wdt); |
| 172 | if (ret < 0) { |
| 173 | dev_err(&pdev->dev, "Failed to stop watchdog, %d\n", ret); |
| 174 | goto err; |
| 175 | } |
| 176 | |
| 177 | dev_set_drvdata(&pdev->dev, driver_data); |
| 178 | |
| 179 | ret = watchdog_register_device(&driver_data->wdt); |
| 180 | if (ret != 0) |
| 181 | dev_err(da9055->dev, "watchdog_register_device() failed: %d\n", |
| 182 | ret); |
| 183 | |
| 184 | err: |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | static int da9055_wdt_remove(struct platform_device *pdev) |
| 189 | { |
| 190 | struct da9055_wdt_data *driver_data = dev_get_drvdata(&pdev->dev); |
| 191 | |
| 192 | watchdog_unregister_device(&driver_data->wdt); |
| 193 | kref_put(&driver_data->kref, da9055_wdt_release_resources); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static struct platform_driver da9055_wdt_driver = { |
| 199 | .probe = da9055_wdt_probe, |
| 200 | .remove = da9055_wdt_remove, |
| 201 | .driver = { |
| 202 | .name = "da9055-watchdog", |
| 203 | }, |
| 204 | }; |
| 205 | |
| 206 | module_platform_driver(da9055_wdt_driver); |
| 207 | |
| 208 | MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); |
| 209 | MODULE_DESCRIPTION("DA9055 watchdog"); |
| 210 | MODULE_LICENSE("GPL"); |
| 211 | MODULE_ALIAS("platform:da9055-watchdog"); |