Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Register map access API - SCCB support
0003 
0004 #include <linux/i2c.h>
0005 #include <linux/module.h>
0006 #include <linux/regmap.h>
0007 
0008 #include "internal.h"
0009 
0010 /**
0011  * sccb_is_available - Check if the adapter supports SCCB protocol
0012  * @adap: I2C adapter
0013  *
0014  * Return true if the I2C adapter is capable of using SCCB helper functions,
0015  * false otherwise.
0016  */
0017 static bool sccb_is_available(struct i2c_adapter *adap)
0018 {
0019     u32 needed_funcs = I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
0020 
0021     /*
0022      * If we ever want support for hardware doing SCCB natively, we will
0023      * introduce a sccb_xfer() callback to struct i2c_algorithm and check
0024      * for it here.
0025      */
0026 
0027     return (i2c_get_functionality(adap) & needed_funcs) == needed_funcs;
0028 }
0029 
0030 /**
0031  * regmap_sccb_read - Read data from SCCB slave device
0032  * @context: Device that will be interacted with
0033  * @reg: Register to be read from
0034  * @val: Pointer to store read value
0035  *
0036  * This executes the 2-phase write transmission cycle that is followed by a
0037  * 2-phase read transmission cycle, returning negative errno else zero on
0038  * success.
0039  */
0040 static int regmap_sccb_read(void *context, unsigned int reg, unsigned int *val)
0041 {
0042     struct device *dev = context;
0043     struct i2c_client *i2c = to_i2c_client(dev);
0044     int ret;
0045     union i2c_smbus_data data;
0046 
0047     i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
0048 
0049     ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
0050                    I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE, NULL);
0051     if (ret < 0)
0052         goto out;
0053 
0054     ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
0055                    I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
0056     if (ret < 0)
0057         goto out;
0058 
0059     *val = data.byte;
0060 out:
0061     i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
0062 
0063     return ret;
0064 }
0065 
0066 /**
0067  * regmap_sccb_write - Write data to SCCB slave device
0068  * @context: Device that will be interacted with
0069  * @reg: Register to write to
0070  * @val: Value to be written
0071  *
0072  * This executes the SCCB 3-phase write transmission cycle, returning negative
0073  * errno else zero on success.
0074  */
0075 static int regmap_sccb_write(void *context, unsigned int reg, unsigned int val)
0076 {
0077     struct device *dev = context;
0078     struct i2c_client *i2c = to_i2c_client(dev);
0079 
0080     return i2c_smbus_write_byte_data(i2c, reg, val);
0081 }
0082 
0083 static const struct regmap_bus regmap_sccb_bus = {
0084     .reg_write = regmap_sccb_write,
0085     .reg_read = regmap_sccb_read,
0086 };
0087 
0088 static const struct regmap_bus *regmap_get_sccb_bus(struct i2c_client *i2c,
0089                     const struct regmap_config *config)
0090 {
0091     if (config->val_bits == 8 && config->reg_bits == 8 &&
0092             sccb_is_available(i2c->adapter))
0093         return &regmap_sccb_bus;
0094 
0095     return ERR_PTR(-ENOTSUPP);
0096 }
0097 
0098 struct regmap *__regmap_init_sccb(struct i2c_client *i2c,
0099                   const struct regmap_config *config,
0100                   struct lock_class_key *lock_key,
0101                   const char *lock_name)
0102 {
0103     const struct regmap_bus *bus = regmap_get_sccb_bus(i2c, config);
0104 
0105     if (IS_ERR(bus))
0106         return ERR_CAST(bus);
0107 
0108     return __regmap_init(&i2c->dev, bus, &i2c->dev, config,
0109                  lock_key, lock_name);
0110 }
0111 EXPORT_SYMBOL_GPL(__regmap_init_sccb);
0112 
0113 struct regmap *__devm_regmap_init_sccb(struct i2c_client *i2c,
0114                        const struct regmap_config *config,
0115                        struct lock_class_key *lock_key,
0116                        const char *lock_name)
0117 {
0118     const struct regmap_bus *bus = regmap_get_sccb_bus(i2c, config);
0119 
0120     if (IS_ERR(bus))
0121         return ERR_CAST(bus);
0122 
0123     return __devm_regmap_init(&i2c->dev, bus, &i2c->dev, config,
0124                   lock_key, lock_name);
0125 }
0126 EXPORT_SYMBOL_GPL(__devm_regmap_init_sccb);
0127 
0128 MODULE_LICENSE("GPL v2");