1/*	$NetBSD: dbcool_ki2c.c,v 1.7 2010/12/20 00:25:37 matt Exp $ */
2
3/*-
4 * Copyright (C) 2005 Michael Lorenz
5 * Copyright (C) 2008 Paul Goyette
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * a driver for the dbCool family of environmental controllers found in the
30 * iBook G4 and probably other Apple machines
31 */
32
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: dbcool_ki2c.c,v 1.7 2010/12/20 00:25:37 matt Exp $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/device.h>
40#include <sys/malloc.h>
41#include <sys/sysctl.h>
42
43#include <dev/ofw/openfirm.h>
44#include <macppc/dev/ki2cvar.h>
45
46#include <dev/i2c/dbcool_var.h>
47#include <dev/i2c/dbcool_reg.h>
48
49static void dbcool_ki2c_attach(device_t, device_t, void *);
50static int dbcool_ki2c_match(device_t, cfdata_t, void *);
51static uint8_t dbcool_ki2c_readreg(struct dbcool_chipset *, uint8_t);
52static void dbcool_ki2c_writereg(struct dbcool_chipset *, uint8_t, uint8_t);
53
54CFATTACH_DECL_NEW(dbcool_ki2c, sizeof(struct dbcool_softc),
55    dbcool_ki2c_match, dbcool_ki2c_attach, NULL, NULL);
56
57int
58dbcool_ki2c_match(device_t parent, cfdata_t cf, void *aux)
59{
60	struct ki2c_confargs *ka = aux;
61	char compat[32];
62
63	if (strcmp(ka->ka_name, "fan") != 0)
64		return 0;
65
66	memset(compat, 0, sizeof(compat));
67	OF_getprop(ka->ka_node, "compatible", compat, sizeof(compat));
68	if (strcmp(compat, "adt7467") != 0 && strcmp(compat, "adt7460") != 0 &&
69	    strcmp(compat, "adm1030") != 0)
70		return 0;
71
72	return 1;
73}
74
75void
76dbcool_ki2c_attach(device_t parent, device_t self, void *aux)
77{
78	struct dbcool_softc *sc = device_private(self);
79	struct ki2c_confargs *ka = aux;
80	uint8_t ver;
81
82	aprint_normal("\n");
83	aprint_naive("\n");
84
85	sc->sc_dc.dc_tag = ka->ka_tag;
86	sc->sc_dc.dc_addr = ka->ka_addr & 0xfe;
87	sc->sc_dc.dc_readreg = dbcool_ki2c_readreg;
88	sc->sc_dc.dc_writereg = dbcool_ki2c_writereg;
89
90	if (dbcool_chip_ident(&sc->sc_dc) < 0) {
91		aprint_error_dev(self, "Unrecognized dbCool chip - "
92					"set-up aborted\n");
93		return;
94	}
95
96	ver = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_REVISION_REG);
97
98	if (sc->sc_dc.dc_chip->flags & DBCFLAG_4BIT_VER)
99		aprint_normal_dev(self, "%s dBCool(tm) Controller "
100			"(rev 0x%02x, stepping 0x%02x)\n", sc->sc_dc.dc_chip->name,
101			ver >> 4, ver & 0x0f);
102	else
103		aprint_normal_dev(self, "%s dBCool(tm) Controller "
104			"(rev 0x%04x)\n", sc->sc_dc.dc_chip->name, ver);
105
106	dbcool_setup(self);
107
108	if (!pmf_device_register(self, dbcool_pmf_suspend, dbcool_pmf_resume))
109		aprint_error_dev(self, "couldn't establish power handler\n");
110}
111
112static uint8_t
113dbcool_ki2c_readreg(struct dbcool_chipset *dc, uint8_t reg)
114{
115	uint8_t data = 0;
116
117	iic_acquire_bus(dc->dc_tag, 0);
118	iic_exec(dc->dc_tag, I2C_OP_READ, dc->dc_addr, &reg, 1, &data, 1, 0);
119	iic_release_bus(dc->dc_tag, 0);
120	return data;
121}
122
123static void
124dbcool_ki2c_writereg(struct dbcool_chipset *dc, uint8_t reg, uint8_t data)
125{
126	uint8_t mdata[2] = {reg, data};
127
128	iic_acquire_bus(dc->dc_tag, 0);
129	iic_exec(dc->dc_tag, I2C_OP_WRITE, dc->dc_addr, &mdata, 2, NULL, 0, 0);
130	iic_release_bus(dc->dc_tag, 0);
131}
132