summaryrefslogtreecommitdiff
path: root/drivers/mfd/da9053.c
blob: 1f3286903893f0bf8640f05bdc81467cb30802cd (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) 2013 Jan Luebbe <jlu@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *
 */

#include <common.h>
#include <driver.h>
#include <xfuncs.h>
#include <errno.h>
#include <watchdog.h>
#include <reset_source.h>

#include <i2c/i2c.h>
#include <restart.h>

#define DRIVERNAME		"da9053"

/* STATUS REGISTERS */
#define DA9053_STATUS_A_REG		1
#define DA9053_STATUS_B_REG		2
#define DA9053_STATUS_C_REG		3
#define DA9053_STATUS_D_REG		4

/* PARK REGISTER */
#define DA9053_PARK_REGISTER		DA9053_STATUS_D_REG

/* EVENT REGISTERS */
#define DA9053_EVENT_A_REG		5
#define DA9053_EVENT_B_REG		6
#define DA9053_EVENT_C_REG		7
#define DA9053_EVENT_D_REG		8
#define DA9053_FAULTLOG_REG		9

/* IRQ REGISTERS */
#define DA9053_IRQ_MASK_A_REG		10
#define DA9053_IRQ_MASK_B_REG		11
#define DA9053_IRQ_MASK_C_REG		12
#define DA9053_IRQ_MASK_D_REG		13

/* CONTROL REGISTERS */
#define DA9053_CONTROL_A_REG		14
#define DA9053_CONTROL_B_REG		15
#define DA9053_CONTROL_C_REG		16
#define DA9053_CONTROL_D_REG		17

#define DA9053_PDDIS_REG		18
#define DA9053_INTERFACE_REG		19
#define DA9053_RESET_REG		20

/* FAULT LOG REGISTER BITS */
#define DA9053_FAULTLOG_WAITSET		0X80
#define DA9053_FAULTLOG_NSDSET		0X40
#define DA9053_FAULTLOG_KEYSHUT		0X20
#define DA9053_FAULTLOG_TEMPOVER	0X08
#define DA9053_FAULTLOG_VDDSTART	0X04
#define DA9053_FAULTLOG_VDDFAULT	0X02
#define DA9053_FAULTLOG_TWDERROR	0X01

/* CONTROL REGISTER B BITS */
#define DA9053_CONTROLB_SHUTDOWN	0X80
#define DA9053_CONTROLB_DEEPSLEEP	0X40
#define DA9053_CONTROLB_WRITEMODE	0X20
#define DA9053_CONTROLB_BBATEN		0X10
#define DA9053_CONTROLB_OTPREADEN	0X08
#define DA9053_CONTROLB_AUTOBOOT	0X04
#define DA9053_CONTROLB_ACTDIODE	0X02
#define DA9053_CONTROLB_BUCKMERGE	0X01

/* CONTROL REGISTER D BITS */
#define DA9053_CONTROLD_WATCHDOG	0X80
#define DA9053_CONTROLD_ACCDETEN	0X40
#define DA9053_CONTROLD_GPI1415SD	0X20
#define DA9053_CONTROLD_NONKEYSD	0X10
#define DA9053_CONTROLD_KEEPACTEN	0X08
#define DA9053_CONTROLD_TWDSCALE	0X07

struct da9053_priv {
	struct watchdog		wd;
	struct i2c_client	*client;
	struct device_d		*dev;
	struct restart_handler	restart;
};

#define wd_to_da9053_priv(x)	container_of(x, struct da9053_priv, wd)

static int da9053_reg_read(struct da9053_priv *da9053, u32 reg, u8 *val)
{
	int ret;

	ret = i2c_read_reg(da9053->client, reg, val, 1);

	return ret == 1 ? 0 : ret;
}

static int da9053_reg_write(struct da9053_priv *da9053, u32 reg, u8 val)
{
	int ret;

	ret = i2c_write_reg(da9053->client, reg, &val, 1);

	return ret == 1 ? 0 : ret;
}

static int da9053_park(struct da9053_priv *da9053)
{
	int ret;
	u8 val;

	ret = i2c_read_reg(da9053->client, DA9053_PARK_REGISTER, &val, 1);

	return ret == 1 ? 0 : ret;
}

static int da9053_enable_multiwrite(struct da9053_priv *da9053)
{
	int ret;
	u8 val;

	ret = da9053_reg_read(da9053, DA9053_CONTROL_B_REG, &val);
	if (ret < 0)
		return ret;

	if (val & DA9053_CONTROLB_WRITEMODE) {
		val &= ~DA9053_CONTROLB_WRITEMODE;
		ret = da9053_reg_write(da9053, DA9053_CONTROL_B_REG, val);
		if (ret < 0)
			return ret;
	}

	ret = da9053_park(da9053);
	if (ret < 0)
		return ret;

	return 0;
}

