~ubuntu-branches/ubuntu/precise/linux-lowlatency/precise

« back to all changes in this revision

Viewing changes to drivers/staging/iio/light/tsl2563.c

  • Committer: Package Import Robot
  • Author(s): Alessio Igor Bogani
  • Date: 2011-10-26 11:13:05 UTC
  • Revision ID: package-import@ubuntu.com-20111026111305-tz023xykf0i6eosh
Tags: upstream-3.2.0
ImportĀ upstreamĀ versionĀ 3.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * drivers/i2c/chips/tsl2563.c
 
3
 *
 
4
 * Copyright (C) 2008 Nokia Corporation
 
5
 *
 
6
 * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
 
7
 * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
 
8
 *
 
9
 * Converted to IIO driver
 
10
 * Amit Kucheria <amit.kucheria@verdurent.com>
 
11
 *
 
12
 * This program is free software; you can redistribute it and/or
 
13
 * modify it under the terms of the GNU General Public License
 
14
 * version 2 as published by the Free Software Foundation.
 
15
 *
 
16
 * This program is distributed in the hope that it will be useful, but
 
17
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
19
 * General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
24
 * 02110-1301 USA
 
25
 */
 
26
 
 
27
#include <linux/module.h>
 
28
#include <linux/i2c.h>
 
29
#include <linux/interrupt.h>
 
30
#include <linux/irq.h>
 
31
#include <linux/sched.h>
 
32
#include <linux/mutex.h>
 
33
#include <linux/delay.h>
 
34
#include <linux/pm.h>
 
35
#include <linux/err.h>
 
36
#include <linux/slab.h>
 
37
 
 
38
#include "../iio.h"
 
39
#include "../sysfs.h"
 
40
#include "tsl2563.h"
 
41
 
 
42
/* Use this many bits for fraction part. */
 
43
#define ADC_FRAC_BITS           (14)
 
44
 
 
45
/* Given number of 1/10000's in ADC_FRAC_BITS precision. */
 
46
#define FRAC10K(f)              (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
 
47
 
 
48
/* Bits used for fraction in calibration coefficients.*/
 
49
#define CALIB_FRAC_BITS         (10)
 
50
/* 0.5 in CALIB_FRAC_BITS precision */
 
51
#define CALIB_FRAC_HALF         (1 << (CALIB_FRAC_BITS - 1))
 
52
/* Make a fraction from a number n that was multiplied with b. */
 
53
#define CALIB_FRAC(n, b)        (((n) << CALIB_FRAC_BITS) / (b))
 
54
/* Decimal 10^(digits in sysfs presentation) */
 
55
#define CALIB_BASE_SYSFS        (1000)
 
56
 
 
57
#define TSL2563_CMD             (0x80)
 
58
#define TSL2563_CLEARINT        (0x40)
 
59
 
 
60
#define TSL2563_REG_CTRL        (0x00)
 
61
#define TSL2563_REG_TIMING      (0x01)
 
62
#define TSL2563_REG_LOWLOW      (0x02) /* data0 low threshold, 2 bytes */
 
63
#define TSL2563_REG_LOWHIGH     (0x03)
 
64
#define TSL2563_REG_HIGHLOW     (0x04) /* data0 high threshold, 2 bytes */
 
65
#define TSL2563_REG_HIGHHIGH    (0x05)
 
66
#define TSL2563_REG_INT         (0x06)
 
67
#define TSL2563_REG_ID          (0x0a)
 
68
#define TSL2563_REG_DATA0LOW    (0x0c) /* broadband sensor value, 2 bytes */
 
69
#define TSL2563_REG_DATA0HIGH   (0x0d)
 
70
#define TSL2563_REG_DATA1LOW    (0x0e) /* infrared sensor value, 2 bytes */
 
71
#define TSL2563_REG_DATA1HIGH   (0x0f)
 
72
 
 
73
#define TSL2563_CMD_POWER_ON    (0x03)
 
74
#define TSL2563_CMD_POWER_OFF   (0x00)
 
75
#define TSL2563_CTRL_POWER_MASK (0x03)
 
76
 
 
77
#define TSL2563_TIMING_13MS     (0x00)
 
78
#define TSL2563_TIMING_100MS    (0x01)
 
79
#define TSL2563_TIMING_400MS    (0x02)
 
80
#define TSL2563_TIMING_MASK     (0x03)
 
