xref: /openbmc/linux/drivers/input/touchscreen/cyttsp_i2c_common.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*97fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29664877eSFerruh Yigit /*
39664877eSFerruh Yigit  * cyttsp_i2c_common.c
49664877eSFerruh Yigit  * Cypress TrueTouch(TM) Standard Product (TTSP) I2C touchscreen driver.
59664877eSFerruh Yigit  * For use with Cypress Txx3xx and Txx4xx parts.
69664877eSFerruh Yigit  * Supported parts include:
79664877eSFerruh Yigit  * CY8CTST341
89664877eSFerruh Yigit  * CY8CTMA340
99664877eSFerruh Yigit  * TMA4XX
109664877eSFerruh Yigit  * TMA1036
119664877eSFerruh Yigit  *
129664877eSFerruh Yigit  * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
139664877eSFerruh Yigit  * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org>
149664877eSFerruh Yigit  *
159664877eSFerruh Yigit  * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com>
169664877eSFerruh Yigit  */
179664877eSFerruh Yigit 
189664877eSFerruh Yigit #include <linux/device.h>
199664877eSFerruh Yigit #include <linux/export.h>
209664877eSFerruh Yigit #include <linux/i2c.h>
219664877eSFerruh Yigit #include <linux/module.h>
229664877eSFerruh Yigit #include <linux/types.h>
239664877eSFerruh Yigit 
249222d806SRashika Kheria #include "cyttsp4_core.h"
259222d806SRashika Kheria 
cyttsp_i2c_read_block_data(struct device * dev,u8 * xfer_buf,u16 addr,u8 length,void * values)269664877eSFerruh Yigit int cyttsp_i2c_read_block_data(struct device *dev, u8 *xfer_buf,
2762f548d0SFerruh Yigit 				      u16 addr, u8 length, void *values)
289664877eSFerruh Yigit {
299664877eSFerruh Yigit 	struct i2c_client *client = to_i2c_client(dev);
3062f548d0SFerruh Yigit 	u8 client_addr = client->addr | ((addr >> 8) & 0x1);
3162f548d0SFerruh Yigit 	u8 addr_lo = addr & 0xFF;
329664877eSFerruh Yigit 	struct i2c_msg msgs[] = {
339664877eSFerruh Yigit 		{
3462f548d0SFerruh Yigit 			.addr = client_addr,
359664877eSFerruh Yigit 			.flags = 0,
369664877eSFerruh Yigit 			.len = 1,
3762f548d0SFerruh Yigit 			.buf = &addr_lo,
389664877eSFerruh Yigit 		},
399664877eSFerruh Yigit 		{
4062f548d0SFerruh Yigit 			.addr = client_addr,
419664877eSFerruh Yigit 			.flags = I2C_M_RD,
429664877eSFerruh Yigit 			.len = length,
439664877eSFerruh Yigit 			.buf = values,
449664877eSFerruh Yigit 		},
459664877eSFerruh Yigit 	};
469664877eSFerruh Yigit 	int retval;
479664877eSFerruh Yigit 
489664877eSFerruh Yigit 	retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
499664877eSFerruh Yigit 	if (retval < 0)
509664877eSFerruh Yigit 		return retval;
519664877eSFerruh Yigit 
529664877eSFerruh Yigit 	return retval != ARRAY_SIZE(msgs) ? -EIO : 0;
539664877eSFerruh Yigit }
549664877eSFerruh Yigit EXPORT_SYMBOL_GPL(cyttsp_i2c_read_block_data);
559664877eSFerruh Yigit 
cyttsp_i2c_write_block_data(struct device * dev,u8 * xfer_buf,u16 addr,u8 length,const void * values)569664877eSFerruh Yigit int cyttsp_i2c_write_block_data(struct device *dev, u8 *xfer_buf,
5762f548d0SFerruh Yigit 				       u16 addr, u8 length, const void *values)
589664877eSFerruh Yigit {
599664877eSFerruh Yigit 	struct i2c_client *client = to_i2c_client(dev);
6062f548d0SFerruh Yigit 	u8 client_addr = client->addr | ((addr >> 8) & 0x1);
6162f548d0SFerruh Yigit 	u8 addr_lo = addr & 0xFF;
6262f548d0SFerruh Yigit 	struct i2c_msg msgs[] = {
6362f548d0SFerruh Yigit 		{
6462f548d0SFerruh Yigit 			.addr = client_addr,
6562f548d0SFerruh Yigit 			.flags = 0,
6662f548d0SFerruh Yigit 			.len = length + 1,
6762f548d0SFerruh Yigit 			.buf = xfer_buf,
6862f548d0SFerruh Yigit 		},
6962f548d0SFerruh Yigit 	};
709664877eSFerruh Yigit 	int retval;
719664877eSFerruh Yigit 
7262f548d0SFerruh Yigit 	xfer_buf[0] = addr_lo;
739664877eSFerruh Yigit 	memcpy(&xfer_buf[1], values, length);
749664877eSFerruh Yigit 
7562f548d0SFerruh Yigit 	retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
7662f548d0SFerruh Yigit 	if (retval < 0)
7762f548d0SFerruh Yigit 		return retval;
789664877eSFerruh Yigit 
7962f548d0SFerruh Yigit 	return retval != ARRAY_SIZE(msgs) ? -EIO : 0;
809664877eSFerruh Yigit }
819664877eSFerruh Yigit EXPORT_SYMBOL_GPL(cyttsp_i2c_write_block_data);
829664877eSFerruh Yigit 
839664877eSFerruh Yigit 
849664877eSFerruh Yigit MODULE_LICENSE("GPL");
859664877eSFerruh Yigit MODULE_AUTHOR("Cypress");
86