18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * ddbridge-i2c.h: Digital Devices bridge i2c driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2010-2017 Digital Devices GmbH 68c2ecf20Sopenharmony_ci * Ralph Metzler <rjkm@metzlerbros.de> 78c2ecf20Sopenharmony_ci * Marcus Metzler <mocm@metzlerbros.de> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or 108c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License 118c2ecf20Sopenharmony_ci * version 2 only, as published by the Free Software Foundation. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, 148c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 168c2ecf20Sopenharmony_ci * GNU General Public License for more details. 178c2ecf20Sopenharmony_ci */ 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#ifndef __DDBRIDGE_I2C_H__ 208c2ecf20Sopenharmony_ci#define __DDBRIDGE_I2C_H__ 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <linux/i2c.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#include "ddbridge.h" 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/******************************************************************************/ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_civoid ddb_i2c_release(struct ddb *dev); 298c2ecf20Sopenharmony_ciint ddb_i2c_init(struct ddb *dev); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci/******************************************************************************/ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_io(struct i2c_adapter *adapter, u8 adr, 348c2ecf20Sopenharmony_ci u8 *wbuf, u32 wlen, u8 *rbuf, u32 rlen) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, 378c2ecf20Sopenharmony_ci .buf = wbuf, .len = wlen }, 388c2ecf20Sopenharmony_ci { .addr = adr, .flags = I2C_M_RD, 398c2ecf20Sopenharmony_ci .buf = rbuf, .len = rlen } }; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_write(struct i2c_adapter *adap, u8 adr, 458c2ecf20Sopenharmony_ci u8 *data, int len) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci struct i2c_msg msg = { .addr = adr, .flags = 0, 488c2ecf20Sopenharmony_ci .buf = data, .len = len }; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1; 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_read(struct i2c_adapter *adapter, u8 adr, u8 *val) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci struct i2c_msg msgs[1] = { { .addr = adr, .flags = I2C_M_RD, 568c2ecf20Sopenharmony_ci .buf = val, .len = 1 } }; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1; 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_read_regs(struct i2c_adapter *adapter, 628c2ecf20Sopenharmony_ci u8 adr, u8 reg, u8 *val, u8 len) 638c2ecf20Sopenharmony_ci{ 648c2ecf20Sopenharmony_ci struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, 658c2ecf20Sopenharmony_ci .buf = ®, .len = 1 }, 668c2ecf20Sopenharmony_ci { .addr = adr, .flags = I2C_M_RD, 678c2ecf20Sopenharmony_ci .buf = val, .len = len } }; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_read_regs16(struct i2c_adapter *adapter, 738c2ecf20Sopenharmony_ci u8 adr, u16 reg, u8 *val, u8 len) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci u8 msg[2] = { reg >> 8, reg & 0xff }; 768c2ecf20Sopenharmony_ci struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, 778c2ecf20Sopenharmony_ci .buf = msg, .len = 2 }, 788c2ecf20Sopenharmony_ci { .addr = adr, .flags = I2C_M_RD, 798c2ecf20Sopenharmony_ci .buf = val, .len = len } }; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; 828c2ecf20Sopenharmony_ci} 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_write_reg16(struct i2c_adapter *adap, 858c2ecf20Sopenharmony_ci u8 adr, u16 reg, u8 val) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci u8 msg[3] = { reg >> 8, reg & 0xff, val }; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci return i2c_write(adap, adr, msg, 3); 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_write_reg(struct i2c_adapter *adap, 938c2ecf20Sopenharmony_ci u8 adr, u8 reg, u8 val) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci u8 msg[2] = { reg, val }; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci return i2c_write(adap, adr, msg, 2); 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_read_reg16(struct i2c_adapter *adapter, 1018c2ecf20Sopenharmony_ci u8 adr, u16 reg, u8 *val) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci return i2c_read_regs16(adapter, adr, reg, val, 1); 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic int __maybe_unused i2c_read_reg(struct i2c_adapter *adapter, 1078c2ecf20Sopenharmony_ci u8 adr, u8 reg, u8 *val) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci return i2c_read_regs(adapter, adr, reg, val, 1); 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci#endif /* __DDBRIDGE_I2C_H__ */ 113