wpcm450-soc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Nuvoton WPCM450 SoC Identification
  4. *
  5. * Copyright (C) 2022 Jonathan Neuschäfer
  6. */
  7. #include <linux/mfd/syscon.h>
  8. #include <linux/of.h>
  9. #include <linux/regmap.h>
  10. #include <linux/slab.h>
  11. #include <linux/sys_soc.h>
  12. #define GCR_PDID 0
  13. #define PDID_CHIP(x) ((x) & 0x00ffffff)
  14. #define CHIP_WPCM450 0x926450
  15. #define PDID_REV(x) ((x) >> 24)
  16. struct revision {
  17. u8 number;
  18. const char *name;
  19. };
  20. static const struct revision revisions[] __initconst = {
  21. { 0x00, "Z1" },
  22. { 0x03, "Z2" },
  23. { 0x04, "Z21" },
  24. { 0x08, "A1" },
  25. { 0x09, "A2" },
  26. { 0x0a, "A3" },
  27. {}
  28. };
  29. static const char * __init get_revision(unsigned int rev)
  30. {
  31. int i;
  32. for (i = 0; revisions[i].name; i++)
  33. if (revisions[i].number == rev)
  34. return revisions[i].name;
  35. return NULL;
  36. }
  37. static struct soc_device_attribute *wpcm450_attr;
  38. static struct soc_device *wpcm450_soc;
  39. static int __init wpcm450_soc_init(void)
  40. {
  41. struct soc_device_attribute *attr;
  42. struct soc_device *soc;
  43. const char *revision;
  44. struct regmap *gcr;
  45. u32 pdid;
  46. int ret;
  47. if (!of_machine_is_compatible("nuvoton,wpcm450"))
  48. return 0;
  49. gcr = syscon_regmap_lookup_by_compatible("nuvoton,wpcm450-gcr");
  50. if (IS_ERR(gcr))
  51. return PTR_ERR(gcr);
  52. ret = regmap_read(gcr, GCR_PDID, &pdid);
  53. if (ret)
  54. return ret;
  55. if (PDID_CHIP(pdid) != CHIP_WPCM450) {
  56. pr_warn("Unknown chip ID in GCR.PDID: 0x%06x\n", PDID_CHIP(pdid));
  57. return -ENODEV;
  58. }
  59. revision = get_revision(PDID_REV(pdid));
  60. if (!revision) {
  61. pr_warn("Unknown chip revision in GCR.PDID: 0x%02x\n", PDID_REV(pdid));
  62. return -ENODEV;
  63. }
  64. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  65. if (!attr)
  66. return -ENOMEM;
  67. attr->family = "Nuvoton NPCM";
  68. attr->soc_id = "WPCM450";
  69. attr->revision = revision;
  70. soc = soc_device_register(attr);
  71. if (IS_ERR(soc)) {
  72. kfree(attr);
  73. pr_warn("Could not register SoC device\n");
  74. return PTR_ERR(soc);
  75. }
  76. wpcm450_soc = soc;
  77. wpcm450_attr = attr;
  78. return 0;
  79. }
  80. module_init(wpcm450_soc_init);
  81. static void __exit wpcm450_soc_exit(void)
  82. {
  83. if (wpcm450_soc) {
  84. soc_device_unregister(wpcm450_soc);
  85. wpcm450_soc = NULL;
  86. kfree(wpcm450_attr);
  87. }
  88. }
  89. module_exit(wpcm450_soc_exit);
  90. MODULE_LICENSE("GPL");
  91. MODULE_AUTHOR("Jonathan Neuschäfer");
  92. MODULE_DESCRIPTION("Nuvoton WPCM450 SoC Identification driver");