max77759-nvmem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright 2020 Google Inc
  4. // Copyright 2025 Linaro Ltd.
  5. //
  6. // NVMEM driver for Maxim MAX77759
  7. #include <linux/dev_printk.h>
  8. #include <linux/device.h>
  9. #include <linux/device/driver.h>
  10. #include <linux/err.h>
  11. #include <linux/mfd/max77759.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/nvmem-provider.h>
  15. #include <linux/overflow.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/string.h>
  18. #define MAX77759_NVMEM_OPCODE_HEADER_LEN 3
  19. /*
  20. * NVMEM commands have a three byte header (which becomes part of the command),
  21. * so we need to subtract that.
  22. */
  23. #define MAX77759_NVMEM_SIZE (MAX77759_MAXQ_OPCODE_MAXLENGTH \
  24. - MAX77759_NVMEM_OPCODE_HEADER_LEN)
  25. struct max77759_nvmem {
  26. struct device *dev;
  27. struct max77759 *max77759;
  28. };
  29. static int max77759_nvmem_reg_read(void *priv, unsigned int offset,
  30. void *val, size_t bytes)
  31. {
  32. struct max77759_nvmem *nvmem = priv;
  33. DEFINE_FLEX(struct max77759_maxq_command, cmd, cmd, length,
  34. MAX77759_NVMEM_OPCODE_HEADER_LEN);
  35. DEFINE_FLEX(struct max77759_maxq_response, rsp, rsp, length,
  36. MAX77759_MAXQ_OPCODE_MAXLENGTH);
  37. int ret;
  38. cmd->cmd[0] = MAX77759_MAXQ_OPCODE_USER_SPACE_READ;
  39. cmd->cmd[1] = offset;
  40. cmd->cmd[2] = bytes;
  41. rsp->length = bytes + MAX77759_NVMEM_OPCODE_HEADER_LEN;
  42. ret = max77759_maxq_command(nvmem->max77759, cmd, rsp);
  43. if (ret < 0)
  44. return ret;
  45. if (memcmp(cmd->cmd, rsp->rsp, MAX77759_NVMEM_OPCODE_HEADER_LEN)) {
  46. dev_warn(nvmem->dev, "protocol error (read)\n");
  47. return -EIO;
  48. }
  49. memcpy(val, &rsp->rsp[MAX77759_NVMEM_OPCODE_HEADER_LEN], bytes);
  50. return 0;
  51. }
  52. static int max77759_nvmem_reg_write(void *priv, unsigned int offset,
  53. void *val, size_t bytes)
  54. {
  55. struct max77759_nvmem *nvmem = priv;
  56. DEFINE_FLEX(struct max77759_maxq_command, cmd, cmd, length,
  57. MAX77759_MAXQ_OPCODE_MAXLENGTH);
  58. DEFINE_FLEX(struct max77759_maxq_response, rsp, rsp, length,
  59. MAX77759_MAXQ_OPCODE_MAXLENGTH);
  60. int ret;
  61. cmd->cmd[0] = MAX77759_MAXQ_OPCODE_USER_SPACE_WRITE;
  62. cmd->cmd[1] = offset;
  63. cmd->cmd[2] = bytes;
  64. memcpy(&cmd->cmd[MAX77759_NVMEM_OPCODE_HEADER_LEN], val, bytes);
  65. cmd->length = bytes + MAX77759_NVMEM_OPCODE_HEADER_LEN;
  66. rsp->length = cmd->length;
  67. ret = max77759_maxq_command(nvmem->max77759, cmd, rsp);
  68. if (ret < 0)
  69. return ret;
  70. if (memcmp(cmd->cmd, rsp->rsp, cmd->length)) {
  71. dev_warn(nvmem->dev, "protocol error (write)\n");
  72. return -EIO;
  73. }
  74. return 0;
  75. }
  76. static int max77759_nvmem_probe(struct platform_device *pdev)
  77. {
  78. struct nvmem_config config = {
  79. .dev = &pdev->dev,
  80. .name = dev_name(&pdev->dev),
  81. .id = NVMEM_DEVID_NONE,
  82. .type = NVMEM_TYPE_EEPROM,
  83. .ignore_wp = true,
  84. .size = MAX77759_NVMEM_SIZE,
  85. .word_size = sizeof(u8),
  86. .stride = sizeof(u8),
  87. .reg_read = max77759_nvmem_reg_read,
  88. .reg_write = max77759_nvmem_reg_write,
  89. };
  90. struct max77759_nvmem *nvmem;
  91. nvmem = devm_kzalloc(&pdev->dev, sizeof(*nvmem), GFP_KERNEL);
  92. if (!nvmem)
  93. return -ENOMEM;
  94. nvmem->dev = &pdev->dev;
  95. nvmem->max77759 = dev_get_drvdata(pdev->dev.parent);
  96. config.priv = nvmem;
  97. return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
  98. }
  99. static const struct of_device_id max77759_nvmem_of_id[] = {
  100. { .compatible = "maxim,max77759-nvmem", },
  101. { }
  102. };
  103. MODULE_DEVICE_TABLE(of, max77759_nvmem_of_id);
  104. static const struct platform_device_id max77759_nvmem_platform_id[] = {
  105. { "max77759-nvmem", },
  106. { }
  107. };
  108. MODULE_DEVICE_TABLE(platform, max77759_nvmem_platform_id);
  109. static struct platform_driver max77759_nvmem_driver = {
  110. .driver = {
  111. .name = "max77759-nvmem",
  112. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  113. .of_match_table = max77759_nvmem_of_id,
  114. },
  115. .probe = max77759_nvmem_probe,
  116. .id_table = max77759_nvmem_platform_id,
  117. };
  118. module_platform_driver(max77759_nvmem_driver);
  119. MODULE_AUTHOR("André Draszik <andre.draszik@linaro.org>");
  120. MODULE_DESCRIPTION("NVMEM driver for Maxim MAX77759");
  121. MODULE_LICENSE("GPL");