81
#define TSL2563_TIMING_GAIN16   (0x10)
 
82
#define TSL2563_TIMING_GAIN1    (0x00)
 
83
 
 
84
#define TSL2563_INT_DISBLED     (0x00)
 
85
#define TSL2563_INT_LEVEL       (0x10)
 
86
#define TSL2563_INT_PERSIST(n)  ((n) & 0x0F)
 
87
 
 
88
struct tsl2563_gainlevel_coeff {
 
89
        u8 gaintime;
 
90
        u16 min;
 
91
        u16 max;
 
92
};
 
93
 
 
94
static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
 
95
        {
 
96
                .gaintime       = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
 
97
                .min            = 0,
 
98
                .max            = 65534,
 
99
        }, {
 
100
                .gaintime       = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
 
101
                .min            = 2048,
 
102
                .max            = 65534,
 
103
        }, {
 
104
                .gaintime       = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
 
105
                .min            = 4095,
 
106
                .max            = 37177,
 
107
        }, {
 
108
                .gaintime       = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
 
109
                .min            = 3000,
 
110
                .max            = 65535,
 
111
        },
 
112
};
 
113
 
 
114
struct tsl2563_chip {
 
115
        struct mutex            lock;
 
116
        struct i2c_client       *client;
 
117
        struct delayed_work     poweroff_work;
 
118
 
 
119
        /* Remember state for suspend and resume functions */
 
120
        pm_message_t            state;
 
121
 
 
122
        struct tsl2563_gainlevel_coeff const *gainlevel;
 
123
 
 
124
        u16                     low_thres;
 
125
        u16                     high_thres;
 
126
        u8                      intr;
 
127
        bool                    int_enabled;
 
128
 
 
129
        /* Calibration coefficients */
 
130
        u32                     calib0;
 
131
        u32                     calib1;
 
132
        int                     cover_comp_gain;
 
133
 
 
134
        /* Cache current values, to be returned while suspended */
 
135
        u32                     data0;
 
136
        u32                     data1;
 
137
};
 
138
 
 
139
static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
 
140
{
 
141
        struct i2c_client *client = chip->client;
 
142
        u8 cmd;
 
143
 
 
144
        cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
 
145
        return i2c_smbus_write_byte_data(client,
 
146
                                         TSL2563_CMD | TSL2563_REG_CTRL, cmd);
 
147
}
 
148
 
 
149
/*
 
150
 * Return value is 0 for off, 1 for on, or a negative error
 
151
 * code if reading failed.
 
152
 */
 
153
static int tsl2563_get_power(struct tsl2563_chip *chip)
 
154
{
 
155
        struct i2c_client *client = chip->client;
 
156
        int ret;
 
157
 
 
158
        ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
 
159
        if (ret < 0)
 
160
                return ret;
 
161
 
 
162
        return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
 
163
}
 
164
 
 
165
static int tsl2563_configure(struct tsl2563_chip *chip)
 
166
{
 
167
        int ret;
 
168
 
 
169
        ret = i2c_smbus_write_byte_data(chip->client,
 
170
                        TSL2563_CMD | TSL2563_REG_TIMING,
 
171
                        chip->gainlevel->gaintime);
 
172
        if (ret)
 
173
                goto error_ret;
 
174
        ret = i2c_smbus_write_byte_data(chip->client,
 
175
                        TSL2563_CMD | TSL2563_REG_HIGHLOW,
 
176
                        chip->high_thres & 0xFF);
 
177
        if (ret)
 
178
                goto error_ret;
 
179
        ret = i2c_smbus_write_byte_data(chip->client,
 
180
                        TSL2563_CMD | TSL2563_REG_HIGHHIGH,
 
181
                        (chip->high_thres >> 8) & 0xFF);
 
182
        if (ret)
 
183
                goto error_ret;
 
184
        ret = i2c_smbus_write_byte_data(chip->client,
 
185
                        TSL2563_CMD | TSL2563_REG_LOWLOW,
 
186
                        chip->low_thres & 0xFF);
 
187
        if (ret)
 
188
                goto error_ret;
 
189
        ret = i2c_smbus_write_byte_data(chip->client,
 
190
                        TSL2563_CMD | TSL2563_REG_LOWHIGH,
 
191
                        (chip->low_thres >> 8) & 0xFF);
 
192
/* Interrupt register is automatically written anyway if it is relevant
 
193
   so is not here */
 
194
error_ret:
 
195
        return ret;
 
196
}
 
