summaryrefslogtreecommitdiff
path: root/drivers/iio/light/cm3391.c
blob: abe8b8e7f5c6fbe4247cadd4ac1ea2940417d86d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/*
 * Copyright (C) 2014-2015 Capella Microsystems Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2, as published
 * by the Free Software Foundation.
 *
 * Special thanks Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
 * help to add ACPI support.
 *
 */

#include <linux/delay.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/regulator/consumer.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/events.h>
#include <linux/init.h>

#ifdef	CONFIG_ACPI
#include <linux/acpi.h>
#endif /* CONFIG_ACPI */

#define OLDAPI

/* Registers Address */
#define CM3391_REG_ADDR_CMD		0x00
#define CM3391_REG_ADDR_TEST		0x01
#define CM3391_REG_ADDR_WH		0x02
#define CM3391_REG_ADDR_WL		0x03
#define CM3391_REG_ADDR_RED		0x08
#define CM3391_REG_ADDR_ALS		0x09
#define CM3391_REG_ADDR_BLUE		0x0A
#define CM3391_REG_ADDR_CLEAR		0x0B
#define CM3391_REG_ADDR_ID		0x0C
#define CM3391_REG_ADDR_STATUS		0x0D

/* Number of Configurable Registers */
#define CM3391_CONF_REG_NUM		16

/* CMD register */
#define CM3391_CMD_CS_DISABLE		BIT(0)
#define CM3391_CMD_CS_INT_EN		BIT(8)

#define CM3391_CMD_CS_PERS_SHIFT	10
#define CM3391_CMD_CS_PERS_MASK		(BIT(10) | BIT(11))
#define CM3391_CMD_CS_PERS_DEFAULT	(0x01 << CM3391_CMD_CS_PERS_SHIFT)

#define CM3391_CMD_CS_IT_SHIFT		4
#define CM3391_CMD_CS_IT_MASK		(BIT(4) | BIT(5) | BIT(6))
#define CM3391_CMD_CS_IT_DEFAULT	(0x01 << CM3391_CMD_CS_IT_SHIFT)

#define CM3391_CMD_CS_HS_SHIFT		BIT(3)

#define CM3391_CMD_DEFAULT		(CM3391_CMD_CS_PERS_DEFAULT |\
					CM3391_CMD_CS_IT_DEFAULT)

#define CM3391_WH_DEFAULT		0xFFFF
#define CM3391_WL_DEFAULT		0x0000

#define CM3391_CALIBSCALE_DEFAULT	100000
#define CM3391_CALIBSCALE_RESOLUTION	100000
#define CM3391_MLUX_PER_LUX		1000
#define CM3391_THRESHOLD_PERCENT	10	/* 10 percent */

#define CM3391_MLUX_PER_BIT_DEFAULT	550
#define CM3391_MLUX_PER_BIT_BASE_IT	100000

static const struct {
	int val;
	int val2;
	u8 it;
} cm3391_cs_it_scales[] = {
	{0, 50000, 0},	/* 0.050000 */
	{0, 100000, 1}, /* 0.100000 */
	{0, 200000, 2},	/* 0.200000 */
	{0, 400000, 3}, /* 0.400000 */
	{0, 800000, 4},	/* 0.800000 */
};

static const int CM3391_cs_it_bits[] = { 0, 1, 2, 3, 4};
static const int CM3391_cs_it_values[] = {
			50000, 100000, 200000, 400000, 800000};

struct cm3391_cs_info {
	u32 id;
	int regs_bmp;
	int calibscale;
	int mlux_per_bit;
	int mlux_per_bit_base_it;
	const int *cs_it_bits;
	const int *cs_it_values;
	const int num_cs_it;
	int als_raw;
};

