Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
  3 * and Digital Power Monitor
  4 *
  5 * Copyright (c) 2011 Ericsson AB.
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License as published by
  9 * the Free Software Foundation; either version 2 of the License, or
 10 * (at your option) any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15 * GNU General Public License for more details.
 16 */
 17
 18#include <linux/kernel.h>
 19#include <linux/module.h>
 20#include <linux/init.h>
 21#include <linux/err.h>
 22#include <linux/slab.h>
 23#include <linux/i2c.h>
 
 
 
 24#include "pmbus.h"
 25
 
 
 
 
 
 
 26#define ADM1275_PEAK_IOUT		0xd0
 27#define ADM1275_PEAK_VIN		0xd1
 28#define ADM1275_PEAK_VOUT		0xd2
 29#define ADM1275_PMON_CONFIG		0xd4
 30
 31#define ADM1275_VIN_VOUT_SELECT		(1 << 6)
 32#define ADM1275_VRANGE			(1 << 5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 33
 34static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 35{
 36	int ret;
 
 37
 38	if (page)
 39		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 40
 41	switch (reg) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 42	case PMBUS_VIRT_READ_IOUT_MAX:
 43		ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
 
 44		break;
 45	case PMBUS_VIRT_READ_VOUT_MAX:
 46		ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
 
 47		break;
 48	case PMBUS_VIRT_READ_VIN_MAX:
 49		ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 50		break;
 51	case PMBUS_VIRT_RESET_IOUT_HISTORY:
 52	case PMBUS_VIRT_RESET_VOUT_HISTORY:
 53	case PMBUS_VIRT_RESET_VIN_HISTORY:
 54		ret = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 55		break;
 56	default:
 57		ret = -ENODATA;
 58		break;
 59	}
 60	return ret;
 61}
 62
 63static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
 64				   u16 word)
 65{
 
 
 66	int ret;
 67
 68	if (page)
 69		return -EINVAL;
 70
 71	switch (reg) {
 
 
 
 
 
 72	case PMBUS_VIRT_RESET_IOUT_HISTORY:
 73		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
 
 
 
 74		break;
 75	case PMBUS_VIRT_RESET_VOUT_HISTORY:
 76		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
 77		break;
 78	case PMBUS_VIRT_RESET_VIN_HISTORY:
 79		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
 80		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 81	default:
 82		ret = -ENODATA;
 83		break;
 84	}
 85	return ret;
 86}
 87
 88static int adm1275_probe(struct i2c_client *client,
 89			 const struct i2c_device_id *id)
 
 
 
 
 
 
 
 
 
 
 
 90{
 91	int config;
 
 
 92	int ret;
 93	struct pmbus_driver_info *info;
 
 
 
 
 
 
 94
 95	if (!i2c_check_functionality(client->adapter,
 96				     I2C_FUNC_SMBUS_READ_BYTE_DATA))
 
 97		return -ENODEV;
 98
 99	info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
100	if (!info)
101		return -ENOMEM;
 
 
 
 
 
 
102
103	config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
104	if (config < 0) {
105		ret = config;
106		goto err_mem;
107	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
109	info->pages = 1;
110	info->format[PSC_VOLTAGE_IN] = direct;
111	info->format[PSC_VOLTAGE_OUT] = direct;
112	info->format[PSC_CURRENT_OUT] = direct;
113	info->m[PSC_CURRENT_OUT] = 807;
114	info->b[PSC_CURRENT_OUT] = 20475;
115	info->R[PSC_CURRENT_OUT] = -1;
116	info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
117
118	info->read_word_data = adm1275_read_word_data;
 
119	info->write_word_data = adm1275_write_word_data;
120
121	if (config & ADM1275_VRANGE) {
122		info->m[PSC_VOLTAGE_IN] = 19199;
123		info->b[PSC_VOLTAGE_IN] = 0;
124		info->R[PSC_VOLTAGE_IN] = -2;
125		info->m[PSC_VOLTAGE_OUT] = 19199;
126		info->b[PSC_VOLTAGE_OUT] = 0;
127		info->R[PSC_VOLTAGE_OUT] = -2;
128	} else {
129		info->m[PSC_VOLTAGE_IN] = 6720;
130		info->b[PSC_VOLTAGE_IN] = 0;
131		info->R[PSC_VOLTAGE_IN] = -1;
132		info->m[PSC_VOLTAGE_OUT] = 6720;
133		info->b[PSC_VOLTAGE_OUT] = 0;
134		info->R[PSC_VOLTAGE_OUT] = -1;
135	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
137	if (config & ADM1275_VIN_VOUT_SELECT)
138		info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
139	else
140		info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
142	ret = pmbus_do_probe(client, id, info);
143	if (ret)
144		goto err_mem;
145	return 0;
146
147err_mem:
148	kfree(info);
149	return ret;
150}
 
151
152static int adm1275_remove(struct i2c_client *client)
153{
154	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
155	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
157	ret = pmbus_do_remove(client);
158	kfree(info);
159	return ret;
160}
161
162static const struct i2c_device_id adm1275_id[] = {
163	{"adm1275", 0},
164	{ }
165};
166MODULE_DEVICE_TABLE(i2c, adm1275_id);
167
168static struct i2c_driver adm1275_driver = {
169	.driver = {
170		   .name = "adm1275",
171		   },
172	.probe = adm1275_probe,
173	.remove = adm1275_remove,
174	.id_table = adm1275_id,
175};
176
177static int __init adm1275_init(void)
178{
179	return i2c_add_driver(&adm1275_driver);
180}
181
182static void __exit adm1275_exit(void)
183{
184	i2c_del_driver(&adm1275_driver);
185}
186
187MODULE_AUTHOR("Guenter Roeck");
188MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275");
189MODULE_LICENSE("GPL");
190module_init(adm1275_init);
191module_exit(adm1275_exit);
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
  4 * and Digital Power Monitor
  5 *
  6 * Copyright (c) 2011 Ericsson AB.
  7 * Copyright (c) 2018 Guenter Roeck
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 12#include <linux/init.h>
 13#include <linux/err.h>
 14#include <linux/slab.h>
 15#include <linux/i2c.h>
 16#include <linux/bitops.h>
 17#include <linux/bitfield.h>
 18#include <linux/log2.h>
 19#include "pmbus.h"
 20
 21enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
 22
 23#define ADM1275_MFR_STATUS_IOUT_WARN2	BIT(0)
 24#define ADM1293_MFR_STATUS_VAUX_UV_WARN	BIT(5)
 25#define ADM1293_MFR_STATUS_VAUX_OV_WARN	BIT(6)
 26
 27#define ADM1275_PEAK_IOUT		0xd0
 28#define ADM1275_PEAK_VIN		0xd1
 29#define ADM1275_PEAK_VOUT		0xd2
 30#define ADM1275_PMON_CONFIG		0xd4
 31
 32#define ADM1275_VIN_VOUT_SELECT		BIT(6)
 33#define ADM1275_VRANGE			BIT(5)
 34#define ADM1075_IRANGE_50		BIT(4)
 35#define ADM1075_IRANGE_25		BIT(3)
 36#define ADM1075_IRANGE_MASK		(BIT(3) | BIT(4))
 37
 38#define ADM1272_IRANGE			BIT(0)
 39
 40#define ADM1278_TEMP1_EN		BIT(3)
 41#define ADM1278_VIN_EN			BIT(2)
 42#define ADM1278_VOUT_EN			BIT(1)
 43
 44#define ADM1293_IRANGE_25		0
 45#define ADM1293_IRANGE_50		BIT(6)
 46#define ADM1293_IRANGE_100		BIT(7)
 47#define ADM1293_IRANGE_200		(BIT(6) | BIT(7))
 48#define ADM1293_IRANGE_MASK		(BIT(6) | BIT(7))
 49
 50#define ADM1293_VIN_SEL_012		BIT(2)
 51#define ADM1293_VIN_SEL_074		BIT(3)
 52#define ADM1293_VIN_SEL_210		(BIT(2) | BIT(3))
 53#define ADM1293_VIN_SEL_MASK		(BIT(2) | BIT(3))
 54
 55#define ADM1293_VAUX_EN			BIT(1)
 56
 57#define ADM1278_PEAK_TEMP		0xd7
 58#define ADM1275_IOUT_WARN2_LIMIT	0xd7
 59#define ADM1275_DEVICE_CONFIG		0xd8
 60
 61#define ADM1275_IOUT_WARN2_SELECT	BIT(4)
 62
 63#define ADM1276_PEAK_PIN		0xda
 64#define ADM1075_READ_VAUX		0xdd
 65#define ADM1075_VAUX_OV_WARN_LIMIT	0xde
 66#define ADM1075_VAUX_UV_WARN_LIMIT	0xdf
 67#define ADM1293_IOUT_MIN		0xe3
 68#define ADM1293_PIN_MIN			0xe4
 69#define ADM1075_VAUX_STATUS		0xf6
 70
 71#define ADM1075_VAUX_OV_WARN		BIT(7)
 72#define ADM1075_VAUX_UV_WARN		BIT(6)
 73
 74#define ADM1275_VI_AVG_SHIFT		0
 75#define ADM1275_VI_AVG_MASK		GENMASK(ADM1275_VI_AVG_SHIFT + 2, \
 76						ADM1275_VI_AVG_SHIFT)
 77#define ADM1275_SAMPLES_AVG_MAX		128
 78
 79#define ADM1278_PWR_AVG_SHIFT		11
 80#define ADM1278_PWR_AVG_MASK		GENMASK(ADM1278_PWR_AVG_SHIFT + 2, \
 81						ADM1278_PWR_AVG_SHIFT)
 82#define ADM1278_VI_AVG_SHIFT		8
 83#define ADM1278_VI_AVG_MASK		GENMASK(ADM1278_VI_AVG_SHIFT + 2, \
 84						ADM1278_VI_AVG_SHIFT)
 85
 86struct adm1275_data {
 87	int id;
 88	bool have_oc_fault;
 89	bool have_uc_fault;
 90	bool have_vout;
 91	bool have_vaux_status;
 92	bool have_mfr_vaux_status;
 93	bool have_iout_min;
 94	bool have_pin_min;
 95	bool have_pin_max;
 96	bool have_temp_max;
 97	bool have_power_sampling;
 98	struct pmbus_driver_info info;
 99};
