max8660.c 13 KB

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