static struct cm3391_cs_info cm3391_cs_info_default = {
	.id = 3391,
	.regs_bmp = 0x0F,
	.calibscale = CM3391_CALIBSCALE_DEFAULT,
	.mlux_per_bit = CM3391_MLUX_PER_BIT_DEFAULT,
	.mlux_per_bit_base_it = CM3391_MLUX_PER_BIT_BASE_IT,
	.cs_it_bits = CM3391_cs_it_bits,
	.cs_it_values = CM3391_cs_it_values,
	.num_cs_it = ARRAY_SIZE(CM3391_cs_it_bits),
};

struct cm3391_chip {
	struct i2c_client *client;
	struct mutex lock;
	u16 conf_regs[CM3391_CONF_REG_NUM];
	struct cm3391_cs_info *cs_info;
};

static int cm3391_get_lux(struct cm3391_chip *chip);
static int cm3391_threshold_update(struct cm3391_chip *chip, int percent);
static int cm3391_read_cs_it(struct cm3391_chip *chip, int *val, int *val2);

/**
 * cm3391_interrupt_config() - Enable/Disable CM3391 interrupt
 * @chip:	pointer of struct cm3391.
 * @enable:	0 to disable; otherwise to enable
 *
 * Config CM3391 interrupt control bit.
 *
 * Return: 0 for success; otherwise for error code.
 */
static int cm3391_interrupt_config(struct cm3391_chip *chip, int enable)
{
	struct i2c_client *client = chip->client;
	struct cm3391_cs_info *cs_info = chip->cs_info;
	int status;

	if (!cs_info)
		return -ENODEV;

	/* Force to clean interrupt */
	status = i2c_smbus_read_word_data(client,
			CM3391_REG_ADDR_STATUS);
	if (status < 0)
		return -ENODEV;

	if (enable)
		chip->conf_regs[CM3391_REG_ADDR_CMD] |=
			CM3391_CMD_CS_INT_EN;
	else
		chip->conf_regs[CM3391_REG_ADDR_CMD] &=
			~CM3391_CMD_CS_INT_EN;

	status = i2c_smbus_write_word_data(client, CM3391_REG_ADDR_CMD,
		chip->conf_regs[CM3391_REG_ADDR_CMD]);

	if (status < 0)
		return -ENODEV;

	return status;
}

#ifdef CONFIG_ACPI
/**
 * cm3391_acpi_get_cpm_info() - Get CPM object from ACPI
 * @client	pointer of struct i2c_client.
 * @obj_name	pointer of ACPI object name.
 * @count	maximum size of return array.
 * @vals	pointer of array for return elements.
 *
 * Convert ACPI CPM table to array. Special thanks Srinivas Pandruvada's
 * help to implement this routine.
 *
 * Return: -ENODEV for fail.  Otherwise is number of elements.
 */
static int cm3391_acpi_get_cpm_info(struct i2c_client *client, char *obj_name,
							int count, u64 *vals)
{
	acpi_handle handle;
	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
	int i;
	acpi_status status;
	union acpi_object *cpm;

	handle = ACPI_HANDLE(&client->dev);
	if (!handle)
		return -ENODEV;

	status = acpi_evaluate_object(handle, obj_name, NULL, &buffer);
	if (ACPI_FAILURE(status)) {
		dev_err(&client->dev, "object %s not found\n", obj_name);
		return -ENODEV;
	}

	cpm = buffer.pointer;
	for (i = 0; i < cpm->package.count && i < count; ++i) {
		union acpi_object *elem;
		elem = &(cpm->package.elements[i]);
		vals[i] = elem->integer.value;
	}

	kfree(buffer.pointer);

	return cpm->package.count;
}
#endif /* CONFIG_ACPI */

/**
 * cm3391_reg_init() - Initialize CM3391 registers
 * @chip:	pointer of struct cm3391.
 *
 * Initialize CM3391 color sensor registers to default values.
 *
  Return: 0 for success; otherwise for error code.
 */