197
 
 
198
static void tsl2563_poweroff_work(struct work_struct *work)
 
199
{
 
200
        struct tsl2563_chip *chip =
 
201
                container_of(work, struct tsl2563_chip, poweroff_work.work);
 
202
        tsl2563_set_power(chip, 0);
 
203
}
 
204
 
 
205
static int tsl2563_detect(struct tsl2563_chip *chip)
 
206
{
 
207
        int ret;
 
208
 
 
209
        ret = tsl2563_set_power(chip, 1);
 
210
        if (ret)
 
211
                return ret;
 
212
 
 
213
        ret = tsl2563_get_power(chip);
 
214
        if (ret < 0)
 
215
                return ret;
 
216
 
 
217
        return ret ? 0 : -ENODEV;
 
218
}
 
219
 
 
220
static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
 
221
{
 
222
        struct i2c_client *client = chip->client;
 
223
        int ret;
 
224
 
 
225
        ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
 
226
        if (ret < 0)
 
227
                return ret;
 
228
 
 
229
        return 0;
 
230
}
 
231
 
 
232
/*
 
233
 * "Normalized" ADC value is one obtained with 400ms of integration time and
 
234
 * 16x gain. This function returns the number of bits of shift needed to
 
235
 * convert between normalized values and HW values obtained using given
 
236
 * timing and gain settings.
 
237
 */
 
238
static int adc_shiftbits(u8 timing)
 
239
{
 
240
        int shift = 0;
 
241
 
 
242
        switch (timing & TSL2563_TIMING_MASK) {
 
243
        case TSL2563_TIMING_13MS:
 
244
                shift += 5;
 
245
                break;
 
246
        case TSL2563_TIMING_100MS:
 
247
                shift += 2;
 
248
                break;
 
249
        case TSL2563_TIMING_400MS:
 
250
                /* no-op */
 
251
                break;
 
252
        }
 
253
 
 
254
        if (!(timing & TSL2563_TIMING_GAIN16))
 
255
                shift += 4;
 
256
 
 
257
        return shift;
 
258
}
 
259
 
 
260
/* Convert a HW ADC value to normalized scale. */
 
261
static u32 normalize_adc(u16 adc, u8 timing)
 
262
{
 
263
        return adc << adc_shiftbits(timing);
 
264
}
 
265
 
 
266
static void tsl2563_wait_adc(struct tsl2563_chip *chip)
 
267
{
 
268
        unsigned int delay;
 
269
 
 
270
        switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
 
271
        case TSL2563_TIMING_13MS:
 
272
                delay = 14;
 
273
                break;
 
274
        case TSL2563_TIMING_100MS:
 
275
                delay = 101;
 
276
                break;
 
277
        default:
 
278
                delay = 402;
 
279
        }
 
280
        /*
 
281
         * TODO: Make sure that we wait at least required delay but why we
 
282
         * have to extend it one tick more?
 
283
         */
 
284
        schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
 
285
}
 
286
 
 
287
static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
 
288
{
 
289
        struct i2c_client *client = chip->client;
 
290
 
 
291
        if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
 
292
 
 
293
                (adc > chip->gainlevel->max) ?
 
294
                        chip->gainlevel++ : chip->gainlevel--;
 
295
 
 
296
                i2c_smbus_write_byte_data(client,
 
297
                                          TSL2563_CMD | TSL2563_REG_TIMING,
 
298
                                          chip->gainlevel->gaintime);
 
299
 
 
300
                tsl2563_wait_adc(chip);
 
301
                tsl2563_wait_adc(chip);
 
302
 
 
303
                return 1;
 
304
        } else
 
305
                return 0;
 
306
}
 
307
 
 
308
static int tsl2563_get_adc(struct tsl2563_chip *chip)
 
