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