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