summaryrefslogtreecommitdiff
path: root/drivers/watchdog/da9063-wdt.c
blob: b7216b578630b82b8f517d20521c785bb0df3bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-License-Identifier: GPL-2.0+
/*
 * Based on the Linux drivers/watchdog/da9063_wdt.c file.
 *
 * Watchdog driver for DA9063 PMICs.
 *
 * Copyright(c) 2012 Dialog Semiconductor Ltd.
 *
 * Author: Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>
 *
 * Ported to U-Boot by Fabio Estevam <festevam@denx.de>
 *
 */

#include <dm.h>
#include <dm/device-internal.h>
#include <dm/device_compat.h>
#include <dm/lists.h>
#include <i2c.h>
#include <linux/delay.h>
#include <wdt.h>

#define	DA9063_REG_CONTROL_D		0x11
/* DA9063_REG_CONTROL_D (addr=0x11) */
#define	DA9063_TWDSCALE_MASK		0x0
#define DA9063_TWDSCALE_DISABLE		0
#define	DA9063_REG_CONTROL_F		0x13
/* DA9063_REG_CONTROL_F (addr=0x13) */
#define	DA9063_WATCHDOG			0x01
#define	DA9063_SHUTDOWN			0x02

/*
 * Watchdog selector to timeout in seconds.
 *   0: WDT disabled;
 *   others: timeout = 2048 ms * 2^(TWDSCALE-1).
 */
static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };

#define DA9063_TWDSCALE_DISABLE		0
#define DA9063_TWDSCALE_MIN		1
#define DA9063_TWDSCALE_MAX		(ARRAY_SIZE(wdt_timeout) - 1)

static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
{
	unsigned int i;

	for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) {
		if (wdt_timeout[i] >= secs)
			return i;
	}

	return DA9063_TWDSCALE_MAX;
}

static int da9063_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
{
	return dm_i2c_read(dev->parent, reg, buff, len);
}

static int da9063_write(struct udevice *dev, uint reg, const u8 *buff, int len)
{
	return dm_i2c_write(dev->parent, reg, buff, len);
}

static int da9063_wdt_disable_timer(struct udevice *dev)
{
	u8 val;

	da9063_read(dev, DA9063_REG_CONTROL_D, &val, 1);
	val &= ~DA9063_TWDSCALE_MASK;
	val |= DA9063_TWDSCALE_DISABLE;
	da9063_write(dev, DA9063_REG_CONTROL_D, &val, 1);

	return 0;
}

static int da9063_wdt_update_timeout(struct udevice *dev, unsigned int timeout)
{
	unsigned int regval;
	int ret;
	u8 val;

	/*
	 * The watchdog triggers a reboot if a timeout value is already
	 * programmed because the timeout value combines two functions
	 * in one: indicating the counter limit and starting the watchdog.
	 * The watchdog must be disabled to be able to change the timeout
	 * value if the watchdog is already running. Then we can set the
	 * new timeout value which enables the watchdog again.
	 */
	ret = da9063_wdt_disable_timer(dev);
	if (ret)
		return ret;

	udelay(300);

	regval = da9063_wdt_timeout_to_sel(timeout);

	da9063_read(dev, DA9063_REG_CONTROL_D, &val, 1);
	val &= ~DA9063_TWDSCALE_MASK;
	val |= regval;
	da9063_write(dev, DA9063_REG_CONTROL_D, &val, 1);

	return 0;
}

static int da9063_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
{
	return da9063_wdt_update_timeout(dev, timeout);
}

static int da9063_wdt_stop(struct udevice *dev)
{
	return da9063_wdt_disable_timer(dev);
}

static int da9063_wdt_reset(struct udevice *dev)
{
	u8 val = DA9063_WATCHDOG;

	return da9063_write(dev, DA9063_REG_CONTROL_F, &val, 1);
}

static int da9063_wdt_expire_now(struct udevice *dev, ulong flags)
{
	u8 val = DA9063_SHUTDOWN;

	return da9063_write(dev, DA9063_REG_CONTROL_F, &val, 1);
}

static const struct wdt_ops da9063_wdt_ops = {
	.start = da9063_wdt_start,
	.stop = da9063_wdt_stop,
	.reset = da9063_wdt_reset,
	.expire_now = da9063_wdt_expire_now,
};

static const struct udevice_id da9063_wdt_ids[] = {
	{ .compatible = "dlg,da9063-watchdog", },
	{}
};

U_BOOT_DRIVER(da9063_wdt) = {
	.name = "da9063-wdt",
	.id = UCLASS_WDT,
	.of_match = da9063_wdt_ids,
	.ops = &da9063_wdt_ops,
	.flags = DM_FLAG_PROBE_AFTER_BIND,
};