summaryrefslogtreecommitdiffstats
path: root/drivers/mfd/da9063.c
blob: a7fff2c5bfa3ffcaceb3a15997f689a14d399925 (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
/*
 * Copyright (C) 2015 Pengutronix, Philipp Zabel <p.zabel@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 <restart.h>
#include <i2c/i2c.h>
#include <malloc.h>
#include <notifier.h>
#include <reset_source.h>
#include <watchdog.h>

struct da9063 {
	struct restart_handler	restart;
	struct watchdog		wd;
	struct i2c_client	*client;
	/* dummy client for accessing bank #1 */
	struct i2c_client	*client1;
	struct device_d		*dev;
	unsigned int		timeout;
};

/* forbidden/impossible value; timeout will be set to this value initially to
 * detect ping vs. set_timeout() operations. */
#define DA9063_INITIAL_TIMEOUT	(~0u)

/* System Control and Event Registers */
#define DA9063_REG_FAULT_LOG	0x05
#define DA9063_REG_CONTROL_D	0x11
#define DA9063_REG_CONTROL_F	0x13

/* bank1: control register I */
#define DA9063_REG1_CONFIG_I	0x10e

#define DA9062AA_DEVICE_ID	0x181

/* DA9063_REG_FAULT_LOG (addr=0x05) */
#define DA9063_TWD_ERROR	0x01
#define DA9063_POR		0x02
#define DA9063_NSHUTDOWN	0x40

/* DA9063_REG_CONTROL_D (addr=0x11) */
#define DA9063_TWDSCALE_MASK	0x07

/* DA9063_REG_CONTROL_F (addr=0x13) */
#define DA9063_WATCHDOG		0x01
#define DA9063_SHUTDOWN		0x02

/* DA9063_REG_CONTROL_I (addr=0x10e) */
#define DA9062_WATCHDOG_SD	BIT(3)

struct da906x_device_data {
	int	(*init)(struct da9063 *priv);
};

static int da906x_reg_update(struct da9063 *priv, unsigned int reg,
			     uint8_t mask, uint8_t val)
{
	struct i2c_client *client;
	uint8_t tmp;
	int ret;

	if (reg < 0x100)
		client = priv->client;
	else if (reg < 0x200)
		client = priv->client1;
	else
		/* this should/can not happen because function is usually
		 * called with a static register number; warn about it
		 * below */
		client = NULL;

	if (WARN_ON(!client))
		return -EINVAL;

	ret = i2c_read_reg(client, reg & 0xffu, &tmp, 1);
	if (ret < 0) {
		dev_warn(priv->dev, "failed to read reg %02x\n", reg);
		return ret;
	}

	tmp &= ~mask;
	tmp |= val;

	ret = i2c_write_reg(client, reg & 0xffu, &tmp, 1);
	if (ret < 0) {
		dev_warn(priv->dev, "failed to write %02x into reg %02x\n",
			 tmp, reg);
		return ret;
	}

	return 0;
}

static int da9063_watchdog_ping(struct da9063 *priv)
{
	int ret;
	u8 val;

	dev_dbg(priv->dev, "ping\n");

	/* reset watchdog timer; register is self clearing */
	val = DA9063_WATCHDOG;
	ret = i2c_write_reg(priv->client, DA9063_REG_CONTROL_F, &val, 1);
	if (ret < 0)
		return ret;

	return 0;
}

