max8952.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. {
  146. struct i2c_adapter *adapter = client->adapter;
  147. struct max8952_platform_data *pdata = dev_get_platdata(&client->dev);
  148. struct regulator_config config = { };
  149. struct max8952_data *max8952;
  150. struct regulator_dev *rdev;
  151. struct gpio_desc *gpiod;
  152. enum gpiod_flags gflags;
  153. int ret = 0;
  154. if (client->dev.of_node)
  155. pdata = max8952_parse_dt(&client->dev);
  156. if (!pdata) {
  157. dev_err(&client->dev, "Require the platform data\n");
  158. return -EINVAL;
  159. }
  160. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  161. return -EIO;
  162. max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data),
  163. GFP_KERNEL);
  164. if (!max8952)
  165. return -ENOMEM;
  166. max8952->client = client;
  167. max8952->pdata = pdata;
  168. config.dev = &client->dev;
  169. config.init_data = pdata->reg_data;
  170. config.driver_data = max8952;
  171. config.of_node = client->dev.of_node;
  172. if (pdata->reg_data->constraints.boot_on)
  173. gflags = GPIOD_OUT_HIGH;
  174. else
  175. gflags = GPIOD_OUT_LOW;
  176. gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  177. /*
  178. * Do not use devm* here: the regulator core takes over the
  179. * lifecycle management of the GPIO descriptor.
  180. */
  181. gpiod = gpiod_get_optional(&client->dev,
  182. "max8952,en",
  183. gflags);
  184. if (IS_ERR(gpiod))
  185. return PTR_ERR(gpiod);
  186. if (gpiod)
  187. config.ena_gpiod = gpiod;
  188. rdev = devm_regulator_register(&client->dev, &regulator, &config);
  189. if (IS_ERR(rdev)) {
  190. ret = PTR_ERR(rdev);
  191. dev_err(&client->dev, "regulator init failed (%d)\n", ret);
  192. return ret;
  193. }
  194. max8952->vid0 = pdata->default_mode & 0x1;
  195. max8952->vid1 = (pdata->default_mode >> 1) & 0x1;
  196. /* Fetch vid0 and vid1 GPIOs if available */
  197. gflags = max8952->vid0 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  198. max8952->vid0_gpiod = devm_gpiod_get_index_optional(&client->dev,
  199. "max8952,vid",
  200. 0, gflags);
  201. if (IS_ERR(max8952->vid0_gpiod))
  202. return PTR_ERR(max8952->vid0_gpiod);
  203. gflags = max8952->vid1 ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  204. max8952->vid1_gpiod = devm_gpiod_get_index_optional(&client->dev,
  205. "max8952,vid",
  206. 1, gflags);
  207. if (IS_ERR(max8952->vid1_gpiod))
  208. return PTR_ERR(max8952->vid1_gpiod);
  209. /* If either VID GPIO is missing just disable this */
  210. if (!max8952->vid0_gpiod || !max8952->vid1_gpiod) {
  211. dev_warn(&client->dev, "VID0/1 gpio invalid: "
  212. "DVS not available.\n");
  213. max8952->vid0 = 0;
  214. max8952->vid1 = 0;
  215. /* Make sure if we have any descriptors they get set to low */
  216. if (max8952->vid0_gpiod)
  217. gpiod_set_value(max8952->vid0_gpiod, 0);
  218. if (max8952->vid1_gpiod)
  219. gpiod_set_value(max8952->vid1_gpiod, 0);
  220. /* Disable Pulldown of EN only */
  221. max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
  222. dev_err(&client->dev, "DVS modes disabled because VID0 and VID1"
  223. " do not have proper controls.\n");
  224. } else {
  225. /*
  226. * Disable Pulldown on EN, VID0, VID1 to reduce
  227. * leakage current of MAX8952 assuming that MAX8952
  228. * is turned on (EN==1). Note that without having VID0/1
  229. * properly connected, turning pulldown off can be
  230. * problematic. Thus, turn this off only when they are
  231. * controllable by GPIO.
  232. */
  233. max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
  234. }
  235. max8952_write_reg(max8952, MAX8952_REG_MODE0,
  236. (max8952_read_reg(max8952,
  237. MAX8952_REG_MODE0) & 0xC0) |
  238. (pdata->dvs_mode[0] & 0x3F));
  239. max8952_write_reg(max8952, MAX8952_REG_MODE1,
  240. (max8952_read_reg(max8952,
  241. MAX8952_REG_MODE1) & 0xC0) |
  242. (pdata->dvs_mode[1] & 0x3F));
  243. max8952_write_reg(max8952, MAX8952_REG_MODE2,
  244. (max8952_read_reg(max8952,
  245. MAX8952_REG_MODE2) & 0xC0) |
  246. (pdata->dvs_mode[2] & 0x3F));
  247. max8952_write_reg(max8952, MAX8952_REG_MODE3,
  248. (max8952_read_reg(max8952,
  249. MAX8952_REG_MODE3) & 0xC0) |
  250. (pdata->dvs_mode[3] & 0x3F));
  251. max8952_write_reg(max8952, MAX8952_REG_SYNC,
  252. (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
  253. ((pdata->sync_freq & 0x3) << 6));
  254. max8952_write_reg(max8952, MAX8952_REG_RAMP,
  255. (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
  256. ((pdata->ramp_speed & 0x7) << 5));
  257. i2c_set_clientdata(client, max8952);
  258. return 0;
  259. }
  260. static const struct i2c_device_id max8952_ids[] = {
  261. { "max8952" },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(i2c, max8952_ids);
  265. static struct i2c_driver max8952_pmic_driver = {
  266. .probe = max8952_pmic_probe,
  267. .driver = {
  268. .name = "max8952",
  269. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  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");