309
{
 
310
        struct i2c_client *client = chip->client;
 
311
        u16 adc0, adc1;
 
312
        int retry = 1;
 
313
        int ret = 0;
 
314
 
 
315
        if (chip->state.event != PM_EVENT_ON)
 
316
                goto out;
 
317
 
 
318
        if (!chip->int_enabled) {
 
319
                cancel_delayed_work(&chip->poweroff_work);
 
320
 
 
321
                if (!tsl2563_get_power(chip)) {
 
322
                        ret = tsl2563_set_power(chip, 1);
 
323
                        if (ret)
 
324
                                goto out;
 
325
                        ret = tsl2563_configure(chip);
 
326
                        if (ret)
 
327
                                goto out;
 
328
                        tsl2563_wait_adc(chip);
 
329
                }
 
330
        }
 
331
 
 
332
        while (retry) {
 
333
                ret = i2c_smbus_read_word_data(client,
 
334
                                TSL2563_CMD | TSL2563_REG_DATA0LOW);
 
335
                if (ret < 0)
 
336
                        goto out;
 
337
                adc0 = ret;
 
338
 
 
339
                ret = i2c_smbus_read_word_data(client,
 
340
                                TSL2563_CMD | TSL2563_REG_DATA1LOW);
 
341
                if (ret < 0)
 
342
                        goto out;
 
343
                adc1 = ret;
 
344
 
 
345
                retry = tsl2563_adjust_gainlevel(chip, adc0);
 
346
        }
 
347
 
 
348
        chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
 
349
        chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
 
350
 
 
351
        if (!chip->int_enabled)
 
352
                schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
 
353
 
 
354
        ret = 0;
 
355
out:
 
356
        return ret;
 
357
}
 
358
 
 
359
static inline int calib_to_sysfs(u32 calib)
 
