cregit-Linux how code gets into the kernel

Release 4.7 drivers/input/touchscreen/pixcir_i2c_ts.c

/*
 * Driver for Pixcir I2C touchscreen controllers.
 *
 * Copyright (C) 2010-2011 Pixcir, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/delay.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/input/touchscreen.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
/*#include <linux/of.h>*/
#include <linux/of_device.h>
#include <linux/platform_data/pixcir_i2c_ts.h>


#define PIXCIR_MAX_SLOTS       5 
/* Max fingers supported by driver */


struct pixcir_i2c_ts_data {
	
struct i2c_client *client;
	
struct input_dev *input;
	
struct gpio_desc *gpio_attb;
	
struct gpio_desc *gpio_reset;
	
struct gpio_desc *gpio_enable;
	
struct gpio_desc *gpio_wake;
	
const struct pixcir_i2c_chip_data *chip;
	
int max_fingers;	/* Max fingers supported in this instance */
	
bool running;
};


struct pixcir_touch {
	
int x;
	
int y;
	
int id;
};


struct pixcir_report_data {
	
int num_touches;
	
struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
};


static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata, struct pixcir_report_data *report) { u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5]; u8 wrbuf[1] = { 0 }; u8 *bufptr; u8 touch; int ret, i; int readsize; const struct pixcir_i2c_chip_data *chip = tsdata->chip; memset(report, 0, sizeof(struct pixcir_report_data)); i = chip->has_hw_ids ? 1 : 0; readsize = 2 + tsdata->max_fingers * (4 + i); if (readsize > sizeof(rdbuf)) readsize = sizeof(rdbuf); ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf)); if (ret != sizeof(wrbuf)) { dev_err(&tsdata->client->dev, "%s: i2c_master_send failed(), ret=%d\n", __func__, ret); return; } ret = i2c_master_recv(tsdata->client, rdbuf, readsize); if (ret != readsize) { dev_err(&tsdata->client->dev, "%s: i2c_master_recv failed(), ret=%d\n", __func__, ret); return; } touch = rdbuf[0] & 0x7; if (touch > tsdata->max_fingers) touch = tsdata->max_fingers; report->num_touches = touch; bufptr = &rdbuf[2]; for (i = 0; i < touch; i++) { report->touches[i].x = (bufptr[1] << 8) | bufptr[0]; report->touches[i].y = (bufptr[3] << 8) | bufptr[2]; if (chip->has_hw_ids) { report->touches[i].id = bufptr[4]; bufptr = bufptr + 5; } else { bufptr = bufptr + 4; } } }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros19157.70%250.00%
jianchun bianjianchun bian13941.99%125.00%
frodo laifrodo lai10.30%125.00%
Total331100.00%4100.00%


static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts, struct pixcir_report_data *report) { struct input_mt_pos pos[PIXCIR_MAX_SLOTS]; int slots[PIXCIR_MAX_SLOTS]; struct pixcir_touch *touch; int n, i, slot; struct device *dev = &ts->client->dev; const struct pixcir_i2c_chip_data *chip = ts->chip; n = report->num_touches; if (n > PIXCIR_MAX_SLOTS) n = PIXCIR_MAX_SLOTS; if (!ts->chip->has_hw_ids) { for (i = 0; i < n; i++) { touch = &report->touches[i]; pos[i].x = touch->x; pos[i].y = touch->y; } input_mt_assign_slots(ts->input, slots, pos, n, 0); } for (i = 0; i < n; i++) { touch = &report->touches[i]; if (chip->has_hw_ids) { slot = input_mt_get_slot_by_key(ts->input, touch->id); if (slot < 0) { dev_dbg(dev, "no free slot for id 0x%x\n", touch->id); continue; } } else { slot = slots[i]; } input_mt_slot(ts->input, slot); input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true); input_event(ts->input, EV_ABS, ABS_MT_POSITION_X, touch->x); input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y, touch->y); dev_dbg(dev, "%d: slot %d, x %d, y %d\n", i, slot, touch->x, touch->y); } input_mt_sync_frame(ts->input); input_sync(ts->input); }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros24177.49%240.00%
jianchun bianjianchun bian6621.22%120.00%
henrik rydberghenrik rydberg20.64%120.00%
dmitry torokhovdmitry torokhov20.64%120.00%
Total311100.00%5100.00%


