max8649.c 9.8 KB

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