xref: /freebsd/sys/dev/chromebook_platform/chromebook_platform.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/errno.h>
38 #include <sys/bus.h>
39 
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
42 #include <dev/iicbus/iicbus.h>
43 #include <dev/iicbus/iiconf.h>
44 
45 /*
46  * Driver that attaches I2C devices.
47  */
48 static struct {
49 	uint32_t	pci_id;
50 	const char	*name;
51 	uint8_t		addr;
52 } slaves[] = {
53 	{ 0x9c628086,	"isl",		0x88 },
54 	{ 0x9c618086,	"cyapa",	0xce },
55 };
56 
57 static void
58 chromebook_i2c_identify(driver_t *driver, device_t bus)
59 {
60 	device_t controller;
61 	device_t child;
62 	int i;
63 
64 	/*
65 	 * A stopgap approach to preserve the status quo.
66 	 * A more intelligent approach is required to correctly
67 	 * identify a machine model and hardware available on it.
68 	 * For instance, DMI could be used.
69 	 * See http://lxr.free-electrons.com/source/drivers/platform/chrome/chromeos_laptop.c
70 	 */
71 	controller = device_get_parent(bus);
72 	if (strcmp(device_get_name(controller), "ig4iic_pci") != 0)
73 		return;
74 
75 	for (i = 0; i < nitems(slaves); i++) {
76 		if (device_find_child(bus, slaves[i].name, -1) != NULL)
77 			continue;
78 		if (slaves[i].pci_id != pci_get_devid(controller))
79 			continue;
80 		child = BUS_ADD_CHILD(bus, 0, slaves[i].name, -1);
81 		if (child != NULL)
82 			iicbus_set_addr(child, slaves[i].addr);
83 	}
84 }
85 
86 static device_method_t chromebook_i2c_methods[] = {
87 	DEVMETHOD(device_identify,	chromebook_i2c_identify),
88 	{ 0, 0 }
89 };
90 
91 static driver_t chromebook_i2c_driver = {
92 	"chromebook_i2c",
93 	chromebook_i2c_methods,
94 	0	/* no softc */
95 };
96 
97 static devclass_t chromebook_i2c_devclass;
98 
99 DRIVER_MODULE(chromebook_i2c, iicbus, chromebook_i2c_driver,
100     chromebook_i2c_devclass, 0, 0);
101 MODULE_VERSION(chromebook_i2c, 1);
102 
103