You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openwrt/target/linux/s3c24xx/patches-2.6.24/1029-hxd8-tsl256x.patch.patch

538 lines
15 KiB
Diff

From d238d3347d19a6df97670624bd0712e61b83b666 Mon Sep 17 00:00:00 2001
From: mokopatches <mokopatches@openmoko.org>
Date: Fri, 4 Apr 2008 11:34:17 +0100
Subject: [PATCH] hxd8-tsl256x.patch
---
drivers/i2c/chips/Kconfig | 10 ++
drivers/i2c/chips/Makefile | 1 +
drivers/i2c/chips/tsl256x.c | 310 +++++++++++++++++++++++++++++++++++++++++++
drivers/i2c/chips/tsl256x.h | 154 +++++++++++++++++++++
include/linux/i2c-id.h | 1 +
5 files changed, 476 insertions(+), 0 deletions(-)
create mode 100644 drivers/i2c/chips/tsl256x.c
create mode 100644 drivers/i2c/chips/tsl256x.h
diff --git a/drivers/i2c/chips/Kconfig b/drivers/i2c/chips/Kconfig
index c0011fc..bf0da3a 100644
--- a/drivers/i2c/chips/Kconfig
+++ b/drivers/i2c/chips/Kconfig
@@ -174,4 +174,14 @@ config MENELAUS
and other features that are often used in portable devices like
cell phones and PDAs.
+config SENSORS_TSL256X
+ tristate "Texas TSL256X Ambient Light Sensor"
+ depends on I2C
+ help
+ If you say yes here you get support for the Texas TSL256X
+ ambient light sensor chip.
+
+ This driver can also be built as a module. If so, the module
+ will be called tsl256x.
+
endmenu
diff --git a/drivers/i2c/chips/Makefile b/drivers/i2c/chips/Makefile
index 864e6cf..a4772c7 100644
--- a/drivers/i2c/chips/Makefile
+++ b/drivers/i2c/chips/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_ISP1301_OMAP) += isp1301_omap.o
obj-$(CONFIG_TPS65010) += tps65010.o
obj-$(CONFIG_MENELAUS) += menelaus.o
obj-$(CONFIG_SENSORS_TSL2550) += tsl2550.o
+obj-$(CONFIG_SENSORS_TSL256X) += tsl256x.o
ifeq ($(CONFIG_I2C_DEBUG_CHIP),y)
EXTRA_CFLAGS += -DDEBUG
diff --git a/drivers/i2c/chips/tsl256x.c b/drivers/i2c/chips/tsl256x.c
new file mode 100644
index 0000000..455ec5e
--- /dev/null
+++ b/drivers/i2c/chips/tsl256x.c
@@ -0,0 +1,310 @@
+/*
+ * tsl256x.c -- TSL256x Light Sensor driver
+ *
+ * Copyright 2007 by Fiwin.
+ * Author: Alec Tsai <alec_tsai@fiwin.com.tw>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This I2C client driver refers to pcf50606.c.
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/i2c.h>
+#include <linux/types.h>
+#include <linux/input.h>
+
+#include "tsl256x.h"
+
+static unsigned short normal_i2c[] = { 0x39, I2C_CLIENT_END };
+/* Magic definition of all other variables and things */
+I2C_CLIENT_INSMOD;
+
+struct tsl256x_data {
+ struct i2c_client client;
+ struct mutex lock;
+ struct input_dev *input_dev;
+};
+
+static struct i2c_driver tsl256x_driver;
+
+/******************************************************************************
+ * Low-Level routines
+ *****************************************************************************/
+static inline int __reg_write(struct tsl256x_data *tsl, u_int8_t reg,
+ u_int8_t val)
+{
+ return i2c_smbus_write_byte_data(&tsl->client, reg, val);
+}
+
+static int reg_write(struct tsl256x_data *tsl, u_int8_t reg, u_int8_t val)
+{
+ int ret;
+
+ mutex_lock(&tsl->lock);
+ ret = __reg_write(tsl, reg, val);
+ mutex_unlock(&tsl->lock);
+
+ return ret;
+}
+
+static inline int32_t __reg_read(struct tsl256x_data *tsl, u_int8_t reg)
+{
+ int32_t ret;
+
+ ret = i2c_smbus_read_byte_data(&tsl->client, reg);
+
+ return ret;
+}
+
+static u_int8_t reg_read(struct tsl256x_data *tsl, u_int8_t reg)
+{
+ int32_t ret;
+
+ mutex_lock(&tsl->lock);
+ ret = __reg_read(tsl, reg);
+ mutex_unlock(&tsl->lock);
+
+ return ret & 0xff;
+}
+
+u_int32_t calculate_lux(u_int32_t iGain, u_int32_t iType, u_int32_t ch0,
+ u_int32_t ch1)
+{
+ u_int32_t channel0 = ch0 * 636 / 10;
+ u_int32_t channel1 = ch1 * 636 / 10;
+ u_int32_t lux_value = 0;
+ u_int32_t ratio = (channel1 * (2^RATIO_SCALE)) / channel0;
+ u_int32_t b = 0, m = 0;
+
+ if (0 == ch0)
+ return 0;
+ else {
+ if (ratio > (13 * (2^RATIO_SCALE) / 10))
+ return 0;
+ }
+
+ switch (iType) {
+ case 0: // T package
+ if ((ratio >= 0) && (ratio <= K1T)) {
+ b = B1T;
+ m = M1T;
+ } else if (ratio <= K2T) {
+ b = B2T;
+ m = M2T;
+ } else if (ratio <= K3T) {
+ b = B3T;
+ m = M3T;
+ } else if (ratio <= K4T) {
+ b = B4T;
+ m = M4T;
+ } else if (ratio <= K5T) {
+ b = B5T;
+ m = M5T;
+ } else if (ratio <= K6T) {
+ b = B6T;
+ m = M6T;
+ } else if (ratio <= K7T) {
+ b = B7T;
+ m = M7T;
+ } else if (ratio > K8T) {
+ b = B8T;
+ m = M8T;
+ }
+ break;
+ case 1:// CS package
+ if ((ratio >= 0) && (ratio <= K1C)) {
+ b = B1C;
+ m = M1C;
+ } else if (ratio <= K2C) {
+ b = B2C;
+ m = M2C;
+ } else if (ratio <= K3C) {
+ b = B3C;
+ m = M3C;
+ } else if (ratio <= K4C) {
+ b = B4C;
+ m = M4C;
+ } else if (ratio <= K5C) {
+ b = B5C;
+ m = M5C;
+ } else if (ratio <= K6C) {
+ b = B6C;
+ m = M6C;
+ } else if (ratio <= K7C) {
+ b = B7C;
+ m = M7C;
+ } else if (ratio > K8C) {
+ b = B8C;
+ m = M8C;
+ }
+ break;
+ default:
+ return 0;
+ break;
+ }
+
+ lux_value = ((channel0 * b) - (channel1 * m)) / 16384;
+ return(lux_value);
+}
+
+static ssize_t tsl256x_show_light_lux(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct tsl256x_data *tsl = i2c_get_clientdata(client);
+ u_int8_t low_byte_of_ch0 = 0, high_byte_of_ch0 = 0;
+ u_int8_t low_byte_of_ch1 = 0, high_byte_of_ch1 = 0;
+ u_int32_t adc_value_ch0, adc_value_ch1, adc_value;
+
+ low_byte_of_ch0 = reg_read(tsl, TSL256X_REG_DATA0LOW);
+ high_byte_of_ch0 = reg_read(tsl, TSL256X_REG_DATA0HIGH);
+ low_byte_of_ch1 = reg_read(tsl, TSL256X_REG_DATA1LOW);
+ high_byte_of_ch1 = reg_read(tsl, TSL256X_REG_DATA1HIGH);
+
+ adc_value_ch0 = (high_byte_of_ch0 * 256 + low_byte_of_ch0) * 16;
+ adc_value_ch1 = (high_byte_of_ch1 * 256 + low_byte_of_ch1) * 16;
+
+ adc_value = calculate_lux(0, 0, adc_value_ch0, adc_value_ch1);
+ return sprintf(buf, "%d\n", adc_value);
+}
+
+static DEVICE_ATTR(light_lux, S_IRUGO, tsl256x_show_light_lux, NULL);
+
+static int tsl256x_detect(struct i2c_adapter *adapter, int address, int kind)
+{
+ struct i2c_client *new_client = NULL;
+ struct tsl256x_data *tsl256x = NULL;
+ u_int8_t id = 0;
+ int res = 0;
+
+ if (!(tsl256x = kzalloc(sizeof(*tsl256x), GFP_KERNEL)))
+ return -ENOMEM;
+
+ mutex_init(&tsl256x->lock);
+ new_client = &tsl256x->client;
+ i2c_set_clientdata(new_client, tsl256x);
+ new_client->addr = address;
+ new_client->adapter = adapter;
+ new_client->driver = &tsl256x_driver;
+ new_client->flags = 0;
+ strlcpy(new_client->name, "tsl256x", I2C_NAME_SIZE);
+
+ /* now we try to detect the chip */
+ /* register with i2c core */
+ res = i2c_attach_client(new_client);
+ if (res) {
+ printk(KERN_DEBUG "[%s]Error: during i2c_attach_client()\n",
+ new_client->name);
+ goto exit_free;
+ } else {
+ printk(KERN_INFO "TSL256X is attached to I2C bus.\n");
+ }
+
+ /* Configure TSL256X. */
+ {
+ /* Power up TSL256X. */
+ reg_write(tsl256x, TSL256X_REG_CONTROL, 0x03);
+
+ /* Check TSL256X ID. */
+ id = reg_read(tsl256x, TSL256X_REG_ID);
+ if (TSL2561_ID == (id & 0xF0)) {
+ /* Configuring the Timing Register.
+ High Gain (16x), integration time of 101ms. */
+ reg_write(tsl256x, TSL256X_REG_TIMING, 0x11);
+ } else {
+ goto exit_free;
+ }
+ }
+
+ res = device_create_file(&new_client->dev, &dev_attr_light_lux);
+ if (res)
+ goto exit_detach;
+
+ return 0;
+
+exit_free:
+ kfree(tsl256x);
+ return res;
+exit_detach:
+ i2c_detach_client(new_client);
+ return res;
+}
+
+static int tsl256x_attach_adapter(struct i2c_adapter *adapter)
+{
+ return i2c_probe(adapter, &addr_data, &tsl256x_detect);
+}
+
+static int tsl256x_detach_client(struct i2c_client *client)
+{
+ struct tsl256x_data *tsl256x = i2c_get_clientdata(client);
+
+ printk(KERN_INFO "Detach TSL256X from I2C bus.\n");
+
+ /* Power down TSL256X. */
+ reg_write(tsl256x, TSL256X_REG_CONTROL, 0x00);
+
+ device_remove_file(&client->dev, &dev_attr_light_lux);
+ kfree(tsl256x);
+
+ return 0;
+}
+
+#ifdef CONFIG_PM
+static int tsl256x_suspend(struct device *dev, pm_message_t state)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct tsl256x_data *tsl256x = i2c_get_clientdata(client);
+
+ /* Power down TSL256X. */
+ reg_write(tsl256x, TSL256X_REG_CONTROL, 0x00);
+
+ return 0;
+}
+
+static int tsl256x_resume(struct device *dev)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct tsl256x_data *tsl256x = i2c_get_clientdata(client);
+
+ /* Power up TSL256X. */
+ reg_write(tsl256x, TSL256X_REG_CONTROL, 0x03);
+
+ return 0;
+}
+#endif
+
+static struct i2c_driver tsl256x_driver = {
+ .driver = {
+ .name = "tsl256x",
+ .owner = THIS_MODULE,
+#ifdef CONFIG_PM
+ .suspend = tsl256x_suspend,
+ .resume = tsl256x_resume,
+#endif
+ },
+ .id = I2C_DRIVERID_TSL256X,
+ .attach_adapter = tsl256x_attach_adapter,
+ .detach_client = tsl256x_detach_client,
+};
+
+static int __init tsl256x_init(void)
+{
+ return i2c_add_driver(&tsl256x_driver);
+}
+
+static void __exit tsl256x_exit(void)
+{
+ i2c_del_driver(&tsl256x_driver);
+}
+
+MODULE_AUTHOR("Alec Tsai <alec_tsai@fiwin.com.tw>");
+MODULE_LICENSE("GPL");
+
+module_init(tsl256x_init);
+module_exit(tsl256x_exit);
+
diff --git a/drivers/i2c/chips/tsl256x.h b/drivers/i2c/chips/tsl256x.h
new file mode 100644
index 0000000..88de0c6
--- /dev/null
+++ b/drivers/i2c/chips/tsl256x.h
@@ -0,0 +1,154 @@
+/*
+ * tsl256x.h -- TSL256x Light Sensor driver
+ *
+ * Copyright 2007 by Fiwin.
+ * Author: Alec Tsai <alec_tsai@fiwin.com.tw>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * The contents of header file is copied from TSL256x Datasheet.
+ */
+
+#ifndef _TSL256X_H
+#define _TSL256X_H
+
+#define TSL2560_ID 0x00
+#define TSL2561_ID 0x10
+
+#define LUX_SCALE 14 /* scale by 2^14 */
+#define RATIO_SCALE 9 /*scale ratio by 2^9 */
+
+/******************************************************************************
+ * Integration time scaling factors
+ *****************************************************************************/
+#define CH_SCALE 10 /* scale channel values by 2^10 */
+#define CHSCALE_TINT0 0x7517 /* 322/11 * 2^CH_SCALE */
+#define CHSCALE_TINT1 0x0fe7 /* 322/81 * 2^CH_SCALE */
+
+/******************************************************************************
+ * T Package coefficients
+ *****************************************************************************/
+/*
+ * For Ch1/Ch0=0.00 to 0.50
+ * Lux/Ch0=0.0304.0.062*((Ch1/Ch0)^1.4)
+ * piecewise approximation
+ * For Ch1/Ch0=0.00 to 0.125:
+ * Lux/Ch0=0.0304.0.0272*(Ch1/Ch0)
+ *
+ * For Ch1/Ch0=0.125 to 0.250:
+ * Lux/Ch0=0.0325.0.0440*(Ch1/Ch0)
+ *
+ * For Ch1/Ch0=0.250 to 0.375:
+ * Lux/Ch0=0.0351.0.0544*(Ch1/Ch0)
+ *
+ * For Ch1/Ch0=0.375 to 0.50:
+ * Lux/Ch0=0.0381.0.0624*(Ch1/Ch0)
+ *
+ * For Ch1/Ch0=0.50 to 0.61:
+ * Lux/Ch0=0.0224.0.031*(Ch1/Ch0)
+ *
+ * For Ch1/Ch0=0.61 to 0.80:
+ * Lux/Ch0=0.0128.0.0153*(Ch1/Ch0)
+ *
+ * For Ch1/Ch0=0.80 to 1.30:
+ * Lux/Ch0=0.00146.0.00112*(Ch1/Ch0)
+ *
+ * For Ch1/Ch0>1.3:
+ * Lux/Ch0=0
+ */
+#define K1T 0x0040 /* 0.125 * 2^RATIO_SCALE */
+#define B1T 0x01f2 /* 0.0304 * 2^LUX_SCALE */
+#define M1T 0x01be /* 0.0272 * 2^LUX_SCALE */
+#define K2T 0x0080 /* 0.250 * 2^RATIO_SCALE */
+#define B2T 0x0214 /* 0.0325 * 2^LUX_SCALE */
+#define M2T 0x02d1 /* 0.0440 * 2^LUX_SCALE */
+#define K3T 0x00c0 /* 0.375 * 2^RATIO_SCALE */
+#define B3T 0x023f /* 0.0351 * 2^LUX_SCALE */
+#define M3T 0x037b /* 0.0544 * 2^LUX_SCALE */
+#define K4T 0x0100 /* 0.50 * 2^RATIO_SCALE */
+#define B4T 0x0270 /* 0.0381 * 2^LUX_SCALE */
+#define M4T 0x03fe /* 0.0624 * 2^LUX_SCALE */
+#define K5T 0x0138 /* 0.61 * 2^RATIO_SCALE */
+#define B5T 0x016f /* 0.0224 * 2^LUX_SCALE */
+#define M5T 0x01fc /* 0.0310 * 2^LUX_SCALE */
+#define K6T 0x019a /* 0.80 * 2^RATIO_SCALE */
+#define B6T 0x00d2 /* 0.0128 * 2^LUX_SCALE */
+#define M6T 0x00fb /* 0.0153 * 2^LUX_SCALE */
+#define K7T 0x029a /* 1.3 * 2^RATIO_SCALE */
+#define B7T 0x0018 /* 0.00146 * 2^LUX_SCALE */
+#define M7T 0x0012 /* 0.00112 * 2^LUX_SCALE */
+#define K8T 0x029a /* 1.3 * 2^RATIO_SCALE */
+#define B8T 0x0000 /* 0.000 * 2^LUX_SCALE */
+#define M8T 0x0000 /* 0.000 * 2^LUX_SCALE */
+
+/******************************************************************************
+ * CS package coefficients
+ *****************************************************************************/
+/*
+ * For 0 <= Ch1/Ch0 <= 0.52
+ * Lux/Ch0 = 0.0315.0.0593*((Ch1/Ch0)^1.4)
+ * piecewise approximation
+ * For 0 <= Ch1/Ch0 <= 0.13
+ * Lux/Ch0 = 0.0315.0.0262*(Ch1/Ch0)
+ * For 0.13 <= Ch1/Ch0 <= 0.26
+ * Lux/Ch0 = 0.0337.0.0430*(Ch1/Ch0)
+ * For 0.26 <= Ch1/Ch0 <= 0.39
+ * Lux/Ch0 = 0.0363.0.0529*(Ch1/Ch0)
+ * For 0.39 <= Ch1/Ch0 <= 0.52
+ * Lux/Ch0 = 0.0392.0.0605*(Ch1/Ch0)
+ * For 0.52 < Ch1/Ch0 <= 0.65
+ * Lux/Ch0 = 0.0229.0.0291*(Ch1/Ch0)
+ * For 0.65 < Ch1/Ch0 <= 0.80
+ * Lux/Ch0 = 0.00157.0.00180*(Ch1/Ch0)
+ * For 0.80 < Ch1/Ch0 <= 1.30
+ * Lux/Ch0 = 0.00338.0.00260*(Ch1/Ch0)
+ * For Ch1/Ch0 > 1.30
+ * Lux = 0
+ */
+#define K1C 0x0043 /* 0.130 * 2^RATIO_SCALE */
+#define B1C 0x0204 /* 0.0315 * 2^LUX_SCALE */
+#define M1C 0x01ad /* 0.0262 * 2^LUX_SCALE */
+#define K2C 0x0085 /* 0.260 * 2^RATIO_SCALE */
+#define B2C 0x0228 /* 0.0337 * 2^LUX_SCALE */
+#define M2C 0x02c1 /* 0.0430 * 2^LUX_SCALE */
+#define K3C 0x00c8 /* 0.390 * 2^RATIO_SCALE */
+#define B3C 0x0253 /* 0.0363 * 2^LUX_SCALE */
+#define M3C 0x0363 /* 0.0529 * 2^LUX_SCALE */
+#define K4C 0x010a /* 0.520 * 2^RATIO_SCALE */
+#define B4C 0x0282 /* 0.0392 * 2^LUX_SCALE */
+#define M4C 0x03df /* 0.0605 * 2^LUX_SCALE */
+#define K5C 0x014d /* 0.65 * 2^RATIO_SCALE */
+#define B5C 0x0177 /* 0.0229 * 2^LUX_SCALE */
+#define M5C 0x01dd /* 0.0291 * 2^LUX_SCALE */
+#define K6C 0x019a /* 0.80 * 2^RATIO_SCALE */
+#define B6C 0x0101 /* 0.0157 * 2^LUX_SCALE */
+#define M6C 0x0127 /* 0.0180 * 2^LUX_SCALE */
+#define K7C 0x029a /* 1.3 * 2^RATIO_SCALE */
+#define B7C 0x0037 /* 0.00338 * 2^LUX_SCALE */
+#define M7C 0x002b /* 0.00260 * 2^LUX_SCALE */
+#define K8C 0x029a /* 1.3 * 2^RATIO_SCALE */
+#define B8C 0x0000 /* 0.000 * 2^LUX_SCALE */
+#define M8C 0x0000 /* 0.000 * 2^LUX_SCALE */
+
+/* TSL256x registers definition . */
+enum tsl256x_regs {
+ TSL256X_REG_CONTROL = 0x80, /* Control of basic functions */
+ TSL256X_REG_TIMING = 0x81, /* Integration time/gain control */
+ TSL256X_REG_THRESHLOWLOW = 0x82, /* Low byte of low interrupt threshold */
+ TSL256X_REG_THRESHLOWHIGH = 0x83, /* High byte of low interrupt threshold */
+ TSL256X_REG_THRESHHIGHLOW = 0x84, /* Low byte of high interrupt threshold */
+ TSL256X_REG_THRESHHIGHHIGH = 0x85, /* High byte of high interrupt threshold */
+ TSL256X_REG_INTERRUPT = 0x86, /* Interrupt control */
+ TSL256X_REG_CRC = 0x88, /* Factory test - not a user register */
+ TSL256X_REG_ID = 0x8A, /* Part number/ Rev ID */
+ TSL256X_REG_DATA0LOW = 0x8C, /* Low byte of ADC channel 0 */
+ TSL256X_REG_DATA0HIGH = 0x8D, /* High byte of ADC channel 0 */
+ TSL256X_REG_DATA1LOW = 0x8E, /* Low byte of ADC channel 1 */
+ TSL256X_REG_DATA1HIGH = 0x8F, /* High byte of ADC channel 1 */
+ __NUM_TSL256X_REGS
+};
+
+#endif /* _TSL256X_H */
+
diff --git a/include/linux/i2c-id.h b/include/linux/i2c-id.h
index 9b8ae78..a089554 100644
--- a/include/linux/i2c-id.h
+++ b/include/linux/i2c-id.h
@@ -168,6 +168,7 @@
#define I2C_DRIVERID_W83L785TS 1047
#define I2C_DRIVERID_OV7670 1048 /* Omnivision 7670 camera */
#define I2C_DRIVERID_PCF50606 1049
+#define I2C_DRIVERID_TSL256X 1050
/*
* ---- Adapter types ----------------------------------------------------
--
1.5.6.5