1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2014-2015 Samsung Electronics 4 * Przemyslaw Marczak <p.marczak@samsung.com> 5 */ 6 7 #include <common.h> 8 #include <fdtdec.h> 9 #include <errno.h> 10 #include <dm.h> 11 #include <vsprintf.h> 12 #include <dm/lists.h> 13 #include <dm/device-internal.h> 14 #include <dm/uclass-internal.h> 15 #include <power/pmic.h> 16 #include <linux/ctype.h> 17 18 #if CONFIG_IS_ENABLED(PMIC_CHILDREN) 19 int pmic_bind_children(struct udevice *pmic, ofnode parent, 20 const struct pmic_child_info *child_info) 21 { 22 const struct pmic_child_info *info; 23 struct driver *drv; 24 struct udevice *child; 25 const char *node_name; 26 const char *reg_name; 27 int bind_count = 0; 28 ofnode node; 29 int prefix_len; 30 int ret; 31 32 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name, 33 dev_of_offset(pmic)); 34 35 ofnode_for_each_subnode(node, parent) { 36 node_name = ofnode_get_name(node); 37 38 debug("* Found child node: '%s'\n", node_name); 39 40 child = NULL; 41 for (info = child_info; info->prefix && info->driver; info++) { 42 debug(" - compatible prefix: '%s'\n", info->prefix); 43 44 prefix_len = strlen(info->prefix); 45 if (strncmp(info->prefix, node_name, prefix_len)) { 46 reg_name = ofnode_read_string(node, 47 "regulator-name"); 48 if (!reg_name) 49 continue; 50 if (strncmp(info->prefix, reg_name, prefix_len)) 51 continue; 52 } 53 54 drv = lists_driver_lookup_name(info->driver); 55 if (!drv) { 56 debug(" - driver: '%s' not found!\n", 57 info->driver); 58 continue; 59 } 60 61 debug(" - found child driver: '%s'\n", drv->name); 62 63 ret = device_bind_with_driver_data(pmic, drv, node_name, 64 0, node, &child); 65 if (ret) { 66 debug(" - child binding error: %d\n", ret); 67 continue; 68 } 69 70 debug(" - bound child device: '%s'\n", child->name); 71 72 child->driver_data = trailing_strtol(node_name); 73 74 debug(" - set 'child->driver_data': %lu\n", 75 child->driver_data); 76 break; 77 } 78 79 if (child) 80 bind_count++; 81 else 82 debug(" - compatible prefix not found\n"); 83 } 84 85 debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name); 86 return bind_count; 87 } 88 #endif 89 90 int pmic_get(const char *name, struct udevice **devp) 91 { 92 return uclass_get_device_by_name(UCLASS_PMIC, name, devp); 93 } 94 95 int pmic_reg_count(struct udevice *dev) 96 { 97 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); 98 99 if (!ops || !ops->reg_count) 100 return -ENOSYS; 101 102 return ops->reg_count(dev); 103 } 104 105 int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len) 106 { 107 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); 108 109 if (!buffer) 110 return -EFAULT; 111 112 if (!ops || !ops->read) 113 return -ENOSYS; 114 115 return ops->read(dev, reg, buffer, len); 116 } 117 118 int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len) 119 { 120 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); 121 122 if (!buffer) 123 return -EFAULT; 124 125 if (!ops || !ops->write) 126 return -ENOSYS; 127 128 return ops->write(dev, reg, buffer, len); 129 } 130 131 int pmic_reg_read(struct udevice *dev, uint reg) 132 { 133 u8 byte; 134 int ret; 135 136 debug("%s: reg=%x", __func__, reg); 137 ret = pmic_read(dev, reg, &byte, 1); 138 debug(", value=%x, ret=%d\n", byte, ret); 139 140 return ret ? ret : byte; 141 } 142 143 int pmic_reg_write(struct udevice *dev, uint reg, uint value) 144 { 145 u8 byte = value; 146 int ret; 147 148 debug("%s: reg=%x, value=%x", __func__, reg, value); 149 ret = pmic_write(dev, reg, &byte, 1); 150 debug(", ret=%d\n", ret); 151 152 return ret; 153 } 154 155 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set) 156 { 157 u8 byte; 158 int ret; 159 160 ret = pmic_reg_read(dev, reg); 161 if (ret < 0) 162 return ret; 163 byte = (ret & ~clr) | set; 164 165 return pmic_reg_write(dev, reg, byte); 166 } 167 168 UCLASS_DRIVER(pmic) = { 169 .id = UCLASS_PMIC, 170 .name = "pmic", 171 }; 172