360
{
 
361
        return (int) (((calib * CALIB_BASE_SYSFS) +
 
362
                       CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
 
363
}
 
364
 
 
365
static inline u32 calib_from_sysfs(int value)
 
366
{
 
367
        return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
 
368
}
 
369
 
 
370
/*
 
371
 * Conversions between lux and ADC values.
 
372
 *
 
373
 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
 
374
 * appropriate constants. Different constants are needed for different
 
375
 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
 
376
 * of the intensities in infrared and visible wavelengths). lux_table below
 
377
 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
 
378
 * constants.
 
379
 */
 
380
 
 
381
struct tsl2563_lux_coeff {
 
382
        unsigned long ch_ratio;
 
383
        unsigned long ch0_coeff;
 
384
        unsigned long ch1_coeff;
 
385
};
 
386
 
 
387
static const struct tsl2563_lux_coeff lux_table[] = {
 
388
        {
 
389
                .ch_ratio       = FRAC10K(1300),
 
390
                .ch0_coeff      = FRAC10K(315),
 
391
                .ch1_coeff      = FRAC10K(262),
 
392
        }, {
 
393
                .ch_ratio       = FRAC10K(2600),
 
394
                .ch0_coeff      = FRAC10K(337),
 
395
                .ch1_coeff      = FRAC10K(430),
 
396
        }, {
 
397
                .ch_ratio       = FRAC10K(3900),
 
398
                .ch0_coeff      = FRAC10K(363),
 
399
                .ch1_coeff      = FRAC10K(529),
 
400
        }, {
 
401
                .ch_ratio       = FRAC10K(5200),
 
402
                .ch0_coeff      = FRAC10K(392),
 
403
                .ch1_coeff      = FRAC10K(605),
 
404
        }, {
 
405
                .ch_ratio       = FRAC10K(6500),
 
406
                .ch0_coeff      = FRAC10K(229),
 
407
                .ch1_coeff      = FRAC10K(291),
 
408
        }, {
 
409
                .ch_ratio       = FRAC10K(8000),
 
410
                .ch0_coeff      = FRAC10K(157),
 
411
                .ch1_coeff      = FRAC10K(180),
 
412
        }, {
 
413
                .ch_ratio       = FRAC10K(13000),
 
414
                .ch0_coeff      = FRAC10K(34),
 
415
                .ch1_coeff      = FRAC10K(26),
 
416
        }, {
 
417
                .ch_ratio       = ULONG_MAX,
 
418
                .ch0_coeff      = 0,
 
419
                .ch1_coeff      = 0,
 
420
        },
 
421
};
 
422
 
 
423
/*
 
424
 * Convert normalized, scaled ADC values to lux.
 
425
 */
 
426
static unsigned int adc_to_lux(u32 adc0, u32 adc1)
 
427
{
 
428
        const struct tsl2563_lux_coeff *lp = lux_table;
 
429
        unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
 
430
 
 
431
        ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
 
432
 
 
433
        while (lp->ch_ratio < ratio)
 
434
                lp++;
 
435
 
 
436
        lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
 
437
 
 
438
        return (unsigned int) (lux >> ADC_FRAC_BITS);
 
439
}
 
440
 
 
441
/*--------------------------------------------------------------*/
 
442
/*                      Sysfs interface                         */
 
443
/*--------------------------------------------------------------*/
 
444
 
 
445
 
 
446
/* Apply calibration coefficient to ADC count. */
 
447
static u32 calib_adc(u32 adc, u32 calib)
 
448
{
 
449
        unsigned long scaled = adc;
 
450
 
 
451
        scaled *= calib;
 
452
        scaled >>= CALIB_FRAC_BITS;
 
453
 
 
454
        return (u32) scaled;
 
455
}
 
456
 
 
457
static int tsl2563_write_raw(struct iio_dev *indio_dev,
 
458
                               struct iio_chan_spec const *chan,
 
459
                               int val,
 
460
                               int val2,
 
461
                               long mask)
 
462
{
 
463
        struct tsl2563_chip *chip = iio_priv(indio_dev);
 
464
 
 
465
        if (chan->channel == 0)
 
466
                chip->calib0 = calib_from_sysfs(val);
 
467
        else
 
468
                chip->calib1 = calib_from_sysfs(val);
 
469
 
 
470
        return 0;
 
471
}
 
472
 
 
473
static int tsl2563_read_raw(struct iio_dev *indio_dev,
 
474
                            struct iio_chan_spec const *chan,
 
475
                            int *val,
 
476
                            int *val2,
 
477
                            long m)
 
478
{
 
479
        int ret = -EINVAL;
 
480
        u32 calib0, calib1;
 
481
        struct tsl2563_chip *chip = iio_priv(indio_dev);
 
482
 
 
483
        mutex_lock(&chip->lock);
 
484
        switch (m) {
 
485
        case 0:
 
486
                switch (chan->type) {
 
487
                case IIO_LIGHT:
 
488
                        ret = tsl2563_get_adc(chip);
 
489
                        if (ret)
 
490
                                goto error_ret;
 
491
                        calib0 = calib_adc(chip->data0, chip->calib0) *
 
492
                                chip->cover_comp_gain;
 
493
                        calib1 = calib_adc(chip->data1, chip->calib1) *
 
494
                                chip->cover_comp_gain;
 
495
                        *val = adc_to_lux(calib0, calib1);
 
496
                        ret = IIO_VAL_INT;
 
497
                        break;
 
498
                case IIO_INTENSITY:
 
499
                        ret = tsl2563_get_adc(chip);
 
500
                        if (ret)
 
501
                                goto error_ret;
 
502
                        if (chan->channel == 0)
 
503
                                *val = chip->data0;
 
504
                        else
 
505
                                *val = chip->data1;
 
506
                        ret = IIO_VAL_INT;
 
507
                        break;
 
508
                default:
 
509
                        break;
 
510
                }
 
511
                break;
 
512
 
 
513
        case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
 
514
                if (chan->channel == 0)
 
515
                        *val = calib_to_sysfs(chip->calib0);
 
516
                else
 
517
                        *val = calib_to_sysfs(chip->calib1);
 
518
                ret = IIO_VAL_INT;
 
519
                break;
 
520
        default:
 
521
                ret = -EINVAL;
 
522
                goto error_ret;
 
523
        }
 
524
 
 
525
error_ret:
 
526
        mutex_unlock(&chip->lock);
 
527
        return ret;
 
528
}
 
529
 
 
530
static const struct iio_chan_spec tsl2563_channels[] = {
 
531
        {
 
532
                .type = IIO_LIGHT,
 
533
                .indexed = 1,
 
534
                .channel = 0,
 
535
        }, {
 
536
                .type = IIO_INTENSITY,
 
537
                .modified = 1,
 
538
                .channel2 = IIO_MOD_LIGHT_BOTH,
 
539
                .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE),
 
540
                .event_mask = (IIO_EV_BIT(IIO_EV_TYPE_THRESH,
 
541
                                          IIO_EV_DIR_RISING) |
 
542
                               IIO_EV_BIT(IIO_EV_TYPE_THRESH,
 
543
                                          IIO_EV_DIR_FALLING)),
 
544
        }, {
 
545
                .type = IIO_INTENSITY,
 
546
                .modified = 1,
 
547
                .channel2 = IIO_MOD_LIGHT_BOTH,
 
548
                .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE),
 
549
        }
 
550
};
 
