Home | History | Annotate | Line # | Download | only in dev
psoc.c revision 1.1
      1  1.1  macallan  /* $NetBSD: psoc.c,v 1.1 2019/11/01 17:51:56 macallan Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2018 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.1  macallan  * fan1 - 0x20 ( status ), 0x31 ( data)
     34  1.1  macallan  * fan2 - 0x26 ( status ), 0x45 ( data )
     35  1.1  macallan  * fan3 - 0x59
     36  1.1  macallan  * temperature sensors start at 6, two bytes each, first appears to be
     37  1.1  macallan  * the temperature in degrees Celsius
     38  1.1  macallan  */
     39  1.1  macallan 
     40  1.1  macallan #include <sys/cdefs.h>
     41  1.1  macallan __KERNEL_RCSID(0, "$NetBSD: psoc.c,v 1.1 2019/11/01 17:51:56 macallan Exp $");
     42  1.1  macallan 
     43  1.1  macallan #include <sys/param.h>
     44  1.1  macallan #include <sys/systm.h>
     45  1.1  macallan #include <sys/device.h>
     46  1.1  macallan #include <sys/conf.h>
     47  1.1  macallan #include <sys/bus.h>
     48  1.1  macallan #include <sys/time.h>
     49  1.1  macallan 
     50  1.1  macallan #include <dev/ofw/openfirm.h>
     51  1.1  macallan 
     52  1.1  macallan #include <dev/i2c/i2cvar.h>
     53  1.1  macallan 
     54  1.1  macallan #include <dev/sysmon/sysmonvar.h>
     55  1.1  macallan 
     56  1.1  macallan struct psoc_softc {
     57  1.1  macallan 	device_t	sc_dev;
     58  1.1  macallan 	i2c_tag_t	sc_i2c;
     59  1.1  macallan 	i2c_addr_t	sc_addr;
     60  1.1  macallan 	int		sc_node;
     61  1.1  macallan 
     62  1.1  macallan 	struct sysmon_envsys *sc_sme;
     63  1.1  macallan 	envsys_data_t	sc_sensors[7];
     64  1.1  macallan 	int		sc_nsensors;
     65  1.1  macallan 	uint8_t		sc_temp[16];
     66  1.1  macallan 	time_t		sc_last;
     67  1.1  macallan };
     68  1.1  macallan 
     69  1.1  macallan static int	psoc_match(device_t, cfdata_t, void *);
     70  1.1  macallan static void	psoc_attach(device_t, device_t, void *);
     71  1.1  macallan 
     72  1.1  macallan static void	psoc_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
     73  1.1  macallan 
     74  1.1  macallan CFATTACH_DECL_NEW(psoc, sizeof(struct psoc_softc),
     75  1.1  macallan     psoc_match, psoc_attach, NULL, NULL);
     76  1.1  macallan 
     77  1.1  macallan static const struct device_compatible_entry compat_data[] = {
     78  1.1  macallan 	{ "Psoc",		0 },
     79  1.1  macallan 	{ NULL,			0 }
     80  1.1  macallan };
     81  1.1  macallan 
     82  1.1  macallan static int
     83  1.1  macallan psoc_match(device_t parent, cfdata_t match, void *aux)
     84  1.1  macallan {
     85  1.1  macallan 	struct i2c_attach_args *ia = aux;
     86  1.1  macallan 	int match_result;
     87  1.1  macallan 
     88  1.1  macallan 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
     89  1.1  macallan 		return match_result;
     90  1.1  macallan 
     91  1.1  macallan 	return 0;
     92  1.1  macallan }
     93  1.1  macallan 
     94  1.1  macallan static void
     95  1.1  macallan psoc_attach(device_t parent, device_t self, void *aux)
     96  1.1  macallan {
     97  1.1  macallan 	struct psoc_softc *sc = device_private(self);
     98  1.1  macallan 	struct i2c_attach_args *ia = aux;
     99  1.1  macallan 	char path[256];
    100  1.1  macallan 	envsys_data_t *s;
    101  1.1  macallan 	int error, ih, r;
    102  1.1  macallan 
    103  1.1  macallan 	sc->sc_dev = self;
    104  1.1  macallan 	sc->sc_i2c = ia->ia_tag;
    105  1.1  macallan 	sc->sc_addr = ia->ia_addr;
    106  1.1  macallan 	sc->sc_node = ia->ia_cookie;
    107  1.1  macallan 	sc->sc_last = 0;
    108  1.1  macallan 
    109  1.1  macallan 	aprint_naive("\n");
    110  1.1  macallan 	aprint_normal(": Psoc fan controller\n");
    111  1.1  macallan 
    112  1.1  macallan 	error = OF_package_to_path(sc->sc_node, path, 256);
    113  1.1  macallan 	path[error] = 0;
    114  1.1  macallan 	printf("path [%s]\n", path);
    115  1.1  macallan 	ih = OF_open("fan");
    116  1.1  macallan 	OF_call_method_1("fan-init", ih, 0);
    117  1.1  macallan 	printf("ih %08x\n", ih);
    118  1.1  macallan 
    119  1.1  macallan 	sc->sc_sme = sysmon_envsys_create();
    120  1.1  macallan 	sc->sc_sme->sme_name = device_xname(self);
    121  1.1  macallan 	sc->sc_sme->sme_cookie = sc;
    122  1.1  macallan 	sc->sc_sme->sme_refresh = psoc_sensors_refresh;
    123  1.1  macallan 	sc->sc_nsensors = 0;
    124  1.1  macallan 
    125  1.1  macallan 	int i, j;
    126  1.1  macallan 	uint8_t data, cmd;
    127  1.1  macallan 	for (i = 0; i < 0x7f; i+= 8) {
    128  1.1  macallan 		printf("%02x:", i);
    129  1.1  macallan 		for (j = 0; j < 8; j++) {
    130  1.1  macallan 			cmd = i + j;
    131  1.1  macallan 			data = 0;
    132  1.1  macallan 			iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    133  1.1  macallan 			    sc->sc_addr, &cmd, 1, &data, 1, 0);
    134  1.1  macallan 			printf(" %02x", data);
    135  1.1  macallan 		}
    136  1.1  macallan 		printf("\n");
    137  1.1  macallan 	}
    138  1.1  macallan 	for (i = 0; i < 4; i++) {
    139  1.1  macallan 		r = i * 2 + 6;
    140  1.1  macallan 		s = &sc->sc_sensors[sc->sc_nsensors];
    141  1.1  macallan 		s->state = ENVSYS_SINVALID;
    142  1.1  macallan 		s->units = ENVSYS_STEMP;
    143  1.1  macallan 		snprintf(s->desc, 16, "temp%d", i);
    144  1.1  macallan 		s->private = r;
    145  1.1  macallan 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
    146  1.1  macallan 		sc->sc_nsensors++;
    147  1.1  macallan 	}
    148  1.1  macallan #if 0
    149  1.1  macallan 	for (r = 0x31; r < 0x50; r += 0x14) {
    150  1.1  macallan 		s = &sc->sc_sensors[sc->sc_nsensors];
    151  1.1  macallan 		s->state = ENVSYS_SINVALID;
    152  1.1  macallan 		s->units = ENVSYS_SFANRPM;
    153  1.1  macallan 		snprintf(s->desc, 16, "reg %02x", r);
    154  1.1  macallan 		s->private = r;
    155  1.1  macallan 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
    156  1.1  macallan 		sc->sc_nsensors++;
    157  1.1  macallan 	}
    158  1.1  macallan #endif
    159  1.1  macallan 	sysmon_envsys_register(sc->sc_sme);
    160  1.1  macallan }
    161  1.1  macallan 
    162  1.1  macallan static void
    163  1.1  macallan psoc_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    164  1.1  macallan {
    165  1.1  macallan 	struct psoc_softc *sc = sme->sme_cookie;
    166  1.1  macallan 	uint8_t cmd = 6;
    167  1.1  macallan 	uint8_t buf[0x28];
    168  1.1  macallan 	int error = 1, data, i;
    169  1.1  macallan 
    170  1.1  macallan 	if ( edata->private < 0x20) {
    171  1.1  macallan 		cmd = 0;
    172  1.1  macallan 		if ((time_second - sc->sc_last) > 2) {
    173  1.1  macallan 			iic_acquire_bus(sc->sc_i2c, 0);
    174  1.1  macallan 			error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    175  1.1  macallan 				    sc->sc_addr, &cmd, 1, sc->sc_temp, 16, 0);
    176  1.1  macallan 			iic_release_bus(sc->sc_i2c, 0);
    177  1.1  macallan 			if (error) return;
    178  1.1  macallan 			sc->sc_last = time_second;
    179  1.1  macallan 		}
    180  1.1  macallan 		error = 0;
    181  1.1  macallan 		if (edata->private > 0) {
    182  1.1  macallan 			data = sc->sc_temp[edata->private];
    183  1.1  macallan 			/* Celsius -> microkelvin */
    184  1.1  macallan 			edata->value_cur = ((int)data * 1000000) + 273150000;
    185  1.1  macallan 		}
    186  1.1  macallan 	} else {
    187  1.1  macallan 		cmd = 0x31;
    188  1.1  macallan 		iic_acquire_bus(sc->sc_i2c, 0);
    189  1.1  macallan 		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
    190  1.1  macallan 			    sc->sc_addr, &cmd, 1, buf, 0x28, 0);
    191  1.1  macallan 		iic_release_bus(sc->sc_i2c, 0);
    192  1.1  macallan 		if (error) return;
    193  1.1  macallan 		if (edata->private > 0) {
    194  1.1  macallan 			data = buf[edata->private - 0x20];
    195  1.1  macallan 			edata->value_cur = data;
    196  1.1  macallan 			for (i = 0; i < 14; i++)
    197  1.1  macallan 				printf(" %02x", buf[edata->private - 0x31 + i]);
    198  1.1  macallan 			printf("\n");
    199  1.1  macallan 		}
    200  1.1  macallan 	}
    201  1.1  macallan 	if (error) {
    202  1.1  macallan 		edata->state = ENVSYS_SINVALID;
    203  1.1  macallan 	} else {
    204  1.1  macallan 		edata->state = ENVSYS_SVALID;
    205  1.1  macallan 	}
    206  1.1  macallan }
    207