sy8824x.c 5.5 KB

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