551
 
 
552
static int tsl2563_read_thresh(struct iio_dev *indio_dev,
 
553
                               u64 event_code,
 
554
                               int *val)
 
555
{
 
556
        struct tsl2563_chip *chip = iio_priv(indio_dev);
 
557
 
 
558
        switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
 
559
        case IIO_EV_DIR_RISING:
 
560
                *val = chip->high_thres;
 
561
                break;
 
562
        case IIO_EV_DIR_FALLING:
 
563
                *val = chip->low_thres;
 
564
                break;
 
565
        default:
 
566
                return -EINVAL;
 
567
        }
 
568
 
 
569
        return 0;
 
570
}
 
571
 
 
572
static int tsl2563_write_thresh(struct iio_dev *indio_dev,
 
573
                                  u64 event_code,
 
574
                                  int val)
 
575
{
 
576
        struct tsl2563_chip *chip = iio_priv(indio_dev);
 
577
        int ret;
 
578
        u8 address;
 
579
 
 
580
        if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
 
581
                address = TSL2563_REG_HIGHLOW;
 
582
        else
 
583
                address = TSL2563_REG_LOWLOW;
 
584
        mutex_lock(&chip->lock);
 
585
        ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
 
586
                                        val & 0xFF);
 
587
        if (ret)
 
588
                goto error_ret;
 
589
        ret = i2c_smbus_write_byte_data(chip->client,
 
590
                                        TSL2563_CMD | (address + 1),
 
591
                                        (val >> 8) & 0xFF);
 
592
        if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
 
593
                chip->high_thres = val;
 
594
        else
 
595
                chip->low_thres = val;
 
596
 
 
597
error_ret:
 
598
        mutex_unlock(&chip->lock);
 
599
 
 
600
        return ret;
 
601
}
 
602
 
 
603
static irqreturn_t tsl2563_event_handler(int irq, void *private)
 
604
{
 
605
        struct iio_dev *dev_info = private;
 
606
        struct tsl2563_chip *chip = iio_priv(dev_info);
 
607
 
 
608
        iio_push_event(dev_info,
 
609
                       IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
 
610
                                            0,
 
611
                                            IIO_EV_TYPE_THRESH,
 
612
                                            IIO_EV_DIR_EITHER),
 
613
                       iio_get_time_ns());
 
614
 
 
615
        /* clear the interrupt and push the event */
 
616
        i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
 
617
        return IRQ_HANDLED;
 
618
}
 
619
 
 
620
static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
 
621
                                          u64 event_code,
 
622
                                          int state)
 
623
{
 
624
        struct tsl2563_chip *chip = iio_priv(indio_dev);
 
625
        int ret = 0;
 
626
 
 
627
        mutex_lock(&chip->lock);
 
628
        if (state && !(chip->intr & 0x30)) {
 
629
                chip->intr &= ~0x30;
 
630
                chip->intr |= 0x10;
 
631
                /* ensure the chip is actually on */
 
632
                cancel_delayed_work(&chip->poweroff_work);
 
633
                if (!tsl2563_get_power(chip)) {
 
634
                        ret = tsl2563_set_power(chip, 1);
 
635
                        if (ret)
 
636
                                goto out;
 
637
                        ret = tsl2563_configure(chip);
 
638
                        if (ret)
 
639
                                goto out;
 
640
                }
 
641
                ret = i2c_smbus_write_byte_data(chip->client,
 
642
                                                TSL2563_CMD | TSL2563_REG_INT,
 
643
                                                chip->intr);
 
644
                chip->int_enabled = true;
 
645
        }
 
646
 
 
647
        if (!state && (chip->intr & 0x30)) {
 
648
                chip->intr |= ~0x30;
 
649
                ret = i2c_smbus_write_byte_data(chip->client,
 
650
                                                TSL2563_CMD | TSL2563_REG_INT,
 
651
                                                chip->intr);
 
652
                chip->int_enabled = false;
 
653
                /* now the interrupt is not enabled, we can go to sleep */
 
654
                schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
 
655
        }
 
656
out:
 
657
        mutex_unlock(&chip->lock);
 
658
 
 
659
        return ret;
 
660
}
 
661
 
 
662
static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
 
663
                                         u64 event_code)
 
