Linux Audio

Check our new training course

Loading...
v3.15
  1/*
  2 * Touchscreen driver for Marvell 88PM860x
  3 *
  4 * Copyright (C) 2009 Marvell International Ltd.
  5 * 	Haojian Zhuang <haojian.zhuang@marvell.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License version 2 as
  9 * published by the Free Software Foundation.
 10 */
 11#include <linux/kernel.h>
 12#include <linux/module.h>
 13#include <linux/of.h>
 14#include <linux/platform_device.h>
 15#include <linux/i2c.h>
 16#include <linux/input.h>
 17#include <linux/mfd/88pm860x.h>
 18#include <linux/slab.h>
 19
 20#define MEAS_LEN		(8)
 21#define ACCURATE_BIT		(12)
 22
 23/* touch register */
 24#define MEAS_EN3		(0x52)
 25
 26#define MEAS_TSIX_1		(0x8D)
 27#define MEAS_TSIX_2		(0x8E)
 28#define MEAS_TSIY_1		(0x8F)
 29#define MEAS_TSIY_2		(0x90)
 30#define MEAS_TSIZ1_1		(0x91)
 31#define MEAS_TSIZ1_2		(0x92)
 32#define MEAS_TSIZ2_1		(0x93)
 33#define MEAS_TSIZ2_2		(0x94)
 34
 35/* bit definitions of touch */
 36#define MEAS_PD_EN		(1 << 3)
 37#define MEAS_TSIX_EN		(1 << 4)
 38#define MEAS_TSIY_EN		(1 << 5)
 39#define MEAS_TSIZ1_EN		(1 << 6)
 40#define MEAS_TSIZ2_EN		(1 << 7)
 41
 42struct pm860x_touch {
 43	struct input_dev *idev;
 44	struct i2c_client *i2c;
 45	struct pm860x_chip *chip;
 46	int irq;
 47	int res_x;		/* resistor of Xplate */
 48};
 49
 50static irqreturn_t pm860x_touch_handler(int irq, void *data)
 51{
 52	struct pm860x_touch *touch = data;
 53	struct pm860x_chip *chip = touch->chip;
 54	unsigned char buf[MEAS_LEN];
 55	int x, y, pen_down;
 56	int z1, z2, rt = 0;
 57	int ret;
 58
 59	ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
 60	if (ret < 0)
 61		goto out;
 62
 63	pen_down = buf[1] & (1 << 6);
 64	x = ((buf[0] & 0xFF) << 4) | (buf[1] & 0x0F);
 65	y = ((buf[2] & 0xFF) << 4) | (buf[3] & 0x0F);
 66	z1 = ((buf[4] & 0xFF) << 4) | (buf[5] & 0x0F);
 67	z2 = ((buf[6] & 0xFF) << 4) | (buf[7] & 0x0F);
 68
 69	if (pen_down) {
 70		if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
 71			rt = z2 / z1 - 1;
 72			rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
 73			dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
 74				z1, z2, rt);
 75		}
 76		input_report_abs(touch->idev, ABS_X, x);
 77		input_report_abs(touch->idev, ABS_Y, y);
 78		input_report_abs(touch->idev, ABS_PRESSURE, rt);
 79		input_report_key(touch->idev, BTN_TOUCH, 1);
 80		dev_dbg(chip->dev, "pen down at [%d, %d].\n", x, y);
 81	} else {
 82		input_report_abs(touch->idev, ABS_PRESSURE, 0);
 83		input_report_key(touch->idev, BTN_TOUCH, 0);
 84		dev_dbg(chip->dev, "pen release\n");
 85	}
 86	input_sync(touch->idev);
 87
 88out:
 89	return IRQ_HANDLED;
 90}
 91
 92static int pm860x_touch_open(struct input_dev *dev)
 93{
 94	struct pm860x_touch *touch = input_get_drvdata(dev);
 95	int data, ret;
 96
 97	data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
 98		| MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
 99	ret = pm860x_set_bits(touch->i2c, MEAS_EN3, data, data);
100	if (ret < 0)
101		goto out;
102	return 0;
103out:
104	return ret;
105}
106
107static void pm860x_touch_close(struct input_dev *dev)
108{
109	struct pm860x_touch *touch = input_get_drvdata(dev);
110	int data;
111
112	data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
113		| MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
114	pm860x_set_bits(touch->i2c, MEAS_EN3, data, 0);
115}
116
117#ifdef CONFIG_OF
118static int pm860x_touch_dt_init(struct platform_device *pdev,
119					  struct pm860x_chip *chip,
120					  int *res_x)
121{
122	struct device_node *np = pdev->dev.parent->of_node;
123	struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
124				 : chip->companion;
125	int data, n, ret;
126	if (!np)
127		return -ENODEV;
128	np = of_find_node_by_name(np, "touch");
129	if (!np) {
130		dev_err(&pdev->dev, "Can't find touch node\n");
131		return -EINVAL;
132	}
133	/* set GPADC MISC1 register */
134	data = 0;
135	if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-prebias", &n))
136		data |= (n << 1) & PM8607_GPADC_PREBIAS_MASK;
137	if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-slot-cycle", &n))
138		data |= (n << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
139	if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-off-scale", &n))
140		data |= (n << 5) & PM8607_GPADC_OFF_SCALE_MASK;
141	if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-sw-cal", &n))
142		data |= (n << 7) & PM8607_GPADC_SW_CAL_MASK;
143	if (data) {
144		ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
145		if (ret < 0)
146			return -EINVAL;
147	}
148	/* set tsi prebias time */
149	if (!of_property_read_u32(np, "marvell,88pm860x-tsi-prebias", &data)) {
150		ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
151		if (ret < 0)
152			return -EINVAL;
153	}
154	/* set prebias & prechg time of pen detect */
155	data = 0;
156	if (!of_property_read_u32(np, "marvell,88pm860x-pen-prebias", &n))
157		data |= n & PM8607_PD_PREBIAS_MASK;
158	if (!of_property_read_u32(np, "marvell,88pm860x-pen-prechg", &n))
159		data |= n & PM8607_PD_PRECHG_MASK;
160	if (data) {
161		ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
162		if (ret < 0)
163			return -EINVAL;
164	}
165	of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
166	return 0;
167}
168#else
169#define pm860x_touch_dt_init(x, y, z)	(-1)
170#endif
171
172static int pm860x_touch_probe(struct platform_device *pdev)
173{
174	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
175	struct pm860x_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
 
 
176	struct pm860x_touch *touch;
177	struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
178				 : chip->companion;
179	int irq, ret, res_x = 0, data = 0;
180
181	irq = platform_get_irq(pdev, 0);
182	if (irq < 0) {
183		dev_err(&pdev->dev, "No IRQ resource!\n");
184		return -EINVAL;
185	}
186
187	if (pm860x_touch_dt_init(pdev, chip, &res_x)) {
188		if (pdata) {
189			/* set GPADC MISC1 register */
190			data = 0;
191			data |= (pdata->gpadc_prebias << 1)
192				& PM8607_GPADC_PREBIAS_MASK;
193			data |= (pdata->slot_cycle << 3)
194				& PM8607_GPADC_SLOT_CYCLE_MASK;
195			data |= (pdata->off_scale << 5)
196				& PM8607_GPADC_OFF_SCALE_MASK;
197			data |= (pdata->sw_cal << 7)
198				& PM8607_GPADC_SW_CAL_MASK;
199			if (data) {
200				ret = pm860x_reg_write(i2c,
201					PM8607_GPADC_MISC1, data);
202				if (ret < 0)
203					return -EINVAL;
204			}
205			/* set tsi prebias time */
206			if (pdata->tsi_prebias) {
207				data = pdata->tsi_prebias;
208				ret = pm860x_reg_write(i2c,
209					PM8607_TSI_PREBIAS, data);
210				if (ret < 0)
211					return -EINVAL;
212			}
213			/* set prebias & prechg time of pen detect */
214			data = 0;
215			data |= pdata->pen_prebias
216				& PM8607_PD_PREBIAS_MASK;
217			data |= (pdata->pen_prechg << 5)
218				& PM8607_PD_PRECHG_MASK;
219			if (data) {
220				ret = pm860x_reg_write(i2c,
221					PM8607_PD_PREBIAS, data);
222				if (ret < 0)
223					return -EINVAL;
224			}
225			res_x = pdata->res_x;
226		} else {
227			dev_err(&pdev->dev, "failed to get platform data\n");
228			return -EINVAL;
229		}
230	}
231	/* enable GPADC */
232	ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1, PM8607_GPADC_EN,
233			      PM8607_GPADC_EN);
234	if (ret)
235		return ret;
236
237	touch = kzalloc(sizeof(struct pm860x_touch), GFP_KERNEL);
238	if (touch == NULL)
239		return -ENOMEM;
240	platform_set_drvdata(pdev, touch);
241
242	touch->idev = input_allocate_device();
243	if (touch->idev == NULL) {
244		dev_err(&pdev->dev, "Failed to allocate input device!\n");
245		ret = -ENOMEM;
246		goto out;
247	}
248
249	touch->idev->name = "88pm860x-touch";
250	touch->idev->phys = "88pm860x/input0";
251	touch->idev->id.bustype = BUS_I2C;
252	touch->idev->dev.parent = &pdev->dev;
253	touch->idev->open = pm860x_touch_open;
254	touch->idev->close = pm860x_touch_close;
255	touch->chip = chip;
256	touch->i2c = i2c;
257	touch->irq = irq;
258	touch->res_x = res_x;
259	input_set_drvdata(touch->idev, touch);
260
261	ret = request_threaded_irq(touch->irq, NULL, pm860x_touch_handler,
262				   IRQF_ONESHOT, "touch", touch);
263	if (ret < 0)
264		goto out_irq;
265
266	__set_bit(EV_ABS, touch->idev->evbit);
267	__set_bit(ABS_X, touch->idev->absbit);
268	__set_bit(ABS_Y, touch->idev->absbit);
269	__set_bit(ABS_PRESSURE, touch->idev->absbit);
270	__set_bit(EV_SYN, touch->idev->evbit);
271	__set_bit(EV_KEY, touch->idev->evbit);
272	__set_bit(BTN_TOUCH, touch->idev->keybit);
273
274	input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
275	input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
276	input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
277				0, 0);
278
279	ret = input_register_device(touch->idev);
280	if (ret < 0) {
281		dev_err(chip->dev, "Failed to register touch!\n");
282		goto out_rg;
283	}
284
285	platform_set_drvdata(pdev, touch);
286	return 0;
287out_rg:
288	free_irq(touch->irq, touch);
289out_irq:
290	input_free_device(touch->idev);
291out:
292	kfree(touch);
293	return ret;
294}
295
296static int pm860x_touch_remove(struct platform_device *pdev)
297{
298	struct pm860x_touch *touch = platform_get_drvdata(pdev);
299
300	input_unregister_device(touch->idev);
301	free_irq(touch->irq, touch);
 
302	kfree(touch);
303	return 0;
304}
305
306static struct platform_driver pm860x_touch_driver = {
307	.driver	= {
308		.name	= "88pm860x-touch",
309		.owner	= THIS_MODULE,
310	},
311	.probe	= pm860x_touch_probe,
312	.remove	= pm860x_touch_remove,
313};
314module_platform_driver(pm860x_touch_driver);
 
 
 
 
 
 
 
 
 
 
 
