2 * Copyright (C) 2010,Imagis Technology Co. Ltd. All Rights Reserved.
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.
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.
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>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/machine.h>
28 #include "ist30xxc_tracking.h"
30 extern char *ist_vdd_name;
31 /******************************************************************************
32 * Return value of Error
33 * EPERM : 1 (Operation not permitted)
34 * ENOENT : 2 (No such file or directory)
36 * ENXIO : 6 (No such device or address)
37 * EINVAL : 22 (Invalid argument)
38 *****************************************************************************/
40 int ist30xx_cmd_gesture(struct i2c_client *client, int value)
47 ret = ist30xx_write_cmd(client,
48 IST30XX_HIB_CMD, (eHCOM_GESTURE_EN << 16) | (value & 0xFFFF));
53 int ist30xx_cmd_start_scan(struct ist30xx_data *data)
55 int ret = ist30xx_write_cmd(data->client, IST30XX_HIB_CMD,
56 (eHCOM_FW_START << 16) | (IST30XX_ENABLE & 0xFFFF));
58 ist30xx_tracking(TRACK_CMD_SCAN);
60 data->status.noise_mode = true;
65 int ist30xx_cmd_calibrate(struct i2c_client *client)
67 int ret = ist30xx_write_cmd(client,
68 IST30XX_HIB_CMD, (eHCOM_RUN_CAL_AUTO << 16));
70 ist30xx_tracking(TRACK_CMD_CALIB);
72 tsp_info("%s\n", __func__);
77 int ist30xx_cmd_check_calib(struct i2c_client *client)
79 int ret = ist30xx_write_cmd(client, IST30XX_HIB_CMD,
80 (eHCOM_RUN_CAL_PARAM << 16) | (IST30XX_ENABLE & 0xFFFF));
82 ist30xx_tracking(TRACK_CMD_CHECK_CALIB);
84 tsp_info("*** Check Calibration cmd ***\n");
89 int ist30xx_cmd_hold(struct i2c_client *client, int enable)
91 int ret = ist30xx_write_cmd(client,
92 IST30XX_HIB_CMD, (eHCOM_FW_HOLD << 16) | (enable & 0xFFFF));
97 ist30xx_tracking(TRACK_CMD_ENTER_REG);
99 ist30xx_tracking(TRACK_CMD_EXIT_REG);
104 int ist30xx_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
105 int msg_num, u8 *cmd_buf)
108 int idx = msg_num - 1;
109 int size = msgs[idx].len;
112 int trans_size, max_size = 0;
114 if (msg_num == WRITE_CMD_MSG_LEN)
115 max_size = I2C_MAX_WRITE_SIZE;
116 else if (msg_num == READ_CMD_MSG_LEN)
117 max_size = I2C_MAX_READ_SIZE;
119 if (unlikely(max_size == 0)) {
120 tsp_err("%s() : transaction size(%d)\n", __func__, max_size);
124 if (msg_num == WRITE_CMD_MSG_LEN) {
125 msg_buf = kmalloc(max_size + IST30XX_ADDR_LEN, GFP_KERNEL);
128 memcpy(msg_buf, cmd_buf, IST30XX_ADDR_LEN);
129 pbuf = msgs[idx].buf;
133 trans_size = (size >= max_size ? max_size : size);
135 msgs[idx].len = trans_size;
136 if (msg_num == WRITE_CMD_MSG_LEN) {
137 memcpy(&msg_buf[IST30XX_ADDR_LEN], pbuf, trans_size);
138 msgs[idx].buf = msg_buf;
139 msgs[idx].len += IST30XX_ADDR_LEN;
141 ret = i2c_transfer(adap, msgs, msg_num);
142 if (unlikely(ret != msg_num)) {
143 tsp_err("%s() : i2c_transfer failed(%d), num=%d\n",
144 __func__, ret, msg_num);
148 if (msg_num == WRITE_CMD_MSG_LEN)
151 msgs[idx].buf += trans_size;
156 if (msg_num == WRITE_CMD_MSG_LEN)
162 int ist30xx_read_buf(struct i2c_client *client, u32 cmd, u32 *buf, u16 len)
165 u32 le_reg = cpu_to_be32(cmd);
167 struct i2c_msg msg[READ_CMD_MSG_LEN] = {
169 .addr = client->addr,
171 .len = IST30XX_ADDR_LEN,
172 .buf = (u8 *)&le_reg,
175 .addr = client->addr,
177 .len = len * IST30XX_DATA_LEN,
182 ret = ist30xx_i2c_transfer(client->adapter, msg, READ_CMD_MSG_LEN, NULL);
183 if (unlikely(ret != READ_CMD_MSG_LEN))
186 for (i = 0; i < len; i++)
187 buf[i] = cpu_to_be32(buf[i]);
192 int ist30xx_write_buf(struct i2c_client *client, u32 cmd, u32 *buf, u16 len)
197 u8 cmd_buf[IST30XX_ADDR_LEN];
198 u8 msg_buf[IST30XX_DATA_LEN * (len + 1)];
200 put_unaligned_be32(cmd, cmd_buf);
202 if (likely(len > 0)) {
203 for (i = 0; i < len; i++)
204 put_unaligned_be32(buf[i], msg_buf + (i * IST30XX_DATA_LEN));
206 /* then add dummy data(4byte) */
207 put_unaligned_be32(0, msg_buf);
211 msg.addr = client->addr;
213 msg.len = len * IST30XX_DATA_LEN;
216 ret = ist30xx_i2c_transfer(client->adapter, &msg, WRITE_CMD_MSG_LEN,
218 if (unlikely(ret != WRITE_CMD_MSG_LEN))
224 int ist30xx_read_reg(struct i2c_client *client, u32 reg, u32 *buf)
227 u32 le_reg = cpu_to_be32(reg);
229 struct i2c_msg msg[READ_CMD_MSG_LEN] = {
231 .addr = client->addr,
233 .len = IST30XX_ADDR_LEN,
234 .buf = (u8 *)&le_reg,
237 .addr = client->addr,
239 .len = IST30XX_DATA_LEN,
244 ret = i2c_transfer(client->adapter, msg, READ_CMD_MSG_LEN);
245 if (ret != READ_CMD_MSG_LEN) {
246 tsp_err("%s: i2c failed (%d), cmd: %x\n", __func__, ret, reg);
249 *buf = cpu_to_be32(*buf);
254 int ist30xx_read_cmd(struct ist30xx_data *data, u32 cmd, u32 *buf)
258 ret = ist30xx_cmd_hold(data->client, 1);
262 ist30xx_read_reg(data->client, IST30XX_DA_ADDR(cmd), buf);
264 ret = ist30xx_cmd_hold(data->client, 0);
266 ist30xx_reset(data, false);
273 int ist30xx_write_cmd(struct i2c_client *client, u32 cmd, u32 val)
278 u8 msg_buf[IST30XX_ADDR_LEN + IST30XX_DATA_LEN];
280 put_unaligned_be32(cmd, msg_buf);
281 put_unaligned_be32(val, msg_buf + IST30XX_ADDR_LEN);
283 msg.addr = client->addr;
285 msg.len = IST30XX_ADDR_LEN + IST30XX_DATA_LEN;
288 ret = i2c_transfer(client->adapter, &msg, WRITE_CMD_MSG_LEN);
289 if (ret != WRITE_CMD_MSG_LEN) {
290 tsp_err("%s: i2c failed (%d), cmd: %x(%x)\n", __func__, ret, cmd, val);
299 int ist30xx_burst_read(struct i2c_client *client, u32 addr,
300 u32 *buf32, u16 len, bool bit_en)
304 u16 max_len = I2C_MAX_READ_SIZE / IST30XX_DATA_LEN;
305 u16 remain_len = len;
308 addr = IST30XX_BA_ADDR(addr);
310 for (i = 0; i < len; i += max_len) {
311 if (remain_len < max_len) max_len = remain_len;
313 ret = ist30xx_read_buf(client, addr, buf32, max_len);
315 tsp_err("Burst fail, addr: %x\n", __func__, addr);
319 addr += max_len * IST30XX_DATA_LEN;
321 remain_len -= max_len;
327 int ist30xx_burst_write(struct i2c_client *client, u32 addr,
332 u16 max_len = I2C_MAX_WRITE_SIZE / IST30XX_DATA_LEN;
333 u16 remain_len = len;
335 addr = IST30XX_BA_ADDR(addr);
337 for (i = 0; i < len; i += max_len) {
338 if (remain_len < max_len) max_len = remain_len;
340 ret = ist30xx_write_buf(client, addr, buf32, max_len);
342 tsp_err("Burst fail, addr: %x\n", __func__, addr);
346 addr += max_len * IST30XX_DATA_LEN;
348 remain_len -= max_len;
354 static struct regulator *touch_regulator;
355 static void ts_power_enable(struct ist30xx_data *data, int en)
357 struct i2c_client *client = data->client;
360 tsp_info("%s %s\n", __func__, (en) ? "on" : "off");
362 if (touch_regulator == NULL) {
363 touch_regulator = regulator_get(&client->dev, ist_vdd_name);
364 if (IS_ERR(touch_regulator)) {
365 ret = PTR_ERR(touch_regulator);
366 dev_err(&client->dev,
367 "Failed to get vdd regulator(%d)\n", ret);
371 ret = regulator_set_voltage(touch_regulator, 3000000, 3000000);
373 dev_err(&client->dev,
374 "Failed to set vdd regulator(%d)\n", ret);
375 regulator_put(touch_regulator);
376 touch_regulator = NULL;
382 if(data->status.power)
383 printk("%s:already enabled\n",__func__);
385 regulator_enable(touch_regulator);
388 if(data->status.power)
389 regulator_disable(touch_regulator);
391 printk("%s:already disabled\n",__func__);
395 int ist30xx_power_on(struct ist30xx_data *data, bool download)
397 if (data->status.power != 1) {
398 tsp_info("%s()\n", __func__);
399 ist30xx_tracking(TRACK_PWR_ON);
401 ts_power_enable(data, 1);
407 data->status.power = 1;
413 int ist30xx_power_off(struct ist30xx_data *data)
415 if (data->status.power != 0) {
416 tsp_info("%s()\n", __func__);
417 ist30xx_tracking(TRACK_PWR_OFF);
419 ts_power_enable(data, 0);
421 data->status.power = 0;
422 data->status.noise_mode = false;
428 int ist30xx_reset(struct ist30xx_data *data, bool download)
430 tsp_info("%s()\n", __func__);
431 ist30xx_power_off(data);
433 ist30xx_power_on(data, download);
438 int ist30xx_internal_suspend(struct ist30xx_data *data)
441 data->suspend = true;
443 ist30xx_reset(data, false);
444 ist30xx_cmd_gesture(data->client, 3);
446 ist30xx_power_off(data);
449 ist30xx_power_off(data);
454 int ist30xx_internal_resume(struct ist30xx_data *data)
457 data->suspend = false;
459 ist30xx_reset(data, false);
461 ist30xx_power_on(data, false);
463 ist30xx_power_on(data, false);
469 int ist30xx_init_system(struct ist30xx_data *data)
473 // TODO : place additional code here.
474 ret = ist30xx_power_on(data, false);
476 tsp_err("%s: ist30xx_init_system failed (%d)\n", __func__, ret);