2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-04-20 18:07:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014-2015 Samsung Electronics
|
|
|
|
* Przemyslaw Marczak <p.marczak@samsung.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <fdtdec.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dm.h>
|
2015-06-23 21:39:16 +00:00
|
|
|
#include <vsprintf.h>
|
2015-04-20 18:07:41 +00:00
|
|
|
#include <dm/lists.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/uclass-internal.h>
|
|
|
|
#include <power/pmic.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
|
2016-01-22 02:43:55 +00:00
|
|
|
#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
|
2017-05-19 02:09:32 +00:00
|
|
|
int pmic_bind_children(struct udevice *pmic, ofnode parent,
|
2015-05-13 11:38:26 +00:00
|
|
|
const struct pmic_child_info *child_info)
|
2015-04-20 18:07:41 +00:00
|
|
|
{
|
|
|
|
const struct pmic_child_info *info;
|
|
|
|
struct driver *drv;
|
|
|
|
struct udevice *child;
|
|
|
|
const char *node_name;
|
2017-11-27 08:14:16 +00:00
|
|
|
const char *reg_name;
|
2015-04-20 18:07:41 +00:00
|
|
|
int bind_count = 0;
|
2017-05-19 02:09:32 +00:00
|
|
|
ofnode node;
|
2015-04-20 18:07:41 +00:00
|
|
|
int prefix_len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
|
2017-01-17 23:52:55 +00:00
|
|
|
dev_of_offset(pmic));
|
2015-04-20 18:07:41 +00:00
|
|
|
|
2017-08-05 21:45:54 +00:00
|
|
|
ofnode_for_each_subnode(node, parent) {
|
2017-05-19 02:09:32 +00:00
|
|
|
node_name = ofnode_get_name(node);
|
2015-04-20 18:07:41 +00:00
|
|
|
|
2017-05-19 02:09:32 +00:00
|
|
|
debug("* Found child node: '%s'\n", node_name);
|
2015-04-20 18:07:41 +00:00
|
|
|
|
|
|
|
child = NULL;
|
2015-05-13 11:38:26 +00:00
|
|
|
for (info = child_info; info->prefix && info->driver; info++) {
|
2015-06-23 21:39:16 +00:00
|
|
|
debug(" - compatible prefix: '%s'\n", info->prefix);
|
|
|
|
|
2015-04-20 18:07:41 +00:00
|
|
|
prefix_len = strlen(info->prefix);
|
2017-11-27 08:14:16 +00:00
|
|
|
if (strncmp(info->prefix, node_name, prefix_len)) {
|
|
|
|
reg_name = ofnode_read_string(node,
|
|
|
|
"regulator-name");
|
|
|
|
if (!reg_name)
|
|
|
|
continue;
|
|
|
|
if (strncmp(info->prefix, reg_name, prefix_len))
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-20 18:07:41 +00:00
|
|
|
|
|
|
|
drv = lists_driver_lookup_name(info->driver);
|
|
|
|
if (!drv) {
|
|
|
|
debug(" - driver: '%s' not found!\n",
|
|
|
|
info->driver);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(" - found child driver: '%s'\n", drv->name);
|
|
|
|
|
2017-05-19 02:09:32 +00:00
|
|
|
ret = device_bind_with_driver_data(pmic, drv, node_name,
|
|
|
|
0, node, &child);
|
2015-04-20 18:07:41 +00:00
|
|
|
if (ret) {
|
|
|
|
debug(" - child binding error: %d\n", ret);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(" - bound child device: '%s'\n", child->name);
|
|
|
|
|
2015-06-23 21:39:16 +00:00
|
|
|
child->driver_data = trailing_strtol(node_name);
|
2015-04-20 18:07:41 +00:00
|
|
|
|
|
|
|
debug(" - set 'child->driver_data': %lu\n",
|
|
|
|
child->driver_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child)
|
|
|
|
bind_count++;
|
|
|
|
else
|
|
|
|
debug(" - compatible prefix not found\n");
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:09:32 +00:00
|
|
|
debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name);
|
2015-04-20 18:07:41 +00:00
|
|
|
return bind_count;
|
|
|
|
}
|
2016-01-22 02:43:55 +00:00
|
|
|
#endif
|
2015-04-20 18:07:41 +00:00
|
|
|
|
|
|
|
int pmic_get(const char *name, struct udevice **devp)
|
|
|
|
{
|
|
|
|
return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_reg_count(struct udevice *dev)
|
|
|
|
{
|
|
|
|
const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
|
2015-05-13 11:38:26 +00:00
|
|
|
|
|
|
|
if (!ops || !ops->reg_count)
|
2015-04-20 18:07:41 +00:00
|
|
|
return -ENOSYS;
|
|
|
|
|
2015-05-13 11:38:26 +00:00
|
|
|
return ops->reg_count(dev);
|
2015-04-20 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
|
|
|
|
{
|
|
|
|
const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (!ops || !ops->read)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
2015-05-13 11:38:26 +00:00
|
|
|
return ops->read(dev, reg, buffer, len);
|
2015-04-20 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
|
|
|
|
{
|
|
|
|
const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (!ops || !ops->write)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
2015-05-13 11:38:26 +00:00
|
|
|
return ops->write(dev, reg, buffer, len);
|
2015-04-20 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 21:39:01 +00:00
|
|
|
int pmic_reg_read(struct udevice *dev, uint reg)
|
|
|
|
{
|
2018-05-15 14:26:36 +00:00
|
|
|
struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
|
|
|
|
u32 val = 0;
|
2015-06-23 21:39:01 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-05-15 14:26:36 +00:00
|
|
|
if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
|
|
|
|
debug("Wrong transmission size [%d]\n", priv->trans_len);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s: reg=%x priv->trans_len:%d", __func__, reg, priv->trans_len);
|
|
|
|
ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
|
|
|
|
debug(", value=%x, ret=%d\n", val, ret);
|
2015-06-23 21:39:01 +00:00
|
|
|
|
2018-05-15 14:26:36 +00:00
|
|
|
return ret ? ret : val;
|
2015-06-23 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_reg_write(struct udevice *dev, uint reg, uint value)
|
|
|
|
{
|
2018-05-15 14:26:36 +00:00
|
|
|
struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
|
2016-01-22 02:43:58 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-05-15 14:26:36 +00:00
|
|
|
if (priv->trans_len < 1 || priv->trans_len > sizeof(value)) {
|
|
|
|
debug("Wrong transmission size [%d]\n", priv->trans_len);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s: reg=%x, value=%x priv->trans_len:%d", __func__, reg, value,
|
|
|
|
priv->trans_len);
|
|
|
|
ret = pmic_write(dev, reg, (uint8_t *)&value, priv->trans_len);
|
2016-01-22 02:43:58 +00:00
|
|
|
debug(", ret=%d\n", ret);
|
2015-06-23 21:39:01 +00:00
|
|
|
|
2016-01-22 02:43:58 +00:00
|
|
|
return ret;
|
2015-06-23 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
|
|
|
|
{
|
2018-05-15 14:26:36 +00:00
|
|
|
struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
|
|
|
|
u32 val = 0;
|
2015-06-23 21:39:01 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-05-15 14:26:36 +00:00
|
|
|
if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
|
|
|
|
debug("Wrong transmission size [%d]\n", priv->trans_len);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
|
2015-06-23 21:39:01 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2018-05-15 14:26:36 +00:00
|
|
|
val = (val & ~clr) | set;
|
|
|
|
return pmic_write(dev, reg, (uint8_t *)&val, priv->trans_len);
|
2015-06-23 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 14:26:35 +00:00
|
|
|
static int pmic_pre_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct uc_pmic_priv *pmic_priv = dev_get_uclass_priv(dev);
|
|
|
|
|
|
|
|
pmic_priv->trans_len = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-20 18:07:41 +00:00
|
|
|
UCLASS_DRIVER(pmic) = {
|
|
|
|
.id = UCLASS_PMIC,
|
|
|
|
.name = "pmic",
|
2018-05-15 14:26:35 +00:00
|
|
|
.pre_probe = pmic_pre_probe,
|
|
|
|
.per_device_auto_alloc_size = sizeof(struct uc_pmic_priv),
|
2015-04-20 18:07:41 +00:00
|
|
|
};
|