tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / input / touchscreen / imagis_30xxc / ist30xxc_sys.c
1 /*
2  *  Copyright (C) 2010,Imagis Technology Co. Ltd. All Rights Reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/delay.h>
21 #include <asm/unaligned.h>
22
23 #include <asm/io.h>
24 #include <linux/gpio.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/machine.h>
27
28 #include <linux/i2c/ist30xxc.h>
29 #include "ist30xxc_tracking.h"
30
31 /******************************************************************************
32  * Return value of Error
33  * EPERM  : 1 (Operation not permitted)
34  * ENOENT : 2 (No such file or directory)
35  * EIO    : 5 (I/O error)
36  * ENXIO  : 6 (No such device or address)
37  * EINVAL : 22 (Invalid argument)
38  *****************************************************************************/
39
40 int ist30xx_cmd_gesture(struct i2c_client *client, int value)
41 {
42         int ret = -EIO;
43
44         if (value > 3)
45                 return ret;
46
47         ret = ist30xx_write_cmd(client,
48                 IST30XX_HIB_CMD, (eHCOM_GESTURE_EN << 16) | (value & 0xFFFF));
49
50         return ret;
51 }
52
53 int ist30xx_cmd_start_scan(struct ist30xx_data *data)
54 {
55         int ret = ist30xx_write_cmd(data->client, IST30XX_HIB_CMD,
56                 (eHCOM_FW_START << 16) | (IST30XX_ENABLE & 0xFFFF));
57
58         ist30xx_tracking(TRACK_CMD_SCAN);
59
60         data->status.noise_mode = true;
61
62         return ret;
63 }
64
65 int ist30xx_cmd_calibrate(struct i2c_client *client)
66 {
67         int ret = ist30xx_write_cmd(client,
68                 IST30XX_HIB_CMD, (eHCOM_RUN_CAL_AUTO << 16));
69
70         ist30xx_tracking(TRACK_CMD_CALIB);
71
72         tsp_info("%s\n", __func__);
73
74         return ret;
75 }
76
77 int ist30xx_cmd_check_calib(struct i2c_client *client)
78 {
79         int ret = ist30xx_write_cmd(client, IST30XX_HIB_CMD,
80                 (eHCOM_RUN_CAL_PARAM << 16) | (IST30XX_ENABLE & 0xFFFF));
81
82         ist30xx_tracking(TRACK_CMD_CHECK_CALIB);
83
84         tsp_info("*** Check Calibration cmd ***\n");
85
86         return ret;
87 }
88
89 int ist30xx_cmd_hold(struct ist30xx_data *data, int enable)
90 {
91         int ret;
92         struct i2c_client *client = data->client;
93
94         if (!data->initialized && (data->status.update != 1))
95                 return 0;
96
97         ret = ist30xx_write_cmd(client,
98                 IST30XX_HIB_CMD, (eHCOM_FW_HOLD << 16) | (enable & 0xFFFF));
99         tsp_info("%s: FW HOLDE %s\n", __func__, enable ? "Enable" : "Disable");
100
101         if (enable)
102                 ist30xx_tracking(TRACK_CMD_ENTER_REG);
103         else
104                 ist30xx_tracking(TRACK_CMD_EXIT_REG);
105
106         return ret;
107 }
108
109 int ist30xx_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
110                          int msg_num, u8 *cmd_buf)
111 {
112         int ret = 0;
113         int idx = msg_num - 1;
114         int size = msgs[idx].len;
115         u8 *msg_buf = NULL;
116         u8 *pbuf = NULL;
117         int trans_size, max_size = 0;
118
119         if (msg_num == WRITE_CMD_MSG_LEN)
120                 max_size = I2C_MAX_WRITE_SIZE;
121         else if (msg_num == READ_CMD_MSG_LEN)
122                 max_size = I2C_MAX_READ_SIZE;
123
124         if (unlikely(max_size == 0)) {
125                 tsp_err("%s() : transaction size(%d)\n", __func__, max_size);
126                 return -EINVAL;
127         }
128
129         if (msg_num == WRITE_CMD_MSG_LEN) {
130                 msg_buf = kmalloc(max_size + IST30XX_ADDR_LEN, GFP_KERNEL);
131                 if (!msg_buf)
132                         return -ENOMEM;
133                 memcpy(msg_buf, cmd_buf, IST30XX_ADDR_LEN);
134                 pbuf = msgs[idx].buf;
135         }
136
137         while (size > 0) {
138                 trans_size = (size >= max_size ? max_size : size);
139
140                 msgs[idx].len = trans_size;
141                 if (msg_num == WRITE_CMD_MSG_LEN) {
142                         memcpy(&msg_buf[IST30XX_ADDR_LEN], pbuf, trans_size);
143                         msgs[idx].buf = msg_buf;
144                         msgs[idx].len += IST30XX_ADDR_LEN;
145                 }
146                 ret = i2c_transfer(adap, msgs, msg_num);
147                 if (unlikely(ret != msg_num)) {
148                         tsp_err("%s() : i2c_transfer failed(%d), num=%d\n",
149                                 __func__, ret, msg_num);
150                         break;
151                 }
152
153                 if (msg_num == WRITE_CMD_MSG_LEN)
154                         pbuf += trans_size;
155                 else
156                         msgs[idx].buf += trans_size;
157
158                 size -= trans_size;
159         }
160
161         if (msg_num == WRITE_CMD_MSG_LEN)
162                 kfree(msg_buf);
163
164         return ret;
165 }
166
167 int ist30xx_read_buf(struct i2c_client *client, u32 cmd, u32 *buf, u16 len)
168 {
169         int ret, i;
170         u32 le_reg = cpu_to_be32(cmd);
171
172         struct i2c_msg msg[READ_CMD_MSG_LEN] = {
173                 {
174                         .addr = client->addr,
175                         .flags = 0,
176                         .len = IST30XX_ADDR_LEN,
177                         .buf = (u8 *)&le_reg,
178                 },
179                 {
180                         .addr = client->addr,
181                         .flags = I2C_M_RD,
182                         .len = len * IST30XX_DATA_LEN,
183                         .buf = (u8 *)buf,
184                 },
185         };
186
187         ret = ist30xx_i2c_transfer(client->adapter, msg, READ_CMD_MSG_LEN, NULL);
188         if (unlikely(ret != READ_CMD_MSG_LEN))
189                 return -EIO;
190
191         for (i = 0; i < len; i++)
192                 buf[i] = cpu_to_be32(buf[i]);
193
194         return 0;
195 }
196
197 int ist30xx_write_buf(struct i2c_client *client, u32 cmd, u32 *buf, u16 len)
198 {
199         int i;
200         int ret;
201         struct i2c_msg msg;
202         u8 cmd_buf[IST30XX_ADDR_LEN];
203         u8 msg_buf[IST30XX_DATA_LEN * (len + 1)];
204
205         put_unaligned_be32(cmd, cmd_buf);
206
207         if (likely(len > 0)) {
208                 for (i = 0; i < len; i++)
209                         put_unaligned_be32(buf[i], msg_buf + (i * IST30XX_DATA_LEN));
210         } else {
211                 /* then add dummy data(4byte) */
212                 put_unaligned_be32(0, msg_buf);
213                 len = 1;
214         }
215
216         msg.addr = client->addr;
217         msg.flags = 0;
218         msg.len = len * IST30XX_DATA_LEN;
219         msg.buf = msg_buf;
220
221         ret = ist30xx_i2c_transfer(client->adapter, &msg, WRITE_CMD_MSG_LEN,
222                                    cmd_buf);
223         if (unlikely(ret != WRITE_CMD_MSG_LEN))
224                 return -EIO;
225
226         return 0;
227 }
228
229 int ist30xx_read_reg(struct i2c_client *client, u32 reg, u32 *buf)
230 {
231         int ret;
232         u32 le_reg = cpu_to_be32(reg);
233
234         struct i2c_msg msg[READ_CMD_MSG_LEN] = {
235                 {
236                         .addr = client->addr,
237                         .flags = 0,
238                         .len = IST30XX_ADDR_LEN,
239                         .buf = (u8 *)&le_reg,
240                 },
241                 {
242                         .addr = client->addr,
243                         .flags = I2C_M_RD,
244                         .len = IST30XX_DATA_LEN,
245                         .buf = (u8 *)buf,
246                 },
247         };
248
249         ret = i2c_transfer(client->adapter, msg, READ_CMD_MSG_LEN);
250         if (ret != READ_CMD_MSG_LEN) {
251                 tsp_err("%s: i2c failed (%d), cmd: %x\n", __func__, ret, reg);
252                 return -EIO;
253         }
254         *buf = cpu_to_be32(*buf);
255
256         return 0;
257 }
258
259 int ist30xx_read_cmd(struct ist30xx_data *data, u32 cmd, u32 *buf)
260 {
261         int ret;
262
263         ret = ist30xx_cmd_hold(data, 1);
264         if (unlikely(ret))
265                 return ret;
266
267         ist30xx_read_reg(data->client, IST30XX_DA_ADDR(cmd), buf);
268
269         ret = ist30xx_cmd_hold(data, 0);
270         if (unlikely(ret)) {
271                 ist30xx_reset(data, false);
272                 return ret;
273         }
274
275         return ret;
276 }
277
278 int ist30xx_write_cmd(struct i2c_client *client, u32 cmd, u32 val)
279 {
280         int ret;
281
282         struct i2c_msg msg;
283         u8 msg_buf[IST30XX_ADDR_LEN + IST30XX_DATA_LEN];
284
285         put_unaligned_be32(cmd, msg_buf);
286         put_unaligned_be32(val, msg_buf + IST30XX_ADDR_LEN);
287
288         msg.addr = client->addr;
289         msg.flags = 0;
290         msg.len = IST30XX_ADDR_LEN + IST30XX_DATA_LEN;
291         msg.buf = msg_buf;
292
293         ret = i2c_transfer(client->adapter, &msg, WRITE_CMD_MSG_LEN);
294         if (ret != WRITE_CMD_MSG_LEN) {
295                 tsp_err("%s: i2c failed (%d), cmd: %x(%x)\n", __func__, ret, cmd, val);
296                 return -EIO;
297         }
298
299         msleep(40);
300
301         return 0;
302 }
303
304 int ist30xx_burst_read(struct i2c_client *client, u32 addr,
305                         u32 *buf32, u16 len, bool bit_en)
306 {
307         int ret = 0;
308         int i;
309         u16 max_len = I2C_MAX_READ_SIZE / IST30XX_DATA_LEN;
310         u16 remain_len = len;
311
312         if (bit_en)
313                 addr = IST30XX_BA_ADDR(addr);
314
315         for (i = 0; i < len; i += max_len) {
316                 if (remain_len < max_len) max_len = remain_len;
317
318                 ret = ist30xx_read_buf(client, addr, buf32, max_len);
319                 if (unlikely(ret)) {
320                         tsp_err("Burst fail, addr: %x\n", __func__, addr);
321                         return ret;
322                 }
323
324                 addr += max_len * IST30XX_DATA_LEN;
325                 buf32 += max_len;
326                 remain_len -= max_len;
327         }
328
329         return 0;
330 }
331
332 int ist30xx_burst_write(struct i2c_client *client, u32 addr,
333                 u32 *buf32, u16 len)
334 {
335         int ret = 0;
336         int i;
337         u16 max_len = I2C_MAX_WRITE_SIZE / IST30XX_DATA_LEN;
338         u16 remain_len = len;
339
340         addr = IST30XX_BA_ADDR(addr);
341
342         for (i = 0; i < len; i += max_len) {
343                 if (remain_len < max_len) max_len = remain_len;
344
345                 ret = ist30xx_write_buf(client, addr, buf32, max_len);
346                 if (unlikely(ret)) {
347                         tsp_err("Burst fail, addr: %x\n", __func__, addr);
348                         return ret;
349                 }
350
351                 addr += max_len * IST30XX_DATA_LEN;
352                 buf32 += max_len;
353                 remain_len -= max_len;
354         }
355
356         return 0;
357 }
358
359 int ts_power_enable(struct ist30xx_data *data, int en)
360 {
361         int rc = 0;
362
363         /*Power from MFD internal LDO */
364         if(data->dt_data->tsp_vdd_name) {
365                 if(!data->dt_data->tsp_power) {
366                         data->dt_data->tsp_power = regulator_get(NULL, data->dt_data->tsp_vdd_name);
367
368                         if (IS_ERR(data->dt_data->tsp_power)) {
369                                 tsp_err("%s: could not get tsp_power, rc = %ld\n",
370                                         __func__,PTR_ERR(data->dt_data->tsp_power));
371                                 return -EINVAL;
372                         }
373                         rc = regulator_set_voltage(data->dt_data->tsp_power, 3000000, 3000000);
374                         if (rc)
375                                 tsp_err("%s: %s set_level failed (%d)\n", __func__, data->dt_data->tsp_vdd_name, rc);
376                 }
377         }
378
379         if (en) {
380                 if(data->dt_data->tsp_vdd_name) {
381                         rc = regulator_set_voltage(data->dt_data->tsp_power, 3000000, 3000000);
382                         if (rc)
383                                 tsp_err("%s: %s set_level failed (%d)\n", __func__, data->dt_data->tsp_vdd_name, rc);
384                         rc = regulator_enable(data->dt_data->tsp_power);
385                         if (rc) {
386                                 tsp_err("%s: %s enable failed (%d)\n", __func__, data->dt_data->tsp_vdd_name, rc);
387                                 return -EINVAL;
388                         }
389                 }
390         } else {
391                 if(data->dt_data->tsp_vdd_name) {
392                         rc = regulator_disable(data->dt_data->tsp_power);
393                         if (rc) {
394                                 tsp_err("%s: %s disable failed (%d)\n", __func__, data->dt_data->tsp_vdd_name, rc);
395                                 return -EINVAL;
396                         }
397                 }
398         }
399         if(!data->dt_data->tsp_vdd_name) {
400                 rc = gpio_direction_output(data->dt_data->touch_en_gpio, en);
401                 if (rc) {
402                         tsp_err("%s: unable to set_direction for TSP_EN [%d]\n",
403                                 __func__, data->dt_data->touch_en_gpio);
404                 }
405                 tsp_info("%s: touch_en: %d\n", __func__, gpio_get_value(data->dt_data->touch_en_gpio) ? "ON":"OFF");
406         } else
407                 tsp_info("%s: %s=[%s]\n", __func__, data->dt_data->tsp_vdd_name, regulator_is_enabled(data->dt_data->tsp_power) ? "ON":"OFF");
408
409         return rc;
410 }
411
412 int ist30xx_power_on(struct ist30xx_data *data, bool download)
413 {
414         int rc = 0;
415         if (data->status.power != 1) {
416                 tsp_info("%s\n", __func__);
417                 ist30xx_tracking(TRACK_PWR_ON);
418                 rc = ts_power_enable(data, 1);
419                 if (download)
420                         msleep(8);
421                 else
422                         msleep(60);
423
424         if (!rc) /*power is enabled successfully*/
425                 data->status.power = 1;
426         }
427
428         return rc;
429 }
430
431 int ist30xx_power_off(struct ist30xx_data *data)
432 {
433         int rc = 0;
434         if (data->status.power != 0) {
435                 tsp_info("%s()\n", __func__);
436                 ist30xx_tracking(TRACK_PWR_OFF);
437                 /* VDDIO disable */
438 /*              msleep(5);*/
439                 /* VDD disable */
440                 rc = ts_power_enable(data, 0);
441                 msleep(50);
442                 if (!rc) /*power is disabled successfully*/
443                         data->status.power = 0;
444                 data->status.noise_mode = false;
445         }
446
447         return 0;
448 }
449
450 int ist30xx_reset(struct ist30xx_data *data, bool download)
451 {
452         tsp_info("%s()\n", __func__);
453         ist30xx_power_off(data);
454         msleep(10);
455         ist30xx_power_on(data, download);
456
457         return 0;
458 }
459
460 int ist30xx_internal_suspend(struct ist30xx_data *data)
461 {
462 #if IST30XX_GESTURE
463         data->suspend = true;
464     if (data->gesture) {
465         ist30xx_reset(data, false);
466         ist30xx_cmd_gesture(data->client, 3);
467     } else {
468         ist30xx_power_off(data);
469     }
470 #else
471         ist30xx_power_off(data);
472 #endif
473         return 0;
474 }
475
476 int ist30xx_internal_resume(struct ist30xx_data *data)
477 {
478 #if IST30XX_GESTURE
479         data->suspend = false;
480     if (data->gesture)
481                 ist30xx_reset(data, false);
482     else
483         ist30xx_power_on(data, false);
484 #else
485         ist30xx_power_on(data, false);
486 #endif
487
488         return 0;
489 }
490
491 int ist30xx_init_system(struct ist30xx_data *data)
492 {
493         int ret;
494
495         // TODO : place additional code here.
496         ret = ist30xx_power_on(data, false);
497         if (ret) {
498                 tsp_err("%s: ist30xx_init_system failed (%d)\n", __func__, ret);
499                 return -EIO;
500         }
501
502 #if 0
503         ret = ist30xx_reset(data, false);
504         if (ret) {
505                 tsp_err("%s: ist30xx_reset failed (%d)\n", __func__, ret);
506                 return -EIO;
507         }
508 #endif
509
510         return 0;
511 }