max8952.c 9.1 KB

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