summaryrefslogtreecommitdiff
path: root/minix/drivers/power/tps65950/tps65950.c
blob: 29358f7eb03a7ba36651e8d37fc7c225ba8b1758 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <minix/ds.h>
#include <minix/drivers.h>
#include <minix/i2c.h>
#include <minix/i2cdriver.h>
#include <minix/log.h>
#include <minix/safecopies.h>

#include "tps65950.h"
#include "rtc.h"

/* logging - use with log_warn(), log_info(), log_debug(), log_trace(), etc */
static struct log log = {
	.name = "tps65950",
	.log_level = LEVEL_INFO,
	.log_func = default_log
};

/* TPS65950 doesn't support configuring the addresses, so there is only 1
 * configuration possible. The chip does have multiple addresses (0x48,
 * 0x49, 0x4a, 0x4b), but because they're all fixed, we only have the
 * user pass the base address as a sanity check.
 */
static i2c_addr_t valid_addrs[2] = {
	0x48, 0x00
};

/* the bus that this device is on (counting starting at 1) */
static uint32_t bus;

/* endpoint for the driver for the bus itself. */
endpoint_t bus_endpoint;

/* slave addresses of the device */
i2c_addr_t addresses[NADDRESSES] = {
	0x48, 0x49, 0x4a, 0x4b
};

/* local functions */
static int check_revision(void);

/* functions for transfering struct tm to/from this driver and calling proc. */
static int fetch_t(endpoint_t ep, cp_grant_id_t gid, struct tm *t);
static int store_t(endpoint_t ep, cp_grant_id_t gid, struct tm *t);

static int
fetch_t(endpoint_t ep, cp_grant_id_t gid, struct tm *t)
{
	int r;

	r = sys_safecopyfrom(ep, gid, (vir_bytes) 0, (vir_bytes) t,
	    sizeof(struct tm));
	if (r != OK) {
		log_warn(&log, "sys_safecopyfrom() failed (r=%d)\n", r);
		return r;
	}

	return OK;
}

static int
store_t(endpoint_t ep, cp_grant_id_t gid, struct tm *t)
{
	int r;

	r = sys_safecopyto(ep, gid, (vir_bytes) 0, (vir_bytes) t,
	    sizeof(struct tm));
	if (r != OK) {
		log_warn(&log, "sys_safecopyto() failed (r=%d)\n", r);
		return r;
	}

	return OK;
}

static int
check_revision(void)
{
	int r;
	uint32_t idcode;
	uint8_t idcode_7_0, idcode_15_8, idcode_23_16, idcode_31_24;

	/* need to write a special code to unlock read protect on IDCODE */
	r = i2creg_write8(bus_endpoint, addresses[ID2], UNLOCK_TEST_REG,
	    UNLOCK_TEST_CODE);
	if (r != OK) {
		log_warn(&log, "Failed to write unlock code to UNLOCK_TEST\n");
		return -1;
	}

	/*
	 * read each part of the IDCODE
	 */
	r = i2creg_read8(bus_endpoint, addresses[ID2], IDCODE_7_0_REG,
	    &idcode_7_0);
	if (r != OK) {
		log_warn(&log, "Failed to read IDCODE part 1\n");
	}

	r = i2creg_read8(bus_endpoint, addresses[ID2], IDCODE_15_8_REG,
	    &idcode_15_8);
	if (r != OK) {
		log_warn(&log, "Failed to read IDCODE part 2\n");
	}

	r = i2creg_read8(bus_endpoint, addresses[ID2], IDCODE_23_16_REG,
	    &idcode_23_16);
	if (r != OK) {
		log_warn(&log, "Failed to read IDCODE part 3\n");
	}

	r = i2creg_read8(bus_endpoint, addresses[ID2], IDCODE_31_24_REG,
	    &idcode_31_24);
	if (r != OK) {
		log_warn(&log, "Failed to read IDCODE part 4\n");
	}

	/* combine the parts to get the full IDCODE */
	idcode =
	    ((idcode_31_24 << 24) | (idcode_23_16 << 16) | (idcode_15_8 << 8) |
	    (idcode_7_0 << 0));

	log_debug(&log, "IDCODE = 0x%x\n", idcode);
	switch (idcode) {
	case IDCODE_REV_1_0:
		log_debug(&log, "TPS65950 rev 1.0\n");
		break;
	case IDCODE_REV_1_1:
		log_debug(&log, "TPS65950 rev 1.1\n");
		break;
	case IDCODE_REV_1_2:
		log_debug(&log, "TPS65950 rev 1.2\n");
		break;
	case 0:
		log_debug(&log, "TPS65950 missing in qemu\n");
		break;
	default:
		log_warn(&log, "Unexpected IDCODE: 0x%x\n", idcode);
		return -1;
	}

	return OK;
}

static int
sef_cb_lu_state_save(int UNUSED(result), int UNUSED(flags))
{
	/* The addresses are fixed/non-configurable so bus is the only state */
	ds_publish_u32("bus", bus, DSF_OVERWRITE);
	return OK;
}

