blob: af95449d3590c81cd2175be940af336f180a707e [file] [log] [blame]
Axel Linfe669cb2019-04-21 10:26:56 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (c) 2015 MediaTek Inc.
4// Author: Henry Chen <henryc.chen@mediatek.com>
Henry Chen87660182015-07-24 13:24:41 +08005
6#include <linux/err.h>
7#include <linux/gpio.h>
8#include <linux/i2c.h>
9#include <linux/init.h>
10#include <linux/interrupt.h>
11#include <linux/module.h>
12#include <linux/regmap.h>
13#include <linux/regulator/driver.h>
14#include <linux/regulator/machine.h>
15#include <linux/regulator/of_regulator.h>
16#include <linux/regulator/mt6311.h>
17#include <linux/slab.h>
18#include "mt6311-regulator.h"
19
20static const struct regmap_config mt6311_regmap_config = {
21 .reg_bits = 8,
22 .val_bits = 8,
23 .max_register = MT6311_FQMTR_CON4,
Daniel Kurtz47769cb2015-12-18 15:11:33 +080024 .cache_type = REGCACHE_RBTREE,
Henry Chen87660182015-07-24 13:24:41 +080025};
26
27/* Default limits measured in millivolts and milliamps */
28#define MT6311_MIN_UV 600000
Henry Chen53e38162015-08-03 22:15:51 +080029#define MT6311_MAX_UV 1393750
Henry Chen87660182015-07-24 13:24:41 +080030#define MT6311_STEP_UV 6250
31
Axel Lin08b472f2015-07-30 21:06:43 +080032static const struct regulator_ops mt6311_buck_ops = {
Axel Lina661b1d2019-02-22 00:28:31 +080033 .list_voltage = regulator_list_voltage_linear,
34 .map_voltage = regulator_map_voltage_linear,
Henry Chen87660182015-07-24 13:24:41 +080035 .set_voltage_sel = regulator_set_voltage_sel_regmap,
36 .get_voltage_sel = regulator_get_voltage_sel_regmap,
37 .set_voltage_time_sel = regulator_set_voltage_time_sel,
38 .enable = regulator_enable_regmap,
39 .disable = regulator_disable_regmap,
40 .is_enabled = regulator_is_enabled_regmap,
41};
42
Axel Lin08b472f2015-07-30 21:06:43 +080043static const struct regulator_ops mt6311_ldo_ops = {
Henry Chen87660182015-07-24 13:24:41 +080044 .enable = regulator_enable_regmap,
45 .disable = regulator_disable_regmap,
46 .is_enabled = regulator_is_enabled_regmap,
47};
48
49#define MT6311_BUCK(_id) \
50{\
51 .name = #_id,\
52 .ops = &mt6311_buck_ops,\
53 .of_match = of_match_ptr(#_id),\
54 .regulators_node = of_match_ptr("regulators"),\
55 .type = REGULATOR_VOLTAGE,\
56 .id = MT6311_ID_##_id,\
57 .n_voltages = (MT6311_MAX_UV - MT6311_MIN_UV) / MT6311_STEP_UV + 1,\
58 .min_uV = MT6311_MIN_UV,\
59 .uV_step = MT6311_STEP_UV,\
60 .owner = THIS_MODULE,\
Henry Chen87660182015-07-24 13:24:41 +080061 .enable_reg = MT6311_VDVFS11_CON9,\
62 .enable_mask = MT6311_PMIC_VDVFS11_EN_MASK,\
63 .vsel_reg = MT6311_VDVFS11_CON12,\
64 .vsel_mask = MT6311_PMIC_VDVFS11_VOSEL_MASK,\
65}
66
67#define MT6311_LDO(_id) \
68{\
69 .name = #_id,\
70 .ops = &mt6311_ldo_ops,\
71 .of_match = of_match_ptr(#_id),\
72 .regulators_node = of_match_ptr("regulators"),\
73 .type = REGULATOR_VOLTAGE,\
74 .id = MT6311_ID_##_id,\
75 .owner = THIS_MODULE,\
76 .enable_reg = MT6311_LDO_CON3,\
77 .enable_mask = MT6311_PMIC_RG_VBIASN_EN_MASK,\
78}
79
Axel Lin08b472f2015-07-30 21:06:43 +080080static const struct regulator_desc mt6311_regulators[] = {
Henry Chen87660182015-07-24 13:24:41 +080081 MT6311_BUCK(VDVFS),
82 MT6311_LDO(VBIASN),
83};
84
85/*
86 * I2C driver interface functions
87 */
88static int mt6311_i2c_probe(struct i2c_client *i2c,
89 const struct i2c_device_id *id)
90{
91 struct regulator_config config = { };
92 struct regulator_dev *rdev;
93 struct regmap *regmap;
Axel Lin08b472f2015-07-30 21:06:43 +080094 int i, ret;
Henry Chen87660182015-07-24 13:24:41 +080095 unsigned int data;
96
97 regmap = devm_regmap_init_i2c(i2c, &mt6311_regmap_config);
98 if (IS_ERR(regmap)) {
Axel Lin08b472f2015-07-30 21:06:43 +080099 ret = PTR_ERR(regmap);
Henry Chen87660182015-07-24 13:24:41 +0800100 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
Axel Lin08b472f2015-07-30 21:06:43 +0800101 ret);
102 return ret;
Henry Chen87660182015-07-24 13:24:41 +0800103 }
104
105 ret = regmap_read(regmap, MT6311_SWCID, &data);
106 if (ret < 0) {
107 dev_err(&i2c->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
108 return ret;
109 }
110
111 switch (data) {
112 case MT6311_E1_CID_CODE:
113 case MT6311_E2_CID_CODE:
114 case MT6311_E3_CID_CODE:
115 break;
116 default:
117 dev_err(&i2c->dev, "Unsupported device id = 0x%x.\n", data);
118 return -ENODEV;
119 }
120
121 for (i = 0; i < MT6311_MAX_REGULATORS; i++) {
122 config.dev = &i2c->dev;
123 config.regmap = regmap;
124
125 rdev = devm_regulator_register(&i2c->dev,
126 &mt6311_regulators[i], &config);
127 if (IS_ERR(rdev)) {
128 dev_err(&i2c->dev,
129 "Failed to register MT6311 regulator\n");
130 return PTR_ERR(rdev);
131 }
132 }
133
134 return 0;
135}
136
137static const struct i2c_device_id mt6311_i2c_id[] = {
138 {"mt6311", 0},
139 {},
140};
141MODULE_DEVICE_TABLE(i2c, mt6311_i2c_id);
142
143#ifdef CONFIG_OF
144static const struct of_device_id mt6311_dt_ids[] = {
145 { .compatible = "mediatek,mt6311-regulator",
146 .data = &mt6311_i2c_id[0] },
147 {},
148};
149MODULE_DEVICE_TABLE(of, mt6311_dt_ids);
150#endif
151
152static struct i2c_driver mt6311_regulator_driver = {
153 .driver = {
154 .name = "mt6311",
Henry Chen87660182015-07-24 13:24:41 +0800155 .of_match_table = of_match_ptr(mt6311_dt_ids),
156 },
157 .probe = mt6311_i2c_probe,
158 .id_table = mt6311_i2c_id,
159};
160
161module_i2c_driver(mt6311_regulator_driver);
162
163MODULE_AUTHOR("Henry Chen <henryc.chen@mediatek.com>");
164MODULE_DESCRIPTION("Regulator device driver for Mediatek MT6311");
165MODULE_LICENSE("GPL v2");