max8952.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * max8952.c - Voltage and current regulation for the Maxim 8952
  4. *
  5. * Copyright (C) 2010 Samsung Electronics
  6. * MyungJoo Ham <myungjoo.ham@samsung.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/i2c.h>
  11. #include <linux/err.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regulator/driver.h>
  14. #include <linux/regulator/max8952.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/regulator/of_regulator.h>
  19. #include <linux/slab.h>
  20. /* Registers */
  21. enum {
  22. MAX8952_REG_MODE0,
  23. MAX8952_REG_MODE1,
  24. MAX8952_REG_MODE2,
  25. MAX8952_REG_MODE3,
  26. MAX8952_REG_CONTROL,
  27. MAX8952_REG_SYNC,
  28. MAX8952_REG_RAMP,
  29. MAX8952_REG_CHIP_ID1,
  30. MAX8952_REG_CHIP_ID2,
  31. };
  32. struct max8952_data {
  33. struct i2c_client *client;
  34. struct max8952_platform_data *pdata;
  35. struct gpio_desc *vid0_gpiod;
  36. struct gpio_desc *vid1_gpiod;
  37. bool vid0;
  38. bool vid1;
  39. };
  40. static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
  41. {
  42. int ret = i2c_smbus_read_byte_data(max8952->client, reg);
  43. if (ret > 0)
  44. ret &= 0xff;
  45. return ret;
  46. }
  47. static int max8952_write_reg(struct max8952_data *max8952,
  48. u8 reg, u8 value)
  49. {
  50. return i2c_smbus_write_byte_data(max8952->client, reg, value);
  51. }
  52. static int max8952_list_voltage(struct regulator_dev *rdev,
  53. unsigned int selector)
  54. {
  55. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  56. if (rdev_get_id(rdev) != 0)
  57. return -EINVAL;
  58. return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000;
  59. }
  60. static int max8952_get_voltage_sel(struct regulator_dev *rdev)
  61. {
  62. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  63. u8 vid = 0;
  64. if (max8952->vid0)
  65. vid += 1;
  66. if (max8952->vid1)
  67. vid += 2;
  68. return vid;
  69. }
  70. static int max8952_set_voltage_sel(struct regulator_dev *rdev,
  71. unsigned selector)
  72. {
  73. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  74. if (!max8952->vid0_gpiod || !max8952->vid1_gpiod) {
  75. /* DVS not supported */
  76. return -EPERM;
  77. }
  78. max8952->vid0 = selector & 0x1;
  79. max8952->vid1 = (selector >> 1) & 0x1;
  80. gpiod_set_value(max8952->vid0_gpiod, max8952->vid0);
  81. gpiod_set_value(max8952->vid1_gpiod, max8952->vid1);
  82. return 0;
  83. }
  84. static const struct regulator_ops max8952_ops = {
  85. .list_voltage = max8952_list_voltage,
  86. .get_voltage_sel = max8952_get_voltage_sel,
  87. .set_voltage_sel = max8952_set_voltage_sel,
  88. };
  89. static const struct regulator_desc regulator = {
  90. .name = "MAX8952_VOUT",
  91. .id = 0,
  92. .n_voltages = MAX8952_NUM_DVS_MODE,
  93. .ops = &max8952_ops,
  94. .type = REGULATOR_VOLTAGE,
  95. .owner = THIS_MODULE,
  96. };
  97. #ifdef CONFIG_OF
  98. static const struct of_device_id max8952_dt_match[] = {
  99. { .compatible = "maxim,max8952" },
  100. {},
  101. };
  102. MODULE_DEVICE_TABLE(of, max8952_dt_match);
  103. static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
  104. {
  105. struct max8952_platform_data *pd;
  106. struct device_node *np = dev->of_node;
  107. int ret;
  108. int i;
  109. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  110. if (!pd)
  111. return NULL;
  112. if (of_property_read_u32(np, "max8952,default-mode", &pd->default_mode))
  113. dev_warn(dev, "Default mode not specified, assuming 0\n");
  114. ret = of_property_read_u32_array(np, "max8952,dvs-mode-microvolt",
  115. pd->dvs_mode, ARRAY_SIZE(pd->dvs_mode));
  116. if (ret) {
  117. dev_err(dev, "max8952,dvs-mode-microvolt property not specified");
  118. return NULL;
  119. }
  120. for (i = 0; i < ARRAY_SIZE(pd->dvs_mode); ++i) {
  121. if (pd->dvs_mode[i] < 770000 || pd->dvs_mode[i] > 1400000) {
  122. dev_err(dev, "DVS voltage %d out of range\n", i);
  123. return NULL;
  124. }
  125. pd->dvs_mode[i] = (pd->dvs_mode[i] - 770000) / 10000;
  126. }
  127. if (of_property_read_u32(np, "max8952,sync-freq", &pd->sync_freq))
  128. dev_warn(dev, "max8952,sync-freq property not specified, defaulting to 26MHz\n");
  129. if (of_property_read_u32(np, "max8952,ramp-speed", &pd->ramp_speed))
  130. dev_warn(dev, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n");
  131. pd->reg_data = of_get_regulator_init_data(dev, np, &regulator);
  132. if (!pd->reg_data) {
  133. dev_err(dev, "Failed to parse regulator init data\n");
  134. return NULL;
  135. }
  136. return pd;
  137. }
  138. #else
  139. static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
  140. {
  141. return NULL;
  142. }
  143. #endif
  144. static int max8952_pmic_probe(struct i2c_client *client,
  145. const struct i2c_device_id *i2c_id)
  146. {
  147. struct i2c_adapter *adapter = client->adapter;
  148. struct max8952_platform_data *pdata = dev_get_platdata(&client->dev);
  149. struct regulator_config config = { };
  150. struct max8952_data *max8952;
  151. struct regulator_dev *rdev;
  152. struct gpio_desc *gpiod;
  153. enum gpiod_flags gflags;
  154. int ret = 0;
  155. if (client->dev.of_node)
  156. pdata = max8952_parse_dt(&client->dev);
  157. if (!pdata) {
  158. dev_err(&client->dev, "Require the platform data\n");
  159. return -EINVAL;
  160. }
  161. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  162. return -EIO;
  163. max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data),
  164. GFP_KERNEL);
  165. if (!max8952)
  166. return -ENOMEM;
  167. max8952->client = client;
  168. max8952->pdata = pdata;
  169. config.dev = &client->dev;
  170. config.init_data = pdata->reg_data;
  171. config.driver_data = max8952;
  172. config.of_node = client->dev.of_node;
  173. if (pdata->reg_data->constraints.boot_on)
  174. gflags = GPIOD_OUT_HIGH;
  175. else
  176. gflags = GPIOD_OUT_LOW;
  177. gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  178. /*
  179. * Do not use devm* here: the regulator core takes over the
  180. * lifecycle management of the GPIO descriptor.
  181. */
  182. gpiod = gpiod_get_optional(&client->dev,
  183. "max8952,en",
  184. gflags);
  185. if (IS_ERR(gpiod))
  186. return PTR_ERR(gpiod);
  187. if (gpiod)
  188. config.ena_gpiod = gpiod;
  189. rdev = devm_regulator_register(&client->dev, &regulator, &config);
  190. if (IS_ERR(rdev)) {
  191. ret = PTR_ERR(rdev);
  192. dev_err(&client->dev, "regulator init failed (%d)\n", ret);
  193. return ret;
  194. }
  195. max8952->vid0 = pdata->default_mode & 0x1;
  196. max8952->vid1 = (pdata->default_mode >> 1) & 0x1;
  197. /* Fetch vid0 and vid1 GPIOs if available */
  198. gflags = max8952->vid0 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  199. max8952->vid0_gpiod = devm_gpiod_get_index_optional(&client->dev,
  200. "max8952,vid",
  201. 0, gflags);
  202. if (IS_ERR(max8952->vid0_gpiod))
  203. return PTR_ERR(max8952->vid0_gpiod);
  204. gflags = max8952->vid1 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  205. max8952->vid1_gpiod = devm_gpiod_get_index_optional(&client->dev,
  206. "max8952,vid",
  207. 1, gflags);
  208. if (IS_ERR(max8952->vid1_gpiod))
  209. return PTR_ERR(max8952->vid1_gpiod);
  210. /* If either VID GPIO is missing just disable this */
  211. if (!max8952->vid0_gpiod || !max8952->vid1_gpiod) {
  212. dev_warn(&client->dev, "VID0/1 gpio invalid: "
  213. "DVS not available.\n");
  214. max8952->vid0 = 0;
  215. max8952->vid1 = 0;
  216. /* Make sure if we have any descriptors they get set to low */
  217. if (max8952->vid0_gpiod)
  218. gpiod_set_value(max8952->vid0_gpiod, 0);
  219. if (max8952->vid1_gpiod)
  220. gpiod_set_value(max8952->vid1_gpiod, 0);
  221. /* Disable Pulldown of EN only */
  222. max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
  223. dev_err(&client->dev, "DVS modes disabled because VID0 and VID1"
  224. " do not have proper controls.\n");
  225. } else {
  226. /*
  227. * Disable Pulldown on EN, VID0, VID1 to reduce
  228. * leakage current of MAX8952 assuming that MAX8952
  229. * is turned on (EN==1). Note that without having VID0/1
  230. * properly connected, turning pulldown off can be
  231. * problematic. Thus, turn this off only when they are
  232. * controllable by GPIO.
  233. */
  234. max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
  235. }
  236. max8952_write_reg(max8952, MAX8952_REG_MODE0,
  237. (max8952_read_reg(max8952,
  238. MAX8952_REG_MODE0) & 0xC0) |
  239. (pdata->dvs_mode[0] & 0x3F));
  240. max8952_write_reg(max8952, MAX8952_REG_MODE1,
  241. (max8952_read_reg(max8952,
  242. MAX8952_REG_MODE1) & 0xC0) |
  243. (pdata->dvs_mode[1] & 0x3F));
  244. max8952_write_reg(max8952, MAX8952_REG_MODE2,
  245. (max8952_read_reg(max8952,
  246. MAX8952_REG_MODE2) & 0xC0) |
  247. (pdata->dvs_mode[2] & 0x3F));
  248. max8952_write_reg(max8952, MAX8952_REG_MODE3,
  249. (max8952_read_reg(max8952,
  250. MAX8952_REG_MODE3) & 0xC0) |
  251. (pdata->dvs_mode[3] & 0x3F));
  252. max8952_write_reg(max8952, MAX8952_REG_SYNC,
  253. (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
  254. ((pdata->sync_freq & 0x3) << 6));
  255. max8952_write_reg(max8952, MAX8952_REG_RAMP,
  256. (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
  257. ((pdata->ramp_speed & 0x7) << 5));
  258. i2c_set_clientdata(client, max8952);
  259. return 0;
  260. }
  261. static const struct i2c_device_id max8952_ids[] = {
  262. { "max8952", 0 },
  263. { },
  264. };
  265. MODULE_DEVICE_TABLE(i2c, max8952_ids);
  266. static struct i2c_driver max8952_pmic_driver = {
  267. .probe = max8952_pmic_probe,
  268. .driver = {
  269. .name = "max8952",
  270. .of_match_table = of_match_ptr(max8952_dt_match),
  271. },
  272. .id_table = max8952_ids,
  273. };
  274. static int __init max8952_pmic_init(void)
  275. {
  276. return i2c_add_driver(&max8952_pmic_driver);
  277. }
  278. subsys_initcall(max8952_pmic_init);
  279. static void __exit max8952_pmic_exit(void)
  280. {
  281. i2c_del_driver(&max8952_pmic_driver);
  282. }
  283. module_exit(max8952_pmic_exit);
  284. MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
  285. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  286. MODULE_LICENSE("GPL");