]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/staging/iio/light/iqs253.c
staging: iio: light: iqs253: update sensor code
[sojka/nv-tegra/linux-3.10.git] / drivers / staging / iio / light / iqs253.c
1 /*
2  * A iio driver for the capacitive sensor IQS253.
3  *
4  * IIO Light driver for monitoring proximity.
5  *
6  * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
7  *
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.
11  *
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
15  * more details.
16  */
17
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>
35
36 /* registers */
37 #define SYSFLAGS                0x10
38 #define PROX_STATUS             0x31
39 #define TOUCH_STATUS            0x35
40 #define TARGET                  0xC4
41 #define COMP0                   0xC5
42 #define CH0_ATI_BASE            0xC8
43 #define CH1_ATI_BASE            0xC9
44 #define CH2_ATI_BASE            0xCA
45 #define CH0_PTH                 0xCB
46 #define CH1_PTH                 0xCC
47 #define CH2_PTH                 0xCD
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
57
58 #define IQS253_PROD_ID          41
59
60 #define PROX_CH0                0x01
61 #define PROX_CH1                0x02
62 #define PROX_CH2                0x04
63
64 #define STYLUS_ONLY             PROX_CH0
65 #define PROXIMITY_ONLY          (PROX_CH1 | PROX_CH2)
66
67 #define CH0_COMPENSATION        0x55
68
69 #define PROX_TH_CH0             0x03
70 #define PROX_TH_CH1             0x03
71 #define PROX_TH_CH2             0x03
72
73 #define DISABLE_DYCAL           0x00
74
75 #define CH0_ATI_TH              0xCC
76 #define CH1_ATI_TH              0x40
77 #define CH2_ATI_TH              0x40
78
79 #define EVENT_PROX_ONLY         0x01
80
81 #define PROX_SETTING_NORMAL     0x25
82 #define PROX_SETTING_STYLUS     0x26
83
84 #define AUTO_ATI_DISABLE        BIT(7)
85 #define ATI_IN_PROGRESS         0x04
86
87 /* initial values */
88
89 #define EVENT_MODE_DISABLE_MASK 0x04
90 #define LTA_DISABLE             BIT(5)
91 #define ACF_DISABLE             BIT(4)
92 /* LTA always halt */
93 #define LTA_HALT_11             (BIT(0) | BIT(1))
94
95 #define ATI_ENABLED_MASK        0x80
96
97 #define IQS253_RESET            BIT(5)
98
99 #define ATI                     0xAF
100
101 #define NUM_REG 17
102
103 struct iqs253_chip {
104         struct i2c_client       *client;
105         const struct i2c_device_id      *id;
106         u32                     rdy_gpio;
107         u32                     wake_gpio;
108         u32                     sar_gpio;
109         u32                     mode;
110         u32                     value;
111         struct regulator        *vddhi;
112         u32                     using_regulator;
113
114         struct workqueue_struct *sar_wq;
115         struct delayed_work     sar_dw;
116 };
117
118 enum mode {
119         MODE_NONE = -1,
120         INIT_MODE,
121         FORCE_ATI_MODE,
122         POST_INIT_MODE,
123         NUM_MODE
124 };
125
126 struct reg_val_pair {
127         u8 reg;
128         u8 val;
129 };
130
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},
137                 { TARGET, 0xE6},
138                 { CH1_ATI_BASE, CH1_ATI_TH},
139                 { CH2_ATI_BASE, CH2_ATI_TH},
140                 { PROX_SETTINGS3, 0x05},
141         },
142         {       /* force on ATI */
143                 { PROX_SETTINGS0, 0x57}, /* enable ATI and force auto ATI */
144                 { ATI, 0x0}, /* wait for ATI to finish */
145         },
146         {
147                 { PROX_SETTINGS1, 0x40},
148                 { PROX_SETTINGS2, 0x57},
149         },
150 };
151
152 static void iqs253_detect_comwindow(struct iqs253_chip *chip)
153 {
154         int gpio_count = 500;
155         while (gpio_get_value(chip->rdy_gpio) && gpio_count--)
156                 usleep_range(100, 110);
157 }
158
159 static int iqs253_i2c_firsthand_shake(struct iqs253_chip *iqs253_chip)
160 {
161         int retry_count = 10;
162         int ret = 0;
163
164         pr_debug("%s :<-- HANDSHAKE START -->\n", __func__);
165
166         do {
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--);
173
174         /* got the window */
175         ret = i2c_smbus_write_byte_data(iqs253_chip->client,
176                                         PROX_SETTINGS2 , 0x17);
177         if (ret)
178                 return ret;
179         usleep_range(1000, 1100);
180
181         pr_debug("%s :<-- HANDSHAKE DONE -->\n", __func__);
182         return 0;
183 }
184
185 static int iqs253_i2c_read_byte(struct iqs253_chip *chip, int reg)
186 {
187         int ret = 0;
188         iqs253_detect_comwindow(chip);
189         ret = i2c_smbus_read_byte_data(chip->client, reg);
190         usleep_range(1000, 1100);
191         return ret;
192 }
193
194 static int iqs253_i2c_write_byte(struct iqs253_chip *chip, int reg, int val)
195 {
196         int ret = 0;
197
198         iqs253_detect_comwindow(chip);
199         ret = i2c_smbus_write_byte_data(chip->client, reg, val);
200         usleep_range(1000, 1100);
201         return ret;
202 }
203
204 static void iqs253_wait_for_ati_finish(struct iqs253_chip *iqs253_chip)
205 {
206         int ret;
207         do {
208                 usleep_range(10 * 1000, 10 * 1000);
209                 ret = iqs253_i2c_read_byte(iqs253_chip, SYSFLAGS);
210         } while (ret & ATI_IN_PROGRESS);
211 }
212
213 /* must call holding lock */
214 static int iqs253_set(struct iqs253_chip *iqs253_chip)
215 {
216         int ret = 0, i, j;
217         struct reg_val_pair *reg_val_pair_map;
218         int modes[NUM_MODE] = {INIT_MODE, FORCE_ATI_MODE, POST_INIT_MODE};
219
220         for (j = 0; j < NUM_MODE; j++) {
221
222                 if (modes[j] == MODE_NONE)
223                         break;
224
225                 reg_val_pair_map = reg_val_map_iqs253[modes[j]];
226
227                 for (i = 0; i < NUM_REG; i++) {
228                         if (!reg_val_pair_map[i].reg &&
229                             !reg_val_pair_map[i].val)
230                                 continue;
231
232                         if (reg_val_pair_map[i].reg == ATI) {
233                                 iqs253_wait_for_ati_finish(iqs253_chip);
234                                 continue;
235                         }
236
237                         ret = iqs253_i2c_write_byte(iqs253_chip,
238                                                 reg_val_pair_map[i].reg,
239                                                 reg_val_pair_map[i].val);
240                         if (ret) {
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);
245                                 return ret;
246                         }
247                 }
248         }
249         return 0;
250 }
251
252 static void iqs253_sar_proximity_detect_work(struct work_struct *ws)
253 {
254         int ret;
255         struct iqs253_chip *chip;
256
257         chip = container_of(ws, struct iqs253_chip, sar_dw.work);
258
259         if (!chip->using_regulator) {
260                 ret = regulator_enable(chip->vddhi);
261                 if (ret)
262                         goto finish;
263                 chip->using_regulator = true;
264         }
265
266         ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
267         if (ret < 0) {
268                 pr_err(
269                   "iqs253: read proximity status fails in func:%s at line:%d\n",
270                   __func__, __LINE__);
271                 return;
272         }
273
274         if (ret & IQS253_RESET) {
275                 ret = iqs253_set(chip);
276                 if (ret)
277                         goto finish;
278         }
279
280         ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
281         chip->value = -1;
282         if (ret >= 0) {
283                 ret = ret & PROXIMITY_ONLY;
284                 /*
285                  * if both channel detect proximity => distance = 0;
286                  * if one channel detects proximity => distance = 1;
287                  * if no channel detects proximity => distance = 2;
288                  */
289                 chip->value = (ret == (PROX_CH1 | PROX_CH2)) ? 0 :
290                                                         ret ? 1 : 2;
291         }
292         if (chip->value == -1)
293                 goto finish;
294         /* provide input to SAR */
295         if (chip->value/2)
296                 gpio_direction_output(chip->sar_gpio, 1);
297         else
298                 gpio_direction_output(chip->sar_gpio, 0);
299
300         ret = regulator_disable(chip->vddhi);
301         if (ret)
302                 goto finish;
303         chip->using_regulator = false;
304
305 finish:
306         queue_delayed_work(chip->sar_wq, &chip->sar_dw, msecs_to_jiffies(1000));
307 }
308
309 static int iqs253_probe(struct i2c_client *client,
310                         const struct i2c_device_id *id)
311 {
312         int ret;
313         struct iqs253_chip *iqs253_chip;
314         struct iio_dev *indio_dev;
315         int rdy_gpio = -1, wake_gpio = -1, sar_gpio = -1;
316
317         rdy_gpio = of_get_named_gpio(client->dev.of_node, "rdy-gpio", 0);
318         if (rdy_gpio == -EPROBE_DEFER)
319                 return -EPROBE_DEFER;
320
321         if (!gpio_is_valid(rdy_gpio))
322                 return -EINVAL;
323
324         wake_gpio = of_get_named_gpio(client->dev.of_node, "wake-gpio", 0);
325         if (wake_gpio == -EPROBE_DEFER)
326                 return -EPROBE_DEFER;
327
328         if (!gpio_is_valid(wake_gpio))
329                 return -EINVAL;
330
331         sar_gpio = of_get_named_gpio(client->dev.of_node, "sar-gpio", 0);
332         if (sar_gpio == -EPROBE_DEFER)
333                 return -EPROBE_DEFER;
334
335         ret = gpio_request_one(sar_gpio, GPIOF_OUT_INIT_LOW, NULL);
336         if (ret < 0)
337                 return -EINVAL;
338
339         indio_dev = iio_device_alloc(sizeof(*iqs253_chip));
340         if (!indio_dev)
341                 return -ENOMEM;
342
343         i2c_set_clientdata(client, indio_dev);
344         iqs253_chip = iio_priv(indio_dev);
345
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",
353                         id->name, __func__);
354                 goto err_regulator_get;
355         }
356
357         ret = gpio_request(rdy_gpio, "iqs253");
358         if (ret) {
359                         dev_err(&client->dev,
360                         "devname:%s func:%s regulator vddhi not found\n",
361                         id->name, __func__);
362                 goto err_gpio_request;
363         }
364         iqs253_chip->rdy_gpio = rdy_gpio;
365         iqs253_chip->wake_gpio = wake_gpio;
366         iqs253_chip->sar_gpio = sar_gpio;
367
368         ret = regulator_enable(iqs253_chip->vddhi);
369         if (ret) {
370                 dev_err(&client->dev,
371                         "devname:%s func:%s regulator enable failed\n",
372                         id->name, __func__);
373                 goto err_gpio_request;
374         }
375
376         ret = iqs253_i2c_firsthand_shake(iqs253_chip);
377         if (ret < 0) {
378                 dev_err(&client->dev,
379                         "devname:%s func:%s first i2c handshake failed\n",
380                         id->name, __func__);
381                 goto err_gpio_request;
382         }
383
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",
388                         id->name, __func__);
389                 goto err_gpio_request;
390         }
391
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;
396         }
397
398         INIT_DELAYED_WORK(&iqs253_chip->sar_dw,
399                                 iqs253_sar_proximity_detect_work);
400
401         queue_delayed_work(iqs253_chip->sar_wq, &iqs253_chip->sar_dw, 0);
402
403         dev_info(&client->dev, "devname:%s func:%s line:%d probe success\n",
404                         id->name, __func__, __LINE__);
405
406         return 0;
407
408 err_gpio_request:
409         if (iqs253_chip->sar_wq)
410                 destroy_workqueue(iqs253_chip->sar_wq);
411 err_regulator_get:
412         iio_device_free(indio_dev);
413
414         dev_err(&client->dev, "devname:%s func:%s line:%d probe failed\n",
415                         id->name, __func__, __LINE__);
416         return ret;
417 }
418
419 static int iqs253_remove(struct i2c_client *client)
420 {
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);
424
425         if (chip->sar_wq)
426                 destroy_workqueue(chip->sar_wq);
427         iio_device_free(indio_dev);
428         return 0;
429 }
430
431 static void iqs253_shutdown(struct i2c_client *client)
432 {
433         struct iio_dev *indio_dev = i2c_get_clientdata(client);
434         struct iqs253_chip *chip = iio_priv(indio_dev);
435
436         if (chip->sar_wq)
437                 cancel_delayed_work_sync(&chip->sar_dw);
438 }
439
440 static const struct i2c_device_id iqs253_id[] = {
441         {"iqs253", 0},
442         {}
443 };
444
445 MODULE_DEVICE_TABLE(i2c, iqs253_id);
446
447 static const struct of_device_id iqs253_of_match[] = {
448         { .compatible = "azoteq,iqs253", },
449         { },
450 };
451 MODULE_DEVICE_TABLE(of, iqs253_of_match);
452
453 static struct i2c_driver iqs253_driver = {
454         .class = I2C_CLASS_HWMON,
455         .driver = {
456                 .name = "iqs253",
457                 .owner = THIS_MODULE,
458                 .of_match_table = of_match_ptr(iqs253_of_match),
459         },
460         .probe = iqs253_probe,
461         .remove = iqs253_remove,
462         .shutdown = iqs253_shutdown,
463         .id_table = iqs253_id,
464 };
465
466 module_i2c_driver(iqs253_driver);
467
468 MODULE_LICENSE("GPL");
469 MODULE_DESCRIPTION("IQS253 Driver");
470 MODULE_AUTHOR("Sri Krishna chowdary <schowdary@nvidia.com>");