static irqreturn_t pixcir_ts_isr(int irq, void *dev_id) { struct pixcir_i2c_ts_data *tsdata = dev_id; struct pixcir_report_data report; while (tsdata->running) { /* parse packet */ pixcir_ts_parse(tsdata, &report); /* report it */ pixcir_ts_report(tsdata, &report); if (gpiod_get_value_cansleep(tsdata->gpio_attb)) { if (report.num_touches) { /* * Last report with no finger up? * Do it now then. */ input_mt_sync_frame(tsdata->input); input_sync(tsdata->input); } break; } msleep(20); } return IRQ_HANDLED; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros6873.12%350.00%
jianchun bianjianchun bian2324.73%116.67%
dmitry torokhovdmitry torokhov22.15%233.33%
Total93100.00%6100.00%


static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata) { if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) { gpiod_set_value_cansleep(tsdata->gpio_reset, 1); ndelay(100); /* datasheet section 1.2.3 says 80ns min. */ gpiod_set_value_cansleep(tsdata->gpio_reset, 0); /* wait for controller ready. 100ms guess. */ msleep(100); } }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros53100.00%1100.00%
Total53100.00%1100.00%


static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts, enum pixcir_power_mode mode) { struct device *dev = &ts->client->dev; int ret; if (mode == PIXCIR_POWER_ACTIVE || mode == PIXCIR_POWER_IDLE) { if (ts->gpio_wake) gpiod_set_value_cansleep(ts->gpio_wake, 1); } ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE); if (ret < 0) { dev_err(dev, "%s: can't read reg 0x%x : %d\n", __func__, PIXCIR_REG_POWER_MODE, ret); return ret; } ret &= ~PIXCIR_POWER_MODE_MASK; ret |= mode; /* Always AUTO_IDLE */ ret |= PIXCIR_POWER_ALLOW_IDLE; ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret); if (ret < 0) { dev_err(dev, "%s: can't write reg 0x%x : %d\n", __func__, PIXCIR_REG_POWER_MODE, ret); return ret; } if (mode == PIXCIR_POWER_HALT) { if (ts->gpio_wake) gpiod_set_value_cansleep(ts->gpio_wake, 0); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros11970.41%150.00%
sander verminsander vermin5029.59%150.00%
Total169100.00%2100.00%

/* * Set the interrupt mode for the device i.e. ATTB line behaviour * * @polarity : 1 for active high, 0 for active low. */
static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts, enum pixcir_int_mode mode, bool polarity) { struct device *dev = &ts->client->dev; int ret; ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE); if (ret < 0) { dev_err(dev, "%s: can't read reg 0x%x : %d\n", __func__, PIXCIR_REG_INT_MODE, ret); return ret; } ret &= ~PIXCIR_INT_MODE_MASK; ret |= mode; if (polarity) ret |= PIXCIR_INT_POL_HIGH; else ret &= ~PIXCIR_INT_POL_HIGH; ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret); if (ret < 0) { dev_err(dev, "%s: can't write reg 0x%x : %d\n", __func__, PIXCIR_REG_INT_MODE, ret); return ret; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros131100.00%1100.00%
Total131100.00%1100.00%

/* * Enable/disable interrupt generation */
static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable) { struct device *dev = &ts->client->dev; int ret; ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE); if (ret < 0) { dev_err(dev, "%s: can't read reg 0x%x : %d\n", __func__, PIXCIR_REG_INT_MODE, ret); return ret; } if (enable) ret |= PIXCIR_INT_ENABLE; else ret &= ~PIXCIR_INT_ENABLE; ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret); if (ret < 0) { dev_err(dev, "%s: can't write reg 0x%x : %d\n", __func__, PIXCIR_REG_INT_MODE, ret); return ret; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros118100.00%1100.00%
Total118100.00%1100.00%


static int pixcir_start(struct pixcir_i2c_ts_data *ts) { struct device *dev = &ts->client->dev; int error; if (ts->gpio_enable) { gpiod_set_value_cansleep(ts->gpio_enable, 1); msleep(100); } /* LEVEL_TOUCH interrupt with active low polarity */ error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0); if (error) { dev_err(dev, "Failed to set interrupt mode: %d\n", error); return error; } ts->running = true; mb(); /* Update status before IRQ can fire */ /* enable interrupt generation */ error = pixcir_int_enable(ts, true); if (error) { dev_err(dev, "Failed to enable interrupt generation: %d\n", error); return error; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros9075.63%133.33%
sander verminsander vermin2218.49%133.33%
jianchun bianjianchun bian75.88%133.33%
Total119100.00%3100.00%


static int pixcir_stop(struct pixcir_i2c_ts_data *ts) { int error; /* Disable interrupt generation */ error = pixcir_int_enable(ts, false); if (error) { dev_err(&ts->client->dev, "Failed to disable interrupt generation: %d\n", error); return error; } /* Exit ISR if running, no more report parsing */ ts->running = false; mb(); /* update status before we synchronize irq */ /* Wait till running ISR is complete */ synchronize_irq(ts->client->irq); if (ts->gpio_enable) gpiod_set_value_cansleep(ts->gpio_enable, 0); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros6474.42%133.33%
sander verminsander vermin1517.44%133.33%
jianchun bianjianchun bian78.14%133.33%
Total86100.00%3100.00%


static int pixcir_input_open(struct input_dev *dev) { struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev); return pixcir_start(ts); }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros2488.89%150.00%
jianchun bianjianchun bian311.11%150.00%
Total27100.00%2100.00%


static void pixcir_input_close(struct input_dev *dev) { struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev); pixcir_stop(ts); }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros26100.00%1100.00%
Total26100.00%1100.00%


static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client); struct input_dev *input = ts->input; int ret = 0; mutex_lock(&input->mutex); if (device_may_wakeup(&client->dev)) { if (!input->users) { ret = pixcir_start(ts); if (ret) { dev_err(dev, "Failed to start\n"); goto unlock; } } } else if (input->users) { ret = pixcir_stop(ts); } unlock: mutex_unlock(&input->mutex); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros9373.23%133.33%
jianchun bianjianchun bian3325.98%133.33%
jingoo hanjingoo han10.79%133.33%
Total127100.00%3100.00%


