22 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
28 #define TSL2550_DRV_NAME "tsl2550"
29 #define DRIVER_VERSION "1.2"
35 #define TSL2550_POWER_DOWN 0x00
36 #define TSL2550_POWER_UP 0x03
37 #define TSL2550_STANDARD_RANGE 0x18
38 #define TSL2550_EXTENDED_RANGE 0x1d
39 #define TSL2550_READ_ADC0 0x43
40 #define TSL2550_READ_ADC1 0x83
58 static const u8 TSL2550_MODE_RANGE[2] = {
112 #define TSL2550_MAX_LUX 1846
114 static const u8 ratio_lut[] = {
115 100, 100, 100, 100, 100, 100, 100, 100,
116 100, 100, 100, 100, 100, 100, 99, 99,
117 99, 99, 99, 99, 99, 99, 99, 99,
118 99, 99, 99, 98, 98, 98, 98, 98,
119 98, 98, 97, 97, 97, 97, 97, 96,
120 96, 96, 96, 95, 95, 95, 94, 94,
121 93, 93, 93, 92, 92, 91, 91, 90,
122 89, 89, 88, 87, 87, 86, 85, 84,
123 83, 82, 81, 80, 79, 78, 77, 75,
124 74, 73, 71, 69, 68, 66, 64, 62,
125 60, 58, 56, 54, 52, 49, 47, 44,
126 42, 41, 40, 40, 39, 39, 38, 38,
127 37, 37, 37, 36, 36, 36, 35, 35,
128 35, 35, 34, 34, 34, 34, 33, 33,
129 33, 33, 32, 32, 32, 32, 32, 31,
130 31, 31, 31, 31, 30, 30, 30, 30,
134 static const u16 count_lut[] = {
135 0, 1, 2, 3, 4, 5, 6, 7,
136 8, 9, 10, 11, 12, 13, 14, 15,
137 16, 18, 20, 22, 24, 26, 28, 30,
138 32, 34, 36, 38, 40, 42, 44, 46,
139 49, 53, 57, 61, 65, 69, 73, 77,
140 81, 85, 89, 93, 97, 101, 105, 109,
141 115, 123, 131, 139, 147, 155, 163, 171,
142 179, 187, 195, 203, 211, 219, 227, 235,
143 247, 263, 279, 295, 311, 327, 343, 359,
144 375, 391, 407, 423, 439, 455, 471, 487,
145 511, 543, 575, 607, 639, 671, 703, 735,
146 767, 799, 831, 863, 895, 927, 959, 991,
147 1039, 1103, 1167, 1231, 1295, 1359, 1423, 1487,
148 1551, 1615, 1679, 1743, 1807, 1871, 1935, 1999,
149 2095, 2223, 2351, 2479, 2607, 2735, 2863, 2991,
150 3119, 3247, 3375, 3503, 3631, 3759, 3887, 4015,
157 static int tsl2550_calculate_lux(
u8 ch0,
u8 ch1)
162 u16 c0 = count_lut[ch0];
163 u16 c1 = count_lut[ch1];
177 lux = ((c0 - c1) * ratio_lut[r]) / 256;
207 if (val < 0 || val > 1)
211 ret = tsl2550_set_power_state(client, val);
221 tsl2550_show_power_state, tsl2550_store_power_state);
223 static ssize_t tsl2550_show_operating_mode(
struct device *dev,
231 static ssize_t tsl2550_store_operating_mode(
struct device *dev,
239 if (val < 0 || val > 1)
246 ret = tsl2550_set_operating_mode(client, val);
256 tsl2550_show_operating_mode, tsl2550_store_operating_mode);
275 ret = tsl2550_calculate_lux(ch0, ch1);
281 return sprintf(buf,
"%d\n", ret);
296 ret = __tsl2550_show_lux(client, buf);
303 tsl2550_show_lux1_input,
NULL);
305 static struct attribute *tsl2550_attributes[] = {
306 &dev_attr_power_state.attr,
307 &dev_attr_operating_mode.attr,
308 &dev_attr_lux1_input.attr,
313 .attrs = tsl2550_attributes,
320 static int tsl2550_init_client(
struct i2c_client *client)
355 int *opmode, err = 0;
369 i2c_set_clientdata(client, data);
372 opmode = client->
dev.platform_data;
374 if (*opmode < 0 || *opmode > 1) {
375 dev_err(&client->
dev,
"invalid operating_mode (%d)\n",
389 err = tsl2550_init_client(client);
413 tsl2550_set_power_state(client, 0);
415 kfree(i2c_get_clientdata(client));
424 return tsl2550_set_power_state(client, 0);
429 return tsl2550_set_power_state(client, 1);
434 #define tsl2550_suspend NULL
435 #define tsl2550_resume NULL
452 .probe = tsl2550_probe,
454 .id_table = tsl2550_id,