rsmu_i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * I2C driver for Renesas Synchronization Management Unit (SMU) devices.
  4. *
  5. * Copyright (C) 2021 Integrated Device Technology, Inc., a Renesas Company.
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mfd/core.h>
  11. #include <linux/mfd/rsmu.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/regmap.h>
  15. #include <linux/slab.h>
  16. #include "rsmu.h"
  17. /*
  18. * 16-bit register address: the lower 8 bits of the register address come
  19. * from the offset addr byte and the upper 8 bits come from the page register.
  20. */
  21. #define RSMU_CM_PAGE_ADDR 0xFD
  22. #define RSMU_CM_PAGE_WINDOW 256
  23. /*
  24. * 15-bit register address: the lower 7 bits of the register address come
  25. * from the offset addr byte and the upper 8 bits come from the page register.
  26. */
  27. #define RSMU_SABRE_PAGE_ADDR 0x7F
  28. #define RSMU_SABRE_PAGE_WINDOW 128
  29. static const struct regmap_range_cfg rsmu_cm_range_cfg[] = {
  30. {
  31. .range_min = 0,
  32. .range_max = 0xD000,
  33. .selector_reg = RSMU_CM_PAGE_ADDR,
  34. .selector_mask = 0xFF,
  35. .selector_shift = 0,
  36. .window_start = 0,
  37. .window_len = RSMU_CM_PAGE_WINDOW,
  38. }
  39. };
  40. static const struct regmap_range_cfg rsmu_sabre_range_cfg[] = {
  41. {
  42. .range_min = 0,
  43. .range_max = 0x400,
  44. .selector_reg = RSMU_SABRE_PAGE_ADDR,
  45. .selector_mask = 0xFF,
  46. .selector_shift = 0,
  47. .window_start = 0,
  48. .window_len = RSMU_SABRE_PAGE_WINDOW,
  49. }
  50. };
  51. static bool rsmu_cm_volatile_reg(struct device *dev, unsigned int reg)
  52. {
  53. switch (reg) {
  54. case RSMU_CM_PAGE_ADDR:
  55. return false;
  56. default:
  57. return true;
  58. }
  59. }
  60. static bool rsmu_sabre_volatile_reg(struct device *dev, unsigned int reg)
  61. {
  62. switch (reg) {
  63. case RSMU_SABRE_PAGE_ADDR:
  64. return false;
  65. default:
  66. return true;
  67. }
  68. }
  69. static const struct regmap_config rsmu_cm_regmap_config = {
  70. .reg_bits = 8,
  71. .val_bits = 8,
  72. .max_register = 0xD000,
  73. .ranges = rsmu_cm_range_cfg,
  74. .num_ranges = ARRAY_SIZE(rsmu_cm_range_cfg),
  75. .volatile_reg = rsmu_cm_volatile_reg,
  76. .cache_type = REGCACHE_RBTREE,
  77. .can_multi_write = true,
  78. };
  79. static const struct regmap_config rsmu_sabre_regmap_config = {
  80. .reg_bits = 8,
  81. .val_bits = 8,
  82. .max_register = 0x400,
  83. .ranges = rsmu_sabre_range_cfg,
  84. .num_ranges = ARRAY_SIZE(rsmu_sabre_range_cfg),
  85. .volatile_reg = rsmu_sabre_volatile_reg,
  86. .cache_type = REGCACHE_RBTREE,
  87. .can_multi_write = true,
  88. };
  89. static const struct regmap_config rsmu_sl_regmap_config = {
  90. .reg_bits = 16,
  91. .val_bits = 8,
  92. .reg_format_endian = REGMAP_ENDIAN_BIG,
  93. .max_register = 0x339,
  94. .cache_type = REGCACHE_NONE,
  95. .can_multi_write = true,
  96. };
  97. static int rsmu_i2c_probe(struct i2c_client *client,
  98. const struct i2c_device_id *id)
  99. {
  100. const struct regmap_config *cfg;
  101. struct rsmu_ddata *rsmu;
  102. int ret;
  103. rsmu = devm_kzalloc(&client->dev, sizeof(*rsmu), GFP_KERNEL);
  104. if (!rsmu)
  105. return -ENOMEM;
  106. i2c_set_clientdata(client, rsmu);
  107. rsmu->dev = &client->dev;
  108. rsmu->type = (enum rsmu_type)id->driver_data;
  109. switch (rsmu->type) {
  110. case RSMU_CM:
  111. cfg = &rsmu_cm_regmap_config;
  112. break;
  113. case RSMU_SABRE:
  114. cfg = &rsmu_sabre_regmap_config;
  115. break;
  116. case RSMU_SL:
  117. cfg = &rsmu_sl_regmap_config;
  118. break;
  119. default:
  120. dev_err(rsmu->dev, "Unsupported RSMU device type: %d\n", rsmu->type);
  121. return -ENODEV;
  122. }
  123. rsmu->regmap = devm_regmap_init_i2c(client, cfg);
  124. if (IS_ERR(rsmu->regmap)) {
  125. ret = PTR_ERR(rsmu->regmap);
  126. dev_err(rsmu->dev, "Failed to allocate register map: %d\n", ret);
  127. return ret;
  128. }
  129. return rsmu_core_init(rsmu);
  130. }
  131. static void rsmu_i2c_remove(struct i2c_client *client)
  132. {
  133. struct rsmu_ddata *rsmu = i2c_get_clientdata(client);
  134. rsmu_core_exit(rsmu);
  135. }
  136. static const struct i2c_device_id rsmu_i2c_id[] = {
  137. { "8a34000", RSMU_CM },
  138. { "8a34001", RSMU_CM },
  139. { "82p33810", RSMU_SABRE },
  140. { "82p33811", RSMU_SABRE },
  141. { "8v19n850", RSMU_SL },
  142. { "8v19n851", RSMU_SL },
  143. {}
  144. };
  145. MODULE_DEVICE_TABLE(i2c, rsmu_i2c_id);
  146. static const struct of_device_id rsmu_i2c_of_match[] = {
  147. { .compatible = "idt,8a34000", .data = (void *)RSMU_CM },
  148. { .compatible = "idt,8a34001", .data = (void *)RSMU_CM },
  149. { .compatible = "idt,82p33810", .data = (void *)RSMU_SABRE },
  150. { .compatible = "idt,82p33811", .data = (void *)RSMU_SABRE },
  151. { .compatible = "idt,8v19n850", .data = (void *)RSMU_SL },
  152. { .compatible = "idt,8v19n851", .data = (void *)RSMU_SL },
  153. {}
  154. };
  155. MODULE_DEVICE_TABLE(of, rsmu_i2c_of_match);
  156. static struct i2c_driver rsmu_i2c_driver = {
  157. .driver = {
  158. .name = "rsmu-i2c",
  159. .of_match_table = of_match_ptr(rsmu_i2c_of_match),
  160. },
  161. .probe = rsmu_i2c_probe,
  162. .remove = rsmu_i2c_remove,
  163. .id_table = rsmu_i2c_id,
  164. };
  165. static int __init rsmu_i2c_init(void)
  166. {
  167. return i2c_add_driver(&rsmu_i2c_driver);
  168. }
  169. subsys_initcall(rsmu_i2c_init);
  170. static void __exit rsmu_i2c_exit(void)
  171. {
  172. i2c_del_driver(&rsmu_i2c_driver);
  173. }
  174. module_exit(rsmu_i2c_exit);
  175. MODULE_DESCRIPTION("Renesas SMU I2C driver");
  176. MODULE_LICENSE("GPL");