static int cm3391_reg_init(struct cm3391_chip *chip)
{
	struct i2c_client *client = chip->client;
	int i;
	s32 ret;
	struct cm3391_cs_info *cs_info;
#ifdef CONFIG_ACPI
	int cpm_elem_count;
	u64 cpm_elems[20];
#endif /* CONFIG_ACPI */

	/* Default device */
	cs_info = chip->cs_info = &cm3391_cs_info_default;
	chip->conf_regs[CM3391_REG_ADDR_CMD] = CM3391_CMD_DEFAULT;
	chip->conf_regs[CM3391_REG_ADDR_WH] = CM3391_WH_DEFAULT;
	chip->conf_regs[CM3391_REG_ADDR_WL] = CM3391_WL_DEFAULT;

	/* Disable interrupt */
	cm3391_interrupt_config(chip, 0);

	/* Disable Test Mode */
	i2c_smbus_write_word_data(client, CM3391_REG_ADDR_TEST, 0x0000);

	/* Disable device */
	i2c_smbus_write_word_data(client, CM3391_REG_ADDR_CMD,
		CM3391_CMD_CS_DISABLE);

	/* Identify device */
	ret = i2c_smbus_read_word_data(client, CM3391_REG_ADDR_ID);
	if (ret < 0)
		return ret;
	if ((ret & 0xFF) != 0x91)
		return -ENODEV;

#ifdef CONFIG_ACPI
	if (ACPI_HANDLE(&client->dev)) {
		/* Load from ACPI */
		cpm_elem_count = cm3391_acpi_get_cpm_info(client, "CPM0",
							ARRAY_SIZE(cpm_elems),
							cpm_elems);
		if (cpm_elem_count > 0) {
			int header_num = 3;
			int reg_num = cpm_elem_count - header_num;

			cs_info->id = cpm_elems[0];
			cs_info->regs_bmp = cpm_elems[2];
			for (i = 0; i < reg_num; i++)
				if (cs_info->regs_bmp & (1<<i))
					chip->conf_regs[i] =
						cpm_elems[header_num+i];
		}

		cpm_elem_count = cm3391_acpi_get_cpm_info(client, "CPM1",
							ARRAY_SIZE(cpm_elems),
							cpm_elems);
		if (cpm_elem_count > 0) {
			cs_info->mlux_per_bit = (int)cpm_elems[0] / 100;
			cs_info->calibscale = (int)cpm_elems[1];
		}
	}
#endif /* CONFIG_ACPI */

	/* Force to disable interrupt */
	chip->conf_regs[CM3391_REG_ADDR_CMD] &= ~CM3391_CMD_CS_INT_EN;

	/* Initialize registers */
	for (i = 0; i < CM3391_CONF_REG_NUM; i++) {
		if (cs_info->regs_bmp & (1<<i)) {
			ret = i2c_smbus_write_word_data(client, i,
						chip->conf_regs[i]);
			if (ret < 0)
				return ret;
		}
	}

	return 0;
}

void cm3391_autoit(struct cm3391_chip *chip, u16 als_raw)
{
	struct i2c_client *client = chip->client;
	u16 cs_it, cmd;
	int i;
	int update;
	s32 ret;

	cs_it = chip->conf_regs[CM3391_REG_ADDR_CMD];
	cs_it &= CM3391_CMD_CS_IT_MASK;
	cs_it >>= CM3391_CMD_CS_IT_SHIFT;
	for (i = 0; i < ARRAY_SIZE(cm3391_cs_it_scales); i++)
		if (cs_it == cm3391_cs_it_scales[i].it)
			break;
	update = 0;
	if (als_raw > 60000) {
		if (i > 0) {
			i--;
			update = 1;
		}
	}
	if (als_raw < 100) {
		if (i < ARRAY_SIZE(cm3391_cs_it_scales) - 1) {
			i++;
			update = 1;
		}
	}

	if (update) {
		cs_it = cm3391_cs_it_scales[i].it;
		cs_it <<= CM3391_CMD_CS_IT_SHIFT;

		cmd = chip->conf_regs[CM3391_REG_ADDR_CMD];
		cmd &= ~CM3391_CMD_CS_IT_MASK;
		cmd |= cs_it;
		ret = i2c_smbus_write_word_data(client,
				CM3391_REG_ADDR_CMD, cmd);

		if (ret < 0)
			return;

		chip->conf_regs[CM3391_REG_ADDR_CMD] = cmd;
		return;
	}
}

