pmbus.c 7.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
G
Guenter Roeck 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Hardware monitoring driver for PMBus devices
 *
 * Copyright (c) 2010, 2011 Ericsson AB.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/i2c.h>
15
#include <linux/pmbus.h>
G
Guenter Roeck 已提交
16 17
#include "pmbus.h"

18 19 20 21 22
struct pmbus_device_info {
	int pages;
	u32 flags;
};

23 24
static const struct i2c_device_id pmbus_id[];

G
Guenter Roeck 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 * Find sensor groups and status registers on each page.
 */
static void pmbus_find_sensor_groups(struct i2c_client *client,
				     struct pmbus_driver_info *info)
{
	int page;

	/* Sensors detected on page 0 only */
	if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
		info->func[0] |= PMBUS_HAVE_VIN;
	if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
		info->func[0] |= PMBUS_HAVE_VCAP;
	if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
		info->func[0] |= PMBUS_HAVE_IIN;
	if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
		info->func[0] |= PMBUS_HAVE_PIN;
	if (info->func[0]
	    && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
		info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
45 46
	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
G
Guenter Roeck 已提交
47 48 49 50
		info->func[0] |= PMBUS_HAVE_FAN12;
		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
			info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
	}
51 52
	if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
	    pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
G
Guenter Roeck 已提交
53 54 55 56
		info->func[0] |= PMBUS_HAVE_FAN34;
		if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
			info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
	}
57
	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
G
Guenter Roeck 已提交
58
		info->func[0] |= PMBUS_HAVE_TEMP;
59 60 61 62
	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
		info->func[0] |= PMBUS_HAVE_TEMP2;
	if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
		info->func[0] |= PMBUS_HAVE_TEMP3;
63 64 65 66 67
	if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
			     | PMBUS_HAVE_TEMP3)
	    && pmbus_check_byte_register(client, 0,
					 PMBUS_STATUS_TEMPERATURE))
			info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
G
Guenter Roeck 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

	/* Sensors detected on all pages */
	for (page = 0; page < info->pages; page++) {
		if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
			info->func[page] |= PMBUS_HAVE_VOUT;
			if (pmbus_check_byte_register(client, page,
						      PMBUS_STATUS_VOUT))
				info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
		}
		if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
			info->func[page] |= PMBUS_HAVE_IOUT;
			if (pmbus_check_byte_register(client, 0,
						      PMBUS_STATUS_IOUT))
				info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
		}
		if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
			info->func[page] |= PMBUS_HAVE_POUT;
	}
}

/*
 * Identify chip parameters.
 */
static int pmbus_identify(struct i2c_client *client,
			  struct pmbus_driver_info *info)
{
94 95
	int ret = 0;

G
Guenter Roeck 已提交
96 97 98 99 100 101 102 103 104 105 106
	if (!info->pages) {
		/*
		 * Check if the PAGE command is supported. If it is,
		 * keep setting the page number until it fails or until the
		 * maximum number of pages has been reached. Assume that
		 * this is the number of pages supported by the chip.
		 */
		if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
			int page;

			for (page = 1; page < PMBUS_PAGES; page++) {
107
				if (pmbus_set_page(client, page, 0xff) < 0)
G
Guenter Roeck 已提交
108 109
					break;
			}
110
			pmbus_set_page(client, 0, 0xff);
G
Guenter Roeck 已提交
111 112 113 114
			info->pages = page;
		} else {
			info->pages = 1;
		}
115 116

		pmbus_clear_faults(client);
G
Guenter Roeck 已提交
117 118
	}

119
	if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
120
		int vout_mode, i;
121 122 123 124 125 126 127 128

		vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
		if (vout_mode >= 0 && vout_mode != 0xff) {
			switch (vout_mode >> 5) {
			case 0:
				break;
			case 1:
				info->format[PSC_VOLTAGE_OUT] = vid;
129 130
				for (i = 0; i < info->pages; i++)
					info->vrm_version[i] = vr11;
131 132 133 134 135 136 137 138 139 140 141
				break;
			case 2:
				info->format[PSC_VOLTAGE_OUT] = direct;
				break;
			default:
				ret = -ENODEV;
				goto abort;
			}
		}
	}

G
Guenter Roeck 已提交
142 143 144 145 146 147 148 149
	/*
	 * We should check if the COEFFICIENTS register is supported.
	 * If it is, and the chip is configured for direct mode, we can read
	 * the coefficients from the chip, one set per group of sensor
	 * registers.
	 *
	 * To do this, we will need access to a chip which actually supports the
	 * COEFFICIENTS command, since the command is too complex to implement
150 151
	 * without testing it. Until then, abort if a chip configured for direct
	 * mode was detected.
G
Guenter Roeck 已提交
152
	 */
