[PATCH v1 04/11] ti948: Add support for configuration via device properties
From: Michael Drake
Date: Tue Jun 11 2019 - 10:10:13 EST
This allows the device to be configured for the board in
device tree, or in ACPI tables. The device node properties
can provide an array of register addresses and values to be
written to configure the device for the board.
The config is written to the device on probe and on PM resume.
Signed-off-by: Michael Drake <michael.drake@xxxxxxxxxxxxxxx>
Cc: Patrick Glaser <pglaser@xxxxxxxxx>
Cc: Nate Case <ncase@xxxxxxxxx>
---
drivers/gpu/drm/bridge/ti948.c | 106 ++++++++++++++++++++++++++++++++-
1 file changed, 105 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/bridge/ti948.c b/drivers/gpu/drm/bridge/ti948.c
index c22252036bbe..9cb37215f049 100644
--- a/drivers/gpu/drm/bridge/ti948.c
+++ b/drivers/gpu/drm/bridge/ti948.c
@@ -19,6 +19,7 @@
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/delay.h>
+#include <linux/slab.h>
#include <linux/i2c.h>
/* Number of times to try checking for device on bringup. */
@@ -122,6 +123,8 @@ struct ti948_reg_val {
* struct ti948_ctx - ti948 driver context
* @i2c: Handle for the device's i2c client.
* @regmap: Handle for the device's regmap.
+ * @config: Array of register values loaded from device properties.
+ * @config_len: Number of entries in config.
* @reg_names: Array of regulator names, or NULL.
* @regs: Array of regulators, or NULL.
* @reg_count: Number of entries in reg_names and regs arrays.
@@ -129,6 +132,8 @@ struct ti948_reg_val {
struct ti948_ctx {
struct i2c_client *i2c;
struct regmap *regmap;
+ struct ti948_reg_val *config;
+ size_t config_len;
const char **reg_names;
struct regulator **regs;
size_t reg_count;
@@ -212,6 +217,42 @@ static const struct regmap_config ti948_regmap_config = {
.writeable_reg = ti948_writeable_reg,
};
+static int ti948_write_sequence(
+ struct ti948_ctx *ti948,
+ const struct ti948_reg_val *sequence,
+ u32 entries)
+{
+ int i;
+
+ for (i = 0; i < entries; i++) {
+ const struct ti948_reg_val *r = sequence + i;
+ int ret = regmap_write(ti948->regmap, r->addr, r->value);
+
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int ti948_write_config_seq(struct ti948_ctx *ti948)
+{
+ int ret;
+
+ if (ti948->config == NULL) {
+ dev_info(&ti948->i2c->dev, "No config for ti948 device\n");
+ return 0;
+ }
+
+ ret = ti948_write_sequence(ti948, ti948->config, ti948->config_len);
+ if (ret < 0)
+ return ret;
+
+ dev_info(&ti948->i2c->dev, "Successfully configured ti948\n");
+
+ return ret;
+}
+
static inline u8 ti948_device_address(struct ti948_ctx *ti948)
{
return ti948->i2c->addr;
@@ -345,7 +386,7 @@ static int ti948_pm_resume(struct device *dev)
if (ret != 0)
return ret;
- return 0;
+ return ti948_write_config_seq(ti948);
}
static int ti948_pm_suspend(struct device *dev)
@@ -434,6 +475,65 @@ static int ti948_get_regulators(struct ti948_ctx *ti948)
return 0;
}
+static int ti948_get_config(struct ti948_ctx *ti948)
+{
+ int i;
+ int ret;
+ u8 *config;
+ size_t config_len;
+
+ ret = device_property_read_u8_array(&ti948->i2c->dev,
+ "config", NULL, 0);
+ if (ret == -EINVAL ||
+ ret == -ENODATA ||
+ ret == 0) {
+ /* "config" property was either:
+ * - unset
+ * - valueless
+ * - set to empty list
+ * Not an error; continue without config.
+ */
+ dev_info(&ti948->i2c->dev, "No config defined for device.\n");
+ return 0;
+
+ } else if (ret < 0) {
+ return ret;
+ } else if (ret & 0x1) {
+ dev_err(&ti948->i2c->dev,
+ "Device property 'config' needs even entry count.\n");
+ return -EINVAL;
+ }
+
+ config_len = ret;
+
+ config = kmalloc_array(config_len, sizeof(*config), GFP_KERNEL);
+ if (!config)
+ return -ENOMEM;
+
+ ret = device_property_read_u8_array(&ti948->i2c->dev, "config",
+ config, config_len);
+ if (ret < 0) {
+ kfree(config);
+ return ret;
+ }
+
+ ti948->config = devm_kmalloc_array(&ti948->i2c->dev,
+ config_len / 2, sizeof(*ti948->config), GFP_KERNEL);
+ if (!ti948->config) {
+ kfree(config);
+ return -ENOMEM;
+ }
+
+ ti948->config_len = config_len / 2;
+ for (i = 0; i < config_len; i += 2) {
+ ti948->config[i / 2].addr = config[i];
+ ti948->config[i / 2].value = config[i + 1];
+ }
+ kfree(config);
+
+ return 0;
+}
+
static int ti948_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
@@ -456,6 +556,10 @@ static int ti948_probe(struct i2c_client *client,
if (ret != 0)
return ret;
+ ret = ti948_get_config(ti948);
+ if (ret != 0)
+ return ret;
+
i2c_set_clientdata(client, ti948);
ret = ti948_pm_resume(&client->dev);
--
2.20.1