xref: /linux/drivers/input/touchscreen/cyttsp_i2c.c (revision 55d0969c451159cff86949b38c39171cab962069)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * cyttsp_i2c.c
4  * Cypress TrueTouch(TM) Standard Product (TTSP) I2C touchscreen driver.
5  * For use with Cypress Txx3xx parts.
6  * Supported parts include:
7  * CY8CTST341
8  * CY8CTMA340
9  *
10  * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
11  * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org>
12  *
13  * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
14  */
15 
16 #include "cyttsp_core.h"
17 
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 
21 #define CY_I2C_NAME		"cyttsp-i2c"
22 
23 #define CY_I2C_DATA_SIZE	128
24 
25 static int cyttsp_i2c_read_block_data(struct device *dev, u8 *xfer_buf,
26 				      u16 addr, u8 length, void *values)
27 {
28 	struct i2c_client *client = to_i2c_client(dev);
29 	u8 client_addr = client->addr | ((addr >> 8) & 0x1);
30 	u8 addr_lo = addr & 0xFF;
31 	struct i2c_msg msgs[] = {
32 		{
33 			.addr = client_addr,
34 			.flags = 0,
35 			.len = 1,
36 			.buf = &addr_lo,
37 		},
38 		{
39 			.addr = client_addr,
40 			.flags = I2C_M_RD,
41 			.len = length,
42 			.buf = values,
43 		},
44 	};
45 	int retval;
46 
47 	retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
48 	if (retval < 0)
49 		return retval;
50 
51 	return retval != ARRAY_SIZE(msgs) ? -EIO : 0;
52 }
53 
54 static int cyttsp_i2c_write_block_data(struct device *dev, u8 *xfer_buf,
55 				       u16 addr, u8 length, const void *values)
56 {
57 	struct i2c_client *client = to_i2c_client(dev);
58 	u8 client_addr = client->addr | ((addr >> 8) & 0x1);
59 	u8 addr_lo = addr & 0xFF;
60 	struct i2c_msg msgs[] = {
61 		{
62 			.addr = client_addr,
63 			.flags = 0,
64 			.len = length + 1,
65 			.buf = xfer_buf,
66 		},
67 	};
68 	int retval;
69 
70 	xfer_buf[0] = addr_lo;
71 	memcpy(&xfer_buf[1], values, length);
72 
73 	retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
74 	if (retval < 0)
75 		return retval;
76 
77 	return retval != ARRAY_SIZE(msgs) ? -EIO : 0;
78 }
79 
80 static const struct cyttsp_bus_ops cyttsp_i2c_bus_ops = {
81 	.bustype	= BUS_I2C,
82 	.write		= cyttsp_i2c_write_block_data,
83 	.read           = cyttsp_i2c_read_block_data,
84 };
85 
86 static int cyttsp_i2c_probe(struct i2c_client *client)
87 {
88 	struct cyttsp *ts;
89 
90 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
91 		dev_err(&client->dev, "I2C functionality not Supported\n");
92 		return -EIO;
93 	}
94 
95 	ts = cyttsp_probe(&cyttsp_i2c_bus_ops, &client->dev, client->irq,
96 			  CY_I2C_DATA_SIZE);
97 
98 	if (IS_ERR(ts))
99 		return PTR_ERR(ts);
100 
101 	i2c_set_clientdata(client, ts);
102 	return 0;
103 }
104 
105 static const struct i2c_device_id cyttsp_i2c_id[] = {
106 	{ CY_I2C_NAME },
107 	{ }
108 };
109 MODULE_DEVICE_TABLE(i2c, cyttsp_i2c_id);
110 
111 static const struct of_device_id cyttsp_of_i2c_match[] = {
112 	{ .compatible = "cypress,cy8ctma340", },
113 	{ .compatible = "cypress,cy8ctst341", },
114 	{ /* sentinel */ }
115 };
116 MODULE_DEVICE_TABLE(of, cyttsp_of_i2c_match);
117 
118 static struct i2c_driver cyttsp_i2c_driver = {
119 	.driver = {
120 		.name	= CY_I2C_NAME,
121 		.pm	= pm_sleep_ptr(&cyttsp_pm_ops),
122 		.of_match_table = cyttsp_of_i2c_match,
123 	},
124 	.probe		= cyttsp_i2c_probe,
125 	.id_table	= cyttsp_i2c_id,
126 };
127 
128 module_i2c_driver(cyttsp_i2c_driver);
129 
130 MODULE_LICENSE("GPL");
131 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard Product (TTSP) I2C driver");
132 MODULE_AUTHOR("Cypress");
133