cm36651.c 19.4 KB
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
/*
 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
 * Author: Beomho Seo <beomho.seo@samsung.com>
 *
 * 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.
 */

#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>

/* Slave address 0x19 for PS of 7 bit addressing protocol for I2C */
#define CM36651_I2C_ADDR_PS		0x19
/* Alert Response Address */
#define CM36651_ARA			0x0C

/* Ambient light sensor */
#define CM36651_CS_CONF1		0x00
#define CM36651_CS_CONF2		0x01
#define CM36651_ALS_WH_M		0x02
#define CM36651_ALS_WH_L		0x03
#define CM36651_ALS_WL_M		0x04
#define CM36651_ALS_WL_L		0x05
#define CM36651_CS_CONF3		0x06
#define CM36651_CS_CONF_REG_NUM		0x02

/* Proximity sensor */
#define CM36651_PS_CONF1		0x00
#define CM36651_PS_THD			0x01
#define CM36651_PS_CANC			0x02
#define CM36651_PS_CONF2		0x03
#define CM36651_PS_REG_NUM		0x04

/* CS_CONF1 command code */
#define CM36651_ALS_ENABLE		0x00
#define CM36651_ALS_DISABLE		0x01
#define CM36651_ALS_INT_EN		0x02
#define CM36651_ALS_THRES		0x04

/* CS_CONF2 command code */
#define CM36651_CS_CONF2_DEFAULT_BIT	0x08

/* CS_CONF3 channel integration time */
#define CM36651_CS_IT1			0x00 /* Integration time 80 msec */
#define CM36651_CS_IT2			0x40 /* Integration time 160 msec */
#define CM36651_CS_IT3			0x80 /* Integration time 320 msec */
#define CM36651_CS_IT4			0xC0 /* Integration time 640 msec */

/* PS_CONF1 command code */
#define CM36651_PS_ENABLE		0x00
#define CM36651_PS_DISABLE		0x01
#define CM36651_PS_INT_EN		0x02
#define CM36651_PS_PERS2		0x04
#define CM36651_PS_PERS3		0x08
#define CM36651_PS_PERS4		0x0C

/* PS_CONF1 command code: integration time */
#define CM36651_PS_IT1			0x00 /* Integration time 0.32 msec */
#define CM36651_PS_IT2			0x10 /* Integration time 0.42 msec */
#define CM36651_PS_IT3			0x20 /* Integration time 0.52 msec */
#define CM36651_PS_IT4			0x30 /* Integration time 0.64 msec */

/* PS_CONF1 command code: duty ratio */
#define CM36651_PS_DR1			0x00 /* Duty ratio 1/80 */
#define CM36651_PS_DR2			0x40 /* Duty ratio 1/160 */
#define CM36651_PS_DR3			0x80 /* Duty ratio 1/320 */
#define CM36651_PS_DR4			0xC0 /* Duty ratio 1/640 */

/* PS_THD command code */
#define CM36651_PS_INITIAL_THD		0x05

/* PS_CANC command code */
#define CM36651_PS_CANC_DEFAULT		0x00

/* PS_CONF2 command code */
#define CM36651_PS_HYS1			0x00
#define CM36651_PS_HYS2			0x01
#define CM36651_PS_SMART_PERS_EN	0x02
#define CM36651_PS_DIR_INT		0x04
#define CM36651_PS_MS			0x10

#define CM36651_CS_COLOR_NUM		4

#define CM36651_CLOSE_PROXIMITY		0x32
#define CM36651_FAR_PROXIMITY			0x33

#define CM36651_CS_INT_TIME_AVAIL	"0.08 0.16 0.32 0.64"
#define CM36651_PS_INT_TIME_AVAIL	"0.000320 0.000420 0.000520 0.000640"

enum cm36651_operation_mode {
	CM36651_LIGHT_EN,
	CM36651_PROXIMITY_EN,
	CM36651_PROXIMITY_EV_EN,
};

