1308104Savg/*-
2308104Savg * Copyright (c) 2016 The FreeBSD Project.
3308104Savg * All rights reserved.
4308104Savg *
5308104Savg * Redistribution and use in source and binary forms, with or without
6308104Savg * modification, are permitted provided that the following conditions
7308104Savg * are met:
8308104Savg *
9308104Savg * 1. Redistributions of source code must retain the above copyright
10308104Savg *    notice, this list of conditions and the following disclaimer.
11308104Savg * 2. Redistributions in binary form must reproduce the above copyright
12308104Savg *    notice, this list of conditions and the following disclaimer in
13308104Savg *    the documentation and/or other materials provided with the
14308104Savg *    distribution.
15308104Savg *
16308104Savg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17308104Savg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18308104Savg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19308104Savg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20308104Savg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21308104Savg * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22308104Savg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23308104Savg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24308104Savg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25308104Savg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26308104Savg * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27308104Savg * SUCH DAMAGE.
28308104Savg */
29308104Savg
30308104Savg#include <sys/cdefs.h>
31308104Savg__FBSDID("$FreeBSD: stable/11/sys/dev/chromebook_platform/chromebook_platform.c 314551 2017-03-02 10:43:42Z avg $");
32308104Savg
33308104Savg#include <sys/param.h>
34308104Savg#include <sys/systm.h>
35308104Savg#include <sys/kernel.h>
36308104Savg#include <sys/module.h>
37308104Savg#include <sys/errno.h>
38308104Savg#include <sys/bus.h>
39308104Savg
40308104Savg#include <dev/pci/pcivar.h>
41308104Savg#include <dev/pci/pcireg.h>
42308104Savg#include <dev/iicbus/iicbus.h>
43308104Savg#include <dev/iicbus/iiconf.h>
44308104Savg
45308104Savg/*
46308104Savg * Driver that attaches I2C devices.
47308104Savg */
48308104Savgstatic struct {
49308104Savg	uint32_t	pci_id;
50308104Savg	const char	*name;
51308104Savg	uint8_t		addr;
52308104Savg} slaves[] = {
53308104Savg	{ 0x9c628086,	"isl",		0x88 },
54308104Savg	{ 0x9c618086,	"cyapa",	0xce },
55308104Savg};
56308104Savg
57308104Savgstatic void
58308104Savgchromebook_i2c_identify(driver_t *driver, device_t bus)
59308104Savg{
60308104Savg	device_t controller;
61308104Savg	device_t child;
62308104Savg	int i;
63308104Savg
64308104Savg	/*
65308104Savg	 * A stopgap approach to preserve the status quo.
66308104Savg	 * A more intelligent approach is required to correctly
67308104Savg	 * identify a machine model and hardware available on it.
68308104Savg	 * For instance, DMI could be used.
69308104Savg	 * See http://lxr.free-electrons.com/source/drivers/platform/chrome/chromeos_laptop.c
70308104Savg	 */
71308104Savg	controller = device_get_parent(bus);
72314551Savg	if (strcmp(device_get_name(controller), "ig4iic_pci") != 0)
73308104Savg		return;
74308104Savg
75308104Savg	for (i = 0; i < nitems(slaves); i++) {
76308104Savg		if (device_find_child(bus, slaves[i].name, -1) != NULL)
77308104Savg			continue;
78308104Savg		if (slaves[i].pci_id != pci_get_devid(controller))
79308104Savg			continue;
80308104Savg		child = BUS_ADD_CHILD(bus, 0, slaves[i].name, -1);
81308104Savg		if (child != NULL)
82308104Savg			iicbus_set_addr(child, slaves[i].addr);
83308104Savg	}
84308104Savg}
85308104Savg
86308104Savgstatic device_method_t chromebook_i2c_methods[] = {
87308104Savg	DEVMETHOD(device_identify,	chromebook_i2c_identify),
88308104Savg	{ 0, 0 }
89308104Savg};
90308104Savg
91308104Savgstatic driver_t chromebook_i2c_driver = {
92308104Savg	"chromebook_i2c",
93308104Savg	chromebook_i2c_methods,
94308104Savg	0	/* no softc */
95308104Savg};
96308104Savg
97308104Savgstatic devclass_t chromebook_i2c_devclass;
98308104Savg
99308104SavgDRIVER_MODULE(chromebook_i2c, iicbus, chromebook_i2c_driver,
100308104Savg    chromebook_i2c_devclass, 0, 0);
101308104SavgMODULE_VERSION(chromebook_i2c, 1);
102308104Savg
103