/**
 *  cm3391_read_cs_it() - Get sensor integration time
 *  @chip:	pointer of struct cm3391
 *  #val:	pointer of int to load the integration time(sec).
 *  @val2:	pointer of int to load the integration time(microsecond).
 *
 *  Report the current integration time.
 *
 *  Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
 */
static int cm3391_read_cs_it(struct cm3391_chip *chip, int *val, int *val2)
{
	u16 cs_it;
	int i;

	cs_it = chip->conf_regs[CM3391_REG_ADDR_CMD];
	cs_it &= CM3391_CMD_CS_IT_MASK;
	cs_it >>= CM3391_CMD_CS_IT_SHIFT;
	for (i = 0; i < ARRAY_SIZE(cm3391_cs_it_scales); i++) {
		if (cs_it == cm3391_cs_it_scales[i].it) {
			*val = cm3391_cs_it_scales[i].val;
			*val2 = cm3391_cs_it_scales[i].val2;
			return IIO_VAL_INT_PLUS_MICRO;
		}
	}

	return -EINVAL;
}

#ifndef OLDAPI
/**
 * cm3391_write_cs_it() - Write sensor integration time
 * @chip:	pointer of struct cm3391.
 * @val:	integration time in milliseconds.
 *
 * Convert integration time (ms) to sensor value.
 *
 * Return: i2c_smbus_write_word_data command return value.
 */
static int cm3391_write_cs_it(struct cm3391_chip *chip, int val, int val2)
{
	struct i2c_client *client = chip->client;
	u16 cs_it, cmd;
	int i;
	s32 ret;

	for (i = 0; i < ARRAY_SIZE(cm3391_cs_it_scales); i++) {
		if (val == cm3391_cs_it_scales[i].val &&
			val2 == cm3391_cs_it_scales[i].val2) {

			cs_it = cm3391_cs_it_scales[i].it;
			cs_it <<= CM3391_CMD_CS_IT_SHIFT;

			cmd = chip->conf_regs[CM3391_REG_ADDR_CMD];
			cmd &= ~CM3391_CMD_CS_IT_MASK;
			cmd |= cs_it;
			ret = i2c_smbus_write_word_data(client,
				CM3391_REG_ADDR_CMD, cmd);

			if (ret < 0)
				return ret;
			
			chip->conf_regs[CM3391_REG_ADDR_CMD] = cmd;
			return 0;
		}
	}
	return ret;
}
#endif	/* !OLDAPI */

/**
 * cm3391_get_lux() - report current lux value
 * @chip:	pointer of struct cm3391.
 *
 * Convert sensor raw data to lux.  It depends on integration
 * time and calibscale variable.
 *
 * Return: Positive value is lux, otherwise is error code.
 */
static int cm3391_get_lux(struct cm3391_chip *chip)
{
	struct i2c_client *client = chip->client;
	struct cm3391_cs_info *cs_info = chip->cs_info;
	int ret;
	int val, val2;
	int cs_it;
	u64 lux;

	/* Calculate mlux per bit based on cs_it */
	ret = cm3391_read_cs_it(chip, &val, &val2);
	if (ret < 0)
		return -EINVAL;
	cs_it = val * 1000000 + val2;
	lux = (__force u64)cs_info->mlux_per_bit;
	lux *= cs_info->mlux_per_bit_base_it;
	lux = div_u64(lux, cs_it);

	/* Get als_raw */
	ret = i2c_smbus_read_word_data(client, CM3391_REG_ADDR_ALS);
	if (ret < 0) {
		dev_err(&client->dev, "Error reading reg_addr_als\n");
		return ret;
	}
	cs_info->als_raw = (u16)ret;
	lux *= cs_info->als_raw;
	lux *= cs_info->calibscale;
	lux = div_u64(lux, CM3391_CALIBSCALE_RESOLUTION);
	lux = div_u64(lux, CM3391_MLUX_PER_LUX);

	if (lux > 0xFFFF)
		lux = 0xFFFF;

	cm3391_autoit(chip, cs_info->als_raw);

	return (int)lux;
}