enum cm36651_light_channel_idx {
	CM36651_LIGHT_CHANNEL_IDX_RED,
	CM36651_LIGHT_CHANNEL_IDX_GREEN,
	CM36651_LIGHT_CHANNEL_IDX_BLUE,
	CM36651_LIGHT_CHANNEL_IDX_CLEAR,
};

enum cm36651_command {
	CM36651_CMD_READ_RAW_LIGHT,
	CM36651_CMD_READ_RAW_PROXIMITY,
	CM36651_CMD_PROX_EV_EN,
	CM36651_CMD_PROX_EV_DIS,
};

static const u8 cm36651_cs_reg[CM36651_CS_CONF_REG_NUM] = {
	CM36651_CS_CONF1,
	CM36651_CS_CONF2,
};

static const u8 cm36651_ps_reg[CM36651_PS_REG_NUM] = {
	CM36651_PS_CONF1,
	CM36651_PS_THD,
	CM36651_PS_CANC,
	CM36651_PS_CONF2,
};

struct cm36651_data {
	const struct cm36651_platform_data *pdata;
	struct i2c_client *client;
	struct i2c_client *ps_client;
	struct i2c_client *ara_client;
	struct mutex lock;
	struct regulator *vled_reg;
	unsigned long flags;
	int cs_int_time[CM36651_CS_COLOR_NUM];
	int ps_int_time;
	u8 cs_ctrl_regs[CM36651_CS_CONF_REG_NUM];
	u8 ps_ctrl_regs[CM36651_PS_REG_NUM];
	u16 color[CM36651_CS_COLOR_NUM];
};

static int cm36651_setup_reg(struct cm36651_data *cm36651)
{
	struct i2c_client *client = cm36651->client;
	struct i2c_client *ps_client = cm36651->ps_client;
	int i, ret;

	/* CS initialization */
	cm36651->cs_ctrl_regs[CM36651_CS_CONF1] = CM36651_ALS_ENABLE |
							     CM36651_ALS_THRES;
	cm36651->cs_ctrl_regs[CM36651_CS_CONF2] = CM36651_CS_CONF2_DEFAULT_BIT;

	for (i = 0; i < CM36651_CS_CONF_REG_NUM; i++) {
		ret = i2c_smbus_write_byte_data(client, cm36651_cs_reg[i],
						     cm36651->cs_ctrl_regs[i]);
		if (ret < 0)
			return ret;
	}

	/* PS initialization */
	cm36651->ps_ctrl_regs[CM36651_PS_CONF1] = CM36651_PS_ENABLE |
								CM36651_PS_IT2;
	cm36651->ps_ctrl_regs[CM36651_PS_THD] = CM36651_PS_INITIAL_THD;
	cm36651->ps_ctrl_regs[CM36651_PS_CANC] = CM36651_PS_CANC_DEFAULT;
	cm36651->ps_ctrl_regs[CM36651_PS_CONF2] = CM36651_PS_HYS2 |
				CM36651_PS_DIR_INT | CM36651_PS_SMART_PERS_EN;

	for (i = 0; i < CM36651_PS_REG_NUM; i++) {
		ret = i2c_smbus_write_byte_data(ps_client, cm36651_ps_reg[i],
						     cm36651->ps_ctrl_regs[i]);
		if (ret < 0)
			return ret;
	}

	/* Set shutdown mode */
	ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
							  CM36651_ALS_DISABLE);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(cm36651->ps_client,
					 CM36651_PS_CONF1, CM36651_PS_DISABLE);
	if (ret < 0)
		return ret;

	return 0;
}

static int cm36651_read_output(struct cm36651_data *cm36651,
				struct iio_chan_spec const *chan, int *val)
{
	struct i2c_client *client = cm36651->client;
	int ret = -EINVAL;

	switch (chan->type) {
	case IIO_LIGHT:
		*val = i2c_smbus_read_word_data(client, chan->address);
		if (*val < 0)
			return ret;

		ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
							CM36651_ALS_DISABLE);
		if (ret < 0)
			return ret;

		ret = IIO_VAL_INT;
		break;
	case IIO_PROXIMITY:
		*val = i2c_smbus_read_byte(cm36651->ps_client);
		if (*val < 0)
			return ret;

