Linux 5.1.15
[linux/fpc-iii.git] / drivers / base / regmap / regmap-sccb.c
blob597042e2d0094887519c330bc11de21a8bc53fe7
1 // SPDX-License-Identifier: GPL-2.0
2 // Register map access API - SCCB support
4 #include <linux/i2c.h>
5 #include <linux/module.h>
6 #include <linux/regmap.h>
8 #include "internal.h"
10 /**
11 * sccb_is_available - Check if the adapter supports SCCB protocol
12 * @adap: I2C adapter
14 * Return true if the I2C adapter is capable of using SCCB helper functions,
15 * false otherwise.
17 static bool sccb_is_available(struct i2c_adapter *adap)
19 u32 needed_funcs = I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
22 * If we ever want support for hardware doing SCCB natively, we will
23 * introduce a sccb_xfer() callback to struct i2c_algorithm and check
24 * for it here.
27 return (i2c_get_functionality(adap) & needed_funcs) == needed_funcs;
30 /**
31 * regmap_sccb_read - Read data from SCCB slave device
32 * @context: Device that will be interacted with
33 * @reg: Register to be read from
34 * @val: Pointer to store read value
36 * This executes the 2-phase write transmission cycle that is followed by a
37 * 2-phase read transmission cycle, returning negative errno else zero on
38 * success.
40 static int regmap_sccb_read(void *context, unsigned int reg, unsigned int *val)
42 struct device *dev = context;
43 struct i2c_client *i2c = to_i2c_client(dev);
44 int ret;
45 union i2c_smbus_data data;
47 i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
49 ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
50 I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE, NULL);
51 if (ret < 0)
52 goto out;
54 ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags,
55 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
56 if (ret < 0)
57 goto out;
59 *val = data.byte;
60 out:
61 i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
63 return ret;
66 /**
67 * regmap_sccb_write - Write data to SCCB slave device
68 * @context: Device that will be interacted with
69 * @reg: Register to write to
70 * @val: Value to be written
72 * This executes the SCCB 3-phase write transmission cycle, returning negative
73 * errno else zero on success.
75 static int regmap_sccb_write(void *context, unsigned int reg, unsigned int val)
77 struct device *dev = context;
78 struct i2c_client *i2c = to_i2c_client(dev);
80 return i2c_smbus_write_byte_data(i2c, reg, val);
83 static struct regmap_bus regmap_sccb_bus = {
84 .reg_write = regmap_sccb_write,
85 .reg_read = regmap_sccb_read,
88 static const struct regmap_bus *regmap_get_sccb_bus(struct i2c_client *i2c,
89 const struct regmap_config *config)
91 if (config->val_bits == 8 && config->reg_bits == 8 &&
92 sccb_is_available(i2c->adapter))
93 return &regmap_sccb_bus;
95 return ERR_PTR(-ENOTSUPP);
98 struct regmap *__regmap_init_sccb(struct i2c_client *i2c,
99 const struct regmap_config *config,
100 struct lock_class_key *lock_key,
101 const char *lock_name)
103 const struct regmap_bus *bus = regmap_get_sccb_bus(i2c, config);
105 if (IS_ERR(bus))
106 return ERR_CAST(bus);
108 return __regmap_init(&i2c->dev, bus, &i2c->dev, config,
109 lock_key, lock_name);
111 EXPORT_SYMBOL_GPL(__regmap_init_sccb);
113 struct regmap *__devm_regmap_init_sccb(struct i2c_client *i2c,
114 const struct regmap_config *config,
115 struct lock_class_key *lock_key,
116 const char *lock_name)
118 const struct regmap_bus *bus = regmap_get_sccb_bus(i2c, config);
120 if (IS_ERR(bus))
121 return ERR_CAST(bus);
123 return __devm_regmap_init(&i2c->dev, bus, &i2c->dev, config,
124 lock_key, lock_name);
126 EXPORT_SYMBOL_GPL(__devm_regmap_init_sccb);
128 MODULE_LICENSE("GPL v2");