Przemyslaw Marczak | 4d9057e | 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 | e15bb3e | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 12 | #include <vsprintf.h> |
Przemyslaw Marczak | 4d9057e | 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 | 1f2b4b0 | 2016-01-21 19:43:55 -0700 | [diff] [blame] | 21 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 22 | int pmic_bind_children(struct udevice *pmic, int offset, |
| 23 | const struct pmic_child_info *child_info) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 24 | { |
| 25 | const struct pmic_child_info *info; |
| 26 | const void *blob = gd->fdt_blob; |
| 27 | struct driver *drv; |
| 28 | struct udevice *child; |
| 29 | const char *node_name; |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 30 | int bind_count = 0; |
| 31 | int node; |
| 32 | int prefix_len; |
| 33 | int ret; |
| 34 | |
| 35 | debug("%s for '%s' at node offset: %d\n", __func__, pmic->name, |
| 36 | pmic->of_offset); |
| 37 | |
| 38 | for (node = fdt_first_subnode(blob, offset); |
| 39 | node > 0; |
| 40 | node = fdt_next_subnode(blob, node)) { |
Simon Glass | e15bb3e | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 41 | node_name = fdt_get_name(blob, node, NULL); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 42 | |
| 43 | debug("* Found child node: '%s' at offset:%d\n", node_name, |
| 44 | node); |
| 45 | |
| 46 | child = NULL; |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 47 | for (info = child_info; info->prefix && info->driver; info++) { |
Simon Glass | e15bb3e | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 48 | debug(" - compatible prefix: '%s'\n", info->prefix); |
| 49 | |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 50 | prefix_len = strlen(info->prefix); |
Simon Glass | 23ec2b5 | 2015-06-23 15:38:56 -0600 | [diff] [blame] | 51 | if (strncmp(info->prefix, node_name, prefix_len)) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 52 | continue; |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 53 | |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 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(pmic, drv, node_name, NULL, |
| 64 | 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 | |
Simon Glass | e15bb3e | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 72 | child->driver_data = trailing_strtol(node_name); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 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 childs for PMIC: '%s'\n", bind_count, pmic->name); |
| 86 | return bind_count; |
| 87 | } |
Simon Glass | 1f2b4b0 | 2016-01-21 19:43:55 -0700 | [diff] [blame] | 88 | #endif |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 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); |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 98 | |
| 99 | if (!ops || !ops->reg_count) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 100 | return -ENOSYS; |
| 101 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 102 | return ops->reg_count(dev); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 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); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 108 | |
| 109 | if (!buffer) |
| 110 | return -EFAULT; |
| 111 | |
| 112 | if (!ops || !ops->read) |
| 113 | return -ENOSYS; |
| 114 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 115 | return ops->read(dev, reg, buffer, len); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 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); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 121 | |
| 122 | if (!buffer) |
| 123 | return -EFAULT; |
| 124 | |
| 125 | if (!ops || !ops->write) |
| 126 | return -ENOSYS; |
| 127 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 128 | return ops->write(dev, reg, buffer, len); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Simon Glass | 6c69c7f | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 131 | int pmic_reg_read(struct udevice *dev, uint reg) |
| 132 | { |
| 133 | u8 byte; |
| 134 | int ret; |
| 135 | |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 136 | debug("%s: reg=%x", __func__, reg); |
Simon Glass | 6c69c7f | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 137 | ret = pmic_read(dev, reg, &byte, 1); |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 138 | debug(", value=%x, ret=%d\n", byte, ret); |
Simon Glass | 6c69c7f | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 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; |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 146 | int ret; |
Simon Glass | 6c69c7f | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 147 | |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 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; |
Simon Glass | 6c69c7f | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 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 | |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 168 | UCLASS_DRIVER(pmic) = { |
| 169 | .id = UCLASS_PMIC, |
| 170 | .name = "pmic", |
| 171 | }; |