blob: d8c40a83097d3c0e856ba3214df4ab73a26e1140 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +02002/*
Jonathan Cameron9c2251d2013-01-12 10:35:00 +00003 * drivers/iio/light/tsl2563.c
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +02004 *
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
8 * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
9 *
10 * Converted to IIO driver
11 * Amit Kucheria <amit.kucheria@verdurent.com>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020012 */
13
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/interrupt.h>
Jonathan Cameron388be482010-06-26 12:54:21 +010017#include <linux/irq.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020018#include <linux/sched.h>
19#include <linux/mutex.h>
20#include <linux/delay.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020021#include <linux/pm.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020022#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020024
Jonathan Cameron06458e22012-04-25 15:54:58 +010025#include <linux/iio/iio.h>
26#include <linux/iio/sysfs.h>
27#include <linux/iio/events.h>
Jonathan Cameron9c2251d2013-01-12 10:35:00 +000028#include <linux/platform_data/tsl2563.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020029
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020030/* Use this many bits for fraction part. */
Jonathan Cameron5ade7632013-01-12 10:35:00 +000031#define ADC_FRAC_BITS 14
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020032
33/* Given number of 1/10000's in ADC_FRAC_BITS precision. */
34#define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
35
36/* Bits used for fraction in calibration coefficients.*/
Jonathan Cameron5ade7632013-01-12 10:35:00 +000037#define CALIB_FRAC_BITS 10
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020038/* 0.5 in CALIB_FRAC_BITS precision */
39#define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1))
40/* Make a fraction from a number n that was multiplied with b. */
41#define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b))
42/* Decimal 10^(digits in sysfs presentation) */
Jonathan Cameron5ade7632013-01-12 10:35:00 +000043#define CALIB_BASE_SYSFS 1000
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020044
Jonathan Cameron5ade7632013-01-12 10:35:00 +000045#define TSL2563_CMD 0x80
46#define TSL2563_CLEARINT 0x40
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020047
Jonathan Cameron5ade7632013-01-12 10:35:00 +000048#define TSL2563_REG_CTRL 0x00
49#define TSL2563_REG_TIMING 0x01
50#define TSL2563_REG_LOWLOW 0x02 /* data0 low threshold, 2 bytes */
51#define TSL2563_REG_LOWHIGH 0x03
52#define TSL2563_REG_HIGHLOW 0x04 /* data0 high threshold, 2 bytes */
53#define TSL2563_REG_HIGHHIGH 0x05
54#define TSL2563_REG_INT 0x06
55#define TSL2563_REG_ID 0x0a
56#define TSL2563_REG_DATA0LOW 0x0c /* broadband sensor value, 2 bytes */
57#define TSL2563_REG_DATA0HIGH 0x0d
58#define TSL2563_REG_DATA1LOW 0x0e /* infrared sensor value, 2 bytes */
59#define TSL2563_REG_DATA1HIGH 0x0f
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020060
Jonathan Cameron5ade7632013-01-12 10:35:00 +000061#define TSL2563_CMD_POWER_ON 0x03
62#define TSL2563_CMD_POWER_OFF 0x00
63#define TSL2563_CTRL_POWER_MASK 0x03
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020064
Jonathan Cameron5ade7632013-01-12 10:35:00 +000065#define TSL2563_TIMING_13MS 0x00
66#define TSL2563_TIMING_100MS 0x01
67#define TSL2563_TIMING_400MS 0x02
68#define TSL2563_TIMING_MASK 0x03
69#define TSL2563_TIMING_GAIN16 0x10
70#define TSL2563_TIMING_GAIN1 0x00
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020071
Jonathan Cameron5ade7632013-01-12 10:35:00 +000072#define TSL2563_INT_DISBLED 0x00
73#define TSL2563_INT_LEVEL 0x10
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020074#define TSL2563_INT_PERSIST(n) ((n) & 0x0F)
75
76struct tsl2563_gainlevel_coeff {
77 u8 gaintime;
78 u16 min;
79 u16 max;
80};
81
Jonathan Cameron1ff7e1d2011-04-18 12:58:56 +010082static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020083 {
84 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
85 .min = 0,
86 .max = 65534,
87 }, {
88 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
89 .min = 2048,
90 .max = 65534,
91 }, {
92 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
93 .min = 4095,
94 .max = 37177,
95 }, {
96 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
97 .min = 3000,
98 .max = 65535,
99 },
100};
101
102struct tsl2563_chip {
103 struct mutex lock;
104 struct i2c_client *client;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200105 struct delayed_work poweroff_work;
106
107 /* Remember state for suspend and resume functions */
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100108 bool suspended;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200109
Jonathan Cameron1ff7e1d2011-04-18 12:58:56 +0100110 struct tsl2563_gainlevel_coeff const *gainlevel;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200111
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200112 u16 low_thres;
113 u16 high_thres;
114 u8 intr;
Jonathan Cameron388be482010-06-26 12:54:21 +0100115 bool int_enabled;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200116
117 /* Calibration coefficients */
118 u32 calib0;
119 u32 calib1;
120 int cover_comp_gain;
121
122 /* Cache current values, to be returned while suspended */
123 u32 data0;
124 u32 data1;
125};
126
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200127static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
128{
129 struct i2c_client *client = chip->client;
130 u8 cmd;
131
132 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700133 return i2c_smbus_write_byte_data(client,
134 TSL2563_CMD | TSL2563_REG_CTRL, cmd);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200135}
136
137/*
138 * Return value is 0 for off, 1 for on, or a negative error
139 * code if reading failed.
140 */
141static int tsl2563_get_power(struct tsl2563_chip *chip)
142{
143 struct i2c_client *client = chip->client;
144 int ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200145
Bryan Freedd9b42c02011-06-24 13:40:47 -0700146 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
147 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200148 return ret;
149
Bryan Freedd9b42c02011-06-24 13:40:47 -0700150 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200151}
152
153static int tsl2563_configure(struct tsl2563_chip *chip)
154{
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200155 int ret;
156
Bryan Freedd9b42c02011-06-24 13:40:47 -0700157 ret = i2c_smbus_write_byte_data(chip->client,
158 TSL2563_CMD | TSL2563_REG_TIMING,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200159 chip->gainlevel->gaintime);
160 if (ret)
Jonathan Cameron388be482010-06-26 12:54:21 +0100161 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700162 ret = i2c_smbus_write_byte_data(chip->client,
163 TSL2563_CMD | TSL2563_REG_HIGHLOW,
Jonathan Cameron388be482010-06-26 12:54:21 +0100164 chip->high_thres & 0xFF);
165 if (ret)
166 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700167 ret = i2c_smbus_write_byte_data(chip->client,
168 TSL2563_CMD | TSL2563_REG_HIGHHIGH,
Jonathan Cameron388be482010-06-26 12:54:21 +0100169 (chip->high_thres >> 8) & 0xFF);
170 if (ret)
171 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700172 ret = i2c_smbus_write_byte_data(chip->client,
173 TSL2563_CMD | TSL2563_REG_LOWLOW,
Jonathan Cameron388be482010-06-26 12:54:21 +0100174 chip->low_thres & 0xFF);
175 if (ret)
176 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700177 ret = i2c_smbus_write_byte_data(chip->client,
178 TSL2563_CMD | TSL2563_REG_LOWHIGH,
Jonathan Cameron388be482010-06-26 12:54:21 +0100179 (chip->low_thres >> 8) & 0xFF);
Jonathan Camerona9e244f62013-01-12 10:35:00 +0000180/*
181 * Interrupt register is automatically written anyway if it is relevant
182 * so is not here.
183 */
Jonathan Cameron388be482010-06-26 12:54:21 +0100184error_ret:
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200185 return ret;
186}
187
188static void tsl2563_poweroff_work(struct work_struct *work)
189{
190 struct tsl2563_chip *chip =
191 container_of(work, struct tsl2563_chip, poweroff_work.work);
192 tsl2563_set_power(chip, 0);
193}
194
195static int tsl2563_detect(struct tsl2563_chip *chip)
196{
197 int ret;
198
199 ret = tsl2563_set_power(chip, 1);
200 if (ret)
201 return ret;
202
203 ret = tsl2563_get_power(chip);
204 if (ret < 0)
205 return ret;
206
207 return ret ? 0 : -ENODEV;
208}
209
210static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
211{
212 struct i2c_client *client = chip->client;
213 int ret;
214
Bryan Freedd9b42c02011-06-24 13:40:47 -0700215 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
216 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200217 return ret;
218
Maxin B. John22dc09c2011-10-26 17:27:37 +0100219 *id = ret;
220
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200221 return 0;
222}
223
224/*
225 * "Normalized" ADC value is one obtained with 400ms of integration time and
226 * 16x gain. This function returns the number of bits of shift needed to
227 * convert between normalized values and HW values obtained using given
228 * timing and gain settings.
229 */
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200230static int tsl2563_adc_shiftbits(u8 timing)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200231{
232 int shift = 0;
233
234 switch (timing & TSL2563_TIMING_MASK) {
235 case TSL2563_TIMING_13MS:
236 shift += 5;
237 break;
238 case TSL2563_TIMING_100MS:
239 shift += 2;
240 break;
241 case TSL2563_TIMING_400MS:
242 /* no-op */
243 break;
244 }
245
246 if (!(timing & TSL2563_TIMING_GAIN16))
247 shift += 4;
248
249 return shift;
250}
251
252/* Convert a HW ADC value to normalized scale. */
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200253static u32 tsl2563_normalize_adc(u16 adc, u8 timing)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200254{
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200255 return adc << tsl2563_adc_shiftbits(timing);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200256}
257
258static void tsl2563_wait_adc(struct tsl2563_chip *chip)
259{
260 unsigned int delay;
261
262 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
263 case TSL2563_TIMING_13MS:
264 delay = 14;
265 break;
266 case TSL2563_TIMING_100MS:
267 delay = 101;
268 break;
269 default:
270 delay = 402;
271 }
272 /*
273 * TODO: Make sure that we wait at least required delay but why we
274 * have to extend it one tick more?
275 */
276 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
277}
278
279static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
280{
281 struct i2c_client *client = chip->client;
282
283 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
284
285 (adc > chip->gainlevel->max) ?
286 chip->gainlevel++ : chip->gainlevel--;
287
Bryan Freedd9b42c02011-06-24 13:40:47 -0700288 i2c_smbus_write_byte_data(client,
289 TSL2563_CMD | TSL2563_REG_TIMING,
290 chip->gainlevel->gaintime);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200291
292 tsl2563_wait_adc(chip);
293 tsl2563_wait_adc(chip);
294
295 return 1;
296 } else
297 return 0;
298}
299
300static int tsl2563_get_adc(struct tsl2563_chip *chip)
301{
302 struct i2c_client *client = chip->client;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200303 u16 adc0, adc1;
304 int retry = 1;
305 int ret = 0;
306
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100307 if (chip->suspended)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200308 goto out;
309
Jonathan Cameron388be482010-06-26 12:54:21 +0100310 if (!chip->int_enabled) {
311 cancel_delayed_work(&chip->poweroff_work);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200312
Jonathan Cameron388be482010-06-26 12:54:21 +0100313 if (!tsl2563_get_power(chip)) {
314 ret = tsl2563_set_power(chip, 1);
315 if (ret)
316 goto out;
317 ret = tsl2563_configure(chip);
318 if (ret)
319 goto out;
320 tsl2563_wait_adc(chip);
321 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200322 }
323
324 while (retry) {
Bryan Freedd9b42c02011-06-24 13:40:47 -0700325 ret = i2c_smbus_read_word_data(client,
326 TSL2563_CMD | TSL2563_REG_DATA0LOW);
327 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200328 goto out;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700329 adc0 = ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200330
Bryan Freedd9b42c02011-06-24 13:40:47 -0700331 ret = i2c_smbus_read_word_data(client,
332 TSL2563_CMD | TSL2563_REG_DATA1LOW);
333 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200334 goto out;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700335 adc1 = ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200336
337 retry = tsl2563_adjust_gainlevel(chip, adc0);
338 }
339
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200340 chip->data0 = tsl2563_normalize_adc(adc0, chip->gainlevel->gaintime);
341 chip->data1 = tsl2563_normalize_adc(adc1, chip->gainlevel->gaintime);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200342
Jonathan Cameron388be482010-06-26 12:54:21 +0100343 if (!chip->int_enabled)
344 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200345
346 ret = 0;
347out:
348 return ret;
349}
350
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200351static inline int tsl2563_calib_to_sysfs(u32 calib)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200352{
353 return (int) (((calib * CALIB_BASE_SYSFS) +
354 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
355}
356
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200357static inline u32 tsl2563_calib_from_sysfs(int value)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200358{
359 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
360}
361
362/*
363 * Conversions between lux and ADC values.
364 *
365 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
366 * appropriate constants. Different constants are needed for different
367 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
368 * of the intensities in infrared and visible wavelengths). lux_table below
369 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
370 * constants.
371 */
372
373struct tsl2563_lux_coeff {
374 unsigned long ch_ratio;
375 unsigned long ch0_coeff;
376 unsigned long ch1_coeff;
377};
378
379static const struct tsl2563_lux_coeff lux_table[] = {
380 {
381 .ch_ratio = FRAC10K(1300),
382 .ch0_coeff = FRAC10K(315),
383 .ch1_coeff = FRAC10K(262),
384 }, {
385 .ch_ratio = FRAC10K(2600),
386 .ch0_coeff = FRAC10K(337),
387 .ch1_coeff = FRAC10K(430),
388 }, {
389 .ch_ratio = FRAC10K(3900),
390 .ch0_coeff = FRAC10K(363),
391 .ch1_coeff = FRAC10K(529),
392 }, {
393 .ch_ratio = FRAC10K(5200),
394 .ch0_coeff = FRAC10K(392),
395 .ch1_coeff = FRAC10K(605),
396 }, {
397 .ch_ratio = FRAC10K(6500),
398 .ch0_coeff = FRAC10K(229),
399 .ch1_coeff = FRAC10K(291),
400 }, {
401 .ch_ratio = FRAC10K(8000),
402 .ch0_coeff = FRAC10K(157),
403 .ch1_coeff = FRAC10K(180),
404 }, {
405 .ch_ratio = FRAC10K(13000),
406 .ch0_coeff = FRAC10K(34),
407 .ch1_coeff = FRAC10K(26),
408 }, {
409 .ch_ratio = ULONG_MAX,
410 .ch0_coeff = 0,
411 .ch1_coeff = 0,
412 },
413};
414
Jonathan Camerona9e244f62013-01-12 10:35:00 +0000415/* Convert normalized, scaled ADC values to lux. */
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200416static unsigned int tsl2563_adc_to_lux(u32 adc0, u32 adc1)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200417{
418 const struct tsl2563_lux_coeff *lp = lux_table;
419 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
420
421 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
422
423 while (lp->ch_ratio < ratio)
424 lp++;
425
426 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
427
428 return (unsigned int) (lux >> ADC_FRAC_BITS);
429}
430
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200431/* Apply calibration coefficient to ADC count. */
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200432static u32 tsl2563_calib_adc(u32 adc, u32 calib)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200433{
434 unsigned long scaled = adc;
435
436 scaled *= calib;
437 scaled >>= CALIB_FRAC_BITS;
438
439 return (u32) scaled;
440}
441
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100442static int tsl2563_write_raw(struct iio_dev *indio_dev,
443 struct iio_chan_spec const *chan,
444 int val,
445 int val2,
446 long mask)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200447{
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100448 struct tsl2563_chip *chip = iio_priv(indio_dev);
449
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000450 if (mask != IIO_CHAN_INFO_CALIBSCALE)
451 return -EINVAL;
452 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200453 chip->calib0 = tsl2563_calib_from_sysfs(val);
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000454 else if (chan->channel2 == IIO_MOD_LIGHT_IR)
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200455 chip->calib1 = tsl2563_calib_from_sysfs(val);
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000456 else
457 return -EINVAL;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100458
459 return 0;
460}
461
462static int tsl2563_read_raw(struct iio_dev *indio_dev,
463 struct iio_chan_spec const *chan,
464 int *val,
465 int *val2,
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000466 long mask)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100467{
468 int ret = -EINVAL;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200469 u32 calib0, calib1;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100470 struct tsl2563_chip *chip = iio_priv(indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200471
472 mutex_lock(&chip->lock);
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000473 switch (mask) {
Jonathan Cameron90354d02012-04-15 17:41:22 +0100474 case IIO_CHAN_INFO_RAW:
475 case IIO_CHAN_INFO_PROCESSED:
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100476 switch (chan->type) {
477 case IIO_LIGHT:
478 ret = tsl2563_get_adc(chip);
479 if (ret)
480 goto error_ret;
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200481 calib0 = tsl2563_calib_adc(chip->data0, chip->calib0) *
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100482 chip->cover_comp_gain;
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200483 calib1 = tsl2563_calib_adc(chip->data1, chip->calib1) *
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100484 chip->cover_comp_gain;
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200485 *val = tsl2563_adc_to_lux(calib0, calib1);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100486 ret = IIO_VAL_INT;
487 break;
488 case IIO_INTENSITY:
489 ret = tsl2563_get_adc(chip);
490 if (ret)
491 goto error_ret;
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000492 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100493 *val = chip->data0;
494 else
495 *val = chip->data1;
496 ret = IIO_VAL_INT;
497 break;
498 default:
499 break;
500 }
Jonathan Cameron388be482010-06-26 12:54:21 +0100501 break;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100502
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100503 case IIO_CHAN_INFO_CALIBSCALE:
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000504 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200505 *val = tsl2563_calib_to_sysfs(chip->calib0);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100506 else
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200507 *val = tsl2563_calib_to_sysfs(chip->calib1);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100508 ret = IIO_VAL_INT;
Jonathan Cameron388be482010-06-26 12:54:21 +0100509 break;
510 default:
Dan Carpenter97d35f22011-10-06 09:15:03 +0300511 ret = -EINVAL;
512 goto error_ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100513 }
514
515error_ret:
516 mutex_unlock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100517 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100518}
519
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100520static const struct iio_event_spec tsl2563_events[] = {
521 {
522 .type = IIO_EV_TYPE_THRESH,
523 .dir = IIO_EV_DIR_RISING,
524 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
525 BIT(IIO_EV_INFO_ENABLE),
526 }, {
527 .type = IIO_EV_TYPE_THRESH,
528 .dir = IIO_EV_DIR_FALLING,
529 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
530 BIT(IIO_EV_INFO_ENABLE),
531 },
532};
533
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100534static const struct iio_chan_spec tsl2563_channels[] = {
Jonathan Cameron79939062011-08-30 12:41:16 +0100535 {
536 .type = IIO_LIGHT,
537 .indexed = 1,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000538 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
Jonathan Cameron79939062011-08-30 12:41:16 +0100539 .channel = 0,
540 }, {
541 .type = IIO_INTENSITY,
542 .modified = 1,
543 .channel2 = IIO_MOD_LIGHT_BOTH,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000544 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
545 BIT(IIO_CHAN_INFO_CALIBSCALE),
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100546 .event_spec = tsl2563_events,
547 .num_event_specs = ARRAY_SIZE(tsl2563_events),
Jonathan Cameron79939062011-08-30 12:41:16 +0100548 }, {
549 .type = IIO_INTENSITY,
550 .modified = 1,
Jonathan Camerona7e3bd62011-10-26 17:27:36 +0100551 .channel2 = IIO_MOD_LIGHT_IR,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000552 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
553 BIT(IIO_CHAN_INFO_CALIBSCALE),
Jonathan Cameron79939062011-08-30 12:41:16 +0100554 }
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100555};
Jonathan Cameron388be482010-06-26 12:54:21 +0100556
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100557static int tsl2563_read_thresh(struct iio_dev *indio_dev,
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100558 const struct iio_chan_spec *chan, enum iio_event_type type,
559 enum iio_event_direction dir, enum iio_event_info info, int *val,
560 int *val2)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100561{
562 struct tsl2563_chip *chip = iio_priv(indio_dev);
563
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100564 switch (dir) {
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100565 case IIO_EV_DIR_RISING:
566 *val = chip->high_thres;
567 break;
568 case IIO_EV_DIR_FALLING:
569 *val = chip->low_thres;
570 break;
571 default:
572 return -EINVAL;
573 }
574
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100575 return IIO_VAL_INT;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100576}
577
Dan Carpenter15fbc192011-10-06 09:15:36 +0300578static int tsl2563_write_thresh(struct iio_dev *indio_dev,
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100579 const struct iio_chan_spec *chan, enum iio_event_type type,
580 enum iio_event_direction dir, enum iio_event_info info, int val,
581 int val2)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100582{
583 struct tsl2563_chip *chip = iio_priv(indio_dev);
584 int ret;
585 u8 address;
586
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100587 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100588 address = TSL2563_REG_HIGHLOW;
589 else
590 address = TSL2563_REG_LOWLOW;
591 mutex_lock(&chip->lock);
Bryan Freedd9b42c02011-06-24 13:40:47 -0700592 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
593 val & 0xFF);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100594 if (ret)
595 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700596 ret = i2c_smbus_write_byte_data(chip->client,
597 TSL2563_CMD | (address + 1),
598 (val >> 8) & 0xFF);
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100599 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100600 chip->high_thres = val;
601 else
602 chip->low_thres = val;
603
604error_ret:
605 mutex_unlock(&chip->lock);
606
607 return ret;
608}
Jonathan Cameron388be482010-06-26 12:54:21 +0100609
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100610static irqreturn_t tsl2563_event_handler(int irq, void *private)
Jonathan Cameron388be482010-06-26 12:54:21 +0100611{
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100612 struct iio_dev *dev_info = private;
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100613 struct tsl2563_chip *chip = iio_priv(dev_info);
Jonathan Cameron388be482010-06-26 12:54:21 +0100614
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100615 iio_push_event(dev_info,
Akinobu Mitaa3507e42017-06-21 01:46:37 +0900616 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
Jonathan Cameronda1d8b62010-10-08 12:14:05 +0100617 0,
618 IIO_EV_TYPE_THRESH,
619 IIO_EV_DIR_EITHER),
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100620 iio_get_time_ns(dev_info));
Jonathan Cameron388be482010-06-26 12:54:21 +0100621
Jonathan Cameron388be482010-06-26 12:54:21 +0100622 /* clear the interrupt and push the event */
Bryan Freedd9b42c02011-06-24 13:40:47 -0700623 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100624 return IRQ_HANDLED;
Jonathan Cameron388be482010-06-26 12:54:21 +0100625}
626
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100627static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100628 const struct iio_chan_spec *chan, enum iio_event_type type,
629 enum iio_event_direction dir, int state)
Jonathan Cameron388be482010-06-26 12:54:21 +0100630{
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100631 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100632 int ret = 0;
Jonathan Cameron388be482010-06-26 12:54:21 +0100633
Jonathan Cameron388be482010-06-26 12:54:21 +0100634 mutex_lock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100635 if (state && !(chip->intr & 0x30)) {
Jonathan Cameron388be482010-06-26 12:54:21 +0100636 chip->intr &= ~0x30;
637 chip->intr |= 0x10;
638 /* ensure the chip is actually on */
639 cancel_delayed_work(&chip->poweroff_work);
640 if (!tsl2563_get_power(chip)) {
641 ret = tsl2563_set_power(chip, 1);
642 if (ret)
643 goto out;
644 ret = tsl2563_configure(chip);
645 if (ret)
646 goto out;
647 }
Bryan Freedd9b42c02011-06-24 13:40:47 -0700648 ret = i2c_smbus_write_byte_data(chip->client,
649 TSL2563_CMD | TSL2563_REG_INT,
650 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100651 chip->int_enabled = true;
652 }
653
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100654 if (!state && (chip->intr & 0x30)) {
Derek Basehore95273f82012-10-05 00:54:00 +0100655 chip->intr &= ~0x30;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700656 ret = i2c_smbus_write_byte_data(chip->client,
657 TSL2563_CMD | TSL2563_REG_INT,
658 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100659 chip->int_enabled = false;
660 /* now the interrupt is not enabled, we can go to sleep */
661 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
662 }
663out:
664 mutex_unlock(&chip->lock);
665
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100666 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100667}
668
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100669static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100670 const struct iio_chan_spec *chan, enum iio_event_type type,
671 enum iio_event_direction dir)
Jonathan Cameron388be482010-06-26 12:54:21 +0100672{
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100673 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100674 int ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100675
676 mutex_lock(&chip->lock);
Bryan Freedd9b42c02011-06-24 13:40:47 -0700677 ret = i2c_smbus_read_byte_data(chip->client,
678 TSL2563_CMD | TSL2563_REG_INT);
Jonathan Cameron388be482010-06-26 12:54:21 +0100679 mutex_unlock(&chip->lock);
680 if (ret < 0)
Jonathan Camerona722dcc2013-01-12 10:35:00 +0000681 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100682
Jonathan Camerona722dcc2013-01-12 10:35:00 +0000683 return !!(ret & 0x30);
Jonathan Cameron388be482010-06-26 12:54:21 +0100684}
Jonathan Cameron388be482010-06-26 12:54:21 +0100685
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100686static const struct iio_info tsl2563_info_no_irq = {
Bryan Freed9e4216f2011-06-21 15:54:57 -0700687 .read_raw = &tsl2563_read_raw,
688 .write_raw = &tsl2563_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100689};
690
691static const struct iio_info tsl2563_info = {
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100692 .read_raw = &tsl2563_read_raw,
693 .write_raw = &tsl2563_write_raw,
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000694 .read_event_value = &tsl2563_read_thresh,
695 .write_event_value = &tsl2563_write_thresh,
696 .read_event_config = &tsl2563_read_interrupt_config,
697 .write_event_config = &tsl2563_write_interrupt_config,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100698};
699
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500700static int tsl2563_probe(struct i2c_client *client,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200701 const struct i2c_device_id *device_id)
702{
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100703 struct iio_dev *indio_dev;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200704 struct tsl2563_chip *chip;
705 struct tsl2563_platform_data *pdata = client->dev.platform_data;
Sebastian Reichel8175bff2013-10-25 10:10:00 +0100706 struct device_node *np = client->dev.of_node;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200707 int err = 0;
Jonathan Camerondeda3862011-08-12 17:08:35 +0100708 u8 id = 0;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200709
Sachin Kamatbace48f2013-07-30 09:44:00 +0100710 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100711 if (!indio_dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200712 return -ENOMEM;
713
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100714 chip = iio_priv(indio_dev);
715
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200716 i2c_set_clientdata(client, chip);
717 chip->client = client;
718
719 err = tsl2563_detect(chip);
720 if (err) {
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800721 dev_err(&client->dev, "detect error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100722 return err;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200723 }
724
725 err = tsl2563_read_id(chip, &id);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800726 if (err) {
727 dev_err(&client->dev, "read id error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100728 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800729 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200730
731 mutex_init(&chip->lock);
732
733 /* Default values used until userspace says otherwise */
734 chip->low_thres = 0x0;
735 chip->high_thres = 0xffff;
736 chip->gainlevel = tsl2563_gainlevel_table;
737 chip->intr = TSL2563_INT_PERSIST(4);
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200738 chip->calib0 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
739 chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200740
741 if (pdata)
742 chip->cover_comp_gain = pdata->cover_comp_gain;
Sebastian Reichel8175bff2013-10-25 10:10:00 +0100743 else if (np)
744 of_property_read_u32(np, "amstaos,cover-comp-gain",
745 &chip->cover_comp_gain);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200746 else
747 chip->cover_comp_gain = 1;
748
749 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100750 indio_dev->name = client->name;
751 indio_dev->channels = tsl2563_channels;
752 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100753 indio_dev->dev.parent = &client->dev;
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100754 indio_dev->modes = INDIO_DIRECT_MODE;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800755
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100756 if (client->irq)
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100757 indio_dev->info = &tsl2563_info;
758 else
759 indio_dev->info = &tsl2563_info_no_irq;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800760
Jonathan Cameron388be482010-06-26 12:54:21 +0100761 if (client->irq) {
Sachin Kamatbace48f2013-07-30 09:44:00 +0100762 err = devm_request_threaded_irq(&client->dev, client->irq,
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100763 NULL,
764 &tsl2563_event_handler,
765 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
766 "tsl2563_event",
767 indio_dev);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800768 if (err) {
769 dev_err(&client->dev, "irq request error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100770 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800771 }
Jonathan Cameron388be482010-06-26 12:54:21 +0100772 }
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800773
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200774 err = tsl2563_configure(chip);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800775 if (err) {
776 dev_err(&client->dev, "configure error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100777 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800778 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200779
780 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800781
Jonathan Cameron388be482010-06-26 12:54:21 +0100782 /* The interrupt cannot yet be enabled so this is fine without lock */
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200783 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
784
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800785 err = iio_device_register(indio_dev);
786 if (err) {
787 dev_err(&client->dev, "iio registration error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100788 goto fail;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800789 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100790
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200791 return 0;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800792
Sachin Kamatbace48f2013-07-30 09:44:00 +0100793fail:
Amitoj Kaur Chawla9e61d902016-03-17 19:25:11 +0530794 cancel_delayed_work_sync(&chip->poweroff_work);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200795 return err;
796}
797
Bill Pemberton447d4f22012-11-19 13:26:37 -0500798static int tsl2563_remove(struct i2c_client *client)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200799{
800 struct tsl2563_chip *chip = i2c_get_clientdata(client);
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100801 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100802
803 iio_device_unregister(indio_dev);
Jonathan Cameron388be482010-06-26 12:54:21 +0100804 if (!chip->int_enabled)
805 cancel_delayed_work(&chip->poweroff_work);
806 /* Ensure that interrupts are disabled - then flush any bottom halves */
Derek Basehore95273f82012-10-05 00:54:00 +0100807 chip->intr &= ~0x30;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700808 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
809 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100810 flush_scheduled_work();
811 tsl2563_set_power(chip, 0);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200812
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200813 return 0;
814}
815
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100816#ifdef CONFIG_PM_SLEEP
817static int tsl2563_suspend(struct device *dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200818{
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100819 struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200820 int ret;
821
822 mutex_lock(&chip->lock);
823
824 ret = tsl2563_set_power(chip, 0);
825 if (ret)
826 goto out;
827
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100828 chip->suspended = true;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200829
830out:
831 mutex_unlock(&chip->lock);
832 return ret;
833}
834
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100835static int tsl2563_resume(struct device *dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200836{
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100837 struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200838 int ret;
839
840 mutex_lock(&chip->lock);
841
842 ret = tsl2563_set_power(chip, 1);
843 if (ret)
844 goto out;
845
846 ret = tsl2563_configure(chip);
847 if (ret)
848 goto out;
849
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100850 chip->suspended = false;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200851
852out:
853 mutex_unlock(&chip->lock);
854 return ret;
855}
856
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100857static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume);
858#define TSL2563_PM_OPS (&tsl2563_pm_ops)
859#else
860#define TSL2563_PM_OPS NULL
861#endif
862
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200863static const struct i2c_device_id tsl2563_id[] = {
Jonathan Camerondbd5d232009-11-22 16:03:25 +0000864 { "tsl2560", 0 },
865 { "tsl2561", 1 },
866 { "tsl2562", 2 },
867 { "tsl2563", 3 },
868 {}
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200869};
870MODULE_DEVICE_TABLE(i2c, tsl2563_id);
871
Javier Martinez Canillas9d3922b22017-03-15 01:44:54 -0300872static const struct of_device_id tsl2563_of_match[] = {
873 { .compatible = "amstaos,tsl2560" },
874 { .compatible = "amstaos,tsl2561" },
875 { .compatible = "amstaos,tsl2562" },
876 { .compatible = "amstaos,tsl2563" },
877 {}
878};
879MODULE_DEVICE_TABLE(of, tsl2563_of_match);
880
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200881static struct i2c_driver tsl2563_i2c_driver = {
882 .driver = {
Jonathan Camerondbd5d232009-11-22 16:03:25 +0000883 .name = "tsl2563",
Javier Martinez Canillas9d3922b22017-03-15 01:44:54 -0300884 .of_match_table = tsl2563_of_match,
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100885 .pm = TSL2563_PM_OPS,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200886 },
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200887 .probe = tsl2563_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500888 .remove = tsl2563_remove,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200889 .id_table = tsl2563_id,
890};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100891module_i2c_driver(tsl2563_i2c_driver);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200892
893MODULE_AUTHOR("Nokia Corporation");
894MODULE_DESCRIPTION("tsl2563 light sensor driver");
895MODULE_LICENSE("GPL");