Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * property.c - Unified device property interface. |
| 3 | * |
| 4 | * Copyright (C) 2014, Intel Corporation |
| 5 | * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com> |
| 6 | * Mika Westerberg <mika.westerberg@linux.intel.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/property.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/acpi.h> |
| 16 | #include <linux/of.h> |
| 17 | |
| 18 | /** |
| 19 | * device_property_present - check if a property of a device is present |
| 20 | * @dev: Device whose property is being checked |
| 21 | * @propname: Name of the property |
| 22 | * |
| 23 | * Check if property @propname is present in the device firmware description. |
| 24 | */ |
| 25 | bool device_property_present(struct device *dev, const char *propname) |
| 26 | { |
| 27 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) |
| 28 | return of_property_read_bool(dev->of_node, propname); |
| 29 | |
| 30 | return !acpi_dev_prop_get(ACPI_COMPANION(dev), propname, NULL); |
| 31 | } |
| 32 | EXPORT_SYMBOL_GPL(device_property_present); |
| 33 | |
| 34 | #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \ |
| 35 | (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \ |
| 36 | : of_property_count_elems_of_size((node), (propname), sizeof(type)) |
| 37 | |
| 38 | #define DEV_PROP_READ_ARRAY(_dev_, _propname_, _type_, _proptype_, _val_, _nval_) \ |
| 39 | IS_ENABLED(CONFIG_OF) && _dev_->of_node ? \ |
| 40 | (OF_DEV_PROP_READ_ARRAY(_dev_->of_node, _propname_, _type_, \ |
| 41 | _val_, _nval_)) : \ |
| 42 | acpi_dev_prop_read(ACPI_COMPANION(_dev_), _propname_, \ |
| 43 | _proptype_, _val_, _nval_) |
| 44 | |
| 45 | /** |
| 46 | * device_property_read_u8_array - return a u8 array property of a device |
| 47 | * @dev: Device to get the property of |
| 48 | * @propname: Name of the property |
| 49 | * @val: The values are stored here |
| 50 | * @nval: Size of the @val array |
| 51 | * |
| 52 | * Function reads an array of u8 properties with @propname from the device |
| 53 | * firmware description and stores them to @val if found. |
| 54 | * |
| 55 | * Return: %0 if the property was found (success), |
| 56 | * %-EINVAL if given arguments are not valid, |
| 57 | * %-ENODATA if the property does not have a value, |
| 58 | * %-EPROTO if the property is not an array of numbers, |
| 59 | * %-EOVERFLOW if the size of the property is not as expected. |
| 60 | */ |
| 61 | int device_property_read_u8_array(struct device *dev, const char *propname, |
| 62 | u8 *val, size_t nval) |
| 63 | { |
| 64 | return DEV_PROP_READ_ARRAY(dev, propname, u8, DEV_PROP_U8, val, nval); |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(device_property_read_u8_array); |
| 67 | |
| 68 | /** |
| 69 | * device_property_read_u16_array - return a u16 array property of a device |
| 70 | * @dev: Device to get the property of |
| 71 | * @propname: Name of the property |
| 72 | * @val: The values are stored here |
| 73 | * @nval: Size of the @val array |
| 74 | * |
| 75 | * Function reads an array of u16 properties with @propname from the device |
| 76 | * firmware description and stores them to @val if found. |
| 77 | * |
| 78 | * Return: %0 if the property was found (success), |
| 79 | * %-EINVAL if given arguments are not valid, |
| 80 | * %-ENODATA if the property does not have a value, |
| 81 | * %-EPROTO if the property is not an array of numbers, |
| 82 | * %-EOVERFLOW if the size of the property is not as expected. |
| 83 | */ |
| 84 | int device_property_read_u16_array(struct device *dev, const char *propname, |
| 85 | u16 *val, size_t nval) |
| 86 | { |
| 87 | return DEV_PROP_READ_ARRAY(dev, propname, u16, DEV_PROP_U16, val, nval); |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(device_property_read_u16_array); |
| 90 | |
| 91 | /** |
| 92 | * device_property_read_u32_array - return a u32 array property of a device |
| 93 | * @dev: Device to get the property of |
| 94 | * @propname: Name of the property |
| 95 | * @val: The values are stored here |
| 96 | * @nval: Size of the @val array |
| 97 | * |
| 98 | * Function reads an array of u32 properties with @propname from the device |
| 99 | * firmware description and stores them to @val if found. |
| 100 | * |
| 101 | * Return: %0 if the property was found (success), |
| 102 | * %-EINVAL if given arguments are not valid, |
| 103 | * %-ENODATA if the property does not have a value, |
| 104 | * %-EPROTO if the property is not an array of numbers, |
| 105 | * %-EOVERFLOW if the size of the property is not as expected. |
| 106 | */ |
| 107 | int device_property_read_u32_array(struct device *dev, const char *propname, |
| 108 | u32 *val, size_t nval) |
| 109 | { |
| 110 | return DEV_PROP_READ_ARRAY(dev, propname, u32, DEV_PROP_U32, val, nval); |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(device_property_read_u32_array); |
| 113 | |
| 114 | /** |
| 115 | * device_property_read_u64_array - return a u64 array property of a device |
| 116 | * @dev: Device to get the property of |
| 117 | * @propname: Name of the property |
| 118 | * @val: The values are stored here |
| 119 | * @nval: Size of the @val array |
| 120 | * |
| 121 | * Function reads an array of u64 properties with @propname from the device |
| 122 | * firmware description and stores them to @val if found. |
| 123 | * |
| 124 | * Return: %0 if the property was found (success), |
| 125 | * %-EINVAL if given arguments are not valid, |
| 126 | * %-ENODATA if the property does not have a value, |
| 127 | * %-EPROTO if the property is not an array of numbers, |
| 128 | * %-EOVERFLOW if the size of the property is not as expected. |
| 129 | */ |
| 130 | int device_property_read_u64_array(struct device *dev, const char *propname, |
| 131 | u64 *val, size_t nval) |
| 132 | { |
| 133 | return DEV_PROP_READ_ARRAY(dev, propname, u64, DEV_PROP_U64, val, nval); |
| 134 | } |
| 135 | EXPORT_SYMBOL_GPL(device_property_read_u64_array); |
| 136 | |
| 137 | /** |
| 138 | * device_property_read_string_array - return a string array property of device |
| 139 | * @dev: Device to get the property of |
| 140 | * @propname: Name of the property |
| 141 | * @val: The values are stored here |
| 142 | * @nval: Size of the @val array |
| 143 | * |
| 144 | * Function reads an array of string properties with @propname from the device |
| 145 | * firmware description and stores them to @val if found. |
| 146 | * |
| 147 | * Return: %0 if the property was found (success), |
| 148 | * %-EINVAL if given arguments are not valid, |
| 149 | * %-ENODATA if the property does not have a value, |
| 150 | * %-EPROTO or %-EILSEQ if the property is not an array of strings, |
| 151 | * %-EOVERFLOW if the size of the property is not as expected. |
| 152 | */ |
| 153 | int device_property_read_string_array(struct device *dev, const char *propname, |
| 154 | const char **val, size_t nval) |
| 155 | { |
| 156 | return IS_ENABLED(CONFIG_OF) && dev->of_node ? |
| 157 | of_property_read_string_array(dev->of_node, propname, val, nval) : |
| 158 | acpi_dev_prop_read(ACPI_COMPANION(dev), propname, |
| 159 | DEV_PROP_STRING, val, nval); |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(device_property_read_string_array); |
| 162 | |
| 163 | /** |
| 164 | * device_property_read_string - return a string property of a device |
| 165 | * @dev: Device to get the property of |
| 166 | * @propname: Name of the property |
| 167 | * @val: The value is stored here |
| 168 | * |
| 169 | * Function reads property @propname from the device firmware description and |
| 170 | * stores the value into @val if found. The value is checked to be a string. |
| 171 | * |
| 172 | * Return: %0 if the property was found (success), |
| 173 | * %-EINVAL if given arguments are not valid, |
| 174 | * %-ENODATA if the property does not have a value, |
| 175 | * %-EPROTO or %-EILSEQ if the property type is not a string. |
| 176 | */ |
| 177 | int device_property_read_string(struct device *dev, const char *propname, |
| 178 | const char **val) |
| 179 | { |
| 180 | return IS_ENABLED(CONFIG_OF) && dev->of_node ? |
| 181 | of_property_read_string(dev->of_node, propname, val) : |
| 182 | acpi_dev_prop_read(ACPI_COMPANION(dev), propname, |
| 183 | DEV_PROP_STRING, val, 1); |
| 184 | } |
| 185 | EXPORT_SYMBOL_GPL(device_property_read_string); |