		if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
			ret = i2c_smbus_write_byte_data(cm36651->ps_client,
					CM36651_PS_CONF1, CM36651_PS_DISABLE);
			if (ret < 0)
				return ret;
		}

		ret = IIO_VAL_INT;
		break;
	default:
		break;
	}

	return ret;
}

static irqreturn_t cm36651_irq_handler(int irq, void *data)
{
	struct iio_dev *indio_dev = data;
	struct cm36651_data *cm36651 = iio_priv(indio_dev);
	struct i2c_client *client = cm36651->client;
	int ev_dir, ret;
	u64 ev_code;

	/*
	 * The PS INT pin is an active low signal that PS INT move logic low
	 * when the object is detect. Once the MCU host received the PS INT
	 * "LOW" signal, the Host needs to read the data at Alert Response
	 * Address(ARA) to clear the PS INT signal. After clearing the PS
	 * INT pin, the PS INT signal toggles from low to high.
	 */
	ret = i2c_smbus_read_byte(cm36651->ara_client);
	if (ret < 0) {
		dev_err(&client->dev,
				"%s: Data read failed: %d\n", __func__, ret);
		return IRQ_HANDLED;
	}
	switch (ret) {
	case CM36651_CLOSE_PROXIMITY:
		ev_dir = IIO_EV_DIR_RISING;
		break;
	case CM36651_FAR_PROXIMITY:
		ev_dir = IIO_EV_DIR_FALLING;
		break;
	default:
		dev_err(&client->dev,
			"%s: Data read wrong: %d\n", __func__, ret);
		return IRQ_HANDLED;
	}

	ev_code = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
				CM36651_CMD_READ_RAW_PROXIMITY,
				IIO_EV_TYPE_THRESH, ev_dir);

	iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev));

	return IRQ_HANDLED;
}

static int cm36651_set_operation_mode(struct cm36651_data *cm36651, int cmd)
{
	struct i2c_client *client = cm36651->client;
	struct i2c_client *ps_client = cm36651->ps_client;
	int ret = -EINVAL;

	switch (cmd) {
	case CM36651_CMD_READ_RAW_LIGHT:
		ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
				cm36651->cs_ctrl_regs[CM36651_CS_CONF1]);
		break;
	case CM36651_CMD_READ_RAW_PROXIMITY:
		if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags))
			return CM36651_PROXIMITY_EV_EN;

		ret = i2c_smbus_write_byte_data(ps_client, CM36651_PS_CONF1,
				cm36651->ps_ctrl_regs[CM36651_PS_CONF1]);
		break;
	case CM36651_CMD_PROX_EV_EN:
		if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
			dev_err(&client->dev,
				"Already proximity event enable state\n");
			return ret;
		}
		set_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);

		ret = i2c_smbus_write_byte_data(ps_client,
			cm36651_ps_reg[CM36651_PS_CONF1],
			CM36651_PS_INT_EN | CM36651_PS_PERS2 | CM36651_PS_IT2);

		if (ret < 0) {
			dev_err(&client->dev, "Proximity enable event failed\n");
			return ret;
		}
		break;
	case CM36651_CMD_PROX_EV_DIS:
		if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
			dev_err(&client->dev,
				"Already proximity event disable state\n");
			return ret;
		}
		clear_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
		ret = i2c_smbus_write_byte_data(ps_client,
					CM36651_PS_CONF1, CM36651_PS_DISABLE);
		break;
	}

	if (ret < 0)
		dev_err(&client->dev, "Write register failed\n");

	return ret;
}

static int cm36651_read_channel(struct cm36651_data *cm36651,
				struct iio_chan_spec const *chan, int *val)
{
	struct i2c_client *client = cm36651->client;
	int cmd, ret;

	if (chan->type == IIO_LIGHT)
		cmd = CM36651_CMD_READ_RAW_LIGHT;
	else if (chan->type == IIO_PROXIMITY)
		cmd = CM36651_CMD_READ_RAW_PROXIMITY;
	else
		return -EINVAL;

