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
127 #define HALT_TIME 0xFF
128 #define I2C_TIMEOUT 0x04
130 #define SYST_SET 0x80
131 #define CH0_MULT 0x1B
132 #define CH1_MULT 0x08
133 #define CH2_MULT 0x08
134 #define CH3_MULT 0x00
138 #define PROX_SET0 0x00
139 #define PROX_SET1 0x00
140 #define PROX_SET2 0x00
141 #define PROX_SET3 0x00
142 #define EVENT_MASK 0x00
145 #define TARGET_T 0x64 /* set the target of 1000, 0x80 */
146 #define TARGET_P 0x64
149 #define FLICK_TIM 0x00
150 #define FLICK_THR 0x07
152 #define IQS263_RESET 0x80
153 #define IQS263_EV_MASK 0x04
160 struct i2c_client *client;
161 const struct i2c_device_id *id;
167 struct regulator *vddhi;
170 struct workqueue_struct *sar_wq;
171 struct delayed_work sar_dw;
182 struct reg_val_pair {
187 struct reg_val_pair reg_val_map_iqs253[NUM_MODE][NUM_REG] = {
188 { /* init settings */
189 { ACTIVE_CHAN, PROXIMITY_ONLY},
190 { CH0_PTH, PROX_TH_CH0},
191 { CH1_PTH, PROX_TH_CH1},
192 { CH2_PTH, PROX_TH_CH2},
194 { CH1_ATI_BASE, CH1_ATI_TH},
195 { CH2_ATI_BASE, CH2_ATI_TH},
196 { PROX_SETTINGS3, 0x05},
199 { PROX_SETTINGS0, 0x57}, /* enable ATI and force auto ATI */
200 { ATI, 0x0}, /* wait for ATI to finish */
203 { PROX_SETTINGS1, 0x40},
204 { PROX_SETTINGS2, 0x57},
208 struct cmd_val_pair {
214 static u8 beforeinit_prox_settings[10] = { PROX_SET0 | 0x80, PROX_SET1 | 0x0,
215 PROX_SET2 , PROX_SET3 | 0x01,
217 static u8 active_ch[] = { ACT_CH0 | 0x05 };
218 static u8 set_thresholds[] = { PROX_THR, TOUCH_THR1, TOUCH_THR2, TOUCH_THR3,
219 MOV_THR, MOV_DEB, HALT_TIME, I2C_TIMEOUT };
220 static u8 set_multipliers[] = { CH0_MULT, CH1_MULT, CH2_MULT, CH3_MULT,
222 static u8 set_tim_targets[] = { LP_TIME, TARGET_T, TARGET_P };
223 static u8 afterinit_prox_settings[] = { PROX_SET0 | 0x10, PROX_SET1 | 0x02,
224 PROX_SET2 | 0x00, PROX_SET3 | 0x01,
226 static u8 set_ati[] = {0 };
228 struct cmd_val_pair cmd_val_map_iqs263[] = {
229 /* iqs263 init sequence */
230 { PROX_SETTINGS, 5, beforeinit_prox_settings},
231 { ACTIVE_CH, 1, active_ch},
232 { THRESHOLDS, 8, set_thresholds},
233 { MULTIPLERS, 5, set_multipliers},
234 { TIM_TARGETS, 3, set_tim_targets},
235 { PROX_SETTINGS, 5, afterinit_prox_settings},
236 { ATI, 0x0, set_ati}, /* wait for ATI to finish */
239 static void iqs253_detect_comwindow(struct iqs253_chip *chip)
241 int gpio_count = 10000; /* wait for a maximum of 1 second */
242 while (gpio_get_value(chip->rdy_gpio) && gpio_count--)
243 usleep_range(100, 110);
246 static int iqs253_i2c_firsthand_shake(struct iqs253_chip *iqs253_chip)
248 int retry_count = 10;
251 pr_debug("%s :<-- HANDSHAKE START -->\n", __func__);
254 gpio_direction_output(iqs253_chip->rdy_gpio, 0);
255 usleep_range(12 * 1000, 12 * 1000);
256 /* put to tristate */
257 gpio_direction_input(iqs253_chip->rdy_gpio);
258 iqs253_detect_comwindow(iqs253_chip);
259 } while (gpio_get_value(iqs253_chip->rdy_gpio) && retry_count--);
262 ret = i2c_smbus_write_byte_data(iqs253_chip->client,
263 PROX_SETTINGS2 , 0x17);
266 usleep_range(1000, 1100);
268 pr_debug("%s :<-- HANDSHAKE DONE -->\n", __func__);
272 static int iqs253_i2c_read_byte(struct iqs253_chip *chip, int reg)
275 iqs253_detect_comwindow(chip);
276 ret = i2c_smbus_read_byte_data(chip->client, reg);
277 usleep_range(1000, 1100);
281 static int iqs253_i2c_write_byte(struct iqs253_chip *chip, int reg, int val)
285 iqs253_detect_comwindow(chip);
286 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
287 usleep_range(1000, 1100);
291 static void iqs253_wait_for_ati_finish(struct iqs253_chip *iqs253_chip)
295 usleep_range(10 * 1000, 10 * 1000);
296 ret = iqs253_i2c_read_byte(iqs253_chip, SYSFLAGS);
297 } while (ret & ATI_IN_PROGRESS);
300 /* must call holding lock */
301 static int iqs253_set(struct iqs253_chip *iqs253_chip)
304 struct reg_val_pair *reg_val_pair_map;
305 int modes[NUM_MODE] = {INIT_MODE, FORCE_ATI_MODE, POST_INIT_MODE};
307 if (iqs253_chip->version != 253)
310 for (j = 0; j < NUM_MODE; j++) {
312 if (modes[j] == MODE_NONE)
315 reg_val_pair_map = reg_val_map_iqs253[modes[j]];
317 for (i = 0; i < NUM_REG; i++) {
318 if (!reg_val_pair_map[i].reg &&
319 !reg_val_pair_map[i].val)
322 if (reg_val_pair_map[i].reg == ATI) {
323 iqs253_wait_for_ati_finish(iqs253_chip);
327 ret = iqs253_i2c_write_byte(iqs253_chip,
328 reg_val_pair_map[i].reg,
329 reg_val_pair_map[i].val);
331 dev_err(&iqs253_chip->client->dev,
332 "iqs253 write val:%x to reg:%x fail\n",
333 reg_val_pair_map[i].val,
334 reg_val_pair_map[i].reg);
342 static int iqs263_i2c_read_bytes(struct iqs253_chip *chip,
347 iqs253_detect_comwindow(chip);
348 ret = i2c_smbus_read_word_data(chip->client, cmd);
350 pr_err("iqs263 i2c read from %d failed", cmd);
351 usleep_range(1000, 1100);
355 static void iqs263_wait_for_ati_finish(struct iqs253_chip *iqs253_chip)
360 iqs253_detect_comwindow(iqs253_chip);
361 ret = iqs263_i2c_read_bytes(iqs253_chip, SYST_FLAGS);
362 } while (ret & ATI_IN_PROGRESS);
365 /* must call holding lock */
366 static int iqs263_i2c_write_bytes(struct iqs253_chip *chip,
367 struct cmd_val_pair *cmd_val)
371 iqs253_detect_comwindow(chip);
372 ret = i2c_smbus_write_i2c_block_data(chip->client, cmd_val->cmd,
373 cmd_val->len, cmd_val->vals);
375 pr_err("iqs263 i2c write to %d failed", cmd_val->cmd);
376 usleep_range(1000, 1100);
380 static int iqs263_i2c_read_word(struct iqs253_chip *chip, u8 cmd)
385 iqs253_detect_comwindow(chip);
386 ret = i2c_smbus_read_i2c_block_data(chip->client, cmd, 2, data_buffer);
388 pr_err("iqs263 i2c word read : %d failed", cmd);
390 ret = (data_buffer[0] & 0x0f) | ((data_buffer[1] << 8) & 0xF0);
395 /* must call holding lock */
396 static int iqs263_set(struct iqs253_chip *iqs253_chip)
399 struct cmd_val_pair *cmd_val_pair_map;
401 pr_debug("%s :<-- INIT START -->\n", __func__);
403 if (iqs253_chip->version != 263)
406 for (j = 0; j < ARRAY_SIZE(cmd_val_map_iqs263); j++) {
408 cmd_val_pair_map = &cmd_val_map_iqs263[j];
410 if (cmd_val_pair_map->cmd == ATI) {
411 iqs263_wait_for_ati_finish(iqs253_chip);
415 if (!cmd_val_pair_map->len)
418 ret = iqs263_i2c_write_bytes(iqs253_chip,
424 pr_debug("%s :<--INIT DONE -->\n", __func__);
428 static void iqs253_sar_proximity_detect_work(struct work_struct *ws)
431 struct iqs253_chip *chip;
433 chip = container_of(ws, struct iqs253_chip, sar_dw.work);
435 if (!chip->using_regulator) {
436 ret = regulator_enable(chip->vddhi);
439 chip->using_regulator = true;
442 if (chip->version == 253) {
443 ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
446 /* need to cross check thie */
448 ret = iqs253_set(chip);
453 /* if this does not work, try ret = iqs263_i2c_read_word, then ret = ret >> 8; then check ret & 0x80*/
454 ret = iqs263_i2c_read_bytes(chip, 0x03);
457 ret = iqs263_set(chip);
464 if (chip->version == 253) {
465 ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
468 ret = ret & PROXIMITY_ONLY;
470 * if both channel detect proximity => distance = 0;
471 * if one channel detects proximity => distance = 1;
472 * if no channel detects proximity => distance = 2;
474 chip->value = (ret == (PROX_CH1 | PROX_CH2)) ? 0 :
477 if (chip->value == -1)
479 /* provide input to SAR */
481 pr_info("%s :<-- undetect -->\n", __func__);
482 gpio_direction_output(chip->sar_gpio, 1);
484 pr_info("%s :--> detect <--\n", __func__);
485 gpio_direction_output(chip->sar_gpio, 0);
488 ret= iqs263_i2c_read_word(chip,0x03);
490 /* provide input to SAR 0x0c -> 0x4 */
491 if ((chip->value&0x04) !=0) {
492 /* pr_info("%s :<-- detect status: 0x%x -->\n", __func__ , chip->value); */
493 gpio_direction_output(chip->sar_gpio, 0);
495 /* pr_info("%s :--> Undetect Status: 0x%x <--\n", __func__, chip->value); */
496 gpio_direction_output(chip->sar_gpio, 1);
500 ret = regulator_disable(chip->vddhi);
503 chip->using_regulator = false;
506 queue_delayed_work(chip->sar_wq, &chip->sar_dw, msecs_to_jiffies(1000));
509 static int iqs253_probe(struct i2c_client *client,
510 const struct i2c_device_id *id)
513 struct iqs253_chip *iqs253_chip;
514 struct iio_dev *indio_dev;
515 int rdy_gpio = -1, wake_gpio = -1, sar_gpio = -1;
517 rdy_gpio = of_get_named_gpio(client->dev.of_node, "rdy-gpio", 0);
518 if (rdy_gpio == -EPROBE_DEFER)
519 return -EPROBE_DEFER;
521 if (!gpio_is_valid(rdy_gpio))
524 wake_gpio = of_get_named_gpio(client->dev.of_node, "wake-gpio", 0);
525 if (wake_gpio == -EPROBE_DEFER)
526 return -EPROBE_DEFER;
528 if (!gpio_is_valid(wake_gpio))
531 sar_gpio = of_get_named_gpio(client->dev.of_node, "sar-gpio", 0);
532 if (sar_gpio == -EPROBE_DEFER)
533 return -EPROBE_DEFER;
535 ret = gpio_request_one(sar_gpio, GPIOF_OUT_INIT_LOW, NULL);
539 indio_dev = iio_device_alloc(sizeof(*iqs253_chip));
543 i2c_set_clientdata(client, indio_dev);
544 iqs253_chip = iio_priv(indio_dev);
546 iqs253_chip->client = client;
547 iqs253_chip->id = id;
548 iqs253_chip->vddhi = devm_regulator_get(&client->dev, "vddhi");
549 if (IS_ERR(iqs253_chip->vddhi)) {
550 dev_err(&client->dev,
551 "devname:%s func:%s regulator vddhi not found\n",
553 goto err_regulator_get;
556 ret = gpio_request(rdy_gpio, "iqs253");
558 dev_err(&client->dev,
559 "devname:%s func:%s regulator vddhi not found\n",
561 goto err_gpio_request;
563 iqs253_chip->rdy_gpio = rdy_gpio;
564 iqs253_chip->wake_gpio = wake_gpio;
565 iqs253_chip->sar_gpio = sar_gpio;
567 ret = regulator_enable(iqs253_chip->vddhi);
569 dev_err(&client->dev,
570 "devname:%s func:%s regulator enable failed\n",
572 goto err_gpio_request;
575 * XXX: Do not set using_regulator = true here as
576 * it affects proximity detection
579 ret = iqs253_i2c_read_byte(iqs253_chip, 0);
580 if (ret == IQS253_PROD_ID) {
581 pr_info("%s :-->Chipid Detected: %d <--\n", __func__, iqs253_chip->version);
582 iqs253_i2c_firsthand_shake(iqs253_chip);
583 iqs253_chip->version = 253;
584 } else if (ret == IQS263_PROD_ID) {
585 pr_info("%s :-->Chipid Detected: %d <--\n", __func__, iqs253_chip->version);
586 iqs253_chip->version = 263;
587 ret = iqs263_set(iqs253_chip);
589 dev_err(&client->dev,
590 "devname:%s func:%s device not present\n",
592 goto err_gpio_request;
595 iqs253_chip->sar_wq = create_freezable_workqueue("iqs253_sar");
596 if (!iqs253_chip->sar_wq) {
597 dev_err(&iqs253_chip->client->dev, "unable to create work queue\n");
598 goto err_gpio_request;
601 INIT_DELAYED_WORK(&iqs253_chip->sar_dw,
602 iqs253_sar_proximity_detect_work);
604 queue_delayed_work(iqs253_chip->sar_wq, &iqs253_chip->sar_dw, 0);
606 dev_info(&client->dev, "devname:%s func:%s line:%d probe success\n",
607 id->name, __func__, __LINE__);
612 if (iqs253_chip->sar_wq)
613 destroy_workqueue(iqs253_chip->sar_wq);
615 iio_device_free(indio_dev);
617 dev_err(&client->dev, "devname:%s func:%s line:%d probe failed\n",
618 id->name, __func__, __LINE__);
622 static int iqs253_remove(struct i2c_client *client)
624 struct iio_dev *indio_dev = i2c_get_clientdata(client);
625 struct iqs253_chip *chip = iio_priv(indio_dev);
626 gpio_free(chip->rdy_gpio);
629 destroy_workqueue(chip->sar_wq);
630 iio_device_free(indio_dev);
634 static void iqs253_shutdown(struct i2c_client *client)
636 struct iio_dev *indio_dev = i2c_get_clientdata(client);
637 struct iqs253_chip *chip = iio_priv(indio_dev);
640 cancel_delayed_work_sync(&chip->sar_dw);
643 static const struct i2c_device_id iqs253_id[] = {
648 MODULE_DEVICE_TABLE(i2c, iqs253_id);
650 static const struct of_device_id iqs253_of_match[] = {
651 { .compatible = "azoteq,iqs253", },
654 MODULE_DEVICE_TABLE(of, iqs253_of_match);
656 static struct i2c_driver iqs253_driver = {
657 .class = I2C_CLASS_HWMON,
660 .owner = THIS_MODULE,
661 .of_match_table = of_match_ptr(iqs253_of_match),
663 .probe = iqs253_probe,
664 .remove = iqs253_remove,
665 .shutdown = iqs253_shutdown,
666 .id_table = iqs253_id,
669 module_i2c_driver(iqs253_driver);
671 MODULE_LICENSE("GPL");
672 MODULE_DESCRIPTION("IQS253 Driver");
673 MODULE_AUTHOR("Sri Krishna chowdary <schowdary@nvidia.com>");