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> |
| 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 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
| 20 | static ulong str_get_num(const char *ptr, const char *maxptr) |
| 21 | { |
| 22 | if (!ptr || !maxptr) |
| 23 | return 0; |
| 24 | |
| 25 | while (!isdigit(*ptr) && ptr++ < maxptr); |
| 26 | |
| 27 | return simple_strtoul(ptr, NULL, 0); |
| 28 | } |
| 29 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame^] | 30 | int pmic_bind_children(struct udevice *pmic, int offset, |
| 31 | const struct pmic_child_info *child_info) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 32 | { |
| 33 | const struct pmic_child_info *info; |
| 34 | const void *blob = gd->fdt_blob; |
| 35 | struct driver *drv; |
| 36 | struct udevice *child; |
| 37 | const char *node_name; |
| 38 | int node_name_len; |
| 39 | int bind_count = 0; |
| 40 | int node; |
| 41 | int prefix_len; |
| 42 | int ret; |
| 43 | |
| 44 | debug("%s for '%s' at node offset: %d\n", __func__, pmic->name, |
| 45 | pmic->of_offset); |
| 46 | |
| 47 | for (node = fdt_first_subnode(blob, offset); |
| 48 | node > 0; |
| 49 | node = fdt_next_subnode(blob, node)) { |
| 50 | node_name = fdt_get_name(blob, node, &node_name_len); |
| 51 | |
| 52 | debug("* Found child node: '%s' at offset:%d\n", node_name, |
| 53 | node); |
| 54 | |
| 55 | child = NULL; |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame^] | 56 | for (info = child_info; info->prefix && info->driver; info++) { |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 57 | prefix_len = strlen(info->prefix); |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame^] | 58 | if (strncasecmp(info->prefix, node_name, prefix_len)) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 59 | continue; |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 60 | |
| 61 | debug(" - compatible prefix: '%s'\n", info->prefix); |
| 62 | |
| 63 | drv = lists_driver_lookup_name(info->driver); |
| 64 | if (!drv) { |
| 65 | debug(" - driver: '%s' not found!\n", |
| 66 | info->driver); |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | debug(" - found child driver: '%s'\n", drv->name); |
| 71 | |
| 72 | ret = device_bind(pmic, drv, node_name, NULL, |
| 73 | node, &child); |
| 74 | if (ret) { |
| 75 | debug(" - child binding error: %d\n", ret); |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | debug(" - bound child device: '%s'\n", child->name); |
| 80 | |
| 81 | child->driver_data = str_get_num(node_name + |
| 82 | prefix_len, |
| 83 | node_name + |
| 84 | node_name_len); |
| 85 | |
| 86 | debug(" - set 'child->driver_data': %lu\n", |
| 87 | child->driver_data); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | if (child) |
| 92 | bind_count++; |
| 93 | else |
| 94 | debug(" - compatible prefix not found\n"); |
| 95 | } |
| 96 | |
| 97 | debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name); |
| 98 | return bind_count; |
| 99 | } |
| 100 | |
| 101 | int pmic_get(const char *name, struct udevice **devp) |
| 102 | { |
| 103 | return uclass_get_device_by_name(UCLASS_PMIC, name, devp); |
| 104 | } |
| 105 | |
| 106 | int pmic_reg_count(struct udevice *dev) |
| 107 | { |
| 108 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame^] | 109 | |
| 110 | if (!ops || !ops->reg_count) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 111 | return -ENOSYS; |
| 112 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame^] | 113 | return ops->reg_count(dev); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len) |
| 117 | { |
| 118 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 119 | |
| 120 | if (!buffer) |
| 121 | return -EFAULT; |
| 122 | |
| 123 | if (!ops || !ops->read) |
| 124 | return -ENOSYS; |
| 125 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame^] | 126 | return ops->read(dev, reg, buffer, len); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len) |
| 130 | { |
| 131 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 132 | |
| 133 | if (!buffer) |
| 134 | return -EFAULT; |
| 135 | |
| 136 | if (!ops || !ops->write) |
| 137 | return -ENOSYS; |
| 138 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame^] | 139 | return ops->write(dev, reg, buffer, len); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | UCLASS_DRIVER(pmic) = { |
| 143 | .id = UCLASS_PMIC, |
| 144 | .name = "pmic", |
| 145 | }; |