xref: /freebsd/sys/dev/chromebook_platform/chromebook_platform.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /*-
2  * Copyright (c) 2016 The FreeBSD Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/errno.h>
36 #include <sys/bus.h>
37 
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
40 #include <dev/iicbus/iicbus.h>
41 #include <dev/iicbus/iiconf.h>
42 
43 /*
44  * Driver that attaches I2C devices.
45  */
46 static struct {
47 	uint32_t	pci_id;
48 	const char	*name;
49 	uint8_t		addr;
50 } slaves[] = {
51 	{ 0x9c628086,	"isl",		0x88 },
52 	{ 0x9c618086,	"cyapa",	0xce },
53 };
54 
55 static void
56 chromebook_i2c_identify(driver_t *driver, device_t bus)
57 {
58 	device_t controller;
59 	device_t child;
60 	int i;
61 
62 	/*
63 	 * A stopgap approach to preserve the status quo.
64 	 * A more intelligent approach is required to correctly
65 	 * identify a machine model and hardware available on it.
66 	 * For instance, DMI could be used.
67 	 * See http://lxr.free-electrons.com/source/drivers/platform/chrome/chromeos_laptop.c
68 	 */
69 	controller = device_get_parent(bus);
70 	if (strcmp(device_get_name(controller), "ig4iic") != 0)
71 		return;
72 
73 	for (i = 0; i < nitems(slaves); i++) {
74 		if (device_find_child(bus, slaves[i].name, -1) != NULL)
75 			continue;
76 		if (slaves[i].pci_id != pci_get_devid(controller))
77 			continue;
78 		child = BUS_ADD_CHILD(bus, 0, slaves[i].name, -1);
79 		if (child != NULL)
80 			iicbus_set_addr(child, slaves[i].addr);
81 	}
82 }
83 
84 static device_method_t chromebook_i2c_methods[] = {
85 	DEVMETHOD(device_identify,	chromebook_i2c_identify),
86 	{ 0, 0 }
87 };
88 
89 static driver_t chromebook_i2c_driver = {
90 	"chromebook_i2c",
91 	chromebook_i2c_methods,
92 	0	/* no softc */
93 };
94 
95 DRIVER_MODULE(chromebook_i2c, iicbus, chromebook_i2c_driver, 0, 0);
96 MODULE_VERSION(chromebook_i2c, 1);
97 
98