100
101#define to_adm1275_data(x)  container_of(x, struct adm1275_data, info)
102
103struct coefficients {
104	s16 m;
105	s16 b;
106	s16 R;
107};
108
109static const struct coefficients adm1075_coefficients[] = {
110	[0] = { 27169, 0, -1 },		/* voltage */
111	[1] = { 806, 20475, -1 },	/* current, irange25 */
112	[2] = { 404, 20475, -1 },	/* current, irange50 */
113	[3] = { 8549, 0, -1 },		/* power, irange25 */
114	[4] = { 4279, 0, -1 },		/* power, irange50 */
115};
116
117static const struct coefficients adm1272_coefficients[] = {
118	[0] = { 6770, 0, -2 },		/* voltage, vrange 60V */
119	[1] = { 4062, 0, -2 },		/* voltage, vrange 100V */
120	[2] = { 1326, 20480, -1 },	/* current, vsense range 15mV */
121	[3] = { 663, 20480, -1 },	/* current, vsense range 30mV */
122	[4] = { 3512, 0, -2 },		/* power, vrange 60V, irange 15mV */
123	[5] = { 21071, 0, -3 },		/* power, vrange 100V, irange 15mV */
124	[6] = { 17561, 0, -3 },		/* power, vrange 60V, irange 30mV */
125	[7] = { 10535, 0, -3 },		/* power, vrange 100V, irange 30mV */
126	[8] = { 42, 31871, -1 },	/* temperature */
127
128};
129
130static const struct coefficients adm1275_coefficients[] = {
131	[0] = { 19199, 0, -2 },		/* voltage, vrange set */
132	[1] = { 6720, 0, -1 },		/* voltage, vrange not set */
133	[2] = { 807, 20475, -1 },	/* current */
134};
135
136static const struct coefficients adm1276_coefficients[] = {
137	[0] = { 19199, 0, -2 },		/* voltage, vrange set */
138	[1] = { 6720, 0, -1 },		/* voltage, vrange not set */
139	[2] = { 807, 20475, -1 },	/* current */
140	[3] = { 6043, 0, -2 },		/* power, vrange set */
141	[4] = { 2115, 0, -1 },		/* power, vrange not set */
142};
143
144static const struct coefficients adm1278_coefficients[] = {
145	[0] = { 19599, 0, -2 },		/* voltage */
146	[1] = { 800, 20475, -1 },	/* current */
147	[2] = { 6123, 0, -2 },		/* power */
148	[3] = { 42, 31880, -1 },	/* temperature */
149};
150
151static const struct coefficients adm1293_coefficients[] = {
152	[0] = { 3333, -1, 0 },		/* voltage, vrange 1.2V */
153	[1] = { 5552, -5, -1 },		/* voltage, vrange 7.4V */
154	[2] = { 19604, -50, -2 },	/* voltage, vrange 21V */
155	[3] = { 8000, -100, -2 },	/* current, irange25 */
156	[4] = { 4000, -100, -2 },	/* current, irange50 */
157	[5] = { 20000, -1000, -3 },	/* current, irange100 */
158	[6] = { 10000, -1000, -3 },	/* current, irange200 */
159	[7] = { 10417, 0, -1 },		/* power, 1.2V, irange25 */
160	[8] = { 5208, 0, -1 },		/* power, 1.2V, irange50 */
161	[9] = { 26042, 0, -2 },		/* power, 1.2V, irange100 */
162	[10] = { 13021, 0, -2 },	/* power, 1.2V, irange200 */
163	[11] = { 17351, 0, -2 },	/* power, 7.4V, irange25 */
164	[12] = { 8676, 0, -2 },		/* power, 7.4V, irange50 */
165	[13] = { 4338, 0, -2 },		/* power, 7.4V, irange100 */
166	[14] = { 21689, 0, -3 },	/* power, 7.4V, irange200 */
167	[15] = { 6126, 0, -2 },		/* power, 21V, irange25 */
168	[16] = { 30631, 0, -3 },	/* power, 21V, irange50 */
169	[17] = { 15316, 0, -3 },	/* power, 21V, irange100 */
170	[18] = { 7658, 0, -3 },		/* power, 21V, irange200 */
171};
172
173static int adm1275_read_pmon_config(const struct adm1275_data *data,
174				    struct i2c_client *client, bool is_power)
175{
176	int shift, ret;
177	u16 mask;
178
179	/*
180	 * The PMON configuration register is a 16-bit register only on chips
181	 * supporting power average sampling. On other chips it is an 8-bit
182	 * register.
183	 */
184	if (data->have_power_sampling) {
185		ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
186		mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
187		shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
188	} else {
189		ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
190		mask = ADM1275_VI_AVG_MASK;
191		shift = ADM1275_VI_AVG_SHIFT;
192	}
193	if (ret < 0)
194		return ret;
195
196	return (ret & mask) >> shift;
197}
198
199static int adm1275_write_pmon_config(const struct adm1275_data *data,
200				     struct i2c_client *client,
201				     bool is_power, u16 word)
202{
203	int shift, ret;
204	u16 mask;
205
206	if (data->have_power_sampling) {
207		ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
208		mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
209		shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
210	} else {
211		ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
212		mask = ADM1275_VI_AVG_MASK;
213		shift = ADM1275_VI_AVG_SHIFT;
214	}
215	if (ret < 0)
216		return ret;
217
218	word = (ret & ~mask) | ((word << shift) & mask);
219	if (data->have_power_sampling)
220		ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG,
221						word);
222	else
223		ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG,
224						word);
225
226	return ret;
227}
228
229static int adm1275_read_word_data(struct i2c_client *client, int page,
230				  int phase, int reg)
231{
232	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
233	const struct adm1275_data *data = to_adm1275_data(info);
234	int ret = 0;
235
236	if (page > 0)
237		return -ENXIO;
238
239	switch (reg) {
240	case PMBUS_IOUT_UC_FAULT_LIMIT:
241		if (!data->have_uc_fault)
242			return -ENXIO;
243		ret = pmbus_read_word_data(client, 0, 0xff,
244					   ADM1275_IOUT_WARN2_LIMIT);
245		break;
246	case PMBUS_IOUT_OC_FAULT_LIMIT:
247		if (!data->have_oc_fault)
248			return -ENXIO;
249		ret = pmbus_read_word_data(client, 0, 0xff,
250					   ADM1275_IOUT_WARN2_LIMIT);
251		break;
252	case PMBUS_VOUT_OV_WARN_LIMIT:
253		if (data->have_vout)
254			return -ENODATA;
255		ret = pmbus_read_word_data(client, 0, 0xff,
256					   ADM1075_VAUX_OV_WARN_LIMIT);
257		break;
258	case PMBUS_VOUT_UV_WARN_LIMIT:
259		if (data->have_vout)
260			return -ENODATA;
261		ret = pmbus_read_word_data(client, 0, 0xff,
262					   ADM1075_VAUX_UV_WARN_LIMIT);
263		break;
264	case PMBUS_READ_VOUT:
265		if (data->have_vout)
266			return -ENODATA;
267		ret = pmbus_read_word_data(client, 0, 0xff,
268					   ADM1075_READ_VAUX);
269		break;
270	case PMBUS_VIRT_READ_IOUT_MIN:
271		if (!data->have_iout_min)
272			return -ENXIO;
273		ret = pmbus_read_word_data(client, 0, 0xff,
274					   ADM1293_IOUT_MIN);
275		break;
276	case PMBUS_VIRT_READ_IOUT_MAX:
277		ret = pmbus_read_word_data(client, 0, 0xff,
278					   ADM1275_PEAK_IOUT);
279		break;
280	case PMBUS_VIRT_READ_VOUT_MAX:
281		ret = pmbus_read_word_data(client, 0, 0xff,
282					   ADM1275_PEAK_VOUT);
283		break;
284	case PMBUS_VIRT_READ_VIN_MAX:
285		ret = pmbus_read_word_data(client, 0, 0xff,
286					   ADM1275_PEAK_VIN);
287		break;
288	case PMBUS_VIRT_READ_PIN_MIN:
289		if (!data->have_pin_min)
290			return -ENXIO;
291		ret = pmbus_read_word_data(client, 0, 0xff,
292					   ADM1293_PIN_MIN);
293		break;
294	case PMBUS_VIRT_READ_PIN_MAX:
295		if (!data->have_pin_max)
296			return -ENXIO;
297		ret = pmbus_read_word_data(client, 0, 0xff,
298					   ADM1276_PEAK_PIN);
299		break;
300	case PMBUS_VIRT_READ_TEMP_MAX:
301		if (!data->have_temp_max)
302			return -ENXIO;
303		ret = pmbus_read_word_data(client, 0, 0xff,
304					   ADM1278_PEAK_TEMP);
305		break;
306	case PMBUS_VIRT_RESET_IOUT_HISTORY:
307	case PMBUS_VIRT_RESET_VOUT_HISTORY:
308	case PMBUS_VIRT_RESET_VIN_HISTORY:
309		break;
310	case PMBUS_VIRT_RESET_PIN_HISTORY:
311		if (!data->have_pin_max)
312			return -ENXIO;
313		break;
314	case PMBUS_VIRT_RESET_TEMP_HISTORY:
315		if (!data->have_temp_max)
316			return -ENXIO;
317		break;
318	case PMBUS_VIRT_POWER_SAMPLES:
319		if (!data->have_power_sampling)
320			return -ENXIO;
321		ret = adm1275_read_pmon_config(data, client, true);
322		if (ret < 0)
323			break;
324		ret = BIT(ret);
325		break;
326	case PMBUS_VIRT_IN_SAMPLES:
327	case PMBUS_VIRT_CURR_SAMPLES:
328		ret = adm1275_read_pmon_config(data, client, false);
329		if (ret < 0)
330			break;
331		ret = BIT(ret);
332		break;
333	default:
334		ret = -ENODATA;
335		break;
336	}
337	return ret;
338}
339
340static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
341				   u16 word)
342{
343	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
344	const struct adm1275_data *data = to_adm1275_data(info);
345	int ret;
346
347	if (page > 0)
348		return -ENXIO;
349
350	switch (reg) {
351	case PMBUS_IOUT_UC_FAULT_LIMIT:
352	case PMBUS_IOUT_OC_FAULT_LIMIT:
353		ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
354					    word);
355		break;
356	case PMBUS_VIRT_RESET_IOUT_HISTORY:
357		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
358		if (!ret && data->have_iout_min)
359			ret = pmbus_write_word_data(client, 0,
360						    ADM1293_IOUT_MIN, 0);
361		break;
362	case PMBUS_VIRT_RESET_VOUT_HISTORY:
363		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
364		break;
365	case PMBUS_VIRT_RESET_VIN_HISTORY:
366		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
367		break;
368	case PMBUS_VIRT_RESET_PIN_HISTORY:
369		ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
370		if (!ret && data->have_pin_min)
371			ret = pmbus_write_word_data(client, 0,
372						    ADM1293_PIN_MIN, 0);
373		break;
374	case PMBUS_VIRT_RESET_TEMP_HISTORY:
375		ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
376		break;
377	case PMBUS_VIRT_POWER_SAMPLES:
378		if (!data->have_power_sampling)
379			return -ENXIO;
380		word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
381		ret = adm1275_write_pmon_config(data, client, true,
382						ilog2(word));
383		break;
384	case PMBUS_VIRT_IN_SAMPLES:
385	case PMBUS_VIRT_CURR_SAMPLES:
386		word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
387		ret = adm1275_write_pmon_config(data, client, false,
388						ilog2(word));
389		break;
390	default:
391		ret = -ENODATA;
392		break;
393	}
394	return ret;
395}
396
397static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
398{
399	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
400	const struct adm1275_data *data = to_adm1275_data(info);
401	int mfr_status, ret;
402
403	if (page > 0)
404		return -ENXIO;
405
406	switch (reg) {
407	case PMBUS_STATUS_IOUT:
408		ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
409		if (ret < 0)
410			break;
411		if (!data->have_oc_fault && !data->have_uc_fault)
412			break;
413		mfr_status = pmbus_read_byte_data(client, page,
414						  PMBUS_STATUS_MFR_SPECIFIC);
415		if (mfr_status < 0)
416			return mfr_status;
417		if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
418			ret |= data->have_oc_fault ?
419			  PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
420		}
421		break;
422	case PMBUS_STATUS_VOUT:
423		if (data->have_vout)
424			return -ENODATA;
425		ret = 0;
426		if (data->have_vaux_status) {
427			mfr_status = pmbus_read_byte_data(client, 0,
428							  ADM1075_VAUX_STATUS);
429			if (mfr_status < 0)
430				return mfr_status;
431			if (mfr_status & ADM1075_VAUX_OV_WARN)
432				ret |= PB_VOLTAGE_OV_WARNING;
433			if (mfr_status & ADM1075_VAUX_UV_WARN)
434				ret |= PB_VOLTAGE_UV_WARNING;
435		} else if (data->have_mfr_vaux_status) {
436			mfr_status = pmbus_read_byte_data(client, page,
437						PMBUS_STATUS_MFR_SPECIFIC);
438			if (mfr_status < 0)
439				return mfr_status;
440			if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN)
441				ret |= PB_VOLTAGE_OV_WARNING;
442			if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN)
443				ret |= PB_VOLTAGE_UV_WARNING;
444		}
445		break;
446	default:
447		ret = -ENODATA;
448		break;
449	}
450	return ret;
451}
452
453static const struct i2c_device_id adm1275_id[] = {
454	{ "adm1075", adm1075 },
455	{ "adm1272", adm1272 },
456	{ "adm1275", adm1275 },
457	{ "adm1276", adm1276 },
458	{ "adm1278", adm1278 },
459	{ "adm1293", adm1293 },
460	{ "adm1294", adm1294 },
461	{ }
462};
463MODULE_DEVICE_TABLE(i2c, adm1275_id);
464
465static int adm1275_probe(struct i2c_client *client)
466{
467	s32 (*config_read_fn)(const struct i2c_client *client, u8 reg);
468	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
469	int config, device_config;
470	int ret;
471	struct pmbus_driver_info *info;
472	struct adm1275_data *data;
473	const struct i2c_device_id *mid;
474	const struct coefficients *coefficients;
475	int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
476	int tindex = -1;
477	u32 shunt;
478
479	if (!i2c_check_functionality(client->adapter,
480				     I2C_FUNC_SMBUS_READ_BYTE_DATA
481				     | I2C_FUNC_SMBUS_BLOCK_DATA))
482		return -ENODEV;
483
484	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
485	if (ret < 0) {
486		dev_err(&client->dev, "Failed to read Manufacturer ID\n");
487		return ret;
488	}
489	if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
490		dev_err(&client->dev, "Unsupported Manufacturer ID\n");
491		return -ENODEV;
492	}
493
494	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
495	if (ret < 0) {
496		dev_err(&client->dev, "Failed to read Manufacturer Model\n");
497		return ret;
498	}
499	for (mid = adm1275_id; mid->name[0]; mid++) {
500		if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
501			break;
502	}
503	if (!mid->name[0]) {
504		dev_err(&client->dev, "Unsupported device\n");
505		return -ENODEV;
506	}
507
508	if (strcmp(client->name, mid->name) != 0)
509		dev_notice(&client->dev,
510			   "Device mismatch: Configured %s, detected %s\n",
511			   client->name, mid->name);
512
513	if (mid->driver_data == adm1272 || mid->driver_data == adm1278 ||
514	    mid->driver_data == adm1293 || mid->driver_data == adm1294)
515		config_read_fn = i2c_smbus_read_word_data;
516	else
517		config_read_fn = i2c_smbus_read_byte_data;
518	config = config_read_fn(client, ADM1275_PMON_CONFIG);
519	if (config < 0)
520		return config;
521
522	device_config = config_read_fn(client, ADM1275_DEVICE_CONFIG);
523	if (device_config < 0)
524		return device_config;
525
526	data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
527			    GFP_KERNEL);
528	if (!data)
529		return -ENOMEM;
530
531	if (of_property_read_u32(client->dev.of_node,
532				 "shunt-resistor-micro-ohms", &shunt))
533		shunt = 1000; /* 1 mOhm if not set via DT */
534
535	if (shunt == 0)
536		return -EINVAL;
537
538	data->id = mid->driver_data;
539
540	info = &data->info;
541
542	info->pages = 1;
543	info->format[PSC_VOLTAGE_IN] = direct;
544	info->format[PSC_VOLTAGE_OUT] = direct;
545	info->format[PSC_CURRENT_OUT] = direct;
546	info->format[PSC_POWER] = direct;
547	info->format[PSC_TEMPERATURE] = direct;
548	info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
549			PMBUS_HAVE_SAMPLES;
550
551	info->read_word_data = adm1275_read_word_data;
552	info->read_byte_data = adm1275_read_byte_data;
553	info->write_word_data = adm1275_write_word_data;
554
555	switch (data->id) {
556	case adm1075:
557		if (device_config & ADM1275_IOUT_WARN2_SELECT)
558			data->have_oc_fault = true;
559		else
560			data->have_uc_fault = true;
561		data->have_pin_max = true;
562		data->have_vaux_status = true;
563
564		coefficients = adm1075_coefficients;
565		vindex = 0;
566		switch (config & ADM1075_IRANGE_MASK) {
567		case ADM1075_IRANGE_25:
568			cindex = 1;
569			pindex = 3;
570			break;
571		case ADM1075_IRANGE_50:
572			cindex = 2;
573			pindex = 4;
574			break;
575		default:
576			dev_err(&client->dev, "Invalid input current range");
577			break;
578		}
579
580		info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
581		  | PMBUS_HAVE_STATUS_INPUT;
582		if (config & ADM1275_VIN_VOUT_SELECT)
583			info->func[0] |=
584			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
585		break;
586	case adm1272:
587		data->have_vout = true;
588		data->have_pin_max = true;
589		data->have_temp_max = true;
590		data->have_power_sampling = true;
591
592		coefficients = adm1272_coefficients;
593		vindex = (config & ADM1275_VRANGE) ? 1 : 0;
594		cindex = (config & ADM1272_IRANGE) ? 3 : 2;
595		/* pindex depends on the combination of the above */
596		switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
597		case 0:
598		default:
599			pindex = 4;
600			break;
601		case ADM1275_VRANGE:
602			pindex = 5;
603			break;
604		case ADM1272_IRANGE:
605			pindex = 6;
606			break;
607		case ADM1275_VRANGE | ADM1272_IRANGE:
608			pindex = 7;
609			break;
610		}
611		tindex = 8;
612
613		info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
614			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
615			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
616
617		/* Enable VOUT & TEMP1 if not enabled (disabled by default) */
618		if ((config & (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) !=
619		    (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) {
620			config |= ADM1278_VOUT_EN | ADM1278_TEMP1_EN;
621			ret = i2c_smbus_write_byte_data(client,
622							ADM1275_PMON_CONFIG,
623							config);
624			if (ret < 0) {
625				dev_err(&client->dev,
626					"Failed to enable VOUT monitoring\n");
627				return -ENODEV;
628			}
629		}
630		if (config & ADM1278_VIN_EN)
631			info->func[0] |= PMBUS_HAVE_VIN;
632		break;
633	case adm1275:
634		if (device_config & ADM1275_IOUT_WARN2_SELECT)
635			data->have_oc_fault = true;
636		else
637			data->have_uc_fault = true;
638		data->have_vout = true;
639
640		coefficients = adm1275_coefficients;
641		vindex = (config & ADM1275_VRANGE) ? 0 : 1;
642		cindex = 2;
643
644		if (config & ADM1275_VIN_VOUT_SELECT)
645			info->func[0] |=
646			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
647		else
648			info->func[0] |=
649			  PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
650		break;
651	case adm1276:
652		if (device_config & ADM1275_IOUT_WARN2_SELECT)
653			data->have_oc_fault = true;
654		else
655			data->have_uc_fault = true;
656		data->have_vout = true;
657		data->have_pin_max = true;
658
659		coefficients = adm1276_coefficients;
660		vindex = (config & ADM1275_VRANGE) ? 0 : 1;
661		cindex = 2;
662		pindex = (config & ADM1275_VRANGE) ? 3 : 4;
663
664		info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
665		  | PMBUS_HAVE_STATUS_INPUT;
666		if (config & ADM1275_VIN_VOUT_SELECT)
667			info->func[0] |=
668			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
669		break;
670	case adm1278:
671		data->have_vout = true;
672		data->have_pin_max = true;
673		data->have_temp_max = true;
674		data->have_power_sampling = true;
675
676		coefficients = adm1278_coefficients;
677		vindex = 0;
678		cindex = 1;
679		pindex = 2;
680		tindex = 3;
681
682		info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
683			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
684			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
685
686		/* Enable VOUT & TEMP1 if not enabled (disabled by default) */
687		if ((config & (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) !=
688		    (ADM1278_VOUT_EN | ADM1278_TEMP1_EN)) {
689			config |= ADM1278_VOUT_EN | ADM1278_TEMP1_EN;
690			ret = i2c_smbus_write_byte_data(client,
691							ADM1275_PMON_CONFIG,
692							config);
693			if (ret < 0) {
694				dev_err(&client->dev,
695					"Failed to enable VOUT monitoring\n");
696				return -ENODEV;
697			}
698		}
699
700		if (config & ADM1278_VIN_EN)
701			info->func[0] |= PMBUS_HAVE_VIN;
702		break;
703	case adm1293:
704	case adm1294:
705		data->have_iout_min = true;
706		data->have_pin_min = true;
707		data->have_pin_max = true;
708		data->have_mfr_vaux_status = true;
709		data->have_power_sampling = true;
710
711		coefficients = adm1293_coefficients;
712
713		voindex = 0;
714		switch (config & ADM1293_VIN_SEL_MASK) {
715		case ADM1293_VIN_SEL_012:	/* 1.2V */
716			vindex = 0;
717			break;
718		case ADM1293_VIN_SEL_074:	/* 7.4V */
719			vindex = 1;
720			break;
721		case ADM1293_VIN_SEL_210:	/* 21V */
722			vindex = 2;
723			break;
724		default:			/* disabled */
725			break;
726		}
727
728		switch (config & ADM1293_IRANGE_MASK) {
729		case ADM1293_IRANGE_25:
730			cindex = 3;
731			break;
732		case ADM1293_IRANGE_50:
733			cindex = 4;
734			break;
735		case ADM1293_IRANGE_100:
736			cindex = 5;
737			break;
738		case ADM1293_IRANGE_200:
739			cindex = 6;
740			break;
741		}
742
743		if (vindex >= 0)
744			pindex = 7 + vindex * 4 + (cindex - 3);
745
746		if (config & ADM1293_VAUX_EN)
747			info->func[0] |=
748				PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
749
750		info->func[0] |= PMBUS_HAVE_PIN |
751			PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
 
 
752
753		break;
754	default:
755		dev_err(&client->dev, "Unsupported device\n");
756		return -ENODEV;
757	}
758
759	if (voindex < 0)
760		voindex = vindex;
761	if (vindex >= 0) {
762		info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
763		info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
764		info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
765	}
766	if (voindex >= 0) {
767		info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
768		info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
769		info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
770	}
771	if (cindex >= 0) {
772		/* Scale current with sense resistor value */
773		info->m[PSC_CURRENT_OUT] =
774			coefficients[cindex].m * shunt / 1000;
775		info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
776		info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
777	}
778	if (pindex >= 0) {
779		info->m[PSC_POWER] =
780			coefficients[pindex].m * shunt / 1000;
781		info->b[PSC_POWER] = coefficients[pindex].b;
782		info->R[PSC_POWER] = coefficients[pindex].R;
783	}
784	if (tindex >= 0) {
785		info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
786		info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
787		info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
788	}
789
790	return pmbus_do_probe(client, info);
 
 
791}
792
 
 
 
 
 
 
793static struct i2c_driver adm1275_driver = {
794	.driver = {
795		   .name = "adm1275",
796		   },
797	.probe_new = adm1275_probe,
 
798	.id_table = adm1275_id,
799};
800
801module_i2c_driver(adm1275_driver);
 
 
 
 
 
 
 
 
802
803MODULE_AUTHOR("Guenter Roeck");
804MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
805MODULE_LICENSE("GPL");
806MODULE_IMPORT_NS(PMBUS);