max8660.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. #include <linux/of.h>
  47. #include <linux/of_device.h>
  48. #include <linux/regulator/of_regulator.h>
  49. #define MAX8660_DCDC_MIN_UV 725000
  50. #define MAX8660_DCDC_MAX_UV 1800000
  51. #define MAX8660_DCDC_STEP 25000
  52. #define MAX8660_DCDC_MAX_SEL 0x2b
  53. #define MAX8660_LDO5_MIN_UV 1700000
  54. #define MAX8660_LDO5_MAX_UV 2000000
  55. #define MAX8660_LDO5_STEP 25000
  56. #define MAX8660_LDO5_MAX_SEL 0x0c
  57. #define MAX8660_LDO67_MIN_UV 1800000
  58. #define MAX8660_LDO67_MAX_UV 3300000
  59. #define MAX8660_LDO67_STEP 100000
  60. #define MAX8660_LDO67_MAX_SEL 0x0f
  61. enum {
  62. MAX8660_OVER1,
  63. MAX8660_OVER2,
  64. MAX8660_VCC1,
  65. MAX8660_ADTV1,
  66. MAX8660_ADTV2,
  67. MAX8660_SDTV1,
  68. MAX8660_SDTV2,
  69. MAX8660_MDTV1,
  70. MAX8660_MDTV2,
  71. MAX8660_L12VCR,
  72. MAX8660_FPWM,
  73. MAX8660_N_REGS, /* not a real register */
  74. };
  75. struct max8660 {
  76. struct i2c_client *client;
  77. u8 shadow_regs[MAX8660_N_REGS]; /* as chip is write only */
  78. };
  79. static int max8660_write(struct max8660 *max8660, u8 reg, u8 mask, u8 val)
  80. {
  81. static const u8 max8660_addresses[MAX8660_N_REGS] = {
  82. 0x10, 0x12, 0x20, 0x23, 0x24, 0x29, 0x2a, 0x32, 0x33, 0x39, 0x80
  83. };
  84. int ret;
  85. u8 reg_val = (max8660->shadow_regs[reg] & mask) | val;
  86. dev_vdbg(&max8660->client->dev, "Writing reg %02x with %02x\n",
  87. max8660_addresses[reg], reg_val);
  88. ret = i2c_smbus_write_byte_data(max8660->client,
  89. max8660_addresses[reg], reg_val);
  90. if (ret == 0)
  91. max8660->shadow_regs[reg] = reg_val;
  92. return ret;
  93. }
  94. /*
  95. * DCDC functions
  96. */
  97. static int max8660_dcdc_is_enabled(struct regulator_dev *rdev)
  98. {
  99. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  100. u8 val = max8660->shadow_regs[MAX8660_OVER1];
  101. u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
  102. return !!(val & mask);
  103. }
  104. static int max8660_dcdc_enable(struct regulator_dev *rdev)
  105. {
  106. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  107. u8 bit = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
  108. return max8660_write(max8660, MAX8660_OVER1, 0xff, bit);
  109. }
  110. static int max8660_dcdc_disable(struct regulator_dev *rdev)
  111. {
  112. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  113. u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? ~1 : ~4;
  114. return max8660_write(max8660, MAX8660_OVER1, mask, 0);
  115. }
  116. static int max8660_dcdc_get_voltage_sel(struct regulator_dev *rdev)
  117. {
  118. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  119. u8 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
  120. u8 selector = max8660->shadow_regs[reg];
  121. return selector;
  122. }
  123. static int max8660_dcdc_set_voltage_sel(struct regulator_dev *rdev,
  124. unsigned int selector)
  125. {
  126. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  127. u8 reg, bits;
  128. int ret;
  129. reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
  130. ret = max8660_write(max8660, reg, 0, selector);
  131. if (ret)
  132. return ret;
  133. /* Select target voltage register and activate regulation */
  134. bits = (rdev_get_id(rdev) == MAX8660_V3) ? 0x03 : 0x30;
  135. return max8660_write(max8660, MAX8660_VCC1, 0xff, bits);
  136. }
  137. static struct regulator_ops max8660_dcdc_ops = {
  138. .is_enabled = max8660_dcdc_is_enabled,
  139. .list_voltage = regulator_list_voltage_linear,
  140. .map_voltage = regulator_map_voltage_linear,
  141. .set_voltage_sel = max8660_dcdc_set_voltage_sel,
  142. .get_voltage_sel = max8660_dcdc_get_voltage_sel,
  143. };
  144. /*
  145. * LDO5 functions
  146. */
  147. static int max8660_ldo5_get_voltage_sel(struct regulator_dev *rdev)
  148. {
  149. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  150. u8 selector = max8660->shadow_regs[MAX8660_MDTV2];
  151. return selector;
  152. }
  153. static int max8660_ldo5_set_voltage_sel(struct regulator_dev *rdev,
  154. unsigned int selector)
  155. {
  156. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  157. int ret;
  158. ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector);
  159. if (ret)
  160. return ret;
  161. /* Select target voltage register and activate regulation */
  162. return max8660_write(max8660, MAX8660_VCC1, 0xff, 0xc0);
  163. }
  164. static const struct regulator_ops max8660_ldo5_ops = {
  165. .list_voltage = regulator_list_voltage_linear,
  166. .map_voltage = regulator_map_voltage_linear,
  167. .set_voltage_sel = max8660_ldo5_set_voltage_sel,
  168. .get_voltage_sel = max8660_ldo5_get_voltage_sel,
  169. };
  170. /*
  171. * LDO67 functions
  172. */
  173. static int max8660_ldo67_is_enabled(struct regulator_dev *rdev)
  174. {
  175. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  176. u8 val = max8660->shadow_regs[MAX8660_OVER2];
  177. u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
  178. return !!(val & mask);
  179. }
  180. static int max8660_ldo67_enable(struct regulator_dev *rdev)
  181. {
  182. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  183. u8 bit = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
  184. return max8660_write(max8660, MAX8660_OVER2, 0xff, bit);
  185. }
  186. static int max8660_ldo67_disable(struct regulator_dev *rdev)
  187. {
  188. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  189. u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? ~2 : ~4;
  190. return max8660_write(max8660, MAX8660_OVER2, mask, 0);
  191. }
  192. static int max8660_ldo67_get_voltage_sel(struct regulator_dev *rdev)
  193. {
  194. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  195. u8 shift = (rdev_get_id(rdev) == MAX8660_V6) ? 0 : 4;
  196. u8 selector = (max8660->shadow_regs[MAX8660_L12VCR] >> shift) & 0xf;
  197. return selector;
  198. }
  199. static int max8660_ldo67_set_voltage_sel(struct regulator_dev *rdev,
  200. unsigned int selector)
  201. {
  202. struct max8660 *max8660 = rdev_get_drvdata(rdev);
  203. if (rdev_get_id(rdev) == MAX8660_V6)
  204. return max8660_write(max8660, MAX8660_L12VCR, 0xf0, selector);
  205. else
  206. return max8660_write(max8660, MAX8660_L12VCR, 0x0f,
  207. selector << 4);
  208. }
  209. static const struct regulator_ops max8660_ldo67_ops = {
  210. .is_enabled = max8660_ldo67_is_enabled,
  211. .enable = max8660_ldo67_enable,
  212. .disable = max8660_ldo67_disable,
  213. .list_voltage = regulator_list_voltage_linear,
  214. .map_voltage = regulator_map_voltage_linear,
  215. .get_voltage_sel = max8660_ldo67_get_voltage_sel,
  216. .set_voltage_sel = max8660_ldo67_set_voltage_sel,
  217. };
  218. static const struct regulator_desc max8660_reg[] = {
  219. {
  220. .name = "V3(DCDC)",
  221. .id = MAX8660_V3,
  222. .ops = &max8660_dcdc_ops,
  223. .type = REGULATOR_VOLTAGE,
  224. .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
  225. .owner = THIS_MODULE,
  226. .min_uV = MAX8660_DCDC_MIN_UV,
  227. .uV_step = MAX8660_DCDC_STEP,
  228. },
  229. {
  230. .name = "V4(DCDC)",
  231. .id = MAX8660_V4,
  232. .ops = &max8660_dcdc_ops,
  233. .type = REGULATOR_VOLTAGE,
  234. .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
  235. .owner = THIS_MODULE,
  236. .min_uV = MAX8660_DCDC_MIN_UV,
  237. .uV_step = MAX8660_DCDC_STEP,
  238. },
  239. {
  240. .name = "V5(LDO)",
  241. .id = MAX8660_V5,
  242. .ops = &max8660_ldo5_ops,
  243. .type = REGULATOR_VOLTAGE,
  244. .n_voltages = MAX8660_LDO5_MAX_SEL + 1,
  245. .owner = THIS_MODULE,
  246. .min_uV = MAX8660_LDO5_MIN_UV,
  247. .uV_step = MAX8660_LDO5_STEP,
  248. },
  249. {
  250. .name = "V6(LDO)",
  251. .id = MAX8660_V6,
  252. .ops = &max8660_ldo67_ops,
  253. .type = REGULATOR_VOLTAGE,
  254. .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
  255. .owner = THIS_MODULE,
  256. .min_uV = MAX8660_LDO67_MIN_UV,
  257. .uV_step = MAX8660_LDO67_STEP,
  258. },
  259. {
  260. .name = "V7(LDO)",
  261. .id = MAX8660_V7,
  262. .ops = &max8660_ldo67_ops,
  263. .type = REGULATOR_VOLTAGE,
  264. .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
  265. .owner = THIS_MODULE,
  266. .min_uV = MAX8660_LDO67_MIN_UV,
  267. .uV_step = MAX8660_LDO67_STEP,
  268. },
  269. };
  270. enum {
  271. MAX8660 = 0,
  272. MAX8661 = 1,
  273. };
  274. #ifdef CONFIG_OF
  275. static const struct of_device_id max8660_dt_ids[] = {
  276. { .compatible = "maxim,max8660", .data = (void *) MAX8660 },
  277. { .compatible = "maxim,max8661", .data = (void *) MAX8661 },
  278. { }
  279. };
  280. MODULE_DEVICE_TABLE(of, max8660_dt_ids);
  281. static int max8660_pdata_from_dt(struct device *dev,
  282. struct device_node **of_node,
  283. struct max8660_platform_data *pdata)
  284. {
  285. int matched, i;
  286. struct device_node *np;
  287. struct max8660_subdev_data *sub;
  288. struct of_regulator_match rmatch[ARRAY_SIZE(max8660_reg)] = { };
  289. np = of_get_child_by_name(dev->of_node, "regulators");
  290. if (!np) {
  291. dev_err(dev, "missing 'regulators' subnode in DT\n");
  292. return -EINVAL;
  293. }
  294. for (i = 0; i < ARRAY_SIZE(rmatch); i++)
  295. rmatch[i].name = max8660_reg[i].name;
  296. matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch));
  297. of_node_put(np);
  298. if (matched <= 0)
  299. return matched;
  300. pdata->subdevs = devm_kzalloc(dev, sizeof(struct max8660_subdev_data) *
  301. matched, GFP_KERNEL);
  302. if (!pdata->subdevs)
  303. return -ENOMEM;
  304. pdata->num_subdevs = matched;
  305. sub = pdata->subdevs;
  306. for (i = 0; i < matched; i++) {
  307. sub->id = i;
  308. sub->name = rmatch[i].name;
  309. sub->platform_data = rmatch[i].init_data;
  310. of_node[i] = rmatch[i].of_node;
  311. sub++;
  312. }
  313. return 0;
  314. }
  315. #else
  316. static inline int max8660_pdata_from_dt(struct device *dev,
  317. struct device_node **of_node,
  318. struct max8660_platform_data *pdata)
  319. {
  320. return 0;
  321. }
  322. #endif
  323. static int max8660_probe(struct i2c_client *client,
  324. const struct i2c_device_id *i2c_id)
  325. {
  326. struct device *dev = &client->dev;
  327. struct max8660_platform_data pdata_of, *pdata = dev_get_platdata(dev);
  328. struct regulator_config config = { };
  329. struct max8660 *max8660;
  330. int boot_on, i, id, ret = -EINVAL;
  331. struct device_node *of_node[MAX8660_V_END];
  332. unsigned long type;
  333. if (dev->of_node && !pdata) {
  334. const struct of_device_id *id;
  335. id = of_match_device(of_match_ptr(max8660_dt_ids), dev);
  336. if (!id)
  337. return -ENODEV;
  338. ret = max8660_pdata_from_dt(dev, of_node, &pdata_of);
  339. if (ret < 0)
  340. return ret;
  341. pdata = &pdata_of;
  342. type = (unsigned long) id->data;
  343. } else {
  344. type = i2c_id->driver_data;
  345. memset(of_node, 0, sizeof(of_node));
  346. }
  347. if (pdata->num_subdevs > MAX8660_V_END) {
  348. dev_err(dev, "Too many regulators found!\n");
  349. return -EINVAL;
  350. }
  351. max8660 = devm_kzalloc(dev, sizeof(struct max8660), GFP_KERNEL);
  352. if (!max8660)
  353. return -ENOMEM;
  354. max8660->client = client;
  355. if (pdata->en34_is_high) {
  356. /* Simulate always on */
  357. max8660->shadow_regs[MAX8660_OVER1] = 5;
  358. } else {
  359. /* Otherwise devices can be toggled via software */
  360. max8660_dcdc_ops.enable = max8660_dcdc_enable;
  361. max8660_dcdc_ops.disable = max8660_dcdc_disable;
  362. }
  363. /*
  364. * First, set up shadow registers to prevent glitches. As some
  365. * registers are shared between regulators, everything must be properly
  366. * set up for all regulators in advance.
  367. */
  368. max8660->shadow_regs[MAX8660_ADTV1] =
  369. max8660->shadow_regs[MAX8660_ADTV2] =
  370. max8660->shadow_regs[MAX8660_SDTV1] =
  371. max8660->shadow_regs[MAX8660_SDTV2] = 0x1b;
  372. max8660->shadow_regs[MAX8660_MDTV1] =
  373. max8660->shadow_regs[MAX8660_MDTV2] = 0x04;
  374. for (i = 0; i < pdata->num_subdevs; i++) {
  375. if (!pdata->subdevs[i].platform_data)
  376. boot_on = false;
  377. else
  378. boot_on = pdata->subdevs[i].platform_data->constraints.boot_on;
  379. switch (pdata->subdevs[i].id) {
  380. case MAX8660_V3:
  381. if (boot_on)
  382. max8660->shadow_regs[MAX8660_OVER1] |= 1;
  383. break;
  384. case MAX8660_V4:
  385. if (boot_on)
  386. max8660->shadow_regs[MAX8660_OVER1] |= 4;
  387. break;
  388. case MAX8660_V5:
  389. break;
  390. case MAX8660_V6:
  391. if (boot_on)
  392. max8660->shadow_regs[MAX8660_OVER2] |= 2;
  393. break;
  394. case MAX8660_V7:
  395. if (type == MAX8661) {
  396. dev_err(dev, "Regulator not on this chip!\n");
  397. return -EINVAL;
  398. }
  399. if (boot_on)
  400. max8660->shadow_regs[MAX8660_OVER2] |= 4;
  401. break;
  402. default:
  403. dev_err(dev, "invalid regulator %s\n",
  404. pdata->subdevs[i].name);
  405. return ret;
  406. }
  407. }
  408. /* Finally register devices */
  409. for (i = 0; i < pdata->num_subdevs; i++) {
  410. struct regulator_dev *rdev;
  411. id = pdata->subdevs[i].id;
  412. config.dev = dev;
  413. config.init_data = pdata->subdevs[i].platform_data;
  414. config.of_node = of_node[i];
  415. config.driver_data = max8660;
  416. rdev = devm_regulator_register(&client->dev,
  417. &max8660_reg[id], &config);
  418. if (IS_ERR(rdev)) {
  419. ret = PTR_ERR(rdev);
  420. dev_err(&client->dev, "failed to register %s\n",
  421. max8660_reg[id].name);
  422. return PTR_ERR(rdev);
  423. }
  424. }
  425. i2c_set_clientdata(client, max8660);
  426. return 0;
  427. }
  428. static const struct i2c_device_id max8660_id[] = {
  429. { .name = "max8660", .driver_data = MAX8660 },
  430. { .name = "max8661", .driver_data = MAX8661 },
  431. { }
  432. };
  433. MODULE_DEVICE_TABLE(i2c, max8660_id);
  434. static struct i2c_driver max8660_driver = {
  435. .probe = max8660_probe,
  436. .driver = {
  437. .name = "max8660",
  438. },
  439. .id_table = max8660_id,
  440. };
  441. static int __init max8660_init(void)
  442. {
  443. return i2c_add_driver(&max8660_driver);
  444. }
  445. subsys_initcall(max8660_init);
  446. static void __exit max8660_exit(void)
  447. {
  448. i2c_del_driver(&max8660_driver);
  449. }
  450. module_exit(max8660_exit);
  451. /* Module information */
  452. MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver");
  453. MODULE_AUTHOR("Wolfram Sang");
  454. MODULE_LICENSE("GPL v2");