sy8824x.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // SY8824C/SY8824E regulator driver
  4. //
  5. // Copyright (C) 2019 Synaptics Incorporated
  6. //
  7. // Author: Jisheng Zhang <[email protected]>
  8. #include <linux/module.h>
  9. #include <linux/i2c.h>
  10. #include <linux/of_device.h>
  11. #include <linux/regmap.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/regulator/of_regulator.h>
  14. #define SY8824C_BUCK_EN (1 << 7)
  15. #define SY8824C_MODE (1 << 6)
  16. struct sy8824_config {
  17. /* registers */
  18. unsigned int vol_reg;
  19. unsigned int mode_reg;
  20. unsigned int enable_reg;
  21. /* Voltage range and step(linear) */
  22. unsigned int vsel_min;
  23. unsigned int vsel_step;
  24. unsigned int vsel_count;
  25. const struct regmap_config *config;
  26. };
  27. struct sy8824_device_info {
  28. struct device *dev;
  29. struct regulator_desc desc;
  30. struct regulator_init_data *regulator;
  31. const struct sy8824_config *cfg;
  32. };
  33. static int sy8824_set_mode(struct regulator_dev *rdev, unsigned int mode)
  34. {
  35. struct sy8824_device_info *di = rdev_get_drvdata(rdev);
  36. const struct sy8824_config *cfg = di->cfg;
  37. switch (mode) {
  38. case REGULATOR_MODE_FAST:
  39. regmap_update_bits(rdev->regmap, cfg->mode_reg,
  40. SY8824C_MODE, SY8824C_MODE);
  41. break;
  42. case REGULATOR_MODE_NORMAL:
  43. regmap_update_bits(rdev->regmap, cfg->mode_reg,
  44. SY8824C_MODE, 0);
  45. break;
  46. default:
  47. return -EINVAL;
  48. }
  49. return 0;
  50. }
  51. static unsigned int sy8824_get_mode(struct regulator_dev *rdev)
  52. {
  53. struct sy8824_device_info *di = rdev_get_drvdata(rdev);
  54. const struct sy8824_config *cfg = di->cfg;
  55. u32 val;
  56. int ret = 0;
  57. ret = regmap_read(rdev->regmap, cfg->mode_reg, &val);
  58. if (ret < 0)
  59. return ret;
  60. if (val & SY8824C_MODE)
  61. return REGULATOR_MODE_FAST;
  62. else
  63. return REGULATOR_MODE_NORMAL;
  64. }
  65. static const struct regulator_ops sy8824_regulator_ops = {
  66. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  67. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  68. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  69. .map_voltage = regulator_map_voltage_linear,
  70. .list_voltage = regulator_list_voltage_linear,
  71. .enable = regulator_enable_regmap,
  72. .disable = regulator_disable_regmap,
  73. .is_enabled = regulator_is_enabled_regmap,
  74. .set_mode = sy8824_set_mode,
  75. .get_mode = sy8824_get_mode,
  76. };
  77. static int sy8824_regulator_register(struct sy8824_device_info *di,
  78. struct regulator_config *config)
  79. {
  80. struct regulator_desc *rdesc = &di->desc;
  81. const struct sy8824_config *cfg = di->cfg;
  82. struct regulator_dev *rdev;
  83. rdesc->name = "sy8824-reg";
  84. rdesc->supply_name = "vin";
  85. rdesc->ops = &sy8824_regulator_ops;
  86. rdesc->type = REGULATOR_VOLTAGE;
  87. rdesc->n_voltages = cfg->vsel_count;
  88. rdesc->enable_reg = cfg->enable_reg;
  89. rdesc->enable_mask = SY8824C_BUCK_EN;
  90. rdesc->min_uV = cfg->vsel_min;
  91. rdesc->uV_step = cfg->vsel_step;
  92. rdesc->vsel_reg = cfg->vol_reg;
  93. rdesc->vsel_mask = cfg->vsel_count - 1;
  94. rdesc->owner = THIS_MODULE;
  95. rdev = devm_regulator_register(di->dev, &di->desc, config);
  96. return PTR_ERR_OR_ZERO(rdev);
  97. }
  98. static const struct regmap_config sy8824_regmap_config = {
  99. .reg_bits = 8,
  100. .val_bits = 8,
  101. .num_reg_defaults_raw = 1,
  102. .cache_type = REGCACHE_FLAT,
  103. };
  104. static const struct regmap_config sy20276_regmap_config = {
  105. .reg_bits = 8,
  106. .val_bits = 8,
  107. .num_reg_defaults_raw = 2,
  108. .cache_type = REGCACHE_FLAT,
  109. };
  110. static int sy8824_i2c_probe(struct i2c_client *client)
  111. {
  112. struct device *dev = &client->dev;
  113. struct device_node *np = dev->of_node;
  114. struct sy8824_device_info *di;
  115. struct regulator_config config = { };
  116. struct regmap *regmap;
  117. int ret;
  118. di = devm_kzalloc(dev, sizeof(struct sy8824_device_info), GFP_KERNEL);
  119. if (!di)
  120. return -ENOMEM;
  121. di->regulator = of_get_regulator_init_data(dev, np, &di->desc);
  122. if (!di->regulator) {
  123. dev_err(dev, "Platform data not found!\n");
  124. return -EINVAL;
  125. }
  126. di->dev = dev;
  127. di->cfg = of_device_get_match_data(dev);
  128. regmap = devm_regmap_init_i2c(client, di->cfg->config);
  129. if (IS_ERR(regmap)) {
  130. dev_err(dev, "Failed to allocate regmap!\n");
  131. return PTR_ERR(regmap);
  132. }
  133. i2c_set_clientdata(client, di);
  134. config.dev = di->dev;
  135. config.init_data = di->regulator;
  136. config.regmap = regmap;
  137. config.driver_data = di;
  138. config.of_node = np;
  139. ret = sy8824_regulator_register(di, &config);
  140. if (ret < 0)
  141. dev_err(dev, "Failed to register regulator!\n");
  142. return ret;
  143. }
  144. static const struct sy8824_config sy8824c_cfg = {
  145. .vol_reg = 0x00,
  146. .mode_reg = 0x00,
  147. .enable_reg = 0x00,
  148. .vsel_min = 762500,
  149. .vsel_step = 12500,
  150. .vsel_count = 64,
  151. .config = &sy8824_regmap_config,
  152. };
  153. static const struct sy8824_config sy8824e_cfg = {
  154. .vol_reg = 0x00,
  155. .mode_reg = 0x00,
  156. .enable_reg = 0x00,
  157. .vsel_min = 700000,
  158. .vsel_step = 12500,
  159. .vsel_count = 64,
  160. .config = &sy8824_regmap_config,
  161. };
  162. static const struct sy8824_config sy20276_cfg = {
  163. .vol_reg = 0x00,
  164. .mode_reg = 0x01,
  165. .enable_reg = 0x01,
  166. .vsel_min = 600000,
  167. .vsel_step = 10000,
  168. .vsel_count = 128,
  169. .config = &sy20276_regmap_config,
  170. };
  171. static const struct sy8824_config sy20278_cfg = {
  172. .vol_reg = 0x00,
  173. .mode_reg = 0x01,
  174. .enable_reg = 0x01,
  175. .vsel_min = 762500,
  176. .vsel_step = 12500,
  177. .vsel_count = 64,
  178. .config = &sy20276_regmap_config,
  179. };
  180. static const struct of_device_id sy8824_dt_ids[] = {
  181. {
  182. .compatible = "silergy,sy8824c",
  183. .data = &sy8824c_cfg
  184. },
  185. {
  186. .compatible = "silergy,sy8824e",
  187. .data = &sy8824e_cfg
  188. },
  189. {
  190. .compatible = "silergy,sy20276",
  191. .data = &sy20276_cfg
  192. },
  193. {
  194. .compatible = "silergy,sy20278",
  195. .data = &sy20278_cfg
  196. },
  197. { }
  198. };
  199. MODULE_DEVICE_TABLE(of, sy8824_dt_ids);
  200. static const struct i2c_device_id sy8824_id[] = {
  201. { "sy8824", },
  202. { },
  203. };
  204. MODULE_DEVICE_TABLE(i2c, sy8824_id);
  205. static struct i2c_driver sy8824_regulator_driver = {
  206. .driver = {
  207. .name = "sy8824-regulator",
  208. .of_match_table = of_match_ptr(sy8824_dt_ids),
  209. },
  210. .probe_new = sy8824_i2c_probe,
  211. .id_table = sy8824_id,
  212. };
  213. module_i2c_driver(sy8824_regulator_driver);
  214. MODULE_AUTHOR("Jisheng Zhang <[email protected]>");
  215. MODULE_DESCRIPTION("SY8824C/SY8824E regulator driver");
  216. MODULE_LICENSE("GPL v2");