// SPDX-License-Identifier: GPL-2.0+
// Driver to instantiate Chromebook i2c/smbus devices.
//
// Copyright (C) 2012 Google, Inc.
// Author: Benson Leung <bleung@chromium.org>
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/acpi.h>
#include <linux/dmi.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#define ATMEL_TP_I2C_ADDR 0x4b
#define ATMEL_TP_I2C_BL_ADDR 0x25
#define ATMEL_TS_I2C_ADDR 0x4a
#define ATMEL_TS_I2C_BL_ADDR 0x26
#define CYAPA_TP_I2C_ADDR 0x67
#define ELAN_TP_I2C_ADDR 0x15
#define ISL_ALS_I2C_ADDR 0x44
#define TAOS_ALS_I2C_ADDR 0x29
static const char *i2c_adapter_names[] = {
"SMBus I801 adapter",
"i915 gmbus vga",
"i915 gmbus panel",
"Synopsys DesignWare I2C adapter",
};
/* Keep this enum consistent with i2c_adapter_names */
enum i2c_adapter_type {
I2C_ADAPTER_SMBUS = 0,
I2C_ADAPTER_VGADDC,
I2C_ADAPTER_PANEL,
I2C_ADAPTER_DESIGNWARE,
};
struct i2c_peripheral {
struct i2c_board_info board_info;
unsigned short alt_addr;
const char *dmi_name;
unsigned long irqflags;
struct resource irq_resource;
enum i2c_adapter_type type;
u32 pci_devid;
struct i2c_client *client;
};
struct acpi_peripheral {
char hid[ACPI_ID_LEN];
const struct property_entry *properties;
};
struct chromeos_laptop {
/*
* Note that we can't mark this pointer as const because
* i2c_new_probed_device() changes passed in I2C board info, so.
*/
struct i2c_peripheral *i2c_peripherals;
unsigned int num_i2c_peripherals;
const struct acpi_peripheral *acpi_peripherals;
unsigned int num_acpi_peripherals;
};
static const struct chromeos_laptop *cros_laptop;
static struct i2c_client *
chromes_laptop_instantiate_i2c_device(struct i2c_adapter *adapter,
struct i2c_board_info *info,
unsigned short alt_addr)
{
const unsigned short addr_list[] = { info->addr, I2C_CLIENT_END };
struct i2c_client *client;
/*
* Add the i2c device. If we can't detect it at the primary
* address we scan secondary addresses. In any case the client
* structure gets assigned primary address.
*/
client = i2c_new_probed_device(adapter, info, addr_list, NULL);
if (!client && alt_addr) {
struct i2c_board_info dummy_info = {
I2C_BOARD_INFO("dummy", info->addr),
};
const unsigned short alt_addr_list[] = {
alt_addr, I2C_CLIENT_END
};
struct i2c_client *dummy;
dummy = i2c_new_probed_device(adapter, &dummy_info,
alt_addr_list, NULL);
if (dummy) {
pr_debug("%d-%02x is probed at %02x\n",
adapter->nr, info->addr, dummy->addr);
i2c_unregister_device(dummy);
client = i2c_new_device(adapter, info);
}
}
if (!client)
pr_debug("failed to register device %d-%02x\n",
adapter->nr, info->addr);
else
pr_debug("added i2c device %d-%02x\n",
adapter->nr, info->addr);
return client;
}
static bool chromeos_laptop_match_adapter_devid(struct device *dev, u32 devid)
{
struct pci_dev *pdev;
if (!dev_is_pci(dev))
return false;
pdev = to_pci_dev(dev);
return devid == pci_dev_id(pdev);
}
static void chromeos_laptop_check_adapter(struct i2c_adapter *adapter)
{
struct i2c_peripheral *i2c_dev;
int i;
for (i = 0; i < cros_laptop->num_i2c_peripherals; i++) {
i2c_dev = &cros_laptop->i2c_peripherals[i];
/* Skip devices already created */
if (i2c_dev->client)
continue;
if (strncmp(adapter->name, i2c_adapter_names[i2c_dev->type],
strlen(i2c_adapter_names[i2c_dev->type])))
continue;
if (i2c_dev->pci_devid &&
!chromeos_laptop_match_adapter_devid(adapter->dev.parent,
i2c_dev->pci_devid)) {
continue;
}
i2c_dev->client =
chromes_laptop_instantiate_i2c_device(adapter,
&i2c_dev->board_info,
i2c_dev->alt_addr);
}
}
static bool chromeos_laptop_adjust_client(struct i2c_client *client)
{
const struct acpi_peripheral *acpi_dev;
struct acpi_device_id acpi_ids[2] = { };
int i;
int error;
if (!has_acpi_companion(&client->dev))
return false;
for (i = 0; i < cros_laptop->num_acpi_peripherals; i++) {
acpi_dev = &cros_laptop->acpi_peripherals[i];
memcpy(acpi_ids[0].id, acpi_dev->hid, ACPI_ID_LEN);
if (acpi_match_device(acpi_ids, &client->dev)) {
error =