153 154 155 156
	if (info->format[PSC_VOLTAGE_OUT] == direct) {
		ret = -ENODEV;
		goto abort;
	}
G
Guenter Roeck 已提交
157 158 159

	/* Try to find sensor groups  */
	pmbus_find_sensor_groups(client, info);
160 161
abort:
	return ret;
G
Guenter Roeck 已提交
162 163
}

164
static int pmbus_probe(struct i2c_client *client)
G
Guenter Roeck 已提交
165 166
{
	struct pmbus_driver_info *info;
167 168
	struct pmbus_platform_data *pdata = NULL;
	struct device *dev = &client->dev;
169
	struct pmbus_device_info *device_info;
G
Guenter Roeck 已提交
170

171
	info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
G
Guenter Roeck 已提交
172 173 174
	if (!info)
		return -ENOMEM;

175
	device_info = (struct pmbus_device_info *)i2c_match_id(pmbus_id, client)->driver_data;
176
	if (device_info->flags & PMBUS_SKIP_STATUS_CHECK) {
177 178 179 180 181 182 183 184
		pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
				     GFP_KERNEL);
		if (!pdata)
			return -ENOMEM;

		pdata->flags = PMBUS_SKIP_STATUS_CHECK;
	}

185
	info->pages = device_info->pages;
G
Guenter Roeck 已提交
186
	info->identify = pmbus_identify;
187
	dev->platform_data = pdata;
G
Guenter Roeck 已提交
188

189
	return pmbus_do_probe(client, info);
G
Guenter Roeck 已提交
190 191
}

192 193 194 195 196 197 198 199 200 201 202 203 204
static const struct pmbus_device_info pmbus_info_one = {
	.pages = 1,
	.flags = 0
};
static const struct pmbus_device_info pmbus_info_zero = {
	.pages = 0,
	.flags = 0
};
static const struct pmbus_device_info pmbus_info_one_skip = {
	.pages = 1,
	.flags = PMBUS_SKIP_STATUS_CHECK
};

G
Guenter Roeck 已提交
205 206 207 208
/*
 * Use driver_data to set the number of pages supported by the chip.
 */
static const struct i2c_device_id pmbus_id[] = {
209 210 211 212
	{"adp4000", (kernel_ulong_t)&pmbus_info_one},
	{"bmr453", (kernel_ulong_t)&pmbus_info_one},
	{"bmr454", (kernel_ulong_t)&pmbus_info_one},
	{"dps460", (kernel_ulong_t)&pmbus_info_one_skip},
213
	{"dps650ab", (kernel_ulong_t)&pmbus_info_one_skip},
214
	{"dps800", (kernel_ulong_t)&pmbus_info_one_skip},
215
	{"max20796", (kernel_ulong_t)&pmbus_info_one},
216 217 218 219 220 221 222 223 224 225 226 227 228 229
	{"mdt040", (kernel_ulong_t)&pmbus_info_one},
	{"ncp4200", (kernel_ulong_t)&pmbus_info_one},
	{"ncp4208", (kernel_ulong_t)&pmbus_info_one},
	{"pdt003", (kernel_ulong_t)&pmbus_info_one},
	{"pdt006", (kernel_ulong_t)&pmbus_info_one},
	{"pdt012", (kernel_ulong_t)&pmbus_info_one},
	{"pmbus", (kernel_ulong_t)&pmbus_info_zero},
	{"sgd009", (kernel_ulong_t)&pmbus_info_one_skip},
	{"tps40400", (kernel_ulong_t)&pmbus_info_one},
	{"tps544b20", (kernel_ulong_t)&pmbus_info_one},
	{"tps544b25", (kernel_ulong_t)&pmbus_info_one},
	{"tps544c20", (kernel_ulong_t)&pmbus_info_one},
	{"tps544c25", (kernel_ulong_t)&pmbus_info_one},
	{"udt020", (kernel_ulong_t)&pmbus_info_one},
G
Guenter Roeck 已提交
230 231 232 233 234 235 236 237 238 239
	{}
};

MODULE_DEVICE_TABLE(i2c, pmbus_id);

/* This is the driver that will be inserted */
static struct i2c_driver pmbus_driver = {
	.driver = {
		   .name = "pmbus",
		   },
240
	.probe_new = pmbus_probe,
G
Guenter Roeck 已提交
241 242 243
	.id_table = pmbus_id,
};

244
module_i2c_driver(pmbus_driver);
G
Guenter Roeck 已提交
245 246 247 248

MODULE_AUTHOR("Guenter Roeck");
MODULE_DESCRIPTION("Generic PMBus driver");
MODULE_LICENSE("GPL");