11f67b599SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21a6e4b74SViresh Kumar /* 31a6e4b74SViresh Kumar * ST Microelectronics MFD: stmpe's i2c client specific driver 41a6e4b74SViresh Kumar * 51a6e4b74SViresh Kumar * Copyright (C) ST-Ericsson SA 2010 61a6e4b74SViresh Kumar * Copyright (C) ST Microelectronics SA 2011 71a6e4b74SViresh Kumar * 81a6e4b74SViresh Kumar * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson 9da89947bSViresh Kumar * Author: Viresh Kumar <vireshk@kernel.org> for ST Microelectronics 101a6e4b74SViresh Kumar */ 111a6e4b74SViresh Kumar 121a6e4b74SViresh Kumar #include <linux/i2c.h> 131a6e4b74SViresh Kumar #include <linux/interrupt.h> 141a6e4b74SViresh Kumar #include <linux/kernel.h> 151a6e4b74SViresh Kumar #include <linux/module.h> 161a6e4b74SViresh Kumar #include <linux/types.h> 175a826feeSLinus Walleij #include <linux/of_device.h> 181a6e4b74SViresh Kumar #include "stmpe.h" 191a6e4b74SViresh Kumar 201a6e4b74SViresh Kumar static int i2c_reg_read(struct stmpe *stmpe, u8 reg) 211a6e4b74SViresh Kumar { 221a6e4b74SViresh Kumar struct i2c_client *i2c = stmpe->client; 231a6e4b74SViresh Kumar 241a6e4b74SViresh Kumar return i2c_smbus_read_byte_data(i2c, reg); 251a6e4b74SViresh Kumar } 261a6e4b74SViresh Kumar 271a6e4b74SViresh Kumar static int i2c_reg_write(struct stmpe *stmpe, u8 reg, u8 val) 281a6e4b74SViresh Kumar { 291a6e4b74SViresh Kumar struct i2c_client *i2c = stmpe->client; 301a6e4b74SViresh Kumar 311a6e4b74SViresh Kumar return i2c_smbus_write_byte_data(i2c, reg, val); 321a6e4b74SViresh Kumar } 331a6e4b74SViresh Kumar 341a6e4b74SViresh Kumar static int i2c_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values) 351a6e4b74SViresh Kumar { 361a6e4b74SViresh Kumar struct i2c_client *i2c = stmpe->client; 371a6e4b74SViresh Kumar 381a6e4b74SViresh Kumar return i2c_smbus_read_i2c_block_data(i2c, reg, length, values); 391a6e4b74SViresh Kumar } 401a6e4b74SViresh Kumar 411a6e4b74SViresh Kumar static int i2c_block_write(struct stmpe *stmpe, u8 reg, u8 length, 421a6e4b74SViresh Kumar const u8 *values) 431a6e4b74SViresh Kumar { 441a6e4b74SViresh Kumar struct i2c_client *i2c = stmpe->client; 451a6e4b74SViresh Kumar 461a6e4b74SViresh Kumar return i2c_smbus_write_i2c_block_data(i2c, reg, length, values); 471a6e4b74SViresh Kumar } 481a6e4b74SViresh Kumar 491a6e4b74SViresh Kumar static struct stmpe_client_info i2c_ci = { 501a6e4b74SViresh Kumar .read_byte = i2c_reg_read, 511a6e4b74SViresh Kumar .write_byte = i2c_reg_write, 521a6e4b74SViresh Kumar .read_block = i2c_block_read, 531a6e4b74SViresh Kumar .write_block = i2c_block_write, 541a6e4b74SViresh Kumar }; 551a6e4b74SViresh Kumar 565a826feeSLinus Walleij static const struct of_device_id stmpe_of_match[] = { 575a826feeSLinus Walleij { .compatible = "st,stmpe610", .data = (void *)STMPE610, }, 585a826feeSLinus Walleij { .compatible = "st,stmpe801", .data = (void *)STMPE801, }, 595a826feeSLinus Walleij { .compatible = "st,stmpe811", .data = (void *)STMPE811, }, 606bb9f0d9SPatrice Chotard { .compatible = "st,stmpe1600", .data = (void *)STMPE1600, }, 615a826feeSLinus Walleij { .compatible = "st,stmpe1601", .data = (void *)STMPE1601, }, 625a826feeSLinus Walleij { .compatible = "st,stmpe1801", .data = (void *)STMPE1801, }, 635a826feeSLinus Walleij { .compatible = "st,stmpe2401", .data = (void *)STMPE2401, }, 645a826feeSLinus Walleij { .compatible = "st,stmpe2403", .data = (void *)STMPE2403, }, 655a826feeSLinus Walleij {}, 665a826feeSLinus Walleij }; 675a826feeSLinus Walleij MODULE_DEVICE_TABLE(of, stmpe_of_match); 685a826feeSLinus Walleij 69f791be49SBill Pemberton static int 70*50b1d5baSUwe Kleine-König stmpe_i2c_probe(struct i2c_client *i2c) 711a6e4b74SViresh Kumar { 72*50b1d5baSUwe Kleine-König const struct i2c_device_id *id = i2c_client_get_device_id(i2c); 73c00572bcSLee Jones enum stmpe_partnum partnum; 745a826feeSLinus Walleij const struct of_device_id *of_id; 755a826feeSLinus Walleij 761a6e4b74SViresh Kumar i2c_ci.data = (void *)id; 771a6e4b74SViresh Kumar i2c_ci.irq = i2c->irq; 781a6e4b74SViresh Kumar i2c_ci.client = i2c; 791a6e4b74SViresh Kumar i2c_ci.dev = &i2c->dev; 801a6e4b74SViresh Kumar 815a826feeSLinus Walleij of_id = of_match_device(stmpe_of_match, &i2c->dev); 825a826feeSLinus Walleij if (!of_id) { 835a826feeSLinus Walleij /* 845a826feeSLinus Walleij * This happens when the I2C ID matches the node name 855a826feeSLinus Walleij * but no real compatible string has been given. 865a826feeSLinus Walleij */ 875a826feeSLinus Walleij dev_info(&i2c->dev, "matching on node name, compatible is preferred\n"); 885a826feeSLinus Walleij partnum = id->driver_data; 895a826feeSLinus Walleij } else 90c00572bcSLee Jones partnum = (enum stmpe_partnum)of_id->data; 915a826feeSLinus Walleij 925a826feeSLinus Walleij return stmpe_probe(&i2c_ci, partnum); 931a6e4b74SViresh Kumar } 941a6e4b74SViresh Kumar 95ed5c2f5fSUwe Kleine-König static void stmpe_i2c_remove(struct i2c_client *i2c) 961a6e4b74SViresh Kumar { 971a6e4b74SViresh Kumar struct stmpe *stmpe = dev_get_drvdata(&i2c->dev); 981a6e4b74SViresh Kumar 99356bbabaSUwe Kleine-König stmpe_remove(stmpe); 1001a6e4b74SViresh Kumar } 1011a6e4b74SViresh Kumar 1021a6e4b74SViresh Kumar static const struct i2c_device_id stmpe_i2c_id[] = { 1031cda2394SViresh Kumar { "stmpe610", STMPE610 }, 1047f7f4ea1SViresh Kumar { "stmpe801", STMPE801 }, 1051a6e4b74SViresh Kumar { "stmpe811", STMPE811 }, 1066bb9f0d9SPatrice Chotard { "stmpe1600", STMPE1600 }, 1071a6e4b74SViresh Kumar { "stmpe1601", STMPE1601 }, 108230f13a5SJean-Nicolas Graux { "stmpe1801", STMPE1801 }, 1091a6e4b74SViresh Kumar { "stmpe2401", STMPE2401 }, 1101a6e4b74SViresh Kumar { "stmpe2403", STMPE2403 }, 1111a6e4b74SViresh Kumar { } 1121a6e4b74SViresh Kumar }; 1134700ef32SZou Wei MODULE_DEVICE_TABLE(i2c, stmpe_i2c_id); 1141a6e4b74SViresh Kumar 1151a6e4b74SViresh Kumar static struct i2c_driver stmpe_i2c_driver = { 11632533983SViresh Kumar .driver = { 11732533983SViresh Kumar .name = "stmpe-i2c", 1181a6e4b74SViresh Kumar #ifdef CONFIG_PM 11932533983SViresh Kumar .pm = &stmpe_dev_pm_ops, 1201a6e4b74SViresh Kumar #endif 1215a826feeSLinus Walleij .of_match_table = stmpe_of_match, 12232533983SViresh Kumar }, 123*50b1d5baSUwe Kleine-König .probe_new = stmpe_i2c_probe, 12484449216SBill Pemberton .remove = stmpe_i2c_remove, 1251a6e4b74SViresh Kumar .id_table = stmpe_i2c_id, 1261a6e4b74SViresh Kumar }; 1271a6e4b74SViresh Kumar 1281a6e4b74SViresh Kumar static int __init stmpe_init(void) 1291a6e4b74SViresh Kumar { 1301a6e4b74SViresh Kumar return i2c_add_driver(&stmpe_i2c_driver); 1311a6e4b74SViresh Kumar } 1321a6e4b74SViresh Kumar subsys_initcall(stmpe_init); 1331a6e4b74SViresh Kumar 1341a6e4b74SViresh Kumar static void __exit stmpe_exit(void) 1351a6e4b74SViresh Kumar { 1361a6e4b74SViresh Kumar i2c_del_driver(&stmpe_i2c_driver); 1371a6e4b74SViresh Kumar } 1381a6e4b74SViresh Kumar module_exit(stmpe_exit); 1391a6e4b74SViresh Kumar 1401a6e4b74SViresh Kumar MODULE_LICENSE("GPL v2"); 1411a6e4b74SViresh Kumar MODULE_DESCRIPTION("STMPE MFD I2C Interface Driver"); 1421a6e4b74SViresh Kumar MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>"); 143