664
{
 
665
        struct tsl2563_chip *chip = iio_priv(indio_dev);
 
666
        int ret;
 
667
 
 
668
        mutex_lock(&chip->lock);
 
669
        ret = i2c_smbus_read_byte_data(chip->client,
 
670
                                       TSL2563_CMD | TSL2563_REG_INT);
 
671
        mutex_unlock(&chip->lock);
 
672
        if (ret < 0)
 
673
                goto error_ret;
 
674
        ret = !!(ret & 0x30);
 
675
error_ret:
 
676
 
 
677
        return ret;
 
678
}
 
679
 
 
680
/*--------------------------------------------------------------*/
 
681
/*                      Probe, Attach, Remove                   */
 
682
/*--------------------------------------------------------------*/
 
683
static struct i2c_driver tsl2563_i2c_driver;
 
684
 
 
685
static const struct iio_info tsl2563_info_no_irq = {
 
686
        .driver_module = THIS_MODULE,
 
687
        .read_raw = &tsl2563_read_raw,
 
688
        .write_raw = &tsl2563_write_raw,
 
689
};
 
690
 
 
691
static const struct iio_info tsl2563_info = {
 
692
        .driver_module = THIS_MODULE,
 
693
        .read_raw = &tsl2563_read_raw,
 
694
        .write_raw = &tsl2563_write_raw,
 
695
        .read_event_value = &tsl2563_read_thresh,
 
696
        .write_event_value = &tsl2563_write_thresh,
 
697
        .read_event_config = &tsl2563_read_interrupt_config,
 
698
        .write_event_config = &tsl2563_write_interrupt_config,
 
699
};
 
700
 
 
701
static int __devinit tsl2563_probe(struct i2c_client *client,
 
702
                                const struct i2c_device_id *device_id)
 
703
{
 
704
        struct iio_dev *indio_dev;
 
705
        struct tsl2563_chip *chip;
 
706
        struct tsl2563_platform_data *pdata = client->dev.platform_data;
 
707
        int err = 0;
 
708
        int ret;
 
709
        u8 id = 0;
 
710
 
 
711
        indio_dev = iio_allocate_device(sizeof(*chip));
 
712
        if (!indio_dev)
 
713
                return -ENOMEM;
 
714
 
 
715
        chip = iio_priv(indio_dev);
 
716
 
 
717
        i2c_set_clientdata(client, chip);
 
718
        chip->client = client;
 
719
 
 
720
        err = tsl2563_detect(chip);
 
721
        if (err) {
 
722
                dev_err(&client->dev, "device not found, error %d\n", -err);
 
723
                goto fail1;
 
724
        }
 
725
 
 
726
        err = tsl2563_read_id(chip, &id);
 
727
        if (err)
 
728
                goto fail1;
 
729
 
 
730
        mutex_init(&chip->lock);
 
731
 
 
732
        /* Default values used until userspace says otherwise */
 
733
        chip->low_thres = 0x0;
 
734
        chip->high_thres = 0xffff;
 
735
        chip->gainlevel = tsl2563_gainlevel_table;
 
736
        chip->intr = TSL2563_INT_PERSIST(4);
 
737
        chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
 
738
        chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
 
739
 
 
740
        if (pdata)
 
741
                chip->cover_comp_gain = pdata->cover_comp_gain;
 
742
        else
 
743
                chip->cover_comp_gain = 1;
 
744
 
 
745
        dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
 
746
        indio_dev->name = client->name;
 
747
        indio_dev->channels = tsl2563_channels;
 
748
        indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
 
749
        indio_dev->dev.parent = &client->dev;
 
750
        indio_dev->modes = INDIO_DIRECT_MODE;
 
751
        if (client->irq)
 
752
                indio_dev->info = &tsl2563_info;
 
753
        else
 
754
                indio_dev->info = &tsl2563_info_no_irq;
 
755
        if (client->irq) {
 
756
                ret = request_threaded_irq(client->irq,
 
757
                                           NULL,
 
758
                                           &tsl2563_event_handler,
 
759
                                           IRQF_TRIGGER_RISING | IRQF_ONESHOT,
 
760
                                           "tsl2563_event",
 
761
                                           indio_dev);
 
762
                if (ret)
 
763
                        goto fail2;
 
764
        }
 
765
        err = tsl2563_configure(chip);
 
766
        if (err)
 
767
                goto fail3;
 
768
 
 
769
        INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
 
770
        /* The interrupt cannot yet be enabled so this is fine without lock */
 
771
        schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
 
772
 
 
773
        ret = iio_device_register(indio_dev);
 
774
        if (ret)
 
775
                goto fail3;
 
776
 
 
777
        return 0;
 
778
fail3:
 
779
        if (client->irq)
 
780
                free_irq(client->irq, indio_dev);
 
781
fail2:
 
782
        iio_free_device(indio_dev);
 
783
fail1:
 
784
        kfree(chip);
 
785
        return err;
 
786
}
 
