2 * Driver for Pixcir I2C touchscreen controllers.
4 * Copyright (C) 2010-2011 Pixcir, Inc.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/i2c.h>
25 #include <linux/input.h>
26 #include <linux/input/pixcir_ts.h>
28 struct pixcir_i2c_ts_data {
29 struct i2c_client *client;
30 struct input_dev *input;
31 const struct pixcir_ts_platform_data *chip;
35 static void pixcir_ts_poscheck(struct pixcir_i2c_ts_data *data)
37 struct pixcir_i2c_ts_data *tsdata = data;
38 u8 rdbuf[10], wrbuf[1] = { 0 };
42 ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
43 if (ret != sizeof(wrbuf)) {
44 dev_err(&tsdata->client->dev,
45 "%s: i2c_master_send failed(), ret=%d\n",
50 ret = i2c_master_recv(tsdata->client, rdbuf, sizeof(rdbuf));
51 if (ret != sizeof(rdbuf)) {
52 dev_err(&tsdata->client->dev,
53 "%s: i2c_master_recv failed(), ret=%d\n",
60 u16 posx1 = (rdbuf[3] << 8) | rdbuf[2];
61 u16 posy1 = (rdbuf[5] << 8) | rdbuf[4];
62 u16 posx2 = (rdbuf[7] << 8) | rdbuf[6];
63 u16 posy2 = (rdbuf[9] << 8) | rdbuf[8];
65 input_report_key(tsdata->input, BTN_TOUCH, 1);
66 input_report_abs(tsdata->input, ABS_X, posx1);
67 input_report_abs(tsdata->input, ABS_Y, posy1);
69 input_report_abs(tsdata->input, ABS_MT_POSITION_X, posx1);
70 input_report_abs(tsdata->input, ABS_MT_POSITION_Y, posy1);
71 input_mt_sync(tsdata->input);
74 input_report_abs(tsdata->input,
75 ABS_MT_POSITION_X, posx2);
76 input_report_abs(tsdata->input,
77 ABS_MT_POSITION_Y, posy2);
78 input_mt_sync(tsdata->input);
81 input_report_key(tsdata->input, BTN_TOUCH, 0);
84 input_sync(tsdata->input);
87 static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
89 struct pixcir_i2c_ts_data *tsdata = dev_id;
91 while (tsdata->running) {
92 pixcir_ts_poscheck(tsdata);
94 if (tsdata->chip->attb_read_val())
103 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
104 enum pixcir_power_mode mode)
106 struct device *dev = &ts->client->dev;
109 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
111 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
112 __func__, PIXCIR_REG_POWER_MODE, ret);
116 ret &= ~PIXCIR_POWER_MODE_MASK;
119 /* Always AUTO_IDLE */
120 ret |= PIXCIR_POWER_ALLOW_IDLE;
122 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
124 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
125 __func__, PIXCIR_REG_POWER_MODE, ret);
133 * Set the interrupt mode for the device i.e. ATTB line behaviour
135 * @polarity : 1 for active high, 0 for active low.
137 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
138 enum pixcir_int_mode mode, bool polarity)
140 struct device *dev = &ts->client->dev;
143 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
145 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
146 __func__, PIXCIR_REG_INT_MODE, ret);
150 ret &= ~PIXCIR_INT_MODE_MASK;
154 ret |= PIXCIR_INT_POL_HIGH;
156 ret &= ~PIXCIR_INT_POL_HIGH;
158 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
160 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
161 __func__, PIXCIR_REG_INT_MODE, ret);
169 * Enable/disable interrupt generation
171 static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
173 struct device *dev = &ts->client->dev;
176 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
178 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
179 __func__, PIXCIR_REG_INT_MODE, ret);
184 ret |= PIXCIR_INT_ENABLE;
186 ret &= ~PIXCIR_INT_ENABLE;
188 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
190 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
191 __func__, PIXCIR_REG_INT_MODE, ret);
198 static int pixcir_start(struct pixcir_i2c_ts_data *ts)
200 struct device *dev = &ts->client->dev;
203 /* LEVEL_TOUCH interrupt with active low polarity */
204 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
206 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
211 mb(); /* Update status before IRQ can fire */
213 /* enable interrupt generation */
214 error = pixcir_int_enable(ts, true);
216 dev_err(dev, "Failed to enable interrupt generation: %d\n",
224 static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
228 /* Disable interrupt generation */
229 error = pixcir_int_enable(ts, false);
231 dev_err(&ts->client->dev,
232 "Failed to disable interrupt generation: %d\n",
237 /* Exit ISR if running, no more report parsing */
239 mb(); /* update status before we synchronize irq */
241 /* Wait till running ISR is complete */
242 synchronize_irq(ts->client->irq);
247 static int pixcir_input_open(struct input_dev *dev)
249 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
251 return pixcir_start(ts);
254 static void pixcir_input_close(struct input_dev *dev)
256 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
261 #ifdef CONFIG_PM_SLEEP
262 static int pixcir_i2c_ts_suspend(struct device *dev)
264 struct i2c_client *client = to_i2c_client(dev);
266 if (device_may_wakeup(&client->dev))
267 enable_irq_wake(client->irq);
272 static int pixcir_i2c_ts_resume(struct device *dev)
274 struct i2c_client *client = to_i2c_client(dev);
276 if (device_may_wakeup(&client->dev))
277 disable_irq_wake(client->irq);
283 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
284 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
286 static int pixcir_i2c_ts_probe(struct i2c_client *client,
287 const struct i2c_device_id *id)
289 const struct pixcir_ts_platform_data *pdata =
290 dev_get_platdata(&client->dev);
291 struct device *dev = &client->dev;
292 struct pixcir_i2c_ts_data *tsdata;
293 struct input_dev *input;
297 dev_err(&client->dev, "platform data not defined\n");
301 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
305 input = devm_input_allocate_device(dev);
307 dev_err(dev, "Failed to allocate input device\n");
311 tsdata->client = client;
312 tsdata->input = input;
313 tsdata->chip = pdata;
315 input->name = client->name;
316 input->id.bustype = BUS_I2C;
317 input->open = pixcir_input_open;
318 input->close = pixcir_input_close;
319 input->dev.parent = &client->dev;
321 __set_bit(EV_KEY, input->evbit);
322 __set_bit(EV_ABS, input->evbit);
323 __set_bit(BTN_TOUCH, input->keybit);
324 input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0);
325 input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0);
326 input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
327 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
329 input_set_drvdata(input, tsdata);
331 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
332 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
333 client->name, tsdata);
335 dev_err(dev, "failed to request irq %d\n", client->irq);
339 /* Always be in IDLE mode to save power, device supports auto wake */
340 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
342 dev_err(dev, "Failed to set IDLE mode\n");
346 /* Stop device till opened */
347 error = pixcir_stop(tsdata);
351 error = input_register_device(input);
355 device_init_wakeup(&client->dev, 1);
360 static int pixcir_i2c_ts_remove(struct i2c_client *client)
362 device_init_wakeup(&client->dev, 0);
367 static const struct i2c_device_id pixcir_i2c_ts_id[] = {
371 MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
373 static struct i2c_driver pixcir_i2c_ts_driver = {
375 .owner = THIS_MODULE,
377 .pm = &pixcir_dev_pm_ops,
379 .probe = pixcir_i2c_ts_probe,
380 .remove = pixcir_i2c_ts_remove,
381 .id_table = pixcir_i2c_ts_id,
384 module_i2c_driver(pixcir_i2c_ts_driver);
386 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
387 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
388 MODULE_LICENSE("GPL");