max8649.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Regulators driver for Maxim max8649
  3. *
  4. * Copyright (C) 2009-2010 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.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 version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/i2c.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/slab.h>
  18. #include <linux/regulator/max8649.h>
  19. #include <linux/regmap.h>
  20. #define MAX8649_DCDC_VMIN 750000 /* uV */
  21. #define MAX8649_DCDC_VMAX 1380000 /* uV */
  22. #define MAX8649_DCDC_STEP 10000 /* uV */
  23. #define MAX8649_VOL_MASK 0x3f
  24. /* Registers */
  25. #define MAX8649_MODE0 0x00
  26. #define MAX8649_MODE1 0x01
  27. #define MAX8649_MODE2 0x02
  28. #define MAX8649_MODE3 0x03
  29. #define MAX8649_CONTROL 0x04
  30. #define MAX8649_SYNC 0x05
  31. #define MAX8649_RAMP 0x06
  32. #define MAX8649_CHIP_ID1 0x08
  33. #define MAX8649_CHIP_ID2 0x09
  34. /* Bits */
  35. #define MAX8649_EN_PD (1 << 7)
  36. #define MAX8649_VID0_PD (1 << 6)
  37. #define MAX8649_VID1_PD (1 << 5)
  38. #define MAX8649_VID_MASK (3 << 5)
  39. #define MAX8649_FORCE_PWM (1 << 7)
  40. #define MAX8649_SYNC_EXTCLK (1 << 6)
  41. #define MAX8649_EXT_MASK (3 << 6)
  42. #define MAX8649_RAMP_MASK (7 << 5)
  43. #define MAX8649_RAMP_DOWN (1 << 1)
  44. struct max8649_regulator_info {
  45. struct regulator_dev *regulator;
  46. struct device *dev;
  47. struct regmap *regmap;
  48. int vol_reg;
  49. unsigned mode:2; /* bit[1:0] = VID1, VID0 */
  50. unsigned extclk_freq:2;
  51. unsigned extclk:1;
  52. unsigned ramp_timing:3;
  53. unsigned ramp_down:1;
  54. };
  55. /* I2C operations */
  56. static inline int check_range(int min_uV, int max_uV)
  57. {
  58. if ((min_uV < MAX8649_DCDC_VMIN) || (max_uV > MAX8649_DCDC_VMAX)
  59. || (min_uV > max_uV))
  60. return -EINVAL;
  61. return 0;
  62. }
  63. static int max8649_list_voltage(struct regulator_dev *rdev, unsigned index)
  64. {
  65. return (MAX8649_DCDC_VMIN + index * MAX8649_DCDC_STEP);
  66. }
  67. static int max8649_get_voltage(struct regulator_dev *rdev)
  68. {
  69. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  70. unsigned int val;
  71. unsigned char data;
  72. int ret;
  73. ret = regmap_read(info->regmap, info->vol_reg, &val);
  74. if (ret != 0)
  75. return ret;
  76. data = (unsigned char)val & MAX8649_VOL_MASK;
  77. return max8649_list_voltage(rdev, data);
  78. }
  79. static int max8649_set_voltage(struct regulator_dev *rdev,
  80. int min_uV, int max_uV, unsigned *selector)
  81. {
  82. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  83. unsigned char data, mask;
  84. if (check_range(min_uV, max_uV)) {
  85. dev_err(info->dev, "invalid voltage range (%d, %d) uV\n",
  86. min_uV, max_uV);
  87. return -EINVAL;
  88. }
  89. data = DIV_ROUND_UP(min_uV - MAX8649_DCDC_VMIN, MAX8649_DCDC_STEP);
  90. mask = MAX8649_VOL_MASK;
  91. *selector = data & mask;
  92. return regmap_update_bits(info->regmap, info->vol_reg, mask, data);
  93. }
  94. /* EN_PD means pulldown on EN input */
  95. static int max8649_enable(struct regulator_dev *rdev)
  96. {
  97. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  98. return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD, 0);
  99. }
  100. /*
  101. * Applied internal pulldown resistor on EN input pin.
  102. * If pulldown EN pin outside, it would be better.
  103. */
  104. static int max8649_disable(struct regulator_dev *rdev)
  105. {
  106. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  107. return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD,
  108. MAX8649_EN_PD);
  109. }
  110. static int max8649_is_enabled(struct regulator_dev *rdev)
  111. {
  112. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  113. unsigned int val;
  114. int ret;
  115. ret = regmap_read(info->regmap, MAX8649_CONTROL, &val);
  116. if (ret != 0)
  117. return ret;
  118. return !((unsigned char)val & MAX8649_EN_PD);
  119. }
  120. static int max8649_enable_time(struct regulator_dev *rdev)
  121. {
  122. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  123. int voltage, rate, ret;
  124. unsigned int val;
  125. /* get voltage */
  126. ret = regmap_read(info->regmap, info->vol_reg, &val);
  127. if (ret != 0)
  128. return ret;
  129. val &= MAX8649_VOL_MASK;
  130. voltage = max8649_list_voltage(rdev, (unsigned char)val); /* uV */
  131. /* get rate */
  132. ret = regmap_read(info->regmap, MAX8649_RAMP, &val);
  133. if (ret != 0)
  134. return ret;
  135. ret = (val & MAX8649_RAMP_MASK) >> 5;
  136. rate = (32 * 1000) >> ret; /* uV/uS */
  137. return DIV_ROUND_UP(voltage, rate);
  138. }
  139. static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode)
  140. {
  141. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  142. switch (mode) {
  143. case REGULATOR_MODE_FAST:
  144. regmap_update_bits(info->regmap, info->vol_reg, MAX8649_FORCE_PWM,
  145. MAX8649_FORCE_PWM);
  146. break;
  147. case REGULATOR_MODE_NORMAL:
  148. regmap_update_bits(info->regmap, info->vol_reg,
  149. MAX8649_FORCE_PWM, 0);
  150. break;
  151. default:
  152. return -EINVAL;
  153. }
  154. return 0;
  155. }
  156. static unsigned int max8649_get_mode(struct regulator_dev *rdev)
  157. {
  158. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  159. unsigned int val;
  160. int ret;
  161. ret = regmap_read(info->regmap, info->vol_reg, &val);
  162. if (ret != 0)
  163. return ret;
  164. if (val & MAX8649_FORCE_PWM)
  165. return REGULATOR_MODE_FAST;
  166. return REGULATOR_MODE_NORMAL;
  167. }
  168. static struct regulator_ops max8649_dcdc_ops = {
  169. .set_voltage = max8649_set_voltage,
  170. .get_voltage = max8649_get_voltage,
  171. .list_voltage = max8649_list_voltage,
  172. .enable = max8649_enable,
  173. .disable = max8649_disable,
  174. .is_enabled = max8649_is_enabled,
  175. .enable_time = max8649_enable_time,
  176. .set_mode = max8649_set_mode,
  177. .get_mode = max8649_get_mode,
  178. };
  179. static struct regulator_desc dcdc_desc = {
  180. .name = "max8649",
  181. .ops = &max8649_dcdc_ops,
  182. .type = REGULATOR_VOLTAGE,
  183. .n_voltages = 1 << 6,
  184. .owner = THIS_MODULE,
  185. };
  186. static struct regmap_config max8649_regmap_config = {
  187. .reg_bits = 8,
  188. .val_bits = 8,
  189. };
  190. static int __devinit max8649_regulator_probe(struct i2c_client *client,
  191. const struct i2c_device_id *id)
  192. {
  193. struct max8649_platform_data *pdata = client->dev.platform_data;
  194. struct max8649_regulator_info *info = NULL;
  195. unsigned int val;
  196. unsigned char data;
  197. int ret;
  198. info = kzalloc(sizeof(struct max8649_regulator_info), GFP_KERNEL);
  199. if (!info) {
  200. dev_err(&client->dev, "No enough memory\n");
  201. return -ENOMEM;
  202. }
  203. info->regmap = regmap_init_i2c(client, &max8649_regmap_config);
  204. if (IS_ERR(info->regmap)) {
  205. ret = PTR_ERR(info->regmap);
  206. dev_err(&client->dev, "Failed to allocate register map: %d\n", ret);
  207. goto fail;
  208. }
  209. info->dev = &client->dev;
  210. i2c_set_clientdata(client, info);
  211. info->mode = pdata->mode;
  212. switch (info->mode) {
  213. case 0:
  214. info->vol_reg = MAX8649_MODE0;
  215. break;
  216. case 1:
  217. info->vol_reg = MAX8649_MODE1;
  218. break;
  219. case 2:
  220. info->vol_reg = MAX8649_MODE2;
  221. break;
  222. case 3:
  223. info->vol_reg = MAX8649_MODE3;
  224. break;
  225. default:
  226. break;
  227. }
  228. ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val);
  229. if (ret != 0) {
  230. dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n",
  231. ret);
  232. goto out;
  233. }
  234. dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val);
  235. /* enable VID0 & VID1 */
  236. regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0);
  237. /* enable/disable external clock synchronization */
  238. info->extclk = pdata->extclk;
  239. data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0;
  240. regmap_update_bits(info->regmap, info->vol_reg, MAX8649_SYNC_EXTCLK, data);
  241. if (info->extclk) {
  242. /* set external clock frequency */
  243. info->extclk_freq = pdata->extclk_freq;
  244. regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK,
  245. info->extclk_freq << 6);
  246. }
  247. if (pdata->ramp_timing) {
  248. info->ramp_timing = pdata->ramp_timing;
  249. regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK,
  250. info->ramp_timing << 5);
  251. }
  252. info->ramp_down = pdata->ramp_down;
  253. if (info->ramp_down) {
  254. regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN,
  255. MAX8649_RAMP_DOWN);
  256. }
  257. info->regulator = regulator_register(&dcdc_desc, &client->dev,
  258. pdata->regulator, info, NULL);
  259. if (IS_ERR(info->regulator)) {
  260. dev_err(info->dev, "failed to register regulator %s\n",
  261. dcdc_desc.name);
  262. ret = PTR_ERR(info->regulator);
  263. goto out;
  264. }
  265. dev_info(info->dev, "Max8649 regulator device is detected.\n");
  266. return 0;
  267. out:
  268. regmap_exit(info->regmap);
  269. fail:
  270. kfree(info);
  271. return ret;
  272. }
  273. static int __devexit max8649_regulator_remove(struct i2c_client *client)
  274. {
  275. struct max8649_regulator_info *info = i2c_get_clientdata(client);
  276. if (info) {
  277. if (info->regulator)
  278. regulator_unregister(info->regulator);
  279. regmap_exit(info->regmap);
  280. kfree(info);
  281. }
  282. return 0;
  283. }
  284. static const struct i2c_device_id max8649_id[] = {
  285. { "max8649", 0 },
  286. { }
  287. };
  288. MODULE_DEVICE_TABLE(i2c, max8649_id);
  289. static struct i2c_driver max8649_driver = {
  290. .probe = max8649_regulator_probe,
  291. .remove = __devexit_p(max8649_regulator_remove),
  292. .driver = {
  293. .name = "max8649",
  294. },
  295. .id_table = max8649_id,
  296. };
  297. static int __init max8649_init(void)
  298. {
  299. return i2c_add_driver(&max8649_driver);
  300. }
  301. subsys_initcall(max8649_init);
  302. static void __exit max8649_exit(void)
  303. {
  304. i2c_del_driver(&max8649_driver);
  305. }
  306. module_exit(max8649_exit);
  307. /* Module information */
  308. MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
  309. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  310. MODULE_LICENSE("GPL");