blob: 5bf2bfbc5379eb013ef373351ace384afcecb317 [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>
Vaishnav M Ad884da12020-10-19 02:05:52 +053015#include <linux/mod_devicetable.h>
16#include <linux/property.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020017#include <linux/i2c.h>
18#include <linux/interrupt.h>
Jonathan Cameron388be482010-06-26 12:54:21 +010019#include <linux/irq.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020020#include <linux/sched.h>
21#include <linux/mutex.h>
22#include <linux/delay.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020023#include <linux/pm.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020024#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020026
Jonathan Cameron06458e22012-04-25 15:54:58 +010027#include <linux/iio/iio.h>
28#include <linux/iio/sysfs.h>
29#include <linux/iio/events.h>
Jonathan Cameron9c2251d2013-01-12 10:35:00 +000030#include <linux/platform_data/tsl2563.h>
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020031
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020032/* Use this many bits for fraction part. */
Jonathan Cameron5ade7632013-01-12 10:35:00 +000033#define ADC_FRAC_BITS 14
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020034
35/* Given number of 1/10000's in ADC_FRAC_BITS precision. */
36#define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
37
38/* Bits used for fraction in calibration coefficients.*/
Jonathan Cameron5ade7632013-01-12 10:35:00 +000039#define CALIB_FRAC_BITS 10
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020040/* 0.5 in CALIB_FRAC_BITS precision */
41#define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1))
42/* Make a fraction from a number n that was multiplied with b. */
43#define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b))
44/* Decimal 10^(digits in sysfs presentation) */
Jonathan Cameron5ade7632013-01-12 10:35:00 +000045#define CALIB_BASE_SYSFS 1000
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020046
Jonathan Cameron5ade7632013-01-12 10:35:00 +000047#define TSL2563_CMD 0x80
48#define TSL2563_CLEARINT 0x40
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020049
Jonathan Cameron5ade7632013-01-12 10:35:00 +000050#define TSL2563_REG_CTRL 0x00
51#define TSL2563_REG_TIMING 0x01
52#define TSL2563_REG_LOWLOW 0x02 /* data0 low threshold, 2 bytes */
53#define TSL2563_REG_LOWHIGH 0x03
54#define TSL2563_REG_HIGHLOW 0x04 /* data0 high threshold, 2 bytes */
55#define TSL2563_REG_HIGHHIGH 0x05
56#define TSL2563_REG_INT 0x06
57#define TSL2563_REG_ID 0x0a
58#define TSL2563_REG_DATA0LOW 0x0c /* broadband sensor value, 2 bytes */
59#define TSL2563_REG_DATA0HIGH 0x0d
60#define TSL2563_REG_DATA1LOW 0x0e /* infrared sensor value, 2 bytes */
61#define TSL2563_REG_DATA1HIGH 0x0f
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020062
Jonathan Cameron5ade7632013-01-12 10:35:00 +000063#define TSL2563_CMD_POWER_ON 0x03
64#define TSL2563_CMD_POWER_OFF 0x00
65#define TSL2563_CTRL_POWER_MASK 0x03
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020066
Jonathan Cameron5ade7632013-01-12 10:35:00 +000067#define TSL2563_TIMING_13MS 0x00
68#define TSL2563_TIMING_100MS 0x01
69#define TSL2563_TIMING_400MS 0x02
70#define TSL2563_TIMING_MASK 0x03
71#define TSL2563_TIMING_GAIN16 0x10
72#define TSL2563_TIMING_GAIN1 0x00
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020073
Nishant Malpani9fd28572020-03-18 13:33:11 +053074#define TSL2563_INT_DISABLED 0x00
Jonathan Cameron5ade7632013-01-12 10:35:00 +000075#define TSL2563_INT_LEVEL 0x10
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020076#define TSL2563_INT_PERSIST(n) ((n) & 0x0F)
77
78struct tsl2563_gainlevel_coeff {
79 u8 gaintime;
80 u16 min;
81 u16 max;
82};
83
Jonathan Cameron1ff7e1d2011-04-18 12:58:56 +010084static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +020085 {
86 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
87 .min = 0,
88 .max = 65534,
89 }, {
90 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
91 .min = 2048,
92 .max = 65534,
93 }, {
94 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
95 .min = 4095,
96 .max = 37177,
97 }, {
98 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
99 .min = 3000,
100 .max = 65535,
101 },
102};
103
104struct tsl2563_chip {
105 struct mutex lock;
106 struct i2c_client *client;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200107 struct delayed_work poweroff_work;
108
109 /* Remember state for suspend and resume functions */
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100110 bool suspended;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200111
Jonathan Cameron1ff7e1d2011-04-18 12:58:56 +0100112 struct tsl2563_gainlevel_coeff const *gainlevel;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200113
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200114 u16 low_thres;
115 u16 high_thres;
116 u8 intr;
Jonathan Cameron388be482010-06-26 12:54:21 +0100117 bool int_enabled;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200118
119 /* Calibration coefficients */
120 u32 calib0;
121 u32 calib1;
122 int cover_comp_gain;
123
124 /* Cache current values, to be returned while suspended */
125 u32 data0;
126 u32 data1;
127};
128
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200129static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
130{
131 struct i2c_client *client = chip->client;
132 u8 cmd;
133
134 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700135 return i2c_smbus_write_byte_data(client,
136 TSL2563_CMD | TSL2563_REG_CTRL, cmd);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200137}
138
139/*
140 * Return value is 0 for off, 1 for on, or a negative error
141 * code if reading failed.
142 */
143static int tsl2563_get_power(struct tsl2563_chip *chip)
144{
145 struct i2c_client *client = chip->client;
146 int ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200147
Bryan Freedd9b42c02011-06-24 13:40:47 -0700148 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
149 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200150 return ret;
151
Bryan Freedd9b42c02011-06-24 13:40:47 -0700152 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200153}
154
155static int tsl2563_configure(struct tsl2563_chip *chip)
156{
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200157 int ret;
158
Bryan Freedd9b42c02011-06-24 13:40:47 -0700159 ret = i2c_smbus_write_byte_data(chip->client,
160 TSL2563_CMD | TSL2563_REG_TIMING,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200161 chip->gainlevel->gaintime);
162 if (ret)
Jonathan Cameron388be482010-06-26 12:54:21 +0100163 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700164 ret = i2c_smbus_write_byte_data(chip->client,
165 TSL2563_CMD | TSL2563_REG_HIGHLOW,
Jonathan Cameron388be482010-06-26 12:54:21 +0100166 chip->high_thres & 0xFF);
167 if (ret)
168 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700169 ret = i2c_smbus_write_byte_data(chip->client,
170 TSL2563_CMD | TSL2563_REG_HIGHHIGH,
Jonathan Cameron388be482010-06-26 12:54:21 +0100171 (chip->high_thres >> 8) & 0xFF);
172 if (ret)
173 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700174 ret = i2c_smbus_write_byte_data(chip->client,
175 TSL2563_CMD | TSL2563_REG_LOWLOW,
Jonathan Cameron388be482010-06-26 12:54:21 +0100176 chip->low_thres & 0xFF);
177 if (ret)
178 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700179 ret = i2c_smbus_write_byte_data(chip->client,
180 TSL2563_CMD | TSL2563_REG_LOWHIGH,
Jonathan Cameron388be482010-06-26 12:54:21 +0100181 (chip->low_thres >> 8) & 0xFF);
Jonathan Camerona9e244f62013-01-12 10:35:00 +0000182/*
183 * Interrupt register is automatically written anyway if it is relevant
184 * so is not here.
185 */
Jonathan Cameron388be482010-06-26 12:54:21 +0100186error_ret:
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200187 return ret;
188}
189
190static void tsl2563_poweroff_work(struct work_struct *work)
191{
192 struct tsl2563_chip *chip =
193 container_of(work, struct tsl2563_chip, poweroff_work.work);
194 tsl2563_set_power(chip, 0);
195}
196
197static int tsl2563_detect(struct tsl2563_chip *chip)
198{
199 int ret;
200
201 ret = tsl2563_set_power(chip, 1);
202 if (ret)
203 return ret;
204
205 ret = tsl2563_get_power(chip);
206 if (ret < 0)
207 return ret;
208
209 return ret ? 0 : -ENODEV;
210}
211
212static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
213{
214 struct i2c_client *client = chip->client;
215 int ret;
216
Bryan Freedd9b42c02011-06-24 13:40:47 -0700217 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
218 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200219 return ret;
220
Maxin B. John22dc09c2011-10-26 17:27:37 +0100221 *id = ret;
222
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200223 return 0;
224}
225
226/*
227 * "Normalized" ADC value is one obtained with 400ms of integration time and
228 * 16x gain. This function returns the number of bits of shift needed to
229 * convert between normalized values and HW values obtained using given
230 * timing and gain settings.
231 */
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200232static int tsl2563_adc_shiftbits(u8 timing)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200233{
234 int shift = 0;
235
236 switch (timing & TSL2563_TIMING_MASK) {
237 case TSL2563_TIMING_13MS:
238 shift += 5;
239 break;
240 case TSL2563_TIMING_100MS:
241 shift += 2;
242 break;
243 case TSL2563_TIMING_400MS:
244 /* no-op */
245 break;
246 }
247
248 if (!(timing & TSL2563_TIMING_GAIN16))
249 shift += 4;
250
251 return shift;
252}
253
254/* Convert a HW ADC value to normalized scale. */
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200255static u32 tsl2563_normalize_adc(u16 adc, u8 timing)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200256{
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200257 return adc << tsl2563_adc_shiftbits(timing);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200258}
259
260static void tsl2563_wait_adc(struct tsl2563_chip *chip)
261{
262 unsigned int delay;
263
264 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
265 case TSL2563_TIMING_13MS:
266 delay = 14;
267 break;
268 case TSL2563_TIMING_100MS:
269 delay = 101;
270 break;
271 default:
272 delay = 402;
273 }
274 /*
275 * TODO: Make sure that we wait at least required delay but why we
276 * have to extend it one tick more?
277 */
278 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
279}
280
281static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
282{
283 struct i2c_client *client = chip->client;
284
285 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
286
287 (adc > chip->gainlevel->max) ?
288 chip->gainlevel++ : chip->gainlevel--;
289
Bryan Freedd9b42c02011-06-24 13:40:47 -0700290 i2c_smbus_write_byte_data(client,
291 TSL2563_CMD | TSL2563_REG_TIMING,
292 chip->gainlevel->gaintime);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200293
294 tsl2563_wait_adc(chip);
295 tsl2563_wait_adc(chip);
296
297 return 1;
298 } else
299 return 0;
300}
301
302static int tsl2563_get_adc(struct tsl2563_chip *chip)
303{
304 struct i2c_client *client = chip->client;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200305 u16 adc0, adc1;
306 int retry = 1;
307 int ret = 0;
308
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100309 if (chip->suspended)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200310 goto out;
311
Jonathan Cameron388be482010-06-26 12:54:21 +0100312 if (!chip->int_enabled) {
313 cancel_delayed_work(&chip->poweroff_work);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200314
Jonathan Cameron388be482010-06-26 12:54:21 +0100315 if (!tsl2563_get_power(chip)) {
316 ret = tsl2563_set_power(chip, 1);
317 if (ret)
318 goto out;
319 ret = tsl2563_configure(chip);
320 if (ret)
321 goto out;
322 tsl2563_wait_adc(chip);
323 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200324 }
325
326 while (retry) {
Bryan Freedd9b42c02011-06-24 13:40:47 -0700327 ret = i2c_smbus_read_word_data(client,
328 TSL2563_CMD | TSL2563_REG_DATA0LOW);
329 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200330 goto out;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700331 adc0 = ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200332
Bryan Freedd9b42c02011-06-24 13:40:47 -0700333 ret = i2c_smbus_read_word_data(client,
334 TSL2563_CMD | TSL2563_REG_DATA1LOW);
335 if (ret < 0)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200336 goto out;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700337 adc1 = ret;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200338
339 retry = tsl2563_adjust_gainlevel(chip, adc0);
340 }
341
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200342 chip->data0 = tsl2563_normalize_adc(adc0, chip->gainlevel->gaintime);
343 chip->data1 = tsl2563_normalize_adc(adc1, chip->gainlevel->gaintime);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200344
Jonathan Cameron388be482010-06-26 12:54:21 +0100345 if (!chip->int_enabled)
346 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200347
348 ret = 0;
349out:
350 return ret;
351}
352
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200353static inline int tsl2563_calib_to_sysfs(u32 calib)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200354{
355 return (int) (((calib * CALIB_BASE_SYSFS) +
356 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
357}
358
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200359static inline u32 tsl2563_calib_from_sysfs(int value)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200360{
361 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
362}
363
364/*
365 * Conversions between lux and ADC values.
366 *
367 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
368 * appropriate constants. Different constants are needed for different
369 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
370 * of the intensities in infrared and visible wavelengths). lux_table below
371 * lists the upper threshold of the adc1/adc0 ratio and the corresponding
372 * constants.
373 */
374
375struct tsl2563_lux_coeff {
376 unsigned long ch_ratio;
377 unsigned long ch0_coeff;
378 unsigned long ch1_coeff;
379};
380
381static const struct tsl2563_lux_coeff lux_table[] = {
382 {
383 .ch_ratio = FRAC10K(1300),
384 .ch0_coeff = FRAC10K(315),
385 .ch1_coeff = FRAC10K(262),
386 }, {
387 .ch_ratio = FRAC10K(2600),
388 .ch0_coeff = FRAC10K(337),
389 .ch1_coeff = FRAC10K(430),
390 }, {
391 .ch_ratio = FRAC10K(3900),
392 .ch0_coeff = FRAC10K(363),
393 .ch1_coeff = FRAC10K(529),
394 }, {
395 .ch_ratio = FRAC10K(5200),
396 .ch0_coeff = FRAC10K(392),
397 .ch1_coeff = FRAC10K(605),
398 }, {
399 .ch_ratio = FRAC10K(6500),
400 .ch0_coeff = FRAC10K(229),
401 .ch1_coeff = FRAC10K(291),
402 }, {
403 .ch_ratio = FRAC10K(8000),
404 .ch0_coeff = FRAC10K(157),
405 .ch1_coeff = FRAC10K(180),
406 }, {
407 .ch_ratio = FRAC10K(13000),
408 .ch0_coeff = FRAC10K(34),
409 .ch1_coeff = FRAC10K(26),
410 }, {
411 .ch_ratio = ULONG_MAX,
412 .ch0_coeff = 0,
413 .ch1_coeff = 0,
414 },
415};
416
Jonathan Camerona9e244f62013-01-12 10:35:00 +0000417/* Convert normalized, scaled ADC values to lux. */
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200418static unsigned int tsl2563_adc_to_lux(u32 adc0, u32 adc1)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200419{
420 const struct tsl2563_lux_coeff *lp = lux_table;
421 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
422
423 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
424
425 while (lp->ch_ratio < ratio)
426 lp++;
427
428 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
429
430 return (unsigned int) (lux >> ADC_FRAC_BITS);
431}
432
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200433/* Apply calibration coefficient to ADC count. */
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200434static u32 tsl2563_calib_adc(u32 adc, u32 calib)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200435{
436 unsigned long scaled = adc;
437
438 scaled *= calib;
439 scaled >>= CALIB_FRAC_BITS;
440
441 return (u32) scaled;
442}
443
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100444static int tsl2563_write_raw(struct iio_dev *indio_dev,
445 struct iio_chan_spec const *chan,
446 int val,
447 int val2,
448 long mask)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200449{
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100450 struct tsl2563_chip *chip = iio_priv(indio_dev);
451
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000452 if (mask != IIO_CHAN_INFO_CALIBSCALE)
453 return -EINVAL;
454 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200455 chip->calib0 = tsl2563_calib_from_sysfs(val);
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000456 else if (chan->channel2 == IIO_MOD_LIGHT_IR)
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200457 chip->calib1 = tsl2563_calib_from_sysfs(val);
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000458 else
459 return -EINVAL;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100460
461 return 0;
462}
463
464static int tsl2563_read_raw(struct iio_dev *indio_dev,
465 struct iio_chan_spec const *chan,
466 int *val,
467 int *val2,
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000468 long mask)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100469{
470 int ret = -EINVAL;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200471 u32 calib0, calib1;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100472 struct tsl2563_chip *chip = iio_priv(indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200473
474 mutex_lock(&chip->lock);
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000475 switch (mask) {
Jonathan Cameron90354d02012-04-15 17:41:22 +0100476 case IIO_CHAN_INFO_RAW:
477 case IIO_CHAN_INFO_PROCESSED:
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100478 switch (chan->type) {
479 case IIO_LIGHT:
480 ret = tsl2563_get_adc(chip);
481 if (ret)
482 goto error_ret;
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200483 calib0 = tsl2563_calib_adc(chip->data0, chip->calib0) *
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100484 chip->cover_comp_gain;
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200485 calib1 = tsl2563_calib_adc(chip->data1, chip->calib1) *
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100486 chip->cover_comp_gain;
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200487 *val = tsl2563_adc_to_lux(calib0, calib1);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100488 ret = IIO_VAL_INT;
489 break;
490 case IIO_INTENSITY:
491 ret = tsl2563_get_adc(chip);
492 if (ret)
493 goto error_ret;
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000494 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100495 *val = chip->data0;
496 else
497 *val = chip->data1;
498 ret = IIO_VAL_INT;
499 break;
500 default:
501 break;
502 }
Jonathan Cameron388be482010-06-26 12:54:21 +0100503 break;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100504
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100505 case IIO_CHAN_INFO_CALIBSCALE:
Ivaylo Dimitrov3b5c1632014-01-13 17:24:00 +0000506 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200507 *val = tsl2563_calib_to_sysfs(chip->calib0);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100508 else
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200509 *val = tsl2563_calib_to_sysfs(chip->calib1);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100510 ret = IIO_VAL_INT;
Jonathan Cameron388be482010-06-26 12:54:21 +0100511 break;
512 default:
Dan Carpenter97d35f22011-10-06 09:15:03 +0300513 ret = -EINVAL;
514 goto error_ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100515 }
516
517error_ret:
518 mutex_unlock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100519 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100520}
521
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100522static const struct iio_event_spec tsl2563_events[] = {
523 {
524 .type = IIO_EV_TYPE_THRESH,
525 .dir = IIO_EV_DIR_RISING,
526 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
527 BIT(IIO_EV_INFO_ENABLE),
528 }, {
529 .type = IIO_EV_TYPE_THRESH,
530 .dir = IIO_EV_DIR_FALLING,
531 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
532 BIT(IIO_EV_INFO_ENABLE),
533 },
534};
535
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100536static const struct iio_chan_spec tsl2563_channels[] = {
Jonathan Cameron79939062011-08-30 12:41:16 +0100537 {
538 .type = IIO_LIGHT,
539 .indexed = 1,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000540 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
Jonathan Cameron79939062011-08-30 12:41:16 +0100541 .channel = 0,
542 }, {
543 .type = IIO_INTENSITY,
544 .modified = 1,
545 .channel2 = IIO_MOD_LIGHT_BOTH,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000546 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
547 BIT(IIO_CHAN_INFO_CALIBSCALE),
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100548 .event_spec = tsl2563_events,
549 .num_event_specs = ARRAY_SIZE(tsl2563_events),
Jonathan Cameron79939062011-08-30 12:41:16 +0100550 }, {
551 .type = IIO_INTENSITY,
552 .modified = 1,
Jonathan Camerona7e3bd62011-10-26 17:27:36 +0100553 .channel2 = IIO_MOD_LIGHT_IR,
Jonathan Camerond292ef82013-02-27 19:33:40 +0000554 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
555 BIT(IIO_CHAN_INFO_CALIBSCALE),
Jonathan Cameron79939062011-08-30 12:41:16 +0100556 }
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100557};
Jonathan Cameron388be482010-06-26 12:54:21 +0100558
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100559static int tsl2563_read_thresh(struct iio_dev *indio_dev,
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100560 const struct iio_chan_spec *chan, enum iio_event_type type,
561 enum iio_event_direction dir, enum iio_event_info info, int *val,
562 int *val2)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100563{
564 struct tsl2563_chip *chip = iio_priv(indio_dev);
565
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100566 switch (dir) {
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100567 case IIO_EV_DIR_RISING:
568 *val = chip->high_thres;
569 break;
570 case IIO_EV_DIR_FALLING:
571 *val = chip->low_thres;
572 break;
573 default:
574 return -EINVAL;
575 }
576
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100577 return IIO_VAL_INT;
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100578}
579
Dan Carpenter15fbc192011-10-06 09:15:36 +0300580static int tsl2563_write_thresh(struct iio_dev *indio_dev,
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100581 const struct iio_chan_spec *chan, enum iio_event_type type,
582 enum iio_event_direction dir, enum iio_event_info info, int val,
583 int val2)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100584{
585 struct tsl2563_chip *chip = iio_priv(indio_dev);
586 int ret;
587 u8 address;
588
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100589 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100590 address = TSL2563_REG_HIGHLOW;
591 else
592 address = TSL2563_REG_LOWLOW;
593 mutex_lock(&chip->lock);
Bryan Freedd9b42c02011-06-24 13:40:47 -0700594 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
595 val & 0xFF);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100596 if (ret)
597 goto error_ret;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700598 ret = i2c_smbus_write_byte_data(chip->client,
599 TSL2563_CMD | (address + 1),
600 (val >> 8) & 0xFF);
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100601 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100602 chip->high_thres = val;
603 else
604 chip->low_thres = val;
605
606error_ret:
607 mutex_unlock(&chip->lock);
608
609 return ret;
610}
Jonathan Cameron388be482010-06-26 12:54:21 +0100611
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100612static irqreturn_t tsl2563_event_handler(int irq, void *private)
Jonathan Cameron388be482010-06-26 12:54:21 +0100613{
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100614 struct iio_dev *dev_info = private;
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100615 struct tsl2563_chip *chip = iio_priv(dev_info);
Jonathan Cameron388be482010-06-26 12:54:21 +0100616
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100617 iio_push_event(dev_info,
Akinobu Mitaa3507e42017-06-21 01:46:37 +0900618 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
Jonathan Cameronda1d8b62010-10-08 12:14:05 +0100619 0,
620 IIO_EV_TYPE_THRESH,
621 IIO_EV_DIR_EITHER),
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100622 iio_get_time_ns(dev_info));
Jonathan Cameron388be482010-06-26 12:54:21 +0100623
Jonathan Cameron388be482010-06-26 12:54:21 +0100624 /* clear the interrupt and push the event */
Bryan Freedd9b42c02011-06-24 13:40:47 -0700625 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100626 return IRQ_HANDLED;
Jonathan Cameron388be482010-06-26 12:54:21 +0100627}
628
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100629static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100630 const struct iio_chan_spec *chan, enum iio_event_type type,
631 enum iio_event_direction dir, int state)
Jonathan Cameron388be482010-06-26 12:54:21 +0100632{
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100633 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100634 int ret = 0;
Jonathan Cameron388be482010-06-26 12:54:21 +0100635
Jonathan Cameron388be482010-06-26 12:54:21 +0100636 mutex_lock(&chip->lock);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100637 if (state && !(chip->intr & 0x30)) {
Jonathan Cameron388be482010-06-26 12:54:21 +0100638 chip->intr &= ~0x30;
639 chip->intr |= 0x10;
640 /* ensure the chip is actually on */
641 cancel_delayed_work(&chip->poweroff_work);
642 if (!tsl2563_get_power(chip)) {
643 ret = tsl2563_set_power(chip, 1);
644 if (ret)
645 goto out;
646 ret = tsl2563_configure(chip);
647 if (ret)
648 goto out;
649 }
Bryan Freedd9b42c02011-06-24 13:40:47 -0700650 ret = i2c_smbus_write_byte_data(chip->client,
651 TSL2563_CMD | TSL2563_REG_INT,
652 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100653 chip->int_enabled = true;
654 }
655
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100656 if (!state && (chip->intr & 0x30)) {
Derek Basehore95273f82012-10-05 00:54:00 +0100657 chip->intr &= ~0x30;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700658 ret = i2c_smbus_write_byte_data(chip->client,
659 TSL2563_CMD | TSL2563_REG_INT,
660 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100661 chip->int_enabled = false;
662 /* now the interrupt is not enabled, we can go to sleep */
663 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
664 }
665out:
666 mutex_unlock(&chip->lock);
667
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100668 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100669}
670
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100671static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
Lars-Peter Clausen6d597472013-10-07 15:11:00 +0100672 const struct iio_chan_spec *chan, enum iio_event_type type,
673 enum iio_event_direction dir)
Jonathan Cameron388be482010-06-26 12:54:21 +0100674{
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100675 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100676 int ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100677
678 mutex_lock(&chip->lock);
Bryan Freedd9b42c02011-06-24 13:40:47 -0700679 ret = i2c_smbus_read_byte_data(chip->client,
680 TSL2563_CMD | TSL2563_REG_INT);
Jonathan Cameron388be482010-06-26 12:54:21 +0100681 mutex_unlock(&chip->lock);
682 if (ret < 0)
Jonathan Camerona722dcc2013-01-12 10:35:00 +0000683 return ret;
Jonathan Cameron388be482010-06-26 12:54:21 +0100684
Jonathan Camerona722dcc2013-01-12 10:35:00 +0000685 return !!(ret & 0x30);
Jonathan Cameron388be482010-06-26 12:54:21 +0100686}
Jonathan Cameron388be482010-06-26 12:54:21 +0100687
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100688static const struct iio_info tsl2563_info_no_irq = {
Bryan Freed9e4216f2011-06-21 15:54:57 -0700689 .read_raw = &tsl2563_read_raw,
690 .write_raw = &tsl2563_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100691};
692
693static const struct iio_info tsl2563_info = {
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100694 .read_raw = &tsl2563_read_raw,
695 .write_raw = &tsl2563_write_raw,
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000696 .read_event_value = &tsl2563_read_thresh,
697 .write_event_value = &tsl2563_write_thresh,
698 .read_event_config = &tsl2563_read_interrupt_config,
699 .write_event_config = &tsl2563_write_interrupt_config,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100700};
701
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500702static int tsl2563_probe(struct i2c_client *client,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200703 const struct i2c_device_id *device_id)
704{
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100705 struct iio_dev *indio_dev;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200706 struct tsl2563_chip *chip;
707 struct tsl2563_platform_data *pdata = client->dev.platform_data;
708 int err = 0;
Jonathan Camerondeda3862011-08-12 17:08:35 +0100709 u8 id = 0;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200710
Sachin Kamatbace48f2013-07-30 09:44:00 +0100711 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100712 if (!indio_dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200713 return -ENOMEM;
714
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100715 chip = iio_priv(indio_dev);
716
Alexandru Ardelean70804e52020-05-22 10:08:01 +0300717 i2c_set_clientdata(client, indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200718 chip->client = client;
719
720 err = tsl2563_detect(chip);
721 if (err) {
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800722 dev_err(&client->dev, "detect error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100723 return err;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200724 }
725
726 err = tsl2563_read_id(chip, &id);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800727 if (err) {
728 dev_err(&client->dev, "read id error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100729 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800730 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200731
732 mutex_init(&chip->lock);
733
734 /* Default values used until userspace says otherwise */
735 chip->low_thres = 0x0;
736 chip->high_thres = 0xffff;
737 chip->gainlevel = tsl2563_gainlevel_table;
738 chip->intr = TSL2563_INT_PERSIST(4);
Peter Meerwald6fa273c2015-04-15 22:39:37 +0200739 chip->calib0 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
740 chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200741
Vaishnav M Ad884da12020-10-19 02:05:52 +0530742 if (pdata) {
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200743 chip->cover_comp_gain = pdata->cover_comp_gain;
Vaishnav M Ad884da12020-10-19 02:05:52 +0530744 } else {
745 err = device_property_read_u32(&client->dev, "amstaos,cover-comp-gain",
746 &chip->cover_comp_gain);
747 if (err)
748 chip->cover_comp_gain = 1;
749 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200750
751 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100752 indio_dev->name = client->name;
753 indio_dev->channels = tsl2563_channels;
754 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
Jonathan Cameron33789dc2011-04-18 12:58:55 +0100755 indio_dev->modes = INDIO_DIRECT_MODE;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800756
Jonathan Cameroncbcdf4d2011-05-18 14:41:45 +0100757 if (client->irq)
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100758 indio_dev->info = &tsl2563_info;
759 else
760 indio_dev->info = &tsl2563_info_no_irq;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800761
Jonathan Cameron388be482010-06-26 12:54:21 +0100762 if (client->irq) {
Sachin Kamatbace48f2013-07-30 09:44:00 +0100763 err = devm_request_threaded_irq(&client->dev, client->irq,
Jonathan Cameronbdab1002011-05-18 14:41:03 +0100764 NULL,
765 &tsl2563_event_handler,
766 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
767 "tsl2563_event",
768 indio_dev);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800769 if (err) {
770 dev_err(&client->dev, "irq request error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100771 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800772 }
Jonathan Cameron388be482010-06-26 12:54:21 +0100773 }
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800774
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200775 err = tsl2563_configure(chip);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800776 if (err) {
777 dev_err(&client->dev, "configure error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100778 return err;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800779 }
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200780
781 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800782
Jonathan Cameron388be482010-06-26 12:54:21 +0100783 /* The interrupt cannot yet be enabled so this is fine without lock */
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200784 schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
785
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800786 err = iio_device_register(indio_dev);
787 if (err) {
788 dev_err(&client->dev, "iio registration error %d\n", -err);
Sachin Kamatbace48f2013-07-30 09:44:00 +0100789 goto fail;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800790 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100791
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200792 return 0;
Grant Grundlerdbf717f2012-03-05 09:15:59 -0800793
Sachin Kamatbace48f2013-07-30 09:44:00 +0100794fail:
Amitoj Kaur Chawla9e61d902016-03-17 19:25:11 +0530795 cancel_delayed_work_sync(&chip->poweroff_work);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200796 return err;
797}
798
Bill Pemberton447d4f22012-11-19 13:26:37 -0500799static int tsl2563_remove(struct i2c_client *client)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200800{
Alexandru Ardelean70804e52020-05-22 10:08:01 +0300801 struct iio_dev *indio_dev = i2c_get_clientdata(client);
802 struct tsl2563_chip *chip = iio_priv(indio_dev);
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100803
804 iio_device_unregister(indio_dev);
Jonathan Cameron388be482010-06-26 12:54:21 +0100805 if (!chip->int_enabled)
806 cancel_delayed_work(&chip->poweroff_work);
807 /* Ensure that interrupts are disabled - then flush any bottom halves */
Derek Basehore95273f82012-10-05 00:54:00 +0100808 chip->intr &= ~0x30;
Bryan Freedd9b42c02011-06-24 13:40:47 -0700809 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
810 chip->intr);
Jonathan Cameron388be482010-06-26 12:54:21 +0100811 flush_scheduled_work();
812 tsl2563_set_power(chip, 0);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200813
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200814 return 0;
815}
816
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100817#ifdef CONFIG_PM_SLEEP
818static int tsl2563_suspend(struct device *dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200819{
Alexandru Ardelean70804e52020-05-22 10:08:01 +0300820 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
821 struct tsl2563_chip *chip = iio_priv(indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200822 int ret;
823
824 mutex_lock(&chip->lock);
825
826 ret = tsl2563_set_power(chip, 0);
827 if (ret)
828 goto out;
829
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100830 chip->suspended = true;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200831
832out:
833 mutex_unlock(&chip->lock);
834 return ret;
835}
836
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100837static int tsl2563_resume(struct device *dev)
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200838{
Alexandru Ardelean70804e52020-05-22 10:08:01 +0300839 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
840 struct tsl2563_chip *chip = iio_priv(indio_dev);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200841 int ret;
842
843 mutex_lock(&chip->lock);
844
845 ret = tsl2563_set_power(chip, 1);
846 if (ret)
847 goto out;
848
849 ret = tsl2563_configure(chip);
850 if (ret)
851 goto out;
852
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100853 chip->suspended = false;
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200854
855out:
856 mutex_unlock(&chip->lock);
857 return ret;
858}
859
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100860static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume);
861#define TSL2563_PM_OPS (&tsl2563_pm_ops)
862#else
863#define TSL2563_PM_OPS NULL
864#endif
865
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200866static const struct i2c_device_id tsl2563_id[] = {
Jonathan Camerondbd5d232009-11-22 16:03:25 +0000867 { "tsl2560", 0 },
868 { "tsl2561", 1 },
869 { "tsl2562", 2 },
870 { "tsl2563", 3 },
871 {}
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200872};
873MODULE_DEVICE_TABLE(i2c, tsl2563_id);
874
Javier Martinez Canillas9d3922b22017-03-15 01:44:54 -0300875static const struct of_device_id tsl2563_of_match[] = {
876 { .compatible = "amstaos,tsl2560" },
877 { .compatible = "amstaos,tsl2561" },
878 { .compatible = "amstaos,tsl2562" },
879 { .compatible = "amstaos,tsl2563" },
880 {}
881};
882MODULE_DEVICE_TABLE(of, tsl2563_of_match);
883
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200884static struct i2c_driver tsl2563_i2c_driver = {
885 .driver = {
Jonathan Camerondbd5d232009-11-22 16:03:25 +0000886 .name = "tsl2563",
Javier Martinez Canillas9d3922b22017-03-15 01:44:54 -0300887 .of_match_table = tsl2563_of_match,
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100888 .pm = TSL2563_PM_OPS,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200889 },
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200890 .probe = tsl2563_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500891 .remove = tsl2563_remove,
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200892 .id_table = tsl2563_id,
893};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100894module_i2c_driver(tsl2563_i2c_driver);
Amit Kucheriaee1f1fa2009-11-09 15:14:28 +0200895
896MODULE_AUTHOR("Nokia Corporation");
897MODULE_DESCRIPTION("tsl2563 light sensor driver");
898MODULE_LICENSE("GPL");