max8660.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * max8660.c -- Voltage regulation for the Maxim 8660/8661
  3. *
  4. * based on max1586.c and wm8400-regulator.c
  5. *
  6. * Copyright (C) 2009 Wolfram Sang, Pengutronix e.K.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. * Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Some info:
  22. *
  23. * Datasheet: http://datasheets.maxim-ic.com/en/ds/MAX8660-MAX8661.pdf
  24. *
  25. * This chip is a bit nasty because it is a write-only device. Thus, the driver
  26. * uses shadow registers to keep track of its values. The main problem appears
  27. * to be the initialization: When Linux boots up, we cannot know if the chip is
  28. * in the default state or not, so we would have to pass such information in
  29. * platform_data. As this adds a bit of complexity to the driver, this is left
  30. * out for now until it is really needed.
  31. *
  32. * [A|S|M]DTV1 registers are currently not used, but [A|S|M]DTV2.
  33. *
  34. * If the driver is feature complete, it might be worth to check if one set of
  35. * functions for V3-V7 is sufficient. For maximum flexibility during
  36. * development, they are separated for now.
  37. *
  38. */
  39. #include <linux/module.h>
  40. #include <linux/err.h>
  41. #include <linux/i2c.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/regulator/driver.h>
  44. #include <linux/slab.h>
  45. #include <linux/regulator/max8660.h>
  46. #define MAX8660_DCDC_MIN_UV 725000
  47. #define MAX8660_DCDC_MAX_UV 1800000
  48. #define MAX8660_DCDC_STEP 25000
  49. #define MAX8660_DCDC_MAX_SEL 0x2b
  50. #define MAX8660_LDO5_MIN_UV 1700000
  51. #define MAX8660_LDO5_MAX_UV 2000000
  52. #define MAX8660_LDO5_STEP 25000
  53. #define MAX8660_LDO5_MAX_SEL 0x0c
  54. #define MAX8660_LDO67_MIN_UV 1800000
  55. #define MAX8660_LDO67_MAX_UV 3300000
  56. #define MAX8660_LDO67_STEP 100000
  57. #define MAX8660_LDO67_MAX_SEL 0x0f
  58. enum {
  59. MAX8660_OVER1,
  60. MAX8660_OVER2,
  61. MAX8660_VCC1,
  62. MAX8660_ADTV1,
  63. MAX8660_ADTV2,
  64. MAX8660_SDTV1,
  65. MAX8660_SDTV2,
  66. MAX8660_MDTV1,
  67. MAX8660_MDTV2,
  68. MAX8660_L12VCR,
  69. MAX8660_FPWM,
  70. MAX8660_N_REGS, /* not a real register */
  71. };
  72. struct max8660 {
  73. struct i2c_client *client;
  74. u8 shadow_regs[MAX8660_N_REGS]; /* as chip is write only */
  75. struct regulator_dev *rdev[];
  76. };
  77. static int max8660_write(struct max8660 *max8660, u8 reg, u8 mask, u8 val)
  78. {
  79. static const u8 max8660_addresses[MAX8660_N_REGS] =
  80. { 0x10, 0x12, 0x20, 0x23, 0x24, 0x29, 0x2a, 0x32, 0x33, 0x39, 0x80 };
  81. int ret;
  82. u8 reg_val = (max8660->shadow_regs[reg] & mask) | val;
  83. dev_vdbg(&max8660->client->dev, "Writing reg %02x with %02x\n",
  84. max8660_addresses[reg], reg_val);
  85. ret = i2c_smbus_write_byte_data(max8660->client,
  86. max8660_addresses[reg], reg_val);
  87. if (ret == 0)
  88. max8660->shadow_regs[reg] = reg_val;
  89. return ret;
  90. }
  91. /*
  92. * DCDC functions
  93. */
  94. static int max8660_dcdc_is_enabled(struct regulator_dev *rdev)
  95. {
  96. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  97. u8 val = max8660->shadow_regs[MAX8660_OVER1];
  98. u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
  99. return !!(val & mask);
  100. }
  101. static int max8660_dcdc_enable(struct regulator_dev *rdev)
  102. {
  103. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  104. u8 bit = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
  105. return max8660_write(max8660, MAX8660_OVER1, 0xff, bit);
  106. }
  107. static int max8660_dcdc_disable(struct regulator_dev *rdev)
  108. {
  109. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  110. u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? ~1 : ~4;
  111. return max8660_write(max8660, MAX8660_OVER1, mask, 0);
  112. }
  113. static int max8660_dcdc_list(struct regulator_dev *rdev, unsigned selector)
  114. {
  115. if (selector > MAX8660_DCDC_MAX_SEL)
  116. return -EINVAL;
  117. return MAX8660_DCDC_MIN_UV + selector * MAX8660_DCDC_STEP;
  118. }
  119. static int max8660_dcdc_get(struct regulator_dev *rdev)
  120. {
  121. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  122. u8 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
  123. u8 selector = max8660->shadow_regs[reg];
  124. return MAX8660_DCDC_MIN_UV + selector * MAX8660_DCDC_STEP;
  125. }
  126. static int max8660_dcdc_set(struct regulator_dev *rdev, int min_uV, int max_uV,
  127. unsigned int *s)
  128. {
  129. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  130. u8 reg, selector, bits;
  131. int ret;
  132. if (min_uV < MAX8660_DCDC_MIN_UV || min_uV > MAX8660_DCDC_MAX_UV)
  133. return -EINVAL;
  134. if (max_uV < MAX8660_DCDC_MIN_UV || max_uV > MAX8660_DCDC_MAX_UV)
  135. return -EINVAL;
  136. selector = (min_uV - (MAX8660_DCDC_MIN_UV - MAX8660_DCDC_STEP + 1))
  137. / MAX8660_DCDC_STEP;
  138. *s = selector;
  139. ret = max8660_dcdc_list(rdev, selector);
  140. if (ret < 0 || ret > max_uV)
  141. return -EINVAL;
  142. reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
  143. ret = max8660_write(max8660, reg, 0, selector);
  144. if (ret)
  145. return ret;
  146. /* Select target voltage register and activate regulation */
  147. bits = (rdev_get_id(rdev) == MAX8660_V3) ? 0x03 : 0x30;
  148. return max8660_write(max8660, MAX8660_VCC1, 0xff, bits);
  149. }
  150. static struct regulator_ops max8660_dcdc_ops = {
  151. .is_enabled = max8660_dcdc_is_enabled,
  152. .list_voltage = max8660_dcdc_list,
  153. .set_voltage = max8660_dcdc_set,
  154. .get_voltage = max8660_dcdc_get,
  155. };
  156. /*
  157. * LDO5 functions
  158. */
  159. static int max8660_ldo5_list(struct regulator_dev *rdev, unsigned selector)
  160. {
  161. if (selector > MAX8660_LDO5_MAX_SEL)
  162. return -EINVAL;
  163. return MAX8660_LDO5_MIN_UV + selector * MAX8660_LDO5_STEP;
  164. }
  165. static int max8660_ldo5_get(struct regulator_dev *rdev)
  166. {
  167. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  168. u8 selector = max8660->shadow_regs[MAX8660_MDTV2];
  169. return MAX8660_LDO5_MIN_UV + selector * MAX8660_LDO5_STEP;
  170. }
  171. static int max8660_ldo5_set(struct regulator_dev *rdev, int min_uV, int max_uV,
  172. unsigned int *s)
  173. {
  174. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  175. u8 selector;
  176. int ret;
  177. if (min_uV < MAX8660_LDO5_MIN_UV || min_uV > MAX8660_LDO5_MAX_UV)
  178. return -EINVAL;
  179. if (max_uV < MAX8660_LDO5_MIN_UV || max_uV > MAX8660_LDO5_MAX_UV)
  180. return -EINVAL;
  181. selector = (min_uV - (MAX8660_LDO5_MIN_UV - MAX8660_LDO5_STEP + 1))
  182. / MAX8660_LDO5_STEP;
  183. ret = max8660_ldo5_list(rdev, selector);
  184. if (ret < 0 || ret > max_uV)
  185. return -EINVAL;
  186. *s = selector;
  187. ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector);
  188. if (ret)
  189. return ret;
  190. /* Select target voltage register and activate regulation */
  191. return max8660_write(max8660, MAX8660_VCC1, 0xff, 0xc0);
  192. }
  193. static struct regulator_ops max8660_ldo5_ops = {
  194. .list_voltage = max8660_ldo5_list,
  195. .set_voltage = max8660_ldo5_set,
  196. .get_voltage = max8660_ldo5_get,
  197. };
  198. /*
  199. * LDO67 functions
  200. */
  201. static int max8660_ldo67_is_enabled(struct regulator_dev *rdev)
  202. {
  203. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  204. u8 val = max8660->shadow_regs[MAX8660_OVER2];
  205. u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
  206. return !!(val & mask);
  207. }
  208. static int max8660_ldo67_enable(struct regulator_dev *rdev)
  209. {
  210. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  211. u8 bit = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
  212. return max8660_write(max8660, MAX8660_OVER2, 0xff, bit);
  213. }
  214. static int max8660_ldo67_disable(struct regulator_dev *rdev)
  215. {
  216. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  217. u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? ~2 : ~4;
  218. return max8660_write(max8660, MAX8660_OVER2, mask, 0);
  219. }
  220. static int max8660_ldo67_list(struct regulator_dev *rdev, unsigned selector)
  221. {
  222. if (selector > MAX8660_LDO67_MAX_SEL)
  223. return -EINVAL;
  224. return MAX8660_LDO67_MIN_UV + selector * MAX8660_LDO67_STEP;
  225. }
  226. static int max8660_ldo67_get(struct regulator_dev *rdev)
  227. {
  228. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  229. u8 shift = (rdev_get_id(rdev) == MAX8660_V6) ? 0 : 4;
  230. u8 selector = (max8660->shadow_regs[MAX8660_L12VCR] >> shift) & 0xf;
  231. return MAX8660_LDO67_MIN_UV + selector * MAX8660_LDO67_STEP;
  232. }
  233. static int max8660_ldo67_set(struct regulator_dev *rdev, int min_uV,
  234. int max_uV, unsigned int *s)
  235. {
  236. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  237. u8 selector;
  238. int ret;
  239. if (min_uV < MAX8660_LDO67_MIN_UV || min_uV > MAX8660_LDO67_MAX_UV)
  240. return -EINVAL;
  241. if (max_uV < MAX8660_LDO67_MIN_UV || max_uV > MAX8660_LDO67_MAX_UV)
  242. return -EINVAL;
  243. selector = (min_uV - (MAX8660_LDO67_MIN_UV - MAX8660_LDO67_STEP + 1))
  244. / MAX8660_LDO67_STEP;
  245. ret = max8660_ldo67_list(rdev, selector);
  246. if (ret < 0 || ret > max_uV)
  247. return -EINVAL;
  248. *s = selector;
  249. if (rdev_get_id(rdev) == MAX8660_V6)
  250. return max8660_write(max8660, MAX8660_L12VCR, 0xf0, selector);
  251. else
  252. return max8660_write(max8660, MAX8660_L12VCR, 0x0f, selector << 4);
  253. }
  254. static struct regulator_ops max8660_ldo67_ops = {
  255. .is_enabled = max8660_ldo67_is_enabled,
  256. .enable = max8660_ldo67_enable,
  257. .disable = max8660_ldo67_disable,
  258. .list_voltage = max8660_ldo67_list,
  259. .get_voltage = max8660_ldo67_get,
  260. .set_voltage = max8660_ldo67_set,
  261. };
  262. static struct regulator_desc max8660_reg[] = {
  263. {
  264. .name = "V3(DCDC)",
  265. .id = MAX8660_V3,
  266. .ops = &max8660_dcdc_ops,
  267. .type = REGULATOR_VOLTAGE,
  268. .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
  269. .owner = THIS_MODULE,
  270. },
  271. {
  272. .name = "V4(DCDC)",
  273. .id = MAX8660_V4,
  274. .ops = &max8660_dcdc_ops,
  275. .type = REGULATOR_VOLTAGE,
  276. .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
  277. .owner = THIS_MODULE,
  278. },
  279. {
  280. .name = "V5(LDO)",
  281. .id = MAX8660_V5,
  282. .ops = &max8660_ldo5_ops,
  283. .type = REGULATOR_VOLTAGE,
  284. .n_voltages = MAX8660_LDO5_MAX_SEL + 1,
  285. .owner = THIS_MODULE,
  286. },
  287. {
  288. .name = "V6(LDO)",
  289. .id = MAX8660_V6,
  290. .ops = &max8660_ldo67_ops,
  291. .type = REGULATOR_VOLTAGE,
  292. .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
  293. .owner = THIS_MODULE,
  294. },
  295. {
  296. .name = "V7(LDO)",
  297. .id = MAX8660_V7,
  298. .ops = &max8660_ldo67_ops,
  299. .type = REGULATOR_VOLTAGE,
  300. .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
  301. .owner = THIS_MODULE,
  302. },
  303. };
  304. static int __devinit max8660_probe(struct i2c_client *client,
  305. const struct i2c_device_id *i2c_id)
  306. {
  307. struct regulator_dev **rdev;
  308. struct max8660_platform_data *pdata = client->dev.platform_data;
  309. struct max8660 *max8660;
  310. int boot_on, i, id, ret = -EINVAL;
  311. if (pdata->num_subdevs > MAX8660_V_END) {
  312. dev_err(&client->dev, "Too many regulators found!\n");
  313. goto out;
  314. }
  315. max8660 = kzalloc(sizeof(struct max8660) +
  316. sizeof(struct regulator_dev *) * MAX8660_V_END,
  317. GFP_KERNEL);
  318. if (!max8660) {
  319. ret = -ENOMEM;
  320. goto out;
  321. }
  322. max8660->client = client;
  323. rdev = max8660->rdev;
  324. if (pdata->en34_is_high) {
  325. /* Simulate always on */
  326. max8660->shadow_regs[MAX8660_OVER1] = 5;
  327. } else {
  328. /* Otherwise devices can be toggled via software */
  329. max8660_dcdc_ops.enable = max8660_dcdc_enable;
  330. max8660_dcdc_ops.disable = max8660_dcdc_disable;
  331. }
  332. /*
  333. * First, set up shadow registers to prevent glitches. As some
  334. * registers are shared between regulators, everything must be properly
  335. * set up for all regulators in advance.
  336. */
  337. max8660->shadow_regs[MAX8660_ADTV1] =
  338. max8660->shadow_regs[MAX8660_ADTV2] =
  339. max8660->shadow_regs[MAX8660_SDTV1] =
  340. max8660->shadow_regs[MAX8660_SDTV2] = 0x1b;
  341. max8660->shadow_regs[MAX8660_MDTV1] =
  342. max8660->shadow_regs[MAX8660_MDTV2] = 0x04;
  343. for (i = 0; i < pdata->num_subdevs; i++) {
  344. if (!pdata->subdevs[i].platform_data)
  345. goto err_free;
  346. boot_on = pdata->subdevs[i].platform_data->constraints.boot_on;
  347. switch (pdata->subdevs[i].id) {
  348. case MAX8660_V3:
  349. if (boot_on)
  350. max8660->shadow_regs[MAX8660_OVER1] |= 1;
  351. break;
  352. case MAX8660_V4:
  353. if (boot_on)
  354. max8660->shadow_regs[MAX8660_OVER1] |= 4;
  355. break;
  356. case MAX8660_V5:
  357. break;
  358. case MAX8660_V6:
  359. if (boot_on)
  360. max8660->shadow_regs[MAX8660_OVER2] |= 2;
  361. break;
  362. case MAX8660_V7:
  363. if (!strcmp(i2c_id->name, "max8661")) {
  364. dev_err(&client->dev, "Regulator not on this chip!\n");
  365. goto err_free;
  366. }
  367. if (boot_on)
  368. max8660->shadow_regs[MAX8660_OVER2] |= 4;
  369. break;
  370. default:
  371. dev_err(&client->dev, "invalid regulator %s\n",
  372. pdata->subdevs[i].name);
  373. goto err_free;
  374. }
  375. }
  376. /* Finally register devices */
  377. for (i = 0; i < pdata->num_subdevs; i++) {
  378. id = pdata->subdevs[i].id;
  379. rdev[i] = regulator_register(&max8660_reg[id], &client->dev,
  380. pdata->subdevs[i].platform_data,
  381. max8660);
  382. if (IS_ERR(rdev[i])) {
  383. ret = PTR_ERR(rdev[i]);
  384. dev_err(&client->dev, "failed to register %s\n",
  385. max8660_reg[id].name);
  386. goto err_unregister;
  387. }
  388. }
  389. i2c_set_clientdata(client, max8660);
  390. dev_info(&client->dev, "Maxim 8660/8661 regulator driver loaded\n");
  391. return 0;
  392. err_unregister:
  393. while (--i >= 0)
  394. regulator_unregister(rdev[i]);
  395. err_free:
  396. kfree(max8660);
  397. out:
  398. return ret;
  399. }
  400. static int __devexit max8660_remove(struct i2c_client *client)
  401. {
  402. struct max8660 *max8660 = i2c_get_clientdata(client);
  403. int i;
  404. for (i = 0; i < MAX8660_V_END; i++)
  405. if (max8660->rdev[i])
  406. regulator_unregister(max8660->rdev[i]);
  407. kfree(max8660);
  408. return 0;
  409. }
  410. static const struct i2c_device_id max8660_id[] = {
  411. { "max8660", 0 },
  412. { "max8661", 0 },
  413. { }
  414. };
  415. MODULE_DEVICE_TABLE(i2c, max8660_id);
  416. static struct i2c_driver max8660_driver = {
  417. .probe = max8660_probe,
  418. .remove = __devexit_p(max8660_remove),
  419. .driver = {
  420. .name = "max8660",
  421. .owner = THIS_MODULE,
  422. },
  423. .id_table = max8660_id,
  424. };
  425. static int __init max8660_init(void)
  426. {
  427. return i2c_add_driver(&max8660_driver);
  428. }
  429. subsys_initcall(max8660_init);
  430. static void __exit max8660_exit(void)
  431. {
  432. i2c_del_driver(&max8660_driver);
  433. }
  434. module_exit(max8660_exit);
  435. /* Module information */
  436. MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver");
  437. MODULE_AUTHOR("Wolfram Sang");
  438. MODULE_LICENSE("GPL v2");