static int cm3391_read_raw(struct iio_dev *indio_dev,
			struct iio_chan_spec const *chan,
			int *val, int *val2, long mask)
{
	struct cm3391_chip *chip = iio_priv(indio_dev);
	struct cm3391_cs_info *cs_info = chip->cs_info;
	int ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		ret = i2c_smbus_read_word_data(chip->client, chan->address);
		if (ret < 0) {
			return ret;
		}
		*val = ret;
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_PROCESSED:
		ret = cm3391_get_lux(chip);
		if (ret < 0)
			return ret;
		*val = ret;
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_CALIBSCALE:
		*val = cs_info->calibscale;
		return IIO_VAL_INT;
#ifndef OLDAPI
	case IIO_CHAN_INFO_INT_TIME:
		ret = cm3391_read_cs_it(chip, val, val2);
		return ret;
#endif	/* OLDAPI */
	}

	return -EINVAL;
}

static int cm3391_write_raw(struct iio_dev *indio_dev,
			struct iio_chan_spec const *chan,
			int val, int val2, long mask)
{
	struct cm3391_chip *chip = iio_priv(indio_dev);
	struct cm3391_cs_info *cs_info = chip->cs_info;

	switch (mask) {
	case IIO_CHAN_INFO_CALIBSCALE:
		cs_info->calibscale = val;
		return 0;
#ifndef OLDAPI
	case IIO_CHAN_INFO_INT_TIME:
		return cm3391_write_cs_it(chip, val, val2);
#endif	/* OLDAPI */
	}

	return -EINVAL;
}

/**
 * cm3391_get_it_available() - Get available CS IT value
 * @dev:	pointer of struct device.
 * @attr:	pointer of struct device_attribute.
 * @buf:	pointer of return string buffer.
 *
 * Display the available integration time in milliseconds.
 *
 * Return: string length.
 */
static ssize_t cm3391_get_it_available(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	int i, len;

	for (i = 0, len = 0; i < ARRAY_SIZE(cm3391_cs_it_scales); i++)
		len += scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ",
			cm3391_cs_it_scales[i].val,
			cm3391_cs_it_scales[i].val2);
	return len + scnprintf(buf + len, PAGE_SIZE - len, "\n");
}

/**
 * cm3391_threshold_update() - Update the threshold registers.
 * @chip:	pointer of struct cm3391_chip.
 * @percent:	+/- percent.
 *
 * Based on the current ALS value, update the hi and low threshold registers.
 *
 * Return: 0 for success; otherwise for error code.
 */
static int cm3391_threshold_update(struct cm3391_chip *chip, int percent)
{
	struct i2c_client *client = chip->client;
	struct cm3391_cs_info *cs_info = chip->cs_info;
	int ret;
	int wh, wl;

	ret = cs_info->als_raw = i2c_smbus_read_word_data(client,
					CM3391_REG_ADDR_ALS);
	if (ret < 0)
		return ret;

	wh = wl = ret;
	ret *= percent;
	ret /= 100;
	if (ret < 1)
		ret = 1;
	wh += ret;
	wl -= ret;
	if (wh > 65535)
		wh = 65535;
	if (wl < 0)
		wl = 0;

	chip->conf_regs[CM3391_REG_ADDR_WH] = wh;
	ret = i2c_smbus_write_word_data(
		client,
		CM3391_REG_ADDR_WH,
		chip->conf_regs[CM3391_REG_ADDR_WH]);
	if (ret < 0)
		return ret;

	chip->conf_regs[CM3391_REG_ADDR_WL] = wl;
	ret = i2c_smbus_write_word_data(
		client,
		CM3391_REG_ADDR_WL,
		chip->conf_regs[CM3391_REG_ADDR_WL]);

	return ret;
}