787
 
 
788
static int tsl2563_remove(struct i2c_client *client)
 
789
{
 
790
        struct tsl2563_chip *chip = i2c_get_clientdata(client);
 
791
        struct iio_dev *indio_dev = iio_priv_to_dev(chip);
 
792
 
 
793
        iio_device_unregister(indio_dev);
 
794
        if (!chip->int_enabled)
 
795
                cancel_delayed_work(&chip->poweroff_work);
 
796
        /* Ensure that interrupts are disabled - then flush any bottom halves */
 
797
        chip->intr |= ~0x30;
 
798
        i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
 
799
                                  chip->intr);
 
800
        flush_scheduled_work();
 
801
        tsl2563_set_power(chip, 0);
 
802
        if (client->irq)
 
803
                free_irq(client->irq, indio_dev);
 
804
 
 
805
        iio_free_device(indio_dev);
 
806
 
 
807
        return 0;
 
808
}
 
809
 
 
810
static int tsl2563_suspend(struct i2c_client *client, pm_message_t state)
 
811
{
 
812
        struct tsl2563_chip *chip = i2c_get_clientdata(client);
 
813
        int ret;
 
814
 
 
815
        mutex_lock(&chip->lock);
 
816
 
 
817
        ret = tsl2563_set_power(chip, 0);
 
818
        if (ret)
 
819
                goto out;
 
820
 
 
821
        chip->state = state;
 
822
 
 
823
out:
 
824
        mutex_unlock(&chip->lock);
 
825
        return ret;
 
826
}
 
827
 
 
828
static int tsl2563_resume(struct i2c_client *client)
 
829
{
 
830
        struct tsl2563_chip *chip = i2c_get_clientdata(client);
 
831
        int ret;
 
832
 
 
833
        mutex_lock(&chip->lock);
 
834
 
 
835
        ret = tsl2563_set_power(chip, 1);
 
836
        if (ret)
 
837
                goto out;
 
838
 
 
839
        ret = tsl2563_configure(chip);
 
840
        if (ret)
 
841
                goto out;
 
842
 
 
843
        chip->state.event = PM_EVENT_ON;
 
844
 
 
845
out:
 
846
        mutex_unlock(&chip->lock);
 
847
        return ret;
 
848
}
 
849
 
 
850
static const struct i2c_device_id tsl2563_id[] = {
 
851
        { "tsl2560", 0 },
 
852
        { "tsl2561", 1 },
 
853
        { "tsl2562", 2 },
 
854
        { "tsl2563", 3 },
 
855
        {}
 
856
};
 
857
MODULE_DEVICE_TABLE(i2c, tsl2563_id);
 
858
 
 
859
static struct i2c_driver tsl2563_i2c_driver = {
 
860
        .driver = {
 
861
                .name    = "tsl2563",
 
862
        },
 
863
        .suspend        = tsl2563_suspend,
 
864
        .resume         = tsl2563_resume,
 
865
        .probe          = tsl2563_probe,
 
866
        .remove         = __devexit_p(tsl2563_remove),
 
867
        .id_table       = tsl2563_id,
 
868
};
 
869
 
 
870
static int __init tsl2563_init(void)
 
871
{
 
872
        return i2c_add_driver(&tsl2563_i2c_driver);
 
873
}
 
874
 
 
875
static void __exit tsl2563_exit(void)
 
876
{
 
877
        i2c_del_driver(&tsl2563_i2c_driver);
 
878
}
 
879
 
 
880
MODULE_AUTHOR("Nokia Corporation");
 
881
MODULE_DESCRIPTION("tsl2563 light sensor driver");
 
882
MODULE_LICENSE("GPL");
 
883
 
 
884
module_init(tsl2563_init);
 
885
module_exit(tsl2563_exit);