static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client); struct input_dev *input = ts->input; int ret = 0; mutex_lock(&input->mutex); if (device_may_wakeup(&client->dev)) { if (!input->users) { ret = pixcir_stop(ts); if (ret) { dev_err(dev, "Failed to stop\n"); goto unlock; } } } else if (input->users) { ret = pixcir_start(ts); } unlock: mutex_unlock(&input->mutex); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros9373.23%133.33%
jianchun bianjianchun bian3325.98%133.33%
jingoo hanjingoo han10.79%133.33%
Total127100.00%3100.00%

static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops, pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume); #ifdef CONFIG_OF static const struct of_device_id pixcir_of_match[];
static int pixcir_parse_dt(struct device *dev, struct pixcir_i2c_ts_data *tsdata) { const struct of_device_id *match; match = of_match_device(of_match_ptr(pixcir_of_match), dev); if (!match) return -EINVAL; tsdata->chip = (const struct pixcir_i2c_chip_data *)match->data; if (!tsdata->chip) return -EINVAL; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros6084.51%150.00%
dmitry torokhovdmitry torokhov1115.49%150.00%
Total71100.00%2100.00%

#else
static int pixcir_parse_dt(struct device *dev, struct pixcir_i2c_ts_data *tsdata) { return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros1470.00%150.00%
dmitry torokhovdmitry torokhov630.00%150.00%
Total20100.00%2100.00%

#endif
static int pixcir_i2c_ts_probe(struct i2c_client *client, const struct i2c_device_id *id) { const struct pixcir_ts_platform_data *pdata = dev_get_platdata(&client->dev); struct device *dev = &client->dev; struct pixcir_i2c_ts_data *tsdata; struct input_dev *input; int error; tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL); if (!tsdata) return -ENOMEM; if (pdata) { tsdata->chip = &pdata->chip; } else if (dev->of_node) { error = pixcir_parse_dt(dev, tsdata); if (error) return error; } else { dev_err(&client->dev, "platform data not defined\n"); return -EINVAL; } if (!tsdata->chip->max_fingers) { dev_err(dev, "Invalid max_fingers in chip data\n"); return -EINVAL; } input = devm_input_allocate_device(dev); if (!input) { dev_err(dev, "Failed to allocate input device\n"); return -ENOMEM; } tsdata->client = client; tsdata->input = input; input->name = client->name; input->id.bustype = BUS_I2C; input->open = pixcir_input_open; input->close = pixcir_input_close; input->dev.parent = &client->dev; if (pdata) { input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0); input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0); } else { input_set_capability(input, EV_ABS, ABS_MT_POSITION_X); input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y); touchscreen_parse_properties(input, true); if (!input_abs_get_max(input, ABS_MT_POSITION_X) || !input_abs_get_max(input, ABS_MT_POSITION_Y)) { dev_err(dev, "Touchscreen size is not specified\n"); return -EINVAL; } } tsdata->max_fingers = tsdata->chip->max_fingers; if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) { tsdata->max_fingers = PIXCIR_MAX_SLOTS; dev_info(dev, "Limiting maximum fingers to %d\n", tsdata->max_fingers); } error = input_mt_init_slots(input, tsdata->max_fingers, INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); if (error) { dev_err(dev, "Error initializing Multi-Touch slots\n"); return error; } input_set_drvdata(input, tsdata); tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN); if (IS_ERR(tsdata->gpio_attb)) { error = PTR_ERR(tsdata->gpio_attb); dev_err(dev, "Failed to request ATTB gpio: %d\n", error); return error; } tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); if (IS_ERR(tsdata->gpio_reset)) { error = PTR_ERR(tsdata->gpio_reset); dev_err(dev, "Failed to request RESET gpio: %d\n", error); return error; } tsdata->gpio_wake = devm_gpiod_get_optional(dev, "wake", GPIOD_OUT_HIGH); if (IS_ERR(tsdata->gpio_wake)) { error = PTR_ERR(tsdata->gpio_wake); if (error != -EPROBE_DEFER) dev_err(dev, "Failed to get wake gpio: %d\n", error); return error; } tsdata->gpio_enable = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH); if (IS_ERR(tsdata->gpio_enable)) { error = PTR_ERR(tsdata->gpio_enable); if (error != -EPROBE_DEFER) dev_err(dev, "Failed to get enable gpio: %d\n", error); return error; } if (tsdata->gpio_enable) msleep(100); error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr, IRQF_TRIGGER_FALLING | IRQF_ONESHOT, client->name, tsdata); if (error) { dev_err(dev, "failed to request irq %d\n", client->irq); return error; } pixcir_reset(tsdata); /* Always be in IDLE mode to save power, device supports auto wake */ error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE); if (error) { dev_err(dev, "Failed to set IDLE mode\n"); return error; } /* Stop device till opened */ error = pixcir_stop(tsdata); if (error) return error; error = input_register_device(input); if (error) return error; i2c_set_clientdata(client, tsdata); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros26436.31%857.14%
jianchun bianjianchun bian20327.92%17.14%
dmitry torokhovdmitry torokhov13919.12%214.29%
sander verminsander vermin11515.82%17.14%
jingoo hanjingoo han40.55%17.14%
lars-peter clausenlars-peter clausen20.28%17.14%
Total727100.00%14100.00%

