|
@@ -1,36 +1,26 @@
|
|
|
-/* i2c-core.c - a device driver for the iic-bus interface */
|
|
|
-/* ------------------------------------------------------------------------- */
|
|
|
-/* Copyright (C) 1995-99 Simon G. Vogl
|
|
|
-
|
|
|
- This program is free software; you can redistribute it and/or modify
|
|
|
- it under the terms of the GNU General Public License as published by
|
|
|
- the Free Software Foundation; either version 2 of the License, or
|
|
|
- (at your option) any later version.
|
|
|
-
|
|
|
- This program is distributed in the hope that it will be useful,
|
|
|
- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
- GNU General Public License for more details. */
|
|
|
-/* ------------------------------------------------------------------------- */
|
|
|
-
|
|
|
-/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
|
|
|
- All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
|
|
|
- SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
|
|
|
- Jean Delvare <jdelvare@suse.de>
|
|
|
- Mux support by Rodolfo Giometti <giometti@enneenne.com> and
|
|
|
- Michael Lawnick <michael.lawnick.ext@nsn.com>
|
|
|
- OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
|
|
|
- (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
|
|
|
- (c) 2013 Wolfram Sang <wsa@the-dreams.de>
|
|
|
- I2C ACPI code Copyright (C) 2014 Intel Corp
|
|
|
- Author: Lan Tianyu <tianyu.lan@intel.com>
|
|
|
- I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
|
|
|
+/*
|
|
|
+ * Linux I2C core
|
|
|
+ *
|
|
|
+ * Copyright (C) 1995-99 Simon G. Vogl
|
|
|
+ * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
|
|
|
+ * Mux support by Rodolfo Giometti <giometti@enneenne.com> and
|
|
|
+ * Michael Lawnick <michael.lawnick.ext@nsn.com>
|
|
|
+ *
|
|
|
+ * Copyright (C) 2013-2017 Wolfram Sang <wsa@the-dreams.de>
|
|
|
+ *
|
|
|
+ * This program is free software; you can redistribute it and/or modify it
|
|
|
+ * under the terms of the GNU General Public License as published by the Free
|
|
|
+ * Software Foundation; either version 2 of the License, or (at your option)
|
|
|
+ * any later version.
|
|
|
+ *
|
|
|
+ * This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
*/
|
|
|
|
|
|
#define pr_fmt(fmt) "i2c-core: " fmt
|
|
|
|
|
|
#include <dt-bindings/i2c/i2c.h>
|
|
|
-#include <linux/uaccess.h>
|
|
|
#include <linux/acpi.h>
|
|
|
#include <linux/clk/clk-conf.h>
|
|
|
#include <linux/completion.h>
|
|
@@ -38,7 +28,6 @@
|
|
|
#include <linux/err.h>
|
|
|
#include <linux/errno.h>
|
|
|
#include <linux/gpio.h>
|
|
|
-#include <linux/hardirq.h>
|
|
|
#include <linux/i2c.h>
|
|
|
#include <linux/idr.h>
|
|
|
#include <linux/init.h>
|
|
@@ -68,9 +57,10 @@
|
|
|
#define I2C_ADDR_7BITS_MAX 0x77
|
|
|
#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
|
|
|
|
|
|
-/* core_lock protects i2c_adapter_idr, and guarantees
|
|
|
- that device detection, deletion of detected devices, and attach_adapter
|
|
|
- calls are serialized */
|
|
|
+/*
|
|
|
+ * core_lock protects i2c_adapter_idr, and guarantees that device detection,
|
|
|
+ * deletion of detected devices, and attach_adapter calls are serialized
|
|
|
+ */
|
|
|
static DEFINE_MUTEX(core_lock);
|
|
|
static DEFINE_IDR(i2c_adapter_idr);
|
|
|
|
|
@@ -90,652 +80,6 @@ void i2c_transfer_trace_unreg(void)
|
|
|
static_key_slow_dec(&i2c_trace_msg);
|
|
|
}
|
|
|
|
|
|
-#if defined(CONFIG_ACPI)
|
|
|
-struct i2c_acpi_handler_data {
|
|
|
- struct acpi_connection_info info;
|
|
|
- struct i2c_adapter *adapter;
|
|
|
-};
|
|
|
-
|
|
|
-struct gsb_buffer {
|
|
|
- u8 status;
|
|
|
- u8 len;
|
|
|
- union {
|
|
|
- u16 wdata;
|
|
|
- u8 bdata;
|
|
|
- u8 data[0];
|
|
|
- };
|
|
|
-} __packed;
|
|
|
-
|
|
|
-struct i2c_acpi_lookup {
|
|
|
- struct i2c_board_info *info;
|
|
|
- acpi_handle adapter_handle;
|
|
|
- acpi_handle device_handle;
|
|
|
- acpi_handle search_handle;
|
|
|
- int n;
|
|
|
- int index;
|
|
|
- u32 speed;
|
|
|
- u32 min_speed;
|
|
|
-};
|
|
|
-
|
|
|
-static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
|
|
|
-{
|
|
|
- struct i2c_acpi_lookup *lookup = data;
|
|
|
- struct i2c_board_info *info = lookup->info;
|
|
|
- struct acpi_resource_i2c_serialbus *sb;
|
|
|
- acpi_status status;
|
|
|
-
|
|
|
- if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
|
|
|
- return 1;
|
|
|
-
|
|
|
- sb = &ares->data.i2c_serial_bus;
|
|
|
- if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
|
|
|
- return 1;
|
|
|
-
|
|
|
- if (lookup->index != -1 && lookup->n++ != lookup->index)
|
|
|
- return 1;
|
|
|
-
|
|
|
- status = acpi_get_handle(lookup->device_handle,
|
|
|
- sb->resource_source.string_ptr,
|
|
|
- &lookup->adapter_handle);
|
|
|
- if (!ACPI_SUCCESS(status))
|
|
|
- return 1;
|
|
|
-
|
|
|
- info->addr = sb->slave_address;
|
|
|
- lookup->speed = sb->connection_speed;
|
|
|
- if (sb->access_mode == ACPI_I2C_10BIT_MODE)
|
|
|
- info->flags |= I2C_CLIENT_TEN;
|
|
|
-
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-static int i2c_acpi_do_lookup(struct acpi_device *adev,
|
|
|
- struct i2c_acpi_lookup *lookup)
|
|
|
-{
|
|
|
- struct i2c_board_info *info = lookup->info;
|
|
|
- struct list_head resource_list;
|
|
|
- int ret;
|
|
|
-
|
|
|
- if (acpi_bus_get_status(adev) || !adev->status.present ||
|
|
|
- acpi_device_enumerated(adev))
|
|
|
- return -EINVAL;
|
|
|
-
|
|
|
- memset(info, 0, sizeof(*info));
|
|
|
- lookup->device_handle = acpi_device_handle(adev);
|
|
|
-
|
|
|
- /* Look up for I2cSerialBus resource */
|
|
|
- INIT_LIST_HEAD(&resource_list);
|
|
|
- ret = acpi_dev_get_resources(adev, &resource_list,
|
|
|
- i2c_acpi_fill_info, lookup);
|
|
|
- acpi_dev_free_resource_list(&resource_list);
|
|
|
-
|
|
|
- if (ret < 0 || !info->addr)
|
|
|
- return -EINVAL;
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static int i2c_acpi_get_info(struct acpi_device *adev,
|
|
|
- struct i2c_board_info *info,
|
|
|
- struct i2c_adapter *adapter,
|
|
|
- acpi_handle *adapter_handle)
|
|
|
-{
|
|
|
- struct list_head resource_list;
|
|
|
- struct resource_entry *entry;
|
|
|
- struct i2c_acpi_lookup lookup;
|
|
|
- int ret;
|
|
|
-
|
|
|
- memset(&lookup, 0, sizeof(lookup));
|
|
|
- lookup.info = info;
|
|
|
- lookup.index = -1;
|
|
|
-
|
|
|
- ret = i2c_acpi_do_lookup(adev, &lookup);
|
|
|
- if (ret)
|
|
|
- return ret;
|
|
|
-
|
|
|
- if (adapter) {
|
|
|
- /* The adapter must match the one in I2cSerialBus() connector */
|
|
|
- if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
|
|
|
- return -ENODEV;
|
|
|
- } else {
|
|
|
- struct acpi_device *adapter_adev;
|
|
|
-
|
|
|
- /* The adapter must be present */
|
|
|
- if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
|
|
|
- return -ENODEV;
|
|
|
- if (acpi_bus_get_status(adapter_adev) ||
|
|
|
- !adapter_adev->status.present)
|
|
|
- return -ENODEV;
|
|
|
- }
|
|
|
-
|
|
|
- info->fwnode = acpi_fwnode_handle(adev);
|
|
|
- if (adapter_handle)
|
|
|
- *adapter_handle = lookup.adapter_handle;
|
|
|
-
|
|
|
- /* Then fill IRQ number if any */
|
|
|
- INIT_LIST_HEAD(&resource_list);
|
|
|
- ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
|
|
|
- if (ret < 0)
|
|
|
- return -EINVAL;
|
|
|
-
|
|
|
- resource_list_for_each_entry(entry, &resource_list) {
|
|
|
- if (resource_type(entry->res) == IORESOURCE_IRQ) {
|
|
|
- info->irq = entry->res->start;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- acpi_dev_free_resource_list(&resource_list);
|
|
|
-
|
|
|
- acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
|
|
|
- sizeof(info->type));
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static void i2c_acpi_register_device(struct i2c_adapter *adapter,
|
|
|
- struct acpi_device *adev,
|
|
|
- struct i2c_board_info *info)
|
|
|
-{
|
|
|
- adev->power.flags.ignore_parent = true;
|
|
|
- acpi_device_set_enumerated(adev);
|
|
|
-
|
|
|
- if (!i2c_new_device(adapter, info)) {
|
|
|
- adev->power.flags.ignore_parent = false;
|
|
|
- dev_err(&adapter->dev,
|
|
|
- "failed to add I2C device %s from ACPI\n",
|
|
|
- dev_name(&adev->dev));
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
|
|
|
- void *data, void **return_value)
|
|
|
-{
|
|
|
- struct i2c_adapter *adapter = data;
|
|
|
- struct acpi_device *adev;
|
|
|
- struct i2c_board_info info;
|
|
|
-
|
|
|
- if (acpi_bus_get_device(handle, &adev))
|
|
|
- return AE_OK;
|
|
|
-
|
|
|
- if (i2c_acpi_get_info(adev, &info, adapter, NULL))
|
|
|
- return AE_OK;
|
|
|
-
|
|
|
- i2c_acpi_register_device(adapter, adev, &info);
|
|
|
-
|
|
|
- return AE_OK;
|
|
|
-}
|
|
|
-
|
|
|
-#define I2C_ACPI_MAX_SCAN_DEPTH 32
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
|
|
|
- * @adap: pointer to adapter
|
|
|
- *
|
|
|
- * Enumerate all I2C slave devices behind this adapter by walking the ACPI
|
|
|
- * namespace. When a device is found it will be added to the Linux device
|
|
|
- * model and bound to the corresponding ACPI handle.
|
|
|
- */
|
|
|
-static void i2c_acpi_register_devices(struct i2c_adapter *adap)
|
|
|
-{
|
|
|
- acpi_status status;
|
|
|
-
|
|
|
- if (!has_acpi_companion(&adap->dev))
|
|
|
- return;
|
|
|
-
|
|
|
- status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
|
|
|
- I2C_ACPI_MAX_SCAN_DEPTH,
|
|
|
- i2c_acpi_add_device, NULL,
|
|
|
- adap, NULL);
|
|
|
- if (ACPI_FAILURE(status))
|
|
|
- dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
|
|
|
-}
|
|
|
-
|
|
|
-static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
|
|
|
- void *data, void **return_value)
|
|
|
-{
|
|
|
- struct i2c_acpi_lookup *lookup = data;
|
|
|
- struct acpi_device *adev;
|
|
|
-
|
|
|
- if (acpi_bus_get_device(handle, &adev))
|
|
|
- return AE_OK;
|
|
|
-
|
|
|
- if (i2c_acpi_do_lookup(adev, lookup))
|
|
|
- return AE_OK;
|
|
|
-
|
|
|
- if (lookup->search_handle != lookup->adapter_handle)
|
|
|
- return AE_OK;
|
|
|
-
|
|
|
- if (lookup->speed <= lookup->min_speed)
|
|
|
- lookup->min_speed = lookup->speed;
|
|
|
-
|
|
|
- return AE_OK;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
|
|
|
- * @dev: The device owning the bus
|
|
|
- *
|
|
|
- * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
|
|
|
- * devices connected to this bus and use the speed of slowest device.
|
|
|
- *
|
|
|
- * Returns the speed in Hz or zero
|
|
|
- */
|
|
|
-u32 i2c_acpi_find_bus_speed(struct device *dev)
|
|
|
-{
|
|
|
- struct i2c_acpi_lookup lookup;
|
|
|
- struct i2c_board_info dummy;
|
|
|
- acpi_status status;
|
|
|
-
|
|
|
- if (!has_acpi_companion(dev))
|
|
|
- return 0;
|
|
|
-
|
|
|
- memset(&lookup, 0, sizeof(lookup));
|
|
|
- lookup.search_handle = ACPI_HANDLE(dev);
|
|
|
- lookup.min_speed = UINT_MAX;
|
|
|
- lookup.info = &dummy;
|
|
|
- lookup.index = -1;
|
|
|
-
|
|
|
- status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
|
|
|
- I2C_ACPI_MAX_SCAN_DEPTH,
|
|
|
- i2c_acpi_lookup_speed, NULL,
|
|
|
- &lookup, NULL);
|
|
|
-
|
|
|
- if (ACPI_FAILURE(status)) {
|
|
|
- dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
|
|
|
-
|
|
|
-static int i2c_acpi_match_adapter(struct device *dev, void *data)
|
|
|
-{
|
|
|
- struct i2c_adapter *adapter = i2c_verify_adapter(dev);
|
|
|
-
|
|
|
- if (!adapter)
|
|
|
- return 0;
|
|
|
-
|
|
|
- return ACPI_HANDLE(dev) == (acpi_handle)data;
|
|
|
-}
|
|
|
-
|
|
|
-static int i2c_acpi_match_device(struct device *dev, void *data)
|
|
|
-{
|
|
|
- return ACPI_COMPANION(dev) == data;
|
|
|
-}
|
|
|
-
|
|
|
-static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
|
|
|
-{
|
|
|
- struct device *dev;
|
|
|
-
|
|
|
- dev = bus_find_device(&i2c_bus_type, NULL, handle,
|
|
|
- i2c_acpi_match_adapter);
|
|
|
- return dev ? i2c_verify_adapter(dev) : NULL;
|
|
|
-}
|
|
|
-
|
|
|
-static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
|
|
|
-{
|
|
|
- struct device *dev;
|
|
|
-
|
|
|
- dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
|
|
|
- return dev ? i2c_verify_client(dev) : NULL;
|
|
|
-}
|
|
|
-
|
|
|
-static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
|
|
|
- void *arg)
|
|
|
-{
|
|
|
- struct acpi_device *adev = arg;
|
|
|
- struct i2c_board_info info;
|
|
|
- acpi_handle adapter_handle;
|
|
|
- struct i2c_adapter *adapter;
|
|
|
- struct i2c_client *client;
|
|
|
-
|
|
|
- switch (value) {
|
|
|
- case ACPI_RECONFIG_DEVICE_ADD:
|
|
|
- if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
|
|
|
- break;
|
|
|
-
|
|
|
- adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
|
|
|
- if (!adapter)
|
|
|
- break;
|
|
|
-
|
|
|
- i2c_acpi_register_device(adapter, adev, &info);
|
|
|
- break;
|
|
|
- case ACPI_RECONFIG_DEVICE_REMOVE:
|
|
|
- if (!acpi_device_enumerated(adev))
|
|
|
- break;
|
|
|
-
|
|
|
- client = i2c_acpi_find_client_by_adev(adev);
|
|
|
- if (!client)
|
|
|
- break;
|
|
|
-
|
|
|
- i2c_unregister_device(client);
|
|
|
- put_device(&client->dev);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- return NOTIFY_OK;
|
|
|
-}
|
|
|
-
|
|
|
-static struct notifier_block i2c_acpi_notifier = {
|
|
|
- .notifier_call = i2c_acpi_notify,
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
|
|
|
- * @dev: Device owning the ACPI resources to get the client from
|
|
|
- * @index: Index of ACPI resource to get
|
|
|
- * @info: describes the I2C device; note this is modified (addr gets set)
|
|
|
- * Context: can sleep
|
|
|
- *
|
|
|
- * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
|
|
|
- * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
|
|
|
- * resources, in that case this function can be used to create an i2c-client
|
|
|
- * for other I2cSerialBus resources in the Current Resource Settings table.
|
|
|
- *
|
|
|
- * Also see i2c_new_device, which this function calls to create the i2c-client.
|
|
|
- *
|
|
|
- * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
|
|
|
- */
|
|
|
-struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
|
|
|
- struct i2c_board_info *info)
|
|
|
-{
|
|
|
- struct i2c_acpi_lookup lookup;
|
|
|
- struct i2c_adapter *adapter;
|
|
|
- struct acpi_device *adev;
|
|
|
- LIST_HEAD(resource_list);
|
|
|
- int ret;
|
|
|
-
|
|
|
- adev = ACPI_COMPANION(dev);
|
|
|
- if (!adev)
|
|
|
- return NULL;
|
|
|
-
|
|
|
- memset(&lookup, 0, sizeof(lookup));
|
|
|
- lookup.info = info;
|
|
|
- lookup.device_handle = acpi_device_handle(adev);
|
|
|
- lookup.index = index;
|
|
|
-
|
|
|
- ret = acpi_dev_get_resources(adev, &resource_list,
|
|
|
- i2c_acpi_fill_info, &lookup);
|
|
|
- acpi_dev_free_resource_list(&resource_list);
|
|
|
-
|
|
|
- if (ret < 0 || !info->addr)
|
|
|
- return NULL;
|
|
|
-
|
|
|
- adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
|
|
|
- if (!adapter)
|
|
|
- return NULL;
|
|
|
-
|
|
|
- return i2c_new_device(adapter, info);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
|
|
|
-#else /* CONFIG_ACPI */
|
|
|
-static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
|
|
|
-extern struct notifier_block i2c_acpi_notifier;
|
|
|
-#endif /* CONFIG_ACPI */
|
|
|
-
|
|
|
-#ifdef CONFIG_ACPI_I2C_OPREGION
|
|
|
-static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
|
|
|
- u8 cmd, u8 *data, u8 data_len)
|
|
|
-{
|
|
|
-
|
|
|
- struct i2c_msg msgs[2];
|
|
|
- int ret;
|
|
|
- u8 *buffer;
|
|
|
-
|
|
|
- buffer = kzalloc(data_len, GFP_KERNEL);
|
|
|
- if (!buffer)
|
|
|
- return AE_NO_MEMORY;
|
|
|
-
|
|
|
- msgs[0].addr = client->addr;
|
|
|
- msgs[0].flags = client->flags;
|
|
|
- msgs[0].len = 1;
|
|
|
- msgs[0].buf = &cmd;
|
|
|
-
|
|
|
- msgs[1].addr = client->addr;
|
|
|
- msgs[1].flags = client->flags | I2C_M_RD;
|
|
|
- msgs[1].len = data_len;
|
|
|
- msgs[1].buf = buffer;
|
|
|
-
|
|
|
- ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
|
|
|
- if (ret < 0)
|
|
|
- dev_err(&client->adapter->dev, "i2c read failed\n");
|
|
|
- else
|
|
|
- memcpy(data, buffer, data_len);
|
|
|
-
|
|
|
- kfree(buffer);
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
|
|
|
- u8 cmd, u8 *data, u8 data_len)
|
|
|
-{
|
|
|
-
|
|
|
- struct i2c_msg msgs[1];
|
|
|
- u8 *buffer;
|
|
|
- int ret = AE_OK;
|
|
|
-
|
|
|
- buffer = kzalloc(data_len + 1, GFP_KERNEL);
|
|
|
- if (!buffer)
|
|
|
- return AE_NO_MEMORY;
|
|
|
-
|
|
|
- buffer[0] = cmd;
|
|
|
- memcpy(buffer + 1, data, data_len);
|
|
|
-
|
|
|
- msgs[0].addr = client->addr;
|
|
|
- msgs[0].flags = client->flags;
|
|
|
- msgs[0].len = data_len + 1;
|
|
|
- msgs[0].buf = buffer;
|
|
|
-
|
|
|
- ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
|
|
|
- if (ret < 0)
|
|
|
- dev_err(&client->adapter->dev, "i2c write failed\n");
|
|
|
-
|
|
|
- kfree(buffer);
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-static acpi_status
|
|
|
-i2c_acpi_space_handler(u32 function, acpi_physical_address command,
|
|
|
- u32 bits, u64 *value64,
|
|
|
- void *handler_context, void *region_context)
|
|
|
-{
|
|
|
- struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
|
|
|
- struct i2c_acpi_handler_data *data = handler_context;
|
|
|
- struct acpi_connection_info *info = &data->info;
|
|
|
- struct acpi_resource_i2c_serialbus *sb;
|
|
|
- struct i2c_adapter *adapter = data->adapter;
|
|
|
- struct i2c_client *client;
|
|
|
- struct acpi_resource *ares;
|
|
|
- u32 accessor_type = function >> 16;
|
|
|
- u8 action = function & ACPI_IO_MASK;
|
|
|
- acpi_status ret;
|
|
|
- int status;
|
|
|
-
|
|
|
- ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
|
|
|
- if (ACPI_FAILURE(ret))
|
|
|
- return ret;
|
|
|
-
|
|
|
- client = kzalloc(sizeof(*client), GFP_KERNEL);
|
|
|
- if (!client) {
|
|
|
- ret = AE_NO_MEMORY;
|
|
|
- goto err;
|
|
|
- }
|
|
|
-
|
|
|
- if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
|
|
|
- ret = AE_BAD_PARAMETER;
|
|
|
- goto err;
|
|
|
- }
|
|
|
-
|
|
|
- sb = &ares->data.i2c_serial_bus;
|
|
|
- if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
|
|
|
- ret = AE_BAD_PARAMETER;
|
|
|
- goto err;
|
|
|
- }
|
|
|
-
|
|
|
- client->adapter = adapter;
|
|
|
- client->addr = sb->slave_address;
|
|
|
-
|
|
|
- if (sb->access_mode == ACPI_I2C_10BIT_MODE)
|
|
|
- client->flags |= I2C_CLIENT_TEN;
|
|
|
-
|
|
|
- switch (accessor_type) {
|
|
|
- case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
|
|
|
- if (action == ACPI_READ) {
|
|
|
- status = i2c_smbus_read_byte(client);
|
|
|
- if (status >= 0) {
|
|
|
- gsb->bdata = status;
|
|
|
- status = 0;
|
|
|
- }
|
|
|
- } else {
|
|
|
- status = i2c_smbus_write_byte(client, gsb->bdata);
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- case ACPI_GSB_ACCESS_ATTRIB_BYTE:
|
|
|
- if (action == ACPI_READ) {
|
|
|
- status = i2c_smbus_read_byte_data(client, command);
|
|
|
- if (status >= 0) {
|
|
|
- gsb->bdata = status;
|
|
|
- status = 0;
|
|
|
- }
|
|
|
- } else {
|
|
|
- status = i2c_smbus_write_byte_data(client, command,
|
|
|
- gsb->bdata);
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- case ACPI_GSB_ACCESS_ATTRIB_WORD:
|
|
|
- if (action == ACPI_READ) {
|
|
|
- status = i2c_smbus_read_word_data(client, command);
|
|
|
- if (status >= 0) {
|
|
|
- gsb->wdata = status;
|
|
|
- status = 0;
|
|
|
- }
|
|
|
- } else {
|
|
|
- status = i2c_smbus_write_word_data(client, command,
|
|
|
- gsb->wdata);
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
|
|
|
- if (action == ACPI_READ) {
|
|
|
- status = i2c_smbus_read_block_data(client, command,
|
|
|
- gsb->data);
|
|
|
- if (status >= 0) {
|
|
|
- gsb->len = status;
|
|
|
- status = 0;
|
|
|
- }
|
|
|
- } else {
|
|
|
- status = i2c_smbus_write_block_data(client, command,
|
|
|
- gsb->len, gsb->data);
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
|
|
|
- if (action == ACPI_READ) {
|
|
|
- status = acpi_gsb_i2c_read_bytes(client, command,
|
|
|
- gsb->data, info->access_length);
|
|
|
- if (status > 0)
|
|
|
- status = 0;
|
|
|
- } else {
|
|
|
- status = acpi_gsb_i2c_write_bytes(client, command,
|
|
|
- gsb->data, info->access_length);
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- default:
|
|
|
- dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
|
|
|
- accessor_type, client->addr);
|
|
|
- ret = AE_BAD_PARAMETER;
|
|
|
- goto err;
|
|
|
- }
|
|
|
-
|
|
|
- gsb->status = status;
|
|
|
-
|
|
|
- err:
|
|
|
- kfree(client);
|
|
|
- ACPI_FREE(ares);
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
|
|
|
-{
|
|
|
- acpi_handle handle;
|
|
|
- struct i2c_acpi_handler_data *data;
|
|
|
- acpi_status status;
|
|
|
-
|
|
|
- if (!adapter->dev.parent)
|
|
|
- return -ENODEV;
|
|
|
-
|
|
|
- handle = ACPI_HANDLE(adapter->dev.parent);
|
|
|
-
|
|
|
- if (!handle)
|
|
|
- return -ENODEV;
|
|
|
-
|
|
|
- data = kzalloc(sizeof(struct i2c_acpi_handler_data),
|
|
|
- GFP_KERNEL);
|
|
|
- if (!data)
|
|
|
- return -ENOMEM;
|
|
|
-
|
|
|
- data->adapter = adapter;
|
|
|
- status = acpi_bus_attach_private_data(handle, (void *)data);
|
|
|
- if (ACPI_FAILURE(status)) {
|
|
|
- kfree(data);
|
|
|
- return -ENOMEM;
|
|
|
- }
|
|
|
-
|
|
|
- status = acpi_install_address_space_handler(handle,
|
|
|
- ACPI_ADR_SPACE_GSBUS,
|
|
|
- &i2c_acpi_space_handler,
|
|
|
- NULL,
|
|
|
- data);
|
|
|
- if (ACPI_FAILURE(status)) {
|
|
|
- dev_err(&adapter->dev, "Error installing i2c space handler\n");
|
|
|
- acpi_bus_detach_private_data(handle);
|
|
|
- kfree(data);
|
|
|
- return -ENOMEM;
|
|
|
- }
|
|
|
-
|
|
|
- acpi_walk_dep_device_list(handle);
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
|
|
|
-{
|
|
|
- acpi_handle handle;
|
|
|
- struct i2c_acpi_handler_data *data;
|
|
|
- acpi_status status;
|
|
|
-
|
|
|
- if (!adapter->dev.parent)
|
|
|
- return;
|
|
|
-
|
|
|
- handle = ACPI_HANDLE(adapter->dev.parent);
|
|
|
-
|
|
|
- if (!handle)
|
|
|
- return;
|
|
|
-
|
|
|
- acpi_remove_address_space_handler(handle,
|
|
|
- ACPI_ADR_SPACE_GSBUS,
|
|
|
- &i2c_acpi_space_handler);
|
|
|
-
|
|
|
- status = acpi_bus_get_private_data(handle, (void **)&data);
|
|
|
- if (ACPI_SUCCESS(status))
|
|
|
- kfree(data);
|
|
|
-
|
|
|
- acpi_bus_detach_private_data(handle);
|
|
|
-}
|
|
|
-#else /* CONFIG_ACPI_I2C_OPREGION */
|
|
|
-static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
|
|
|
-{ }
|
|
|
-
|
|
|
-static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
|
|
|
-{ return 0; }
|
|
|
-#endif /* CONFIG_ACPI_I2C_OPREGION */
|
|
|
-
|
|
|
-/* ------------------------------------------------------------------------- */
|
|
|
-
|
|
|
const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
|
|
|
const struct i2c_client *client)
|
|
|
{
|
|
@@ -1195,7 +539,7 @@ static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
|
|
|
|
|
|
/* This is a permissive address validity check, I2C address map constraints
|
|
|
* are purposely not enforced, except for the general call address. */
|
|
|
-static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
|
|
|
+int i2c_check_addr_validity(unsigned addr, unsigned short flags)
|
|
|
{
|
|
|
if (flags & I2C_CLIENT_TEN) {
|
|
|
/* 10-bit address, all values are valid */
|
|
@@ -1213,7 +557,7 @@ static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
|
|
|
* device uses a reserved address, then it shouldn't be probed. 7-bit
|
|
|
* addressing is assumed, 10-bit address devices are rare and should be
|
|
|
* explicitly enumerated. */
|
|
|
-static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
|
|
|
+int i2c_check_7bit_addr_validity_strict(unsigned short addr)
|
|
|
{
|
|
|
/*
|
|
|
* Reserved addresses per I2C specification:
|
|
@@ -1733,240 +1077,36 @@ EXPORT_SYMBOL_GPL(i2c_adapter_type);
|
|
|
*
|
|
|
* When traversing the driver model tree, perhaps using driver model
|
|
|
* iterators like @device_for_each_child(), you can't assume very much
|
|
|
- * about the nodes you find. Use this function to avoid oopses caused
|
|
|
- * by wrongly treating some non-I2C device as an i2c_adapter.
|
|
|
- */
|
|
|
-struct i2c_adapter *i2c_verify_adapter(struct device *dev)
|
|
|
-{
|
|
|
- return (dev->type == &i2c_adapter_type)
|
|
|
- ? to_i2c_adapter(dev)
|
|
|
- : NULL;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_verify_adapter);
|
|
|
-
|
|
|
-#ifdef CONFIG_I2C_COMPAT
|
|
|
-static struct class_compat *i2c_adapter_compat_class;
|
|
|
-#endif
|
|
|
-
|
|
|
-static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
|
|
|
-{
|
|
|
- struct i2c_devinfo *devinfo;
|
|
|
-
|
|
|
- down_read(&__i2c_board_lock);
|
|
|
- list_for_each_entry(devinfo, &__i2c_board_list, list) {
|
|
|
- if (devinfo->busnum == adapter->nr
|
|
|
- && !i2c_new_device(adapter,
|
|
|
- &devinfo->board_info))
|
|
|
- dev_err(&adapter->dev,
|
|
|
- "Can't create device at 0x%02x\n",
|
|
|
- devinfo->board_info.addr);
|
|
|
- }
|
|
|
- up_read(&__i2c_board_lock);
|
|
|
-}
|
|
|
-
|
|
|
-/* OF support code */
|
|
|
-
|
|
|
-#if IS_ENABLED(CONFIG_OF)
|
|
|
-static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
|
|
|
- struct device_node *node)
|
|
|
-{
|
|
|
- struct i2c_client *result;
|
|
|
- struct i2c_board_info info = {};
|
|
|
- struct dev_archdata dev_ad = {};
|
|
|
- const __be32 *addr_be;
|
|
|
- u32 addr;
|
|
|
- int len;
|
|
|
-
|
|
|
- dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
|
|
|
-
|
|
|
- if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
|
|
|
- dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
|
|
|
- node->full_name);
|
|
|
- return ERR_PTR(-EINVAL);
|
|
|
- }
|
|
|
-
|
|
|
- addr_be = of_get_property(node, "reg", &len);
|
|
|
- if (!addr_be || (len < sizeof(*addr_be))) {
|
|
|
- dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
|
|
|
- node->full_name);
|
|
|
- return ERR_PTR(-EINVAL);
|
|
|
- }
|
|
|
-
|
|
|
- addr = be32_to_cpup(addr_be);
|
|
|
- if (addr & I2C_TEN_BIT_ADDRESS) {
|
|
|
- addr &= ~I2C_TEN_BIT_ADDRESS;
|
|
|
- info.flags |= I2C_CLIENT_TEN;
|
|
|
- }
|
|
|
-
|
|
|
- if (addr & I2C_OWN_SLAVE_ADDRESS) {
|
|
|
- addr &= ~I2C_OWN_SLAVE_ADDRESS;
|
|
|
- info.flags |= I2C_CLIENT_SLAVE;
|
|
|
- }
|
|
|
-
|
|
|
- if (i2c_check_addr_validity(addr, info.flags)) {
|
|
|
- dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
|
|
|
- addr, node->full_name);
|
|
|
- return ERR_PTR(-EINVAL);
|
|
|
- }
|
|
|
-
|
|
|
- info.addr = addr;
|
|
|
- info.of_node = of_node_get(node);
|
|
|
- info.archdata = &dev_ad;
|
|
|
-
|
|
|
- if (of_property_read_bool(node, "host-notify"))
|
|
|
- info.flags |= I2C_CLIENT_HOST_NOTIFY;
|
|
|
-
|
|
|
- if (of_get_property(node, "wakeup-source", NULL))
|
|
|
- info.flags |= I2C_CLIENT_WAKE;
|
|
|
-
|
|
|
- result = i2c_new_device(adap, &info);
|
|
|
- if (result == NULL) {
|
|
|
- dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
|
|
|
- node->full_name);
|
|
|
- of_node_put(node);
|
|
|
- return ERR_PTR(-EINVAL);
|
|
|
- }
|
|
|
- return result;
|
|
|
-}
|
|
|
-
|
|
|
-static void of_i2c_register_devices(struct i2c_adapter *adap)
|
|
|
-{
|
|
|
- struct device_node *bus, *node;
|
|
|
- struct i2c_client *client;
|
|
|
-
|
|
|
- /* Only register child devices if the adapter has a node pointer set */
|
|
|
- if (!adap->dev.of_node)
|
|
|
- return;
|
|
|
-
|
|
|
- dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
|
|
|
-
|
|
|
- bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
|
|
|
- if (!bus)
|
|
|
- bus = of_node_get(adap->dev.of_node);
|
|
|
-
|
|
|
- for_each_available_child_of_node(bus, node) {
|
|
|
- if (of_node_test_and_set_flag(node, OF_POPULATED))
|
|
|
- continue;
|
|
|
-
|
|
|
- client = of_i2c_register_device(adap, node);
|
|
|
- if (IS_ERR(client)) {
|
|
|
- dev_warn(&adap->dev,
|
|
|
- "Failed to create I2C device for %s\n",
|
|
|
- node->full_name);
|
|
|
- of_node_clear_flag(node, OF_POPULATED);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- of_node_put(bus);
|
|
|
-}
|
|
|
-
|
|
|
-static int of_dev_node_match(struct device *dev, void *data)
|
|
|
-{
|
|
|
- return dev->of_node == data;
|
|
|
-}
|
|
|
-
|
|
|
-/* must call put_device() when done with returned i2c_client device */
|
|
|
-struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
|
|
|
-{
|
|
|
- struct device *dev;
|
|
|
- struct i2c_client *client;
|
|
|
-
|
|
|
- dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
|
|
|
- if (!dev)
|
|
|
- return NULL;
|
|
|
-
|
|
|
- client = i2c_verify_client(dev);
|
|
|
- if (!client)
|
|
|
- put_device(dev);
|
|
|
-
|
|
|
- return client;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(of_find_i2c_device_by_node);
|
|
|
-
|
|
|
-/* must call put_device() when done with returned i2c_adapter device */
|
|
|
-struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
|
|
|
-{
|
|
|
- struct device *dev;
|
|
|
- struct i2c_adapter *adapter;
|
|
|
-
|
|
|
- dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
|
|
|
- if (!dev)
|
|
|
- return NULL;
|
|
|
-
|
|
|
- adapter = i2c_verify_adapter(dev);
|
|
|
- if (!adapter)
|
|
|
- put_device(dev);
|
|
|
-
|
|
|
- return adapter;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
|
|
|
-
|
|
|
-/* must call i2c_put_adapter() when done with returned i2c_adapter device */
|
|
|
-struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
|
|
|
-{
|
|
|
- struct i2c_adapter *adapter;
|
|
|
-
|
|
|
- adapter = of_find_i2c_adapter_by_node(node);
|
|
|
- if (!adapter)
|
|
|
- return NULL;
|
|
|
-
|
|
|
- if (!try_module_get(adapter->owner)) {
|
|
|
- put_device(&adapter->dev);
|
|
|
- adapter = NULL;
|
|
|
- }
|
|
|
-
|
|
|
- return adapter;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
|
|
|
-
|
|
|
-static const struct of_device_id*
|
|
|
-i2c_of_match_device_sysfs(const struct of_device_id *matches,
|
|
|
- struct i2c_client *client)
|
|
|
-{
|
|
|
- const char *name;
|
|
|
-
|
|
|
- for (; matches->compatible[0]; matches++) {
|
|
|
- /*
|
|
|
- * Adding devices through the i2c sysfs interface provides us
|
|
|
- * a string to match which may be compatible with the device
|
|
|
- * tree compatible strings, however with no actual of_node the
|
|
|
- * of_match_device() will not match
|
|
|
- */
|
|
|
- if (sysfs_streq(client->name, matches->compatible))
|
|
|
- return matches;
|
|
|
-
|
|
|
- name = strchr(matches->compatible, ',');
|
|
|
- if (!name)
|
|
|
- name = matches->compatible;
|
|
|
- else
|
|
|
- name++;
|
|
|
-
|
|
|
- if (sysfs_streq(client->name, name))
|
|
|
- return matches;
|
|
|
- }
|
|
|
-
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-
|
|
|
-const struct of_device_id
|
|
|
-*i2c_of_match_device(const struct of_device_id *matches,
|
|
|
- struct i2c_client *client)
|
|
|
+ * about the nodes you find. Use this function to avoid oopses caused
|
|
|
+ * by wrongly treating some non-I2C device as an i2c_adapter.
|
|
|
+ */
|
|
|
+struct i2c_adapter *i2c_verify_adapter(struct device *dev)
|
|
|
{
|
|
|
- const struct of_device_id *match;
|
|
|
+ return (dev->type == &i2c_adapter_type)
|
|
|
+ ? to_i2c_adapter(dev)
|
|
|
+ : NULL;
|
|
|
+}
|
|
|
+EXPORT_SYMBOL(i2c_verify_adapter);
|
|
|
|
|
|
- if (!(client && matches))
|
|
|
- return NULL;
|
|
|
+#ifdef CONFIG_I2C_COMPAT
|
|
|
+static struct class_compat *i2c_adapter_compat_class;
|
|
|
+#endif
|
|
|
|
|
|
- match = of_match_device(matches, &client->dev);
|
|
|
- if (match)
|
|
|
- return match;
|
|
|
+static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
|
|
|
+{
|
|
|
+ struct i2c_devinfo *devinfo;
|
|
|
|
|
|
- return i2c_of_match_device_sysfs(matches, client);
|
|
|
+ down_read(&__i2c_board_lock);
|
|
|
+ list_for_each_entry(devinfo, &__i2c_board_list, list) {
|
|
|
+ if (devinfo->busnum == adapter->nr
|
|
|
+ && !i2c_new_device(adapter,
|
|
|
+ &devinfo->board_info))
|
|
|
+ dev_err(&adapter->dev,
|
|
|
+ "Can't create device at 0x%02x\n",
|
|
|
+ devinfo->board_info.addr);
|
|
|
+ }
|
|
|
+ up_read(&__i2c_board_lock);
|
|
|
}
|
|
|
-EXPORT_SYMBOL_GPL(i2c_of_match_device);
|
|
|
-#else
|
|
|
-static void of_i2c_register_devices(struct i2c_adapter *adap) { }
|
|
|
-#endif /* CONFIG_OF */
|
|
|
|
|
|
static int i2c_do_add_adapter(struct i2c_driver *driver,
|
|
|
struct i2c_adapter *adap)
|
|
@@ -2562,62 +1702,6 @@ void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
|
|
|
}
|
|
|
EXPORT_SYMBOL(i2c_clients_command);
|
|
|
|
|
|
-#if IS_ENABLED(CONFIG_OF_DYNAMIC)
|
|
|
-static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
|
|
|
- void *arg)
|
|
|
-{
|
|
|
- struct of_reconfig_data *rd = arg;
|
|
|
- struct i2c_adapter *adap;
|
|
|
- struct i2c_client *client;
|
|
|
-
|
|
|
- switch (of_reconfig_get_state_change(action, rd)) {
|
|
|
- case OF_RECONFIG_CHANGE_ADD:
|
|
|
- adap = of_find_i2c_adapter_by_node(rd->dn->parent);
|
|
|
- if (adap == NULL)
|
|
|
- return NOTIFY_OK; /* not for us */
|
|
|
-
|
|
|
- if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
|
|
|
- put_device(&adap->dev);
|
|
|
- return NOTIFY_OK;
|
|
|
- }
|
|
|
-
|
|
|
- client = of_i2c_register_device(adap, rd->dn);
|
|
|
- put_device(&adap->dev);
|
|
|
-
|
|
|
- if (IS_ERR(client)) {
|
|
|
- dev_err(&adap->dev, "failed to create client for '%s'\n",
|
|
|
- rd->dn->full_name);
|
|
|
- of_node_clear_flag(rd->dn, OF_POPULATED);
|
|
|
- return notifier_from_errno(PTR_ERR(client));
|
|
|
- }
|
|
|
- break;
|
|
|
- case OF_RECONFIG_CHANGE_REMOVE:
|
|
|
- /* already depopulated? */
|
|
|
- if (!of_node_check_flag(rd->dn, OF_POPULATED))
|
|
|
- return NOTIFY_OK;
|
|
|
-
|
|
|
- /* find our device by node */
|
|
|
- client = of_find_i2c_device_by_node(rd->dn);
|
|
|
- if (client == NULL)
|
|
|
- return NOTIFY_OK; /* no? not meant for us */
|
|
|
-
|
|
|
- /* unregister takes one ref away */
|
|
|
- i2c_unregister_device(client);
|
|
|
-
|
|
|
- /* and put the reference of the find */
|
|
|
- put_device(&client->dev);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- return NOTIFY_OK;
|
|
|
-}
|
|
|
-static struct notifier_block i2c_of_notifier = {
|
|
|
- .notifier_call = of_i2c_notify,
|
|
|
-};
|
|
|
-#else
|
|
|
-extern struct notifier_block i2c_of_notifier;
|
|
|
-#endif /* CONFIG_OF_DYNAMIC */
|
|
|
-
|
|
|
static int __init i2c_init(void)
|
|
|
{
|
|
|
int retval;
|
|
@@ -3156,676 +2240,6 @@ void i2c_put_adapter(struct i2c_adapter *adap)
|
|
|
}
|
|
|
EXPORT_SYMBOL(i2c_put_adapter);
|
|
|
|
|
|
-/* The SMBus parts */
|
|
|
-
|
|
|
-#define POLY (0x1070U << 3)
|
|
|
-static u8 crc8(u16 data)
|
|
|
-{
|
|
|
- int i;
|
|
|
-
|
|
|
- for (i = 0; i < 8; i++) {
|
|
|
- if (data & 0x8000)
|
|
|
- data = data ^ POLY;
|
|
|
- data = data << 1;
|
|
|
- }
|
|
|
- return (u8)(data >> 8);
|
|
|
-}
|
|
|
-
|
|
|
-/* Incremental CRC8 over count bytes in the array pointed to by p */
|
|
|
-static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
|
|
|
-{
|
|
|
- int i;
|
|
|
-
|
|
|
- for (i = 0; i < count; i++)
|
|
|
- crc = crc8((crc ^ p[i]) << 8);
|
|
|
- return crc;
|
|
|
-}
|
|
|
-
|
|
|
-/* Assume a 7-bit address, which is reasonable for SMBus */
|
|
|
-static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
|
|
|
-{
|
|
|
- /* The address will be sent first */
|
|
|
- u8 addr = i2c_8bit_addr_from_msg(msg);
|
|
|
- pec = i2c_smbus_pec(pec, &addr, 1);
|
|
|
-
|
|
|
- /* The data buffer follows */
|
|
|
- return i2c_smbus_pec(pec, msg->buf, msg->len);
|
|
|
-}
|
|
|
-
|
|
|
-/* Used for write only transactions */
|
|
|
-static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
|
|
|
-{
|
|
|
- msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
|
|
|
- msg->len++;
|
|
|
-}
|
|
|
-
|
|
|
-/* Return <0 on CRC error
|
|
|
- If there was a write before this read (most cases) we need to take the
|
|
|
- partial CRC from the write part into account.
|
|
|
- Note that this function does modify the message (we need to decrease the
|
|
|
- message length to hide the CRC byte from the caller). */
|
|
|
-static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
|
|
|
-{
|
|
|
- u8 rpec = msg->buf[--msg->len];
|
|
|
- cpec = i2c_smbus_msg_pec(cpec, msg);
|
|
|
-
|
|
|
- if (rpec != cpec) {
|
|
|
- pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
|
|
|
- rpec, cpec);
|
|
|
- return -EBADMSG;
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_read_byte - SMBus "receive byte" protocol
|
|
|
- * @client: Handle to slave device
|
|
|
- *
|
|
|
- * This executes the SMBus "receive byte" protocol, returning negative errno
|
|
|
- * else the byte received from the device.
|
|
|
- */
|
|
|
-s32 i2c_smbus_read_byte(const struct i2c_client *client)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
- int status;
|
|
|
-
|
|
|
- status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_READ, 0,
|
|
|
- I2C_SMBUS_BYTE, &data);
|
|
|
- return (status < 0) ? status : data.byte;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_read_byte);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_write_byte - SMBus "send byte" protocol
|
|
|
- * @client: Handle to slave device
|
|
|
- * @value: Byte to be sent
|
|
|
- *
|
|
|
- * This executes the SMBus "send byte" protocol, returning negative errno
|
|
|
- * else zero on success.
|
|
|
- */
|
|
|
-s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
|
|
|
-{
|
|
|
- return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_write_byte);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_read_byte_data - SMBus "read byte" protocol
|
|
|
- * @client: Handle to slave device
|
|
|
- * @command: Byte interpreted by slave
|
|
|
- *
|
|
|
- * This executes the SMBus "read byte" protocol, returning negative errno
|
|
|
- * else a data byte received from the device.
|
|
|
- */
|
|
|
-s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
- int status;
|
|
|
-
|
|
|
- status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_READ, command,
|
|
|
- I2C_SMBUS_BYTE_DATA, &data);
|
|
|
- return (status < 0) ? status : data.byte;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_read_byte_data);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_write_byte_data - SMBus "write byte" protocol
|
|
|
- * @client: Handle to slave device
|
|
|
- * @command: Byte interpreted by slave
|
|
|
- * @value: Byte being written
|
|
|
- *
|
|
|
- * This executes the SMBus "write byte" protocol, returning negative errno
|
|
|
- * else zero on success.
|
|
|
- */
|
|
|
-s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
|
|
|
- u8 value)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
- data.byte = value;
|
|
|
- return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_WRITE, command,
|
|
|
- I2C_SMBUS_BYTE_DATA, &data);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_write_byte_data);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_read_word_data - SMBus "read word" protocol
|
|
|
- * @client: Handle to slave device
|
|
|
- * @command: Byte interpreted by slave
|
|
|
- *
|
|
|
- * This executes the SMBus "read word" protocol, returning negative errno
|
|
|
- * else a 16-bit unsigned "word" received from the device.
|
|
|
- */
|
|
|
-s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
- int status;
|
|
|
-
|
|
|
- status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_READ, command,
|
|
|
- I2C_SMBUS_WORD_DATA, &data);
|
|
|
- return (status < 0) ? status : data.word;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_read_word_data);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_write_word_data - SMBus "write word" protocol
|
|
|
- * @client: Handle to slave device
|
|
|
- * @command: Byte interpreted by slave
|
|
|
- * @value: 16-bit "word" being written
|
|
|
- *
|
|
|
- * This executes the SMBus "write word" protocol, returning negative errno
|
|
|
- * else zero on success.
|
|
|
- */
|
|
|
-s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
|
|
|
- u16 value)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
- data.word = value;
|
|
|
- return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_WRITE, command,
|
|
|
- I2C_SMBUS_WORD_DATA, &data);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_write_word_data);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_read_block_data - SMBus "block read" protocol
|
|
|
- * @client: Handle to slave device
|
|
|
- * @command: Byte interpreted by slave
|
|
|
- * @values: Byte array into which data will be read; big enough to hold
|
|
|
- * the data returned by the slave. SMBus allows at most 32 bytes.
|
|
|
- *
|
|
|
- * This executes the SMBus "block read" protocol, returning negative errno
|
|
|
- * else the number of data bytes in the slave's response.
|
|
|
- *
|
|
|
- * Note that using this function requires that the client's adapter support
|
|
|
- * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
|
|
|
- * support this; its emulation through I2C messaging relies on a specific
|
|
|
- * mechanism (I2C_M_RECV_LEN) which may not be implemented.
|
|
|
- */
|
|
|
-s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
|
|
|
- u8 *values)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
- int status;
|
|
|
-
|
|
|
- status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_READ, command,
|
|
|
- I2C_SMBUS_BLOCK_DATA, &data);
|
|
|
- if (status)
|
|
|
- return status;
|
|
|
-
|
|
|
- memcpy(values, &data.block[1], data.block[0]);
|
|
|
- return data.block[0];
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_read_block_data);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_write_block_data - SMBus "block write" protocol
|
|
|
- * @client: Handle to slave device
|
|
|
- * @command: Byte interpreted by slave
|
|
|
- * @length: Size of data block; SMBus allows at most 32 bytes
|
|
|
- * @values: Byte array which will be written.
|
|
|
- *
|
|
|
- * This executes the SMBus "block write" protocol, returning negative errno
|
|
|
- * else zero on success.
|
|
|
- */
|
|
|
-s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
|
|
|
- u8 length, const u8 *values)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
-
|
|
|
- if (length > I2C_SMBUS_BLOCK_MAX)
|
|
|
- length = I2C_SMBUS_BLOCK_MAX;
|
|
|
- data.block[0] = length;
|
|
|
- memcpy(&data.block[1], values, length);
|
|
|
- return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_WRITE, command,
|
|
|
- I2C_SMBUS_BLOCK_DATA, &data);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_write_block_data);
|
|
|
-
|
|
|
-/* Returns the number of read bytes */
|
|
|
-s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
|
|
|
- u8 length, u8 *values)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
- int status;
|
|
|
-
|
|
|
- if (length > I2C_SMBUS_BLOCK_MAX)
|
|
|
- length = I2C_SMBUS_BLOCK_MAX;
|
|
|
- data.block[0] = length;
|
|
|
- status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_READ, command,
|
|
|
- I2C_SMBUS_I2C_BLOCK_DATA, &data);
|
|
|
- if (status < 0)
|
|
|
- return status;
|
|
|
-
|
|
|
- memcpy(values, &data.block[1], data.block[0]);
|
|
|
- return data.block[0];
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
|
|
|
-
|
|
|
-s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
|
|
|
- u8 length, const u8 *values)
|
|
|
-{
|
|
|
- union i2c_smbus_data data;
|
|
|
-
|
|
|
- if (length > I2C_SMBUS_BLOCK_MAX)
|
|
|
- length = I2C_SMBUS_BLOCK_MAX;
|
|
|
- data.block[0] = length;
|
|
|
- memcpy(data.block + 1, values, length);
|
|
|
- return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
|
|
|
- I2C_SMBUS_WRITE, command,
|
|
|
- I2C_SMBUS_I2C_BLOCK_DATA, &data);
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
|
|
|
-
|
|
|
-/* Simulate a SMBus command using the i2c protocol
|
|
|
- No checking of parameters is done! */
|
|
|
-static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
|
|
|
- unsigned short flags,
|
|
|
- char read_write, u8 command, int size,
|
|
|
- union i2c_smbus_data *data)
|
|
|
-{
|
|
|
- /* So we need to generate a series of msgs. In the case of writing, we
|
|
|
- need to use only one message; when reading, we need two. We initialize
|
|
|
- most things with sane defaults, to keep the code below somewhat
|
|
|
- simpler. */
|
|
|
- unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
|
|
|
- unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
|
|
|
- int num = read_write == I2C_SMBUS_READ ? 2 : 1;
|
|
|
- int i;
|
|
|
- u8 partial_pec = 0;
|
|
|
- int status;
|
|
|
- struct i2c_msg msg[2] = {
|
|
|
- {
|
|
|
- .addr = addr,
|
|
|
- .flags = flags,
|
|
|
- .len = 1,
|
|
|
- .buf = msgbuf0,
|
|
|
- }, {
|
|
|
- .addr = addr,
|
|
|
- .flags = flags | I2C_M_RD,
|
|
|
- .len = 0,
|
|
|
- .buf = msgbuf1,
|
|
|
- },
|
|
|
- };
|
|
|
-
|
|
|
- msgbuf0[0] = command;
|
|
|
- switch (size) {
|
|
|
- case I2C_SMBUS_QUICK:
|
|
|
- msg[0].len = 0;
|
|
|
- /* Special case: The read/write field is used as data */
|
|
|
- msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
|
|
|
- I2C_M_RD : 0);
|
|
|
- num = 1;
|
|
|
- break;
|
|
|
- case I2C_SMBUS_BYTE:
|
|
|
- if (read_write == I2C_SMBUS_READ) {
|
|
|
- /* Special case: only a read! */
|
|
|
- msg[0].flags = I2C_M_RD | flags;
|
|
|
- num = 1;
|
|
|
- }
|
|
|
- break;
|
|
|
- case I2C_SMBUS_BYTE_DATA:
|
|
|
- if (read_write == I2C_SMBUS_READ)
|
|
|
- msg[1].len = 1;
|
|
|
- else {
|
|
|
- msg[0].len = 2;
|
|
|
- msgbuf0[1] = data->byte;
|
|
|
- }
|
|
|
- break;
|
|
|
- case I2C_SMBUS_WORD_DATA:
|
|
|
- if (read_write == I2C_SMBUS_READ)
|
|
|
- msg[1].len = 2;
|
|
|
- else {
|
|
|
- msg[0].len = 3;
|
|
|
- msgbuf0[1] = data->word & 0xff;
|
|
|
- msgbuf0[2] = data->word >> 8;
|
|
|
- }
|
|
|
- break;
|
|
|
- case I2C_SMBUS_PROC_CALL:
|
|
|
- num = 2; /* Special case */
|
|
|
- read_write = I2C_SMBUS_READ;
|
|
|
- msg[0].len = 3;
|
|
|
- msg[1].len = 2;
|
|
|
- msgbuf0[1] = data->word & 0xff;
|
|
|
- msgbuf0[2] = data->word >> 8;
|
|
|
- break;
|
|
|
- case I2C_SMBUS_BLOCK_DATA:
|
|
|
- if (read_write == I2C_SMBUS_READ) {
|
|
|
- msg[1].flags |= I2C_M_RECV_LEN;
|
|
|
- msg[1].len = 1; /* block length will be added by
|
|
|
- the underlying bus driver */
|
|
|
- } else {
|
|
|
- msg[0].len = data->block[0] + 2;
|
|
|
- if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
|
|
|
- dev_err(&adapter->dev,
|
|
|
- "Invalid block write size %d\n",
|
|
|
- data->block[0]);
|
|
|
- return -EINVAL;
|
|
|
- }
|
|
|
- for (i = 1; i < msg[0].len; i++)
|
|
|
- msgbuf0[i] = data->block[i-1];
|
|
|
- }
|
|
|
- break;
|
|
|
- case I2C_SMBUS_BLOCK_PROC_CALL:
|
|
|
- num = 2; /* Another special case */
|
|
|
- read_write = I2C_SMBUS_READ;
|
|
|
- if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
|
|
|
- dev_err(&adapter->dev,
|
|
|
- "Invalid block write size %d\n",
|
|
|
- data->block[0]);
|
|
|
- return -EINVAL;
|
|
|
- }
|
|
|
- msg[0].len = data->block[0] + 2;
|
|
|
- for (i = 1; i < msg[0].len; i++)
|
|
|
- msgbuf0[i] = data->block[i-1];
|
|
|
- msg[1].flags |= I2C_M_RECV_LEN;
|
|
|
- msg[1].len = 1; /* block length will be added by
|
|
|
- the underlying bus driver */
|
|
|
- break;
|
|
|
- case I2C_SMBUS_I2C_BLOCK_DATA:
|
|
|
- if (read_write == I2C_SMBUS_READ) {
|
|
|
- msg[1].len = data->block[0];
|
|
|
- } else {
|
|
|
- msg[0].len = data->block[0] + 1;
|
|
|
- if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
|
|
|
- dev_err(&adapter->dev,
|
|
|
- "Invalid block write size %d\n",
|
|
|
- data->block[0]);
|
|
|
- return -EINVAL;
|
|
|
- }
|
|
|
- for (i = 1; i <= data->block[0]; i++)
|
|
|
- msgbuf0[i] = data->block[i];
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
|
|
|
- return -EOPNOTSUPP;
|
|
|
- }
|
|
|
-
|
|
|
- i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
|
|
|
- && size != I2C_SMBUS_I2C_BLOCK_DATA);
|
|
|
- if (i) {
|
|
|
- /* Compute PEC if first message is a write */
|
|
|
- if (!(msg[0].flags & I2C_M_RD)) {
|
|
|
- if (num == 1) /* Write only */
|
|
|
- i2c_smbus_add_pec(&msg[0]);
|
|
|
- else /* Write followed by read */
|
|
|
- partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
|
|
|
- }
|
|
|
- /* Ask for PEC if last message is a read */
|
|
|
- if (msg[num-1].flags & I2C_M_RD)
|
|
|
- msg[num-1].len++;
|
|
|
- }
|
|
|
-
|
|
|
- status = i2c_transfer(adapter, msg, num);
|
|
|
- if (status < 0)
|
|
|
- return status;
|
|
|
-
|
|
|
- /* Check PEC if last message is a read */
|
|
|
- if (i && (msg[num-1].flags & I2C_M_RD)) {
|
|
|
- status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
|
|
|
- if (status < 0)
|
|
|
- return status;
|
|
|
- }
|
|
|
-
|
|
|
- if (read_write == I2C_SMBUS_READ)
|
|
|
- switch (size) {
|
|
|
- case I2C_SMBUS_BYTE:
|
|
|
- data->byte = msgbuf0[0];
|
|
|
- break;
|
|
|
- case I2C_SMBUS_BYTE_DATA:
|
|
|
- data->byte = msgbuf1[0];
|
|
|
- break;
|
|
|
- case I2C_SMBUS_WORD_DATA:
|
|
|
- case I2C_SMBUS_PROC_CALL:
|
|
|
- data->word = msgbuf1[0] | (msgbuf1[1] << 8);
|
|
|
- break;
|
|
|
- case I2C_SMBUS_I2C_BLOCK_DATA:
|
|
|
- for (i = 0; i < data->block[0]; i++)
|
|
|
- data->block[i+1] = msgbuf1[i];
|
|
|
- break;
|
|
|
- case I2C_SMBUS_BLOCK_DATA:
|
|
|
- case I2C_SMBUS_BLOCK_PROC_CALL:
|
|
|
- for (i = 0; i < msgbuf1[0] + 1; i++)
|
|
|
- data->block[i] = msgbuf1[i];
|
|
|
- break;
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_xfer - execute SMBus protocol operations
|
|
|
- * @adapter: Handle to I2C bus
|
|
|
- * @addr: Address of SMBus slave on that bus
|
|
|
- * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
|
|
|
- * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
|
|
|
- * @command: Byte interpreted by slave, for protocols which use such bytes
|
|
|
- * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
|
|
|
- * @data: Data to be read or written
|
|
|
- *
|
|
|
- * This executes an SMBus protocol operation, and returns a negative
|
|
|
- * errno code else zero on success.
|
|
|
- */
|
|
|
-s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
|
|
|
- char read_write, u8 command, int protocol,
|
|
|
- union i2c_smbus_data *data)
|
|
|
-{
|
|
|
- unsigned long orig_jiffies;
|
|
|
- int try;
|
|
|
- s32 res;
|
|
|
-
|
|
|
- /* If enabled, the following two tracepoints are conditional on
|
|
|
- * read_write and protocol.
|
|
|
- */
|
|
|
- trace_smbus_write(adapter, addr, flags, read_write,
|
|
|
- command, protocol, data);
|
|
|
- trace_smbus_read(adapter, addr, flags, read_write,
|
|
|
- command, protocol);
|
|
|
-
|
|
|
- flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
|
|
|
-
|
|
|
- if (adapter->algo->smbus_xfer) {
|
|
|
- i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
|
|
|
-
|
|
|
- /* Retry automatically on arbitration loss */
|
|
|
- orig_jiffies = jiffies;
|
|
|
- for (res = 0, try = 0; try <= adapter->retries; try++) {
|
|
|
- res = adapter->algo->smbus_xfer(adapter, addr, flags,
|
|
|
- read_write, command,
|
|
|
- protocol, data);
|
|
|
- if (res != -EAGAIN)
|
|
|
- break;
|
|
|
- if (time_after(jiffies,
|
|
|
- orig_jiffies + adapter->timeout))
|
|
|
- break;
|
|
|
- }
|
|
|
- i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
|
|
|
-
|
|
|
- if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
|
|
|
- goto trace;
|
|
|
- /*
|
|
|
- * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
|
|
|
- * implement native support for the SMBus operation.
|
|
|
- */
|
|
|
- }
|
|
|
-
|
|
|
- res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
|
|
|
- command, protocol, data);
|
|
|
-
|
|
|
-trace:
|
|
|
- /* If enabled, the reply tracepoint is conditional on read_write. */
|
|
|
- trace_smbus_reply(adapter, addr, flags, read_write,
|
|
|
- command, protocol, data);
|
|
|
- trace_smbus_result(adapter, addr, flags, read_write,
|
|
|
- command, protocol, res);
|
|
|
-
|
|
|
- return res;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_xfer);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
|
|
|
- * @client: Handle to slave device
|
|
|
- * @command: Byte interpreted by slave
|
|
|
- * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
|
|
|
- * @values: Byte array into which data will be read; big enough to hold
|
|
|
- * the data returned by the slave. SMBus allows at most
|
|
|
- * I2C_SMBUS_BLOCK_MAX bytes.
|
|
|
- *
|
|
|
- * This executes the SMBus "block read" protocol if supported by the adapter.
|
|
|
- * If block read is not supported, it emulates it using either word or byte
|
|
|
- * read protocols depending on availability.
|
|
|
- *
|
|
|
- * The addresses of the I2C slave device that are accessed with this function
|
|
|
- * must be mapped to a linear region, so that a block read will have the same
|
|
|
- * effect as a byte read. Before using this function you must double-check
|
|
|
- * if the I2C slave does support exchanging a block transfer with a byte
|
|
|
- * transfer.
|
|
|
- */
|
|
|
-s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
|
|
|
- u8 command, u8 length, u8 *values)
|
|
|
-{
|
|
|
- u8 i = 0;
|
|
|
- int status;
|
|
|
-
|
|
|
- if (length > I2C_SMBUS_BLOCK_MAX)
|
|
|
- length = I2C_SMBUS_BLOCK_MAX;
|
|
|
-
|
|
|
- if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
|
|
|
- return i2c_smbus_read_i2c_block_data(client, command, length, values);
|
|
|
-
|
|
|
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
|
|
|
- return -EOPNOTSUPP;
|
|
|
-
|
|
|
- if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
|
|
|
- while ((i + 2) <= length) {
|
|
|
- status = i2c_smbus_read_word_data(client, command + i);
|
|
|
- if (status < 0)
|
|
|
- return status;
|
|
|
- values[i] = status & 0xff;
|
|
|
- values[i + 1] = status >> 8;
|
|
|
- i += 2;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- while (i < length) {
|
|
|
- status = i2c_smbus_read_byte_data(client, command + i);
|
|
|
- if (status < 0)
|
|
|
- return status;
|
|
|
- values[i] = status;
|
|
|
- i++;
|
|
|
- }
|
|
|
-
|
|
|
- return i;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
|
|
|
-
|
|
|
-#if IS_ENABLED(CONFIG_I2C_SLAVE)
|
|
|
-int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
|
|
|
-{
|
|
|
- int ret;
|
|
|
-
|
|
|
- if (!client || !slave_cb) {
|
|
|
- WARN(1, "insufficient data\n");
|
|
|
- return -EINVAL;
|
|
|
- }
|
|
|
-
|
|
|
- if (!(client->flags & I2C_CLIENT_SLAVE))
|
|
|
- dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
|
|
|
- __func__);
|
|
|
-
|
|
|
- if (!(client->flags & I2C_CLIENT_TEN)) {
|
|
|
- /* Enforce stricter address checking */
|
|
|
- ret = i2c_check_7bit_addr_validity_strict(client->addr);
|
|
|
- if (ret) {
|
|
|
- dev_err(&client->dev, "%s: invalid address\n", __func__);
|
|
|
- return ret;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!client->adapter->algo->reg_slave) {
|
|
|
- dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
|
|
|
- return -EOPNOTSUPP;
|
|
|
- }
|
|
|
-
|
|
|
- client->slave_cb = slave_cb;
|
|
|
-
|
|
|
- i2c_lock_adapter(client->adapter);
|
|
|
- ret = client->adapter->algo->reg_slave(client);
|
|
|
- i2c_unlock_adapter(client->adapter);
|
|
|
-
|
|
|
- if (ret) {
|
|
|
- client->slave_cb = NULL;
|
|
|
- dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
|
|
|
- }
|
|
|
-
|
|
|
- return ret;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(i2c_slave_register);
|
|
|
-
|
|
|
-int i2c_slave_unregister(struct i2c_client *client)
|
|
|
-{
|
|
|
- int ret;
|
|
|
-
|
|
|
- if (!client->adapter->algo->unreg_slave) {
|
|
|
- dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
|
|
|
- return -EOPNOTSUPP;
|
|
|
- }
|
|
|
-
|
|
|
- i2c_lock_adapter(client->adapter);
|
|
|
- ret = client->adapter->algo->unreg_slave(client);
|
|
|
- i2c_unlock_adapter(client->adapter);
|
|
|
-
|
|
|
- if (ret == 0)
|
|
|
- client->slave_cb = NULL;
|
|
|
- else
|
|
|
- dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
|
|
|
-
|
|
|
- return ret;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(i2c_slave_unregister);
|
|
|
-
|
|
|
-/**
|
|
|
- * i2c_detect_slave_mode - detect operation mode
|
|
|
- * @dev: The device owning the bus
|
|
|
- *
|
|
|
- * This checks the device nodes for an I2C slave by checking the address
|
|
|
- * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS
|
|
|
- * flag this means the device is configured to act as a I2C slave and it will
|
|
|
- * be listening at that address.
|
|
|
- *
|
|
|
- * Returns true if an I2C own slave address is detected, otherwise returns
|
|
|
- * false.
|
|
|
- */
|
|
|
-bool i2c_detect_slave_mode(struct device *dev)
|
|
|
-{
|
|
|
- if (IS_BUILTIN(CONFIG_OF) && dev->of_node) {
|
|
|
- struct device_node *child;
|
|
|
- u32 reg;
|
|
|
-
|
|
|
- for_each_child_of_node(dev->of_node, child) {
|
|
|
- of_property_read_u32(child, "reg", ®);
|
|
|
- if (reg & I2C_OWN_SLAVE_ADDRESS) {
|
|
|
- of_node_put(child);
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- } else if (IS_BUILTIN(CONFIG_ACPI) && ACPI_HANDLE(dev)) {
|
|
|
- dev_dbg(dev, "ACPI slave is not supported yet\n");
|
|
|
- }
|
|
|
- return false;
|
|
|
-}
|
|
|
-EXPORT_SYMBOL_GPL(i2c_detect_slave_mode);
|
|
|
-
|
|
|
-#endif
|
|
|
-
|
|
|
MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
|
|
|
MODULE_DESCRIPTION("I2C-Bus main module");
|
|
|
MODULE_LICENSE("GPL");
|