315
316MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
317MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
318MODULE_LICENSE("GPL");
319MODULE_ALIAS("platform:88pm860x-touch");
320
v3.1
  1/*
  2 * Touchscreen driver for Marvell 88PM860x
  3 *
  4 * Copyright (C) 2009 Marvell International Ltd.
  5 * 	Haojian Zhuang <haojian.zhuang@marvell.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License version 2 as
  9 * published by the Free Software Foundation.
 10 */
 11#include <linux/kernel.h>
 12#include <linux/module.h>
 
 13#include <linux/platform_device.h>
 14#include <linux/i2c.h>
 15#include <linux/input.h>
 16#include <linux/mfd/88pm860x.h>
 17#include <linux/slab.h>
 18
 19#define MEAS_LEN		(8)
 20#define ACCURATE_BIT		(12)
 21
 22/* touch register */
 23#define MEAS_EN3		(0x52)
 24
 25#define MEAS_TSIX_1		(0x8D)
 26#define MEAS_TSIX_2		(0x8E)
 27#define MEAS_TSIY_1		(0x8F)
 28#define MEAS_TSIY_2		(0x90)
 29#define MEAS_TSIZ1_1		(0x91)
 30#define MEAS_TSIZ1_2		(0x92)
 31#define MEAS_TSIZ2_1		(0x93)
 32#define MEAS_TSIZ2_2		(0x94)
 33
 34/* bit definitions of touch */
 35#define MEAS_PD_EN		(1 << 3)
 36#define MEAS_TSIX_EN		(1 << 4)
 37#define MEAS_TSIY_EN		(1 << 5)
 38#define MEAS_TSIZ1_EN		(1 << 6)
 39#define MEAS_TSIZ2_EN		(1 << 7)
 40
 41struct pm860x_touch {
 42	struct input_dev *idev;
 43	struct i2c_client *i2c;
 44	struct pm860x_chip *chip;
 45	int irq;
 46	int res_x;		/* resistor of Xplate */
 47};
 48
 49static irqreturn_t pm860x_touch_handler(int irq, void *data)
 50{
 51	struct pm860x_touch *touch = data;
 52	struct pm860x_chip *chip = touch->chip;
 53	unsigned char buf[MEAS_LEN];
 54	int x, y, pen_down;
 55	int z1, z2, rt = 0;
 56	int ret;
 57
 58	ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
 59	if (ret < 0)
 60		goto out;
 61
 62	pen_down = buf[1] & (1 << 6);
 63	x = ((buf[0] & 0xFF) << 4) | (buf[1] & 0x0F);
 64	y = ((buf[2] & 0xFF) << 4) | (buf[3] & 0x0F);
 65	z1 = ((buf[4] & 0xFF) << 4) | (buf[5] & 0x0F);
 66	z2 = ((buf[6] & 0xFF) << 4) | (buf[7] & 0x0F);
 67
 68	if (pen_down) {
 69		if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
 70			rt = z2 / z1 - 1;
 71			rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
 72			dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
 73				z1, z2, rt);
 74		}
 75		input_report_abs(touch->idev, ABS_X, x);
 76		input_report_abs(touch->idev, ABS_Y, y);
 77		input_report_abs(touch->idev, ABS_PRESSURE, rt);
 78		input_report_key(touch->idev, BTN_TOUCH, 1);
 79		dev_dbg(chip->dev, "pen down at [%d, %d].\n", x, y);
 80	} else {
 81		input_report_abs(touch->idev, ABS_PRESSURE, 0);
 82		input_report_key(touch->idev, BTN_TOUCH, 0);
 83		dev_dbg(chip->dev, "pen release\n");
 84	}
 85	input_sync(touch->idev);
 86
 87out:
 88	return IRQ_HANDLED;
 89}
 90
 91static int pm860x_touch_open(struct input_dev *dev)
 92{
 93	struct pm860x_touch *touch = input_get_drvdata(dev);
 94	int data, ret;
 95
 96	data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
 97		| MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
 98	ret = pm860x_set_bits(touch->i2c, MEAS_EN3, data, data);
 99	if (ret < 0)
100		goto out;
101	return 0;
102out:
103	return ret;
104}
105
106static void pm860x_touch_close(struct input_dev *dev)
107{
108	struct pm860x_touch *touch = input_get_drvdata(dev);
109	int data;
110
111	data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
112		| MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
113	pm860x_set_bits(touch->i2c, MEAS_EN3, data, 0);
114}
115
116static int __devinit pm860x_touch_probe(struct platform_device *pdev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117{
118	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
119	struct pm860x_platform_data *pm860x_pdata =		\
120				pdev->dev.parent->platform_data;
121	struct pm860x_touch_pdata *pdata = NULL;
122	struct pm860x_touch *touch;
123	int irq, ret;
 
 
124
125	irq = platform_get_irq(pdev, 0);
126	if (irq < 0) {
127		dev_err(&pdev->dev, "No IRQ resource!\n");
128		return -EINVAL;
129	}
130
131	if (!pm860x_pdata) {
132		dev_err(&pdev->dev, "platform data is missing\n");
133		return -EINVAL;
134	}
135
136	pdata = pm860x_pdata->touch;
137	if (!pdata) {
138		dev_err(&pdev->dev, "touchscreen data is missing\n");
139		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140	}
 
 
 
 
 
141
142	touch = kzalloc(sizeof(struct pm860x_touch), GFP_KERNEL);
143	if (touch == NULL)
144		return -ENOMEM;
145	dev_set_drvdata(&pdev->dev, touch);
146
147	touch->idev = input_allocate_device();
148	if (touch->idev == NULL) {
149		dev_err(&pdev->dev, "Failed to allocate input device!\n");
150		ret = -ENOMEM;
151		goto out;
152	}
153
154	touch->idev->name = "88pm860x-touch";
155	touch->idev->phys = "88pm860x/input0";
156	touch->idev->id.bustype = BUS_I2C;
157	touch->idev->dev.parent = &pdev->dev;
158	touch->idev->open = pm860x_touch_open;
159	touch->idev->close = pm860x_touch_close;
160	touch->chip = chip;
161	touch->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
162	touch->irq = irq + chip->irq_base;
163	touch->res_x = pdata->res_x;
164	input_set_drvdata(touch->idev, touch);
165
166	ret = request_threaded_irq(touch->irq, NULL, pm860x_touch_handler,
167				   IRQF_ONESHOT, "touch", touch);
168	if (ret < 0)
169		goto out_irq;
170
171	__set_bit(EV_ABS, touch->idev->evbit);
172	__set_bit(ABS_X, touch->idev->absbit);
173	__set_bit(ABS_Y, touch->idev->absbit);
174	__set_bit(ABS_PRESSURE, touch->idev->absbit);
175	__set_bit(EV_SYN, touch->idev->evbit);
176	__set_bit(EV_KEY, touch->idev->evbit);
177	__set_bit(BTN_TOUCH, touch->idev->keybit);
178
179	input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
180	input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
181	input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
182				0, 0);
183
184	ret = input_register_device(touch->idev);
185	if (ret < 0) {
186		dev_err(chip->dev, "Failed to register touch!\n");
187		goto out_rg;
188	}
189
190	platform_set_drvdata(pdev, touch);
191	return 0;
192out_rg:
193	free_irq(touch->irq, touch);
194out_irq:
195	input_free_device(touch->idev);
196out:
197	kfree(touch);
198	return ret;
199}
200
201static int __devexit pm860x_touch_remove(struct platform_device *pdev)
202{
203	struct pm860x_touch *touch = platform_get_drvdata(pdev);
204
205	input_unregister_device(touch->idev);
206	free_irq(touch->irq, touch);
207	platform_set_drvdata(pdev, NULL);
208	kfree(touch);
209	return 0;
210}
211
212static struct platform_driver pm860x_touch_driver = {
213	.driver	= {
214		.name	= "88pm860x-touch",
215		.owner	= THIS_MODULE,
216	},
217	.probe	= pm860x_touch_probe,
218	.remove	= __devexit_p(pm860x_touch_remove),
219};
220
221static int __init pm860x_touch_init(void)
222{
223	return platform_driver_register(&pm860x_touch_driver);
224}
225module_init(pm860x_touch_init);
226
227static void __exit pm860x_touch_exit(void)
228{
229	platform_driver_unregister(&pm860x_touch_driver);
230}
231module_exit(pm860x_touch_exit);
232
233MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
234MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
235MODULE_LICENSE("GPL");
236MODULE_ALIAS("platform:88pm860x-touch");
237