max8649.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 device *dev;
  46. struct regmap *regmap;
  47. unsigned mode:2; /* bit[1:0] = VID1, VID0 */
  48. unsigned extclk_freq:2;
  49. unsigned extclk:1;
  50. unsigned ramp_timing:3;
  51. unsigned ramp_down:1;
  52. };
  53. static int max8649_enable_time(struct regulator_dev *rdev)
  54. {
  55. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  56. int voltage, rate, ret;
  57. unsigned int val;
  58. /* get voltage */
  59. ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val);
  60. if (ret != 0)
  61. return ret;
  62. val &= MAX8649_VOL_MASK;
  63. voltage = regulator_list_voltage_linear(rdev, (unsigned char)val);
  64. /* get rate */
  65. ret = regmap_read(info->regmap, MAX8649_RAMP, &val);
  66. if (ret != 0)
  67. return ret;
  68. ret = (val & MAX8649_RAMP_MASK) >> 5;
  69. rate = (32 * 1000) >> ret; /* uV/uS */
  70. return DIV_ROUND_UP(voltage, rate);
  71. }
  72. static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode)
  73. {
  74. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  75. switch (mode) {
  76. case REGULATOR_MODE_FAST:
  77. regmap_update_bits(info->regmap, rdev->desc->vsel_reg,
  78. MAX8649_FORCE_PWM, MAX8649_FORCE_PWM);
  79. break;
  80. case REGULATOR_MODE_NORMAL:
  81. regmap_update_bits(info->regmap, rdev->desc->vsel_reg,
  82. MAX8649_FORCE_PWM, 0);
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. return 0;
  88. }
  89. static unsigned int max8649_get_mode(struct regulator_dev *rdev)
  90. {
  91. struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
  92. unsigned int val;
  93. int ret;
  94. ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val);
  95. if (ret != 0)
  96. return ret;
  97. if (val & MAX8649_FORCE_PWM)
  98. return REGULATOR_MODE_FAST;
  99. return REGULATOR_MODE_NORMAL;
  100. }
  101. static const struct regulator_ops max8649_dcdc_ops = {
  102. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  103. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  104. .list_voltage = regulator_list_voltage_linear,
  105. .map_voltage = regulator_map_voltage_linear,
  106. .enable = regulator_enable_regmap,
  107. .disable = regulator_disable_regmap,
  108. .is_enabled = regulator_is_enabled_regmap,
  109. .enable_time = max8649_enable_time,
  110. .set_mode = max8649_set_mode,
  111. .get_mode = max8649_get_mode,
  112. };
  113. static struct regulator_desc dcdc_desc = {
  114. .name = "max8649",
  115. .ops = &max8649_dcdc_ops,
  116. .type = REGULATOR_VOLTAGE,
  117. .n_voltages = 1 << 6,
  118. .owner = THIS_MODULE,
  119. .vsel_mask = MAX8649_VOL_MASK,
  120. .min_uV = MAX8649_DCDC_VMIN,
  121. .uV_step = MAX8649_DCDC_STEP,
  122. .enable_reg = MAX8649_CONTROL,
  123. .enable_mask = MAX8649_EN_PD,
  124. .enable_is_inverted = true,
  125. };
  126. static const struct regmap_config max8649_regmap_config = {
  127. .reg_bits = 8,
  128. .val_bits = 8,
  129. };
  130. static int max8649_regulator_probe(struct i2c_client *client,
  131. const struct i2c_device_id *id)
  132. {
  133. struct max8649_platform_data *pdata = dev_get_platdata(&client->dev);
  134. struct max8649_regulator_info *info = NULL;
  135. struct regulator_dev *regulator;
  136. struct regulator_config config = { };
  137. unsigned int val;
  138. unsigned char data;
  139. int ret;
  140. info = devm_kzalloc(&client->dev, sizeof(struct max8649_regulator_info),
  141. GFP_KERNEL);
  142. if (!info)
  143. return -ENOMEM;
  144. info->regmap = devm_regmap_init_i2c(client, &max8649_regmap_config);
  145. if (IS_ERR(info->regmap)) {
  146. ret = PTR_ERR(info->regmap);
  147. dev_err(&client->dev, "Failed to allocate register map: %d\n", ret);
  148. return ret;
  149. }
  150. info->dev = &client->dev;
  151. i2c_set_clientdata(client, info);
  152. info->mode = pdata->mode;
  153. switch (info->mode) {
  154. case 0:
  155. dcdc_desc.vsel_reg = MAX8649_MODE0;
  156. break;
  157. case 1:
  158. dcdc_desc.vsel_reg = MAX8649_MODE1;
  159. break;
  160. case 2:
  161. dcdc_desc.vsel_reg = MAX8649_MODE2;
  162. break;
  163. case 3:
  164. dcdc_desc.vsel_reg = MAX8649_MODE3;
  165. break;
  166. default:
  167. break;
  168. }
  169. ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val);
  170. if (ret != 0) {
  171. dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n",
  172. ret);
  173. return ret;
  174. }
  175. dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val);
  176. /* enable VID0 & VID1 */
  177. regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0);
  178. /* enable/disable external clock synchronization */
  179. info->extclk = pdata->extclk;
  180. data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0;
  181. regmap_update_bits(info->regmap, dcdc_desc.vsel_reg,
  182. MAX8649_SYNC_EXTCLK, data);
  183. if (info->extclk) {
  184. /* set external clock frequency */
  185. info->extclk_freq = pdata->extclk_freq;
  186. regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK,
  187. info->extclk_freq << 6);
  188. }
  189. if (pdata->ramp_timing) {
  190. info->ramp_timing = pdata->ramp_timing;
  191. regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK,
  192. info->ramp_timing << 5);
  193. }
  194. info->ramp_down = pdata->ramp_down;
  195. if (info->ramp_down) {
  196. regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN,
  197. MAX8649_RAMP_DOWN);
  198. }
  199. config.dev = &client->dev;
  200. config.init_data = pdata->regulator;
  201. config.driver_data = info;
  202. config.regmap = info->regmap;
  203. regulator = devm_regulator_register(&client->dev, &dcdc_desc,
  204. &config);
  205. if (IS_ERR(regulator)) {
  206. dev_err(info->dev, "failed to register regulator %s\n",
  207. dcdc_desc.name);
  208. return PTR_ERR(regulator);
  209. }
  210. return 0;
  211. }
  212. static const struct i2c_device_id max8649_id[] = {
  213. { "max8649", 0 },
  214. { }
  215. };
  216. MODULE_DEVICE_TABLE(i2c, max8649_id);
  217. static struct i2c_driver max8649_driver = {
  218. .probe = max8649_regulator_probe,
  219. .driver = {
  220. .name = "max8649",
  221. },
  222. .id_table = max8649_id,
  223. };
  224. static int __init max8649_init(void)
  225. {
  226. return i2c_add_driver(&max8649_driver);
  227. }
  228. subsys_initcall(max8649_init);
  229. static void __exit max8649_exit(void)
  230. {
  231. i2c_del_driver(&max8649_driver);
  232. }
  233. module_exit(max8649_exit);
  234. /* Module information */
  235. MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
  236. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  237. MODULE_LICENSE("GPL");