max8952.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * max8952.c - Voltage and current regulation for the Maxim 8952
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. * MyungJoo Ham <myungjoo.ham@samsung.com>
  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 device *dev;
  49. struct max8952_platform_data *pdata;
  50. struct regulator_dev *rdev;
  51. bool vid0;
  52. bool vid1;
  53. };
  54. static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
  55. {
  56. int ret = i2c_smbus_read_byte_data(max8952->client, reg);
  57. if (ret > 0)
  58. ret &= 0xff;
  59. return ret;
  60. }
  61. static int max8952_write_reg(struct max8952_data *max8952,
  62. u8 reg, u8 value)
  63. {
  64. return i2c_smbus_write_byte_data(max8952->client, reg, value);
  65. }
  66. static int max8952_list_voltage(struct regulator_dev *rdev,
  67. unsigned int selector)
  68. {
  69. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  70. if (rdev_get_id(rdev) != 0)
  71. return -EINVAL;
  72. return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000;
  73. }
  74. static int max8952_get_voltage_sel(struct regulator_dev *rdev)
  75. {
  76. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  77. u8 vid = 0;
  78. if (max8952->vid0)
  79. vid += 1;
  80. if (max8952->vid1)
  81. vid += 2;
  82. return vid;
  83. }
  84. static int max8952_set_voltage_sel(struct regulator_dev *rdev,
  85. unsigned selector)
  86. {
  87. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  88. if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
  89. !gpio_is_valid(max8952->pdata->gpio_vid1)) {
  90. /* DVS not supported */
  91. return -EPERM;
  92. }
  93. max8952->vid0 = selector & 0x1;
  94. max8952->vid1 = (selector >> 1) & 0x1;
  95. gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
  96. gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
  97. return 0;
  98. }
  99. static struct regulator_ops max8952_ops = {
  100. .list_voltage = max8952_list_voltage,
  101. .get_voltage_sel = max8952_get_voltage_sel,
  102. .set_voltage_sel = max8952_set_voltage_sel,
  103. };
  104. static const struct regulator_desc regulator = {
  105. .name = "MAX8952_VOUT",
  106. .id = 0,
  107. .n_voltages = MAX8952_NUM_DVS_MODE,
  108. .ops = &max8952_ops,
  109. .type = REGULATOR_VOLTAGE,
  110. .owner = THIS_MODULE,
  111. };
  112. #ifdef CONFIG_OF
  113. static struct of_device_id max8952_dt_match[] = {
  114. { .compatible = "maxim,max8952" },
  115. {},
  116. };
  117. MODULE_DEVICE_TABLE(of, max8952_dt_match);
  118. static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
  119. {
  120. struct max8952_platform_data *pd;
  121. struct device_node *np = dev->of_node;
  122. int ret;
  123. int i;
  124. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  125. if (!pd) {
  126. dev_err(dev, "Failed to allocate platform data\n");
  127. return NULL;
  128. }
  129. pd->gpio_vid0 = of_get_named_gpio(np, "max8952,vid-gpios", 0);
  130. pd->gpio_vid1 = of_get_named_gpio(np, "max8952,vid-gpios", 1);
  131. pd->gpio_en = of_get_named_gpio(np, "max8952,en-gpio", 0);
  132. if (of_property_read_u32(np, "max8952,default-mode", &pd->default_mode))
  133. dev_warn(dev, "Default mode not specified, assuming 0\n");
  134. ret = of_property_read_u32_array(np, "max8952,dvs-mode-microvolt",
  135. pd->dvs_mode, ARRAY_SIZE(pd->dvs_mode));
  136. if (ret) {
  137. dev_err(dev, "max8952,dvs-mode-microvolt property not specified");
  138. return NULL;
  139. }
  140. for (i = 0; i < ARRAY_SIZE(pd->dvs_mode); ++i) {
  141. if (pd->dvs_mode[i] < 770000 || pd->dvs_mode[i] > 1400000) {
  142. dev_err(dev, "DVS voltage %d out of range\n", i);
  143. return NULL;
  144. }
  145. pd->dvs_mode[i] = (pd->dvs_mode[i] - 770000) / 10000;
  146. }
  147. if (of_property_read_u32(np, "max8952,sync-freq", &pd->sync_freq))
  148. dev_warn(dev, "max8952,sync-freq property not specified, defaulting to 26MHz\n");
  149. if (of_property_read_u32(np, "max8952,ramp-speed", &pd->ramp_speed))
  150. dev_warn(dev, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n");
  151. pd->reg_data = of_get_regulator_init_data(dev, np);
  152. if (!pd->reg_data) {
  153. dev_err(dev, "Failed to parse regulator init data\n");
  154. return NULL;
  155. }
  156. return pd;
  157. }
  158. #else
  159. static struct max8952_platform_data *max8952_parse_dt(struct device *dev)
  160. {
  161. return NULL;
  162. }
  163. #endif
  164. static int max8952_pmic_probe(struct i2c_client *client,
  165. const struct i2c_device_id *i2c_id)
  166. {
  167. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  168. struct max8952_platform_data *pdata = dev_get_platdata(&client->dev);
  169. struct regulator_config config = { };
  170. struct max8952_data *max8952;
  171. int ret = 0, err = 0;
  172. if (client->dev.of_node)
  173. pdata = max8952_parse_dt(&client->dev);
  174. if (!pdata) {
  175. dev_err(&client->dev, "Require the platform data\n");
  176. return -EINVAL;
  177. }
  178. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  179. return -EIO;
  180. max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data),
  181. GFP_KERNEL);
  182. if (!max8952)
  183. return -ENOMEM;
  184. max8952->client = client;
  185. max8952->dev = &client->dev;
  186. max8952->pdata = pdata;
  187. config.dev = max8952->dev;
  188. config.init_data = pdata->reg_data;
  189. config.driver_data = max8952;
  190. config.of_node = client->dev.of_node;
  191. config.ena_gpio = pdata->gpio_en;
  192. if (pdata->reg_data->constraints.boot_on)
  193. config.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  194. max8952->rdev = regulator_register(&regulator, &config);
  195. if (IS_ERR(max8952->rdev)) {
  196. ret = PTR_ERR(max8952->rdev);
  197. dev_err(max8952->dev, "regulator init failed (%d)\n", ret);
  198. return ret;
  199. }
  200. max8952->vid0 = pdata->default_mode & 0x1;
  201. max8952->vid1 = (pdata->default_mode >> 1) & 0x1;
  202. if (gpio_is_valid(pdata->gpio_vid0) &&
  203. gpio_is_valid(pdata->gpio_vid1)) {
  204. if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0"))
  205. gpio_direction_output(pdata->gpio_vid0,
  206. (pdata->default_mode) & 0x1);
  207. else
  208. err = 1;
  209. if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1"))
  210. gpio_direction_output(pdata->gpio_vid1,
  211. (pdata->default_mode >> 1) & 0x1);
  212. else {
  213. if (!err)
  214. gpio_free(pdata->gpio_vid0);
  215. err = 2;
  216. }
  217. } else
  218. err = 3;
  219. if (err) {
  220. dev_warn(max8952->dev, "VID0/1 gpio invalid: "
  221. "DVS not available.\n");
  222. max8952->vid0 = 0;
  223. max8952->vid1 = 0;
  224. /* Mark invalid */
  225. pdata->gpio_vid0 = -1;
  226. pdata->gpio_vid1 = -1;
  227. /* Disable Pulldown of EN only */
  228. max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
  229. dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1"
  230. " do not have proper controls.\n");
  231. } else {
  232. /*
  233. * Disable Pulldown on EN, VID0, VID1 to reduce
  234. * leakage current of MAX8952 assuming that MAX8952
  235. * is turned on (EN==1). Note that without having VID0/1
  236. * properly connected, turning pulldown off can be
  237. * problematic. Thus, turn this off only when they are
  238. * controllable by GPIO.
  239. */
  240. max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
  241. }
  242. max8952_write_reg(max8952, MAX8952_REG_MODE0,
  243. (max8952_read_reg(max8952,
  244. MAX8952_REG_MODE0) & 0xC0) |
  245. (pdata->dvs_mode[0] & 0x3F));
  246. max8952_write_reg(max8952, MAX8952_REG_MODE1,
  247. (max8952_read_reg(max8952,
  248. MAX8952_REG_MODE1) & 0xC0) |
  249. (pdata->dvs_mode[1] & 0x3F));
  250. max8952_write_reg(max8952, MAX8952_REG_MODE2,
  251. (max8952_read_reg(max8952,
  252. MAX8952_REG_MODE2) & 0xC0) |
  253. (pdata->dvs_mode[2] & 0x3F));
  254. max8952_write_reg(max8952, MAX8952_REG_MODE3,
  255. (max8952_read_reg(max8952,
  256. MAX8952_REG_MODE3) & 0xC0) |
  257. (pdata->dvs_mode[3] & 0x3F));
  258. max8952_write_reg(max8952, MAX8952_REG_SYNC,
  259. (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
  260. ((pdata->sync_freq & 0x3) << 6));
  261. max8952_write_reg(max8952, MAX8952_REG_RAMP,
  262. (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
  263. ((pdata->ramp_speed & 0x7) << 5));
  264. i2c_set_clientdata(client, max8952);
  265. return 0;
  266. }
  267. static int max8952_pmic_remove(struct i2c_client *client)
  268. {
  269. struct max8952_data *max8952 = i2c_get_clientdata(client);
  270. struct max8952_platform_data *pdata = max8952->pdata;
  271. struct regulator_dev *rdev = max8952->rdev;
  272. regulator_unregister(rdev);
  273. gpio_free(pdata->gpio_vid0);
  274. gpio_free(pdata->gpio_vid1);
  275. return 0;
  276. }
  277. static const struct i2c_device_id max8952_ids[] = {
  278. { "max8952", 0 },
  279. { },
  280. };
  281. MODULE_DEVICE_TABLE(i2c, max8952_ids);
  282. static struct i2c_driver max8952_pmic_driver = {
  283. .probe = max8952_pmic_probe,
  284. .remove = max8952_pmic_remove,
  285. .driver = {
  286. .name = "max8952",
  287. .of_match_table = of_match_ptr(max8952_dt_match),
  288. },
  289. .id_table = max8952_ids,
  290. };
  291. static int __init max8952_pmic_init(void)
  292. {
  293. return i2c_add_driver(&max8952_pmic_driver);
  294. }
  295. subsys_initcall(max8952_pmic_init);
  296. static void __exit max8952_pmic_exit(void)
  297. {
  298. i2c_del_driver(&max8952_pmic_driver);
  299. }
  300. module_exit(max8952_pmic_exit);
  301. MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
  302. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  303. MODULE_LICENSE("GPL");