static int
lu_state_restore(void)
{
	/* Restore the state. */
	u32_t value;

	ds_retrieve_u32("bus", &value);
	ds_delete_u32("bus");
	bus = (int) value;

	return OK;
}

static int
sef_cb_init(int type, sef_init_info_t * UNUSED(info))
{
	int r, i;

	if (type == SEF_INIT_LU) {
		/* Restore the state. */
		lu_state_restore();
	}

	/* look-up the endpoint for the bus driver */
	bus_endpoint = i2cdriver_bus_endpoint(bus);
	if (bus_endpoint == 0) {
		log_warn(&log, "Couldn't find bus driver.\n");
		return EXIT_FAILURE;
	}

	for (i = 0; i < NADDRESSES; i++) {

		/* claim the device */
		r = i2cdriver_reserve_device(bus_endpoint, addresses[i]);
		if (r != OK) {
			log_warn(&log, "Couldn't reserve device 0x%x (r=%d)\n",
			    addresses[i], r);
			return EXIT_FAILURE;
		}
	}

	/* check that the chip / rev is reasonable */
	r = check_revision();
	if (r != OK) {
		/* prevent user from using the driver with a different chip */
		log_warn(&log, "Bad IDCODE\n");
		return EXIT_FAILURE;
	}

	r = rtc_init();
	if (r != OK) {
		log_warn(&log, "RTC Start-up Failed\n");
		return EXIT_FAILURE;
	}

	if (type != SEF_INIT_LU) {

		/* sign up for updates about the i2c bus going down/up */
		r = i2cdriver_subscribe_bus_updates(bus);
		if (r != OK) {
			log_warn(&log, "Couldn't subscribe to bus updates\n");
			return EXIT_FAILURE;
		}

		i2cdriver_announce(bus);
		log_debug(&log, "announced\n");
	}

	return OK;
}

static void
sef_local_startup(void)
{
	/*
	 * Register init callbacks. Use the same function for all event types
	 */
	sef_setcb_init_fresh(sef_cb_init);
	sef_setcb_init_lu(sef_cb_init);
	sef_setcb_init_restart(sef_cb_init);

	/*
	 * Register live update callbacks.
	 */
	sef_setcb_lu_state_save(sef_cb_lu_state_save);

	/* Let SEF perform startup. */
	sef_startup();
}

int
main(int argc, char *argv[])
{
	int r, i;
	struct tm t;
	endpoint_t user, caller;
	message m;
	int ipc_status, reply_status;

	env_setargs(argc, argv);

	r = i2cdriver_env_parse(&bus, &addresses[0], valid_addrs);
	if (r < 0) {
		log_warn(&log, "Expecting -args 'bus=X address=0xYY'\n");
		log_warn(&log, "Example -args 'bus=1 address=0x48'\n");
		return EXIT_FAILURE;
	} else if (r > 0) {
		log_warn(&log,
		    "Invalid slave address for device, expecting 0x48\n");
		return EXIT_FAILURE;
	}

	sef_local_startup();

	while (TRUE) {

		/* Receive Message */
		r = sef_receive_status(ANY, &m, &ipc_status);
		if (r != OK) {
			log_warn(&log, "sef_receive_status() failed\n");
			continue;
		}

		if (is_ipc_notify(ipc_status)) {

			if (m.m_source == DS_PROC_NR) {
				for (i = 0; i < NADDRESSES; i++) {
					/* changed state, update endpoint */
					i2cdriver_handle_bus_update
					    (&bus_endpoint, bus, addresses[i]);
				}
			}

			/* Do not reply to notifications. */
			continue;
		}

		caller = m.m_source;

		log_debug(&log, "Got message 0x%x from 0x%x\n", m.m_type,
		    caller);

		switch (m.m_type) {
		case RTCDEV_GET_TIME_G:
			/* Any user can read the time */
			reply_status = rtc_get_time(&t, m.m_lc_readclock_rtcdev.flags);
			if (reply_status != OK) {
				break;
			}

			/* write results back to calling process */
			reply_status =
			    store_t(caller, m.m_lc_readclock_rtcdev.grant, &t);
			break;

		case RTCDEV_SET_TIME_G:
			/* Only super user is allowed to set the time */
			if (getnuid(caller) == SUPER_USER) {
				/* read time from calling process */
				reply_status =
				    fetch_t(caller,
					    m.m_lc_readclock_rtcdev.grant, &t);
				if (reply_status != OK) {
					break;
				}

				reply_status =
				    rtc_set_time(&t,
					    m.m_lc_readclock_rtcdev.flags);
			} else {
				reply_status = EPERM;
			}
			break;

		case RTCDEV_PWR_OFF:
			reply_status = ENOSYS;
			break;

		default:
			/* Unrecognized call */
			reply_status = EINVAL;
			break;
		}

		/* Send Reply */
		m.m_type = RTCDEV_REPLY;
		m.m_readclock_lc_rtcdev.status = reply_status;

		log_debug(&log, "Sending Reply");

		r = ipc_sendnb(caller, &m);
		if (r != OK) {
			log_warn(&log, "ipc_sendnb() failed\n");
			continue;
		}
	}

	rtc_exit();

	return 0;
}