18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * I2C slave mode testunit 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2020 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com> 68c2ecf20Sopenharmony_ci * Copyright (C) 2020 by Renesas Electronics Corporation 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/bitops.h> 108c2ecf20Sopenharmony_ci#include <linux/i2c.h> 118c2ecf20Sopenharmony_ci#include <linux/init.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/of.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#include <linux/workqueue.h> /* FIXME: is system_long_wq the best choice? */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define TU_CUR_VERSION 0x01 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cienum testunit_cmds { 208c2ecf20Sopenharmony_ci TU_CMD_READ_BYTES = 1, /* save 0 for ABORT, RESET or similar */ 218c2ecf20Sopenharmony_ci TU_CMD_HOST_NOTIFY, 228c2ecf20Sopenharmony_ci TU_NUM_CMDS 238c2ecf20Sopenharmony_ci}; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cienum testunit_regs { 268c2ecf20Sopenharmony_ci TU_REG_CMD, 278c2ecf20Sopenharmony_ci TU_REG_DATAL, 288c2ecf20Sopenharmony_ci TU_REG_DATAH, 298c2ecf20Sopenharmony_ci TU_REG_DELAY, 308c2ecf20Sopenharmony_ci TU_NUM_REGS 318c2ecf20Sopenharmony_ci}; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cienum testunit_flags { 348c2ecf20Sopenharmony_ci TU_FLAG_IN_PROCESS, 358c2ecf20Sopenharmony_ci}; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistruct testunit_data { 388c2ecf20Sopenharmony_ci unsigned long flags; 398c2ecf20Sopenharmony_ci u8 regs[TU_NUM_REGS]; 408c2ecf20Sopenharmony_ci u8 reg_idx; 418c2ecf20Sopenharmony_ci struct i2c_client *client; 428c2ecf20Sopenharmony_ci struct delayed_work worker; 438c2ecf20Sopenharmony_ci}; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistatic void i2c_slave_testunit_work(struct work_struct *work) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci struct testunit_data *tu = container_of(work, struct testunit_data, worker.work); 488c2ecf20Sopenharmony_ci struct i2c_msg msg; 498c2ecf20Sopenharmony_ci u8 msgbuf[256]; 508c2ecf20Sopenharmony_ci int ret = 0; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci msg.addr = I2C_CLIENT_END; 538c2ecf20Sopenharmony_ci msg.buf = msgbuf; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci switch (tu->regs[TU_REG_CMD]) { 568c2ecf20Sopenharmony_ci case TU_CMD_READ_BYTES: 578c2ecf20Sopenharmony_ci msg.addr = tu->regs[TU_REG_DATAL]; 588c2ecf20Sopenharmony_ci msg.flags = I2C_M_RD; 598c2ecf20Sopenharmony_ci msg.len = tu->regs[TU_REG_DATAH]; 608c2ecf20Sopenharmony_ci break; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci case TU_CMD_HOST_NOTIFY: 638c2ecf20Sopenharmony_ci msg.addr = 0x08; 648c2ecf20Sopenharmony_ci msg.flags = 0; 658c2ecf20Sopenharmony_ci msg.len = 3; 668c2ecf20Sopenharmony_ci msgbuf[0] = tu->client->addr; 678c2ecf20Sopenharmony_ci msgbuf[1] = tu->regs[TU_REG_DATAL]; 688c2ecf20Sopenharmony_ci msgbuf[2] = tu->regs[TU_REG_DATAH]; 698c2ecf20Sopenharmony_ci break; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci default: 728c2ecf20Sopenharmony_ci break; 738c2ecf20Sopenharmony_ci } 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci if (msg.addr != I2C_CLIENT_END) { 768c2ecf20Sopenharmony_ci ret = i2c_transfer(tu->client->adapter, &msg, 1); 778c2ecf20Sopenharmony_ci /* convert '0 msgs transferred' to errno */ 788c2ecf20Sopenharmony_ci ret = (ret == 0) ? -EIO : ret; 798c2ecf20Sopenharmony_ci } 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if (ret < 0) 828c2ecf20Sopenharmony_ci dev_err(&tu->client->dev, "CMD%02X failed (%d)\n", tu->regs[TU_REG_CMD], ret); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci clear_bit(TU_FLAG_IN_PROCESS, &tu->flags); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic int i2c_slave_testunit_slave_cb(struct i2c_client *client, 888c2ecf20Sopenharmony_ci enum i2c_slave_event event, u8 *val) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci struct testunit_data *tu = i2c_get_clientdata(client); 918c2ecf20Sopenharmony_ci int ret = 0; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci switch (event) { 948c2ecf20Sopenharmony_ci case I2C_SLAVE_WRITE_RECEIVED: 958c2ecf20Sopenharmony_ci if (test_bit(TU_FLAG_IN_PROCESS, &tu->flags)) 968c2ecf20Sopenharmony_ci return -EBUSY; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci if (tu->reg_idx < TU_NUM_REGS) 998c2ecf20Sopenharmony_ci tu->regs[tu->reg_idx] = *val; 1008c2ecf20Sopenharmony_ci else 1018c2ecf20Sopenharmony_ci ret = -EMSGSIZE; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci if (tu->reg_idx <= TU_NUM_REGS) 1048c2ecf20Sopenharmony_ci tu->reg_idx++; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* TU_REG_CMD always written at this point */ 1078c2ecf20Sopenharmony_ci if (tu->regs[TU_REG_CMD] >= TU_NUM_CMDS) 1088c2ecf20Sopenharmony_ci ret = -EINVAL; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci break; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci case I2C_SLAVE_STOP: 1138c2ecf20Sopenharmony_ci if (tu->reg_idx == TU_NUM_REGS) { 1148c2ecf20Sopenharmony_ci set_bit(TU_FLAG_IN_PROCESS, &tu->flags); 1158c2ecf20Sopenharmony_ci queue_delayed_work(system_long_wq, &tu->worker, 1168c2ecf20Sopenharmony_ci msecs_to_jiffies(10 * tu->regs[TU_REG_DELAY])); 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci fallthrough; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci case I2C_SLAVE_WRITE_REQUESTED: 1218c2ecf20Sopenharmony_ci tu->reg_idx = 0; 1228c2ecf20Sopenharmony_ci break; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci case I2C_SLAVE_READ_REQUESTED: 1258c2ecf20Sopenharmony_ci case I2C_SLAVE_READ_PROCESSED: 1268c2ecf20Sopenharmony_ci *val = TU_CUR_VERSION; 1278c2ecf20Sopenharmony_ci break; 1288c2ecf20Sopenharmony_ci } 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci return ret; 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic int i2c_slave_testunit_probe(struct i2c_client *client) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci struct testunit_data *tu; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci tu = devm_kzalloc(&client->dev, sizeof(struct testunit_data), GFP_KERNEL); 1388c2ecf20Sopenharmony_ci if (!tu) 1398c2ecf20Sopenharmony_ci return -ENOMEM; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci tu->client = client; 1428c2ecf20Sopenharmony_ci i2c_set_clientdata(client, tu); 1438c2ecf20Sopenharmony_ci INIT_DELAYED_WORK(&tu->worker, i2c_slave_testunit_work); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci return i2c_slave_register(client, i2c_slave_testunit_slave_cb); 1468c2ecf20Sopenharmony_ci}; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic int i2c_slave_testunit_remove(struct i2c_client *client) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct testunit_data *tu = i2c_get_clientdata(client); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&tu->worker); 1538c2ecf20Sopenharmony_ci i2c_slave_unregister(client); 1548c2ecf20Sopenharmony_ci return 0; 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic const struct i2c_device_id i2c_slave_testunit_id[] = { 1588c2ecf20Sopenharmony_ci { "slave-testunit", 0 }, 1598c2ecf20Sopenharmony_ci { } 1608c2ecf20Sopenharmony_ci}; 1618c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, i2c_slave_testunit_id); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic struct i2c_driver i2c_slave_testunit_driver = { 1648c2ecf20Sopenharmony_ci .driver = { 1658c2ecf20Sopenharmony_ci .name = "i2c-slave-testunit", 1668c2ecf20Sopenharmony_ci }, 1678c2ecf20Sopenharmony_ci .probe_new = i2c_slave_testunit_probe, 1688c2ecf20Sopenharmony_ci .remove = i2c_slave_testunit_remove, 1698c2ecf20Sopenharmony_ci .id_table = i2c_slave_testunit_id, 1708c2ecf20Sopenharmony_ci}; 1718c2ecf20Sopenharmony_cimodule_i2c_driver(i2c_slave_testunit_driver); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ciMODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>"); 1748c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("I2C slave mode test unit"); 1758c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 176