max77650-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2018 BayLibre SAS
  4. // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
  5. //
  6. // Regulator driver for MAXIM 77650/77651 charger/power-supply.
  7. #include <linux/of.h>
  8. #include <linux/mfd/max77650.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regmap.h>
  12. #include <linux/regulator/driver.h>
  13. #define MAX77650_REGULATOR_EN_CTRL_MASK GENMASK(3, 0)
  14. #define MAX77650_REGULATOR_EN_CTRL_BITS(_reg) \
  15. ((_reg) & MAX77650_REGULATOR_EN_CTRL_MASK)
  16. #define MAX77650_REGULATOR_ENABLED GENMASK(2, 1)
  17. #define MAX77650_REGULATOR_DISABLED BIT(2)
  18. #define MAX77650_REGULATOR_V_LDO_MASK GENMASK(6, 0)
  19. #define MAX77650_REGULATOR_V_SBB_MASK GENMASK(5, 0)
  20. #define MAX77651_REGULATOR_V_SBB1_MASK GENMASK(5, 2)
  21. #define MAX77651_REGULATOR_V_SBB1_RANGE_MASK GENMASK(1, 0)
  22. #define MAX77650_REGULATOR_AD_MASK BIT(3)
  23. #define MAX77650_REGULATOR_AD_DISABLED 0x00
  24. #define MAX77650_REGULATOR_AD_ENABLED BIT(3)
  25. #define MAX77650_REGULATOR_CURR_LIM_MASK GENMASK(7, 6)
  26. enum {
  27. MAX77650_REGULATOR_ID_LDO = 0,
  28. MAX77650_REGULATOR_ID_SBB0,
  29. MAX77650_REGULATOR_ID_SBB1,
  30. MAX77650_REGULATOR_ID_SBB2,
  31. MAX77650_REGULATOR_NUM_REGULATORS,
  32. };
  33. struct max77650_regulator_desc {
  34. struct regulator_desc desc;
  35. unsigned int regA;
  36. unsigned int regB;
  37. };
  38. static struct max77650_regulator_desc max77651_SBB1_desc;
  39. static const unsigned int max77651_sbb1_volt_range_sel[] = {
  40. 0x0, 0x1, 0x2, 0x3
  41. };
  42. static const struct regulator_linear_range max77651_sbb1_volt_ranges[] = {
  43. /* range index 0 */
  44. REGULATOR_LINEAR_RANGE(2400000, 0x00, 0x0f, 50000),
  45. /* range index 1 */
  46. REGULATOR_LINEAR_RANGE(3200000, 0x00, 0x0f, 50000),
  47. /* range index 2 */
  48. REGULATOR_LINEAR_RANGE(4000000, 0x00, 0x0f, 50000),
  49. /* range index 3 */
  50. REGULATOR_LINEAR_RANGE(4800000, 0x00, 0x09, 50000),
  51. };
  52. static const unsigned int max77650_current_limit_table[] = {
  53. 1000000, 866000, 707000, 500000,
  54. };
  55. static int max77650_regulator_is_enabled(struct regulator_dev *rdev)
  56. {
  57. struct max77650_regulator_desc *rdesc;
  58. struct regmap *map;
  59. int val, rv, en;
  60. rdesc = rdev_get_drvdata(rdev);
  61. map = rdev_get_regmap(rdev);
  62. rv = regmap_read(map, rdesc->regB, &val);
  63. if (rv)
  64. return rv;
  65. en = MAX77650_REGULATOR_EN_CTRL_BITS(val);
  66. return en != MAX77650_REGULATOR_DISABLED;
  67. }
  68. static int max77650_regulator_enable(struct regulator_dev *rdev)
  69. {
  70. struct max77650_regulator_desc *rdesc;
  71. struct regmap *map;
  72. rdesc = rdev_get_drvdata(rdev);
  73. map = rdev_get_regmap(rdev);
  74. return regmap_update_bits(map, rdesc->regB,
  75. MAX77650_REGULATOR_EN_CTRL_MASK,
  76. MAX77650_REGULATOR_ENABLED);
  77. }
  78. static int max77650_regulator_disable(struct regulator_dev *rdev)
  79. {
  80. struct max77650_regulator_desc *rdesc;
  81. struct regmap *map;
  82. rdesc = rdev_get_drvdata(rdev);
  83. map = rdev_get_regmap(rdev);
  84. return regmap_update_bits(map, rdesc->regB,
  85. MAX77650_REGULATOR_EN_CTRL_MASK,
  86. MAX77650_REGULATOR_DISABLED);
  87. }
  88. static const struct regulator_ops max77650_regulator_LDO_ops = {
  89. .is_enabled = max77650_regulator_is_enabled,
  90. .enable = max77650_regulator_enable,
  91. .disable = max77650_regulator_disable,
  92. .list_voltage = regulator_list_voltage_linear,
  93. .map_voltage = regulator_map_voltage_linear,
  94. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  95. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  96. .set_active_discharge = regulator_set_active_discharge_regmap,
  97. };
  98. static const struct regulator_ops max77650_regulator_SBB_ops = {
  99. .is_enabled = max77650_regulator_is_enabled,
  100. .enable = max77650_regulator_enable,
  101. .disable = max77650_regulator_disable,
  102. .list_voltage = regulator_list_voltage_linear,
  103. .map_voltage = regulator_map_voltage_linear,
  104. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  105. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  106. .get_current_limit = regulator_get_current_limit_regmap,
  107. .set_current_limit = regulator_set_current_limit_regmap,
  108. .set_active_discharge = regulator_set_active_discharge_regmap,
  109. };
  110. /* Special case for max77651 SBB1 - pickable linear-range voltage mapping. */
  111. static const struct regulator_ops max77651_SBB1_regulator_ops = {
  112. .is_enabled = max77650_regulator_is_enabled,
  113. .enable = max77650_regulator_enable,
  114. .disable = max77650_regulator_disable,
  115. .list_voltage = regulator_list_voltage_pickable_linear_range,
  116. .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
  117. .set_voltage_sel = regulator_set_voltage_sel_pickable_regmap,
  118. .get_current_limit = regulator_get_current_limit_regmap,
  119. .set_current_limit = regulator_set_current_limit_regmap,
  120. .set_active_discharge = regulator_set_active_discharge_regmap,
  121. };
  122. static struct max77650_regulator_desc max77650_LDO_desc = {
  123. .desc = {
  124. .name = "ldo",
  125. .of_match = of_match_ptr("ldo"),
  126. .regulators_node = of_match_ptr("regulators"),
  127. .supply_name = "in-ldo",
  128. .id = MAX77650_REGULATOR_ID_LDO,
  129. .ops = &max77650_regulator_LDO_ops,
  130. .min_uV = 1350000,
  131. .uV_step = 12500,
  132. .n_voltages = 128,
  133. .vsel_step = 1,
  134. .vsel_mask = MAX77650_REGULATOR_V_LDO_MASK,
  135. .vsel_reg = MAX77650_REG_CNFG_LDO_A,
  136. .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
  137. .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
  138. .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
  139. .active_discharge_reg = MAX77650_REG_CNFG_LDO_B,
  140. .enable_time = 100,
  141. .type = REGULATOR_VOLTAGE,
  142. .owner = THIS_MODULE,
  143. },
  144. .regA = MAX77650_REG_CNFG_LDO_A,
  145. .regB = MAX77650_REG_CNFG_LDO_B,
  146. };
  147. static struct max77650_regulator_desc max77650_SBB0_desc = {
  148. .desc = {
  149. .name = "sbb0",
  150. .of_match = of_match_ptr("sbb0"),
  151. .regulators_node = of_match_ptr("regulators"),
  152. .supply_name = "in-sbb0",
  153. .id = MAX77650_REGULATOR_ID_SBB0,
  154. .ops = &max77650_regulator_SBB_ops,
  155. .min_uV = 800000,
  156. .uV_step = 25000,
  157. .n_voltages = 64,
  158. .vsel_step = 1,
  159. .vsel_mask = MAX77650_REGULATOR_V_SBB_MASK,
  160. .vsel_reg = MAX77650_REG_CNFG_SBB0_A,
  161. .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
  162. .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
  163. .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
  164. .active_discharge_reg = MAX77650_REG_CNFG_SBB0_B,
  165. .enable_time = 100,
  166. .type = REGULATOR_VOLTAGE,
  167. .owner = THIS_MODULE,
  168. .csel_reg = MAX77650_REG_CNFG_SBB0_A,
  169. .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
  170. .curr_table = max77650_current_limit_table,
  171. .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
  172. },
  173. .regA = MAX77650_REG_CNFG_SBB0_A,
  174. .regB = MAX77650_REG_CNFG_SBB0_B,
  175. };
  176. static struct max77650_regulator_desc max77650_SBB1_desc = {
  177. .desc = {
  178. .name = "sbb1",
  179. .of_match = of_match_ptr("sbb1"),
  180. .regulators_node = of_match_ptr("regulators"),
  181. .supply_name = "in-sbb1",
  182. .id = MAX77650_REGULATOR_ID_SBB1,
  183. .ops = &max77650_regulator_SBB_ops,
  184. .min_uV = 800000,
  185. .uV_step = 12500,
  186. .n_voltages = 64,
  187. .vsel_step = 1,
  188. .vsel_mask = MAX77650_REGULATOR_V_SBB_MASK,
  189. .vsel_reg = MAX77650_REG_CNFG_SBB1_A,
  190. .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
  191. .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
  192. .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
  193. .active_discharge_reg = MAX77650_REG_CNFG_SBB1_B,
  194. .enable_time = 100,
  195. .type = REGULATOR_VOLTAGE,
  196. .owner = THIS_MODULE,
  197. .csel_reg = MAX77650_REG_CNFG_SBB1_A,
  198. .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
  199. .curr_table = max77650_current_limit_table,
  200. .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
  201. },
  202. .regA = MAX77650_REG_CNFG_SBB1_A,
  203. .regB = MAX77650_REG_CNFG_SBB1_B,
  204. };
  205. static struct max77650_regulator_desc max77651_SBB1_desc = {
  206. .desc = {
  207. .name = "sbb1",
  208. .of_match = of_match_ptr("sbb1"),
  209. .regulators_node = of_match_ptr("regulators"),
  210. .supply_name = "in-sbb1",
  211. .id = MAX77650_REGULATOR_ID_SBB1,
  212. .ops = &max77651_SBB1_regulator_ops,
  213. .linear_range_selectors = max77651_sbb1_volt_range_sel,
  214. .linear_ranges = max77651_sbb1_volt_ranges,
  215. .n_linear_ranges = ARRAY_SIZE(max77651_sbb1_volt_ranges),
  216. .n_voltages = 58,
  217. .vsel_step = 1,
  218. .vsel_range_mask = MAX77651_REGULATOR_V_SBB1_RANGE_MASK,
  219. .vsel_range_reg = MAX77650_REG_CNFG_SBB1_A,
  220. .vsel_mask = MAX77651_REGULATOR_V_SBB1_MASK,
  221. .vsel_reg = MAX77650_REG_CNFG_SBB1_A,
  222. .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
  223. .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
  224. .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
  225. .active_discharge_reg = MAX77650_REG_CNFG_SBB1_B,
  226. .enable_time = 100,
  227. .type = REGULATOR_VOLTAGE,
  228. .owner = THIS_MODULE,
  229. .csel_reg = MAX77650_REG_CNFG_SBB1_A,
  230. .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
  231. .curr_table = max77650_current_limit_table,
  232. .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
  233. },
  234. .regA = MAX77650_REG_CNFG_SBB1_A,
  235. .regB = MAX77650_REG_CNFG_SBB1_B,
  236. };
  237. static struct max77650_regulator_desc max77650_SBB2_desc = {
  238. .desc = {
  239. .name = "sbb2",
  240. .of_match = of_match_ptr("sbb2"),
  241. .regulators_node = of_match_ptr("regulators"),
  242. .supply_name = "in-sbb0",
  243. .id = MAX77650_REGULATOR_ID_SBB2,
  244. .ops = &max77650_regulator_SBB_ops,
  245. .min_uV = 800000,
  246. .uV_step = 50000,
  247. .n_voltages = 64,
  248. .vsel_step = 1,
  249. .vsel_mask = MAX77650_REGULATOR_V_SBB_MASK,
  250. .vsel_reg = MAX77650_REG_CNFG_SBB2_A,
  251. .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
  252. .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
  253. .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
  254. .active_discharge_reg = MAX77650_REG_CNFG_SBB2_B,
  255. .enable_time = 100,
  256. .type = REGULATOR_VOLTAGE,
  257. .owner = THIS_MODULE,
  258. .csel_reg = MAX77650_REG_CNFG_SBB2_A,
  259. .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
  260. .curr_table = max77650_current_limit_table,
  261. .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
  262. },
  263. .regA = MAX77650_REG_CNFG_SBB2_A,
  264. .regB = MAX77650_REG_CNFG_SBB2_B,
  265. };
  266. static struct max77650_regulator_desc max77651_SBB2_desc = {
  267. .desc = {
  268. .name = "sbb2",
  269. .of_match = of_match_ptr("sbb2"),
  270. .regulators_node = of_match_ptr("regulators"),
  271. .supply_name = "in-sbb0",
  272. .id = MAX77650_REGULATOR_ID_SBB2,
  273. .ops = &max77650_regulator_SBB_ops,
  274. .min_uV = 2400000,
  275. .uV_step = 50000,
  276. .n_voltages = 64,
  277. .vsel_step = 1,
  278. .vsel_mask = MAX77650_REGULATOR_V_SBB_MASK,
  279. .vsel_reg = MAX77650_REG_CNFG_SBB2_A,
  280. .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
  281. .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
  282. .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
  283. .active_discharge_reg = MAX77650_REG_CNFG_SBB2_B,
  284. .enable_time = 100,
  285. .type = REGULATOR_VOLTAGE,
  286. .owner = THIS_MODULE,
  287. .csel_reg = MAX77650_REG_CNFG_SBB2_A,
  288. .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
  289. .curr_table = max77650_current_limit_table,
  290. .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
  291. },
  292. .regA = MAX77650_REG_CNFG_SBB2_A,
  293. .regB = MAX77650_REG_CNFG_SBB2_B,
  294. };
  295. static int max77650_regulator_probe(struct platform_device *pdev)
  296. {
  297. struct max77650_regulator_desc **rdescs;
  298. struct max77650_regulator_desc *rdesc;
  299. struct regulator_config config = { };
  300. struct device *dev, *parent;
  301. struct regulator_dev *rdev;
  302. struct regmap *map;
  303. unsigned int val;
  304. int i, rv;
  305. dev = &pdev->dev;
  306. parent = dev->parent;
  307. if (!dev->of_node)
  308. dev->of_node = parent->of_node;
  309. rdescs = devm_kcalloc(dev, MAX77650_REGULATOR_NUM_REGULATORS,
  310. sizeof(*rdescs), GFP_KERNEL);
  311. if (!rdescs)
  312. return -ENOMEM;
  313. map = dev_get_regmap(parent, NULL);
  314. if (!map)
  315. return -ENODEV;
  316. rv = regmap_read(map, MAX77650_REG_CID, &val);
  317. if (rv)
  318. return rv;
  319. rdescs[MAX77650_REGULATOR_ID_LDO] = &max77650_LDO_desc;
  320. rdescs[MAX77650_REGULATOR_ID_SBB0] = &max77650_SBB0_desc;
  321. switch (MAX77650_CID_BITS(val)) {
  322. case MAX77650_CID_77650A:
  323. case MAX77650_CID_77650C:
  324. rdescs[MAX77650_REGULATOR_ID_SBB1] = &max77650_SBB1_desc;
  325. rdescs[MAX77650_REGULATOR_ID_SBB2] = &max77650_SBB2_desc;
  326. break;
  327. case MAX77650_CID_77651A:
  328. case MAX77650_CID_77651B:
  329. rdescs[MAX77650_REGULATOR_ID_SBB1] = &max77651_SBB1_desc;
  330. rdescs[MAX77650_REGULATOR_ID_SBB2] = &max77651_SBB2_desc;
  331. break;
  332. default:
  333. return -ENODEV;
  334. }
  335. config.dev = parent;
  336. for (i = 0; i < MAX77650_REGULATOR_NUM_REGULATORS; i++) {
  337. rdesc = rdescs[i];
  338. config.driver_data = rdesc;
  339. rdev = devm_regulator_register(dev, &rdesc->desc, &config);
  340. if (IS_ERR(rdev))
  341. return PTR_ERR(rdev);
  342. }
  343. return 0;
  344. }
  345. static struct platform_driver max77650_regulator_driver = {
  346. .driver = {
  347. .name = "max77650-regulator",
  348. },
  349. .probe = max77650_regulator_probe,
  350. };
  351. module_platform_driver(max77650_regulator_driver);
  352. MODULE_DESCRIPTION("MAXIM 77650/77651 regulator driver");
  353. MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
  354. MODULE_LICENSE("GPL v2");
  355. MODULE_ALIAS("platform:max77650-regulator");