nvmem.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. NVMEM SUBSYSTEM
  2. Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  3. This document explains the NVMEM Framework along with the APIs provided,
  4. and how to use it.
  5. 1. Introduction
  6. ===============
  7. *NVMEM* is the abbreviation for Non Volatile Memory layer. It is used to
  8. retrieve configuration of SOC or Device specific data from non volatile
  9. memories like eeprom, efuses and so on.
  10. Before this framework existed, NVMEM drivers like eeprom were stored in
  11. drivers/misc, where they all had to duplicate pretty much the same code to
  12. register a sysfs file, allow in-kernel users to access the content of the
  13. devices they were driving, etc.
  14. This was also a problem as far as other in-kernel users were involved, since
  15. the solutions used were pretty much different from one driver to another, there
  16. was a rather big abstraction leak.
  17. This framework aims at solve these problems. It also introduces DT
  18. representation for consumer devices to go get the data they require (MAC
  19. Addresses, SoC/Revision ID, part numbers, and so on) from the NVMEMs. This
  20. framework is based on regmap, so that most of the abstraction available in
  21. regmap can be reused, across multiple types of buses.
  22. NVMEM Providers
  23. +++++++++++++++
  24. NVMEM provider refers to an entity that implements methods to initialize, read
  25. and write the non-volatile memory.
  26. 2. Registering/Unregistering the NVMEM provider
  27. ===============================================
  28. A NVMEM provider can register with NVMEM core by supplying relevant
  29. nvmem configuration to nvmem_register(), on success core would return a valid
  30. nvmem_device pointer.
  31. nvmem_unregister(nvmem) is used to unregister a previously registered provider.
  32. For example, a simple qfprom case:
  33. static struct nvmem_config econfig = {
  34. .name = "qfprom",
  35. .owner = THIS_MODULE,
  36. };
  37. static int qfprom_probe(struct platform_device *pdev)
  38. {
  39. ...
  40. econfig.dev = &pdev->dev;
  41. nvmem = nvmem_register(&econfig);
  42. ...
  43. }
  44. It is mandatory that the NVMEM provider has a regmap associated with its
  45. struct device. Failure to do would return error code from nvmem_register().
  46. NVMEM Consumers
  47. +++++++++++++++
  48. NVMEM consumers are the entities which make use of the NVMEM provider to
  49. read from and to NVMEM.
  50. 3. NVMEM cell based consumer APIs
  51. =================================
  52. NVMEM cells are the data entries/fields in the NVMEM.
  53. The NVMEM framework provides 3 APIs to read/write NVMEM cells.
  54. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name);
  55. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *name);
  56. void nvmem_cell_put(struct nvmem_cell *cell);
  57. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
  58. void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len);
  59. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len);
  60. *nvmem_cell_get() apis will get a reference to nvmem cell for a given id,
  61. and nvmem_cell_read/write() can then read or write to the cell.
  62. Once the usage of the cell is finished the consumer should call *nvmem_cell_put()
  63. to free all the allocation memory for the cell.
  64. 4. Direct NVMEM device based consumer APIs
  65. ==========================================
  66. In some instances it is necessary to directly read/write the NVMEM.
  67. To facilitate such consumers NVMEM framework provides below apis.
  68. struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
  69. struct nvmem_device *devm_nvmem_device_get(struct device *dev,
  70. const char *name);
  71. void nvmem_device_put(struct nvmem_device *nvmem);
  72. int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
  73. size_t bytes, void *buf);
  74. int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
  75. size_t bytes, void *buf);
  76. int nvmem_device_cell_read(struct nvmem_device *nvmem,
  77. struct nvmem_cell_info *info, void *buf);
  78. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  79. struct nvmem_cell_info *info, void *buf);
  80. Before the consumers can read/write NVMEM directly, it should get hold
  81. of nvmem_controller from one of the *nvmem_device_get() api.
  82. The difference between these apis and cell based apis is that these apis always
  83. take nvmem_device as parameter.
  84. 5. Releasing a reference to the NVMEM
  85. =====================================
  86. When a consumers no longer needs the NVMEM, it has to release the reference
  87. to the NVMEM it has obtained using the APIs mentioned in the above section.
  88. The NVMEM framework provides 2 APIs to release a reference to the NVMEM.
  89. void nvmem_cell_put(struct nvmem_cell *cell);
  90. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
  91. void nvmem_device_put(struct nvmem_device *nvmem);
  92. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);
  93. Both these APIs are used to release a reference to the NVMEM and
  94. devm_nvmem_cell_put and devm_nvmem_device_put destroys the devres associated
  95. with this NVMEM.
  96. Userspace
  97. +++++++++
  98. 6. Userspace binary interface
  99. ==============================
  100. Userspace can read/write the raw NVMEM file located at
  101. /sys/bus/nvmem/devices/*/nvmem
  102. ex:
  103. hexdump /sys/bus/nvmem/devices/qfprom0/nvmem
  104. 0000000 0000 0000 0000 0000 0000 0000 0000 0000
  105. *
  106. 00000a0 db10 2240 0000 e000 0c00 0c00 0000 0c00
  107. 0000000 0000 0000 0000 0000 0000 0000 0000 0000
  108. ...
  109. *
  110. 0001000
  111. 7. DeviceTree Binding
  112. =====================
  113. See Documentation/devicetree/bindings/nvmem/nvmem.txt