Hi list, Just want to send here to get reviewed, make sure I'm not making any major mistakes. Thanks.
BTW, I've tried to upstream this patch [1], and already got some good review there. But after that I've changed a lot, mainly backlight and i2c enumeration stuff. 1: https://lkml.org/lkml/2011/1/10/21 Kangkai >From fc281d00eda18e35f6640f12bc0939885fbf2c6b Mon Sep 17 00:00:00 2001 From: Yin Kangkai <[email protected]> Date: Tue, 22 Feb 2011 18:05:52 +0800 Subject: [PATCH RFC] platform/oaktrail: ACPI EC Extra driver for Oaktrail This driver implements an Extra ACPI EC driver for products based on Intel Oaktrail platform. This driver does below things: 1. exports 2 platform device attributes files in /sys/devices/platform/intel_oaktrail/: camera - Camera subsystem enabled: contains either 0 or 1. (rw) touchscreen - Touchscreen subsystem enabled: contains either 0 or 1. (ro) 2. registers itself in the Linux backlight control in /sys/class/backlight/intel_oaktrail/ 3. registers in the rfkill subsystem here: /sys/class/rfkill/rfkillX/ for these components: wifi, bluetooth, wwan (3g), gps 4. handle the ACPI enumeration of I2C client devices, and register those devices to I2C subsytem. Signed-off-by: Yin Kangkai <[email protected]> --- .../ABI/testing/sysfs-platform-intel-oaktrail | 13 + drivers/platform/x86/Kconfig | 9 + drivers/platform/x86/Makefile | 1 + drivers/platform/x86/intel_oaktrail.c | 637 ++++++++++++++++++++ 4 files changed, 660 insertions(+), 0 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-oaktrail create mode 100644 drivers/platform/x86/intel_oaktrail.c diff --git a/Documentation/ABI/testing/sysfs-platform-intel-oaktrail b/Documentation/ABI/testing/sysfs-platform-intel-oaktrail new file mode 100644 index 0000000..0512235 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-platform-intel-oaktrail @@ -0,0 +1,13 @@ +What: /sys/devices/platform/intel_oaktrail/camera +Date: Jan 2011 +KernelVersion: 2.6.37 +Contact: "Yin Kangkai" <[email protected]> +Description: + Control the camera. 1 means on, 0 means off. + +What: /sys/devices/platform/intel_oaktrail/touchscreen +Date: Jan 2011 +KernelVersion: 2.6.37 +Contact: "Yin Kangkai" <[email protected]> +Description: + Show the status of the touch screen. 1 means on, 0 means off. diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig index 2b4038a..61ede7f 100644 --- a/drivers/platform/x86/Kconfig +++ b/drivers/platform/x86/Kconfig @@ -655,4 +655,13 @@ config XO1_RFKILL Support for enabling/disabling the WLAN interface on the OLPC XO-1 laptop. +config INTEL_OAKTRAIL + tristate "Intel Oaktrail Platform Extras" + depends on ACPI + depends on RFKILL || RFKILL = n + ---help--- + Intel Oaktrail platform need this driver to provide interfaces to + enable/disable the Camera, WiFi, BT etc. devices. If in doubt, say Y + here; it will only load on supported platforms. + endif # X86_PLATFORM_DEVICES diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile index 7ff60e6..add8ab7 100644 --- a/drivers/platform/x86/Makefile +++ b/drivers/platform/x86/Makefile @@ -35,3 +35,4 @@ obj-$(CONFIG_INTEL_IPS) += intel_ips.o obj-$(CONFIG_GPIO_INTEL_PMIC) += intel_pmic_gpio.o obj-$(CONFIG_XO1_RFKILL) += xo1-rfkill.o obj-$(CONFIG_IBM_RTL) += ibm_rtl.o +obj-$(CONFIG_INTEL_OAKTRAIL) += intel_oaktrail.o diff --git a/drivers/platform/x86/intel_oaktrail.c b/drivers/platform/x86/intel_oaktrail.c new file mode 100644 index 0000000..9c725eb --- /dev/null +++ b/drivers/platform/x86/intel_oaktrail.c @@ -0,0 +1,637 @@ +/* + Copyright (C) 2010-2011 Intel Corporation + Author: Yin Kangkai ([email protected]) + + based on Compal driver + + Copyright (C) 2008 Cezary Jackiewicz <cezary.jackiewicz (at) gmail.com> + + based on MSI driver + + Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) 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. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + */ + +/* + * intel_oaktrail.c - Intel OakTrail Platform support. + * + * This driver does below things: + * 1. exports 2 platform device attributes files in + * /sys/devices/platform/intel_oaktrail/: + * camera - Camera subsystem enabled: contains either 0 or 1. (rw) + * touchscreen - Touchscreen subsystem enabled: contains either 0 or 1. (ro) + * + * 2. registers itself in the Linux backlight control in + * /sys/class/backlight/intel_oaktrail/ + * + * 3. registers in the rfkill subsystem here: /sys/class/rfkill/rfkillX/ + * for these components: wifi, bluetooth, wwan (3g), gps + * + * 4. handle the ACPI enumeration of I2C client devices, and register those + * devices to I2C subsytem. + * + * This driver might work on other products based on Oaktrail. If you + * want to try it you can pass force=1 as argument to the module which + * will force it to load even when the DMI data doesn't identify the + * product as compatible. + * + * version 0.4: + * handle ACPI enumeration of I2C client devices; + * version 0.3: + * add backlight control support; + * version 0.2: + * use the rfkill subsystem for wifi, bt, gps, wwan; + * version 0.1: + * initial version. + * + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/init.h> +#include <linux/acpi.h> +#include <linux/fb.h> +#include <linux/mutex.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/backlight.h> +#include <linux/platform_device.h> +#include <linux/dmi.h> +#include <linux/rfkill.h> +#include <acpi/acpi_bus.h> +#include <acpi/acpi_drivers.h> + + +#define DRIVER_NAME "intel_oaktrail" +#define DRIVER_VERSION "0.4" + +/* + * This is the devices status address in EC space, and the control bits + * definition: + * + * (1 << 0): Camera enable/disable, RW. + * (1 << 1): Bluetooth enable/disable, RW. + * (1 << 2): GPS enable/disable, RW. + * (1 << 3): WiFi enable/disable, RW. + * (1 << 4): WWAN (3G) enable/disalbe, RW. + * (1 << 5): Touchscreen enable/disable, Read Only. + */ +#define OT_EC_DEVICE_STATE_ADDRESS 0xD6 + +#define OT_EC_CAMERA_MASK (1 << 0) +#define OT_EC_BT_MASK (1 << 1) +#define OT_EC_GPS_MASK (1 << 2) +#define OT_EC_WIFI_MASK (1 << 3) +#define OT_EC_WWAN_MASK (1 << 4) +#define OT_EC_TS_MASK (1 << 5) + +/* + * This is the address in EC space and commands used to control LCD backlight: + * + * Two steps needed to change the LCD backlight: + * 1. write the backlight percentage into OT_EC_BL_BRIGHTNESS_ADDRESS; + * 2. write OT_EC_BL_CONTROL_ON_DATA into OT_EC_BL_CONTROL_ADDRESS. + * + * To read the LCD back light, just read out the value from + * OT_EC_BL_BRIGHTNESS_ADDRESS. + * + * LCD backlight brightness range: 0 - 100 (OT_EC_BL_BRIGHTNESS_MAX) + */ +#define OT_EC_BL_BRIGHTNESS_ADDRESS 0x44 +#define OT_EC_BL_BRIGHTNESS_MAX 100 +#define OT_EC_BL_CONTROL_ADDRESS 0x3A +#define OT_EC_BL_CONTROL_ON_DATA 0x1A + + +static int force; +module_param(force, bool, 0); +MODULE_PARM_DESC(force, "Force driver load, ignore DMI data"); + +static struct platform_device *oaktrail_device; +static struct backlight_device *oaktrail_bl_device; +static struct rfkill *bt_rfkill; +static struct rfkill *gps_rfkill; +static struct rfkill *wifi_rfkill; +static struct rfkill *wwan_rfkill; + + +/* platform device attributes */ +static ssize_t camera_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + u8 value; + + ec_read(OT_EC_DEVICE_STATE_ADDRESS, &value); + return sprintf(buf, "%d\n", ((value & OT_EC_CAMERA_MASK) != 0)); +} + +static ssize_t camera_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + int state; + u8 old_val; + + ec_read(OT_EC_DEVICE_STATE_ADDRESS, &old_val); + if (sscanf(buf, "%d", &state) != 1 || (state < 0 || state > 1)) + return -EINVAL; + ec_write(OT_EC_DEVICE_STATE_ADDRESS, state ? + (old_val | OT_EC_CAMERA_MASK) : + (old_val & ~OT_EC_CAMERA_MASK)); + + return count; +} +static DEVICE_ATTR(camera, 0644, camera_show, camera_store); + +static ssize_t touchscreen_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + u8 value; + + ec_read(OT_EC_DEVICE_STATE_ADDRESS, &value); + return sprintf(buf, "%d\n", ((value & OT_EC_TS_MASK) != 0)); +} +static DEVICE_ATTR(touchscreen, 0444, touchscreen_show, NULL); + +static struct attribute *oaktrail_attributes[] = { + &dev_attr_camera.attr, + &dev_attr_touchscreen.attr, + NULL +}; + +static struct attribute_group oaktrail_attribute_group = { + .attrs = oaktrail_attributes +}; + + +/* rfkill */ +static int oaktrail_rfkill_set(void *data, bool blocked) +{ + u8 value; + u8 result; + unsigned long radio = (unsigned long) data; + + ec_read(OT_EC_DEVICE_STATE_ADDRESS, &result); + + if (!blocked) + value = (u8) (result | radio); + else + value = (u8) (result & ~radio); + + ec_write(OT_EC_DEVICE_STATE_ADDRESS, value); + + return 0; +} + +static const struct rfkill_ops oaktrail_rfkill_ops = { + .set_block = oaktrail_rfkill_set, +}; + +static struct rfkill *oaktrail_rfkill_new(char *name, enum rfkill_type type, + unsigned long mask) +{ + struct rfkill *rfkill_dev; + u8 value; + int err; + + rfkill_dev = rfkill_alloc(name, &oaktrail_device->dev, type, + &oaktrail_rfkill_ops, (void *)mask); + if (!rfkill_dev) + return ERR_PTR(-ENOMEM); + + ec_read(OT_EC_DEVICE_STATE_ADDRESS, &value); + rfkill_init_sw_state(rfkill_dev, (value & mask) != 1); + + err = rfkill_register(rfkill_dev); + if (err) { + rfkill_destroy(rfkill_dev); + return ERR_PTR(err); + } + + return rfkill_dev; +} + +static inline void __oaktrail_rfkill_cleanup(struct rfkill *rf) +{ + if (rf) { + rfkill_unregister(rf); + rfkill_destroy(rf); + } +} + +static void oaktrail_rfkill_cleanup(void) +{ + __oaktrail_rfkill_cleanup(wifi_rfkill); + __oaktrail_rfkill_cleanup(bt_rfkill); + __oaktrail_rfkill_cleanup(gps_rfkill); + __oaktrail_rfkill_cleanup(wwan_rfkill); +} + +static int oaktrail_rfkill_init(void) +{ + int ret; + + wifi_rfkill = oaktrail_rfkill_new("oaktrail-wifi", + RFKILL_TYPE_WLAN, + OT_EC_WIFI_MASK); + if (IS_ERR(wifi_rfkill)) { + ret = PTR_ERR(wifi_rfkill); + wifi_rfkill = NULL; + goto cleanup; + } + + bt_rfkill = oaktrail_rfkill_new("oaktrail-bluetooth", + RFKILL_TYPE_BLUETOOTH, + OT_EC_BT_MASK); + if (IS_ERR(bt_rfkill)) { + ret = PTR_ERR(bt_rfkill); + bt_rfkill = NULL; + goto cleanup; + } + + gps_rfkill = oaktrail_rfkill_new("oaktrail-gps", + RFKILL_TYPE_GPS, + OT_EC_GPS_MASK); + if (IS_ERR(gps_rfkill)) { + ret = PTR_ERR(gps_rfkill); + gps_rfkill = NULL; + goto cleanup; + } + + wwan_rfkill = oaktrail_rfkill_new("oaktrail-wwan", + RFKILL_TYPE_WWAN, + OT_EC_WWAN_MASK); + if (IS_ERR(wwan_rfkill)) { + ret = PTR_ERR(wwan_rfkill); + wwan_rfkill = NULL; + goto cleanup; + } + + return 0; + +cleanup: + oaktrail_rfkill_cleanup(); + return ret; +} + + +/* backlight */ +static int get_backlight_brightness(struct backlight_device *b) +{ + u8 value; + ec_read(OT_EC_BL_BRIGHTNESS_ADDRESS, &value); + + return value; +} + +static int set_backlight_brightness(struct backlight_device *b) +{ + u8 percent = (u8) b->props.brightness; + if (percent < 0 || percent > OT_EC_BL_BRIGHTNESS_MAX) + return -EINVAL; + + ec_write(OT_EC_BL_BRIGHTNESS_ADDRESS, percent); + ec_write(OT_EC_BL_CONTROL_ADDRESS, OT_EC_BL_CONTROL_ON_DATA); + + return 0; +} + +static const struct backlight_ops oaktrail_bl_ops = { + .get_brightness = get_backlight_brightness, + .update_status = set_backlight_brightness, +}; + +static int oaktrail_backlight_init(void) +{ + struct backlight_device *bd; + struct backlight_properties props; + + memset(&props, 0, sizeof(struct backlight_properties)); + props.max_brightness = OT_EC_BL_BRIGHTNESS_MAX; + bd = backlight_device_register(DRIVER_NAME, + &oaktrail_device->dev, NULL, + &oaktrail_bl_ops, + &props); + + if (IS_ERR(bd)) { + oaktrail_bl_device = NULL; + pr_warning("Unable to register backlight device\n"); + return PTR_ERR(bd); + } + + oaktrail_bl_device = bd; + + bd->props.brightness = get_backlight_brightness(bd); + bd->props.power = FB_BLANK_UNBLANK; + backlight_update_status(bd); + + return 0; +} + +static void oaktrail_backlight_exit(void) +{ + if (oaktrail_bl_device) + backlight_device_unregister(oaktrail_bl_device); +} + +/* I2C clients */ +struct name_mapping { + char * acpi_hid; + char * device_name; +}; + +static const struct name_mapping i2c_device_names[] = { + { "SMO8601", "lsm303dlh_a" }, + #if 1 + { "COOLDEV", "bma150" }, + #endif + + /* other sensors not exposed by BIOS yet */ + #if 0 + { "SMO8601", "lsm303dlh_m" }, + { "xxx", "AK8975" }, + { "xxx", "L3G4200D" }, + { "xxx", "mpu3050" }, + #endif +}; + +static const struct acpi_device_id i2c_device_ids[] = { + { "SMO8601", 0}, + + /* other sensors not exposed by BIOS yet */ + { "", 0} +}; +MODULE_DEVICE_TABLE(acpi, i2c_device_ids); + + +struct oaktrail_i2c_node { + struct i2c_client *client; + struct oaktrail_i2c_node *next; +}; + +static struct oaktrail_i2c_node *oaktrail_i2c_list = NULL; +static struct mutex oaktrail_lock; /* lock for above list */ + +static int oaktrail_i2c_device_register(const char *hid, unsigned short addr) +{ + /* register sensors of oaktrail on bus 0 */ + struct i2c_adapter *i2c_adap; + struct i2c_client *client; + struct i2c_board_info i2c_info; + struct oaktrail_i2c_node *node; + int i; + + node = kzalloc(sizeof *node, GFP_KERNEL); + if (node == NULL) { + pr_err("No enough memory\n"); + return -ENOMEM; + } + + memset(&i2c_info, 0, sizeof(struct i2c_board_info)); + + for (i = 0; i < ARRAY_SIZE(i2c_device_names); i++) { + if (strncmp(i2c_device_names[i].acpi_hid, hid, + ACPI_ID_LEN) == 0) { + strlcpy(i2c_info.type, i2c_device_names[i].device_name, + I2C_NAME_SIZE); + break; + } + } + + if (strlen(i2c_info.type) == 0) + return -ENODEV; + + i2c_info.addr = addr; + + i2c_adap = i2c_get_adapter(0); + client = i2c_new_device(i2c_adap, &i2c_info); + i2c_put_adapter(i2c_adap); + + node->client = client; + + mutex_lock(&oaktrail_lock); + node->next = oaktrail_i2c_list; + oaktrail_i2c_list = node; + mutex_unlock(&oaktrail_lock); + + pr_info("i2c client device registered: hid: %s, type: %s, addr: 0x%x\n", + hid, i2c_info.type, addr); + return 0; +} + +static int oaktrail_i2c_device_add(struct acpi_device *device) +{ + const char *hid = acpi_device_hid(device); + acpi_status status = AE_OK; + unsigned long long addr; + + status = acpi_evaluate_integer(device->handle, "ICID", NULL, &addr); + if (ACPI_FAILURE(status)) + return -ENODEV; + + if (oaktrail_i2c_device_register(hid, (unsigned short)addr) < 0) + return -ENODEV; + +#if 1 + /* Add this bma150 temporary before BIOS exported it. */ + oaktrail_i2c_device_register("COOLDEV", 0x38); +#endif + + return 0; +} + +static int oaktrail_i2c_device_remove(struct acpi_device *device, int type) +{ + return 0; +} + +static struct acpi_driver oaktrail_i2c_driver = { + .name = "Oaktrail I2C", + .class = "Oaktrail", + .ids = i2c_device_ids, + .ops = { + .add = oaktrail_i2c_device_add, + .remove = oaktrail_i2c_device_remove, + }, + .owner =THIS_MODULE, +}; + + +static int __init oaktrail_i2c_init(void) +{ + int result = 0; + + mutex_init(&oaktrail_lock); + + result = acpi_bus_register_driver(&oaktrail_i2c_driver); + if (result < 0) { + ACPI_DEBUG_PRINT((ACPI_DB_ERROR, + "Error registering driver\n")); + return -ENODEV; + } + + return 0; +} + +static void __exit oaktrail_i2c_exit(void) +{ + struct oaktrail_i2c_node *node; + + mutex_lock(&oaktrail_lock); + node = oaktrail_i2c_list; + while (node) { + i2c_unregister_device(node->client); + oaktrail_i2c_list = node->next; + + kfree(node); + node = oaktrail_i2c_list; + } + mutex_unlock(&oaktrail_lock); + + acpi_bus_unregister_driver(&oaktrail_i2c_driver); +} + + +static int __devinit oaktrail_probe(struct platform_device *pdev) +{ + return sysfs_create_group(&pdev->dev.kobj, &oaktrail_attribute_group); +} + +static int __devexit oaktrail_remove(struct platform_device *pdev) +{ + sysfs_remove_group(&pdev->dev.kobj, &oaktrail_attribute_group); + + return 0; +} + +static struct platform_driver oaktrail_driver = { + .driver = { + .name = DRIVER_NAME, + .owner = THIS_MODULE, + }, + .probe = oaktrail_probe, + .remove = __devexit_p(oaktrail_remove) +}; + +static int dmi_check_cb(const struct dmi_system_id *id) +{ + pr_info("Identified model '%s'\n", id->ident); + return 0; +} + +static struct dmi_system_id __initdata oaktrail_dmi_table[] = { + { + .ident = "OakTrail platform", + .matches = { + DMI_MATCH(DMI_PRODUCT_NAME, "OakTrail platform"), + }, + .callback = dmi_check_cb + }, + { } +}; + +static int __init oaktrail_init(void) +{ + int ret; + + if (acpi_disabled) { + pr_err("ACPI needs to be enabled for this driver to work!\n"); + return -ENODEV; + } + + if (!force && !dmi_check_system(oaktrail_dmi_table)) { + pr_err("Platform not recognized (You could try the module's force-parameter)"); + return -ENODEV; + } + + ret = platform_driver_register(&oaktrail_driver); + if (ret) { + pr_warning("Unable to register platform driver\n"); + goto err_driver_reg; + } + + oaktrail_device = platform_device_alloc(DRIVER_NAME, -1); + if (!oaktrail_device) { + pr_warning("Unable to allocate platform device\n"); + ret = -ENOMEM; + goto err_device_alloc; + } + + ret = platform_device_add(oaktrail_device); + if (ret) { + pr_warning("Unable to add platform device\n"); + goto err_device_add; + } + + if (!acpi_video_backlight_support()) { + ret = oaktrail_backlight_init(); + if (ret) + goto err_backlight; + + } else + pr_info("Backlight controlled by ACPI video driver\n"); + + ret = oaktrail_rfkill_init(); + if (ret) { + pr_warning("Setup rfkill failed\n"); + goto err_rfkill; + } + + ret = oaktrail_i2c_init(); + if (ret) { + pr_warning("Setup i2c clients failed\n"); + goto err_i2c; + } + + pr_info("Driver "DRIVER_VERSION" successfully loaded\n"); + return 0; + +err_i2c: + oaktrail_rfkill_cleanup(); +err_rfkill: + oaktrail_backlight_exit(); +err_backlight: + platform_device_del(oaktrail_device); +err_device_add: + platform_device_put(oaktrail_device); +err_device_alloc: + platform_driver_unregister(&oaktrail_driver); +err_driver_reg: + + return ret; +} + +static void __exit oaktrail_cleanup(void) +{ + oaktrail_i2c_exit(); + oaktrail_backlight_exit(); + oaktrail_rfkill_cleanup(); + platform_device_unregister(oaktrail_device); + platform_driver_unregister(&oaktrail_driver); + + pr_info("Driver unloaded\n"); +} + +module_init(oaktrail_init); +module_exit(oaktrail_cleanup); + +MODULE_AUTHOR("Yin Kangkai ([email protected])"); +MODULE_DESCRIPTION("Intel Oaktrail Platform ACPI Extras"); +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("dmi:*:svnIntelCorporation:pnOakTrailplatform:*"); -- 1.7.2.3 _______________________________________________ MeeGo-kernel mailing list [email protected] http://lists.meego.com/listinfo/meego-kernel
