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 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 21 | int pmic_bind_children(struct udevice *pmic, int offset, |
| 22 | const struct pmic_child_info *child_info) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 23 | { |
| 24 | const struct pmic_child_info *info; |
| 25 | const void *blob = gd->fdt_blob; |
| 26 | struct driver *drv; |
| 27 | struct udevice *child; |
| 28 | const char *node_name; |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 29 | int bind_count = 0; |
| 30 | int node; |
| 31 | int prefix_len; |
| 32 | int ret; |
| 33 | |
| 34 | debug("%s for '%s' at node offset: %d\n", __func__, pmic->name, |
| 35 | pmic->of_offset); |
| 36 | |
| 37 | for (node = fdt_first_subnode(blob, offset); |
| 38 | node > 0; |
| 39 | node = fdt_next_subnode(blob, node)) { |
Simon Glass | e15bb3e | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 40 | node_name = fdt_get_name(blob, node, NULL); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 41 | |
| 42 | debug("* Found child node: '%s' at offset:%d\n", node_name, |
| 43 | node); |
| 44 | |
| 45 | child = NULL; |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 46 | for (info = child_info; info->prefix && info->driver; info++) { |
Simon Glass | e15bb3e | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 47 | debug(" - compatible prefix: '%s'\n", info->prefix); |
| 48 | |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 49 | prefix_len = strlen(info->prefix); |
Simon Glass | 23ec2b5 | 2015-06-23 15:38:56 -0600 | [diff] [blame] | 50 | if (strncmp(info->prefix, node_name, prefix_len)) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 51 | continue; |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 52 | |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 53 | drv = lists_driver_lookup_name(info->driver); |
| 54 | if (!drv) { |
| 55 | debug(" - driver: '%s' not found!\n", |
| 56 | info->driver); |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | debug(" - found child driver: '%s'\n", drv->name); |
| 61 | |
| 62 | ret = device_bind(pmic, drv, node_name, NULL, |
| 63 | node, &child); |
| 64 | if (ret) { |
| 65 | debug(" - child binding error: %d\n", ret); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | debug(" - bound child device: '%s'\n", child->name); |
| 70 | |
Simon Glass | e15bb3e | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 71 | child->driver_data = trailing_strtol(node_name); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 72 | |
| 73 | debug(" - set 'child->driver_data': %lu\n", |
| 74 | child->driver_data); |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | if (child) |
| 79 | bind_count++; |
| 80 | else |
| 81 | debug(" - compatible prefix not found\n"); |
| 82 | } |
| 83 | |
| 84 | debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name); |
| 85 | return bind_count; |
| 86 | } |
| 87 | |
| 88 | int pmic_get(const char *name, struct udevice **devp) |
| 89 | { |
| 90 | return uclass_get_device_by_name(UCLASS_PMIC, name, devp); |
| 91 | } |
| 92 | |
| 93 | int pmic_reg_count(struct udevice *dev) |
| 94 | { |
| 95 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 96 | |
| 97 | if (!ops || !ops->reg_count) |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 98 | return -ENOSYS; |
| 99 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 100 | return ops->reg_count(dev); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len) |
| 104 | { |
| 105 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 106 | |
| 107 | if (!buffer) |
| 108 | return -EFAULT; |
| 109 | |
| 110 | if (!ops || !ops->read) |
| 111 | return -ENOSYS; |
| 112 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 113 | return ops->read(dev, reg, buffer, len); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int pmic_write(struct udevice *dev, uint reg, const 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->write) |
| 124 | return -ENOSYS; |
| 125 | |
Przemyslaw Marczak | f415a3e | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 126 | return ops->write(dev, reg, buffer, len); |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Simon Glass | 6c69c7f | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 129 | int pmic_reg_read(struct udevice *dev, uint reg) |
| 130 | { |
| 131 | u8 byte; |
| 132 | int ret; |
| 133 | |
| 134 | ret = pmic_read(dev, reg, &byte, 1); |
| 135 | debug("%s: reg=%x, value=%x\n", __func__, reg, byte); |
| 136 | |
| 137 | return ret ? ret : byte; |
| 138 | } |
| 139 | |
| 140 | int pmic_reg_write(struct udevice *dev, uint reg, uint value) |
| 141 | { |
| 142 | u8 byte = value; |
| 143 | |
| 144 | debug("%s: reg=%x, value=%x\n", __func__, reg, value); |
Simon Glass | cd367d8 | 2015-07-02 18:16:03 -0600 | [diff] [blame^] | 145 | return pmic_write(dev, reg, &byte, 1); |
Simon Glass | 6c69c7f | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set) |
| 149 | { |
| 150 | u8 byte; |
| 151 | int ret; |
| 152 | |
| 153 | ret = pmic_reg_read(dev, reg); |
| 154 | if (ret < 0) |
| 155 | return ret; |
| 156 | byte = (ret & ~clr) | set; |
| 157 | |
| 158 | return pmic_reg_write(dev, reg, byte); |
| 159 | } |
| 160 | |
Przemyslaw Marczak | 4d9057e | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 161 | UCLASS_DRIVER(pmic) = { |
| 162 | .id = UCLASS_PMIC, |
| 163 | .name = "pmic", |
| 164 | }; |