	ret = cm36651_set_operation_mode(cm36651, cmd);
	if (ret < 0) {
		dev_err(&client->dev, "CM36651 set operation mode failed\n");
		return ret;
	}
	/* Delay for work after enable operation */
	msleep(50);
	ret = cm36651_read_output(cm36651, chan, val);
	if (ret < 0) {
		dev_err(&client->dev, "CM36651 read output failed\n");
		return ret;
	}

	return ret;
}

static int cm36651_read_int_time(struct cm36651_data *cm36651,
				struct iio_chan_spec const *chan, int *val2)
{
	switch (chan->type) {
	case IIO_LIGHT:
		if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT1)
			*val2 = 80000;
		else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT2)
			*val2 = 160000;
		else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT3)
			*val2 = 320000;
		else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT4)
			*val2 = 640000;
		else
			return -EINVAL;
		break;
	case IIO_PROXIMITY:
		if (cm36651->ps_int_time == CM36651_PS_IT1)
			*val2 = 320;
		else if (cm36651->ps_int_time == CM36651_PS_IT2)
			*val2 = 420;
		else if (cm36651->ps_int_time == CM36651_PS_IT3)
			*val2 = 520;
		else if (cm36651->ps_int_time == CM36651_PS_IT4)
			*val2 = 640;
		else
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	return IIO_VAL_INT_PLUS_MICRO;
}

static int cm36651_write_int_time(struct cm36651_data *cm36651,
				struct iio_chan_spec const *chan, int val)
{
	struct i2c_client *client = cm36651->client;
	struct i2c_client *ps_client = cm36651->ps_client;
	int int_time, ret;

	switch (chan->type) {
	case IIO_LIGHT:
		if (val == 80000)
			int_time = CM36651_CS_IT1;
		else if (val == 160000)
			int_time = CM36651_CS_IT2;
		else if (val == 320000)
			int_time = CM36651_CS_IT3;
		else if (val == 640000)
			int_time = CM36651_CS_IT4;
		else
			return -EINVAL;

		ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF3,
					   int_time >> 2 * (chan->address));
		if (ret < 0) {
			dev_err(&client->dev, "CS integration time write failed\n");
			return ret;
		}
		cm36651->cs_int_time[chan->address] = int_time;
		break;
	case IIO_PROXIMITY:
		if (val == 320)
			int_time = CM36651_PS_IT1;
		else if (val == 420)
			int_time = CM36651_PS_IT2;
		else if (val == 520)
			int_time = CM36651_PS_IT3;
		else if (val == 640)
			int_time = CM36651_PS_IT4;
		else
			return -EINVAL;

		ret = i2c_smbus_write_byte_data(ps_client,
						CM36651_PS_CONF1, int_time);
		if (ret < 0) {
			dev_err(&client->dev, "PS integration time write failed\n");
			return ret;
		}
		cm36651->ps_int_time = int_time;
		break;
	default:
		return -EINVAL;
	}

	return ret;
}

static int cm36651_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long mask)
{
	struct cm36651_data *cm36651 = iio_priv(indio_dev);
	int ret;

	mutex_lock(&cm36651->lock);

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		ret = cm36651_read_channel(cm36651, chan, val);
		break;
	case IIO_CHAN_INFO_INT_TIME:
		*val = 0;
		ret = cm36651_read_int_time(cm36651, chan, val2);
		break;
	default:
		ret = -EINVAL;
	}

	mutex_unlock(&cm36651->lock);

	return ret;
}

static int cm36651_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{
	struct cm36651_data *cm36651 = iio_priv(indio_dev);
	struct i2c_client *client = cm36651->client;
	int ret = -EINVAL;

	if (mask == IIO_CHAN_INFO_INT_TIME) {
		ret = cm36651_write_int_time(cm36651, chan, val2);
		if (ret < 0)
			dev_err(&client->dev, "Integration time write failed\n");
	}

	return ret;
}