/**
 * cm3391_event_handler() - Interrupt handling routine.
 * @irq:	irq number.
 * @private:	pointer of void.
 *
 * Clean interrupt and reset threshold registers.
 *
 * Return: IRQ_HANDLED.
 */
static irqreturn_t cm3391_event_handler(int irq, void *private)
{
	struct iio_dev *dev_info = private;
	struct cm3391_chip *chip = iio_priv(dev_info);
	int ret;

	mutex_lock(&chip->lock);

	/* Disable interrupt */
	ret = cm3391_interrupt_config(chip, 0);
	if (ret < 0)
		goto error_handler_unlock;

	/* Update Hi/Lo windows */
	ret = cm3391_threshold_update(chip, CM3391_THRESHOLD_PERCENT);
	if (ret < 0)
		goto error_handler_unlock;

	/* Enable interrupt */
	cm3391_interrupt_config(chip, 1);

error_handler_unlock:
	mutex_unlock(&chip->lock);
	return IRQ_HANDLED;
}

#ifdef OLDAPI
#define CM3391_COLOR_CHANNEL(_color, _addr) { \
	.type = IIO_INTENSITY, \
	.modified = 1, \
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
	.channel2 = IIO_MOD_LIGHT_##_color, \
	.address = _addr, \
}

static const struct iio_chan_spec cm3391_channels[] = {
	{
		.type = IIO_LIGHT,
		.info_mask_separate =
			BIT(IIO_CHAN_INFO_PROCESSED) |
			BIT(IIO_CHAN_INFO_CALIBSCALE),
	},
	CM3391_COLOR_CHANNEL(RED, CM3391_REG_ADDR_RED),
	CM3391_COLOR_CHANNEL(GREEN, CM3391_REG_ADDR_ALS),
	CM3391_COLOR_CHANNEL(BLUE, CM3391_REG_ADDR_BLUE),
	CM3391_COLOR_CHANNEL(CLEAR, CM3391_REG_ADDR_CLEAR),
};
#else
#define CM3391_COLOR_CHANNEL(_color, _addr) { \
	.type = IIO_INTENSITY, \
	.modified = 1, \
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
	.channel2 = IIO_MOD_LIGHT_##_color, \
	.address = _addr, \
}

static const struct iio_chan_spec cm3391_channels[] = {
	{
		.type = IIO_LIGHT,
		.info_mask_separate =
			BIT(IIO_CHAN_INFO_PROCESSED) |
			BIT(IIO_CHAN_INFO_CALIBSCALE) |
			BIT(IIO_CHAN_INFO_INT_TIME),
	},
	CM3391_COLOR_CHANNEL(RED, CM3391_REG_ADDR_RED),
	CM3391_COLOR_CHANNEL(GREEN, CM3391_REG_ADDR_ALS),
	CM3391_COLOR_CHANNEL(BLUE, CM3391_REG_ADDR_BLUE),
	CM3391_COLOR_CHANNEL(CLEAR, CM3391_REG_ADDR_CLEAR),
};
#endif	/* OLDAPI */

static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
			S_IRUGO, cm3391_get_it_available, NULL, 0);

static struct attribute *cm3391_attributes[] = {
	&iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
	NULL,
};

static const struct attribute_group cm3391_attribute_group = {
	.attrs = cm3391_attributes
};

static const struct iio_info cm3391_info = {
	.driver_module		= THIS_MODULE,
	.read_raw		= &cm3391_read_raw,
	.write_raw		= &cm3391_write_raw,
	.attrs			= &cm3391_attribute_group,
};

static int cm3391_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct cm3391_chip *chip;
	struct iio_dev *indio_dev;
	int ret;