static const struct i2c_device_id pixcir_i2c_ts_id[] = { { "pixcir_ts", 0 }, { "pixcir_tangoc", 0 }, { } }; MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id); #ifdef CONFIG_OF static const struct pixcir_i2c_chip_data pixcir_ts_data = { .max_fingers = 2, /* no hw id support */ }; static const struct pixcir_i2c_chip_data pixcir_tangoc_data = { .max_fingers = 5, .has_hw_ids = true, }; static const struct of_device_id pixcir_of_match[] = { { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data }, { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data }, { } }; MODULE_DEVICE_TABLE(of, pixcir_of_match); #endif static struct i2c_driver pixcir_i2c_ts_driver = { .driver = { .name = "pixcir_ts", .pm = &pixcir_dev_pm_ops, .of_match_table = of_match_ptr(pixcir_of_match), }, .probe = pixcir_i2c_ts_probe, .id_table = pixcir_i2c_ts_id, }; module_i2c_driver(pixcir_i2c_ts_driver); MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>"); MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver"); MODULE_LICENSE("GPL");

Overall Contributors

PersonTokensPropCommitsCommitProp
roger quadrosroger quadros181463.54%840.00%
jianchun bianjianchun bian64022.42%15.00%
sander verminsander vermin2127.43%15.00%
dmitry torokhovdmitry torokhov1786.23%525.00%
jingoo hanjingoo han60.21%210.00%
henrik rydberghenrik rydberg20.07%15.00%
lars-peter clausenlars-peter clausen20.07%15.00%
frodo laifrodo lai10.04%15.00%
Total2855100.00%20100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}