123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- #include "pt_regs.h"
- #include <linux/i2c.h>
- #include <linux/version.h>
- #define PT_I2C_DATA_SIZE (2 * 256)
- #define PT_I2C_NAME "pt_i2c_adapter"
- static int pt_i2c_read_default(struct device *dev, void *buf, int size)
- {
- struct i2c_client *client = to_i2c_client(dev);
- int rc;
- int read_size = size;
- if (!buf || !size || size > PT_I2C_DATA_SIZE)
- return -EINVAL;
- rc = i2c_master_recv(client, buf, read_size);
- return (rc < 0) ? rc : rc != read_size ? -EIO : 0;
- }
- static int pt_i2c_read_default_nosize(struct device *dev, u8 *buf, u32 max)
- {
- struct i2c_client *client = to_i2c_client(dev);
- struct i2c_msg msgs[2];
- u8 msg_count = 1;
- int rc;
- u32 size;
- if (!buf)
- return -EINVAL;
- msgs[0].addr = client->addr;
- msgs[0].flags = (client->flags & I2C_M_TEN) | I2C_M_RD;
- msgs[0].len = 2;
- msgs[0].buf = buf;
- rc = i2c_transfer(client->adapter, msgs, msg_count);
- if (rc < 0 || rc != msg_count)
- return (rc < 0) ? rc : -EIO;
- size = get_unaligned_le16(&buf[0]);
- if (!size || size == 2 || size >= PT_PIP_1P7_EMPTY_BUF)
-
- return 0;
- if (size > max)
- return -EINVAL;
- rc = i2c_master_recv(client, buf, size);
- return (rc < 0) ? rc : rc != (int)size ? -EIO : 0;
- }
- static int pt_i2c_write_read_specific(struct device *dev, u16 write_len,
- u8 *write_buf, u8 *read_buf)
- {
- struct i2c_client *client = to_i2c_client(dev);
- struct i2c_msg msgs[2];
- u8 msg_count = 1;
- int rc;
-
- if (write_len > PT_MAX_PIP2_MSG_SIZE)
- return -EINVAL;
- if (!write_buf || !write_len) {
- if (!write_buf)
- pt_debug(dev, DL_ERROR,
- "%s write_buf is NULL", __func__);
- if (!write_len)
- pt_debug(dev, DL_ERROR,
- "%s write_len is NULL", __func__);
- return -EINVAL;
- }
- msgs[0].addr = client->addr;
- msgs[0].flags = client->flags & I2C_M_TEN;
- msgs[0].len = write_len;
- msgs[0].buf = write_buf;
- rc = i2c_transfer(client->adapter, msgs, msg_count);
- if (rc < 0 || rc != msg_count)
- return (rc < 0) ? rc : -EIO;
- rc = 0;
- if (read_buf) {
- rc = pt_i2c_read_default_nosize(dev, read_buf,
- PT_I2C_DATA_SIZE);
- }
- return rc;
- }
- static struct pt_bus_ops pt_i2c_bus_ops = {
- .bustype = BUS_I2C,
- .read_default = pt_i2c_read_default,
- .read_default_nosize = pt_i2c_read_default_nosize,
- .write_read_specific = pt_i2c_write_read_specific,
- };
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- static const struct of_device_id pt_i2c_of_match[] = {
- { .compatible = "parade,pt_i2c_adapter", },
- { }
- };
- MODULE_DEVICE_TABLE(of, pt_i2c_of_match);
- #endif
- static int pt_i2c_probe(struct i2c_client *client,
- const struct i2c_device_id *i2c_id)
- {
- struct device *dev = &client->dev;
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- const struct of_device_id *match;
- #endif
- int rc;
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
- pt_debug(dev, DL_ERROR, "I2C functionality not Supported\n");
- return -EIO;
- }
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- match = of_match_device(of_match_ptr(pt_i2c_of_match), dev);
- if (match) {
- rc = pt_devtree_create_and_get_pdata(dev);
- if (rc < 0)
- return rc;
- }
- #endif
- rc = pt_probe(&pt_i2c_bus_ops, &client->dev, client->irq,
- PT_I2C_DATA_SIZE);
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- if (rc && match)
- pt_devtree_clean_pdata(dev);
- #endif
- return rc;
- }
- static int pt_i2c_remove(struct i2c_client *client)
- {
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- const struct of_device_id *match;
- #endif
- struct device *dev = &client->dev;
- struct pt_core_data *cd = i2c_get_clientdata(client);
- pt_release(cd);
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- match = of_match_device(of_match_ptr(pt_i2c_of_match), dev);
- if (match)
- pt_devtree_clean_pdata(dev);
- #endif
- return 0;
- }
- void pt_i2c_shutdown(struct i2c_client *client)
- {
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- const struct of_device_id *match;
- #endif
- struct device *dev = &client->dev;
- struct pt_core_data *cd = i2c_get_clientdata(client);
- pt_release(cd);
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- match = of_match_device(of_match_ptr(pt_i2c_of_match), dev);
- if (match)
- pt_devtree_clean_pdata(dev);
- #endif
- }
- static const struct i2c_device_id pt_i2c_id[] = {
- { PT_I2C_NAME, 0, },
- { }
- };
- MODULE_DEVICE_TABLE(i2c, pt_i2c_id);
- static struct i2c_driver pt_i2c_driver = {
- .driver = {
- .name = PT_I2C_NAME,
- .owner = THIS_MODULE,
- .pm = &pt_pm_ops,
- #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
- .of_match_table = pt_i2c_of_match,
- #endif
- },
- .probe = pt_i2c_probe,
- .remove = pt_i2c_remove,
- .shutdown = pt_i2c_shutdown,
- .id_table = pt_i2c_id,
- };
- #if (KERNEL_VERSION(3, 3, 0) <= LINUX_VERSION_CODE)
- module_i2c_driver(pt_i2c_driver);
- #else
- static int __init pt_i2c_init(void)
- {
- int rc = i2c_add_driver(&pt_i2c_driver);
- pr_info("%s: Parade TTDL I2C Driver (Build %s) rc=%d\n",
- __func__, PT_DRIVER_VERSION, rc);
- return rc;
- }
- module_init(pt_i2c_init);
- static void __exit pt_i2c_exit(void)
- {
- i2c_del_driver(&pt_i2c_driver);
- }
- module_exit(pt_i2c_exit);
- #endif
- MODULE_LICENSE("GPL");
- MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product I2C driver");
- MODULE_AUTHOR("Parade Technologies <ttdrivers@paradetech.com>");
|