nvmem.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2022 Sean Anderson <sean.anderson@seco.com>
  4. */
  5. #ifndef NVMEM_H
  6. #define NVMEM_H
  7. /**
  8. * DOC: Design
  9. *
  10. * The NVMEM subsystem is a "meta-uclass" in that it abstracts over several
  11. * different uclasses all with read/write APIs. One approach to implementing
  12. * this could be to add a new sub-device for each nvmem-style device of
  13. * UCLASS_NVMEM. This subsystem has taken the approach of using the existing
  14. * access methods (i2c_eeprom_write, misc_write, etc.) directly. This has the
  15. * advantage of not requiring an extra device/driver, saving on binary size and
  16. * runtime memory usage. On the other hand, it is not idiomatic. Similar
  17. * efforts should generally use a new uclass.
  18. */
  19. /**
  20. * struct nvmem_cell - One datum within non-volatile memory
  21. * @nvmem: The backing storage device
  22. * @offset: The offset of the cell from the start of @nvmem
  23. * @size: The size of the cell, in bytes
  24. */
  25. struct nvmem_cell {
  26. struct udevice *nvmem;
  27. unsigned int offset;
  28. size_t size;
  29. };
  30. struct udevice;
  31. #if CONFIG_IS_ENABLED(NVMEM)
  32. /**
  33. * nvmem_cell_read() - Read the value of an nvmem cell
  34. * @cell: The nvmem cell to read
  35. * @buf: The buffer to read into
  36. * @size: The size of @buf
  37. *
  38. * Return:
  39. * * 0 on success
  40. * * -EINVAL if @buf is not the same size as @cell.
  41. * * -ENOSYS if CONFIG_NVMEM is disabled
  42. * * A negative error if there was a problem reading the underlying storage
  43. */
  44. int nvmem_cell_read(struct nvmem_cell *cell, void *buf, size_t size);
  45. /**
  46. * nvmem_cell_write() - Write a value to an nvmem cell
  47. * @cell: The nvmem cell to write
  48. * @buf: The buffer to write from
  49. * @size: The size of @buf
  50. *
  51. * Return:
  52. * * 0 on success
  53. * * -EINVAL if @buf is not the same size as @cell
  54. * * -ENOSYS if @cell is read-only, or if CONFIG_NVMEM is disabled
  55. * * A negative error if there was a problem writing the underlying storage
  56. */
  57. int nvmem_cell_write(struct nvmem_cell *cell, const void *buf, size_t size);
  58. /**
  59. * nvmem_cell_get_by_index() - Get an nvmem cell from a given device and index
  60. * @dev: The device that uses the nvmem cell
  61. * @index: The index of the cell in nvmem-cells
  62. * @cell: The cell to initialize
  63. *
  64. * Look up the nvmem cell referenced by the phandle at @index in nvmem-cells in
  65. * @dev.
  66. *
  67. * Return:
  68. * * 0 on success
  69. * * -EINVAL if the regs property is missing, empty, or undersized
  70. * * -ENODEV if the nvmem device is missing or unimplemented
  71. * * -ENOSYS if CONFIG_NVMEM is disabled
  72. * * A negative error if there was a problem reading nvmem-cells or getting the
  73. * device
  74. */
  75. int nvmem_cell_get_by_index(struct udevice *dev, int index,
  76. struct nvmem_cell *cell);
  77. /**
  78. * nvmem_cell_get_by_name() - Get an nvmem cell from a given device and name
  79. * @dev: The device that uses the nvmem cell
  80. * @name: The name of the nvmem cell
  81. * @cell: The cell to initialize
  82. *
  83. * Look up the nvmem cell referenced by @name in the nvmem-cell-names property
  84. * of @dev.
  85. *
  86. * Return:
  87. * * 0 on success
  88. * * -EINVAL if the regs property is missing, empty, or undersized
  89. * * -ENODEV if the nvmem device is missing or unimplemented
  90. * * -ENODATA if @name is not in nvmem-cell-names
  91. * * -ENOSYS if CONFIG_NVMEM is disabled
  92. * * A negative error if there was a problem reading nvmem-cell-names,
  93. * nvmem-cells, or getting the device
  94. */
  95. int nvmem_cell_get_by_name(struct udevice *dev, const char *name,
  96. struct nvmem_cell *cell);
  97. #else /* CONFIG_NVMEM */
  98. static inline int nvmem_cell_read(struct nvmem_cell *cell, void *buf, int size)
  99. {
  100. return -ENOSYS;
  101. }
  102. static inline int nvmem_cell_write(struct nvmem_cell *cell, const void *buf,
  103. int size)
  104. {
  105. return -ENOSYS;
  106. }
  107. static inline int nvmem_cell_get_by_index(struct udevice *dev, int index,
  108. struct nvmem_cell *cell)
  109. {
  110. return -ENOSYS;
  111. }
  112. static inline int nvmem_cell_get_by_name(struct udevice *dev, const char *name,
  113. struct nvmem_cell *cell)
  114. {
  115. return -ENOSYS;
  116. }
  117. #endif /* CONFIG_NVMEM */
  118. #endif /* NVMEM_H */