2 * wm8350.c -- Voltage and current regulation for the Wolfson WM8350 PMIC
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood
7 * linux@wolfsonmicro.com
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/bitops.h>
19 #include <linux/err.h>
20 #include <linux/i2c.h>
21 #include <linux/mfd/wm8350/core.h>
22 #include <linux/mfd/wm8350/pmic.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/machine.h>
28 static const int isink_cur[] = {
95 static int get_isink_val(int min_uA, int max_uA, u16 *setting)
99 for (i = ARRAY_SIZE(isink_cur) - 1; i >= 0; i--) {
100 if (min_uA <= isink_cur[i] && max_uA >= isink_cur[i]) {
108 static inline int wm8350_ldo_val_to_mvolts(unsigned int val)
111 return (val * 50) + 900;
113 return ((val - 16) * 100) + 1800;
117 static inline unsigned int wm8350_ldo_mvolts_to_val(int mV)
120 return (mV - 900) / 50;
122 return ((mV - 1800) / 100) + 16;
125 static inline int wm8350_dcdc_val_to_mvolts(unsigned int val)
127 return (val * 25) + 850;
130 static inline unsigned int wm8350_dcdc_mvolts_to_val(int mV)
132 return (mV - 850) / 25;
135 static int wm8350_isink_set_current(struct regulator_dev *rdev, int min_uA,
138 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
139 int isink = rdev_get_id(rdev);
143 ret = get_isink_val(min_uA, max_uA, &setting);
149 val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
150 ~WM8350_CS1_ISEL_MASK;
151 wm8350_reg_write(wm8350, WM8350_CURRENT_SINK_DRIVER_A,
155 val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
156 ~WM8350_CS1_ISEL_MASK;
157 wm8350_reg_write(wm8350, WM8350_CURRENT_SINK_DRIVER_B,
167 static int wm8350_isink_get_current(struct regulator_dev *rdev)
169 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
170 int isink = rdev_get_id(rdev);
175 val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
176 WM8350_CS1_ISEL_MASK;
179 val = wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
180 WM8350_CS1_ISEL_MASK;
186 return (isink_cur[val] + 50) / 100;
189 /* turn on ISINK followed by DCDC */
190 static int wm8350_isink_enable(struct regulator_dev *rdev)
192 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
193 int isink = rdev_get_id(rdev);
197 switch (wm8350->pmic.isink_A_dcdc) {
200 wm8350_set_bits(wm8350, WM8350_POWER_MGMT_7,
202 wm8350_set_bits(wm8350, WM8350_CSA_FLASH_CONTROL,
204 wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
205 1 << (wm8350->pmic.isink_A_dcdc -
213 switch (wm8350->pmic.isink_B_dcdc) {
216 wm8350_set_bits(wm8350, WM8350_POWER_MGMT_7,
218 wm8350_set_bits(wm8350, WM8350_CSB_FLASH_CONTROL,
220 wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
221 1 << (wm8350->pmic.isink_B_dcdc -
234 static int wm8350_isink_disable(struct regulator_dev *rdev)
236 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
237 int isink = rdev_get_id(rdev);
241 switch (wm8350->pmic.isink_A_dcdc) {
244 wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
245 1 << (wm8350->pmic.isink_A_dcdc -
247 wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_7,
255 switch (wm8350->pmic.isink_B_dcdc) {
258 wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED,
259 1 << (wm8350->pmic.isink_B_dcdc -
261 wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_7,
274 static int wm8350_isink_is_enabled(struct regulator_dev *rdev)
276 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
277 int isink = rdev_get_id(rdev);
281 return wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_A) &
284 return wm8350_reg_read(wm8350, WM8350_CURRENT_SINK_DRIVER_B) &
290 int wm8350_isink_set_flash(struct wm8350 *wm8350, int isink, u16 mode,
291 u16 trigger, u16 duration, u16 on_ramp, u16 off_ramp,
296 wm8350_reg_write(wm8350, WM8350_CSA_FLASH_CONTROL,
297 (mode ? WM8350_CS1_FLASH_MODE : 0) |
298 (trigger ? WM8350_CS1_TRIGSRC : 0) |
299 duration | on_ramp | off_ramp | drive);
302 wm8350_reg_write(wm8350, WM8350_CSB_FLASH_CONTROL,
303 (mode ? WM8350_CS2_FLASH_MODE : 0) |
304 (trigger ? WM8350_CS2_TRIGSRC : 0) |
305 duration | on_ramp | off_ramp | drive);
312 EXPORT_SYMBOL_GPL(wm8350_isink_set_flash);
314 static int wm8350_dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
317 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
318 int volt_reg, dcdc = rdev_get_id(rdev), mV,
319 min_mV = min_uV / 1000, max_mV = max_uV / 1000;
322 if (min_mV < 850 || min_mV > 4025)
324 if (max_mV < 850 || max_mV > 4025)
327 /* step size is 25mV */
328 mV = (min_mV - 826) / 25;
329 if (wm8350_dcdc_val_to_mvolts(mV) > max_mV)
331 BUG_ON(wm8350_dcdc_val_to_mvolts(mV) < min_mV);
335 volt_reg = WM8350_DCDC1_CONTROL;
338 volt_reg = WM8350_DCDC3_CONTROL;
341 volt_reg = WM8350_DCDC4_CONTROL;
344 volt_reg = WM8350_DCDC6_CONTROL;
352 /* all DCDCs have same mV bits */
353 val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_DC1_VSEL_MASK;
354 wm8350_reg_write(wm8350, volt_reg, val | mV);
358 static int wm8350_dcdc_get_voltage(struct regulator_dev *rdev)
360 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
361 int volt_reg, dcdc = rdev_get_id(rdev);
366 volt_reg = WM8350_DCDC1_CONTROL;
369 volt_reg = WM8350_DCDC3_CONTROL;
372 volt_reg = WM8350_DCDC4_CONTROL;
375 volt_reg = WM8350_DCDC6_CONTROL;
383 /* all DCDCs have same mV bits */
384 val = wm8350_reg_read(wm8350, volt_reg) & WM8350_DC1_VSEL_MASK;
385 return wm8350_dcdc_val_to_mvolts(val) * 1000;
388 static int wm8350_dcdc_set_suspend_voltage(struct regulator_dev *rdev, int uV)
390 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
391 int volt_reg, mV = uV / 1000, dcdc = rdev_get_id(rdev);
394 dev_dbg(wm8350->dev, "%s %d mV %d\n", __func__, dcdc, mV);
396 if (mV && (mV < 850 || mV > 4025)) {
398 "DCDC%d suspend voltage %d mV out of range\n",
407 volt_reg = WM8350_DCDC1_LOW_POWER;
410 volt_reg = WM8350_DCDC3_LOW_POWER;
413 volt_reg = WM8350_DCDC4_LOW_POWER;
416 volt_reg = WM8350_DCDC6_LOW_POWER;
424 /* all DCDCs have same mV bits */
425 val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_DC1_VSEL_MASK;
426 wm8350_reg_write(wm8350, volt_reg,
427 val | wm8350_dcdc_mvolts_to_val(mV));
431 static int wm8350_dcdc_set_suspend_enable(struct regulator_dev *rdev)
433 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
434 int dcdc = rdev_get_id(rdev);
439 val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER)
440 & ~WM8350_DCDC_HIB_MODE_MASK;
441 wm8350_reg_write(wm8350, WM8350_DCDC1_LOW_POWER,
442 wm8350->pmic.dcdc1_hib_mode);
445 val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER)
446 & ~WM8350_DCDC_HIB_MODE_MASK;
447 wm8350_reg_write(wm8350, WM8350_DCDC3_LOW_POWER,
448 wm8350->pmic.dcdc3_hib_mode);
451 val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER)
452 & ~WM8350_DCDC_HIB_MODE_MASK;
453 wm8350_reg_write(wm8350, WM8350_DCDC4_LOW_POWER,
454 wm8350->pmic.dcdc4_hib_mode);
457 val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER)
458 & ~WM8350_DCDC_HIB_MODE_MASK;
459 wm8350_reg_write(wm8350, WM8350_DCDC6_LOW_POWER,
460 wm8350->pmic.dcdc6_hib_mode);
471 static int wm8350_dcdc_set_suspend_disable(struct regulator_dev *rdev)
473 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
474 int dcdc = rdev_get_id(rdev);
479 val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER);
480 wm8350->pmic.dcdc1_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
481 wm8350_reg_write(wm8350, WM8350_DCDC1_LOW_POWER,
482 WM8350_DCDC_HIB_MODE_DIS);
485 val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER);
486 wm8350->pmic.dcdc3_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
487 wm8350_reg_write(wm8350, WM8350_DCDC3_LOW_POWER,
488 WM8350_DCDC_HIB_MODE_DIS);
491 val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER);
492 wm8350->pmic.dcdc4_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
493 wm8350_reg_write(wm8350, WM8350_DCDC4_LOW_POWER,
494 WM8350_DCDC_HIB_MODE_DIS);
497 val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER);
498 wm8350->pmic.dcdc6_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
499 wm8350_reg_write(wm8350, WM8350_DCDC6_LOW_POWER,
500 WM8350_DCDC_HIB_MODE_DIS);
511 static int wm8350_dcdc25_set_suspend_enable(struct regulator_dev *rdev)
513 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
514 int dcdc = rdev_get_id(rdev);
519 val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
520 & ~WM8350_DC2_HIB_MODE_MASK;
521 wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
522 WM8350_DC2_HIB_MODE_ACTIVE);
525 val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
526 & ~WM8350_DC2_HIB_MODE_MASK;
527 wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
528 WM8350_DC5_HIB_MODE_ACTIVE);
536 static int wm8350_dcdc25_set_suspend_disable(struct regulator_dev *rdev)
538 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
539 int dcdc = rdev_get_id(rdev);
544 val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
545 & ~WM8350_DC2_HIB_MODE_MASK;
546 wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
547 WM8350_DC2_HIB_MODE_DISABLE);
550 val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
551 & ~WM8350_DC2_HIB_MODE_MASK;
552 wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
553 WM8350_DC2_HIB_MODE_DISABLE);
561 static int wm8350_dcdc_set_suspend_mode(struct regulator_dev *rdev,
564 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
565 int dcdc = rdev_get_id(rdev);
570 hib_mode = &wm8350->pmic.dcdc1_hib_mode;
573 hib_mode = &wm8350->pmic.dcdc3_hib_mode;
576 hib_mode = &wm8350->pmic.dcdc4_hib_mode;
579 hib_mode = &wm8350->pmic.dcdc6_hib_mode;
588 case REGULATOR_MODE_NORMAL:
589 *hib_mode = WM8350_DCDC_HIB_MODE_IMAGE;
591 case REGULATOR_MODE_IDLE:
592 *hib_mode = WM8350_DCDC_HIB_MODE_STANDBY;
594 case REGULATOR_MODE_STANDBY:
595 *hib_mode = WM8350_DCDC_HIB_MODE_LDO_IM;
604 static int wm8350_ldo_set_suspend_voltage(struct regulator_dev *rdev, int uV)
606 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
607 int volt_reg, mV = uV / 1000, ldo = rdev_get_id(rdev);
610 dev_dbg(wm8350->dev, "%s %d mV %d\n", __func__, ldo, mV);
612 if (mV < 900 || mV > 3300) {
613 dev_err(wm8350->dev, "LDO%d voltage %d mV out of range\n",
620 volt_reg = WM8350_LDO1_LOW_POWER;
623 volt_reg = WM8350_LDO2_LOW_POWER;
626 volt_reg = WM8350_LDO3_LOW_POWER;
629 volt_reg = WM8350_LDO4_LOW_POWER;
635 /* all LDOs have same mV bits */
636 val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_VSEL_MASK;
637 wm8350_reg_write(wm8350, volt_reg,
638 val | wm8350_ldo_mvolts_to_val(mV));
642 static int wm8350_ldo_set_suspend_enable(struct regulator_dev *rdev)
644 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
645 int volt_reg, ldo = rdev_get_id(rdev);
650 volt_reg = WM8350_LDO1_LOW_POWER;
653 volt_reg = WM8350_LDO2_LOW_POWER;
656 volt_reg = WM8350_LDO3_LOW_POWER;
659 volt_reg = WM8350_LDO4_LOW_POWER;
665 /* all LDOs have same mV bits */
666 val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_HIB_MODE_MASK;
667 wm8350_reg_write(wm8350, volt_reg, val);
671 static int wm8350_ldo_set_suspend_disable(struct regulator_dev *rdev)
673 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
674 int volt_reg, ldo = rdev_get_id(rdev);
679 volt_reg = WM8350_LDO1_LOW_POWER;
682 volt_reg = WM8350_LDO2_LOW_POWER;
685 volt_reg = WM8350_LDO3_LOW_POWER;
688 volt_reg = WM8350_LDO4_LOW_POWER;
694 /* all LDOs have same mV bits */
695 val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_HIB_MODE_MASK;
696 wm8350_reg_write(wm8350, volt_reg, WM8350_LDO1_HIB_MODE_DIS);
700 static int wm8350_ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
703 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
704 int volt_reg, ldo = rdev_get_id(rdev), mV, min_mV = min_uV / 1000,
705 max_mV = max_uV / 1000;
708 if (min_mV < 900 || min_mV > 3300)
710 if (max_mV < 900 || max_mV > 3300)
714 /* step size is 50mV < 1800mV */
715 mV = (min_mV - 851) / 50;
716 if (wm8350_ldo_val_to_mvolts(mV) > max_mV)
718 BUG_ON(wm8350_ldo_val_to_mvolts(mV) < min_mV);
720 /* step size is 100mV > 1800mV */
721 mV = ((min_mV - 1701) / 100) + 16;
722 if (wm8350_ldo_val_to_mvolts(mV) > max_mV)
724 BUG_ON(wm8350_ldo_val_to_mvolts(mV) < min_mV);
729 volt_reg = WM8350_LDO1_CONTROL;
732 volt_reg = WM8350_LDO2_CONTROL;
735 volt_reg = WM8350_LDO3_CONTROL;
738 volt_reg = WM8350_LDO4_CONTROL;
744 /* all LDOs have same mV bits */
745 val = wm8350_reg_read(wm8350, volt_reg) & ~WM8350_LDO1_VSEL_MASK;
746 wm8350_reg_write(wm8350, volt_reg, val | mV);
750 static int wm8350_ldo_get_voltage(struct regulator_dev *rdev)
752 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
753 int volt_reg, ldo = rdev_get_id(rdev);
758 volt_reg = WM8350_LDO1_CONTROL;
761 volt_reg = WM8350_LDO2_CONTROL;
764 volt_reg = WM8350_LDO3_CONTROL;
767 volt_reg = WM8350_LDO4_CONTROL;
773 /* all LDOs have same mV bits */
774 val = wm8350_reg_read(wm8350, volt_reg) & WM8350_LDO1_VSEL_MASK;
775 return wm8350_ldo_val_to_mvolts(val) * 1000;
778 int wm8350_dcdc_set_slot(struct wm8350 *wm8350, int dcdc, u16 start,
784 dev_dbg(wm8350->dev, "%s %d start %d stop %d\n",
785 __func__, dcdc, start, stop);
788 if (start > 15 || stop > 15)
793 slot_reg = WM8350_DCDC1_TIMEOUTS;
796 slot_reg = WM8350_DCDC2_TIMEOUTS;
799 slot_reg = WM8350_DCDC3_TIMEOUTS;
802 slot_reg = WM8350_DCDC4_TIMEOUTS;
805 slot_reg = WM8350_DCDC5_TIMEOUTS;
808 slot_reg = WM8350_DCDC6_TIMEOUTS;
814 val = wm8350_reg_read(wm8350, slot_reg) &
815 ~(WM8350_DC1_ENSLOT_MASK | WM8350_DC1_SDSLOT_MASK |
816 WM8350_DC1_ERRACT_MASK);
817 wm8350_reg_write(wm8350, slot_reg,
818 val | (start << WM8350_DC1_ENSLOT_SHIFT) |
819 (stop << WM8350_DC1_SDSLOT_SHIFT) |
820 (fault << WM8350_DC1_ERRACT_SHIFT));
824 EXPORT_SYMBOL_GPL(wm8350_dcdc_set_slot);
826 int wm8350_ldo_set_slot(struct wm8350 *wm8350, int ldo, u16 start, u16 stop)
831 dev_dbg(wm8350->dev, "%s %d start %d stop %d\n",
832 __func__, ldo, start, stop);
835 if (start > 15 || stop > 15)
840 slot_reg = WM8350_LDO1_TIMEOUTS;
843 slot_reg = WM8350_LDO2_TIMEOUTS;
846 slot_reg = WM8350_LDO3_TIMEOUTS;
849 slot_reg = WM8350_LDO4_TIMEOUTS;
855 val = wm8350_reg_read(wm8350, slot_reg) & ~WM8350_LDO1_SDSLOT_MASK;
856 wm8350_reg_write(wm8350, slot_reg, val | ((start << 10) | (stop << 6)));
859 EXPORT_SYMBOL_GPL(wm8350_ldo_set_slot);
861 int wm8350_dcdc25_set_mode(struct wm8350 *wm8350, int dcdc, u16 mode,
862 u16 ilim, u16 ramp, u16 feedback)
866 dev_dbg(wm8350->dev, "%s %d mode: %s %s\n", __func__, dcdc,
867 mode ? "normal" : "boost", ilim ? "low" : "normal");
871 val = wm8350_reg_read(wm8350, WM8350_DCDC2_CONTROL)
872 & ~(WM8350_DC2_MODE_MASK | WM8350_DC2_ILIM_MASK |
873 WM8350_DC2_RMP_MASK | WM8350_DC2_FBSRC_MASK);
874 wm8350_reg_write(wm8350, WM8350_DCDC2_CONTROL, val |
875 (mode << WM8350_DC2_MODE_SHIFT) |
876 (ilim << WM8350_DC2_ILIM_SHIFT) |
877 (ramp << WM8350_DC2_RMP_SHIFT) |
878 (feedback << WM8350_DC2_FBSRC_SHIFT));
881 val = wm8350_reg_read(wm8350, WM8350_DCDC5_CONTROL)
882 & ~(WM8350_DC5_MODE_MASK | WM8350_DC5_ILIM_MASK |
883 WM8350_DC5_RMP_MASK | WM8350_DC5_FBSRC_MASK);
884 wm8350_reg_write(wm8350, WM8350_DCDC5_CONTROL, val |
885 (mode << WM8350_DC5_MODE_SHIFT) |
886 (ilim << WM8350_DC5_ILIM_SHIFT) |
887 (ramp << WM8350_DC5_RMP_SHIFT) |
888 (feedback << WM8350_DC5_FBSRC_SHIFT));
896 EXPORT_SYMBOL_GPL(wm8350_dcdc25_set_mode);
898 static int wm8350_dcdc_enable(struct regulator_dev *rdev)
900 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
901 int dcdc = rdev_get_id(rdev);
904 if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
907 shift = dcdc - WM8350_DCDC_1;
908 wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED, 1 << shift);
912 static int wm8350_dcdc_disable(struct regulator_dev *rdev)
914 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
915 int dcdc = rdev_get_id(rdev);
918 if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
921 shift = dcdc - WM8350_DCDC_1;
922 wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED, 1 << shift);
927 static int wm8350_ldo_enable(struct regulator_dev *rdev)
929 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
930 int ldo = rdev_get_id(rdev);
933 if (ldo < WM8350_LDO_1 || ldo > WM8350_LDO_4)
936 shift = (ldo - WM8350_LDO_1) + 8;
937 wm8350_set_bits(wm8350, WM8350_DCDC_LDO_REQUESTED, 1 << shift);
941 static int wm8350_ldo_disable(struct regulator_dev *rdev)
943 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
944 int ldo = rdev_get_id(rdev);
947 if (ldo < WM8350_LDO_1 || ldo > WM8350_LDO_4)
950 shift = (ldo - WM8350_LDO_1) + 8;
951 wm8350_clear_bits(wm8350, WM8350_DCDC_LDO_REQUESTED, 1 << shift);
955 static int force_continuous_enable(struct wm8350 *wm8350, int dcdc, int enable)
961 reg = WM8350_DCDC1_FORCE_PWM;
964 reg = WM8350_DCDC3_FORCE_PWM;
967 reg = WM8350_DCDC4_FORCE_PWM;
970 reg = WM8350_DCDC6_FORCE_PWM;
977 ret = wm8350_set_bits(wm8350, reg,
978 WM8350_DCDC1_FORCE_PWM_ENA);
980 ret = wm8350_clear_bits(wm8350, reg,
981 WM8350_DCDC1_FORCE_PWM_ENA);
985 static int wm8350_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
987 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
988 int dcdc = rdev_get_id(rdev);
991 if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
994 if (dcdc == WM8350_DCDC_2 || dcdc == WM8350_DCDC_5)
997 val = 1 << (dcdc - WM8350_DCDC_1);
1000 case REGULATOR_MODE_FAST:
1001 /* force continuous mode */
1002 wm8350_set_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
1003 wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
1004 force_continuous_enable(wm8350, dcdc, 1);
1006 case REGULATOR_MODE_NORMAL:
1007 /* active / pulse skipping */
1008 wm8350_set_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
1009 wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
1010 force_continuous_enable(wm8350, dcdc, 0);
1012 case REGULATOR_MODE_IDLE:
1014 force_continuous_enable(wm8350, dcdc, 0);
1015 wm8350_clear_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
1016 wm8350_clear_bits(wm8350, WM8350_DCDC_ACTIVE_OPTIONS, val);
1018 case REGULATOR_MODE_STANDBY:
1020 force_continuous_enable(wm8350, dcdc, 0);
1021 wm8350_set_bits(wm8350, WM8350_DCDC_SLEEP_OPTIONS, val);
1028 static unsigned int wm8350_dcdc_get_mode(struct regulator_dev *rdev)
1030 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
1031 int dcdc = rdev_get_id(rdev);
1032 u16 mask, sleep, active, force;
1033 int mode = REGULATOR_MODE_NORMAL;
1035 if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
1038 if (dcdc == WM8350_DCDC_2 || dcdc == WM8350_DCDC_5)
1041 mask = 1 << (dcdc - WM8350_DCDC_1);
1042 active = wm8350_reg_read(wm8350, WM8350_DCDC_ACTIVE_OPTIONS) & mask;
1043 sleep = wm8350_reg_read(wm8350, WM8350_DCDC_SLEEP_OPTIONS) & mask;
1044 force = wm8350_reg_read(wm8350, WM8350_DCDC1_FORCE_PWM)
1045 & WM8350_DCDC1_FORCE_PWM_ENA;
1046 dev_dbg(wm8350->dev, "mask %x active %x sleep %x force %x",
1047 mask, active, sleep, force);
1049 if (active && !sleep) {
1051 mode = REGULATOR_MODE_FAST;
1053 mode = REGULATOR_MODE_NORMAL;
1054 } else if (!active && !sleep)
1055 mode = REGULATOR_MODE_IDLE;
1057 mode = REGULATOR_MODE_STANDBY;
1062 static unsigned int wm8350_ldo_get_mode(struct regulator_dev *rdev)
1064 return REGULATOR_MODE_NORMAL;
1067 struct wm8350_dcdc_efficiency {
1073 static const struct wm8350_dcdc_efficiency dcdc1_6_efficiency[] = {
1074 {0, 10000, REGULATOR_MODE_STANDBY}, /* 0 - 10mA - LDO */
1075 {10000, 100000, REGULATOR_MODE_IDLE}, /* 10mA - 100mA - Standby */
1076 {100000, 1000000, REGULATOR_MODE_NORMAL}, /* > 100mA - Active */
1077 {-1, -1, REGULATOR_MODE_NORMAL},
1080 static const struct wm8350_dcdc_efficiency dcdc3_4_efficiency[] = {
1081 {0, 10000, REGULATOR_MODE_STANDBY}, /* 0 - 10mA - LDO */
1082 {10000, 100000, REGULATOR_MODE_IDLE}, /* 10mA - 100mA - Standby */
1083 {100000, 800000, REGULATOR_MODE_NORMAL}, /* > 100mA - Active */
1084 {-1, -1, REGULATOR_MODE_NORMAL},
1087 static unsigned int get_mode(int uA, const struct wm8350_dcdc_efficiency *eff)
1091 while (eff[i].uA_load_min != -1) {
1092 if (uA >= eff[i].uA_load_min && uA <= eff[i].uA_load_max)
1095 return REGULATOR_MODE_NORMAL;
1098 /* Query the regulator for it's most efficient mode @ uV,uA
1099 * WM8350 regulator efficiency is pretty similar over
1100 * different input and output uV.
1102 static unsigned int wm8350_dcdc_get_optimum_mode(struct regulator_dev *rdev,
1103 int input_uV, int output_uV,
1106 int dcdc = rdev_get_id(rdev), mode;
1111 mode = get_mode(output_uA, dcdc1_6_efficiency);
1115 mode = get_mode(output_uA, dcdc3_4_efficiency);
1118 mode = REGULATOR_MODE_NORMAL;
1124 static int wm8350_dcdc_is_enabled(struct regulator_dev *rdev)
1126 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
1127 int dcdc = rdev_get_id(rdev), shift;
1129 if (dcdc < WM8350_DCDC_1 || dcdc > WM8350_DCDC_6)
1132 shift = dcdc - WM8350_DCDC_1;
1133 return wm8350_reg_read(wm8350, WM8350_DCDC_LDO_REQUESTED)
1137 static int wm8350_ldo_is_enabled(struct regulator_dev *rdev)
1139 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
1140 int ldo = rdev_get_id(rdev), shift;
1142 if (ldo < WM8350_LDO_1 || ldo > WM8350_LDO_4)
1145 shift = (ldo - WM8350_LDO_1) + 8;
1146 return wm8350_reg_read(wm8350, WM8350_DCDC_LDO_REQUESTED)
1150 static struct regulator_ops wm8350_dcdc_ops = {
1151 .set_voltage = wm8350_dcdc_set_voltage,
1152 .get_voltage = wm8350_dcdc_get_voltage,
1153 .enable = wm8350_dcdc_enable,
1154 .disable = wm8350_dcdc_disable,
1155 .get_mode = wm8350_dcdc_get_mode,
1156 .set_mode = wm8350_dcdc_set_mode,
1157 .get_optimum_mode = wm8350_dcdc_get_optimum_mode,
1158 .is_enabled = wm8350_dcdc_is_enabled,
1159 .set_suspend_voltage = wm8350_dcdc_set_suspend_voltage,
1160 .set_suspend_enable = wm8350_dcdc_set_suspend_enable,
1161 .set_suspend_disable = wm8350_dcdc_set_suspend_disable,
1162 .set_suspend_mode = wm8350_dcdc_set_suspend_mode,
1165 static struct regulator_ops wm8350_dcdc2_5_ops = {
1166 .enable = wm8350_dcdc_enable,
1167 .disable = wm8350_dcdc_disable,
1168 .is_enabled = wm8350_dcdc_is_enabled,
1169 .set_suspend_enable = wm8350_dcdc25_set_suspend_enable,
1170 .set_suspend_disable = wm8350_dcdc25_set_suspend_disable,
1173 static struct regulator_ops wm8350_ldo_ops = {
1174 .set_voltage = wm8350_ldo_set_voltage,
1175 .get_voltage = wm8350_ldo_get_voltage,
1176 .enable = wm8350_ldo_enable,
1177 .disable = wm8350_ldo_disable,
1178 .is_enabled = wm8350_ldo_is_enabled,
1179 .get_mode = wm8350_ldo_get_mode,
1180 .set_suspend_voltage = wm8350_ldo_set_suspend_voltage,
1181 .set_suspend_enable = wm8350_ldo_set_suspend_enable,
1182 .set_suspend_disable = wm8350_ldo_set_suspend_disable,
1185 static struct regulator_ops wm8350_isink_ops = {
1186 .set_current_limit = wm8350_isink_set_current,
1187 .get_current_limit = wm8350_isink_get_current,
1188 .enable = wm8350_isink_enable,
1189 .disable = wm8350_isink_disable,
1190 .is_enabled = wm8350_isink_is_enabled,
1193 static struct regulator_desc wm8350_reg[NUM_WM8350_REGULATORS] = {
1196 .id = WM8350_DCDC_1,
1197 .ops = &wm8350_dcdc_ops,
1198 .irq = WM8350_IRQ_UV_DC1,
1199 .type = REGULATOR_VOLTAGE,
1200 .owner = THIS_MODULE,
1204 .id = WM8350_DCDC_2,
1205 .ops = &wm8350_dcdc2_5_ops,
1206 .irq = WM8350_IRQ_UV_DC2,
1207 .type = REGULATOR_VOLTAGE,
1208 .owner = THIS_MODULE,
1212 .id = WM8350_DCDC_3,
1213 .ops = &wm8350_dcdc_ops,
1214 .irq = WM8350_IRQ_UV_DC3,
1215 .type = REGULATOR_VOLTAGE,
1216 .owner = THIS_MODULE,
1220 .id = WM8350_DCDC_4,
1221 .ops = &wm8350_dcdc_ops,
1222 .irq = WM8350_IRQ_UV_DC4,
1223 .type = REGULATOR_VOLTAGE,
1224 .owner = THIS_MODULE,
1228 .id = WM8350_DCDC_5,
1229 .ops = &wm8350_dcdc2_5_ops,
1230 .irq = WM8350_IRQ_UV_DC5,
1231 .type = REGULATOR_VOLTAGE,
1232 .owner = THIS_MODULE,
1236 .id = WM8350_DCDC_6,
1237 .ops = &wm8350_dcdc_ops,
1238 .irq = WM8350_IRQ_UV_DC6,
1239 .type = REGULATOR_VOLTAGE,
1240 .owner = THIS_MODULE,
1245 .ops = &wm8350_ldo_ops,
1246 .irq = WM8350_IRQ_UV_LDO1,
1247 .type = REGULATOR_VOLTAGE,
1248 .owner = THIS_MODULE,
1253 .ops = &wm8350_ldo_ops,
1254 .irq = WM8350_IRQ_UV_LDO2,
1255 .type = REGULATOR_VOLTAGE,
1256 .owner = THIS_MODULE,
1261 .ops = &wm8350_ldo_ops,
1262 .irq = WM8350_IRQ_UV_LDO3,
1263 .type = REGULATOR_VOLTAGE,
1264 .owner = THIS_MODULE,
1269 .ops = &wm8350_ldo_ops,
1270 .irq = WM8350_IRQ_UV_LDO4,
1271 .type = REGULATOR_VOLTAGE,
1272 .owner = THIS_MODULE,
1276 .id = WM8350_ISINK_A,
1277 .ops = &wm8350_isink_ops,
1278 .irq = WM8350_IRQ_CS1,
1279 .type = REGULATOR_CURRENT,
1280 .owner = THIS_MODULE,
1284 .id = WM8350_ISINK_B,
1285 .ops = &wm8350_isink_ops,
1286 .irq = WM8350_IRQ_CS2,
1287 .type = REGULATOR_CURRENT,
1288 .owner = THIS_MODULE,
1292 static void pmic_uv_handler(struct wm8350 *wm8350, int irq, void *data)
1294 struct regulator_dev *rdev = (struct regulator_dev *)data;
1296 if (irq == WM8350_IRQ_CS1 || irq == WM8350_IRQ_CS2)
1297 regulator_notifier_call_chain(rdev,
1298 REGULATOR_EVENT_REGULATION_OUT,
1301 regulator_notifier_call_chain(rdev,
1302 REGULATOR_EVENT_UNDER_VOLTAGE,
1306 static int wm8350_regulator_probe(struct platform_device *pdev)
1308 struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
1309 struct regulator_dev *rdev;
1313 if (pdev->id < WM8350_DCDC_1 || pdev->id > WM8350_ISINK_B)
1316 /* do any regulatior specific init */
1319 val = wm8350_reg_read(wm8350, WM8350_DCDC1_LOW_POWER);
1320 wm8350->pmic.dcdc1_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
1323 val = wm8350_reg_read(wm8350, WM8350_DCDC3_LOW_POWER);
1324 wm8350->pmic.dcdc3_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
1327 val = wm8350_reg_read(wm8350, WM8350_DCDC4_LOW_POWER);
1328 wm8350->pmic.dcdc4_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
1331 val = wm8350_reg_read(wm8350, WM8350_DCDC6_LOW_POWER);
1332 wm8350->pmic.dcdc6_hib_mode = val & WM8350_DCDC_HIB_MODE_MASK;
1337 /* register regulator */
1338 rdev = regulator_register(&wm8350_reg[pdev->id], &pdev->dev,
1339 dev_get_drvdata(&pdev->dev));
1341 dev_err(&pdev->dev, "failed to register %s\n",
1342 wm8350_reg[pdev->id].name);
1343 return PTR_ERR(rdev);
1346 /* register regulator IRQ */
1347 ret = wm8350_register_irq(wm8350, wm8350_reg[pdev->id].irq,
1348 pmic_uv_handler, rdev);
1350 regulator_unregister(rdev);
1351 dev_err(&pdev->dev, "failed to register regulator %s IRQ\n",
1352 wm8350_reg[pdev->id].name);
1356 wm8350_unmask_irq(wm8350, wm8350_reg[pdev->id].irq);
1361 static int wm8350_regulator_remove(struct platform_device *pdev)
1363 struct regulator_dev *rdev = platform_get_drvdata(pdev);
1364 struct wm8350 *wm8350 = rdev_get_drvdata(rdev);
1366 wm8350_mask_irq(wm8350, wm8350_reg[pdev->id].irq);
1367 wm8350_free_irq(wm8350, wm8350_reg[pdev->id].irq);
1369 regulator_unregister(rdev);
1374 int wm8350_register_regulator(struct wm8350 *wm8350, int reg,
1375 struct regulator_init_data *initdata)
1377 struct platform_device *pdev;
1380 if (wm8350->pmic.pdev[reg])
1383 pdev = platform_device_alloc("wm8350-regulator", reg);
1387 wm8350->pmic.pdev[reg] = pdev;
1389 initdata->driver_data = wm8350;
1391 pdev->dev.platform_data = initdata;
1392 pdev->dev.parent = wm8350->dev;
1393 platform_set_drvdata(pdev, wm8350);
1395 ret = platform_device_add(pdev);
1398 dev_err(wm8350->dev, "Failed to register regulator %d: %d\n",
1400 platform_device_del(pdev);
1401 wm8350->pmic.pdev[reg] = NULL;
1406 EXPORT_SYMBOL_GPL(wm8350_register_regulator);
1408 static struct platform_driver wm8350_regulator_driver = {
1409 .probe = wm8350_regulator_probe,
1410 .remove = wm8350_regulator_remove,
1412 .name = "wm8350-regulator",
1416 static int __init wm8350_regulator_init(void)
1418 return platform_driver_register(&wm8350_regulator_driver);
1420 subsys_initcall(wm8350_regulator_init);
1422 static void __exit wm8350_regulator_exit(void)
1424 platform_driver_unregister(&wm8350_regulator_driver);
1426 module_exit(wm8350_regulator_exit);
1428 /* Module information */
1429 MODULE_AUTHOR("Liam Girdwood");
1430 MODULE_DESCRIPTION("WM8350 voltage and current regulator driver");
1431 MODULE_LICENSE("GPL");