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
64 #define STYLUS_ONLY PROX_CH0
65 #define PROXIMITY_ONLY (PROX_CH1 | PROX_CH2)
67 #define CH0_COMPENSATION 0x55
69 #define PROX_TH_CH0 0x03
70 #define PROX_TH_CH1 0x03
71 #define PROX_TH_CH2 0x03
73 #define DISABLE_DYCAL 0x00
75 #define CH0_ATI_TH 0xCC
76 #define CH1_ATI_TH 0x40
77 #define CH2_ATI_TH 0x40
79 #define EVENT_PROX_ONLY 0x01
81 #define PROX_SETTING_NORMAL 0x25
82 #define PROX_SETTING_STYLUS 0x26
84 #define AUTO_ATI_DISABLE BIT(7)
85 #define ATI_IN_PROGRESS 0x04
89 #define EVENT_MODE_DISABLE_MASK 0x04
90 #define LTA_DISABLE BIT(5)
91 #define ACF_DISABLE BIT(4)
93 #define LTA_HALT_11 (BIT(0) | BIT(1))
95 #define ATI_ENABLED_MASK 0x80
97 #define IQS253_RESET BIT(5)
104 struct i2c_client *client;
105 const struct i2c_device_id *id;
111 struct regulator *vddhi;
114 struct workqueue_struct *sar_wq;
115 struct delayed_work sar_dw;
126 struct reg_val_pair {
131 struct reg_val_pair reg_val_map_iqs253[NUM_MODE][NUM_REG] = {
132 { /* init settings */
133 { ACTIVE_CHAN, PROXIMITY_ONLY},
134 { CH0_PTH, PROX_TH_CH0},
135 { CH1_PTH, PROX_TH_CH1},
136 { CH2_PTH, PROX_TH_CH2},
138 { CH1_ATI_BASE, CH1_ATI_TH},
139 { CH2_ATI_BASE, CH2_ATI_TH},
140 { PROX_SETTINGS3, 0x05},
143 { PROX_SETTINGS0, 0x57}, /* enable ATI and force auto ATI */
144 { ATI, 0x0}, /* wait for ATI to finish */
147 { PROX_SETTINGS1, 0x40},
148 { PROX_SETTINGS2, 0x57},
152 static void iqs253_detect_comwindow(struct iqs253_chip *chip)
154 int gpio_count = 500;
155 while (gpio_get_value(chip->rdy_gpio) && gpio_count--)
156 usleep_range(100, 110);
159 static int iqs253_i2c_firsthand_shake(struct iqs253_chip *iqs253_chip)
161 int retry_count = 10;
164 pr_debug("%s :<-- HANDSHAKE START -->\n", __func__);
167 gpio_direction_output(iqs253_chip->rdy_gpio, 0);
168 usleep_range(12 * 1000, 12 * 1000);
169 /* put to tristate */
170 gpio_direction_input(iqs253_chip->rdy_gpio);
171 iqs253_detect_comwindow(iqs253_chip);
172 } while (gpio_get_value(iqs253_chip->rdy_gpio) && retry_count--);
175 ret = i2c_smbus_write_byte_data(iqs253_chip->client,
176 PROX_SETTINGS2 , 0x17);
179 usleep_range(1000, 1100);
181 pr_debug("%s :<-- HANDSHAKE DONE -->\n", __func__);
185 static int iqs253_i2c_read_byte(struct iqs253_chip *chip, int reg)
188 iqs253_detect_comwindow(chip);
189 ret = i2c_smbus_read_byte_data(chip->client, reg);
190 usleep_range(1000, 1100);
194 static int iqs253_i2c_write_byte(struct iqs253_chip *chip, int reg, int val)
198 iqs253_detect_comwindow(chip);
199 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
200 usleep_range(1000, 1100);
204 static void iqs253_wait_for_ati_finish(struct iqs253_chip *iqs253_chip)
208 usleep_range(10 * 1000, 10 * 1000);
209 ret = iqs253_i2c_read_byte(iqs253_chip, SYSFLAGS);
210 } while (ret & ATI_IN_PROGRESS);
213 /* must call holding lock */
214 static int iqs253_set(struct iqs253_chip *iqs253_chip)
217 struct reg_val_pair *reg_val_pair_map;
218 int modes[NUM_MODE] = {INIT_MODE, FORCE_ATI_MODE, POST_INIT_MODE};
220 for (j = 0; j < NUM_MODE; j++) {
222 if (modes[j] == MODE_NONE)
225 reg_val_pair_map = reg_val_map_iqs253[modes[j]];
227 for (i = 0; i < NUM_REG; i++) {
228 if (!reg_val_pair_map[i].reg &&
229 !reg_val_pair_map[i].val)
232 if (reg_val_pair_map[i].reg == ATI) {
233 iqs253_wait_for_ati_finish(iqs253_chip);
237 ret = iqs253_i2c_write_byte(iqs253_chip,
238 reg_val_pair_map[i].reg,
239 reg_val_pair_map[i].val);
241 dev_err(&iqs253_chip->client->dev,
242 "iqs253 write val:%x to reg:%x fail\n",
243 reg_val_pair_map[i].val,
244 reg_val_pair_map[i].reg);
252 static void iqs253_sar_proximity_detect_work(struct work_struct *ws)
255 struct iqs253_chip *chip;
257 chip = container_of(ws, struct iqs253_chip, sar_dw.work);
259 if (!chip->using_regulator) {
260 ret = regulator_enable(chip->vddhi);
263 chip->using_regulator = true;
266 ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
269 "iqs253: read proximity status fails in func:%s at line:%d\n",
274 if (ret & IQS253_RESET) {
275 ret = iqs253_set(chip);
280 ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
283 ret = ret & PROXIMITY_ONLY;
285 * if both channel detect proximity => distance = 0;
286 * if one channel detects proximity => distance = 1;
287 * if no channel detects proximity => distance = 2;
289 chip->value = (ret == (PROX_CH1 | PROX_CH2)) ? 0 :
292 if (chip->value == -1)
294 /* provide input to SAR */
296 gpio_direction_output(chip->sar_gpio, 1);
298 gpio_direction_output(chip->sar_gpio, 0);
300 ret = regulator_disable(chip->vddhi);
303 chip->using_regulator = false;
306 queue_delayed_work(chip->sar_wq, &chip->sar_dw, msecs_to_jiffies(1000));
309 static int iqs253_probe(struct i2c_client *client,
310 const struct i2c_device_id *id)
313 struct iqs253_chip *iqs253_chip;
314 struct iio_dev *indio_dev;
315 int rdy_gpio = -1, wake_gpio = -1, sar_gpio = -1;
317 rdy_gpio = of_get_named_gpio(client->dev.of_node, "rdy-gpio", 0);
318 if (rdy_gpio == -EPROBE_DEFER)
319 return -EPROBE_DEFER;
321 if (!gpio_is_valid(rdy_gpio))
324 wake_gpio = of_get_named_gpio(client->dev.of_node, "wake-gpio", 0);
325 if (wake_gpio == -EPROBE_DEFER)
326 return -EPROBE_DEFER;
328 if (!gpio_is_valid(wake_gpio))
331 sar_gpio = of_get_named_gpio(client->dev.of_node, "sar-gpio", 0);
332 if (sar_gpio == -EPROBE_DEFER)
333 return -EPROBE_DEFER;
335 ret = gpio_request_one(sar_gpio, GPIOF_OUT_INIT_LOW, NULL);
339 indio_dev = iio_device_alloc(sizeof(*iqs253_chip));
343 i2c_set_clientdata(client, indio_dev);
344 iqs253_chip = iio_priv(indio_dev);
346 iqs253_chip->client = client;
347 iqs253_chip->id = id;
348 iqs253_chip->mode = MODE_NONE;
349 iqs253_chip->vddhi = devm_regulator_get(&client->dev, "vddhi");
350 if (IS_ERR(iqs253_chip->vddhi)) {
351 dev_err(&client->dev,
352 "devname:%s func:%s regulator vddhi not found\n",
354 goto err_regulator_get;
357 ret = gpio_request(rdy_gpio, "iqs253");
359 dev_err(&client->dev,
360 "devname:%s func:%s regulator vddhi not found\n",
362 goto err_gpio_request;
364 iqs253_chip->rdy_gpio = rdy_gpio;
365 iqs253_chip->wake_gpio = wake_gpio;
366 iqs253_chip->sar_gpio = sar_gpio;
368 ret = regulator_enable(iqs253_chip->vddhi);
370 dev_err(&client->dev,
371 "devname:%s func:%s regulator enable failed\n",
373 goto err_gpio_request;
376 ret = iqs253_i2c_firsthand_shake(iqs253_chip);
378 dev_err(&client->dev,
379 "devname:%s func:%s first i2c handshake failed\n",
381 goto err_gpio_request;
384 ret = iqs253_i2c_read_byte(iqs253_chip, 0);
385 if (ret != IQS253_PROD_ID) {
386 dev_err(&client->dev,
387 "devname:%s func:%s device not present\n",
389 goto err_gpio_request;
392 iqs253_chip->sar_wq = create_freezable_workqueue("iqs253_sar");
393 if (!iqs253_chip->sar_wq) {
394 dev_err(&iqs253_chip->client->dev, "unable to create work queue\n");
395 goto err_gpio_request;
398 INIT_DELAYED_WORK(&iqs253_chip->sar_dw,
399 iqs253_sar_proximity_detect_work);
401 queue_delayed_work(iqs253_chip->sar_wq, &iqs253_chip->sar_dw, 0);
403 dev_info(&client->dev, "devname:%s func:%s line:%d probe success\n",
404 id->name, __func__, __LINE__);
409 if (iqs253_chip->sar_wq)
410 destroy_workqueue(iqs253_chip->sar_wq);
412 iio_device_free(indio_dev);
414 dev_err(&client->dev, "devname:%s func:%s line:%d probe failed\n",
415 id->name, __func__, __LINE__);
419 static int iqs253_remove(struct i2c_client *client)
421 struct iio_dev *indio_dev = i2c_get_clientdata(client);
422 struct iqs253_chip *chip = iio_priv(indio_dev);
423 gpio_free(chip->rdy_gpio);
426 destroy_workqueue(chip->sar_wq);
427 iio_device_free(indio_dev);
431 static void iqs253_shutdown(struct i2c_client *client)
433 struct iio_dev *indio_dev = i2c_get_clientdata(client);
434 struct iqs253_chip *chip = iio_priv(indio_dev);
437 cancel_delayed_work_sync(&chip->sar_dw);
440 static const struct i2c_device_id iqs253_id[] = {
445 MODULE_DEVICE_TABLE(i2c, iqs253_id);
447 static const struct of_device_id iqs253_of_match[] = {
448 { .compatible = "azoteq,iqs253", },
451 MODULE_DEVICE_TABLE(of, iqs253_of_match);
453 static struct i2c_driver iqs253_driver = {
454 .class = I2C_CLASS_HWMON,
457 .owner = THIS_MODULE,
458 .of_match_table = of_match_ptr(iqs253_of_match),
460 .probe = iqs253_probe,
461 .remove = iqs253_remove,
462 .shutdown = iqs253_shutdown,
463 .id_table = iqs253_id,
466 module_i2c_driver(iqs253_driver);
468 MODULE_LICENSE("GPL");
469 MODULE_DESCRIPTION("IQS253 Driver");
470 MODULE_AUTHOR("Sri Krishna chowdary <schowdary@nvidia.com>");