max8952.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/mutex.h>
  29. #include <linux/gpio.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. /* Registers */
  33. enum {
  34. MAX8952_REG_MODE0,
  35. MAX8952_REG_MODE1,
  36. MAX8952_REG_MODE2,
  37. MAX8952_REG_MODE3,
  38. MAX8952_REG_CONTROL,
  39. MAX8952_REG_SYNC,
  40. MAX8952_REG_RAMP,
  41. MAX8952_REG_CHIP_ID1,
  42. MAX8952_REG_CHIP_ID2,
  43. };
  44. struct max8952_data {
  45. struct i2c_client *client;
  46. struct device *dev;
  47. struct mutex mutex;
  48. struct max8952_platform_data *pdata;
  49. struct regulator_dev *rdev;
  50. bool vid0;
  51. bool vid1;
  52. bool en;
  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_voltage(struct max8952_data *max8952, u8 mode)
  67. {
  68. return (max8952->pdata->dvs_mode[mode] * 10 + 770) * 1000;
  69. }
  70. static int max8952_list_voltage(struct regulator_dev *rdev,
  71. unsigned int selector)
  72. {
  73. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  74. if (rdev_get_id(rdev) != 0)
  75. return -EINVAL;
  76. return max8952_voltage(max8952, selector);
  77. }
  78. static int max8952_is_enabled(struct regulator_dev *rdev)
  79. {
  80. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  81. return max8952->en;
  82. }
  83. static int max8952_enable(struct regulator_dev *rdev)
  84. {
  85. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  86. /* If not valid, assume "ALWAYS_HIGH" */
  87. if (gpio_is_valid(max8952->pdata->gpio_en))
  88. gpio_set_value(max8952->pdata->gpio_en, 1);
  89. max8952->en = true;
  90. return 0;
  91. }
  92. static int max8952_disable(struct regulator_dev *rdev)
  93. {
  94. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  95. /* If not valid, assume "ALWAYS_HIGH" -> not permitted */
  96. if (gpio_is_valid(max8952->pdata->gpio_en))
  97. gpio_set_value(max8952->pdata->gpio_en, 0);
  98. else
  99. return -EPERM;
  100. max8952->en = false;
  101. return 0;
  102. }
  103. static int max8952_get_voltage(struct regulator_dev *rdev)
  104. {
  105. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  106. u8 vid = 0;
  107. if (max8952->vid0)
  108. vid += 1;
  109. if (max8952->vid1)
  110. vid += 2;
  111. return max8952_voltage(max8952, vid);
  112. }
  113. static int max8952_set_voltage(struct regulator_dev *rdev,
  114. int min_uV, int max_uV, unsigned *selector)
  115. {
  116. struct max8952_data *max8952 = rdev_get_drvdata(rdev);
  117. s8 vid = -1, i;
  118. if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
  119. !gpio_is_valid(max8952->pdata->gpio_vid1)) {
  120. /* DVS not supported */
  121. return -EPERM;
  122. }
  123. for (i = 0; i < MAX8952_NUM_DVS_MODE; i++) {
  124. int volt = max8952_voltage(max8952, i);
  125. /* Set the voltage as low as possible within the range */
  126. if (volt <= max_uV && volt >= min_uV)
  127. if (vid == -1 || max8952_voltage(max8952, vid) > volt)
  128. vid = i;
  129. }
  130. if (vid >= 0 && vid < MAX8952_NUM_DVS_MODE) {
  131. max8952->vid0 = (vid % 2 == 1);
  132. max8952->vid1 = (((vid >> 1) % 2) == 1);
  133. *selector = vid;
  134. gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
  135. gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
  136. } else
  137. return -EINVAL;
  138. return 0;
  139. }
  140. static struct regulator_ops max8952_ops = {
  141. .list_voltage = max8952_list_voltage,
  142. .is_enabled = max8952_is_enabled,
  143. .enable = max8952_enable,
  144. .disable = max8952_disable,
  145. .get_voltage = max8952_get_voltage,
  146. .set_voltage = max8952_set_voltage,
  147. .set_suspend_disable = max8952_disable,
  148. };
  149. static struct regulator_desc regulator = {
  150. .name = "MAX8952_VOUT",
  151. .id = 0,
  152. .n_voltages = MAX8952_NUM_DVS_MODE,
  153. .ops = &max8952_ops,
  154. .type = REGULATOR_VOLTAGE,
  155. .owner = THIS_MODULE,
  156. };
  157. static int __devinit max8952_pmic_probe(struct i2c_client *client,
  158. const struct i2c_device_id *i2c_id)
  159. {
  160. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  161. struct max8952_platform_data *pdata = client->dev.platform_data;
  162. struct max8952_data *max8952;
  163. int ret = 0, err = 0;
  164. if (!pdata) {
  165. dev_err(&client->dev, "Require the platform data\n");
  166. return -EINVAL;
  167. }
  168. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  169. return -EIO;
  170. max8952 = kzalloc(sizeof(struct max8952_data), GFP_KERNEL);
  171. if (!max8952)
  172. return -ENOMEM;
  173. max8952->client = client;
  174. max8952->dev = &client->dev;
  175. max8952->pdata = pdata;
  176. mutex_init(&max8952->mutex);
  177. max8952->rdev = regulator_register(&regulator, max8952->dev,
  178. &pdata->reg_data, max8952);
  179. if (IS_ERR(max8952->rdev)) {
  180. ret = PTR_ERR(max8952->rdev);
  181. dev_err(max8952->dev, "regulator init failed (%d)\n", ret);
  182. goto err_reg;
  183. }
  184. max8952->en = !!(pdata->reg_data.constraints.boot_on);
  185. max8952->vid0 = (pdata->default_mode % 2) == 1;
  186. max8952->vid1 = ((pdata->default_mode >> 1) % 2) == 1;
  187. if (gpio_is_valid(pdata->gpio_en)) {
  188. if (!gpio_request(pdata->gpio_en, "MAX8952 EN"))
  189. gpio_direction_output(pdata->gpio_en, max8952->en);
  190. else
  191. err = 1;
  192. } else
  193. err = 2;
  194. if (err) {
  195. dev_info(max8952->dev, "EN gpio invalid: assume that EN"
  196. "is always High\n");
  197. max8952->en = 1;
  198. pdata->gpio_en = -1; /* Mark invalid */
  199. }
  200. err = 0;
  201. if (gpio_is_valid(pdata->gpio_vid0) &&
  202. gpio_is_valid(pdata->gpio_vid1)) {
  203. if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0"))
  204. gpio_direction_output(pdata->gpio_vid0,
  205. (pdata->default_mode) % 2);
  206. else
  207. err = 1;
  208. if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1"))
  209. gpio_direction_output(pdata->gpio_vid1,
  210. (pdata->default_mode >> 1) % 2);
  211. else {
  212. if (!err)
  213. gpio_free(pdata->gpio_vid0);
  214. err = 2;
  215. }
  216. } else
  217. err = 3;
  218. if (err) {
  219. dev_warn(max8952->dev, "VID0/1 gpio invalid: "
  220. "DVS not available.\n");
  221. max8952->vid0 = 0;
  222. max8952->vid1 = 0;
  223. /* Mark invalid */
  224. pdata->gpio_vid0 = -1;
  225. pdata->gpio_vid1 = -1;
  226. /* Disable Pulldown of EN only */
  227. max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
  228. dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1"
  229. " do not have proper controls.\n");
  230. } else {
  231. /*
  232. * Disable Pulldown on EN, VID0, VID1 to reduce
  233. * leakage current of MAX8952 assuming that MAX8952
  234. * is turned on (EN==1). Note that without having VID0/1
  235. * properly connected, turning pulldown off can be
  236. * problematic. Thus, turn this off only when they are
  237. * controllable by GPIO.
  238. */
  239. max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
  240. }
  241. max8952_write_reg(max8952, MAX8952_REG_MODE0,
  242. (max8952_read_reg(max8952,
  243. MAX8952_REG_MODE0) & 0xC0) |
  244. (pdata->dvs_mode[0] & 0x3F));
  245. max8952_write_reg(max8952, MAX8952_REG_MODE1,
  246. (max8952_read_reg(max8952,
  247. MAX8952_REG_MODE1) & 0xC0) |
  248. (pdata->dvs_mode[1] & 0x3F));
  249. max8952_write_reg(max8952, MAX8952_REG_MODE2,
  250. (max8952_read_reg(max8952,
  251. MAX8952_REG_MODE2) & 0xC0) |
  252. (pdata->dvs_mode[2] & 0x3F));
  253. max8952_write_reg(max8952, MAX8952_REG_MODE3,
  254. (max8952_read_reg(max8952,
  255. MAX8952_REG_MODE3) & 0xC0) |
  256. (pdata->dvs_mode[3] & 0x3F));
  257. max8952_write_reg(max8952, MAX8952_REG_SYNC,
  258. (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
  259. ((pdata->sync_freq & 0x3) << 6));
  260. max8952_write_reg(max8952, MAX8952_REG_RAMP,
  261. (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
  262. ((pdata->ramp_speed & 0x7) << 5));
  263. i2c_set_clientdata(client, max8952);
  264. return 0;
  265. err_reg:
  266. kfree(max8952);
  267. return ret;
  268. }
  269. static int __devexit max8952_pmic_remove(struct i2c_client *client)
  270. {
  271. struct max8952_data *max8952 = i2c_get_clientdata(client);
  272. struct max8952_platform_data *pdata = max8952->pdata;
  273. struct regulator_dev *rdev = max8952->rdev;
  274. regulator_unregister(rdev);
  275. gpio_free(pdata->gpio_vid0);
  276. gpio_free(pdata->gpio_vid1);
  277. gpio_free(pdata->gpio_en);
  278. kfree(max8952);
  279. return 0;
  280. }
  281. static const struct i2c_device_id max8952_ids[] = {
  282. { "max8952", 0 },
  283. { },
  284. };
  285. MODULE_DEVICE_TABLE(i2c, max8952_ids);
  286. static struct i2c_driver max8952_pmic_driver = {
  287. .probe = max8952_pmic_probe,
  288. .remove = __devexit_p(max8952_pmic_remove),
  289. .driver = {
  290. .name = "max8952",
  291. },
  292. .id_table = max8952_ids,
  293. };
  294. static int __init max8952_pmic_init(void)
  295. {
  296. return i2c_add_driver(&max8952_pmic_driver);
  297. }
  298. subsys_initcall(max8952_pmic_init);
  299. static void __exit max8952_pmic_exit(void)
  300. {
  301. i2c_del_driver(&max8952_pmic_driver);
  302. }
  303. module_exit(max8952_pmic_exit);
  304. MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
  305. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  306. MODULE_LICENSE("GPL");