1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
208ff6f2aSHaojian Zhuang /*
308ff6f2aSHaojian Zhuang * Regulators driver for Maxim max8925
408ff6f2aSHaojian Zhuang *
508ff6f2aSHaojian Zhuang * Copyright (C) 2009 Marvell International Ltd.
608ff6f2aSHaojian Zhuang * Haojian Zhuang <haojian.zhuang@marvell.com>
708ff6f2aSHaojian Zhuang */
808ff6f2aSHaojian Zhuang #include <linux/kernel.h>
965602c32SPaul Gortmaker #include <linux/module.h>
1008ff6f2aSHaojian Zhuang #include <linux/init.h>
1108ff6f2aSHaojian Zhuang #include <linux/err.h>
1208ff6f2aSHaojian Zhuang #include <linux/i2c.h>
1308ff6f2aSHaojian Zhuang #include <linux/platform_device.h>
1408ff6f2aSHaojian Zhuang #include <linux/regulator/driver.h>
1508ff6f2aSHaojian Zhuang #include <linux/regulator/machine.h>
1608ff6f2aSHaojian Zhuang #include <linux/mfd/max8925.h>
1745b6f8e8SQing Xu #include <linux/of.h>
1845b6f8e8SQing Xu #include <linux/regulator/of_regulator.h>
1908ff6f2aSHaojian Zhuang
2008ff6f2aSHaojian Zhuang #define SD1_DVM_VMIN 850000
2108ff6f2aSHaojian Zhuang #define SD1_DVM_VMAX 1000000
2208ff6f2aSHaojian Zhuang #define SD1_DVM_STEP 50000
2308ff6f2aSHaojian Zhuang #define SD1_DVM_SHIFT 5 /* SDCTL1 bit5 */
2408ff6f2aSHaojian Zhuang #define SD1_DVM_EN 6 /* SDV1 bit 6 */
2508ff6f2aSHaojian Zhuang
2665364e9aSKevin Liu /* bit definitions in LDO control registers */
2765364e9aSKevin Liu #define LDO_SEQ_I2C 0x7 /* Power U/D by i2c */
2865364e9aSKevin Liu #define LDO_SEQ_MASK 0x7 /* Power U/D sequence mask */
2965364e9aSKevin Liu #define LDO_SEQ_SHIFT 2 /* Power U/D sequence offset */
3065364e9aSKevin Liu #define LDO_I2C_EN 0x1 /* Enable by i2c */
3165364e9aSKevin Liu #define LDO_I2C_EN_MASK 0x1 /* Enable mask by i2c */
3265364e9aSKevin Liu #define LDO_I2C_EN_SHIFT 0 /* Enable offset by i2c */
33db13c040SHaojian Zhuang
3408ff6f2aSHaojian Zhuang struct max8925_regulator_info {
3508ff6f2aSHaojian Zhuang struct regulator_desc desc;
3608ff6f2aSHaojian Zhuang struct i2c_client *i2c;
3708ff6f2aSHaojian Zhuang
3808ff6f2aSHaojian Zhuang int vol_reg;
3908ff6f2aSHaojian Zhuang int enable_reg;
4008ff6f2aSHaojian Zhuang };
4108ff6f2aSHaojian Zhuang
max8925_set_voltage_sel(struct regulator_dev * rdev,unsigned int selector)42cd8318cbSAxel Lin static int max8925_set_voltage_sel(struct regulator_dev *rdev,
43cd8318cbSAxel Lin unsigned int selector)
4408ff6f2aSHaojian Zhuang {
4508ff6f2aSHaojian Zhuang struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
46cd8318cbSAxel Lin unsigned char mask = rdev->desc->n_voltages - 1;
4708ff6f2aSHaojian Zhuang
48cd8318cbSAxel Lin return max8925_set_bits(info->i2c, info->vol_reg, mask, selector);
4908ff6f2aSHaojian Zhuang }
5008ff6f2aSHaojian Zhuang
max8925_get_voltage_sel(struct regulator_dev * rdev)513c8983b9SAxel Lin static int max8925_get_voltage_sel(struct regulator_dev *rdev)
5208ff6f2aSHaojian Zhuang {
5308ff6f2aSHaojian Zhuang struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
5408ff6f2aSHaojian Zhuang unsigned char data, mask;
5508ff6f2aSHaojian Zhuang int ret;
5608ff6f2aSHaojian Zhuang
5708ff6f2aSHaojian Zhuang ret = max8925_reg_read(info->i2c, info->vol_reg);
5808ff6f2aSHaojian Zhuang if (ret < 0)
5908ff6f2aSHaojian Zhuang return ret;
607f52ba77SAxel Lin mask = rdev->desc->n_voltages - 1;
617f52ba77SAxel Lin data = ret & mask;
6208ff6f2aSHaojian Zhuang
633c8983b9SAxel Lin return data;
6408ff6f2aSHaojian Zhuang }
6508ff6f2aSHaojian Zhuang
max8925_enable(struct regulator_dev * rdev)6608ff6f2aSHaojian Zhuang static int max8925_enable(struct regulator_dev *rdev)
6708ff6f2aSHaojian Zhuang {
6808ff6f2aSHaojian Zhuang struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
6908ff6f2aSHaojian Zhuang
7008ff6f2aSHaojian Zhuang return max8925_set_bits(info->i2c, info->enable_reg,
7165364e9aSKevin Liu LDO_SEQ_MASK << LDO_SEQ_SHIFT |
7265364e9aSKevin Liu LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
7365364e9aSKevin Liu LDO_SEQ_I2C << LDO_SEQ_SHIFT |
7465364e9aSKevin Liu LDO_I2C_EN << LDO_I2C_EN_SHIFT);
7508ff6f2aSHaojian Zhuang }
7608ff6f2aSHaojian Zhuang
max8925_disable(struct regulator_dev * rdev)7708ff6f2aSHaojian Zhuang static int max8925_disable(struct regulator_dev *rdev)
7808ff6f2aSHaojian Zhuang {
7908ff6f2aSHaojian Zhuang struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
8008ff6f2aSHaojian Zhuang
8108ff6f2aSHaojian Zhuang return max8925_set_bits(info->i2c, info->enable_reg,
8265364e9aSKevin Liu LDO_SEQ_MASK << LDO_SEQ_SHIFT |
8365364e9aSKevin Liu LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
8465364e9aSKevin Liu LDO_SEQ_I2C << LDO_SEQ_SHIFT);
8508ff6f2aSHaojian Zhuang }
8608ff6f2aSHaojian Zhuang
max8925_is_enabled(struct regulator_dev * rdev)8708ff6f2aSHaojian Zhuang static int max8925_is_enabled(struct regulator_dev *rdev)
8808ff6f2aSHaojian Zhuang {
8908ff6f2aSHaojian Zhuang struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
9065364e9aSKevin Liu int ldo_seq, ret;
9108ff6f2aSHaojian Zhuang
924b99fef0SHaojian Zhuang ret = max8925_reg_read(info->i2c, info->enable_reg);
9308ff6f2aSHaojian Zhuang if (ret < 0)
9408ff6f2aSHaojian Zhuang return ret;
9565364e9aSKevin Liu ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
9665364e9aSKevin Liu if (ldo_seq != LDO_SEQ_I2C)
9765364e9aSKevin Liu return 1;
9865364e9aSKevin Liu else
9965364e9aSKevin Liu return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
10008ff6f2aSHaojian Zhuang }
10108ff6f2aSHaojian Zhuang
max8925_set_dvm_voltage(struct regulator_dev * rdev,int uV)10208ff6f2aSHaojian Zhuang static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
10308ff6f2aSHaojian Zhuang {
10408ff6f2aSHaojian Zhuang struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
10508ff6f2aSHaojian Zhuang unsigned char data, mask;
10608ff6f2aSHaojian Zhuang
10708ff6f2aSHaojian Zhuang if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
10808ff6f2aSHaojian Zhuang return -EINVAL;
10908ff6f2aSHaojian Zhuang
1105db6d09aSAxel Lin data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP);
11108ff6f2aSHaojian Zhuang data <<= SD1_DVM_SHIFT;
11208ff6f2aSHaojian Zhuang mask = 3 << SD1_DVM_SHIFT;
11308ff6f2aSHaojian Zhuang
11408ff6f2aSHaojian Zhuang return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
11508ff6f2aSHaojian Zhuang }
11608ff6f2aSHaojian Zhuang
max8925_set_dvm_enable(struct regulator_dev * rdev)11708ff6f2aSHaojian Zhuang static int max8925_set_dvm_enable(struct regulator_dev *rdev)
11808ff6f2aSHaojian Zhuang {
11908ff6f2aSHaojian Zhuang struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
12008ff6f2aSHaojian Zhuang
12108ff6f2aSHaojian Zhuang return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
12208ff6f2aSHaojian Zhuang 1 << SD1_DVM_EN);
12308ff6f2aSHaojian Zhuang }
12408ff6f2aSHaojian Zhuang
max8925_set_dvm_disable(struct regulator_dev * rdev)12508ff6f2aSHaojian Zhuang static int max8925_set_dvm_disable(struct regulator_dev *rdev)
12608ff6f2aSHaojian Zhuang {
12708ff6f2aSHaojian Zhuang struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
12808ff6f2aSHaojian Zhuang
12908ff6f2aSHaojian Zhuang return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
13008ff6f2aSHaojian Zhuang }
13108ff6f2aSHaojian Zhuang
13285214c9aSBhumika Goyal static const struct regulator_ops max8925_regulator_sdv_ops = {
133cd8318cbSAxel Lin .map_voltage = regulator_map_voltage_linear,
1344093e739SAxel Lin .list_voltage = regulator_list_voltage_linear,
135cd8318cbSAxel Lin .set_voltage_sel = max8925_set_voltage_sel,
1363c8983b9SAxel Lin .get_voltage_sel = max8925_get_voltage_sel,
13708ff6f2aSHaojian Zhuang .enable = max8925_enable,
13808ff6f2aSHaojian Zhuang .disable = max8925_disable,
13908ff6f2aSHaojian Zhuang .is_enabled = max8925_is_enabled,
14008ff6f2aSHaojian Zhuang .set_suspend_voltage = max8925_set_dvm_voltage,
14108ff6f2aSHaojian Zhuang .set_suspend_enable = max8925_set_dvm_enable,
14208ff6f2aSHaojian Zhuang .set_suspend_disable = max8925_set_dvm_disable,
14308ff6f2aSHaojian Zhuang };
14408ff6f2aSHaojian Zhuang
14585214c9aSBhumika Goyal static const struct regulator_ops max8925_regulator_ldo_ops = {
146cd8318cbSAxel Lin .map_voltage = regulator_map_voltage_linear,
1474093e739SAxel Lin .list_voltage = regulator_list_voltage_linear,
148cd8318cbSAxel Lin .set_voltage_sel = max8925_set_voltage_sel,
1493c8983b9SAxel Lin .get_voltage_sel = max8925_get_voltage_sel,
15008ff6f2aSHaojian Zhuang .enable = max8925_enable,
15108ff6f2aSHaojian Zhuang .disable = max8925_disable,
15208ff6f2aSHaojian Zhuang .is_enabled = max8925_is_enabled,
15308ff6f2aSHaojian Zhuang };
15408ff6f2aSHaojian Zhuang
15508ff6f2aSHaojian Zhuang #define MAX8925_SDV(_id, min, max, step) \
15608ff6f2aSHaojian Zhuang { \
15708ff6f2aSHaojian Zhuang .desc = { \
15808ff6f2aSHaojian Zhuang .name = "SDV" #_id, \
1596185de50SAxel Lin .of_match = of_match_ptr("SDV" #_id), \
1606185de50SAxel Lin .regulators_node = of_match_ptr("regulators"), \
16108ff6f2aSHaojian Zhuang .ops = &max8925_regulator_sdv_ops, \
16208ff6f2aSHaojian Zhuang .type = REGULATOR_VOLTAGE, \
16308ff6f2aSHaojian Zhuang .id = MAX8925_ID_SD##_id, \
16408ff6f2aSHaojian Zhuang .owner = THIS_MODULE, \
1657f52ba77SAxel Lin .n_voltages = 64, \
16608ff6f2aSHaojian Zhuang .min_uV = min * 1000, \
1674093e739SAxel Lin .uV_step = step * 1000, \
1684093e739SAxel Lin }, \
16908ff6f2aSHaojian Zhuang .vol_reg = MAX8925_SDV##_id, \
17008ff6f2aSHaojian Zhuang .enable_reg = MAX8925_SDCTL##_id, \
17108ff6f2aSHaojian Zhuang }
17208ff6f2aSHaojian Zhuang
17308ff6f2aSHaojian Zhuang #define MAX8925_LDO(_id, min, max, step) \
17408ff6f2aSHaojian Zhuang { \
17508ff6f2aSHaojian Zhuang .desc = { \
17608ff6f2aSHaojian Zhuang .name = "LDO" #_id, \
1776185de50SAxel Lin .of_match = of_match_ptr("LDO" #_id), \
1786185de50SAxel Lin .regulators_node = of_match_ptr("regulators"), \
17908ff6f2aSHaojian Zhuang .ops = &max8925_regulator_ldo_ops, \
18008ff6f2aSHaojian Zhuang .type = REGULATOR_VOLTAGE, \
18108ff6f2aSHaojian Zhuang .id = MAX8925_ID_LDO##_id, \
18208ff6f2aSHaojian Zhuang .owner = THIS_MODULE, \
1837f52ba77SAxel Lin .n_voltages = 64, \
18408ff6f2aSHaojian Zhuang .min_uV = min * 1000, \
1854093e739SAxel Lin .uV_step = step * 1000, \
1864093e739SAxel Lin }, \
18708ff6f2aSHaojian Zhuang .vol_reg = MAX8925_LDOVOUT##_id, \
18808ff6f2aSHaojian Zhuang .enable_reg = MAX8925_LDOCTL##_id, \
18908ff6f2aSHaojian Zhuang }
19008ff6f2aSHaojian Zhuang
19108ff6f2aSHaojian Zhuang static struct max8925_regulator_info max8925_regulator_info[] = {
19208ff6f2aSHaojian Zhuang MAX8925_SDV(1, 637.5, 1425, 12.5),
19308ff6f2aSHaojian Zhuang MAX8925_SDV(2, 650, 2225, 25),
19408ff6f2aSHaojian Zhuang MAX8925_SDV(3, 750, 3900, 50),
19508ff6f2aSHaojian Zhuang
19608ff6f2aSHaojian Zhuang MAX8925_LDO(1, 750, 3900, 50),
19708ff6f2aSHaojian Zhuang MAX8925_LDO(2, 650, 2250, 25),
19808ff6f2aSHaojian Zhuang MAX8925_LDO(3, 650, 2250, 25),
19908ff6f2aSHaojian Zhuang MAX8925_LDO(4, 750, 3900, 50),
20008ff6f2aSHaojian Zhuang MAX8925_LDO(5, 750, 3900, 50),
20108ff6f2aSHaojian Zhuang MAX8925_LDO(6, 750, 3900, 50),
20208ff6f2aSHaojian Zhuang MAX8925_LDO(7, 750, 3900, 50),
20308ff6f2aSHaojian Zhuang MAX8925_LDO(8, 750, 3900, 50),
20408ff6f2aSHaojian Zhuang MAX8925_LDO(9, 750, 3900, 50),
20508ff6f2aSHaojian Zhuang MAX8925_LDO(10, 750, 3900, 50),
20608ff6f2aSHaojian Zhuang MAX8925_LDO(11, 750, 3900, 50),
20708ff6f2aSHaojian Zhuang MAX8925_LDO(12, 750, 3900, 50),
20808ff6f2aSHaojian Zhuang MAX8925_LDO(13, 750, 3900, 50),
20908ff6f2aSHaojian Zhuang MAX8925_LDO(14, 750, 3900, 50),
21008ff6f2aSHaojian Zhuang MAX8925_LDO(15, 750, 3900, 50),
21108ff6f2aSHaojian Zhuang MAX8925_LDO(16, 750, 3900, 50),
21208ff6f2aSHaojian Zhuang MAX8925_LDO(17, 650, 2250, 25),
21308ff6f2aSHaojian Zhuang MAX8925_LDO(18, 650, 2250, 25),
21408ff6f2aSHaojian Zhuang MAX8925_LDO(19, 750, 3900, 50),
21508ff6f2aSHaojian Zhuang MAX8925_LDO(20, 750, 3900, 50),
21608ff6f2aSHaojian Zhuang };
21708ff6f2aSHaojian Zhuang
max8925_regulator_probe(struct platform_device * pdev)218a5023574SBill Pemberton static int max8925_regulator_probe(struct platform_device *pdev)
21908ff6f2aSHaojian Zhuang {
22008ff6f2aSHaojian Zhuang struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
221dff91d0bSJingoo Han struct regulator_init_data *pdata = dev_get_platdata(&pdev->dev);
222c172708dSMark Brown struct regulator_config config = { };
2233fff6c04SDmitry Torokhov struct max8925_regulator_info *ri;
22451acdb61SHaojian Zhuang struct resource *res;
22508ff6f2aSHaojian Zhuang struct regulator_dev *rdev;
2266185de50SAxel Lin int i;
22708ff6f2aSHaojian Zhuang
22851acdb61SHaojian Zhuang res = platform_get_resource(pdev, IORESOURCE_REG, 0);
22951acdb61SHaojian Zhuang if (!res) {
23051acdb61SHaojian Zhuang dev_err(&pdev->dev, "No REG resource!\n");
23151acdb61SHaojian Zhuang return -EINVAL;
23251acdb61SHaojian Zhuang }
23351acdb61SHaojian Zhuang for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
23451acdb61SHaojian Zhuang ri = &max8925_regulator_info[i];
2356185de50SAxel Lin if (ri->vol_reg == res->start)
23651acdb61SHaojian Zhuang break;
23751acdb61SHaojian Zhuang }
23845b6f8e8SQing Xu
23951acdb61SHaojian Zhuang if (i == ARRAY_SIZE(max8925_regulator_info)) {
24051acdb61SHaojian Zhuang dev_err(&pdev->dev, "Failed to find regulator %llu\n",
24151acdb61SHaojian Zhuang (unsigned long long)res->start);
24208ff6f2aSHaojian Zhuang return -EINVAL;
24308ff6f2aSHaojian Zhuang }
24408ff6f2aSHaojian Zhuang ri->i2c = chip->i2c;
24508ff6f2aSHaojian Zhuang
2466185de50SAxel Lin config.dev = chip->dev;
247c172708dSMark Brown config.driver_data = ri;
248c172708dSMark Brown
24945b6f8e8SQing Xu if (pdata)
25045b6f8e8SQing Xu config.init_data = pdata;
25145b6f8e8SQing Xu
25258765e24SJingoo Han rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
25308ff6f2aSHaojian Zhuang if (IS_ERR(rdev)) {
25408ff6f2aSHaojian Zhuang dev_err(&pdev->dev, "failed to register regulator %s\n",
25508ff6f2aSHaojian Zhuang ri->desc.name);
25608ff6f2aSHaojian Zhuang return PTR_ERR(rdev);
25708ff6f2aSHaojian Zhuang }
25808ff6f2aSHaojian Zhuang
25908ff6f2aSHaojian Zhuang platform_set_drvdata(pdev, rdev);
26008ff6f2aSHaojian Zhuang return 0;
26108ff6f2aSHaojian Zhuang }
26208ff6f2aSHaojian Zhuang
26308ff6f2aSHaojian Zhuang static struct platform_driver max8925_regulator_driver = {
26408ff6f2aSHaojian Zhuang .driver = {
26508ff6f2aSHaojian Zhuang .name = "max8925-regulator",
266*259b93b2SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
26708ff6f2aSHaojian Zhuang },
26808ff6f2aSHaojian Zhuang .probe = max8925_regulator_probe,
26908ff6f2aSHaojian Zhuang };
27008ff6f2aSHaojian Zhuang
max8925_regulator_init(void)27108ff6f2aSHaojian Zhuang static int __init max8925_regulator_init(void)
27208ff6f2aSHaojian Zhuang {
27308ff6f2aSHaojian Zhuang return platform_driver_register(&max8925_regulator_driver);
27408ff6f2aSHaojian Zhuang }
27508ff6f2aSHaojian Zhuang subsys_initcall(max8925_regulator_init);
27608ff6f2aSHaojian Zhuang
max8925_regulator_exit(void)27708ff6f2aSHaojian Zhuang static void __exit max8925_regulator_exit(void)
27808ff6f2aSHaojian Zhuang {
27908ff6f2aSHaojian Zhuang platform_driver_unregister(&max8925_regulator_driver);
28008ff6f2aSHaojian Zhuang }
28108ff6f2aSHaojian Zhuang module_exit(max8925_regulator_exit);
28208ff6f2aSHaojian Zhuang
28308ff6f2aSHaojian Zhuang MODULE_LICENSE("GPL");
28408ff6f2aSHaojian Zhuang MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
28508ff6f2aSHaojian Zhuang MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
28608ff6f2aSHaojian Zhuang MODULE_ALIAS("platform:max8925-regulator");
287