Browse Source

drivers: syscon: do not assume that a function isn't supported

When a function pointer is `NULL`, it could be that the underlying
hardware doesn't support it, or it isn't implemented.

If a function isn't supported by the hardware, its driver should
return `-ENOTSUP`, the API layer should return `-ENOSYS` when a
function isn't implemented.

Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
pull/92716/head
Yong Cong Sin 3 weeks ago committed by Dan Kalowsky
parent
commit
1967d1e699
  1. 26
      include/zephyr/drivers/syscon.h

26
include/zephyr/drivers/syscon.h

@ -71,7 +71,8 @@ __subsystem struct syscon_driver_api {
* *
* @param dev The device to get the register size for. * @param dev The device to get the register size for.
* @param addr Where to write the base address. * @param addr Where to write the base address.
* @return 0 When addr was written to. * @return 0 When @a addr was written to.
* @return -ENOSYS If the API or function isn't implemented.
*/ */
__syscall int syscon_get_base(const struct device *dev, uintptr_t *addr); __syscall int syscon_get_base(const struct device *dev, uintptr_t *addr);
@ -79,8 +80,8 @@ static inline int z_impl_syscon_get_base(const struct device *dev, uintptr_t *ad
{ {
const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api; const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
if (api == NULL) { if ((api == NULL) || (api->get_base == NULL)) {
return -ENOTSUP; return -ENOSYS;
} }
return api->get_base(dev, addr); return api->get_base(dev, addr);
@ -96,7 +97,8 @@ static inline int z_impl_syscon_get_base(const struct device *dev, uintptr_t *ad
* @param reg The register offset * @param reg The register offset
* @param val The returned value read from the syscon register * @param val The returned value read from the syscon register
* *
* @return 0 on success, negative on error * @return 0 on success.
* @return -ENOSYS If the API or function isn't implemented.
*/ */
__syscall int syscon_read_reg(const struct device *dev, uint16_t reg, uint32_t *val); __syscall int syscon_read_reg(const struct device *dev, uint16_t reg, uint32_t *val);
@ -104,8 +106,8 @@ static inline int z_impl_syscon_read_reg(const struct device *dev, uint16_t reg,
{ {
const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api; const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
if (api == NULL) { if ((api == NULL) || (api->read == NULL)) {
return -ENOTSUP; return -ENOSYS;
} }
return api->read(dev, reg, val); return api->read(dev, reg, val);
@ -121,7 +123,8 @@ static inline int z_impl_syscon_read_reg(const struct device *dev, uint16_t reg,
* @param reg The register offset * @param reg The register offset
* @param val The value to be written in the register * @param val The value to be written in the register
* *
* @return 0 on success, negative on error * @return 0 on success.
* @return -ENOSYS If the API or function isn't implemented.
*/ */
__syscall int syscon_write_reg(const struct device *dev, uint16_t reg, uint32_t val); __syscall int syscon_write_reg(const struct device *dev, uint16_t reg, uint32_t val);
@ -129,8 +132,8 @@ static inline int z_impl_syscon_write_reg(const struct device *dev, uint16_t reg
{ {
const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api; const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
if (api == NULL) { if ((api == NULL) || (api->write == NULL)) {
return -ENOTSUP; return -ENOSYS;
} }
return api->write(dev, reg, val); return api->write(dev, reg, val);
@ -142,6 +145,7 @@ static inline int z_impl_syscon_write_reg(const struct device *dev, uint16_t reg
* @param dev The device to get the register size for. * @param dev The device to get the register size for.
* @param size Pointer to write the size to. * @param size Pointer to write the size to.
* @return 0 for success. * @return 0 for success.
* @return -ENOSYS If the API or function isn't implemented.
*/ */
__syscall int syscon_get_size(const struct device *dev, size_t *size); __syscall int syscon_get_size(const struct device *dev, size_t *size);
@ -149,6 +153,10 @@ static inline int z_impl_syscon_get_size(const struct device *dev, size_t *size)
{ {
const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api; const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
if ((api == NULL) || (api->get_size == NULL)) {
return -ENOSYS;
}
return api->get_size(dev, size); return api->get_size(dev, size);
} }

Loading…
Cancel
Save