Linux Audio

Check our new training course

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