max8952.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/slab.h>
  31. /* Registers */
  32. enum {
  33. MAX8952_REG_MODE0,
  34. MAX8952_REG_MODE1,
  35. MAX8952_REG_MODE2,
  36. MAX8952_REG_MODE3,
  37. MAX8952_REG_CONTROL,
  38. MAX8952_REG_SYNC,
  39. MAX8952_REG_RAMP,
  40. MAX8952_REG_CHIP_ID1,
  41. MAX8952_REG_CHIP_ID2,
  42. };
  43. struct max8952_data {
  44. struct i2c_client *client;
  45. struct device *dev;
  46. struct max8952_platform_data *pdata;
  47. struct regulator_dev *rdev;
  48. bool vid0;
  49. bool vid1;
  50. bool en;
  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_voltage(struct max8952_data *max8952, u8 mode)
  65. {
  66. return (max8952->pdata->dvs_mode[mode] * 10 + 770) * 1000;
  67. }
  68. static int max8952_list_voltage(struct regulator_dev *rdev,
  69. unsigned int selector)
  70. {
  71. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  72. if (rdev_get_id(rdev) != 0)
  73. return -EINVAL;
  74. return max8952_voltage(max8952, selector);
  75. }
  76. static int max8952_is_enabled(struct regulator_dev *rdev)
  77. {
  78. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  79. return max8952->en;
  80. }
  81. static int max8952_enable(struct regulator_dev *rdev)
  82. {
  83. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  84. /* If not valid, assume "ALWAYS_HIGH" */
  85. if (gpio_is_valid(max8952->pdata->gpio_en))
  86. gpio_set_value(max8952->pdata->gpio_en, 1);
  87. max8952->en = true;
  88. return 0;
  89. }
  90. static int max8952_disable(struct regulator_dev *rdev)
  91. {
  92. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  93. /* If not valid, assume "ALWAYS_HIGH" -> not permitted */
  94. if (gpio_is_valid(max8952->pdata->gpio_en))
  95. gpio_set_value(max8952->pdata->gpio_en, 0);
  96. else
  97. return -EPERM;
  98. max8952->en = false;
  99. return 0;
  100. }
  101. static int max8952_get_voltage(struct regulator_dev *rdev)
  102. {
  103. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  104. u8 vid = 0;
  105. if (max8952->vid0)
  106. vid += 1;
  107. if (max8952->vid1)
  108. vid += 2;
  109. return max8952_voltage(max8952, vid);
  110. }
  111. static int max8952_set_voltage(struct regulator_dev *rdev,
  112. int min_uV, int max_uV, unsigned *selector)
  113. {
  114. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  115. s8 vid = -1, i;
  116. if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
  117. !gpio_is_valid(max8952->pdata->gpio_vid1)) {
  118. /* DVS not supported */
  119. return -EPERM;
  120. }
  121. for (i = 0; i < MAX8952_NUM_DVS_MODE; i++) {
  122. int volt = max8952_voltage(max8952, i);
  123. /* Set the voltage as low as possible within the range */
  124. if (volt <= max_uV && volt >= min_uV)
  125. if (vid == -1 || max8952_voltage(max8952, vid) > volt)
  126. vid = i;
  127. }
  128. if (vid >= 0 && vid < MAX8952_NUM_DVS_MODE) {
  129. max8952->vid0 = (vid % 2 == 1);
  130. max8952->vid1 = (((vid >> 1) % 2) == 1);
  131. *selector = vid;
  132. gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
  133. gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
  134. } else
  135. return -EINVAL;
  136. return 0;
  137. }
  138. static struct regulator_ops max8952_ops = {
  139. .list_voltage = max8952_list_voltage,
  140. .is_enabled = max8952_is_enabled,
  141. .enable = max8952_enable,
  142. .disable = max8952_disable,
  143. .get_voltage = max8952_get_voltage,
  144. .set_voltage = max8952_set_voltage,
  145. .set_suspend_disable = max8952_disable,
  146. };
  147. static struct regulator_desc regulator = {
  148. .name = "MAX8952_VOUT",
  149. .id = 0,
  150. .n_voltages = MAX8952_NUM_DVS_MODE,
  151. .ops = &max8952_ops,
  152. .type = REGULATOR_VOLTAGE,
  153. .owner = THIS_MODULE,
  154. };
  155. static int __devinit max8952_pmic_probe(struct i2c_client *client,
  156. const struct i2c_device_id *i2c_id)
  157. {
  158. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  159. struct max8952_platform_data *pdata = client->dev.platform_data;
  160. struct max8952_data *max8952;
  161. int ret = 0, err = 0;
  162. if (!pdata) {
  163. dev_err(&client->dev, "Require the platform data\n");
  164. return -EINVAL;
  165. }
  166. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  167. return -EIO;
  168. max8952 = kzalloc(sizeof(struct max8952_data), GFP_KERNEL);
  169. if (!max8952)
  170. return -ENOMEM;
  171. max8952->client = client;
  172. max8952->dev = &client->dev;
  173. max8952->pdata = pdata;
  174. max8952->rdev = regulator_register(&regulator, max8952->dev,
  175. &pdata->reg_data, max8952, NULL);
  176. if (IS_ERR(max8952->rdev)) {
  177. ret = PTR_ERR(max8952->rdev);
  178. dev_err(max8952->dev, "regulator init failed (%d)\n", ret);
  179. goto err_reg;
  180. }
  181. max8952->en = !!(pdata->reg_data.constraints.boot_on);
  182. max8952->vid0 = (pdata->default_mode % 2) == 1;
  183. max8952->vid1 = ((pdata->default_mode >> 1) % 2) == 1;
  184. if (gpio_is_valid(pdata->gpio_en)) {
  185. if (!gpio_request(pdata->gpio_en, "MAX8952 EN"))
  186. gpio_direction_output(pdata->gpio_en, max8952->en);
  187. else
  188. err = 1;
  189. } else
  190. err = 2;
  191. if (err) {
  192. dev_info(max8952->dev, "EN gpio invalid: assume that EN"
  193. "is always High\n");
  194. max8952->en = 1;
  195. pdata->gpio_en = -1; /* Mark invalid */
  196. }
  197. err = 0;
  198. if (gpio_is_valid(pdata->gpio_vid0) &&
  199. gpio_is_valid(pdata->gpio_vid1)) {
  200. if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0"))
  201. gpio_direction_output(pdata->gpio_vid0,
  202. (pdata->default_mode) % 2);
  203. else
  204. err = 1;
  205. if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1"))
  206. gpio_direction_output(pdata->gpio_vid1,
  207. (pdata->default_mode >> 1) % 2);
  208. else {
  209. if (!err)
  210. gpio_free(pdata->gpio_vid0);
  211. err = 2;
  212. }
  213. } else
  214. err = 3;
  215. if (err) {
  216. dev_warn(max8952->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(max8952->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. err_reg:
  263. kfree(max8952);
  264. return ret;
  265. }
  266. static int __devexit max8952_pmic_remove(struct i2c_client *client)
  267. {
  268. struct max8952_data *max8952 = i2c_get_clientdata(client);
  269. struct max8952_platform_data *pdata = max8952->pdata;
  270. struct regulator_dev *rdev = max8952->rdev;
  271. regulator_unregister(rdev);
  272. gpio_free(pdata->gpio_vid0);
  273. gpio_free(pdata->gpio_vid1);
  274. gpio_free(pdata->gpio_en);
  275. kfree(max8952);
  276. return 0;
  277. }
  278. static const struct i2c_device_id max8952_ids[] = {
  279. { "max8952", 0 },
  280. { },
  281. };
  282. MODULE_DEVICE_TABLE(i2c, max8952_ids);
  283. static struct i2c_driver max8952_pmic_driver = {
  284. .probe = max8952_pmic_probe,
  285. .remove = __devexit_p(max8952_pmic_remove),
  286. .driver = {
  287. .name = "max8952",
  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");