static int cm36651_read_prox_thresh(struct iio_dev *indio_dev,
					const struct iio_chan_spec *chan,
					enum iio_event_type type,
					enum iio_event_direction dir,
					enum iio_event_info info,
					int *val, int *val2)
{
	struct cm36651_data *cm36651 = iio_priv(indio_dev);

	*val = cm36651->ps_ctrl_regs[CM36651_PS_THD];

	return 0;
}

static int cm36651_write_prox_thresh(struct iio_dev *indio_dev,
					const struct iio_chan_spec *chan,
					enum iio_event_type type,
					enum iio_event_direction dir,
					enum iio_event_info info,
					int val, int val2)
{
	struct cm36651_data *cm36651 = iio_priv(indio_dev);
	struct i2c_client *client = cm36651->client;
	int ret;

	if (val < 3 || val > 255)
		return -EINVAL;

	cm36651->ps_ctrl_regs[CM36651_PS_THD] = val;
	ret = i2c_smbus_write_byte_data(cm36651->ps_client, CM36651_PS_THD,
					cm36651->ps_ctrl_regs[CM36651_PS_THD]);

	if (ret < 0) {
		dev_err(&client->dev, "PS threshold write failed: %d\n", ret);
		return ret;
	}

	return 0;
}

static int cm36651_write_prox_event_config(struct iio_dev *indio_dev,
					const struct iio_chan_spec *chan,
					enum iio_event_type type,
					enum iio_event_direction dir,
					int state)
{
	struct cm36651_data *cm36651 = iio_priv(indio_dev);
	int cmd, ret = -EINVAL;

	mutex_lock(&cm36651->lock);

	cmd = state ? CM36651_CMD_PROX_EV_EN : CM36651_CMD_PROX_EV_DIS;
	ret = cm36651_set_operation_mode(cm36651, cmd);

	mutex_unlock(&cm36651->lock);

	return ret;
}

static int cm36651_read_prox_event_config(struct iio_dev *indio_dev,
					const struct iio_chan_spec *chan,
					enum iio_event_type type,
					enum iio_event_direction dir)
{
	struct cm36651_data *cm36651 = iio_priv(indio_dev);
	int event_en;

	mutex_lock(&cm36651->lock);

	event_en = test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);

	mutex_unlock(&cm36651->lock);

	return event_en;
}

#define CM36651_LIGHT_CHANNEL(_color, _idx) {		\
	.type = IIO_LIGHT,				\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
			BIT(IIO_CHAN_INFO_INT_TIME),	\
	.address = _idx,				\
	.modified = 1,					\
	.channel2 = IIO_MOD_LIGHT_##_color,		\
}							\

static const struct iio_event_spec cm36651_event_spec[] = {
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_EITHER,
		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
				BIT(IIO_EV_INFO_ENABLE),
	}
};

static const struct iio_chan_spec cm36651_channels[] = {
	{
		.type = IIO_PROXIMITY,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_INT_TIME),
		.event_spec = cm36651_event_spec,
		.num_event_specs = ARRAY_SIZE(cm36651_event_spec),
	},
	CM36651_LIGHT_CHANNEL(RED, CM36651_LIGHT_CHANNEL_IDX_RED),
	CM36651_LIGHT_CHANNEL(GREEN, CM36651_LIGHT_CHANNEL_IDX_GREEN),
	CM36651_LIGHT_CHANNEL(BLUE, CM36651_LIGHT_CHANNEL_IDX_BLUE),
	CM36651_LIGHT_CHANNEL(CLEAR, CM36651_LIGHT_CHANNEL_IDX_CLEAR),
};

static IIO_CONST_ATTR(in_illuminance_integration_time_available,
					CM36651_CS_INT_TIME_AVAIL);
static IIO_CONST_ATTR(in_proximity_integration_time_available,
					CM36651_PS_INT_TIME_AVAIL);

static struct attribute *cm36651_attributes[] = {
	&iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
	&iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
	NULL,
};

static const struct attribute_group cm36651_attribute_group = {
	.attrs = cm36651_attributes
};

