1 From d238d3347d19a6df97670624bd0712e61b83b666 Mon Sep 17 00:00:00 2001
2 From: mokopatches <mokopatches@openmoko.org>
3 Date: Fri, 4 Apr 2008 11:34:17 +0100
4 Subject: [PATCH] hxd8-tsl256x.patch
7 drivers/i2c/chips/Kconfig | 10 ++
8 drivers/i2c/chips/Makefile | 1 +
9 drivers/i2c/chips/tsl256x.c | 310 +++++++++++++++++++++++++++++++++++++++++++
10 drivers/i2c/chips/tsl256x.h | 154 +++++++++++++++++++++
11 include/linux/i2c-id.h | 1 +
12 5 files changed, 476 insertions(+), 0 deletions(-)
13 create mode 100644 drivers/i2c/chips/tsl256x.c
14 create mode 100644 drivers/i2c/chips/tsl256x.h
16 diff --git a/drivers/i2c/chips/Kconfig b/drivers/i2c/chips/Kconfig
17 index c0011fc..bf0da3a 100644
18 --- a/drivers/i2c/chips/Kconfig
19 +++ b/drivers/i2c/chips/Kconfig
20 @@ -174,4 +174,14 @@ config MENELAUS
21 and other features that are often used in portable devices like
24 +config SENSORS_TSL256X
25 + tristate "Texas TSL256X Ambient Light Sensor"
28 + If you say yes here you get support for the Texas TSL256X
29 + ambient light sensor chip.
31 + This driver can also be built as a module. If so, the module
32 + will be called tsl256x.
35 diff --git a/drivers/i2c/chips/Makefile b/drivers/i2c/chips/Makefile
36 index 864e6cf..a4772c7 100644
37 --- a/drivers/i2c/chips/Makefile
38 +++ b/drivers/i2c/chips/Makefile
39 @@ -16,6 +16,7 @@ obj-$(CONFIG_ISP1301_OMAP) += isp1301_omap.o
40 obj-$(CONFIG_TPS65010) += tps65010.o
41 obj-$(CONFIG_MENELAUS) += menelaus.o
42 obj-$(CONFIG_SENSORS_TSL2550) += tsl2550.o
43 +obj-$(CONFIG_SENSORS_TSL256X) += tsl256x.o
45 ifeq ($(CONFIG_I2C_DEBUG_CHIP),y)
46 EXTRA_CFLAGS += -DDEBUG
47 diff --git a/drivers/i2c/chips/tsl256x.c b/drivers/i2c/chips/tsl256x.c
49 index 0000000..455ec5e
51 +++ b/drivers/i2c/chips/tsl256x.c
54 + * tsl256x.c -- TSL256x Light Sensor driver
56 + * Copyright 2007 by Fiwin.
57 + * Author: Alec Tsai <alec_tsai@fiwin.com.tw>
59 + * This program is free software; you can redistribute it and/or modify
60 + * it under the terms of the GNU General Public License version 2 as
61 + * published by the Free Software Foundation.
63 + * This I2C client driver refers to pcf50606.c.
66 +#include <linux/module.h>
67 +#include <linux/init.h>
68 +#include <linux/i2c.h>
69 +#include <linux/types.h>
70 +#include <linux/input.h>
74 +static unsigned short normal_i2c[] = { 0x39, I2C_CLIENT_END };
75 +/* Magic definition of all other variables and things */
78 +struct tsl256x_data {
79 + struct i2c_client client;
81 + struct input_dev *input_dev;
84 +static struct i2c_driver tsl256x_driver;
86 +/******************************************************************************
87 + * Low-Level routines
88 + *****************************************************************************/
89 +static inline int __reg_write(struct tsl256x_data *tsl, u_int8_t reg,
92 + return i2c_smbus_write_byte_data(&tsl->client, reg, val);
95 +static int reg_write(struct tsl256x_data *tsl, u_int8_t reg, u_int8_t val)
99 + mutex_lock(&tsl->lock);
100 + ret = __reg_write(tsl, reg, val);
101 + mutex_unlock(&tsl->lock);
106 +static inline int32_t __reg_read(struct tsl256x_data *tsl, u_int8_t reg)
110 + ret = i2c_smbus_read_byte_data(&tsl->client, reg);
115 +static u_int8_t reg_read(struct tsl256x_data *tsl, u_int8_t reg)
119 + mutex_lock(&tsl->lock);
120 + ret = __reg_read(tsl, reg);
121 + mutex_unlock(&tsl->lock);
126 +u_int32_t calculate_lux(u_int32_t iGain, u_int32_t iType, u_int32_t ch0,
129 + u_int32_t channel0 = ch0 * 636 / 10;
130 + u_int32_t channel1 = ch1 * 636 / 10;
131 + u_int32_t lux_value = 0;
132 + u_int32_t ratio = (channel1 * (2^RATIO_SCALE)) / channel0;
133 + u_int32_t b = 0, m = 0;
138 + if (ratio > (13 * (2^RATIO_SCALE) / 10))
143 + case 0: // T package
144 + if ((ratio >= 0) && (ratio <= K1T)) {
147 + } else if (ratio <= K2T) {
150 + } else if (ratio <= K3T) {
153 + } else if (ratio <= K4T) {
156 + } else if (ratio <= K5T) {
159 + } else if (ratio <= K6T) {
162 + } else if (ratio <= K7T) {
165 + } else if (ratio > K8T) {
170 + case 1:// CS package
171 + if ((ratio >= 0) && (ratio <= K1C)) {
174 + } else if (ratio <= K2C) {
177 + } else if (ratio <= K3C) {
180 + } else if (ratio <= K4C) {
183 + } else if (ratio <= K5C) {
186 + } else if (ratio <= K6C) {
189 + } else if (ratio <= K7C) {
192 + } else if (ratio > K8C) {
202 + lux_value = ((channel0 * b) - (channel1 * m)) / 16384;
206 +static ssize_t tsl256x_show_light_lux(struct device *dev,
207 + struct device_attribute *attr, char *buf)
209 + struct i2c_client *client = to_i2c_client(dev);
210 + struct tsl256x_data *tsl = i2c_get_clientdata(client);
211 + u_int8_t low_byte_of_ch0 = 0, high_byte_of_ch0 = 0;
212 + u_int8_t low_byte_of_ch1 = 0, high_byte_of_ch1 = 0;
213 + u_int32_t adc_value_ch0, adc_value_ch1, adc_value;
215 + low_byte_of_ch0 = reg_read(tsl, TSL256X_REG_DATA0LOW);
216 + high_byte_of_ch0 = reg_read(tsl, TSL256X_REG_DATA0HIGH);
217 + low_byte_of_ch1 = reg_read(tsl, TSL256X_REG_DATA1LOW);
218 + high_byte_of_ch1 = reg_read(tsl, TSL256X_REG_DATA1HIGH);
220 + adc_value_ch0 = (high_byte_of_ch0 * 256 + low_byte_of_ch0) * 16;
221 + adc_value_ch1 = (high_byte_of_ch1 * 256 + low_byte_of_ch1) * 16;
223 + adc_value = calculate_lux(0, 0, adc_value_ch0, adc_value_ch1);
224 + return sprintf(buf, "%d\n", adc_value);
227 +static DEVICE_ATTR(light_lux, S_IRUGO, tsl256x_show_light_lux, NULL);
229 +static int tsl256x_detect(struct i2c_adapter *adapter, int address, int kind)
231 + struct i2c_client *new_client = NULL;
232 + struct tsl256x_data *tsl256x = NULL;
236 + if (!(tsl256x = kzalloc(sizeof(*tsl256x), GFP_KERNEL)))
239 + mutex_init(&tsl256x->lock);
240 + new_client = &tsl256x->client;
241 + i2c_set_clientdata(new_client, tsl256x);
242 + new_client->addr = address;
243 + new_client->adapter = adapter;
244 + new_client->driver = &tsl256x_driver;
245 + new_client->flags = 0;
246 + strlcpy(new_client->name, "tsl256x", I2C_NAME_SIZE);
248 + /* now we try to detect the chip */
249 + /* register with i2c core */
250 + res = i2c_attach_client(new_client);
252 + printk(KERN_DEBUG "[%s]Error: during i2c_attach_client()\n",
256 + printk(KERN_INFO "TSL256X is attached to I2C bus.\n");
259 + /* Configure TSL256X. */
261 + /* Power up TSL256X. */
262 + reg_write(tsl256x, TSL256X_REG_CONTROL, 0x03);
264 + /* Check TSL256X ID. */
265 + id = reg_read(tsl256x, TSL256X_REG_ID);
266 + if (TSL2561_ID == (id & 0xF0)) {
267 + /* Configuring the Timing Register.
268 + High Gain (16x), integration time of 101ms. */
269 + reg_write(tsl256x, TSL256X_REG_TIMING, 0x11);
275 + res = device_create_file(&new_client->dev, &dev_attr_light_lux);
285 + i2c_detach_client(new_client);
289 +static int tsl256x_attach_adapter(struct i2c_adapter *adapter)
291 + return i2c_probe(adapter, &addr_data, &tsl256x_detect);
294 +static int tsl256x_detach_client(struct i2c_client *client)
296 + struct tsl256x_data *tsl256x = i2c_get_clientdata(client);
298 + printk(KERN_INFO "Detach TSL256X from I2C bus.\n");
300 + /* Power down TSL256X. */
301 + reg_write(tsl256x, TSL256X_REG_CONTROL, 0x00);
303 + device_remove_file(&client->dev, &dev_attr_light_lux);
310 +static int tsl256x_suspend(struct device *dev, pm_message_t state)
312 + struct i2c_client *client = to_i2c_client(dev);
313 + struct tsl256x_data *tsl256x = i2c_get_clientdata(client);
315 + /* Power down TSL256X. */
316 + reg_write(tsl256x, TSL256X_REG_CONTROL, 0x00);
321 +static int tsl256x_resume(struct device *dev)
323 + struct i2c_client *client = to_i2c_client(dev);
324 + struct tsl256x_data *tsl256x = i2c_get_clientdata(client);
326 + /* Power up TSL256X. */
327 + reg_write(tsl256x, TSL256X_REG_CONTROL, 0x03);
333 +static struct i2c_driver tsl256x_driver = {
336 + .owner = THIS_MODULE,
338 + .suspend = tsl256x_suspend,
339 + .resume = tsl256x_resume,
342 + .id = I2C_DRIVERID_TSL256X,
343 + .attach_adapter = tsl256x_attach_adapter,
344 + .detach_client = tsl256x_detach_client,
347 +static int __init tsl256x_init(void)
349 + return i2c_add_driver(&tsl256x_driver);
352 +static void __exit tsl256x_exit(void)
354 + i2c_del_driver(&tsl256x_driver);
357 +MODULE_AUTHOR("Alec Tsai <alec_tsai@fiwin.com.tw>");
358 +MODULE_LICENSE("GPL");
360 +module_init(tsl256x_init);
361 +module_exit(tsl256x_exit);
363 diff --git a/drivers/i2c/chips/tsl256x.h b/drivers/i2c/chips/tsl256x.h
365 index 0000000..88de0c6
367 +++ b/drivers/i2c/chips/tsl256x.h
370 + * tsl256x.h -- TSL256x Light Sensor driver
372 + * Copyright 2007 by Fiwin.
373 + * Author: Alec Tsai <alec_tsai@fiwin.com.tw>
375 + * This program is free software; you can redistribute it and/or modify
376 + * it under the terms of the GNU General Public License version 2 as
377 + * published by the Free Software Foundation.
379 + * The contents of header file is copied from TSL256x Datasheet.
385 +#define TSL2560_ID 0x00
386 +#define TSL2561_ID 0x10
388 +#define LUX_SCALE 14 /* scale by 2^14 */
389 +#define RATIO_SCALE 9 /*scale ratio by 2^9 */
391 +/******************************************************************************
392 + * Integration time scaling factors
393 + *****************************************************************************/
394 +#define CH_SCALE 10 /* scale channel values by 2^10 */
395 +#define CHSCALE_TINT0 0x7517 /* 322/11 * 2^CH_SCALE */
396 +#define CHSCALE_TINT1 0x0fe7 /* 322/81 * 2^CH_SCALE */
398 +/******************************************************************************
399 + * T Package coefficients
400 + *****************************************************************************/
402 + * For Ch1/Ch0=0.00 to 0.50
403 + * Lux/Ch0=0.0304.0.062*((Ch1/Ch0)^1.4)
404 + * piecewise approximation
405 + * For Ch1/Ch0=0.00 to 0.125:
406 + * Lux/Ch0=0.0304.0.0272*(Ch1/Ch0)
408 + * For Ch1/Ch0=0.125 to 0.250:
409 + * Lux/Ch0=0.0325.0.0440*(Ch1/Ch0)
411 + * For Ch1/Ch0=0.250 to 0.375:
412 + * Lux/Ch0=0.0351.0.0544*(Ch1/Ch0)
414 + * For Ch1/Ch0=0.375 to 0.50:
415 + * Lux/Ch0=0.0381.0.0624*(Ch1/Ch0)
417 + * For Ch1/Ch0=0.50 to 0.61:
418 + * Lux/Ch0=0.0224.0.031*(Ch1/Ch0)
420 + * For Ch1/Ch0=0.61 to 0.80:
421 + * Lux/Ch0=0.0128.0.0153*(Ch1/Ch0)
423 + * For Ch1/Ch0=0.80 to 1.30:
424 + * Lux/Ch0=0.00146.0.00112*(Ch1/Ch0)
429 +#define K1T 0x0040 /* 0.125 * 2^RATIO_SCALE */
430 +#define B1T 0x01f2 /* 0.0304 * 2^LUX_SCALE */
431 +#define M1T 0x01be /* 0.0272 * 2^LUX_SCALE */
432 +#define K2T 0x0080 /* 0.250 * 2^RATIO_SCALE */
433 +#define B2T 0x0214 /* 0.0325 * 2^LUX_SCALE */
434 +#define M2T 0x02d1 /* 0.0440 * 2^LUX_SCALE */
435 +#define K3T 0x00c0 /* 0.375 * 2^RATIO_SCALE */
436 +#define B3T 0x023f /* 0.0351 * 2^LUX_SCALE */
437 +#define M3T 0x037b /* 0.0544 * 2^LUX_SCALE */
438 +#define K4T 0x0100 /* 0.50 * 2^RATIO_SCALE */
439 +#define B4T 0x0270 /* 0.0381 * 2^LUX_SCALE */
440 +#define M4T 0x03fe /* 0.0624 * 2^LUX_SCALE */
441 +#define K5T 0x0138 /* 0.61 * 2^RATIO_SCALE */
442 +#define B5T 0x016f /* 0.0224 * 2^LUX_SCALE */
443 +#define M5T 0x01fc /* 0.0310 * 2^LUX_SCALE */
444 +#define K6T 0x019a /* 0.80 * 2^RATIO_SCALE */
445 +#define B6T 0x00d2 /* 0.0128 * 2^LUX_SCALE */
446 +#define M6T 0x00fb /* 0.0153 * 2^LUX_SCALE */
447 +#define K7T 0x029a /* 1.3 * 2^RATIO_SCALE */
448 +#define B7T 0x0018 /* 0.00146 * 2^LUX_SCALE */
449 +#define M7T 0x0012 /* 0.00112 * 2^LUX_SCALE */
450 +#define K8T 0x029a /* 1.3 * 2^RATIO_SCALE */
451 +#define B8T 0x0000 /* 0.000 * 2^LUX_SCALE */
452 +#define M8T 0x0000 /* 0.000 * 2^LUX_SCALE */
454 +/******************************************************************************
455 + * CS package coefficients
456 + *****************************************************************************/
458 + * For 0 <= Ch1/Ch0 <= 0.52
459 + * Lux/Ch0 = 0.0315.0.0593*((Ch1/Ch0)^1.4)
460 + * piecewise approximation
461 + * For 0 <= Ch1/Ch0 <= 0.13
462 + * Lux/Ch0 = 0.0315.0.0262*(Ch1/Ch0)
463 + * For 0.13 <= Ch1/Ch0 <= 0.26
464 + * Lux/Ch0 = 0.0337.0.0430*(Ch1/Ch0)
465 + * For 0.26 <= Ch1/Ch0 <= 0.39
466 + * Lux/Ch0 = 0.0363.0.0529*(Ch1/Ch0)
467 + * For 0.39 <= Ch1/Ch0 <= 0.52
468 + * Lux/Ch0 = 0.0392.0.0605*(Ch1/Ch0)
469 + * For 0.52 < Ch1/Ch0 <= 0.65
470 + * Lux/Ch0 = 0.0229.0.0291*(Ch1/Ch0)
471 + * For 0.65 < Ch1/Ch0 <= 0.80
472 + * Lux/Ch0 = 0.00157.0.00180*(Ch1/Ch0)
473 + * For 0.80 < Ch1/Ch0 <= 1.30
474 + * Lux/Ch0 = 0.00338.0.00260*(Ch1/Ch0)
475 + * For Ch1/Ch0 > 1.30
478 +#define K1C 0x0043 /* 0.130 * 2^RATIO_SCALE */
479 +#define B1C 0x0204 /* 0.0315 * 2^LUX_SCALE */
480 +#define M1C 0x01ad /* 0.0262 * 2^LUX_SCALE */
481 +#define K2C 0x0085 /* 0.260 * 2^RATIO_SCALE */
482 +#define B2C 0x0228 /* 0.0337 * 2^LUX_SCALE */
483 +#define M2C 0x02c1 /* 0.0430 * 2^LUX_SCALE */
484 +#define K3C 0x00c8 /* 0.390 * 2^RATIO_SCALE */
485 +#define B3C 0x0253 /* 0.0363 * 2^LUX_SCALE */
486 +#define M3C 0x0363 /* 0.0529 * 2^LUX_SCALE */
487 +#define K4C 0x010a /* 0.520 * 2^RATIO_SCALE */
488 +#define B4C 0x0282 /* 0.0392 * 2^LUX_SCALE */
489 +#define M4C 0x03df /* 0.0605 * 2^LUX_SCALE */
490 +#define K5C 0x014d /* 0.65 * 2^RATIO_SCALE */
491 +#define B5C 0x0177 /* 0.0229 * 2^LUX_SCALE */
492 +#define M5C 0x01dd /* 0.0291 * 2^LUX_SCALE */
493 +#define K6C 0x019a /* 0.80 * 2^RATIO_SCALE */
494 +#define B6C 0x0101 /* 0.0157 * 2^LUX_SCALE */
495 +#define M6C 0x0127 /* 0.0180 * 2^LUX_SCALE */
496 +#define K7C 0x029a /* 1.3 * 2^RATIO_SCALE */
497 +#define B7C 0x0037 /* 0.00338 * 2^LUX_SCALE */
498 +#define M7C 0x002b /* 0.00260 * 2^LUX_SCALE */
499 +#define K8C 0x029a /* 1.3 * 2^RATIO_SCALE */
500 +#define B8C 0x0000 /* 0.000 * 2^LUX_SCALE */
501 +#define M8C 0x0000 /* 0.000 * 2^LUX_SCALE */
503 +/* TSL256x registers definition . */
505 + TSL256X_REG_CONTROL = 0x80, /* Control of basic functions */
506 + TSL256X_REG_TIMING = 0x81, /* Integration time/gain control */
507 + TSL256X_REG_THRESHLOWLOW = 0x82, /* Low byte of low interrupt threshold */
508 + TSL256X_REG_THRESHLOWHIGH = 0x83, /* High byte of low interrupt threshold */
509 + TSL256X_REG_THRESHHIGHLOW = 0x84, /* Low byte of high interrupt threshold */
510 + TSL256X_REG_THRESHHIGHHIGH = 0x85, /* High byte of high interrupt threshold */
511 + TSL256X_REG_INTERRUPT = 0x86, /* Interrupt control */
512 + TSL256X_REG_CRC = 0x88, /* Factory test - not a user register */
513 + TSL256X_REG_ID = 0x8A, /* Part number/ Rev ID */
514 + TSL256X_REG_DATA0LOW = 0x8C, /* Low byte of ADC channel 0 */
515 + TSL256X_REG_DATA0HIGH = 0x8D, /* High byte of ADC channel 0 */
516 + TSL256X_REG_DATA1LOW = 0x8E, /* Low byte of ADC channel 1 */
517 + TSL256X_REG_DATA1HIGH = 0x8F, /* High byte of ADC channel 1 */
521 +#endif /* _TSL256X_H */
523 diff --git a/include/linux/i2c-id.h b/include/linux/i2c-id.h
524 index 9b8ae78..a089554 100644
525 --- a/include/linux/i2c-id.h
526 +++ b/include/linux/i2c-id.h
528 #define I2C_DRIVERID_W83L785TS 1047
529 #define I2C_DRIVERID_OV7670 1048 /* Omnivision 7670 camera */
530 #define I2C_DRIVERID_PCF50606 1049
531 +#define I2C_DRIVERID_TSL256X 1050
534 * ---- Adapter types ----------------------------------------------------