blob: ebb962c5c323a025f9c9e1ef8cfb27f40f2ba8d5 [file] [log] [blame]
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +02001/*
Jonathan Cameron9c2251d2013-01-12 10:35:00 +00002 * drivers/iio/light/tsl2563.c
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +02003 *
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
Jonathan Cameron06458e22012-04-25 15:54:58 +010038#include <linux/iio/iio.h>
39#include <linux/iio/sysfs.h>
40#include <linux/iio/events.h>
Jonathan Cameron9c2251d2013-01-12 10:35:00 +000041#include <linux/platform_data/tsl2563.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020042
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020043/* Use this many bits for fraction part. */
Jonathan Cameron5ade7632013-01-12 10:35:00 +000044#define ADC_FRAC_BITS 14
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020045
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.*/
Jonathan Cameron5ade7632013-01-12 10:35:00 +000050#define CALIB_FRAC_BITS 10
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020051/* 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) */
Jonathan Cameron5ade7632013-01-12 10:35:00 +000056#define CALIB_BASE_SYSFS 1000
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020057
Jonathan Cameron5ade7632013-01-12 10:35:00 +000058#define TSL2563_CMD 0x80
59#define TSL2563_CLEARINT 0x40
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020060
Jonathan Cameron5ade7632013-01-12 10:35:00 +000061#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
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020073
Jonathan Cameron5ade7632013-01-12 10:35:00 +000074#define TSL2563_CMD_POWER_ON 0x03
75#define TSL2563_CMD_POWER_OFF 0x00
76#define TSL2563_CTRL_POWER_MASK 0x03
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020077
Jonathan Cameron5ade7632013-01-12 10:35:00 +000078#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
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020084
Jonathan Cameron5ade7632013-01-12 10:35:00 +000085#define TSL2563_INT_DISBLED 0x00
86#define TSL2563_INT_LEVEL 0x10
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020087#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 */
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100121 bool suspended;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200122
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);
Jonathan Camerona9e244f62013-01-12 10:35:00 +0000193/*
194 * Interrupt register is automatically written anyway if it is relevant
195 * so is not here.
196 */
Jonathan Cameron388be482010-06-26 12:54:21 +0100197error_ret:
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200198 return ret;
199}
200
201static void tsl2563_poweroff_work(struct work_struct *work)
202{
203 struct tsl2563_chip *chip =
204 container_of(work, struct tsl2563_chip, poweroff_work.work);
205 tsl2563_set_power(chip, 0);
206}
207
208static int tsl2563_detect(struct tsl2563_chip *chip)
209{
210 int ret;
211
212 ret = tsl2563_set_power(chip, 1);
213 if (ret)
214 return ret;
215
216 ret = tsl2563_get_power(chip);
217 if (ret < 0)
218 return ret;
219
220 return ret ? 0 : -ENODEV;
221}
222
223static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
224{
225 struct i2c_client *client = chip->client;
226 int ret;
227
Bryan Freedd9b42c02011-06-24 13:40:47 -0700228 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
229 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200230 return ret;
231
Maxin B. John22dc09c2011-10-26 17:27:37 +0100232 *id = ret;
233
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200234 return 0;
235}
236
237/*
238 * "Normalized" ADC value is one obtained with 400ms of integration time and
239 * 16x gain. This function returns the number of bits of shift needed to
240 * convert between normalized values and HW values obtained using given
241 * timing and gain settings.
242 */
243static int adc_shiftbits(u8 timing)
244{
245 int shift = 0;
246
247 switch (timing & TSL2563_TIMING_MASK) {
248 case TSL2563_TIMING_13MS:
249 shift += 5;
250 break;
251 case TSL2563_TIMING_100MS:
252 shift += 2;
253 break;
254 case TSL2563_TIMING_400MS:
255 /* no-op */
256 break;
257 }
258
259 if (!(timing & TSL2563_TIMING_GAIN16))
260 shift += 4;
261
262 return shift;
263}
264
265/* Convert a HW ADC value to normalized scale. */
266static u32 normalize_adc(u16 adc, u8 timing)
267{
268 return adc << adc_shiftbits(timing);
269}
270
271static void tsl2563_wait_adc(struct tsl2563_chip *chip)
272{
273 unsigned int delay;
274
275 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
276 case TSL2563_TIMING_13MS:
277 delay = 14;
278 break;
279 case TSL2563_TIMING_100MS:
280 delay = 101;
281 break;
282 default:
283 delay = 402;
284 }
285 /*
286 * TODO: Make sure that we wait at least required delay but why we
287 * have to extend it one tick more?
288 */
289 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
290}
291
292static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
293{
294 struct i2c_client *client = chip->client;
295
296 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
297
298 (adc > chip->gainlevel->max) ?
299 chip->gainlevel++ : chip->gainlevel--;
300
Bryan Freedd9b42c02011-06-24 13:40:47 -0700301 i2c_smbus_write_byte_data(client,
302 TSL2563_CMD | TSL2563_REG_TIMING,
303 chip->gainlevel->gaintime);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200304
305 tsl2563_wait_adc(chip);
306 tsl2563_wait_adc(chip);
307
308 return 1;
309 } else
310 return 0;
311}
312
313static int tsl2563_get_adc(struct tsl2563_chip *chip)
314{
315 struct i2c_client *client = chip->client;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200316 u16 adc0, adc1;
317 int retry = 1;
318 int ret = 0;
319
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100320 if (chip->suspended)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200321 goto out;
322
Jonathan Cameron388be482010-06-26 12:54:21 +0100323 if (!chip->int_enabled) {
324 cancel_delayed_work(&chip->poweroff_work);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200325
Jonathan Cameron388be482010-06-26 12:54:21 +0100326 if (!tsl2563_get_power(chip)) {
327 ret = tsl2563_set_power(chip, 1);
328 if (ret)
329 goto out;
330 ret = tsl2563_configure(chip);
331 if (ret)
332 goto out;
333 tsl2563_wait_adc(chip);
334 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200335 }
336
337 while (retry) {
Bryan Freedd9b42c02011-06-24 13:40:47 -0700338 ret = i2c_smbus_read_word_data(client,
339 TSL2563_CMD | TSL2563_REG_DATA0LOW);
340 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200341 goto out;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700342 adc0 = ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200343
Bryan Freedd9b42c02011-06-24 13:40:47 -0700344 ret = i2c_smbus_read_word_data(client,
345 TSL2563_CMD | TSL2563_REG_DATA1LOW);
346 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200347 goto out;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700348 adc1 = ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200349
350 retry = tsl2563_adjust_gainlevel(chip, adc0);
351 }
352
353 chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime);
354 chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime);
355
Jonathan Cameron388be482010-06-26 12:54:21 +0100356 if (!chip->int_enabled)
357 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200358
359 ret = 0;
360out:
361 return ret;
362}
363
364static inline int calib_to_sysfs(u32 calib)
365{
366 return (int) (((calib * CALIB_BASE_SYSFS) +
367 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
368}
369
370static inline u32 calib_from_sysfs(int value)
371{
372 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
373}
374
375/*
376 * Conversions between lux and ADC values.
377 *
378 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
379 * appropriate constants. Different constants are needed for different
380 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
381 * of the intensities in infrared and visible wavelengths). lux_table below
382 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
383 * constants.
384 */
385
386struct tsl2563_lux_coeff {
387 unsigned long ch_ratio;
388 unsigned long ch0_coeff;
389 unsigned long ch1_coeff;
390};
391
392static const struct tsl2563_lux_coeff lux_table[] = {
393 {
394 .ch_ratio = FRAC10K(1300),
395 .ch0_coeff = FRAC10K(315),
396 .ch1_coeff = FRAC10K(262),
397 }, {
398 .ch_ratio = FRAC10K(2600),
399 .ch0_coeff = FRAC10K(337),
400 .ch1_coeff = FRAC10K(430),
401 }, {
402 .ch_ratio = FRAC10K(3900),
403 .ch0_coeff = FRAC10K(363),
404 .ch1_coeff = FRAC10K(529),
405 }, {
406 .ch_ratio = FRAC10K(5200),
407 .ch0_coeff = FRAC10K(392),
408 .ch1_coeff = FRAC10K(605),
409 }, {
410 .ch_ratio = FRAC10K(6500),
411 .ch0_coeff = FRAC10K(229),
412 .ch1_coeff = FRAC10K(291),
413 }, {
414 .ch_ratio = FRAC10K(8000),
415 .ch0_coeff = FRAC10K(157),
416 .ch1_coeff = FRAC10K(180),
417 }, {
418 .ch_ratio = FRAC10K(13000),
419 .ch0_coeff = FRAC10K(34),
420 .ch1_coeff = FRAC10K(26),
421 }, {
422 .ch_ratio = ULONG_MAX,
423 .ch0_coeff = 0,
424 .ch1_coeff = 0,
425 },
426};
427
Jonathan Camerona9e244f62013-01-12 10:35:00 +0000428/* Convert normalized, scaled ADC values to lux. */
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200429static 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
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200444/* Apply calibration coefficient to ADC count. */
445static u32 calib_adc(u32 adc, u32 calib)
446{
447 unsigned long scaled = adc;
448
449 scaled *= calib;
450 scaled >>= CALIB_FRAC_BITS;
451
452 return (u32) scaled;
453}
454
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100455static int tsl2563_write_raw(struct iio_dev *indio_dev,
456 struct iio_chan_spec const *chan,
457 int val,
458 int val2,
459 long mask)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200460{
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100461 struct tsl2563_chip *chip = iio_priv(indio_dev);
462
Jonathan Cameronfe3f8f82012-04-13 10:24:18 +0100463 if (chan->channel == IIO_MOD_LIGHT_BOTH)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100464 chip->calib0 = calib_from_sysfs(val);
465 else
466 chip->calib1 = calib_from_sysfs(val);
467
468 return 0;
469}
470
471static int tsl2563_read_raw(struct iio_dev *indio_dev,
472 struct iio_chan_spec const *chan,
473 int *val,
474 int *val2,
475 long m)
476{
477 int ret = -EINVAL;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200478 u32 calib0, calib1;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100479 struct tsl2563_chip *chip = iio_priv(indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200480
481 mutex_lock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100482 switch (m) {
Jonathan Cameron90354d02012-04-15 17:41:22 +0100483 case IIO_CHAN_INFO_RAW:
484 case IIO_CHAN_INFO_PROCESSED:
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100485 switch (chan->type) {
486 case IIO_LIGHT:
487 ret = tsl2563_get_adc(chip);
488 if (ret)
489 goto error_ret;
490 calib0 = calib_adc(chip->data0, chip->calib0) *
491 chip->cover_comp_gain;
492 calib1 = calib_adc(chip->data1, chip->calib1) *
493 chip->cover_comp_gain;
494 *val = adc_to_lux(calib0, calib1);
495 ret = IIO_VAL_INT;
496 break;
497 case IIO_INTENSITY:
498 ret = tsl2563_get_adc(chip);
499 if (ret)
500 goto error_ret;
501 if (chan->channel == 0)
502 *val = chip->data0;
503 else
504 *val = chip->data1;
505 ret = IIO_VAL_INT;
506 break;
507 default:
508 break;
509 }
Jonathan Cameron388be482010-06-26 12:54:21 +0100510 break;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100511
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100512 case IIO_CHAN_INFO_CALIBSCALE:
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100513 if (chan->channel == 0)
514 *val = calib_to_sysfs(chip->calib0);
515 else
516 *val = calib_to_sysfs(chip->calib1);
517 ret = IIO_VAL_INT;
Jonathan Cameron388be482010-06-26 12:54:21 +0100518 break;
519 default:
Dan Carpenter97d35f22011-10-06 09:15:03 +0300520 ret = -EINVAL;
521 goto error_ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100522 }
523
524error_ret:
525 mutex_unlock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100526 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100527}
528
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100529static const struct iio_chan_spec tsl2563_channels[] = {
Jonathan Cameron79939062011-08-30 12:41:16 +0100530 {
531 .type = IIO_LIGHT,
532 .indexed = 1,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000533 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
Jonathan Cameron79939062011-08-30 12:41:16 +0100534 .channel = 0,
535 }, {
536 .type = IIO_INTENSITY,
537 .modified = 1,
538 .channel2 = IIO_MOD_LIGHT_BOTH,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000539 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
540 BIT(IIO_CHAN_INFO_CALIBSCALE),
Jonathan Cameron79939062011-08-30 12:41:16 +0100541 .event_mask = (IIO_EV_BIT(IIO_EV_TYPE_THRESH,
542 IIO_EV_DIR_RISING) |
543 IIO_EV_BIT(IIO_EV_TYPE_THRESH,
544 IIO_EV_DIR_FALLING)),
545 }, {
546 .type = IIO_INTENSITY,
547 .modified = 1,
Jonathan Camerona7e3bd62011-10-26 17:27:36 +0100548 .channel2 = IIO_MOD_LIGHT_IR,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000549 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
550 BIT(IIO_CHAN_INFO_CALIBSCALE),
Jonathan Cameron79939062011-08-30 12:41:16 +0100551 }
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100552};
Jonathan Cameron388be482010-06-26 12:54:21 +0100553
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100554static int tsl2563_read_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100555 u64 event_code,
556 int *val)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100557{
558 struct tsl2563_chip *chip = iio_priv(indio_dev);
559
560 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
561 case IIO_EV_DIR_RISING:
562 *val = chip->high_thres;
563 break;
564 case IIO_EV_DIR_FALLING:
565 *val = chip->low_thres;
566 break;
567 default:
568 return -EINVAL;
569 }
570
571 return 0;
572}
573
Dan Carpenter15fbc192011-10-06 09:15:36 +0300574static int tsl2563_write_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100575 u64 event_code,
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100576 int val)
577{
578 struct tsl2563_chip *chip = iio_priv(indio_dev);
579 int ret;
580 u8 address;
581
582 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
583 address = TSL2563_REG_HIGHLOW;
584 else
585 address = TSL2563_REG_LOWLOW;
586 mutex_lock(&chip->lock);
Bryan Freedd9b42c02011-06-24 13:40:47 -0700587 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
588 val & 0xFF);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100589 if (ret)
590 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700591 ret = i2c_smbus_write_byte_data(chip->client,
592 TSL2563_CMD | (address + 1),
593 (val >> 8) & 0xFF);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100594 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
595 chip->high_thres = val;
596 else
597 chip->low_thres = val;
598
599error_ret:
600 mutex_unlock(&chip->lock);
601
602 return ret;
603}
Jonathan Cameron388be482010-06-26 12:54:21 +0100604
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100605static irqreturn_t tsl2563_event_handler(int irq, void *private)
Jonathan Cameron388be482010-06-26 12:54:21 +0100606{
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100607 struct iio_dev *dev_info = private;
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100608 struct tsl2563_chip *chip = iio_priv(dev_info);
Jonathan Cameron388be482010-06-26 12:54:21 +0100609
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100610 iio_push_event(dev_info,
Jonathan Cameronc4b14d92011-08-12 17:56:04 +0100611 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
Jonathan Cameronda1d8b62010-10-08 12:14:05 +0100612 0,
613 IIO_EV_TYPE_THRESH,
614 IIO_EV_DIR_EITHER),
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100615 iio_get_time_ns());
Jonathan Cameron388be482010-06-26 12:54:21 +0100616
Jonathan Cameron388be482010-06-26 12:54:21 +0100617 /* clear the interrupt and push the event */
Bryan Freedd9b42c02011-06-24 13:40:47 -0700618 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100619 return IRQ_HANDLED;
Jonathan Cameron388be482010-06-26 12:54:21 +0100620}
621
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100622static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100623 u64 event_code,
624 int state)
Jonathan Cameron388be482010-06-26 12:54:21 +0100625{
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100626 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100627 int ret = 0;
Jonathan Cameron388be482010-06-26 12:54:21 +0100628
Jonathan Cameron388be482010-06-26 12:54:21 +0100629 mutex_lock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100630 if (state && !(chip->intr & 0x30)) {
Jonathan Cameron388be482010-06-26 12:54:21 +0100631 chip->intr &= ~0x30;
632 chip->intr |= 0x10;
633 /* ensure the chip is actually on */
634 cancel_delayed_work(&chip->poweroff_work);
635 if (!tsl2563_get_power(chip)) {
636 ret = tsl2563_set_power(chip, 1);
637 if (ret)
638 goto out;
639 ret = tsl2563_configure(chip);
640 if (ret)
641 goto out;
642 }
Bryan Freedd9b42c02011-06-24 13:40:47 -0700643 ret = i2c_smbus_write_byte_data(chip->client,
644 TSL2563_CMD | TSL2563_REG_INT,
645 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100646 chip->int_enabled = true;
647 }
648
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100649 if (!state && (chip->intr & 0x30)) {
Derek Basehore95273f82012-10-05 00:54:00 +0100650 chip->intr &= ~0x30;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700651 ret = i2c_smbus_write_byte_data(chip->client,
652 TSL2563_CMD | TSL2563_REG_INT,
653 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100654 chip->int_enabled = false;
655 /* now the interrupt is not enabled, we can go to sleep */
656 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
657 }
658out:
659 mutex_unlock(&chip->lock);
660
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100661 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100662}
663
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100664static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100665 u64 event_code)
Jonathan Cameron388be482010-06-26 12:54:21 +0100666{
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100667 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100668 int ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100669
670 mutex_lock(&chip->lock);
Bryan Freedd9b42c02011-06-24 13:40:47 -0700671 ret = i2c_smbus_read_byte_data(chip->client,
672 TSL2563_CMD | TSL2563_REG_INT);
Jonathan Cameron388be482010-06-26 12:54:21 +0100673 mutex_unlock(&chip->lock);
674 if (ret < 0)
Jonathan Camerona722dcc2013-01-12 10:35:00 +0000675 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100676
Jonathan Camerona722dcc2013-01-12 10:35:00 +0000677 return !!(ret & 0x30);
Jonathan Cameron388be482010-06-26 12:54:21 +0100678}
Jonathan Cameron388be482010-06-26 12:54:21 +0100679
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100680static const struct iio_info tsl2563_info_no_irq = {
681 .driver_module = THIS_MODULE,
Bryan Freed9e4216f2011-06-21 15:54:57 -0700682 .read_raw = &tsl2563_read_raw,
683 .write_raw = &tsl2563_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100684};
685
686static const struct iio_info tsl2563_info = {
687 .driver_module = THIS_MODULE,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100688 .read_raw = &tsl2563_read_raw,
689 .write_raw = &tsl2563_write_raw,
690 .read_event_value = &tsl2563_read_thresh,
691 .write_event_value = &tsl2563_write_thresh,
692 .read_event_config = &tsl2563_read_interrupt_config,
693 .write_event_config = &tsl2563_write_interrupt_config,
694};
695
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500696static int tsl2563_probe(struct i2c_client *client,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200697 const struct i2c_device_id *device_id)
698{
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100699 struct iio_dev *indio_dev;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200700 struct tsl2563_chip *chip;
701 struct tsl2563_platform_data *pdata = client->dev.platform_data;
702 int err = 0;
Jonathan Camerondeda3862011-08-12 17:08:35 +0100703 u8 id = 0;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200704
Sachin Kamatbace48f2013-07-30 09:44:00 +0100705 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100706 if (!indio_dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200707 return -ENOMEM;
708
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100709 chip = iio_priv(indio_dev);
710
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200711 i2c_set_clientdata(client, chip);
712 chip->client = client;
713
714 err = tsl2563_detect(chip);
715 if (err) {
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800716 dev_err(&client->dev, "detect error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100717 return err;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200718 }
719
720 err = tsl2563_read_id(chip, &id);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800721 if (err) {
722 dev_err(&client->dev, "read id error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100723 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800724 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200725
726 mutex_init(&chip->lock);
727
728 /* Default values used until userspace says otherwise */
729 chip->low_thres = 0x0;
730 chip->high_thres = 0xffff;
731 chip->gainlevel = tsl2563_gainlevel_table;
732 chip->intr = TSL2563_INT_PERSIST(4);
733 chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS);
734 chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS);
735
736 if (pdata)
737 chip->cover_comp_gain = pdata->cover_comp_gain;
738 else
739 chip->cover_comp_gain = 1;
740
741 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100742 indio_dev->name = client->name;
743 indio_dev->channels = tsl2563_channels;
744 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100745 indio_dev->dev.parent = &client->dev;
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100746 indio_dev->modes = INDIO_DIRECT_MODE;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800747
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100748 if (client->irq)
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100749 indio_dev->info = &tsl2563_info;
750 else
751 indio_dev->info = &tsl2563_info_no_irq;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800752
Jonathan Cameron388be482010-06-26 12:54:21 +0100753 if (client->irq) {
Sachin Kamatbace48f2013-07-30 09:44:00 +0100754 err = devm_request_threaded_irq(&client->dev, client->irq,
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100755 NULL,
756 &tsl2563_event_handler,
757 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
758 "tsl2563_event",
759 indio_dev);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800760 if (err) {
761 dev_err(&client->dev, "irq request error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100762 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800763 }
Jonathan Cameron388be482010-06-26 12:54:21 +0100764 }
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800765
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200766 err = tsl2563_configure(chip);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800767 if (err) {
768 dev_err(&client->dev, "configure error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100769 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800770 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200771
772 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800773
Jonathan Cameron388be482010-06-26 12:54:21 +0100774 /* The interrupt cannot yet be enabled so this is fine without lock */
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200775 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
776
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800777 err = iio_device_register(indio_dev);
778 if (err) {
779 dev_err(&client->dev, "iio registration error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100780 goto fail;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800781 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100782
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200783 return 0;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800784
Sachin Kamatbace48f2013-07-30 09:44:00 +0100785fail:
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800786 cancel_delayed_work(&chip->poweroff_work);
787 flush_scheduled_work();
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200788 return err;
789}
790
Bill Pemberton447d4f22012-11-19 13:26:37 -0500791static int tsl2563_remove(struct i2c_client *client)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200792{
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 */
Derek Basehore95273f82012-10-05 00:54:00 +0100800 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);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200805
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200806 return 0;
807}
808
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100809#ifdef CONFIG_PM_SLEEP
810static int tsl2563_suspend(struct device *dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200811{
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100812 struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200813 int ret;
814
815 mutex_lock(&chip->lock);
816
817 ret = tsl2563_set_power(chip, 0);
818 if (ret)
819 goto out;
820
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100821 chip->suspended = true;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200822
823out:
824 mutex_unlock(&chip->lock);
825 return ret;
826}
827
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100828static int tsl2563_resume(struct device *dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200829{
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100830 struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200831 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
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100843 chip->suspended = false;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200844
845out:
846 mutex_unlock(&chip->lock);
847 return ret;
848}
849
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100850static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume);
851#define TSL2563_PM_OPS (&tsl2563_pm_ops)
852#else
853#define TSL2563_PM_OPS NULL
854#endif
855
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200856static const struct i2c_device_id tsl2563_id[] = {
Jonathan Camerondbd5d232009-11-22 16:03:25 +0000857 { "tsl2560", 0 },
858 { "tsl2561", 1 },
859 { "tsl2562", 2 },
860 { "tsl2563", 3 },
861 {}
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200862};
863MODULE_DEVICE_TABLE(i2c, tsl2563_id);
864
865static struct i2c_driver tsl2563_i2c_driver = {
866 .driver = {
Jonathan Camerondbd5d232009-11-22 16:03:25 +0000867 .name = "tsl2563",
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100868 .pm = TSL2563_PM_OPS,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200869 },
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200870 .probe = tsl2563_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500871 .remove = tsl2563_remove,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200872 .id_table = tsl2563_id,
873};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100874module_i2c_driver(tsl2563_i2c_driver);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200875
876MODULE_AUTHOR("Nokia Corporation");
877MODULE_DESCRIPTION("tsl2563 light sensor driver");
878MODULE_LICENSE("GPL");