blob: 32dfce0a644fcd37628aae60bad106cb2c8484a6 [file] [log] [blame]
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +02001/*
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>
Jonathan Cameron388be482010-06-26 12:54:21 +010030#include <linux/irq.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020031#include <linux/sched.h>
32#include <linux/mutex.h>
33#include <linux/delay.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020034#include <linux/pm.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020035#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020037
38#include "../iio.h"
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010039#include "../sysfs.h"
Jonathan Cameronaf5046a2011-10-26 17:41:32 +010040#include "../events.h"
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020041#include "tsl2563.h"
42
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020043/* Use this many bits for fraction part. */
44#define ADC_FRAC_BITS (14)
45
46/* Given number of 1/10000's in ADC_FRAC_BITS precision. */
47#define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
48
49/* Bits used for fraction in calibration coefficients.*/
50#define CALIB_FRAC_BITS (10)
51/* 0.5 in CALIB_FRAC_BITS precision */
52#define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1))
53/* Make a fraction from a number n that was multiplied with b. */
54#define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b))
55/* Decimal 10^(digits in sysfs presentation) */
56#define CALIB_BASE_SYSFS (1000)
57
58#define TSL2563_CMD (0x80)
59#define TSL2563_CLEARINT (0x40)
60
61#define TSL2563_REG_CTRL (0x00)
62#define TSL2563_REG_TIMING (0x01)
63#define TSL2563_REG_LOWLOW (0x02) /* data0 low threshold, 2 bytes */
64#define TSL2563_REG_LOWHIGH (0x03)
65#define TSL2563_REG_HIGHLOW (0x04) /* data0 high threshold, 2 bytes */
66#define TSL2563_REG_HIGHHIGH (0x05)
67#define TSL2563_REG_INT (0x06)
68#define TSL2563_REG_ID (0x0a)
69#define TSL2563_REG_DATA0LOW (0x0c) /* broadband sensor value, 2 bytes */
70#define TSL2563_REG_DATA0HIGH (0x0d)
71#define TSL2563_REG_DATA1LOW (0x0e) /* infrared sensor value, 2 bytes */
72#define TSL2563_REG_DATA1HIGH (0x0f)
73
74#define TSL2563_CMD_POWER_ON (0x03)
75#define TSL2563_CMD_POWER_OFF (0x00)
76#define TSL2563_CTRL_POWER_MASK (0x03)
77
78#define TSL2563_TIMING_13MS (0x00)
79#define TSL2563_TIMING_100MS (0x01)
80#define TSL2563_TIMING_400MS (0x02)
81#define TSL2563_TIMING_MASK (0x03)
82#define TSL2563_TIMING_GAIN16 (0x10)
83#define TSL2563_TIMING_GAIN1 (0x00)
84
85#define TSL2563_INT_DISBLED (0x00)
86#define TSL2563_INT_LEVEL (0x10)
87#define TSL2563_INT_PERSIST(n) ((n) & 0x0F)
88
89struct tsl2563_gainlevel_coeff {
90 u8 gaintime;
91 u16 min;
92 u16 max;
93};
94
Jonathan Cameron1ff7e1d2011-04-18 12:58:56 +010095static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020096 {
97 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
98 .min = 0,
99 .max = 65534,
100 }, {
101 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
102 .min = 2048,
103 .max = 65534,
104 }, {
105 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
106 .min = 4095,
107 .max = 37177,
108 }, {
109 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
110 .min = 3000,
111 .max = 65535,
112 },
113};
114
115struct tsl2563_chip {
116 struct mutex lock;
117 struct i2c_client *client;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200118 struct delayed_work poweroff_work;
119
120 /* Remember state for suspend and resume functions */
121 pm_message_t state;
122
Jonathan Cameron1ff7e1d2011-04-18 12:58:56 +0100123 struct tsl2563_gainlevel_coeff const *gainlevel;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200124
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200125 u16 low_thres;
126 u16 high_thres;
127 u8 intr;
Jonathan Cameron388be482010-06-26 12:54:21 +0100128 bool int_enabled;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200129
130 /* Calibration coefficients */
131 u32 calib0;
132 u32 calib1;
133 int cover_comp_gain;
134
135 /* Cache current values, to be returned while suspended */
136 u32 data0;
137 u32 data1;
138};
139
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200140static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
141{
142 struct i2c_client *client = chip->client;
143 u8 cmd;
144
145 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700146 return i2c_smbus_write_byte_data(client,
147 TSL2563_CMD | TSL2563_REG_CTRL, cmd);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200148}
149
150/*
151 * Return value is 0 for off, 1 for on, or a negative error
152 * code if reading failed.
153 */
154static int tsl2563_get_power(struct tsl2563_chip *chip)
155{
156 struct i2c_client *client = chip->client;
157 int ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200158
Bryan Freedd9b42c02011-06-24 13:40:47 -0700159 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
160 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200161 return ret;
162
Bryan Freedd9b42c02011-06-24 13:40:47 -0700163 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200164}
165
166static int tsl2563_configure(struct tsl2563_chip *chip)
167{
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200168 int ret;
169
Bryan Freedd9b42c02011-06-24 13:40:47 -0700170 ret = i2c_smbus_write_byte_data(chip->client,
171 TSL2563_CMD | TSL2563_REG_TIMING,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200172 chip->gainlevel->gaintime);
173 if (ret)
Jonathan Cameron388be482010-06-26 12:54:21 +0100174 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700175 ret = i2c_smbus_write_byte_data(chip->client,
176 TSL2563_CMD | TSL2563_REG_HIGHLOW,
Jonathan Cameron388be482010-06-26 12:54:21 +0100177 chip->high_thres & 0xFF);
178 if (ret)
179 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700180 ret = i2c_smbus_write_byte_data(chip->client,
181 TSL2563_CMD | TSL2563_REG_HIGHHIGH,
Jonathan Cameron388be482010-06-26 12:54:21 +0100182 (chip->high_thres >> 8) & 0xFF);
183 if (ret)
184 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700185 ret = i2c_smbus_write_byte_data(chip->client,
186 TSL2563_CMD | TSL2563_REG_LOWLOW,
Jonathan Cameron388be482010-06-26 12:54:21 +0100187 chip->low_thres & 0xFF);
188 if (ret)
189 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700190 ret = i2c_smbus_write_byte_data(chip->client,
191 TSL2563_CMD | TSL2563_REG_LOWHIGH,
Jonathan Cameron388be482010-06-26 12:54:21 +0100192 (chip->low_thres >> 8) & 0xFF);
193/* Interrupt register is automatically written anyway if it is relevant
194 so is not here */
195error_ret:
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200196 return ret;
197}
198
199static void tsl2563_poweroff_work(struct work_struct *work)
200{
201 struct tsl2563_chip *chip =
202 container_of(work, struct tsl2563_chip, poweroff_work.work);
203 tsl2563_set_power(chip, 0);
204}
205
206static int tsl2563_detect(struct tsl2563_chip *chip)
207{
208 int ret;
209
210 ret = tsl2563_set_power(chip, 1);
211 if (ret)
212 return ret;
213
214 ret = tsl2563_get_power(chip);
215 if (ret < 0)
216 return ret;
217
218 return ret ? 0 : -ENODEV;
219}
220
221static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
222{
223 struct i2c_client *client = chip->client;
224 int ret;
225
Bryan Freedd9b42c02011-06-24 13:40:47 -0700226 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
227 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200228 return ret;
229
Maxin B. John22dc09c2011-10-26 17:27:37 +0100230 *id = ret;
231
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200232 return 0;
233}
234
235/*
236 * "Normalized" ADC value is one obtained with 400ms of integration time and
237 * 16x gain. This function returns the number of bits of shift needed to
238 * convert between normalized values and HW values obtained using given
239 * timing and gain settings.
240 */
241static int adc_shiftbits(u8 timing)
242{
243 int shift = 0;
244
245 switch (timing & TSL2563_TIMING_MASK) {
246 case TSL2563_TIMING_13MS:
247 shift += 5;
248 break;
249 case TSL2563_TIMING_100MS:
250 shift += 2;
251 break;
252 case TSL2563_TIMING_400MS:
253 /* no-op */
254 break;
255 }
256
257 if (!(timing & TSL2563_TIMING_GAIN16))
258 shift += 4;
259
260 return shift;
261}
262
263/* Convert a HW ADC value to normalized scale. */
264static u32 normalize_adc(u16 adc, u8 timing)
265{
266 return adc << adc_shiftbits(timing);
267}
268
269static void tsl2563_wait_adc(struct tsl2563_chip *chip)
270{
271 unsigned int delay;
272
273 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
274 case TSL2563_TIMING_13MS:
275 delay = 14;
276 break;
277 case TSL2563_TIMING_100MS:
278 delay = 101;
279 break;
280 default:
281 delay = 402;
282 }
283 /*
284 * TODO: Make sure that we wait at least required delay but why we
285 * have to extend it one tick more?
286 */
287 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
288}
289
290static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
291{
292 struct i2c_client *client = chip->client;
293
294 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
295
296 (adc > chip->gainlevel->max) ?
297 chip->gainlevel++ : chip->gainlevel--;
298
Bryan Freedd9b42c02011-06-24 13:40:47 -0700299 i2c_smbus_write_byte_data(client,
300 TSL2563_CMD | TSL2563_REG_TIMING,
301 chip->gainlevel->gaintime);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200302
303 tsl2563_wait_adc(chip);
304 tsl2563_wait_adc(chip);
305
306 return 1;
307 } else
308 return 0;
309}
310
311static int tsl2563_get_adc(struct tsl2563_chip *chip)
312{
313 struct i2c_client *client = chip->client;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200314 u16 adc0, adc1;
315 int retry = 1;
316 int ret = 0;
317
318 if (chip->state.event != PM_EVENT_ON)
319 goto out;
320
Jonathan Cameron388be482010-06-26 12:54:21 +0100321 if (!chip->int_enabled) {
322 cancel_delayed_work(&chip->poweroff_work);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200323
Jonathan Cameron388be482010-06-26 12:54:21 +0100324 if (!tsl2563_get_power(chip)) {
325 ret = tsl2563_set_power(chip, 1);
326 if (ret)
327 goto out;
328 ret = tsl2563_configure(chip);
329 if (ret)
330 goto out;
331 tsl2563_wait_adc(chip);
332 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200333 }
334
335 while (retry) {
Bryan Freedd9b42c02011-06-24 13:40:47 -0700336 ret = i2c_smbus_read_word_data(client,
337 TSL2563_CMD | TSL2563_REG_DATA0LOW);
338 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200339 goto out;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700340 adc0 = ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200341
Bryan Freedd9b42c02011-06-24 13:40:47 -0700342 ret = i2c_smbus_read_word_data(client,
343 TSL2563_CMD | TSL2563_REG_DATA1LOW);
344 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200345 goto out;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700346 adc1 = ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200347
348 retry = tsl2563_adjust_gainlevel(chip, adc0);
349 }
350
351 chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
352 chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
353
Jonathan Cameron388be482010-06-26 12:54:21 +0100354 if (!chip->int_enabled)
355 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200356
357 ret = 0;
358out:
359 return ret;
360}
361
362static inline int calib_to_sysfs(u32 calib)
363{
364 return (int) (((calib * CALIB_BASE_SYSFS) +
365 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
366}
367
368static inline u32 calib_from_sysfs(int value)
369{
370 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
371}
372
373/*
374 * Conversions between lux and ADC values.
375 *
376 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
377 * appropriate constants. Different constants are needed for different
378 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
379 * of the intensities in infrared and visible wavelengths). lux_table below
380 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
381 * constants.
382 */
383
384struct tsl2563_lux_coeff {
385 unsigned long ch_ratio;
386 unsigned long ch0_coeff;
387 unsigned long ch1_coeff;
388};
389
390static const struct tsl2563_lux_coeff lux_table[] = {
391 {
392 .ch_ratio = FRAC10K(1300),
393 .ch0_coeff = FRAC10K(315),
394 .ch1_coeff = FRAC10K(262),
395 }, {
396 .ch_ratio = FRAC10K(2600),
397 .ch0_coeff = FRAC10K(337),
398 .ch1_coeff = FRAC10K(430),
399 }, {
400 .ch_ratio = FRAC10K(3900),
401 .ch0_coeff = FRAC10K(363),
402 .ch1_coeff = FRAC10K(529),
403 }, {
404 .ch_ratio = FRAC10K(5200),
405 .ch0_coeff = FRAC10K(392),
406 .ch1_coeff = FRAC10K(605),
407 }, {
408 .ch_ratio = FRAC10K(6500),
409 .ch0_coeff = FRAC10K(229),
410 .ch1_coeff = FRAC10K(291),
411 }, {
412 .ch_ratio = FRAC10K(8000),
413 .ch0_coeff = FRAC10K(157),
414 .ch1_coeff = FRAC10K(180),
415 }, {
416 .ch_ratio = FRAC10K(13000),
417 .ch0_coeff = FRAC10K(34),
418 .ch1_coeff = FRAC10K(26),
419 }, {
420 .ch_ratio = ULONG_MAX,
421 .ch0_coeff = 0,
422 .ch1_coeff = 0,
423 },
424};
425
426/*
427 * Convert normalized, scaled ADC values to lux.
428 */
429static unsigned int adc_to_lux(u32 adc0, u32 adc1)
430{
431 const struct tsl2563_lux_coeff *lp = lux_table;
432 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
433
434 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
435
436 while (lp->ch_ratio < ratio)
437 lp++;
438
439 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
440
441 return (unsigned int) (lux >> ADC_FRAC_BITS);
442}
443
444/*--------------------------------------------------------------*/
445/* Sysfs interface */
446/*--------------------------------------------------------------*/
447
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200448
449/* Apply calibration coefficient to ADC count. */
450static u32 calib_adc(u32 adc, u32 calib)
451{
452 unsigned long scaled = adc;
453
454 scaled *= calib;
455 scaled >>= CALIB_FRAC_BITS;
456
457 return (u32) scaled;
458}
459
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100460static int tsl2563_write_raw(struct iio_dev *indio_dev,
461 struct iio_chan_spec const *chan,
462 int val,
463 int val2,
464 long mask)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200465{
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100466 struct tsl2563_chip *chip = iio_priv(indio_dev);
467
468 if (chan->channel == 0)
469 chip->calib0 = calib_from_sysfs(val);
470 else
471 chip->calib1 = calib_from_sysfs(val);
472
473 return 0;
474}
475
476static int tsl2563_read_raw(struct iio_dev *indio_dev,
477 struct iio_chan_spec const *chan,
478 int *val,
479 int *val2,
480 long m)
481{
482 int ret = -EINVAL;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200483 u32 calib0, calib1;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100484 struct tsl2563_chip *chip = iio_priv(indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200485
486 mutex_lock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100487 switch (m) {
Jonathan Cameron388be482010-06-26 12:54:21 +0100488 case 0:
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100489 switch (chan->type) {
490 case IIO_LIGHT:
491 ret = tsl2563_get_adc(chip);
492 if (ret)
493 goto error_ret;
494 calib0 = calib_adc(chip->data0, chip->calib0) *
495 chip->cover_comp_gain;
496 calib1 = calib_adc(chip->data1, chip->calib1) *
497 chip->cover_comp_gain;
498 *val = adc_to_lux(calib0, calib1);
499 ret = IIO_VAL_INT;
500 break;
501 case IIO_INTENSITY:
502 ret = tsl2563_get_adc(chip);
503 if (ret)
504 goto error_ret;
505 if (chan->channel == 0)
506 *val = chip->data0;
507 else
508 *val = chip->data1;
509 ret = IIO_VAL_INT;
510 break;
511 default:
512 break;
513 }
Jonathan Cameron388be482010-06-26 12:54:21 +0100514 break;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100515
516 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
517 if (chan->channel == 0)
518 *val = calib_to_sysfs(chip->calib0);
519 else
520 *val = calib_to_sysfs(chip->calib1);
521 ret = IIO_VAL_INT;
Jonathan Cameron388be482010-06-26 12:54:21 +0100522 break;
523 default:
Dan Carpenter97d35f22011-10-06 09:15:03 +0300524 ret = -EINVAL;
525 goto error_ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100526 }
527
528error_ret:
529 mutex_unlock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100530 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100531}
532
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100533static const struct iio_chan_spec tsl2563_channels[] = {
Jonathan Cameron79939062011-08-30 12:41:16 +0100534 {
535 .type = IIO_LIGHT,
536 .indexed = 1,
537 .channel = 0,
538 }, {
539 .type = IIO_INTENSITY,
540 .modified = 1,
541 .channel2 = IIO_MOD_LIGHT_BOTH,
542 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE),
543 .event_mask = (IIO_EV_BIT(IIO_EV_TYPE_THRESH,
544 IIO_EV_DIR_RISING) |
545 IIO_EV_BIT(IIO_EV_TYPE_THRESH,
546 IIO_EV_DIR_FALLING)),
547 }, {
548 .type = IIO_INTENSITY,
549 .modified = 1,
Jonathan Camerona7e3bd62011-10-26 17:27:36 +0100550 .channel2 = IIO_MOD_LIGHT_IR,
Jonathan Cameron79939062011-08-30 12:41:16 +0100551 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE),
552 }
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100553};
Jonathan Cameron388be482010-06-26 12:54:21 +0100554
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100555static int tsl2563_read_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100556 u64 event_code,
557 int *val)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100558{
559 struct tsl2563_chip *chip = iio_priv(indio_dev);
560
561 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
562 case IIO_EV_DIR_RISING:
563 *val = chip->high_thres;
564 break;
565 case IIO_EV_DIR_FALLING:
566 *val = chip->low_thres;
567 break;
568 default:
569 return -EINVAL;
570 }
571
572 return 0;
573}
574
Dan Carpenter15fbc192011-10-06 09:15:36 +0300575static int tsl2563_write_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100576 u64 event_code,
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100577 int val)
578{
579 struct tsl2563_chip *chip = iio_priv(indio_dev);
580 int ret;
581 u8 address;
582
583 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
584 address = TSL2563_REG_HIGHLOW;
585 else
586 address = TSL2563_REG_LOWLOW;
587 mutex_lock(&chip->lock);
Bryan Freedd9b42c02011-06-24 13:40:47 -0700588 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
589 val & 0xFF);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100590 if (ret)
591 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700592 ret = i2c_smbus_write_byte_data(chip->client,
593 TSL2563_CMD | (address + 1),
594 (val >> 8) & 0xFF);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100595 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
596 chip->high_thres = val;
597 else
598 chip->low_thres = val;
599
600error_ret:
601 mutex_unlock(&chip->lock);
602
603 return ret;
604}
Jonathan Cameron388be482010-06-26 12:54:21 +0100605
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100606static irqreturn_t tsl2563_event_handler(int irq, void *private)
Jonathan Cameron388be482010-06-26 12:54:21 +0100607{
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100608 struct iio_dev *dev_info = private;
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100609 struct tsl2563_chip *chip = iio_priv(dev_info);
Jonathan Cameron388be482010-06-26 12:54:21 +0100610
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100611 iio_push_event(dev_info,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +0100612 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
Jonathan Cameronda1d8b62010-10-08 12:14:05 +0100613 0,
614 IIO_EV_TYPE_THRESH,
615 IIO_EV_DIR_EITHER),
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100616 iio_get_time_ns());
Jonathan Cameron388be482010-06-26 12:54:21 +0100617
Jonathan Cameron388be482010-06-26 12:54:21 +0100618 /* clear the interrupt and push the event */
Bryan Freedd9b42c02011-06-24 13:40:47 -0700619 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100620 return IRQ_HANDLED;
Jonathan Cameron388be482010-06-26 12:54:21 +0100621}
622
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100623static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100624 u64 event_code,
625 int state)
Jonathan Cameron388be482010-06-26 12:54:21 +0100626{
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100627 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100628 int ret = 0;
Jonathan Cameron388be482010-06-26 12:54:21 +0100629
Jonathan Cameron388be482010-06-26 12:54:21 +0100630 mutex_lock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100631 if (state && !(chip->intr & 0x30)) {
Jonathan Cameron388be482010-06-26 12:54:21 +0100632 chip->intr &= ~0x30;
633 chip->intr |= 0x10;
634 /* ensure the chip is actually on */
635 cancel_delayed_work(&chip->poweroff_work);
636 if (!tsl2563_get_power(chip)) {
637 ret = tsl2563_set_power(chip, 1);
638 if (ret)
639 goto out;
640 ret = tsl2563_configure(chip);
641 if (ret)
642 goto out;
643 }
Bryan Freedd9b42c02011-06-24 13:40:47 -0700644 ret = i2c_smbus_write_byte_data(chip->client,
645 TSL2563_CMD | TSL2563_REG_INT,
646 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100647 chip->int_enabled = true;
648 }
649
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100650 if (!state && (chip->intr & 0x30)) {
Jonathan Cameron388be482010-06-26 12:54:21 +0100651 chip->intr |= ~0x30;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700652 ret = i2c_smbus_write_byte_data(chip->client,
653 TSL2563_CMD | TSL2563_REG_INT,
654 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100655 chip->int_enabled = false;
656 /* now the interrupt is not enabled, we can go to sleep */
657 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
658 }
659out:
660 mutex_unlock(&chip->lock);
661
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100662 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100663}
664
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100665static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100666 u64 event_code)
Jonathan Cameron388be482010-06-26 12:54:21 +0100667{
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100668 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100669 int ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100670
671 mutex_lock(&chip->lock);
Bryan Freedd9b42c02011-06-24 13:40:47 -0700672 ret = i2c_smbus_read_byte_data(chip->client,
673 TSL2563_CMD | TSL2563_REG_INT);
Jonathan Cameron388be482010-06-26 12:54:21 +0100674 mutex_unlock(&chip->lock);
675 if (ret < 0)
676 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700677 ret = !!(ret & 0x30);
Jonathan Cameron388be482010-06-26 12:54:21 +0100678error_ret:
679
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100680 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100681}
Jonathan Cameron388be482010-06-26 12:54:21 +0100682
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200683/*--------------------------------------------------------------*/
684/* Probe, Attach, Remove */
685/*--------------------------------------------------------------*/
686static struct i2c_driver tsl2563_i2c_driver;
687
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100688static const struct iio_info tsl2563_info_no_irq = {
689 .driver_module = THIS_MODULE,
Bryan Freed9e4216f2011-06-21 15:54:57 -0700690 .read_raw = &tsl2563_read_raw,
691 .write_raw = &tsl2563_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100692};
693
694static const struct iio_info tsl2563_info = {
695 .driver_module = THIS_MODULE,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100696 .read_raw = &tsl2563_read_raw,
697 .write_raw = &tsl2563_write_raw,
698 .read_event_value = &tsl2563_read_thresh,
699 .write_event_value = &tsl2563_write_thresh,
700 .read_event_config = &tsl2563_read_interrupt_config,
701 .write_event_config = &tsl2563_write_interrupt_config,
702};
703
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200704static int __devinit tsl2563_probe(struct i2c_client *client,
705 const struct i2c_device_id *device_id)
706{
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100707 struct iio_dev *indio_dev;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200708 struct tsl2563_chip *chip;
709 struct tsl2563_platform_data *pdata = client->dev.platform_data;
710 int err = 0;
711 int ret;
Jonathan Camerondeda3862011-08-12 17:08:35 +0100712 u8 id = 0;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200713
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100714 indio_dev = iio_allocate_device(sizeof(*chip));
715 if (!indio_dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200716 return -ENOMEM;
717
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100718 chip = iio_priv(indio_dev);
719
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200720 i2c_set_clientdata(client, chip);
721 chip->client = client;
722
723 err = tsl2563_detect(chip);
724 if (err) {
Jonathan Cameroncb6405b2010-05-04 22:34:37 +0100725 dev_err(&client->dev, "device not found, error %d\n", -err);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200726 goto fail1;
727 }
728
729 err = tsl2563_read_id(chip, &id);
730 if (err)
731 goto fail1;
732
733 mutex_init(&chip->lock);
734
735 /* Default values used until userspace says otherwise */
736 chip->low_thres = 0x0;
737 chip->high_thres = 0xffff;
738 chip->gainlevel = tsl2563_gainlevel_table;
739 chip->intr = TSL2563_INT_PERSIST(4);
740 chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
741 chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
742
743 if (pdata)
744 chip->cover_comp_gain = pdata->cover_comp_gain;
745 else
746 chip->cover_comp_gain = 1;
747
748 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100749 indio_dev->name = client->name;
750 indio_dev->channels = tsl2563_channels;
751 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100752 indio_dev->dev.parent = &client->dev;
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100753 indio_dev->modes = INDIO_DIRECT_MODE;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100754 if (client->irq)
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100755 indio_dev->info = &tsl2563_info;
756 else
757 indio_dev->info = &tsl2563_info_no_irq;
Jonathan Cameron388be482010-06-26 12:54:21 +0100758 if (client->irq) {
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100759 ret = request_threaded_irq(client->irq,
760 NULL,
761 &tsl2563_event_handler,
762 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
763 "tsl2563_event",
764 indio_dev);
Jonathan Cameron388be482010-06-26 12:54:21 +0100765 if (ret)
766 goto fail2;
767 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200768 err = tsl2563_configure(chip);
769 if (err)
Jonathan Cameron388be482010-06-26 12:54:21 +0100770 goto fail3;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200771
772 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
Jonathan Cameron388be482010-06-26 12:54:21 +0100773 /* The interrupt cannot yet be enabled so this is fine without lock */
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200774 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
775
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100776 ret = iio_device_register(indio_dev);
777 if (ret)
778 goto fail3;
779
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200780 return 0;
Jonathan Cameron388be482010-06-26 12:54:21 +0100781fail3:
782 if (client->irq)
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100783 free_irq(client->irq, indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200784fail2:
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100785 iio_free_device(indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200786fail1:
787 kfree(chip);
788 return err;
789}
790
791static int tsl2563_remove(struct i2c_client *client)
792{
793 struct tsl2563_chip *chip = i2c_get_clientdata(client);
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100794 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100795
796 iio_device_unregister(indio_dev);
Jonathan Cameron388be482010-06-26 12:54:21 +0100797 if (!chip->int_enabled)
798 cancel_delayed_work(&chip->poweroff_work);
799 /* Ensure that interrupts are disabled - then flush any bottom halves */
800 chip->intr |= ~0x30;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700801 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
802 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100803 flush_scheduled_work();
804 tsl2563_set_power(chip, 0);
805 if (client->irq)
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100806 free_irq(client->irq, indio_dev);
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100807
808 iio_free_device(indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200809
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200810 return 0;
811}
812
813static int tsl2563_suspend(struct i2c_client *client, pm_message_t state)
814{
815 struct tsl2563_chip *chip = i2c_get_clientdata(client);
816 int ret;
817
818 mutex_lock(&chip->lock);
819
820 ret = tsl2563_set_power(chip, 0);
821 if (ret)
822 goto out;
823
824 chip->state = state;
825
826out:
827 mutex_unlock(&chip->lock);
828 return ret;
829}
830
831static int tsl2563_resume(struct i2c_client *client)
832{
833 struct tsl2563_chip *chip = i2c_get_clientdata(client);
834 int ret;
835
836 mutex_lock(&chip->lock);
837
838 ret = tsl2563_set_power(chip, 1);
839 if (ret)
840 goto out;
841
842 ret = tsl2563_configure(chip);
843 if (ret)
844 goto out;
845
846 chip->state.event = PM_EVENT_ON;
847
848out:
849 mutex_unlock(&chip->lock);
850 return ret;
851}
852
853static const struct i2c_device_id tsl2563_id[] = {
Jonathan Camerondbd5d232009-11-22 16:03:25 +0000854 { "tsl2560", 0 },
855 { "tsl2561", 1 },
856 { "tsl2562", 2 },
857 { "tsl2563", 3 },
858 {}
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200859};
860MODULE_DEVICE_TABLE(i2c, tsl2563_id);
861
862static struct i2c_driver tsl2563_i2c_driver = {
863 .driver = {
Jonathan Camerondbd5d232009-11-22 16:03:25 +0000864 .name = "tsl2563",
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200865 },
866 .suspend = tsl2563_suspend,
867 .resume = tsl2563_resume,
868 .probe = tsl2563_probe,
869 .remove = __devexit_p(tsl2563_remove),
870 .id_table = tsl2563_id,
871};
872
873static int __init tsl2563_init(void)
874{
875 return i2c_add_driver(&tsl2563_i2c_driver);
876}
877
878static void __exit tsl2563_exit(void)
879{
880 i2c_del_driver(&tsl2563_i2c_driver);
881}
882
883MODULE_AUTHOR("Nokia Corporation");
884MODULE_DESCRIPTION("tsl2563 light sensor driver");
885MODULE_LICENSE("GPL");
886
887module_init(tsl2563_init);
888module_exit(tsl2563_exit);