2 * A iio driver for the capacitive sensor IQS253.
4 * IIO Light driver for monitoring proximity.
6 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/platform_device.h>
25 #include <linux/gpio.h>
26 #include <linux/of_gpio.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/irqchip/tegra.h>
30 #include <linux/input.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
33 #include <linux/iio/light/ls_sysfs.h>
34 #include <linux/iio/light/ls_dt.h>
38 #define PROX_STATUS 0x31
39 #define TOUCH_STATUS 0x35
42 #define CH0_ATI_BASE 0xC8
43 #define CH1_ATI_BASE 0xC9
44 #define CH2_ATI_BASE 0xCA
48 #define PROX_SETTINGS0 0xD1
49 #define PROX_SETTINGS1 0xD2
50 #define PROX_SETTINGS2 0xD3
51 #define PROX_SETTINGS3 0xD4
52 #define ACTIVE_CHAN 0xD5
53 #define LOW_POWER 0xD6
54 #define DYCAL_CHANS 0xD8
55 #define EVENT_MODE_MASK 0xD9
56 #define DEFAULT_COMMS_POINTER 0xDD
58 #define IQS253_PROD_ID 41
59 #define IQS263_PROD_ID 0x3C
65 #define STYLUS_ONLY PROX_CH0
66 #define PROXIMITY_ONLY (PROX_CH1 | PROX_CH2)
68 #define CH0_COMPENSATION 0x55
70 #define PROX_TH_CH0 0x03
71 #define PROX_TH_CH1 0x03
72 #define PROX_TH_CH2 0x03
74 #define DISABLE_DYCAL 0x00
76 #define CH0_ATI_TH 0xCC
77 #define CH1_ATI_TH 0x40
78 #define CH2_ATI_TH 0x40
80 #define EVENT_PROX_ONLY 0x01
82 #define PROX_SETTING_NORMAL 0x25
83 #define PROX_SETTING_STYLUS 0x26
85 #define AUTO_ATI_DISABLE BIT(7)
86 #define ATI_IN_PROGRESS 0x04
90 #define EVENT_MODE_DISABLE_MASK 0x04
91 #define LTA_DISABLE BIT(5)
92 #define ACF_DISABLE BIT(4)
94 #define LTA_HALT_11 (BIT(0) | BIT(1))
96 #define ATI_ENABLED_MASK 0x80
98 #define IQS253_RESET BIT(5)
105 /* iqs263 registers and values */
106 #define DEV_INFO 0x00
107 #define SYST_FLAGS 0x01
108 #define CO_NATES 0x02
109 #define TOUCH_STAT 0x03
113 #define MULTIPLERS 0x07
114 #define COMPENSATION 0x08
115 #define PROX_SETTINGS 0x09
116 #define THRESHOLDS 0x0A
117 #define TIM_TARGETS 0x0B
118 #define GESTURE_TIME 0x0C
119 #define ACTIVE_CH 0x0D
121 #define PROX_THR 0x04
122 #define TOUCH_THR1 0x14
123 #define TOUCH_THR2 0x02
124 #define TOUCH_THR3 0x03
125 #define MOV_THR 0x04 /* 0x15 -->0x4 (Enable motion detection) */
127 #define HALT_TIME 0x30 /* 0xFF -> 0x64(2min) -> 0x30(55sec) --> 0x16 (30 sec) */
128 #define HALT_RESEED 0x00 /* RESEED CMD */
129 #define I2C_TIMEOUT 0x04
131 #define SYST_SET 0x80
132 #define CH0_MULT 0x1B
133 #define CH1_MULT 0x08
134 #define CH2_MULT 0x08
135 #define CH3_MULT 0x00
139 #define PROX_SET0 0x00
140 #define PROX_SET1 0x00
141 #define PROX_SET2 0x00
142 #define PROX_SET3 0x00
143 #define EVENT_MASK 0x00
146 #define TARGET_T 0x64 /* set the target of 1000, 0x80 */
147 #define TARGET_P 0x64
150 #define FLICK_TIM 0x00
151 #define FLICK_THR 0x07
153 #define IQS263_RESET 0x80
154 #define IQS263_EV_MASK 0x04
161 struct i2c_client *client;
162 const struct i2c_device_id *id;
168 struct regulator *vddhi;
171 struct workqueue_struct *sar_wq;
172 struct delayed_work sar_dw;
183 struct reg_val_pair {
188 struct reg_val_pair reg_val_map_iqs253[NUM_MODE][NUM_REG] = {
189 { /* init settings */
190 { ACTIVE_CHAN, PROXIMITY_ONLY},
191 { CH0_PTH, PROX_TH_CH0},
192 { CH1_PTH, PROX_TH_CH1},
193 { CH2_PTH, PROX_TH_CH2},
195 { CH1_ATI_BASE, CH1_ATI_TH},
196 { CH2_ATI_BASE, CH2_ATI_TH},
197 { PROX_SETTINGS3, 0x05},
200 { PROX_SETTINGS0, 0x57}, /* enable ATI and force auto ATI */
201 { ATI, 0x0}, /* wait for ATI to finish */
204 { PROX_SETTINGS1, 0x40},
205 { PROX_SETTINGS2, 0x57},
209 struct cmd_val_pair {
215 static u8 beforeinit_prox_settings[10] = { PROX_SET0 | 0x80, PROX_SET1 | 0x0,
216 PROX_SET2 , PROX_SET3 | 0x01,
218 static u8 active_ch[] = { ACT_CH0 | 0x05 };
219 static u8 set_thresholds[] = { PROX_THR, TOUCH_THR1, TOUCH_THR2, TOUCH_THR3,
220 MOV_THR, HALT_RESEED, HALT_TIME, I2C_TIMEOUT };
221 static u8 set_multipliers[] = { CH0_MULT, CH1_MULT, CH2_MULT, CH3_MULT,
223 static u8 set_tim_targets[] = { LP_TIME, TARGET_T, TARGET_P };
224 static u8 afterinit_prox_settings[] = { PROX_SET0 | 0x10, PROX_SET1 | 0x02,
225 PROX_SET2 | 0x04, PROX_SET3 | 0x05,
227 static u8 set_ati[] = {0 };
229 struct cmd_val_pair cmd_val_map_iqs263[] = {
230 /* iqs263 init sequence */
231 { PROX_SETTINGS, 5, beforeinit_prox_settings},
232 { ACTIVE_CH, 1, active_ch},
233 { THRESHOLDS, 8, set_thresholds},
234 { MULTIPLERS, 5, set_multipliers},
235 { TIM_TARGETS, 3, set_tim_targets},
236 { PROX_SETTINGS, 5, afterinit_prox_settings},
237 { ATI, 0x0, set_ati}, /* wait for ATI to finish */
240 static void iqs253_detect_comwindow(struct iqs253_chip *chip)
242 int gpio_count = 10000; /* wait for a maximum of 1 second */
243 while (gpio_get_value(chip->rdy_gpio) && gpio_count--)
244 usleep_range(100, 110);
247 static int iqs253_i2c_firsthand_shake(struct iqs253_chip *iqs253_chip)
249 int retry_count = 10;
252 pr_debug("%s :<-- HANDSHAKE START -->\n", __func__);
255 gpio_direction_output(iqs253_chip->rdy_gpio, 0);
256 usleep_range(12 * 1000, 12 * 1000);
257 /* put to tristate */
258 gpio_direction_input(iqs253_chip->rdy_gpio);
259 iqs253_detect_comwindow(iqs253_chip);
260 } while (gpio_get_value(iqs253_chip->rdy_gpio) && retry_count--);
263 ret = i2c_smbus_write_byte_data(iqs253_chip->client,
264 PROX_SETTINGS2 , 0x17);
267 usleep_range(1000, 1100);
269 pr_debug("%s :<-- HANDSHAKE DONE -->\n", __func__);
273 static int iqs253_i2c_read_byte(struct iqs253_chip *chip, int reg)
276 iqs253_detect_comwindow(chip);
277 ret = i2c_smbus_read_byte_data(chip->client, reg);
278 usleep_range(1000, 1100);
282 static int iqs253_i2c_write_byte(struct iqs253_chip *chip, int reg, int val)
286 iqs253_detect_comwindow(chip);
287 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
288 usleep_range(1000, 1100);
292 static void iqs253_wait_for_ati_finish(struct iqs253_chip *iqs253_chip)
296 usleep_range(10 * 1000, 10 * 1000);
297 ret = iqs253_i2c_read_byte(iqs253_chip, SYSFLAGS);
298 } while (ret & ATI_IN_PROGRESS);
301 /* must call holding lock */
302 static int iqs253_set(struct iqs253_chip *iqs253_chip)
305 struct reg_val_pair *reg_val_pair_map;
306 int modes[NUM_MODE] = {INIT_MODE, FORCE_ATI_MODE, POST_INIT_MODE};
308 if (iqs253_chip->version != 253)
311 for (j = 0; j < NUM_MODE; j++) {
313 if (modes[j] == MODE_NONE)
316 reg_val_pair_map = reg_val_map_iqs253[modes[j]];
318 for (i = 0; i < NUM_REG; i++) {
319 if (!reg_val_pair_map[i].reg &&
320 !reg_val_pair_map[i].val)
323 if (reg_val_pair_map[i].reg == ATI) {
324 iqs253_wait_for_ati_finish(iqs253_chip);
328 ret = iqs253_i2c_write_byte(iqs253_chip,
329 reg_val_pair_map[i].reg,
330 reg_val_pair_map[i].val);
332 dev_err(&iqs253_chip->client->dev,
333 "iqs253 write val:%x to reg:%x fail\n",
334 reg_val_pair_map[i].val,
335 reg_val_pair_map[i].reg);
343 static int iqs263_i2c_read_bytes(struct iqs253_chip *chip,
348 iqs253_detect_comwindow(chip);
349 ret = i2c_smbus_read_word_data(chip->client, cmd);
351 pr_err("iqs263 i2c read from %d failed", cmd);
352 usleep_range(1000, 1100);
356 static void iqs263_wait_for_ati_finish(struct iqs253_chip *iqs253_chip)
361 iqs253_detect_comwindow(iqs253_chip);
362 ret = iqs263_i2c_read_bytes(iqs253_chip, SYST_FLAGS);
363 } while (ret & ATI_IN_PROGRESS);
366 /* must call holding lock */
367 static int iqs263_i2c_write_bytes(struct iqs253_chip *chip,
368 struct cmd_val_pair *cmd_val)
372 iqs253_detect_comwindow(chip);
373 ret = i2c_smbus_write_i2c_block_data(chip->client, cmd_val->cmd,
374 cmd_val->len, cmd_val->vals);
376 pr_err("iqs263 i2c write to %d failed", cmd_val->cmd);
377 usleep_range(1000, 1100);
381 static int iqs263_i2c_read_word(struct iqs253_chip *chip, u8 cmd)
386 iqs253_detect_comwindow(chip);
387 ret = i2c_smbus_read_i2c_block_data(chip->client, cmd, 2, data_buffer);
389 pr_err("iqs263 i2c word read : %d failed", cmd);
391 ret = (data_buffer[0] & 0x0f) | ((data_buffer[1] << 8) & 0xF0);
396 /* must call holding lock */
397 static int iqs263_set(struct iqs253_chip *iqs253_chip)
400 struct cmd_val_pair *cmd_val_pair_map;
402 pr_debug("%s :<-- INIT START -->\n", __func__);
404 if (iqs253_chip->version != 263)
407 for (j = 0; j < ARRAY_SIZE(cmd_val_map_iqs263); j++) {
409 cmd_val_pair_map = &cmd_val_map_iqs263[j];
411 if (cmd_val_pair_map->cmd == ATI) {
412 iqs263_wait_for_ati_finish(iqs253_chip);
416 if (!cmd_val_pair_map->len)
419 ret = iqs263_i2c_write_bytes(iqs253_chip,
425 pr_debug("%s :<--INIT DONE -->\n", __func__);
429 static void iqs253_sar_proximity_detect_work(struct work_struct *ws)
432 struct iqs253_chip *chip;
434 chip = container_of(ws, struct iqs253_chip, sar_dw.work);
436 if (!chip->using_regulator) {
437 ret = regulator_enable(chip->vddhi);
440 chip->using_regulator = true;
443 if (chip->version == 253) {
444 ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
447 /* need to cross check thie */
449 ret = iqs253_set(chip);
454 /* if this does not work, try ret = iqs263_i2c_read_word, then ret = ret >> 8; then check ret & 0x80*/
455 ret = iqs263_i2c_read_bytes(chip, 0x03);
458 ret = iqs263_set(chip);
465 if (chip->version == 253) {
466 ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
469 ret = ret & PROXIMITY_ONLY;
471 * if both channel detect proximity => distance = 0;
472 * if one channel detects proximity => distance = 1;
473 * if no channel detects proximity => distance = 2;
475 chip->value = (ret == (PROX_CH1 | PROX_CH2)) ? 0 :
478 if (chip->value == -1)
480 /* provide input to SAR */
482 /* pr_info("%s :<-- undetect -->\n", __func__); */
483 gpio_direction_output(chip->sar_gpio, 1);
485 /* pr_info("%s :--> detect <--\n", __func__); */
486 gpio_direction_output(chip->sar_gpio, 0);
489 ret= iqs263_i2c_read_word(chip,0x03);
491 /* provide input to SAR 0x0c -> 0x4 */
492 if ((chip->value&0x04) !=0) {
493 /* pr_info("%s :<-- detect status: 0x%x -->\n", __func__ , chip->value); */
494 gpio_direction_output(chip->sar_gpio, 0);
496 /* pr_info("%s :--> Undetect Status: 0x%x <--\n", __func__, chip->value); */
497 gpio_direction_output(chip->sar_gpio, 1);
501 ret = regulator_disable(chip->vddhi);
504 chip->using_regulator = false;
507 queue_delayed_work(chip->sar_wq, &chip->sar_dw, msecs_to_jiffies(1000));
510 static ssize_t show_proximity_status(struct device * dev,
511 struct device_attribute *attr, char *buf)
513 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
514 struct iqs253_chip *chip = iio_priv(indio_dev);
515 if (chip->version == 253) {
517 return sprintf(buf, "1"); /* undetect */
518 return sprintf(buf, "0"); /* detect */
520 if ((chip->value & 0x04) != 0)
521 return sprintf(buf, "0"); /* detect */
522 return sprintf(buf, "1"); /* undetect */
524 return sprintf(buf, "invalid iqs sensor version..\n");
527 static IIO_CONST_ATTR(name, "iqs_sar_sensor");
528 static IIO_DEVICE_ATTR(proximity_status, S_IRUGO, show_proximity_status,
531 static struct attribute *iqs253_iio_attr[] = {
532 &iio_dev_attr_proximity_status.dev_attr.attr,
533 &iio_const_attr_name.dev_attr.attr,
537 static const struct attribute_group iqs253_iio_attr_grp = {
538 .attrs = iqs253_iio_attr,
541 static const struct iio_info iqs253_iio_info = {
542 .attrs = &iqs253_iio_attr_grp,
543 .driver_module = THIS_MODULE,
546 static int iqs253_probe(struct i2c_client *client,
547 const struct i2c_device_id *id)
550 struct iqs253_chip *iqs253_chip;
551 struct iio_dev *indio_dev;
552 int rdy_gpio = -1, wake_gpio = -1, sar_gpio = -1;
554 rdy_gpio = of_get_named_gpio(client->dev.of_node, "rdy-gpio", 0);
555 if (rdy_gpio == -EPROBE_DEFER)
556 return -EPROBE_DEFER;
558 if (!gpio_is_valid(rdy_gpio))
561 wake_gpio = of_get_named_gpio(client->dev.of_node, "wake-gpio", 0);
562 if (wake_gpio == -EPROBE_DEFER)
563 return -EPROBE_DEFER;
565 if (!gpio_is_valid(wake_gpio))
568 sar_gpio = of_get_named_gpio(client->dev.of_node, "sar-gpio", 0);
569 if (sar_gpio == -EPROBE_DEFER)
570 return -EPROBE_DEFER;
572 ret = gpio_request_one(sar_gpio, GPIOF_OUT_INIT_LOW, NULL);
576 indio_dev = iio_device_alloc(sizeof(*iqs253_chip));
580 i2c_set_clientdata(client, indio_dev);
581 iqs253_chip = iio_priv(indio_dev);
583 iqs253_chip->client = client;
584 iqs253_chip->id = id;
585 iqs253_chip->vddhi = devm_regulator_get(&client->dev, "vddhi");
586 if (IS_ERR(iqs253_chip->vddhi)) {
587 dev_err(&client->dev,
588 "devname:%s func:%s regulator vddhi not found\n",
590 goto err_regulator_get;
593 ret = gpio_request(rdy_gpio, "iqs253");
595 dev_err(&client->dev,
596 "devname:%s func:%s regulator vddhi not found\n",
598 goto err_gpio_request;
600 iqs253_chip->rdy_gpio = rdy_gpio;
601 iqs253_chip->wake_gpio = wake_gpio;
602 iqs253_chip->sar_gpio = sar_gpio;
604 ret = regulator_enable(iqs253_chip->vddhi);
606 dev_err(&client->dev,
607 "devname:%s func:%s regulator enable failed\n",
609 goto err_gpio_request;
612 * XXX: Do not set using_regulator = true here as
613 * it affects proximity detection
616 ret = iqs253_i2c_read_byte(iqs253_chip, 0);
617 if (ret == IQS253_PROD_ID) {
618 pr_info("%s :-->Chipid Detected: %d <--\n", __func__, iqs253_chip->version);
619 iqs253_i2c_firsthand_shake(iqs253_chip);
620 iqs253_chip->version = 253;
621 } else if (ret == IQS263_PROD_ID) {
622 pr_info("%s :-->Chipid Detected: %d <--\n", __func__, iqs253_chip->version);
623 iqs253_chip->version = 263;
624 ret = iqs263_set(iqs253_chip);
626 dev_err(&client->dev,
627 "devname:%s func:%s device not present\n",
629 goto err_gpio_request;
632 iqs253_chip->sar_wq = create_freezable_workqueue("iqs253_sar");
633 if (!iqs253_chip->sar_wq) {
634 dev_err(&iqs253_chip->client->dev, "unable to create work queue\n");
635 goto err_gpio_request;
638 INIT_DELAYED_WORK(&iqs253_chip->sar_dw,
639 iqs253_sar_proximity_detect_work);
641 queue_delayed_work(iqs253_chip->sar_wq, &iqs253_chip->sar_dw, 0);
643 indio_dev->info = &iqs253_iio_info;
644 indio_dev->dev.parent = &client->dev;
645 indio_dev->modes = INDIO_DIRECT_MODE;
646 ret = iio_device_register(indio_dev);
648 dev_err(&client->dev, "%s iio_device_register err\n", __func__);
649 goto err_iio_register;
652 dev_info(&client->dev, "devname:%s func:%s line:%d probe success\n",
653 id->name, __func__, __LINE__);
659 if (iqs253_chip->sar_wq)
660 destroy_workqueue(iqs253_chip->sar_wq);
662 iio_device_free(indio_dev);
664 dev_err(&client->dev, "devname:%s func:%s line:%d probe failed\n",
665 id->name, __func__, __LINE__);
669 static int iqs253_remove(struct i2c_client *client)
671 struct iio_dev *indio_dev = i2c_get_clientdata(client);
672 struct iqs253_chip *chip = iio_priv(indio_dev);
673 gpio_free(chip->rdy_gpio);
676 destroy_workqueue(chip->sar_wq);
677 iio_device_free(indio_dev);
681 static void iqs253_shutdown(struct i2c_client *client)
683 struct iio_dev *indio_dev = i2c_get_clientdata(client);
684 struct iqs253_chip *chip = iio_priv(indio_dev);
687 cancel_delayed_work_sync(&chip->sar_dw);
690 static const struct i2c_device_id iqs253_id[] = {
695 MODULE_DEVICE_TABLE(i2c, iqs253_id);
697 static const struct of_device_id iqs253_of_match[] = {
698 { .compatible = "azoteq,iqs253", },
701 MODULE_DEVICE_TABLE(of, iqs253_of_match);
703 static struct i2c_driver iqs253_driver = {
704 .class = I2C_CLASS_HWMON,
707 .owner = THIS_MODULE,
708 .of_match_table = of_match_ptr(iqs253_of_match),
710 .probe = iqs253_probe,
711 .remove = iqs253_remove,
712 .shutdown = iqs253_shutdown,
713 .id_table = iqs253_id,
716 module_i2c_driver(iqs253_driver);
718 MODULE_LICENSE("GPL");
719 MODULE_DESCRIPTION("IQS253 Driver");
720 MODULE_AUTHOR("Sri Krishna chowdary <schowdary@nvidia.com>");