Linux Audio

Check our new training course

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