regmap: Add support for SCCB

This is an I2C subset.
 -----BEGIN PGP SIGNATURE-----
 
 iQFHBAABCgAxFiEEreZoqmdXGLWf4p/qJNaLcl1Uh9AFAltPUrMTHGJyb29uaWVA
 a2VybmVsLm9yZwAKCRAk1otyXVSH0KODB/oDV+4c82VIUopoaXxK8PbBqDMn5n2r
 EKyb1D/GFiT54x2666arRT1NymL/uk6/nK1hryd/4HE5zB1lUQnu/Z7HCHgF4N+/
 iWHtAgFUSZtNlMTRT6DW/tcsNDGnH9vjNJUhOLH9Y21h3Q4BpOo5HQVR8ynN4TDh
 2inSy5qgADyrosxRpW5mp0Nt0tpFTfvYOHoRaLTbQUbBnpb/MmqcTzkYHcVNqA8V
 7+e9/SOVU0XXHQD5k28F7eB84Mk+TXQrZQ181RwNOKUf3je86Im6WfrFqKHqKP5N
 UAq28AjsjRiBs1qwOYK0ytWR8gQvs39cZmdud507R1mLWEO4/++TruRt
 =wkUm
 -----END PGP SIGNATURE-----
gpgsig -----BEGIN PGP SIGNATURE-----
 
 iQFHBAABCgAxFiEEreZoqmdXGLWf4p/qJNaLcl1Uh9AFAltPUyETHGJyb29uaWVA
 a2VybmVsLm9yZwAKCRAk1otyXVSH0CJsB/oD+83KWX8V1Q4HhaO3e6BN2SPYphQr
 jbImC9QUSRl/65+0ek6VjqLu4znY/LARRjNThN0l01wvWv7+h71DJBSKL67zK9en
 h4Lr8ZoewBQ408YaC5zl7ilFXgRBMhfCanDWWHv8jAv1rk9VQU8q9uNGo518e0Ks
 2Gg/zTmQYp7dNMHsVO4AEr3UQye1yZdGdP1q1yk3nbNgZbDq4xWWZWl1p4Iy+QG3
 e4SY+OYenQC/+pafUQOzPoiG3I/f8RAQG9SAtne+gtIyF85+Q8mVizuU89GdtH3O
 rKcaiNk/FpSbcBxAXEGb0qNy6PW6gnAbSznrOdc7MEj2V5E3KfoR7oVO
 =VO/+
 -----END PGP SIGNATURE-----

Merge tag 'regmap-sccb' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap into regmap-4.19

regmap: Add support for SCCB

This is an I2C subset.
This commit is contained in:
Mark Brown 2018-07-18 15:47:54 +01:00
commit e594a0636b
4 changed files with 168 additions and 0 deletions

View File

@ -45,3 +45,7 @@ config REGMAP_IRQ
config REGMAP_SOUNDWIRE config REGMAP_SOUNDWIRE
tristate tristate
depends on SOUNDWIRE_BUS depends on SOUNDWIRE_BUS
config REGMAP_SCCB
tristate
depends on I2C

View File

@ -15,3 +15,4 @@ obj-$(CONFIG_REGMAP_MMIO) += regmap-mmio.o
obj-$(CONFIG_REGMAP_IRQ) += regmap-irq.o obj-$(CONFIG_REGMAP_IRQ) += regmap-irq.o
obj-$(CONFIG_REGMAP_W1) += regmap-w1.o obj-$(CONFIG_REGMAP_W1) += regmap-w1.o
obj-$(CONFIG_REGMAP_SOUNDWIRE) += regmap-sdw.o obj-$(CONFIG_REGMAP_SOUNDWIRE) += regmap-sdw.o
obj-$(CONFIG_REGMAP_SCCB) += regmap-sccb.o

View File