static int da9053_set_timeout(struct watchdog *wd, unsigned timeout)
{
	struct da9053_priv *da9053 = wd_to_da9053_priv(wd);
	struct device_d *dev = da9053->dev;
	unsigned scale = 0;
	int ret;
	u8 val;

	if (timeout > 131)
		return -EINVAL;

	if (timeout) {
		timeout *= 1000; /* convert to ms */
		scale = 0;
		while (timeout > (2048 << scale) && scale <= 6)
			scale++;
		dev_dbg(dev, "calculated TWDSCALE=%u (req=%ims calc=%ims)\n",
		        scale, timeout, 2048 << scale);
		scale++; /* scale 0 disables the WD */
	}

	ret = da9053_reg_read(da9053, DA9053_CONTROL_D_REG, &val);
	if (ret < 0)
		return ret;

	dev_dbg(dev, "read watchdog (val=0x%02x)\n", val);

	if (scale && scale == (val & DA9053_CONTROLD_TWDSCALE)) {
		/* trigger WD */
		val |= DA9053_CONTROLD_WATCHDOG;
		ret = da9053_reg_write(da9053, DA9053_CONTROL_D_REG, val);
		if (ret < 0)
			return ret;
		dev_dbg(dev, "triggered watchdog (val=0x%02x)\n", val);
	} else {
		/* disable WD first */
		val &= ~DA9053_CONTROLD_TWDSCALE;
		ret = da9053_reg_write(da9053, DA9053_CONTROL_D_REG, val);
		if (ret < 0)
			return ret;

		dev_dbg(dev, "disabled watchdog (val=0x%02x)\n", val);
		if (scale) {
			/* park before waiting */
			ret = da9053_park(da9053);
			if (ret < 0)
				return ret;

			/* wait required time */
			udelay(150);

			/* enable WD with new timeout */
			val |= scale;
			ret = da9053_reg_write(da9053, DA9053_CONTROL_D_REG, val);
			if (ret < 0)
				return ret;
			dev_dbg(dev, "enabled watchdog (val=0x%02x)\n", val);
		}
	}

	ret = da9053_park(da9053);
	if (ret < 0)
		return ret;

	return 0;
}

static void da9053_detect_reset_source(struct da9053_priv *da9053)
{
	enum reset_src_type type;
	int ret;
	u8 val;

	ret = da9053_reg_read(da9053, DA9053_FAULTLOG_REG, &val);
	if (ret < 0)
		return;

	ret = da9053_park(da9053);
	if (ret < 0)
		return;

	if (val & DA9053_FAULTLOG_TWDERROR)
		type = RESET_WDG;
	else if (val & DA9053_FAULTLOG_VDDFAULT)
		type = RESET_POR;
	else if (val & DA9053_FAULTLOG_NSDSET)
		type = RESET_RST;
	else
		return;

	reset_source_set_device(da9053->dev, type);

	ret = da9053_reg_write(da9053, DA9053_FAULTLOG_REG, val);
	if (ret < 0)
		return;
}

static void __noreturn da9053_force_system_reset(struct restart_handler *rst)
{
	struct da9053_priv *da9053 = container_of(rst, struct da9053_priv, restart);
	u8 val;
	int ret;

	ret = da9053_reg_read(da9053, DA9053_CONTROL_B_REG, &val);
	if (ret < 0)
		hang();

	val |= DA9053_CONTROLB_SHUTDOWN;
	ret = da9053_reg_write(da9053, DA9053_CONTROL_B_REG, val);
	if (ret < 0)
		hang();

	da9053_park(da9053);

	hang();
}

static int da9053_probe(struct device_d *dev)
{
	struct da9053_priv *da9053;
	int ret;

	da9053 = xzalloc(sizeof(*da9053));
	da9053->dev = dev;
	da9053->client = to_i2c_client(dev);
	da9053->wd.set_timeout = da9053_set_timeout;
	da9053->wd.hwdev = dev;

	ret = da9053_enable_multiwrite(da9053);
	if (ret < 0)
		return ret;

	ret = watchdog_register(&da9053->wd);
	if (ret)
		return ret;

	da9053_detect_reset_source(da9053);

	da9053->restart.priority = of_get_restart_priority(dev->device_node);
	da9053->restart.name = "da9063";
	da9053->restart.restart = &da9053_force_system_reset;

	restart_handler_register(&da9053->restart);

	return 0;
}

static __maybe_unused struct of_device_id da9053_dt_ids[] = {
	{
		.compatible = "dlg,da9052",
	}, {
		/* sentinel */
	}
};

static struct driver_d da9053_driver = {
	.name  = DRIVERNAME,
	.probe = da9053_probe,
	.of_compatible = DRV_OF_COMPAT(da9053_dt_ids),
};
device_i2c_driver(da9053_driver);