r4724 - in developers/werner: . pmu
werner at docs.openmoko.org
werner at docs.openmoko.org
Sun Oct 19 03:58:02 CEST 2008
Author: werner
Date: 2008-10-19 03:58:00 +0200 (Sun, 19 Oct 2008)
New Revision: 4724
Added:
developers/werner/pmu/
developers/werner/pmu/Makefile
developers/werner/pmu/main.c
developers/werner/pmu/pmu.c
developers/werner/pmu/pmu.h
Log:
Utility to directly read/write PMU registers.
Added: developers/werner/pmu/Makefile
===================================================================
--- developers/werner/pmu/Makefile (rev 0)
+++ developers/werner/pmu/Makefile 2008-10-19 01:58:00 UTC (rev 4724)
@@ -0,0 +1,25 @@
+CC=arm-angstrom-linux-gnueabi-gcc
+
+CFLAGS=-Wall -Wshadow -g -O
+
+OBJS=main.o pmu.o
+
+.PHONY: all clean dep depend spotless
+
+all: pmu
+
+pmu: $(OBJS)
+
+dep depend:
+ $(CPP) $(CFLAGS) -MM -MG *.c >.depend || \
+ { rm -f .depend; exit 1; }
+
+ifeq (.depend,$(wildcard .depend))
+include .depend
+endif
+
+clean:
+ rm -f $(OBJS) .depend
+
+spotless: clean
+ rm -f pmu
Added: developers/werner/pmu/main.c
===================================================================
--- developers/werner/pmu/main.c (rev 0)
+++ developers/werner/pmu/main.c 2008-10-19 01:58:00 UTC (rev 4724)
@@ -0,0 +1,63 @@
+/*
+ * main.c - PMU command line processing
+ *
+ * Copyright (C) 2008 by OpenMoko, Inc.
+ * Written by Werner Almesberger <werner at openmoko.org>
+ * All Rights Reserved
+ *
+ * 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.
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "pmu.h"
+
+
+static void usage(const char *name)
+{
+ fprintf(stderr,
+ "usage: %s reg[=value] ...\n\n"
+ " Register address and values are in hex.\n", name);
+ exit(1);
+}
+
+
+static void do_arg(int pmu, const char *name, const char *arg)
+{
+ unsigned long reg, value;
+ char *end;
+
+ reg = strtoul(arg, &end, 16);
+ if (reg & ~0xffUL)
+ usage(name);
+ if (!*end) {
+ printf("0x%02x\n", pmu_read(pmu, reg));
+ return;
+ }
+ if (*end != '=')
+ usage(name);
+ value = strtoul(end+1, &end, 16);
+ if (*end)
+ usage(name);
+ if (value & ~0xffUL)
+ usage(name);
+ pmu_write(pmu, reg, value);
+}
+
+
+int main(int argc, char **argv)
+{
+ int pmu, i;
+
+ if (argc == 1)
+ usage(*argv);
+ pmu = pmu_open();
+ for (i = 1; i != argc; i++)
+ do_arg(pmu, argv[0], argv[i]);
+ return 0;
+}
Added: developers/werner/pmu/pmu.c
===================================================================
--- developers/werner/pmu/pmu.c (rev 0)
+++ developers/werner/pmu/pmu.c 2008-10-19 01:58:00 UTC (rev 4724)
@@ -0,0 +1,78 @@
+/*
+ * pmu.c - Read/write PMU registers
+ *
+ * Copyright (C) 2008 by OpenMoko, Inc.
+ * Written by Werner Almesberger <werner at openmoko.org>
+ * All Rights Reserved
+ *
+ * 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.
+ */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+#include <linux/i2c.h>
+#include <linux/i2c-dev.h>
+
+#include "pmu.h"
+
+
+#define I2C_DEVICE "/dev/i2c-0"
+#define I2C_ADDRESS 0x73
+
+
+uint8_t pmu_read(int fd, uint8_t reg)
+{
+ uint8_t res;
+ struct i2c_smbus_ioctl_data msg = {
+ .read_write = I2C_SMBUS_READ,
+ .command = reg,
+ .size = I2C_SMBUS_BYTE_DATA,
+ .data = (void *) &res,
+ };
+
+ if (ioctl(fd, I2C_SMBUS, (void *) &msg) < 0) {
+ perror("ioctl(I2C_SMBUS)");
+ exit(1);
+ }
+ return res;
+}
+
+
+void pmu_write(int fd, uint8_t reg, uint8_t value)
+{
+ struct i2c_smbus_ioctl_data msg = {
+ .read_write = I2C_SMBUS_WRITE,
+ .command = reg,
+ .size = I2C_SMBUS_BYTE_DATA,
+ .data = (void *) &value,
+ };
+
+ if (ioctl(fd, I2C_SMBUS, (void *) &msg) < 0) {
+ perror("ioctl(I2C_SMBUS)");
+ exit(1);
+ }
+}
+
+
+int pmu_open(void)
+{
+ int fd;
+
+ fd = open(I2C_DEVICE, O_RDWR);
+ if (fd < 0) {
+ perror(I2C_DEVICE);
+ exit(1);
+ }
+ if (ioctl(fd, I2C_SLAVE_FORCE, I2C_ADDRESS) < 0) {
+ perror("ioctl(I2C_SLAVE_FORCE)");
+ exit(1);
+ }
+ return fd;
+}
Added: developers/werner/pmu/pmu.h
===================================================================
--- developers/werner/pmu/pmu.h (rev 0)
+++ developers/werner/pmu/pmu.h 2008-10-19 01:58:00 UTC (rev 4724)
@@ -0,0 +1,24 @@
+/*
+ * pmu.h - Read/write PMU registers
+ *
+ * Copyright (C) 2008 by OpenMoko, Inc.
+ * Written by Werner Almesberger <werner at openmoko.org>
+ * All Rights Reserved
+ *
+ * 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.
+ */
+
+#ifndef PMU_H
+#define PMU_H
+
+#include <stdint.h>
+
+
+uint8_t pmu_read(int fd, uint8_t reg);
+void pmu_write(int fd, uint8_t reg, uint8_t value);
+int pmu_open(void);
+
+#endif /* !PMU_H */
More information about the commitlog
mailing list