#ifdef OLDAPI
	indio_dev = iio_device_alloc(sizeof(*chip));
	if (!indio_dev) {
		dev_err(&client->dev, "iio_device_alloc failed\n");
		return -ENOMEM;
	}
#else
	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
	if (!indio_dev) {
		dev_err(&client->dev, "devm_iio_device_alloc failed\n");
		return -ENOMEM;
	}
#endif	/* OLDAPI */

	chip = iio_priv(indio_dev);
	i2c_set_clientdata(client, indio_dev);
	chip->client = client;

	mutex_init(&chip->lock);
	indio_dev->dev.parent = &client->dev;
	indio_dev->channels = cm3391_channels;
	indio_dev->num_channels = ARRAY_SIZE(cm3391_channels);
	indio_dev->info = &cm3391_info;
	if (id && id->name)
		indio_dev->name = id->name;
	else
		indio_dev->name = (char *)dev_name(&client->dev);
	indio_dev->channels = cm3391_channels;
	indio_dev->num_channels = ARRAY_SIZE(cm3391_channels);
	indio_dev->modes = INDIO_DIRECT_MODE;

	ret = cm3391_reg_init(chip);
	if (ret) {
		dev_err(&client->dev,
			"%s: register init failed\n",
			__func__);
		return ret;
	}

	if (client->irq) {
		ret = request_threaded_irq(client->irq,
					NULL,
					cm3391_event_handler,
					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
					"cm3391_event",
					indio_dev);

		if (ret < 0) {
			dev_err(&client->dev, "irq request error %d\n",
				-ret);
			goto error_disable_int;
		}
	}

	ret = iio_device_register(indio_dev);
	if (ret < 0) {
		dev_err(&client->dev,
			"%s: regist device failed\n",
			__func__);
		goto error_free_irq;
	}

	if (client->irq) {
		ret = cm3391_threshold_update(chip, CM3391_THRESHOLD_PERCENT);
		if (ret < 0)
			goto error_free_irq;

		ret = cm3391_interrupt_config(chip, 1);
		if (ret < 0)
			goto error_free_irq;
	}

	return 0;

error_free_irq:
	if (client->irq)
		free_irq(client->irq, indio_dev);
#ifdef OLDAPI
	iio_device_free(indio_dev);
#endif	/* OLDAPI */
error_disable_int:
	cm3391_interrupt_config(chip, 0);
	return ret;
}

static int cm3391_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct cm3391_chip *chip = iio_priv(indio_dev);

	cm3391_interrupt_config(chip, 0);
	if (client->irq)
		free_irq(client->irq, indio_dev);
	iio_device_unregister(indio_dev);
#ifdef OLDAPI
	iio_device_free(indio_dev);
#endif	/* OLDAPI */
	return 0;
}

static const struct i2c_device_id cm3391_id[] = {
	{ "cm3391", 0},
	{ }
};

MODULE_DEVICE_TABLE(i2c, cm3391_id);

static const struct of_device_id cm3391_of_match[] = {
	{ .compatible = "capella,cm3391" },
	{ }
};

#ifdef CONFIG_ACPI
static const struct acpi_device_id cm3391_acpi_match[] = {
	{ "CPLM3391", 0},
	{},
};

MODULE_DEVICE_TABLE(acpi, cm3391_acpi_match);
#endif /* CONFIG_ACPI */

static struct i2c_driver cm3391_driver = {
	.driver = {
		.name	= "cm3391",
#ifdef CONFIG_ACPI
		.acpi_match_table = ACPI_PTR(cm3391_acpi_match),
#endif /* CONFIG_ACPI */
		.of_match_table = of_match_ptr(cm3391_of_match),
		.owner	= THIS_MODULE,
	},
	.id_table	= cm3391_id,
	.probe		= cm3391_probe,
	.remove		= cm3391_remove,
};

module_i2c_driver(cm3391_driver);

MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
MODULE_DESCRIPTION("CM3391 color sensor driver");
MODULE_LICENSE("GPL");