18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// Register map access API - SCCB support
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/i2c.h>
58c2ecf20Sopenharmony_ci#include <linux/module.h>
68c2ecf20Sopenharmony_ci#include <linux/regmap.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "internal.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/**
118c2ecf20Sopenharmony_ci * sccb_is_available - Check if the adapter supports SCCB protocol
128c2ecf20Sopenharmony_ci * @adap: I2C adapter
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Return true if the I2C adapter is capable of using SCCB helper functions,
158c2ecf20Sopenharmony_ci * false otherwise.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_cistatic bool sccb_is_available(struct i2c_adapter *adap)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	u32 needed_funcs = I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	/*
228c2ecf20Sopenharmony_ci	 * If we ever want support for hardware doing SCCB natively, we will
238c2ecf20Sopenharmony_ci	 * introduce a sccb_xfer() callback to struct i2c_algorithm and check
248c2ecf20Sopenharmony_ci	 * for it here.
258c2ecf20Sopenharmony_ci	 */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	return (i2c_get_functionality(adap) & needed_funcs) == needed_funcs;
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/**
318c2ecf20Sopenharmony_ci * regmap_sccb_read - Read data from SCCB slave device
328c2ecf20Sopenharmony_ci * @context: Device that will be interacted with
338c2ecf20Sopenharmony_ci * @reg: Register to be read from
348c2ecf20Sopenharmony_ci * @val: Pointer to store read value
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * This executes the 2-phase write transmission cycle that is followed by a
378c2ecf20Sopenharmony_ci * 2-phase read transmission cycle, returning negative errno else zero on
388c2ecf20Sopenharmony_ci * success.
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cistatic int regmap_sccb_read(void *context, unsigned int reg, unsigned int *val)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct device *dev = context;
438c2ecf20Sopenharmony_ci	struct i2c_client *i2c = to_i2c_client(dev);
448c2ecf20Sopenharmony_ci	int ret;
458c2ecf20Sopenharmony_ci	union i2c_smbus_data data;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
508c2ecf20Sopenharmony_ci			       I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE, NULL);
518c2ecf20Sopenharmony_ci	if (ret < 0)
528c2ecf20Sopenharmony_ci		goto out;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
558c2ecf20Sopenharmony_ci			       I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
568c2ecf20Sopenharmony_ci	if (ret < 0)
578c2ecf20Sopenharmony_ci		goto out;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	*val = data.byte;
608c2ecf20Sopenharmony_ciout:
618c2ecf20Sopenharmony_ci	i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return ret;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/**
678c2ecf20Sopenharmony_ci * regmap_sccb_write - Write data to SCCB slave device
688c2ecf20Sopenharmony_ci * @context: Device that will be interacted with
698c2ecf20Sopenharmony_ci * @reg: Register to write to
708c2ecf20Sopenharmony_ci * @val: Value to be written
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * This executes the SCCB 3-phase write transmission cycle, returning negative
738c2ecf20Sopenharmony_ci * errno else zero on success.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistatic int regmap_sccb_write(void *context, unsigned int reg, unsigned int val)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct device *dev = context;
788c2ecf20Sopenharmony_ci	struct i2c_client *i2c = to_i2c_client(dev);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(i2c, reg, val);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic struct regmap_bus regmap_sccb_bus = {
848c2ecf20Sopenharmony_ci	.reg_write = regmap_sccb_write,
858c2ecf20Sopenharmony_ci	.reg_read = regmap_sccb_read,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic const struct regmap_bus *regmap_get_sccb_bus(struct i2c_client *i2c,
898c2ecf20Sopenharmony_ci					const struct regmap_config *config)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	if (config->val_bits == 8 && config->reg_bits == 8 &&
928c2ecf20Sopenharmony_ci			sccb_is_available(i2c->adapter))
938c2ecf20Sopenharmony_ci		return &regmap_sccb_bus;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return ERR_PTR(-ENOTSUPP);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct regmap *__regmap_init_sccb(struct i2c_client *i2c,
998c2ecf20Sopenharmony_ci				  const struct regmap_config *config,
1008c2ecf20Sopenharmony_ci				  struct lock_class_key *lock_key,
1018c2ecf20Sopenharmony_ci				  const char *lock_name)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	const struct regmap_bus *bus = regmap_get_sccb_bus(i2c, config);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (IS_ERR(bus))
1068c2ecf20Sopenharmony_ci		return ERR_CAST(bus);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return __regmap_init(&i2c->dev, bus, &i2c->dev, config,
1098c2ecf20Sopenharmony_ci			     lock_key, lock_name);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__regmap_init_sccb);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistruct regmap *__devm_regmap_init_sccb(struct i2c_client *i2c,
1148c2ecf20Sopenharmony_ci				       const struct regmap_config *config,
1158c2ecf20Sopenharmony_ci				       struct lock_class_key *lock_key,
1168c2ecf20Sopenharmony_ci				       const char *lock_name)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	const struct regmap_bus *bus = regmap_get_sccb_bus(i2c, config);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if (IS_ERR(bus))
1218c2ecf20Sopenharmony_ci		return ERR_CAST(bus);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return __devm_regmap_init(&i2c->dev, bus, &i2c->dev, config,
1248c2ecf20Sopenharmony_ci				  lock_key, lock_name);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__devm_regmap_init_sccb);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
129