@ -0,0 +1,128 @@
// SPDX-License-Identifier: GPL-2.0
// Register map access API - SCCB support
#include <linux/regmap.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include "internal.h"
/**
* sccb_is_available - Check if the adapter supports SCCB protocol
* @adap: I2C adapter
*
* Return true if the I2C adapter is capable of using SCCB helper functions,
* false otherwise.
*/
static bool sccb_is_available(struct i2c_adapter *adap)
{
u32 needed_funcs = I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
/*
* If we ever want support for hardware doing SCCB natively, we will
* introduce a sccb_xfer() callback to struct i2c_algorithm and check
* for it here.
*/
return (i2c_get_functionality(adap) & needed_funcs) == needed_funcs;
}
/**
* regmap_sccb_read - Read data from SCCB slave device
* @context: Device that will be interacted wit
* @reg: Register to be read from
* @val: Pointer to store read value
*
* This executes the 2-phase write transmission cycle that is followed by a
* 2-phase read transmission cycle, returning negative errno else zero on
* success.
*/
static int regmap_sccb_read(void *context, unsigned int reg, unsigned int *val)
{
struct device *dev = context;
struct i2c_client *i2c = to_i2c_client(dev);
int ret;
union i2c_smbus_data data;
i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE, NULL);
if (ret < 0)
goto out;
ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
if (ret < 0)
goto out;
*val = data.byte;
out:
i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
return ret;
}
/**
* regmap_sccb_write - Write data to SCCB slave device
* @context: Device that will be interacted wit
* @reg: Register to write to
* @val: Value to be written
*
* This executes the SCCB 3-phase write transmission cycle, returning negative
* errno else zero on success.
*/
static int regmap_sccb_write(void *context, unsigned int reg, unsigned int val)
{
struct device *dev = context;
struct i2c_client *i2c = to_i2c_client(dev);
return i2c_smbus_write_byte_data(i2c, reg, val);
}
static struct regmap_bus regmap_sccb_bus = {
.reg_write = regmap_sccb_write,
.reg_read = regmap_sccb_read,
};
static const struct regmap_bus *regmap_get_sccb_bus(struct i2c_client *i2c,
const struct regmap_config *config)
{
if (config->val_bits == 8 && config->reg_bits == 8 &&
sccb_is_available(i2c->adapter))
return &regmap_sccb_bus;
return ERR_PTR(-ENOTSUPP);
}
struct regmap *__regmap_init_sccb(struct i2c_client *i2c,
const struct regmap_config *config,
struct lock_class_key *lock_key,
const char *lock_name)
{
const struct regmap_bus *bus = regmap_get_sccb_bus(i2c, config);
if (IS_ERR(bus))
return ERR_CAST(bus);
return __regmap_init(&i2c->dev, bus, &i2c->dev, config,
lock_key, lock_name);
}
EXPORT_SYMBOL_GPL(__regmap_init_sccb);
struct regmap *__devm_regmap_init_sccb(struct i2c_client *i2c,
const struct regmap_config *config,
struct lock_class_key *lock_key,
const char *lock_name)
{
const struct regmap_bus *bus = regmap_get_sccb_bus(i2c, config);
if (IS_ERR(bus))
return ERR_CAST(bus);
return __devm_regmap_init(&i2c->dev, bus, &i2c->dev, config,
lock_key, lock_name);
}
EXPORT_SYMBOL_GPL(__devm_regmap_init_sccb);
MODULE_LICENSE("GPL v2");

View File

@ -514,6 +514,10 @@ struct regmap *__regmap_init_i2c(struct i2c_client *i2c,
const struct regmap_config *config, const struct regmap_config *config,
struct lock_class_key *lock_key, struct lock_class_key *lock_key,
const char *lock_name); const char *lock_name);
struct regmap *__regmap_init_sccb(struct i2c_client *i2c,
const struct regmap_config *config,
struct lock_class_key *lock_key,
const char *lock_name);
struct regmap *__regmap_init_slimbus(struct slim_device *slimbus, struct regmap *__regmap_init_slimbus(struct slim_device *slimbus,
const struct regmap_config *config, const struct regmap_config *config,
struct lock_class_key *lock_key, struct lock_class_key *lock_key,
@ -558,6 +562,10 @@ struct regmap *__devm_regmap_init_i2c(struct i2c_client *i2c,
const struct regmap_config *config, const struct regmap_config *config,
struct lock_class_key *lock_key, struct lock_class_key *lock_key,
const char *lock_name); const char *lock_name);
struct regmap *__devm_regmap_init_sccb(struct i2c_client *i2c,
const struct regmap_config *config,
struct lock_class_key *lock_key,
const char *lock_name);
struct regmap *__devm_regmap_init_spi(struct spi_device *dev, struct regmap *__devm_regmap_init_spi(struct spi_device *dev,
const struct regmap_config *config, const struct regmap_config *config,
struct lock_class_key *lock_key, struct lock_class_key *lock_key,
@ -645,6 +653,19 @@ int regmap_attach_dev(struct device *dev, struct regmap *map,
__regmap_lockdep_wrapper(__regmap_init_i2c, #config, \ __regmap_lockdep_wrapper(__regmap_init_i2c, #config, \
i2c, config) i2c, config)
/**
* regmap_init_sccb() - Initialise register map
*
* @i2c: Device that will be interacted with
* @config: Configuration for register map
*
* The return value will be an ERR_PTR() on error or a valid pointer to
* a struct regmap.
*/
#define regmap_init_sccb(i2c, config) \
__regmap_lockdep_wrapper(__regmap_init_sccb, #config, \
i2c, config)
/** /**
* regmap_init_slimbus() - Initialise register map * regmap_init_slimbus() - Initialise register map
* *
@ -797,6 +818,20 @@ bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
__regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config, \ __regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config, \
i2c, config) i2c, config)
/**
* devm_regmap_init_sccb() - Initialise managed register map
*
* @i2c: Device that will be interacted with
* @config: Configuration for register map
*
* The return value will be an ERR_PTR() on error or a valid pointer
* to a struct regmap. The regmap will be automatically freed by the
* device management code.
*/
#define devm_regmap_init_sccb(i2c, config) \
__regmap_lockdep_wrapper(__devm_regmap_init_sccb, #config, \
i2c, config)
/** /**
* devm_regmap_init_spi() - Initialise register map * devm_regmap_init_spi() - Initialise register map
* *