static int da9063_watchdog_set_timeout(struct watchdog *wd, unsigned timeout)
{
	struct da9063 *priv = container_of(wd, struct da9063, wd);
	struct device_d *dev = priv->dev;
	unsigned int scale = 0;
	int ret;

	if (timeout > 131)
		return -EINVAL;

	timeout *= 1000; /* convert to ms */

	if (timeout == priv->timeout)
		/* set_timeout called with previous parameter; just ping the
		 * watchdog */
		goto out;

	if (timeout) {
		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 = da906x_reg_update(priv, DA9063_REG_CONTROL_D,
				DA9063_TWDSCALE_MASK, scale);
	if (ret < 0)
		return ret;

	priv->timeout = timeout;

out:
	return da9063_watchdog_ping(priv);
}

static void da9063_detect_reset_source(struct da9063 *priv)
{
	int ret;
	u8 val;
	unsigned int priority;
	enum reset_src_type type;

	ret = i2c_read_reg(priv->client, DA9063_REG_FAULT_LOG, &val, 1);
	if (ret < 0)
		return;

	/* Write one to clear */
	i2c_write_reg(priv->client, DA9063_REG_FAULT_LOG, &val, 1);

	if (val & DA9063_TWD_ERROR)
		type = RESET_WDG;
	else if (val & DA9063_POR)
		type = RESET_POR;
	else if (val & DA9063_NSHUTDOWN)
		type = RESET_RST;
	else
		return;

	priority = of_get_reset_source_priority(priv->dev->device_node);

	reset_source_set_priority(type, priority);
}

static void da9063_restart(struct restart_handler *rst)
{
	struct da9063 *priv = container_of(rst, struct da9063, restart);

	u8 val = DA9063_SHUTDOWN;

	i2c_write_reg(priv->client, DA9063_REG_CONTROL_F, &val, 1);
}

static int da9062_device_init(struct da9063 *priv)
{
	int ret;
	uint8_t id[4];

	priv->client1 = i2c_new_dummy(priv->client->adapter,
				      priv->client->addr + 1);
	if (!priv->client1) {
		dev_warn(priv->dev, "failed to create bank 1 device\n");
		/* TODO: return -EINVAL; i2c api does not return more
		 * details */
		return -EINVAL;
	}

	ret = i2c_read_reg(priv->client1, DA9062AA_DEVICE_ID & 0xffu,
			   id, sizeof id);
	if (ret < 0) {
		dev_warn(priv->dev, "failed to read ID: %d\n", ret);
		return ret;
	}

	dev_info(priv->dev, "da9062 with id %02x.%02x.%02x.%02x detected\n",
		 id[0], id[1], id[2], id[3]);

	/* clear CONFIG_I[WATCHDOG_SD] */
	ret = da906x_reg_update(priv, DA9063_REG1_CONFIG_I,
				DA9062_WATCHDOG_SD, DA9062_WATCHDOG_SD);

	return ret;
}

static struct da906x_device_data const	da9062_device_data = {
	.init	= da9062_device_init,
};

static int da9063_probe(struct device_d *dev)
{
	struct da9063 *priv = NULL;
	struct da906x_device_data const *dev_data;
	void const *dev_data_tmp;
	int ret;

	ret = dev_get_drvdata(dev, &dev_data_tmp);
	dev_data = ret < 0 ? NULL : dev_data_tmp;

	priv = xzalloc(sizeof(struct da9063));
	priv->wd.priority = of_get_watchdog_priority(dev->device_node);
	priv->wd.set_timeout = da9063_watchdog_set_timeout;
	priv->wd.hwdev = dev;
	priv->timeout = DA9063_INITIAL_TIMEOUT;
	priv->client = to_i2c_client(dev);
	priv->dev = dev;

	if (dev_data && dev_data->init) {
		ret = dev_data->init(priv);
		if (ret < 0)
			goto on_error;
	}

	ret = watchdog_register(&priv->wd);
	if (ret)
		goto on_error;

	da9063_detect_reset_source(priv);

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

	restart_handler_register(&priv->restart);

	return 0;

on_error:
	if (priv)
		free(priv);
	return ret;
}

static struct platform_device_id da9063_id[] = {
	{ "da9063", (uintptr_t)(NULL) },
	{ "da9062", (uintptr_t)(&da9062_device_data) },
	{ }
};

static struct of_device_id const	da906x_dt_ids[] = {
	{
		.compatible	= "dlg,da9063",
		.data		= NULL,
	}, {
		.compatible	= "dlg,da9062",
		.data		= &da9062_device_data,
	}, {
	}
};

static struct driver_d da9063_driver = {
	.name = "da9063",
	.probe = da9063_probe,
	.id_table = da9063_id,
	.of_compatible  = DRV_OF_COMPAT(da906x_dt_ids),
};
device_i2c_driver(da9063_driver);