Home | History | Annotate | Line # | Download | only in dev
psoc.c revision 1.6.4.1
      1  1.6.4.1   thorpej  /* $NetBSD: psoc.c,v 1.6.4.1 2021/05/09 22:36:35 thorpej Exp $ */
      2      1.1  macallan 
      3      1.1  macallan /*-
      4      1.3  macallan  * Copyright (c) 2019 Michael Lorenz
      5      1.1  macallan  * All rights reserved.
      6      1.1  macallan  *
      7      1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8      1.1  macallan  * modification, are permitted provided that the following conditions
      9      1.1  macallan  * are met:
     10      1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11      1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12      1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15      1.1  macallan  *
     16      1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  macallan  */
     28      1.1  macallan 
     29      1.1  macallan /*
     30      1.1  macallan  * fan controller found in 1GHz TiBook
     31      1.1  macallan  *
     32      1.1  macallan  * register values from OF:
     33      1.2  macallan  * fan1 - 0x20 ( status ), 0x31 ( data )
     34      1.1  macallan  * fan2 - 0x26 ( status ), 0x45 ( data )
     35      1.2  macallan  * fan status byte 0:
     36      1.2  macallan  * 0x5* - fan is running, 0x6* - fan stopped
     37      1.2  macallan  * byte 1: unknown, 0x80 seems always set, lower bits seem to fluctuate
     38      1.2  macallan  * byte 2: lower 6 bit seem to indicate speed
     39      1.2  macallan  * fan speed may be lower 6 bit of byte 2 and lower 6 of byte 1
     40      1.1  macallan  * temperature sensors start at 6, two bytes each, first appears to be
     41      1.1  macallan  * the temperature in degrees Celsius
     42      1.1  macallan  */
     43      1.1  macallan 
     44      1.1  macallan #include <sys/cdefs.h>
     45  1.6.4.1   thorpej __KERNEL_RCSID(0, "$NetBSD: psoc.c,v 1.6.4.1 2021/05/09 22:36:35 thorpej Exp $");
     46      1.1  macallan 
     47      1.1  macallan #include <sys/param.h>
     48      1.1  macallan #include <sys/systm.h>
     49      1.1  macallan #include <sys/device.h>
     50      1.1  macallan #include <sys/conf.h>
     51      1.1  macallan #include <sys/bus.h>
     52      1.1  macallan #include <sys/time.h>
     53      1.1  macallan 
     54      1.1  macallan #include <dev/ofw/openfirm.h>
     55      1.1  macallan 
     56      1.1  macallan #include <dev/i2c/i2cvar.h>
     57      1.1  macallan 
     58      1.1  macallan #include <dev/sysmon/sysmonvar.h>
     59      1.1  macallan 
     60      1.1  macallan struct psoc_softc {
     61      1.1  macallan 	device_t	sc_dev;
     62      1.1  macallan 	i2c_tag_t	sc_i2c;
     63      1.1  macallan 	i2c_addr_t	sc_addr;
     64      1.1  macallan 	int		sc_node;
     65      1.1  macallan 
     66      1.1  macallan 	struct sysmon_envsys *sc_sme;
     67      1.1  macallan 	envsys_data_t	sc_sensors[7];
     68      1.1  macallan 	int		sc_nsensors;
     69      1.1  macallan 	uint8_t		sc_temp[16];
     70      1.1  macallan 	time_t		sc_last;
     71      1.1  macallan };
     72      1.1  macallan 
     73      1.1  macallan static int	psoc_match(device_t, cfdata_t, void *);
     74      1.1  macallan static void	psoc_attach(device_t, device_t, void *);
     75      1.1  macallan 
     76      1.1  macallan static void	psoc_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
     77      1.1  macallan 
     78      1.2  macallan static void	psoc_dump(struct psoc_softc *);
     79      1.2  macallan 
     80      1.1  macallan CFATTACH_DECL_NEW(psoc, sizeof(struct psoc_softc),
     81      1.1  macallan     psoc_match, psoc_attach, NULL, NULL);
     82      1.1  macallan 
     83      1.1  macallan static const struct device_compatible_entry compat_data[] = {
     84      1.4   thorpej 	{ .compat = "Psoc" },
     85      1.6   thorpej 	DEVICE_COMPAT_EOL
     86      1.1  macallan };
     87      1.1  macallan 
     88      1.1  macallan static int
     89      1.1  macallan psoc_match(device_t parent, cfdata_t match, void *aux)
     90      1.1  macallan {
     91      1.1  macallan 	struct i2c_attach_args *ia = aux;
     92      1.1  macallan 	int match_result;
     93      1.1  macallan 
     94      1.1  macallan 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
     95      1.1  macallan 		return match_result;
     96      1.1  macallan 
     97      1.1  macallan 	return 0;
     98      1.1  macallan }
     99      1.1  macallan 
    100      1.1  macallan static void
    101      1.1  macallan psoc_attach(device_t parent, device_t self, void *aux)
    102      1.1  macallan {
    103      1.1  macallan 	struct psoc_softc *sc = device_private(self);
    104      1.1  macallan 	struct i2c_attach_args *ia = aux;
    105      1.1  macallan 	char path[256];
    106      1.1  macallan 	envsys_data_t *s;
    107      1.2  macallan 	int error, ih, r, i;
    108      1.1  macallan 
    109      1.1  macallan 	sc->sc_dev = self;
    110      1.1  macallan 	sc->sc_i2c = ia->ia_tag;
    111      1.1  macallan 	sc->sc_addr = ia->ia_addr;
    112  1.6.4.1   thorpej 	sc->sc_node = devhandle_to_of(device_handle(self));
    113      1.1  macallan 	sc->sc_last = 0;
    114      1.1  macallan 
    115      1.1  macallan 	aprint_naive("\n");
    116      1.1  macallan 	aprint_normal(": Psoc fan controller\n");
    117      1.1  macallan 
    118      1.1  macallan 	error = OF_package_to_path(sc->sc_node, path, 256);
    119      1.1  macallan 	path[error] = 0;
    120      1.1  macallan 	printf("path [%s]\n", path);
    121      1.1  macallan 	ih = OF_open("fan");
    122      1.1  macallan 	OF_call_method_1("fan-init", ih, 0);
    123      1.1  macallan 	printf("ih %08x\n", ih);
    124      1.1  macallan 
    125      1.1  macallan 	sc->sc_sme = sysmon_envsys_create();
    126      1.1  macallan 	sc->sc_sme->sme_name = device_xname(self);
    127      1.1  macallan 	sc->sc_sme->sme_cookie = sc;
    128      1.1  macallan 	sc->sc_sme->sme_refresh = psoc_sensors_refresh;
    129      1.1  macallan 	sc->sc_nsensors = 0;
    130      1.1  macallan 
    131      1.2  macallan 	psoc_dump(sc);
    132      1.2  macallan 
    133      1.1  macallan 	for (i = 0; i < 4; i++) {
    134      1.1  macallan 		r = i * 2 + 6;
    135      1.1  macallan 		s = &sc->sc_sensors[sc->sc_nsensors];
    136      1.1  macallan 		s->state = ENVSYS_SINVALID;
    137      1.1  macallan 		s->units = ENVSYS_STEMP;
    138      1.1  macallan 		snprintf(s->desc, 16, "temp%d", i);
    139      1.1  macallan 		s->private = r;
    140      1.1  macallan 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
    141      1.1  macallan 		sc->sc_nsensors++;
    142      1.1  macallan 	}
    143      1.2  macallan 
    144      1.2  macallan 	for (r = 0x20; r < 0x2b; r += 0x06) {
    145      1.1  macallan 		s = &sc->sc_sensors[sc->sc_nsensors];
    146      1.1  macallan 		s->state = ENVSYS_SINVALID;
    147      1.1  macallan 		s->units = ENVSYS_SFANRPM;
    148      1.1  macallan 		snprintf(s->desc, 16, "reg %02x", r);
    149      1.1  macallan 		s->private = r;
    150      1.1  macallan 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
    151      1.1  macallan 		sc->sc_nsensors++;
    152      1.1  macallan 	}
    153      1.2  macallan 
    154      1.1  macallan 	sysmon_envsys_register(sc->sc_sme);
    155      1.1  macallan }
    156      1.1  macallan 
    157      1.1  macallan static void
    158      1.1  macallan psoc_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    159      1.1  macallan {
    160      1.1  macallan 	struct psoc_softc *sc = sme->sme_cookie;
    161      1.1  macallan 	uint8_t cmd = 6;
    162      1.3  macallan 	uint8_t buf[0x28];
    163      1.2  macallan 	int error = 1, data;
    164      1.1  macallan 
    165      1.1  macallan 	if ( edata->private < 0x20) {
    166      1.1  macallan 		cmd = 0;
    167      1.1  macallan 		if ((time_second - sc->sc_last) > 2) {
    168      1.1  macallan 			iic_acquire_bus(sc->sc_i2c, 0);
    169      1.1  macallan 			error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    170      1.1  macallan 				    sc->sc_addr, &cmd, 1, sc->sc_temp, 16, 0);
    171      1.1  macallan 			iic_release_bus(sc->sc_i2c, 0);
    172      1.1  macallan 			if (error) return;
    173      1.1  macallan 			sc->sc_last = time_second;
    174      1.1  macallan 		}
    175      1.1  macallan 		error = 0;
    176      1.1  macallan 		if (edata->private > 0) {
    177      1.1  macallan 			data = sc->sc_temp[edata->private];
    178      1.1  macallan 			/* Celsius -> microkelvin */
    179      1.1  macallan 			edata->value_cur = ((int)data * 1000000) + 273150000;
    180      1.1  macallan 		}
    181      1.2  macallan #ifdef PSOC_DEBUG
    182      1.2  macallan 		if (edata->private == 6)
    183      1.2  macallan 			psoc_dump(sc);
    184      1.2  macallan #endif
    185      1.1  macallan 	} else {
    186      1.3  macallan 		cmd = edata->private;
    187      1.1  macallan 		iic_acquire_bus(sc->sc_i2c, 0);
    188      1.1  macallan 		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    189      1.3  macallan 			    sc->sc_addr, &cmd, 1, buf, 3, 0);
    190      1.1  macallan 		iic_release_bus(sc->sc_i2c, 0);
    191      1.1  macallan 		if (error) return;
    192      1.3  macallan 		switch (buf[0] & 0xf0) {
    193      1.3  macallan 			case 0x50:
    194      1.3  macallan 				data = buf[edata->private - 0x20];
    195      1.3  macallan 				edata->value_cur = ((buf[2] & 0x3f) << 6) |
    196      1.3  macallan 						    (buf[1] & 0x3f);
    197      1.3  macallan 				break;
    198      1.3  macallan 			case 0x60:
    199      1.3  macallan 				edata->value_cur = 0;
    200      1.3  macallan 				break;
    201      1.3  macallan 			default:
    202      1.3  macallan 				error = 0;
    203      1.3  macallan 		}
    204      1.1  macallan 	}
    205      1.1  macallan 	if (error) {
    206      1.1  macallan 		edata->state = ENVSYS_SINVALID;
    207      1.1  macallan 	} else {
    208      1.1  macallan 		edata->state = ENVSYS_SVALID;
    209      1.1  macallan 	}
    210      1.1  macallan }
    211      1.2  macallan 
    212      1.2  macallan static void
    213      1.2  macallan psoc_dump(struct psoc_softc *sc)
    214      1.2  macallan {
    215      1.2  macallan 	int i, j;
    216      1.2  macallan 	uint8_t data, cmd;
    217      1.2  macallan 	for (i = 0x20; i < 0x5f; i+= 8) {
    218      1.2  macallan 		printf("%02x:", i);
    219      1.2  macallan 		for (j = 0; j < 8; j++) {
    220      1.2  macallan 			cmd = i + j;
    221      1.2  macallan 			data = 0;
    222      1.2  macallan 			iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    223      1.2  macallan 			    sc->sc_addr, &cmd, 1, &data, 1, 0);
    224      1.2  macallan 			printf(" %02x", data);
    225      1.2  macallan 		}
    226      1.2  macallan 		printf("\n");
    227      1.2  macallan 	}
    228      1.2  macallan }
    229