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/gpio.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/machine.h>
28 #include <linux/i2c/ist30xxc.h>
29 #include "ist30xxc_tracking.h"
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 ist30xx_data *data, int enable)
92 struct i2c_client *client = data->client;
94 if (!data->initialized && (data->status.update != 1))
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");
102 ist30xx_tracking(TRACK_CMD_ENTER_REG);
104 ist30xx_tracking(TRACK_CMD_EXIT_REG);
109 int ist30xx_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
110 int msg_num, u8 *cmd_buf)
113 int idx = msg_num - 1;
114 int size = msgs[idx].len;
117 int trans_size, max_size = 0;
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;
124 if (unlikely(max_size == 0)) {
125 tsp_err("%s() : transaction size(%d)\n", __func__, max_size);
129 if (msg_num == WRITE_CMD_MSG_LEN) {
130 msg_buf = kmalloc(max_size + IST30XX_ADDR_LEN, GFP_KERNEL);
133 memcpy(msg_buf, cmd_buf, IST30XX_ADDR_LEN);
134 pbuf = msgs[idx].buf;
138 trans_size = (size >= max_size ? max_size : size);
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;
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);
153 if (msg_num == WRITE_CMD_MSG_LEN)
156 msgs[idx].buf += trans_size;
161 if (msg_num == WRITE_CMD_MSG_LEN)
167 int ist30xx_read_buf(struct i2c_client *client, u32 cmd, u32 *buf, u16 len)
170 u32 le_reg = cpu_to_be32(cmd);
172 struct i2c_msg msg[READ_CMD_MSG_LEN] = {
174 .addr = client->addr,
176 .len = IST30XX_ADDR_LEN,
177 .buf = (u8 *)&le_reg,
180 .addr = client->addr,
182 .len = len * IST30XX_DATA_LEN,
187 ret = ist30xx_i2c_transfer(client->adapter, msg, READ_CMD_MSG_LEN, NULL);
188 if (unlikely(ret != READ_CMD_MSG_LEN))
191 for (i = 0; i < len; i++)
192 buf[i] = cpu_to_be32(buf[i]);
197 int ist30xx_write_buf(struct i2c_client *client, u32 cmd, u32 *buf, u16 len)
202 u8 cmd_buf[IST30XX_ADDR_LEN];
203 u8 msg_buf[IST30XX_DATA_LEN * (len + 1)];
205 put_unaligned_be32(cmd, cmd_buf);
207 if (likely(len > 0)) {
208 for (i = 0; i < len; i++)
209 put_unaligned_be32(buf[i], msg_buf + (i * IST30XX_DATA_LEN));
211 /* then add dummy data(4byte) */
212 put_unaligned_be32(0, msg_buf);
216 msg.addr = client->addr;
218 msg.len = len * IST30XX_DATA_LEN;
221 ret = ist30xx_i2c_transfer(client->adapter, &msg, WRITE_CMD_MSG_LEN,
223 if (unlikely(ret != WRITE_CMD_MSG_LEN))
229 int ist30xx_read_reg(struct i2c_client *client, u32 reg, u32 *buf)
232 u32 le_reg = cpu_to_be32(reg);
234 struct i2c_msg msg[READ_CMD_MSG_LEN] = {
236 .addr = client->addr,
238 .len = IST30XX_ADDR_LEN,
239 .buf = (u8 *)&le_reg,
242 .addr = client->addr,
244 .len = IST30XX_DATA_LEN,
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);
254 *buf = cpu_to_be32(*buf);
259 int ist30xx_read_cmd(struct ist30xx_data *data, u32 cmd, u32 *buf)
263 ret = ist30xx_cmd_hold(data, 1);
267 ist30xx_read_reg(data->client, IST30XX_DA_ADDR(cmd), buf);
269 ret = ist30xx_cmd_hold(data, 0);
271 ist30xx_reset(data, false);
278 int ist30xx_write_cmd(struct i2c_client *client, u32 cmd, u32 val)
283 u8 msg_buf[IST30XX_ADDR_LEN + IST30XX_DATA_LEN];
285 put_unaligned_be32(cmd, msg_buf);
286 put_unaligned_be32(val, msg_buf + IST30XX_ADDR_LEN);
288 msg.addr = client->addr;
290 msg.len = IST30XX_ADDR_LEN + IST30XX_DATA_LEN;
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);
304 int ist30xx_burst_read(struct i2c_client *client, u32 addr,
305 u32 *buf32, u16 len, bool bit_en)
309 u16 max_len = I2C_MAX_READ_SIZE / IST30XX_DATA_LEN;
310 u16 remain_len = len;
313 addr = IST30XX_BA_ADDR(addr);
315 for (i = 0; i < len; i += max_len) {
316 if (remain_len < max_len) max_len = remain_len;
318 ret = ist30xx_read_buf(client, addr, buf32, max_len);
320 tsp_err("Burst fail, addr: %x\n", __func__, addr);
324 addr += max_len * IST30XX_DATA_LEN;
326 remain_len -= max_len;
332 int ist30xx_burst_write(struct i2c_client *client, u32 addr,
337 u16 max_len = I2C_MAX_WRITE_SIZE / IST30XX_DATA_LEN;
338 u16 remain_len = len;
340 addr = IST30XX_BA_ADDR(addr);
342 for (i = 0; i < len; i += max_len) {
343 if (remain_len < max_len) max_len = remain_len;
345 ret = ist30xx_write_buf(client, addr, buf32, max_len);
347 tsp_err("Burst fail, addr: %x\n", __func__, addr);
351 addr += max_len * IST30XX_DATA_LEN;
353 remain_len -= max_len;
359 int ts_power_enable(struct ist30xx_data *data, int en)
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);
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));
373 rc = regulator_set_voltage(data->dt_data->tsp_power, 3000000, 3000000);
375 tsp_err("%s: %s set_level failed (%d)\n", __func__, data->dt_data->tsp_vdd_name, rc);
380 if(data->dt_data->tsp_vdd_name) {
381 rc = regulator_set_voltage(data->dt_data->tsp_power, 3000000, 3000000);
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);
386 tsp_err("%s: %s enable failed (%d)\n", __func__, data->dt_data->tsp_vdd_name, rc);
391 if(data->dt_data->tsp_vdd_name) {
392 rc = regulator_disable(data->dt_data->tsp_power);
394 tsp_err("%s: %s disable failed (%d)\n", __func__, data->dt_data->tsp_vdd_name, rc);
399 if(!data->dt_data->tsp_vdd_name) {
400 rc = gpio_direction_output(data->dt_data->touch_en_gpio, en);
402 tsp_err("%s: unable to set_direction for TSP_EN [%d]\n",
403 __func__, data->dt_data->touch_en_gpio);
405 tsp_info("%s: touch_en: %d\n", __func__, gpio_get_value(data->dt_data->touch_en_gpio) ? "ON":"OFF");
407 tsp_info("%s: %s=[%s]\n", __func__, data->dt_data->tsp_vdd_name, regulator_is_enabled(data->dt_data->tsp_power) ? "ON":"OFF");
412 int ist30xx_power_on(struct ist30xx_data *data, bool download)
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);
424 if (!rc) /*power is enabled successfully*/
425 data->status.power = 1;
431 int ist30xx_power_off(struct ist30xx_data *data)
434 if (data->status.power != 0) {
435 tsp_info("%s()\n", __func__);
436 ist30xx_tracking(TRACK_PWR_OFF);
440 rc = ts_power_enable(data, 0);
442 if (!rc) /*power is disabled successfully*/
443 data->status.power = 0;
444 data->status.noise_mode = false;
450 int ist30xx_reset(struct ist30xx_data *data, bool download)
452 tsp_info("%s()\n", __func__);
453 ist30xx_power_off(data);
455 ist30xx_power_on(data, download);
460 int ist30xx_internal_suspend(struct ist30xx_data *data)
463 data->suspend = true;
465 ist30xx_reset(data, false);
466 ist30xx_cmd_gesture(data->client, 3);
468 ist30xx_power_off(data);
471 ist30xx_power_off(data);
476 int ist30xx_internal_resume(struct ist30xx_data *data)
479 data->suspend = false;
481 ist30xx_reset(data, false);
483 ist30xx_power_on(data, false);
485 ist30xx_power_on(data, false);
491 int ist30xx_init_system(struct ist30xx_data *data)
495 // TODO : place additional code here.
496 ret = ist30xx_power_on(data, false);
498 tsp_err("%s: ist30xx_init_system failed (%d)\n", __func__, ret);
503 ret = ist30xx_reset(data, false);
505 tsp_err("%s: ist30xx_reset failed (%d)\n", __func__, ret);