static const struct iio_info cm36651_info = {
	.driver_module		= THIS_MODULE,
	.read_raw		= &cm36651_read_raw,
	.write_raw		= &cm36651_write_raw,
	.read_event_value	= &cm36651_read_prox_thresh,
	.write_event_value	= &cm36651_write_prox_thresh,
	.read_event_config	= &cm36651_read_prox_event_config,
	.write_event_config	= &cm36651_write_prox_event_config,
	.attrs			= &cm36651_attribute_group,
};

static int cm36651_probe(struct i2c_client *client,
			     const struct i2c_device_id *id)
{
	struct cm36651_data *cm36651;
	struct iio_dev *indio_dev;
	int ret;

	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm36651));
	if (!indio_dev)
		return -ENOMEM;

	cm36651 = iio_priv(indio_dev);

	cm36651->vled_reg = devm_regulator_get(&client->dev, "vled");
	if (IS_ERR(cm36651->vled_reg)) {
		dev_err(&client->dev, "get regulator vled failed\n");
		return PTR_ERR(cm36651->vled_reg);
	}

	ret = regulator_enable(cm36651->vled_reg);
	if (ret) {
		dev_err(&client->dev, "enable regulator vled failed\n");
		return ret;
	}

	i2c_set_clientdata(client, indio_dev);

	cm36651->client = client;
	cm36651->ps_client = i2c_new_dummy(client->adapter,
						     CM36651_I2C_ADDR_PS);
	if (!cm36651->ps_client) {
		dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
		ret = -ENODEV;
		goto error_disable_reg;
	}

	cm36651->ara_client = i2c_new_dummy(client->adapter, CM36651_ARA);
	if (!cm36651->ara_client) {
		dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
		ret = -ENODEV;
		goto error_i2c_unregister_ps;
	}

	mutex_init(&cm36651->lock);
	indio_dev->dev.parent = &client->dev;
	indio_dev->channels = cm36651_channels;
	indio_dev->num_channels = ARRAY_SIZE(cm36651_channels);
	indio_dev->info = &cm36651_info;
	indio_dev->name = id->name;
	indio_dev->modes = INDIO_DIRECT_MODE;

	ret = cm36651_setup_reg(cm36651);
	if (ret) {
		dev_err(&client->dev, "%s: register setup failed\n", __func__);
		goto error_i2c_unregister_ara;
	}

	ret = request_threaded_irq(client->irq, NULL, cm36651_irq_handler,
					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
							"cm36651", indio_dev);
	if (ret) {
		dev_err(&client->dev, "%s: request irq failed\n", __func__);
		goto error_i2c_unregister_ara;
	}

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

	return 0;

error_free_irq:
	free_irq(client->irq, indio_dev);
error_i2c_unregister_ara:
	i2c_unregister_device(cm36651->ara_client);
error_i2c_unregister_ps:
	i2c_unregister_device(cm36651->ps_client);
error_disable_reg:
	regulator_disable(cm36651->vled_reg);
	return ret;
}

static int cm36651_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct cm36651_data *cm36651 = iio_priv(indio_dev);

	iio_device_unregister(indio_dev);
	regulator_disable(cm36651->vled_reg);
	free_irq(client->irq, indio_dev);
	i2c_unregister_device(cm36651->ps_client);
	i2c_unregister_device(cm36651->ara_client);

	return 0;
}

static const struct i2c_device_id cm36651_id[] = {
	{ "cm36651", 0 },
	{ }
};

MODULE_DEVICE_TABLE(i2c, cm36651_id);

static const struct of_device_id cm36651_of_match[] = {
	{ .compatible = "capella,cm36651" },
	{ }
};
MODULE_DEVICE_TABLE(of, cm36651_of_match);

static struct i2c_driver cm36651_driver = {
	.driver = {
		.name	= "cm36651",
		.of_match_table = cm36651_of_match,
	},
	.probe		= cm36651_probe,
	.remove		= cm36651_remove,
	.id_table	= cm36651_id,
};

module_i2c_driver(cm36651_driver);

MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
MODULE_DESCRIPTION("CM36651 proximity/ambient light sensor driver");
MODULE_LICENSE("GPL v2");